<?
session_start();
include("../opendb.php");
include("../test.php");
if($foc=='foc')
$re=mysqli_query($db,"select * from spareinvoicefoc where invno='$invno' and invtype='$invtype' and brcode='$brcode'")or die('err:'.mysqli_error());
else
$re=mysqli_query($db,"select * from spareinvoice where invno='$invno'  and brcode='$brcode'")or die(mysqli_error());
$row=mysqli_fetch_array($re)or die("Fetch Error:".mysqli_error());

$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
?>
<style>
th{font-size:13}
</style>
<style>
 td,body{font-family:'arial narrow';font-size:15;letter-spacing:1}
</style>

<center>

<table width=100% BORDER=0>
<tr><td>Invoice No :
<?
print '['.substr($brcode,0,3).']';
?>
 S-<?

if($foc=='foc') 
print "FOC ";
print $invno;
?>
</td><td align=right>Date : <?print datetohtml($row[entrydate])?> </td></tr>
<tr><td>Ref . <?print $row[ref]?></TD><TD></TD></TR>
<?
if($brcode == 'PSN880')
{?>
	<TR><TD COLSPAN=2 ALIGN=CENTER><b> PSN AUTOMOTIVE MARKETING PVT LTD (AC DELCO Division)</b><BR>
<?
}
else
{
?>
	<TR><TD COLSPAN=2 ALIGN=CENTER><b> PSN AUTOMOTIVE MARKETING PVT LTD (GEN SET Division)</b><BR>
<?
}
?>
	 <?print $brrow[braddr].",".$brrow[brcity].",<br><u> PH - ".$brrow[brphone]?></u>
TIN: <?print $brrow[tin]?>, CST: <?print $brrow[cstno]?>&nbsp;
</td></tr>
<TR><TD COLSPAN=2 ALIGN=CENTER> FORM <?print $row[invtype]?> (<?print $row[paytype]?> Invoice)</td></tr>
<TR><TD COLSPAN=2 ALIGN=left> Customer : <?print $row[customer].'<br>'.$row[addr].'<br>TIN : '. $row[tin]?></td></tr>
</table>

<table border=0 cellspacing=1 cellpadding=0 WIDTH=90%>
<th width=15>NO.<th style="text-align:left">Location<th width=210 style="text-align:left">DESCRIPTION OF GOODS <TH width=40>QUANTITY <TH width=50>RATE <th width=80>DIS% <th width=80>AMOUNT  
<?
 if($row[vatorcst]=='VAT')
   print "<TH>VAT (14.5%)";
 else
   print "<th>CST (2%)";
?>
<th>CESS(1%) <TH>NET AMOUNT</TH>
<tr><td colspan=10><hr></td></tr>
<?
//$re1=mysqli_query($db,"select * from spareinvoice where invno='$invno' and invtype='$invtype' and brcode='$brcode'")or die('ERr'.mysqli_error());
if($foc=='foc')
 $sql="select * from spareinvoicefoc where invno='$invno' and invtype='$invtype' and brcode='$brcode'";
else
 $sql="select * from spareinvoice where invno='$invno' and brcode='$brcode'";
$re1=mysqli_query($db,$sql)or die('err:'.mysqli_error());

$i=1;
$tq=0;
$tp=0;
$cnt=1;
while($row1=mysqli_fetch_array($re1))
{
if($row1[tot]<=0)
{
 print "<script> alert('Invalid Entry Total')</script>";
 print "<h1>Error</h1>";
}
if($row1[taxamt]<=0)
{
 print "<script> alert('Invalid Entry Tax')</script>";
 print "<h1>Error</h1>";
}
if($row1[cessamt]<=0&&$row1[vatorcst]=='VAT')
{
// print "<script> alert('Invalid Entry Cess')</script>";
// print "<h1>Error</h1>";
}
 print "<tr><td align=center width=15>".$cnt++."</td>";
 $locres=mysqli_query($db,"select location from sparelocmast where brcode='$brcode' and itemcode='$row1[partcode]'");
 $locrow=mysqli_fetch_row($locres);
print "<td>$locrow[0]</td>";
 PRINT "<td>$row1[partcode] ($row1[partname]) <br> Batch: ".substr($row1[ref],0,30)."<br>";
if (substr($row1[ref],31,30)<>'')
	print substr($row1[ref],31,30)."<br>";
if (substr($row1[ref],61,30)<>'')
	print substr($row1[ref],61,30)."<br>";
if (substr($row1[ref],91,30)<>'')
	print substr($row1[ref],91,30)."<br>";
if (substr($row1[ref],121,30)<>'')
	print substr($row1[ref],121,30)."<br>";
if (substr($row1[ref],151,30)<>'')
	print substr($row1[ref],151,60)."<br>";
print "<br>";
print "</td><td align=left>$row1[qty] </td><td align=right>".putComa($row1[price])."</td>";
print "<td align=center>$row1[disc]</td>";
print "<td align=right>".putComa($row1[aprice])."</td>";
print "<td align=right>$row1[taxamt]</td>";
print " <td align=right>$row1[cessamt]</td>";
print "<td align=right>".putComa($row1[tot])."</td></tr>";
//print "<tr><td colspan=10><hr></td></tr>";
$i++;
$tq+=$row1[qty];
$tp+=$row1[tot];
$tamount+=($row1[aprice]);
$tvat+=$row1[taxamt];
$tcess+=$row1[cessamt];
}

for($j=$i;$j<=16;$j++)
{
 print "<tr><td colspan=10>&nbsp;</td></tr>";
}

print "<tr><td colspan=110>";

print "<HR></tD></tR>";

print "<tr><td align=right>Total </td><td></td><td align=right>$tq Nos</td>";
print "<td></td><td></td><td align=right>".putComa($tamount)."</td>";
print "<td align=right>".putComa($tvat)."</td>";
print "<td align=right>".putComa($tcess)."</td>";
print "<td colspan=6 align=right>".putComa($tp)."</td></tr>";
$rnd=round($tp);
$rnd2=round(($tp-$rnd),2);
if($rnd2!=0)
{
  print "<td colspan=9 align=right>Rounded ".putComa(abs($rnd2))."</td></tr>";
  $gtot=($tp-$rnd2);
  print "<td colspan=9 align=right><b>Grand Total = ".putComa($gtot)."</td></tr>";
}
else
 $gtot=$tp;

print "<tr><td colspan=160>";
for($j=0;$j<=100;$j++)
{
 print "=";
}
print "</td></tr>";

print "<tr><td colspan=3>Amount Chargeable (In Words)</td><td colspan=17 align=right>E. & O.E</td></tr>";
$tt= str_replace(',','',number_format($gtot,2));

print "<tr><td colspan=4>".word($tt)." Only</td><td colspan=6 align=right><font size=2>for PSN AUTOMOTIVE MARKETING P LTD</tr>";
print "<tr><td colspan=9 align=center>(Goods once sold will not be taken back)</td></tr>";
print "<tr><td colspan=9 align=right><font size=2>Authorised Signatory</td></tr>";
log_entry($brcode,$user,$REMOTE_ADDR,"Print spareinvoice:$invno , inv type: $invtype");
?>
</table>



