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

page(_($help_context = "List values"));

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");

//below file should have been included by the system already. But it is not. So included manually. Once the issues found remove this inclusion
include_once($path_to_root . "/purchasing/includes/db/pr_approval_hierarchy_db.inc");

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

simple_page_mode(true);
//--------------------------------------------------------------------------------------------------

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

   	$input_error = 0;
   	if ($_POST['list_code'] == "" || !isset($_POST['list_code']))
   	{
      	$input_error = 1;
      	display_error( _("There is no list selected."));
		set_focus('list_code');
   	}

   	elseif ($_POST['value_name'] == '')
   	{
      	$input_error = 1;
      	display_error( _("Value name cannot be empty."));
		set_focus('value_description');
   	}
	elseif($selected_id == -1)
	{
            $existing_values = get_existing_values($_POST['list_code'],$_POST['value_code']);

            if (db_num_rows($existing_values)) {
		  	$input_error = 1;
    	  	display_error( _("This code is already available in the selected list."));
			set_focus('value_code');
		}
   	}
	
	if ($input_error == 0)
	{
     	if ($Mode == 'ADD_ITEM') 
       	{
			add_list_value($_POST['list_code'], $_POST['value_code'], $_POST['value_name'], 
				$_POST['value_description'], $_POST['inactive']); 

    		display_notification(_("New list value has been added."));
       	} else
       	{
			update_list_value($selected_id, $_POST['list_code'], $_POST['value_code'], 
				$_POST['value_name'], $_POST['value_description'], $_POST['inactive']); 

    	  	display_notification(_("List value has been updated."));
       	}
		$Mode = 'RESET';
	}
}

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

if ($Mode == 'Delete')
{
	delete_item_code1($selected_id);
	
	display_notification(_("Item code has been sucessfully deleted."));
	$Mode = 'RESET';
}

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

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

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

start_form();


if (!isset($_POST['list_code']))
	$_POST['list_code'] = get_global_list_code();

//echo "<center>" . _("Item:"). "&nbsp;";
echo "<center>" ;
//Chaitanya : Manufcatured item visible
echo lists_list('list_code', 'PR_HIERARCHY', $_POST['list_code'], false, true);
//echo stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true);

echo "<hr></center>";

set_global_list_code($_POST['list_code']);

/*
$result = get_item_code_dflts1($_POST['list_code']);
$dec = $result['decimals'];
$units = $result['units'];
$dflt_desc = $result['description'];
$dflt_cat = $result['category_id'];
*/
//$_POST['inactive'] =0;
//var_dump($_POST['inactive']);

$result = get_all_approval_hierarchy_records($_POST['list_code']);
div_start('code_table');
	start_table(TABLESTYLE, "width=60%");

	$th = array(_("Seq. No."), _("Name"), _("Approval Group"), 
		_("Status"), "", "");

        table_header($th);

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

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

            label_cell($myrow["value_code"]);
            label_cell($myrow["value_name"]);
            label_cell($myrow["value_description"]);
            
            if ($myrow["inactive"] == 0) 
                    $display_status = "Active";
            else 
                    $display_status = "Inactive";
                
            label_cell($display_status);
		 	edit_button_cell("Edit".$myrow['id'], _("Edit"));
		 	edit_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();

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

if ($selected_id != '') {
	if ($Mode =='Edit')
	{
		$myrow = get_list_value($selected_id);
		$_POST['value_code'] = $myrow["value_code"];
		$_POST['value_name'] = $myrow["value_name"];
		$_POST['value_description'] = $myrow["value_description"];
		$_POST['inactive'] = $myrow["inactive"];
	}
	hidden('selected_id', $selected_id);
} else {
	$_POST['value_code'] = '';
	$_POST['value_name'] = '';
        $_POST['value_description'] = '';
	$_POST['inactive'] = 0;
}
if(!isset($_POST['inactive'])) 
    $_POST['inactive'] = 0;

echo "<br>";
start_table(TABLESTYLE2);

hidden('id', $selected_id);



text_row(_("Seq. No.:"), 'value_code', null, 20, 21);
text_row(_("Name:"), 'value_name', null, 50, 100);
value_list_row("Approval Group:", 'value_description', 'LH_PR_APPROVAL_GROUP');
//textarea_row(_("Description:"), 'value_description', null, 50, 2);
yesno_list_row(_("Value Status:"), 'inactive', 
        $_POST['inactive'], 'Inactive', 'Active');

//record_status_list_row(_("Value Status:"), 'inactive1');

end_table(1);

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

end_form();
end_page();

?>
