<?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> Summary Report </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

<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: 13px; 
                  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);
}

$PDateFrom = $_GET['PDateFrom']; if ($PDateFrom == ""){$PDateFrom = $_POST["PDateFrom"];}
	
$PDateTo = current_server_time($connection);

//if (empty($PDateFrom)) $PDateFrom = displayToday();
			 
$query = " Select 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 ".
		 " Where Upper(CashRegister_Name) = Upper('".$_SESSION['cashregister']."') ";
		 
if ($PDateFrom != "")
	$query .= " And Sale_Date >= '".formatDateClosing($PDateFrom)."'";

			 
$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;
		
	
		
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=90>
	  <col width=80>
	  <col width=80>";

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;'>". $_SESSION['cashregister'] ."</td></tr>";	  
echo"	<tr><td align='left' colspan='3' style='height:30px; border-top:1px dashed black;'>&nbsp;</th>";

echo"	<tr><th align='left'>&nbsp;</th><th align='center' style='border-bottom: 1px solid black;'>$Base1</th><th align='center' style='border-bottom: 1px solid black;'>$Base2</th></tr>";
echo"	<tr><th align='left'>Total Taxable</th><td align='right'>". number_format($SaleTaxableBase1, 0) ."</td><td align='right'>". number_format($SaleTaxableBase2, $DecimalBase2) ."</td></tr>";
echo"	<tr><th align='left'>Total NoneTaxable</th><td align='right'>". number_format($SaleNoneTaxableBase1, 0) ."</td><td align='right'>". number_format($SaleNoneTaxableBase2, $DecimalBase2) ."</td></tr>";
echo"	<tr><th align='left'>Total Discount</th><td align='right'>". number_format($SaleDiscountBase1, 0) ."</td><td align='right'>". number_format($SaleDiscountBase2, $DecimalBase2) ."</td></tr>";
echo"	<tr><th align='left'>Total VAT</th><td align='right'>". number_format($SaleVATBase1, 0) ."</td><td align='right'>". number_format($SaleVATBase2, $DecimalBase2) ."</td></tr>";
echo"	<tr><th align='left' style='height:30px;' >Net Sales</th><td align='right' style='border-top:1px dotted black;'>". number_format($NetSalesBase1, 0) ."</td><td align='right' style='border-top:1px dotted black;'>". number_format($NetSalesBase2, $DecimalBase2) ."</td></tr>";
echo"	<tr><td align='left' colspan='3' style='height:30px; border-top:1px dashed black;'>&nbsp;</th>";

echo"	<tr><th align='left' style='border-bottom: 1px solid black;'>Summary</th><th align='center'>$Base1</th><th align='center'>$Base2</th></tr>";
echo"	<tr><th align='left'>Cash</th><td align='right'>". number_format($TotalCashB1, 0) ."</td><td align='right'>". number_format($TotalCashB2, $DecimalBase2) ."</td></tr>";
echo"	<tr><th align='left'>Checks</th><td align='right'>". number_format($TotalChecksB1, 0) ."</td><td align='right'>". number_format($TotalChecksB2, $DecimalBase2) ."</td></tr>";
echo"	<tr><th align='left'>Credits</th><td align='right'>". number_format($TotalCreditsB1, 0) ."</td><td align='right'>". number_format($TotalCreditsB2, $DecimalBase2) ."</td></tr>";
echo"	<tr><th align='left'>Coupon</th><td align='right'>". number_format($TotalCouponB1, 0) ."</td><td align='right'>". number_format($TotalCouponB2, $DecimalBase2) ."</td></tr>";
echo"	<tr><th align='left'>Tips</th><td align='right'>". number_format($TotalTipsB1, 0) ."</td><td align='right'>". number_format($TotalTipsB2, $DecimalBase2) ."</td></tr>";
echo"	<tr><th align='left' style='height:30px;'>Net Total</th><td align='right' style='border-top:1px dotted black;'>". number_format($NetTotalB1, 0) ."</td><td align='right' style='border-top:1px dotted black;'>". number_format($NetTotalB2, $DecimalBase2) ."</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];

}

/*
<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>
*/


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;

?>

