<?
session_start();
$brcode = $_SESSION['brcode'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
$d = $_REQUEST['d'];
$m = $_REQUEST['m'];
$y = $_REQUEST['y'];
$d1 = $_REQUEST['d1'];
$m1 = $_REQUEST['m1'];
$y1 = $_REQUEST['y1'];
$invno = $_REQUEST['invno'];
$invtype = $_REQUEST['invtype'];
$customer = $_REQUEST['customer'];
$addr = $_REQUEST['addr'];
$retno = $_REQUEST['retno'];
$saleordno = $_REQUEST['saleordno'];
$paytype = $_REQUEST['paytype'];
$vatorcst = $_REQUEST['vatorcst'];
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 salesreturnitem where brcode='$brcode'");
$prow=mysqli_fetch_row($re);
if($prow[0]<=0)

 
 $retno='23'.$brcode.'090001'; 
else
 $retno=($prow[0]+1);

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

$i=0;
$partcode = $_REQUEST['partcode'];
foreach($partcode as $a)
{
 $partcode[$i]=$a; 
 $i++;
}
$i=0;
$partname = $_REQUEST['partname'];
foreach($partname as $a)
{
 $partname1[$i]=$a; 
 $i++;
}

$i=0;
$itemtype1 = $_REQUEST['itemtype1'];
foreach($itemtype1 as $a)
{
 $itemtype1[$i]=$a; 
 $i++;
}

$i=0;
$retqty = $_REQUEST['retqty'];
foreach($retqty as $a)
{
 $retqty1[$i]=$a; 
 $i++;
}
$i=0;
$price = $_REQUEST['price'];
foreach($price as $a)
{
 $price[$i]=$a; 
 $i++;
}
$i=0;
$disc = $_REQUEST['disc'];
foreach($disc as $a)
{
 $disc[$i]=$a; 
 $i++;
}
$i=0;
$aprice = $_REQUEST['aprice'];
foreach($aprice as $a)
{
 $aprice[$i]=$a; 
 $i++;
}
$i=0;
$hand = $_REQUEST['hand'];
foreach($hand as $a)
{
 $hand[$i]=$a; 
 $i++;
}

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

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

$i=0;
$cessp = $_REQUEST['cessp'];
foreach($cessp as $a)
{
 $cessp[$i]=$a; 
 $i++;
}
$i=0;
$cessamt = $_REQUEST['cessamt'];
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 salesreturnitem 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','$itemtype1[$j]')";
mysqli_query($db,$sql)or die(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));
}
}


if($vatorcst=='INTRASTATE')
{
    $sumres=mysqli_query($db,"select sum(aprice),sum(taxamt),sum(tot),sum(cessamt) from salesreturnitem where retno='$retno' and brcode='$brcode' and vatorcst='INTRASTATE' and itemtype!='LABOUR'");
$sumrow=mysqli_fetch_row($sumres);

$sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Dr','Outward Supply - Sales Others','$sumrow[0]', '0','Spare Sales Ret  No: $retno invno $invno', '$brcode','Sale ret' ,'retNO','$retno', '$retno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error($db));

$sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Dr','Output Tax','$sumrow[1]', '0','Spare Sales Ret  No: $retno invno $invno', '$brcode','Sale ret' ,'retNO','$retno', '$retno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error($db));


$sumresl=mysqli_query($db,"select sum(aprice),sum(taxamt),sum(tot),sum(cessamt) from salesreturnitem where retno='$retno' and brcode='$brcode' and vatorcst='INTRASTATE' and itemtype='LABOUR'");
$sumrowl=mysqli_fetch_row($sumresl);

$sqll="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Dr','OUTWARD SUPPLY - LABOUR OTHERS','$sumrowl[0]', '0','Spare Sales Ret  No: $retno invno $invno', '$brcode','Sale ret' ,'retNO','$retno', '$retno')";
mysqli_query($db,$sqll) or die("J V Write error 2:".mysqli_error($db));

$sqll="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Dr','Output Tax','$sumrowl[1]', '0','Spare Sales Ret  No: $retno invno $invno', '$brcode','Sale ret' ,'retNO','$retno', '$retno')";
mysqli_query($db,$sqll) or die("J V Write error 2:".mysqli_error($db));

$sumrest=mysqli_query($db,"select sum(tot) from salesreturnitem where retno='$retno' and brcode='$brcode' and vatorcst='INTRASTATE' ");
$sumrowt=mysqli_fetch_row($sumrest);

$sqlt="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Cr','$customer','0','$sumrowt[0]', 'Spare Sales Ret  No: $retno','$brcode','sale ret','retno','$retno','$retno')";
//print $sql;
mysqli_query($db,$sqlt) or die("J V Write error 1:".mysqli_error($db));
}
else
{
    $sumres=mysqli_query($db,"select sum(aprice),sum(taxamt),sum(tot),sum(cessamt) from salesreturnitem where retno='$retno' and brcode='$brcode' and vatorcst='INTERSTATE' and itemtype!='LABOUR'");
$sumrow=mysqli_fetch_row($sumres);

    $sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Dr','Outward Supply - Sales Others','$sumrow[0]', '0','Spare Sales Ret  No: $retno invno $invno', '$brcode','Sale ret' ,'retNO','$retno', '$retno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error($db));

$sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Dr','Output Tax','$sumrow[1]', '0','Spare Sales Ret  No: $retno invno $invno', '$brcode','Sale ret' ,'retNO','$retno', '$retno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error($db));


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

    $sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Dr','OUTWARD SUPPLY - LABOUR OTHERS','$sumrow[0]', '0','Spare Sales Ret  No: $retno invno $invno', '$brcode','Sale ret' ,'retNO','$retno', '$retno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error($db));

$sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Dr','Output Tax','$sumrow[1]', '0','Spare Sales Ret  No: $retno invno $invno', '$brcode','Sale ret' ,'retNO','$retno', '$retno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error($db));


 
$sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) 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));
}

$jvdifres=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
$jvdifrow=mysqli_fetch_row($jvdifres);
$dif=$jvdifrow[0];
if($dif<0)
{
    $xx=abs($dif);
    $sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Dr','Round Off','".$xx."', '0','Round ofSINV No: $invno','$brcode','Spare Invoice Other','S ORd no','$retno','$invno')";
    mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
}
else if($dif>0)
{
     $xx=abs($dif);
     $sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Cr','Round Off','0','".$xx."','Round of adjusted SINV No: $invno','$brcode','Spare Invoice Other','S ORd no','$retno','$invno')";
     mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
}
$ip_address = "$jvno|Sales Return|Add";
log_entry($brcode,$user,$ip_address,"spare sale return:$invno, Journal: $jvno",$db);
?>
<script>
 document.location='prnsalereturn.php?retno=<?print $retno?>'; 
</script>
