<?php
header("Content-type: text/html; charset=windows-1256");

session_start();
if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}


$export = $_GET['export'];

if($export ==1){
	$filename = "Exp_SalesAnalysis" . date('Ymd') . ".xls";
	header("Content-Disposition: attachment; filename=\"$filename\"");
	header("Content-Type: application/vnd.ms-excel");
} 

if($export ==1){
}
print <<< HERE
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<TITLE> Analysis Report </TITLE>

<style type="text/css">
table.params td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }

table.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                      }				 
table.master td { font-size: 14px; 
                  font-family:Times-New-Roman;
                 }
				 
table.master1 th { font-size: 14px; 
                   font-weight: bold;
                   font-family:Times-New-Roman;
                 }				 
table.master1 td { font-size: 13px; 
				   font-weight: bold;
                   font-family:Times-New-Roman;
                 }	
				 
table.group td { font-size: 14px; 
                 font-weight: bold;
				 font-style: italic;
                 font-family:Times-New-Roman;
               }	

table.detail th { font-size: 12px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                 }				 			   
table.detail td { font-size: 12px; 
				  font-weight: normal;
                  font-family:Times-New-Roman;
                }		
table.detail td.sr  { font-size: 11px; 
					  font-style: italic; 
                      font-family:Times-New-Roman;
                    }				
				 
.totals { font-size: 12px; 
          font-weight: bold;
          font-family:Times-New-Roman;
        }					  
				 
h4 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 2px;
   }
h5 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }   
   
</style>




</HEAD>

HERE;



include("config.inc.php");

//Set Time Execution For the Report
ini_set('max_execution_time', $config['ReportTimeExecution']);

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
 
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

$querylog="SELECT print as printaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')	";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}



$querysetup="select grid_color1,grid_color2, DecimalBase, DecimalBase1, DecimalBase2, Base1, Base2, Company_Name As CompanyName from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultsetup)){
  extract($rows);
}

//Read From CashRegister Table
/*$queryCashRegister=" Select ifNull(CashRegisterTTC,0) As CashRegisterTTC ".
				   " From CashRegister ".
				   " Where CashRegister_Name = '".$_SESSION['cashregister']."' ";*/

$queryCashRegister = " Select ifNull(CashRegisterTTC,0) As CashRegisterTTC, ifNull(retailserialno,0) As RetailSerialNo  ".
					 " From CashRegister Inner Join ".$HTTP_SESSION_VARS['dbLabel'].".logindt on logindt.Cash_Register = CashRegister.CashRegister_Name ".
					 " Where logindt.User_Account = '".$HTTP_SESSION_VARS['useraccount']."' ";
$resultCashRegister=mysql_query($queryCashRegister,$connection) or die(mysql_error());
while($rowCashRegister=mysql_fetch_array($resultCashRegister)){
	extract($rowCashRegister);
}

$querylog=" select ifNull(FI_RepRestrictProject,0) As FI_RepRestrictProject, ifNull(FI_RepRestrictCostcenter,0) As FI_RepRestrictCostcenter, ".
		  " ifNull(FI_RepRestrictWarehouse,0) As FI_RepRestrictWarehouse ".
          " from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."') ";
$resultlog=mysql_query($querylog) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}


$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%') ".
			  " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
			  " and (loginstk.project_code is not null and loginstk.project_code != '') ".
			  " order by project.project_code";
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectexist  = mysql_num_rows($projectresult);

$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%') ".
			   " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);		

$querywh=" select distinct warehouse.warehouse_code, warehouse_name ".
         " from warehouse where warehouse_code in ( select warehouse_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
		 "                                          where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
		 "                                          and (loginstk.warehouse_code is not null and loginstk.warehouse_code != '') ) ".
		 " order by warehouse.warehouse_code";
$resultwh = mysql_query($querywh,$connection) or die(mysql_error());
$whexist  = mysql_num_rows($resultwh);

$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);

$RepTitle = 'Sales Analysis ';
 echo'<BODY>';
 
 
print <<< HERE
<table width="100%" border="0" align="center">
<tr><td>
<table width='800' border='0' align='left' style='table-layout:fixed'>
<tr>
  <td width='30%'><b>$CompanyName</b></td>
  <td width='50%' align='center'><font size=4><b>$RepTitle</b></font></td>
  <td width='10%'><font size=1><b> Printed Date: </b> <br><b>Time: </b>
  <td width='10%'><font size=1>
HERE;

echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
//echo DATE("d"."/"."m"."/"."Y");
echo "<br>".$dt[1]." ".$dt[2];

$dispfromdate = $fromdate;
$disptodate   = $todate;
$dispfromuser = $fromuser;
$dispcurr     = $curr;
$dispcashregister     = $fromcashreg;

$dispfromitem = $fromitem;
$disptoitem = $toitem;

if (empty($fromitem))  $fromitem = 'null';
else  $fromitem = "'".$fromitem."'";   
if (empty($toitem))  $toitem = 'null';
else  $toitem = "'".$toitem."'";      


if (empty($fromuser))  $fromuser = 'null';
else  $fromuser = "'".$fromuser."'";   
if (empty($fromcashreg))  $fromcashreg = 'null';
else  $fromcashreg = "'".$fromcashreg."'";   
if (empty($fromsalesman))  $fromsalesman = 'null';
else  $fromsalesman = "'".$fromsalesman."'";   
if (empty($fromdriver))  $fromdriver = 'null';
else  $fromdriver = "'".$fromdriver."'";   


if (empty($fromgroup))  $fromgroup = 'null';
else   $fromgroup = "'".$fromgroup."'";    
if (empty($togroup))  $togroup = 'null';
else  $togroup = "'".$togroup."'";   

if (empty($fromsubgroup))  $fromsubgroup = 'null';
else  $fromsubgroup = "'".$fromsubgroup."'";    
if (empty($tosubgroup))  $tosubgroup = 'null';
else  $tosubgroup = "'".$tosubgroup."'";      


if (empty($curr))  $curr = '';
//else  $curr = "'".$curr."'";   

if (!empty($fromdate))
   $fromdate = explodeword($fromdate);
if (!empty($todate))
   $todate = explodeword($todate);  

if (empty($fromdate))  $fromdate = 'null';
else  $fromdate = "'".$fromdate."'"; 
if (empty($todate))  $todate = 'null';
else  $todate = "'".$todate."'";

if (empty($fromcustomer))  $fromcustomer = 'null';
else  $fromcustomer = "'".$fromcustomer."'";    
if (empty($tocustomer))  $tocustomer = 'null';
else  $tocustomer = "'".$tocustomer."'";       
   
if (empty($fromwarehouse))  $fromwarehouse = 'null';
else  $fromwarehouse = "'".$fromwarehouse."'";    

if (empty($fromproject))  $fromproject = 'null';
else  $fromproject = "'".$fromproject."'";    
if (empty($toproject))  $toproject = 'null';
else  $toproject = "'".$toproject."'";       

if (empty($fromcostcent))  $fromcostcent = 'null';
else  $fromcostcent = "'".$fromcostcent."'";    
if (empty($tocostcent))  $tocostcent = 'null';
else  $tocostcent = "'".$tocostcent."'";     
   
if (empty($fromvendor))  $fromvendor = 'null';
else  $fromvendor = "'".$fromvendor."'";    
if (empty($tovendor))  $tovendor = 'null';
else  $tovendor = "'".$tovendor."'";     

if (empty($fromcredit))  $fromcredit = 'null';
else  $fromcredit = "'".$fromcredit."'";    
if (empty($tocredit))  $tocredit = 'null';
else  $tocredit = "'".$tocredit."'";  
   
if (empty($showunpost))  $showunpost = 0;
if (empty($showpending)) $showpending = 0;
if (empty($custperitem)) $custperitem = 0;


print <<<HERE
</font></td>
</tr>
<tr height='5'><td colspan=4><h5></td></tr>
</table>
</td></tr>

<tr><td>
<table class='params' width='800' border='0' align='left' style='table-layout:fixed' >
<tr>
  <td width="12%"><b>Date From/To:</b></td>
  <td width="18%">$dispfromdate - $disptodate </td>
  <td width="8%" valign='top'><b>Currency:</b></td>
  <td width="12%">$dispcurr</td>
 <td width="6%" valign='top'><b>User:</b></td>
  <td width="18%">$dispfromuser</td>
  <td width="10%" valign='top'><b>CashRegister:</b></td>
  <td width="13%">$dispcashregister</td>
</tr>

<tr height='5'><td colspan=8><h4></td></tr>
</table>
</td></tr>

<tr><td>
HERE;
 
$QueryRestrictPrj = " Inner join ( select distinct project_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
					" 			   where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
					"              and (project_code is not null and project_code != '') ".
					"			 ) Q4  ";
					
$QueryRestrictCst = " Inner join ( select distinct costcent_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
					"			   where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
					"              and (costcent_code is not null and costcent_code != '') ".
					"			 ) Q5  ";
				
				
 $sub_query6 = " Select date_format(Retail.Sale_Date,'%d/%m/%Y') Sale_Date, date_format(Retail.Sale_Date,'%M') MonthName, date_format(Retail.Sale_Date,'%Y') YearName,Retail.Currency_Code as CurrencyCode, ".
				 " Round(Sum(ifNull(Sale_TaxableBase1,0))) As  SaleTaxableBase1, Round(Sum(ifNull(Sale_TaxableBase2,0)), ".$DecimalBase2.") As  SaleTaxableBase2, ".
				 " Round(Sum(ifNull(Sale_NoneTaxableBase1,0))) As  SaleNoneTaxableBase1, Round(Sum(ifNull(Sale_NoneTaxableBase2,0)), ".$DecimalBase2.") As  SaleNoneTaxableBase2, ".
				 " Round(Sum(ifNull(Sale_DiscountBase1,0))) As  SaleDiscountBase1, Round(Sum(ifNull(Sale_DiscountBase2,0)), ".$DecimalBase2.") As  SaleDiscountBase2, ".
				 " Round(Sum(ifNull(Sale_VATBase1,0))) As  SaleVATBase1, Round(Sum(ifNull(Sale_VATBase2,0)), ".$DecimalBase2.") As  SaleVATBase2, ".
				 " Round(Sum(ifNull(Sales_CashInBase1,0)),5) As SalesCashInBase1, Round(Sum(ifNull(Sales_CashOutBase1,0)),5) As SalesCashOutBase1,  ".
				 " Round(Sum(ifNull(Sales_CashInBase2,0)),5) As SalesCashInBase2, Round(Sum(ifNull(Sales_CashOutBase2,0)),2) As SalesCashOutBase2, ".
				 " Round(Sum(ifNull(Sale_NetTotalBase1,0)),5) As NetSalesBase1, Round(Sum(ifNull(Sale_NetTotalBase2,0)),2) As NetSalesBase2, ".
				 " Sum(ifNull(Sale_CashBase1,0)) As SaleCheckBase1, Sum(ifNull(Sale_CashBase2,0)) As SaleCheckBase2, ";
		
	$sub_query6 = $sub_query6." Sum(ifNull(Sales_CreditAmountBase1,0)) As SalesCreditAmountBase1, Sum(ifNull(Sales_CreditAmountBase2,0)) As SalesCreditAmountBase2, ";
				 
	$sub_query6 = $sub_query6." Sum(ifNull(Sales_TipsBase1,0)) As SalesTipsBase1, ".
				 " Sum(ifNull(Coupon_AmountBase1,0)) As CouponAmountBase1, Sum(ifNull(Coupon_AmountBase2,0)) As CouponAmountBase2 ".
				 " ,Retail.Project_Code,Project.Project_Name".
				 " ,Retail.Costcenter_Code,Costcent.Costcent_Name".
				 " ,User_Account,CashRegister_Name".
				 " From Retail ".
				 " Left Join Costcent on Costcent.Costcent_Code = Retail.Costcenter_code ".
				 " Left Join Project on Project.Project_Code = Retail.Project_Code ";
				 
if ($projectexist > 0 && $FI_RepRestrictProject == 1)
			$sub_query6= $sub_query6.$QueryRestrictPrj." on Retail.Project_Code like concat(Q4.project_code,'%') ";
							 
		if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
			$sub_query6= $sub_query6.$QueryRestrictCst." on Retail.CostCenter_Code like concat(Q5.costcent_code,'%') ";
					
$sub_query6 = $sub_query6. " Where 1 = 1".	
				 " And Date_format(Retail.Sale_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Retail.Sale_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Retail.Sale_Date,'%Y-%m-%d'))  ";

$sub_query6 = $sub_query6. " And Retail.User_Account = ifNull(".$fromuser.", Retail.User_Account) ".
		  " And Retail.CashRegister_Name = ifNull(".$fromcashreg.", Retail.CashRegister_Name) ".  
		  " And ifNull(Retail.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(Retail.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(Retail.Project_Code,'-1')) ".
		  " And ifNull(Retail.CostCenter_Code,'-1') between ifNull(".$fromcostcent.",ifNull(Retail.CostCenter_Code,'-1')) and ifNull(". $tocostcent.",ifNull(Retail.CostCenter_Code,'-1')) ";
							
				 
if($salestype==0) {
			 
			if($groupby == 2)
				$sub_query6 = $sub_query6 . " Group By date_format(Retail.Sale_Date,'%Y%m%d'),Retail.Currency_Code,Retail.Project_Code ";	
			else
			if($groupby == 3)
				$sub_query6 = $sub_query6 . " Group By date_format(Retail.Sale_Date,'%Y%m%d'),Retail.Currency_Code,Retail.Costcenter_Code ";	
			else
			if($groupby == 4)
				$sub_query6 = $sub_query6 . " Group By date_format(Retail.Sale_Date,'%Y%m%d'),Retail.Currency_Code,Retail.User_Account ";	
			else
			if($groupby == 5)
				$sub_query6 = $sub_query6 . " Group By date_format(Retail.Sale_Date,'%Y%m%d'),Retail.Currency_Code,Retail.CashRegister_Name ";	
			 else
				$sub_query6 = $sub_query6 . " Group By date_format(Retail.Sale_Date,'%Y%m%d'),Retail.Currency_Code ";	
			 
			 
			 }
else {
			if($groupby == 2)
				$sub_query6 = $sub_query6 . " Group By date_format(Retail.Sale_Date,'%Y%m'),Retail.Currency_Code,Retail.Project_Code ";	
			else
			if($groupby == 3)
				$sub_query6 = $sub_query6 . " Group By date_format(Retail.Sale_Date,'%Y%m'),Retail.Currency_Code,Retail.Costcenter_Code ";	
			else
			if($groupby == 4)
				$sub_query6 = $sub_query6 . " Group By date_format(Retail.Sale_Date,'%Y%m'),Retail.Currency_Code,Retail.User_Account ";	
			else
			if($groupby == 5)
				$sub_query6 = $sub_query6 . " Group By date_format(Retail.Sale_Date,'%Y%m'),Retail.Currency_Code,Retail.CashRegister_Name ";	
			 else
				$sub_query6 = $sub_query6 . " Group By date_format(Retail.Sale_Date,'%Y%m'),Retail.Currency_Code ";	
			 
			 
			 }
			 
if($groupby == 2)			 
		$sub_query6 = $sub_query6 . " Order by Retail.Project_Code,Retail.Sale_Date,Retail.Sale_reference ";
else
if($groupby == 3)			 
		$sub_query6 = $sub_query6 . " Order by Retail.Costcenter_Code,Retail.Sale_Date,Retail.Sale_reference ";
else
if($groupby == 4)			 
		$sub_query6 = $sub_query6 . " Order by Retail.User_Account,Retail.Sale_Date,Retail.Sale_reference ";
else
if($groupby == 5)			 
		$sub_query6 = $sub_query6 . " Order by Retail.CashRegister_Name,Retail.Sale_Date,Retail.Sale_reference ";
else
		$sub_query6 = $sub_query6 . " Order by Retail.Sale_Date,Retail.Sale_reference ";
 echo $sub_query6;
// exit;
 
echo "<table width='800' border='0' align='left' class='master' style='table-layout:fixed'>
			  <col width=100>
			  <col width=100>
			  <col width=100>
			  <col width=100>
			  <col width=100>
			  <col width=100>
			  
			  <tr>";
			  

			echo"<th >Date</th>
				<th >Cash Sales</th>
				<th >Cheque Sales</th>
				<th >Credit Sales</th>
				<th >Gross Sales</th>
				<th >Discount</th>
				
		  	    </tr>
			    <tr height='5'><td colspan=7><h4></td></tr>";
//<th >Net Sales</th>

 $sub_result6=mysql_query($sub_query6,$connection) or die(mysql_error());
 $projectcode = '';
 $i=1;
	while($sub_row6=mysql_fetch_array($sub_result6)){
		extract($sub_row6);
		
	if($groupby	== 2){
	
		if($projectcode != $Project_Code and $i!=1 ){
			 echo "<tr>";
					echo"<td><b>Total</b></td>";
					echo"<td align=right><b>". number_format($TotalTotalCashAmountProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalTotalChecksAmountProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalTotalCreditsAmountProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalNetSalesProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalSaleDiscountProj, $DecimalBase1) ."<b></td>";
				//	echo"<td align=right><b>". number_format($TotalSaleDtTotalTTCProj, $DecimalBase1) ."<b></td>";
			 echo "</tr>";
			 
			$TotalNetSalesProj = 0;
			$TotalTotalCashAmountProj = 0;
			$TotalTotalChecksAmountProj = 0;
			$TotalTotalCreditsAmountProj = 0;
			$TotalSaleDiscountProj = 0;
			$TotalSaleDtTotalTTCProj = 0;
		}
	
		if($projectcode != $Project_Code )
				echo"<tr height=35><td><b><u>Project</u></b></td><td><u>$Project_Code - $Project_Name</u></td></tr>";
	
	}
	
if($groupby	== 3){
	
		if($costcentercode != $Costcenter_Code and $i!=1 ){
			 echo "<tr>";
					echo"<td><b>Total</b></td>";
					echo"<td align=right><b>". number_format($TotalTotalCashAmountProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalTotalChecksAmountProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalTotalCreditsAmountProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalNetSalesProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalSaleDiscountProj, $DecimalBase1) ."<b></td>";
					//echo"<td align=right><b>". number_format($TotalSaleDtTotalTTCProj, $DecimalBase1) ."<b></td>";
			 echo "</tr>";
			 
			$TotalNetSalesProj = 0;
			$TotalTotalCashAmountProj = 0;
			$TotalTotalChecksAmountProj = 0;
			$TotalTotalCreditsAmountProj = 0;
			$TotalSaleDiscountProj = 0;
			$TotalSaleDtTotalTTCProj = 0;
		}
	
		if($costcentercode != $Costcenter_Code )
				echo"<tr height=35><td><b><u>Costcenter</u></b></td><td><u>$Costcenter_Code - $Costcent_Name</u></td></tr>";
	
	}
	
if($groupby	== 4){
	
		if($user_account != $User_Account and $i!=1 ){
			 echo "<tr>";
					echo"<td><b>Total</b></td>";
					echo"<td align=right><b>". number_format($TotalTotalCashAmountProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalTotalChecksAmountProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalTotalCreditsAmountProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalNetSalesProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalSaleDiscountProj, $DecimalBase1) ."<b></td>";
				//	echo"<td align=right><b>". number_format($TotalSaleDtTotalTTCProj, $DecimalBase1) ."<b></td>";
			 echo "</tr>";
			 
			$TotalNetSalesProj = 0;
			$TotalTotalCashAmountProj = 0;
			$TotalTotalChecksAmountProj = 0;
			$TotalTotalCreditsAmountProj = 0;
			$TotalSaleDiscountProj = 0;
			$TotalSaleDtTotalTTCProj = 0;
		}
 
		if($user_account != $User_Account )
				echo"<tr height=35><td><b><u>User</u></b></td><td><u>$User_Account</u></td></tr>";
	
	}
	
if($groupby	== 5){
	
		if($cashregister_name != $CashRegister_Name and $i!=1 ){
			 echo "<tr>";
					echo"<td><b>Total</b></td>";
					echo"<td align=right><b>". number_format($TotalTotalCashAmountProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalTotalChecksAmountProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalTotalCreditsAmountProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalNetSalesProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalSaleDiscountProj, $DecimalBase1) ."<b></td>";
				//	echo"<td align=right><b>". number_format($TotalSaleDtTotalTTCProj, $DecimalBase1) ."<b></td>";
			 echo "</tr>";
			 
			$TotalNetSalesProj = 0;
			$TotalTotalCashAmountProj = 0;
			$TotalTotalChecksAmountProj = 0;
			$TotalTotalCreditsAmountProj = 0;
			$TotalSaleDiscountProj = 0;
			$TotalSaleDtTotalTTCProj = 0;
		}
 
		if($cashregister_name != $CashRegister_Name )
				echo"<tr height=35><td><b><u>CashRegister</u></b></td><td><u>$CashRegister_Name</u></td></tr>";
	
	}
	
	$TotalCashInB1  = $SalesCashInBase1;
	$TotalCashInB2  = $SalesCashInBase2;
	$TotalCashOutB1 = $SalesCashOutBase1;
	$TotalCashOutB2 = $SalesCashOutBase2;
	$TotalChecksB1  = $SaleCheckBase1;
	$TotalChecksB2  = $SaleCheckBase2;
	$TotalCreditsB1 = $SalesCreditAmountBase1;
	$TotalCreditsB2 = $SalesCreditAmountBase2;
	$TotalCouponB1  = $CouponAmountBase1;
	$TotalCouponB2  = $CouponAmountBase2;
	$TotalTipsB1 	= $SalesTipsBase1;
			
	$SubTotalCashB1 = $TotalCashInB1 - $TotalCashOutB1;
	$SubTotalCashB2 = $TotalCashInB2 - $TotalCashOutB2;
			
	$CurrDate = Date('Y-m-d h:i:s');
	$SubTotalCashB11 = ConvertCurrency($Base2, $Base1, $CurrDate, $SubTotalCashB2, $connection);
	$SubTotalCashB22 = ConvertCurrency($Base1, $Base2, $CurrDate, $SubTotalCashB1, $connection);
			
	$TotalCashB1 = $SubTotalCashB1 + $SubTotalCashB11;
	$TotalCashB2 = $SubTotalCashB2 + $SubTotalCashB22;
			
	$TotalTipsB2 = ConvertCurrency($Base1, $Base2, $CurrDate, $TotalTipsB1, $connection);
			
	$NetTotalB1	 = $TotalCashB1 + $TotalChecksB1 + $TotalCreditsB1 + $TotalCouponB1 + $TotalTipsB1;
	$NetTotalB2	 = $TotalCashB2 + $TotalChecksB2 + $TotalCreditsB2 + $TotalCouponB2 + $TotalTipsB2;
			
	if ($CurrencyCode == $Base1) {
		$TotalCashAmount    = $TotalCashB1;
		$TotalChecksAmount  = $TotalChecksB1;
		$TotalCreditsAmount = $TotalCreditsB1;
		$TotalCashPayOut    = $ctransdt_amountb1;
		$TotalCashInDrawer  = $ctrans_totalcashstart + $TotalCashAmount + $TotalChecksAmount + $TotalCreditsAmount - $TotalCashPayOut;
		$NetSales = $NetSalesBase1;
		$SaleDiscount=$SaleDiscountBase1;
	}	
	else {
		$TotalCashAmount 	= $TotalCashB2;
		$TotalChecksAmount  = $TotalChecksB2;
		$TotalCreditsAmount = $TotalCreditsB2;
		$TotalCashPayOut    = $ctransdt_amountb2;
		$TotalCashInDrawer  = $ctrans_totalcashstart + $TotalCashAmount + $TotalChecksAmount + $TotalCreditsAmount - $TotalCashPayOut;
		$NetSales = $NetSalesBase2;
		$SaleDiscount=$SaleDiscountBase2;
	}
	
	
	echo "<tr>";
	
	if($salestype==0)
		echo"<td>".$Sale_Date."</td>";
	else
		echo"<td>".$MonthName." ".$YearName."</td>";
	
		
		echo"<td align=right>". number_format($TotalCashAmount, $DecimalBase1) ."</td>";
		echo"<td align=right>". number_format($TotalChecksAmount, $DecimalBase1) ."</td>";
		echo"<td align=right>". number_format($TotalCreditsAmount, $DecimalBase1) ."</td>";
		echo"<td align=right>". number_format($NetSales, $DecimalBase1) ."</td>";
		echo"<td align=right>". number_format($SaleDiscount, $DecimalBase1) ."</td>";
	
	
	/*$querydt = " Select Round(Sum((ifNull(SaleDt_Quantity,0)*ifNull(SaleDt_Price,0))-((ifNull(SaleDt_Quantity,0)*ifNull(SaleDt_Price,0)*ifNull(SaleDt_DiscountPer,0))/100) - ifNull(SaleDt_DiscountAmount,0)), ".$DecimalBase.") As SaleDtTotal, ".
				 " Round(Sum((ifNull(SaleDt_Quantity,0)*ifNull(SaleDt_PriceTTC,0))-((ifNull(SaleDt_Quantity,0)*ifNull(SaleDt_PriceTTC,0)*ifNull(SaleDt_DiscountPer,0))/100) - (ifNull(SaleDt_DiscountAmount,0)*(1 + RetailDt.Item_Vat/100))), ".$DecimalBase.") As SaleDtTotalTTC, ".
				 " Round(Sum((ifNull(SaleDt_Quantity,0)*ifNull(SaleDt_PriceBase2,0))-((ifNull(SaleDt_Quantity,0)*ifNull(SaleDt_PriceBase2,0)*ifNull(SaleDt_DiscountPer,0))/100) - ifNull(SaleDt_DiscountBase2,0)), ".$DecimalBase.") As SaleDtTotalBase2, ".
				 " Round(Sum((ifNull(SaleDt_Quantity,0)*ifNull(SaleDt_PriceTTCBase2,0))-((ifNull(SaleDt_Quantity,0)*ifNull(SaleDt_PriceTTCBase2,0)*ifNull(SaleDt_DiscountPer,0))/100) - (ifNull(SaleDt_DiscountBase2,0)*(1 + RetailDt.Item_Vat/100))), ".$DecimalBase.") As SaleDtTotalTTCBase2 ".
				 " From Retail Inner Join Retaildt on Retail.Sale_id = Retaildt.Sale_id".
				 " Where 1 = 1".	
				 " And Date_format(Retail.Sale_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Retail.Sale_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Retail.Sale_Date,'%Y-%m-%d'))  ".
				 " Group By date_format(Retail.Sale_Date,'%Y%m%d'),Retail.Currency_Code ";	*/
	
	$SaleDtTotalTTC = $NetSales - $SaleDiscount;
	
	//echo"<td align=right>". number_format($SaleDtTotalTTC, $DecimalBase1) ."</td>";
		 
	echo "</tr>";
		
		
		$TotalNetSales = $TotalNetSales + $NetSales;
		$TotalTotalCashAmount = $TotalTotalCashAmount + $TotalCashAmount;
		$TotalTotalChecksAmount = $TotalTotalChecksAmount + $TotalChecksAmount;
		$TotalTotalCreditsAmount = $TotalTotalCreditsAmount + $TotalCreditsAmount;
		$TotalSaleDiscount = $TotalSaleDiscount + $SaleDiscount;
		$TotalSaleDtTotalTTC = $TotalSaleDtTotalTTC + $SaleDtTotalTTC;
		
	
if($groupby	== 2){
		$projectcode = $Project_Code;
		}
else if($groupby	== 3){
		$costcentercode = $Costcenter_Code;
		}
else if($groupby	== 4){
		$user_account = $User_Account;
		}
else if($groupby	== 5){
		$cashregister_name = $CashRegister_Name;
		}
		
		$TotalNetSalesProj = $TotalNetSalesProj + $NetSales;
		$TotalTotalCashAmountProj = $TotalTotalCashAmountProj + $TotalCashAmount;
		$TotalTotalChecksAmountProj = $TotalTotalChecksAmountProj + $TotalChecksAmount;
		$TotalTotalCreditsAmountProj = $TotalTotalCreditsAmountProj + $TotalCreditsAmount;
		$TotalSaleDiscountProj = $TotalSaleDiscountProj + $SaleDiscount;
		$TotalSaleDtTotalTTCProj = $TotalSaleDtTotalTTCProj + $SaleDtTotalTTC;
	
	
	$i+=1;
	}
 
 echo "<tr>";
 
 if($groupby	== 2 or $groupby	== 3  or $groupby	== 4   or $groupby	== 5 ){
	
		{
			 echo "<tr>";
					echo"<td><b>Total</b></td>";
					echo"<td align=right><b>". number_format($TotalTotalCashAmountProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalTotalChecksAmountProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalTotalCreditsAmountProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalNetSalesProj, $DecimalBase1) ."<b></td>";
					echo"<td align=right><b>". number_format($TotalSaleDiscountProj, $DecimalBase1) ."<b></td>";
					//echo"<td align=right><b>". number_format($TotalSaleDtTotalTTCProj, $DecimalBase1) ."<b></td>";
			 echo "</tr>";
			 
			$TotalNetSalesProj = 0;
			$TotalTotalCashAmountProj = 0;
			$TotalTotalChecksAmountProj = 0;
			$TotalTotalCreditsAmountProj = 0;
			$TotalSaleDiscountProj = 0;
			$TotalSaleDtTotalTTCProj = 0;
		}
		
}		

echo "<tr height=30>";
		echo"<td><b></b></td>";
echo "</tr>";
 
 
 echo "<tr>";
		echo"<td><b>Total</b></td>";
		echo"<td align=right><b>". number_format($TotalTotalCashAmount, $DecimalBase1) ."<b></td>";
		echo"<td align=right><b>". number_format($TotalTotalChecksAmount, $DecimalBase1) ."<b></td>";
		echo"<td align=right><b>". number_format($TotalTotalCreditsAmount, $DecimalBase1) ."<b></td>";
		echo"<td align=right><b>". number_format($TotalNetSales, $DecimalBase1) ."<b></td>";
		echo"<td align=right><b>". number_format($TotalSaleDiscount, $DecimalBase1) ."<b></td>";
	//	echo"<td align=right><b>". number_format($TotalSaleDtTotalTTC, $DecimalBase1) ."<b></td>";
 echo "</tr>";
 
 //Stopped By Elie and maroun.
 
// echo "<tr height=30>";
		//echo"<td><b>Summary</b></td>";
 //echo "</tr>";		
		
 //echo "<tr height=30>";
//		echo"<td><b> Gross Sales</b></td>";
		//echo"<td align=right><b>". number_format($TotalNetSales, $DecimalBase1) ."<b></td>";
// echo "</tr>";
 
// echo "<tr height=30>";
//		echo"<td><b> Total Discount</b></td>";
//		echo"<td align=right><b>". number_format($TotalSaleDiscount, $DecimalBase1) ."<b></td>";
// echo "</tr>";

 
// $NetSales = $TotalNetSales - $TotalSaleDiscount;
 
// echo "<tr height=30>";
//		echo"<td><b> Net Sales</b></td>";
//		echo"<td align=right><b>". number_format($NetSales, $DecimalBase1) ."<b></td>";
// echo "</tr>";
 
// echo "<tr height=30>";
//		echo"<td><b> Total Cash</b></td>";
//		echo"<td align=right><b>". number_format($TotalTotalCashAmount, $DecimalBase1) ."<b></td>";
// echo "</tr>";


// echo "<tr height=30>";
//		echo"<td><b> Credit Sales</b></td>";
//		echo"<td align=right><b>". number_format($TotalTotalCreditsAmount, $DecimalBase1) ."<b></td>";
// echo "</tr>";
 
// echo "<tr height=30>";
//		echo"<td><b> Cheque Sales</b></td>";
//		echo"<td align=right><b>". number_format($TotalTotalChecksAmount, $DecimalBase1) ."<b></td>";
// echo "</tr>";

 
 
 
function current_server_time($connection){

  $string="Select date_format(NOW(),'%Y-%m-%d %h:%i %p') ";
  $result=mysql_query($string,$connection) or die(mysql_error());
  $now=mysql_fetch_row($result);

  return $now;
}

function explodeword($date)
{

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim;
}


if ($printaccess==1 && $_GET['print'] == 1)
print <<< HERE

<script language=javascript>
if (typeof(window.print) != 'undefined')
{
    window.print();
}
</script>
</BODY>
</HTML>
HERE;

 
 
function ConvertCurrency($FromCurr, $ToCurr, $FromDate, $Amount, $connection)
{
	$query = "SELECT ifNull(convertcurfunc('".$FromCurr."', '".$ToCurr."', '".$FromDate."', 0, 0, '', '', ifNull($Amount,0)),0) From Dual ";
	$result=mysql_query($query,$connection);
	$row=mysql_fetch_row($result);
	//echo $query;
	Return	$row[0];

}
 
 
 ?>