<?
session_start();
include ("../opendb.php");
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
$sq="select jvno from jobcard where jcno='$jcno' and brcode='$brcode'";
$r_r=mysqli_query($db,$sq) or die(mysqli_error());
$r_row=mysqli_fetch_array($r_r);
if($r_row[0]>0)
{
 print "<script> alert('Duplicate saving of jobcard $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)
{
 if($a<=0)
 {
  print "<script> alert('Invalid Cess Amount'); </script>";
  print "<h2><font color=red>Invalid Cess, data cannot be saved</font></h2>";
  return;
}
 $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($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="sparetmp".$brcode;


$invres=mysqli_query($db,"select max(invno) from jobcard 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 jobcard where jcno='$jcno' and brcode='$brcode'";

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

include ("../spares/calcsparestock.php");

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

/*
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 jobcard set jvno='0',invno='0' where jcno='$jcno' and brcode='$brcode'";
         mysqli_query($db,$sq) or die(mysqli_error());
      return;
    }
}
}
*/
$t=1;
$dt=date('Y-m-d');
for($j=0;$j<$i;$j++)

{
if($qty[$j]>0)
{
if($updprn)
  {
 if (strlen($spareengineer[$j])!=0)
{
  $sql="insert into jcardparts values('$jcno','$brcode','$partcode[$j]','$partname[$j]','$rate[$j]', '$qty[$j]', '$amt[$j]','$disc[$j]', '12.5', '1' , '$cessamt[$j]', '$hnd[$j]', '$taxamt[$j]', '$gtot[$j]','$jvno','$invno','$dt','$spareengineer[$j]')";
 mysqli_query($db,$sql) or die("Err 1".mysqli_error());
}
else
{
  $sql="insert into jcardparts values('$jcno','$brcode','$partcode[$j]','$partname[$j]','$rate[$j]', '$qty[$j]', '$amt[$j]','$disc[$j]', '12.5', '1' , '$cessamt[$j]', '$hnd[$j]', '$taxamt[$j]', '$gtot[$j]','$jvno','$invno','$dt','$engineer[$j]')";
 mysqli_query($db,$sql) or die("Err 1".mysqli_error());
}
 
 }
    else
  {
if (strlen($spareengineer[$j])!=0)
 {
  $sql="insert into jcardparts values('$jcno','$brcode','$partcode[$j]','$partname[$j]','$rate[$j]', '$qty[$j]', '$amt[$j]','$disc[$j]', '12.5', '1' , '$cessamt[$j]', '$hnd[$j]', '$taxamt[$j]', '$gtot[$j]','0','0','$dt','$spareengineer[$j]')";
 mysqli_query($db,$sql) or die("Err 1".mysqli_error());

  }
else
   {

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

  }

}
//print $spareengineer[$j];

print $engineer[$j];

}
}

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

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

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

}
foreach($engineer1 as $a)
{
 $engineer1[$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)
{
if($updprn)
{
if (strlen($spareengineer2[$j])!=0)
{ 
$sql="insert into jcardserv (jcno, brcode, service,servamt, descr, servtx,edutx,hredutx,tot,jvno,invno,engineer)   values('$jcno','$brcode','$service[$j]','$servamt[$j]','$descr[$j]','$servtx[$j]','$edutx[$j]','$hredutx[$j]','$tot[$j]','$jvno','$invno','$spareengineer2[$j]')";
}
else
{
$sql="insert into jcardserv (jcno, brcode, service,servamt, descr, servtx,edutx,hredutx,tot,jvno,invno,engineer)   values('$jcno','$brcode','$service[$j]','$servamt[$j]','$descr[$j]','$servtx[$j]','$edutx[$j]','$hredutx[$j]','$tot[$j]','$jvno','$invno','$engineer1[$j]')";
}

}


else
{
if (strlen($spareengineer2[$j])!=0)
{
 $sql="insert into jcardserv (jcno, brcode, service,servamt, descr, servtx,edutx,hredutx,tot,engineer)   values('$jcno','$brcode','$service[$j]','$servamt[$j]','$descr[$j]','$servtx[$j]','$edutx[$j]','$hredutx[$j]','$tot[$j]','$spareengineer2[$j]')";
}
else

{
 $sql="insert into jcardserv (jcno, brcode, service,servamt, descr, servtx,edutx,hredutx,tot,engineer)   values('$jcno','$brcode','$service[$j]','$servamt[$j]','$descr[$j]','$servtx[$j]','$edutx[$j]','$hredutx[$j]','$tot[$j]','$engineer1[$j]')";

}
} 
 mysqli_query($db,$sql) or die("Err 3".mysqli_error());
}
}
print "<font color=green> Data has been updated </font>";
$x=0;
if($updprn)
{
// check for duplicate entry in jv
$chkjvnores=mysqli_query($db,"select * from journal2 where brcode='$brcode' and jvno='$jvno'");
if(mysqli_num_rows($chkjvnores)>0)
{
  print "<script> alert('Posting  already done'); </script>";
  return;
}
// end check
  $jpres=mysqli_query($db,"select sum(gtot) from jcardparts where jcno='$jcno' and brcode='$brcode'")or die('jpres err'.mysqli_error());
  $jprow=mysqli_fetch_row($jpres);
  
 if($jprow[0]>0)
  $x=1;  
$spq="select sum(servamt+((servamt*servtx/100)+(servamt*edutx/100)+(servamt*hredutx/100))) from jcardserv where jcno='$jcno' and brcode='$brcode'";
 // $jservres=mysqli_query($db,"select sum(tot) from jcardserv where jcno='$jcno' and brcode='$brcode'")or die('jservres err'.mysqli_error());
  $jservres=mysqli_query($db,$spq)or die('jservres err'.mysqli_error());
  $jservrow=mysqli_fetch_row($jservres);
 if($jservrow[0]>0)
  $x=1;  

  $gtot=($jprow[0]+$jservrow[0]);
 if($x<=0)
  {
    print "<script> alert('Invalid Details'); </script>";
  $sql="insert into journal2 values('$entrydate','$jvno','Dr','$cname','0','0','Empty Job Card :$jcno','$brcode','JC Spare Invoice','JB Crd','$jcno','$jcno')";
 mysqli_query($db,$sql) or die('jv1 err:'.mysqli_error());
  $sql="insert into journal2 values('$entrydate','$jvno','Dr','$cname','0','0', 'Empty Job Card :$jcno', '$brcode','JC Spare Invoice','JB Crd','$jcno','$jcno')";
 mysqli_query($db,$sql) or die('jv1 err:'.mysqli_error());
    return;
  }
  $sql="insert into journal2 values('$entrydate','$jvno','Dr','$cname','$gtot','0','Job Card :$jcno','$brcode','JC Spare Invoice','JB Crd','$jcno','$jcno')";
 mysqli_query($db,$sql) or die('jv1 err:'.mysqli_error());

//******** service

 $taxres11=mysqli_query($db,"select servtx, sum(servamt) from jcardserv where jcno='$jcno' and brcode='$brcode' group by servtx")or die('Led Post Err3'.mysqli_error());
  while($taxrow1=mysqli_fetch_array($taxres11))
  {
    $ledger='Labour (JC)'.$taxrow1[servtx].'%';  
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow1[1]','JC Spare jcno: $jcno','$brcode','JC Spare Invoice','Job Crd','$jcno','0')";
     mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error());
  }

//******** service2

 $taxres12=mysqli_query($db,"select servtx, sum((servamt*servtx)/100) from jcardserv where jcno='$jcno' and brcode='$brcode' group by servtx")or die('Led Post Err3'.mysqli_error());
  while($taxrow1=mysqli_fetch_array($taxres12))
  {
    $ledger='Service Tax (JC)'.$taxrow1[servtx].'%';  
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow1[1]','JC Spare jcno: $jcno','$brcode','JC Spare Invoice','Job Crd','$jcno','0')";
     mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error());
  }
//******** service3

 $taxres13=mysqli_query($db,"select edutx, sum((servamt*servtx)/100)*edutx/100 from jcardserv where jcno='$jcno' and brcode='$brcode' group by edutx")or die('Led Post Err3'.mysqli_error());
  while($taxrow1=mysqli_fetch_array($taxres13))
  {
    $ledger='Educational Tax (JC)'.$taxrow1[edutx].'%';  
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow1[1]','JC Spare jcno: $jcno','$brcode','JC Spare Invoice','Job Crd','$jcno','0')";
     mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error());
  }
//******** service3 HR

 $taxres14=mysqli_query($db,"select hredutx, sum((servamt*servtx)/100)*hredutx/100 from jcardserv where jcno='$jcno' and brcode='$brcode' group by hredutx")or die('Led Post Err3'.mysqli_error());
  while($taxrow1=mysqli_fetch_array($taxres14))
  {
    $ledger='HR Educational Tax (JC)'.$taxrow1[hredutx].'%';  
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow1[1]','JC Spare jcno: $jcno','$brcode','JC Spare Invoice','Job Crd','$jcno','0')";
     mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error());
  }
//***************
  $sql="select sum(amt+(amt*hnd/100)),vat from jcardparts where jcno='$jcno' and brcode='$brcode' group by vat";
  $bfrres=mysqli_query($db,$sql) or die(mysqli_error());
  while($brrrow=mysqli_fetch_array($bfrres))
  {
    $ledger='Spare invoice (JC)'.$brrrow[vat].'%';  
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$brrrow[0]','Spare invno(jc): $jcno','$brcode','Spare Invoice(JC)','J Card','$jcno','0')";
     mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
  }
//************vat
 $taxres=mysqli_query($db,"select vat, sum(taxamt) from jcardparts where jcno='$jcno' and brcode='$brcode' group by vat")or die('Led Post Err3'.mysqli_error());
  while($taxrow=mysqli_fetch_array($taxres))
  {
    $ledger='VAT Collected (JC)'.$taxrow[vat].'%';  
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[1]','JC Spare jcno: $jcno','$brcode','JC Spare Invoice','Job Crd','$jcno','0')";
     mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error());
  }
//*************** cess
 $taxcessres=mysqli_query($db,"select sum(cessamt),cessp from jcardparts where jcno='$jcno' and brcode='$brcode' group by jcno")or die('cess err.'.mysqli_error());
  while($taxcessrow=mysqli_fetch_array($taxcessres))
  {
    $ledger='Output Cess (JC)'.$taxcessrow[1].'%';
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxcessrow[0]','Spare invno (JC): $jcno','$brcode','JC Spare Invoice','J Card','$jcno','0')";
     mysqli_query($db,$sql) or die('Led Post Err6'.mysqli_error());
  }
$jvdifres=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
$jvdifrow=mysqli_fetch_row($jvdifres);
$dif=$jvdifrow[0];

if($dif<0)
{
$sql="insert into journal2 values('$entrydate','$jvno','Dr','Round Off','".abs($dif)."', '0','Round of adjusted JC No: $jcno','$brcode','JC Spare Invoice','jcno','$jcno','$invno')";
mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error());
}
else if($dif>0)
{
 $sql="insert into journal2 values('$entrydate','$jvno','Cr','Round Off','0','".abs($dif)."','Round of adjusted JC No: $jcno','$brcode','JC Spare Invoice','jcno','$jcno','$invno')";
mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error());
}

$jdres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
$jdiff=mysqli_fetch_row($jdres);
if($jdiff[0]!=$jdiff[1]||$jdiff[0]==0)
{
 print "<sctipt> alert('Error in posting. Please check the data now');</script>";
}

if($ptype=='Cash')
{
$rs=mysqli_query($db,"select max(crno) from cshrec where brcode='$brcode'");
$cr_row=mysqli_fetch_row($rs);
 if($cr_row[0]<=0)
   $crno=1;
 else   
   $crno=$cr_row[0]+1;

$jpostres=mysqli_query($db,"select sum(dramt) from journal2 where jvno='$jvno' and partic='$cname' and brcode='$brcode'");
$jpostrow=mysqli_fetch_row($jpostres);
$jvno++;

$sql="insert into journal2 values('$entrydate','$jvno','Dr','Cash','$jpostrow[0]','0','jobcard of $cname(jcno:$jcno,CR:$crno)','$brcode','CR','JC invoice','$jcno','$crno')";
mysqli_query($db,$sql) or die(mysqli_error());

 $sql="insert into journal2 values('$entrydate','$jvno','Cr','$cname','0','$jpostrow[0]','jobcard of $cname(jcno:$jcno)','$brcode','CR','JC invoice','$jcvno','$crno')";
mysqli_query($db,$sql) or die(mysqli_error());

 $sql="insert into cshrec values('$entrydate','$crno','Cr','$cname','0','$jpostrow[0]','jobcard of $cname(jcno:$jcno)','$brcode','$jvno','jcno','$jcno')";
mysqli_query($db,$sql) or die(mysqli_error());
print "<script> window.open('../accounts/payrec/prncshrec.php?no=$crno'); </script>";

} // end if pay type
  print "<script> window.open('prnjcardbill.php?jcno=$jcno'); </script>";
}
?>
<input type=button value='Close' onClick='window.close()'>
