<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}

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

$i=0;
foreach($partcode as $a)
{
 $partcode[$i]=$a; 
 $i++;
}
$i=0;
foreach($partname as $a)
{
 $partname1[$i]=$a; 
 $i++;
}

$i=0;
foreach($rate as $a)
{
 $rate[$i]=$a; 
 $i++;
}

$i=0;
foreach($qty as $a)
{
 $qty[$i]=$a; 
 $i++;
}
$i=0;
foreach($amt as $a)
{
 $amt[$i]=$a; 
 $i++;
}
$i=0;
foreach($disc as $a)
{
 $disc[$i]=$a; 
 $i++;
}
$i=0;
foreach($vat as $a)
{
 $vat[$i]=$a; 
 $i++;
}
$i=0;
foreach($cessp as $a)
{
 $cessp[$i]=$a; 
 $i++;
}
$i=0;
foreach($cesamt as $a)
{
 if($a<=0)
 {
  print "<script> alert('Invalid Cess Amount'); </script>";
  print "<h2><font color=red>Invalid Cess, data cannot be saved</font></h2>";
  return;
}

 $cesamt[$i]=$a; 
 $i++;
}
$i=0;
foreach($hnd as $a)
{
 $hnd[$i]=$a; 
 $i++;
}
$i=0;
foreach($taxamt as $a)
{
 $taxamt[$i]=$a; 
 $i++;
}

$i=0;
foreach($gtot as $a)
{
 $gtot[$i]=$a; 
 $i++;
}

$date=$y.'-'.$m.'-'.$d;
$comd=explode('-',$dcomm);
$dcomm=$comd[2].'-'.$comd[1].'-'.$comd[0];

$stdate1=explode('-',$stdate);
$stdate=$stdate1[2].'-'.$stdate1[1].'-'.$stdate1[0].' '.$stdate1[3].$sttime;

$cldate1=explode('-',$cldate);
$cldate=$stdate1[2].'-'.$cldate1[1].'-'.$cldate1[0].' '.$cldate1[3].$cldatetm;

$wtystdate1=explode('-',$wtystdate);
$wtystdate=$wtystdate1[2].'-'.$wtystdate1[1].'-'.$wtystdate1[0];

$wtyenddate1=explode('-',$wtyenddate);
$wtyenddate=$wtyenddate1[2].'-'.$wtyenddate1[1].'-'.$wtyenddate1[0];

 $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);


$sql="insert into jobcardq (jcno , date, msno, dcomm, model, cname, stdate, cldate, wtystdate, wtyenddate, servtype,hmr,engineer, brcode,user) values('$jcno' , '$date', '$msno','$dcomm', '$model', '$cname', '$stdate', '$cldate', '$wtystdate', '$wtyenddate', '$servtype','$hmr', '$engineer3','$brcode','$user')";


 mysqli_query($db,$sql) or die("Err 0".mysqli_error());

$dt=date('Y-m-d');

for($j=0;$j<$i;$j++)
{
if($qty[$j]>0)
{
 $sql="insert into jcardpartsq values('$jcno','$brcode','$partcode[$j]','$partname[$j]','$rate[$j]', '$qty[$j]', '$amt[$j]','$disc[$j]', '$vat[$j]', '$cessp[$j]' , '$cesamt[$j]', '$hnd[$j]', '$taxamt[$j]', '$gtot[$j]','0','0','$dt','-')";

 mysqli_query($db,$sql) or die("Err 1".mysqli_error());
}
}

$i=0;
foreach($descr as $a)
{
 $descr[$i]=$a; 
 $i++;
}

$i=0;
foreach($service as $a)
{
 $service[$i]=$a; 
 $i++;
}

$i=0;
foreach($servamt as $a)
{
 $servamt[$i]=$a; 
 $i++;
}
$i=0;
foreach($servtx as $a)
{
 $servtx[$i]=$a; 
 $i++;
}
$i=0;
foreach($edutx as $a)
{
 $edutx[$i]=$a; 
 $i++;
}
$i=0;
foreach($hredutx as $a)
{
 $hredutx[$i]=$a; 
 $i++;
}
$i=0;
foreach($tot as $a)
{
 $tot[$i]=$a; 
 $i++;
}

for($j=0;$j<$i;$j++)
{
if($tot[$j]>0)
{
 $sql="insert into jcardservq (jcno, brcode, service,servamt, descr, servtx,edutx,hredutx,tot)   values('$jcno','$brcode','$service[$j]','$servamt[$j]','$descr[$j]','$servtx[$j]','$edutx[$j]','$hredutx[$j]','$tot[$j]')";

 mysqli_query($db,$sql) or die("Err 3".mysqli_error());
}
}
print "<font color=green> Data has been saved </font>";
?>
<input type=button value='Close' onClick='window.close()'>
