<?
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 GRNNo,InvoiceNo,GRNDate from eicherpurchase where ac_postdt IS NULL and brcode='$brcode' and GRNDate>='$acf' and GRNDate<='$act'  order by GRNNo")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))
{
       $grno = $row[0];
        $entrydate = $row[2];
	   $invno = $row[1];
       $re1=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
       $jvrow=mysqli_fetch_row($re1);
       $jvno = $jvrow[0]+1;
       
      
          //*****************
     
      $tinvamt=0;
           $taxdrres=mysqli_query($db,"select sum(BTAmt),Taxp,TaxType,sum(TaxAmt),sum(Hand),Disc,sum(Other),sum(InvAmt) from eicherpurchase where GRNNo='$grno' and brcode='$brcode' ")or die('Led Post Err3'.mysqli_error($db));
           while($taxdrrow=mysqli_fetch_array($taxdrres))
           {
               if($taxdrrow[0]>0)
               {
                     $tbtamt=  $taxdrrow[0]+$taxdrrow[4];
                     $tinvamt+=$taxdrrow[7];
                  if($taxdrrow[2]=='CGST')
               {
              $gstp=$taxdrrow[1];
              $gstamt=$taxdrrow[3];
              
              $ledger='Inward Supply - Purchase';
                  $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','$ledger','$tbtamt','0','sparepurchase No: $grno invno: $invno ','$brcode','eich','spare purchase','$grno','$grno','pur')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
             
                  $ledger1='INPUT TAX';
                  $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','$ledger1','$gstamt','0','sparepurchase No: $grno invno: $invno ','$brcode','eich','spare purchase','$grno','$grno','pur')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
              
             /* $ledger2='INPUT SGST PURCHASE '.$gstp;
                  $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','$ledger2','$gstamt','0','sparepurchase No: $grno invno: $invno ','$brcode','eich','spare purchase','$grno','$grno','pur')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
              */
               }
               else
               {
                   $igstp=$taxdrrow[1];
              //$igstamt=$tbtamt*$igstp/100;
             $igstamt=$taxdrrow[3];
                  $ledger='Inward Supply - Purchase';
          
                 $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','$ledger','$tbtamt','0','sparepurchase No: $grno invno: $invno ','$brcode','eich','spare purchase','$grno','$grno','pur')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
                  
                  $ledger2='INPUT TAX';
                  $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','$ledger2','$igstamt','0','sparepurchase No: $grno invno: $invno ','$brcode','eich','spare purchase','$grno','$grno','pur')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
          
          
               } 
                /* if($taxdrrow[4]>0)
               {
                 $ledger='Handling Charges';
                  $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','$ledger','$taxdrrow[4]','0','sparepurchase No: $grno invno: $invno ','$brcode','eich','spare purchase','$grno','$grno','pur')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
               }*/
                
                 if($taxdrrow[6]>0)
               {
             
               $ledger='OTHER CHARGES PAID';
                  $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','$ledger','$taxdrrow[6]','0','sparepurchase No: $grno invno: $invno ','$brcode','eich','spare purchase','$grno','$grno','pur')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
               
               }
                  
           
               }
           }
           
           if($tinvamt>0)
           {
   $sql22="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Cr','Eicher Truck And Buses (VE2000)','0','$tinvamt','sparepurchase No: $grno invno: $invno','$brcode','eich','spare purchase','$grno','$grno','pur')";
                  echo $sql22."<br>";
                  mysqli_query($db,$sql22) 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)
{
    $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: $grno','$brcode','eich','spare purchase','$grno','$grno','pur')";
    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 ofSINV No: $grno','$brcode','eich','spare purchase','$grno','$grno','pur')";
     echo $sql."<br>";
     mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
}
mysqli_query($db,"update eicherpurchase set jvno='$jvno',ac_postdt='$dt_tm' where GRNNo='$grno' and brcode='$brcode'");
       
$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>";
}
?>