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

//Report Version For MARIO
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: 12px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
table.master td { font-size: 12px; 
                  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-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");
include("function.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);
}*/


//Read From CashRegister Table
$queryCashRegister=" Select Upper(CashRegister_Name) As CashRegister_Name, Currency_Code As StpCurrCode, CostCenter_Code As StpCostCentCode, Project_Code As StpProjectCode, ".
                   " ifNull(CashRegister_BarCode,0) As CashRegisterBarCode, ifNull(CashRegister_SerialNo,0) As CashRegisterSerialNo, ifNull(CashRegister_Discount,0) As CashRegisterDiscount, ".
				   " CashRegisterTTC, Client_Account As StpClientAccount, ifNull(CashRegister_SlipPrinter,0) As CashRegister_SlipPrinter, ".
				   " ifNull(CashRegister_AutoPost,0) As CashRegister_AutoPost, ifNull(CashReg_Vendor,0) As CashReg_Vendor, ".
				   " ifNull(CashRegister_DisplayLed,0) As CashRegister_DisplayLed, ifNull(CashRegister_DispCOM,0) As CashRegister_DispCOM, ".
				   " ifNull(CashRegister_PrintBarcode,0) As CashRegister_PrintBarcode, ifNull(CashReg_ForcingVendor,0) As CashReg_ForcingVendor, ".
				   " ifNull(CashRegister_LedDescLength,0) As CashRegister_LedDescLength, ifNull(CashRegister_Printer,0) As CashRegister_Printer, ".
				   " ifNull(Module_CashRegisterFastItem,0) As Module_CashRegisterFastItem, ifNull(Module_RestaurantDelivery,0) As Module_RestaurantDelivery, ".
				   " ifNull(Module_CashRegisterStandardView,0) As Module_CashRegisterStandardView, ifNull(Module_CashRegisterTable,0) As Module_CashRegisterTable, ".
				   " ifNull(Module_CashRegAutoItemsList,0) As Module_CashRegAutoItemsList  ".
				   " From CashRegister ".
				   " Where CashRegister_Name = '".$_SESSION['cashregister']."' ";
				 //echo$queryCashRegister;
$resultCashRegister=mysql_query($queryCashRegister,$connection) or die(mysql_error());
while($rowCashRegister=mysql_fetch_array($resultCashRegister)){
	extract($rowCashRegister);
}


$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, Company_Logo As CompanyLogo, ".
			 " Setup_VatNo, Setup_VatPercentage, ifNull(CashReg_RoundDivisor,1) As CashReg_RoundDivisor 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, Retail.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, Customer.Customer_Name As CustomerName, Customer_Telephone1, Customer_Address, ".
			 " ProjectDt.ProjectDt_Name, Retail.Sale_numberofguests ".
			 " 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 ".
			 " Left Join Customer on Retail.Customer_Code =  Customer.Customer_Code ".
			 " Left Join ProjectDt on Retail.projectdt_id =  ProjectDt.projectdt_id ".
	         " Where Sale_ID = '".$RetailId."'";		 
			 
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
	
		
	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=40>
		  <col width=60>
		  <col width=50>
		  <col width=100>";

		
	
	echo "<tr>";
	echo "  <th align='left' valign='top' style='border-top:1px solid black;'>INV.</th>";
	echo "  <td align='left' valign='top' style='border-top:1px solid black;'>$Sale_Reference</td>";
	echo "  <th align='left' valign='top' style='border-top:1px solid black;'>DATE</th>";
	echo "  <td align='left' valign='top' style='border-top:1px solid black;'>$Sale_Date</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "  <th align='left' valign='top'>POS</th>";
	echo "  <td align='left' valign='top'>".$_SESSION['cashregister']."</td>";
	echo "  <th align='left' valign='top'>TIME</th>";
	echo "  <td align='left' valign='top'>$Sale_Time</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "  <th align='left' valign='top'>TABLE</th>";
	echo "  <td align='left' valign='top'>$ProjectDt_Name</td>";
	echo "  <th align='left' valign='top'>GUESTS</th>";
	echo "  <td align='left' valign='top'>$Sale_numberofguests</td>";
	echo "</tr>";
	
	
	echo "</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, ".
			   " SalesDt_Remark as SalesDtRemark, RetailDt.RelatedDt_Id as RelatedDtId ".
			   " From RetailDt Left Join Items on RetailDt.Item_code = Items.Item_code ".
			   " Where RetailDt.Sale_ID = '".$RetailId."' ".
			   " And IfNull(SaleDt_ItemStatus,0) Not In (2,3) ".
			   //" Order By RetailDt.SaleDt_id  ";
			   " Order By ifNull(RelatedDt_Id, RetailDt.SaleDt_ID) Asc  ";
			  // " Order By RetailDt.Item_CodePacked, RetailDt.Item_code  ";
			   
					   
	//echo $querydt;
	echo "<tr><td align='left'>";
	echo "<table border=0 width='250' cellspacing=0 class='detail' style='table-layout:fixed;' >
		  <col width=200>
		  <col width=50> ";
	echo "<tr height='20'>";
	echo "   <th align='left' style='border-top:2px solid black; border-bottom:2px solid black;'>Desc.</th>";
	echo "	 <th align='center' style='border-top:2px solid black; border-bottom:2px solid black;'>Qty.</th>";
	echo "</tr>";			
		  
	
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultdt)){
		extract($rows);
		
		$TotalQty = $TotalQty + $SaleDt_Quantity;
		
		if ($RelatedDtId != '') {
			echo "<tr>";
			echo "  <td align='left' valign='top' colspan='2' style='padding-left:20px;'>$ItemDescription</td>";
			echo "</tr>";
		} else {
			echo "<tr>";
			echo "  <td align='left' valign='top'>$ItemVat&nbsp;$ItemDescription</td>";
			echo "	<td align='right' valign='top'>$SaleDt_Quantity</td>";
			echo "</tr>";
		}
		
		if($SalesDtRemark != '') {
			echo "<tr>";
			echo "  <td align='left' valign='top' colspan='2' style='padding-left:20px;'>".$SalesDtRemark."</td>";
			echo "</tr>";		
		}
		
	}
	
	echo "</table></td></tr>";
	
	
	echo "</table>";
	echo "<br>";
	

}


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>

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

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

</BODY>
</HTML>
HERE;


?>

