<?php

//Report Version For Sika
session_start();
if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}
include("Functions_Sales.php");
print <<< HERE
<HTML>
<HEAD>
<TITLE> Sales Report </TITLE>

<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;
                        }	

h4 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }

h3 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 2px;
	 width: 180px;
   } 
   
h5 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 2px;
   }      
						
				 

</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, ".
			 " 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, InvoiceComment, WaybillComment 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, Sorder_Reference, Sale_RelatedId as Delivery_Reference, ".
	         " 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_Telephone2, 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,journal.journal_refgovernment as JVRef, journal.journal_folio as Jfolio ,date_format(sale_checkdate,'%d/%m/%Y') as checkdate,
			 sale_checkno as checkno,sale_bank as bank ,sale_bankbranch as bankbranch,sale_mode as sale_mode".
			 " From Sales ".
			 " Left Join Clients on Sales.Client_code = Clients.Ledger_Number ".
			 " Left Join journal on Sales.journal_id = journal.journal_id ".
			 " Left Join Project on Sales.Project_Code = Project.Project_Code ".
			 " Left Join CostCent on Sales.Costcenter_code = CostCent.CostCent_Code ".
	         " Where Sale_ID = ".$SalesId;
			 //echo $query;
			

	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
				$querysalesmen = "select salesmen_code,salesmen.ledger_name as salesmen_name from clients
inner join salesmen  on salesmen.ledger_number = clients.salesmen_code where clients.ledger_number ='$ClientCode'";
$resultsalesmen=mysql_query($querysalesmen,$connection) or die(mysql_error());
while($rowsalesmen=mysql_fetch_array($resultsalesmen)){
		extract($rowsalesmen);
	}
	
	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 = $Sale_Reference;
		
	if (empty($Sale_NetTotal)) $Sale_NetTotal = 0;
	$queryalpha  = " SELECT Alpha_Num ('ENGLISH', '".$CurrencyCode."', ".$Sale_NetTotal.") As AlphaAmount FROM Dual ";
		
	$resultalpha = mysql_query($queryalpha,$connection) or die(mysql_error());
	$rowalpha=mysql_fetch_array($resultalpha);
	echo "<table border=0  cellspacing=0 cellpadding=0 style = 'width:21cm;'><tr style = 'height:4.5cm;width:21cm;'><td align='left'>";
	echo "<table border=0  cellspacing=0 cellpadding=0  style='table-layout:fixed;width:20cm;' >
	";
	echo "<tr style = 'height:6cm;' valign =bottom>
	<td style = 'width:8cm;'  ></td><td align='center' style = 'width:6cm;'><b><font size=4>$Title </font></td>
	<td align='right' colspan=2 style = 'width:6cm;' ><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 style = 'height:0.4cm;'><td colspan = '4' style = 'border-left:thin solid;border-top:thin solid;border-right:thin solid;'></td></tr></table></tr></td><tr><td>
	<table border=0 cellspacing=0 cellpadding=0  style='table-layout:fixed;width:20cm;' >";
	
	echo "<tr>";
	echo "  <th align='left' style = 'width:2.5cm;border-left:thin solid;'  >Reference:</th>";
	echo "  <td align='left'  style = 'width:1.5cm;' >$Sale_Reference</td>";

	echo "  <th align='left'  style = 'width:2cm;'>Folio:</th>";
	echo "  <td align='left'  style = 'width:1.5cm;'>$Jfolio</td>";
	echo "  <th align='right' style='padding-right:10px;'  style = 'width:1.5cm;'>Date:</th>";
	echo "  <td align='left'  style = 'width:3cm;'>$Sale_Date</td><th  style = 'width:4cm;' align = 'right'>Project :</th><td  style = 'width:4cm;border-right:thin solid;'>$ProjectCode</td>";
	echo "</tr>";
	
	echo "<tr>";
		echo "  <th style = 'border-left:thin solid;border-bottom:thin solid;' align='left' >JV Ref.:</th>";
	echo "  <td align='left' style ='border-bottom:thin solid;' >$JVRef</td>";
	
	
	

	
		echo "  <th align='left' valign='top'  style='padding-right:10px;border-bottom:thin solid;'>Amount: </th>";
		echo "  <td align='left' style = 'border-right:thin solid;border-bottom:thin solid;' valign='top' colspan='5'>$Sale_NetTotal  $CurrencyCode</td>";

	
	echo "</tr></table></tr></td><tr><td >	<table border=0 cellspacing=0 cellpadding=0  style='table-layout:fixed;width:20cm;' >
";
	
	echo "<tr >";
	echo "  <th style = 'border-left:thin solid;width:4cm;'  valign='top' align = 'right'>Recieved From :</th>";
	echo "  <td align='left'  style = 'width:8.1cm;'>$ClientName</td>";
	echo "  <td align='left'  style = 'width:2.5cm;'>($ClientCode</td>";
	echo "  <td align='left'  style = 'border-right:thin solid;width:5.4cm;'>)</td>";
	//echo "  <td align='left' colspan='7' style = 'border-right:thin solid;'>$Trans_Representative</td>";


	echo "</tr>";
	
	
		echo "<tr height='10' align='right'><th style = 'border-left:thin solid;'>The Sum of :</th><td align = left style ='border-right:thin solid;' colspan =7>".$rowalpha[0]."</td></tr>";
		echo "<tr  align='right'><th valign = 'top' style = 'border-left:thin solid;height:0.9cm'>Description :</th><td align = left valign = top style = 'border-bottom:tchin solid;border-right:thin solid;' colspan = 7>$Sale_Remark</td></tr>";
	echo "<tr height='10' align='right'>
	<th style = 'border-left:thin solid;border-bottom:thin solid;'>Receipt Mode:</th>
	<td style = 'border-bottom:thin solid;border-bottom:thin solid;width:2.1cm;' >$Trans_Remark</td>
	<td style = 'border-bottom:tchin solid;border-right:thin solid;border-bottom:thin solid;width:14cm;' colspan = 7>$Trans_Remark</td>
	</tr>";
	echo "</table>";
	echo "<tr><td><table border=1 cellspacing=0 cellpadding=0  style='table-layout:fixed;width:20cm;' >
	<tr>
	<td  style ='width:7cm;'></td> 
	<td  style ='width:1.8cm;'></td> 
	<td  style ='width:3.8cm;'></td> 
	<th align = left style ='width:7.4cm;'>Amount: $Sale_NetTotal</th>
</tr>
<tr>
	<td  style ='width:7cm;'></td> 
	<th  align = left style ='width:1.6cm;'>Check #:</th> 
	<td  style ='width:4cm;'>$checkno</td> 
	
	<th align = left style ='width:7.4cm;'>Date : $checkdate</th>
</tr>
<tr>
	<td  style ='width:7cm;'></td> 
	<th  align = left style ='width:1.6cm;'>Bank:</th> 
	<td  style ='width:4cm;'>$bank</td> 
	<th align = left style ='width:7.4cm;'>Branch: $bankbranch</th>
</tr>

</table></tr></td></table>"
;

echo "<tr><td><table border=0 cellspacing=0 cellpadding=0  style='table-layout:fixed;width:20cm;' >
	<tr style = 'height:1.6cm;' valign = middle>
	<th  style = 'width:5cm;'>Received By</th>
	<td style = 'width:5cm;'><table border = 0><tr><td style = 'height:0.8cm;width:5cm;'></td></tr><tr><td valign = top style = 'height:0.8cm;width:5cm;'><hr></td></tr></table></td>
	<th style = 'width:5cm;'>Received From</th>
	<td style = 'width:5cm;'><table border = 0><tr><td style = 'height:0.8cm;width:5cm;'></td></tr><tr><td valign = top style = 'height:0.8cm;width:5cm;'><hr></td></tr></table></td>
</tr>

</table></tr></td></table>"
;

}


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;
?>

