<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
$invno=$_REQUEST['invno'];
$customer=$_REQUEST['customer'];
$addr=$_REQUEST['addr'];

$porderno=$_REQUEST['porderno'];

include("../opendb.php");
include("../test.php");

 $re=mysqli_query($db,"select * from sparepurreturn where purno='$invno'")or die(mysqli_error($db));
 if(mysqli_num_rows($re)>0)
 {
    print "<script> alert('Purchase no already Exists'); </script>";
    return;
 }


$re1=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
$jvrow=mysqli_fetch_row($re1);
if($jvrow[0]<=0)
 $jvno=1;
else
 $jvno=($jvrow[0]+1);
$vatorcst=$_REQUEST['vatorcst'];
$re=mysqli_query($db,"select max(retno) from sparepurreturn where brcode='$brcode' and taxtype='$vatorcst'");
$prow=mysqli_fetch_row($re);
if($prow[0]<=0)
 $retno=1;
else
 $retno=($prow[0]+1);
$d=$_REQUEST['d'];
$m=$_REQUEST['m'];
$y=$_REQUEST['y'];

$d1=$_REQUEST['d1'];
$m1=$_REQUEST['m1'];
$y1=$_REQUEST['y1'];
$invdate=$y.'-'.$m.'-'.$d;
$entrydate=$y1.'-'.$m1.'-'.$d1;
$partcode =$_REQUEST['partcode'];
$partname =$_REQUEST['partname'];
$rate =$_REQUEST['rate'];
$retqty =$_REQUEST['retqty'];
$disc =$_REQUEST['disc'];
$aprice=$_REQUEST['aprice'];
$price=$_REQUEST['price'];
$cessp =$_REQUEST['cessp'];
$cesamt =$_REQUEST['cesamt'];
$hand =$_REQUEST['hand'];
$tax =$_REQUEST['tax'];
$taxamt =$_REQUEST['taxamt'];
$tot =$_REQUEST['tot'];
$i=0;
foreach($partcode as $a)
{
 $partcode[$i]=$a; 
 $i++;
}
$i=0;
foreach($partname as $a)
{
 $partname1[$i]=$a; 
 $i++;
}
$i=0;
foreach($retqty as $a)
{
 $retqty1[$i]=$a; 
 $i++;
}
$i=0;
foreach($price as $a)
{
 $price[$i]=$a; 
 $i++;
}
/*
$i=0;
foreach($disc as $a)
{
 $disc[$i]=$a; 
 $i++;
}
*/
$i=0;
foreach($aprice as $a)
{
 $aprice[$i]=$a; 
 $i++;
}
$i=0;
foreach($hand as $a)
{
 $hand[$i]=$a; 
 $i++;
}

$i=0;
foreach($tax as $a)
{
 $tax[$i]=$a; 
 $i++;
}

$i=0;
foreach($taxamt as $a)
{
 $taxamt[$i]=$a; 
 $i++;
}
$i=0;
foreach($tot as $a)
{
 $tot[$i]=$a; 
 $i++;
}

$i=0;
foreach($cessp as $a)
{
 $cessp[$i]=$a; 
 $i++;
}
$i=0;
foreach($cessamt as $a)
{
 $cessamt[$i]=$a; 
 $i++;
}

//

$pv=$_REQUEST['pv'];

$to_branch=$_REQUEST['to_branch'];
if($pv)
{
 $re=mysqli_query($db,"select brname from branch where brcode='$to_branch'")or die('err2'.mysqli_error($db));
 $rr=mysqli_fetch_row($re);
 $branch_na=$rr[0];
$c=1;
print "Purchase return #$retno Preview<table border=1>";
print "<th>No<th>Part Code <th>Part Name<th>Qty<th>Supplier<th>Location";
for($j=0;$j<$i;$j++)
{

	if($retqty[$j]>0)
	{
  $sql="select location from sparelocmast where itemcode='$partcode[$j]' and brcode='$brcode'";
  $locre=mysqli_query($db,$sql)or die(mysqli_error($db));
  $locrow=mysqli_fetch_array($locre);

$amtres=mysqli_query($db,"select lstprice from spareprmast where partcode='$partcode[$j]' order by effectfrom desc")or die(mysqli_error($db));
$amtrow=mysqli_fetch_row($amtres)or die(mysqli_error($db));

 print "<tr><td>".$c++."</td><td>$partcode[$j]</td><td>$partname[$j]</td><td align=right>$retqty[$j]";
 print "</td><td>$customer</td><td>$locrow[0]</td></tr>";
 $tq+=$retqty[$j];

        }
}
 print "<tr><td colspan=3><center>Total</td><td align=right>$tq</td><td>&nbsp;</td><td>&nbsp;</td>";
 print "<td align=right>".putComa($tamt)."</td></tr>";
return;
} // end if prev









//


$entrydate=$y.'-'.$m.'-'.$d;

for($j=0;$j<$i;$j++)
{
if($retqty1[$j]>0)
{
 // $sql="insert into salesreturn values('$invno','$invtype','$customer','$addr','$partcode[$j]','$partname1[$j]','$retqty1[$j]','$price[$j]','$disc[$j]','$aprice[$j]','$hand[$j]','$tax[$j]', '$taxamt[$j]','$tot[$j]','$brcode','$paytype','$retno','$vatorcst','$entrydate','$cessp[$j]','$cessamt[$j]','$jvno')";
$sql="insert into sparepurreturn values('$invno','$customer','$invno','$entrydate','$partcode[$j]','$partname1[$j]','$retqty1[$j]','0','$price[$j]','$disc[$j]','$aprice[$j]','$hand[$j]','$tax[$j]','$taxamt[$j]','$tot[$j]','$brcode','$vatorcst','$jvno','$cessp[$j]','$cessamt[$j]','$retno')";
//$sql="insert into sparepurreturn values('$invno','$sup','$invno','$entrydate','$partcode[$j]','$partname[$j]','$retqty[$j]','0','$price[$j]','$disc[$j]','$aprice[$j]','$hand[$j]','$tax[$j]','$taxamt[$j]','$tot[$j]','$brcode','$taxtype','$jvno','$cessp[$j]','$cessamt[$j]','$retno')";

mysqli_query($db,$sql)or die('ERr'.mysqli_error($db));

}
}

if($vatorcst=='VAT')
{
 $taxdrres=mysqli_query($db,"select sum(aprice),tax from sparepurreturn where purno='$invno' and pbillno='$invno' and taxtype='VAT' and brcode='$brcode' and retno='$retno' group by tax");
  while($taxdrrow=mysqli_fetch_array($taxdrres))
  {
    $ledger='Spare Purchase VAT '.$taxdrrow[tax].'%';  
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxdrrow[0]','Spare P Ret purno: $invno,PBill:$invno','$brcode','Spare P Ret','Purno','$invno','$retno')";
     mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
  }


 $taxres=mysqli_query($db,"select tax, sum(taxamt) from sparepurreturn where purno='$invno' and pbillno='$invno' and taxtype='VAT' and brcode='$brcode' and retno='$retno' group by tax");
  while($taxrow=mysqli_fetch_array($taxres))
  {
    $ledger='VAT Collected '.$taxrow[tax].'%';  
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[1]','Spare P Ret purno: $invno,PBill:$invno','$brcode','Spare P Ret','Pur no','$invno','$retno')";
     mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
  }

//*************** cess
 $taxcessres=mysqli_query($db,"select sum(cessamt),cessp from sparepurreturn where purno='$invno' and pbillno='$invno' and taxtype='VAT' and brcode='$brcode' and retno='$retno' group by purno")or die('cess err.'.mysqli_error($db));
  while($taxcessrow=mysqli_fetch_array($taxcessres))
  {
    $ledger='Cess Paid '.$taxcessrow[1].'%';
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxcessrow[0]','Spare P Ret purno: $invno,PBill:$invno','$brcode','Spare P ret','Pur no','$invno','$retno')";

     mysqli_query($db,$sql) or die('Led Post Err6'.mysqli_error($db));
  }


//************************
 $taxcrres=mysqli_query($db,"select sum(tot) from sparepurreturn where purno='$invno' and pbillno='$invno' and taxtype='VAT' and brcode='$brcode' and retno='$retno'");
  while($taxcrrow=mysqli_fetch_array($taxcrres))
  {
    $sql="insert into journal2 values('$entrydate','$jvno','Dr','$customer','$taxcrrow[0]','0','Spare P Ret purno: $invno,PBill:$invno','$brcode','Spare P Ret','Pur No','$invno','$retno')";
     mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));
  }
} // end if
else
{
//********************* CST
 $taxdrres=mysqli_query($db,"select sum(aprice),tax from sparepurreturn where purno='$invno' and pbillno='$invno' and taxtype='CST' and brcode='$brcode' and retno='$retno' group by tax");
  while($taxdrrow=mysqli_fetch_array($taxdrres))
  {
    $ledger='Spare Purchase CST '.$taxdrrow[tax].'%';  
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxdrrow[0]','Spare P Ret purno: $invno,PBill:$invno','$brcode','Spare Pur ret','Purno','$invno','$retno')";
     mysqli_query($db,$sql) or die('Led Post Err4'.mysqli_error($db));
  }


 $taxres=mysqli_query($db,"select tax, sum(taxamt) from sparepurreturn where purno='$invno' and pbillno='$invno' and taxtype='CST' and brcode='$brcode' and retno='$retno' group by tax");
  while($taxrow=mysqli_fetch_array($taxres))
  {
    $ledger='CST Collected '.$taxrow[tax].'%';  
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[1]','Spare P Ret purno: $invno,PBill:$invno','$brcode','Spare Pur ret','Purno','$invno','$retno')";
     mysqli_query($db,$sql) or die('Led Post Err5'.mysqli_error($db));
  }
//************************
 $taxcrres=mysqli_query($db,"select sum(tot) from sparepurreturn where purno='$invno' and pbillno='$invno' and taxtype='CST' and brcode='$brcode' and retno='$retno'");
  while($taxcrrow=mysqli_fetch_array($taxcrres))
  {
    $sql="insert into journal2 values('$entrydate','$jvno','Dr','$customer','$taxcrrow[0]','0','Spare P Ret purno: $invno,PBill:$invno','$brcode','Spare Pur ret','Pur Order','$porderno','$retno')";
echo $sql;exit;
     mysqli_query($db,$sql) or die('Led Post Err6'.mysqli_error($db));
  }
}



log_entry($brcode,$user,$REMOTE_ADDR,"p return old :$invno, Journal: $jvno");

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

?>
<script>
 alert('Data has been saved');
</script>
