<?
include_once('includes/top.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
include_once('opendb.php');
if(isset($_POST[upd]))
{
 
$resc=mysqli_query($db,"select distinct sapcode from supsap where brcode='$brcode' ")or die(mysqli_error($db));
$i=1;

$sqlc2="delete from ledger where brcode='$brcode' and dataexchdt='0000-00-00' and grp='Sundry Creditors' and op!='0'";
//echo $sqlc1;
mysqli_query($db,$sqlc2)or die("led err".mysqli_error($db));

while($rowsc=mysqli_fetch_array($resc))
{
// print "\n<form method='post' action='editcust.php'>\n<input type=hidden name='code' value='$row[1]'><input type=hidden name='acname' value='$row[5]'>";
 //print "\n<tr bgcolor=aliceblue><td>".$i++."</td><td>$row[cname]</td><td>$row[sapcode]</td></tr></form>";

$re=mysqli_query($db,"select cname,sapcode from supsap where brcode='$brcode' and sapcode='$rowsc[0]'")or die(mysqli_error($db));
$row=mysqli_fetch_array($re);


//print $acname;

$sqlc1="delete from sup_master where branch='$brcode' and code='$row[sapcode]' and web='upd'";
//echo $sqlc1;
mysqli_query($db,$sqlc1)or die("sup err".mysqli_error($db));


$res=mysqli_query($db,"select * from sup_master where code='$row[sapcode]'") or die(mysqli_error($db));
 if(mysqli_num_rows($res)>0)
    {
      $reac=mysqli_query($db,"select * from ledger where code='$row[sapcode]' and grp='Sundry Creditors' ") or die(mysqli_error($db));
      
       if(mysqli_num_rows($reac)>0)
        {
           $rowac=mysqli_fetch_array($reac); 
           $acname=$rowac[led];
           $reop=mysqli_query($db,"select sum(amt) from supsap where brcode='$brcode' and sapcode='$row[sapcode]'")or              
die(mysqli_error($db));
           $rowop=mysqli_fetch_array($reop);
//print $rowop[0];

            $sqlop="insert into ledger values('$acname','Sundry Creditors','$rowop[0]','Cr','$row[sapcode]','$brcode','0000-00-00')";
           mysqli_query($db,$sqlop) or die('<b>Ledger creation error:'.mysqli_error($db));


         }
   }

 else
   {
$acname=$row[cname].' ('.$row[sapcode].')';

$sqlc="insert into sup_master values('$row[sapcode]','$row[cname]','','','','','','','','','','','','upd', '$brcode','','0')";
echo $sqlc;
mysqli_query($db,$sqlc)or die("customer err".mysqli_error($db));

$reop=mysqli_query($db,"select sum(amt) from supsap where brcode='$brcode' and sapcode='$row[sapcode]'")or die(mysqli_error($db));
$rowop=mysqli_fetch_array($reop);
//print $rowop[0];

$sqlop="insert into ledger values('$acname','Sundry Creditors','$rowop[0]','Cr','$row[sapcode]','$brcode','0000-00-00')";
mysqli_query($db,$sqlop) or die('<b>Ledger creation error:'.mysqli_error($db));
  }

}
  
           
}
?>


<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link type="text/css" href="css/form1.css" rel="stylesheet" />
<style>
tr{background-color:c1d799}
.mnu{margin-left:5}
</style>
<center>
<br><br><br><br><br><br>
<form action='upldsupop.php' method='post' ENCTYPE="multipart/form-data">
<table border=1>
<tr><td>
<table>
<th colspan=2>Update SAP Supplier Opening
<tr><td><input type=submit value='Upload' name='upd'></td></tr>
</table>
</td></tr>
</table>
</form>
