<?
include "./auth.inc.php";
#commission
#should probably be changed to a .inc.php.php.php of sorts
#or dynamic system to allow different rates..
#$resellercommissionrate=0.05;
#$agentcommissionrate=0.05;
function CC_checksum($ccard){
if(strlen($ccard) < 1)
return 0;
$ccard = ereg_replace("[^[:digit:]]+", "", $ccard);
$checksum = 0;
$factor = (strlen($ccard) % 2) ? 1 : 2;
for($i = 0; $i < strlen($ccard); $i++){
$digit = substr($ccard, $i, 1);
$checksum += ($digit * $factor > 9) ? $digit * $factor - 9 : $digit * $factor;
$factor = ($factor == 1) ? 2 : 1;
}
return !($checksum % 10);
}
if ($ccnum){
if (!CC_checksum($ccnum)){
print "The Credit Card Number appears to be invalid";
exit;
}
#check credit card date
#add the credit card to the database
$ccexpire = "$ExpiryDateYear-$ExpiryDateMonth-01";
$msql="UPDATE MasterAccounts SET CreditCardNumbor='$ccnum', CreditCardExp='$ccexpire' WHERE CustomerID=$CustomerID";
mysql_query($msql);
if (mysql_error())
print "Credit Card Update Error: ".mysql_error();
}
if (!$PaymentType){
print "Please specify a payment type!";
exit;
}
if (!$PaymentObject){
print "Nothing to do!";
exit;
}
if (!$NumHours)
$NumHours=$hours;
if ($PaymentObject=="Casual"){
if ($NumHours){
$Rate=intval($dollars*100/$NumHours)/100;
$amount=$dollars;
$purchase="$NumHours Casual Hrs @ $Rate/hour";
}else{
print "<H1>No Hours Entered!</H1>";
exit;
}
}elseif($PaymentObject=="Plan"){
#do plans
if (!$PlanType){
print "Error in Plan Type";
exit;
}
$psql="SELECT * FROM PlanTypes WHERE PlanID=$PlanType";
$pres=mysql_query($psql);
$prow=mysql_fetch_array($pres);
$purchase=$prow[PlanName];
$amount=$prow[Cost];
$kbytequota = $prow[WeeklyDownloadLimit];
$excesscharge = $prow[ExcessCharge];
$maxcon = $prow[MaxCon];
$mailboxsize = $prow[MailBoxSize];
$maxlogins = $prow[MaxLogins];
$startminutes = $prow[StartMinutes];
$endminutes = $prow[EndMinutes];
}elseif($PaymentObject=="Misc"){
#miscellaneous payment
if (!$Misc&& $Misc<>'0'){
print "You selected Miscellaneous Payment But didn't specify an amount!";
exit;
}
if (!$MiscDesc){
print "Please provide a description for any Miscellaneous Payment.";
exit;
}
$amount=$Misc;
$purchase=$MiscDesc;
}
$rsql="SELECT Reseller, RecurDealer, TimeRemaining, ExpireDate FROM
MasterAccounts WHERE CustomerID=$ID";
$result=mysql_query($rsql);
if (mysql_num_rows($result)){
$marow=mysql_fetch_array($result);
if ($marow[RecurDealer])
{
$ResellerOriginal=$marow[Reseller];
##$resellercommissionamt=$amount*$resellercommissionrate;
}else{
print "No Reseller!";
$resellercommissionamt=0;
}
}else{
die("MasterAccounts Query Error");
}
########################
# dealer commissions #
########################
if ($ResellerOriginal){
$dcsql = "SELECT ResellerCommission FROM Dealer WHERE DealerID=$ResellerOriginal";
$dcres = mysql_query($dcsql);
if (mysql_num_rows($dcres)){
$resellercommissionrate=mysql_result($dcres,0);
print "Reseller Commission assigned: $resellercommissionrate\n";
#don't bother calculating unless we're in here.
$resellercommissionamt=$amount * $resellercommissionrate;
print "R$ $resellercommissionamt. A$ $amount R% $resellercommissionrate.";
}else{
$perr.="Reseller Error with ResellerCommission for Reseller $ResellerOriginal::";
}
}
if($ResellerPay){
#agent took this payment
$agentcommissionamt=$amount*$agentcommissionrate;
}
if ($PaymentType=='Credit Card Pend'||$PaymentType=='Cheque in Mail'||$PaymentType=='Invoice'){
$credit=1;
}
$ptsql="SELECT CreditCheck
FROM PaymentTypes
WHERE PaymentTypeName='$PaymentType'";
$ptres=mysql_query($ptsql);
$ptrow=mysql_fetch_row($ptres);
if ($ptrow[0]>0)
$credit=1;
print "Original Reseller: $ResellerOriginal\n";
$identity="$REMOTE_ADDR-$PHP_AUTH_USER";
$isql="INSERT INTO Payments(
PaymentID,CustomerID,PaymentType,PaymentAmount,PaymentDate,Purchase,
RecallName,RecallPhone,ResellerOriginal,ResellerPayment,Identifier,
ResellerCommission,AgentCommission,Credit)
VALUES('',$ID,'$PaymentType','$amount',Now(),'$purchase',
'$RecallName','$RecallPhone','$ResellerOriginal','$ResellerPay','$identity',
'$resellercommissionamt', '$agentcommissionamt','$credit')";
print $isql;
mysql_query($isql);
if (mysql_errno())
print "<H1>Payment Error: ".mysql_error()."</H1>";
if ($PaymentObject=="Casual"){
$numminutes=$NumHours*60;
$usql="UPDATE MasterAccounts SET
TimeRemaining=TimeRemaining+$numminutes,LastModifyDate=now(),LastModifyUser='$PHP_AUTH_USER'
WHERE CustomerID=$ID";
mysql_query($usql);
if (mysql_errno())
print "<H1>Add Time Error: ".mysql_error()."</H1>";
print $usql;
}elseif($PaymentObject=="Plan"){
#Audit Customer? - check for existing expire etc.
if ($prow[AccountType]=='casual'||$prow[AccountType] == 'Blocks'){
$usql="UPDATE MasterAccounts
SET KbyteQuota='$kbytequota',
ExcessCharge='$excesscharge',";
if ($accounttype <> $prow[AccountType]){
#overwrite existing account stuff
#make a note first..
$note = "Account Changed. TimeRemaining was $marow[TimeRemaining], Expire was $marow[ExpireDate],
Account Type was $accounttype";
$susql = "UPDATE SubAccounts
SET AccountType = '$prow[AccountType]',
MaxConnectTime = '$maxcon',
MailBoxSize = '$mailboxsize',
LoginLimit = '$maxlogins',
StartMinutes = '$startminutes',
EndMinutes = '$endminutes',
WHERE CustomerID=$CustomerID
AND AccountType='$accounttype'";
$usql.=" TimeRemaining='$prow[TimeRemaining]', ExpireDate='0000-00-00' ";
}else{
$usql.=" TimeRemaining=TimeRemaining+'$prow[TimeRemaining]'";
$susql = "UPDATE SubAccounts
SET MaxConnectTime = '$maxcon',
MailBoxSize = '$mailboxsize',
LoginLimit = '$maxlogins'
WHERE CustomerID=$CustomerID
AND AccountType='$accounttype'";
}
$usql.=" WHERE CustomerID=$CustomerID";
}else{
#check for existing expire date
$esql = "SELECT ExpireDate FROM MasterAccounts WHERE CustomerID = $CustomerID";
$eres = mysql_query($esql);
$expire = mysql_result($eres,0);
if (!$expire || substr($expire,0,4)=="0000" || $accounttype <> $prow[AccountType]){
$exstr = 'now()';
#make a note
$note = "Account Changed. TimeRemaining was $marow[TimeRemaining], Expire was $marow[ExpireDate],
Account Type was $accounttype";
$susql = "UPDATE SubAccounts
SET AccountType = '$prow[AccountType]',
MaxConnectTime = '$maxcon',
MailBoxSize = '$mailboxsize',
LoginLimit = '$maxlogins'
WHERE CustomerID=$CustomerID
AND AccountType='$accounttype'";
}else{
$exstr = 'ExpireDate';
$susql = "UPDATE SubAccounts
SET MaxConnectTime = '$maxcon',
MailBoxSize = '$mailboxsize',
LoginLimit = '$maxlogins'
WHERE CustomerID=$CustomerID
AND AccountType='$accounttype'";
}
if ($prow[Months]){
$interval = "$prow[Months] month";
}elseif($prow[Days]){
$interval = "$prow[Days] day";
}else{
print "<H2>Error. No Expire Amount in Days or months!!</H2>";
}
$usql="UPDATE MasterAccounts
SET ExpireDate=DATE_ADD($exstr, Interval $interval),
KbyteQuota='$kbytequota',
ExcessCharge='$excesscharge'";
if ($prow[AccountType] <> $accounttype)
$usql.=", TimeRemaining='$prow[TimeRemaining]' ";
else
$usql.=", TimeRemaining=Timeremaining + '$prow[TimeRemaining]' ";
$usql.=" WHERE CustomerID=$CustomerID";
}
if ($note){
$nsql = "INSERT INTO Notes(NoteDate, NoteAuthor, CustomerID, NoteText)
VALUES(now(),'$PHP_AUTH_USER',$CustomerID,'$note')";
mysql_query($nsql);
if (mysql_error())
print "<H2>Notes: ".mysql_error()."</H2>";
}
mysql_query($usql);
if (mysql_errno())
print "<H1>Add Time Error: ".mysql_error()."</H1>";
print $usql;
if ($susql){
mysql_query($susql);
if (mysql_error())
print "<H1>Add Time Error (SubAccount): ".mysql_error()."</H1>";
}
}
?>