<?
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,invdate,claimno from warcouponinvoicepassey where ac_postdt IS NULL and brcode='$brcode' and invdate>='$acf' and invdate<='$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 = "Eicher Truck And Buses (VE1000)";
       $entrydate = $row[1];
      $narr='Wty coupon';
     $saleordno = $row[2];
       $re1=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
       $jvrow=mysqli_fetch_row($re1);
       $jvno = $jvrow[0]+1;
       
       /* $recno=mysqli_query($db,"select saleordno from warspareinvoice where brcode='$brcode' and claimno='$row[2]' ");
       $rowcno=mysqli_fetch_row($recno);
       $brcno = $rowcno[0];
   
   
       $taxres=mysqli_query($db,"select sum(tot) from warcouponinvoicepassey where invno='$invno' and brcode='$brcode' and itemtype='998714' ")or die('Led Post Err3'.mysqli_error($db));
			
       while($taxrow=mysqli_fetch_array($taxres))
       {
             if($taxrow[0]>0)
             {
			 	 
             $ledger='VECV Free Service Claim pending Labour';
             $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 Claimno: $saleordno Invno: $row[0] Refno: $brcno','$brcode','$narr Invoice P','$narr','$invno','$invno','$row[2]')";
             mysqli_query($db,$sql) or die('Led Post Err0'.mysqli_error($db));
             }
       }
       */
       $taxres1=mysqli_query($db,"select sum(tot),sum(tdsamt) from warcouponinvoicepassey where invno='$invno' and brcode='$brcode' ")or die('Led Post Err3'.mysqli_error($db));
			
       while($taxrow1=mysqli_fetch_array($taxres1))
       {
             if($taxrow1[0]>0)
             {
			 	 $wtyamt=$taxrow1[0]-$taxrow1[1];
             $ledger='Eicher Truck And Buses (VE1000)';
             $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','$ledger','$wtyamt','0','$narr Claimno: $saleordno Invno: $row[0] Refno: $brcno','$brcode','$narr Invoice P','$narr','$invno','$invno','$row[2]')";
             mysqli_query($db,$sql) or die('Led Post Err0'.mysqli_error($db));
             }
             if($taxrow1[1]>0)
             {
			 	 
             $ledgertds='TDS RECEIVABLE WTY';
             $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','$ledgertds','$taxrow1[1]','0','$narr Claimno: $saleordno Invno: $row[0] Refno: $brcno','$brcode','$narr Invoice P','$narr','$invno','$invno','$row[2]')";
             mysqli_query($db,$sql) or die('Led Post Errtds'.mysqli_error($db));
             }
             
       }
       
//*************** service
       $taxcessres=mysqli_query($db,"select sum(gstamt),gstp from warcouponinvoicepassey where invno='$invno' and brcode='$brcode' group by invno")or die('cess err.'.mysqli_error($db));
       while($taxcessrow=mysqli_fetch_array($taxcessres))
       {
             if($taxcessrow[0]>0)
             {
                   $ledger='Output 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 Claimno: $saleordno Invno:$row[0] Refno: $brcno','$brcode','$narr Invoice P','$narr','$invno','$invno','$row[2]')";
             mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
             }
       }
       
      /* $taxcessres1=mysqli_query($db,"select sum(gstamt),gstp from warcouponinvoicepassey where invno='$invno' and brcode='$brcode' and itemtype!='998714'  group by gstp")or die('cess err.'.mysqli_error($db));
       while($taxcessrow1=mysqli_fetch_array($taxcessres1))
       {
             if($taxcessrow1[0]>0)
             {
                    $ledger='Output IGST Sales '.$taxcessrow1[gstp];
                
             
             $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 Claimno: $saleordno Invno: $row[0] Refno: $brcno','$brcode','$narr Invoice P','$narr','$invno','$invno','$row[2]')";
             mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
             }
       }*/
       
       
        $taxcessres2=mysqli_query($db,"select sum(aprice),gstp,invtype from warcouponinvoicepassey where invno='$invno' and brcode='$brcode'  and itemtype='998714'  group by invno")or die('cess err.'.mysqli_error($db));
       while($taxcessrow2=mysqli_fetch_array($taxcessres2))
       {
             if($taxcessrow2[0]>0)
             {
                 
                     $ledger='Outward Supply - Warranty 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','$taxcessrow2[0]','$narr Claimno: $saleordno Invno: $row[0] Refno: $brcno','$brcode','$narr Invoice P','$narr','$invno','$invno','$row[2]')";
             mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
             }
       }
       
       
       $taxcessres3=mysqli_query($db,"select sum(aprice),gstp,invtype from warcouponinvoicepassey where invno='$invno' and brcode='$brcode' and itemtype!='998714'  group by invno")or die('cess err.'.mysqli_error($db));
       while($taxcessrow3=mysqli_fetch_array($taxcessres3))
       {
             if($taxcessrow3[0]>0)
             {
                 
                     $ledger='Outward Supply - Warranty Sales';   
                    
                    
                
             $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','$taxcessrow3[0]','$narr Claimno: $saleordno Invno:$row[0] Refno: $brcno','$brcode','$narr Invoice P','$narr','$invno','$invno','$row[2]')";
             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 Claimno: $saleordno Invno:$row[0] Refno: $brcno','$brcode','$narr Invoice P','$narr','$invno','$invno','$row[2]')";
             mysqli_query($db,$sql) or die("J V Write error dif0:".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)."','$narr Claimno: $saleordno Invno:$row[0] Refno: $brcno','$brcode','$narr Invoice P','$narr','$invno','$invno','$row[2]')";
             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 $jvno;
             print "<sctipt> alert('Error in posting. Please check the data now');</script>";
       }
       mysqli_query($db,"update warcouponinvoicepassey set jvno='$jvno',ac_postdt='$dt_tm' where invno='$invno' and brcode='$brcode'");
       
}
print "<script> alert('Posting Done Successfully...!!!'); </script>";
}
?>