<?php


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

print <<< HERE
<HTML>
<HEAD>
<TITLE> Return Waybill 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 th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
table.master td { font-size: 14px; 
                  font-family:Times-New-Roman;
                 }
table.detail th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
				 
table.detail td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }				 
				 
table.detail td.sr  { font-size: 10px; 
					  font-style: italic; 
                      font-family:Times-New-Roman;
                    }
				 
table.totals td.labels { font-size: 14px; 
                         font-weight: bold;
                         font-family:Times-New-Roman;
						 border-width: 1px;
						 border-style: none solid solid solid;
  						 border-color: #666666;
                       }					  
table.totals td.amounts { font-size: 14px; 
                          font-family:Times-New-Roman;
						  border-width: 1px;
						  border-style: none none solid none;
  						  border-color: #666666;
				        }
table.totals td.totalborder {
								font-size: 14px; 
								font-family:Times-New-Roman;
								border-width: 1px;
								border-style: none solid solid none;
								border-color: #666666;
							}	
table.totals td.labels1 { font-size: 14px; 
                          font-weight: bold;
                          font-family:Times-New-Roman;
					   	  border-width: 1px;
						  border-style: none none solid solid;
  						  border-color: #666666;
                        }							
				 

</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 Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage 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 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 ".
			 " 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]);

$TotalQty = 0;

if($SalesId != '')
{
			 
	$query = " Select Sale_ID, Sale_Reference, Date_Format(Sale_Date,'%d/%m/%Y') As Sale_Date, ".
	         " Date_Format(JournalDt_ValueDate,'%d/%m/%Y') As MaturityDate, ifNull(Sales.Journal_ID,0) As JournalID, ".
			 " Sales.Client_code As ClientCode, Clients.Ledger_name As ClientName, Client_VAT, Client_Telephone1, Client_CreditLimit, Client_Address As ClientAddress,   ".
			 " Client_VATNo, Sales.Currency_code As CurrencyCode, Client_Address, Sale_ShippedTo, Sale_Marks, Sale_ShippedVia, ".
			 " Sale_Remark, Sales.Project_Code As ProjectCode, Project_Name, Sales.Costcenter_code As Costcentcode, Costcent_name,  ".
			 " Round(ifNull(Sale_Taxable,0), ".$DecimalBase.") As SaleTaxable, Round(ifNull(Sale_NoneTaxable,0), ".$DecimalBase.") As SaleNoneTaxable, ".
			 " Round(ifNull(Sale_Total,0), ".$DecimalBase.") As GrossTotal, ".
			 " Round(ifNull(Sale_DiscountPer,0), ".$DecimalBase.") As SaleDiscountPer, Round(ifNull(Sale_DiscountAmount,0), ".$DecimalBase.") As SaleDiscountAmount,  ".
			 " Round(ifNull(Sale_VAT,0), ".$DecimalBase.") As Sale_VAT, ".
			 " Round((ifNull(Sale_Total,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_NetTotal,  ".
			 " Round((ifNull(Sale_Tax1,0) + ifNull(Sale_Tax2,0) + ifNull(Sale_Tax3,0) + ifNull(Sale_Tax4,0) + ifNull(Sale_Tax5,0)), ".$DecimalBase.") As Sale_TotalTaxes, Round(ifNull(Sale_DiscountTTC,0), ".$DecimalBase.") As SaleDiscountTTC, ".
			 " Round((ifNull(Sale_Total,0) + IfNull(Sale_VAT,0) - IfNull(Sale_DiscountTTC,0) + (ifNull(Sale_Tax1,0) + ifNull(Sale_Tax2,0) + ifNull(Sale_Tax3,0) + ifNull(Sale_Tax4,0) + ifNull(Sale_Tax5,0))), ".$DecimalBase.") As Sale_NetTotalTTC  ".
			 " From Sales ".
			 " Left Join Clients on Sales.Client_code = Clients.Ledger_Number ".
			 " Left Join Project on Sales.Project_Code = Project.Project_Code ".
			 " Left Join CostCent on Sales.Costcenter_code = CostCent.CostCent_Code ".
	         " Where Sales.Sale_ID = '".$SalesId."' ".
			 " And Sales.Journal_Type = 'SR' ";
			 
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
	
	if (empty($JournalID)) $JournalID = 0;
	//Get Client Balance
	$queryprevbalance = "SELECT IFNULL(SUM(if(journaldt_dbcd = 'D', journaldt_amount1, (-1)*journaldt_amount1)), 0) As Balance FROM journaldt where ledger_number = '".$ClientCode."' And Journal_Id != $JournalID ";
	//echo "<br><br>".$queryprevbalance;
	$resultprevbalance=mysql_query($queryprevbalance,$connection) or die(mysql_error());
	$rowprevbalance=mysql_fetch_array($resultprevbalance);
	$ClientPrevBalance = $rowprevbalance[0];
	
	$title = 'Return Waybill No '.$Sale_Reference;
		
	echo "<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing=2 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=115>
		  <col width=320>
		  <col width=95>
		  <col width=110>";
	echo "<tr><td height='60'><img src='LogoFile.php' width='115' height='50' /></td><td align='center'><b><font size=5>$title </font></td><td align='right' colspan=2 ><font size='2'><b>Printed&nbsp;Date&nbsp;&amp;&nbsp;Time:</b>&nbsp;&nbsp;";
	echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
	echo "<br>".$dt[1]." ".$dt[2];
	echo "</font></td></tr>";
	
	echo "<tr height='10'><td colspan=4><hr></td></tr>";
	
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Client</th>";
	echo "  <td align='left' >$ClientCode&nbsp;&nbsp;$ClientName</td>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Date</th>";
	echo "  <td align='left' >$Sale_Date</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "  <th align='left' valign='top' bgcolor='#D8D8D8'>Address</th>";
	echo "  <td align='left' >$ClientAddress</td>";
	echo "  <th align='left' valign='top' bgcolor='#D8D8D8'>Currency</th>";
	echo "  <td align='left' valign='top' >$CurrencyCode</td>";
	echo "</tr>";
	
	
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Telephone 1</th>";
	echo "  <td align='left' >$Client_Telephone1</td>";
	echo "  <th align='left' bgcolor='#D8D8D8'>VAT No.</th>";
	echo "  <td align='left' >$Client_VATNo</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Remark</th>";
	echo "  <td align='left' colspan='3'>$Sale_Remark</td>";
	echo "</tr>";
	
	if ($Module_ShowLayoutProj	== 1) {
		echo "<tr>";
		echo "  <th align='left' bgcolor='#D8D8D8'>Project</th>";
		echo "  <td align='left' colspan='3'>$ProjectCode &nbsp;&nbsp;$Project_Name</td>";
		echo "</tr>";
	}	
	
	if ($Module_ShowLayoutCost	== 1) {
		echo "<tr>";
		echo "  <th align='left' bgcolor='#D8D8D8'>Cost Center</th>";
		echo "  <td align='left' colspan='3'>$Costcentcode&nbsp;&nbsp;$Costcent_name</td>";
		echo "</tr>";
	}	
	
	
	echo "<tr height='10'><td colspan=4><hr></td></tr>";
	echo "</table></td></tr>";

	
		   
	$querydt = " Select SalesDt.SaleDt_id, SalesDt.Item_CodePacked, SalesDt.Item_code, if(Items.Item_NonStock=0, Item_Description, SalesDt_Remark) As ItemDescription, SalesDt.Warehouse_Code As WarehouseCode, ".
			   " SalesDt_Remark, 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 SalesDt Left Join Items on SalesDt.Item_code = Items.Item_code ".
			   " Where SalesDt.Sale_ID = ".$SalesId.
			   " Order By SalesDt.Item_CodePacked, SalesDt.Item_code  ";
					   
	//echo $querydt;
	echo "<tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing=2 class='detail' style='table-layout:fixed;' >
		  <col width=110>
		  <col width=360>
		  <col width=80>
		  <col width=85>
		  ";
	echo "<tr bgcolor=#D8D8D8>";
	echo "   <th align='left'>Code</th>";
	echo "   <th align='left'>Description</th>";
	echo "	 <th align='center'>Wh</th>";
	echo "	 <th align='center'>Qty.</th>";
	echo "</tr>";			
		  
	echo "<tr height='10'><td colspan=7><hr></td></tr>";
	
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultdt)){
		extract($rows);
		
		$TotalQty = $TotalQty + $SaleDt_Quantity;
		
		echo "<tr>";
		echo "  <td align='left' valign='top'>$Item_code</td>";
		echo "  <td align='left' valign='top'>$ItemDescription</td>";
		echo "  <td align='left' valign='top'>$WarehouseCode</td>";
		echo "	<td align='right' valign='top'>".number_format($SaleDt_Quantity, $DecimalBase)."</td>";
		echo "</tr>";
		
		
		
		
		//echo "<tr height='10'><td colspan=7><hr></td></tr>";
	}
	
	echo "<tr height='10'><td colspan=7><hr></td></tr>";
	
	echo "<tr>";
	echo "   <td align='left' colspan='2'>&nbsp;</td>";
	echo "	 <th align='left' class='labels'>Total Qty.</th>";
	echo "	 <th align='right' class='amounts'>".number_format($TotalQty, $DecimalBase)."</th>";
	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;
?>

