<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='index.php'; </script>";
 return;
}
include("includes/top.php");
include("opendb.php");
$acpd=mysqli_query($db,"select * from acperiod") or die ('err'.mysqli_error($db));
     $acrow=mysqli_fetch_row($acpd);

      $acf= $acrow[0];
      $act= $acrow[1];
      
$sql1=mysqli_query($db,"select distinct invno,cname,date,msno,model,user,ptype from jobcard where ac_postdt IS NULL and brcode='$brcode' and date>='$acf' and date<='$act' order by invno")or die('Led Post Err3'.mysqli_error($db));

$dt_tm=date("Y-m-d");
if(mysqli_num_rows($sql1)<=0)
{
print "<script> alert('Posting Already Done...!!!'); </script>";
return;
}
else
{
while($row=mysqli_fetch_array($sql1))
{
       $invno = $row[0];
      $customer = ucwords($row[1]);
      $user = ucwords($row[5]);
      
       $entrydate = $row[2];
       $saleordno = $row[0];
       $re1=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
       $jvrow=mysqli_fetch_row($re1);
       $jvno = $jvrow[0]+1;
       
      /* $chre=mysqli_query($db,"select * from ledger where code='$row[3]' ");
       if(mysqli_num_rows($chre)<=0)
       {
          $code=$row[3];
          $code=str_replace(' ','-',$code);
          $sql="insert into ledger values('$customer','SUNDRY DEBTORS','0','Dr','$code','$brcode','2000-01-01')";
          echo $sql."<br>";
          mysqli_query($db,$sql);
       }*/
       
       //*****************
       
       

$res=mysqli_query($db,"select * from customer where code='$row[3]'") or die(mysqli_error($db));
 if(mysqli_num_rows($res)>0)
    {

$reacd=mysqli_query($db,"select * from ledger where code='$row[3]' and grp='Sundry Debtors' and brcode='$brcode' ") or die(mysqli_error($db));
  $rowacd=mysqli_fetch_array($reacd);

    if(mysqli_num_rows($reacd)<=0)
            {

 $reac=mysqli_query($db,"select * from ledger where code='$row[3]' and grp='Sundry Debtors' ") or die(mysqli_error($db));
 
      
       if(mysqli_num_rows($reac)>0)
             {
           $rowac=mysqli_fetch_array($reac); 
           $acname=$rowac['led'];
           
$sqlop="insert into ledger values('".addslashes(trim($acname))."','Sundry Debtors','0','Dr','$row[3]','$brcode','2017-01-01')";
mysqli_query($db,$sqlop) or die('<b>Ledger creation error:'.mysqli_error($db));
             }
             else
             {
                $acname=$customer;
            $rea=mysqli_query($db,"select * from ledger where led='$acname' and grp='Sundry Debtors' and brcode='$brcode' ") or die(mysqli_error($db));
  $r=mysqli_fetch_array($rea);
  if(mysqli_num_rows($rea)>0)
             {
                 
               $sqlop="update ledger set code='$row[3]' where led='$acname' and brcode='$brcode' and grp='Sundry Debtors'";
              mysqli_query($db,$sqlop) or die('<b>Ledger updation error:'.mysqli_error($db));
}
else
{
    $sqlop="insert into ledger values('".addslashes(trim($acname))."','Sundry Debtors','0','Dr','$row[3]','$brcode','2017-01-01')";
mysqli_query($db,$sqlop) or die('<b>Ledger creation error:'.mysqli_error($db)); 
    
}


             }
        }
        else
        {
            $acname = trim($rowacd['led'], '"');

            
        }
    }
    
  else
   {
       
$acname=$customer;

$sqlc="insert into customer values('$brcode','$row[3]', '2017-01-01','".addslashes(trim($customer))."','','".addslashes(trim($acname))."','', '','','','','','','','','','','', '','0000-00-00','','new','','','','0','0','0')";
//echo $sqlc;
mysqli_query($db,$sqlc)or die("customer err".mysqli_error($db));


$res11=mysqli_query($db,"select * from ledger where led='".addslashes(trim($acname))."' and brcode='$brcode' ") or die(mysqli_error($db));
 if(mysqli_num_rows($res11)<=0)
{
$sqlop="insert into ledger values('".addslashes(trim($acname))."','Sundry Debtors','0','Dr','$row[3]','$brcode','2017-01-01')";
mysqli_query($db,$sqlop) or die('<b>Ledger creation error:'.mysqli_error($db));
}

  }


//*************

// $acname = trim($customer, '"');
//print "ok";
  // return;
   

       $jpres=mysqli_query($db,"select sum(gtot) from jcardparts where invno='$invno' 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+cgstamt+sgstamt+igstamt+servtx) from jcardserv where invno='$invno' 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]);
       $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','".addslashes(trim($acname))."','".round($gtot)."','0','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno','$row[3]')";
       mysqli_query($db,$sql) or die('jv1 err:'.mysqli_error($db));
       //******** service
     /*  $taxres11=mysqli_query($db,"select servtx, sum(servamt) from jcardserv where invno='$invno' and brcode='$brcode' group by servtx")or die('Led Post Err3'.mysqli_error($db));
       while($taxrow1=mysqli_fetch_array($taxres11))
       {
              if($taxrow1[1]>0)
              {
                    //$ledger='Labour (JC)'.$taxrow1[servtx].'%';
                    $ledger='LABOUR CHARGES';
                    $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Cr','$ledger','0','$taxrow1[1]','JC Spare Invno: $invno $row[4]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno','$row[3]')";
                    mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
              }
       }
       */
       
     //******** service
       $taxres11=mysqli_query($db,"select servtx, sum(servamt),cgstp,sgstp from jcardserv where invno='$invno' and cgstp>'0' and brcode='$brcode' group by invno")or die('Led Post Err3'.mysqli_error($db));
       while($taxrow1=mysqli_fetch_array($taxres11))
       {
              if($taxrow1[2]>0 )
              {
                  $tot_tax = $taxrow1['cgstp']+$taxrow1['sgstp'];
                    //$ledger='Labour (JC)'.$tot_tax.'%';
                    $ledger='Outward Supply - Labour';
                    $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$taxrow1[1]','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno')";
                    mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
              }
       }
  
  //flood
  $taxres11f=mysqli_query($db,"select  sum(servtx) from jcardserv where invno='$invno' and brcode='$brcode' group by invno")or die('Led Post Err3'.mysqli_error($db));
       while($taxrow1f=mysqli_fetch_array($taxres11f))
       {
              if($taxrow1f[0]>0 )
              {
                  
                    $ledger='Flood Cess';
                    $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$taxrow1f[0]','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno')";
                   // mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
              }
       }
       
  //******** service IGST
       $igstaxres11=mysqli_query($db,"select servtx, sum(servamt),igstp from jcardserv where invno='$invno' and brcode='$brcode' and igstp>'0'    group by invno")or die('Led Post Err3'.mysqli_error($db));
       while($igstaxrow1=mysqli_fetch_array($igstaxres11))
       {
              if($igstaxrow1[2]>0 )
              {
                  $tot_tax = $igstaxrow1['igstp'];
                    //$ledger='Labour (JC)'.$tot_tax.'%';
                    $ledger='Outward Supply - Labour';
                    $igssql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$igstaxrow1[1]','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno')";
                    mysqli_query($db,$igssql) or die('Led Post Err2'.mysqli_error($db));
              }
       }
       
       
       
       //******** service 0 tax
     /*  $taxres110=mysqli_query($db,"select servtx, sum(servamt) from jcardserv where invno='$invno' and brcode='$brcode' and cgstp='0.00' and sgstp='0.00' and igstp='0.00' ")or die('Led Post Err3'.mysqli_error($db));
       while($taxrow10=mysqli_fetch_array($taxres110))
       {
              if($taxrow10[1]>0 )
              {
                  //$tot_tax = $taxrow1[cgstp]+$taxrow1[sgstp];
                    //$ledger='Labour (JC)'.$tot_tax.'%';
                    $ledger='Outward Supply - Labour';
                    $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$taxrow10[1]','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno')";
                    mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
              }
       }
  */
  
//******** service2
     /*  $taxres12=mysqli_query($db,"select servtx, sum((servamt*servtx)/100) from jcardserv where invno='$invno' and brcode='$brcode' group by servtx")or die('Led Post Err3'.mysqli_error($db));
       while($taxrow1=mysqli_fetch_array($taxres12))
       {      if($taxrow1[1]>0)
              {
              $ledger='SERVICE TAX 15.00';
              $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Cr','$ledger','0','$taxrow1[1]','JC Spare Invno: $invno $row[4]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno','$row[3]')";
              mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
              }
       }*/
//******** service3

$taxres12=mysqli_query($db,"select cgstp, sum(cgstamt),sgstp, sum(sgstamt) from jcardserv where invno='$invno' and brcode='$brcode' group by invno")or die('Led Post Err3'.mysqli_error($db));
       while($taxrow1=mysqli_fetch_array($taxres12))
       {      if($taxrow1[1]>0)
              {
              $ledger='Output Tax';
              $gsta=$taxrow1[1]+$taxrow1[3];
              
              $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$gsta','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno')";
              mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
             // $ledger='Output SGST Labour '.$taxrow1[sgstp];
             // $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$taxrow1[3]','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno')";
             // mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
              }
       }

      //******** service3 IGST
      
      $itaxres12=mysqli_query($db,"select igstp, sum(igstamt) from jcardserv where invno='$invno' and brcode='$brcode' group by invno")or die('Led Post Err3'.mysqli_error($db));
       while($itaxrow1=mysqli_fetch_array($itaxres12))
       {      if($itaxrow1[1]>0)
              {
              $ledger='Output Tax';
              $isql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$itaxrow1[1]','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno')";
              mysqli_query($db,$isql) or die('Led Post Err2'.mysqli_error($db));
               }
       }

      //***************
     /*  $sql="select sum(amt+(amt*hnd/100)),vat from jcardparts where invno='$invno' and brcode='$brcode' and itemtype='Spares' group by vat";
       $bfrres=mysqli_query($db,$sql) or die(mysqli_error($db));
       while($brrrow=mysqli_fetch_array($bfrres))
       {
              if($brrrow[0]>0)
              {
             $ledger='SPARES SALES '.$brrrow[vat];
             $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Cr','$ledger','0','$brrrow[0]','JC Spare Invno: $invno $row[4]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno','$row[3]')";
             mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
             }
       }
       //------------------------------
       $sql11="select sum(amt+(amt*hnd/100)),vat from jcardparts where invno='$invno' and brcode='$brcode' and itemtype!='Spares' group by vat";
       $bfrres1=mysqli_query($db,$sql11) or die(mysqli_error($db));
       while($brrrow1=mysqli_fetch_array($bfrres1))
       {
              if($brrrow1[0]>0)
              {
             $ledger='LUBRICANT SALES '.$brrrow1[vat];
             $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Cr','$ledger','0','$brrrow1[0]','JC Spare Invno: $invno $row[4]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno','$row[3]')";
             mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
             }
       }*/
       
   //parts cgst
   
       $sql="select sum(amt+(amt*hnd/100)),cgstp,sgstp from jcardparts where invno='$invno' and brcode='$brcode' and amt>'0'  group by invno";
       $bfrres=mysqli_query($db,$sql) or die(mysqli_error($db));
       while($brrrow=mysqli_fetch_array($bfrres))
       {
              if($brrrow[1]>0)
              {
                  $tot_tax = $brrrow[1]+$brrrow[2];
             $ledger='Outward Supply - Sales';
             $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$brrrow[0]','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Card','$invno','$invno')";
             //print $sql;
             mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
             }
       }
       //------------------------------
      /* $sql11="select sum(amt+(amt*hnd/100)),cgstp,sgstp from jcardparts where invno='$invno' and brcode='$brcode' and itemtype!='Spares' group by invno";
       $bfrres1=mysqli_query($db,$sql11) or die(mysqli_error($db));
       while($brrrow1=mysqli_fetch_array($bfrres1))
       {
              if($brrrow1[1]>0)
              {
                  $tot_tax = $brrrow1[1]+$brrrow1[2];
             $ledger='Outward Supply - Sales';
             $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$brrrow1[0]','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Card','$invno','$invno')";
             mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
             }
       }*/
 //parts IGST
 
       $isql="select sum(amt+(amt*hnd/100)),igstp from jcardparts where invno='$invno' and brcode='$brcode'  and amt>'0' group by invno";
       $ibfrres=mysqli_query($db,$isql) or die(mysqli_error($db));
       while($ibrrrow=mysqli_fetch_array($ibfrres))
       {
              if($ibrrrow[1]>0)
              {
                  $tot_tax = $ibrrrow[1];
             $ledger='Outward Supply - Sales';
             $isql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$ibrrrow[0]','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Card','$invno','$invno')";
             mysqli_query($db,$isql) or die('Led Post Err1'.mysqli_error($db));
             }
       }
       //------------------------------
      /* $igsql11="select sum(amt+(amt*hnd/100)),igstp from jcardparts where invno='$invno' and brcode='$brcode' and itemtype!='Spares' group by invno";
       $igbfrres1=mysqli_query($db,$igsql11) or die(mysqli_error($db));
       while($igbrrrow1=mysqli_fetch_array($igbfrres1))
       {
              if($igbrrrow1[1]>0)
              {
                  $tot_tax = $igbrrrow1[1];
             $ledger='Outward Supply - Sales';
             $igsql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$igbrrrow1[0]','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Card','$invno','$invno')";
             mysqli_query($db,$igsql) or die('Led Post Err1'.mysqli_error($db));
             }
       }
       */
       
       // spare 0 tax 
       
       
      /* $isql="select sum(amt+(amt*hnd/100)) from jcardparts where invno='$invno' and brcode='$brcode'  and cgstp='0.00' and sgstp='0.00' and igstp='0.00'";
       $ibfrres=mysqli_query($db,$isql) or die(mysqli_error($db));
       while($ibrrrow=mysqli_fetch_array($ibfrres))
       {
              if($ibrrrow[0]>0)
              {
                  //$tot_tax = $ibrrrow[1];
             $ledger='Outward Supply - Sales';
             $isql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$ibrrrow[0]','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Card','$invno','$invno')";
             mysqli_query($db,$isql) or die('Led Post Err1'.mysqli_error($db));
             }
       }*/
       
      /* $isql="select sum(amt+(amt*hnd/100)) from jcardparts where invno='$invno' and brcode='$brcode' and itemtype!='Spares' and cgstp='0.00' and sgstp='0.00' and igstp='0.00'";
       $ibfrres=mysqli_query($db,$isql) or die(mysqli_error($db));
       while($ibrrrow=mysqli_fetch_array($ibfrres))
       {
              if($ibrrrow[0]>0)
              {
                  //$tot_tax = $ibrrrow[1];
             $ledger='Lubricant Sales 0 %';
             $isql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$ibrrrow[0]','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Card','$invno','$invno')";
             mysqli_query($db,$isql) or die('Led Post Err1'.mysqli_error($db));
             }
       }*/
       
       //prts 0 tax
//************vat
      /* $taxres=mysqli_query($db,"select vat, sum(taxamt) from jcardparts where invno='$invno' and brcode='$brcode' group by vat")or die('Led Post Err3'.mysqli_error($db));
       while($taxrow=mysqli_fetch_array($taxres))
       {
             if($taxrow[1]>0)
             {
             $ledger='LOCAL SALES TAX RECEIVED '.$taxrow[vat];
             $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[1]','JC Spare Invno: $invno $row[4]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno','$row[3]')";
             mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
             }
       }
       */
 //parts Local      
        $taxres=mysqli_query($db,"select cgstp,sum(cgstamt),sgstp,sum(sgstamt) from jcardparts where invno='$invno' and brcode='$brcode' group by invno")or die('Led Post Err3'.mysqli_error($db));
       while($taxrow=mysqli_fetch_array($taxres))
       {
             if($taxrow[1]>0)
             {
             $ledger='Output Tax';
             $gsta1=$taxrow[1]+$taxrow[3];
             $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$gsta1','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno')";
             mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
             /* $ledger='Output CGST Sales '.$taxrow[0];
             $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[1]','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno')";
             mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));*/
             }
       }
     ///parts IGST
     
        $taxres=mysqli_query($db,"select igstp,sum(igstamt) from jcardparts where invno='$invno' and brcode='$brcode' group by invno")or die('Led Post Err3'.mysqli_error($db));
       while($taxrow=mysqli_fetch_array($taxres))
       {
             if($taxrow[1]>0)
             {
             $ledger='Output Tax';
             $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[1]','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno')";
             mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
              }
       }
       
//*************** cess
       $taxcessresf=mysqli_query($db,"select sum(cessamt) from jcardparts where invno='$invno' and brcode='$brcode' group by invno")or die('cess err.'.mysqli_error($db));
       while($taxcessrowf=mysqli_fetch_array($taxcessresf))
       {
             if($taxcessrowf[0]>0)
             {
             $ledger='Flood Cess';
             $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Cr','$ledger','0','$taxcessrowf[0]','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno','$row[3]')";
             //mysqli_query($db,$sql) or die('Led Post Err6f'.mysqli_error($db));
             }
       }
       
      //********************tcs assurant    
       $taxcessrestcs=mysqli_query($db,"select sum(tcsamt) from jcardparts where invno='$invno' and brcode='$brcode' group by invno")or die('cess err.'.mysqli_error($db));
       while($taxcessrowtcs=mysqli_fetch_array($taxcessrestcs))
       {
             if($taxcessrowtcs[0]>0)
             {
             $ledger='TCS PAYABLE';
             $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Cr','$ledger','0','$taxcessrowtcs[0]','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno','$row[3]')";
             mysqli_query($db,$sql) or die('Led Post Err6f'.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(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','Round Off','".abs($dif)."', '0','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno','$row[3]')";
             mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
       }
       else if($dif>0 && $dif<1)
       {
             $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Cr','Round Off','0','".abs($dif)."','JC Spare Invno: $invno $row[4] $row[6]','$brcode','JC Spare Invoice','Job Crd','$invno','$invno','$row[3]')";
             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)
       {
            //temp lock for stop msging// print "<sctipt> alert('Error in posting. Please check the data now');</script>";
       }
       mysqli_query($db,"update jobcard set jvno='$jvno',ac_postdt='$dt_tm',dcomm='$acname' where invno='$invno' and brcode='$brcode'");
       mysqli_query($db,"update jcardparts set jvno='$jvno' where invno='$invno' and brcode='$brcode'");
       mysqli_query($db,"update jcardserv set jvno='$jvno' where invno='$invno' and brcode='$brcode'");
}
print "<script> alert('Posting Done Successfully...!!!'); </script>";
}
?>