<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}



//include ("conrepo1.php");
include ("stockdw1.php");

//=============================================================================================//

$t1=0;
$t2=0;
$t3=0;
$t=0;

mysqli_query($db,"delete from payments");
mysqli_query($db,"delete from finance");
mysqli_query($db,"delete from cash");


$r=mysqli_query($db,"select * from orderform where formno not in (select formno from veh_invoice where brcode='$brcode') and brcode='$brcode' and dt>='2010-04-01'")or die(mysqli_error());
while($re=mysqli_fetch_row($r))
{

$rr=mysqli_query($db,"select * from advcollection where brcode='$brcode' and formno='$re[0]' ")or die(mysqli_error());
while($ree=mysqli_fetch_row($rr))
{
mysqli_query($db,"insert into payments values('$ree[0]','$ree[1]','$ree[2]','$ree[3]','$ree[4]','$ree[5]','$ree[6]','$ree[7]','$re[7]')")or die(mysqli_error());
}
}
mysqli_query($db,"delete from payments where formno='0'");

mysqli_query($db,"delete from blockda where oformno in (select formno from veh_invoice where brcode='$brcode')");

$r2=mysqli_query($db,"select distinct(formno) from payments where formno not in (select oformno from blockda where brcode='$brcode') and brcode='$brcode' order by date ")or die(mysqli_error());
while($re3=mysqli_fetch_row($r2))
{

$rr22=mysqli_query($db,"select sum(amount) from payments where brcode='$brcode' and formno='$re3[0]' ")or die(mysqli_error());
$re222=mysqli_fetch_row($rr22);

$rr11=mysqli_query($db,"select netamt,config,rname,cust from orderform where brcode='$brcode' and formno='$re3[0]' ")or die(mysqli_error());
$re111=mysqli_fetch_row($rr11);

$a=$re111[0]*70/100;
$net=$re111[0];
$recd=$re222[0];
if ($a>=$re222[0])
{
}
else
{

$sql="select * from vtmp where chno not in (select chno from blockda where brcode='$brcode') and config='$re111[1]' order by pdate";
//print $sql;

$rr112=mysqli_query($db,"select * from vtmp where chno not in (select chno from blockda where brcode='$brcode') and config='$re111[1]' order by pdate")or die(mysqli_error());
 if(mysqli_num_rows($rr112)>0)
{

$re1112=mysqli_fetch_row($rr112);
mysqli_query($db,"insert into blockda values('$re1112[0]','$re111[1]','".getRep($re111[2])."','$re1112[5]','$re111[3]','$brcode','$re1112[4]','$re3[0]','$net','$recd')")or die(mysqli_error());
}

}

}

return;

 function getRep($code)
 {
   $reep=mysqli_query($db,"select repname from rep_master where code='$code'");
   $reeprow=mysqli_fetch_row($reep);
   return $reeprow[0];
}
?>

