<<?
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' and invtype='JC'");
$prow=mysqli_fetch_row($re);
if($prow[0]<=0)
 $retno=1;
else
 $retno=($prow[0]+1);

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


$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($rate as $a)
{
 $rate[$i]=$a; 
 $i++;
}
$i=0;
foreach($disc as $a)
{
 $disc[$i]=$a; 
 $i++;
}
$i=0;
foreach($amt as $a)
{
 $amt[$i]=$a; 
 $i++;
}
$i=0;
foreach($hnd as $a)
{
 $hnd[$i]=$a; 
 $i++;
}

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

$i=0;
foreach($taxamt as $a)
{
 $taxamt[$i]=$a; 
 $i++;
}
$i=0;
foreach($gtot as $a)
{
 $gtot[$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('$jcno','$invtype','$customer','$addr','$partcode[$j]','$partname1[$j]','$retqty1[$j]','$rate[$j]','$disc[$j]','$amt[$j]','$hnd[$j]','$vat[$j]', '$taxamt[$j]','$gtot[$j]','$brcode','$paytype','$retno','$vatorcst','$entrydate','$cessp[$j]','$cessamt[$j]','$jvno')";
print $sql;
mysqli_query($db,$sql)or die(mysqli_error($db));
}
}

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

$sql="insert into journal2 values('$entrydate','$jvno','Dr','Output tax 12.5 (Sale Ret Spare)','$sumrow[1]', '0','JCSpare Sales Ret INV No: $retno', '$brcode','Sale ret' ,'jcNO','$jcno', '$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','JCSpare Sales Ret INV No: $retno', '$brcode','Sale ret' ,'jcNO','$jcno', '$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','JC Spare Sales Ret No: $retno','$brcode','Sale ret','jcNO','$jcno','$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]', 'JCSpare Sales Ret  No: $retno','$brcode','sale ret','jcno','$jcno','$retno')";
//print $sql;
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error($db));


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