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

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

$table_name = $_SESSION['db_prefix']."_maintenance_additional_supply";
$add_supply_query = "SELECT additional_supply_no  FROM $table_name ORDER BY additional_supply_no DESC LIMIT 1  ";
$add_supply_result = mySQL_query($add_supply_query);
$add_supply_num = mysql_num_rows($add_supply_result);
if ($add_supply_num!=0) {
$additional_supply_no = mysql_result($add_supply_result, 0, "additional_supply_no");
}
else {
	$additional_supply_no=0;
}
$additional_supply_no=$additional_supply_no+1;


$table_name = $_SESSION['db_prefix']."_maintenance_additional_supply";
$supply_query = "INSERT INTO $table_name (additional_supply_no, maintenance_card_no, issue_date,  issued_to, authorised_by ) VALUES ( '".$additional_supply_no."', '".$_SESSION['maintenance_card_no']."', '".$_SESSION['issue_date']."',  '".$_SESSION['issued_to']."', '".$_SESSION['authorised_by']."' ) ";
execsql($supply_query);

$table_name = $_SESSION['db_prefix']."_maintenance_additional_supply";
$supply_id_query = "SELECT additional_supply_no  FROM $table_name ORDER BY additional_supply_no DESC LIMIT 1  ";
$supply_id_result = mySQL_query($supply_id_query);
$additional_supply_no = mysql_result($supply_id_result, 0, "additional_supply_no");

	
$email_message='';
$table_name3 = $_SESSION['db_prefix']."_maintenance_additional_supply_lines_temp";
$items_query = "SELECT * FROM $table_name3 WHERE  user_name =  '".$_SESSION['username']."' ";
$items_result = mySQL_query($items_query);
$items_num = mysql_num_rows($items_result);


$i=0;
while ($i<$items_num) {
   $store_item_id = mysql_result($items_result, $i, "store_item_id");
   $quantity = mysql_result($items_result, $i, "quantity");
   $price = mysql_result($items_result, $i, "price");
   
   
   $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");
   
   
$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['maintenance_card_no']."', maintenance = 'yes', delivery_date = '".$entry_date."', supply_date = '".$entry_date."'  WHERE store_register_id = '".$store_register_id."' ";
execsql($register_query); 
}      
   

   $table_name4 = $_SESSION['db_prefix']."_maintenance_additional_supply_lines";  
   $insert_item_query = "INSERT INTO $table_name4 (maintenance_card_no, additional_supply_no, store_item_id, quantity, price, issue_date, store_reg_id ) VALUES ('".$_SESSION['maintenance_card_no']."', '".$additional_supply_no."', '".$store_item_id."', '".$quantity."', '".$price."', '".$_SESSION['issue_date']."', '".$store_register_id."' ) ";
execsql($insert_item_query);



	$table_name = $_SESSION['db_prefix']."_maintenance_additional_supply_lines";
	$job_query = "INSERT INTO $table_name (maintenance_card_no, store_item_id, item_name, quantity, price, delivered, delivered_price, misc ) VALUES ('".$_SESSION['maintenance_card_no']."', '".$store_item_id."', '".$item_name."', '0', '0.00', '".$quantity."', '".$price."', 'additional' ) ";
	execsql($job_query);


   $total_quantity = $total_quantity - $quantity;
      
   $stockQuery = "UPDATE $table_name2 SET quantity = '".$total_quantity."' , lastModDate = '".$_SESSION['issue_date']."' WHERE store_item_id = '".$store_item_id."'";
	$stockResult = mySQL_query($stockQuery);
	$group_name=$subCategory;
    $supplied_to = $_SESSION['maintenance_card_no']; 
   
   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."', '".$entry_date."', '".$quantity."', '".$total_quantity."', 'minimum stock', 'Additional supply order', '".$additional_supply_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."', '40', '".$_SESSION['maintenance_card_no']."','".$additional_supply_no."', '".$additional_supply_no."', '".$quantity."', '".$total_quantity."', '".$price."', '".$_SESSION['issue_date']."','".$entry_date."','".$_SESSION['username']."')";
$historyResult = mySQL_query($historyQuery);


$i++;
}

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

	
$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']."', '188', '".$additional_supply_no."', '".$_SESSION['maintenance_card_no']."' ,'','' )";
$trackingResult = mySQL_query($trackingQuery);	


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

	
	if (isset($_GET['new_button']) )
	   {
		 $action='maintenance_additionalWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='maintenance_additional_supply.php?additional_supply_no='.$additional_supply_no;
	  }
	
 	$today = date("Y-m-d");
 
     $_SESSION['maintenance_card_no'] = '';	
	 $_SESSION['additional_note'] = '';	
     $_SESSION['issue_date']  = date("Y-m-d");
	 	 
	 $_SESSION['authorised_by' ]= '';
	 $_SESSION['issued_to'] = '';
	 
	 $_SESSION['store_item_id'] = '';
	 $_SESSION['store_item_name'] = '';
	 
	 $_SESSION['purchase_line_id'] = '';
	 $_SESSION['sup_name'] = '';
	 $_SESSION['total'] = '';
	 
	 $_SESSION['value'] = '';
	 
	

	
?>

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

