<?
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());
     $acrow=mysqli_fetch_row($acpd);

      $acf= $acrow[0];
      $act= $acrow[1];
$sql1=mysqli_query($db,"select distinct invno,customer,entrydate,msno from salesretnewjc where ac_postdt IS NULL and brcode='$brcode' and entrydate>='$acf' and entrydate<='$act' order by invno")or die('Led Post Err3'.mysqli_error());

$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;
       
       /*$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());
 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());
  $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());
 
      
       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());
             }
             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());
             }
        }
        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());


$res11=mysqli_query($db,"select * from ledger where led='".addslashes(trim($acname))."' and brcode='$brcode' ") or die(mysqli_error());
 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());
}

  }


//*************


     // $acname = trim($acname, '"');

       
         
           
  
   // sales local  
           $taxdrres=mysqli_query($db,"select sum(aprice),cgstp,sgstp from salesretnewjc where invno='$invno' and itemtype='Spares' and brcode='$brcode' group by invno")or die('Led Post Err3'.mysqli_error());
           while($taxdrrow=mysqli_fetch_array($taxdrres))
           {
               if($taxdrrow[1]>0)
               {
                  $mtot_tax= $taxdrrow['cgstp']+$taxdrrow['sgstp'];
                  $amt2=abs($taxdrrow[0]);
                  $ledger='Outward Supply - Sales';
                  $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','$ledger','$amt2','0','jobcard sale ret: $invno','$brcode','jobcard sale ret','Sale Order','$saleordno','$saleordno')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
               }
           }
          $taxdrres=mysqli_query($db,"select sum(aprice),cgstp,sgstp from salesretnewjc where invno='$invno' and itemtype='Labour' and brcode='$brcode' group by invno")or die('Led Post Err3'.mysqli_error());
           while($taxdrrow=mysqli_fetch_array($taxdrres))
           {
               if($taxdrrow[1]>0)
               {
                  $mtot_tax= $taxdrrow['cgstp']+$taxdrrow['sgstp'];
                  $amt2=abs($taxdrrow[0]);
                  $ledger='Outward Supply - Labour';
                  $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','$ledger','$amt2','0','jobcard sale ret: $invno','$brcode','jobcard sale ret','Sale Order','$saleordno','$saleordno')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
               }
           }
           
           // sales igst
           
           
           $taxdrres=mysqli_query($db,"select sum(aprice),igstp from salesretnewjc where invno='$invno' and brcode='$brcode' and itemtype='Spares' group by invno")or die('Led Post Err3'.mysqli_error());
           while($taxdrrow=mysqli_fetch_array($taxdrres))
           {
               if($taxdrrow[1]>0)
               {
                  $mtot_tax= $taxdrrow['igstp'];
                  $amt2=abs($taxdrrow[0]);
                  $ledger='Outward Supply - Sales';
                  $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','$ledger','$amt2','0','jobcard sale ret: $invno','$brcode','jobcard sale ret','Sale Order','$saleordno','$saleordno')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
               }
           }
           $taxdrres=mysqli_query($db,"select sum(aprice),igstp from salesretnewjc where invno='$invno' and brcode='$brcode' and itemtype='Labour' group by invno")or die('Led Post Err3'.mysqli_error());
           while($taxdrrow=mysqli_fetch_array($taxdrres))
           {
               if($taxdrrow[1]>0)
               {
                  $mtot_tax= $taxdrrow['igstp'];
                  $amt2=abs($taxdrrow[0]);
                  $ledger='Outward Supply - Labour';
                  $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','$ledger','$amt2','0','jobcard sale ret: $invno','$brcode','jobcard sale ret','Sale Order','$saleordno','$saleordno')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
               }
           }
     // tax local
     
                    $taxres=mysqli_query($db,"select tax, sum(cgstamt),sum(sgstamt),cgstp,sgstp from salesretnewjc where invno='$invno' and brcode='$brcode' group by invno")or die('Led Post Err3'.mysqli_error());
           while($taxrow=mysqli_fetch_array($taxres))
           {
               if($taxrow[1]>0)
               {
                  $gsta=$taxrow[1]+$taxrow[2];
                  $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','$gsta','0','jobcard sale ret: $invno','$brcode','jobcard sale ret','Sale Order','$saleordno','$saleordno')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error());
                  /*$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','Dr','$ledger','$taxrow[2]','0','jobcard sale ret: $invno','$brcode','jobcard sale ret','Sale Order','$saleordno','$saleordno')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error());*/
               }
           }
           
           //tax IGST
           
           $taxres=mysqli_query($db,"select tax, sum(igstamt),igstp from salesretnewjc where invno='$invno' and brcode='$brcode' group by invno")or die('Led Post Err3'.mysqli_error());
           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','jobcard sale ret: $invno','$brcode','jobcard sale ret','Sale Order','$saleordno','$saleordno')";
                  echo $sql."<br>";
                  mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error());
                   }
           }
           
        $taxcessres=mysqli_query($db,"select sum(hand) from salesretnewjc where invno='$invno'  and brcode='$brcode' group by invno")or die('cess err.'.mysqli_error());
           while($taxcessrow=mysqli_fetch_array($taxcessres))
           {
               $hand=$taxcessrow[0]*-1;
               if($hand>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','Dr','$ledger','$hand','0','jobcard sale ret: $invno','$brcode','jobcard sale ret','Sale Order','$saleordno','$saleordno','$code')";
                  //echo $sql."<br>";
                  //mysqli_query($db,$sql) or die('Led Post Err6'.mysqli_error());
               }
           }
                 
           
           
            // flood cess
           
           $itaxdrresf=mysqli_query($db,"select sum(fcessa) from salesretnewjc where invno='$invno' and brcode='$brcode'  group by invno")or die('Led Post Err3'.mysqli_error());
           while($itaxdrrowf=mysqli_fetch_array($itaxdrresf))
           {
               if($itaxdrrowf[0]>0)
               {
                  
                  $ledger='Flood Cess';
                  //$isqlf="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$itaxdrrowf[0]','Spare Invoice No: $invno SAP INVNO: $sapinvno','$brcode','Spare Invoice','Sale Order','$saleordno','$saleordno')";
                  
                   $isqlf="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','$ledger','$itaxdrrowf[0]','0','jobcard sale ret: $invno','$brcode','jobcard sale ret','Sale Order','$saleordno','$saleordno','$code')";
                   
                  echo $isqlf."<br>";
                  mysqli_query($db,$isqlf) or die('Led Post Err1 fcess'.mysqli_error());
               }
           }
           
           
           //tcs
           
           $itaxdrresft=mysqli_query($db,"select sum(tcsa) from salesretnewjc where invno='$invno' and brcode='$brcode'  group by invno")or die('Led Post Err3'.mysqli_error());
           while($itaxdrrowft=mysqli_fetch_array($itaxdrresft))
           {
               if($itaxdrrowft[0]>0)
               {
                  
                  $ledger='TCS PAYABLE';
                  //$isqlf="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$itaxdrrowf[0]','Spare Invoice No: $invno SAP INVNO: $sapinvno','$brcode','Spare Invoice','Sale Order','$saleordno','$saleordno')";
                  
                   $isqlft="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','$ledger','$itaxdrrowft[0]','0','jobcard sale ret: $invno','$brcode','jobcard sale ret','Sale Order','$saleordno','$saleordno','$code')";
                   
                  echo $isqlft."<br>";
                  mysqli_query($db,$isqlft) or die('Led Post Err1 tcs'.mysqli_error());
               }
           }
           
           $sq="select sum(tot) from salesretnewjc where invno='$invno'  and brcode='$brcode'";
           $taxcrres=mysqli_query($db,$sq)or die('Led Post Err3'.mysqli_error());
           while($taxcrrow=mysqli_fetch_array($taxcrres))
           {
               $amt4=$taxcrrow[0]*-1;
               
               if($amt4>0)
               {
                   $gtot=$amt4;
                  $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Cr','$acname','0','".round($gtot)."','jobcard sale ret No: $invno','$brcode','jobcard sale ret','Sale Order','$saleordno','$saleordno','$code')";
                  print 'Rounding '.$sql;
                  mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error());
               }
           } 
           mysqli_query($db,"update salesretnewjc 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','$brcode','jobcard sale ret','S ORd no','$saleordno','$invno','$code')";
    echo $sql."<br>";
    mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error());
}
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','jobcard sale ret','S ORd no','$saleordno','$invno','$code')";
     echo $sql."<br>";
     mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error());
}

$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>";
}
?>