<?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");
include_once($path_to_root . "/inventory/includes/db/item_bin_locations_db.inc");

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

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


function can_process_bin_location($selected_id)
{
   	if (!isset($_POST['stock_id']) || $_POST['stock_id'] == "" )
   	{
      	display_error( _("There is no item selected."));
		set_focus('stock_id');
        return false;
   	}
    
    if (!isset($_POST['bin_location']) || $_POST['bin_location'] == "")
   	{
      	display_error( _("There is no bun location entered."));
		set_focus('stock_id');
        return false;
   	}
    
    $bin = get_item_bin_location_by_name($_POST['stock_id'], $_POST['bin_location'],$selected_id);
   	if ($bin)
   	{
      	display_error( _("Entered bin location is already defined for this item."));
		set_focus('stock_id');
        return false;
   	}
    
    return true;
    
}
//--------------------------------------------------------------------------------------------------
$input_error = 0;

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
{
    if(can_process_bin_location($selected_id)){
        if ($input_error == 0)
        {
            if ($Mode == 'ADD_ITEM') 
            {
                add_item_bin_location($_POST['stock_id'], $_POST['bin_location']);

                display_notification(_("Item Bin Location has been added."));
            } 
            else
            {
                update_item_bin_location($selected_id, $_POST['stock_id'], $_POST['bin_location']);
                display_notification(_("Item Bin Location has been updated."));
            }
            $Mode = 'RESET';
        }
    }
}

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

if ($Mode == 'Delete')
{
        delete_item_bin_location($selected_id, $_POST['stock_id']);
	display_notification(_("The item bin location 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_item_bin_locations($_POST['stock_id']);
div_start('price_table');
if (db_num_rows($result) == 0)
{
    display_note(_("There are no bin locations defined for this stock item"));
}
else
{
    start_table(TABLESTYLE, "width=50%");

            $th = array(_("Bin Location"), "", "");

    table_header($th);

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

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

        label_cell($myrow["bin_location"]);

        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_table();
}
div_end();
//}

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

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

    $_POST['bin_location'] = $myrow["bin_location"];
//    $_POST['inactive'] = $myrow["inactive"];
}

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

$bin_location = "";
$inactive = "";

start_table(TABLESTYLE2);

if(isset($_POST['bin_location']))
    $bin_location = $_POST['bin_location'];
text_row(_("Bin Location:"), 'bin_location', $bin_location, 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);
}
?>
