<?php
	include 'inc/conx.php';

	$invoice = $_POST['invoice'];

	$select = $db->query("SELECT * FROM stock WHERE invoiceNo = '$invoice' AND last_up IS NULL ORDER BY id DESC");
	while($fetch = $select->fetchArray()){
		echo $id = $fetch['id'];
		echo $itemName = $fetch['itemDescription'];
		echo $supplierName = $fetch['supplierName'];
		echo $qty      = $fetch['qty'];
		echo $rate     = $fetch['rate'];
		echo $item_dispr = $fetch['dis'];
		echo $item_disrs = $fetch['amount'];
		$update = $db->query("UPDATE stock SET last_up = 1 WHERE itemDescription = '$itemName' AND id = '$id' ");

		$sitem  = $db->query("SELECT * FROM itemsetup WHERE item_name = '$itemName' ");
		$fitem  = $sitem->fetchArray();
		$prST   = $fitem['stock'];
		$prRS	= $fitem['purchase_price'];

		if($fetch['type'] == 'Purchase'){
			$nwST   =  $prST + $fetch['qty'];
			$previousPricenQty = $prRS * $prST;
			$currentPricenQty = $rate * $qty;
			if ($item_dispr > 0) {
				$currentPricenQty  = $currentPricenQty - ((($rate * $item_dispr)/100) * $qty);
			}
			if ($item_disrs > 0) {
				$currentPricenQty  = $currentPricenQty - ($item_disrs); // * $item_qty
			}
			$nwPR = ($previousPricenQty + $currentPricenQty) / ($prST + $qty);

			$update = $db->query("UPDATE itemsetup SET stock = $nwST, purchase_price = $nwPR WHERE item_name = '$itemName' ");
		}
		if($fetch['type'] == 'Sale'){
			$nwST   =  $prST - $fetch['qty'];
			$update = $db->query("UPDATE itemsetup SET stock = $nwST WHERE item_name = '$itemName' ");
		}
		if($fetch['type'] == 'Misc'){
			$nwST   =  $prST + $fetch['qty'];
			$update = $db->query("UPDATE itemsetup SET stock = $nwST WHERE item_name = '$itemName' ");
		}
		if($fetch['type'] == 'Return'){
			echo $cus_ch = preg_replace('/[^a-z]/', "", strtolower($supplierName));
			if($cus_ch == 'c'){
				$nwST   =  $prST + $fetch['qty'];
			}elseif($cus_ch == 's'){ 
				$nwST   =  $prST - $fetch['qty'];
			}else{
				$nwST   =  $prST + $fetch['qty'];
			}
			
			$update = $db->query("UPDATE itemsetup SET stock = $nwST WHERE item_name = '$itemName' ");
		}
		if($fetch['type'] == 'PReturn'){
			echo $cus_ch = preg_replace('/[^a-z]/', "", strtolower($supplierName));
			if($cus_ch == 'c'){
				$nwST   =  $prST + $fetch['qty'];
			}elseif($cus_ch == 's'){ 
				$nwST   =  $prST - $fetch['qty'];
			}else{
				$nwST   =  $prST - $fetch['qty'];
			}
			
			$update = $db->query("UPDATE itemsetup SET stock = $nwST WHERE item_name = '$itemName' ");
		}

		echo 'Pre st: '. $prST . $fetch['itemDescription'] . ' ' .$fetch['qty'] . ' New ST' . $nwST;
		
	}
	
	
$saleType = $_POST['saleType'];
$paymentType = $_POST['paymentType'];
$date = $_POST['date'];

$change_date = explode("/", $date);
$show_date = $change_date[1]."/".$change_date[0]."/".$change_date[2];
$php_date = $change_date[2]."/".$change_date[0]."/".$change_date[1];

$name = $_POST['name'];
$ID = 0;
	$countQuery = $db->query("SELECT count(*) totalCount FROM customer_setup WHERE customer_name = '".$name."'");
	$rowcount = $countQuery->fetchArray();
	$totalCount = $rowcount['totalCount'];
	if ($totalCount < 1) {
	
	$countQuery = $db->query("SELECT count(*) totalCount FROM supplier_setup WHERE supplier_name = '".$name."'");
	$rowcount = $countQuery->fetchArray();
	$totalCount = $rowcount['totalCount'];
		if ($totalCount < 1) {
			$ID = 0;
	 	}else{
	 		$supplier = $db->query("SELECT * FROM supplier_setup WHERE supplier_name = '".$name."'");
	 		$fetchdate = $supplier->fetchArray();
	 		$ID = "s".$fetchdate['id'];
	 	}
	 }else{
	 	$customer = $db->query("SELECT * FROM customer_setup WHERE customer_name = '".$name."'");
 		$fetchdate = $customer->fetchArray();
 		$ID = "c".$fetchdate['id'];
	 }

$bilty = $_POST['bilty'];
@$salesman=$_POST['salesman'];
$update = $db->query("UPDATE purchases SET bilty = '$bilty', payment_type = '$paymentType', sale_type= '$saleType', user_name = '".$_SESSION['name']."', name = '$ID', name_word = '$name', date = '$date', php_date = '$php_date', show_date = '$show_date' WHERE invoiceNo = '$invoice'");

?>