<?php
include('../include/session.php');
date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 

$production_no = $_GET['production_no'];
$mix_design_no = $_GET['mix_design_no'];

$skips_no = $_GET['skips_text'];
$est_qty = $_GET['est_qty_text'];
$produced_no = $_GET['produced_text'];
$production_date = $_GET['production_date_edit'];
$product_product_item_id = $_GET['product_item_id'];
$production_cost = 0;
$skip_products_no = $skips_no * $est_qty;


$table_name = $_SESSION['db_prefix']."_mix_design";
$cost_query = "SELECT design_cost FROM $table_name WHERE design_no = '".$mix_design_no."' ";

$cost_result = $con->query($cost_query);
$row = mysqli_fetch_assoc($cost_result);
$design_cost = $row['design_cost'];


$production_cost = $skips_no * $design_cost;

$table_name = $_SESSION['db_prefix']."_production";
$saved_production_query = "SELECT  skips_no, produced_no, production_date, batch_no, batch_code  FROM $table_name WHERE production_no = '".$production_no."' ";
$saved_production_result = $con->query($saved_production_query);
$row = mysqli_fetch_assoc($saved_production_result);
$saved_skips_no = $row['skips_no'];
$saved_produced_no = $row['produced_no'];
$saved_production_date = $row['production_date'];
$saved_batch_no = $row['batch_no'];
$saved_batch_code = $row['batch_code'];


if ($saved_production_date != $production_date) {
$batch_no = str_pad($saved_batch_no, 2, '0', STR_PAD_LEFT);
$bb_date = date("d-m-Y", strtotime(date("d-m-Y", strtotime($production_date)) . " + 2 year"));
$bb_date =date("dmy", strtotime($bb_date));
$batch_code = $bb_date.'     '.$batch_no.'     '.date("my", strtotime($production_date));
}
else {
$batch_code = $saved_batch_code;
}

$table_name = $_SESSION['db_prefix']."_production";
$update_query = "UPDATE $table_name SET skips_no = '".$skips_no."', skip_products_no = '".$skip_products_no."', production_cost = '".$production_cost."',  produced_no = '".$produced_no."', production_date = '".$production_date."', batch_code = '".$batch_code."',  edit_date = '".$entry_date."' WHERE production_no = '".$production_no."' ";
$update_result = $con->query($update_query);

$table_name = $_SESSION['db_prefix']."_product_store_lines";
$update2_query = "UPDATE $table_name SET production_date = '".$production_date."', batch_code = '".$batch_code."' WHERE production_no = '".$production_no."' ";
$update2_result = $con->query($update2_query);



if ($produced_no!=$saved_produced_no) {
	$table_name = $_SESSION['db_prefix']."_product_store";
	$qty_query = "SELECT quantity FROM $table_name WHERE product_item_id = '".$product_product_item_id."'   ";
	$qty_result = $con->query($qty_query);
	$row = mysqli_fetch_assoc($qty_result);
	$stock_quantity = $row['quantity'];
	
	$diff_produced = $produced_no - $saved_produced_no;	
	$new_stock_quantity = $stock_quantity + $diff_produced;
	
	$table_name = $_SESSION['db_prefix']."_product_store";
	$update_query = "UPDATE $table_name SET quantity = '".$new_stock_quantity."' WHERE product_item_id = '".$product_product_item_id."'   ";
	$update_result = $con->query($update_query);
	
	$table_name = $_SESSION['db_prefix']."_product_store_lines";
	$line_update_query = "UPDATE $table_name SET quantity_in = '".$produced_no."' WHERE production_no = '".$production_no."' ";
	$line_update_result = $con->query($line_update_query);
	
	$table_name = $_SESSION['db_prefix']."_product_store_lines";
	$stock_quantity_query = "SELECT SUM(quantity_in-quantity_out) AS stock_quantity FROM $table_name WHERE product_item_id = '".$product_product_item_id."'  ";
	$stock_quantity_result = $con->query($stock_quantity_query);
	$row = mysqli_fetch_assoc($stock_quantity_result);
	$stock_quantity = $row['stock_quantity'];
		
	 $table_name2 = $_SESSION['db_prefix']."_product_history";	
	 $history_query = "INSERT INTO $table_name2 (product_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$product_product_item_id."', '22', '".$production_no."', '".$diff_produced."',  '".$stock_quantity."', '".$entry_date."','".$entry_date."','".$_SESSION['username']."')";
	 $history_result = $con->query($history_query);
}


if ($skips_no!=$saved_skips_no) {
	 $table_name = $_SESSION['db_prefix']."_tracking_production";
	 $tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' , '".$_SESSION['username']."', '70', '".$production_no."' ,'".$saved_skips_no."','".$skips_no."','".$produced_no."' )";
	 $tracking_result = $con->query($tracking_query);
	 
	 $production_cost = 0;
	 $item_production_cost = 0;
	 	 
	 $table_name = $_SESSION['db_prefix']."_production_mix_lines";
	 $used_query = "SELECT store_item_id, item_quantity, item_price FROM $table_name WHERE production_no = '".$production_no."' ";
	 $used_result = $con->query($used_query);
	 while($row = $used_result->fetch_assoc()) {
		 
	 $store_item_id = $row["store_item_id"];
	 $item_quantity = $row["item_quantity"];
	 $item_price = $row["item_price"];
	 $used_quantity = $saved_skips_no * $item_quantity;
	 $new_used_quantity = $skips_no * $item_quantity;
	 
	 $stock_adjustment = $used_quantity - $new_used_quantity;
	 	 
	 $table_name = $_SESSION['db_prefix']."_store_items";
	 $stock_update_query = "UPDATE $table_name SET quantity = quantity + '".$stock_adjustment."' WHERE store_item_id = '".$store_item_id."'  ";
	 $stock_update_result = $con->query($stock_update_query);
	 
	 $table_name = $_SESSION['db_prefix']."_store_items";
	 $stock_qty_query = "SELECT quantity FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
	 $stock_qty_result = $con->query($stock_qty_query);
	 $stock_qty_num = mysqli_num_rows($stock_qty_result);
	 $row = mysqli_fetch_assoc($stock_qty_result);
	 $stock_quantity = $row['quantity'];
	 
	 
	 $item_production_cost = $new_used_quantity * $item_price;
	 $production_cost = $production_cost + $item_production_cost;
	 
	 	 
	 $table_name2 = $_SESSION['db_prefix']."_item_history";	
     $history_query = "INSERT INTO $table_name2 (store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$store_item_id."', '25', '".$production_no."', '".$stock_adjustment."',  '".$stock_quantity."', '".$entry_date."','".$entry_date."','".$_SESSION['username']."')";
	 $history_result = $con->query($history_query);	 
	 
	 }
	 
	 $table_name = $_SESSION['db_prefix']."_production";
	 $cost_update_query = "UPDATE $table_name SET production_cost = '".$production_cost."' WHERE production_no = '".$production_no."' ";
	 $cost_update_result = $con->query($cost_update_query);

}


if  ($production_date!=$saved_production_date) {
	 $table_name = $_SESSION['db_prefix']."_tracking_production";
	 $tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' , '".$_SESSION['username']."', '83', '".$production_no."' ,'".$production_date."','".$saved_production_date."','' )";
	 $tracking_result = $con->query($tracking_query);
	 
	 $table_name = $_SESSION['db_prefix']."_tracking_production";
	 $tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' , '".$_SESSION['username']."', '84', '".$production_no."' ,'".$batch_code."','".$saved_batch_code."','' )";
	 $tracking_result = $con->query($tracking_query);
}

	
	if (isset($_GET['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="../production/production_slip.php?production_no=<?php echo $production_no?>";
		</script>
        <?php		 
	   }
?>