<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
include ("../opendb.php");
$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;
}
$jcno=$_REQUEST['jcno'];

$sq="select jvno from jobcard 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 jobcard $jcno'); </script>";
 return;
}

$partcode =$_REQUEST['partcode'];
$partname =$_REQUEST['partname'];
$rate =$_REQUEST['rate'];
$qty =$_REQUEST['qty'];
$amt =$_REQUEST['amt'];
$disc =$_REQUEST['disc'];
$spareengineer =$_REQUEST['spareengineer'];
$hnd =$_REQUEST['hnd'];
$taxamt =$_REQUEST['taxamt'];
$gtot =$_REQUEST['gtot'];
$engineer =$_REQUEST['engineer'];
$cname=$_REQUEST['cname'];
$invtype=$_REQUEST['invtype'];
$msno=$_REQUEST['msno'];
$model=$_REQUEST['model'];
$servtype=$_REQUEST['servtype'];
$hmr=$_REQUEST['hmr'];
$engineer3=$_REQUEST['engineer3'];
$ptype=$_REQUEST['ptype'];
$intr=$_REQUEST['intr'];

    $cgstpp =$_REQUEST['cgstpp'];
    $sgstpp =$_REQUEST['sgstpp'];
    $igstpp =$_REQUEST['igstpp'];
    $cgstamtp =$_REQUEST['cgstamtp'];
    $sgstamtp =$_REQUEST['sgstamtp'];
    $igstamtp =$_REQUEST['igstamtp'];

       //****service
    $cgstp =$_REQUEST['cgstp'];
    $sgstp =$_REQUEST['sgstp'];
    $igstp =$_REQUEST['igstp'];
    $cgstamt =$_REQUEST['cgstamt'];
    $sgstamt =$_REQUEST['sgstamt'];
    $igstamt =$_REQUEST['igstamt'];

$cessamt  =$_REQUEST['cessamt'];



$complaints  =$_REQUEST['complaints'];
$observation  =$_REQUEST['observation'];
$actiontaken  =$_REQUEST['actiontaken'];

$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($cgstpp as $a)
{
 $cgstpp[$i]=$a; 
 $i++;
}
$i=0;
foreach($sgstpp as $a)
{
 $sgstpp[$i]=$a; 
 $i++;
}
$i=0;
foreach($igstpp as $a)
{
 $igstpp[$i]=$a; 
 $i++;
}
$i=0;
foreach($cgstamtp as $a)
{
 $cgstamtp[$i]=$a; 
 $i++;
}
$i=0;
foreach($cessamt as $a)
{
    $cessamt[$i]=$a; 
    $i++;
}
$i=0;
foreach($sgstamtp as $a)
{
 $sgstamtp[$i]=$a; 
 $i++;
}
$i=0;
foreach($igstamtp as $a)
{
 $igstamtp[$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++;
}
$entrdate3=$_REQUEST['entrdate3'];
$entrdate2=$_REQUEST['entrdate2'];
$entrdate1=$_REQUEST['entrdate1'];
$dcomm =$_REQUEST['dcomm']; 
$stdate =$_REQUEST['stdate']; 
$cldate =$_REQUEST['cldate']; 
$wtystdate =$_REQUEST['wtystdate']; 
$wtyenddate =$_REQUEST['wtyenddate']; 
$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'");
$invrow=mysqli_fetch_row($invres);
if($invrow[0]>0)
 $invno=($invrow[0]+1);
else
{
    
        $invno = '23240001';  
}
$sql="delete from jobcard where jcno='$jcno' and brcode='$brcode'";

mysqli_query($db,$sql) or die('err'.mysqli_error($db));
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=$entrydate;

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

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

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

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

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


if($_REQUEST['updprn'])
{
    
    $cust11=mysqli_query($db,"select gst from customer where acname ='$cname'");
    $custrow=mysqli_fetch_array($cust11);
    $sql="insert into jobcard (jcno, date, msno, dcomm, model, cname, stdate, cldate, wtystdate, wtyenddate, servtype,hmr,engineer, brcode,user,jvno,invtype,ptype,invno,entrydate,gstno,complaints,action_taken,observation) values('$jcno' , '$date', '$msno','$dcomm', '$model', '$cname', '$stdate', '$cldate', '$wtystdate', '$wtyenddate', '$servtype','$hmr','$engineer3', '$brcode','$user','$jvno','$invtype','$ptype','$invno','$entrydate','$custrow[0]','$complaints','$actiontaken','$observation')";
 
}
else
{
    $sql="insert into jobcard (jcno, date, msno, dcomm, model, cname, stdate, cldate, wtystdate, wtyenddate, servtype,hmr,engineer, brcode,user,complaints,action_taken,observation) values('$jcno' , '$date', '$msno','$dcomm', '$model', '$cname', '$stdate', '$cldate', '$wtystdate', '$wtyenddate', '$servtype','$hmr','$engineer3', '$brcode','$user','$complaints','$actiontaken','$observation')";
}
echo $sql;

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

            if (strlen($spareengineer[$j])!=0)
            {
                if($intr=='INTRASTATE')
                {
                    if($cessamt[$j]>0)
                        $mcessamt = $cessamt[$j];
                    else
                        $mcessamt = 0;
                    
                  $sql="insert into jcardparts values('$jcno','$brcode','$partcode[$j]','$partname[$j]','$rate[$j]', '$qty[$j]', '$amt[$j]','$disc[$j]', '0', '1' , '$mcessamt','$cgstpp[$j]','$sgstpp[$j]','0','$cgstamtp[$j]','$sgstamtp[$j]','0', '$hnd[$j]', '$taxamt[$j]', '$gtot[$j]','$jvno','$invno','$dt')";
                }
                else
                {
                   $sql="insert into jcardparts values('$jcno','$brcode','$partcode[$j]','$partname[$j]','$rate[$j]', '$qty[$j]', '$amt[$j]','$disc[$j]', '0', '0' , '0','0','0','$igstpp[$j]','0','0','$igstamtp[$j]', '$hnd[$j]', '$taxamt[$j]', '$gtot[$j]','$jvno','$invno','$dt')";   
                }
                print $sql."000";
                mysqli_query($db,$sql) or die("Err 1".mysqli_error($db));
            }
            else
            {
                if($intr=='INTRASTATE')
                  {
                      if($cessamt[$j]>0)
                        $mcessamt = $cessamt[$j];
                    else
                        $mcessamt = 0;
                  $sql="insert into jcardparts values('$jcno','$brcode','$partcode[$j]','$partname[$j]','$rate[$j]', '$qty[$j]', '$amt[$j]','$disc[$j]', '0', '1' , '$mcessamt', '$cgstpp[$j]','$sgstpp[$j]','0','$cgstamtp[$j]','$sgstamtp[$j]','0','$hnd[$j]', '$taxamt[$j]', '$gtot[$j]','$jvno','$invno','$dt')";
                  }
                  else
                  {
                   $sql="insert into jcardparts values('$jcno','$brcode','$partcode[$j]','$partname[$j]','$rate[$j]', '$qty[$j]', '$amt[$j]','$disc[$j]', '0', '0' , '0', '0','0','$igstpp[$j]','0','0','$igstamtp[$j]','$hnd[$j]', '$taxamt[$j]','$gtot[$j]','$jvno','$invno','$dt')";   
                  }
                  print $sql."111";
                  mysqli_query($db,$sql) or die("Err 2".mysqli_error($db));
            }
      }
      else
      {
            if (strlen($spareengineer[$j])!=0)
            {
              
                  if($intr=='INTRASTATE')
                  { 
                      if($cessamt[$j]>0)
                        $mcessamt = $cessamt[$j];
                    else
                        $mcessamt = 0;
                  $sql="insert into jcardparts values('$jcno','$brcode','$partcode[$j]','$partname[$j]','$rate[$j]', '$qty[$j]', '$amt[$j]','$disc[$j]', '0', '1' , '$mcessamt','$cgstpp[$j]','$sgstpp[$j]','0','$cgstamtp[$j]','$sgstamtp[$j]','0', '$hnd[$j]', '$taxamt[$j]', '$gtot[$j]','0','0','$dt')";
                  
                  }
                  else
                  {
                  $sql="insert into jcardparts values('$jcno','$brcode','$partcode[$j]','$partname[$j]','$rate[$j]', '$qty[$j]', '$amt[$j]','$disc[$j]', '0', '0' , '0','0','0','$igstpp[$j]','0','0','$igstamtp[$j]', '$hnd[$j]', '$taxamt[$j]', '$gtot[$j]','0','0','$dt')";

                  }
                  mysqli_query($db,$sql) or die("Err 3".mysqli_error($db));
            }
            else
            {
                if($intr=='INTRASTATE')
                  {
                      if($cessamt[$j]>0)
                        $mcessamt = $cessamt[$j];
                    else
                        $mcessamt = 0;
                  $sql="insert into jcardparts values('$jcno','$brcode','$partcode[$j]','$partname[$j]','$rate[$j]', '$qty[$j]', '$amt[$j]','$disc[$j]', '0', '1' , '$mcessamt','$cgstpp[$j]','$sgstpp[$j]','0','$cgstamtp[$j]','$sgstamtp[$j]','0', '$hnd[$j]', '$taxamt[$j]', '$gtot[$j]','0','0','$dt')";
                  }
                  else
                  {
                  $sql="insert into jcardparts values('$jcno','$brcode','$partcode[$j]','$partname[$j]','$rate[$j]', '$qty[$j]', '$amt[$j]','$disc[$j]', '0', '0' , '0','0','0','$igstpp[$j]','0','0','$igstamtp[$j]', '$hnd[$j]', '$taxamt[$j]', '$gtot[$j]','0','0','$dt')";    
                  }
                  mysqli_query($db,$sql) or die("Err 4".mysqli_error($db));

            }
      }
//print $spareengineer[$j];
      print $engineer[$j];
   }
}

$cessamtl=$_REQUEST['cessamtl'];
$service =$_REQUEST['service'];
$i=0;
foreach($service as $a)
{
 $service[$i]=$a; 
 $i++;
}
$servamt =$_REQUEST['servamt'];
$i=0;
foreach($servamt as $a)
{
 $servamt[$i]=$a; 
 $i++;
}
$descr =$_REQUEST['descr'];
$i=0;
foreach($descr as $a)
{
 $descr[$i]=$a; 
 $i++;
}
$i=0;
foreach($cgstp as $a)
{
 $cgstp[$i]=$a; 
 $i++;
}
$i=0;
foreach($sgstp as $a)
{
 $sgstp[$i]=$a; 
 $i++;
}
$i=0;
foreach($igstp as $a)
{
 $igstp[$i]=$a; 
 $i++;
}
$i=0;
foreach($cgstamt as $a)
{
 $cgstamt[$i]=$a; 
 $i++;
}
$i=0;
foreach($sgstamt as $a)
{
 $sgstamt[$i]=$a; 
 $i++;
}
$i=0;
foreach($igstamt as $a)
{
 $igstamt[$i]=$a; 
 $i++;
}
$i=0;
foreach($cessamtl as $a)
{
    $cessamtl[$i]=$a; 
    $i++;
}
$spareengineer2 =$_REQUEST['spareengineer2'];
$i=0;
foreach($spareengineer2 as $a)
{
 $spareengineer2[$i]=$a; 
 $i++;
}
$engineer1 =$_REQUEST['engineer1'];
foreach($engineer1 as $a)
{
 $engineer1[$i]=$a; 
 $i++;
}

$tot =$_REQUEST['tot'];
$i=0;
foreach($tot as $a)
{
 $tot[$i]=$a; 
 $i++;
}
for($j=0;$j<$i;$j++)
{
    if($tot[$j]>0)
    {
        if($_REQUEST['updprn'])
        {
            if (strlen($spareengineer2[$j])!=0)
            { 
                if($intr=='INTRASTATE')
                {
                    if($cessamtl[$j]>0)
                        $mcessamtl = $cessamtl[$j];
                    else
                        $mcessamtl = 0;
                    $sql="insert into jcardserv (jcno, brcode, service,servamt, descr,cgstp,sgstp,igstp,cgstamt,sgstamt,igstamt,tot,jvno,invno,hredutx)   values('$jcno','$brcode','$service[$j]','$servamt[$j]','$descr[$j]','$cgstp[$j]','$sgstp[$j]','0','$cgstamt[$j]','$sgstamt[$j]','0','$tot[$j]','$jvno','$invno','$mcessamtl')";
                }
                else
                {
                    $sql="insert into jcardserv (jcno, brcode, service,servamt, descr,cgstp,sgstp,igstp,cgstamt,sgstamt,igstamt,tot,jvno,invno)   values('$jcno','$brcode','$service[$j]','$servamt[$j]','$descr[$j]','0','0','$igstp[$j]','0','0','$igstamt[$j]','$tot[$j]','$jvno','$invno')";  
                }
                mysqli_query($db,$sql) or die("Err 9".mysqli_error($db));
                   
            }
            else
            {
                if($intr=='INTRASTATE')
                {
                    if($cessamtl[$j]>0)
                        $mcessamtl = $cessamtl[$j];
                    else
                        $mcessamtl = 0;
                    $sql="insert into jcardserv (jcno, brcode, service,servamt, descr,cgstp,sgstp,igstp,cgstamt,sgstamt,igstamt,tot,jvno,invno,hredutx)   values('$jcno','$brcode','$service[$j]','$servamt[$j]','$descr[$j]','$cgstp[$j]','$sgstp[$j]','0','$cgstamt[$j]','$sgstamt[$j]','0','$tot[$j]','$jvno','$invno','$mcessamtl')";
                }
                else
                {
                    $sql="insert into jcardserv (jcno, brcode, service,servamt, descr,cgstp,sgstp,igstp,cgstamt,sgstamt,igstamt,tot,jvno,invno)   values('$jcno','$brcode','$service[$j]','$servamt[$j]','$descr[$j]','0','0','$igstp[$j]','0','0','$igstamt[$j]','$tot[$j]','$jvno','$invno')";  
                }
                mysqli_query($db,$sql) or die("Err 10".mysqli_error($db));
            }
        }
        else
        {
            if (strlen($spareengineer2[$j])!=0)
            {
                if($intr=='INTRASTATE')
                {
                    if($cessamtl[$j]>0)
                        $mcessamtl = $cessamtl[$j];
                    else
                        $mcessamtl = 0;
                    $sql="insert into jcardserv (jcno, brcode, service,servamt, descr,cgstp,sgstp,igstp,cgstamt,sgstamt,igstamt,tot,hredutx)   values('$jcno','$brcode','$service[$j]','$servamt[$j]','$descr[$j]','$cgstp[$j]','$sgstp[$j]','0','$cgstamt[$j]','$sgstamt[$j]','0','$tot[$j]','$mcessamtl')";
                }
                else
                {
                    $sql="insert into jcardserv (jcno, brcode, service,servamt, descr,cgstp,sgstp,igstp,cgstamt,sgstamt,igstamt,tot)   values('$jcno','$brcode','$service[$j]','$servamt[$j]','$descr[$j]','0','0','$igstp[$j]','0','0','$igstamt[$j]','$tot[$j]')";
                }
                mysqli_query($db,$sql) or die("Err 11".mysqli_error($db));
            }
            else
            {
                if($intr=='INTRASTATE')
                {
                    if($cessamtl[$j]>0)
                        $mcessamtl = $cessamtl[$j];
                    else
                        $mcessamtl = 0;
                    $sql="insert into jcardserv (jcno, brcode, service,servamt, descr,cgstp,sgstp,igstp,cgstamt,sgstamt,igstamt,tot,hredutx) values('$jcno','$brcode','$service[$j]','$servamt[$j]','$descr[$j]','$cgstp[$j]','$sgstp[$j]','0','$cgstamt[$j]','$sgstamt[$j]','0','$tot[$j]','$mcessamtl')";
               
                }
                else
                {
                    $sql="insert into jcardserv (jcno, brcode, service,servamt, descr,cgstp,sgstp,igstp,cgstamt,sgstamt,igstamt,tot) values('$jcno','$brcode','$service[$j]','$servamt[$j]','$descr[$j]','0','0','$igstp[$j]','0','0','$igstamt[$j]','$tot[$j]')"; 
                }
                mysqli_query($db,$sql) or die("Err 12".mysqli_error($db));
            }
        } 
          
    }
}

if($_REQUEST['updprn']) 
{
    $x=0;
    $minvno = '';
    $minvno = $invno;
    if($msno>'')
        $minvno = $invno."(".$msno.")";
    // 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;
    }
    else
    {
        if($intr=='INTRASTATE')
        {
            $taxdrres=mysqli_query($db,"select sum(amt),cgstp+sgstp from jcardparts  where jcno='$jcno' and brcode='$brcode' group by cgstp")or die('Led Post Err3'.mysqli_error($db));
            while($taxdrrow=mysqli_fetch_array($taxdrres))
            {
                $ledger='Spare invoice (JC) GST '.$taxdrrow[1].'%';
                $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxdrrow[0]','JC Spare Invno: $minvno','$brcode','Spare Invoice(JC)','J Card','$jcno','0')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }
            $cess_amt =0;
            $taxdrres=mysqli_query($db,"select sum(cessamt) from jcardparts  where jcno='$jcno' and brcode='$brcode' ")or die('Led Post Err3'.mysqli_error($db));
            while($taxdrrow=mysqli_fetch_array($taxdrres))
            {
                $cess_amt += $taxdrrow[0];
            }
            $taxdrres=mysqli_query($db,"select sum(hredutx) from jcardserv  where jcno='$jcno' and brcode='$brcode' ")or die('Led Post Err3'.mysqli_error($db));
            while($taxdrrow=mysqli_fetch_array($taxdrres))
            {
                $cess_amt += $taxdrrow[0];
            }
            if($cess_amt > 0)
            {
                $ledger='Kerala Flood Cess @1%';
                $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$cess_amt','JC Spare Invno: $minvno','$brcode','Spare Invoice(JC)','J Card','$jcno','0')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }
            $taxres=mysqli_query($db,"select cgstp,sgstp,sum(cgstamt),sum(sgstamt) from jcardparts where jcno='$jcno' and brcode='$brcode' group by cgstp")or die('Led Post Err3'.mysqli_error($db));
            while($taxrow=mysqli_fetch_array($taxres))
            {
                $ledger='Output CGST '.$taxrow[0].'%';
                $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[2]','JC Spare Invno: $minvno','$brcode','Spare Invoice(JC)','J Card','$jcno','0')";
                mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
                $ledger='Output SGST '.$taxrow[1].'%';
                $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[3]','JC Spare Invno: $minvno','$brcode','Spare Invoice(JC)','J Card','$jcno','0')";
                mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
            }
            $taxdrres=mysqli_query($db,"select sum(servamt),cgstp+sgstp from jcardserv  where jcno='$jcno' and brcode='$brcode' group by cgstp")or die('Led Post Err3'.mysqli_error($db));
            while($taxdrrow=mysqli_fetch_array($taxdrres))
            {
                if($brcode=='MEN888')
                {
                    if($servtype=='Rent')
                    {
                        $ledger='Rent Received';
                    }
                    if($servtype=='Manpower')
                    {
                        $ledger='Manpower Supply';
                    }
                }
                else
                {
                    $ledger=$servtype; 
                }
            
                $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxdrrow[0]','JC Service Invno: $minvno','$brcode','JC Service(JC)','J Card','$jcno','0')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }
            $taxres=mysqli_query($db,"select cgstp,sgstp,sum(cgstamt),sum(sgstamt) from jcardserv where jcno='$jcno' and brcode='$brcode' group by cgstp")or die('Led Post Err3'.mysqli_error($db));
            while($taxrow=mysqli_fetch_array($taxres))
            {
                $ledger='Output CGST '.$taxrow[0].'%';
                $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[2]','JC Service Invno: $minvno','$brcode','Service Invoice(JC)','J Card','$jcno','0')";
                mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
                $ledger='Output SGST '.$taxrow[1].'%';
                $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[3]','JC Service Invno: $minvno','$brcode','Service Invoice(JC)','J Card','$jcno','0')";
                mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
            }
               
        }    
        
        if($intr=='INTERSTATE')
        {
            
            $taxdrres=mysqli_query($db,"select sum(amt),igstp from jcardparts  where jcno='$jcno' and brcode='$brcode' group by igstp")or die('Led Post Err3'.mysqli_error($db));
            while($taxdrrow=mysqli_fetch_array($taxdrres))
            {
                $ledger='Spare invoice (JC) GST '.$taxdrrow[1].'%';
                $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxdrrow[0]','JC Spare Invno: $minvno','$brcode','Spare Invoice(JC)','J Card','$jcno','0')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }
            $taxres=mysqli_query($db,"select igstp,sum(igstamt) from jcardparts where jcno='$jcno' and brcode='$brcode' group by igstp")or die('Led Post Err3'.mysqli_error($db));
            while($taxrow=mysqli_fetch_array($taxres))
            {
                $ledger='Output IGST '.$taxrow[0].'%';
                $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[1]','JC Spare Invno: $minvno','$brcode','Spare Invoice(JC)','J Card','$jcno','0')";
                mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
            }
            $taxdrres=mysqli_query($db,"select sum(servamt),igstp from jcardserv  where jcno='$jcno' and brcode='$brcode' group by igstp")or die('Led Post Err3'.mysqli_error($db));
            while($taxdrrow=mysqli_fetch_array($taxdrres))
            {
            
                if($brcode=='MEN888')
                {
                    if($servtype=='Rent')
                    {
                        $ledger='Rent Received';
                    }
                    if($servtype=='Manpower')
                    {
                        $ledger='Manpower Supply';
                    }
                }
                else
                {
                    $ledger='Labour (JC) GST '.$taxdrrow[1].'%'; 
                }
            
            
                $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxdrrow[0]','JC Service Invno: $minvno','$brcode','JC Service(JC)','J Card','$jcno','0')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }
            $taxres=mysqli_query($db,"select igstp,sum(igstamt) from jcardserv where jcno='$jcno' and brcode='$brcode' group by igstp")or die('Led Post Err3'.mysqli_error($db));
            while($taxrow=mysqli_fetch_array($taxres))
            {
                $ledger='Output IGST '.$taxrow[0].'%';
                $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[1]','JC Service Invno: $minvno','$brcode','Service Invoice(JC)','J Card','$jcno','0')";
                mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
            }
               
        }   
   
        $jpres=mysqli_query($db,"select sum(gtot) from jcardparts where jcno='$jcno' and brcode='$brcode'")or die('jpres err'.mysqli_error($db));
        $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'";
        $spq="select sum(tot) from jcardserv where jcno='$jcno' and brcode='$brcode'";
        $jservres=mysqli_query($db,$spq)or die('jservres err'.mysqli_error($db));
        $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($db));
            $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($db));
            return;
        }
    
        $fr=mysqli_query($db,"select sum(hnd) from jcardparts where jcno='$jcno' and brcode='$brcode' group by invno")or die('cess err.'.mysqli_error($db));
        while($fr1=mysqli_fetch_array($fr))
        {
            $ledger='FRIEGHT CHARGES';
            $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$fr1[0]','Spare invno: $invno','$brcode','Spare Invoice','Sale Order','$saleordno','0')";
            mysqli_query($db,$sql) or die('Led Post Err7'.mysqli_error($db));
        }
        echo $sql="insert into journal2 values('$entrydate','$jvno','Dr','$cname','".round($gtot)."','0','JC Spare Invno: $minvno','$brcode','JC Spare Invoice','JB Crd','$jcno','0')";
        mysqli_query($db,$sql) or die('jv1 err:'.mysqli_error($db));
        $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','JC Spare Invno: $minvno','$brcode','JC Spare Invoice','jcno','$jcno','$invno')";
            mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
        }
        else if($dif>0)
        {
            $sql="insert into journal2 values('$entrydate','$jvno','Cr','Round Off','0','".abs($dif)."','JC Spare Invno: $minvno','$brcode','JC Spare Invoice','jcno','$jcno','$invno')";
            mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
        }
        $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','JC Spare Invno: $minvno','$brcode','CR','JC invoice','$jcno','$crno')";
            mysqli_query($db,$sql) or die(mysqli_error($db));
            $sql="insert into journal2 values('$entrydate','$jvno','Cr','$cname','0','$jpostrow[0]','JC Spare Invno: $minvno','$brcode','CR','JC invoice','$jcvno','$crno')";
            mysqli_query($db,$sql) or die(mysqli_error($db));
            $sql="insert into cshrec values('$entrydate','$crno','Cr','$cname','0','$jpostrow[0]','JC Spare Invno: $minvno','$brcode','$jvno','jcno','$jcno')";
            mysqli_query($db,$sql) or die(mysqli_error($db));
            print "<script> window.open('../accounts/payrec/prncshrec.php?no=$crno'); </script>";
        } // end if pay type
    }
    
    print "<script> window.open('prnjcardbill_sbsrv.php?jcno=$jcno'); </script>";
    print "<script>alert('Data has been updated'); </script>";
    print "<script>document.location='jcardrepo_sb.php'</script>";
}
?>
