<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed In'); 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,customer,entrydate from sparepurreteichcancel where ac_postdt IS NULL and brcode='$brcode' and entrydate>='$acf' and entrydate<='$act' and customer  like 'VE Comm%'  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 = strtoupper($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;
       
        
           
           // Purchase igst
           
           
           $itaxdrres=mysqli_query($db,"select sum(aprice),cgstp from sparepurreteichcancel where invno='$invno' and brcode='$brcode' group by invno")or die('Led Post Err3'.mysqli_error($db));
           while($itaxdrrow=mysqli_fetch_array($itaxdrres))
           {
               if($itaxdrrow[1]>0)
               {
                  $mtot_tax= $itaxdrrow['tax'];
                  $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','Dr','$ledger','$itaxdrrow[0]','0','Spare Invoice No: $invno','$brcode','Spare Purret Eich','Sale Order','$saleordno','$saleordno')";
                  echo $isql."<br>";
                  mysqli_query($db,$isql) or die('Led Post Err1'.mysqli_error($db));
               }
           }
           
     
          $itaxdrres=mysqli_query($db,"select sum(aprice),igstp from sparepurreteichcancel where invno='$invno' and brcode='$brcode' group by invno")or die('Led Post Err3'.mysqli_error($db));
           while($itaxdrrow=mysqli_fetch_array($itaxdrres))
           {
               if($itaxdrrow[1]>0)
               {
                  $mtot_tax= $itaxdrrow['tax'];
                  $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','Dr','$ledger','$itaxdrrow[0]','0','Spare Invoice No: $invno','$brcode','Spare Purret Eich','Sale Order','$saleordno','$saleordno')";
                  echo $isql."<br>";
                  mysqli_query($db,$isql) or die('Led Post Err1'.mysqli_error($db));
               }
           }
           
           //tax IGST
           
           $taxres=mysqli_query($db,"select cgstp,sum(cgstamt+sgstamt) from sparepurreteichcancel 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','Dr','$ledger','$taxrow[1]','0','Spare Invoice No: $invno','$brcode','Spare Purret Eich','Sale Order','$saleordno','$saleordno')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
                   }
           }
           
           
           
            $taxres=mysqli_query($db,"select igstp,sum(igstamt) from sparepurreteichcancel 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','Dr','$ledger','$taxrow[1]','0','Spare Invoice No: $invno','$brcode','Spare Purret Eich','Sale Order','$saleordno','$saleordno')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
                   }
           }
           
           
          $taxcessres=mysqli_query($db,"select sum(hand) from sparepurreteichcancel 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='OTHER CHARGES RECEIVED';
                  $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$taxcessrow[0]','Spare Invoice No: $invno','$brcode','Spare Purret Eich','Sale Order','$saleordno','$saleordno')";
                 // echo $sql."<br>";
                  //mysqli_query($db,$sql) or die('Led Post Err6'.mysqli_error($db));
               }
           }
           $sq="select sum(tot),sum(hand) from sparepurreteichcancel where invno='$invno'  and brcode='$brcode'";
           $taxcrres=mysqli_query($db,$sq)or die('Led Post Err3'.mysqli_error($db));
           while($taxcrrow=mysqli_fetch_array($taxcrres))
           {
               if($taxcrrow[0]>0)
               {
                   $gtot=$taxcrrow[0]+$taxcrrow[1];
                  $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','Eicher Truck And Buses (VE2000)','0','".round($gtot)."','Spare Invoice No: $invno','$brcode','Spare Purret Eich','Sale Order','$saleordno','$saleordno')";
                  print 'Rounding '.$sql;
                  mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));
               }
           } 
           mysqli_query($db,"update sparepurreteichcancel set ac_postdt='$dt_tm',jvno='$jvno',ref='$acname' where invno='$invno' and brcode='$brcode'");


$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)
{
    $xx=abs($dif);
    $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','Round Off','".$xx."', '0','Round ofSINV No: $invno','$brcode','Spare Purret Eich','S ORd no','$saleordno','$invno')";
    echo $sql."<br>";
    mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
}
else if($dif>0 && $dif<1)
{
     $xx=abs($dif);
     $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','Round Off','0','".$xx."','Round of adjusted SINV No: $invno','$brcode','Spare Purret Eich','S ORd no','$saleordno','$invno')";
     echo $sql."<br>";
     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>";
}
}
print "<script> alert('Posting Done Successfully...!!!'); </script>";
}

?>