HEX
Server: LiteSpeed
System: Linux lp015.web24.net.au 2.6.32-954.3.5.lve1.4.93.el6.x86_64 #1 SMP Wed Oct 4 17:04:29 UTC 2023 x86_64
User: pgkdistr (10190)
PHP: 8.1.32
Disabled: opcache_get_status
Upload Files
File: /var/www/vhosts/pgkdistribution.com.au/citisolar.com.au/thedon0915/report/display.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">

<?php 
	include 'db.php';
?>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" name="viewport">
<meta content="true" name="HandheldFriendly">
<title>The Don Sales Results</title>
<LINK href="thedon.css" rel="stylesheet" type="text/css">
</head>
<body id="page3">
<?php

				function aasort (&$array, $key) {
					$sorter=array();
					$ret=array();
					reset($array);
					foreach ($array as $ii => $va) {
						$sorter[$ii]=$va[$key];
					}
					asort($sorter);
					foreach ($sorter as $ii => $va) {
						$ret[$ii]=$array[$ii];
					}
					$array=$ret;
				}

function outorders ($from, $to, $dep) {
		echo "<table>";
		$result = mysql_query("SELECT us.last_name AS us_last, ld.last_name AS ld_last, app_date_c, sys_size_c, num_stcs_c, stc_val_c, inverter_list_c, sale_price_c, us.first_name AS us_first
			FROM leads AS ld, leads_cstm AS cs, users AS us 
			WHERE ld.assigned_user_id = us.id AND ld.id = cs.id_c AND cs.branch_c = '$dep' AND (ld.status = 'Converted' OR ld.status = 'Sold') AND cs.app_date_c >= '$from' AND cs.app_date_c <= '$to' AND contract_in_c = '0' AND ld.deleted = '0'
			ORDER BY app_date_c ASC");
		
		echo "<thead><tr>";
		echo "<th scope='col'>Date</th>";
		echo "<th scope='col'>Sales Rep</th>";
		echo "<th scope='col'>Name</th>";
		echo "<th scope='col'>System Size</th>";
		echo "<th scope='col'>Inverter</th>";
		echo "<th scope='col'>Sale Price</th>";
		echo "</tr></thead>";
		
		$totalNum = 0;
		$totalVal = 0;
		
		while ($row = mysql_fetch_array($result)) {
			if ($totalNum % 2 == 0) {
				echo "<tr>";
			} else {
				echo "<tr class='odd'>";
			}
			echo "<td>".$row{'app_date_c'}."</td>";
			echo "<td>".$row{'us_first'}." ".$row{'us_last'}."</td>";
			echo "<td>".$row{'ld_last'}."</td>";
			echo "<td>".$row{'sys_size_c'}."</td>";
			echo "<td>".$row{'inverter_list_c'}."</td>";
			$salePrice = $row{'sale_price_c'} + $row{'num_stcs_c'} * $row{'stc_val_c'};
			echo "<td>".round($salePrice)."</td>";
			echo "</tr>";
			$totalNum++;
			$totalVal += $salePrice;
		}
		
		echo "</table>";
		echo "<h4>Total Orders: $totalNum</br>Total Value: \$".round($totalVal)."</h4>";
		return array ($totalNum, round($totalVal));
}

if (isset($_GET["reptype"])) {
	$reptype = $_GET["reptype"];
	$dep = $_GET["department"];
	
	// Check to see if the date from field has been populated
	if (isset($_GET["q7_dateFrom"])) {
		$from = $_GET["q7_dateFrom"];
		$from = $from['year']."-".$from['month']."-".$from['day'];
	} else {
		$from = "";
	}
	
	// Check to see if the date to field has been populated
	if (isset($_GET["q8_dateTo8"])) {
		$to = $_GET["q8_dateTo8"];
		$to = $to['year']."-".$to['month']."-".$to['day'];
	} else {
		$to = "";
	}
	
	$employee = $_GET["employee"];
	
	// Display the report that is being generated
	echo "<h2>$reptype: $from - $to</h2>";
	
	// Display the report
	if ($reptype == 'Sales Results') {
		echo "<table>";
		$result = mysql_query("SELECT us.last_name AS us_last, ld.last_name AS ld_last, app_date_c, sys_size_c, num_stcs_c, stc_val_c, inverter_list_c, order_can_c, sale_price_c, us.first_name AS us_first
			FROM leads AS ld, leads_cstm AS cs, users AS us 
			WHERE ld.assigned_user_id = us.id AND ld.id = cs.id_c AND cs.branch_c = '$dep' AND (ld.status = 'Converted' OR ld.status = 'Sold') AND cs.app_date_c >= '$from' AND cs.app_date_c <= '$to'  AND ld.deleted = '0'
			ORDER BY cs.app_date_c ASC");
		
		echo "<thead><tr>";
		echo "<th scope='col'>Date</th>";
		echo "<th scope='col'>Sales Rep</th>";
		echo "<th scope='col'>Name</th>";
		echo "<th scope='col'>System Size</th>";
		echo "<th scope='col'>Inverter</th>";
		echo "<th scope='col'>Sale Price</th>";
		echo "<th scope='col'>Cancelled</th>";
		echo "</tr></thead>";
		
		$totalNum = 0;
		$totalVal = 0;
		
		while ($row = mysql_fetch_array($result)) {
			if ($totalNum % 2 == 0) {
				echo "<tr>";
			} else {
				echo "<tr class='odd'>";
			}
			echo "<td>".$row{'app_date_c'}."</td>";
			echo "<td>".$row{'us_first'}." ".$row{'us_last'}."</td>";
			echo "<td>".$row{'ld_last'}."</td>";
			echo "<td>".$row{'sys_size_c'}."</td>";
			echo "<td>".$row{'inverter_list_c'}."</td>";
			$salePrice = $row{'sale_price_c'} + $row{'num_stcs_c'} * $row{'stc_val_c'};
			echo "<td>".round($salePrice)."</td>";
			echo "<td>";
			if ($row{'order_can_c'} == 1) {
				echo "Yes";
			}
			echo "</td>";
			echo "</tr>";
			$totalNum++;
			$totalVal += $salePrice;
		}
		
		echo "</table>";
		echo "<h4>Total Sales: $totalNum</br>Total Value: \$".round($totalVal)."</h4>";
	} else if ($reptype == 'Daily Lead Results') {
		echo "<table>";
		
		$result = mysql_query("SELECT  ld.last_name AS ld_last, app_date_c, sys_size_c, num_stcs_c, stc_val_c, inverter_list_c, sale_price_c, app_comm_c, app_time_c, ld.status, display_of_c, cancel_note_c,
			us.first_name AS us_first, us.last_name AS us_last, us1.first_name AS us1_first, us1.last_name AS us1_last, us2.first_name AS us2_first, us2.last_name AS us2_last
			FROM (leads ld 
				LEFT JOIN users us ON ld.assigned_user_id = us.id),
			(leads_cstm AS cs
				LEFT JOIN users us1 ON cs.user_id_c = us1.id
				LEFT JOIN users us2 ON cs.user_id1_c = us2.id)
			WHERE ld.id = cs.id_c AND cs.branch_c = '$dep' AND cs.app_date_c >= '$from' AND cs.app_date_c <= '$to' AND ld.deleted = '0'
			GROUP BY ld.id
			ORDER BY cs.app_date_c, app_time_c ASC");
		
		echo "<thead><tr>";
		echo "<th scope='col'>Date</th>";
		echo "<th scope='col'>Time</th>";
		echo "<th scope='col'>Generator</th>";
		echo "<th scope='col'>Confirmed By</th>";
		echo "<th scope='col'>Sales Rep</th>";
		echo "<th scope='col'>Name</th>";
		echo "<th scope='col'>Result</th>";
		echo "<th scope='col'>System Size</th>";
		echo "<th scope='col'>Inverter</th>";
		echo "<th scope='col'>Display Offer</th>";
		echo "<th scope='col'>Sale Price</th>";
		echo "<th scope='col'>App Comments</th>";
		echo "<th scope='col'>Cancellation Notes</th>";
		echo "</tr></thead>";
		
		$totalNum = 0;
		$totalVal = 0;
		
		while ($row = mysql_fetch_array($result)) {
			if ($totalNum % 2 == 0) {
				echo "<tr>";
			} else {
				echo "<tr class='odd'>";
			}
			echo "<td>".$row{'app_date_c'}."</td>";
			echo "<td>".$row{'app_time_c'}."</td>";
			echo "<td>".$row{'us1_first'}." ".$row{'us1_last'}."</td>";
			echo "<td>".$row{'us2_first'}." ".$row{'us2_last'}."</td>";
			echo "<td>".$row{'us_first'}." ".$row{'us_last'}."</td>";
			echo "<td>".$row{'ld_last'}."</td>";
			echo "<td>".$row{'status'}."</td>";
			echo "<td>".$row{'sys_size_c'}."</td>";
			echo "<td>".$row{'inverter_list_c'}."</td>";
			echo "<td>".$row{'display_of_c'}."</td>";
			$salePrice = $row{'sale_price_c'};
			if ($salePrice != 0) {
				$salePrice += $row{'num_stcs_c'} * $row{'stc_val_c'};
			}
			echo "<td>".round($salePrice)."</td>";
			echo "<td>".$row{'app_comm_c'}."</td>";
			echo "<td>".$row{'cancel_note_c'}."</td>";
			echo "</tr>";
			$totalNum++;
			$totalVal += $salePrice;
		}
		
		echo "</table>";
		echo "<h4>Total Leads: $totalNum</br>Total Value: \$".round($totalVal)."</h4>";
	} else	if ($reptype == 'Outstanding Orders') {
		outorders ($from, $to, $dep);
	} else	if ($reptype == 'Sales by Consultant') {
		echo "<table>";
		list($first, $last) = explode(" ", $employee);
		$result = mysql_query("SELECT us.last_name AS us_last, ld.last_name AS ld_last, num_stcs_c, stc_val_c, app_date_c, sys_size_c, est_comm_c, inverter_list_c, sale_price_c, order_can_c, us.first_name AS us_first
			FROM leads AS ld, leads_cstm AS cs, users AS us 
			WHERE ld.assigned_user_id = us.id AND ld.id = cs.id_c AND us.first_name = '$first' AND us.last_name = '$last' AND (ld.status = 'Converted' OR ld.status = 'Sold') AND cs.app_date_c >= '$from' AND cs.app_date_c <= '$to' AND ld.deleted = '0'
			ORDER BY app_date_c ASC");
		
		echo "<thead><tr>";
		echo "<th scope='col'>Date</th>";
		echo "<th scope='col'>Sales Rep</th>";
		echo "<th scope='col'>Name</th>";
		echo "<th scope='col'>System Size</th>";
		echo "<th scope='col'>Inverter</th>";
		echo "<th scope='col'>Sale Price</th>";
		echo "<th scope='col'>Comms</th>";
		echo "<th scope='col'>Cancelled</th>";
		echo "</tr></thead>";
		
		$totalNum = 0;
		$totalVal = 0;
		
		while ($row = mysql_fetch_array($result)) {
			if ($totalNum % 2 == 0) {
				echo "<tr>";
			} else {
				echo "<tr class='odd'>";
			}
			echo "<td>".$row{'app_date_c'}."</td>";
			echo "<td>".$row{'us_first'}." ".$row{'us_last'}."</td>";
			echo "<td>".$row{'ld_last'}."</td>";
			echo "<td>".$row{'sys_size_c'}."</td>";
			echo "<td>".$row{'inverter_list_c'}."</td>";
			$salePrice = $row{'sale_price_c'} + $row{'num_stcs_c'} * $row{'stc_val_c'};
			echo "<td>".round($salePrice)."</td>";
			echo "<td>".$row{'est_comm_c'}."</td>";
			echo "<td>";
			if ($row{'order_can_c'} == 1) {
				echo "Yes";
			}
			echo "</td>";
			echo "</tr>";
			$totalNum++;
			$totalVal += $salePrice;
		}
		
		echo "</table>";
		echo "<h4>Total Orders: $totalNum</br>Total Value: \$".round($totalVal)."</h4>";
	} else	if ($reptype == 'Lead To Sales Ratio') {
		echo "<table>";
		
		$sql = "SELECT us.last_name AS us_last, ld.last_name AS ld_last, app_res_c, sale_price_c, num_stcs_c, stc_val_c, ld.status, cs.order_can_c, us.first_name AS us_first
			FROM leads AS ld, leads_cstm AS cs, users AS us 
			WHERE ld.assigned_user_id = us.id AND ld.id = cs.id_c AND cs.branch_c = '$dep' AND (ld.status <> 'One Party RAP' AND ld.status <> 'Reschedule' AND ld.status <> 'Cancelled' AND ld.status <> 'New' AND ld.status <> 'Assigned' AND ld.status <> 'Confirmed') AND ld.deleted = '0'
			AND cs.app_date_c >= '$from' AND cs.app_date_c <= '$to'";
		
		if ($employee != '') {
			list($first, $last) = explode(" ", $employee);
			$sql = $sql."AND us.first_name = '$first' AND us.last_name = '$last'";
		}
		
		$result = mysql_query($sql);
		
		echo "<thead><tr>";
		echo "<th scope='col'>Sales Rep</th>";
		echo "<th scope='col'>Appts</th>";
		echo "<th scope='col'>Sales</th>";
		echo "<th scope='col'>Close Ratio</th>";
		echo "<th scope='col'>Quotes</th>";
		echo "<th scope='col'>No Pres</th>";
		echo "<th scope='col'>\$\$Sales</th>";
		echo "<th scope='col'>\$\$Sales/Appt</th>";
		echo "<th scope='col'>Canc</th>";
		echo "<th scope='col'>\$\$Cancelled</th>";
		echo "<th scope='col'>% Canc</th>";
		echo "<th scope='col'>\$\$NetSales</th>";
		echo "<th scope='col'>Net Close Ratio</th>";
		echo "<th scope='col'>\$\$NetSales/Appt</th>";
		echo "</tr></thead>";
		
		$totalNum = 0;
		$totalVal = 0;
		$totAppCount = 0;
		$totSaleCount = 0;
		$totCanCount = 0;
		$totNpCount = 0;
		$totQuoteCount = 0;
		$totSaleValue = 0;
		$totCanValue = 0;
		$totQuoteValue = 0;
		
		$list = array();
		
		while ($row = mysql_fetch_array($result)) {
			$name = $row{'us_first'}." ".$row{'us_last'};
			$list[$name][]= $row;
		}
		
		foreach ($list as $employee) {
			
			if ($totalNum % 2 == 0) {
				echo "<tr>";
			} else {
				echo "<tr class='odd'>";
			}
			
			$appCount = 0;
			$saleCount = 0;
			$canCount = 0;
			$npCount = 0;
			$quoteCount = 0;
			$saleValue = 0;
			$canValue = 0;
			$quoteValue = 0;
			
			foreach ($employee as $row) {
				if ($row{'status'} == 'Converted' || $row{'status'} == 'Sold') {
					$salePrice = $row{'sale_price_c'} + $row{'num_stcs_c'} * $row{'stc_val_c'};
					$saleCount++;
					$saleValue += $salePrice;
					if ($row{'order_can_c'} == '1') {
						$canCount++;
						$canValue += $salePrice;
					}
				} else if ($row{'app_res_c'} == 3) {
					$npCount++;
				} else {
					$quoteCount++;	
				}
				$appCount++;
			}
			
			echo "<td>".$row{'us_first'}." ".$row{'us_last'}."</td>";
			echo "<td>$appCount</td>";
			echo "<td>$saleCount</td>";
			
			if ($saleCount == 0) {
				echo "<td>0</td>";
			} else {
				echo "<td>1:".round($appCount/$saleCount, 2)."</td>";
			}
			
			echo "<td>$quoteCount</td>";
			echo "<td>$npCount</td>";
			echo "<td>".round($saleValue)."</td>";
			echo "<td>".round($saleValue/$appCount, 2)."</td>";
			echo "<td>$canCount</td>";
			echo "<td>".round($canValue)."</td>";
			echo "<td>".round(($canCount/$saleCount)*100, 2)."%</td>";
			$netValue = $saleValue - $canValue;
			$netSales = $saleCount - $canCount;
			echo "<td>".round($netValue)."</td>";
			
			if ($netSales == 0) {
				echo "<td>0</td>";
			} else {
				echo "<td>1:".round($appCount/$netSales, 2)."</td>";
			}

			echo "<td>".round($netValue/$appCount, 2)."</td>";
			echo "</tr>";
			
			// Accumulate the totals for each employee
			$totalNum++;
			$totAppCount += $appCount;
			$totSaleCount += $saleCount;
			$totCanCount += $canCount;
			$totNpCount += $npCount;
			$totQuoteCount += $quoteCount;
			$totSaleValue += $saleValue;
			$totCanValue += $canValue;
			$totQuoteValue += $quoteValue;
			
		}
		
		if ($totalNum % 2 == 0) {
			echo "<tr style='font-weight:bold;'>";
		} else {
			echo "<tr class='odd' style='font-weight:bold;'>";
		}
		
		echo "<td>Total</td>";
		echo "<td>$totAppCount</td>";
		echo "<td>$totSaleCount</td>";
		
		if ($totSaleCount == 0) {
			echo "<td>0</td>";
		} else {
			echo "<td>1:".round($totAppCount/$totSaleCount, 2)."</td>";
		}
		
		echo "<td>$totQuoteCount</td>";
		echo "<td>$totNpCount</td>";
		echo "<td>".round($totSaleValue)."</td>";
		echo "<td>".round($totSaleValue/$totAppCount, 2)."</td>";
		echo "<td>$totCanCount</td>";
		echo "<td>".round($totCanValue)."</td>";
		echo "<td>".round(($totCanCount/$totSaleCount)*100, 2)."%</td>";
		$netValue = $totSaleValue - $totCanValue;
		$netSales = $totSaleCount - $totCanCount;
		echo "<td>".round($netValue)."</td>";
		
		if ($netSales == 0) {
			echo "<td>0</td>";
		} else {
			echo "<td>1:".round($totAppCount/$netSales, 2)."</td>";
		}
		
		echo "<td>".round($netValue/$totAppCount, 2)."</td>";
		echo "</tr>";
		
		echo "</table>";
	} else if ($reptype == 'Sales by Generator') {
		echo "<table>";
		list($first, $last) = explode(" ", $employee);
		$result = mysql_query("SELECT us.last_name AS us_last, ld.last_name AS ld_last, num_stcs_c, stc_val_c, app_date_c, sys_size_c, inverter_list_c, sale_price_c, us.first_name AS us_first
			FROM leads AS ld, leads_cstm AS cs, users AS us 
			WHERE ld.id = cs.id_c AND cs.user_id_c = us.id AND us.first_name = '$first' AND us.last_name = '$last' AND (ld.status = 'Converted' OR ld.status = 'Sold') AND cs.app_date_c >= '$from' AND cs.app_date_c <= '$to'  AND ld.deleted = '0'
			ORDER BY app_date_c ASC");
		
		echo "<thead><tr>";
		echo "<th scope='col'>Date</th>";
		echo "<th scope='col'>Generator</th>";
		echo "<th scope='col'>Name</th>";
		echo "<th scope='col'>System Size</th>";
		echo "<th scope='col'>Inverter</th>";
		echo "<th scope='col'>Sale Price</th>";
		echo "</tr></thead>";
		
		$totalNum = 0;
		$totalVal = 0;
		
		while ($row = mysql_fetch_array($result)) {
			if ($totalNum % 2 == 0) {
				echo "<tr>";
			} else {
				echo "<tr class='odd'>";
			}
			echo "<td>".$row{'app_date_c'}."</td>";
			echo "<td>".$row{'us_first'}." ".$row{'us_last'}."</td>";
			echo "<td>".$row{'ld_last'}."</td>";
			echo "<td>".$row{'sys_size_c'}."</td>";
			echo "<td>".$row{'inverter_list_c'}."</td>";
			$salePrice = $row{'sale_price_c'} + $row{'num_stcs_c'} * $row{'stc_val_c'};
			echo "<td>".round($salePrice)."</td>";
			echo "</tr>";
			$totalNum++;
			$totalVal += $salePrice;
		}
		
		echo "</table>";
		echo "<h4>Total Orders: $totalNum</br>Total Value: \$".round($totalVal)."</h4>";
	} else	if ($reptype == 'Consultants Order Status') {
		
		$sql = "SELECT us.last_name AS us_last, us.first_name AS us_first, cs.con_date_c, cs.acc_status_c, ac.name, ac.billing_address_street, ac.billing_address_city, cs.tot_sys_size_c, cs.inverter_list_c, cs.install_date_c
			FROM accounts AS ac, users AS us, accounts_cstm AS cs 
			WHERE ac.assigned_user_id = us.id AND ac.id = cs.id_c AND cs.department_c = '$dep'
			AND cs.con_date_c >= '$from' AND cs.con_date_c <= '$to' AND ac.deleted = '0'";
		
		if ($employee != '') {
			list($first, $last) = explode(" ", $employee);
			$sql = $sql."AND us.first_name = '$first' AND us.last_name = '$last'";
		}
		
		$result = mysql_query($sql);
		
		$list = array();
		
		while ($row = mysql_fetch_array($result)) {
			$name = $row{'acc_status_c'};
			$list[$name][]= $row;
		}
		
		foreach ($list as $key => $account) {
			echo "<h3>".$key."</h3>";
			
			$totalNum = 0;
			echo "<table>";
				
			echo "<thead><tr>";
			echo "<th scope='col'>Contract Number</th>";
			echo "<th scope='col'>Contract Date</th>";
			echo "<th scope='col'>Surname</th>";
			echo "<th scope='col'>Address</th>";
			echo "<th scope='col'>Sales Rep</th>";
			echo "<th scope='col'>System Size</th>";
			echo "<th scope='col'>Inverter</th>";
			echo "<th scope='col'>Install Date</th>";
			echo "</tr></thead>";
			
			foreach ($account as $row) {
				if ($totalNum % 2 == 0) {
					echo "<tr>";
				} else {
					echo "<tr class='odd'>";
				}
				echo "<td>".$row{'name'}."</td>";
				echo "<td>".$row{'con_date_c'}."</td>";
				echo "<td>".$row{'surname_c'}."</td>";
				echo "<td>".$row{'billing_address_street'}.", ".$row{'billing_address_city'}."</td>";
				echo "<td>".$row{'us_first'}." ".$row{'us_last'}."</td>";
				echo "<td>".round($row{'tot_sys_size_c'}, 2)."</td>";
				echo "<td>".$row{'inverter_list_c'}."</td>";
				echo "<td>".$row{'install_date_c'}."</td>";
				echo "</tr>";
			}
			
			echo "</table>";
		}
        } else if ($reptype == 'Sales By Generator Ratio') {
                echo "<table>";

                $sql = "SELECT us.last_name AS us_last, ld.last_name AS ld_last, num_stcs_c, stc_val_c, app_date_c, sys_size_c, inverter_list_c, sale_price_c, us.first_name AS us_first, ld.status
                        FROM leads AS ld, leads_cstm AS cs, users AS us
                        WHERE ld.id = cs.id_c AND cs.user_id_c = us.id AND cs.app_date_c >= '$from' AND cs.app_date_c <= '$to' AND cs.branch_c = '$dep' AND (ld.status <> 'One Party RAP' AND ld.status <> 'Reschedule' AND ld.status <> 'New' AND ld.status <> 'Assigned' AND ld.status <> 'Confirmed')  AND ld.deleted = '0'";

                if ($employee != '') {
                        list($first, $last) = explode(" ", $employee);
                        $sql = $sql."AND us_first = '$first' AND us_last = '$last'";
                }

                $result = mysql_query($sql);

                echo "<thead><tr>";
                echo "<th scope='col'>Generator</th>";
                echo "<th scope='col'>Leads</th>";
                echo "<th scope='col'>Lead Canc</th>";
                echo "<th scope='col'>% Canc</th>";
                echo "<th scope='col'>Appts</th>";
                echo "<th scope='col'>Sales</th>";
                echo "<th scope='col'>Lead to Sale Ratio</th>";
                echo "<th scope='col'>Appt to Sale Ratio</th>";
                echo "<th scope='col'>\$\$Sales</th>";
                echo "<th scope='col'>\$\$Sales/Lead</th>";
                echo "<th scope='col'>\$\$Sales/Appt</th>";
                echo "</tr></thead>";

                $totalNum = 0;
                $totalVal = 0;
                $totLeadCount = 0;
                $totAppCount = 0;
                $totSaleCount = 0;
                $totCanCount = 0;
                $totQuoteCount = 0;
                $totSaleValue = 0;
                $totCanValue = 0;
                $totQuoteValue = 0;

                $list = array();

		while ($row = mysql_fetch_array($result)) {
                        $name = $row{'us_first'}." ".$row{'us_last'};
                        $list[$name][]= $row;
                }

                foreach ($list as $employee) {

                        if ($totalNum % 2 == 0) {
                                echo "<tr>";
                        } else {
                                echo "<tr class='odd'>";
                        }

                        $leadCount = 0;
                        $appCount = 0;
                        $saleCount = 0;
                        $canCount = 0;
                        $npCount = 0;
                        $saleValue = 0;
                        $canValue = 0;
                        $quoteValue = 0;

                        foreach ($employee as $row) {
                                if ($row{'status'} == 'Cancelled') {
                                        $canCount++;
                                        $canValue += $salePrice;
                                } else if ($row{'status'} == 'Converted' || $row{'status'} == 'Sold') {
                                        $salePrice = $row{'sale_price_c'} + $row{'num_stcs_c'} * $row{'stc_val_c'};
                                        $saleCount++;
                                        $saleValue += $salePrice;
                                        $appCount++;
                                } else {
                                        $appCount++;
                                }
                                $leadCount++;
                        }

                        echo "<td>".$row{'us_first'}." ".$row{'us_last'}."</td>";
                        echo "<td>$leadCount</td>";
                        echo "<td>$canCount</td>";
                        echo "<td>".round(($canCount/$leadCount)*100, 2)."%</td>";
                        echo "<td>$appCount</td>";
                        echo "<td>$saleCount</td>";

                        if ($saleCount == 0) {
                                echo "<td>0</td>";
                                echo "<td>0</td>";
                        } else {
                                echo "<td>1:".round($leadCount/$saleCount, 2)."</td>";
                                echo "<td>1:".round($appCount/$saleCount, 2)."</td>";
                        }

                        echo "<td>".round($saleValue)."</td>";
                        echo "<td>".round($saleValue/$leadCount, 2)."</td>";
                        echo "<td>".round($saleValue/$appCount, 2)."</td>";
                        echo "</tr>";

                        // Accumulate the totals for each employee
                        $totalNum++;
                        $totLeadCount += $leadCount;
                        $totAppCount += $appCount;
                        $totSaleCount += $saleCount;
                        $totCanCount += $canCount;
                        $totSaleValue += $saleValue;

                }

                if ($totalNum % 2 == 0) {
                        echo "<tr style='font-weight:bold;'>";
                } else {
                        echo "<tr class='odd' style='font-weight:bold;'>";
                }

                echo "<td>Total</td>";
                echo "<td>$totLeadCount</td>";
                echo "<td>$totCanCount</td>";
                echo "<td>".round(($totCanCount/$totLeadCount)*100, 2)."%</td>";
                echo "<td>$totAppCount</td>";
                echo "<td>$totSaleCount</td>";

                if ($totSaleCount == 0) {
                        echo "<td>0</td>";
                        echo "<td>0</td>";
                } else {
                        echo "<td>1:".round($totLeadCount/$totSaleCount, 2)."</td>";
                        echo "<td>1:".round($totAppCount/$totSaleCount, 2)."</td>";
                }

                echo "<td>".round($totSaleValue)."</td>";
                echo "<td>".round($totSaleValue/$totLeadCount, 2)."</td>";
                echo "<td>".round($totSaleValue/$totAppCount, 2)."</td>";
                echo "</tr>";

                echo "</table>";

	} else if ($reptype == 'Lead Generation') {
		echo "<table>";
		
		$sql = "SELECT us.last_name AS us_last, ld.last_name AS ld_last, num_stcs_c, stc_val_c, app_date_c, sys_size_c, inverter_list_c, sale_price_c, us.first_name AS us_first, ld.status
			FROM leads AS ld, leads_cstm AS cs, users AS us 
			WHERE ld.id = cs.id_c AND cs.user_id_c = us.id AND cs.gen_date_c >= '$from' AND cs.gen_date_c <= '$to' AND cs.branch_c = '$dep' AND ld.deleted = '0'";
		
		if ($employee != '') {
			list($first, $last) = explode(" ", $employee);
			$sql = $sql."AND us_first = '$first' AND us_last = '$last'";
		}
		
		$result = mysql_query($sql);
		
		echo "<thead><tr>";
		echo "<th scope='col'>Generator</th>";
		echo "<th scope='col'>Leads</th>";
		echo "<th scope='col'>Lead Canc</th>";
		echo "<th scope='col'>% Canc</th>";
		echo "<th scope='col'>Reschedule</th>";
		echo "<th scope='col'>% Resc</th>";
		echo "<th scope='col'>Sales</th>";
		echo "<th scope='col'>% Sales</th>";
		echo "<th scope='col'>Pending</th>";
		echo "</tr></thead>";
		
		$totalNum = 0;
		$totalVal = 0;
		$totLeadCount = 0;
		$totSaleCount = 0;
		$totRescCount = 0;
		$totCanCount = 0;
		$totPendCount = 0;
		
		$list = array();
		
		while ($row = mysql_fetch_array($result)) {
			$name = $row{'us_first'}." ".$row{'us_last'};
			$list[$name][]= $row;
		}
		
		foreach ($list as $employee) {
			
			if ($totalNum % 2 == 0) {
				echo "<tr>";
			} else {
				echo "<tr class='odd'>";
			}
			
			$leadCount = 0;
			$saleCount = 0;
			$canCount = 0;
			$rescCount = 0;
			$pendCount = 0;
			
			foreach ($employee as $row) {
				if ($row{'status'} == 'Cancelled') {
					$canCount++;
				} else if ($row{'status'} == 'Converted' || $row{'status'} == 'Sold') {
					$saleCount++;
                                } else if ($row{'status'} == 'Reschedule' || $row{'status'} == 'One Party RAP') {
					$rescCount++;	
				} else {
					$pendCount++;
				}
				$leadCount++;
			}
			
			echo "<td>".$row{'us_first'}." ".$row{'us_last'}."</td>";
			echo "<td>$leadCount</td>";
			echo "<td>$canCount</td>";
			echo "<td>".round(($canCount/$leadCount)*100, 2)."%</td>";
			echo "<td>$rescCount</td>";
                        echo "<td>".round(($rescCount/$leadCount)*100, 2)."%</td>";
			echo "<td>$saleCount</td>";
                        echo "<td>".round(($saleCount/$leadCount)*100, 2)."%</td>";
			echo "<td>$pendCount</td>";
			
			echo "</tr>";
			
			// Accumulate the totals for each employee
			$totalNum++;
			$totLeadCount += $leadCount;
			$totAppCount += $appCount;
			$totSaleCount += $saleCount;
			$totCanCount += $canCount;
			$totPendCount += $pendCount;
		}
		
		if ($totalNum % 2 == 0) {
			echo "<tr style='font-weight:bold;'>";
		} else {
			echo "<tr class='odd' style='font-weight:bold;'>";
		}
		
		echo "<td>Total</td>";
		echo "<td>$totLeadCount</td>";
		echo "<td>$totCanCount</td>";
		echo "<td>".round(($totCanCount/$totLeadCount)*100, 2)."%</td>";
                echo "<td>$totRescCount</td>";
                echo "<td>".round(($totRescCount/$totLeadCount)*100, 2)."%</td>";
                echo "<td>$totSaleCount</td>";
                echo "<td>".round(($totSaleCount/$totLeadCount)*100, 2)."%</td>";
		echo "<td>$totPendCount</td>";
		echo "</tr>";
		
		echo "</table>";
	} else	if ($reptype == 'Daily Gross Sales') {
		
		$sql = "SELECT app_date_c, num_stcs_c, stc_val_c, sale_price_c, cs.branch_c AS cs_branch
			FROM leads AS ld, leads_cstm AS cs, users AS us
			WHERE ld.assigned_user_id = us.id AND ld.id = cs.id_c 
			AND (ld.status = 'Converted' OR ld.status = 'Sold') 
			AND cs.app_date_c >= '$from' AND cs.app_date_c <= '$to' AND ld.deleted = '0'";
			
		
		if ($employee != '') {
			list($first, $last) = explode(" ", $employee);
	//		$sql = $sql."AND us.first_name = '$first' AND us.last_name = '$last'";
		}
		
		if ($dep != '') {
		//	$sql = $sql." AND cs.branch_c = '$dep'";
		}
		
		$sql = $sql."ORDER BY app_date_c ASC";
		
		$result = mysql_query($sql);
		
		$list = array();
		$branches = array();
		$totalBranch = array();
		$totalDay = array();

		
		while ($row = mysql_fetch_array($result)) {
			$date = $row{'app_date_c'};
			$branch = $row{'cs_branch'};
			$branches[] = $branch;
			$salevalue = $row{'sale_price_c'} + $row{'num_stcs_c'} * $row{'stc_val_c'};
			
			// Create the pivot table for each date and branch
			if (array_key_exists($date, $list)) {
				if (array_key_exists($branch, $list[$date])) {
					$list[$date][$branch] += $salevalue;
				} else {
					$list[$date][$branch] = $salevalue;
				}
				$totalDay[$date] += $salevalue;
			} else {
				$list[$date] = array();
				$list[$date][$branch] = $salevalue;
				$totalDay[$date] = $salevalue;
			}
			
			// Add the sales total for the branch
			if (array_key_exists($branch, $totalBranch)) {
				$totalBranch[$branch] += $salevalue;
			} else {
				$totalBranch[$branch] = $salevalue;
			}
		}

		$branches = array_unique($branches);
		natsort($branches);

		$totalNum = 0;
		echo "<table>";
		echo "<thead><tr>";
		echo "<th scope='col'>Date</th>";
		
		foreach ($branches as $branch) {
			echo "<th scope='col'>".$branch."</th>";
		}
		
		echo "<th scope='col'>Daily Total</th>";
		
		echo "</tr></thead>";
		
		foreach ($list as $key => $date) {
			if ($totalNum % 2 == 0) {
				echo "<tr>";
			} else {
				echo "<tr class='odd'>";
			}
			echo "<td>".$key."</td>";
			
			foreach ($branches as $branch) {
				$value = 0;
				if (array_key_exists($branch, $date)) {
					$value = $date[$branch];
				}
				echo "<td>".$value."</td>";
			}
			
			$value = 0;
			if (array_key_exists($key, $totalDay)) {
				$value = $totalDay[$key];
			}
			echo "<td>".$value."</td>";
			
			echo "</tr>";
			$totalNum++;
		}
		
		// The total for each branch
		$total = 0;
		echo "<tr>";
		echo "<td>Branch Total</td>";
		
		foreach ($branches as $branch) {
			$value = 0;
			if (array_key_exists($branch, $totalBranch)) {
				$value = $totalBranch[$branch];
			}
			$total += $value;
			echo "<td>".$value."</td>";
		}
		
		echo "<td>".$total."</td>";
		
		echo "</tr>";
		
		echo "</table>";
	} else	if ($reptype == 'Production Report') {
		// Outstanding Orders
		echo "<br/><h3> Outstanding Orders </h3>";
		list ($prodNum, $prodVal) = outorders ($from, $to, $dep);

		// Orders that have been received but have not been entered
		echo "<br/><h3> Orders Received and To Be Entered </h3>";
		echo "<table>";
		$result = mysql_query("SELECT us.last_name AS us_last, ld.last_name AS ld_last, app_date_c, sys_size_c, num_stcs_c, stc_val_c, inverter_list_c, sale_price_c, us.first_name AS us_first, cs.order_can_c
			FROM leads AS ld, leads_cstm AS cs, users AS us 
			WHERE ld.assigned_user_id = us.id AND ld.id = cs.id_c AND cs.branch_c = '$dep' AND (ld.status = 'Sold') AND cs.app_date_c >= '$from' AND cs.app_date_c <= '$to' AND contract_in_c = '1' AND cs.order_can_c = '0' AND ld.deleted = '0'
			ORDER BY app_date_c ASC");
		
		echo "<thead><tr>";
		echo "<th scope='col'>Date</th>";
		echo "<th scope='col'>Sales Rep</th>";
		echo "<th scope='col'>Name</th>";
		echo "<th scope='col'>System Size</th>";
		echo "<th scope='col'>Inverter</th>";
		echo "<th scope='col'>Sale Price</th>";
		echo "</tr></thead>";
		
		$totalNum = 0;
		$totalVal = 0;
		
		while ($row = mysql_fetch_array($result)) {
			if ($totalNum % 2 == 0) {
				echo "<tr>";
			} else {
				echo "<tr class='odd'>";
			}
			echo "<td>".$row{'app_date_c'}."</td>";
			echo "<td>".$row{'us_first'}." ".$row{'us_last'}."</td>";
			echo "<td>".$row{'ld_last'}."</td>";
			echo "<td>".$row{'sys_size_c'}."</td>";
			echo "<td>".$row{'inverter_list_c'}."</td>";
			$salePrice = $row{'sale_price_c'} + $row{'num_stcs_c'} * $row{'stc_val_c'};
			echo "<td>".round($salePrice)."</td>";
			echo "</tr>";
			$totalNum++;
			$totalVal += $salePrice;
		}
		
		echo "</table>";
		echo "<h4>Total Orders: $totalNum</br>Total Value: \$".round($totalVal)."</h4>";

		$prodNum += $totalNum;
		$prodVal += $totalVal;

		// Orders that are now in Production 
		$sql = "SELECT us.last_name AS us_last, us.first_name AS us_first, cs.con_date_c, cs.acc_status_c, ac.name, ac.billing_address_street, ac.billing_address_city, cs.tot_sys_size_c, cs.inverter_list_c, cs.install_date_c, cs.equip_cost_c, cs.surname_c
			FROM accounts AS ac, users AS us, accounts_cstm AS cs 
			WHERE ac.assigned_user_id = us.id AND ac.id = cs.id_c AND cs.department_c = '$dep'
			AND cs.con_date_c >= '$from' AND cs.con_date_c <= '$to' AND ac.deleted = '0'";
		
		if ($employee != '') {
			list($first, $last) = explode(" ", $employee);
			$sql = $sql."AND us.first_name = '$first' AND us.last_name = '$last'";
		}
		
		$result = mysql_query($sql);
		
		$list = array();
		
		while ($row = mysql_fetch_array($result)) {
			$name = $row{'acc_status_c'};
			$list[$name][]= $row;
		}
		
		$totSysSize = 0;

		// Set up Inverter Array

		$inverter_array = array();
		
		foreach ($list as $key => $account) {
			if ($key != 'Installed' && $key != 'Cancelled') {
				$statValue = 0;
				$statNum = 0;
				echo "<br/><h3>".$key."</h3>";
				
				aasort($account,"con_date_c");

				$totalNum = 0;
				echo "<table>";
					
				echo "<thead><tr>";
				echo "<th scope='col'>Contract Number</th>";
				echo "<th scope='col'>Contract Date</th>";
				echo "<th scope='col'>Surname</th>";
				echo "<th scope='col'>Address</th>";
				echo "<th scope='col'>Sales Rep</th>";
				echo "<th scope='col'>System Size</th>";
				echo "<th scope='col'>Inverter</th>";
				echo "<th scope='col'>Install Date</th>"; 
				echo "<th scope='col'>Value</th>";
				echo "</tr></thead>";
				
				foreach ($account as $row) {
					if ($totalNum % 2 == 0) {
						echo "<tr>";
					} else {
						echo "<tr class='odd'>";
					}
					echo "<td>".$row{'name'}."</td>";
					echo "<td>".$row{'con_date_c'}."</td>";
					echo "<td>".$row{'surname_c'}."</td>";
					echo "<td>".$row{'billing_address_street'}.", ".$row{'billing_address_city'}."</td>";
					echo "<td>".$row{'us_first'}." ".$row{'us_last'}."</td>";
					$systemSize = round($row{'tot_sys_size_c'}, 2);
					echo "<td>".$systemSize."</td>";
					$inverter = $row{'inverter_list_c'};
					echo "<td>".$inverter."</td>";
					echo "<td>".$row{'install_date_c'}."</td>";
					$value = $row{'equip_cost_c'};
					echo "<td>$".number_format($value, 0)."</td>";
					echo "</tr>";

					// Increment the required values
					$statValue += $value;
					$statNum++;
					$prodVal += $value;
					$prodNum++;
					$totSysSize += $systemSize;
					
					if (array_key_exists($inverter, $inverter_array)) {
    						$inverter_array[$inverter]++;
					} else {
						$inverter_array[$inverter] = 1;
					}
				}
				
				echo "</table>";
				echo "<h3>".$key." Number: ".$statNum.", Value: $".number_format($statValue,0)."</h3>";
			}
		}
		
		echo "<br/><h3>Total Number Outstanding Installs: ".$prodNum.", Value: $".number_format($prodVal,0)."</h3><br/>";

		echo "<br/><h2>Stock Required for Orders In Production</h2>"; 
		echo "<br/><h3>Total Watts: ".number_format($totSysSize,0)."<h3/>";

		// Display the inverters required for remaining jobs
		ksort($inverter_array);
		echo "<br/><h3>Inverters Required:</h3>";
		foreach ($inverter_array as $key => $value) {
			echo $key.": ".$value."<br/>";
		}
		
	}
} else {
	echo "<h3>No report Selected</h3>";
}
?>
</body>
</html>