<?php
session_start();
 
/*
/////////////////////////////////////////////////////// 
//BLOCK USED TO REDIRECT TO COMPANY SPECIFIC REPORTS//
/////////////////////////////////////////////////////
 $curfolder = substr(getcwd(), strrpos(getcwd(), '\\') + 1);
 $curfolder = strtolower($curfolder);
 if ($curfolder == "contract")
 {
$pagename = basename($_SERVER['REQUEST_URI']);
$pagenamewithoutparams =  basename($_SERVER['PHP_SELF']);
$CompYear = explode('_',$_SESSION["dbName"]);
$companyname = strtoupper($CompYear[0]);
$clientrelatedpage = getcwd()  . "\\" .$companyname . "\\".$$pagenamewithoutparams  ;

 
if (file_exists($clientrelatedpage.$pagenamewithoutparams )) {
header("location:$companyname/$pagename");
} 

if (!file_exists($clientrelatedpage)) {
    mkdir($clientrelatedpage, 0777, true);
}
	
}
////////////////////////////////////////////////////////////////////////////////////////// 
///adjust include,css, javascript library etc... in cloned report ex ////////////////////
// $success = include "config.inc.php"; if (!$success) {include "../config.inc.php";}///
//js exemplage <script src = '../scriptname.js' >  </script>     //////////////////////
//////////////////////////////////////////////////////////////////////////////////////

*/


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

 $success = include "config.inc.php"; if (!$success) {include "../config.inc.php";}

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

$query_setup  = "select company_name, company_memo, DecimalBase, Setup_VatPercentage, SalesTTC, setup_invoiceremarks from setup";
$result_setup = mysql_query($query_setup, $connection); 
$row_setup    = mysql_fetch_array($result_setup);
extract($row_setup);

if($exp ==1){
	$filename = "SalesOrder_" . date('Ymd') . ".xls";
	header("Content-Disposition: attachment; filename=\"$filename\"");
	header("Content-Type: application/vnd.ms-excel");
} 

print <<<HERE
<html>
<head>
<title>SALES ORDER REPORT</title>
</head>
<body>
HERE;

$query = " Select Sorder_ID, Sorder_Reference, Sorder_Type, Date_Format(Sorder_Date,'%d/%m/%Y') As Sorder_Date, Sorder_Remark, ".
			 " Date_Format(Sorder_MaturityDate,'%d/%m/%Y') As MaturityDate, Date_Format(Sorder_RequestDate,'%d/%m/%Y') As RequestDate, Sorder_RequestBy, ".
			 " Sorder.Project_Code As ProjectCode, Project_Name, Sorder.Costcent_code As Costcentcode, Costcent_name, Sorder.Currency_code As CurrencyCode, ".
			 " Sorder.Client_code As ClientCode, Clients.Ledger_name As ClientName, Client_VAT, Client_Fax, Client_CreditLimit, client_telephone1, client_telephone2, ".
			 " Client_PaymentTerms,  Sorder_ClientContact, Sorder_DeliveryPlace, ".
			 " Sorder.Salesman_Code As SalesmanCode, Salesmen.Ledger_Name As SalesmanName, if(sorder_confirmed=1,'Yes','No') As SorderConfirmed, ".
			 " Round(if($SalesTTC=1,ifNull(Sorder_Taxable,0)*(1+$Setup_VatPercentage/100),ifNull(Sorder_Taxable,0)), ".$DecimalBase.") As SorderTaxable, Round(ifNull(Sorder_NoneTaxable,0), ".$DecimalBase.") As SorderNoneTaxable, ".
			 " Round(if($SalesTTC=1,ifNull(Sorder_Taxable,0)*(1+$Setup_VatPercentage/100),ifNull(Sorder_Taxable,0)) + ifNull(Sorder_NoneTaxable,0), ".$DecimalBase.") As GrossTotal, ".
			 " Round(ifNull(Sorder_DiscountPer,0), ".$DecimalBase.") As SorderDiscountPer, Round(ifNull(Sorder_DiscountAmount,0), ".$DecimalBase.") As SorderDiscountAmount,  ".
			 " Round(ifNull(Sorder_VAT,0), ".$DecimalBase.") As Sorder_VAT, ".
			 " Round((if($SalesTTC=1,ifNull(Sorder_Taxable,0)*(1+$Setup_VatPercentage/100),ifNull(Sorder_Taxable,0)) + ifNull(Sorder_NoneTaxable,0) + if($SalesTTC=1,0,IfNull(Sorder_VAT,0)) - IfNull(Sorder_DiscountAmount,0) + (ifNull(Sorder_Tax1,0) + ifNull(Sorder_Tax2,0) + ifNull(Sorder_Tax3,0) + ifNull(Sorder_Tax4,0) + ifNull(Sorder_Tax5,0))), ".$DecimalBase.") As Sorder_NetTotal,  ".
			 " if(Sorder_PaymentMode=0, 'Credit', if(Sorder_PaymentMode=1,'Cash', 'Check')) As SorderPaymentMode, ".
			 " Round((ifNull(Sorder_Tax1,0) + ifNull(Sorder_Tax2,0) + ifNull(Sorder_Tax3,0) + ifNull(Sorder_Tax4,0) + ifNull(Sorder_Tax5,0)), ".$DecimalBase.") As Sorder_TotalTaxes,  ".
			 " ClientsDetail.Detail_Code As DetailCode, ClientsDetail.Detail_Name As DetailName, clients.client_address as ClientAddress, clients.Client_VATNo ".
			 " From Sorder ".
			 " Left Join Clients on Sorder.Client_code = Clients.Ledger_Number ".
			 " Left Join ClientsDetail on Sorder.Detail_Code = ClientsDetail.Detail_Code ".
			 " Left Join Salesmen on Sorder.Salesman_Code = Salesmen.Ledger_Number ".
			 " Left Join Project on Sorder.Project_Code = Project.Project_Code ".
			 " Left Join CostCent on Sorder.Costcent_code = CostCent.CostCent_Code ".
			 " Where Sorder_ID = ".$SorderId;
//echo $query;	exit;		 
			 
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
$sub_query = " Select Sorder_TypeName From SorderType ".
	             " Where Sorder_Type = '".$Sorder_Type."'"; 
	$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());
	while($sub_rows=mysql_fetch_array($sub_result)){
		extract($sub_rows);
	}	
	
echo "<table width=800>
<tr>
<td width=50%><img src='LogoFile.php' width=180 /></td>
<td width=50% style='text-align: center; font-family: Arial; font-size: 40px;  color: #a6a6a6;'>&nbsp;&nbsp;$Sorder_TypeName</td>
</tr>
</table>
<table width=800>
<tr style='height: 20px'>
<td></td>
</tr>
</table>
<table width=800>
<tr>
<td width=500 style='font-family: Arial; font-size: 14px'><i><b><u>$company_name</u></b></i>
<br><b>
<pre>$company_memo</pre>
</td>
<td style='width: 300px; vertical-align: top;'>
<table width=100%>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>DATE:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 14px'>$Sorder_Date</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>TIN:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 14px'>21204511-0001</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>Invoice #:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 14px'>$Sorder_Reference</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>Customer ID:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 14px'>$ClientCode</td>
</tr>
</table>
</td>
</tr>
<tr style='height: 15px'>
<td></td>
</tr>
</table>
<table width=800>
<tr>
<td style='font-weight: bold; font-size: 14px; font-family: Arial'>Bill To:</td>
</tr>
</table>
<table width=800>
<tr>
<td style='font-size: 14px; font-family: Arial' width=55>Name</td>
<td style='font-size: 14px; font-family: Arial' width=745>: $ClientName</td>
</tr>
<tr>
<td style='font-size: 14px; font-family: Arial' width=55>Address</td>
<td style='font-size: 14px; font-family: Arial' width=745>: $ClientAddress</td>
</tr>
<tr>
<td style='font-size: 14px; font-family: Arial' width=55>Phone</td>
<td style='font-size: 14px; font-family: Arial' width=745>: $client_telephone1 $client_telephone2</td>
</tr>";
if($Client_VAT == 1){
echo"<tr>
<td style='font-size: 14px; font-family: Arial' width=55>VAT #</td>
<td style='font-size: 14px; font-family: Arial' width=745>: $Client_VATNo</td>
</tr>";
}
echo "<tr style='height: 15px'>
<td colspan=2></td>
</tr>";
echo "<tr>
<td colspan=2 style='font-size: 14px; font-family: Arial; font-weight: bold'>Comments or special instructions:</td>
</tr>";
echo "<tr style='height: 15px'>
<td colspan=2></td>
</tr>";
echo"</table>";
echo "<table width=800 style='border-collapse:collapse'>";
echo "<tr >";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; background: #d0d0d0'>Code</th>";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 285px; background: #d0d0d0 '>Description</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 50px; background: #d0d0d0 '>Qty</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; background: #d0d0d0 '>Unit Price</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; background: #d0d0d0 '>Disc.</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; background: #d0d0d0 '>Total Price</th>";
	echo "</tr>";
	$querydt = " Select SorderDt.SorderDt_ID, SorderDt.Item_code As ItemCode, Item_Description As ItemDescription, if(Items.Item_NonStock=1, SorderDt_Remark, '') As SorderDtRemark, SorderDt.Warehouse_Code As WarehouseCode, ".
				   " SorderDt_Remark, SorderDt_Quantity, Round(SorderDt_Price, ".$DecimalBase.") As SorderDtPrice, SorderDt_PriceBase1, SroderDt_PriceBase2, ".
				   " Round(ifNull(SorderDt_Discount,0), ".$DecimalBase.") As SorderDtDiscountPer, Round(SorderDt_PriceTTC, ".$DecimalBase.") As SorderDtPriceTTC,  ".
				   " Round(((SorderDt_Quantity*SorderDt_Price)-((SorderDt_Quantity*SorderDt_Price*ifNull(SorderDt_Discount,0))/100)), ".$DecimalBase.") As SorderDtTotal, ".
				   " Round(((SorderDt_Quantity*SorderDt_PriceTTC)-((SorderDt_Quantity*SorderDt_PriceTTC*ifNull(SorderDt_Discount,0))/100)), ".$DecimalBase.") As SorderDtTotalTTC, ".
				   " 1 As ItemsCount, (SorderDt_Quantity*ifNull(Items.Item_Weight,0)) As GrossWeight, sorderdt.sorderdt_clientdiscount, sorderdt.sorderdt_itemdiscount ".
				   " From SorderDt Left Join Items on SorderDt.Item_code = Items.Item_code ".
				   " Where SorderDt.Sorder_ID = '".$SorderId."' ".
				   " And SorderDt.Item_CodePacked Is Null ".
				   " Union ".
				   " Select SorderDt.SorderDt_ID, SorderDt.Item_CodePacked As ItemCode, Item_Description As ItemDescription, if(Items.Item_NonStock=1, SorderDt_Remark, '') As SorderDtRemark, SorderDt.Warehouse_Code As WarehouseCode, ".
				   " SorderDt_Remark, ifNull(Item_qtyPacked,0) As SorderDt_Quantity, Round(Sum(SorderDt_Price), ".$DecimalBase.") As SorderDtPrice, SorderDt_PriceBase1, SroderDt_PriceBase2, ".
				   " Round(ifNull(SorderDt_Discount,0), ".$DecimalBase.") As SorderDtDiscountPer, Round(Sum(SorderDt_PriceTTC), ".$DecimalBase.") As SorderDtPriceTTC,  ".
				   " Round(Sum((SorderDt_Quantity*SorderDt_Price)-((SorderDt_Quantity*SorderDt_Price*ifNull(SorderDt_Discount,0))/100)), ".$DecimalBase.") As SorderDtTotal, ".
				   " Round(Sum((SorderDt_Quantity*SorderDt_PriceTTC)-((SorderDt_Quantity*SorderDt_PriceTTC*ifNull(SorderDt_Discount,0))/100)), ".$DecimalBase.") As SorderDtTotalTTC, ".
				   " Count(*) As ItemsCount, (SorderDt_Quantity*ifNull(Items.Item_Weight,0)) As GrossWeight, sorderdt.sorderdt_clientdiscount, sorderdt.sorderdt_itemdiscount ".
				   " From SorderDt Left Join Items on SorderDt.Item_code = Items.Item_code ".
				   " Where SorderDt.Sorder_ID = '".$SorderId."' ".
				   " And SorderDt.Item_CodePacked Is Not Null ".
				   " Group By SorderDt.Item_CodePacked, SorderDt.Warehouse_Code ";
	$resultdt = mysql_query($querydt, $connection);
	$total_beforediscount=0;
	$total_discountamount=0;
	$total_discountamount_oninvoice=0;
	$totalqty=0;
	while($rowdt = mysql_fetch_array($resultdt)){
		extract($rowdt);
		$total_beforediscount = $total_beforediscount + ($SorderDt_Quantity*$SorderDtPrice);
		$total_discountamount = $total_discountamount + ($SorderDt_Quantity*$SorderDtPrice * $sorderdt_itemdiscount / 100);
		$total_discountamount_oninvoice = $total_discountamount_oninvoice + ($SorderDt_Quantity*$SorderDtPrice * $sorderdt_clientdiscount / 100);
		
		echo "<tr >";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; vertical-align: top; '>$ItemCode</th>";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 285px; vertical-align: top;'>$ItemDescription</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 50px; vertical-align: middle; '>$SorderDt_Quantity</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;'>".number_format($SorderDtPrice, $DecimalBase)."</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; vertical-align: middle; text-align: right;'>$SorderDtDiscountPer %</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;'>".number_format($SorderDtTotal, $DecimalBase)."</th>";
	echo "</tr>";
	$totalqty=$totalqty+$SorderDt_Quantity;
	}
	//echo $total_beforediscount;
	$total_afterdiscount=$total_beforediscount-$total_discountamount-$total_discountamount_oninvoice + $Sorder_VAT;
	echo "<tr>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;  border-top: none;border-bottom: none;background: #d0d0d0;'></td>
	<td colspan=1 style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; background: #d0d0d0; text-align: left; border-bottom: none; font-weight: bold'>Total Qty</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; border-top: none;border-bottom: none;text-align:center;font-weight: bold;'>".cleanNumber(number_format($totalqty, 4))."</td>
	<td colspan=2 style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; border-bottom: none; font-weight: bold'>Gross Total</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right; border-bottom: none'>".number_format($total_beforediscount, $DecimalBase)."</td>
	</tr>
	<tr>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;  border-top: none;border-bottom: none;background: #d0d0d0;'></td>
	<td colspan=1 style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; border-bottom: none; font-weight: bold;border-top: none;'></td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;  border-top: none;border-bottom: none;background: #d0d0d0;'></td>
	<td colspan=2 style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; border-bottom: none; font-weight: bold; border-top: none'>Discount</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right; border-bottom: none; border-top: none'>".number_format($SorderDiscountAmount, $DecimalBase)."</td> 
	</tr>
	<tr>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;  border-top: none;border-bottom: none;background: #d0d0d0;'></td>
	<td colspan=1 style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; border-bottom: none; font-weight: bold;border-top: none;'></td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;  border-top: none;border-bottom: none;background: #d0d0d0;'></td>
	<td colspan=2 style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; border-bottom: none; font-weight: bold; border-top: none'>VAT $Setup_VatPercentage%</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right; border-bottom: none; border-top: none'>".number_format($Sorder_VAT, $DecimalBase)."</td>
	</tr>
	<tr>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;  border-top: none;background: #d0d0d0;'></td>
	<td colspan=1 style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; font-weight: bold;border-top: none;'></td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;  border-top: none;background: #d0d0d0;'></td>
	<td colspan=2 style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; font-weight: bold; border-top: none'>NET Total</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;  border-top: none'>".number_format($total_afterdiscount, $DecimalBase)."</td>
	</tr>
	<tr style='height: 35px'>
	<th colspan=6></th>
	</tr>
	</tr>
	<tr>
	<td colspan=6 style='font-size: 14px; font-weight: none; font-family: Arial'>$setup_invoiceremarks</td>
	</tr>
	<tr style='height: 20px'>
	<td colspan='6'><b>Inspected By:</b></td>
	</tr>
	<tr style='height: 20px'>
	</tr>
	<tr style='height: 20px'>
	<td colspan='3' style='text-align:left;'>Manager's Sign:---------------------------------</td>
	<td colspan='3' style='text-align:left;'>Customer's Sign:---------------------------------</td>
	</tr>
	<tr style='height: 45px'>
	<th colspan=6>
	</th>
	</tr>
	<tr>
	<th colspan=6 style='font-size: 14px; font-family: Arial'>THANK YOU FOR YOUR BUSINESS!</th>
	</tr>
	";
echo"</table>";
function cleanNumber($num){
    $explode = explode('.', $num);
    $count   = strlen(rtrim($explode[1],'0'));
    return bcmul("$num",'1', $count);
}
print <<<HERE
<script>
window.print();
</script>
</body>
</html>
HERE;
?>