<?
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,SupplyingPlantName,GRNDate,DealerInvoiceNumber,SupplyingPlantCode from transferin 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))
{
       $invno = $row[0];
       $supbr = $row[4];
       $supbr=trim($supbr);
	   $entrydate = $row[2];
	   $saleordno = $row[3];
       $re1=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
       $jvrow=mysqli_fetch_row($re1);
       $jvno = $jvrow[0]+1;
       
      $supbr=mysqli_query($db,"select brled from branch where brcode='$supbr'");
       $supbr1=mysqli_fetch_row($supbr);
       $supbrled = $supbr1[0];
       $supbrled=trim($supbrled);
          //*****************
     
      
           $taxdrres=mysqli_query($db,"select sum(InvoiceValue),igstp,sum(igstamt) from transferin where GRNNo='$invno' and brcode='$brcode' group by GRNNo ")or die('Led Post Err3'.mysqli_error($db));
           while($taxdrrow=mysqli_fetch_array($taxdrres))
           {
               if($taxdrrow[0]>0)
               {
                   $totval=$taxdrrow[0]+$taxdrrow[2];
                  //$ledger=$row[1];
                  $ledger=$supbrled;
                  $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','$totval','Stock Transfer In No: $invno chno: $saleordno ','$brcode','GRN','Transfer in','$saleordno','$saleordno','grn')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
                  
                  if($taxdrrow[1]>0)
               {
                   $ledger1='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','$ledger1','$taxdrrow[0]','0','Stock Transfer In No: $invno chno: $saleordno ','$brcode','GRN','Transfer in','$saleordno','$saleordno','grn')";
                  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','$taxdrrow[2]','0','Stock Transfer In No: $invno chno: $saleordno ','$brcode','GRN','Transfer in','$saleordno','$saleordno','grn')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
              
               }
               
           else
           {
                  $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','Inward Supply - Purchase','$taxdrrow[0]','0','Stock Transfer In No: $invno chno: $saleordno ','$brcode','GRN','Transfer in','$saleordno','$saleordno','grn')";
                  echo $sql."<br>";
                  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)
{
    $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','$brcode','GRN','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','$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));
}
mysqli_query($db,"update transferin set jvno='$jvno',ac_postdt='$dt_tm' where grnno='$invno' 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>";
}
?>