<?
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");
$sql1=mysqli_query($db,"select distinct invno,invdate,invtype,saleordno from couponinvoice 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");
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 = "Eicher Truck And Buses (VE1000)";
       $entrydate = $row[1];
       $saleordno = $row[3];
				 if($row[2]=='Warranty' ||$row[2]=='AMC')
					 $narr='Warranty';
				 else
					 $narr='Coupon';
       $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','0000-00-00')";
          echo $sql."<br>";
          mysqli_query($db,$sql);
       }
       $taxres=mysqli_query($db,"select sum(tot),invtype from couponinvoice where invno='$invno' and brcode='$brcode'")or die('Led Post Err3'.mysqli_error($db));
			
       while($taxrow=mysqli_fetch_array($taxres))
       {
             if($taxrow[0]>0)
             {
			 	 
             $ledger=$customer;
             $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','$ledger','$taxrow[0]','0','$narr Spare Claimno: $invno','$brcode','$narr Spare Invoice','$narr','$invno','$invno','$row[3]')";
             mysqli_query($db,$sql) or die('Led Post Err0'.mysqli_error($db));
             }
       }
//*************** cess
       $taxcessres=mysqli_query($db,"select sum(taxamt),tax,invtype from couponinvoice where invno='$invno' and brcode='$brcode' group by tax")or die('cess err.'.mysqli_error($db));
       while($taxcessrow=mysqli_fetch_array($taxcessres))
       {
             if($taxcessrow[0]>0)
             {
                 
                    $ledger='SERVICE TAX '.$taxcessrow[tax];
                
             $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','$taxcessrow[0]','$narr Spare Claimno: $invno ','$brcode','$narr Spare Invoice','$narr','$invno','$invno','$row[3]')";
             mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
             }
       }
        $taxcessres1=mysqli_query($db,"select sum(aprice),invtype from couponinvoice where invno='$invno' and brcode='$brcode' group by invtype")or die('cess err.'.mysqli_error($db));
       while($taxcessrow1=mysqli_fetch_array($taxcessres1))
       {
             if($taxcessrow1[0]>0)
             {
                 
                    
                  $ledger='Fs Labour';
            
             $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','$taxcessrow1[0]','$narr Spare Claimno: $invno','$brcode','$narr Spare Invoice','$narr','$invno','$invno','$row[3]')";
             mysqli_query($db,$sql) or die('Led Post Err1'.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','$narr Spare Claimno: $invno','$brcode','$narr Spare Invoice','$narr','$invno','$invno','$row[3]')";
             mysqli_query($db,$sql) or die("J V Write error dif0:".mysqli_error($db));
       }
       else 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','Cr','Round Off','0','".abs($dif)."','$narr Spare Claimno: $invno','$brcode','$narr Spare Invoice','$narr','$invno','$invno','$row[3]')";
             mysqli_query($db,$sql) or die("J V Write error dif1:".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>";
       }
       mysqli_query($db,"update couponinvoice set jvno='$jvno',ac_postdt='$dt_tm' where invno='$invno' and brcode='$brcode'");
       
}
print "<script> alert('Posting Done Successfully...!!!'); </script>";
}
?>