<?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
$page_security = 'SA_REORDER';

	$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/data_checks.inc");
include_once($path_to_root . "/inventory/includes/inventory_db.inc");
include_once( $path_to_root . "/modules/external/stock/db.php");

$js = "";
if ($SysPrefs->use_popup_windows && $SysPrefs->use_popup_search)
	$js .= get_js_open_window(900, 500);
page(_($help_context = "Stock UOM"), false, false, "", $js);

check_db_has_stock_items(_("There are no inventory items defined in the system."));

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

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

if (list_updated('stock_id')) 
{
	$Ajax->activate('uom');
}

function stock_units_list($name, $value=null, $enabled=true)
{
	$result = get_all_item_units();

        $units['-'] = 'select UOM';
	while($unit = db_fetch($result))
		$units[$unit['abbr']] = $unit['name'];

	echo array_selector($name, $value, $units, array( 'disabled' => !$enabled) );

}
//------------------------------------------------------------------------------------

if(isset($_POST['UpdateData']) && isset($_POST['stock_id']) && isset($_POST['uom'])){
    if(add_stock_uom($_POST['stock_id'], $_POST['uom'])){
        display_notification('Stock Updated.');
    } else {
        display_warning('Error Updating');
    }
}


start_form();

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


{
	echo "<center>" . _("Item:"). "&nbsp;";
	echo stock_items_list('stock_id', $_POST['stock_id'], false, true);

	echo "<hr></center>";
}


set_global_stock_item($_POST['stock_id']);

div_start('uom');
{
    $stock_uom = get_stock_uom($_POST['stock_id']);
	echo "<center>" . _("Units of Measure:"). "&nbsp;";
        //echo stock_units_list('uom', $stock_uom['uom']);
        if(empty($stock_uom['uom'])){
           echo stock_units_list('uom', '-'); 
        } else {
            echo stock_units_list('uom', $stock_uom['uom']);
        }

	echo "<hr></center>";
}
div_end();
submit_center('UpdateData', _("Update"), true, false, 'default');

end_form();
end_page();
