<?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());
	
$excess_timber=$_GET['excess_timber'];
$all_meter_cost_total=$_GET['all_meter_cost_total'];
$timber_cost=$_GET['timber_cost']; 

$excess_plate=$_GET['excess_plate'];
$total_plate_charge=$_GET['total_plate_charge'];
$quoted_charge=$_GET['quoted_charge'];
 

$table_name = $_SESSION['db_prefix']."_trusses_temp";
$truss_query = "SELECT * FROM $table_name WHERE job_no = '".$_SESSION['job_no']."' AND select_it<>'no' AND user =  '".$_SESSION['username']."' ";
$truss_result = mySQL_query($truss_query);
$truss_num = mysql_num_rows($truss_result);

$table_name = $_SESSION['db_prefix']."_cutting_temp";
$timber_query = "SELECT * FROM $table_name WHERE job_id = '".$_SESSION['job_no']."' AND user =  '".$_SESSION['username']."' ";
$timber_result = mySQL_query($timber_query);
$timber_num = mysql_num_rows($timber_result);

$table_name = $_SESSION['db_prefix']."_plates_temp";
$plates_query = "SELECT * FROM $table_name WHERE job_id = '".$_SESSION['job_no']."'  AND user =  '".$_SESSION['username']."' ";
$plates_result = mySQL_query($plates_query);
$plates_num = mysql_num_rows($plates_result);
	
	
$table_name = $_SESSION['db_prefix']."_cutting_bill";
$bill_query = "INSERT INTO $table_name (job_no, bill_date) VALUES ('".$_SESSION['job_no']."', '".$entry_date."'  ) ";
execsql($bill_query);

$table_name = $_SESSION['db_prefix']."_cutting_bill";
$bill_id_query = "SELECT bill_id FROM $table_name ORDER BY bill_id DESC LIMIT 1  ";
$bill_id_result = mySQL_query($bill_id_query);
$bill_id = mysql_result($bill_id_result, 0, "bill_id");	

$table_name10 = $_SESSION['db_prefix']."_captured_timber";
$cap_timber_reserved_query = "SELECT cap_timber_id FROM $table_name10 WHERE cap_job = '".$_SESSION['job_no']."' AND cap_timber_group = 'Trusses' ";
$cap_timber_reserved_result = mySQL_query($cap_timber_reserved_query);
$cap_timber_reserved_num = mysql_num_rows($cap_timber_reserved_result);

$z=0;
while ($z<$cap_timber_reserved_num) {
$make_zero_query = "UPDATE $table_name10 SET prereserved = '0', reserved = '0' WHERE cap_job= '".$_SESSION['job_no']."' AND cap_timber_group = 'Trusses' ";
execsql($make_zero_query);
$z++;
}

$table_name = $_SESSION['db_prefix']."_presets";
$press_charge_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'Press points' ";
$press_charge_result = mySQL_query($press_charge_query);
$press_charge = mysql_result($press_charge_result, 0, "preset_value");
//echo '<br>'.$press_charge;

$i=0;
while ($i<$truss_num) {
	$truss_type = mysql_result($truss_result, $i, "truss_type");	
	$span = mysql_result($truss_result, $i, "span");	
	$press_points = mysql_result($truss_result, $i, "press_points");	
	$truss_quantity = mysql_result($truss_result, $i, "qty");
	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
	$trusses_query = "INSERT INTO $table_name (bill_id, job_no, type, truss_type, quantity, span, press_points, price ) VALUES ('".$bill_id."', '".$_SESSION['job_no']."', 'truss', '".$truss_type."', '".$truss_quantity."', '".$span."', '".$press_points."', '".$press_charge."' ) ";
	execsql($trusses_query);
	$i++;
}

$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
$table_name2 = $_SESSION['db_prefix']."_captured_timber";
$table_name3 = $_SESSION['db_prefix']."_store_items";
$j=0;
while ($j<$timber_num) {
	$store_item_id = mysql_result($timber_result, $j, "store_item_id");	
	$timber_quantity = mysql_result($timber_result, $j, "quantity");
	
	$price_query = "SELECT price, quantity FROM $table_name3 WHERE store_item_id = '".$store_item_id."' ";
	$price_result = mySQL_query($price_query);
	$price = mysql_result($price_result, 0, "price");

	
	
	$timber_query = "INSERT INTO $table_name (bill_id, job_no, type, store_item_id, quantity, price ) VALUES ('".$bill_id."', '".$_SESSION['job_no']."', 'timber', '".$store_item_id."', '".$timber_quantity."', '".$price."' ) ";
	execsql($timber_query);
	
	$item_query = "SELECT depth, width, class, length FROM $table_name3 WHERE store_item_id = '".$store_item_id."' ";
	$item_result = mySQL_query($item_query);
	$depth = mysql_result($item_result, 0, "depth");
	$width = mysql_result($item_result, 0, "width");
	$class = mysql_result($item_result, 0, "class");
	$length = mysql_result($item_result, 0, "length");
	$group = $depth.'x'.$width.' ('.$class.')';
	$length = ($length*$timber_quantity)/1000;
	
	$reserved_query = "UPDATE $table_name2 SET reserved=reserved+".$length." WHERE cap_timber = '".$group."' AND cap_job = '".$_SESSION['job_no']."' AND cap_timber_group = 'Trusses' ";
	execsql($reserved_query);
	//echo '<br>'.$reserved_query;
	
	$update_stock_query = "UPDATE $table_name3 SET reserved = reserved+".$timber_quantity." WHERE store_item_id = '".$store_item_id."' ";
	execsql($update_stock_query);
	//echo "<br>".$update_stock_query;
	
	$j++;
}

$k=0;
while ($k<$plates_num) {
	$store_item_id = mysql_result($plates_result, $k, "store_item_id");	
	$plates_quantity = mysql_result($plates_result, $k, "quantity");
	
	$price_query = "SELECT price, quantity FROM $table_name3 WHERE store_item_id = '".$store_item_id."' ";
	$price_result = mySQL_query($price_query);
	$price = mysql_result($price_result, 0, "price");
	
	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
	$plates_query = "INSERT INTO $table_name (bill_id, job_no, type, store_item_id, quantity, price ) VALUES ('".$bill_id."', '".$_SESSION['job_no']."', 'plate', '".$store_item_id."', '".$plates_quantity."', '".$price."' ) ";
	execsql($plates_query);	
	
	//$update2_stock_query = "UPDATE $table_name3 SET reserved = reserved+".$plates_quantity." WHERE store_item_id = '".$store_item_id."' ";
//	execsql($update2_stock_query);
	//echo "<br>".$update2_stock_query;

	$k++;
}

$timber_value=$excess_timber;
include('../include/cutting_timber_alert.php');

$plate_value=$excess_plate;
include('../include/cutting_plate_alert.php');

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

$table_name = $_SESSION['db_prefix']."_trusses_temp";
$delete_query = "DELETE FROM $table_name WHERE user =  '".$_SESSION['username']."' "; 
execsql($delete_query);

$table_name = $_SESSION['db_prefix']."_cutting_temp";
$delete2_query = "DELETE FROM $table_name WHERE user =  '".$_SESSION['username']."' "; 
execsql($delete2_query);
	 
$table_name = $_SESSION['db_prefix']."_plates_temp";
$delete3_query = "DELETE FROM $table_name WHERE user =  '".$_SESSION['username']."' "; 

$table_name8 = $_SESSION['db_prefix']."_captured_jobs";
$set_query = "UPDATE $table_name8 SET cutting_bill = 'yes' WHERE job_no = '".$_SESSION['job_no']."' ";
execsql($set_query);


execsql($delete3_query);

$job_no = $_SESSION['job_no'];

$_SESSION['job_no']="";
$_SESSION['name']="";
$_SESSION['line_no']='';
	
?>

<script language="javascript" type="text/javascript">
        
	        window.location="cutting_bill.php?bill_id=<?php echo $bill_id ?>&job_no=<?php echo $job_no ?>";
        
</script>




