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

	$d = date("d");
	$m = date("m");
	$y = date("Y");
	
	$user_id = $_SESSION['username'];
	date_default_timezone_set('Africa/Khartoum');
	$date = date("Y-m-d");
    $entry_date = date("Y-m-d H:i:s",time()-3600); 
	$truss_balance = 'yes';

$table_name = $_SESSION['db_prefix']."_production_bill";
$production_query = "INSERT INTO $table_name (job_no, follow_no, prod_date ) VALUES ('".$_SESSION['job_no']."', '1', '".$_SESSION['production_date']."' ) ";
execsql($production_query);

$table_name = $_SESSION['db_prefix']."_production_bill";
$bill_query = "SELECT production_bill_id  FROM $table_name ORDER BY production_bill_id DESC Limit 1 ";
$bill_result = mySQL_query($bill_query);
$production_bill_id = mysql_result($bill_result, 0, "production_bill_id");

$table_name = $_SESSION['db_prefix']."_presets";
$charge_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'Press points' ";
$charge_result = mySQL_query($charge_query);
$press_charge = mysql_result($charge_result, 0, "preset_value");

$i=0;
while ($i<$_SESSION['line_no2']){ 
    $table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
	$press_query = "SELECT quantity, span, press_points FROM $table_name WHERE bill_id = '".$_SESSION['bill_id']."' AND truss_type = '".$_SESSION['truss_type'.$i]."' ";
	$press_result = mySQL_query($press_query);
	
	$span = mysql_result($press_result, 0, "span");
	$press_points = mysql_result($press_result, 0, "press_points");
	$a2 = mysql_result($press_result, 0, "quantity");
	
	if($_SESSION['truss_no'.$i]>0){
	if ($_SESSION['truss_no'.$i]!=0) {
	$table_name = $_SESSION['db_prefix']."_production_bill_lines";
	$produced_query = "INSERT INTO $table_name (production_bill_id, job_no, truss_type, quantity, span, press_points, press_charge, delivered ) VALUES ('".$production_bill_id."', '".$_SESSION['job_no']."', '".$_SESSION['truss_type'.$i]."', '".$_SESSION['truss_no'.$i]."', '".$span."', '".$press_points."', '".$press_charge."', '0' ) ";
	execsql($produced_query);
	}
	}
	
	$table_name2 = $_SESSION['db_prefix']."_production_bill_lines";
	$supplied_query = "SELECT SUM(quantity) AS summed_quantity FROM $table_name2 WHERE truss_type = '".$_SESSION['truss_type'.$i]."' AND job_no = '".$_SESSION['job_no']."'  ";
	$supplied_result = mySQL_query($supplied_query);
	$a4 = mysql_result($supplied_result, 0, "summed_quantity");
	if ($a4=='') {$a4=0;}
	$a5 = $a2-$a4;
	if ($a5>0) {$truss_balance='no';}
	
	$i++;
}

if ($truss_balance=='yes') {
		
	$table_name1 = $_SESSION['db_prefix']."_cutting_bill_lines";
	$cutting_bill_query = "SELECT truss_type FROM $table_name1 WHERE  job_no = '".$_SESSION['job_no']."' AND type='truss'";
	$cutting_bill_result = mySQL_query($cutting_bill_query);
	$cutting_bill_num = mysql_num_rows($cutting_bill_result);
	$j=0;
	while ($j<$cutting_bill_num) {
		$truss_type = mysql_result($cutting_bill_result, $j, "truss_type");
				
		$table_name1 = $_SESSION['db_prefix']."_cutting_bill_lines";
		$table_name2 = $_SESSION['db_prefix']."_cutting_bill_lines_done";
		$copy_query = "INSERT INTO $table_name2 SELECT * FROM $table_name1 WHERE job_no = '".$_SESSION['job_no']."' AND truss_type = '".$truss_type."' ";
		execsql($copy_query);
    			
		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
		$delete_query = "DELETE FROM $table_name  WHERE job_no = '".$_SESSION['job_no']."' AND truss_type = '".$truss_type."' ";
		execsql($delete_query);
		
		$j++;
	}
	
	$table_name = $_SESSION['db_prefix']."_captured_jobs";
	$check_query = "SELECT supplied FROM $table_name WHERE job_no = '".$_SESSION['job_no']."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	$check = mysql_result($check_result, 0, "supplied");
	
	
	if ($check!='yes') {
	
	$table_name1 = $_SESSION['db_prefix']."_cutting_bill_lines";
	$cutting_bill_query = "SELECT store_item_id FROM $table_name1 WHERE  job_no = '".$_SESSION['job_no']."' AND ( type='timber' OR type='plate')";
	$cutting_bill_result = mySQL_query($cutting_bill_query);
	$cutting_bill_num = mysql_num_rows($cutting_bill_result);
	$y=0;
	while ($y<$cutting_bill_num) {
		$store_item_id = mysql_result($cutting_bill_result, $y, "store_item_id");
				
		$table_name1 = $_SESSION['db_prefix']."_cutting_bill_lines";
		$table_name2 = $_SESSION['db_prefix']."_cutting_bill_lines_done";
		$copy_query = "INSERT INTO $table_name2 SELECT * FROM $table_name1 WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' ";
		execsql($copy_query);
    			
		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
		$delete_query = "DELETE FROM $table_name  WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' ";
		execsql($delete_query);
	
		$y++;
	}
	
	
	$table_name1 = $_SESSION['db_prefix']."_cutting_supplies_lines";
	$cutting_supplies_query = "SELECT store_item_id FROM $table_name1 WHERE  job_no = '".$_SESSION['job_no']."' ";
	$cutting_supplies_result = mySQL_query($cutting_supplies_query);
	$cutting_supplies_num = mysql_num_rows($cutting_supplies_result);
	$y=0;
	while ($y<$cutting_bill_num) {
		$store_item_id = mysql_result($cutting_supplies_result, $y, "store_item_id");
				
		$table_name1 = $_SESSION['db_prefix']."_cutting_supplies_lines";
		$table_name2 = $_SESSION['db_prefix']."_cutting_supplies_lines_done";
		$copy_query = "INSERT INTO $table_name2 SELECT * FROM $table_name1 WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' ";
    	execsql($copy_query);
		
		$table_name = $_SESSION['db_prefix']."_cutting_supplies_lines";
		$delete_query = "DELETE FROM $table_name  WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."'  ";
		execsql($delete_query);
		
		$y++;
	}
	
	$table_name = $_SESSION['db_prefix']."_captured_jobs";
	$captured_query = "UPDATE $table_name SET supplied = 'yes' WHERE job_no = '".$_SESSION['job_no']."' ";
	execsql($captured_query);
	}
}

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

$table_name = $_SESSION['db_prefix']."_captured_jobs";
$captured_query = "UPDATE $table_name SET produced = '".$truss_balance."' WHERE job_no = '".$_SESSION['job_no']."' ";
execsql($captured_query);


$job_no = $_SESSION['job_no'];
$quote='no';

$_SESSION['job_no']="";
$_SESSION['production_date']  = date('Y-m-d');
$i=0;
	 while ($i<150) {
		 $_SESSION['truss_no'.$i]='';
		 $_SESSION['bill_line_id'.$i]='';
		 $_SESSION['truss_type'.$i]='';
		 $i++;
	 }
	
?>

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




