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

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

include("Functions_Sales.php");

print <<< HERE
<HTML>
<HEAD>
<TITLE> Cash Summary Report </TITLE>
<style type="text/css">

table.master h1 { font-size: 26px; 
                  font-weight: bold;
				  font-style: italic;
                  font-family:Times-New-Roman;
                }				 
				
table.master th { font-size: 13px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
table.master td { font-size: 11px; 
                  font-family:Times-New-Roman;
                 }
table.detail th { font-size: 13px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
				 
table.detail td { font-size: 11px; 
                  font-family:Times-New-Roman;
                 }				 
				 
				 
table.totals td.labels { font-size: 13px; 
                         font-weight: bold;
                         font-family:Times-New-Roman;
					  }					  
table.totals td.amounts { font-size: 13px; 
                          font-family:Times-New-Roman;
					    }
table.totals p { border-width: 1px;
 				 border-style: none none dotted none;
				 border-color: #666666;
				}	
h4 { border-top-style: double; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }				
				 
table.notes th { font-size: 12px; 
                 font-weight: bold;
                 font-family:Times-New-Roman;
                }				 
table.notes td { font-size: 12px; 
                 font-family:Times-New-Roman;
                }
</style>


</HEAD>

<BODY>
HERE;

include("config.inc.php");


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


$queryusers=" Select User_Name As User_FullName ".
			" From ".$_SESSION['dbLabel'].".users where user_account='".$_SESSION['useraccount']."' ";
$resultusers=mysql_query($queryusers,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultusers)){
	extract($rows);
}


//Read From Setup Table							
$querysetup= " Select DecimalBase, DecimalBase1, DecimalBase2, Base1, Base2, grid_color1, grid_color2, ".
             " Company_Name As CompanyName, Company_Memo As CompanyMemo, Company_City As CompanyCity, ".
			 " Company_Telephone As CompanyTelephone, Company_Fax As CompanyFax, ".
			 " Setup_VatNo, Setup_VatPercentage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}


//Read From CashRegister Table
$queryCashRegister=" Select ifNull(CashRegisterTTC,0) As CashRegisterTTC ".
				   " From CashRegister ".
				   " Where CashRegister_Name = '".$_SESSION['cashregister']."' ";
$resultCashRegister=mysql_query($queryCashRegister,$connection) or die(mysql_error());
while($rowCashRegister=mysql_fetch_array($resultCashRegister)){
	extract($rowCashRegister);
}

$PCashReg = $_GET['PCashReg'];
if (empty($PCashReg)) $PCashReg = $_SESSION['cashregister'];

$PrintDetail = $_GET['PrintDetail'];
if (empty($PrintDetail)) $PrintDetail = 0;

$sub_query = " Select ctrans_id, date_format(ctrans_timein, '%Y-%m-%d %h:%i %p') as ctrans_timein, ctrans_timeout, currency_code as ctrans_currency, ctrans_additionalcash, ".
			 " ifnull(ctrans_totalcashstart,0) as ctrans_totalcashstart, ifnull(ctrans_totalcashclose,0) as ctrans_totalcashclose ".
			 " From cashregistertrans ".
			 " Where upper(cashregister_name) = upper('".$PCashReg."') ".
			 " And cashregistertrans.ctrans_timeout is null ";
$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());
while($sub_row=mysql_fetch_array($sub_result)){
	extract($sub_row);
}
		
$sub_query1 = " Select Round(Sum(ifNull(ctransdt_amountb1,0)), ".$DecimalBase1.") As  ctransdt_amountb1, Round(Sum(ifNull(ctransdt_amountb2,0)), ".$DecimalBase2.") As  ctransdt_amountb2 ".
			  " From cashregistertransdt ".
			  " Where ctrans_id = '".$ctrans_id."' ".
			  " And upper(ctransdt_type) = 'OUT' ";
$sub_result1=mysql_query($sub_query1,$connection) or die(mysql_error());
while($sub_row1=mysql_fetch_array($sub_result1)){
	extract($sub_row1);
}
		
$query = " Select 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, ".
		 " Sum(ifNull(Sales_CreditAmountBase1,0)) As SalesCreditAmountBase1, Sum(ifNull(Sales_CreditAmountBase2,0)) As SalesCreditAmountBase2, ".
		 " Sum(ifNull(Sales_TipsBase1,0)) As SalesTipsBase1, ".
		 " Sum(ifNull(Coupon_AmountBase1,0)) As CouponAmountBase1, Sum(ifNull(Coupon_AmountBase2,0)) As CouponAmountBase2 ".
		 " From Retail ".
		 " Inner Join cashregistertrans on Upper(cashregistertrans.CashRegister_Name) = Upper(Retail.CashRegister_Name) ".
		 " Where Upper(Retail.CashRegister_Name) = Upper('".$PCashReg."') ".
		 " And cashregistertrans.ctrans_timeout is null ".
		 " And Retail.Sale_Date >= cashregistertrans.ctrans_timein ".
		 " Group By Retail.Currency_Code ";
		 
$result=mysql_query($query,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result)){
	extract($rows);
}


$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;
}	
else {
	$TotalCashAmount 	= $TotalCashB2;
	$TotalChecksAmount  = $TotalChecksB2;
	$TotalCreditsAmount = $TotalCreditsB2;
	$TotalCashPayOut    = $ctransdt_amountb2;
	$TotalCashInDrawer  = $ctrans_totalcashstart + $TotalCashAmount + $TotalChecksAmount + $TotalCreditsAmount - $TotalCashPayOut;
}

	
$PDateFrom = $ctrans_timein;
$PDateTo   = Date('Y-m-d h:i A');	
		
echo "<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td align='left'>";
echo "<table border=0 width='250' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
	  <col width=100>
	  <col width=120>
	  <col width=40>";
echo"	<tr><th align='left' valign='top'>Date From/To:</th><td align='left' colspan='2'>". $PDateFrom ." <br> ". $PDateTo ."</td></tr>";
echo"	<tr><th align='left' style='padding-bottom:10px;'>CashRegister:</th><td align='left' colspan='2' style='padding-bottom:10px;'>". $PCashReg ."</td></tr>";	  
echo"	<tr><td align='left' colspan='3' style='height:10px; border-top:1px dashed black;'>&nbsp;</th>";

echo"	<tr><th align='left'>Open Balance</th><td align='right'>". number_format($ctrans_totalcashstart, $DecimalBase1) ."</td><th align='center'>$ctrans_currency</th></tr>";
echo"	<tr><th align='left'>Total Cash</th><td align='right'>". number_format($TotalCashAmount, $DecimalBase1) ."</td><th align='center'>$CurrencyCode</th></tr>";
echo"	<tr><td align='left' colspan='3' style='padding-left:10px;'>
			<table border=0 width='150' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
			<col width=50>
			<col width=100>";
echo"			<tr><th align='left'>$Base1</th><td align='right'>". number_format($SubTotalCashB1, $DecimalBase1) ."</td></tr>";
echo"			<tr><th align='left'>$Base2</th><td align='right'>". number_format($SubTotalCashB2, $DecimalBase2) ."</td></tr>";
echo"	   	</table>";
echo"	</td></tr>";

if ($TotalChecksAmount != 0) {
	echo"<tr><th align='left'>Total Checks</th><td align='right'>". number_format($TotalChecksAmount, $DecimalBase1) ."</td><th align='center'>$CurrencyCode</th></tr>";
	echo"	<tr><td align='left' colspan='3' style='padding-left:10px;'>
			<table border=0 width='150' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
			<col width=50>
			<col width=100>";
			
	$sub_query2 = " Select Retail.Sale_CashCur as Sale_CashCur, Sum(ifNull(Sale_Cash,0)) As Sale_Cash ".
				  " From Retail ".
				  " Inner Join cashregistertrans on Upper(cashregistertrans.CashRegister_Name) = Upper(Retail.CashRegister_Name) ".
				  " Where Upper(Retail.CashRegister_Name) = Upper('".$PCashReg."') ".
				  " And cashregistertrans.ctrans_timeout is null ".
				  " And Retail.Sale_Date >= cashregistertrans.ctrans_timein ".
				  " Group By Retail.Sale_CashCur ";
			 
	$sub_result2=mysql_query($sub_query2,$connection) or die(mysql_error());
	while($sub_rows2=mysql_fetch_array($sub_result2)){
		extract($sub_rows2);
		
		echo"<tr><th align='left'>$Sale_CashCur</th><td align='right'>". number_format($Sale_Cash, $DecimalBase1) ."</td></tr>";
	}
	
	echo"  	</table>";
	echo"</td></tr>";
}
	
if ($TotalCreditsAmount != 0) {
	echo"<tr><th align='left'>Total Credits</th><td align='right'>". number_format($TotalCreditsAmount, $DecimalBase1) ."</td><th align='center'>$CurrencyCode</th></tr>";
	
	echo"	<tr><td align='left' colspan='3' style='padding-left:10px;'>
			<table border=0 width='150' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
			<col width=50>
			<col width=100>";
			
	$sub_query3 = " Select Retail.Sales_creditacccur as Sales_CreditAccCur, Sum(ifNull(Sales_CreditAmount,0)) As Sales_CreditAmount ".
				  " From Retail ".
				  " Inner Join cashregistertrans on Upper(cashregistertrans.CashRegister_Name) = Upper(Retail.CashRegister_Name) ".
				  " Where Upper(Retail.CashRegister_Name) = Upper('".$PCashReg."') ".
				  " And cashregistertrans.ctrans_timeout is null ".
				  " And Retail.Sale_Date >= cashregistertrans.ctrans_timein ".
				  " Group By Retail.Sales_CreditAccCur ";
			 
	$sub_result3=mysql_query($sub_query3,$connection) or die(mysql_error());
	while($sub_rows3=mysql_fetch_array($sub_result3)){
		extract($sub_rows3);
		
		echo"<tr><th align='left'>$Sales_CreditAccCur</th><td align='right'>". number_format($Sales_CreditAmount, $DecimalBase1) ."</td></tr>";
	}
	
	echo"  	</table>";
	echo"</td></tr>";
}	
echo"	<tr><th align='left'>Total Cash Out</th><td align='right'>". number_format($TotalCashPayOut, $DecimalBase1) ."</td><th align='center'>$CurrencyCode</th></tr>";
echo"	<tr><th align='left' style='height:30px;'>Drawer Count</th><th align='right' style='border-top:1px dotted black;'>". number_format($TotalCashInDrawer, $DecimalBase1) ."</th><th align='center'>$CurrencyCode</th></tr>";
echo"	<tr><td align='left' colspan='3' style='height:10px; border-top:1px dashed black;'>&nbsp;</th>";

echo "</table></td></tr>";

if ($PrintDetail == 1) {

	$query = "";
	$query = " Select Retail.Sale_Id as SaleId, Retail.Sale_Reference as SaleReference, Retail.Currency_Code as CurrencyCode, ".
			 " Round(ifNull(Sale_NetTotalBase1,0),5) As NetSalesBase1, Round(ifNull(Sale_NetTotalBase2,0),2) As NetSalesBase2 ".
			 " ,Sale_Type ".
			 " ,Round((ifNull(Sales_CashInBase1,0)),5) As SalesCashInBase1, Round((ifNull(Sales_CashOutBase1,0)),5) As SalesCashOutBase1  ".
			 " ,Round((ifNull(Sales_CashInBase2,0)),5) As SalesCashInBase2, Round((ifNull(Sales_CashOutBase2,0)),2) As SalesCashOutBase2 ".
			 " From Retail ".
			 " Inner Join cashregistertrans on Upper(cashregistertrans.CashRegister_Name) = Upper(Retail.CashRegister_Name) ".
			 " Where Upper(Retail.CashRegister_Name) = Upper('".$PCashReg."') ".
			 " And cashregistertrans.ctrans_timeout is null ".
			 " And Retail.Sale_Date >= cashregistertrans.ctrans_timein Order by ifnull(Sale_Type,-1),sale_date,sale_reference ";
			// " 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'))  ";
	
	
	echo"<tr><td align='left'>";
	echo "<table border=0 width='240' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=100>
		  <col width=120>
		  <col width=20> ";
	echo"<tr>
			<th align='center' style='border-bottom:1px dashed black;'>Reference</th>
			<th align='center' style='border-bottom:1px dashed black;'>Total $Base1</th>
			<th align='center' style='border-bottom:1px dashed black;'>&nbsp;</th>
		</tr>";
	$i=1;	  
	$total=0;	  
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
		
		
				$TotalCashInB1  = $SalesCashInBase1;
				$TotalCashInB2  = $SalesCashInBase2;
				$TotalCashOutB1 = $SalesCashOutBase1;
				$TotalCashOutB2 = $SalesCashOutBase2;
					
				$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;
						
				if ($CurrencyCode == $Base1) {
					$TotalCashAmount    = $TotalCashB1;
				}	
				else {
					$TotalCashAmount 	= $TotalCashB2;
				}
	
			if ($CurrencyCode == $Base1) {
					$NetSales    = $NetSalesBase1;
				}	
				else {
					$NetSales 	= $NetSalesBase2;
				}
	
	
		if($Sale_Type != $Vsaletype ){
		
		
		$querydt = " Select Null,Retail.Currency_Code as CurrencyCode, ".
			 " Sum(Round(ifNull(Sale_NetTotalBase1,0),5)) As SumNetSalesBase1, Sum(Round(ifNull(Sale_NetTotalBase2,0),2)) As SumNetSalesBase2 ".
			 " ,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 ".			
			" From Retail ".
			 " Inner Join cashregistertrans on Upper(cashregistertrans.CashRegister_Name) = Upper(Retail.CashRegister_Name) ".
			 " Where Upper(Retail.CashRegister_Name) = Upper('".$PCashReg."') ".
			 " And cashregistertrans.ctrans_timeout is null ".
			 " And Retail.Sale_Date >= cashregistertrans.ctrans_timein ".
			 " And ifNull(Sale_Type,-1) = if('$Vsaletype'='',-1,'$Vsaletype') ".
			 " group by ifNull(Sale_Type,-1),Retail.Currency_Code " ;
				//echo $querydt;
				$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
					while($rowsdt=mysql_fetch_array($resultdt)){
						extract($rowsdt);
						
						$TotalCashInB1  = $SalesCashInBase1;
						$TotalCashInB2  = $SalesCashInBase2;
						$TotalCashOutB1 = $SalesCashOutBase1;
						$TotalCashOutB2 = $SalesCashOutBase2;
							
						$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;
								
						if ($CurrencyCode == $Base1) {
							//$TotalCashAmountdt    = $TotalCashB1;
							$TotalCashAmountdt    = $SumNetSalesBase1;
						}	
						else {
							//$TotalCashAmountdt 	= $TotalCashB2;
							$TotalCashAmountdt 	= $SumNetSalesBase2;
						}
						
						
							echo"<tr>
								<td align='left'><b>Total: ".StrToUpper($Vsaletype)." $total</b></td>
								<td align='right'><b>". number_format($TotalCashAmountdt, $DecimalBase1) ."</b></td><td align=right><b>".$CurrencyCode."</b></td>
								<td align='center'>&nbsp;</td>
							</tr>";


				echo"<tr><td align='center' style='border-top:1px dashed black;' colspan='3'>&nbsp;</td></tr>";
				$total=0;
				}
		
		}
		
			echo"<tr>
				<td align='left'>".$SaleReference."</td>
				<td align='right'>". number_format($NetSales, $DecimalBase1) ."</td>
				<td align='center'>&nbsp;</td>
			</tr>";
			
			
	$Vsaletype = $Sale_Type;
	$i+=1;
	$total+=1;
	}
	
	


	echo "</table></td></tr>";
}

echo"<tr><td align='left'>";
	echo "<table border=0 width='240' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=100>
		  <col width=120>
		  <col width=20> ";
	$querydt = " Select Null,Retail.Currency_Code as CurrencyCode, ".
			 " Sum(Round(ifNull(Sale_NetTotalBase1,0),5)) As SumNetSalesBase1, Sum(Round(ifNull(Sale_NetTotalBase2,0),2)) As SumNetSalesBase2 ".
			 " ,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 ".			
			" From Retail ".
			 " Inner Join cashregistertrans on Upper(cashregistertrans.CashRegister_Name) = Upper(Retail.CashRegister_Name) ".
			 " Where Upper(Retail.CashRegister_Name) = Upper('".$PCashReg."') ".
			 " And cashregistertrans.ctrans_timeout is null ".
			 " And Retail.Sale_Date >= cashregistertrans.ctrans_timein ".
			 " And ifNull(Sale_Type,-1) = if('$Vsaletype'='',-1,'$Vsaletype') ".
			 " group by ifNull(Sale_Type,-1),Retail.Currency_Code " ;
			 
						
//echo $query;
$result=mysql_query($querydt,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
		
		$TotalCashInB1  = $SalesCashInBase1;
						$TotalCashInB2  = $SalesCashInBase2;
						$TotalCashOutB1 = $SalesCashOutBase1;
						$TotalCashOutB2 = $SalesCashOutBase2;
							
						$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;
								
						if ($CurrencyCode == $Base1) {
							$TotalCashAmountdt1    = $TotalCashB1;
						}	
						else {
							$TotalCashAmountdt1 	= $TotalCashB2;
						}
						
						
			echo"<tr>
				<td align='left'><b>Total:".StrToUpper($Vsaletype)." $total</b></td>
				<td align='right'><b>". number_format($TotalCashAmountdt1, $DecimalBase1) ."</b></td><td align=right><b>".$CurrencyCode."</b></td>
				<td align='center'>&nbsp;</td>
			</tr>";



}
echo"<tr><td align='center' style='border-top:1px dashed black;' colspan='3'>&nbsp;</td></tr>";
echo "</table></td></tr>";




echo"<tr><td align='left'>";
	echo "<table border=0 width='240' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=100>
		  <col width=120>
		  <col width=20> ";
		  
$query = " Select Count(1) SumofTypes,if(SaleDt_ItemStatus=1,'Gift',if(SaleDt_ItemStatus=2,'Void','Waste')) As Status ".
			 " From RetailDt,Retail ".
			 " Inner Join cashregistertrans on Upper(cashregistertrans.CashRegister_Name) = Upper(Retail.CashRegister_Name) ".
			 " Where Upper(Retail.CashRegister_Name) = Upper('".$PCashReg."') ".
			 " And RetailDt.Sale_id = Retail.Sale_Id ".
			 " And cashregistertrans.ctrans_timeout is null ".
			 " And Retail.Sale_Date >= cashregistertrans.ctrans_timein ".
			 " And RetailDt.SaleDt_ItemStatus in (1,2,3) ".
			 " group by ifNull(SaleDt_ItemStatus,-1) " ;
//echo $query;
$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
			echo"<tr>
				<td align='left'><b>".StrToUpper($Status)."</b></td>
				<td align='right'><b>". $SumofTypes ."</b></td><td align=right><b></b></td>
				<td align='center'>&nbsp;</td>
			</tr>";



}
echo"<tr><td align='center' style='border-top:1px dashed black;' colspan='3'>&nbsp;</td></tr>";
echo "</table></td></tr>";



echo "</table>";



function current_server_time($connection){

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

  return $now[0];
}

function formatDateClosing($data){
	return substr($data, 0, 4) ."-". substr($data, 5, 2) . "-". substr($data, 8, 2) . substr($data, 10, 9);
}

function format_currency($amount, $DecimalBase) { 

    if($amount < 0) 
        return "(".number_format(abs($amount),$DecimalBase).")"; 
 
    else return number_format($amount, $DecimalBase); 
} 

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];

}



 
print <<< HERE
<script language='VBScript'>
Sub Print()
       OLECMDID_PRINT = 6
       OLECMDEXECOPT_DONTPROMPTUSER = 2
       OLECMDEXECOPT_PROMPTUSER = 1
       call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
End Sub
document.write "<object ID='WB' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>"
</script>

<script type="text/javascript" language="javascript">
if (typeof(window.print) != 'undefined')
{
    window.print();
	
	window.setTimeout(function() {
		window.close();
	}, 500 );
}

</script>


</BODY>
</HTML>
HERE;
 
?>

