<?
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,customer,entrydate,msno,sapinvno from spareinvoice where ac_postdt IS NULL and brcode='$brcode' and entrydate>='$acf' and entrydate<='$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 = strtoupper($row[1]);
	   $entrydate = $row[2];
	   $saleordno = $row[0];
	   $sapinvno = $row[4];
       $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=$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;
               $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($user))."','','".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($acname, '"');

       
         /*  $taxdrres=mysqli_query($db,"select sum(aprice),tax from spareinvoice where invno='$invno' and itemtype='Spares' and brcode='$brcode' group by tax")or die('Led Post Err3'.mysqli_error($db));
           while($taxdrrow=mysqli_fetch_array($taxdrres))
           {
               if($taxdrrow[0]>0)
               {
                  $ledger='SPARES SALES '.$taxdrrow[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','$taxdrrow[0]','Spare Invoice No: $invno','$brcode','Spare Invoice','Sale Order','$saleordno','$saleordno','$code')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
               }
           }
           
           $taxdrres1=mysqli_query($db,"select sum(aprice),tax from spareinvoice where invno='$invno' and itemtype='Lubricant' and brcode='$brcode' group by tax")or die('Led Post Err3'.mysqli_error($db));
           while($taxdrrow1=mysqli_fetch_array($taxdrres1))
           {
               if($taxdrrow1[0]>0)
               {
                  $ledger='LUBRICANT SALES '.$taxdrrow1[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','$taxdrrow1[0]','Spare Invoice No: $invno','$brcode','Spare Invoice','Sale Order','$saleordno','$saleordno','$code')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
               }
           }
           
           */
         // sales local  
           $taxdrres=mysqli_query($db,"select sum(aprice),cgstp,sgstp from spareinvoice where invno='$invno' and brcode='$brcode' and itemtype !='Lubricant' group by cgstp")or die('Led Post Err3'.mysqli_error($db));
           while($taxdrrow=mysqli_fetch_array($taxdrres))
           {
               if($taxdrrow[1]>0)
               {
                  $mtot_tax= $taxdrrow[cgstp]+$taxdrrow[sgstp];
                  $ledger='Spare Sales Local '.$mtot_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','$taxdrrow[0]','Spare Invoice No: $invno SAP INVNO: $sapinvno','$brcode','Spare Invoice','Sale Order','$saleordno','$saleordno')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
               }
           }
           $taxdrres1=mysqli_query($db,"select sum(aprice),cgstp,sgstp from spareinvoice where invno='$invno' and brcode='$brcode' and itemtype ='Lubricant' group by cgstp")or die('Led Post Err3'.mysqli_error($db));
           while($taxdrrow1=mysqli_fetch_array($taxdrres1))
           {
               if($taxdrrow1[1]>0)
               {
                  $mtot_tax= $taxdrrow1[cgstp]+$taxdrrow1[sgstp];
                  $ledger='Lubricant Sales Local '.$mtot_tax;
                  $sql9="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$taxdrrow1[0]','Spare Invoice No: $invno SAP INVNO: $sapinvno','$brcode','Spare Invoice','Sale Order','$saleordno','$saleordno')";
                  echo $sql9."<br>";
                  mysqli_query($db,$sql9) or die('Led Post Err1'.mysqli_error($db));
               }
           }
           
           //0 tax
           
            $taxdrres00=mysqli_query($db,"select sum(aprice) from spareinvoice where invno='$invno' and brcode='$brcode' and itemtype !='Lubricant' and cgstp='0.00' and sgstp='0.00' and igstp='0.00'")or die('Led Post Err3'.mysqli_error($db));
           while($taxdrrow00=mysqli_fetch_array($taxdrres00))
           {
               if($taxdrrow00[0]>0)
               {
                  
                  $ledger='Spare Sales 0 %';
                  $sql00="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$taxdrrow00[0]','Spare Invoice No: $invno SAP INVNO: $sapinvno','$brcode','Spare Invoice','Sale Order','$saleordno','$saleordno')";
                  echo $sql00."<br>";
                  mysqli_query($db,$sql00) or die('Led Post Err1'.mysqli_error($db));
               }
           }
           $taxdrres100=mysqli_query($db,"select sum(aprice) from spareinvoice where invno='$invno' and brcode='$brcode' and itemtype ='Lubricant' and cgstp='0.00' and sgstp='0.00' and igstp='0.00'")or die('Led Post Err3'.mysqli_error($db));
           while($taxdrrow100=mysqli_fetch_array($taxdrres100))
           {
               if($taxdrrow100[0]>0)
               {
                 
                  $ledger='Lubricant Sales 0%';
                  $sql900="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$taxdrrow100[0]','Spare Invoice No: $invno SAP INVNO: $sapinvno','$brcode','Spare Invoice','Sale Order','$saleordno','$saleordno')";
                  echo $sql900."<br>";
                  mysqli_query($db,$sql900) or die('Led Post Err1'.mysqli_error($db));
               }
           }
           
           
           //0 tax  end 
           
           // sales igst
           
           
           $itaxdrres=mysqli_query($db,"select sum(aprice),igstp from spareinvoice where invno='$invno' and brcode='$brcode' and itemtype !='Lubricant' group by igstp")or die('Led Post Err3'.mysqli_error($db));
           while($itaxdrrow=mysqli_fetch_array($itaxdrres))
           {
               if($itaxdrrow[1]>0)
               {
                  $mtot_tax= $itaxdrrow[igstp];
                  $ledger='Spare Sales Inter state '.$mtot_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','$itaxdrrow[0]','Spare Invoice No: $invno SAP INVNO: $sapinvno','$brcode','Spare Invoice','Sale Order','$saleordno','$saleordno')";
                  echo $isql."<br>";
                  mysqli_query($db,$isql) or die('Led Post Err1'.mysqli_error($db));
               }
           }
           $iitaxdrres=mysqli_query($db,"select sum(aprice),igstp from spareinvoice where invno='$invno' and brcode='$brcode' and itemtype ='Lubricant' group by igstp")or die('Led Post Err3'.mysqli_error($db));
           while($iitaxdrrow=mysqli_fetch_array($iitaxdrres))
           {
               if($iitaxdrrow[1]>0)
               {
                  $mtot_tax= $iitaxdrrow[igstp];
                  $ledger='Lubricant Sales Inter state '.$mtot_tax;
                  $iisql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$iitaxdrrow[0]','Spare Invoice No: $invno SAP INVNO: $sapinvno','$brcode','Spare Invoice','Sale Order','$saleordno','$saleordno')";
                  echo $iisql."<br>";
                  mysqli_query($db,$iisql) or die('Led Post Err1'.mysqli_error($db));
               }
           }
     // tax local
     
           /*$taxres=mysqli_query($db,"select tax, sum(taxamt) from spareinvoice where invno='$invno' and brcode='$brcode' group by tax")or die('Led Post Err3'.mysqli_error($db));
           while($taxrow=mysqli_fetch_array($taxres))
           {
               if($taxrow[1]>0)
               {
                  $ledger='LOCAL SALES TAX RECEIVED '.$taxrow[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','$taxrow[1]','Spare Invoice No: $invno','$brcode','Spare Invoice','Sale Order','$saleordno','$saleordno','$code')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
               }
           }*/
           
           $taxres=mysqli_query($db,"select tax, sum(cgstamt),sum(sgstamt),cgstp,sgstp from spareinvoice where invno='$invno' and brcode='$brcode' group by cgstp")or die('Led Post Err3'.mysqli_error($db));
           while($taxrow=mysqli_fetch_array($taxres))
           {
               if($taxrow[1]>0)
               {
                  
                  $ledger='Output CGST Sales '.$taxrow[cgstp];
                  $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]','Spare Invoice No: $invno SAP INVNO: $sapinvno','$brcode','Spare Invoice','Sale Order','$saleordno','$saleordno')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
                  $ledger='Output SGST Sales '.$taxrow[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','$taxrow[2]','Spare Invoice No: $invno SAP INVNO: $sapinvno','$brcode','Spare Invoice','Sale Order','$saleordno','$saleordno')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
               }
           }
           
           //tax IGST
           
           $taxres=mysqli_query($db,"select tax, sum(igstamt),igstp from spareinvoice where invno='$invno' and brcode='$brcode' group by igstp")or die('Led Post Err3'.mysqli_error($db));
           while($taxrow=mysqli_fetch_array($taxres))
           {
               if($taxrow[1]>0)
               {
                  
                  $ledger='Output IGST Sales '.$taxrow[igstp];
                  $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]','Spare Invoice No: $invno SAP INVNO: $sapinvno','$brcode','Spare Invoice','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 spareinvoice 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,prftcntr) values('$entrydate','$jvno','Cr','$ledger','0','$taxcessrow[0]','Spare Invoice No: $invno SAP INVNO: $sapinvno','$brcode','Spare Invoice','Sale Order','$saleordno','$saleordno','$code')";
                 // echo $sql."<br>";
                  //mysqli_query($db,$sql) or die('Led Post Err6'.mysqli_error($db));
               }
           }
           $sq="select sum(tot) from spareinvoice 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];
                  $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','$acname','".round($gtot)."','0','Spare Invoice No: $invno SAP INVNO: $sapinvno','$brcode','Spare Invoice','Sale Order','$saleordno','$saleordno','$code')";
                  print 'Rounding '.$sql;
                  mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));
               }
           } 
           mysqli_query($db,"update spareinvoice 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,prftcntr) values('$entrydate','$jvno','Dr','Round Off','".$xx."', '0','Round ofSINV No: $invno SAP INVNO: $sapinvno','$brcode','Spare Invoice','S ORd no','$saleordno','$invno','$code')";
    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,prftcntr) values('$entrydate','$jvno','Cr','Round Off','0','".$xx."','Round of adjusted SINV No: $invno SAP INVNO: $sapinvno','$brcode','Spare Invoice','S ORd no','$saleordno','$invno','$code')";
     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>";
}
?>