<?php
$page_security = 'SA_INVENTORYADJUSTMENT';
$path_to_root = "../..";
include_once($path_to_root . "/includes/db_pager.inc");
include($path_to_root . "/includes/session.inc");
$js = "";
if ($SysPrefs->use_popup_windows)
    $js .= get_js_open_window(900, 500);
if (user_use_date_picker())
    $js .= get_js_date_picker();
page(_($help_context = "Item Lot list"), @$_REQUEST['popup'], false, "", $js);

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/inventory/includes/inventory_db.inc");
//include_once($path_to_root . "/inventory/includes/lot_list_ui.inc");
include_once($path_to_root . "/inventory/includes/db/item_lot_db.inc");
include_once($path_to_root . "/inventory/includes/db/items_units_db.inc");


if(isset($_POST["Submit"])){
    //Process button is clicked, do validation
    $val1 = true;
    //validate to inputted qty in parent window
    $sum = 0;
    $lot_qty_array = array();
    foreach($_POST["lot_ids"] as $s){
        $name = implode("#",array($_POST['line_ref'],$_POST['stock_id'],"lot",$s,"tran_qty"));
        
        $tran_qty= 0;
        if($_POST[$name]  > 0){
            $tran_qty = $_POST[$name];
        }
        $info = array("line_ref" => $_POST['line_ref'], 
                        "stock_id" => $_POST['stock_id'], 
                        "lot_id" => $s, 
                        "lot_reference" => $_POST["lot_references"][$s], 
                        "tran_qty" => $tran_qty, 
                        "row_state" => $_POST["row_states"][$s]);
        
        $lot_qty_array[$name] = $info;
        $sum += $_POST[$name];
    }
    if($sum != $_POST["parent_item_qty"]){
        display_error("Tthe total number of item defined in lot is different than in the main form.");
        $val1 = false;
    }
    
    //validate to database
//    $val2 = qty_lot_validation($_POST['lot_id'],$lot_qty_array);
//    if(!$val2){
//        display_error("The lot data inputtedd are different from the database");
//    }
    
    //validation success
    if($val1){
        $stock_item_line_ref = $_POST['line_ref']."_".$_POST['stock_id'];
        $lot_info_name = "lot_info#".$_POST['parent_guid'];
        $_SESSION[$lot_info_name][$stock_item_line_ref]=$lot_qty_array;
        process_lot_to_parent(null,$_POST['parent']);
    }
    
}

if (isset($_GET['stock_id'])){
    $stock_id = $_GET['stock_id'];
}else{
    $stock_id = $_POST['stock_id'];
}
if (isset($_GET['parent'])){
    $parent = $_GET['parent'];
}else{
    $parent = $_POST['parent'];
}

$parent_unit = "";
if(isset($_GET['unit_of_parent'])){
    $parent_unit = $_GET['unit_of_parent'];
}else{
    $parent_unit = $_POST['unit_of_parent'];
}

$guid_ref = "";
if(isset($_GET['value_ref'])){
    $guid_ref = $_GET['value_ref'];
}else{
    $guid_ref = $_POST['value_ref'];
}

$line_ref = "";
if(isset($_GET['line_ref'])){
    $line_ref = $_GET['line_ref'];
}else{
    $line_ref = $_POST['line_ref'];
}

start_form();

hidden("parent",$parent);
hidden("parent_guid");
hidden("stock_id",$stock_id);
hidden("parent_item_qty");
hidden("parent_unit",$parent_unit);
hidden("unit_of_parent",$parent_unit);
hidden("value_ref",$guid_ref);
hidden("line_ref",$line_ref);

start_table(TABLESTYLE, "width=40%");
$th = array(_("Lot Reference"), _("Available Qty"),_("Qty"));
//inactive_control_column($th);
$item = get_item($stock_id);
echo "<h1>Lot List of ".$item["description"]."</h1>";
if (isset($_POST["parent_item_qty"])){
    $view = $_POST["parent_item_qty"];
}else{
    $view = "";
}
echo "<h2>Total quantity entered <span id='parent_qty_entered_view'>".$view."</span></h1>";

$unit_name = get_unit_descr($parent_unit);
display_heading("Selected unit: ".$unit_name);
br();

$alternate_unit = get_alternate_unit_info($stock_id, $parent_unit);
$convertion_factor = $alternate_unit['convertion_factor'];

table_header($th);
$result = get_available_lots($stock_id);
$k = 0;
while ($myrow = db_fetch($result)) 
{
	
	alt_table_row_color($k);
	
	label_cell($myrow["lot_reference"]);
    
    $alternate_unit_qty = number_format($myrow["qty"] / $convertion_factor, 2);
	label_cell($alternate_unit_qty, "align=right");
        
    $entered_qty = 0;
    
    $lot_details_line_ref = implode("#",array($line_ref,$stock_id,"lot",$myrow["id"],"tran_qty"));
    $v = get_session_variable("lot_info#".$guid_ref);
    $stock_item_line_ref = $line_ref."_".$stock_id;
    if(isset($v[$stock_item_line_ref])){
        $r = $v[$stock_item_line_ref];
        if(isset($r[$lot_details_line_ref])){
            $lot_details = $r[$lot_details_line_ref];
            $entered_qty = $lot_details['tran_qty'];
        }
    }
    
    qty_cells(null, $lot_details_line_ref, $entered_qty, null, null, null);
    
    //hidden("lot_details[]",array("lot_id" =>$myrow["id"],"row_state" =>$myrow["row_state"]));
    hidden("lot_ids[]",$myrow["id"]);
    hidden("lot_references[".$myrow["id"]."]",$myrow["lot_reference"]);
    hidden("row_states[".$myrow["id"]."]",$myrow["row_state"]);
	end_row();
}

//inactive_control_row($th);
end_table(1);
echo "<center>";
submit("Submit", "submit");
echo "<input type='button' name='cancel' class='inputsubmit' onclick='goBack()' value='cancel'/>";
echo "</center>";
end_form();

js_get_data_in_parent("guid","parent_guid");
js_get_data_in_parent($parent,"parent_item_qty");
js_get_data_in_parent($parent_unit,"parent_unit");
show_quantity("parent_item_qty");
end_page();
