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

$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);

$cusres=mysqli_query($db,"select * from customer where acname='$row[customer]'");
$cusrow=mysqli_fetch_array($cusres);
?>
<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 COLSPAN=2 ALIGN=CENTER ><BR>
<?print "PSN AUTOMOBILES PVT LTD" ?></u>
<br>CIN U50300KL1996PTC010842
<TR><TD COLSPAN=2 ALIGN=CENTER >
<?print "TIN :".$brrow[tin] ?></u>
</td></tr>
<TR><TD COLSPAN=2 ALIGN=CENTER>
	 <?print $brrow[braddr].",".$brrow[brcity].",<br><u> PH - ".$brrow[brphone]?></u>

</td></tr>
<tr><td>Reg. Off.:<br>35/189, NH, Palarivattom, Kochi-682025
<br>Ph:0484 2343754 Email :psnsales@eth.net Website:www.psn.in
<tr><td>.</td></tr>
<tr><td>Invoice No :
<?
if($brcode == 'ERA153')
{
	print 'ZFEMCC';
}
else if($brcode == 'MAN140')
{
	print 'ZFMDCC';
}
else if($brcode == 'CAL556')
{
	print 'ZFCTCC';
}
else
{
	//print '['.substr($brcode,0,3).']';
}
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( $row[invtype]=='H')
{
?>
	<TR><TD COLSPAN=2 ALIGN=CENTER> AGAINST FORM <?print $row[invtype]?> (<?print $row[paytype]?> Invoice)</td></tr>
<?
}
else
{
?>
	<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>'.$cusrow[addr1].'<br>'.$cusrow[dist1].'<br>TIN : '. $row[tin].'<br>PHONE : '.$cusrow[phone1]?>
</td></tr>
<TR><TD COLSPAN=2 ALIGN=left></td></tr>
</table>

<table border=0 cellspacing=1 cellpadding=0 width=90%>

<?


if($kar[0]!='Kerala') 
{
?>
<th align=left>NO.<th align=left>PART NO<th align=left>DESCRIPTION OF GOODS <th>QUANTITY <th>RATE <th>DIS% <th>AMOUNT  

<?
}
else
{
?>
<th>NO.<th align=left>PART NO<th style="text-align:left">DESCRIPTION OF GOODS <th>QUANTITY <th>RATE <th>DIS% <th>AMOUNT  
<?
}
?>

<?
 if($row[vatorcst]=='VAT')
   print "<th align=right>VAT ($row[tax])";
 else

   print "<th>CST ($row[tax]%)";
?>
<?
if($kar[0]=='Kerala') 
{
?>
<th>CESS(0%) 
<?
}
?>

<th align=right>FREIGHT</th>

<th align=right>NET AMOUNT</th>

<tr><td colspan=11><hr></td></tr>
<?
//$re1=mysqli_query($db,"select * from spareinvoice where invno='$invno' and invtype='$invtype' and brcode='$brcode'")or die('ERr'.mysqli_error($db));
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($db));

$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($row[invtype]!='H' )
{
if(substr($row[ref],0,2)!='E1')
{
if($row1[taxamt]<=0)
{
 print "<script> alert('Invalid Entry Tax')</script>";
 print "<h1>Error</h1>";
}
}
}


 print "<tr><td align=left>".$cnt++."</td>";
 $locres=mysqli_query($db,"select location from sparelocmast where brcode='$brcode' and itemcode='$row1[partcode]'");
 $locrow=mysqli_fetch_row($locres);
if($kar[0]=='Kerala') 
{
//print "<td>$locrow[0]</td>";
}
 print "<td align=left>$row1[partcode]</td><td> $row1[partname]</td><td align=center>".round($row1[qty])."</td><td align=right>$row1[price]</td>";
print "<td align=center>$row1[disc]</td>";
print "<td align=right>$row1[aprice]</td>";
print "<td align=right>$row1[taxamt]</td>";

if($brcode=='BAN682' ||$brcode=='THU558'|| $brcode=='MYS543'|| $brcode=='HYD664')
{
}
else
{
//print " <td align=right>$row1[cessamt]</td>";
}
print " <td align=right>$row1[hand]</td>";
print "<td align=right>".putComa($row1[tot])."</td></tr>";

$i++;
$tq+=$row1[qty];
$tp+=$row1[tot];
$tamount+=($row1[aprice]);
$tvat+=$row1[taxamt];
$tcess+=$row1[cessamt];
$thand+=$row1[hand];

}

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

print "<HR></tD></tR>";
if($kar[0]=='Kerala')
{
print "<tr><td align=right>Total </td><td></td><td align=right>$tq Nos</td>";
}
else
{
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>$tvat</td>";
if($kar[0]=='Kerala')
{
print "<td align=right>".putComa($tcess)."</td>";
}

print "<td align=right>".putComa($thand)."</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=11 align=right>Rounded ".putComa(abs($rnd2))."</td></tr>";
  $gtot=($tp-$rnd2);
  print "<td colspan=11 align=right><b>Grand Total = ".putComa($gtot)."</td></tr>";
}
else
 $gtot=$tp;

print "<tr><td colspan=160>";
for($j=0;$j<=160;$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 AUTOMOBILES PVT 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>";
print "<tr><td colspan=2>Customer Signature</td></tr>";

log_entry($brcode,$user,$REMOTE_ADDR,"Print spareinvoice:$invno , inv type: $invtype");
?>
</table>



