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

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

print <<< HERE
<HTML>
<HEAD>
<TITLE> Slip 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: 16px; 
                  font-weight: bold;
                  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.labels1 { font-size: 16px; 
                          font-weight: bold;
                          font-family:Times-New-Roman;
					   }					  					  
table.totals td.amounts { font-size: 13px; 
                          font-family:Times-New-Roman;
					    }
table.totals td.amounts1 { font-size: 16px; 
						   font-weight: bold;
                           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");

/*$querylog=" Select FullName As User_FullName ".
          " From ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}*/

$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,ifNull(CashRegister_DrawerCOM,0) As CashRegister_DrawerCOM ".
				   " From CashRegister ".
				   " Where CashRegister_Name = '".$_SESSION['cashregister']."' ";
$resultCashRegister=mysql_query($queryCashRegister,$connection) or die(mysql_error());
while($rowCashRegister=mysql_fetch_array($resultCashRegister)){
	extract($rowCashRegister);
}

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

$TotalQty = 0;

$RetailId = $_GET['RetailId']; if ($RetailId == ""){$RetailId = $_POST["RetailId"];}

if($RetailId != '')
{
			 
	$query = " Select Sale_ID, Sale_Reference, Date_Format(Sale_Date,'%d/%m/%Y') As Sale_Date, Retail.Currency_code As CurrCode, ".
			 " Retail.Client_code As ClientCode, Clients.Ledger_name As ClientName, CashRegister_Name, Sale_Remark, ".
			 " if(Sale_Information='' or Sale_Information is null, Clients.Ledger_name, Sale_Information) As Sale_Information, ".
			 " Round(ifNull(Sale_Taxable,0), ".$DecimalBase.") As SaleTaxable, Round(ifNull(Sale_NoneTaxable,0), ".$DecimalBase.") As SaleNoneTaxable, ".
			 " Round(ifNull(Sale_Taxable,0) + ifNull(Sale_NoneTaxable,0), ".$DecimalBase.") As GrossTotal, ".
			 " Round(ifNull(Sale_DiscountPer,0), ".$DecimalBase."), Round(ifNull(Sale_DiscountAmount,0), ".$DecimalBase.") As Sale_DiscountAmount,  ".
			 " Round(ifNull(Sale_VAT,0), ".$DecimalBase.") As Sale_VAT, ".
			 " Round((ifNull(Sale_Taxable,0) + ifNull(Sale_NoneTaxable,0) + IfNull(Sale_VAT,0) - IfNull(Sale_DiscountAmount,0) + (ifNull(Sale_Tax1,0) + ifNull(Sale_Tax2,0) + ifNull(Sale_Tax3,0) + ifNull(Sale_Tax4,0) + ifNull(Sale_Tax5,0))), ".$DecimalBase.") As Sale_NetTotal1,  ".
			 " Round((ifNull(Sale_Tax1,0) + ifNull(Sale_Tax2,0) + ifNull(Sale_Tax3,0) + ifNull(Sale_Tax4,0) + ifNull(Sale_Tax5,0)), ".$DecimalBase.") As TotalTaxes,  ".
			 " Round(ifNull(Sale_NetTotal,0), ".$DecimalBase.") As  Sale_NetTotal, ".
			 " ifNull(Sales_CashInBase1,0) As CashInBase1, ifNull(Sales_CashInBase2,0) As CashInBase2, ".
			 " ifNull(Sales_CashOutBase1,0) As CashOutBase1, ifNull(Sales_CashOutBase2,0) As CashOutBase2,  ".
			 " Retail.Coupon_Id, Coupon_Reference As CouponReference, Coupon_Name As CouponName, ifNull(Retail.Coupon_Amount,0) As CouponAmount, ".
			 " ifNull(Retail.Coupon_Amount,0) - ifNull(convertcurfunc(Coupon.Currency_Code, Retail.Currency_code, Coupon_Date, 0, 0, '', '', ifNull(Coupon.Coupon_AmountConsum,0)),0) As CouponRemAmount, ".
			 " Sale_CashCur As CashCur, ifNull(Sale_Cash,0) As SaleCash, Sale_CheckNo As CheckNo, Date_Format(Sale_CheckDate,'%d/%m/%Y') As CheckDate, Sale_Bank As BankName, Sale_BankBranch As BankBranch, ".
			 " Sales_CreditAccCur As CreditAccCur, ifNull(Sales_CreditAmount,0) As CreditAmount, Sales_CreditAcc As CreditAcc, Ledger.Ledger_Name As CreditName,  ".
			 " ifNull(Sales_TipsBase1,0) As SalesTipsBase1, ".
			 " Round((ifNull(Sale_Taxable,0)*(1+$Setup_VatPercentage/100)) + ifNull(Sale_NoneTaxable,0), ".$DecimalBase.") As GrossTotalTTC, ".
			 " Round((((ifNull(Sale_Taxable,0)*(1+$Setup_VatPercentage/100)) + ifNull(Sale_NoneTaxable,0))*(ifNull(Sale_DiscountPer,0)/100)), ".$DecimalBase.") As Sale_DiscountAmountTTC, ".
			 " Round((ifNull(Sale_Taxable,0)*(1+$Setup_VatPercentage/100)), ".$DecimalBase.") As SaleTaxableTTC, ".
			 " Date_Format(Sale_Date,'%h:%i %p') As Sale_Time ".
			 " From Retail ".
			 " Left Join Clients on Retail.Client_code = Clients.Ledger_Number ".
			 " Left Join Coupon on Retail.Coupon_Id =  Coupon.Coupon_Id ".
			 " Left Join Ledger on Retail.Sales_CreditAcc =  Ledger.Ledger_Number ".
	         " Where Sale_ID = '".$RetailId."'";		 
			 
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
	
		
	$data .= "<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td align='left'>";
	$data .=  "<table border=0 width='250' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=40>
		  <col width=60>
		  <col width=40>
		  <col width=120>";
	
	$data .=  "<tr height='25'>";
	$data .=  "  <td align='left' valign='top' colspan='4'><h1>$CompanyName</h1></td>";
	$data .=  "</tr>";
	
	
	$data .=  "<tr>";
	$data .=  "  <td align='left' valign='top' colspan='3'>TEL: $CompanyTelephone</td>";
	$data .=  "  <td align='right' valign='center'>Vat# $Setup_VatNo &nbsp;</td>";
	$data .=  "</tr>";
	$data .=  "<tr>";
	$data .=  "  <td align='left' valign='top' colspan='4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$CompanyFax</td>";
	$data .=  "</tr>";
	$data .=  "<tr>";
	$data .=  "  <td align='Center' valign='top' colspan='4'><b><h4></b></td>";
	$data .=  "</tr>";

	$data .=  "<tr>";
	$data .=  "  <td align='Center' valign='top' colspan='4'><b></b></td>";
	$data .=  "</tr>";
	
	
	//$data .=  "<tr height='10'><td colspan=4><hr></td></tr>";
	$data .=  "<tr height='10'><td colspan=4></td></tr>";
	
	$data .=  "<tr>";
	$data .=  "  <th align='left' valign='top' >Inv.</th>";
	$data .=  "  <td align='left' valign='top' >$Sale_Reference</td>";
	$data .=  "  <th align='left' valign='top' >Date</th>";
	$data .=  "  <td align='left' valign='top' >$Sale_Date</td>";
	$data .=  "</tr>";
	$data .=  "<tr>";
	$data .=  "  <th align='left' valign='top' ></th>";
	$data .=  "  <td align='left' valign='top' ></td>";
	$data .=  "  <th align='left' valign='top' >Time</th>";
	$data .=  "  <td align='left' valign='top' >".$Sale_Time."</td>";
	$data .=  "</tr>";
	
	//$data .=  "<tr height='10'><td colspan=4></td></tr>";
	
	
	$data .=  "<tr>";
	$data .=  "  <th align='left' >Reg</th>";
	$data .=  "  <td align='left'  >".$_SESSION['cashregister']."</td>";
	$data .=  "  <th align='left' >Client</th>";
	$data .=  "  <td align='left' >$Sale_Information</td>";
	$data .=  "</tr>";
	
	
	$data .=  "</table></td></tr>";

	
		   
	$querydt = " Select RetailDt.SaleDt_id, RetailDt.Item_CodePacked, Substr(RetailDt.Item_code,1,11) As ItemCode, Substr(Item_Description,1,22) As ItemDescription, ".
			   " if(ifNull(Items.Item_Vat,0) > 0, '*', ' ') As ItemVat, SaleDt_Quantity, Round(SaleDt_Price, ".$DecimalBase.") As SaleDtPrice, SaleDt_PriceBase1, SaleDt_PriceBase2, ".
			   " Round(SaleDt_DiscountPer, ".$DecimalBase.") As SaleDtDiscountPer,  ".
			   " Round(((SaleDt_Quantity*SaleDt_Price)-((SaleDt_Quantity*SaleDt_Price*ifNull(SaleDt_DiscountPer,0))/100)), ".$DecimalBase.") As SaleDtTotal, ".
			   " Round(SaleDt_PriceTTC, ".$DecimalBase.") As SaleDtPriceTTC, ".
			   " Round(((SaleDt_Quantity*SaleDt_PriceTTC)-((SaleDt_Quantity*SaleDt_PriceTTC*ifNull(SaleDt_DiscountPer,0))/100)), ".$DecimalBase.") As SaleDtTotalTTC ".
			   " From RetailDt Left Join Items on RetailDt.Item_code = Items.Item_code ".
			   " Where RetailDt.Sale_ID = '".$RetailId."' ".
			   " Order By RetailDt.SaleDt_id  ";
			  // " Order By RetailDt.Item_CodePacked, RetailDt.Item_code  ";
			   
					   
	//$data .=  $querydt;
	$data .=  "<tr><td align='left'>";
	$data .=  "<table border=0 width='250' cellspacing=0 class='detail' style='table-layout:fixed;' >
		  <col width=40>
		  <col width=40>
		  <col width=70>
		  <col width=100> ";
$data .=  "<tr>";
	$data .=  "<tr height='10'><td colspan=4><b><h4></b></td></tr>";	
	$data .=  "</tr>";		  
	$data .=  "<tr height='25'>";
	$data .=  "   <th align='left'>Desc.</th>";
	$data .=  "	 <th align='center'>Qty.</th>";
	$data .=  "	 <th align='center'>Price</th>";
	$data .=  "	 <th align='center'>Total $CurrCode</th>";
	$data .=  "</tr>";			
		  
	//$data .=  "<tr height='10'><td colspan=7><hr></td></tr>";
	$data .=  "<tr height='10'><td colspan=4><b><h4></b></td></tr>";
	
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultdt)){
		extract($rows);
		
		$TotalQty = $TotalQty + $SaleDt_Quantity;
		
		$data .=  "<tr>";
		$data .=  "  <td align='left' valign='top' colspan='4'>$ItemVat&nbsp;$ItemDescription</td>";
		$data .=  "</tr>";
		
		$data .=  "<tr>";
		$data .=  "  <td align='left' valign='top'>&nbsp;</td>";
		$data .=  "	<td align='right' valign='top'>$SaleDt_Quantity</td>";
			
		if ($CashRegisterTTC == 1) {
			$data .=  "	<td align='right' valign='top'>".number_format($SaleDtPriceTTC, $DecimalBase)."</td>";
			$data .=  "	<td align='right' valign='top'>".number_format($SaleDtTotalTTC, $DecimalBase)."</td>";
		}	
		else {
			$data .=  "	<td align='right' valign='top'>".number_format($SaleDtPrice, $DecimalBase)."</td>";
			$data .=  "	<td align='right' valign='top'>".number_format($SaleDtTotal, $DecimalBase)."</td>";
		}	
				
				
		$data .=  "</tr>";
		
	}
	
	//$data .=  "<tr height='10'><td colspan=7><hr></td></tr>";
	$data .=  "<tr height='10'><td colspan=4><b><h4></b></td></tr>";
	$data .=  "</table></td></tr>";
	
	
		
	$data .=  "<tr><td align='left'>";
	$data .=  "<table border=0 width='245' cellspacing=0 cellpadding=1  class='totals' style='table-layout:fixed;' >
		  <col width=40>
		  <col width=100>
		  <col width=105>";
		  

	$data .=  "<tr>";
	$data .=  "   <td align='left' >&nbsp;</td>";
	$data .=  "	 <td align='left' class='labels'>Taxable</td>";
	
	if ($CashRegisterTTC == 1) 
		$data .=  "	 <td align='right' class='amounts'>".number_format($SaleTaxableTTC, $DecimalBase)."</td>";
	else
		$data .=  "	 <td align='right' class='amounts'>".number_format($SaleTaxable, $DecimalBase)."</td>";
	$data .=  "</tr>";	
	
	$data .=  "<tr>";
	$data .=  "   <td align='left' >&nbsp;</td>";
	$data .=  "	 <td align='left' class='labels'>None Taxable</td>";
	$data .=  "	 <td align='right' class='amounts'>".number_format($SaleNoneTaxable, $DecimalBase)."</td>";
	$data .=  "</tr>";	
	
	$data .=  "<tr>";
	$data .=  "   <td align='left' >&nbsp;</td>";
	$data .=  "	 <td align='left' class='labels1'>Net Total $CurrCode</td>";
	$data .=  "	 <td align='right' class='amounts1' style='border-top:1px dotted black;'>".number_format($Sale_NetTotal, $DecimalBase)."</td>";
	$data .=  "</tr>";	
	
	$data .=  "<tr height='10'><td colspan=3><b><h4></b></td></tr>";	
	
	$data .=  "<tr>";
	$data .=  "   <td align='left' >&nbsp;</td>";
	$data .=  "	 <td align='left' class='labels'>VAT $Setup_VatPercentage% $CurrCode</td>";
	$data .=  "	 <td align='right' class='amounts'>".number_format($Sale_VAT, $DecimalBase)."</td>";
	$data .=  "</tr>";	
	
	if (Round($SalesTipsBase1) != 0) {
		$data .=  "<tr>";
		$data .=  "   <td align='left' >&nbsp;</td>";
		$data .=  "	 <td align='left' class='labels'>Dead Balance $base1</td>";
		$data .=  "	 <td align='right' class='amounts'>".number_format($SalesTipsBase1, $DecimalBase)."</td>";
		$data .=  "</tr>";	
	
	}
	
		
	$data .=  "</table></td></tr>";
	
	$data .=  "<tr height='30'>";
	$data .=  "	 <td align='left' colspan='2'>&nbsp;</td>";
	$data .=  "</tr>";
	
	if ($CashInBase1 !=0 || $CashInBase2 !=0 || $CashOutBase1 !=0 || $CashOutBase2 !=0) {
		
		$data .=  "<tr><td align='left'>";
		$data .=  "<table border=0 width='245' cellspacing=0 cellpadding=1  class='totals' style='table-layout:fixed;' >
			  <col width=130>
			  <col width=115>";
		
			$data .=  "<tr>";
			
			$data .=  "	 <td align='right' colspan = 2 class='amounts'><b><h4></b></td>";
			$data .=  "</tr>";
		
		if ($CashInBase1 !=0) {
			$data .=  "<tr>";
			$data .=  "	 <td align='left' class='labels'>Cash Paid $base1</td>";
			$data .=  "	 <td align='right' class='amounts'>".number_format($CashInBase1, $DecimalBase)."</td>";
			$data .=  "</tr>";
		}

		
		
		if ($CashInBase2 !=0) {
			$data .=  "<tr>";
			$data .=  "	 <td align='left' class='labels'>Cash Paid $base2</td>";
			$data .=  "	 <td align='right' class='amounts'>".number_format($CashInBase2, $DecimalBase)."</td>";
			$data .=  "</tr>";
		}

		if ($CashOutBase1 !=0) {
			$data .=  "<tr>";
			$data .=  "	 <td align='left' class='labels'>Cash Returned $base1</td>";
			$data .=  "	 <td align='right' class='amounts'>".number_format($CashOutBase1, $DecimalBase)."</td>";
			$data .=  "</tr>";
		}		
		
		if ($CashOutBase2 !=0) {
			$data .=  "<tr>";
			$data .=  "	 <td align='left' class='labels'>Cash Returned $base2</td>";
			$data .=  "	 <td align='right' class='amounts'>".number_format($CashOutBase2, $DecimalBase)."</td>";
			$data .=  "</tr>";
		}
		
			
			
		$data .=  "</table></td></tr>";
	
	
	}
	
	
	if ($SaleCash !=0 || $CreditAmount != 0 || $CouponAmount != 0) {
		
		$data .=  "<tr><td align='left'>";
		$data .=  "<table border=0 width='245' cellspacing=0 cellpadding=1  class='totals' style='table-layout:fixed;' >
			  <col width=130>
			  <col width=115>";
		
		
		if ($SaleCash !=0) {
			$data .=  "<tr>";
			$data .=  "	 <td align='left' class='labels'>Check# $CheckNo $CashCur</td>";
			$data .=  "	 <td align='right' class='amounts'>".number_format($SaleCash, $DecimalBase)."</td>";
			$data .=  "</tr>";
		
			$data .=  "<tr>";
			$data .=  "	 <td align='left' class='labels'>$BankName</td>";
			$data .=  "	 <td align='left' class='labels'>$BankBranch</td>";
			$data .=  "</tr>";
			
		}	
		
		if ($CreditAmount != 0) {
			$data .=  "<tr>";
			$data .=  "	 <td align='left' class='labels'>Credit $CreditName</td>";
			$data .=  "	 <td align='right' class='amounts'>".number_format($CreditAmount, $DecimalBase)."</td>";
			$data .=  "</tr>";
		}
		
		if ($CouponAmount != 0) {
			$data .=  "<tr>";
			$data .=  "	 <td align='left' class='labels'>Coupon</td>";
			$data .=  "	 <td align='right' class='amounts'>".number_format($CouponAmount, $DecimalBase)."</td>";
			$data .=  "</tr>";
		}	
			
		$data .=  "</table></td></tr>";
		
	}
	
	$data .=  "<tr><td align='left'>";
	$data .=  "<table border=0 width='245' cellspacing=0 cellpadding=1  class='notes' style='table-layout:fixed;' >
		  <col width=130>
		  <col width=115>";

	$data .=  "<tr height='40'>";
	$data .=  "	 <th align='left' colspan='2'>&nbsp;</th>";
	$data .=  "</tr>";	
	
	$data .=  "<tr>";
	$data .=  "	 <th align='center' colspan='2'>Thank you for your visit</th>";
	$data .=  "</tr>";	
		
	$data .=  "<tr>";
	$data .=  "   <th align='center' colspan='2'>You are served by ".ucwords(strtolower($User_FullName))."</th>";
	//$data .=  "	 <th align='left' >".ucwords(strtolower($User_FullName))."</th>";
	$data .=  "</tr>";	
		
	$data .=  "</table></td></tr>";
	
	$data .=  "</table>";
	$data .=  "<br>";
	

}
//echo $data;
//Open Cash Drawer
$com=$CashRegister_DrawerCOM;
if (empty($com)) $com = 0;
$fp = fopen ("com".$com.":9600,n,8,1,DS", "wb+");
$string=chr(12); //simulating device
fputs ($fp, $string );             
fclose ($fp);


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 format_currency($amount, $DecimalBase) { 

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


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>
HERE;
/*
<script language=javascript>
if (typeof(window.print) != 'undefined')
{
    window.print();
	
	window.setTimeout(function() {
		window.close();
	}, 500 );
}

//self.close();
</script>
HERE;
*/

$printer = "Custom KUBE 80mm (200dpi)";

if($ph = printer_open($printer))
{
   $content= $data;
       
   // Set print mode to RAW and send PDF to printer
   printer_set_option($ph, PRINTER_MODE, "RAW");
   printer_write($ph, $content);
   printer_close($ph);
}
else "Couldn't connect...";


echo"</BODY>
</HTML>";

?>

