<?
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 DealerInvoiceNumber,ReceiverPlantName,InvoiceDate,GRNNo,ReceiverPlant from transferout where ac_postdt IS NULL and brcode='$brcode' and InvoiceDate>='$acf' and InvoiceDate<='$act' order by DealerInvoiceNumber")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];
       $recbr = $row[4];
       $recbr=trim($recbr);
	   $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;
       
       $rebr=mysqli_query($db,"select brled from branch where brcode='$recbr'");
       $rebr1=mysqli_fetch_row($rebr);
       $recbrled = $rebr1[0];
       $recbrled=trim($recbrled);
      
          //*****************
     
      
           $taxdrres=mysqli_query($db,"select InvoiceValue,igstp,igstamt from transferout where DealerInvoiceNumber='$invno' and brcode='$brcode' group by igstp ")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=$recbrled;
                  $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','$ledger','$totval','0','Stock Transfer Out No: $invno grnno: $saleordno','$brcode','CH','Transfer Out','$saleordno','$saleordno','ch')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
                  
                  if($taxdrrow[1]>0)
               {
                   $ledger1='STOCK TRANSFER OUT IGST '.$taxdrrow[1];
                 $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Cr','$ledger1','0','$taxdrrow[0]','Stock Transfer Out No: $invno grnno: $saleordno','$brcode','CH','Transfer Out','$saleordno','$saleordno','ch')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
                  $ledger2='OUTPUT IGST STOCK TRANSFER OUT '.$taxdrrow[1];
                   $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Cr','$ledger2','0','$taxdrrow[2]','Stock Transfer Out No: $invno grnno: $saleordno','$brcode','CH','Transfer Out','$saleordno','$saleordno','ch')";
                  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','Cr','STOCK TRANSFER OUT','0','$taxdrrow[0]','Stock Transfer Out No: $invno grnno: $saleordno','$brcode','CH','Transfer Out','$saleordno','$saleordno','ch')";
                  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','CH','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)
{
     $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','CH','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 transferout set jvno='$jvno',ac_postdt='$dt_tm' where DealerInvoiceNumber='$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>";
}
?>