<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");
include("../util/exqty.php");
//include ("calcsparestock.php");
$result=mysqli_query($db,"select * from warorder_spare where orderno='$pono' and brcode='$brcode' and closed='N'");
$row=mysqli_fetch_array($result);

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

<form name="f1" action="svinvoice1.php" method=post onSubmit="return valid()">
<input type=hidden name='saleordno' value='<?print $pono?>'>
<input type=hidden name='invtype' value='<?print $taxtype?>'>
<input type=hidden name='vatorcst' value='<?print $vatorcst?>'>

<?
$sq="select max(invno) from warspareinvoice where brcode='$brcode' ";
//and invtype='$taxtype' and vatorcst='$vatorcst'
$re=mysqli_query($db,$sq);
$prow=mysqli_fetch_row($re);
if($prow[0]<=0)
 $invno=1;
else
 $invno=($prow[0]+1);
$tbl="sparetmp".$brcode.$user;

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

print "<td>WTY 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><font color=red>Payment Type</font></td><td><select name='paytype'><option></option>";
if($cusrow[cramt]<=0)
{
print "<option>Cash</option></select></td></tr>";
}
else
{
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>Wty Bill No.</td><td><input type=text readonly name='invno' value='$invno'></td></tr>";
print "<tr><td>SAP INVNO.</td><td><input type=text name='sinvno'></td>";
print "<td>SAP Date</td><td>dd<input type=text name='sd' size=2 maxlength=2 value='".date("d")."'>
mm<input type=text name='sm' size=2 maxlength=2 value='".date("m")."'>
yy<input type=text name='sy' size=4 maxlength=4 value='".date("Y")."'>
</td>";




print "<tr><td>Sale Date</td><td>dd<input type=text readonly 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>";
print "<td colspan=2>Ref: <input type=text name='ref' size=30></td> </tr>";
print "<tr><td>Engineer </td><td><select name='engineer'><option>Counter Sale</option>";
$enres=mysqli_query($db,"select * from empmaster where design like '%eng%' order by empname");
while($enrow=mysqli_fetch_array($enres))
{
 print "<option>$enrow[empname]</option>";
}
print "</select></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>CESS%<th>CESS <br>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 warorder_spare where orderno='$pono' and brcode='$brcode' and sqty>sup_qty and closed='N'");
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>";
}
/*$qtyres=mysqli_query($db,"select supqty from $tbl where partcode='$row[0]'");
$qtyrow=mysqli_fetch_array($qtyres);
if($qtyrow[0]<=0)
 $availqty=0;
else
 $availqty=$qtyrow[0];
*/
$availqty=exqty1($row[0],$brcode,$user);

print "\n<input type=hidden   name='availqty[]' id='availqty$i' value='$availqty'>";
 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>";
print "<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 "\n<td align=center><input type=text name='focqty[]' class=right id='foc$i' size=4  value='0'></td>";

 print "\n<td align=center><input type=text name='warqty[]' class=right id='war$i' size=4 onChange=calc($i) value='0'></td>";
*/

 print "<td align=center><input class=right type=text name='price[]' id='price$i' size=8 value='$prrow[lstprice]' onChange=calc($i)></td>";
if($level =='5')
 print "<td align=center><input class=right type=text name='disc[]' id='dis$i' size=1 value='0' onChange=calc(this.id)></td>";
else
 print "<td align=center><input class=right type=text name='disc[]' id='dis$i' size=1 value='0' onChange=calc(this.id)></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   name='hand[]' id='hnd$i' size=3 value='$prtrow[hand_charge]'></td>";
//********* vat or cst
if($vatorcst=='CST')
 print "<td align=center><input class=right type=text   name='tax[]' id='tax$i' size=4 value='3' onChange=calc($i)></td>";
else
 print "<td align=center><input class=right type=text   name='tax[]' id='tax$i' size=4 value='12.50' onChange=calc($i)></td>";
//**********************
 print "<td align=center><input class=right type=text   name='taxamt[]' id='taxamt$i' size=8 value='0'></td>";

if($vatorcst=='CST')
 print "<td align=center><input class=right type=text value='0.00' name='cessp[]' id='cessp$i' size=2 value='$prtrow[cessonvat]' onChange=calc($i)></td>";
else
 print "<td align=center><input class=right type=text   value='1.00' name='cessp[]' id='cessp$i' size=2  onChange=calc($i)></td>";

 print "<td align=center><input class=right type=text readonly  readonly name='cessamt[]' id='cessamt$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=5 align=center></td><td align=center>";
print "<input type=text class=right id='tottx' size=8></td>";
print "<td></td><td></td><td><input type=text name=ggtot id='ggtot' size=10></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 avail='availqty'+x;

 var cesspele='cessp'+x;
 var cessele='cessamt'+x;

 if(parseFloat(document.getElementById(x).value)>parseFloat(document.getElementById(avail).value) || document.getElementById(avail).value=='')
 {
   alert('Stock only '+document.getElementById(avail).value);
  document.getElementById(x).value=0;
  document.getElementById(x).focus();
   return false;
 }
 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);

var cessp1=document.getElementById(cesspele).value;
var cessam=(cessp1*tax)/100;
document.getElementById(cessele).value=cessam.toFixed(2);
var cs1=cessam; 
document.getElementById(totele).value=(actamt+hnd+tax+cs1).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;
gt=0;
for(j=1;j<<?print $i?>;j++)
 {
 var tx='taxamt'+j; 
 var gtot='tot'+j
   t=document.getElementById(tx).value; 
   if(parseFloat(document.getElementById(gtot).value)>0)
   gt=parseFloat(gt)+parseFloat(document.getElementById(gtot).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);
document.getElementById('ggtot').value=parseFloat(gt).toFixed(2);

 }
</script>

