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

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

$table_name = $_SESSION['db_prefix']."_supply_job";
$number_query = "SELECT supply_job_no FROM $table_name  ORDER BY supply_job_no DESC  LIMIT 1";
$number_result = mySQL_query($number_query);
$number_num = mysql_num_rows($number_result);
if ($number_num==0) {
	$supply_job_no = 0;
}
else {
$supply_job_no = mysql_result($number_result, 0, "supply_job_no");
}
$supply_job_no = $supply_job_no +1;


$table_name = $_SESSION['db_prefix']."_supply_job";
$supply_note_query = "INSERT INTO $table_name (supply_job_no, job_card_no, supply_date) VALUES ('".$supply_job_no."', '".$_SESSION['job_card_no']."', '".$entry_date."') ";
execsql($supply_note_query);

$table_name = $_SESSION['db_prefix']."_supply_job";
$supply_id_query = "SELECT supply_id FROM $table_name WHERE job_card_no = '".$_SESSION['job_card_no']."' ";
$supply_id_result = mySQL_query($supply_id_query);
$supply_id_num = mysql_num_rows($supply_id_result);
$supply_id = mysql_result($supply_id_result, 0, "supply_id");


$table_name = $_SESSION['db_prefix']."_order_job_lines_supply_temp";
$supply_query = "SELECT order_line_id, store_item_id, quantity, price FROM $table_name WHERE job_card_no = '".$_SESSION['job_card_no']."' AND user_name = '".$_SESSION['username']."' ";
$supply_result = mySQL_query($supply_query);
$supply_num = mysql_num_rows($supply_result);

$email_message='';
$i=0;
while ($i<$supply_num) {
	$store_item_id = mysql_result($supply_result, $i, "store_item_id");
	$quantity = mysql_result($supply_result, $i, "quantity");
	$price = mysql_result($supply_result, $i, "price");
	$order_line_id = mysql_result($supply_result, $i, "order_line_id");
	
$register = $_GET['register_text'.$i];
$store_register_id= '';	
if ($register == 'yes') {
$serial = $_GET['serial_select'.$i];

$table_name = $_SESSION['db_prefix']."_store_register";
$reg_id_query = "SELECT store_register_id FROM $table_name WHERE serial_number = '".$serial."' AND store_item_id = '".$store_item_id."'";
$reg_id_result = mySQL_query($reg_id_query);
$reg_id_num = mysql_num_rows($reg_id_result);
$store_register_id = mysql_result($reg_id_result, 0, "store_register_id");


$table_name = $_SESSION['db_prefix']."_store_register";
$register_query = "UPDATE $table_name SET job_card = '".$_SESSION['job_card_no']."', maintenance = '', delivery_date = '".$entry_date."', supply_date = '".$entry_date."'  WHERE store_register_id = '".$store_register_id."' ";
execsql($register_query); 
}	
	
	
	if ($quantity>0) {
	$table_name = $_SESSION['db_prefix']."_supply_job_lines";
	$supply_lines_query = "INSERT INTO $table_name (supply_job_no, job_card_no, store_item_id, item_name, delivered, delivered_price, store_reg_id  ) VALUES ('".$supply_job_no."', '".$_SESSION['job_card_no']."',  '".$store_item_id."', '', '".$quantity."', '".$price."', '".$store_register_id."'  ) ";
	execsql($supply_lines_query);
	}

	
	
	$table_name = $_SESSION['db_prefix']."_order_job_lines";
	$update_query = "UPDATE $table_name SET delivered = delivered+'".$quantity."', delivered_price = '".$price."', store_reg_id = '".$store_register_id."'  WHERE order_line_id = '".$order_line_id."' ";
	execsql($update_query);
	
if  (($store_item_id>0) AND($store_item_id<100000)) {	
	$table_name2 = $_SESSION['db_prefix']."_store_items";
   $stock_total_query = "SELECT item_name, subCategory, quantity, minimumStock FROM $table_name2 WHERE store_item_id = '".$store_item_id."'";
   $stock_total_result = mySQL_query($stock_total_query);
   $subCategory = mysql_result($stock_total_result ,0, "subCategory");
   $total_quantity = mysql_result($stock_total_result ,0, "quantity");
   $minimumStock = mysql_result($stock_total_result ,0, "minimumStock");
   $item_name = mysql_result($stock_total_result, 0, "item_name");
	
    $total_quantity = $total_quantity - $quantity;
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$update_query = "UPDATE $table_name SET quantity = '".$total_quantity."', lastModDate = '".$entry_date."' WHERE store_item_id = '".$store_item_id."' ";
	execsql($update_query);
	$group_name=$subCategory;
    $supplied_to = $_SESSION['job_card_no']; 
	
	
if ($quantity>0) {   
   if ($total_quantity<=$minimumStock) {
	   	if (($subCategory!='Electric fencing') AND ($subCategory!='Fencing') AND ($subCategory!='Palisade') AND ($subCategory!='M-works')) {
			    $subCategory='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', 'Supply order', '".$supply_job_no."' ) ";
		execsql($minimum_query);
		
		$email_message = $email_message.'<tr><td>'.$item_name.'</td><td>'.$group_name.'</td><td align=center>'.$total_quantity.'</td><td align=center>'.$minimumStock.'</td></tr>';
	}
}

	
	$table_name2 = $_SESSION['db_prefix']."_item_history";	
	$historyQuery = "INSERT INTO $table_name2 (store_item_id, entry, reference, reference_2, reference_3, quantity, total, price, date, entryDate, userId) VALUES ('".$store_item_id."', '32', '".$_SESSION['job_card_no']."','".$store_item_id."', '".$supply_job_no."', '".$quantity."', '".$total_quantity."', '".$price."', '".$_SESSION['deliver_date']."','".$entry_date."','".$_SESSION['username']."')";
	$historyResult = mySQL_query($historyQuery);
}

if ($store_item_id>100000) {	
	$table_name = $_SESSION['db_prefix']."_store_items_special";
	$update_query = "UPDATE $table_name SET supplied_quantity = supplied_quantity+'".$quantity."', supply_date = '".$_SESSION['deliver_date']."' WHERE special_store_item_id = '".$store_item_id."' ";
	execsql($update_query);


$table_name = $_SESSION['db_prefix']."_store_items_special";
$special_order_id_query = "SELECT special_order_id  FROM $table_name WHERE special_store_item_id = '".$store_item_id."' ";
$special_order_id_result = mySQL_query($special_order_id_query);
$special_order_id = mysql_result($special_order_id_result, 0, "special_order_id");

$table_name = $_SESSION['db_prefix']."_special_orders";
$special_update_query = "UPDATE $table_name SET date_supplied = '".$_SESSION['deliver_date']."', quantity_supplied = quantity_supplied+'".$quantity."'  WHERE special_order_id = '".$special_order_id."' ";
execsql($special_update_query);

}

if ($store_item_id=0) {	
	//$table_name = $_SESSION['db_prefix']."_store_items_special";
//	$update_query = "UPDATE $table_name SET supplied = '".$quantity."', supply_date = '".$entry_date."' WHERE special_store_item_id = '".$store_item_id."' ";
//	execsql($update_query);
}
	
		
	$i++;
}

if ($email_message!='') {
		  include('../include/min_stock_email_alert.php');
	}

$table_name = $_SESSION['db_prefix']."_mworks_job_components_temp";
$comp_query = "SELECT * FROM $table_name WHERE job_card_no = '".$_SESSION['job_card_no']."' AND user_name = '".$_SESSION['username']."' ";
$comp_result = mySQL_query($comp_query);
$comp_num = mysql_num_rows($comp_result);
$i=0;
while ($i<$comp_num) {
   	$component = mysql_result($comp_result, $i, "component");
   	$quantity = mysql_result($comp_result, $i, "quantity");	
	
	$table_name = $_SESSION['db_prefix']."_mworks_job_components";
	$update_query = "UPDATE $table_name SET quantity_delivered = quantity_delivered+'".$quantity."' WHERE job_card_no = '".$_SESSION['job_card_no']."' AND component = '".$component."' ";
	execsql($update_query);
   
   	$table_name = $_SESSION['db_prefix']."_supply_job_lines";
	$supply_lines_query = "INSERT INTO $table_name (supply_job_no, job_card_no, item_name, delivered ) VALUES ('".$supply_job_no."', '".$_SESSION['job_card_no']."',  '".$component."',  '".$quantity."' ) ";
	execsql($supply_lines_query);
	
	$i++;	
	}


$table_name = $_SESSION['db_prefix']."_tracking_store";
$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '84', '".$_SESSION['cust_id']."', '".$_SESSION['job_card_no']."' ,'".$_SESSION['forman_name']."','' )";
$trackingResult = mySQL_query($trackingQuery);

$table_name6 = $_SESSION['db_prefix']."_order_history";
    $history_query = "INSERT INTO $table_name6 (job_card_number, note_date, note_text, user_name ) VALUES ('".$_SESSION['job_card_no']."', '".$entry_date."', 'Supplies from store on supply no ".$supply_job_no.".', '".$_SESSION['username']."' ) ";
	execsql($history_query);


$table_name = $_SESSION['db_prefix']."_order_job_lines";
$supplied_query = "SELECT store_item_id FROM $table_name WHERE job_card_no = '".$_SESSION['job_card_no']."' AND (quantity-delivered)>0 ";
$supplied_result = mySQL_query($supplied_query);
$supplied_num = mysql_num_rows($supplied_result);

$table_name = $_SESSION['db_prefix']."_mworks_job_components";
$comp_supplied_query = "SELECT component FROM $table_name WHERE job_card_no = '".$_SESSION['job_card_no']."' AND (quantity-quantity_delivered)>0 ";
$comp_supplied_result = mySQL_query($comp_supplied_query);
$comp_supplied_num = mysql_num_rows($comp_supplied_result);

if (($supplied_num == 0) AND ($comp_supplied_num == 0)) { 
		$supplied = 'yes';
}
else {
	$supplied =  'part';
}




$table_name = $_SESSION['db_prefix']."_compiled_jobs";
$update_query = "UPDATE $table_name SET  supplied='".$supplied."'  WHERE job_card_no = '".$_SESSION['job_card_no']."' ";
execsql($update_query);

//

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

$table_name = $_SESSION['db_prefix']."_mworks_job_components_temp";
$delete_query = "DELETE FROM $table_name WHERE  user_name =  '".$_SESSION['username']."' "; 
execsql($delete_query);



$action='supply_job.php?job_card_no='.$_SESSION['job_card_no'].'&supply_job_no='.$supply_job_no ;

	

	 	 
	 $_SESSION['job_card_no']='';
	 $_SESSION['name']="";
	 $_SESSION['deliver_date']=date("Y-m-d");
	 $_SESSION['cust_id']='';
	 		
		$_SESSION['cust_id']='';
		$_SESSION['order_date']='';
		$_SESSION['project_description']='';
		$_SESSION['project_no']='';
		$_SESSION['project_place']='';
		$_SESSION['project_area']='';
		$_SESSION['deliver_date']='';
		$_SESSION['work_days']='';
		$_SESSION['value']='';
		$_SESSION['deliver_date']= date("Y-m-d");

        $_SESSION['forman']='';
		$_SESSION['forman_name']='';
	 	$_SESSION['labour']='';
	 	$_SESSION['travel']='';
	 	$_SESSION['accomodation']='';
		$_SESSION['mworks_charge']='';
		$_SESSION['mworks']='';
		
		$_SESSION['all_total'] = '';
		
		$_SESSION['stock_error']=0;
		
				
		$table_name = $_SESSION['db_prefix']."_mworks_job_components_temp";
		$delete_query = "DELETE FROM $table_name WHERE  user_name =  '".$_SESSION['username']."' "; 
		execsql($delete_query);
		
			
		$table_name = $_SESSION['db_prefix']."_order_job_lines_supply_temp";
		$delete2_query = "DELETE FROM $table_name WHERE  user_name =  '".$_SESSION['username']."' "; 
		execsql($delete2_query);
	 
	 	
?>

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

