<?php

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

print <<< HERE
<HTML>
<HEAD>
<TITLE> Work Order  </TITLE>

<style type="text/css">

table.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Calibri;
                }				 
table.master td { font-size: 14px; 
                  font-family:Calibri;
                 }
table.detail th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Calibri;
                }				 
				 
table.detail td { font-size: 12px; 
                  font-family:Calibri;
                 }				 
				 
table.detail td.sr  { font-size: 10px; 
					  font-style: italic; 
                      font-family:Calibri;
                    }
				 
table.totals td.labels { font-size: 14px; 
                         font-weight: bold;
                         font-family:Calibri;
						 padding-left:5px;
						 border-width: 1px;
						 border-style: none solid solid solid;
  						 border-color: #666666;
                       }					  
table.totals td.amounts { font-size: 14px; 
                          font-family:Calibri;
						  border-width: 1px;
						  border-style: none none solid none;
  						  border-color: #666666;
				        }
table.totals td.totalborder {
								font-size: 14px; 
								font-family:Calibri;
								border-width: 1px;
								border-style: none solid solid none;
								border-color: #666666;
							}						
				 
h4 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }
   
h5 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 2px;
   }   
   
h3 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 2px;
	 width: 180px;
   }
   
</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($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");


//Read From Login Table
$querylog=" select ifNull(FI_DeliverySalesPrice,0) As FI_DeliverySalesPrice ".
          " from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, Setup_VatNo as SetupVatNo,  ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage, ".
			 " Company_Name as CompanyName, Company_Memo as CompanyMemo, Company_City as CompanyWebsite, Company_Country as CompanyEmail  ".
			 " From Setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//Read From SalesSetup Table
$querySalesSetup="Select SalesTTC, SorderComment From SalesSetup";
$resultSalesSetup=mysql_query($querySalesSetup,$connection) or die(mysql_error());
while($rowSalesSetup=mysql_fetch_array($resultSalesSetup)){
	extract($rowSalesSetup);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_AdditionalTaxes,0) As Module_AdditionalTaxes, ".
             " ifNull(Module_ShowLayoutProj,0) As Module_ShowLayoutProj, ifNull(Module_ShowLayoutCost,0) As Module_ShowLayoutCost, ifNull(Module_ShowWarehouseSalesorder,0) As Module_ShowWarehouseSalesorder From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}
	
$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);




if($ProductionId != '')
{
	$query = " Select Production_ID, Production_Reference, Date_Format(Production_Date,'%d/%m/%Y') As ProductionDate, ".
	         " Date_Format(Production_ExpireDate,'%d/%m/%Y') As ExpireDate, PreProduction.Currency_Code As CurrencyCode, ".
			 " PreProduction.Item_code, Item_Description, PreProduction.Warehouse_Code, Warehouse_Name, Production_Quantity,  ".
			 " Production_IdealQty,  ifNull(Production_IdealQty,0) - ifNull(Production_Quantity,0) As ProductionWasteCalc, ".
			 " Production_Waste, Production_Remark, Production_Type, PreProduction.Project_Code As ProjectCode, Project_Name, ".
             " PreProduction.Costcenter_code As Costcentcode, Costcent_name,Client_code,  ".
			 " Round(ifnull(Production_Total,0), ".$DecimalBase.") As ProductionTotal, ".
			 " Round(ifnull(Production_IdealTotal,0), ".$DecimalBase.") As ProductionIdealTotal, ".
			 " Round(Production_Total/ifNull(Production_Quantity,1), ".$DecimalBase.") As CostPricePerUnit, ".
			 " Round(Production_IdealTotal/ifNull(Production_IdealQty,1), ".$DecimalBase.") As CostPricePerUnitIdeal,Production_unit,Production_mixer,Production_size ".
			 " ,if(Production_size=0,0,(Production_Quantity/Production_size)) as ProdLine ".
			 " From PreProduction ".
			 " Left Join Items on PreProduction.Item_code = Items.Item_code ".
			 " Left Join Warehouse on PreProduction.Warehouse_Code = Warehouse.Warehouse_Code ".
			 " Left Join Project on PreProduction.Project_Code = Project.Project_Code ".
			 " Left Join CostCent on PreProduction.Costcenter_code = CostCent.CostCent_Code ".
	         " Where Production_ID = ".$ProductionId;
//echo $query;			 
			 
	$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);
	}
	
	//Read From Project Table
	$queryProject="Select Project_Memo As ProjectMemo From Project Where Project_Code = '".$ProjectCode."'";
	$resultProject=mysql_query($queryProject,$connection) or die(mysql_error());
	while($rowProject=mysql_fetch_array($resultProject)){
		extract($rowProject);
	}
	
	//Get Client Balance
	$querybalance = "SELECT IFNULL(SUM(if(journaldt_dbcd = 'D', journaldt_amount1, (-1)*journaldt_amount1)), 0) As Balance FROM journaldt where ledger_number = '".$ClientCode."'";
	//echo "<br><br>".$querybalance;
	$resultbalance=mysql_query($querybalance,$connection) or die(mysql_error());
	$rowbalance=mysql_fetch_array($resultbalance);
	$ClientBalance = $rowbalance[0];
	
	
	echo "<table border=0 width=100% cellspacing=0 cellpadding=0>";
	echo "<tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=165>
				  <col width=270>";
		  
	echo"<tr>";
	echo"<td rowspan='2' colspan=2></td><td colspan=1 >";
	echo"</table></td></tr>";
	
		echo "<table border=0 width='750' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=150>
			<col width=200>";
			
			echo "<tr><td align='left' colspan='2'><img src='./img/logo2.png' width='180px' height='70px'/></td>
      <td align='right' colspan='2' style='font-size: 32;font-weight:bold; font-weight: 900;color: silver;'>Work Order</td></tr>";

			
			
	echo"</table>";
	
	echo "<tr><td align='left'>";
	echo "<table border=0 width='750' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		     <col width=500>
			 <col width=250>";

    echo "<tr>";
	echo "	<td align='left'  valign='top' style='font-size:20px;  font-weight:bold;'>".nl2br($CompanyName)."</td>";
	echo "<td align='left'    style='font-size:12px;'>Date :$ProductionDate</td></tr>";
	
	echo "<tr>";
	echo "	<td align='left'  valign='top' style='font-size:20px;  font-weight:bold;'></td>";
	echo "<td align='left'    style='font-size:12px;'>Work Order #:[$Production_Reference] </td></tr>";
	
	echo "<tr>";
	echo "	<td align='left'  valign='top' style='font-size:20px;  font-weight:bold;'></td>";
	echo "<td align='left'    style='font-size:12px;'>Customer ID :$Client_code </td></tr>";
	
	

	echo "</table></td></tr>";
	echo "<tr height='50'><td>&nbsp;</td></tr>";
	    $bright='border-right:black solid 1px;';
	$brleft='border-left:black solid 1px;';
	$brtop='border-top:black solid 1px;';
	$brbottom='border-bottom:black solid 1px;';
	echo "<tr><td align='left'>";
	echo "<table border=0 width='750' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		     <col width=125>
			 <col width=400>
			 <col width=100>
			 <col width=125>";

    echo "<tr>";
	echo "	<td align='left'  valign='top' style='font-size:12px;  font-weight:bold;'>Reference</td>";
	echo "<td align='left' colspan=3   style='font-size:12px;'>: $Item_code-$Item_Description</td></tr>";
	
	 echo "<tr>";
	echo "	<td align='left'  valign='top' style='font-size:12px;  font-weight:bold;'>QTY</td>";
	echo "<td align='left'  colspan=3  style='font-size:12px;'>: $Production_Quantity</td></tr>";
	
	 echo "<tr>";
	echo "	<td align='left'  valign='top' style='font-size:12px;  font-weight:bold;'>PRODUCTION UNIT</td>";
	echo "<td align='left'  colspan=3  style='font-size:12px;'>: $Production_unit </td></tr>";
	
	 echo "<tr>";
	echo "	<td align='left'  valign='top' style='font-size:12px;  font-weight:bold;'>MIXER</td>";
	echo "<td align='left' colspan=3   style='font-size:12px;'>: $Production_mixer</td></tr>";	
	
	 echo "<tr>";
	echo "	<td align='left'  valign='top' style='font-size:12px;  font-weight:bold;'>SIZE</td>";
	echo "<td align='left'     valign='top' style='font-size:12px;'>: $Production_size </td>";
	echo "	<td align='right'   style='font-size:30px;  font-weight:bold;'>LINES </td>";
	
	if ($ProdLine<1)
	echo "<td align='center'    valign='top' style='font-size:30px; font-weight:bold;'>: 1 </td></tr>";	
    else echo "<td align='center'    valign='top' style='font-size:30px; font-weight:bold;'>: $ProdLine </td></tr>";	
	 
	
	echo "</table></td></tr>";
	

	echo "<tr height='50'><td>&nbsp;</td></tr>";
	
echo "<tr><td align='left'>";
    $bright='border-right:black solid 1px;';
	$brleft='border-left:black solid 1px;';
	$brtop='border-top:black solid 1px;';
	$brbottom='border-bottom:black solid 1px;';
	echo "<tr height='40'><td></td></tr>";
	
	

	
	
	
	
    $bright='border-right:black solid 1px;';
	$brleft='border-left:black solid 1px;';
	$brtop='border-top:black solid 1px;';
	$brbottom='border-bottom:black solid 1px;';
	
	$querydt = " Select PreProductionDt.ProductionDt_ID, PreProductionDt.Item_CodePacked, PreProductionDt.Item_code As ItemCode, Item_Description As ItemDescription, ".
			   " PreProductionDt.Warehouse_Code As WarehouseCode, if(Items.Item_NonStock=1, ProductionDt_Remark, '') As ProductionDtRemark, ProductionDt_Quantity, ".
			   " ProductionDt_IdealQty, Round(ProductionDt_Cost, ".$DecimalBase.") As ProductionDtCost, ProductionDt_CostBase1, ProductionDt_CostBase2, ".
			   " ifNull(ProductionDt_IdealQty,0) - ifNull(ProductionDt_Quantity,0) As ProductionDtWasteCalc, ProductionDt_Waste, ".
			   " Round((ProductionDt_Quantity*ProductionDt_Cost), ".$DecimalBase.") As TotalActual, ".
			   " Round((ProductionDt_IdealQty*ProductionDt_Cost), ".$DecimalBase.") As TotalIdeal ".
			   " From PreProductionDt ".
			   " Left Join Items on PreProductionDt.Item_code = Items.Item_code ".
			   " Where PreProductionDt.Production_ID = ".$ProductionId;

	//echo $querydt;
	echo "<tr><td align='left'>";
	echo "<table border=0 width='750' cellspacing=0 class='detail' style='table-layout:fixed;' >
		  <col width=225>
		  <col width=50>
		  <col width=75>
		  <col width=75>
		  <col width=75>
		  <col width=75>
		  <col width=175>";
	
	echo "<tr bgcolor=#D8D8D8>";
	

		echo "   <tr height='20' bgcolor='#D8D8D8' >
									<td style='font-size: 12pt;font-weight: bold;$bright;$brleft;$brbottom;$brtop'  align='left' > Item </td>
									<td style='font-size: 12pt;font-weight: bold; $bright;$brbottom;$brtop'  align='center'> % </td>
									<td style='font-size: 12pt;font-weight: bold; $bright;$brbottom;$brtop'  align='center'> Bags WT </td>
									<td style='font-size: 12pt;font-weight: bold; $bright;$brbottom;$brtop'  align='center'  > KGS </td>
									<td style='font-size: 12pt;font-weight: bold;$bright;$brbottom;$brtop'  align='center'  > Bags </td>
									<td style='font-size: 12pt;font-weight: bold;$bright;$brbottom;$brtop'  align='center'  > Bal./KGS </td>
									<td style='font-size: 12pt;font-weight: bold;$bright;$brbottom;$brtop' align='left'> Notes </td>
								</tr>";
		
	
	
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
	$Subtotal = 0;	
	$totalOrderAmount =0;
	$vatCount=0;
	$Freight=0;
	$SerialNo=0;
	$SubQty=0;
	$SubAdd=0;
	
	while($rows=mysql_fetch_array($resultdt)){
		extract($rows);
		
		
		$SerialNo = $SerialNo + 1;
		
			echo "<tr height='20'>";
			
			echo "	<td align='left' style='font-size: 12pt;$brleft;$bright;$brbottom;' valign='top'>&nbsp;$ItemCode&nbsp;</td>";
			echo "  <td align='left' style='font-size: 12pt;font-weight: bold;$bright;$brbottom; ' valign='top'>&nbsp;</td>";
            echo "  <td align='right' style='font-size: 12pt;font-weight: bold;$bright;$brbottom; ' valign='top'>&nbsp;</td>";			
			echo "  <td align='right' style='font-size: 12pt;font-weight: bold;$bright;$brbottom; ' valign='top'>$ProductionDt_Quantity&nbsp;</td>";
			echo "  <td align='left' style='font-size: 12pt;$bright;$brbottom; ' valign='top'>&nbsp;</td>";
			echo "  <td align='left' style='font-size: 12pt;$bright;$brbottom; ' valign='top'>&nbsp;</td>";
			echo "  <td align='right' style='font-size: 12pt;$bright;$brbottom; ' valign='top'>&nbsp;</td>";
			echo "</tr>";
			
			$Subtotal = $Subtotal+$SaleDtTotal;
			if ($productiondt_initialQty==0)
			$SubQty = $SubQty+$ProductionDt_Quantity;
			else $SubQty = $SubQty+$productiondt_initialQty;
			$SubAdd = $SubAdd+$productiondt_addition;
		

	}
	
	

	while($SerialNo<20){
     $SerialNo=$SerialNo+1;
	 
		echo "   <tr height='20' >";
			echo "	<td align='left' style='font-size: 12pt;$brleft;$bright;$brbottom;' valign='top'>&nbsp;&nbsp;</td>";
            echo "  <td align='right' style='font-size: 12pt;font-weight: bold;$bright;$brbottom; ' valign='top'>&nbsp;</td>";	
            echo "  <td align='right' style='font-size: 12pt;font-weight: bold;$bright;$brbottom; ' valign='top'>&nbsp;</td>";				
			echo "  <td align='left' style='font-size: 12pt;font-weight: bold;$bright;$brbottom; ' valign='top'>&nbsp;</td>";
			echo "  <td align='left' style='font-size: 12pt;$bright;$brbottom; ' valign='top'>&nbsp;</td>";
			echo "  <td align='left' style='font-size: 12pt;$bright;$brbottom; ' valign='top'>&nbsp;</td>";
			echo "  <td align='right' style='font-size: 12pt;$bright;$brbottom; ' valign='top'>&nbsp;</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=javascript>
if (typeof(window.print) != 'undefined')
{
    window.print();
}
</script>
</BODY>
</HTML>
HERE;
?>

