<?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 = $con->query($items_query);
$row = mysqli_fetch_assoc($items_result);
$price = $row["price"];


if ($_SESSION['supply_group']=='employee') {
$table_name = $_SESSION['db_prefix']."_employee_supply";
$emp_supply_query = "SELECT emp_supply_no  FROM $table_name ORDER BY emp_supply_no DESC LIMIT 1  ";
$emp_supply_result = $con->query($emp_supply_query);
$emp_supply_num = mysqli_num_rows($emp_supply_result);
if ($emp_supply_num!=0) {
$row = mysqli_fetch_assoc($emp_supply_result);
$emp_supply_no = $row["emp_supply_no"];
}
else {
	$emp_supply_no=0;
}
$emp_supply_no=$emp_supply_no+1;



$table_name = $_SESSION['db_prefix']."_employee_supply";
$supply_query = "INSERT INTO $table_name (emp_id, emp_supply_no, status, issue_date, issued_by, authorised_by ) VALUES ('".$_SESSION['supply_to']."', '".$emp_supply_no."', 'S', '".$_SESSION['issue_date']."',  '".$_SESSION['issued_by']."', '".$_SESSION['authorised_by']."' ) ";
$supply_result = $con->query($supply_query);

$table_name = $_SESSION['db_prefix']."_employee_supply";
$supply_id_query = "SELECT emp_supply_no  FROM $table_name ORDER BY emp_supply_no DESC LIMIT 1  ";
$supply_id_result = $con->query($supply_id_query);
$row = mysqli_fetch_assoc($supply_id_result);
$emp_supply_no = $row["emp_supply_no"];


$table_name4 = $_SESSION['db_prefix']."_employee_supply_lines";  
$insert_item_query = "INSERT INTO $table_name4 (emp_id, emp_supply_no, status, store_item_id, quantity, price, issue_date ) VALUES ('".$_SESSION['supply_to']."', '".$emp_supply_no."', 'S', '".$_SESSION['store_item_id']."', '".$_SESSION['supply_qty']."', '".$price."', '".$_SESSION['issue_date']."' ) ";
$insert_item_result = $con->query($insert_item_query);
}


if ($_SESSION['supply_group']=='department') {
$table_name = $_SESSION['db_prefix']."_department_supply";
$dep_supply_query = "SELECT dep_supply_no  FROM $table_name ORDER BY dep_supply_no DESC LIMIT 1  ";
$dep_supply_result = $con->query($dep_supply_query);
$dep_supply_num = mysqli_num_rows($dep_supply_result);
if ($dep_supply_num!=0) {
$row = mysqli_fetch_assoc($dep_supply_result);
$dep_supply_no = $row["dep_supply_no"];
}
else {
	$dep_supply_no=0;
}
$dep_supply_no=$dep_supply_no+1;


$table_name = $_SESSION['db_prefix']."_department_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']."' ) ";
$supply_result = $con->query($supply_query);

$table_name = $_SESSION['db_prefix']."_department_supply";
$supply_id_query = "SELECT dep_supply_no  FROM $table_name ORDER BY dep_supply_no DESC LIMIT 1  ";
$supply_id_result = $con->query($supply_id_query);
$row = mysqli_fetch_assoc($supply_id_result);
$dep_supply_no = $row["dep_supply_no"];


$table_name4 = $_SESSION['db_prefix']."_department_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']."' ) ";
$insert_item_result = $con->query($insert_item_query);
}



	$stock_alert = 'no';
	$alert_array = array();
	$alert2_array = array();
	$alert_text = '';  

    
   $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 = $con->query($stock_total_query);
   $row = mysqli_fetch_assoc($stock_total_result);
   $subCategory = $row["subCategory"];
   $total_quantity = $row["quantity"];
   $minimumStock = $row["minimumStock"];
   $item_name = $row["item_name"];
	
   $total_quantity = $total_quantity - $_SESSION['supply_qty'];
   
   if ($minimumStock > $total_quantity) {
	$stock_alert = 'yes';
	$alert_array[] = $_SESSION['store_item_id'];
	$alert2_array[] = $total_quantity;
	
	$alert_text = $alert_text.'General stock quantity for <b>'.$item_name.'</b> is below the minimum stock of <b>'.$minimumStock.'</b>.<br>The present stock quantity is now <b>'.$total_quantity.'</b>.<br><br>';
	}
      
   $stockQuery = "UPDATE $table_name2 SET quantity = '".$total_quantity."' , lastModDate = '".$_SESSION['issue_date']."' WHERE store_item_id = '".$_SESSION['store_item_id']."'";
   $stockResult = $con->query($stockQuery);
   $group_name=$subCategory;
	//$supplied_to=$_SESSION['employee_name'];	
	
	
		
	//if (($_SESSION['supply_group']=='employee') OR ($_SESSION['supply_group']=='department')) {$company = ''; } else { $company = ''; }
	
	if ($_SESSION['supply_group']=='employee') { $supply_no = $emp_supply_no ; }
	if ($_SESSION['supply_group']=='department') { $supply_no = $dep_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) 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']."', '', '".$_SESSION['supply_reason']."' ) ";
	$internal_supply_result = $con->query($internal_supply_query);
	
	
if ($_SESSION['supply_group']=='employee') {	
	$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']."', '16', '".$_SESSION['store_item_id']."', '".$_SESSION['supply_to']."' ,'".$emp_supply_no."','' )";
	$tracking_result = $con->query($tracking_query);
}

if ($_SESSION['supply_group']=='department') {	
	$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']."', '15', '".$_SESSION['store_item_id']."', '".$_SESSION['supply_to']."', '".$_SESSION['received_by']."', '".$dep_supply_no."' )";
	$tracking_result = $con->query($tracking_query);
}

	
if ($_SESSION['supply_group']=='employee') {
$table_name2 = $_SESSION['db_prefix']."_item_history";	
$history_query = "INSERT INTO $table_name2 (store_item_id, entry, reference, reference_2, reference_3, quantity, total, price, date, entryDate, userId) VALUES ('".$_SESSION['store_item_id']."', '14', '".$_SESSION['supply_to']."','".$emp_supply_no."', '".$emp_supply_no."', '".$_SESSION['supply_qty']."', '".$total_quantity."', '".$price."', '".$_SESSION['issue_date']."','".$entry_date."','".$_SESSION['username']."')";
$history_result = $con->query($history_query);
}

if ($_SESSION['supply_group']=='department') {
$table_name2 = $_SESSION['db_prefix']."_item_history";	
$history_query = "INSERT INTO $table_name2 (store_item_id, entry, reference, reference_2, reference_3, quantity, total, price, date, entryDate, userId) VALUES ('".$_SESSION['store_item_id']."', '15', '".$_SESSION['supply_to']."','".$dep_supply_no."', '".$dep_supply_no."', '".$_SESSION['supply_qty']."', '".$total_quantity."', '".$price."', '".$_SESSION['issue_date']."','".$entry_date."','".$_SESSION['username']."')";
$history_result = $con->query($history_query);
}

if ($stock_alert=='yes') {
		include('../include/min_general_stock_alert.php');
}

	
if (isset($_GET['new_button']) )
	   {
		 $action='supply_internalWiz0.php';
	   }
		 
if (isset($_GET['finish_button']) )
	  {
		if ($_SESSION['supply_group']=='employee') { $action='employee_supply.php?emp_supply_no='.$emp_supply_no; }
		if ($_SESSION['supply_group']=='department') { $action='department_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>

