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

	$d = date("d");
	$m = date("m");
	$y = date("Y");
	
	$user_id = $_SESSION['username'];
	date_default_timezone_set('Africa/Gaborone');
	$entry_date = date("Y-m-d H:i:s", time()); 
	
	$time=$_SESSION['hour'].':'.$_SESSION['min'];
	
	
//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 ('cash', '".$_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");	


$line_num = $_GET['line2'];

$i=0;
while ($i<$line_num) {	
$store_item_id = $_GET['store_item_id'.$i];
$quantity = $_GET['quantity'.$i];

$table_name = $_SESSION['db_prefix']."_store_items";
$name_query = "SELECT subCategory, item_name, length, quantity, price FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
$name_result = mySQL_query($name_query);
$subCategory = mysql_result($name_result, 0, "subCategory");
$item_name = mysql_result($name_result, 0, "item_name");
$length = mysql_result($name_result, 0, "length");
$price = mysql_result($name_result, 0, "price");
$total_quantity = mysql_result($name_result, 0, "quantity");


$table_name = $_SESSION['db_prefix']."_store_items";
$update_query = "UPDATE $table_name SET quantity = quantity-'".$quantity."' WHERE store_item_id = '".$store_item_id."'  ";
execsql($update_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."', '39', '".$supply_id."','".$quantity."', '".($total_quantity - $quantity)."','".$_SESSION['delivery_date']."','".$entry_date."','".$user_id."','".$price."')";
execsql($historyQuery);

$table_name = $_SESSION['db_prefix']."_cash_job_lines";
$job_query = "UPDATE $table_name SET delivered = delivered + '".$quantity."'  WHERE quote_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' ";
execsql($job_query);

if ($quantity>0) {
$table_name = $_SESSION['db_prefix']."_supply_cash_lines";
$supply_query = "INSERT INTO $table_name (supply_id, job_no, store_item_id, quantity) VALUES ('".$supply_id."', '".$_SESSION['job_no']."', '".$store_item_id."', '".$quantity."' ) ";
execsql($supply_query);

if ($subCategory=='Timber') {

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

$table_name = $_SESSION['db_prefix']."_authorised_timber";
$timber_authorise_query = "UPDATE $table_name SET delivered = delivered + '".$quantity."' WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' ";
execsql($timber_authorise_query);

$table_name = $_SESSION['db_prefix']."_captured_timber";
$timber_capture_query = "UPDATE $table_name SET delivered = delivered + '".$item_length."' WHERE cap_job = '".$_SESSION['job_no']."' AND cap_timber_name = '".$item_name."'  ";
execsql($timber_capture_query);
}

if ($subCategory=='Mitek') {
$table_name = $_SESSION['db_prefix']."_captured_mitek";
$mitek_authorise_query = "UPDATE $table_name SET delivered = delivered + '".$quantity."' WHERE cap_job = '".$_SESSION['job_no']."' AND cap_item_id = '".$store_item_id."' ";
execsql($mitek_authorise_query);

$table_name = $_SESSION['db_prefix']."_cash_job_lines";
$job_query = "UPDATE $table_name SET delivered = delivered + '".$quantity."' WHERE quote_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' ";
execsql($job_query);
}

if ($subCategory=='Sundries') {
$table_name = $_SESSION['db_prefix']."_captured_sundries";
$sundries_authorise_query = "UPDATE $table_name SET delivered = delivered + '".$quantity."' WHERE cap_job = '".$_SESSION['job_no']."' AND cap_item_id = '".$store_item_id."' ";
execsql($sundries_authorise_query);

$table_name = $_SESSION['db_prefix']."_cash_job_lines";
$job_query = "UPDATE $table_name SET delivered = delivered + '".$quantity."' WHERE quote_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' ";
execsql($job_query);
}
}
$i++;
}


$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']."', '27', '".$store_item_id."' ,'".$_SESSION['cust_id']."','".$_SESSION['delivery_note_no']."', '".$_SESSION['job_no']."' )";
$trackingResult = mySQL_query($trackingQuery);



$table_name = $_SESSION['db_prefix']."_captured_cash_sale";
$delivered_query = "UPDATE $table_name SET delivered = 'yes' WHERE job_no = '".$_SESSION['job_no']."' ";
execsql($delivered_query);

$table_name1 = $_SESSION['db_prefix']."_cash_job_lines";
$table_name2 = $_SESSION['db_prefix']."_cash_job_lines_done";
$copy_query = "INSERT INTO $table_name2 SELECT * FROM $table_name1 WHERE quote_no = '".$_SESSION['job_no']."' ";
execsql($copy_query);

$table_name = $_SESSION['db_prefix']."_cash_job_lines";
$delete_query = "DELETE FROM $table_name WHERE quote_no = '".$_SESSION['job_no']."' ";
execsql($delete_query);

	$table_name2 = $_SESSION['db_prefix']."_captured_timber";
	$cap_timber_query = "SELECT cap_timber_id FROM $table_name2 WHERE  cap_job = '".$_SESSION['job_no']."' ";
	$cap_timber_result = mySQL_query($cap_timber_query);
	$cap_timber_num = mysql_num_rows($cap_timber_result);
	$j=0;
	while ($j<$cap_timber_num) {
		$cap_timber_id = mysql_result($cap_timber_result, $j, "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);
				
		$j++;
	}


	$table_name2 = $_SESSION['db_prefix']."_authorised_timber";
	$authorised_query = "SELECT auth_id FROM $table_name2 WHERE  job_no = '".$_SESSION['job_no']."' ";
	$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."' ";
    	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."' ";
		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++;
	}


?>

<script language="javascript" type="text/javascript">
        
   window.location="cash_sale_supplied.php?del_note=<?php echo $_SESSION['delivery_note_no'] ?>&supply_id=<?php echo $supply_id ?>&pr_option=all";
        
</script>




