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

	$d = date("d");
	$m = date("m");
	$y = date("Y");
	
	$user_id = $_SESSION['username'];
	$date = date("Y-m-d");
    date_default_timezone_set('Africa/Gaborone');
	$entry_date = date("Y-m-d H:i:s", time()); 
	$balance = 'yes';
	$_SESSION['message_timber']='';
	$_SESSION['message_mitek']='';
	$_SESSION['message_sundries']='';
	$_SESSION['message_other']='';
	
	$time=$_SESSION['hour'].':'.$_SESSION['min'];
	
	$ip_address = $_SERVER['REMOTE_ADDR'];
	$prev_page = $_SERVER['HTTP_REFERER'];
	$table_name = "test_tracking_log";
	$track_check_query = "INSERT INTO $table_name (entry_date, user, company, page, location, prev_page) VALUES ('".$entry_date."' ,'".$_SESSION['username']."','Tlhomeso', 'deliverJobWiz8.php', '".$ip_address."', '".$prev_page."' )";
	execsql($track_check_query);

	
//INSERT DELIVERY DETAILS IN SUPPLY_CUSTOMER TABLE
$table_name = $_SESSION['db_prefix']."_supply_customer";
$del_note_query = "SELECT delivery_note FROM $table_name ORDER BY delivery_note DESC LIMIT 1  ";
$del_note_result = mySQL_query($del_note_query);
$_SESSION['delivery_note_no'] = mysql_result($del_note_result, 0, "delivery_note")+1;

	$table_name = $_SESSION['db_prefix']."_supply_customer";
	$deliver_query = "INSERT INTO $table_name (type, job_no, cust_id, delivery_note, delivery_address, delivered_by, vehicle_reg, delivery_date, delivery_time ) VALUES ('job', '".$_SESSION['job_no']."', '".$_SESSION['cust_id']."', '".$_SESSION['delivery_note_no']."', '".$_SESSION['delivery_address']."', '".$_SESSION['driver']."', '".$_SESSION['vehicle']."', '".$_SESSION['delivery_date']."', '".$time."') ";
	execsql($deliver_query);
	
	$table_name = $_SESSION['db_prefix']."_supply_customer";
	$id_query = "SELECT supply_id FROM $table_name  ORDER BY supply_id DESC ";
	$id_result = mySQL_query($id_query);
	$id_num = mysql_num_rows($id_result);
	$supply_id = mysql_result($id_result, 0, "supply_id");

	
	
//=====================COPY ITEMS FROM SUPPLY_CUSTOMER_TEMP INTO SUPPLY_CUSTOMER_LINES===========================	
	$table_name1 = $_SESSION['db_prefix']."_supply_customer_temp";
	$table_name2 = $_SESSION['db_prefix']."_supply_customer_lines";
	$supply_query = "insert into $table_name2 (supply_id, job_no, truss_type, store_item_id, timber_type, quantity, tot_length)
    select '".$supply_id."', job_no, truss_type, store_item_id, timber_type, quantity, tot_length from $table_name1 WHERE job_no = '".$_SESSION['job_no']."' AND user =  '".$_SESSION['username']."' AND quantity>0 ";
    execsql($supply_query); 
	//echo '<br>'.$supply_query;
	
//=============================COPY EXTRA DELIVERIES INTO VARIATIONS===================================
	$table_name1 = $_SESSION['db_prefix']."_supply_customer_temp";
	$extra_query = "SELECT * FROM $table_name1 WHERE job_no = '".$_SESSION['job_no']."' AND user =  '".$_SESSION['username']."' AND ( extra = 'yes' OR extra = 'var' )";
	$extra_result = mySQL_query($extra_query);
	$extra_num = mysql_num_rows($extra_result);
	
	$i=0;
	while ($i<$extra_num) {
	$job_no = mysql_result($extra_result, $i, "job_no");
	$truss_type = mysql_result($extra_result, $i, "truss_type");
	$store_item_id = mysql_result($extra_result, $i, "store_item_id");
	$timber_name = mysql_result($extra_result, $i, "timber_type");
	$delivered = mysql_result($extra_result, $i, "quantity");
	$extra = mysql_result($extra_result, $i, "extra");
	$reason = mysql_result($extra_result, $i, "notes_text");
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$price_query = "SELECT price FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
	$price_result = mySQL_query($price_query);
	$price = mysql_result($price_result, 0, "price");
	
	if ($extra=='var') {
	if ($truss_type=='bracing') {
		   	$table_name = $_SESSION['db_prefix']."_authorised_timber";
			$authorised_query = "SELECT  reserved  FROM $table_name WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' AND auth_timber_name = '".$timber_name."'  ";
			$authorised_result = mySQL_query($authorised_query);
			$authorised_num = mysql_num_rows($authorised_result);
			$reserved = mysql_result($authorised_result, 0, "reserved");
			
			$delivered = $delivered - $reserved;  //calculating the variation
	}
	
	if ($truss_type=='sundries') {
		   	$table_name = $_SESSION['db_prefix']."_authorised_timber";
			$authorised_query = "SELECT  reserved  FROM $table_name WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' AND auth_timber_name = '".$timber_name."'  ";
			$authorised_result = mySQL_query($authorised_query);
			$authorised_num = mysql_num_rows($authorised_result);
			$reserved = mysql_result($authorised_result, 0, "reserved");
			
			$delivered = $delivered - $reserved;  //calculating the variation
	}
	
	if ($truss_type=='Mitek') {
		   	$table_name = $_SESSION['db_prefix']."_captured_mitek";
			$authorised_query = "SELECT  auth_quantity  FROM $table_name WHERE cap_job = '".$_SESSION['job_no']."' AND cap_item_id = '".$store_item_id."' ";
			$authorised_result = mySQL_query($authorised_query);
			$authorised_num = mysql_num_rows($authorised_result);
			$auth_quantity = mysql_result($authorised_result, 0, "auth_quantity");
			
			$delivered = $delivered - $auth_quantity;  //calculating the variation
	}
	
	if ($truss_type=='Additional') {
		   	$table_name = $_SESSION['db_prefix']."_captured_sundries";
			$authorised_query = "SELECT  auth_quantity  FROM $table_name WHERE cap_job = '".$_SESSION['job_no']."' AND cap_item_id = '".$store_item_id."' ";
			$authorised_result = mySQL_query($authorised_query);
			$authorised_num = mysql_num_rows($authorised_result);
			$auth_quantity = mysql_result($authorised_result, 0, "auth_quantity");
			
			$delivered = $delivered - $auth_quantity;  //calculating the variation
	}
	}
		
	$table_name16 = $_SESSION['db_prefix']."_variations";
	$variations_query = "insert into $table_name16 (job_no, store_item_id, timber_name, delivered, price, notes_text) VALUES ('".$job_no."', '".$store_item_id."', '".$timber_name."', '".$delivered."', '".$price."', '".$reason."')";
	execsql($variations_query); 
	//echo '<br>'.$variations_query;
	$i++;
	}
	

//====================================SET TRUSSES DELIVERED IN PRODUCTION BILL LINES=====================================
$table_name2 = $_SESSION['db_prefix']."_supply_customer_lines";
$truss_query = "SELECT truss_type, quantity FROM $table_name2 WHERE timber_type = 'Trusses' AND supply_id = '".$supply_id."' ";
//echo '<br>'.$truss_query;
$truss_result = mySQL_query($truss_query);
$truss_num = mysql_num_rows($truss_result);

$i=0;
while ($i<$truss_num) {
$truss_type = mysql_result($truss_result, $i, "truss_type");
$quantity = mysql_result($truss_result, $i, "quantity");

$table_name = $_SESSION['db_prefix']."_production_bill_lines";
$check_query = "SELECT production_bill_line_id FROM $table_name WHERE truss_type = '".$truss_type."' AND job_no = '".$_SESSION['job_no']."' ";

$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);


$k=0;
while ($k<$check_num) {

$production_bill_line_id = mysql_result($check_result, $k, "production_bill_line_id");

$table_name = $_SESSION['db_prefix']."_production_bill_lines";
$line_query = "SELECT truss_type ,delivered, quantity FROM $table_name WHERE production_bill_line_id = '".$production_bill_line_id."' ";
$line_result = mySQL_query($line_query);
$line_num = mysql_num_rows($line_result);
$line_truss_type = mysql_result($line_result, 0, "truss_type");
$line_delivered = mysql_result($line_result, 0, "delivered");
$line_quantity = mysql_result($line_result, 0, "quantity");
$line_balance = $line_quantity-$line_delivered;

if ($line_balance<$quantity) {
      $del_quantity = $quantity-($quantity-$line_balance);
}
else {
	$del_quantity = $quantity;
}

$table_name3 = $_SESSION['db_prefix']."_production_bill_lines";	
$truss_delivered_query = "UPDATE $table_name3 SET delivered = (delivered + '".$del_quantity."') WHERE job_no = '".$_SESSION['job_no']."' AND truss_type = '".$truss_type."' AND production_bill_line_id = '".$production_bill_line_id."' ";

execsql($truss_delivered_query);

$quantity = $quantity - $del_quantity;
if ($quantity<0) {
	$quantity=0;
}


$k++;
}

$table_name = $_SESSION['db_prefix']."_tracking_store";
$trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '15', '".$truss_type."', '".$_SESSION['cust_id']."' ,'".$_SESSION['delivery_note_no']."','".$_SESSION['job_no']."' )";
$trackingResult = mySQL_query($trackingQuery);	

$i++;
}


//======================BRACING, REDUCE QUANTITIES AND RESERVED IN STORE_ITEMS TABLE. REDUCE RESERVED AND INCREASE DELIVERED IN CAPTURED_TIMBER TABLE
$table_name2 = $_SESSION['db_prefix']."_supply_customer_lines";
$bracing_query = "SELECT timber_type, store_item_id, quantity FROM $table_name2 WHERE truss_type = 'Bracing' AND supply_id = '".$supply_id."' ";
//echo '<br>'.$bracing_query;
$bracing_result = mySQL_query($bracing_query);
$bracing_num = mysql_num_rows($bracing_result);
//echo '<br>'.$bracing_num;
$email_message='';
$j=0;
while ($j<$bracing_num) {
$timber_type = mysql_result($bracing_result, $j, "timber_type");
$store_item_id = mysql_result($bracing_result, $j, "store_item_id");
$quantity = mysql_result($bracing_result, $j, "quantity");
//echo '<br>'.$timber_type;


$table_name4 = $_SESSION['db_prefix']."_store_items";
$length_query = "SELECT length, price, quantity, subCategory, minimumStock, item_name FROM $table_name4 WHERE store_item_id = '".$store_item_id."' ";
$length_result = mySQL_query($length_query);
$length = mysql_result($length_result, 0, "length");
$price = mysql_result($length_result, 0, "price");
$total_quantity = mysql_result($length_result, 0, "quantity");
$sub_category = mysql_result($length_result ,0, "subCategory");
$min_stock = mysql_result($length_result ,0, "minimumStock");
$item_name = mysql_result($length_result ,0, "item_name");

$table_name4 = $_SESSION['db_prefix']."_store_items";
$stock_update_query = "UPDATE $table_name4 SET quantity = quantity-'".$quantity."', reserved = reserved-'".$quantity."' WHERE store_item_id = '".$store_item_id."' ";
//echo '<br>'.$stock_update_query;
execsql($stock_update_query);

$length = ($quantity*$length)/1000;

$table_name5 = $_SESSION['db_prefix']."_captured_timber";
$bracing_delivered_query = "UPDATE $table_name5 SET delivered = delivered+".$length.", reserved = reserved-".$length." WHERE cap_job = '".$_SESSION['job_no']."' AND cap_timber_name = '".$timber_type."' ";
execsql($bracing_delivered_query);
//echo '<br>'.$bracing_delivered_query;
$table_name11 = $_SESSION['db_prefix']."_authorised_timber";
$auth_delivered_query = "UPDATE $table_name11 SET delivered = delivered+".$quantity." WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' AND auth_timber_name = '".$timber_type."' ";
execsql($auth_delivered_query);
//echo '<br>bracing'.$auth_delivered_query;


$table_name = $_SESSION['db_prefix']."_variations_temp";
$variation_query = "SELECT de_auth_quantity FROM $table_name WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' AND status = 'minus' AND timber_name = '".$timber_type."'  AND user = '".$_SESSION['username']."' ";
//echo '<br>'.$variation_query;
$variation_result = mySQL_query($variation_query);
$variation_num = mysql_num_rows($variation_result);

if ($variation_num>0) {
$de_auth_quantity = mysql_result($variation_result, 0, "de_auth_quantity");
$de_auth_query = "UPDATE $table_name11 SET de_authorised= de_authorised+'".$de_auth_quantity."'  WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' AND auth_timber_name = '".$timber_type."' ";
//echo '<br>'.$de_auth_query;
execsql($de_auth_query);
}

$table_name8 = $_SESSION['db_prefix']."_del_timber";
$delivered_query = "INSERT INTO $table_name8 (del_note, job_no, store_item_id, type, delivered, price ) VALUES ('".$_SESSION['delivery_note_no']."', '".$_SESSION['job_no']."', '".$store_item_id."', 'Bracing',  '".$quantity."', '".$price."' ) ";
//echo '<br>'.$delivered_query;
execsql($delivered_query);

$table_name10 = $_SESSION['db_prefix']."_item_history";
$historyQuery = "INSERT INTO $table_name10 (store_item_id, entry, reference, quantity, total, date, entryDate, userId, price) VALUES ('".$store_item_id."', '40', '".$_SESSION['job_no']."','".$quantity."', '".($total_quantity - $quantity)."','".$_SESSION['delivery_date']."','".$entry_date."','".$user_id."','".$price."')";
//echo '<br>'.$historyQuery;
execsql($historyQuery);

$table_name = $_SESSION['db_prefix']."_tracking_store";
$trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '16', '".$store_item_id."', '".$_SESSION['cust_id']."' ,'".$_SESSION['delivery_note_no']."','".$_SESSION['job_no']."' )";
$trackingResult = mySQL_query($trackingQuery);	

//if ($total_quantity<=$min_stock) {
//	   	if (($sub_category!='Timber') AND ($sub_category!='Mitek') AND ($sub_category!='Sundries')) {
//			    $sub_category='Other';
//		}		
//		include('../include/min_stock_alert.php');
//		
//		$table_name = $_SESSION['db_prefix']."_min_stock_report";
//		$minimum_query = "INSERT INTO $table_name (store_item_id, stock_date, supplied, new_stock_level, report, supplied_to, supply_no ) VALUES ('".$store_item_id."', '".$entryDate."', '".$quantity."', '".$total_quantity."', 'minimum stock', 'Cash delivery', '".$_SESSION['delivery_no']."' ) ";
//		execsql($minimum_query);
//		
//		$email_message = $email_message.'<tr><td>'.$item_name.'</td><td>'.$sub_category.'</td><td align=center>'.$total_quantity.'</td><td align=center>'.$min_stock.'</td></tr>';
//	}	
		
	
$j++;
}

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

//if ($email_message!='') {
//		  include('../include/min_stock_email_alert.php');
//	}

//===========================================SUNDRIES, REDUCE QUANTITIES AND RESERVED IN STORE_ITEMS TABLE. REDUCE RESERVED, INCREASE DELIVERED IN CAPTURED_TIMBER TABLE======================
$table_name2 = $_SESSION['db_prefix']."_supply_customer_lines";
$sundries_query = "SELECT timber_type, store_item_id, quantity FROM $table_name2 WHERE truss_type = 'Sundries' AND supply_id = '".$supply_id."' ";
//echo '<br>'.$sundries_query;
$sundries_result = mySQL_query($sundries_query);
$sundries_num = mysql_num_rows($sundries_result);
//echo '<br>'.$sundries_num;

$j=0;
while ($j<$sundries_num) {
$timber_type = mysql_result($sundries_result, $j, "timber_type");
$store_item_id = mysql_result($sundries_result, $j, "store_item_id");
$quantity = mysql_result($sundries_result, $j, "quantity");
//echo '<br>'.$timber_type;


$table_name4 = $_SESSION['db_prefix']."_store_items";
$length_query = "SELECT length, price, quantity, subCategory, minimumStock, item_name  FROM $table_name4 WHERE store_item_id = '".$store_item_id."' ";
$length_result = mySQL_query($length_query);
$length = mysql_result($length_result, 0, "length");
$price = mysql_result($length_result, 0, "price");
$total_quantity = mysql_result($length_result, 0, "quantity");
$sub_category = mysql_result($length_result ,0, "subCategory");
$min_stock = mysql_result($length_result ,0, "minimumStock");
$item_name = mysql_result($length_result ,0, "item_name");

$length = ($quantity*$length)/1000;

$table_name4 = $_SESSION['db_prefix']."_store_items";
$stock_update_query = "UPDATE $table_name4 SET quantity = quantity-'".$quantity."', reserved = reserved-'".$quantity."' WHERE store_item_id = '".$store_item_id."' ";
//echo '<br>'.$stock_update_query;
execsql($stock_update_query);

$table_name5 = $_SESSION['db_prefix']."_captured_timber";
$sundries_delivered_query = "UPDATE $table_name5 SET delivered = delivered+".$length.", reserved = reserved-".$length." WHERE cap_job = '".$_SESSION['job_no']."' AND cap_timber_name = '".$timber_type."' ";
execsql($sundries_delivered_query);
//echo '<br>'.$sundries_delivered_query;
$table_name11 = $_SESSION['db_prefix']."_authorised_timber";
$auth_delivered_query = "UPDATE $table_name11 SET delivered = delivered+".$quantity." WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' AND auth_timber_name = '".$timber_type."' ";
execsql($auth_delivered_query);
//echo '<br>sundries'.$auth_delivered_query;

$table_name = $_SESSION['db_prefix']."_variations_temp";
$variation_query = "SELECT de_auth_quantity FROM $table_name WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' AND status = 'minus'  AND timber_name = '".$timber_type."'   AND user = '".$_SESSION['username']."' ";
//echo '<br>'.$variation_query;
$variation_result = mySQL_query($variation_query);
$variation_num = mysql_num_rows($variation_result);

if ($variation_num>0) {
$de_auth_quantity = mysql_result($variation_result, 0, "de_auth_quantity");
$table_name11 = $_SESSION['db_prefix']."_authorised_timber";
$de_auth_query = "UPDATE $table_name11 SET de_authorised= de_authorised+'".$de_auth_quantity."'  WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' AND auth_timber_name = '".$timber_type."' "; 
//echo '<br>'.$de_auth_query;
execsql($de_auth_query);
}

$table_name8 = $_SESSION['db_prefix']."_del_timber";
$delivered_query = "INSERT INTO $table_name8 (del_note, job_no, store_item_id, type, delivered, price ) VALUES ('".$_SESSION['delivery_note_no']."', '".$_SESSION['job_no']."', '".$store_item_id."', 'Sundries',  '".$quantity."', '".$price."' ) ";
//echo '<br>'.$delivered_query;
execsql($delivered_query);

$table_name10 = $_SESSION['db_prefix']."_item_history";
$historyQuery = "INSERT INTO $table_name10 (store_item_id, entry, reference, quantity, total, date, entryDate, userId, price) VALUES ('".$store_item_id."', '40', '".$_SESSION['job_no']."','".$quantity."', '".($total_quantity - $quantity)."','".$_SESSION['delivery_date']."','".$entry_date."','".$user_id."','".$price."')";
//echo '<br>'.$historyQuery;
execsql($historyQuery);

$table_name = $_SESSION['db_prefix']."_tracking_store";
$trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '16', '".$store_item_id."', '".$_SESSION['cust_id']."' ,'".$_SESSION['delivery_note_no']."','".$_SESSION['job_no']."' )";
$trackingResult = mySQL_query($trackingQuery);	

//if ($total_quantity<=$min_stock) {
//	   	if (($sub_category!='Timber') AND ($sub_category!='Mitek') AND ($sub_category!='Sundries')) {
//			    $sub_category='Other';
//		}		
//		include('../include/min_stock_alert.php');
//		
//		$table_name = $_SESSION['db_prefix']."_min_stock_report";
//		$minimum_query = "INSERT INTO $table_name (store_item_id, stock_date, supplied, new_stock_level, report, supplied_to, supply_no ) VALUES ('".$store_item_id."', '".$entryDate."', '".$quantity."', '".$total_quantity."', 'minimum stock', 'Cash delivery', '".$_SESSION['delivery_no']."' ) ";
//		execsql($minimum_query);
//		
//		$email_message = $email_message.'<tr><td>'.$item_name.'</td><td>'.$sub_category.'</td><td align=center>'.$total_quantity.'</td><td align=center>'.$min_stock.'</td></tr>';
//	}	
		
	
    $j++;
}

//==========================LAMINATED BEAMS, ENTER DELIVERY QUANTITIES FOR JOB IN _STORE_ITEMS_REF_LINES
$table_name2 = $_SESSION['db_prefix']."_supply_customer_lines";
$laminated_query = "SELECT timber_type, store_item_id, quantity FROM $table_name2 WHERE truss_type = 'laminated' AND supply_id = '".$supply_id."' ";
//echo '<br>'.$sundries_query;
$laminated_result = mySQL_query($laminated_query);
$laminated_num = mysql_num_rows($laminated_result);

echo '<br>'.$laminated_num;

$v=0;
while ($v<$laminated_num) {
	$store_item_id = mysql_result($laminated_result, $v, "store_item_id");
	$quantity = mysql_result($laminated_result, $v, "quantity");
	$timber_type = mysql_result($laminated_result, $v, "timber_type");
	
	
	$table_name = $_SESSION['db_prefix']."_store_items_ref_lines";
	$job_delivered_query = "UPDATE $table_name SET ref_3 = '".$quantity."' WHERE store_item_id = '".$store_item_id."' AND ref_1 = '".$_SESSION['job_no']."' ";
	execsql($job_delivered_query);
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$stock_query = "UPDATE $table_name SET quantity = quantity-'".$quantity."' WHERE store_item_id = '".$store_item_id."' ";
	execsql($stock_query);
	
	$table_name5 = $_SESSION['db_prefix']."_captured_timber";
	$laminated_delivered_query = "UPDATE $table_name5 SET delivered = delivered+".$quantity.", reserved = reserved-".$quantity." WHERE cap_job = '".$_SESSION['job_no']."' AND cap_timber_name = '".$timber_type."' ";
	execsql($laminated_delivered_query);
	//echo '<br>'.$bracing_delivered_query;
	$table_name11 = $_SESSION['db_prefix']."_authorised_timber";
	$laminated_auth_delivered_query = "UPDATE $table_name11 SET delivered = delivered+".$quantity." WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' ";
	execsql($laminated_auth_delivered_query);
	
	$v++;
}


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

//if ($email_message!='') {
//		  include('../include/min_stock_email_alert.php');
//	}

//==========================MITEK PRODUCTS, REDUCE QUANTITIES AND RESERVED IN STORE_ITEMS TABLE. REDUCE RESERVED, INCREASE DELIVERED IN CAPTURED_MITEK=====================
$table_name2 = $_SESSION['db_prefix']."_supply_customer_lines";
$mitek_query = "SELECT store_item_id, quantity FROM $table_name2 WHERE truss_type = 'Mitek' AND supply_id = '".$supply_id."' ";
$mitek_result = mySQL_query($mitek_query);
$mitek_num = mysql_num_rows($mitek_result);

$m=0;
while ($m<$mitek_num) {
$store_item_id = mysql_result($mitek_result, $m, "store_item_id");
$quantity = mysql_result($mitek_result, $m, "quantity");

$table_name4 = $_SESSION['db_prefix']."_store_items";
$stock_update_query = "UPDATE $table_name4 SET quantity = quantity-'".$quantity."' WHERE store_item_id = '".$store_item_id."' ";
execsql($stock_update_query);

$table_name4 = $_SESSION['db_prefix']."_store_items";
$price_query = "SELECT price, quantity, subCategory, minimumStock, item_name  FROM $table_name4 WHERE store_item_id = '".$store_item_id."'  ";
$price_result = mySQL_query($price_query);
$price = mysql_result($price_result, 0, "price");
$total_quantity = mysql_result($price_result, 0, "quantity");
$sub_category = mysql_result($price_result ,0, "subCategory");
$min_stock = mysql_result($price_result ,0, "minimumStock");
$item_name = mysql_result($price_result ,0, "item_name");

$table_name6 = $_SESSION['db_prefix']."_captured_mitek";
$mitek_delivered_query = "UPDATE $table_name6 SET delivered = delivered+'".$quantity."' WHERE cap_job = '".$_SESSION['job_no']."' AND cap_item_id = '".$store_item_id."' ";
execsql($mitek_delivered_query);

$table_name = $_SESSION['db_prefix']."_variations_temp";
$variation_query = "SELECT de_auth_quantity FROM $table_name WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' AND status = 'minus' AND user = '".$_SESSION['username']."' ";
$variation_result = mySQL_query($variation_query);
$variation_num = mysql_num_rows($variation_result);

if ($variation_num>0) {
$de_auth_quantity = mysql_result($variation_result, 0, "de_auth_quantity");
$table_name6 = $_SESSION['db_prefix']."_captured_mitek";
$de_auth_query = "UPDATE $table_name6 SET de_authorised= de_authorised+'".$de_auth_quantity."'  WHERE cap_job = '".$_SESSION['job_no']."' AND cap_item_id = '".$store_item_id."' ";
execsql($de_auth_query);
}




$table_name10 = $_SESSION['db_prefix']."_item_history";
$historyQuery = "INSERT INTO $table_name10 (store_item_id, entry, reference, quantity, total, date, entryDate, userId, price) VALUES ('".$store_item_id."', '40', '".$_SESSION['job_no']."','".$quantity."', '".$total_quantity."','".$_SESSION['delivery_date']."','".$entry_date."','".$user_id."','".$price."')";
	execsql($historyQuery);
	
$table_name = $_SESSION['db_prefix']."_tracking_store";
$trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '16', '".$store_item_id."', '".$_SESSION['cust_id']."' ,'".$_SESSION['delivery_note_no']."','".$_SESSION['job_no']."' )";
$trackingResult = mySQL_query($trackingQuery);	


//if ($total_quantity<=$min_stock) {
//	   	if (($sub_category!='Timber') AND ($sub_category!='Mitek') AND ($sub_category!='Sundries')) {
//			    $sub_category='Other';
//		}		
//		include('../include/min_stock_alert.php');
//		
//		$table_name = $_SESSION['db_prefix']."_min_stock_report";
//		$minimum_query = "INSERT INTO $table_name (store_item_id, stock_date, supplied, new_stock_level, report, supplied_to, supply_no ) VALUES ('".$store_item_id."', '".$entryDate."', '".$quantity."', '".$total_quantity."', 'minimum stock', 'Cash delivery', '".$_SESSION['delivery_no']."' ) ";
//		execsql($minimum_query);
//		
//		$email_message = $email_message.'<tr><td>'.$item_name.'</td><td>'.$sub_category.'</td><td align=center>'.$total_quantity.'</td><td align=center>'.$min_stock.'</td></tr>';
//	}	
		
	
    $m++;
}

//ADDITIONAL, REDUCE QUANTITIES AND RESERVED IN STORE_ITEMS TABLE. REDUCE RESERVED, INCREASE DELIVERED IN CAPTURED_MITEK
$table_name2 = $_SESSION['db_prefix']."_supply_customer_lines";
$additional_query = "SELECT store_item_id, quantity FROM $table_name2 WHERE truss_type = 'Additional' AND supply_id = '".$supply_id."' ";
$additional_result = mySQL_query($additional_query);
$additional_num = mysql_num_rows($additional_result);

$n=0;
while ($n<$additional_num) {
$store_item_id = mysql_result($additional_result, $n, "store_item_id");
$quantity = mysql_result($additional_result, $n, "quantity");

$table_name4 = $_SESSION['db_prefix']."_store_items";
$stock_update_query = "UPDATE $table_name4 SET quantity = quantity-'".$quantity."' WHERE store_item_id = '".$store_item_id."' ";
execsql($stock_update_query);

$table_name4 = $_SESSION['db_prefix']."_store_items";
$price_query = "SELECT price, quantity, subCategory, minimumStock, item_name  FROM $table_name4 WHERE store_item_id = '".$store_item_id."'  ";
$price_result = mySQL_query($price_query);
$price = mysql_result($price_result, 0, "price");
$total_quantity = mysql_result($price_result, 0, "quantity");
$sub_category = mysql_result($price_result ,0, "subCategory");
$min_stock = mysql_result($price_result ,0, "minimumStock");
$item_name = mysql_result($price_result ,0, "item_name");

$table_name7 = $_SESSION['db_prefix']."_captured_sundries";
$additional_delivered_query = "UPDATE $table_name7 SET delivered = delivered+'".$quantity."' WHERE cap_job = '".$_SESSION['job_no']."' AND cap_item_id = '".$store_item_id."' ";
execsql($additional_delivered_query);

$table_name10 = $_SESSION['db_prefix']."_item_history";
$historyQuery = "INSERT INTO $table_name10 (store_item_id, entry, reference, quantity, total, date, entryDate, userId, price) VALUES ('".$store_item_id."', '40', '".$_SESSION['job_no']."','".$quantity."', '".$total_quantity."','".$_SESSION['delivery_date']."','".$entry_date."','".$user_id."','".$price."')";
	execsql($historyQuery);
	
$table_name = $_SESSION['db_prefix']."_tracking_store";
$trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '16', '".$store_item_id."', '".$_SESSION['cust_id']."' ,'".$_SESSION['delivery_note_no']."','".$_SESSION['job_no']."' )";
$trackingResult = mySQL_query($trackingQuery);

//if ($total_quantity<=$min_stock) {
//	   	if (($sub_category!='Timber') AND ($sub_category!='Mitek') AND ($sub_category!='Sundries')) {
//			    $sub_category='Other';
//		}		
//		include('../include/min_stock_alert.php');
//		
//		$table_name = $_SESSION['db_prefix']."_min_stock_report";
//		$minimum_query = "INSERT INTO $table_name (store_item_id, stock_date, supplied, new_stock_level, report, supplied_to, supply_no ) VALUES ('".$store_item_id."', '".$entryDate."', '".$quantity."', '".$total_quantity."', 'minimum stock', 'Cash delivery', '".$_SESSION['delivery_no']."' ) ";
//		execsql($minimum_query);
//		
//		$email_message = $email_message.'<tr><td>'.$item_name.'</td><td>'.$sub_category.'</td><td align=center>'.$total_quantity.'</td><td align=center>'.$min_stock.'</td></tr>';
//	}	
		
	
    $n++;
}

//if ($email_message!='') {
//		  include('../include/min_stock_email_alert.php');
//	}


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

$truss_delivered='no';
$mitek_delivered='no';
$sundries_delivered='no';
$timber_delivered='no';



$table_name15 = $_SESSION['db_prefix']."_production_bill_lines";	
$delivered_query = "SELECT SUM(quantity-delivered) AS balance FROM $table_name15 WHERE job_no = '".$_SESSION['job_no']."' GROUP BY job_no ";
$delivered_result = mySQL_query($delivered_query);
$delivered_num = mysql_num_rows($delivered_result);
if ($delivered_num!=0) {
$balance = mysql_result($delivered_result, 0, "balance");
if ($balance <= 0) {
	$truss_delivered = 'yes';
}
else {
	$truss_delivered = 'no';
}
}
else {
	$truss_delivered = 'no';
}

$table_name = $_SESSION['db_prefix']."_authorised_timber";
$balance_query = " SELECT reserved FROM $table_name WHERE job_no= '".$_SESSION['job_no']."' AND ((auth_timber_group='bracing') OR (auth_timber_group='sundries')) AND ((reserved-delivered)>0) ";
$balance_result = mySQL_query($balance_query);
$balance_num = mysql_num_rows($balance_result);
if ($balance_num==0) {
$balance_num = mysql_num_rows($balance_result);

if ($balance_num <= 0) {
	$timber_delivered = 'yes';
}
else {
	$timber_delivered = 'no';
}
}
else {
	$timber_delivered = 'no';
}


$table_name16 = $_SESSION['db_prefix']."_captured_mitek";	
$mitek_delivered_query = "SELECT SUM(auth_quantity-delivered) AS balance FROM $table_name16 WHERE cap_job = '".$_SESSION['job_no']."' AND authorised='yes' GROUP BY cap_job ";
//echo '<br>'.$delivered_query;
$mitek_delivered_result = mySQL_query($mitek_delivered_query);
$mitek_delivered_num = mysql_num_rows($mitek_delivered_result);
if ($mitek_delivered_num!=0) {
$balance = mysql_result($mitek_delivered_result, 0, "balance");
if ($balance <= 0) {
	$mitek_delivered = 'yes';
}
else {
	$mitek_delivered = 'no';
}
}
else {
	$mitek_delivered = 'no';
}

$table_name16 = $_SESSION['db_prefix']."_captured_sundries";	
$sundries_delivered_query = "SELECT SUM(auth_quantity-delivered) AS balance FROM $table_name16 WHERE cap_job = '".$_SESSION['job_no']."' AND authorised='yes' GROUP BY cap_job ";
//echo '<br>'.$delivered_query;
$sundries_delivered_result = mySQL_query($sundries_delivered_query);
$sundries_delivered_num = mysql_num_rows($sundries_delivered_result);
if ($sundries_delivered_num!=0) {
$balance = mysql_result($sundries_delivered_result, 0, "balance");
if ($balance <= 0) {
	$sundries_delivered = 'yes';
	
}
else {
	$sundries_delivered = 'no';
}
}
else {
	$sundries_delivered = 'no';
}



if ( ($truss_delivered=='yes') && ($timber_delivered=='yes') ) {
$table_name3 = $_SESSION['db_prefix']."_captured_timber";
	$timber_query = "SELECT cap_timber_id FROM $table_name3 WHERE  cap_job = '".$_SESSION['job_no']."' ";
	$timber_result = mySQL_query($timber_query);
	$timber_num = mysql_num_rows($timber_result);
	
	if ($timber_num>0) {
	$m=0;
	while ($m<$timber_num) {
		$cap_timber_id = mysql_result($timber_result, $m, "cap_timber_id");
		$table_name1 = $_SESSION['db_prefix']."_captured_timber";
		$table_name2 = $_SESSION['db_prefix']."_captured_timber_done";
		$copy_query = "INSERT INTO $table_name2 SELECT * FROM $table_name1 WHERE cap_job = '".$_SESSION['job_no']."' AND cap_timber_id = '".$cap_timber_id."' ";
    	execsql($copy_query);
		
		$table_name = $_SESSION['db_prefix']."_captured_timber";
		$delete_query = "DELETE FROM $table_name  WHERE cap_job = '".$_SESSION['job_no']."' AND cap_timber_id = '".$cap_timber_id."'  ";
		execsql($delete_query);				
		$m++;
	}
	}
}

if ( ($truss_delivered=='yes') && ($mitek_delivered=='yes') && ($sundries_delivered=='yes') && ($timber_delivered=='yes') ) {
	$delivered = 'yes';
	
	$table_name2 = $_SESSION['db_prefix']."_authorised_timber";
	$authorised_query = "SELECT auth_id FROM $table_name2 WHERE  job_no = '".$_SESSION['job_no']."' AND ((auth_timber_group='bracing') OR (auth_timber_group='sundries')) ";
	$authorised_result = mySQL_query($authorised_query);
	$authorised_num = mysql_num_rows($authorised_result);
	$j=0;
	while ($j<$authorised_num) {
		$auth_id = mysql_result($authorised_result, $j, "auth_id");		
		$table_name1 = $_SESSION['db_prefix']."_authorised_timber";
		$table_name2 = $_SESSION['db_prefix']."_authorised_timber_done";
		$copy_query = "INSERT INTO $table_name2 SELECT * FROM $table_name1 WHERE job_no = '".$_SESSION['job_no']."' AND auth_id = '".$auth_id."' AND ((auth_timber_group='bracing') OR (auth_timber_group='sundries'))";
    	execsql($copy_query);
		
		$table_name = $_SESSION['db_prefix']."_authorised_timber";
		$delete_query = "DELETE FROM $table_name  WHERE job_no = '".$_SESSION['job_no']."' AND auth_id = '".$auth_id."'  AND ((auth_timber_group='bracing') OR (auth_timber_group='sundries'))";
		execsql($delete_query);
				
		$j++;
	}
	
	$table_name = $_SESSION['db_prefix']."_captured_mitek";
	$mitek_query = "SELECT cap_mitek_id FROM $table_name WHERE  cap_job = '".$_SESSION['job_no']."' ";
	$mitek_result = mySQL_query($mitek_query);
	$mitek_num = mysql_num_rows($mitek_result);
	
	$i=0;
	while ($i<$mitek_num) {
		$cap_mitek_id = mysql_result($mitek_result, $i, "cap_mitek_id");		
		$table_name1 = $_SESSION['db_prefix']."_captured_mitek";
		$table_name2 = $_SESSION['db_prefix']."_captured_mitek_done";
		$copy_query = "INSERT INTO $table_name2 SELECT * FROM $table_name1 WHERE cap_job = '".$_SESSION['job_no']."' AND cap_mitek_id = '".$cap_mitek_id."' ";
		execsql($copy_query);
		
		$table_name = $_SESSION['db_prefix']."_captured_mitek";
		$delete_query = "DELETE FROM $table_name  WHERE cap_job = '".$_SESSION['job_no']."' AND cap_mitek_id = '".$cap_mitek_id."'  ";
		execsql($delete_query);
					
		$i++;
	}
	
	
	$table_name2 = $_SESSION['db_prefix']."_captured_sundries";
	$sundries_query = "SELECT cap_sundries_id FROM $table_name2 WHERE  cap_job = '".$_SESSION['job_no']."' ";
	$sundries_result = mySQL_query($sundries_query);
	$sundries_num = mysql_num_rows($sundries_result);
	$j=0;
	while ($j<$sundries_num) {
		$cap_sundries_id = mysql_result($sundries_result, $j, "cap_sundries_id");		
		$table_name1 = $_SESSION['db_prefix']."_captured_sundries";
		$table_name2 = $_SESSION['db_prefix']."_captured_sundries_done";
		$copy_query = "INSERT INTO $table_name2 SELECT * FROM $table_name1 WHERE cap_job = '".$_SESSION['job_no']."' AND cap_sundries_id = '".$cap_sundries_id."' ";
    	execsql($copy_query);
		
		$table_name = $_SESSION['db_prefix']."_captured_sundries";
		$delete_query = "DELETE FROM $table_name  WHERE cap_job = '".$_SESSION['job_no']."' AND cap_sundries_id = '".$cap_sundries_id."'  ";
		execsql($delete_query);
						
		$j++;
	}
	
	
	
}
else {
	$delivered = 'no';
}



$table_name18 = $_SESSION['db_prefix']."_captured_jobs";
$job_delivered_query = "UPDATE $table_name18 SET delivered = '".$delivered."', del_date = '".$entry_date."' WHERE job_no = '".$_SESSION['job_no']."' ";
execsql($job_delivered_query);


     $del_note = $_SESSION['delivery_note_no'];

	 $_SESSION['name']="";
	 $_SESSION['delivery_date']  = date('Y-m-d');
	 $_SESSION['delivery_note_no']="";
	 $_SESSION['delivery_address']="";
	 $_SESSION['driver']="";
	 $_SESSION['vehicle']="";
	 $_SESSION['hour']="";
	 $_SESSION['min']="";
	 
	 $_SESSION['job_no']="";
	 $_SESSION['error1']="";
	 	 
	 $i=0;
	 while ($i<25) {
		 $_SESSION['truss_no'.$i]='';
		 $i++;
	 }
	 
	 $_SESSION['line_no2']=0; 
	 
	 $_SESSION['line_no3']=0;
	 $i=0;
	 while ($i<50) {
	     $_SESSION['sub_line3'.$i]=0;
		 $i++;
	 }
	 
	 $_SESSION['line_no4']=0;
	 $i=0;
	 while ($i<50) {
	     $_SESSION['sub_line4'.$i]=0;
		 $i++;
	 }
	 
	 $j=0;
	 while ($i<50) {
	     $_SESSION['timber_type'.$j]='';
		 $j++;
	 }
	 
	 $j=0;
	 while ($i<50) {
	     $_SESSION['timber2_type'.$j]='';
		 $j++;
	 }
	 
	 $_SESSION['line_no5']=0;
	 $_SESSION['line_no6']=0;
	 
	 $table_name = $_SESSION['db_prefix']."_supply_customer_temp";
	 $delete_query = "DELETE FROM $table_name WHERE user =  '".$_SESSION['username']."'  "; 
	 execsql($delete_query);	 
	
	 $table_name = $_SESSION['db_prefix']."_variations_temp";
	 $delete_query = "DELETE FROM $table_name WHERE job_no = '".$_SESSION['job_no']."' AND user =  '".$_SESSION['username']."'  "; 
	 execsql($delete_query);

?>

<script language="javascript" type="text/javascript">
        
	     window.location="customer_supplied.php?del_note=<?php echo $del_note ?>&supply_id=<?php echo $supply_id ?>&pr_option=all";
        
       </script>




