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

page(_($help_context = "Lots"));

include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/inventory/includes/lots_validation.inc");
include_once($path_to_root . "/inventory/includes/db/lots_db.inc");

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

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

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

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{
	//initialise no input errors assumed initially before we test
    
    $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['lot_reference'] = $_POST['lot_reference'];
    $info['lot_id'] = $selected_id;
    $info['Mode'] = $Mode;
    
	$cpl_msg = can_process_lot($info);
    
	if ($cpl_msg['success'] == true)
        {
            write_lot($info);
            
            if($selected_id != '')
            {
                display_notification(_('Selected lot has been updated'));
            }
            else
            {
                display_notification(_('New lot 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['lot_reference']);
	$_POST['show_inactive'] = $sav;
}

//----------------------------------------------------------------------------------
function display_lots($stock_id)
{
    $result = get_lots_for_stock_id($stock_id);
    
    start_table(TABLESTYLE, "width=40%");
    $th = array(_('Lot Reference'),_('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["lot_reference"]);
        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();

start_table(TABLESTYLE2);
    lot_cotrolled_items_row(_("Stock Item:"),'stock_id', $_POST['stock_id'], false, true);
end_table(2);

div_start('lot_table');
display_lots($_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_lot_details($selected_id);
        
        $_POST['lot_reference']  = $myrow["lot_reference"];
        $_POST['lot_id']  = $myrow["id"];
        
    }
    if(!isset($_POST['lot_id'])){
        $_POST['lot_id'] = 0;
    }
}
else{
    $_POST['lot_id'] = 0;
}

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

text_row(_("Lot Reference:"), 'lot_reference', null, 50, 50);
  
end_table(2);
 
submit_add_or_update_center($selected_id == '', '', 'both');

end_form(2);

end_page();

?>
