<?php
include("../include/session.php");

$userId = $_SESSION['username'];
date_default_timezone_set('Africa/Khartoum');
$entryDate = date("Y-m-d H:i:s",time()-3600); 
$entry_date = date("Y-m-d H:i:s",time()-3600);

if ($_SESSION['job_no']=='') { $_SESSION['job_no']='0';}

$_SESSION['message_timber']='';
$_SESSION['message_mitek']='';
$_SESSION['message_sundries']='';
$_SESSION['message_other']='';

$table_name = $_SESSION['db_prefix']."_supply_customer";
$del_note_query = "SELECT delivery_note FROM $table_name ORDER BY delivery_note DESC LIMIT 1  ";
$del_note_result = mySQL_query($del_note_query);
$_SESSION['delivery_no'] = mysql_result($del_note_result, 0, "delivery_note")+1;	

$table_name = $_SESSION['db_prefix']."_supply_customer";
$supply_query = "INSERT INTO $table_name (type, cust_id, delivery_note, receipt_no, delivery_date, job_no ) VALUES ('cash', '".$_SESSION['cust_id']."', '".$_SESSION['delivery_no']."', '".$_SESSION['receipt_no']."', '".$_SESSION['supply_date']."', '".$_SESSION['job_no']."' ) ";
execsql($supply_query);

$table_name = $_SESSION['db_prefix']."_supply_customer";
$id_query = "SELECT supply_id  FROM $table_name ORDER BY supply_id DESC LIMIT 1;  ";
$id_result = mySQL_query($id_query);
$supply_id = mysql_result($id_result, 0, "supply_id");


$table_name2 = $_SESSION['db_prefix']."_supply_customer_lines";	
$table_name4 = $_SESSION['db_prefix']."_supply_customer_temp";	
$supply_query = "insert into $table_name2 (supply_id, job_no, truss_type, store_item_id, quantity)
select '".$supply_id."', job_no, truss_type, store_item_id, quantity from $table_name4 WHERE job_no = '".$_SESSION['job_no']."' AND user =  '".$_SESSION['username']."' ";
execsql($supply_query); 

$quantity_query = "SELECT store_item_id, quantity FROM $table_name4 WHERE  job_no = '".$_SESSION['job_no']."' AND user =  '".$_SESSION['username']."' ";
$quantity_result = mySQL_query($quantity_query);
$quantity_num = mysql_num_rows($quantity_result);

$email_message='';
$i=0;
while ($i<$quantity_num) {
    $quantity = mysql_result($quantity_result ,$i, "quantity");
	$store_item_id = mysql_result($quantity_result ,$i, "store_item_id");
	
	$table_name3 = $_SESSION['db_prefix']."_store_items";
	$stock_total_query = "SELECT item_name, quantity, price, subCategory, minimumStock FROM $table_name3 WHERE store_item_id = '".$store_item_id."'";
	$stock_total_result = mySQL_query($stock_total_query);
	$total_quantity = mysql_result($stock_total_result ,0, "quantity");
	$price = mysql_result($stock_total_result ,0, "price");
	$sub_category = mysql_result($stock_total_result ,0, "subCategory");
	$min_stock = mysql_result($stock_total_result ,0, "minimumStock");
	$item_name = mysql_result($stock_total_result ,0, "item_name");
	
    
	$total_quantity = $total_quantity - $quantity;
   
    $stockQuery = "UPDATE $table_name3 SET quantity = '".$total_quantity."' , lastModDate = '".$_SESSION['supply_date']."'   WHERE store_item_id = '".$store_item_id."'";
	$stockResult = mySQL_query($stockQuery);
	
	
	$table_name6 = $_SESSION['db_prefix']."_item_history";		
	$historyQuery = "INSERT INTO $table_name6 (store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$store_item_id."', '43', '".$_SESSION['delivery_no']."','".$quantity."', '".$total_quantity."','".$_SESSION['supply_date']."','".$entryDate."','".$userId."')";
	execsql($historyQuery);
	

$table_name = $_SESSION['db_prefix']."_tracking_store";
$trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '17', '".$store_item_id."', '".$_SESSION['cust_id']."' ,'".$_SESSION['delivery_no']."','".$_SESSION['job_no']."' )";
$trackingResult = mySQL_query($trackingQuery);	

/*if ($total_quantity<$min_stock) {
	include('../include/min_stock_alert.php');
	}*/
	
if ($total_quantity<=$min_stock) {
	   	if (($sub_category!='Timber') AND ($sub_category!='Mitek') AND ($sub_category!='Sundries')) {
			    $sub_category='Other';
		}		
		include('../include/min_stock_alert.php');
		
		$table_name = $_SESSION['db_prefix']."_min_stock_report";
		$minimum_query = "INSERT INTO $table_name (store_item_id, stock_date, supplied, new_stock_level, report, supplied_to, supply_no ) VALUES ('".$store_item_id."', '".$entryDate."', '".$quantity."', '".$total_quantity."', 'minimum stock', 'Cash delivery', '".$_SESSION['delivery_no']."' ) ";
		execsql($minimum_query);
		
		$email_message = $email_message.'<tr><td>'.$item_name.'</td><td>'.$sub_category.'</td><td align=center>'.$total_quantity.'</td><td align=center>'.$min_stock.'</td></tr>';
	}	
		
	
    $i++;
}


//include('../include/min_stock_email.php');

//if ($email_message!='') {
//		  include('../include/min_stock_email_alert.php');
//	}
		
	if (isset($_GET['new_button']) )
	   {
		 $action='supplyOrderWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='cash_supplied.php?supply_id='.$supply_id.'&pr_option=all';
	  }
	
	
	//$d = date("d");
//	$m = date("m");
//	$y = date("Y");
//	$modDate=date('Y-m-d');
//	$active="1";
	

	
	
	
$table_name = $_SESSION['db_prefix']."_supply_store_lines_temp";
$delete2_query = "DELETE FROM $table_name WHERE  user =  '".$_SESSION['username']."' "; 
execsql($delete2_query);



$_SESSION['job_no'] = '';	
     $_SESSION['supply_date']  = date('Y-m-d');
	 $_SESSION['supply_id'] = '';
	 $_SESSION['cust_name'] = '';
	 $_SESSION['cust_id']='';
	 $_SESSION['delivery_no'] = '';
	 
	 $_SESSION['job_cash'] = '';
	 $_SESSION['type'] = '';
	 $_SESSION['sort'] = '';
	 
	 
	 $_SESSION['type']='';
	 $_SESSION['error']='';	

		$table_name = $_SESSION['db_prefix']."_supply_customer_temp";
		$delete2_query = "DELETE FROM $table_name WHERE  user =  '".$_SESSION['username']."'  "; 
		execsql($delete2_query);
	
?>
<script language="javascript" type="text/javascript">
        
		 window.location="<?php echo $action?>";
        
       </script>