<?
session_start();
include("test.php");
include("opendb.php");
include("includes/function.php");
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}


$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
$kar=mysqli_fetch_array($ker);
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
$sq="select jvno from ojobcard where jcno='$jcno' and brcode='$brcode'";
$r_r=mysqli_query($db,$sq) or die(mysqli_error($db));
$r_row=mysqli_fetch_array($r_r);
if($r_row[0]>0)

{
 print "<script> alert('Duplicate saving of ojobcard $jcno'); </script>";
 return;
}

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

 $cessamt[$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($spareengineer as $a)
{
 $spareengineer[$i]=$a; 
 $i++;
}
foreach($engineer as $a)
{
 $engineer[$i]=$a; 
 $i++;
}

$i=0;
foreach($day as $a)
{
 $day[$i]=$a; 
 $i++;
}
$i=0;
foreach($month as $a)
{
 $month[$i]=$a; 
 $i++;
}
$i=0;
foreach($year as $a)
{
 $year[$i]=$a; 
 $i++;
}



$i=0;
foreach($gtot as $a)
{
 $gtot[$i]=$a; 
 $i++;
}
$entrydate=$entrdate3.'-'.$entrdate2.'-'.$entrdate1;

//********** chk ledger

$chre=mysqli_query($db,"select * from ledger where led='$cname' and brcode='$brcode'");
if(mysqli_num_rows($chre)<=0)
{
$code=substr($customer,0,3).date(ymds);
$code=str_replace(' ','-',$code);

$sql="insert into ledger values('$cname','Sundry Debtors','0','Dr','$code','$brcode')";
mysqli_query($db,$sql);
}


$tbl="stockrepdet10".$brcode.$user;


$invres=mysqli_query($db,"select max(invno) from ojobcard where brcode='$brcode' and invtype='$invtype'");
$invrow=mysqli_fetch_row($invres);
if($invrow[0]>0)
 $invno=($invrow[0]+1);
else
 $invno=1;

$sql="delete from ojobcard where jcno='$jcno' and brcode='$brcode'";

mysqli_query($db,$sql) or die('err'.mysqli_error($db));
mysqli_query($db,"delete from ojcardparts where jcno='$jcno' and brcode='$brcode'");
mysqli_query($db,"delete from ojcardserv where jcno='$jcno' and brcode='$brcode'");

include ("spares/calcstock.php");
mysqli_query($db,"delete from $tbl where supqty<=0");

$jvres=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
$jvrow=mysqli_fetch_row($jvres);
if($jvrow[0]>0)
 $jvno=($jvrow[0]+1);
else
 $jvno=1;

/*
$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];
*/


if($updprn)
{
$sql="insert into ojobcard (jcno , date, msno, dcomm, model, cname, stdate, cldate, wtystdate, wtyenddate, servtype,hmr,engineer, brcode,user,jvno,invtype,ptype,invno,entrydate) values('$jcno' , '$date', '$msno','$dcomm', '$model', '$cname', '$stdate', '$cldate', '$wtystdate', '$wtyenddate', '$servtype','$hmr', '$engineer3','$brcode','$user','$jvno','$invtype','$ptype','$invno','$entrydate')";
}
else
{
$sql="insert into ojobcard (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));

/*
for($j1=0;$j1<$i;$j1++)
{
if($qty[$j1]>0)
{
  $chstkre=mysqli_query($db,"select * from $tbl where partcode='$partcode[$j1]'");
  $chstkrow=mysqli_fetch_array($chstkre);
   if($qty[$j1]>$chstkrow[supqty])
    {
      print "Insufficient Quantity ".$partcode[$j1];
	$sq="update ojobcard set jvno='0',invno='0' where jcno='$jcno' and brcode='$brcode'";
         mysqli_query($db,$sq) or die(mysqli_error($db));
      return;
    }
}
}
*/
$t=1;
$s=1;
print $i;
$dt=date('Y-m-d');
for($j=0;$j<$i;$j++)

{
if($qty[$j]>0)
{

$edate=$year[$j].'-'.$month[$j].'-'.$day[$j];

 $sql="insert into ojcardparts values('$jcno','$brcode','$partcode[$j]','$partname[$j]','$rate[$j]', '$qty[$j]', '$amt[$j]','$disc[$j]', '$vat[$j]', '1' , '$cessamt[$j]', '$hnd[$j]', '$taxamt[$j]', '$gtot[$j]','0','0','$dt','$spareengineer[$j]','".$s++."','$edate')";
 mysqli_query($db,$sql) or die("Err 1".mysqli_error($db));



}
}






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

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


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

$i=0;
foreach($day1 as $a)
{
 $day1[$i]=$a; 
 $i++;
}
$i=0;
foreach($month1 as $a)
{
 $month1[$i]=$a; 
 $i++;
}
$i=0;
foreach($year1 as $a)
{
 $year1[$i]=$a; 
 $i++;
}



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



$s=1;

for($j=0;$j<$i;$j++)
{

if(strlen($service[$j])>0)
{

$edate=$year1[$j].'-'.$month1[$j].'-'.$day1[$j];

 $sql="insert into ojcardserv (jcno, brcode, service,servamt, descr, servtx,edutx,hredutx,tot,engineer,edate,sno)   values('$jcno','$brcode','$service[$j]','$servamt[$j]','$jobs[$j]','$servtx[$j]','$edutx[$j]','$hredutx[$j]','$tot[$j]','$engineer1[$j]','$edate','".$s++."')";
 mysqli_query($db,$sql) or die("Err 2".mysqli_error($db));
print "<script> alert('Saved');document.location='jobcard.php';</script> ";
}
}
$us=mysqli_query($db,"select level from bruser where username='$user'")or die(mysqli_error($db));
$r=mysqli_fetch_row($us);
$user_level=$r[0];
 //print "User Level:".$user_level;
        
?>

