I would like to ask for your help. I'd like to create a PHP script that would count the number of sales each affiliate has made and the total amount of their sales. There are two files involved (csv and txt). Here's the sample data of the CSV and TXT:
//Sales.csv
Customer Id,Name,Surname,ID Number,Type,Member Type,Amount,Lead Supplier,Type,Outcome
2759,Hlompho,Molakalaka,8305066090080,Debit Order,Subscriber,69,Company,New Sale,Sucessful
6884,Ndanganen,Tshidavhu,7902065808081,Debit Order,Subscriber,69,Company,New Sale,Sucessful
11583,Shoaib,Solomon,8308015072084,Debit Order,Subscriber,59,Company,Revenue,Sucessful
15507,Rathanambal,Govender,7312190242083,Debit Order,Subscriber,69,Company,Revenue,Sucessful
17456,Dumisani,Tshabalala,8406275675081,Debit Order,Subscriber,59,Company,Revenue,Sucessful
17640,Nomadlozi,Magagula,9201110277089,Debit Order,Subscriber,59,Company,Revenue,Sucessful
21545,Jason,Goosen,9003105124080,Debit Order,Subscriber,59,Company,Revenue,Sucessful
22001,Jacques,Kok,8503025107082,Debit Order,Subscriber,59,Company,Revenue,Sucessful
25137,Natelly,Erasmus,8803210085089,Debit Order,Subscriber,59,Company,Revenue,Sucessful
25431,Mfanufikile,Nala,8211085667082,Debit Order,Subscriber,59,Company,Revenue,Sucessful
//Report.txt
Date,Affiliate ID,Unique ID,Firstname,Lastname,ID Number,Phone,Email,Status,Reference Number
2013-05-01 0:05,1017,u104101722ne3001hj,Shoaib,Solomon,8308015072084,083-651-9877,mxoalarm.alarm@gmail.com,success=true;,146213
2013-05-01 1:58,1009,kf04100923fr3053vp,Hlompho,Molakalaka,8305066090080,0781257255,nkoanat@yahoo.com,success=true;,0
2013-05-01 1:58,1018,dv04101823c730573u,nasiera,hardy,8810160145086,082-346-6104,nasierahardy@yahoo.com,success=true;,146216
2013-05-01 2:00,1013,v04101323ks3057gp,Hlompho,Molakalaka,8305066090080,0763283541,pimpmybodytattoos@gmail.com,success=true;,146217
2013-05-01 2:01,1018,n504101823mh3059w2,zaid,slamdien,8409245240085,071-232-7059,zaid.slamdien@yahoo.com,success=true;,146218
2013-05-01 2:17,1013,pr0510130qy0116j4,zaid,slamdien,8409245240085,071-232-7059,zaid.slamdien@yahoo.com,success=true;,0
2013-05-01 2:18,1013,gd0510130pj0117rm,Dumisani,Tshabalala,8406275675081,082-346-6104,nasierahardy@yahoo.com,success=true;,0
2013-05-01 3:11,1013,oh05101312x0104gf,Lucky Magnificent,Mabena,8104165398084,072-758-4097,lucky.mabena@sasol.com,success=true;,0
2013-05-01 3:34,1001,8c0510011180132t,Nomadlozi,Magagula,9201110277089,084-028-7128,chaddo913@gmail.com,success=true
Basically, what the script would do is to get the ID number in every line of the csv file and then it will search for the first affiliate in the txt with the same ID number. (In case an ID number appears multiple times in the file, it will only be counted/credited to the first affiliate with that ID number) It will then count the number of sales that affiliate has made and the total of its sales. The affiliate will be determined by its Affiliate ID. In case an ID number does not match any affiliate in the txt, 1 will be used as the affiliate ID. Here's the sample output:
/* AffiliateID,NumberOfSales,TotalAmount */
1001,24,678
1009,72,328
1,3,144 //Sales with no match in the txt uses 1 as aff ID.
I've made several attempts to create the script but to no avail. I hope someone could help me out here. Thanks!
Update: I've decided to create two tables in the database: commissions and log in order to save the csv and txt data respectively. From here, I executed a MySQL query to get the output I wanted. Here's the code:
<?php
//Convert Spreadsheet (XLSX) to CSV
require_once '../classes/PHPExcel/IOFactory.php';
//Set maximum execution time to 1 hour.
ini_set('max_execution_time', 3600);
$excel = PHPExcel_IOFactory::load("Kudough Commision Report.xlsx");
$writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->setDelimiter(",");
$writer->setEnclosure("");
$writer->setSheetIndex(1);
$writer->save("commissions".date('Ym').".csv");
//Establish a MySQL Database Connection.
$con = mysqli_connect("localhost","root","","kudough");
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Save the CSV to the database.
$file1 = "commissions".date('Ym').".csv"; //CSV filename
$handle1 = @fopen($file1, "r");
if ($handle1) {
while (($buffer1 = fgets($handle1, 4096)) !== false) {
//Get Variables.
$var1 = explode(",", $buffer1);
$cid = @$var1[0];
$fname = @str_replace("'","",$var1[1]);
$lname = @str_replace("'","",$var1[2]);
$idnumber = @$var1[3];
$type = @$var1[4];
$membertype = @$var1[5];
$amount = @$var1[6];
$leadsupplier = @$var1[7];
$saletype = @$var1[8];
$outcome = @$var1[9];
$sql = "INSERT IGNORE INTO commissions (customer_id, name, surname, idnumber, type, member_type, amount, leadsupplier, saletype, outcome) VALUES ('$cid', '$fname', '$lname', '$idnumber', '$type', '$membertype', '$amount', '$leadsupplier', '$saletype', '$outcome')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
}
if (!feof($handle1)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle1);
}
//Save the TXT to the database.
$file2 = "kudough201305.txt"; //TXT filename
$handle2 = @fopen($file2, "r");
if ($handle2) {
while (($buffer2 = fgets($handle2, 4096)) !== false) {
//Get Variables.
$var2 = explode(",", $buffer2);
$logdate = @$var2[0];
$aff_id = @$var2[1];
$unique_id = @$var2[2];
$fname = @$var2[3];
$lname = @$var2[4];
$idnumber = @$var2[5];
$phone = @$var2[6];
$email = @$var2[7];
$trax = @$var2[8];
$ref = @$var2[9];
$sql = "INSERT IGNORE INTO log (logdate, aff_id, unique_id, fname, lname, idnumber, phone, email, trax, ref) VALUES ('$logdate', '$aff_id', '$unique_id', '$fname', '$lname', '$idnumber', '$phone', '$email', '$trax', '$ref')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
}
if (!feof($handle2)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle2);
}
//Count the number of sales and total amount of sales each affiliate has made.
$sql = "SELECT T2.aff_id AS affID, COUNT(T2.idnumber) AS numofsales, SUM(amount) AS totalsales FROM (SELECT aff_id, T1.idnumber, T1.amount FROM (SELECT * FROM commissions WHERE leadsupplier = 'IMUpstart' GROUP BY commissions.idnumber) AS T1 INNER JOIN log WHERE T1.idnumber = log.idnumber GROUP BY T1.idnumber) AS T2 GROUP BY T2.aff_id";
$rs = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($rs)) {
$affID = $row['affID'];
$numofsales = $row['numofsales'];
$totalsales = $row['totalsales'];
//Write to CSV
$entrydetails = $affID.", ".$numofsales.", ".$totalsales;
WriteCSVFile($entrydetails);
}
mysqli_close($con);
?>
I'm not quite sure of the MySQL statement if it's correct. My problem now is to find the total of all the sales of those ID numbers found in the csv but not in the txt. If you have any suggestions for improving the code, Please let me know. Thanks! :)