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

	$d = date("d");
	$m = date("m");
	$y = date("Y");
	
	date_default_timezone_set('Africa/Khartoum');
    $entryDate = date("Y-m-d H:i:s",time()-3600); 
	$date = date("Y-m-d");
    $entry_date = date("Y-m-d H:i:s",time()-3600); 
	$timber_balance = 'yes';
	$entry = '41';
	$userId = $_SESSION['username'];
	
$table_name = $_SESSION['db_prefix']."_cutting_bill";
$cutting_query = "SELECT * FROM $table_name WHERE job_no = '".$_SESSION['job_no']."' ";
$cutting_result = mySQL_query($cutting_query);
$bill_id = mysql_result($cutting_result, 0, "bill_id");

$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
$truss_query = "SELECT * FROM $table_name WHERE type = 'Truss' AND bill_id = '".$bill_id."' ";
$truss_result = mySQL_query($truss_query);
$truss_num = mysql_num_rows($truss_result);

$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
$timber_query = "SELECT * FROM $table_name WHERE type = 'Timber' AND bill_id = '".$bill_id."' ";
$timber_result = mySQL_query($timber_query);
$timber_num = mysql_num_rows($timber_result);

$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
$plates_query = "SELECT * FROM $table_name WHERE type = 'Plate' AND bill_id = '".$bill_id."' ";
$plates_result = mySQL_query($plates_query);
$plates_num = mysql_num_rows($plates_result);

$email_message='';

$table_name = $_SESSION['db_prefix']."_cutting_supplies";
$cutting_query = "INSERT INTO $table_name (job_no, follow_no, date ) VALUES ('".$_SESSION['job_no']."', '".$_SESSION['follow_no']."', '".$date."' ) ";
execsql($cutting_query);

$table_name = $_SESSION['db_prefix']."_cutting_supplies";
$cutting_supply_query = "SELECT cutting_supply_id FROM $table_name ORDER BY cutting_supply_id DESC LIMIT 1 ";
$cutting_supply_result = mySQL_query($cutting_supply_query);
$cutting_supply_id = mysql_result($cutting_supply_result, 0, "cutting_supply_id");


$table_name1 = $_SESSION['db_prefix']."_cutting_bill_lines";
$table_name3 = $_SESSION['db_prefix']."_cutting_supplies_lines";
$table_name7 = $_SESSION['db_prefix']."_store_items";
$table_name6 = $_SESSION['db_prefix']."_captured_timber";
$table_name5 = $_SESSION['db_prefix']."_item_history";	
$j=0;
echo '<br>*****timber num:'.$timber_num;
while ($j<$timber_num) {
	$store_item_id = mysql_result($timber_result, $j, "store_item_id");	
	echo '<br>*****'.$store_item_id .'='.$_SESSION['timber_no'.$j];
	
$price_query = "SELECT price, quantity, minimumStock, item_name FROM $table_name7 WHERE store_item_id = '".$store_item_id."' ";
$price_result = mySQL_query($price_query);
$price = mysql_result($price_result, 0, "price");
$stock_quantity = mysql_result($price_result, 0, "quantity");
$min_stock = mysql_result($price_result, 0, "minimumStock");
$item_name = mysql_result($price_result, 0, "item_name");
	
if ($_SESSION['timber_no'.$j]!=0) {
	echo '<br>*****'.'Is this repeating itself?'.$item_name.'-'.$_SESSION['timber_no'.$j];
	$timber_query = "INSERT INTO $table_name3 (cutting_supply_id, job_no, type, store_item_id, quantity, price ) VALUES ('".$cutting_supply_id."',  '".$_SESSION['job_no']."', 'timber', '".$store_item_id."', '".$_SESSION['timber_no'.$j]."', '".$price."' ) ";
	execsql($timber_query);
	
	$stock_query = "UPDATE $table_name7 SET reserved = reserved - ".$_SESSION['timber_no'.$j].", quantity = quantity-".$_SESSION['timber_no'.$j]." WHERE store_item_id = '".$store_item_id."' ";
	execsql($stock_query);
	
$historyQuery = "INSERT INTO $table_name5 (store_item_id, entry, reference, quantity, total, price, date, entryDate, userId) VALUES ('".$store_item_id."', '".$entry."', '".$_SESSION['job_no']."','".$_SESSION['timber_no'.$j]."', '".$stock_quantity."', '".$price."', '".$date."','".$entry_date."','".$userId."')";
	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 ('".$entryDate."' ,'".$_SESSION['username']."', '14', '".$store_item_id."', '".$_SESSION['timber_no'.$j]."' ,'','".$_SESSION['job_no']."' )";
$trackingResult = mySQL_query($trackingQuery);

$stock_quantity = $stock_quantity-$_SESSION['timber_no'.$j];
if ($stock_quantity<=$min_stock) {
	   	   	$sub_category='Timber';
			$total_quantity = $stock_quantity;
			echo '<br>*****'.$total_quantity; 
		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."', '".$_SESSION['timber_no'.$j]."', '".$stock_quantity."', 'minimum stock', 'Cutting supply', '".$_SESSION['job_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>';
	}		
	
	
	$item_query = "SELECT depth, width, class, length FROM $table_name7 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*$_SESSION['timber_no'.$j])/1000;
	
	$reserved_query = "UPDATE $table_name6 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;
}

$timber2_query = "SELECT * FROM $table_name1 WHERE type = 'timber' AND bill_id = '".$bill_id."' ";
$timber2_result = mySQL_query($timber2_query);
$a2 = mysql_result($timber2_result, 0, "quantity");

$table_name2 = $_SESSION['db_prefix']."_cutting_supplies_lines";
$supplied_query = "SELECT SUM(quantity) AS summed_quantity FROM $table_name2 WHERE store_item_id = '".$store_item_id."' 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) {$timber_balance='no';}
$j++;
}

$k=0;
while ($k<$plates_num) {
	$store_item_id = mysql_result($plates_result, $k, "store_item_id");	
	
	$price_query = "SELECT price, quantity FROM $table_name7 WHERE store_item_id = '".$store_item_id."' ";
	$price_result = mySQL_query($price_query);
	$price = mysql_result($price_result, 0, "price");
	$stock_quantity = mysql_result($price_result, 0, "quantity");

	$stock_quantity = $stock_quantity-$_SESSION['plates_no'.$k];
		
if ($_SESSION['plates_no'.$k]!=0) {
	$plates_query = "INSERT INTO $table_name3 (cutting_supply_id, job_no, type, store_item_id, quantity, price ) VALUES ('".$cutting_supply_id."', '".$_SESSION['job_no']."', 'plate', '".$store_item_id."', '".$_SESSION['plates_no'.$k]."', '".$price."' ) ";
	execsql($plates_query);
	
	$stock_query = "UPDATE $table_name7 SET reserved = reserved - ".$_SESSION['plates_no'.$k].", quantity = quantity-".$_SESSION['plates_no'.$k]." WHERE store_item_id = '".$store_item_id."' ";
	execsql($stock_query);
	
	$historyQuery = "INSERT INTO $table_name5 (store_item_id, entry, reference, quantity, total, price, date, entryDate, userId) VALUES ('".$store_item_id."', '".$entry."', '".$_SESSION['job_no']."','".$_SESSION['plates_no'.$k]."', '".$stock_quantity."', '".$price."', '".$date."','".$entry_date."','".$userId."')";
	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 ('".$entryDate."' ,'".$_SESSION['username']."', '14', '".$store_item_id."', '".$_SESSION['plates_no'.$k]."' ,'','".$_SESSION['job_no']."' )";
$trackingResult = mySQL_query($trackingQuery);	
	
	
}

$timber_query = "SELECT * FROM $table_name1 WHERE type = 'plate' AND bill_id = '".$bill_id."' ";
$timber_result = mySQL_query($timber_query);
$a1 = mysql_result($timber_result, $k, "quantity");


$supplied_query = "SELECT SUM(quantity) AS summed_quantity FROM $table_name3 WHERE store_item_id = '".$store_item_id."' AND job_no = '".$_SESSION['job_no']."'  ";
$supplied_result = mySQL_query($supplied_query);

$a3 = mysql_result($supplied_result, 0, "summed_quantity");
if ($a3=='') {$a3=0;}
$a5 = $a1-$a3;
if ($a5>0) {$timber_balance='no';}	

$k++;
}

if ($timber_balance=='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);
	$j=0;
	while ($j<$cutting_bill_num) {
		$store_item_id = mysql_result($cutting_bill_result, $j, "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);
	
		$j++;
	}
	
	
	$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);
	$j=0;
	while ($j<$cutting_bill_num) {
		$store_item_id = mysql_result($cutting_supplies_result, $j, "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);
		
		$j++;
	}
}

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

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


$job_no = $_SESSION['job_no'];

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

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




