<?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 card_closed='yes'  WHERE maintenance_card_no = '".$_SESSION['maintenance_card_no']."' ";
execsql($update_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");

   $table_name = $_SESSION['db_prefix']."_team_stock";
   $stock_query = "SELECT total_quantity FROM $table_name WHERE store_item_id = '".$store_item_id."' AND team_id = '".$_SESSION['team']."' ";
   $stock_result = mySQL_query($stock_query);
   $stock = mysql_result($stock_result, 0, "total_quantity");	

	$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 ) VALUES ('".$_SESSION['maintenance_card_no']."', '".$store_item_id."', '".$item_name."', '0', '0.00', '".$quantity."', '".$price."' ) ";
	execsql($job_query);

 	$table_name4 = $_SESSION['db_prefix']."_team_stock_lines";  
   	$insert_item_query = "INSERT INTO $table_name4 (team_supply_no, team_id, store_item_id, del_quantity, price, deliver_date, issued_by ) VALUES ('0',  '".$_SESSION['team']."',  '".$store_item_id."', '".$quantity."', '".$price."', '".$_SESSION['start_date']."', '".$_SESSION['username']."' ) ";
  	execsql($insert_item_query);


   $total_quantity = $stock - $quantity;
   
   $table_name3 = $_SESSION['db_prefix']."_team_stock";   
   $stockQuery = "UPDATE $table_name3 SET total_quantity = '".$total_quantity."'   WHERE store_item_id = '".$store_item_id."' AND team_id = '".$_SESSION['team']."' ";
   $stockResult = mySQL_query($stockQuery);

   


$i++;
}

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

$maintenance_card_no = $_SESSION['maintenance_card_no'];

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

	 	 
	 	 $_SESSION['maintenance_card_no']="";
	 	$_SESSION['maintenance_no']="";	 
	 	$_SESSION['name']="";
	 	$_SESSION['start_date']=date("Y-m-d");
	 	$_SESSION['end_date']=date("Y-m-d");
	  	
		
		$_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['team']="";

        $_SESSION['all_total'] = '';
	 
	
	
?>

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

finished