<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_ITEM_SERIAL_NOS';
$path_to_root = "..";
include($path_to_root . "/includes/session.inc");

page(_($help_context = "Serial Numbers"));

include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/inventory/includes/serial_no_validation.inc");
include_once($path_to_root . "/inventory/includes/db/serial_no_db.inc");

simple_page_mode(false);
//----------------------------------------------------------------------------------

if(!isset($_POST['stock_id'])){
    $_POST['stock_id'] = get_global_stock_item();
}
//else{
//    set_global_stock_item($_POST['item']);
//}
    

if (list_updated('stock_id')) 
	$Ajax->activate('serial_no_table');

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{
	//initialise no input errors assumed initially before we test
    $serial_no = $_POST['serial_no'];
    
    $stock_item = get_stock_item_edit_info($_POST['stock_id']);
    
    $info['stock_id'] = $_POST['stock_id'];;
    $info['single_item'] = 1;
    $info['stock_control_method'] = $stock_item['stock_control_method'];
    $info['serial_no'] = $_POST['serial_no'];
    $info['serial_no_id'] = $selected_id;
    $info['Mode'] = $Mode;
    
	$cpsna_msg = can_process_serial_nos($info);
    
	if ($cpsna_msg['success'] == true)
    {
        write_serial_no($info);

        if($selected_id > 0)
        {
            display_notification(_('Selected serial_no has been updated'));
        }
        else
        {
            display_notification(_('New serial_no has been inserted'));
        }    
	}
    
    $Mode = 'RESET';
}

//----------------------------------------------------------------------------------

if ($Mode == 'Delete')
{
//    delete_lot($selected_id);
//    display_notification(_('Selected lot has been deleted'));
    $Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = '';
	$sav = get_post('show_inactive');
	unset($_POST['serial_no']);
	$_POST['show_inactive'] = $sav;
}

//----------------------------------------------------------------------------------
function display_serial_nos($stock_id)
{
    $result = get_serial_nos_for_stock_id($stock_id);
    
    start_table(TABLESTYLE, "width=40%");
    $th = array(_('Serial Number'),_('Item Name'), "", "");
    //inactive_control_column($th);

    table_header($th);
    $k = 0; //row colour counter

    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

        label_cell($myrow["serial_no"]);
        label_cell($myrow["description"]);

        edit_button_cell("Edit".$myrow["id"], _("Edit"));

        delete_button_cell("Delete".$myrow["id"], _("Delete"));

        end_row();
    }

    //inactive_control_row($th);
    end_table(1);
}
start_form();

serial_no_cotrolled_items_row(_("Stock Item:"),'stock_id', $_POST['stock_id'], false, true);
br(2);
div_start('serial_no_table');
display_serial_nos($_POST['stock_id']);
div_end();
//----------------------------------------------------------------------------------
set_global_stock_item($_POST['stock_id']);
start_table(TABLESTYLE2);
//
if ($selected_id != '') 
{
    if ($Mode == 'Edit') {
        //editing an existing item category

        $myrow = get_serial_no_details($selected_id);
        
        $_POST['serial_no']  = $myrow["serial_no"];
        $_POST['serial_no_id']  = $myrow["id"];
        
    }
    if(!isset($_POST['serial_no_id'])){
        $_POST['serial_no_id'] = 0;
    }
}
else{
    $_POST['serial_no_id'] = 0;
}

hidden('selected_id', $_POST['serial_no_id']);
//

text_row(_("Serial Number:"), 'serial_no', null, 50, 50);
  
end_table(1);
br(2);
 
submit_add_or_update_center($selected_id == '', '', 'both');

end_form();

end_page();

?>
