CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Code not running correctly

 



newera
Novice

May 8, 2007, 10:51 AM

Post #1 of 5 (444 views)
Code not running correctly Can't Post

Why does this code stop running after processing the first record? There are 3 records in the database that meet the date criteria, but it only does the commission subroutine first one. What am I missing? Any help appreciated....


Code
$SQL = "SELECT * FROM transactions WHERE commission_date = DATE_SUB(CURDATE(),INTERVAL 1 DAY)";  
&Do_SQL;
if($sth->rows != 0)
{
$total_rows = $sth->rows;

for ($counter = 0; $counter < $total_rows; $counter++)
{
my($ref) = $sth->fetchrow_hashref;
%DISPLAY = %$ref;

$payment_status = $DISPLAY{'payment_status'};
$cash = $DISPLAY{'payment_net'};
$affiliate = $DISPLAY{'pp_sponsor'};

&commission;
}
}
$sth->finish;



KevinR
Veteran


May 8, 2007, 1:24 PM

Post #2 of 5 (443 views)
Re: [newera] Code not running correctly [In reply to] Can't Post

could be here:

$total_rows = $sth->rows;

or:

&commission;


whats the value of $total_rows?

does &commission do something that stops the loop?
-------------------------------------------------


newera
Novice

May 8, 2007, 1:44 PM

Post #3 of 5 (441 views)
Re: [KevinR] Code not running correctly [In reply to] Can't Post

Total rows for testing is 3.
The thing is, the script runs fine from the browser window, but I want to put it as a cron job once a day instead of at every sale. So I need to pull from the transactions table, and then process each record with the sale script.
Here's the rest of the script if you can find the problem.


Code
sub commission { 
&find_cookie;
$SQL = "SELECT * FROM ua_products WHERE Product_name LIKE ' '";
&Do_SQL;
if ($scheme) { &process_scheme; }
if ($sth->rows) {
$scheme = " ,";
&process_scheme;
}
&award_commissions;
}

sub process_scheme { #first
@multiple = split (/\,/, $scheme);
@scheme_qty = split (/\,/, $scheme_quantity);
$temp_getcookie = $getcookie;
@temps_flatfee_tiers = @flatfee_tiers;
@temps_percentage_tiers = @percentage_tiers;
$temp_cashflow = $cashflow;
$temp_recurring = $recurring;
$SQL = "SELECT * FROM main_members WHERE ID LIKE '$getcookie'"; &Do_SQL;
while ($row = $sth->fetchrow_hashref) {
$RANK = $row->{'RANK'};
}

foreach $item (@multiple) {
&calc_scheme;
}

}

sub calc_scheme { #second
@percentage_tiers = @temps_percentage_tiers;
@flatfee_tiers = @temps_flatfee_tiers;
$getcookie = $temp_getcookie;
$cashflow = $temp_cashflow;
$recurring = $temp_recurring;
$foundmatch = 0;
if ($are_pending eq 1) {$the_status = "PENDING";} else {$the_status = "NOTPAID";}
$SQL = "SELECT * FROM ua_products WHERE Product_name LIKE '$item' && category LIKE '$getcookie'"; &Do_SQL;
while ($row = $sth->fetchrow_hashref) { $foundmatch++; }
if ($foundmatch) { $query = "Product_name LIKE '$item' && category LIKE '$getcookie'"; }
else { $query = "Product_name LIKE '$item'"; }

$SQL = "SELECT * FROM ua_products WHERE $query ORDER BY bonus"; &Do_SQL;
while ($row = $sth->fetchrow_hashref) {

if ($bfound) { next; }

($bonus_from,$bonus_to,$bonus_days) = split (/\|/, $row->{'bonus'});
$b_time = $bonus_days * 60 * 60 * 24; $b_timelimit = $time - $b_time;
$SQL2 = "SELECT * FROM commissions WHERE Id LIKE '$getcookie' && DateKey > $b_timelimit && Commission > 0"; &Do_SQL2;
while ($rowm = $mth->fetchrow_hashref) { $b_comcount++; }
if ($b_comcount >= $bonus_from && $b_comcount <= $bonus_to) { $bfound++; $b_comcount = 0; if ($item eq " ") { $scheme = "DEFAULT"; } } else { $b_comcount = 0; next;}


if (!$row->{'category'} || $row->{'category'} eq $category) {
if ($row->{'ComType'} eq "percentage") { @percentage_tiers = split (/\,/, $row->{'ComSettings'}); }
if ($row->{'ComType'} eq "flatfee") { @flatfee_tiers = split (/\,/, $row->{'ComSettings'}); }
$what_type_of_commission = $row->{'ComType'};
if ($row->{'Recurring'}) { $recurring = $row->{'Recurring'}; } else { $recurring = "OFF"; }
if ($row->{'Trial_on'}) {
$trial_on = 1;
if ($row->{'ComType'} eq "percentage") { @percentage_tiers = split (/\,/, $row->{'Trial_value'}); }
if ($row->{'ComType'} eq "flatfee") { @flatfee_tiers = split (/\,/, $row->{'Trial_value'}); }
$the_status = "TRIAL";
}
} elsif ($row->{'alt'}) {
$temp = $row->{'alt'};
$SQL2 = "SELECT * FROM ua_products WHERE Product_name LIKE '$temp'"; &Do_SQL2;
while ($rows = $mth->fetchrow_hashref) {
if ($rows->{'ComType'} eq "percentage") { @percentage_tiers = split (/\,/, $rows->{'ComSettings'}); }
if ($rows->{'ComType'} eq "flatfee") { @flatfee_tiers = split (/\,/, $rows->{'ComSettings'}); }
$what_type_of_commission = $rows->{'ComType'};
if ($rows->{'Recurring'}) { $recurring = $rows->{'Recurring'}; } else { $recurring = "OFF"; }
if ($rows->{'Trial_on'}) {
$trial_on = 1;
if ($rows->{'ComType'} eq "percentage") { @percentage_tiers = split (/\,/, $rows->{'Trial_value'}); }
if ($rows->{'ComType'} eq "flatfee") { @flatfee_tiers = split (/\,/, $rows->{'Trial_value'}); }
$the_status = "TRIAL";
}
}
}
}

if (!$qty) { $qty = 0; } else { $qty++; }
$bfound = 0;
}

################################## AWARD COMMISSIONS!
sub award_commissions { # third
$e = 0; if ($cashflow) {} else { $cashflow = 0; }
@temp_percentage_tiers = @percentage_tiers;
@temp_flatfee_tiers = @flatfee_tiers;
&prepare_tiers;
if ($getcookie) {
while ($goahead ne 1) {

$SQL="SELECT * FROM main_members WHERE ID LIKE '$getcookie'";
&Do_SQL;
while ($pointer = $sth->fetchrow_hashref){
foreach $field (@fields) {
${$field} = $pointer->{$field};
} $found_them = "YES"; }
&date;
if ($what_type_of_commission eq "percentage") {
$value = $cashflow * $temp_percentage_tiers[$e];
} else {
$value = $temp_flatfee_tiers[$e];
}
$e++;
&unique_time;
$level = "Level $e";
if ($level eq 1) { $refaff = $ID; }
if ($found_them eq "YES") {
if ($value > 0 || $value < 0) {
$total_value = $total_value + $value;
$to_admin .= "$FIRST_NAME $LAST_NAME ($ID) at $Email received a $level commission of $currency$value. \n";
if ($scheme_qty[$qty] > 1) { $value = $value * $scheme_qty[$qty]; $goods = $goods . "\(x" . $scheme_qty[$qty] . "\)"; }
$SQL = "INSERT INTO commissions (DateKey,Year,Month,Day,Level,Status,Type,Id,Commission,Referer,Ip,Extras,SeriesNumber,SaleAmount,AdminInfo,ReferringAffiliate,Scheme)
VALUES ('$time','$year','$month','$day','$level','$the_status','$recurring','$getcookie','$value','$referer','$ip','$goods','$session','$cashflow','$admin','$refaff','$scheme')";
&Do_SQL;
if ($camp_id) { $SQL = "UPDATE ua_campaigns SET Commissions = (Commissions + 1) WHERE Extra1 = '$camp_id' && Id = '$getcookie'"; &Do_SQL; }
if ($email_affiliates_after_sale eq 1 && $the_status ne "PENDING") { &email_affiliate; }
}}
$getcookie = "$CO_SPONSOR_ID";
if ($CO_SPONSOR_ID eq "NO_SPONSOR" || $found_them ne "YES") { $goahead = 1; }
@temp_percentage_tiers = @percentage_tiers; @temp_flatfee_tiers = @flatfee_tiers;
&prepare_tiers; $value = 0; $found_them = "NO";
}
}
}


sub prepare_tiers { # fourth
$SQL="SELECT * FROM main_members WHERE ID like '$getcookie'";
&Do_SQL;
while ($pointer = $sth->fetchrow_hashref){
foreach $field (@fields) {
${$field} = $pointer->{$field};
}
}
$sth->finish;
(@user_tiers) = split (/\,/, $Tier); # Custom tiers
foreach $item (@user_tiers) {
$i++;
if ($item > 0) { $user_final_level = $i;}
}
$i = 0;
if ($user_final_level && !$scheme) { @temp_percentage_tiers = @user_tiers; @temp_flatfee_tiers = @user_tiers;}
$user_final_level = 0;
if ($fast_start_days && $getcookie) { # Fast start tiers
$SQL = "SELECT * FROM main_members WHERE ID LIKE '$getcookie'"; &Do_SQL;
while ($row = $sth->fetchrow_hashref) {
$zmonth = $row->{'Month'};$zday = $row->{'Day'};$zyear = $row->{'Year'};
}
$tyear = $zyear - 1900;
$signuptime = timelocal(0,0,0,$zday,$zmonth,$tyear);
$differencetime = $time - $signuptime;
$difference = $differencetime / (60*60*24);
if ($difference <= $fast_start_days) {
@temp_percentage_tiers = @fast_start_tiers;
@temp_flatfee_tiers = @fast_start_tiers;
}
}

$SQL = "SELECT * FROM main_members WHERE ID LIKE '$getcookie'"; &Do_SQL;
while ($row = $sth->fetchrow_hashref){
$aff_category = $row->{'RANK'};
}
if ($aff_category) {
$temp_getcookie = $getcookie;
@temps_flatfee_tiers = @flatfee_tiers;
@temps_percentage_tiers = @percentage_tiers;
$temp_cashflow = $cashflow;
$temp_recurring = $recurring;
$SQL = "SELECT * FROM ua_products WHERE Product_name LIKE '$aff_category'"; &Do_SQL;
if ($sth->rows) {
$item = $aff_category;
&calc_scheme;
@temp_flatfee_tiers = @flatfee_tiers;
@temp_percentage_tiers = @percentage_tiers;
}
}

}

sub find_cookie {
if ($are_pending eq 1) {$the_status = "PENDING";} else {$the_status = "NOTPAID";}
if ($recur && $send_to_sale eq 1) { $recurring = "$recur"; }
if ($affiliate && $send_to_sale eq 1) {
$getcookie = "$affiliate"; # OK, this is what we need
} else {
$getcookie = $q->cookie($cookiename);
$camp_id = $q->cookie(-name=>'ua_campaign');
}
}

sub date {
$time = time();
($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time);
$month = $mon + 1;
$year = $year + 1900;
}

sub unique_time {
$found = "";
while (!$found) {
$SQL="SELECT * FROM commissions WHERE DateKey = '$time'";
&Do_SQL;
if (!$sth->fetchrow_hashref){
$found = "YES";
} else { $time++; }
}
}
1;



KevinR
Veteran


May 8, 2007, 1:57 PM

Post #4 of 5 (440 views)
Re: [newera] Code not running correctly [In reply to] Can't Post

sorry, thats way too much code for me to look through. Not using "strict" is going to make debugging that code near impossible if the problem is not obvious.
-------------------------------------------------


newera
Novice

May 9, 2007, 5:28 AM

Post #5 of 5 (433 views)
Re: [KevinR] Code not running correctly [In reply to] Can't Post

I think I found the problem. This routine is designed to do one record and then quit. In the award_commissions sub, near the end there is a goahead that stops the process if true. It checks this at the start of the award_commissions sub.

So I changed goahead to 'return;', and the check for goahead was changed to if($getcookie != ""), and it goes to the next record in the list.


(This post was edited by newera on May 9, 2007, 9:02 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives