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

include ("../opendb.php");
$partcode =$_REQUEST['partcode'];
$partname =$_REQUEST['partname'];
$rate =$_REQUEST['rate'];
$qty =$_REQUEST['qty'];
$amt =$_REQUEST['amt'];
$disc =$_REQUEST['disc'];
$price=$_REQUEST['price'];
$vat =$_REQUEST['vat'];
$cessp =$_REQUEST['cessp'];
$cesamt =$_REQUEST['cesamt'];
$hnd =$_REQUEST['hnd'];
$taxamt =$_REQUEST['taxamt'];
$gtot =$_REQUEST['gtot'];
$d=$_REQUEST['d'];
$m=$_REQUEST['m'];
$y=$_REQUEST['y'];
$dcomm=$_REQUEST['dcomm'];
$stdate=$_REQUEST['stdate'];
$cldate=$_REQUEST['cldate'];
$wtystdate=$_REQUEST['wtystdate'];
$wtyenddate=$_REQUEST['wtyenddate'];

$msno=$_REQUEST['msno'];
$model=$_REQUEST['model'];
$cname=$_REQUEST['cname'];
$servtype=$_REQUEST['servtype'];
$hmr=$_REQUEST['hmr'];
$engineer3=$_REQUEST['engineer3'];
$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 jobcard where brcode='$brcode'");
 $jcrow=mysqli_fetch_row($jcres);
 if($jcrow[0]<=0)
   $jcno=1;
 else
  $jcno=($jcrow[0]+1);


$sql="insert into jobcard (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($db));

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

for($j=0;$j<$i;$j++)
{
if($qty[$j]>0)
{
 $sql="insert into jcardparts 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($db));
}
}
$descr =$_REQUEST['descr'];
$service =$_REQUEST['service'];
$servamt =$_REQUEST['servamt'];
$servtx =$_REQUEST['servtx'];
$edutx =$_REQUEST['edutx'];
$hredutx =$_REQUEST['hredutx'];
$tot =$_REQUEST['tot'];

$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 jcardserv (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($db));
}
}
print "<font color=green> Data has been saved </font>";
?>
<input type=button value='Close' onClick='window.close()'>
