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

include("../opendb.php");
include("../test.php");
$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 salesreturn where brcode='$brcode' and vatorcst='$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'];
$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++;
}

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

$invno=$_REQUEST['invno'];
$customer=$_REQUEST['customer'];
$addr=$_REQUEST['addr'];
$paytype=$_REQUEST['paytype'];
$saleordno=$_REQUEST['saleordno'];
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')";

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

$sql="update saleorder_spare set sup_qty='$supqty1[$j]',sup_purno='$invno',sup_date='$entrydate' , trans_type='$invtype' where orderno='$saleordno' and partcode='$partcode[$j]' and brcode='$brcode'";
mysqli_query($db,$sql)or die('err'.mysqli_error($db));
}
}

$sumres=mysqli_query($db,"select sum(aprice),sum(taxamt),sum(tot),sum(cessamt) from salesreturn where retno='$retno' and brcode='$brcode' and vatorcst='$vatorcst'");
$sumrow=mysqli_fetch_row($sumres);

$sql="insert into journal2 values('$entrydate','$jvno','Dr','Output tax 12.5 (Sale Ret Spare)','$sumrow[1]', '0','Spare Sales Ret INV No: $retno', '$brcode','Sale ret' ,'retNO','$retno', '$retno')";

mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error($db));

$sql="insert into journal2 values('$entrydate','$jvno','Dr','Output Cess 1% (Sale Ret Spare)','$sumrow[3]', '0','Spare Sales Ret INV No: $retno', '$brcode','Sale ret' ,'retNO','$retno', '$retno')";

mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error($db));


 $sql="insert into journal2 values('$entrydate','$jvno','Dr','Sales ret 12.5(Spare)','$sumrow[0]','0','Spare Sales Ret No: $retno','$brcode','Sale ret','retNO','$retno','$retno')";
mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error($db));

$sql="insert into journal2 values('$entrydate','$jvno','Cr','$customer','0','$sumrow[2]', 'Spare Sales Ret  No: $retno','$brcode','sale ret','retno','$retno','$retno')";
//print $sql;
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error($db));

log_entry($brcode,$user,$REMOTE_ADDR,"spare sale return:$invno, Journal: $jvno");
?>
<script>
 document.location='prnsalereturn.php?retno=<?print $retno?>&invtype=<?print $invtype?>'; 
</script>
