<?php
session_start();

if($_SESSION['dbName']==""){
header("location:../index.php");
}
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");

//echo " hello "; exit;
echo "<title>Preperation Order Report</title>";
echo "<style>
body,html{width:99.8%; height:99%;  }
#container {
   min-height:99%; 
   position:relative;
}


a {outline:none ; text-decoration:none;}

area {outline:none}
p{padding:0px; margin:0px;}
</style>";

$SorderId      = $_GET['SorderId'];


$query_setup = " select COMPANY_NAME as CompanyName, Company_memo as CompanyAddress, COMPANY_TELEPHONE as CompanyPhone, Setup_vatno as VATNO, ".
			   " DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, Company_memo As CompanyMemo, ".
               " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage, SalesTTC from setup ";
			   
$result_setup = mysql_query($query_setup, $connection);
while($row_setup = mysql_fetch_array($result_setup)){
	extract($row_setup);
}
echo "<div style='min-height: 100%; position: relative;'> ";

$query_sorder = " 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, clients.Client_Address as ClientAddress, clients.Client_Telephone1, Client_VAT, Client_Fax, Client_CreditLimit,  ".
			 " Client_PaymentTerms,  Sorder_ClientContact, Sorder_DeliveryPlace, sorder_type as sorder_type, ".
			 " 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,  ".
			 " Round((if($SalesTTC=1,ifNull(Sorder_TaxableBase1,0)*(1+$Setup_VatPercentage/100),ifNull(Sorder_TaxableBase1,0)) + ifNull(Sorder_NoneTaxableBase1,0) + if($SalesTTC=1,0,IfNull(Sorder_VatBase1,0)) - IfNull(Sorder_DiscountBase1,0) + (ifNull(sorder_Tax1Base1,0) + ifNull(sorder_Tax2Base1,0) + ifNull(Sorder_Tax3Base1,0) + ifNull(Sorder_Tax4Base1,0) + ifNull(Sorder_Tax5Base1,0))), ".$DecimalBase.") As Sorder_NetTotalBase1,  ".
			 " 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, clients.client_financialno as ClientMOF, date_format(sorder.Sorder_ClientSorderDate, '%d/%m/%Y') as ClientSorderDate, Sorder_DeliveryPlace, Sorder_ShipName as SorderContact, Sorder_ClientPhone as ClientContactPhone, Sorder_ShippingWay as CardMessage,".
			 " ClientsDetail.Detail_Code As DetailCode, ClientsDetail.Detail_Name As DetailName, sorder.Sorder_VatBase1 ".
			 " 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_delivery;
$result_sorder = mysql_query($query_sorder, $connection); 
$row_sorder    = mysql_fetch_array($result_sorder);
extract($row_sorder);
print_header($query_sorder, $CompanyName, $CompanyAddress, $sorder_type, $connection);	


$query_sorderdt = " Select SorderDt.SorderDt_ID, SorderDt.Item_code As ItemCode, Item_Description As ItemDescription, SorderDt.Warehouse_Code As WarehouseCode, ".
				   " SorderDt_Remark, SorderDt_Quantity, Round(SorderDt_Price, ".$DecimalBase.") As SorderDtPrice, SorderDt_PriceBase1, SroderDt_PriceBase2, ".
				   " Round(SorderDt_Discount, ".$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 ".
				   " From SorderDt Left Join Items on SorderDt.Item_code = Items.Item_code ".
				   " Where SorderDt.Sorder_ID = ".$SorderId;

//echo $query_sorderdt; exit;

$result_sorderdt = mysql_query($query_sorderdt, $connection);
$numsorderdt = mysql_num_rows($result_sorderdt);
$i=1;
while($row_sorderdt = mysql_fetch_array($result_sorderdt))
{
	extract($row_sorderdt);
	$totalX = $SorderDtPrice * $SorderDt_Quantity;
	$discountX = (($totalX - $SorderDtTotal) * 100) / $totalX;
	
	echo "<table width=1000 cellspacing='0' cellpading='0'><col width='300px'><col width='20px'><col width='380px'><col width='300px'>";
	echo "<tr>
			<th style='font-size: 16px; font-family: Times New Roman; border-top: solid 2px #000000;border-left: solid 2px #000000; border-bottom: solid 2px #000000; border-right: solid 0.5px #000000;'>Code</th>
			<th style='font-size: 16px; font-family: Times New Roman; border-top: solid 2px #000000;border-bottom: solid 2px #000000; border-right: solid 0.5px #000000; ' colspan='2'>Description</th>
			<th style='font-size: 16px; font-family: Times New Roman; border-top: solid 2px #000000; border-bottom: solid 2px #000000; border-right: solid 0.5px #000000; '>Quantity</th>
		</tr>";
	echo "<tr>
			<td style='font-size: 16px; font-family: Times New Roman; '>".$ItemCode."</td>
			<td style='font-size: 16px; font-family: Times New Roman; ' colspan='2'>".$ItemDescription."</td>
			<td style='font-size: 16px; font-family: Times New Roman;  text-align: center'>".$SorderDt_Quantity."</td>
		</tr>";
	
	echo "<tr><td colspan='2'><table width='320px' cellpading='0' cellspacing='0'><col width='120px'><col width='200px'>";
        if ($sorder_type != 'PF'){	
	      $querydt = "select ItemParam_Code, itemdt_value from itemsdt where item_code='$ItemCode' order by ItemParam_Code;";}
        if ($sorder_type == 'PF') { $querydt = "select ItemParam_Code, itemdt_value from temp_itemsdt where transid='$SorderDt_ID' order by ItemParam_Code;";}	
	$resdt = mysql_query($querydt,$connection);
	$numdt = mysql_num_rows($resdt);
	if($numdt>0){
		echo "<tr height='20px'>
				<td style='font-size: 16px; font-family: Times New Roman;border-bottom: 1px dotted black; ' colspan='2' ><i><b>Attributes</b></i></td>
			</tr>";
		while($rwdt = mysql_fetch_array($resdt)){
			extract($rwdt);
			echo "<tr height='20px'>
				<td style='font-size: 16px; font-family: Times New Roman; ' >$ItemParam_Code</td>
				<td style='font-size: 16px; font-family: Times New Roman; ' >$itemdt_value</td>
			</tr>";
		}
		
	}
	echo "</table></td>";
	
	if ($sorder_type != 'PF'){
		$queryimg = "select Item_Picture, item_picture2 from items where item_code='$ItemCode';"; }
	
        if ($sorder_type == 'PF') { $queryimg = "select Item_Picture, item_picture2 from temp_items where trans_id='$SorderDt_ID';";}
	
       
        //echo $queryimg;exit;
	$resimg = mysql_query($queryimg,$connection);
	$rowimg = mysql_fetch_row($resimg);
	$item_picture = $rowimg[0];
	$item_picture2 = $rowimg[1];
         
	echo "<td align='center' colspan='2'>";
	if($item_picture != ''){
		echo "<img src='uploads/$item_picture' width='320px' height='430px'>";
	}
	if($item_picture2 != ''){
		echo "<img src='uploads/$item_picture2' width='320px' height='430px'>";
	}
	echo "</td></tr>";
	if($SorderDt_Remark != ''){
		echo "<tr height='30px'>
				<td style='font-size: 20px; font-family: Times New Roman;border-bottom: 1px dotted black; ' colspan='4'><i><b>Remark</b></i></td>
			</tr>";
		echo "<tr height='10px'>
			<td colspan='4'></td>
		</tr>";
		echo "<tr  >
				<td style='font-size: 20px; font-family: Times New Roman; ' colspan='4'>";
				echo nl2br($SorderDt_Remark);
		echo "	</td>
			</tr>";	
	}
	
	if($numsorderdt != $i)
	echo "<tr height='20px'>
				<td style='font-size: 20px; font-family: Times New Roman;border-bottom: 1px dotted black; ' colspan='4'><hr></td>
			</tr>";
	echo "</table>";
	if($numsorderdt != $i){
		echo "<div style='page-break-after:always;'></div>";	
		print_header($query_sorder, $CompanyName, $CompanyAddress, $sorder_type, $connection);
	}
	$i++;
}

function print_header($query_sorder, $CompanyName, $CompanyAddress, $sorder_type, $connection) {
		
	$result_sorder = mysql_query($query_sorder, $connection); 
	$row_sorder    = mysql_fetch_array($result_sorder);
	extract($row_sorder);
	
	echo "<table width=1000>
			<tr style='height: 40px'>
				<td style='text-align: left; width: 300px; vertical-align: top' colspan='3'></td>
				<td colspan='2' style='width: 500px'></td>
				<td style='font-size: 14px; font-family: Times New Roman; width: 200px; vertical-align: top; text-align: right' colspan='3'>
					<span style='font-size: 18px; font-weight: bold; font-family: Times New Roman; padding-top: 10px'>".$CompanyName." </span><div style='height: 10px'></div>".$CompanyAddress."<br>
				</td>
			</tr>
		</table>";


	if ($sorder_type != 'PF') {

	echo "<table width=1000>
			<tr style='height: 40px'>
				<td  style='text-align: center; font-size: 22px; font-weight: bold; padding-bottom: 10px; border-bottom: solid 2px #000000; font-family: Times New Roman'>Sales Order No. ".$Sorder_Reference." </td>
			</tr>
		</table>";
	}

	if ($sorder_type == 'PF') {

	echo "<table width=1000>
			<tr style='height: 40px'>
				<td  style='text-align: center; font-size: 22px; font-weight: bold; padding-bottom: 10px; border-bottom: solid 2px #000000; font-family: Times New Roman'>Proforma Invoice No. ".$Sorder_Reference." </td>
			</tr>
		</table>";
	}



	echo "<table width=1000><col width='160px'><col width='200px'><col width='280px'><col width='160px'><col width='200px'>";
	echo "<tr>
			<td style='font-size: 14px; font-weight: bold; background-color: #a9abac; vertical-align: top'>Client</td>
			<td style='font-size: 14px; font-family: Times New Roman; vertical-align: top'>".$ClientCode."</td>
			<td style='font-size: 14px; font-family: Times New Roman; vertical-align: top'>".$ClientName."</td>
			<td style='font-size: 14px; font-weight: bold; background-color: #a9abac; vertical-align: top'>Invoice Date</td>
			<td style='font-size: 14px; vertical-align: top'>".$Sorder_Date."</td>
		</tr>";

	echo "<tr>
			<td ></td>
			<td colspan='2'></td>
			<td style='font-size: 14px; font-weight: bold; background-color: #a9abac; vertical-align: top'>Delivery Date</td>
			<td style='font-size: 14px; vertical-align: top'>".$RequestDate."</td>
		</tr>";		

	echo "<tr height='30px'>
			<td colspan='5'></td>
		</tr>";	
	echo "</table>";

	
	
}
/*
echo "<table width=800 cellspacing='0' cellpading='0'><col width='350px'><col width='100px'><col width='350px'>";
echo "<tr height='30px'><td colspan='3'></td></tr>";
echo "<tr height='30px'>
		<td  style='font-size: 16px; font-weight: bold'>Date: ____________________________</td>
		<td></td>
		<td  style='text-align: right; font-size: 16px; font-weight: bold'>Signature: ____________________________</td>		
	  </tr>";	
echo "</table>";
*/

echo "</div>";
print <<< HERE

<script language=javascript>
 
if (typeof(window.print) != 'undefined')
{
    window.print();
} 
</script>
</BODY>
</HTML>
HERE;

?>