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

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


$table_name = $_SESSION['db_prefix']."_maintenance_jobs";
$update_query = "UPDATE $table_name SET  start_date = '".$_SESSION['start_date']."', end_date = '".$_SESSION['end_date']."', project_no = '".$_SESSION['project_no']."', project_area = '".$_SESSION['project_area']."', project_place = '".$_SESSION['project_place']."', team = '".$_SESSION['team']."', quote_note = '".$_SESSION['quote_note']."', call_out_charge = '".$_SESSION['call_out_charge']."', additional_charge = '".$_SESSION['additional_charge']."', card_opened='yes'  WHERE maintenance_no = '".$_SESSION['maintenance_no']."' ";
execsql($update_query);

$table_name = $_SESSION['db_prefix']."_maintenance_invoices";
$invoice_query = "UPDATE $table_name SET maintenance_card_no = '".$_SESSION['maintenance_card_no']."' WHERE invoice_no  = '".$_SESSION['invoice_no']."' ";
execsql($invoice_query);


$table_name3 = $_SESSION['db_prefix']."_maintenance_job_lines_supply_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);
$stock_line='';
$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, quantity FROM $table_name2 WHERE store_item_id = '".$store_item_id."'";
   $stock_total_result = mySQL_query($stock_total_query);
   $total_quantity = mysql_result($stock_total_result ,0, "quantity");
   $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 = '".$entryDate."', supply_date = '".$entryDate."'  WHERE store_register_id = '".$store_register_id."' ";
execsql($register_query); 
}   

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


   $total_quantity = $total_quantity - $quantity;
      
   $stockQuery = "UPDATE $table_name2 SET quantity = '".$total_quantity."' , lastModDate = '".$_SESSION['start_date']."' WHERE store_item_id = '".$store_item_id."'";
   $stockResult = mySQL_query($stockQuery);


			
	

$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."', '38', '".$_SESSION['maintenance_card_no']."','maintenance', '', '".$quantity."', '".$total_quantity."', '".$price."', '".$_SESSION['start_date']."','".$entryDate."','".$_SESSION['username']."')";
$historyResult = mySQL_query($historyQuery);


$i++;
}

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

$table_name = $_SESSION['db_prefix']."_tracking_maintenance";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '124', '".$_SESSION['maintenance_card_no']."', '".$_SESSION['project']."' ,'".$_SESSION['cust_id']."','".$_SESSION['start_date']."' )";
	$trackingResult = mySQL_query($trackingQuery);	


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

$maintenance_card_no = $_SESSION['maintenance_card_no'];
	 	 

$_SESSION['maintenance_card_no']="";
	 $_SESSION['maintenance_no']='';	 
	 
	 $_SESSION['name']="";
	 $_SESSION['start_date']=date("Y-m-d");
	 $_SESSION['end_date']=date("Y-m-d");
	 
	 
	 	
	 	$_SESSION['quote_note'] = '';
		$_SESSION['maintenance_card_no'] = '';
		$_SESSION['cust_id'] = '';
		$_SESSION['order_date'] ='';
		$_SESSION['project_description'] = '';
		$_SESSION['project_no'] = '';
		$_SESSION['project_place'] = '';
		$_SESSION['project_area'] = '';
		$_SESSION['start_date'] = '';
		$_SESSION['value'] = '';
		
		$_SESSION['call_out_charge'] = '';
	 	$_SESSION['additional_charge'] = '';
		$_SESSION['invoice_no'] = '';
		
		$_SESSION['team'] = '';
		$_SESSION['all_total'] = '';
		
		
?>

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

