<?php

//Report Version For Tiba
session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}

print <<< HERE
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<TITLE> Purchase 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: 0px;
						 border-style: none solid solid solid;
  						 border-color: #666666;
                       }					  
table.totals td.amounts { font-size: 14px; 
                          font-family:Times-New-Roman;
						  border-width: 0px;
						  border-style: none none solid none;
  						  border-color: #666666;
				        }
table.totals td.totalborder {
								font-size: 14px; 
								font-family:Times-New-Roman;
								border-width: 0px;
								border-style: none solid solid none;
								border-color: #666666;
							}						
				 
h4 { border-top-style: double; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }
   
h3 { border-top-style: double; 
	 border-top-color: black; 
	 border-top-width: 2px;
	 width: 150px;
   }   
   .bigfont{
	   font-size:150%; 
   }
    .midfont{
	   font-size: 100%; 
   }
   
   .headerheight{
	height:250px;
}
.inlinetable{
	display: inline-table;
}
   
html {
  height: 100%;
  box-sizing: border-box;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}

body {
  position: relative;
  margin: 0;
  padding-bottom: 6rem;
  min-height: 100%;
  font-family: "Helvetica Neue", Arial, sans-serif;
}

.demo {
  margin: 0 auto;
  padding-top: 64px;
  width: 95%;
  position:fixed;
   padding: 1rem;
}



/**
 * Footer Styles
 */

.footer {
position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  padding: 1rem;
  text-align: center;
}

.footer table{
	font-size:11px;
}

.horizantalline{
	border-top:solid 1px black;
}
</style>


</HEAD>

<BODY>
HERE;

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");
//mysql_set_charset('utf8',$connection);
if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}
//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, Setup_vatno, ".
             " 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_NameF, Company_Memo, Company_MemoF, Company_Capital, Company_CapitalF,
			 company_telephone,company_city,company_fax,company_country,company_address,company_phone2,company_phone3
			 ,company_address2,company_city2,company_city_arabic,company_address_arabic,company_address2_arabic,
			 company_city_arabic,company_city2_arabic,company_country_arabic,company_name_arabic,company_memo_arabic,company_phone4,company_city3,company_city4,company_phone2_city3
			 ,company_phone1_city3,company_telephone_city3 ,company_phone2_city4
			 ,company_phone1_city4,company_telephone_city4,website,company_email".
			 " from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//Read From PurchaseSetup Table
$queryPurchaseSetup="Select PurchaseTTC From PurchaseSetup";
$resultPurchaseSetup=mysql_query($queryPurchaseSetup,$connection) or die(mysql_error());
while($rowPurchaseSetup=mysql_fetch_array($resultPurchaseSetup)){
	extract($rowPurchaseSetup);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_AdditionalTaxes,0) As Module_AdditionalTaxes, ifNull(Module_ShowInvoiceAging,0) As Module_ShowInvoiceAging, ".
             " 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);
}

	

$TotalQty = 0;

if($_GET['PurchaseId'] != '')
{
	$query = " Select Purchase_ID,project_name,currency.currency_name , Purchase_Reference, Date_Format(Purchase_Date,'%d/%m/%Y') As Purchase_Date, Purchase_Date As InvoiceDate, ".
			 " Date_Format(Journaldt_valuedate,'%d/%m/%Y') As MaturityDate, Purchases.Currency_code As CurrencyCode, ".
			 " Purchase_Remark, Purchase_InvoiceNo, Journal.Journal_RefGovernment, Purchases.Project_Code As ProjectCode, Purchases.Costcent_code As CostcentCode,  ".
			 " Purchases.Supplier_code As SupplierCode,	ledger.Ledger_name As SupplierName,	Suppliers.Ledger_nameF As SupplierNameF, Supplier_PaymentTerms,  ".
			 " Supplier_Representative As SupplierContact, Supplier_Address As SupplierAddress, Supplier_VAT, Supplier_VatNo As SupplierVatNo,  ".
			 " Round(Purchase_Taxable, ".$DecimalBase.") As PurchaseTaxable, Round(Purchase_NoneTaxable, ".$DecimalBase.")as PurchaseNoneTaxable, ".
			 " Round(Purchase_Total, ".$DecimalBase.") As GrossTotal, ".
			 " Round(Purchase_DiscountPer, ".$DecimalBase.") As PurchaseDiscountPer, Round(Purchase_DiscountAmount, ".$DecimalBase.") As PurchaseDiscountAmount,  ".
			 " Round(Purchase_VAT, ".$DecimalBase.") As Purchase_VAT, Round(Purchase_VATBase1, ".$DecimalBase.") As Purchase_VATBase1, ".
			 " Round((Purchase_Total + IfNull(Purchase_VAT,0) - IfNull(Purchase_DiscountAmount,0) + (ifNull(Purchase_Tax1,0) + ifNull(Purchase_Tax2,0) + ifNull(Purchase_Tax3,0) + ifNull(Purchase_Tax4,0) + ifNull(Purchase_Tax5,0))), ".$DecimalBase.") As Purchase_NetTotal,  ".
			 " Round((Purchase_TotalBase1 + IfNull(Purchase_VATBase1,0) - IfNull(Purchase_DiscountBase1,0) + (ifNull(Purchase_Tax1Base1,0) + ifNull(Purchase_Tax2Base1,0) + ifNull(Purchase_Tax3Base1,0) + ifNull(Purchase_Tax4Base1,0) + ifNull(Purchase_Tax5Base1,0))), ".$DecimalBase.") As Purchase_NetTotalBase1,  ".
			 " Round(Purchase_Charges, ".$DecimalBase.") As PurchaseCharges, Round(Purchase_ChargesBase1, ".$DecimalBase.") As PurchaseChargesBase1, Round(Purchase_ChargesBase2, ".$DecimalBase.") As PurchaseChargesBase2, ".
			 " Purchase_DebitAccount As PurchaseDebitAccount, if(Purchase_Mode=0, 'Credit', if(Purchase_Mode=1,'Cash', 'Check')) As PurchaseMode, ".
			 " Round((ifNull(Purchase_Tax1,0) + ifNull(Purchase_Tax2,0) + ifNull(Purchase_Tax3,0) + ifNull(Purchase_Tax4,0) + ifNull(Purchase_Tax5,0)), ".$DecimalBase.") As Purchase_TotalTaxes  ".
			 
				"From Purchases ".
			 " Left Join Suppliers on Purchases.Supplier_code = Suppliers.Ledger_Number ".
			 " Left Join Journal on Purchases.Journal_Id = Journal.Journal_Id ".
			 " Left Join ledger on ledger.ledger_number = Suppliers.ledger_number ".
			  " Left Join Project on Purchases.Project_Code = Project.Project_Code ".
			  "left join currency on currency.currency_code=Purchases.Currency_code".
			 " Where Purchase_ID = ".$_GET['PurchaseId'];
			 
		 
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
	
	$account="select User_account from purchases  Where Purchase_ID = ".$_GET['PurchaseId'];
	$resulta=mysql_query($account,$connection) or die(mysql_error());
	if($rw=mysql_fetch_row($resulta)){
		$account=$rw[0];
	}
	$query_user = "select User_Name as preparedBy from ".$_SESSION['dbLabel'].".users where User_account = '".$account."'";
$result_user = mysql_query($query_user, $connection);
$rows_user = mysql_fetch_array($result_user);
extract($rows_user);

	echo "<div class='demo' >";
		if ($hide=='0'){
			
			echo "<table border='0' width=100% cellspacing=0 cellpadding=0>
			<col width=270>
			<col width=100>
			<col width=270>
			<tr>
			<td>
				<table border=0 width=270>
					<tr><td class='bigfont' align='center'><b><pre>".str_replace( '\n', '<br>', $CompanyName)."<pre></b></td>";

					echo "</tr>
					<tr><td class='midfont' align='center'><pre>".str_replace( '\n', '<br>', $CompanyMemo)."<pre></td>";

					echo "</tr>
		
				</table>
			</td>
			
			<td align=center><img src='LogoFile.php' width='100' height='100' /></td>
			<td align=right>
			<table border=0 width=270>
					<tr><td class='bigfont' align='center'><b><pre>".str_replace( '\n', '<br>', $company_name_arabic)."<pre></b></td>";

					echo "</tr>
					<tr><td class='midfont' align='center'><pre>".str_replace( '\n', '<br>', $company_memo_arabic)."<pre></td>";

					echo "</tr>
		
				</table>
			</td>
			</tr>
			<tr height=20></tr>
			<tr height='40'><td align='center' colspan='3'><b><font size=5>Purchase </font></td></tr>
			</table>";
		}

		

	echo "</div>";
	
	echo "<table width=100% border=0>";

echo "<div class='headerheight'></div>";
		echo "<tr><td><table border=0 width='100%' cellspacing=2 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=70>
		  <col width=300>
		  <col width=70>
		  <col width=140>";
	echo "<tr height='5'><td colspan=4><h4></h4></td></tr>";
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Reference</th>";
	echo "  <td align='left'>$Purchase_Reference</td>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Date</th>";
	echo "  <td align='left'>$Purchase_Date</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Supplier</th>";
	echo "  <td align='left' >$SupplierCode&nbsp;-&nbsp;$SupplierName</td>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Currency</th>";
	echo "  <td align='left' >$currency_name</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "</tr>";
	
	echo "<tr>";
	
	echo "  <th align='left' bgcolor='#D8D8D8'>Contact</th>";
	echo "  <td align='left' >$SupplierContact</td>";
	echo "</tr>";
echo "<tr>";
		echo "  <th align='left' bgcolor='#D8D8D8'>Project</th>";
		echo "  <td align='left' >$ProjectCode&nbsp;-&nbsp;$project_name</td>";
	echo "</tr>";

	echo "<tr>";
		
	if ($Module_ShowLayoutCost == 1) {	
		echo "  <th align='left' bgcolor='#D8D8D8'>Cost Center</th>";
		echo "  <td align='left' >$Costcentcode&nbsp;&nbsp;$Costcent_name</td>";
	} 	
	echo "</tr>";
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Remark</th>";
	echo "  <td align='left' colspan='3'>$Purchase_Remark</td>";
	echo "</tr>";
	echo "<tr height='5'><td colspan=4><h4></td></tr>";
		echo "</table></td></tr>";
	$querydt = " Select PurchasesDt.PurchaseDt_ID, PurchasesDt.Item_code as ItemCode, Item_Description As ItemDescription, Item_Unit as ItemUnit, ".
			   " Item_DescriptionF1 As ItemDescriptionF1, Item_DescriptionF2 As ItemDescriptionF2, if(ifNull(PurchasesDt.Item_Vat,0) > 0, '*', ' ') as ItemIsVat,  ".
			   " PurchaseDt_Quantity as PurchaseDtQuantity, Round(PurchaseDt_Price, ".$DecimalBase.") As PurchaseDtPrice, ".
			   " Round(PurchaseDt_DiscountPer, ".$DecimalBase.") As PurchaseDtDiscountPer, Round(PurchaseDt_PriceTTC, ".$DecimalBase.") As PurchaseDtPriceTTC,  ".
			   " Round(((PurchaseDt_Quantity*PurchaseDt_Price)-((PurchaseDt_Quantity*PurchaseDt_Price*ifNull(PurchaseDt_DiscountPer,0))/100)), ".$DecimalBase.") As PurchaseDtTotal, ".
			   " Round(((PurchaseDt_Quantity*PurchaseDt_PriceTTC)-((PurchaseDt_Quantity*PurchaseDt_PriceTTC*ifNull(PurchaseDt_DiscountPer,0))/100)), ".$DecimalBase.") As PurchaseDtTotalTTC ".
			   " From PurchasesDt Left Join Items on PurchasesDt.Item_code = Items.Item_code ".
			   " Where PurchasesDt.Purchase_ID = ".$_GET['PurchaseId'];
		echo "<tr><td align='left' >";
echo "<table border=0 width='100%' cellspacing=2 class='detail' style='table-layout:fixed;' >
		  <col width=90>
		  <col width=150>
		  <col width=120>
		  <col width=70>
		  <col width=70>
		  <col width=85>";
	echo "<tr bgcolor=#D8D8D8>";
	echo "   <th align='left'>Item</th>";
	echo "   <th align='left'>Description</th>";
	echo "	 <th align='center'>Qty</th>";
	echo "	 <th align='center'>Price</th>";
	echo "	 <th align='center'>Disc.%</th>";
	echo "	 <th align='center'>Total Price</th>";
	echo "</tr>";			
	echo "<tr height='5'><td colspan=6><h4></h4></td></tr>";
	
		
	
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
	
	$i=0;
	$j=0;
	$page=0;
	$nbline=8;
	while($rows=mysql_fetch_array($resultdt)){
			//while(($rows=mysql_fetch_array($resultdt))||($j<=4)){
		extract($rows);
		
		$TotalQty = $TotalQty + $PurchaseDtQuantity;
		
		echo "<tr>";

			echo "  <td align='left' valign='top'>$ItemCode</td>";
		echo "  <td align='left' valign='top'> &nbsp; $ItemDescription</td>";
		echo "	<td align='center' valign='top'>".number_format($PurchaseDtQuantity, $DecimalBase)."</td>";
		
		if ($SalesTTC == 1) 
			echo "	<td align='center' valign='top'>".number_format($PurchaseDtPriceTTC, $DecimalBase)."</td>";
		else
			echo "	<td align='center' valign='top'>".number_format($PurchaseDtPrice, $DecimalBase)."</td>";
		
		echo "	<td align='center' valign='top'>".number_format($PurchaseDtDiscountPer, $DecimalBase)."</td>";
					
			if ($SalesTTC == 1) 
				echo "	<td align='center' valign='top' >".number_format($PurchaseDtTotalTTC, $DecimalBase)."</td>";
			else
				echo "	<td align='center' valign='top' >".number_format($PurchaseDtTotal, $DecimalBase)."</td>";
		
			
				
		echo "</tr>";
		echo "<tr height='5'><td colspan=6><h4></h4></td></tr>";
		if ($page!=0){$nbline=10;}
		else {$nbline=8;}
		
		if ($i==$nbline){
	$page++;
	$i=0;
		
	echo "<tr><td>&nbsp;<p style='page-break-before: always'></p>
		</td></tr>";
		echo "<tr><td><div class='headerheight'></div></td></tr>";
		
		echo "<tr bgcolor=#D8D8D8>";
	echo "   <th align='left'>Item</th>";
	echo "   <th align='left'>Description</th>";
	echo "	 <th align='center'>Qty</th>";
	echo "	 <th align='center'>Price</th>";
	echo "	 <th align='center'>Disc.%</th>";
	echo "	 <th align='center'>Total Price</th>";
	echo "</tr>";			
	echo "<tr height='5'><td colspan=6><h4></h4></td></tr>";
	
		}
		$j++;
		$i++;
		

	}

	
	
	echo "<tr>";
	echo "   <td align='left' colspan='2'>&nbsp;</td>";
	echo "	 <th align='center' class='labels'>Total Qty: $TotalQty</th>";
	echo "</tr>";
	
		$queryalpha  = " SELECT Alpha_NumLower ('ENGLISH', '".$CurrencyCode."', ".$Purchase_NetTotal.") As AlphaAmount FROM Dual ";
	$resultalpha = mysql_query($queryalpha,$connection) or die(mysql_error());
	echo "<br>";
	$rowalpha=mysql_fetch_array($resultalpha);
	echo "<tr height='10'></tr>";
	echo "</table></td></tr>";
	echo "<tr height='20'></tr>";
		echo "<tr><td align='right'>";
	echo "<table border=0 width='100%' cellspacing=0 cellpadding=1  class='totals' style='table-layout:fixed;' >
		  <col width=400>
		  <col width=100>
		  <col width=80>
		  <col width=20>";
	echo "<tr>";
	echo "   <td align='' >&nbsp;</td>";
	echo "	 <td align='right' class='labels' >Taxable</td>";
	echo "	 <td align='right' class='amounts' >".number_format($PurchaseTaxable, $DecimalBase)."</td>";
	
	echo "</tr>";	
		
	echo "<tr>";
	 echo "   <td  valign='top' style='font-size:14px; font-family:Times-New-Roman; '><br>".ucfirst($rowalpha[0])."<br></td>";
	echo "	 <td align='right' class='labels'>None Taxable</td>";
	echo "	 <td align='right' class='amounts'>".number_format($PurchaseNoneTaxable, $DecimalBase)."</td>";
	echo "	 <td></td>";
	echo "</tr>";
		
	echo "<tr>";
	echo "   <td align='left'>&nbsp;</td>";
	echo "	 <td align='right' class='labels'>Gross Total</td>";
	echo "	 <td align='right' class='amounts'>".number_format($GrossTotal, $DecimalBase)."</td>";
	echo "	 <td></td>";
	echo "</tr>";
		
	echo "<tr>";
	echo "   <td align='left'>&nbsp;</td>";
	echo "	 <td align='right' class='labels'>Discount</td>";
	echo "	 <td align='right' class='amounts'>".number_format($PurchaseDiscountAmount, $DecimalBase)."</td>";
	echo "	 <td></td>";
	echo "</tr>";
		
	echo "<tr>";
	echo "   <td align='left'>&nbsp;</td>";
	echo "	 <td align='right' class='labels'>VAT</td>";
	echo "	 <td align='right' class='amounts'>".number_format($Purchase_VAT, $DecimalBase)."</td>";
	echo "	 <td></td>";
	echo "</tr>";
		
	if ($Module_AdditionalTaxes == 1) {
		echo "<tr>";
		echo "   <td align='left'>&nbsp;</td>";
		echo "	 <td align='right' class='labels'>Taxes</td>";
		echo "	 <td align='right' class='amounts'>".number_format($Purchase_TotalTaxes, $DecimalBase)."</td>";
		echo "	 <td></td>";
		echo "</tr>";
	}
		
	echo "<tr>";
	echo "<td>&nbsp;</td>";
	echo "	 <td align='right' class='labels'>Net Total $currency_name</td>";
	echo "	 <td align='right' class='amounts'>".number_format($Purchase_NetTotal, $DecimalBase)."</td>";
	echo "</tr>";
	echo "</table></td></tr>";
	echo "<tr><td align='center'>";
	echo "<table border=0 width='635' cellspacing=0 cellpadding=1  class='master' style='table-layout:fixed;' >
		  <col width=235>
		  <col width=175>
		  <col width=235>";
	
	echo "<tr height='>";
	echo "  <td align='left' colspan='2'>&nbsp;</td>";
	echo "</tr>";	
	
	echo "<tr height=''>";
	
echo "  <td align='center'><h3></td>";
	
	echo "</tr>";	
	
	echo "<tr height=''>";
	echo "  <td align='center' valign='top' >PREPARED BY <br> $preparedBy</td>";
	echo "</tr>";	
	echo "</table>";
	echo "<br>";
	echo "</td></tr></table>";
}
if ($hide=='0'){
echo "<div class='footer'>";
echo "<div class='horizantalline'></div>";
echo "<table border='0' class='inlinetable'>
<col width=150px><col width=150px><col width=150px><col width=150px><col width=150px>
<tr>
	<td align='center'>";
		echo "<table border='0'>
				<tr><td><i>$website</i></td></tr>
				<tr><td><i>$company_email</i></td></tr>
			</table>
	</td>";
echo "<td>
		<table border=0><col width='70'><col width=100>
			<tr><th colspan='2'><i>$company_city</i></th></tr>
			<tr><td align='right'>phone:</td><td><i>$company_telephone</i></td></tr>
			<tr><td></td><td><i>$company_phone2</i></td></tr>
			<tr><td align='right'>fax:</td><td><i>$company_phone4</i></td></tr>
		</table>
	</td>
	<td>
		<table>
			<tr><th colspan='2'><i> $company_city3  </i></th></tr>
			<tr><td align='right'>phone:</td><td><i>$company_telephone_city3</i></td></tr>
			<tr><td></td><td><i>$company_phone1_city3</i></td></tr>
			<tr><td align='right'>fax:</td><td><i>$company_phone2_city3</i></td></tr>
		</table>
	</td>
	<td>
		<table border='0'>
			<tr><th colspan='2'><i> $company_city4 </i></th></tr>
			<tr><td align='right'>phone:</td><td><i>$company_telephone_city4</i></td></tr>
			<tr><td></td><td><i>$company_phone1_city4</i></td></tr>
			<tr><td align='right'>fax:</td><td><i>$company_phone2_city4</i></td></tr>
		</table>
	</td>
	<td valign='top'>
		<table border='0'>
			<tr><th colspan='2'><i> $company_city2 </i> </th></tr>
			<tr><td>phone:</td><td><i>$company_phone3</i></td></tr>
		</table>
	</td>
</tr>";
echo "<tr><td></td></tr>";
echo "</table>";


echo "</div>";

}

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

