<?
session_start();
include("../opendb.php");
include("../test.php");
$result=mysqli_query($db,"select * from saleorder_spare where orderno='$pono' and brcode='$brcode'");
$row=mysqli_fetch_array($result);

$cusres=mysqli_query($db,"select * from customer where acname='$row[orderto]'");
$cusrow=mysqli_fetch_array($cusres);
?>
<title>Spare Sales </title>
<script>
function valid()
{
 if(document.f1.paytype.selectedIndex<=0)
 {
  alert('Invalid Payment Type');
  document.f1.paytype.focus();
  return false;
 }
 return true;
}
</script>

<form name="f1" action="svinvoicefoc.php" method=post >
<input type=hidden name='saleordno' value='<?print $pono?>'>
<input type=hidden name='invtype' value='<?print $taxtype?>'>

<?
$re=mysqli_query($db,"select max(invno) from spareinvoicefoc where brcode='$brcode' and invtype='$taxtype'");
$prow=mysqli_fetch_row($re);
if($prow[0]<=0)
 $invno=1;
else
 $invno=($prow[0]+1);

print "<table border=0 bgcolor=ivory cellspacing=1 cellpadding=3 ><th colspan=6><H3> SPARE INVOICE $taxtype (FOC)</TH>";
print "<tr><td>Sale Order No </td><td>$row[orderno]</td>";

print "<td>Sale Order Date </td><td>".datetohtml($row[date])."</td>";
print "<td>MSNO</td><td><input type=text name='msno' value='$row[msno]'></td></tr>";
print "<tr><td>Customer</td><td colspan=3><select name='customer'><option value='$row[orderto]'>$row[orderto]
</option></select></td>";
print "<td>Payment Type</td><td><select name='paytype'><option></option>";
print "<option>Cash</option><option>Credit</option></select></td></tr>";
print "<tr><td>Address</td><td><textarea name='addr'>$cusrow[addr2]</textarea></td>";
print "<td>TIN No.</td><td><input type=text name='tin' value='$cusrow[tin]'></td>";

print "<td>Sale Bill No.</td><td><input type=text name='invno' value='$invno'></td></tr>";
print "<tr><td>Sale Date</td><td>dd<input type=text name='d' size=2 maxlength=2 value='".date("d")."'>
mm<input type=text name='m' size=2 maxlength=2 value='".date("m")."'>
yy<input type=text name='y' size=4 maxlength=4 value='".date("Y")."'>
</td>";
print "<td>Entry Date</td><td>dd<input type=text name='d1' size=2 maxlength=2 value='".date("d")."'>
mm<input type=text name='m1' size=2 maxlength=2 value='".date("m")."'>
yy<input type=text name='y1' size=4 maxlength=4 value='".date("Y")."'>
</td></tr>";
print "<tr><th colspan=10></tr>";
print"</table><br>";

?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
//	tr{background-color:white}
	th{background-color:steelblue;color:white}
.right{text-align:right};
</style>
<center>

<table border=0 cellspacing=1 cellpadding=3 bgcolor='silver'>
<th>No<th>P Code <th> Name <th>Ord Qty <th> Sale Qty
<th>Price <th>Disc% <th> A Price<th>Hand Chrge 
<?
if($vatorcst=='CST')
   print "<th>CST%";
else
 print "<th>VAT%";
?>
<th>Tax Amt<th> Total
<?
    $taxres=mysqli_query($db,"select tax from taxmaster where type='$taxtype'");
    $taxrow=mysqli_fetch_row($taxres);
    $tax=$taxrow[0];
$i=1;
$result=mysqli_query($db,"select * from saleorder_spare where orderno='$pono' and brcode='$brcode'");
while($row=mysqli_fetch_array($result))
{
 $itmres=mysqli_query($db,"select * from partmaster where partcode='$row[0]'");
 $prtrow=mysqli_fetch_array($itmres);

 $prres=mysqli_query($db,"select * from spareprmast where partcode='$row[0]' order by effectfrom desc");
 $prrow=mysqli_fetch_array($prres);
 if(mysqli_num_rows($prres)<=0)
 {
  print "<tr><td colspan=10 align=center><font color=red>$row[0] - Price could not be found</font></td></tr>";

}
 print "\n<tr bgcolor='white'; onMouseOver=bgColor='c1d7bb'; onMouseOut=bgColor='white'>\n<td>".$i."</td><td><input type=text name='partcode[]' value='$row[0]' size=15></td><td>";
print "<textarea name='partname[]' rows=1>$row[1]</textarea></td><td align=right>$row[sqty]</td>";
 print "\n<td align=center><input type=text name='supqty[]' class=right id='$i' size=4 onChange=calc(this.id) value='0'></td>";

 print "<td align=center><input class=right type=text readonly name='price[]' id='price$i' size=8 value='$prrow[lstprice]'></td>";
 print "<td align=center><input class=right type=text  readonly name='disc[]' id='dis$i' size=6 value='0'></td>";
 print "<td align=center><input class=right type=text  readonly name='aprice[]' id='ap$i' size=8></td>";
 print "<td align=center><input class=right type=text  readonly name='hand[]' id='hnd$i' size=8 value='$prtrow[hand_charge]'></td>";
//********* vat or cst
if($vatorcst=='CST')
 print "<td align=center><input class=right type=text  readonly name='tax[]' id='tax$i' size=4 value='3'></td>";
else
 print "<td align=center><input class=right type=text  readonly name='tax[]' id='tax$i' size=4 value='$prtrow[tax_sales]'></td>";
//**********************
 print "<td align=center><input class=right type=text  readonly name='taxamt[]' id='taxamt$i' size=8 value='0'></td>";

 print "<td align=center><input class=right type=text  readonly name='tot[]' id='tot$i' size=10></td></tr>";
$i++;
}
print "\n<tr><td colspan=4> <center><b>Total </td><td align=center><input type=text class=right name='totsupqty[]' id='totsupqty' size=8></td>";
print "\n<td colspan=6 align=center></td><td align=center>";
print "<input type=text class=right id='tottx' size=8></td></tr>";

?>
<tr>
<th colspan=18><input type=submit value="Save" name="sv">
<input type=button value='Cancel' onClick=window.close()>
</table>
</form>

<script>
 function calc(x)
 {
 var ele='price'+x; 
 var disele='dis'+x;
 var actele='ap'+x;
 var hndele='hnd'+x;
 var taxele='tax'+x;
 var totele='tot'+x;
 var txamtele='taxamt'+x;
 var focele='foc'+x;
 var warele='war'+x;

 var totsupq='totsupqty';
 var tx='tottx';

 var amt=parseFloat(document.getElementById(ele).value)*(parseFloat(document.getElementById(x).value)); //+parseFloat(document.getElementById(warele).value));
 var disc=amt*parseFloat(document.getElementById(disele).value)/100;
 var actamt=amt-disc;
//document.getElementById(disele).value=parseFloat(disc);

document.getElementById(actele).value=parseFloat(actamt);

var hnd=actamt*document.getElementById(hndele).value/100;
var tx=parseFloat(document.getElementById(taxele).value);
var tax=((actamt+hnd)*tx)/100;
document.getElementById(txamtele).value=tax.toFixed(2);
document.getElementById(totele).value=(actamt+hnd+tax).toFixed(2);

tot=0;
for(j=1;j<<?print $i?>;j++)
 {
   var focele1='foc'+j;
   var warele1='war'+j;
//   f=parseFloat(document.getElementById(focele1).value); 
//   w=parseFloat(document.getElementById(warele1).value); 

   t=document.getElementById(j).value; 
   if(t.length<=0) 
     t=0;
   tot=parseFloat(tot)+parseFloat(t); //+f+w;
}

tottax1=0;
for(j=1;j<<?print $i?>;j++)
 {
 var tx='taxamt'+j; 
   t=document.getElementById(tx).value; 

   if(t.length<=0) 
     t=0;
   tottax1=parseFloat(tottax1)+parseFloat(t);
}

document.getElementById(totsupq).value=parseFloat(tot).toFixed(2);
document.getElementById('tottx').value=parseFloat(tottax1).toFixed(2);

 }
</script>

