<?
session_start();
include("../opendb.php");
include("../test.php");
include ("../spares/calcsparestock.php");
print 'Branch Code:'.$brcode;
?>
<script>
var qt=[];
var partcode=[];
var partname=[];
var lstprice=[];
var handp=[];
var conv=[];
var discar=[];
var cessar=[];

var cnamear=[];
var dcommar=[];
var modar=[];

var cur=1;
function valid()
{
/* if(document.f1.msno.value.length<=0)
 {
   alert('Inalid Machine Serial Number');
   document.f1.msno.focus();
   return false;
 }
 if(document.f1.servtype.selectedIndex<=0)
 {
   alert('Inalid Service Type');
   document.f1.servtype.focus();
   return false;
 }
 if(document.f1.hmr.value.length<=0)
 {
   alert('Inalid HMR');
   document.f1.hmr.focus();
   return false;
 }
 if(document.f1.engineer.selectedIndex<=0)
 {
   alert('Inalid Engineer');
   document.f1.engineer.focus();
   return false;
 }*/
 if(confirm('Do you want to save?'))
  return true;
 else
  return false;
}
function setval(x)
{
 var ele1='partname'+(cur-1);
 var ele2='rate'+(cur-1);
 var hand='hnd'+(cur-1);
 var cpele='cessp'+(cur-1);
 var disele='disc'+(cur-1);
 var cessele='cessp'+(cur-1);
 var exqele='qt'+(cur-1);
 document.getElementById(ele1).value=partname[x-1];
 document.getElementById(ele2).value=lstprice[x-1];
 document.getElementById(hand).value=handp[x-1];
 document.getElementById(cpele).value=conv[x-1];
 document.getElementById(disele).value=0; //discar[x-1];
 document.getElementById(cessele).value=cessar[x-1];
 document.getElementById(exqele).value=qt[x-1];
}
function calcamt(y)
{y--;
 var ele2='rate'+(y);
 var qele='qty'+y;
 var amtele='amt'+y;
 var disele='disc'+y;
 var handele='hnd'+y;
 var vatele='vat'+y;
 var taxele='taxamt'+y;
 var cessele='cesamt'+y;
 var cesspele='cessp'+y;
 var gtotele='gtot'+y;
 var exqele='qt'+y;
handamt=0;
vatamt=0;

 var qty=document.getElementById(qele).value;
 var exqty=parseFloat(document.getElementById(exqele).value);
 if(parseFloat(qty)>exqty)
 {
   alert('Insufficient Quantity');
   document.getElementById(qele).value=0;
   document.getElementById(qele).focus();
   return false
 }


 var rate=document.getElementById(ele2).value;
 var amount=parseFloat(qty)*parseFloat(rate);
 var discp=parseFloat(document.getElementById(disele).value);
 if(discp>0)
 {
   disamt=amount*discp/100;
   amount-=disamt;
 }
document.getElementById(amtele).value=amount.toFixed(2);
handp=parseFloat(document.getElementById(handele).value);
if(handp>0)
 handamt=(amount*handp)/100;
vatp=parseFloat(document.getElementById(vatele).value);
vatamt=((amount+handamt)*vatp)/100;
amt1=(amount+handamt)+vatamt;

document.getElementById(taxele).value=vatamt.toFixed(2);
cessamt=(vatamt*document.getElementById(cesspele).value)/100;
amt1+=cessamt;
document.getElementById(cessele).value=cessamt.toFixed(2);
document.getElementById(gtotele).value=amt1.toFixed(2);
}
function delrow(r)
{
 var i=r.parentNode.parentNode.rowIndex
 document.getElementById('myTable').deleteRow(i)
 cur--;
}

function insRow()
{
var x=document.getElementById('myTable').insertRow(cur)
var a=x.insertCell(0)
var y=x.insertCell(1)
var z=x.insertCell(2)	
var z1=x.insertCell(3)	
var ag=x.insertCell(4)
var dis=x.insertCell(5)
var agval=x.insertCell(6)
var hnd=x.insertCell(7);
var vatt=x.insertCell(8);
var taxamt1=x.insertCell(9);
var cessper=x.insertCell(10);
var cessamt=x.insertCell(11);
var tot=x.insertCell(12);
var dd=x.insertCell(13);
var q1=x.insertCell(14);
a.innerHTML=cur;

var value="<select name=partcode[] id='partcode"+cur+"'onChange=setval(this.selectedIndex)><option></option>";

for(k=0;k<partcode.length;k++)
 value=value+"<option>"+partcode[k]+"</option>";

value=value+"</select>";
y.innerHTML=value

dd.innerHTML="<input type=button value='Del' onClick='delrow(this)'>";
z.innerHTML="<input type=text name=partname[] id=partname"+cur+" size=20>"
z1.innerHTML="<input type=text id='rate"+cur+"' name=rate[] size=10>"


ag.innerHTML="<input type=text name='qty[]' id=qty"+cur+" size=3 onChange=calcamt(cur)>";
agval.innerHTML="<input type=text name='amt[]' id=amt"+cur+" size=8>";
dis.innerHTML="<input type=text name='disc[]' id=disc"+cur+" size=6 onChange=calcamt(cur)>";
vatt.innerHTML="<input type=text  name='vat[]' value='12.5' id=vat"+cur+" size=4 onChange=calcamt(cur)>";

cessper.innerHTML="<input type=text value='cessp' name='cessp[]' value='1' id=cessp"+cur+" size=4 onChange=calcamt(cur)>";
cessamt.innerHTML="<input type=text readonly value='cessamt' name='cesamt[]' id=cesamt"+cur+" size=6 onChange=calcamt(cur)>";

hnd.innerHTML="<input type=text value='0.0' name='hnd[]' id=hnd"+cur+" size=3 onChange=calcamt(cur)>";
taxamt1.innerHTML="<input type=text readonly value='0.0' name='taxamt[]' id=taxamt"+cur+" size=6>";

tot.innerHTML="<input type=text value='total' name='gtot[]' id=gtot"+cur+" size=8>";
q1.innerHTML="<input type=text READONLY value='' name='gt2[]' id=qt"+cur+" size=8>";
//alert(cur);
cur++; 
}// end function insrow
var cur2=1;
function delrow2(r)
{
 var i=r.parentNode.parentNode.rowIndex
 document.getElementById('myTable2').deleteRow(i)
 //cur2--;
}


function insRow2()
{
var x=document.getElementById('myTable2').insertRow(cur2)
var a=x.insertCell(0)
var y=x.insertCell(1)
var z=x.insertCell(2)	
var z1=x.insertCell(3)	
var dd3=x.insertCell(4)	
var dd4=x.insertCell(5)	
var dd5=x.insertCell(6)	
var dd7=x.insertCell(7)
var dd8=x.insertCell(8)
var dd6=x.insertCell(9)	

var dd2=x.insertCell(10)	

a.innerHTML=cur2
y.innerHTML="<input type=text name=service[] id=service"+cur2+" size=30>"
z.innerHTML="<input type=text name=servamt[] id=servamt"+cur2+" size=8 onChange='calctotal(cur2)'>"
z1.innerHTML="<input type=text name=descr[] id=desc"+cur2+" size=20>";
dd3.innerHTML="<input type=text name=servtx[] id=servtx"+cur2+" size=8 value='10' onChange='calctotal(cur2)'>";
dd4.innerHTML="<input type=text name=edutx[] id=edutx"+cur2+" size=8 value='3' onChange='calctotal(cur2)'>";
dd5.innerHTML="<input type=text name=hredutx[] id=hredutx"+cur2+" size=8 value='0' onChange='calctotal(cur2)'>";
dd7.innerHTML="<input type=text name=vat_amt[] id=vat_amt"+cur2+" size=8 value='4' onChange='calctotal(cur2)'>";
dd8.innerHTML="<input type=text name=cess_amt[] id=cess_amt"+cur2+" size=8 value='1' onChange='calctotal(cur2)'>";
dd6.innerHTML="<input type=text readonly name=tot[] id=tot"+cur2+" size=8 value='0'>";
dd2.innerHTML="<input type=button value='Del' onClick='delrow2(this)'>";
cur2++;
} // end function insrow2

function calctotal(vv)
{vv--;
  var sele='servamt'+vv;
  var tx='servtx'+vv;
  var ed='edutx'+vv;
  var hred='hredutx'+vv;
  var mvat='vat_amt'+vv;
  var mcess='cess_amt'+vv;
  var ttele='tot'+vv;

  amt=document.getElementById(sele).value;
  stx=(amt*parseFloat(document.getElementById(tx).value))/100;
  edtx=(stx*parseFloat(document.getElementById(ed).value))/100;
  hredtx=(stx*parseFloat(document.getElementById(hred).value))/100;
  vatamt=(amt*parseFloat(document.getElementById(mvat).value))/100;
  cessamt=(vatamt*parseFloat(document.getElementById(mcess).value))/100;
  totamt=parseFloat(amt)+parseFloat(stx)+parseFloat(edtx)+parseFloat(hredtx)+parseFloat(vatamt)+parseFloat(cessamt);
  document.getElementById(ttele).value=parseFloat(totamt).toFixed(2);
}
function getcname()
{ 
 document.f1.cname.value=cnamear[document.f1.msno.selectedIndex];
 document.f1.dcomm.value=dcommar[document.f1.msno.selectedIndex];
 document.f1.model.value=modar[document.f1.msno.selectedIndex];

}
</script>

<CENTER>
<H3><font color='darkbrown'>AMC BILL</font></h3>
</CENTER>
</CENTER>
<form name=f1 action='svjcard.php' method='post' onSubmit="return valid()">
<p  style={background-color:silver}>
<br>&nbsp;
<?
 $jcres=mysqli_query($db,"select max(jcno) from jobcardamc where brcode='$brcode'");
 $jcrow=mysqli_fetch_row($jcres);
 if($jcrow[0]<=0)
   $jcno=1;
 else
  $jcno=($jcrow[0]+1);
?>
No <input type=text name='jcno' value='<?print $jcno?>'>
Date dd <input type=text name='d' size='2' value='<?print date("d",time());?>'>
mm <input type=text name='m' size='2' value='<?print date("m",time());?>'>
yy <input type=text name='y' size='4' value='<?print date("Y",time());?>'>

Customer <select name='cname'>
<option></option>
<?

$cusre=mysqli_query($db,"select acname from customer where brcode='$brcode' order by acname");
while($cusrow=mysqli_fetch_array($cusre))
{
 print "<option>$cusrow[0]</option>";
}
?>
</select>
<br>&nbsp;
</p>

<input type="button" onclick="insRow()" value=" Add Item ">

<br>&nbsp;
<table id="myTable" border="1">
<th>No.<th>Part Code <th>Part Name <th>Rate <th>QTY <th>Disc <th>Amount <th>Hand <th>VAT
<th>TaxAmt <th>CESS <th>Cess Amt<th>Total<th>Del
<tr>
<!--
<td><input type=text name=dr[] onClick=setval()></td>
<td><input type=text name=dr_amt[]></td>
-->
</tr>
</table><br>
Total  Amount <input type=text readonly name='tdamt'>
<hr>

Service <input type="button" onclick="insRow2()" value=" Add Service ">
<input type='button' onclick='delRow2(this)' value=' - '>
<input type='button' onclick='calccr()' value=' CALCULATE SERVICE SUM  '>

<br>&nbsp;

<table id="myTable2" border="1">
<th>No.<th>Service  <th>Amt <th>Description<th>Serv Tax <th>Edu Cess <th>Hr.Edu Cess<th>VAT<th>Cess<th>Total
</table>

<br>
Total Service Amount <input type=text readonly name='tcamt'>
<Hr><input type=submit value='    SAVE JOB CARD    '>
</form>
<?
$re=mysqli_query($db,"select a.partcode,a.partname,b.lstprice,a.hand_charge,a.cessonvat,b.discount, a.cessonvat from partmaster a,spareprmast b where a.partcode=b.partcode  order by b.effectfrom desc,a.partcode")or die('prt err:'.mysqli_error());
$i=1;
$tbl="sparetmp".$brcode.$user;
while($row=mysqli_fetch_array($re))
{
 $qtres=mysqli_query($db,"select supqty from $tbl where partcode='$row[0]'");
 $qtrow=mysqli_fetch_row($qtres);
 if($qtrow[0]>0)
   $qqty=$qtrow[0];
 else
   $qqty=0;
 print "\n<script> qt[$i]='$qqty'; </script>";
 print "\n<script>partcode[$i]='$row[0]'; partname[$i]='$row[1]'; lstprice[$i]='$row[2]';";
 print " \nhandp[$i]='$row[hand_charge]'; conv[$i]='$row[cessonvat]'; discar[$i]='$row[discount]'; ";
 print "\n cessar[$i]='$row[cessonvat]'; </script>";
 $i++;
}
?>
