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

$js = '';

if (user_use_date_picker()) {
        $js .= get_js_date_picker();
}

if (!@$_GET['popup'])
        page(_($help_context = "Inventory Item Alternate Units"),false,false,"",$js);

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

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

check_db_has_sales_types(_("There are no sales types in the system. Please set up sales types befor entering pricing."));

simple_page_mode(true);
//---------------------------------------------------------------------------------------------------
$input_error = 0;

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

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

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

if (!@$_GET['popup'])
{
	echo "<center>" . _("Item:"). "&nbsp;";
//	echo sales_items_list('stock_id', $_POST['stock_id'], false, true, '', array('editable' => false));
        stock_items_list_cells(_("Select an item:"), 'stock_id', null,
	  _('New item'), true, check_value('show_inactive'));
	echo "<hr></center>";
}
else
	br(2);
set_global_stock_item($_POST['stock_id']);

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

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

	if (!check_num('convertion_factor', 0.001))
	{
		$input_error = 1;
		display_error( _("The convertion factor must be positive and numeric."));
		set_focus('convertion_factor');
	}
   	elseif ($Mode == 'ADD_ITEM')
   	{
            $item = get_item($_POST['stock_id']);
            $item_unit = get_item_unit($item['units']);
            if($item['units'] != $item_unit['abbr']){
                $input_error = 1;
                display_error( _("The base unit is changed in the item master."));
            }
                
	}

	if ($input_error != 1)
	{

            if ($selected_id != -1) 
            {
                //editing an existing unit
                update_alternate_unit($selected_id, $_POST['alternate_unit'], input_num('convertion_factor'), 
                        $_POST['inactive']);

                $msg = _("This alternate unit has been updated.");
            }
            else
            {

                add_alternate_unit($_POST['stock_id'], $_POST['alternate_unit'], $item_unit['abbr'],
                input_num('convertion_factor'), $_POST['inactive']);

                $msg = _("The new alternate unit has been added.");
            }
            
            display_notification($msg);
            $Mode = 'RESET';
	}

}

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

if ($Mode == 'RESET')
{
	$selected_id = -1;
        unset($_POST['alternate_unit']);
        unset($_POST['convertion_factor']);
}

if (list_updated('stock_id')) {
	$Ajax->activate('units_table');
	$Ajax->activate('units_details');
}
if (list_updated('stock_id') || isset($_POST['_curr_abrev_update']) || isset($_POST['_sales_type_id_update'])) {
	// after change of stock, currency or salestype selector
	// display default calculated price for new settings. 
	// If we have this price already in db it is overwritten later.
	unset($_POST['price']);
	$Ajax->activate('units_details');
}

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

$alternate_units = get_alternate_unit_all_details($_POST['stock_id']);
$item = get_item($_POST['stock_id']);
$item_unit = get_item_unit($item['units']);

div_start('units_table');
start_table(TABLESTYLE, "width=30%");

$th = array("", _("Alt Unit"), "", _("Convertion"), _("Base Unit"),_("Inactive"), "");
//inactive_control_column($th);
table_header($th);
$k = 0; //row colour counter
$calculated = false;

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

    label_cell(spaces(2)."1".spaces(2), "align=center");
    label_cell($myrow["alternate_unit_name"], "align=center");
    label_cell(spaces(2)."=".spaces(2), "align=center");
//    amount_decimal_cell($myrow["convertion_factor"]);
    label_cell($myrow["convertion_factor"], "align=right");
    label_cell($myrow["base_unit_name"], "align=center");
    
    if($myrow["inactive"] == 0){
        $inactive = 'No';
    }
    else{
        $inactive = 'Yes';
    }
    label_cell($inactive, "align=center");
    
    if($myrow['alternate_unit'] == $myrow['base_unit']){
        label_cell("");
    }
    else{
        edit_button_cell("Edit".$myrow['id'], _("Edit"));
    }
//    delete_button_cell("Delete".$myrow['id'], _("Delete"));
    end_row();
}

end_table();
if (db_num_rows($alternate_units) == 0)
{
    display_note(_("There are no alternate units this part."), 1);
}
div_end();
//------------------------------------------------------------------------------------------------

echo "<br>";

if ($Mode == 'Edit')
{
	$myrow = get_alternate_unit_details($selected_id);
	$_POST['alternate_unit'] = $myrow["alternate_unit"];
        $_POST['inactive'] = $myrow["inactive"];
	$_POST['convertion_factor'] = $myrow["convertion_factor"];
}

hidden('selected_id', $selected_id);
if (@$_GET['popup'])
{
	hidden('_tabs_sel', get_post('_tabs_sel'));
	hidden('popup', @$_GET['popup']);
}
div_start('units_details');
start_table(TABLESTYLE2);

label_row(_("Base Unit:"), $item_unit['name']);

stock_units_list_row(_("Alternate Units:"), 'alternate_unit', null);

amount_row(_("Base Unit to Alt. Unit Convertion Factor:"), 'convertion_factor', null, null, null, 12);

yesno_list_row(_("Inactive:"), 'inactive');

end_table(1);

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

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