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

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





$table_name = $_SESSION['db_prefix']."_mworks_job_components";
$component_query = "SELECT component, quantity, produced_quantity, quantity_delivered, notes FROM $table_name WHERE job_card_no = '".$_SESSION['job_card_no']."' ";
$component_result = mySQL_query($component_query);
$component_num = mysql_num_rows($component_result);	


$table_name = $_SESSION['db_prefix']."_mworks_delivered";
$deliver_query = "INSERT INTO $table_name (job_card_no, cust_id, del_date, vehicle_id ) VALUES ('".$_SESSION['job_card_no']."', '".$_SESSION['cust_id']."', '".$_SESSION['deliver_date']."', '".$_SESSION['vehicle_id']."' ) ";
execsql($deliver_query);

$table_name = $_SESSION['db_prefix']."_mworks_delivered";
$id_query = "SELECT m_delivered_id  FROM $table_name  ORDER BY  m_delivered_id DESC LIMIT 1";
$id_result = mySQL_query($id_query);
$m_delivered_id = mysql_result($id_result, 0, "m_delivered_id");


$i=0;
while ($i<$component_num) {
$component = mysql_result($component_result, $i, "component");	
$table_name = $_SESSION['db_prefix']."_mworks_job_components";
$update_query = "UPDATE $table_name SET quantity_delivered = quantity_delivered+'".$_SESSION['new_delivered'.$i]."' WHERE job_card_no = '".$_SESSION['job_card_no']."' AND component = '".$component."' ";
execsql($update_query);

if ($_SESSION['new_delivered'.$i]>0) {
	   	$table_name = $_SESSION['db_prefix']."_mworks_delivered_lines";
		$deliver_lines_query = "INSERT INTO $table_name (m_delivered_id, job_card_no, component, delivered_qty) VALUES ('".$m_delivered_id."', '".$_SESSION['job_card_no']."', '".$component."', '".$_SESSION['new_delivered'.$i]."' ) ";
		execsql($deliver_lines_query);
}


$table_name = $_SESSION['db_prefix']."_mworks_job_components";
$check_query = "SELECT job_card_no FROM $table_name WHERE job_card_no = '".$_SESSION['job_card_no']."' AND (quantity-quantity_delivered) > 0 ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);

if ($check_num>0) {
	$done ='part';
}
else {
	$done = 'yes';
}

$i++;
}

$table_name = $_SESSION['db_prefix']."_mworks_jobs";
$update_query = "UPDATE $table_name SET  delivered='".$done."'  WHERE job_card_no = '".$_SESSION['job_card_no']."' ";
execsql($update_query);


if ($_SESSION['mworks']=='only') {
$table_name = $_SESSION['db_prefix']."_vehicle";
$mileage_query = "SELECT clock_reading FROM $table_name WHERE vehicle_id = '".$_SESSION['vehicle_id']."' ";
$mileage_result = mySQL_query($mileage_query);
$mileage = mysql_result($mileage_result, 0, "clock_reading");

$table_name = $_SESSION['db_prefix']."_vehicle_project";
$vehicle_query = "INSERT INTO $table_name (job_card_no, vehicle_id, use_date, mileage) VALUES ('".$_SESSION['job_card_no']."', '".$_SESSION['vehicle_id']."', '".$_SESSION['deliver_date']."', '".$mileage."'  ) ";
execsql($vehicle_query);
}



$table_name = $_SESSION['db_prefix']."_tracking_production";
$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '104', '".$_SESSION['job_card_no']."', '".$_SESSION['project']."' ,'','' )";
$trackingResult = mySQL_query($trackingQuery);
	
	
$job_card_no = $_SESSION['job_card_no'];
 
	 	$_SESSION['job_card_no']='';
	 	$_SESSION['name']="";
	 	$_SESSION['deliver_date']='';
	 	$_SESSION['cust_id']='';
	 
	 	$_SESSION['quote_note'] = '';
		
		$_SESSION['cust_id']='';
		$_SESSION['order_date']='';
		$_SESSION['project']='';
		$_SESSION['project_description'] = '';
		$_SESSION['project_no'] = '';
		$_SESSION['project_place'] = '';
		$_SESSION['project_area'] = '';
		$_SESSION['deliver_date'] = '';
		$_SESSION['work_days'] = '';
		$_SESSION['value'] = '';
		$_SESSION['deliver_date']= '';

      $_SESSION['forman']='';
		$_SESSION['forman_name']='';
	 	
		
?>

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

