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

date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 

$recycle_no = $_GET['recycle_text'];
$product_item_id = $_GET['product_item_id_text'];
$batch_code = $_GET['batch_code_text'];
$base_product_item_id = $_GET['base_product_item_id_text'];
$recycle_reason = $_GET['reason_select'];
$recycle_notes = $_GET['notes_text'];

$table_name2 = $_SESSION['db_prefix']."_product_store_lines";      
$reduce_query = "UPDATE $table_name2 SET quantity_out = quantity_out + '".$recycle_no."' WHERE product_item_id = '".$product_item_id."' AND batch_code = '".$batch_code."' ";
$reduce_result = $con->query($reduce_query);

$table_name2 = $_SESSION['db_prefix']."_product_store";      
$recycle_query = "UPDATE $table_name2 SET recycle_qty = recycle_qty + '".$recycle_no."' WHERE product_item_id = '".$base_product_item_id."'";
$recycle_result = $con->query($recycle_query);

$table_name = $_SESSION['db_prefix']."_product_store";
$recycle_stock_query = "SELECT quantity, recycle_qty FROM $table_name WHERE product_item_id = '".$base_product_item_id."'";
$recycle_stock_result = $con->query($recycle_stock_query);
$row = mysqli_fetch_assoc($recycle_stock_result);
$recycle_stock = $row['recycle_qty'];


$table_name2 = $_SESSION['db_prefix']."_recycle_history";	
$recycle_history_query = "INSERT INTO $table_name2 (product_item_id, batch_code, base_item_id, entry, recycle_no, recycle_stock, date, recycle_reason, recycle_notes, entryDate, userId) VALUES ('".$product_item_id."', '".$batch_code."', '".$base_product_item_id."', '10', '".$recycle_no."', '".$recycle_stock."', '".$entry_date."',  '".$recycle_reason."', '".$recycle_notes."', '".$entry_date."','".$_SESSION['username']."')";
$recycle_history_result = $con->query($recycle_history_query);

$table_name = $_SESSION['db_prefix']."_product_store";
$recycle_stock_query = "SELECT quantity, recycle_qty FROM $table_name WHERE product_item_id = '".$product_item_id."'";
$recycle_stock_result = $con->query($recycle_stock_query);
$row = mysqli_fetch_assoc($recycle_stock_result);
$stock_quantity = $row['quantity'];

$new_stock_quantity = $stock_quantity - $recycle_no;

$table_name = $_SESSION['db_prefix']."_product_store";
$product_update_query = "UPDATE $table_name SET quantity = '".$new_stock_quantity."' WHERE product_item_id = '".$product_item_id."'";
$product_update_result = $con->query($product_update_query);

$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_item_id."', '29', '', '-".$recycle_no."',  '".$new_stock_quantity."', '".$entry_date."','".$entry_date."','".$_SESSION['username']."')";
$history_result = $con->query($history_query);




$table_name = $_SESSION['db_prefix']."_tracking_store";
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '28', '".$product_item_id."', '".$batch_code."' ,'".$base_product_item_id."','".$recycle_no."' )";
$tracking_result = $con->query($tracking_query);


?>

<script language="javascript" type="text/javascript">
        
		 window.location="store_main.php?date=0";
        
       </script>