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

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

$table_name = $_SESSION['db_prefix']."_store_items";
$items_query = "SELECT price FROM $table_name WHERE  store_item_id = '".$_SESSION['store_item_id']."' ";
$items_result = mySQL_query($items_query);
$price = mysql_result($items_result, 0, "price");


if ($_SESSION['supply_group']=='vehicle') {
$table_name = $_SESSION['db_prefix']."_vehicle_supply";
$veh_supply_query = "SELECT veh_supply_no  FROM $table_name ORDER BY veh_supply_no DESC LIMIT 1  ";
$veh_supply_result = mySQL_query($veh_supply_query);
$veh_supply_num = mysql_num_rows($veh_supply_result);
if ($veh_supply_num!=0) {
$veh_supply_no = mysql_result($veh_supply_result, 0, "veh_supply_no");
}
else {
	$veh_supply_no=0;
}
$veh_supply_no=$veh_supply_no+1;


$table_name = $_SESSION['db_prefix']."_vehicle_supply";
$supply_query = "INSERT INTO $table_name (veh_id, veh_supply_no, status, issue_date, issued_by, authorised_by, received_by, clock_reading ) VALUES ('".$_SESSION['supply_to']."', '".$veh_supply_no."', 'S', '".$_SESSION['issue_date']."',  '".$_SESSION['issued_by']."', '".$_SESSION['authorised_by']."', '".$_SESSION['received_by']."', '".$_SESSION['clock_reading']."' ) ";
execsql($supply_query);

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

$table_name4 = $_SESSION['db_prefix']."_vehicle_supply_lines";  
$insert_item_query = "INSERT INTO $table_name4 (veh_id, veh_supply_no, status, store_item_id, quantity, price, issue_date ) VALUES ('".$_SESSION['supply_to']."', '".$veh_supply_no."', 'S', '".$_SESSION['store_item_id']."', '".$_SESSION['supply_qty']."', '".$price."', '".$_SESSION['issue_date']."' ) ";
execsql($insert_item_query);
}


if ($_SESSION['supply_group']=='machinery') {
$table_name = $_SESSION['db_prefix']."_machine_supply";
$mach_supply_query = "SELECT mach_supply_no  FROM $table_name ORDER BY mach_supply_no DESC LIMIT 1  ";
$mach_supply_result = mySQL_query($mach_supply_query);
$mach_supply_num = mysql_num_rows($mach_supply_result);
if ($mach_supply_num!=0) {
$mach_supply_no = mysql_result($mach_supply_result, 0, "mach_supply_no");
}
else {
	$mach_supply_no=0;
}
$mach_supply_no=$mach_supply_no+1;


$table_name = $_SESSION['db_prefix']."_machine_supply";
$supply_query = "INSERT INTO $table_name (mach_id, mach_supply_no, status, issue_date, issued_by, authorised_by, received_by, clock_reading ) VALUES ('".$_SESSION['supply_to']."', '".$mach_supply_no."', 'S', '".$_SESSION['issue_date']."',  '".$_SESSION['issued_by']."', '".$_SESSION['authorised_by']."', '".$_SESSION['received_by']."', '".$_SESSION['clock_reading']."' ) ";
execsql($supply_query);

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

$table_name4 = $_SESSION['db_prefix']."_machine_supply_lines";  
$insert_item_query = "INSERT INTO $table_name4 (mach_id, mach_supply_no, status, store_item_id, quantity, price, issue_date ) VALUES ('".$_SESSION['supply_to']."', '".$mach_supply_no."', 'S', '".$_SESSION['store_item_id']."', '".$_SESSION['supply_qty']."', '".$price."', '".$_SESSION['issue_date']."' ) ";
execsql($insert_item_query);
}


if ($_SESSION['supply_group']=='company') {
$table_name = $_SESSION['db_prefix']."_company_supply";
$comp_supply_query = "SELECT dep_supply_no  FROM $table_name ORDER BY dep_supply_no DESC LIMIT 1  ";
$comp_supply_result = mySQL_query($comp_supply_query);
$comp_supply_num = mysql_num_rows($comp_supply_result);
if ($comp_supply_num!=0) {
$dep_supply_no = mysql_result($comp_supply_result, 0, "dep_supply_no");
}
else {
	$dep_supply_no=0;
}
$dep_supply_no=$dep_supply_no+1;


$table_name = $_SESSION['db_prefix']."_company_supply";
$supply_query = "INSERT INTO $table_name (dep_supply_no, department, supplied_to, issue_date, issued_by, authorised_by ) VALUES ( '".$dep_supply_no."', '".$_SESSION['supply_to']."','".$_SESSION['received_by']."', '".$_SESSION['issue_date']."',  '".$_SESSION['issued_by']."', '".$_SESSION['authorised_by']."' ) ";
execsql($supply_query);

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

$table_name4 = $_SESSION['db_prefix']."_company_supply_lines";  
$insert_item_query = "INSERT INTO $table_name4 (dep_supply_no, department, store_item_id, quantity, price, issue_date ) VALUES ('".$dep_supply_no."', '".$_SESSION['supply_to']."',  '".$_SESSION['store_item_id']."', '".$_SESSION['supply_qty']."', '".$price."', '".$_SESSION['issue_date']."' ) ";
execsql($insert_item_query);
}

  

    
   $table_name2 = $_SESSION['db_prefix']."_store_items";
   $stock_total_query = "SELECT item_name, subCategory, quantity, minimumStock FROM $table_name2 WHERE store_item_id = '".$_SESSION['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 - $_SESSION['supply_qty'];
      
   $stockQuery = "UPDATE $table_name2 SET quantity = '".$total_quantity."' , lastModDate = '".$_SESSION['issue_date']."' WHERE store_item_id = '".$_SESSION['store_item_id']."'";
   	$stockResult = mySQL_query($stockQuery);
	$group_name=$subCategory;
	
	
	if ($total_quantity<=$minimumStock) {
	   	$store_item_id = $_SESSION['store_item_id'];	
		include('../include/min_stock_alert.php');
		$message_text = '<tr><td>'.$item_name.'</td><td>'.$group_name.'</td><td align=center>'.$total_quantity.'</td><td align=center>'.$minimumStock.'</td></tr>';
	}
	
	if ($_SESSION['supply_group']=='vehicle') {
			$table_name = $_SESSION['db_prefix']."_vehicle";
			$comp_query = "SELECT  department    FROM $table_name WHERE vehicle_id = '".$_SESSION['supply_to']."' ";
			$comp_result = mySQL_query($comp_query);
			$company = mysql_result($comp_result, 0, "department");
	}
	
	if ($_SESSION['supply_group']=='machinery') {
			$table_name = $_SESSION['db_prefix']."_machinery";
			$comp_query = "SELECT  department    FROM $table_name WHERE machine_id = '".$_SESSION['supply_to']."' ";
			$comp_result = mySQL_query($comp_query);
			$company = mysql_result($comp_result, 0, "department");
	}
	
	if ($_SESSION['supply_group']=='vehicle') { $supply_no = $veh_supply_no ; }
	if ($_SESSION['supply_group']=='machinery') { $supply_no = $mach_supply_no ; }
			
	$table_name = $_SESSION['db_prefix']."_internal_supply";
	$internal_supply_query = "INSERT INTO $table_name (supply_date, supply_no, store_item_id, store_item_name, quantity, price, supply_group, issued_to, received_by, company, supply_reason, clock_reading) VALUES ('".$_SESSION['issue_date']."', '".$supply_no."', '".$_SESSION['store_item_id']."', '".$item_name."', '".$_SESSION['supply_qty']."', '".$price."', '".$_SESSION['supply_group']."', '".$_SESSION['supply_to']."', '".$_SESSION['received_by']."', '".$company."', '".$_SESSION['supply_reason']."', '".$_SESSION['clock_reading']."' ) ";
	execsql($internal_supply_query);
	
	
if ($_SESSION['supply_group']=='vehicle') {	
	$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']."', '36', '".$_SESSION['store_item_id']."', '".$_SESSION['supply_to']."' ,'".$veh_supply_no."','' )";
	$trackingResult = mySQL_query($trackingQuery);
}

if ($_SESSION['supply_group']=='machinery') {	
	$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']."', '37', '".$_SESSION['store_item_id']."', '".$_SESSION['supply_to']."' ,'".$_SESSION['received_by']."','' )";
	$trackingResult = mySQL_query($trackingQuery);
}

if ($_SESSION['supply_group']=='company') {	
	$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']."', '22', '".$_SESSION['store_item_id']."', '".$_SESSION['supply_to']."' ,'".$_SESSION['received_by']."','' )";
	$trackingResult = mySQL_query($trackingQuery);
}

if ($_SESSION['supply_group']=='vehicle') {	
	$table_name = $_SESSION['db_prefix']."_vehicle_history";
	$historyQuery = "INSERT INTO $table_name (vehicle_id, history_date, store_item_id, cost, clock_reading, supply_reason, veh_supply_no) VALUES ('".$_SESSION['supply_to']."' ,'".$entry_date."', '".$_SESSION['store_item_id']."', '".$price."', '".$_SESSION['clock_reading']."', '".$_SESSION['supply_reason']."', '".$veh_supply_no."')";
	$historyResult = mySQL_query($historyQuery);
}
	
if ($_SESSION['supply_group']=='vehicle') {
$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 ('".$_SESSION['store_item_id']."', '36', '".$_SESSION['supply_to']."','".$veh_supply_no."', '".$veh_supply_no."', '".$_SESSION['supply_qty']."', '".$total_quantity."', '".$price."', '".$_SESSION['issue_date']."','".$entry_date."','".$_SESSION['username']."')";
$historyResult = mySQL_query($historyQuery);
}


if ($_SESSION['supply_group']=='machinery') {	
	$table_name = $_SESSION['db_prefix']."_machinery_history";
	$historyQuery = "INSERT INTO $table_name (machine_id, history_date, store_item_id, cost, clock_reading, supply_reason, mach_supply_no) VALUES ('".$_SESSION['supply_to']."' ,'".$entry_date."', '".$_SESSION['store_item_id']."', '".$price."', '".$_SESSION['clock_reading']."', '".$_SESSION['supply_reason']."', '".$mach_supply_no."')";
	$historyResult = mySQL_query($historyQuery);
}

if ($_SESSION['supply_group']=='machinery') {
$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 ('".$_SESSION['store_item_id']."', '37', '".$_SESSION['supply_to']."','".$mach_supply_no."', '".$mach_supply_no."', '".$_SESSION['supply_qty']."', '".$total_quantity."', '".$price."', '".$_SESSION['issue_date']."','".$entry_date."','".$_SESSION['username']."')";
$historyResult = mySQL_query($historyQuery);
}

if ($_SESSION['supply_group']=='company') {
$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 ('".$_SESSION['store_item_id']."', '19', '".$_SESSION['supply_to']."','".$dep_supply_no."', '".$dep_supply_no."', '".$_SESSION['supply_qty']."', '".$total_quantity."', '".$price."', '".$_SESSION['issue_date']."','".$entry_date."','".$_SESSION['username']."')";
$historyResult = mySQL_query($historyQuery);
}

	
if ($message_text!='') {
	   include('../include/min_stock_alert_email.php');
	}	


if (isset($_GET['new_button']) )
	   {
		$action='supply_internalWiz0.php';
	   }
		 
if (isset($_GET['finish_button']) )
	  {
		if ($_SESSION['supply_group']=='vehicle') { $action='vehicle_supply.php?veh_supply_no='.$veh_supply_no; }
		if ($_SESSION['supply_group']=='machinery') { $action='machinery_supply.php?mach_supply_no='.$mach_supply_no; }
		if ($_SESSION['supply_group']=='company') { $action='company_supply.php?dep_supply_no='.$dep_supply_no; }
	  }
	
	 $today = date("Y-m-d");
	 $_SESSION['stores_group'] = '';
	 $_SESSION['supply_group'] = '';
	 $_SESSION['emp_id'] = '';
	 $_SESSION['emp_name'] = '';
	 $_SESSION['vehicle_id'] = '';
	 $_SESSION['vehicle_name'] = '';
	 $_SESSION['clock_reading'] = '';
	 $_SESSION['last_reading'] = '';
	 $_SESSION['clock'] = '';
	 $_SESSION['company'] = '';
	 $_SESSION['external'] = '';
	 $_SESSION['order_book'] = '';
	 $_SESSION['machine_id'] = '';
	 $_SESSION['machine_name'] = '';
	 $_SESSION['store_item_id'] = '';
	 $_SESSION['store_item_name'] = '';	
	 $_SESSION['available'] = '';
	 $_SESSION['supply_to'] = '';	
	 $_SESSION['supply_qty'] = ''; '';
	 $_SESSION['supply_reason'] = '';
	 $_SESSION['available_stock'] = '';
	 $_SESSION['department'] = '';
	 $_SESSION['received_by'] = '';
	 $_SESSION['del_note'] = '';	
     $_SESSION['issue_date']  = date("Y-m-d");
	 $_SESSION['authorised_by' ]= '';
	 $_SESSION['issued_by'] = '';
	 $_SESSION['store_item_id'] = '';
	 $_SESSION['store_item_name'] = '';
	 $_SESSION['pl'] = '0';
	
?>

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

