<?php
session_start();
$username = $_SESSION['staff'];
if($_SESSION['staff']!= null)
{
$login=mysql_connect("localhost","root","");
mysql_select_db("bookstore",$login);
include("connect.php");

$billNo = $_POST['billNo'];
$memberName = $_POST['memberName'];
$j = $_POST['counter'];
$total = $_POST['total'];
$cash = $_POST['cash'];
$date = date("Y-m-d");
$admin = $username;

	$result3 = mysql_query("INSERT INTO receipt(receiptNo, cashierName, memberLname, receiptDate, receiptTotal, receiptCash)
	VALUES
	('$billNo', '$admin', '$memberName', '$date', '$total', '$cash')");
	
	$result4 = mysql_query("SELECT memberpoint FROM membership WHERE memberLname = '$memberName'");
	$row4 = mysql_fetch_array($result4);

$point = $row4['memberpoint'];
$new = $total/10;
$newpoint = $point + $new;

	$result5 = mysql_query("UPDATE membership SET memberpoint = '$newpoint' WHERE memberLname = '$memberName'");

for ($k=0; $k <$j; $k++)
{
$description= $_POST['description'.$k];
$quantity = $_POST['quantity'.$k];
$price = $_POST['price'.$k];
$discount = $_POST['discount'.$k];

	$result = mysql_query("SELECT * FROM item WHERE itemName = '$description'");
	$row = mysql_fetch_array($result);
$ISBN = $row["ISBN"];
$itemQuantity = $row["itemQuantity"];
$itemSales = $row["itemSales"];
if($ISBN!=""){
$result2 = mysql_query("INSERT INTO receiptitem(receiptItem, receiptQuantity, receiptPrice, receiptDiscount, receiptNo)
	VALUES
	('$ISBN', '$quantity', '$price', '$discount', '$billNo')");

$newitemQuantity = $itemQuantity - $quantity;
$newSales = $itemSales + $quantity;

	$result6 = mysql_query("UPDATE item SET itemQuantity = '$newitemQuantity', itemSales = '$newSales' WHERE ISBN='$ISBN'");
//if(($quantity<1)||($price<0)){
//echo "<script language='javascript'>alert('Please check your amount and unit price.')</script>";
//echo "<script language='javascript'>window.location='addstock.php?po=$poNo'</script>";
//}else{
	//$query2 = "UPDATE poitem SET poQuantity = '$quantity', poPrice = '$price' WHERE poItem = '$ISBN'&&poNo = '$poNo'";
	//$result2 = $db->query($query2);
//}
//}
}
}
	$db = mysql_close();
	//echo "<script language='javascript'>alert('$j')</script>";
	echo "<script language='javascript'>window.location='receiptprint.php?billNo=".$billNo."&point=".$newpoint."'</script>";
	//echo "<script language='javascript'>window.location='cashiermain.php'</script>";
}
?>