<<?
session_start();
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);

$re=mysqli_query($db,"select max(retno) from salesreturn where brcode='$brcode'");
$prow=mysqli_fetch_row($re);
if($prow[0]<=0)
 $retno=1;
else
 $retno=($prow[0]+1);

$invdate=$y.'-'.$m.'-'.$d;
$entrydate=$y1.'-'.$m1.'-'.$d1;

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

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(mysqli_error());

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

$sumres=mysqli_query($db,"select sum(aprice),sum(taxamt),sum(tot),sum(cessamt) from salesreturn where retno='$retno' and brcode='$brcode' and vatorcst='VAT'");
$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());

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


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

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


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