<?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 = "Serial Nos 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");
include_once($path_to_root . "/inventory/includes/db/serial_no_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["serial_nos"] as $s){
        $name = implode("#",array($_POST['stock_id'],"lot",$s,"tran_qty"));
        
        $serial_no = '';
        $tran_qty = 0;
        if(strlen($_POST[$name])  > 0){
            $serial_no = $_POST[$name];
            $existing_serial_no = get_existing_serial_no($_POST['stock_id'], $serial_no);
            if($existing_serial_no){
                $tran_qty = 1;
                $info = array("lot_id" => $existing_serial_no['id'], "lot_reference" => $serial_no, "tran_qty" => 1, "row_state" => $existing_serial_no['row_state']);
                $lot_qty_array[$name] = $info;
            }
            else{
                display_error("Serial no [".$serial_no."] is not defined in the system.");
                $val1 = false;
                break;
            }
        }
        
        $sum += $tran_qty;
    }
    
    $_SESSION["lot_info#".$_POST['parent_guid']][$_POST['stock_id']]=$lot_qty_array;
    
    if($sum != $_POST["parent_item_qty"]){
        display_error("Tthe total number of serial numbers entered is different than in the total quantity 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){
        process_lot_to_parent(null,$_POST['parent']);
    }
    
}


function create_serial_no_box(&$boxes_in_the_row){
    
    if($boxes_in_the_row >= 5){
        end_row();
        $boxes_in_the_row = 0;
    }
    
    if($boxes_in_the_row == 0){
        start_row();
    }
    
    text_cells(null, "serial_no", '');
    
    $boxes_in_the_row += 1;
}

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'];
}

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);

start_table(TABLESTYLE, "width=60%");
$th = array(_("Serial No"), _("Serial No"), _("Serial No"), _("Serial No"), _("Serial No"));
//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>";
$parent_qty_1111= js_get_data_in_parent($parent,"parent_item_qty");
$parent_quantity_entered = $parent_qty_1111;

$parent_quantity_entered = 100;

$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);

$k = 0;
$boxes_in_the_row = 0;
for ($i = 0; $i < $parent_quantity_entered; $i++) {
    //create_serial_no_box($boxes_in_the_row);
	
	
    
    if($boxes_in_the_row >= 5){
        end_row();
        $boxes_in_the_row = 0;
    }
    
    if($boxes_in_the_row == 0){
        //start_row();
        alt_table_row_color($k);
    }
    
    $boxes_in_the_row += 1;

    $var_name = implode("#",array($stock_id,"lot",$i,"tran_qty"));
    hidden($var_name, 1);
    
    $v = get_session_variable("lot_info#".$guid_ref);
    if(isset($v[$stock_id][$var_name]['lot_reference'])){
        $serial_no = $v[$stock_id][$var_name]['lot_reference'];
    }
    else{
        $serial_no = '';
    }
    
    //qty_cells(null, $var_name, $entered_qty, null, null, null);
    text_cells(null, $var_name, $serial_no);
    
    //hidden("lot_details[]",array("lot_id" =>$myrow["id"],"row_state" =>$myrow["row_state"]));
    hidden("serial_nos[]",$i);
//    hidden("row_states[".$i,0);
    //hidden("lot_reference[".$i."]",$myrow["serial_no"]);
	//end_row();
}

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();
