<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='inde.php'; </script>";
 return;
}
include("includes/top.php");
include("opendb.php");
$sql1=mysqli_query($db,"select distinct invno,customer,entrydate from spareinvoice where ac_postdt IS NULL and brcode='$brcode' order by invno")or die('Led Post Err3'.mysqli_error($db));

$dt_tm=date("Y-m-d");

while($row=mysqli_fetch_array($sql1))
{
       $invno = $row[0];
       $customer = $row[1];
       $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 led='$customer' 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('$customer','Sundry Debtors','0','Dr','$code','$brcode')";
          echo $sql."<br>";
          mysqli_query($db,$sql);
       }
       $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+((servamt*servtx/100)+(servamt*edutx/100)+(servamt*hredutx/100))) 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 values('$entrydate','$jvno','Dr','$cname','".round($gtot)."','0','JC Spare Invno: $invno','$brcode','JC Spare Invoice','JB Crd','$invno','$invno')";
       mysqli_query($db,$sql) or die('jv1 err:'.mysqli_error($db));
       //******** 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($db));
       while($taxrow1=mysqli_fetch_array($taxres11))
       {
              if($taxrow1[1]>0)
              {
                    $ledger='Labour (JC)'.$taxrow1[servtx].'%';
                    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow1[1]','JC Spare Invno: $invno','$brcode','JC Spare Invoice','Job Crd','$invno','0')";
                    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 (JC)'.$taxrow1[servtx].'%';
              $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow1[1]','JC Spare Invno: $invno','$brcode','JC Spare Invoice','Job Crd','$invno','0')";
              mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
              }
       }
//******** service3
       $taxres13=mysqli_query($db,"select edutx, sum((servamt*edutx)/100) from jcardserv where invno='$invno' and brcode='$brcode' group by edutx")or die('Led Post Err3'.mysqli_error($db));
       while($taxrow1=mysqli_fetch_array($taxres13))
       {
              if($taxrow1[1]>0)
              {
              $ledger='SB Cess (JC)'.$taxrow1[edutx].'%';
              $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow1[1]','JC Spare Invno: $invno','$brcode','JC Spare Invoice','Job Crd','$invno','0')";
              mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
              }
       }
//******** service3 HR
       $taxres14=mysqli_query($db,"select hredutx, sum((servamt*hredutx)/100) from jcardserv where invno='$invno' and brcode='$brcode' group by hredutx")or die('Led Post Err3'.mysqli_error($db));
       while($taxrow1=mysqli_fetch_array($taxres14))
       {
             if($taxrow1[1]>0)
             {
             $ledger='K K Cess @ 0.50%';
             $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow1[1]','JC Spare Invno: $invno','$brcode','JC Spare Invoice','Job Crd','$invno','0')";
             mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
             }
       }
//***************
       $sql="select sum(amt+(amt*hnd/100)),vat from jcardparts where invno='$invno' and brcode='$brcode' group by vat";
       $bfrres=mysqli_query($db,$sql) or die(mysqli_error($db));
       while($brrrow=mysqli_fetch_array($bfrres))
       {
              if($brrrow[0]>0)
              {
             $ledger='Spare invoice (JC)'.$brrrow[vat].'%';
             $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$brrrow[0]','JC Spare Invno: $invno','$brcode','Spare Invoice(JC)','J Card','$invno','0')";
             mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
             }
       }
//************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='Output VAT(JC)'.$taxrow[vat].'%';
             $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[1]','JC Spare Invno: $invno','$brcode','JC Spare Invoice','Job Crd','$invno','0')";
             mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
             }
       }
//*************** 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($db));
       while($taxcessrow=mysqli_fetch_array($taxcessres))
       {
             if($taxcessrow[0]>0)
             {
             $ledger='Output Cess (JC)'.$taxcessrow[1].'%';
             $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxcessrow[0]','JC Spare Invno: $minvno','$brcode','JC Spare Invoice','J Card','$jcno','0')";
             mysqli_query($db,$sql) or die('Led Post Err6'.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: $invno','$brcode','JC Spare Invoice','jcno','$invno','$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: $invno','$brcode','JC Spare Invoice','jcno','$invno','$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>";
       }
}
?>