<?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);
	}
	
	
//$printer = "Custom KUBE 80mm (200dpi)";
$printer = $printername;

$handle = printer_open($printer);

   //$content= $data1;
	
   // Set print mode to RAW and send PDF to printer
   printer_set_option($handle, PRINTER_MODE, "RAW");
   printer_set_option($handle, PRINTER_TEXT_ALIGN, PRINTER_TA_LEFT);
   /*printer_write($handle, $CompanyName);
   printer_write($handle, "\n");
   printer_write($handle, "TEL: $CompanyTelephone");
   printer_write($handle, "Vat# $Setup_VatNo"."\n");
  
	 
	printer_write($handle, "    $CompanyFax"."\n");
	printer_write($handle, "\n");
	printer_write($handle, "\n");*/
	
	printer_write($handle, "Inv.    ");
	printer_write($handle, "$Sale_Reference");
	printer_write($handle, "    Date    ");
	printer_write($handle, "$Sale_Date"."\n");
	
	
	printer_write($handle, "        ");
	printer_write($handle, "     ");
	printer_write($handle, "    Time    ");
	printer_write($handle, "$Sale_Time"."\n");
	
	 
	printer_write($handle, "  Reg      ");
	printer_write($handle,  $_SESSION['cashregister']);
	printer_write($handle, "\n");
	
	 	   
	$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."' ".
			   " and ifnull(items.subgroup_code,-1) = if('$subgroupcode'='',-1,'$subgroupcode') ".
			   " and ifnull(saleDt_ItemStatus,0) = 0".
			   " Order By RetailDt.SaleDt_id  ";
			  // " Order By RetailDt.Item_CodePacked, RetailDt.Item_code  ";
			   
					   
	//$data .=  $querydt;
		printer_write($handle, "__________________________________________");
		printer_write($handle, "\n");	  
		printer_write($handle, "  Desc.                  ");
		printer_write($handle, "     Qty.");
		
		printer_write($handle, "\n");	  
		
		printer_write($handle, "__________________________________________");
		printer_write($handle, "\n");	  
	
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultdt)){
		extract($rows);
		
		$TotalQty = $TotalQty + $SaleDt_Quantity;
		
		printer_write($handle, " $ItemVat $ItemDescription    ");
		
		printer_write($handle, "  $SaleDt_Quantity    ");
		
 	
		printer_write($handle, "\n");		
		 
		
	}
	
	 	  
	$qq = "update temp_RetailDt,items set saleDt_ItemStatus = 1 ".
				" Where temp_RetailDt.Sale_ID = '".$RetailId."' ".
			   " and ifnull(items.subgroup_code,-1) = if('$subgroupcode'='',-1,'$subgroupcode') ".
			   " and ifnull(saleDt_ItemStatus,0) = 0".
			   " and items.item_code = temp_RetailDt.item_code";
	$resultdt=mysql_query($qq,$connection) or die(mysql_error());

	
	$qq = "select 1 from temp_RetailDt ".
				" where temp_RetailDt.Sale_ID = '".$RetailId."' ".
			   " and ifnull(saleDt_ItemStatus,0) = 0";
    $resultdt=mysql_query($qq,$connection) or die(mysql_error());
    $rows=mysql_fetch_array($resultdt);
	$isexists = $rows[0];
	
	
	if($isexists==0 or $isexists=='')
	{
	print <<<HERE
	<script language=javascript>
		if(document.opener.name)
			document.opener.name.disabled = true;
	</script>
HERE;
	}
	/*printer_write($handle, "\n");
	printer_write($handle, "	Thank you for your visit	");
	printer_write($handle, "\n");
	
	printer_write($handle, "	You are served by ".ucwords(strtolower($User_FullName))."");
	printer_write($handle, "\n");
	*/
		

}
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");

//echo $data1;
//Open Cash Drawer
$com=$CashRegister_DrawerCOM;
if (empty($com)) $com = 0;
$fp = fopen ("com".$com.":9600,n,8,1,DS", "wb+");
$string=chr(14); //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); 
} 

 printer_close($handle);
 
 
print <<< HERE
<script language=javascript>
		self.close();
</script>
HERE;



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

?>

