<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_ITEM';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");

if (!@$_GET['popup'])
	page(_($help_context = "Item Barcodes"));

//check_db_has_purchasable_items(_("There are no purchasable inventory items defined in the system."));
//check_db_has_suppliers(_("There are no suppliers defined in the system."));

//----------------------------------------------------------------------------------------
simple_page_mode(true);
if (isset($_GET['stock_id']))
{
	$_POST['stock_id'] = $_GET['stock_id'];
}

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

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
{

   	$input_error = 0;
   	if ($_POST['stock_id'] == "" || !isset($_POST['stock_id']))
   	{
      	$input_error = 1;
      	display_error( _("There is no item selected."));
		set_focus('stock_id');
   	}
        
   	if (get_barcode($_POST['barcode'],$selected_id))
   	{
      	$input_error = 1;
      	display_error( _("This barcode has already been used."));
		set_focus('barcode');
	}
        
	if ($input_error == 0)
	{
     	if ($Mode == 'ADD_ITEM') 
       	{
            add_item_barcode($_POST['stock_id'], $_POST['barcode'], $_POST['inactive']);
            
            display_notification(_("Barcode has been added."));
       	} 
       	else
       	{
            update_item_barcode($selected_id, $_POST['stock_id'], $_POST['barcode'], $_POST['inactive']);
            display_notification(_("Barcode has been updated."));
       	}
		$Mode = 'RESET';
	}
}

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

if ($Mode == 'Delete')
{
        delete_item_barcode($selected_id, $_POST['stock_id']);
	display_notification(_("The item barcode has been sucessfully deleted."));
	$Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
}

if (isset($_POST['_selected_id_update']) )
{
	$selected_id = $_POST['selected_id'];
	$Ajax->activate('_page_body');
}

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

if (!@$_GET['popup'])
	start_form();

if (!isset($_POST['stock_id']))
	$_POST['stock_id'] = get_global_stock_item();

if (!@$_GET['popup'])
{
	echo "<center>" . _("Item:"). "&nbsp;";
	//Chaitanya : All items can be purchased
	echo stock_items_list('stock_id', $_POST['stock_id'], false, true);
	//echo stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true);
	echo "<hr></center>";
}
else
	br(2);

set_global_stock_item($_POST['stock_id']);

$result = get_items_barcodes($_POST['stock_id']);
div_start('price_table');
if (db_num_rows($result) == 0)
{
    display_note(_("There are no barcords defined for this stock item"));
}
else
{
    start_table(TABLESTYLE, "width=50%");

            $th = array(_("Barcode"), _("Obsolete"), _("Created On"),
                    _("Updated On"), "", "");

    table_header($th);

    $k = $j = 0; //row colour counter

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

        label_cell($myrow["barcode"]);

        if($myrow["inactive"] == 1)
            $obsolete = "Yes";
        else
            $obsolete = "No";

        label_cell($obsolete);
        label_cell($myrow["created_on"]);
        if ($myrow["updated_on"] != INFINITE_START_DATE.' 00:00:00') {
            label_cell($myrow["updated_on"]);
        } else {
            label_cell('');
        }
                    edit_button_cell("Edit".$myrow['id'], _("Edit"));
                    delete_button_cell("Delete".$myrow['id'], _("Delete"));
        end_row();

        $j++;
        If ($j == 12)
        {
            $j = 1;
                    table_header($th);
        } //end of page full new headings
    } //end of while loop

    end_table();
}
div_end();
//}

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

$dec2 = 6;
if ($Mode =='Edit')
{
    $myrow = get_item_barcode_details($selected_id);

    $_POST['barcode'] = $myrow["barcode"];
    $_POST['inactive'] = $myrow["inactive"];
}

br();
hidden('selected_id', $selected_id);
if (@$_GET['popup'])
{
	hidden('_tabs_sel', get_post('_tabs_sel'));
	hidden('popup', @$_GET['popup']);
}

$barcode = "";
$inactive = "";

start_table(TABLESTYLE2);

if(isset($_POST['barcode']))
    $barcode = $_POST['barcode'];
text_row(_("Barcode:"), 'barcode', $barcode, 50, 128);

if(isset($_POST['inactive']))
    $inactive = $_POST['inactive'];
yesno_list_row("Obsolete:", "inactive", $inactive);

end_table(1);

submit_add_or_update_center($selected_id == -1, '', 'both');

if (!@$_GET['popup'])
{
	end_form();
	end_page(@$_GET['popup'], false, false);
}
?>
