<?php 
session_start();
//mysql_connect("localhost","root","vertrigo") or die("Error Connecting Server");
//mysql_select_db("sm") or die("Error Opening Database");
include 'dbc.php';

$today=date("Y-m-d");

$userid=$_SESSION["userid"];
$username=$_SESSION["username"];
$id=$_REQUEST["id"];
$sql = "SELECT * FROM invoice_details where id = '$id' and user_id = '$userid'";
$result=$conn->query($sql);
$row = $result->fetch_assoc();
$item_no=$row['itemcode'] ;
$inv_no=$row['inv_no'] ;
$saleqty=$row['qty'] ;
$amount=$row['amount'] ;
$barcode=$row['barcode'] ;
$type_name=$row['type_name'] ;
$item_name=$row['item_name'] ;
$srate=$row['srate'] ;
$username=$row['username'] ;

$sql9 = "Insert into invoice_return (id,date,inv_no,item_no,barcode,type_name,item_name,srate,qty,amount,username,user_id)values (null,'$today','$inv_no','$item_no','$barcode','$type_name','$item_name','$srate','$saleqty','$amount','$username','$userid')";
$conn->query($sql9);

$sql1 = "SELECT * FROM items where id = '$item_no' and user_id = '$userid'";
$result1=$conn->query($sql1);
$row1 = $result1->fetch_assoc();
$prebal=$row1['bal'] ;
$netbal=$prebal + $saleqty;
$sql2 = "UPDATE items SET bal='$netbal' WHERE id='$item_no' and user_id = '$userid'";
$conn->query($sql2);

$sql4 = "SELECT * FROM invoice where id = '$inv_no' and user_id = '$userid'";
$result4=$conn->query($sql4);
$row4 = $result4->fetch_assoc();
$netamount=$row4['netamount'] ;
$discount=$row4['discount'] ;
$cashrec=$row4['cashrec'] ;
$dis=$row4['dis'] ;
$gamount = $netamount - $amount;

$difdis = $gamount * $dis / 100;
$netdis = $discount - $difdis;

$netgamount = $gamount - $netdis;
$netcashrec = $cashrec - $amount;

$sql5 = "UPDATE invoice SET netamount='$gamount',discount='$netdis',gamount='$netgamount' ,cashrec='$netcashrec' WHERE id='$inv_no' and user_id = '$userid'";
$conn->query($sql5);

$sql3 = "delete from invoice_details where id='$id'";
$conn->query($sql3);

$Message = "Record Delete";
    header("Location:Invoice_Return.php?Message={$Message}");

/*$sql="Select * from agents where userid='$user_id'";
$data=mysql_query($sql) or die ("Error:" . mysql_error());
$userdata=mysql_fetch_array($data);
$email=$userdata["e_mail"];
mail($email . ",info@lalburj.com","Candidate Information","Candidate Data Posted Successfully","Passport No:" . $app_pp_no . " of " . $app_name . "Has been Posted Successfully By:" . $_SESSION["userid"]);*/
?>
