<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$level=$_SESSION['level'];

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

//include("top.php");

include("../opendb.php");
include("../test.php");

?>
<link href="../css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body >

<script>
var x = navigator;
if(x.appName!='Mozilla'&& x.appName!='Netscape')
{
 alert('Invalid Browser, Use only valid Web Browser');
 document.location='../blank.html'
}
</script>
<?
//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('Invalid Machine Serial Number');
   document.f1.msno.focus();
   return false;
 }*/
 if(document.f1.servtype.selectedIndex<=0)
 {
 //  alert('Invalid Service Type');
//   document.f1.servtype.focus();
   //return false;
 }
 if(document.f1.hmr.value.length<=0)
 {
   alert('Invalid HMR');
   document.f1.hmr.focus();
   return false;
 }
/* if(document.f1.engineer3.selectedIndex<=0)
 {
   alert('Invalid Engineer');
   document.f1.engineer3.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);
gg=parseFloat(document.getElementById('tdamt').value);
gg+=parseFloat(document.getElementById(gtotele).value);
document.getElementById('tdamt').value=gg;
}
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 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[]' readonly value='12.5' id=vat"+cur+" size=4 onChange=calcamt(cur)>";

cessper.innerHTML="<input type=text value='cessp' readonly 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' readonly  name='gtot[]' id=gtot"+cur+" size=8>";
q1.innerHTML="<input type=text 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 dd6=x.insertCell(7)	

var dd2=x.insertCell(8)	

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 name=edutx[] id=edutx"+cur2+" size=8 value='0' onChange='calctotal(cur2)>";
dd5.innerHTML="<input type=text 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)'>";
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 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'>JOB CARD  </font></h3>

<form name='f1' action='svjcard.php' method='post' onSubmit="return valid()">
<table border=1>
<p  style={background-color:silver}>
<br>
<?
 $jcres=mysqli_query($db,"select max(jcno) from jobcard where brcode='$brcode'");
 $jcrow=mysqli_fetch_row($jcres);
 if($jcrow[0]<=0)
   $jcno=1;
 else
  $jcno=($jcrow[0]+1);
 
?>
<tr><td>No</td><td> <input type=text name='jcno' value='<?print $jcno?>'></td>
<td>Date</td><td> 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='2' value='<?print date("Y",time());?>'>
</td><td>MSNO</td> <td><select name='msno' onChange=getcname()>
    <option value='select'>Direct Customer</option>

</select>

</td></tr><tr><td>D_Comm </td><td><input type=text name='dcomm' size=15></td>
<td>
Model</td><td> <input type=text name='model' size=20></td>
<td>
Customer</td><td>
<select name='cname' id='cname'>
<?
$cusre=mysqli_query($db,"select distinct(acname) from customer  order by acname asc");
while($cusrow=mysqli_fetch_array($cusre))
{
 print "<option>$cusrow[acname]</option>";
}
?>
</select>
</tr><tr>
<td> Start Date/Time</td><td> <input type=text name=stdate value='<?print date("d-m-Y")?>'>
 <input type=text name=sttime value='<?print date("H:m:s")?>' size=6></td>
<td>Close  Date/Time </td><td><input type=text name=cldate value='<?print date("d-m-Y")?>'>
<input type=text name=cldatetm value='<?print date("H:m:s")?>' size=6></td>

<td>WTY Start Date/Time</td><td> <input type=text name=wtystdate value='<?print date("d-m-Y")?>' size=10></td></tr><tr>
<td>WTY End Date/Time</td><td> <input type=text name=wtyenddate value='<?print date("d-m-Y")?>' size=10></td>

<td><font color=red>*</font>Type of Service</td><td> <select name='servtype'>
<?if($brcode=='MEN888'){?>
<option>Rent</option>
<option>Manpower</option>
<?}else{?>
<option>AUDIT FEE</option>
<option>Commission received</option>
<option>Management Fee</option>
</select></td>
<?}?>
<td><font color=red>*</font> HMR/KM</td><td> <input type=text value=1 name='hmr' size=6 ></td>
</tr><tr>
<td><font color=red>*</font> Engineer</td><td> <input type=text name='engineer3'>



</td>
<td>Supervisor</td><td><input type=text name=engineer3></td>
 
</tr>
<br>&nbsp;
<input type=hidden id='mnfr_cd' name='mnfr_cd' value='<?echo $manufact ?>'>
<tr><td colspan=6 align=center><input type=submit  value='SAVE JOB CARD'></td></tr>
</table>
</form>
