<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Invalid Login') </script>";
 return;
}

include("../opendb.php");
include("../test.php");
print 'Branch Code:'.$brcode;

?>
<script>
var cur=1
var partcode=[];
var partname=[];
var lstprice=[];
var handp=[];
var conv=[];
var discar=[];
var cessar=[];
var exq=[];
function delrow(r)
{
 var i=r.parentNode.parentNode.rowIndex
 document.getElementById('myTable').deleteRow(i)
 cur--;
}

function insRow()
{
var x=document.getElementById('myTable').insertRow(1)
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 agval=x.insertCell(5)

var dis=x.insertCell(6)
var vatt=x.insertCell(7);
var cessper=x.insertCell(8);
var cessamt=x.insertCell(9);

var hnd=x.insertCell(10);

var taxamt1=x.insertCell(11);

var tot=x.insertCell(12);
var dd=x.insertCell(13);
var qqty=x.insertCell(14);
var eng=x.insertCell(15);

a.innerHTML=cur;

var value="<select name=partcode[] id='partcode"+cur+"'onChange=setval(this.selectedIndex,this)><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 readonly 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 readonly 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' name='vat[]' readonly value='12.5' id=vat"+cur+" size=4 onChange=calcamt(cur)>";

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

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

tot.innerHTML="<input type=text readonly value='total' name='gtot[]' id=gtot"+cur+" size=8>";
qqty.innerHTML="<input type=text readonly name='exqty[]' id=exqty"+cur+" size=3 >";
var xv="";
xv="<select name='spareengineer[]' id=spareengineer"+cur+"><option></option>\n";

<?
 $empre=mysqli_query($db,"select empname from empmaster where design like '%eng%'");
 while($emprow=mysqli_fetch_row($empre))
 {
  print "xv=xv+'<option>$emprow[0]</option>' \n";
 }
?>

eng.innerHTML=xv+"</select> ";
//alert(cur);
cur++; 
}// end function insrow

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 qqtyele='exqty'+(cur-1);
 var qele='qty'+(cur-1)

 document.getElementById(qele).value=0;
 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=discar[x-1];
 document.getElementById(cessele).value=1; //cessar[x-1];
 document.getElementById(qqtyele).value=exq[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 qqtyele='exqty'+y;
handamt=0;
vatamt=0;

 var exisqty=parseFloat(document.getElementById(qqtyele).value);
 
 var qty=parseFloat(document.getElementById(qele).value);
 if(qty>exisqty)
 {
  alert('Insufficient Quantity');
 document.getElementById(qele).value=0;
 document.getElementById(amtele).value=0;

 // return;
 }
 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 calcamt2(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 qqtyele='exqty'+y;
handamt=0;
vatamt=0;

 var qty=document.getElementById(qele).value;
 var exqty2=document.getElementById(qqtyele).value;

 if(parseFloat(qty)>parseFloat(exqty2))
 {
  alert('Invalid quantity');
  document.getElementById(qele).value=0;
  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);
}

//**********************************
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 dd6=x.insertCell(7)	
var dd2=x.insertCell(8)	
var engs=x.insertCell(9)

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.30' onChange='calctotal(cur2)>";
dd4.innerHTML="<input type=text readonly name=edutx[] id=edutx"+cur2+" size=8 value='0' onChange='calctotal(cur2)>";
dd5.innerHTML="<input type=text readonly name=hredutx[] id=hredutx"+cur2+" size=8 value='0' 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)'>";
var v="";
v="<select name='spareengineer2[]' id=spareengineer2"+cur+"><option></option>\n";

<?
 $empre1=mysqli_query($db,"select empname from empmaster where design like '%eng%'");
 while($emprow1=mysqli_fetch_row($empre1))
 {
  print "v=v+'<option>$emprow1[0]</option>' \n";
 }
?>
engs.innerHTML=v+"</select> ";
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 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;
  totamt=parseFloat(amt)+parseFloat(stx)+parseFloat(edtx)+parseFloat(hredtx);
  document.getElementById(ttele).value=parseFloat(totamt).toFixed(2);
}
function calctotal2(vv)
{
  var sele='servamt'+vv
  var tx='servtx'+vv
  var ed='edutx'+vv;
  var hred='hredutx'+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;
  totamt=parseFloat(amt)+parseFloat(stx)+parseFloat(edtx)+parseFloat(hredtx);
  document.getElementById(ttele).value=parseFloat(totamt).toFixed(2);
}

function valid()
{
   alert('Select Cash or Credit, Cash will be posted automatically');
 if(document.getElementById('ptype').selectedIndex<=0)
 {
   alert('Select Cash or Credit, Cash will be posted automatically');
   return false;
 }
 if(confirm('Do you want to save?')) 
  return true;
 else
  return false;
}
</script>
<?
if($show)
{
include("../spares/calcsparestock.php");
 $res=mysqli_query($db,"select * from jobcard where jcno='$jcno' and brcode='$brcode'");
 $jrow=mysqli_fetch_array($res);
?>
<form action='updjobcard.php' method='post' onSubmit='return valid()'>
<table border=1>
<th colspan=10 bgcolor=#AED69A>JCNo: <input type=text readonly name='jcno' size=5 value='<?print $jcno?>'></th>
<tr><td>Date </td><td><input type=text readonly name='date' value= '<?print $jrow[date]?>' size=8></td>
    <td>MSNO </td><td><input type=text readonly name='msno' value='<?print $jrow[msno]?>' size=12></td>

	<td>D Comm </td><td><input type=text readonly name='dcomm' value='<?print $jrow[dcomm]?>' size=8></td>
	<td>Model </td><td><input type=text readonly name='model' value='<?print $jrow[model]?>' size=8></td>
	<td>Customer </td><td><input type=text readonly name='cname' value='<?print $jrow[cname]?>'></td>
</tr>
<tr>	<td>St Date </td><td><input type=text readonly name='stdate' value='<?print $jrow[stdate]?>'></td>
	<td>Cl Date </td><td><input type=text readonly name='cldate' value='<?print $jrow[cldate]?>'></td>
	<td>WTY St Date </td><td><input type=text readonly name='wtstdate' value='<?print $jrow[wtystdate]?>' size=8></td>
	<td>WTY End Date </td><td><input type=text readonly name='wtyenddate' value='<?print $jrow[wtyenddate]?>' size=8></td>
	<td>Serv Type </td><td><input type=text readonly name='servtype' value='<?print $jrow[servtype]?>'></td>
</tr>
<tr>
	<td>HMR </td><td><input type=text readonly name='hmr' size=8 value='<?print $jrow[hmr]?>'></td>
	<td>Engineer </td><td><input type=text readonly name='engineer3' value='<?print $jrow[engineer]?>'></td>
	<td>User </td><td><input type=text readonly name='user' value='<?print $jrow[user]?>' size=8></td>
<td>Entry Date </td><td colspan=3>
dd <input type=text name='entrdate1' size=2 value='<?print date('d')?>'>
mm <input type=text name='entrdate2' size=2 value='<?print date('m')?>'>
yy <input type=text name='entrdate3' size=4 value='<?print date('Y')?>'>
</td>
</tr>
</table> 
<h3>Parts Issued </h3>
<input type="button" onclick="insRow()" value=" Add Item ">
<table border=1  id="myTable" >
<th> No <th>Part Code <th>Part Name  <th>Rate<th>Qty <th>Amount <th>Disc <th>VAT
<th>CessP <th>Cessamt <th>Hand <th>Tax Amt <th>Gtot<th>Del<th>Exqty <th>Engineer
<?
$tbl="sparetmp".$brcode.$user;
 $res=mysqli_query($db,"select * from jcardparts where jcno='$jcno' and brcode='$brcode'");
 $i=1;
 $j=100;
 while($jrow=mysqli_fetch_array($res))
  {
   print "<tr><td>".$i."</td><td><input type=text name='partcode[]' value='$jrow[2]' size=12></td>";
 print "<td><input type=text size=20 name='partname[]' value='$jrow[3]'></td>";
  print "<td><input type=text size=8  name='rate[]' id='rate$j' value='$jrow[rate]' onChange=calcamt2($j)> </td>";
   print "<td><input type=text size=4 name='qty[]' id='qty$j' value='$jrow[qty]' onChange=calcamt2($j)></td>";
   print "<td><input type=text size=8 readonly name='amt[]' id='amt$j' value='$jrow[amt]' onChange=calcamt2($j)></td>";
   print "<td><input type=text size=4 name='disc[]' id='disc$j' value='$jrow[disc]'></td>";
   print "<td><input type=text size=3 name='vat[]' readonly id='vat$j' value='12.5' onChange=calcamt2($j)></td>";

   print "<td><input type=text size=3 name='cessp[]' readonly id='cessp$j' value='1' onChange=calcamt2($j)></td>";
   print "<td><input type=text size=3 name='cessamt[]' readonly id='cesamt$j' value='".round(($jrow[taxamt]*1/100),2)."'></td>";

   print "<td><input type=text size=3 name='hnd[]' id='hnd$j' value='$jrow[hnd]' onChange=calcamt2($j)></td>";

   print "<td><input type=text size=5 name='taxamt[]' readonly id='taxamt$j' value='$jrow[taxamt]'></td>";
   print "<td><input type=text readonly size=8 name='gtot[]' id='gtot$j' value='$jrow[gtot]'></td>";
   print "<td><input type=button value='Del' onClick='delrow(this)'></td>";
 $qtres=mysqli_query($db,"select supqty from $tbl where partcode='$jrow[2]'");
 $qtrow=mysqli_fetch_row($qtres);
 if($qtrow[0]>0)
   $qqty=($qtrow[0]+$jrow[qty]);
 else
   $qqty=$jrow[qty];
   print "<td><input type=text  readonly name='exqty[]' id='exqty$j' value='$qqty' size=4></td>";
   print "<td><input type=text  readonly name='engineer[]' id='engineer$j' value='$jrow[engineer]' size=20></td></tr>";

   $i++;
   $j++;
  }
?>
</table> 
<h3>Service Details </h3>
 <input type="button" onclick="insRow2()" value="   Add Service   ">
<table border=1 id="myTable2">
<th>No.<th>Service  <th>Amt <th>Description<th>Serv Tax <th>Edu Cess <th>Hr.Edu Cess
<th>Total<th>Del<th>Engineer

<?
 $res=mysqli_query($db,"select * from jcardserv where jcno='$jcno' and brcode='$brcode'");
 $i=1;
 while($jrow=mysqli_fetch_array($res))
  {
   print "\n<tr><td>".$i."</td>";
   print "<td><input type=text size=20 name='service[]' value='$jrow[2]'></td>";
   print "\n<td><input type=text size=8 name='servamt[]' value='$jrow[3]' id='servamt$i' onChange='calctotal2($i)'></td>";
   print "\n<td><input type=text size=25 name='descr[]' id='descr$i' value='$jrow[4]'></td>";
   print "<td><input type=text size=6 name='servtx[]' id='servtx$i' value='$jrow[servtx]' onChange='calctotal2($i)'></td>";
   print "\n<td><input type=text size=6 name='edutx[]' id='edutx$i' value='$jrow[edutx]' onChange='calctotal2($i)'></td>";
   print "<td><input type=text size=6 name='hredutx[]' id='hredutx$i' value='$jrow[hredutx]' onChange='calctotal2($i)'></td>";
   print "<td><input type=text size=8 readonly name='tot[]' id='tot$i' value='$jrow[tot]'></td>";


   print "<td><input type=button value='Del' onClick='delrow2(this)'></td>";
   print "<td><input type=text  readonly name='engineer1[]' id='engineer1$j' value='$jrow[engineer]' size=20></td>";
 
 print "</tr>";
   $i++;
  }
?>
</table><br>
<table width=100%>
<tr bgcolor='#AED69A'><td>Invoice Type <select name='invtype'>
<option>8b</option>
<option>8</option>
</select>&nbsp;&nbsp;&nbsp;
 Payment Type  <select name='ptype' id='ptype'> 
<option></option>
<option>Cash</option>
<option>Credit</option>
</select>
</tr>
<tr><td bgcolor='#AED69A' align=right colspan=4>
<input type=submit value='  U p d a t e f o r m  ' name='upd' style="font-size:15; font-weight:bold">
<input type=submit value='  U p d a t e &nbsp; f o r m  &nbsp; a n d  &nbsp; P r i n t  &nbsp;  B i l l  ' name='updprn' style="font-size:15; font-weight:bold">

</td></tr>
</table>
</span>
</form>
<?
$re11=mysqli_query($db,"select partcode,partname,hand_charge,cessonvat, cessonvat from partmaster order by partcode")or die('prt err:'.mysqli_error());
$i=1;
while($row=mysqli_fetch_array($re11))
{
 $prres=mysqli_query($db,"select lstprice,discount from spareprmast where partcode='$row[partcode]' order by effectfrom desc limit 1");
 $prrow=mysqli_fetch_array($prres);

$stkres=mysqli_query($db,"select * from $tbl where partcode='$row[0]'");
$stkrow=mysqli_fetch_array($stkres);
if(mysqli_num_rows($stkres)>0)
 print "\n<script> exq[$i]='$stkrow[supqty]'</script>";
else
 print "\n<script> exq[$i]='0'</script>";

 print "\n<script>partcode[$i]='$row[0]'; partname[$i]='$row[1]'; lstprice[$i]='$prrow[0]';";
 print " \nhandp[$i]='0'; conv[$i]='$row[cessonvat]'; discar[$i]='0'; ";
 print "\n cessar[$i]='$row[cessonvat]'; </script>";
 $i++;
}
return;
}
?>
<CENTER>
<H3><font color='darkbrown'>JOB CARD REPORT / EDIT</font></h3>
</center>
<form name=f1 action='<?print $PHP_SELF?>' method='post' onSubmit='return valid1()'>
Select Jobcard No. <select name=jcno>
<?
$re=mysqli_query($db,"select jcno,msno from jobcard where brcode='$brcode' and jvno<=0");
while($row=mysqli_fetch_row($re))
{
 print "<option value='$row[0]'>$row[0]-$row[1]</option>";
}
?>
</select>
<input type=submit value='Show ->' name='show'>
</form>
<script>
 function valid1()
 {
  if(document.f1.jcno.value.length<=0)
   {
    alert('Invalid jcno');
    return false
   }
 }
</script>
