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

include("../opendb.php");
include("../test.php");
?>
<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('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.engineer3.selectedIndex<=0)
 {
   alert('Inalid 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'>QUOTATION </font></h3>
</CENTER>
</CENTER>
<form name=f1 action='svjcardq.php' method='post' onSubmit="return valid()">
<p  style={background-color:silver}>
<br>
<?
 $jcres=mysqli_query($db,"select max(jcno) from jobcardq 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());?>'>
<font color=red>*</font>MSNO <select name='msno' onChange=getcname()>
<option></option>
<?
$c=1;
$msres=mysqli_query($db,"select a.acname,b.msno,b.dcomm,b.model from customer a,minput b where a.code=b.cust order by b.msno")or die('errr'.mysqli_error());
while($msrow=mysqli_fetch_array($msres))
{
 $modelres=mysqli_query($db,"select maname from machmaster where code='$msrow[model]'")or die('err3'.mysqli_error());
 $modelrow=mysqli_fetch_row($modelres);
 print "\n<option>$msrow[1]</option>";
 print "<script>\n cnamear[$c]='$msrow[0]'; dcommar[$c]='".datetohtml($msrow[dcomm])."';";
 print "modar[$c]='$modelrow[0]'; </script>";
 $c++;
}
?>
</select>
D_Comm <input type=text name='dcomm' size=10>
Model <input type=text name='model' size=6>
<br>
Customer <input name='cname' readonly size=40>
<?
/*$cusre=mysqli_query($db,"select * from customer where brcode='$brcode' order by acname");
while($cusrow=mysqli_fetch_array($cusre))
{
 print "<option>$cusrow[acname]</option>";
}*/
?>

<br>
 Start Date/Time <input type=text name=stdate value='<?print date("d-m-Y")?>'>
 <input type=text name=sttime value='<?print date("H:m:s")?>' size=6>
Close  Date/Time <input type=text name=cldate value='<?print date("d-m-Y")?>'>
<input type=text name=cldatetm value='<?print date("H:m:s")?>' size=6>
<br>
WTY Start Date/Time <input type=text name=wtystdate value='<?print date("d-m-Y")?>' size=10>
WTY End Date/Time <input type=text name=wtyenddate value='<?print date("d-m-Y")?>' size=10>
<font color=red>*</font>Type of Service <select name='servtype'>
<option></option>
<option>EC</option>
<option>S0</option>
<option>S1</option>
<option>S2</option>
<option>S3</option>
<option>S4</option>
<option>S5</option>
<option>ASC</option>
<option>Paid</option>
<option>Courtesy</option>
<option>PDI</option>
</select>
<font color=red>*</font> HMR <input type=text name='hmr' size=6 value='1'>
<br><font color=red>*</font> Engineer <select name='engineer3'>
<?
 $empre=mysqli_query($db,"select empname from empmaster where design like '%eng%'");
 while($emprow=mysqli_fetch_row($empre))
 {
  print "<option>$emprow[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<TH>Ex Qty
<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 id='tdamt' name='tdamt' value='0'>
<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>Total
</table>

<br>
Total Service Amount <input type=text readonly id='tcamt' name='tcamt'>
<Hr><input type=submit value='    SAVE JOB CARD    '>
</form>
<?
$re=mysqli_query($db,"select partcode,partname,hand_charge,cessonvat, cessonvat from partmaster order by partcode")or die('prt err:'.mysqli_error());
$i=1;
$tbl="sparetmp".$brcode.$user;
while($row=mysqli_fetch_array($re))
{
 $prres=mysqli_query($db,"select lstprice,discount from spareprmast where partcode='$row[partcode]' order by effectfrom desc limit 1");
 $prrow=mysqli_fetch_array($prres);
 $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]='$prrow[0]';";
 print " \nhandp[$i]='$row[hand_charge]'; conv[$i]='$row[cessonvat]'; discar[$i]='$prrow[discount]'; ";
 print "\n cessar[$i]='$row[cessonvat]'; </script>";
 $i++;
}
?>
