I am making an eCommerce web application having commission as one of the feature and I am working on it.
The orders table:
CREATE TABLE `orders` (
`OrderId` int(11) NOT NULL AUTO_INCREMENT,
`OrderCode` varchar(20) NOT NULL,
`CustEmailAdd` varchar(80) NOT NULL,
`CustDelAddId` varchar(255) NOT NULL,
`ProdCode` varchar(255) NOT NULL,
`Quantity` varchar(100) NOT NULL,
`PaytMethod` varchar(255) NOT NULL,
`ShippingCharges` float NOT NULL,
`TaxedAmount` float NOT NULL,
`AppliedCredits` int(11) NOT NULL,
`PayableAmount` varchar(255) NOT NULL,
`OrderDate` datetime NOT NULL,
`OrderModified` datetime NOT NULL,
`OrderStatus` varchar(255) NOT NULL,
`OrderIPAddress` varchar(20) NOT NULL,
PRIMARY KEY (`OrderId`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
The commission table:
CREATE TABLE `commission` (
`CommId` int(11) NOT NULL AUTO_INCREMENT,
`OrderCode` varchar(20) NOT NULL,
`ProdCode` varchar(255) NOT NULL,
`ModCode` varchar(255) NOT NULL,
`AffiCode` varchar(255) NOT NULL,
`BenCode` varchar(255) NOT NULL,
`ModCommAmount` varchar(255) NOT NULL,
`AffiCommAmount` varchar(255) NOT NULL,
`BenCommAmount` varchar(255) NOT NULL,
PRIMARY KEY (`CommId`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
The affiliate_commission_payable table
CREATE TABLE `affiliate_commission_payable` (
`ACP_Id` int(11) NOT NULL AUTO_INCREMENT,
`CommId` int(11) NOT NULL,
`ACP_PaymentStatus` varchar(255) NOT NULL,
`ACP_PaymentDate` varchar(255) NOT NULL,
`ACP_PaymentDetails` text NOT NULL,
PRIMARY KEY (`ACP_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
I have an array like so:
// For Order 000001
Array
(
[0] => ORD-000001
)
Array
(
[Paid] => 26.25
[Due] => 42.75
)
// For Order 000002
Array
(
[0] => ORD-000002
)
Array
(
[Cancelled] => 33.75
[Due] => 13.5
)
// For Order 000003
Array
(
[0] => ORD-000003
)
Array
(
[Paid] => 13.50
[Paid] => 14.25
)
What I am trying to do is showing the order commission summary to the user. It is in the table format like so:
OrderCode Paid Comm Cancelled Comm Due Comm View Details
ORD-000001 addAllPaid addAllCancelled addAllDue View
ORD-000002 addAllPaid addAllCancelled addAllDue View
ORD-000003 addAllPaid addAllCancelled addAllDue View
If there is no value in the array for commission, replace it with 0, else add them and display it based on the order code. How do I achieve this ?
The code that I have tried so far:->
<?php
$qu = "SELECT
c.*, o.*, acp.*, .*, a.*
FROM
customers c, orders o, affiliate_commission_payable acp, commission com, affiliates a
WHERE
a.AffiCode = '".$affiCode."'
AND
c.AffiCode = '".$affiCode."'
AND
c.CustEmailAdd = o.CustEmailAdd
AND
acp.CommId = com.commId
AND
com.OrderCode = o.OrderCode
AND
com.AffiCode = c.AffiCode";
$validate->Query($qu);
if ($validate->NumRows() >= 1) {
while ($rows = $validate->FetchAllDatas()) {
$commId = $rows["CommId"];
$orderCode = $rows["OrderCode"];
$orderDate = $rows["OrderDate"];
$arrCommissionStatus = explode(', ', $rows["ACP_PaymentStatus"]);
$prdCodes = explode(', ', $rows["ProdCode"] );
$arrAffiCommAmount = explode(', ', $rows["AffiCommAmount"]);
$ord = explode(', ', $rows["OrderCode"]);
$prdCodeAndCommStatus = array_combine($prdCodes, $arrCommissionStatus);
$arrCommAndStatus = array_combine($arrCommissionStatus, $arrAffiCommAmount);
$table .= "<tr>";
$table .= "<td>".$orderCode."</td>";
$table .= "<td>".$orderDate."</td>";
$table .= "<td>".count($prdCodes)."</td>";
foreach ($arrCommAndStatus as $key => $value) {
if ($value == 'Due') {
$totDueComm += $key;
} else {
$totDueComm = 0;
}
}
foreach ($arrCommAndStatus as $key => $value) {
if ($value == 'Cancelled') {
$totCancellComm += $key;
} else {
$totCancellComm = 0;
}
}
foreach ($arrCommAndStatus as $key => $value) {
if ($value == 'Paid') {
$totPaidComm += $key;
} else {
$totPaidComm = 0;
}
}
$table .= "<td>".number_format($totPaidComm, 2)."</td>";
$table .= "<td>".number_format($totCancellComm, 2)."</td>";
$table .= "<td>".number_format($totDueComm, 2)."</td>";
$table .= "<td>".number_format(array_sum($arrAffiCommAmount), 2)."</td>";
$table .= "<td><a href='//www.example.com/nbs/Affiliates/Commission.php?ord=".$orderCode."&id=".$commId."'>View</a></td>";
$table .= "</tr>";
}
}
UPDATE 1:
I have somehow tried to get it done.. Here's the code that I had to change inside the while loop above:
$strQ = "SELECT com.*, acp.* FROM commission com, affiliate_commission_payable acp WHERE com.AffiCode = '".$affiCode."' AND com.OrderCode = '".$orderCode."' AND com.CommId = acp.CommId";
$validate->Query($strQ);
if ($validate->NumRows() >= 1) {
while ($rows_strQ = $validate->FetchAllDatas()) {
$comAmt = explode(', ', $rows_strQ["AffiCommAmount"]);
$coStat = explode(', ', $rows_strQ["ACP_PaymentStatus"]);
$ts = array_combine( $comAmt, $coStat);
foreach ($ts as $key => $value) {
if (in_array($value, $sta) && $value == 'Due') {
$totDueComm += $key;
$totalDueCommission += $totDueComm;
}
}
}
}
But the update has just done it for 1st order and not all the orders. Where have I gone wrong ? Kindly help me out. Thanks.
UPDATE 2:
I have somehow managed it to get it done.. But there is a problem that the amount is getting added to the previous order amount and then the amount is displayed.. For example, Say I have Rs. 300 in Order 1 and Rs. 200 in Order 2.. So for Order 1, it will display Rs. 300 and for Order 2, the amount shown is Rs. 500. I want it to show Rs. 200 only and not Rs 500. How do I solve this ? Please help me.. Thanks once again.
Here's the code that I have used so far:
$sta = array('Due', 'Cancelled', 'Paid');
$qu = "SELECT c.*, o.*, acp.*, com.*, a.* FROM customers c, orders o, affiliate_commission_payable acp, commission com, affiliates a WHERE a.AffiCode = '".$affiCode."' AND c.AffiCode = '".$affiCode."' AND c.CustEmailAdd = o.CustEmailAdd AND acp.CommId = com.commId AND com.OrderCode = o.OrderCode AND com.AffiCode = c.AffiCode GROUP BY o.OrderCode";
$validate->Query($qu);
if ($validate->NumRows() >= 1) {
while ($rows = $validate->FetchAllDatas()) {
$commId = $rows["CommId"];
$orderCode = $rows["OrderCode"];
$orderDate = $rows["OrderDate"];
$arrCommissionStatus = explode(', ', $rows["ACP_PaymentStatus"]);
$prdCodes = explode(', ', $rows["ProdCode"] );
$arrAffiCommAmount = explode(', ', $rows["AffiCommAmount"]);
$prdCodeAndCommStatus = array_combine($prdCodes, $arrCommissionStatus);
$arrCommAndStatus = array_combine($arrAffiCommAmount, $arrCommissionStatus);
$table .= "<tr>";
$table .= "<td>".$orderCode."</td>";
$table .= "<td>".$orderDate."</td>";
$table .= "<td>".count($prdCodes)."</td>";
foreach ($rows as $key => $value) {
if ($key === "AffiCommAmount") {
$comAmt = explode(', ', $rows["AffiCommAmount"]);
}
if ($key === "ACP_PaymentStatus") {
$coStat = explode(', ', $rows["ACP_PaymentStatus"]);
}
}
$ts = array_combine( $comAmt, $coStat);
foreach ($ts as $k => $v) {
if (in_array($v, $sta) && $v == 'Due') {
$totDueComm += $k;
$totalDueCommission = $totDueComm;
}
}
$table .= "<td>".number_format($totPaidComm, 2)."</td>";
$table .= "<td>".number_format($totCancellComm, 2)."</td>";
$table .= "<td>".number_format($totDueComm, 2)."</td>";
$table .= "<td>".number_format(array_sum($arrAffiCommAmount), 2)."</td>";
$table .= "<td><a href='//www.example.com/nbs/Affiliates/Commission.php?ord=".$orderCode."&id=".$commId."'>View</a></td>";
$table .= "</tr>";
}
}
?>
SHOW CREATE TABLE schema.tablefor the concerned tables