Find this useful? Enter your email to receive occasional updates for securing PHP code.
Signing you up...
Thank you for signing up!
PHP Decode
<style> .table tbody > tr > th, .table tbody > tr > td, .table tfoot > tr > th, .table t..
Decoded Output download
<style>
.table tbody > tr > th, .table tbody > tr > td, .table tfoot > tr > th, .table tfoot > tr > td {
vertical-align: top;
text-align: left;
padding: 0.25em!important;
border-left: 1px dotted #333333;
}</style>
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-lqy6{text-align:right;vertical-align:top}
.table tbody > tr > td{
border-left:1px solid black;
}
.table {
border-collapse: collapse;
}
tr {
background-color: white!important;
}
div {
background-color: white;
}
td {
background-color: white!important;
}
</style>
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/auth.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/db.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/functions.php');
$page = "Reports";
require_once('includes/header.php');
$test_day = '2013-01-05';
$use_test_day = false;
// Report Configuration
// ###########################################
$payPeriodCount = 4;
$startDate = null;
$totals = array();
$sales_reps = array();
$repData = array();
$report_name = "Rep Commission Report";
// ###########################################
// build array of pay period months
$payPeriods = array();
$testDate = strtotime('2015-01-01');
while($testDate <= time()){
$start = strtotime(date('Y-m-1',$testDate));
$end = strtotime(date('Y-m-t',$testDate)); // date('Y-m-d',strtotime('+1 month',$start));
$label = date('Y-m-01',$testDate).' to '.date('Y-m-t',$testDate);
$key = date('Y-m-01',$testDate); // .' - '.date('Y-m-t',$testDate);
$payPeriods[$key] = array(
'start' => $start,
'end' => $end,
'label' => $label,
);
$testDate = strtotime('+1 month',$testDate);
}
ksort($payPeriods);
/*
echo "<pre>";
print_r($payPeriods);
echo "</pre>";
*/
foreach($payPeriods as $year => $months){
ksort($months);
$payPeriods[$year] = $months;
}
$payPeriods = array_slice($payPeriods,-$payPeriodCount);
$firstPeriod = array_slice($payPeriods,0,1);
$firstPeriod = array_values($firstPeriod);
$startDate = $firstPeriod[0]['start'];
// load sales reps
$q = "
SELECT qb_name FROM user_groups_users
LEFT JOIN user_groups ON user_groups_users.user_group_id = user_groups.id
LEFT JOIN reps ON reps.id = user_groups_users.user_id
WHERE user_groups.key = 'sales'
AND reps.id NOT IN ('25')
";
if(!hasPermission('admin') && !hasPermission('sales_mgr')){
$q.= "AND qb_name = '".$_SESSION['logged_in']['qb_name']."' ";
}
$res = mysql_query($q,$dbhandlesalt);
while ($sales_rep = mysql_fetch_array($res)) {
$sales_reps[] = $sales_rep['qb_name'];
}
// get all line items from invoices (except shipping) and COGS
$q = "
SELECT
invoice.TxnID, invoice.TxnDate, invoice.SalesRepRef_FullName,
invoicelinedetail.Amount, invoicelinedetail.Quantity, iteminventory.AverageCost
FROM invoice
LEFT JOIN invoicelinedetail ON invoice.TxnID = invoicelinedetail.IDKEY
LEFT JOIN iteminventory ON iteminventory.ListID = invoicelinedetail.ItemRef_ListID
LEFT JOIN txnitemlinedetail ON txnitemlinedetail.TxnLineID = invoicelinedetail.TxnLineID ";
$q.= "WHERE DATE(invoice.TxnDate) >= '".date('Y-m-01',$startDate)."' ";
if(!hasPermission('admin') && !hasPermission('sales_mgr')){
$q.= "AND invoice.SalesRepRef_FullName = '".$_SESSION['logged_in']['qb_name']."' ";
}
$q.="
AND LOWER(invoicelinedetail.ItemRef_FullName) NOT LIKE 'shipping%'
AND LOWER(invoicelinedetail.ItemRef_FullName) NOT LIKE '%avatax%'
and (invoicelinedetail.itemref_fullname not like 'Shipping%')
and (invoicelinedetail.itemref_fullname not like 'Warrant%')
and (invoicelinedetail.itemref_fullname not like 'Freight%')
and (invoicelinedetail.itemref_fullname not like 'Discount:Damaged Goods')
and (invoicelinedetail.itemref_fullname not like 'Ground')
";
// This seems to be pulling line item numbers correct - it's rev minus discounts
//echo "<p>".$q."</p>";
$res = mysql_query($q,$dbhandleqb);
if(!$res){
die(mysql_error($dbhandleqb));
}
// items
while ($item = mysql_fetch_array($res)) {
$dateKey = '';
foreach($payPeriods as $key => $per){
if(strtotime($item['TxnDate']) >= $per['start'] && strtotime($item['TxnDate']) <= $per['end']){
$dateKey = $key;
}
}
if($dateKey){
$repSplit = array();
$repSplit = explode("/",$item['SalesRepRef_FullName']);
foreach($repSplit as $repID){
if(
$repID == $_SESSION['logged_in']['qb_name']
|| hasPermission('admin')
){
$repID = trim($repID);
$amount = 0;
$discounts = 0;
if($item['Amount'] >= 0) {
$amount = $item['Amount'] ? $item['Amount'] : 0;
// echo "Rep: ".$repID." | Month of: ".$dateKey." | Transaction Date: ".$item['TxnDate']." | Line Item Amount: ".$amount."<br>";
} else {
$discounts = $item['Amount'] ? $item['Amount'] : 0;
// echo "Rep: ".$repID." | Month of: ".$dateKey." | Transaction Date: ".$item['TxnDate']." | Line Item Discount: ".$discounts."<br>";
}
$cost = $item['AverageCost'] ? $item['AverageCost'] * $item['Quantity'] : 0;
if(!in_array($repID,$sales_reps)) {
$repID = "Other";
}
if(!isset($repData[$repID]['revenue'][$dateKey])){
$repData[$repID]['revenue'][$dateKey] = 0;
}
if(!isset($repData[$repID]['discounts'][$dateKey])){
$repData[$repID]['discounts'][$dateKey] = 0;
}
if(!isset($repData[$repID]['count'][$dateKey])){
$repData[$repID]['count'][$dateKey] = 0;
}
if(!isset($repData[$repID]['cogs'][$dateKey])){
$repData[$repID]['cogs'][$dateKey] = 0;
}
if(!isset($repData[$repID]['profit'][$dateKey])){
$repData[$repID]['profit'][$dateKey] = 0;
}
$repData[$repID]['revenue'][$dateKey] += ($amount / count($repSplit));
$repData[$repID]['discounts'][$dateKey] += ($discounts / count($repSplit));
$repData[$repID]['cogs'][$dateKey] += ($cost / count($repSplit));
$repData[$repID]['profit'][$dateKey] += ($amount / count($repSplit)) - ($cost / count($repSplit));
$repData[$repID]['count'][$dateKey] += 1;
}
}
if(!isset($totals['revenue'][$dateKey])){
$totals['revenue'][$dateKey] = 0;
}
if(!isset($totals['discounts'][$dateKey])){
$totals['discounts'][$dateKey] = 0;
}
if(!isset($totals['count'][$dateKey])){
$totals['count'][$dateKey] = 0;
}
if(!isset($totals['cogs'][$dateKey])){
$totals['cogs'][$dateKey] = 0;
}
if(!isset($totals['profit'][$dateKey])){
$totals['profit'][$dateKey] = 0;
}
$totals['revenue'][$dateKey] += ($amount / count($repSplit));
$totals['discounts'][$dateKey] += ($discounts / count($repSplit));
$totals['cogs'][$dateKey] += ($cost / count($repSplit));
$totals['profit'][$dateKey] += ($amount / count($repSplit)) - ($cost / count($repSplit));
$totals['count'][$dateKey] += 1;
$total_rev += $amount;
$total_count += 1;
}
}
$q = "
SELECT
creditmemo.TxnID, creditmemo.TxnDate, creditmemo.SalesRepRef_FullName,
creditmemolinedetail.Amount, creditmemolinedetail.Quantity, iteminventory.AverageCost
FROM creditmemo
LEFT JOIN creditmemolinedetail ON creditmemo.TxnID = creditmemolinedetail.IDKEY
LEFT JOIN iteminventory ON iteminventory.ListID = creditmemolinedetail.ItemRef_ListID ";
$q.= "WHERE DATE(creditmemo.TxnDate) >= '".date('Y-m-01',$startDate)."' ";
if(!hasPermission('admin') && !hasPermission('sales_mgr')){
$q.= "AND creditmemo.SalesRepRef_FullName = '".$_SESSION['logged_in']['qb_name']."' ";
}
$q.="
AND LOWER(creditmemolinedetail.ItemRef_FullName) LIKE 'discount%'
";
// This seems to be pulling line item numbers correct - it's rev minus discounts
//echo "<p>".$q."</p>";
$res = mysql_query($q,$dbhandleqb);
if(!$res){
die(mysql_error($dbhandleqb));
}
// items
while ($item = mysql_fetch_array($res)) {
$dateKey = '';
foreach($payPeriods as $key => $per){
if(strtotime($item['TxnDate']) >= $per['start'] && strtotime($item['TxnDate']) <= $per['end']){
$dateKey = $key;
}
}
if($dateKey){
$repSplit = array();
$repSplit = explode("/",$item['SalesRepRef_FullName']);
foreach($repSplit as $repID){
if(
$repID == $_SESSION['logged_in']['qb_name']
|| hasPermission('admin')
){
$repID = trim($repID);
$amount = 0;
$discounts = 0;
if($item['Amount'] >= 0) {
$amount = $item['Amount'] ? $item['Amount'] : 0;
// echo "Rep: ".$repID." | Month of: ".$dateKey." | Transaction Date: ".$item['TxnDate']." | Line Item Amount: ".$amount."<br>";
} else {
$discounts = ($item['Amount'] ? $item['Amount'] : 0)*-1;
// echo "Rep: ".$repID." | Month of: ".$dateKey." | Transaction Date: ".$item['TxnDate']." | Negative credit memo Discount: ".$discounts."<br>";
}
$cost = $item['AverageCost'] ? $item['AverageCost'] * $item['Quantity'] : 0;
if(!in_array($repID,$sales_reps)) {
$repID = "Other";
}
if(!isset($repData[$repID]['revenue'][$dateKey])){
$repData[$repID]['revenue'][$dateKey] = 0;
}
if(!isset($repData[$repID]['discounts'][$dateKey])){
$repData[$repID]['discounts'][$dateKey] = 0;
}
if(!isset($repData[$repID]['count'][$dateKey])){
$repData[$repID]['count'][$dateKey] = 0;
}
if(!isset($repData[$repID]['cogs'][$dateKey])){
$repData[$repID]['cogs'][$dateKey] = 0;
}
if(!isset($repData[$repID]['profit'][$dateKey])){
$repData[$repID]['profit'][$dateKey] = 0;
}
$repData[$repID]['revenue'][$dateKey] += ($amount / count($repSplit));
$repData[$repID]['discounts'][$dateKey] += ($discounts / count($repSplit));
$repData[$repID]['cogs'][$dateKey] += ($cost / count($repSplit));
$repData[$repID]['profit'][$dateKey] += ($amount / count($repSplit)) - ($cost / count($repSplit));
$repData[$repID]['count'][$dateKey] += 1;
}
}
if(!isset($totals['revenue'][$dateKey])){
$totals['revenue'][$dateKey] = 0;
}
if(!isset($totals['discounts'][$dateKey])){
$totals['discounts'][$dateKey] = 0;
}
if(!isset($totals['count'][$dateKey])){
$totals['count'][$dateKey] = 0;
}
if(!isset($totals['cogs'][$dateKey])){
$totals['cogs'][$dateKey] = 0;
}
if(!isset($totals['profit'][$dateKey])){
$totals['profit'][$dateKey] = 0;
}
$totals['revenue'][$dateKey] += ($amount / count($repSplit));
$totals['discounts'][$dateKey] += ($discounts / count($repSplit));
$totals['cogs'][$dateKey] += ($cost / count($repSplit));
$totals['profit'][$dateKey] += ($amount / count($repSplit)) - ($cost / count($repSplit));
$totals['count'][$dateKey] += 1;
$total_rev += $amount;
$total_count += 1;
}
}
$q = "
SELECT
creditmemo.TxnID, creditmemo.TxnDate, creditmemo.SalesRepRef_FullName,
creditmemolinedetail.Amount, creditmemolinedetail.Quantity, iteminventory.AverageCost
FROM creditmemo
LEFT JOIN creditmemolinedetail ON creditmemo.TxnID = creditmemolinedetail.IDKEY
LEFT JOIN iteminventory ON iteminventory.ListID = creditmemolinedetail.ItemRef_ListID ";
$q.= "WHERE DATE(creditmemo.TxnDate) >= '".date('Y-m-01',$startDate)."' ";
if(!hasPermission('admin') && !hasPermission('sales_mgr')){
$q.= "AND creditmemo.SalesRepRef_FullName = '".$_SESSION['logged_in']['qb_name']."' ";
}
$q.="
AND LOWER(creditmemolinedetail.ItemRef_FullName) LIKE 'customer%'
";
// This seems to be pulling line item numbers correct - it's rev minus discounts
//echo "<p>".$q."</p>";
$res = mysql_query($q,$dbhandleqb);
if(!$res){
die(mysql_error($dbhandleqb));
}
// items
while ($item = mysql_fetch_array($res)) {
$dateKey = '';
foreach($payPeriods as $key => $per){
if(strtotime($item['TxnDate']) >= $per['start'] && strtotime($item['TxnDate']) <= $per['end']){
$dateKey = $key;
}
}
if($dateKey){
$repSplit = array();
$repSplit = explode("/",$item['SalesRepRef_FullName']);
foreach($repSplit as $repID){
if(
$repID == $_SESSION['logged_in']['qb_name']
|| hasPermission('admin')
){
$repID = trim($repID);
$amount = 0;
$discounts = 0;
if($item['Amount'] >= 0) {
$discounts = ($item['Amount'] ? $item['Amount'] : 0)*-1;
// echo "Rep: ".$repID." | Month of: ".$dateKey." | Transaction Date: ".$item['TxnDate']." | Line Item Amount: ".$amount."<br>";
} else {
$discounts = $item['Amount'] ? $item['Amount'] : 0;
// echo "Rep: ".$repID." | Month of: ".$dateKey." | Transaction Date: ".$item['TxnDate']." | Negative credit memo Discount: ".$discounts."<br>";
}
$cost = $item['AverageCost'] ? $item['AverageCost'] * $item['Quantity'] : 0;
if(!in_array($repID,$sales_reps)) {
$repID = "Other";
}
if(!isset($repData[$repID]['revenue'][$dateKey])){
$repData[$repID]['revenue'][$dateKey] = 0;
}
if(!isset($repData[$repID]['discounts'][$dateKey])){
$repData[$repID]['discounts'][$dateKey] = 0;
}
if(!isset($repData[$repID]['count'][$dateKey])){
$repData[$repID]['count'][$dateKey] = 0;
}
if(!isset($repData[$repID]['cogs'][$dateKey])){
$repData[$repID]['cogs'][$dateKey] = 0;
}
if(!isset($repData[$repID]['profit'][$dateKey])){
$repData[$repID]['profit'][$dateKey] = 0;
}
$repData[$repID]['revenue'][$dateKey] += ($amount / count($repSplit));
$repData[$repID]['discounts'][$dateKey] += ($discounts / count($repSplit));
$repData[$repID]['cogs'][$dateKey] += ($cost / count($repSplit));
$repData[$repID]['profit'][$dateKey] += ($amount / count($repSplit)) - ($cost / count($repSplit));
$repData[$repID]['count'][$dateKey] += 1;
}
}
if(!isset($totals['revenue'][$dateKey])){
$totals['revenue'][$dateKey] = 0;
}
if(!isset($totals['discounts'][$dateKey])){
$totals['discounts'][$dateKey] = 0;
}
if(!isset($totals['count'][$dateKey])){
$totals['count'][$dateKey] = 0;
}
if(!isset($totals['cogs'][$dateKey])){
$totals['cogs'][$dateKey] = 0;
}
if(!isset($totals['profit'][$dateKey])){
$totals['profit'][$dateKey] = 0;
}
$totals['revenue'][$dateKey] += ($amount / count($repSplit));
$totals['discounts'][$dateKey] += ($discounts / count($repSplit));
$totals['cogs'][$dateKey] += ($cost / count($repSplit));
$totals['profit'][$dateKey] += ($amount / count($repSplit)) - ($cost / count($repSplit));
$totals['count'][$dateKey] += 1;
$total_rev += $amount;
$total_count += 1;
}
}
// CREDIT MEMOS - subtracts the total credit memos for each month
// if(!$use_test_day) {
$q = "
SELECT
creditmemo.TxnID, creditmemo.TxnDate, creditmemo.SalesRepRef_FullName,
creditmemolinedetail.Amount, creditmemolinedetail.Quantity, iteminventory.AverageCost
FROM creditmemo
LEFT JOIN creditmemolinedetail ON creditmemo.TxnID = creditmemolinedetail.IDKEY
LEFT JOIN iteminventory ON iteminventory.ListID = creditmemolinedetail.ItemRef_ListID
WHERE DATE(creditmemo.TxnDate) >= '".date('Y-m-01',$startDate)."'
AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE 'shipping%'
AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE '%avatax%'
AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE 'fraudulent%'
AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE 'discount%'
AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE 'warrant%'
AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE 'restock%'
AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE 'ground'
and creditmemolinedetail.description not in ('Shipping','Customer Satisfaction','Discount')
";
// } else {
// $q = "
// SELECT
// creditmemo.TxnID, creditmemo.TxnDate, creditmemo.SalesRepRef_FullName,
// creditmemolinedetail.Amount, creditmemolinedetail.Quantity, iteminventory.AverageCost
// FROM creditmemo
// LEFT JOIN creditmemolinedetail ON creditmemo.TxnID = creditmemolinedetail.IDKEY
// LEFT JOIN iteminventory ON iteminventory.ListID = creditmemolinedetail.ItemRef_ListID
// WHERE DATE(creditmemo.TxnDate) = '".$test_day."'
// AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE 'shipping%'
// AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE '%avatax%'
//
// ";
//
//
//
// }
if(!hasPermission('admin') && !hasPermission('sales_mgr')){
$q.= "AND creditmemo.SalesRepRef_FullName = '".$_SESSION['logged_in']['qb_name']."' ";
}
// echo "<hr><p>".$q."</p><hr>";
$res = mysql_query($q,$dbhandleqb);
if(!$res){
die(mysql_error($dbhandleqb));
}
while ($row = mysql_fetch_array($res)) {
$dateKey = '';
foreach($payPeriods as $key => $per){
if(strtotime($row['TxnDate']) >= $per['start'] && strtotime($row['TxnDate']) <= $per['end']){
$dateKey = $key;
}
}
// echo "Key: ".$dateKey."<hr>";
// print_r($row);
// echo "<hr>";
if($dateKey){
$amount = $row['Amount'] ? $row['Amount'] : 0;
$cost = $row['AverageCost'] ? $row['AverageCost'] : 0;
$qty = $row['Quantity'] ? $row['Quantity'] : 0;
$cost = $cost * $qty;
$repSplit = array();
$repSplit = explode("/",$row['SalesRepRef_FullName']);
foreach($repSplit as $repID){
$repID = trim($repID);
if(
$repID == $_SESSION['logged_in']['qb_name']
|| hasPermission('admin')
){
if(!in_array($repID,$sales_reps)) {
$repID = "Other";
}
if(!isset($repData[$repID]['revenue'][$dateKey])){
$repData[$repID]['revenue'][$dateKey] = 0;
}
if(!isset($repData[$repID]['cogs'][$dateKey])){
// $repData[$repID]['revenue'][$dateKey] = 0;
$repData[$repID]['cogs'][$dateKey] = 0;
}
if(!isset($repData[$repID]['discounts'][$dateKey])){
$repData[$repID]['discounts'][$dateKey] = 0;
}
if(!isset($repData[$repID]['profit'][$dateKey])){
$repData[$repID]['profit'][$dateKey] = 0;
}
if(!isset($repData[$repID]['credit_memo_amt'][$dateKey])){
$repData[$repID]['credit_memo_amt'][$dateKey] = 0;
}
if(!isset($repData[$repID]['credit_memo_cost'][$dateKey])){
$repData[$repID]['credit_memo_cost'][$dateKey] = 0;
}
// $repData[$repID]['revenue'][$dateKey] -= ($amount / count($repSplit));
// $repData[$repID]['cogs'][$dateKey] -= ($cost / count($repSplit));
$repData[$repID]['credit_memo_amt'][$dateKey] += ($amount / count($repSplit));
$repData[$repID]['credit_memo_cost'][$dateKey] += ($cost / count($repSplit));
// MIKE NOTE:
// echo "CM: | ".$item['TxnDate']." | ".$dateKey." | ".$amount." | ".$cost."<br>";
// $repData[$repID]['profit'][$dateKey] -= ($amount / count($repSplit));
// $repData[$repID]['profit'][$dateKey] += ($cost / count($repSplit));
}
}
if(!isset($totals['revenue'][$dateKey])){
$totals['revenue'][$dateKey] = 0;
}
if(!isset($totals['creditmemos'][$dateKey])){
$totals['creditmemos'][$dateKey] = 0;
}
$totals['revenue'][$dateKey] -= $amount;
$totals['creditmemos'][$dateKey] += $amount;
}
}
ksort($repData);
/*
echo "<pre>";
print_r($repData);
echo "</pre>";
exit;
*/
$revenue_tiers = array();
$revenue_tiers['1']['min'] = 0;
$revenue_tiers['1']['base'] = 2000;
$revenue_tiers['1']['commission_percentage'] = 5;
$revenue_tiers['2']['min'] = 10000;
$revenue_tiers['2']['base'] = 2250;
$revenue_tiers['2']['commission_percentage'] = 5;
$revenue_tiers['3']['min'] = 20000;
$revenue_tiers['3']['base'] = 2500;
$revenue_tiers['3']['commission_percentage'] = 5;
$revenue_tiers['4']['min'] = 30000;
$revenue_tiers['4']['base'] = 2750;
$revenue_tiers['4']['commission_percentage'] = 5;
$revenue_tiers['5']['min'] = 40000;
$revenue_tiers['5']['base'] = 3000;
$revenue_tiers['5']['commission_percentage'] = 5;
$revenue_tiers['6']['min'] = 50000;
$revenue_tiers['6']['base'] = 3250;
$revenue_tiers['6']['commission_percentage'] = 5;
$revenue_tiers['7']['min'] = 60000;
$revenue_tiers['7']['base'] = 3500;
$revenue_tiers['7']['commission_percentage'] = 5;
?>
<div id="control-bar" class="grey-bg clearfix" style="opacity: 1; ">
<div class="container_12">
<div class="float-left">
<a href="/reports.php"><button type="button"><img src="/images/icons/fugue/navigation-180.png" width="16" height="16"> View All Reports</button></a></div>
</div>
</div>
<article class="container_12">
<section class="grid_12">
<?php
echo '<div class="no-margin">
<table class="table" cellspacing="0" width="100%" >
<thead>
<tr>
<th>Rep</th>';
foreach ($payPeriods as $key => $per) {
echo "<th>".$per['label']."</th>";
}
echo " <th>Total</th>
<tr>
</thead>";
foreach ($repData as $rep => $repdata) {
echo "<tr><td style='border-bottom:1px solid black;'><strong>".$rep."</strong></td>";
$q2 = "SELECT healthcare_cost FROM reps WHERE qb_name = '".$rep."'";
$r2 = mysql_query($q2,$dbhandlesalt);
while ($sales_rep = mysql_fetch_array($r2)) {
$healthcare_cost = $sales_rep['healthcare_cost'];
}
foreach ($payPeriods as $key => $per) {
$rev = 0;
$cogs = 0;
$profit = 0;
$discounts = 0;
$credit_memos = 0;
if(isset($repdata['revenue'][$key])){
$rev = $repdata['revenue'][$key];
}
if(isset($repdata['credit_memo_amt'][$key])){
$credit_memos = $repdata['credit_memo_amt'][$key];
}
if(isset($repdata['discounts'][$key])){
$discounts = ($repdata['discounts'][$key]);
}
if(isset($repdata['cogs'][$key])){
$cogs = $repdata['cogs'][$key] - $repdata['credit_memo_cost'][$key];
}
if(isset($repdata['profit'][$key])){
// $profit = $repdata['profit'][$key];
$profit = $rev - $credit_memos + $discounts - $cogs;
}
echo "<td style='border-bottom:1px solid black;'>";
if($repdata['revenue'][$key] !== 0) {
echo "
<table style='width:100%'>
<tr>
<td><div style='text-align:left; width:135px; display:inline-block;'>Total Sales (incl. credit memos): </div></td>
<td><div style='float:right; text-align: right; width:108px; display:inline-block;'>$".number_format(($rev-$credit_memos), 2, '.', ',')."</div></td></table>";
} else {
echo "
<table>
<tr>
<td><div style='text-align:left; width:135px; display:inline-block;'>Total Sales (incl. credit memos): </div></td>
<td><div style='float:right; text-align: right; width:108px; display:inline-block;'>--</div></td></table>";
}
// if($repdata['credit_memo_amt'][$key] !== 0) {
// echo "
// <table style='width:100%'>
// <tr>
// <td><div style='text-align:left; width:135px; display:inline-block;'>Credit Memos: </div></td>
// <td><div style='float:right; text-align: right; width:108px; display:inline-block;'>$".number_format($credit_memos, 2, '.', ',')."</div></td></table>";
//
// } else {
// echo "
// <table>
// <tr>
// <td><div style='text-align:left; width:135px; display:inline-block; color:#f00;'>Credit Memos: </div></td>
// <td><div style='float:right; text-align: right; width:108px; display:inline-block; color:#f00;'>--</div></td></table>";
// }
if($discounts !== 0) {
echo "
<table style='width:100%;'>
<tr>
<td><div style='text-align:left; color:#f00; width:135px; display:inline-block; '>Total Sales Discounts:</div></td>
<td><div style='float:right; text-align: right; width:108px; display:inline-block; color:#f00;'>$".number_format($discounts*-1, 2, '.', ',')."</div></td></table>";
} else {
echo "
<table style='width:100%;'>
<tr>
<td><div style='text-align:left; color:#f00; width:135px; display:inline-block; '>Total Sales Discounts:</div></td>
<td><div style='float:right; text-align: right; width:108px; display:inline-block; color:#f00;'>--</div></td></table>";
}
if($repdata['revenue'][$key] !== 0) {
echo "
<table style='width:100%;'>
<tr style='border-bottom: 1px solid black;'>
<td><div style='text-align:left; width:135px; display:inline-block;'>Total Income: </div></td>
<td><div style='float:right; text-align: right; width:108px; display:inline-block;'>$".number_format(($rev-$credit_memos+$discounts), 2, '.', ',')."</div></td></table>";
} else {
echo "
<table>
<tr style='border-bottom: 1px solid black;'>
<td><div style='text-align:left; width:135px; display:inline-block;'>Total Income: </div></td>
<td><div style='float:right; text-align: right; width:108px; display:inline-block;'>--</div></td></table>";
}
if($repdata['revenue'][$key] !== 0) {
echo "
<table style='width:100%'>
<tr>
<td><div style='text-align:left; width:135px; display:inline-block;'><strong>Net Income: </strong></div></td>
<td><div style='float:right; text-align: right; width:108px; display:inline-block;'><strong>$".number_format($profit, 2, '.', ',')."</strong></div></td></table>";
} else {
echo "
<table>
<tr>
<td><div style='text-align:left; width:135px; display:inline-block;'><strong>Net Income: </strong></div></td>
<td><div style='float:right; text-align: right; width:108px; display:inline-block;'><strong>--</strong></div></td></table>";
}
if($profit < 10000) {
$tier = 1;
} elseif($profit < 20000) {
$tier = 2;
} elseif($profit < 30000) {
$tier = 3;
} elseif($profit < 40000) {
$tier = 4;
} elseif($profit < 50000) {
$tier = 5;
} elseif($profit < 60000) {
$tier = 6;
} else {
$tier = 7;
}
$base_salary = $revenue_tiers[$tier]['base'];
$holdback = (2.5 * $base_salary) + $healthcare_cost*2.5;
$commission = $profit*.05;
if($commission < 0) { $commission = 0; }
$paycheck_first = 1000;
$paycheck_fifteenth = $base_salary - $paycheck_first + $commission;
$total_monthly_income = $paycheck_first + $paycheck_fifteenth;
if($repdata['revenue'][$key] < 1) {
$next_tier_revenue_estimate = "n/a";
} elseif($tier < 7) {
$next_tier_revenue_estimate = "only $".number_format(($revenue_tiers[$tier+1]['min']-$profit)*$repdata['revenue'][$key]/$profit)."!";
} else {
$next_tier_revenue_estimate = "Already top tier!";
}
// CALCULATE HOW MUCH MORE THEY'LL MAKE IF THEY HIT THE NEXT TIER
if($profit >= 60000) {
$next_tier_monthly_income = "n/a";
} else {
$next_tier_monthly_income = $revenue_tiers[$tier+1]['base'] + ($revenue_tiers[$tier+1]['min'] - $revenue_tiers[$tier+1]['base']*2.5 - $healthcare_cost*2.5)*$revenue_tiers[$tier+1]['commission_percentage']/100;
}
// END OF NEXT TIER
echo "<table style='width:100%'><tr><td><div style='text-align:left; width:135px; display:inline-block;'>
Commission Rate:
</div></td><td><div style='float:right; text-align: right; width:108px; display:inline-block;'>5%
</div></td></table>";
echo "<table style='width:100%'><tr><td><div style='text-align:left; width:135px; display:inline-block;'><strong>
Total Commission:</strong>
</div></td><td><div style='float:right; text-align: right; width:108px; display:inline-block;'><strong>$"
.number_format($commission)."
</strong></div></td></table>";
}
echo "<td style='border-bottom: 1px solid black;'><strong>$".number_format(array_sum($repdata['revenue']))."</strong></td></tr>";
}
if(hasPermission('admin')){
echo "<tr><td><strong>TOTAL REVENUE</strong></td>";
foreach ($payPeriods as $key => $per) {
echo "<td align='center'><strong>$".number_format($totals['revenue'][$key], 0, '.', ',')."</strong></td>";
}
echo "<td align='center'><strong>$".number_format(array_sum($totals['revenue']))."</strong></td></tr>";
echo "</tr>";
}
echo '</table>';
include('includes/row_count.php');
echo '<div class="block-footer clearfix">
<div class="float-left">
</div>
</div>';
echo '</div></div></form></div></section>';
?>
<? /*
<section class="grid_12">
<div class="block-border">
<div class="block-content form">
<h1>Rev Report</h1>
<div id="graph_div" style="width: 100%; height: 700px;"></div>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Month', '<?php
$new_rep_array = array();
foreach ($repData as $rep => $repdata) {
$new_rep_array[] = $rep;
}
echo implode("', '",$new_rep_array); ?>',' test'],
<?php
foreach ($payPeriods as $key => $per) {
if($month != " Old") {
echo "['".$per['label']."', ";
foreach($new_rep_array as $rep){
if($repData[$rep]['revenue'][$key]) {
echo number_format($repData[$rep]['revenue'][$key],0, '.', '').",";
} else {
echo "0,";
}
}
echo "0],
";
}
}
?>
]);
var options = {
title: 'Rep Commission Report'
};
var chart = new google.visualization.ColumnChart(document.getElementById('graph_div'));
chart.draw(data, options);
}
<?php // ColumnChart, PieChart, LineChart ?>
</script>
</div><!-- /.block-content -->
</div><!-- /.block-border -->
</section><!-- /section.grid_12 -->
*/ ?>
<div class="clear"></div>
<?php /*
<pre><?php print_r($repData); ?></pre>
*/ ?>
<section>
<?php
/*
echo "<pre>";
print_r($repData);
echo "</pre>";
echo "<pre>";
print_r($totals);
echo "</pre>";
*/
?>
</section>
<?php
include('includes/footer.php');
//close the connection
mysql_close($dbhandlelive);
?>
Did this file decode correctly?
Original Code
<style>
.table tbody > tr > th, .table tbody > tr > td, .table tfoot > tr > th, .table tfoot > tr > td {
vertical-align: top;
text-align: left;
padding: 0.25em!important;
border-left: 1px dotted #333333;
}</style>
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-lqy6{text-align:right;vertical-align:top}
.table tbody > tr > td{
border-left:1px solid black;
}
.table {
border-collapse: collapse;
}
tr {
background-color: white!important;
}
div {
background-color: white;
}
td {
background-color: white!important;
}
</style>
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/auth.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/db.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/functions.php');
$page = "Reports";
require_once('includes/header.php');
$test_day = '2013-01-05';
$use_test_day = false;
// Report Configuration
// ###########################################
$payPeriodCount = 4;
$startDate = null;
$totals = array();
$sales_reps = array();
$repData = array();
$report_name = "Rep Commission Report";
// ###########################################
// build array of pay period months
$payPeriods = array();
$testDate = strtotime('2015-01-01');
while($testDate <= time()){
$start = strtotime(date('Y-m-1',$testDate));
$end = strtotime(date('Y-m-t',$testDate)); // date('Y-m-d',strtotime('+1 month',$start));
$label = date('Y-m-01',$testDate).' to '.date('Y-m-t',$testDate);
$key = date('Y-m-01',$testDate); // .' - '.date('Y-m-t',$testDate);
$payPeriods[$key] = array(
'start' => $start,
'end' => $end,
'label' => $label,
);
$testDate = strtotime('+1 month',$testDate);
}
ksort($payPeriods);
/*
echo "<pre>";
print_r($payPeriods);
echo "</pre>";
*/
foreach($payPeriods as $year => $months){
ksort($months);
$payPeriods[$year] = $months;
}
$payPeriods = array_slice($payPeriods,-$payPeriodCount);
$firstPeriod = array_slice($payPeriods,0,1);
$firstPeriod = array_values($firstPeriod);
$startDate = $firstPeriod[0]['start'];
// load sales reps
$q = "
SELECT qb_name FROM user_groups_users
LEFT JOIN user_groups ON user_groups_users.user_group_id = user_groups.id
LEFT JOIN reps ON reps.id = user_groups_users.user_id
WHERE user_groups.key = 'sales'
AND reps.id NOT IN ('25')
";
if(!hasPermission('admin') && !hasPermission('sales_mgr')){
$q.= "AND qb_name = '".$_SESSION['logged_in']['qb_name']."' ";
}
$res = mysql_query($q,$dbhandlesalt);
while ($sales_rep = mysql_fetch_array($res)) {
$sales_reps[] = $sales_rep['qb_name'];
}
// get all line items from invoices (except shipping) and COGS
$q = "
SELECT
invoice.TxnID, invoice.TxnDate, invoice.SalesRepRef_FullName,
invoicelinedetail.Amount, invoicelinedetail.Quantity, iteminventory.AverageCost
FROM invoice
LEFT JOIN invoicelinedetail ON invoice.TxnID = invoicelinedetail.IDKEY
LEFT JOIN iteminventory ON iteminventory.ListID = invoicelinedetail.ItemRef_ListID
LEFT JOIN txnitemlinedetail ON txnitemlinedetail.TxnLineID = invoicelinedetail.TxnLineID ";
$q.= "WHERE DATE(invoice.TxnDate) >= '".date('Y-m-01',$startDate)."' ";
if(!hasPermission('admin') && !hasPermission('sales_mgr')){
$q.= "AND invoice.SalesRepRef_FullName = '".$_SESSION['logged_in']['qb_name']."' ";
}
$q.="
AND LOWER(invoicelinedetail.ItemRef_FullName) NOT LIKE 'shipping%'
AND LOWER(invoicelinedetail.ItemRef_FullName) NOT LIKE '%avatax%'
and (invoicelinedetail.itemref_fullname not like 'Shipping%')
and (invoicelinedetail.itemref_fullname not like 'Warrant%')
and (invoicelinedetail.itemref_fullname not like 'Freight%')
and (invoicelinedetail.itemref_fullname not like 'Discount:Damaged Goods')
and (invoicelinedetail.itemref_fullname not like 'Ground')
";
// This seems to be pulling line item numbers correct - it's rev minus discounts
//echo "<p>".$q."</p>";
$res = mysql_query($q,$dbhandleqb);
if(!$res){
die(mysql_error($dbhandleqb));
}
// items
while ($item = mysql_fetch_array($res)) {
$dateKey = '';
foreach($payPeriods as $key => $per){
if(strtotime($item['TxnDate']) >= $per['start'] && strtotime($item['TxnDate']) <= $per['end']){
$dateKey = $key;
}
}
if($dateKey){
$repSplit = array();
$repSplit = explode("/",$item['SalesRepRef_FullName']);
foreach($repSplit as $repID){
if(
$repID == $_SESSION['logged_in']['qb_name']
|| hasPermission('admin')
){
$repID = trim($repID);
$amount = 0;
$discounts = 0;
if($item['Amount'] >= 0) {
$amount = $item['Amount'] ? $item['Amount'] : 0;
// echo "Rep: ".$repID." | Month of: ".$dateKey." | Transaction Date: ".$item['TxnDate']." | Line Item Amount: ".$amount."<br>";
} else {
$discounts = $item['Amount'] ? $item['Amount'] : 0;
// echo "Rep: ".$repID." | Month of: ".$dateKey." | Transaction Date: ".$item['TxnDate']." | Line Item Discount: ".$discounts."<br>";
}
$cost = $item['AverageCost'] ? $item['AverageCost'] * $item['Quantity'] : 0;
if(!in_array($repID,$sales_reps)) {
$repID = "Other";
}
if(!isset($repData[$repID]['revenue'][$dateKey])){
$repData[$repID]['revenue'][$dateKey] = 0;
}
if(!isset($repData[$repID]['discounts'][$dateKey])){
$repData[$repID]['discounts'][$dateKey] = 0;
}
if(!isset($repData[$repID]['count'][$dateKey])){
$repData[$repID]['count'][$dateKey] = 0;
}
if(!isset($repData[$repID]['cogs'][$dateKey])){
$repData[$repID]['cogs'][$dateKey] = 0;
}
if(!isset($repData[$repID]['profit'][$dateKey])){
$repData[$repID]['profit'][$dateKey] = 0;
}
$repData[$repID]['revenue'][$dateKey] += ($amount / count($repSplit));
$repData[$repID]['discounts'][$dateKey] += ($discounts / count($repSplit));
$repData[$repID]['cogs'][$dateKey] += ($cost / count($repSplit));
$repData[$repID]['profit'][$dateKey] += ($amount / count($repSplit)) - ($cost / count($repSplit));
$repData[$repID]['count'][$dateKey] += 1;
}
}
if(!isset($totals['revenue'][$dateKey])){
$totals['revenue'][$dateKey] = 0;
}
if(!isset($totals['discounts'][$dateKey])){
$totals['discounts'][$dateKey] = 0;
}
if(!isset($totals['count'][$dateKey])){
$totals['count'][$dateKey] = 0;
}
if(!isset($totals['cogs'][$dateKey])){
$totals['cogs'][$dateKey] = 0;
}
if(!isset($totals['profit'][$dateKey])){
$totals['profit'][$dateKey] = 0;
}
$totals['revenue'][$dateKey] += ($amount / count($repSplit));
$totals['discounts'][$dateKey] += ($discounts / count($repSplit));
$totals['cogs'][$dateKey] += ($cost / count($repSplit));
$totals['profit'][$dateKey] += ($amount / count($repSplit)) - ($cost / count($repSplit));
$totals['count'][$dateKey] += 1;
$total_rev += $amount;
$total_count += 1;
}
}
$q = "
SELECT
creditmemo.TxnID, creditmemo.TxnDate, creditmemo.SalesRepRef_FullName,
creditmemolinedetail.Amount, creditmemolinedetail.Quantity, iteminventory.AverageCost
FROM creditmemo
LEFT JOIN creditmemolinedetail ON creditmemo.TxnID = creditmemolinedetail.IDKEY
LEFT JOIN iteminventory ON iteminventory.ListID = creditmemolinedetail.ItemRef_ListID ";
$q.= "WHERE DATE(creditmemo.TxnDate) >= '".date('Y-m-01',$startDate)."' ";
if(!hasPermission('admin') && !hasPermission('sales_mgr')){
$q.= "AND creditmemo.SalesRepRef_FullName = '".$_SESSION['logged_in']['qb_name']."' ";
}
$q.="
AND LOWER(creditmemolinedetail.ItemRef_FullName) LIKE 'discount%'
";
// This seems to be pulling line item numbers correct - it's rev minus discounts
//echo "<p>".$q."</p>";
$res = mysql_query($q,$dbhandleqb);
if(!$res){
die(mysql_error($dbhandleqb));
}
// items
while ($item = mysql_fetch_array($res)) {
$dateKey = '';
foreach($payPeriods as $key => $per){
if(strtotime($item['TxnDate']) >= $per['start'] && strtotime($item['TxnDate']) <= $per['end']){
$dateKey = $key;
}
}
if($dateKey){
$repSplit = array();
$repSplit = explode("/",$item['SalesRepRef_FullName']);
foreach($repSplit as $repID){
if(
$repID == $_SESSION['logged_in']['qb_name']
|| hasPermission('admin')
){
$repID = trim($repID);
$amount = 0;
$discounts = 0;
if($item['Amount'] >= 0) {
$amount = $item['Amount'] ? $item['Amount'] : 0;
// echo "Rep: ".$repID." | Month of: ".$dateKey." | Transaction Date: ".$item['TxnDate']." | Line Item Amount: ".$amount."<br>";
} else {
$discounts = ($item['Amount'] ? $item['Amount'] : 0)*-1;
// echo "Rep: ".$repID." | Month of: ".$dateKey." | Transaction Date: ".$item['TxnDate']." | Negative credit memo Discount: ".$discounts."<br>";
}
$cost = $item['AverageCost'] ? $item['AverageCost'] * $item['Quantity'] : 0;
if(!in_array($repID,$sales_reps)) {
$repID = "Other";
}
if(!isset($repData[$repID]['revenue'][$dateKey])){
$repData[$repID]['revenue'][$dateKey] = 0;
}
if(!isset($repData[$repID]['discounts'][$dateKey])){
$repData[$repID]['discounts'][$dateKey] = 0;
}
if(!isset($repData[$repID]['count'][$dateKey])){
$repData[$repID]['count'][$dateKey] = 0;
}
if(!isset($repData[$repID]['cogs'][$dateKey])){
$repData[$repID]['cogs'][$dateKey] = 0;
}
if(!isset($repData[$repID]['profit'][$dateKey])){
$repData[$repID]['profit'][$dateKey] = 0;
}
$repData[$repID]['revenue'][$dateKey] += ($amount / count($repSplit));
$repData[$repID]['discounts'][$dateKey] += ($discounts / count($repSplit));
$repData[$repID]['cogs'][$dateKey] += ($cost / count($repSplit));
$repData[$repID]['profit'][$dateKey] += ($amount / count($repSplit)) - ($cost / count($repSplit));
$repData[$repID]['count'][$dateKey] += 1;
}
}
if(!isset($totals['revenue'][$dateKey])){
$totals['revenue'][$dateKey] = 0;
}
if(!isset($totals['discounts'][$dateKey])){
$totals['discounts'][$dateKey] = 0;
}
if(!isset($totals['count'][$dateKey])){
$totals['count'][$dateKey] = 0;
}
if(!isset($totals['cogs'][$dateKey])){
$totals['cogs'][$dateKey] = 0;
}
if(!isset($totals['profit'][$dateKey])){
$totals['profit'][$dateKey] = 0;
}
$totals['revenue'][$dateKey] += ($amount / count($repSplit));
$totals['discounts'][$dateKey] += ($discounts / count($repSplit));
$totals['cogs'][$dateKey] += ($cost / count($repSplit));
$totals['profit'][$dateKey] += ($amount / count($repSplit)) - ($cost / count($repSplit));
$totals['count'][$dateKey] += 1;
$total_rev += $amount;
$total_count += 1;
}
}
$q = "
SELECT
creditmemo.TxnID, creditmemo.TxnDate, creditmemo.SalesRepRef_FullName,
creditmemolinedetail.Amount, creditmemolinedetail.Quantity, iteminventory.AverageCost
FROM creditmemo
LEFT JOIN creditmemolinedetail ON creditmemo.TxnID = creditmemolinedetail.IDKEY
LEFT JOIN iteminventory ON iteminventory.ListID = creditmemolinedetail.ItemRef_ListID ";
$q.= "WHERE DATE(creditmemo.TxnDate) >= '".date('Y-m-01',$startDate)."' ";
if(!hasPermission('admin') && !hasPermission('sales_mgr')){
$q.= "AND creditmemo.SalesRepRef_FullName = '".$_SESSION['logged_in']['qb_name']."' ";
}
$q.="
AND LOWER(creditmemolinedetail.ItemRef_FullName) LIKE 'customer%'
";
// This seems to be pulling line item numbers correct - it's rev minus discounts
//echo "<p>".$q."</p>";
$res = mysql_query($q,$dbhandleqb);
if(!$res){
die(mysql_error($dbhandleqb));
}
// items
while ($item = mysql_fetch_array($res)) {
$dateKey = '';
foreach($payPeriods as $key => $per){
if(strtotime($item['TxnDate']) >= $per['start'] && strtotime($item['TxnDate']) <= $per['end']){
$dateKey = $key;
}
}
if($dateKey){
$repSplit = array();
$repSplit = explode("/",$item['SalesRepRef_FullName']);
foreach($repSplit as $repID){
if(
$repID == $_SESSION['logged_in']['qb_name']
|| hasPermission('admin')
){
$repID = trim($repID);
$amount = 0;
$discounts = 0;
if($item['Amount'] >= 0) {
$discounts = ($item['Amount'] ? $item['Amount'] : 0)*-1;
// echo "Rep: ".$repID." | Month of: ".$dateKey." | Transaction Date: ".$item['TxnDate']." | Line Item Amount: ".$amount."<br>";
} else {
$discounts = $item['Amount'] ? $item['Amount'] : 0;
// echo "Rep: ".$repID." | Month of: ".$dateKey." | Transaction Date: ".$item['TxnDate']." | Negative credit memo Discount: ".$discounts."<br>";
}
$cost = $item['AverageCost'] ? $item['AverageCost'] * $item['Quantity'] : 0;
if(!in_array($repID,$sales_reps)) {
$repID = "Other";
}
if(!isset($repData[$repID]['revenue'][$dateKey])){
$repData[$repID]['revenue'][$dateKey] = 0;
}
if(!isset($repData[$repID]['discounts'][$dateKey])){
$repData[$repID]['discounts'][$dateKey] = 0;
}
if(!isset($repData[$repID]['count'][$dateKey])){
$repData[$repID]['count'][$dateKey] = 0;
}
if(!isset($repData[$repID]['cogs'][$dateKey])){
$repData[$repID]['cogs'][$dateKey] = 0;
}
if(!isset($repData[$repID]['profit'][$dateKey])){
$repData[$repID]['profit'][$dateKey] = 0;
}
$repData[$repID]['revenue'][$dateKey] += ($amount / count($repSplit));
$repData[$repID]['discounts'][$dateKey] += ($discounts / count($repSplit));
$repData[$repID]['cogs'][$dateKey] += ($cost / count($repSplit));
$repData[$repID]['profit'][$dateKey] += ($amount / count($repSplit)) - ($cost / count($repSplit));
$repData[$repID]['count'][$dateKey] += 1;
}
}
if(!isset($totals['revenue'][$dateKey])){
$totals['revenue'][$dateKey] = 0;
}
if(!isset($totals['discounts'][$dateKey])){
$totals['discounts'][$dateKey] = 0;
}
if(!isset($totals['count'][$dateKey])){
$totals['count'][$dateKey] = 0;
}
if(!isset($totals['cogs'][$dateKey])){
$totals['cogs'][$dateKey] = 0;
}
if(!isset($totals['profit'][$dateKey])){
$totals['profit'][$dateKey] = 0;
}
$totals['revenue'][$dateKey] += ($amount / count($repSplit));
$totals['discounts'][$dateKey] += ($discounts / count($repSplit));
$totals['cogs'][$dateKey] += ($cost / count($repSplit));
$totals['profit'][$dateKey] += ($amount / count($repSplit)) - ($cost / count($repSplit));
$totals['count'][$dateKey] += 1;
$total_rev += $amount;
$total_count += 1;
}
}
// CREDIT MEMOS - subtracts the total credit memos for each month
// if(!$use_test_day) {
$q = "
SELECT
creditmemo.TxnID, creditmemo.TxnDate, creditmemo.SalesRepRef_FullName,
creditmemolinedetail.Amount, creditmemolinedetail.Quantity, iteminventory.AverageCost
FROM creditmemo
LEFT JOIN creditmemolinedetail ON creditmemo.TxnID = creditmemolinedetail.IDKEY
LEFT JOIN iteminventory ON iteminventory.ListID = creditmemolinedetail.ItemRef_ListID
WHERE DATE(creditmemo.TxnDate) >= '".date('Y-m-01',$startDate)."'
AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE 'shipping%'
AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE '%avatax%'
AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE 'fraudulent%'
AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE 'discount%'
AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE 'warrant%'
AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE 'restock%'
AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE 'ground'
and creditmemolinedetail.description not in ('Shipping','Customer Satisfaction','Discount')
";
// } else {
// $q = "
// SELECT
// creditmemo.TxnID, creditmemo.TxnDate, creditmemo.SalesRepRef_FullName,
// creditmemolinedetail.Amount, creditmemolinedetail.Quantity, iteminventory.AverageCost
// FROM creditmemo
// LEFT JOIN creditmemolinedetail ON creditmemo.TxnID = creditmemolinedetail.IDKEY
// LEFT JOIN iteminventory ON iteminventory.ListID = creditmemolinedetail.ItemRef_ListID
// WHERE DATE(creditmemo.TxnDate) = '".$test_day."'
// AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE 'shipping%'
// AND LOWER(creditmemolinedetail.ItemRef_FullName) NOT LIKE '%avatax%'
//
// ";
//
//
//
// }
if(!hasPermission('admin') && !hasPermission('sales_mgr')){
$q.= "AND creditmemo.SalesRepRef_FullName = '".$_SESSION['logged_in']['qb_name']."' ";
}
// echo "<hr><p>".$q."</p><hr>";
$res = mysql_query($q,$dbhandleqb);
if(!$res){
die(mysql_error($dbhandleqb));
}
while ($row = mysql_fetch_array($res)) {
$dateKey = '';
foreach($payPeriods as $key => $per){
if(strtotime($row['TxnDate']) >= $per['start'] && strtotime($row['TxnDate']) <= $per['end']){
$dateKey = $key;
}
}
// echo "Key: ".$dateKey."<hr>";
// print_r($row);
// echo "<hr>";
if($dateKey){
$amount = $row['Amount'] ? $row['Amount'] : 0;
$cost = $row['AverageCost'] ? $row['AverageCost'] : 0;
$qty = $row['Quantity'] ? $row['Quantity'] : 0;
$cost = $cost * $qty;
$repSplit = array();
$repSplit = explode("/",$row['SalesRepRef_FullName']);
foreach($repSplit as $repID){
$repID = trim($repID);
if(
$repID == $_SESSION['logged_in']['qb_name']
|| hasPermission('admin')
){
if(!in_array($repID,$sales_reps)) {
$repID = "Other";
}
if(!isset($repData[$repID]['revenue'][$dateKey])){
$repData[$repID]['revenue'][$dateKey] = 0;
}
if(!isset($repData[$repID]['cogs'][$dateKey])){
// $repData[$repID]['revenue'][$dateKey] = 0;
$repData[$repID]['cogs'][$dateKey] = 0;
}
if(!isset($repData[$repID]['discounts'][$dateKey])){
$repData[$repID]['discounts'][$dateKey] = 0;
}
if(!isset($repData[$repID]['profit'][$dateKey])){
$repData[$repID]['profit'][$dateKey] = 0;
}
if(!isset($repData[$repID]['credit_memo_amt'][$dateKey])){
$repData[$repID]['credit_memo_amt'][$dateKey] = 0;
}
if(!isset($repData[$repID]['credit_memo_cost'][$dateKey])){
$repData[$repID]['credit_memo_cost'][$dateKey] = 0;
}
// $repData[$repID]['revenue'][$dateKey] -= ($amount / count($repSplit));
// $repData[$repID]['cogs'][$dateKey] -= ($cost / count($repSplit));
$repData[$repID]['credit_memo_amt'][$dateKey] += ($amount / count($repSplit));
$repData[$repID]['credit_memo_cost'][$dateKey] += ($cost / count($repSplit));
// MIKE NOTE:
// echo "CM: | ".$item['TxnDate']." | ".$dateKey." | ".$amount." | ".$cost."<br>";
// $repData[$repID]['profit'][$dateKey] -= ($amount / count($repSplit));
// $repData[$repID]['profit'][$dateKey] += ($cost / count($repSplit));
}
}
if(!isset($totals['revenue'][$dateKey])){
$totals['revenue'][$dateKey] = 0;
}
if(!isset($totals['creditmemos'][$dateKey])){
$totals['creditmemos'][$dateKey] = 0;
}
$totals['revenue'][$dateKey] -= $amount;
$totals['creditmemos'][$dateKey] += $amount;
}
}
ksort($repData);
/*
echo "<pre>";
print_r($repData);
echo "</pre>";
exit;
*/
$revenue_tiers = array();
$revenue_tiers['1']['min'] = 0;
$revenue_tiers['1']['base'] = 2000;
$revenue_tiers['1']['commission_percentage'] = 5;
$revenue_tiers['2']['min'] = 10000;
$revenue_tiers['2']['base'] = 2250;
$revenue_tiers['2']['commission_percentage'] = 5;
$revenue_tiers['3']['min'] = 20000;
$revenue_tiers['3']['base'] = 2500;
$revenue_tiers['3']['commission_percentage'] = 5;
$revenue_tiers['4']['min'] = 30000;
$revenue_tiers['4']['base'] = 2750;
$revenue_tiers['4']['commission_percentage'] = 5;
$revenue_tiers['5']['min'] = 40000;
$revenue_tiers['5']['base'] = 3000;
$revenue_tiers['5']['commission_percentage'] = 5;
$revenue_tiers['6']['min'] = 50000;
$revenue_tiers['6']['base'] = 3250;
$revenue_tiers['6']['commission_percentage'] = 5;
$revenue_tiers['7']['min'] = 60000;
$revenue_tiers['7']['base'] = 3500;
$revenue_tiers['7']['commission_percentage'] = 5;
?>
<div id="control-bar" class="grey-bg clearfix" style="opacity: 1; ">
<div class="container_12">
<div class="float-left">
<a href="/reports.php"><button type="button"><img src="/images/icons/fugue/navigation-180.png" width="16" height="16"> View All Reports</button></a></div>
</div>
</div>
<article class="container_12">
<section class="grid_12">
<?php
echo '<div class="no-margin">
<table class="table" cellspacing="0" width="100%" >
<thead>
<tr>
<th>Rep</th>';
foreach ($payPeriods as $key => $per) {
echo "<th>".$per['label']."</th>";
}
echo " <th>Total</th>
<tr>
</thead>";
foreach ($repData as $rep => $repdata) {
echo "<tr><td style='border-bottom:1px solid black;'><strong>".$rep."</strong></td>";
$q2 = "SELECT healthcare_cost FROM reps WHERE qb_name = '".$rep."'";
$r2 = mysql_query($q2,$dbhandlesalt);
while ($sales_rep = mysql_fetch_array($r2)) {
$healthcare_cost = $sales_rep['healthcare_cost'];
}
foreach ($payPeriods as $key => $per) {
$rev = 0;
$cogs = 0;
$profit = 0;
$discounts = 0;
$credit_memos = 0;
if(isset($repdata['revenue'][$key])){
$rev = $repdata['revenue'][$key];
}
if(isset($repdata['credit_memo_amt'][$key])){
$credit_memos = $repdata['credit_memo_amt'][$key];
}
if(isset($repdata['discounts'][$key])){
$discounts = ($repdata['discounts'][$key]);
}
if(isset($repdata['cogs'][$key])){
$cogs = $repdata['cogs'][$key] - $repdata['credit_memo_cost'][$key];
}
if(isset($repdata['profit'][$key])){
// $profit = $repdata['profit'][$key];
$profit = $rev - $credit_memos + $discounts - $cogs;
}
echo "<td style='border-bottom:1px solid black;'>";
if($repdata['revenue'][$key] !== 0) {
echo "
<table style='width:100%'>
<tr>
<td><div style='text-align:left; width:135px; display:inline-block;'>Total Sales (incl. credit memos): </div></td>
<td><div style='float:right; text-align: right; width:108px; display:inline-block;'>$".number_format(($rev-$credit_memos), 2, '.', ',')."</div></td></table>";
} else {
echo "
<table>
<tr>
<td><div style='text-align:left; width:135px; display:inline-block;'>Total Sales (incl. credit memos): </div></td>
<td><div style='float:right; text-align: right; width:108px; display:inline-block;'>--</div></td></table>";
}
// if($repdata['credit_memo_amt'][$key] !== 0) {
// echo "
// <table style='width:100%'>
// <tr>
// <td><div style='text-align:left; width:135px; display:inline-block;'>Credit Memos: </div></td>
// <td><div style='float:right; text-align: right; width:108px; display:inline-block;'>$".number_format($credit_memos, 2, '.', ',')."</div></td></table>";
//
// } else {
// echo "
// <table>
// <tr>
// <td><div style='text-align:left; width:135px; display:inline-block; color:#f00;'>Credit Memos: </div></td>
// <td><div style='float:right; text-align: right; width:108px; display:inline-block; color:#f00;'>--</div></td></table>";
// }
if($discounts !== 0) {
echo "
<table style='width:100%;'>
<tr>
<td><div style='text-align:left; color:#f00; width:135px; display:inline-block; '>Total Sales Discounts:</div></td>
<td><div style='float:right; text-align: right; width:108px; display:inline-block; color:#f00;'>$".number_format($discounts*-1, 2, '.', ',')."</div></td></table>";
} else {
echo "
<table style='width:100%;'>
<tr>
<td><div style='text-align:left; color:#f00; width:135px; display:inline-block; '>Total Sales Discounts:</div></td>
<td><div style='float:right; text-align: right; width:108px; display:inline-block; color:#f00;'>--</div></td></table>";
}
if($repdata['revenue'][$key] !== 0) {
echo "
<table style='width:100%;'>
<tr style='border-bottom: 1px solid black;'>
<td><div style='text-align:left; width:135px; display:inline-block;'>Total Income: </div></td>
<td><div style='float:right; text-align: right; width:108px; display:inline-block;'>$".number_format(($rev-$credit_memos+$discounts), 2, '.', ',')."</div></td></table>";
} else {
echo "
<table>
<tr style='border-bottom: 1px solid black;'>
<td><div style='text-align:left; width:135px; display:inline-block;'>Total Income: </div></td>
<td><div style='float:right; text-align: right; width:108px; display:inline-block;'>--</div></td></table>";
}
if($repdata['revenue'][$key] !== 0) {
echo "
<table style='width:100%'>
<tr>
<td><div style='text-align:left; width:135px; display:inline-block;'><strong>Net Income: </strong></div></td>
<td><div style='float:right; text-align: right; width:108px; display:inline-block;'><strong>$".number_format($profit, 2, '.', ',')."</strong></div></td></table>";
} else {
echo "
<table>
<tr>
<td><div style='text-align:left; width:135px; display:inline-block;'><strong>Net Income: </strong></div></td>
<td><div style='float:right; text-align: right; width:108px; display:inline-block;'><strong>--</strong></div></td></table>";
}
if($profit < 10000) {
$tier = 1;
} elseif($profit < 20000) {
$tier = 2;
} elseif($profit < 30000) {
$tier = 3;
} elseif($profit < 40000) {
$tier = 4;
} elseif($profit < 50000) {
$tier = 5;
} elseif($profit < 60000) {
$tier = 6;
} else {
$tier = 7;
}
$base_salary = $revenue_tiers[$tier]['base'];
$holdback = (2.5 * $base_salary) + $healthcare_cost*2.5;
$commission = $profit*.05;
if($commission < 0) { $commission = 0; }
$paycheck_first = 1000;
$paycheck_fifteenth = $base_salary - $paycheck_first + $commission;
$total_monthly_income = $paycheck_first + $paycheck_fifteenth;
if($repdata['revenue'][$key] < 1) {
$next_tier_revenue_estimate = "n/a";
} elseif($tier < 7) {
$next_tier_revenue_estimate = "only $".number_format(($revenue_tiers[$tier+1]['min']-$profit)*$repdata['revenue'][$key]/$profit)."!";
} else {
$next_tier_revenue_estimate = "Already top tier!";
}
// CALCULATE HOW MUCH MORE THEY'LL MAKE IF THEY HIT THE NEXT TIER
if($profit >= 60000) {
$next_tier_monthly_income = "n/a";
} else {
$next_tier_monthly_income = $revenue_tiers[$tier+1]['base'] + ($revenue_tiers[$tier+1]['min'] - $revenue_tiers[$tier+1]['base']*2.5 - $healthcare_cost*2.5)*$revenue_tiers[$tier+1]['commission_percentage']/100;
}
// END OF NEXT TIER
echo "<table style='width:100%'><tr><td><div style='text-align:left; width:135px; display:inline-block;'>
Commission Rate:
</div></td><td><div style='float:right; text-align: right; width:108px; display:inline-block;'>5%
</div></td></table>";
echo "<table style='width:100%'><tr><td><div style='text-align:left; width:135px; display:inline-block;'><strong>
Total Commission:</strong>
</div></td><td><div style='float:right; text-align: right; width:108px; display:inline-block;'><strong>$"
.number_format($commission)."
</strong></div></td></table>";
}
echo "<td style='border-bottom: 1px solid black;'><strong>$".number_format(array_sum($repdata['revenue']))."</strong></td></tr>";
}
if(hasPermission('admin')){
echo "<tr><td><strong>TOTAL REVENUE</strong></td>";
foreach ($payPeriods as $key => $per) {
echo "<td align='center'><strong>$".number_format($totals['revenue'][$key], 0, '.', ',')."</strong></td>";
}
echo "<td align='center'><strong>$".number_format(array_sum($totals['revenue']))."</strong></td></tr>";
echo "</tr>";
}
echo '</table>';
include('includes/row_count.php');
echo '<div class="block-footer clearfix">
<div class="float-left">
</div>
</div>';
echo '</div></div></form></div></section>';
?>
<? /*
<section class="grid_12">
<div class="block-border">
<div class="block-content form">
<h1>Rev Report</h1>
<div id="graph_div" style="width: 100%; height: 700px;"></div>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Month', '<?php
$new_rep_array = array();
foreach ($repData as $rep => $repdata) {
$new_rep_array[] = $rep;
}
echo implode("', '",$new_rep_array); ?>',' test'],
<?php
foreach ($payPeriods as $key => $per) {
if($month != " Old") {
echo "['".$per['label']."', ";
foreach($new_rep_array as $rep){
if($repData[$rep]['revenue'][$key]) {
echo number_format($repData[$rep]['revenue'][$key],0, '.', '').",";
} else {
echo "0,";
}
}
echo "0],\n";
}
}
?>
]);
var options = {
title: 'Rep Commission Report'
};
var chart = new google.visualization.ColumnChart(document.getElementById('graph_div'));
chart.draw(data, options);
}
<?php // ColumnChart, PieChart, LineChart ?>
</script>
</div><!-- /.block-content -->
</div><!-- /.block-border -->
</section><!-- /section.grid_12 -->
*/ ?>
<div class="clear"></div>
<?php /*
<pre><?php print_r($repData); ?></pre>
*/ ?>
<section>
<?php
/*
echo "<pre>";
print_r($repData);
echo "</pre>";
echo "<pre>";
print_r($totals);
echo "</pre>";
*/
?>
</section>
<?php
include('includes/footer.php');
//close the connection
mysql_close($dbhandlelive);
?>
Function Calls
None |
Stats
MD5 | 1d783ffebe65195d346deb88801b0560 |
Eval Count | 0 |
Decode Time | 145 ms |