<?php
session_start();
if(!isset($_SESSION['aid']))
{
	echo '<script>location.href="../index.php"</script>';
}

include "../connection/config.php";

if(isset($_POST['submit']))
{
    $flag=$_POST['flag'];
    $srid=$_POST['srid'];

    $date=date("Y/m/d");
    $item=$_POST['item'];
    $billno=$_POST['billno'];
    $quantity=$_POST['quantity'];
    $reason=$_POST['reason'];
    $quality=$_POST['quality'];
    $amount=$_POST['amount'];
    $type=$_POST['type'];


    $s= mysqli_query($con,"select * from sales_reg1 where sales_billno='$billno'");
    $f = mysqli_fetch_assoc($s);
    $customer = $f['cuid'];

  $sql="insert into sales_return(cuid,iid,date,bill_no,quantity,reason_return,item_quality,amount_return,type)
        values('$customer','$item','$date','$billno','$quantity','$reason','$quality','$amount','$type')";
  $exe=mysqli_query($con,$sql);

  //add good stock and dead stock
  $gsql=mysqli_query($con,"select * from stock where iid='$item' ");
  $gfetch=mysqli_fetch_assoc($gsql);
  $goodstock=$gfetch['good_stock'];
  echo $deadstock=$gfetch['dead_stock'];

  if($quality=="Good")
  {
    $gdstock = $goodstock + $quantity ;
    $qsql="update stock set good_stock='$gdstock' where iid='$item'";
    $qexe=mysqli_query($con,$qsql);
  }

  if($quality=="Dead")
  {
    echo $ddstock = $deadstock + $quantity ;
    $qsqli="update stock set dead_stock='$ddstock' where iid='$item'";
    $qexee=mysqli_query($con,$qsqli);
  }

  echo '<script>location.href="sales_return.php"</script>';

}
?>