<?php
include("../include/session.php");
	
	date_default_timezone_set('Africa/Gaborone');
	$entry_date = date("Y-m-d H:i:s", time()); 
	
	$pallets = $_GET['pallets_text'];
	$pallets_price = $_GET['pallets_price'];
	
	$table_name = $_SESSION['db_prefix']."_cash_deliver_product";
	$cash_deliver_query = "INSERT INTO $table_name (cash_sale_no, cash_receipt_no, sale_date, pallets, pallets_price, cust_name, cust_address, cust_place, cust_phone, cust_vat_number, additional_info) VALUES ('".$_SESSION['cash_sale_no']."', '".$_SESSION['receipt_no']."', '".$_SESSION['sale_date']."', '".$pallets."', '".$pallets_price."', '".$_SESSION['cust_name']."', '".$_SESSION['cust_address']."', '".$_SESSION['cust_place']."', '".$_SESSION['phone_no']."', '".$_SESSION['cust_vat_number']."', '".$_SESSION['additional_info']."' ) ";
	$cash_deliver_result = $con->query($cash_deliver_query);
	
	$table_name = $_SESSION['db_prefix']."_cash_deliver_product_lines_temp";
	$cash_items_query = "SELECT product_item_id, item_type, quantity, batch_code, product_store_line_id, price  FROM $table_name WHERE cash_sale_no = '".$_SESSION['cash_sale_no']."' and user_name = '".$_SESSION['username']."' ";
	$cash_items_result = $con->query($cash_items_query);
	
	$stock_alert = 'no';
	$alert_array = array();
	$alert2_array = array();
	$alert_text = '';
	while($row = $cash_items_result->fetch_assoc()) {
	$product_item_id = $row["product_item_id"];
	$item_type = $row["item_type"];
	$quantity = $row["quantity"];
	$price = $row["price"];
	$batch_code = $row["batch_code"];
	$product_store_line_id = $row["product_store_line_id"];
	
	$table_name = $_SESSION['db_prefix']."_cash_deliver_product_lines";
	$cash_item_deliver_query = "INSERT INTO $table_name (cash_sale_no, sale_date, product_item_id, item_type, quantity, batch_code, price) VALUES ('".$_SESSION['cash_sale_no']."', '".$_SESSION['sale_date']."', '".$product_item_id."', '".$item_type."', '".$quantity."', '".$batch_code."', '".$price."' ) ";
	$cash_item_deliver_result = $con->query($cash_item_deliver_query);
	
	
	$table_name2 = $_SESSION['db_prefix']."_product_store";
    $stock_total_query = "SELECT item_name, quantity, minimumStock FROM $table_name2 WHERE product_item_id = '".$product_item_id."'";
    $stock_total_result = $con->query($stock_total_query);
    $row = mysqli_fetch_assoc($stock_total_result);
    $total_quantity = $row['quantity'];
	$min_stock = $row['minimumStock'];
	$product_item_name = $row['item_name'];

    $total_quantity = $total_quantity - $quantity;
	
	if ($min_stock > $total_quantity) {
	$stock_alert = 'yes';
	$alert_array[] = $product_item_id;
	$alert2_array[] = $total_quantity;
	
	$alert_text = $alert_text.'The stock quantity for <b>'.$product_item_name.'</b> is below the minimum stock of <b>'.$min_stock.'</b>.<br>The present stock quantity is now <b>'.$total_quantity.'</b>.<br><br>';
	}
	
       
    $stock_query = "UPDATE $table_name2 SET quantity = '".$total_quantity."' , lastModDate = '".$_SESSION['sale_date']."'  WHERE product_item_id = '".$product_item_id."'";
    $stock_result = $con->query($stock_query);
		
	$table_name3 = $_SESSION['db_prefix']."_product_history";
	$history_query = "INSERT INTO $table_name3 (product_item_id, entry, reference, reference_2, quantity, total, date, entryDate, userId) VALUES ('".$product_item_id."', '14', '".$_SESSION['cash_sale_no']."', '".$_SESSION['cust_name']."', '".$quantity."', '".$total_quantity."','".$entry_date."','".$entry_date."', '".$_SESSION['username']."')";
	$history_result = $con->query($history_query);
	
	$table_name = $_SESSION['db_prefix']."_product_store_lines";
	$product_stock_query = "UPDATE $table_name SET quantity_out = quantity_out + '".$quantity."', delivery_note_no = CONCAT(delivery_note_no, '  ".$_SESSION['cash_sale_no']."')  WHERE product_store_line_id = '".$product_store_line_id."' ";
	$product_stock_result = $con->query($product_stock_query);
	
}

$table_name = $_SESSION['db_prefix']."_tracking_store";
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."', '".$_SESSION['username']."', '38', '".$_SESSION['cash_sale_no']."' ,'".$_SESSION['receipt_no']."', '".$_SESSION['cash_sale_no']."', '".$_SESSION['cust_name']."')";
$tracking_result = $con->query($tracking_query);

if ($stock_alert=='yes') {
		include('../include/min_sales_product_alert.php');
}
	
	
	 	
    if (isset($_GET['new_button']) )
	   {
		 $action='newCashWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		 $action='cash_deliver_product_display.php?cash_sale_no='.$_SESSION['cash_sale_no'] ;
	  }
	 
?>

<script language="javascript" type="text/javascript">
        
		 window.location="<?php echo $action ?> ";
        
       </script>



