<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_LIST_VALUES';
$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/manufacturing.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 . "/admin/db/list_values_db.inc");

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

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();


page(_($help_context = "List values"), @$_REQUEST['popup'], false, "", $js);

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

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
{
    $input_error = 0;
        $cic_msg = check_invalid_characters($_POST['value_code']);
        if ($cic_msg["success"] !== true){
            $input_error = 1;
            display_error(cic_msg["message"]);  
            set_focus('value_code');
        }
    
   	if (!(isset($_POST['list_code']) && strlen($_POST['list_code']) > 0))
   	{
            $input_error = 1;
            display_error( _("There is no list selected."));
            set_focus('list_code');
   	}
    
   	if (!(isset($_POST['value_code']) && strlen($_POST['value_code']) > 0))
   	{
            $input_error = 1;
            display_error( _("Code cannot be empty."));
            set_focus('value_code');
   	}

   	elseif ($_POST['value_name'] == '')
   	{
            $input_error = 1;
            display_error( _("Value name cannot be empty."));
            set_focus('value_name');
   	}
    
    if ($Mode == 'ADD_ITEM'){
        $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');
        }
    }
    
    $list = get_list($_POST['list_code']);
    if($list['date1_active']){
        if(!is_date(get_post('date1'))){
            $input_error = 1;
            display_error( _("Entered date is not a valid date."));
            set_focus('date1');
        }
    }
    
	if ($Mode == 'ADD_ITEM') 
	{
        $list_type = get_list_type($_POST['list_code']);
        if($list_type['list_type'] != ""){

            $list_type_list_existing_value = get_list_type_list_existing_value($list_type['list_type'], $_POST['value_code']);
            if($list_type_list_existing_value){
                $input_error = 1;
                display_error( _("This code is already available in another list belongs to the same list group."));
                set_focus('value_code');
            }  
        }    
   	}
	
	if ($input_error == 0)
	{
        if($list['date1_active']){
            $date1 = date2sql(get_post('date1'));
        }
        else{
            $date1 = INFINITE_START_DATE;
        }
        
        set_post_not_set_numeric('group_value_id', 0);
        
     	if ($Mode == 'ADD_ITEM') 
       	{
			add_list_value($_POST['list_code'], $_POST['value_code'], $_POST['value_name'], 
                            $_POST['value_description'], input_num('display_order'), $_POST['group_value_id'], 
                            get_post('text1'), get_post('text2'), input_num('amount1'), $date1,
                            $_POST['inactive']); 

    		display_notification(_("New list value has been added."));
       	} else
       	{
			update_list_value($selected_id, $_POST['value_code'], 
                            $_POST['value_name'], $_POST['value_description'], input_num('display_order'), $_POST['group_value_id'], 
                            get_post('text1'), get_post('text2'), input_num('amount1'), $date1,
                            $_POST['inactive']); 

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

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

if ($Mode == 'Delete')
{
    delete_list_value($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>" . _("Select List Code:"). "&nbsp;";
////echo "<center>" ;
////Chaitanya : Manufcatured item visible
//echo lists_list('list_code', '', $_POST['list_code'], false, true);
////echo stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true);
//
//echo "<hr></center>";

start_table(TABLESTYLE);
//value_list_row($label, 'list_code', $_POST['list_code'], null, '', true);
lists_list_row("Select List :", 'list_code', "", null, '', true);
end_table();

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']);

$list = get_list($_POST['list_code']);

$result = get_all_list_values($_POST['list_code']);

br();

div_start('code_table');
	
    $table_width = 60;
    $th = array(_("ID"), _("Code"), _("Name"), _("Description"), _("Display Order"), "Group");
    if(isset($list['id']) && $list['id'] > 0){
        if($list['text1_active']){
            $th[] = $list['text1_name'];
            $table_width += 10;
        }
        if($list['text2_active']){
            $th[] = $list['text2_name'];
            $table_width += 10;
        }
        if($list['amount1_active']){
            $th[] = $list['amount1_name'];
            $table_width += 5;
        }
        if($list['date1_active']){
            $th[] = $list['date1_name'];
            $table_width += 5;
        }
    } 
    
    $th[] = "Status";
    $th[] = "";
    $th[] = "";
    
//	$th[] =	_("Status"), "", "");

    start_table(TABLESTYLE, "width=$table_width%");
        table_header($th);

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

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

            label_cell($myrow["id"]);
            label_cell($myrow["value_code"]);
            label_cell($myrow["value_name"]);
            label_cell($myrow["value_description"]);
            label_cell($myrow["display_order"], 'align=right');
            label_cell($myrow["group_value_name"]);
            
            if($list['text1_active']){
                label_cell($myrow["text1"]);
            }
            if($list['text2_active']){
                label_cell($myrow["text2"]);
            }
            if($list['amount1_active']){
                label_cell(number_format($myrow["amount1"],$list['amount1_decimals']), "align=right");
            }
            if($list['date1_active']){
                label_cell(sql2date($myrow["date1"]), "align=center");
            }
    
            if ($myrow["inactive"] == 0) 
                    $display_status = "Active";
            else 
                    $display_status = "Inactive";
                
            label_cell($display_status, "align=center");
		 	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['group_value_id'] = $myrow["group_value_id"];
            $_POST['display_order'] = $myrow["display_order"];
            $_POST['text1'] = $myrow["text1"];
            $_POST['text2'] = $myrow["text2"];
            $_POST['amount1'] = number_format($myrow["amount1"],$list['amount1_decimals']);
        
        if($list['date1_active']){
            $_POST['date1'] = sql2date($myrow["date1"]);
        }
        else{
            $_POST['date1'] = $myrow["date1"];
        }
        
		$_POST['inactive'] = $myrow["inactive"];
	}
	hidden('selected_id', $selected_id);
} 

if(!isset($_POST['inactive'])) 
    $_POST['inactive'] = 0;

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

hidden('id', $selected_id);

if ($selected_id>0) {
    label_row ("Code:", $_POST['value_code']);
    $_POST['value_code'] = $_POST['value_code'];
    hidden('value_code', $_POST['value_code']);
} else {
    text_row(_("Code:"), 'value_code', null, 25, 20);
}

text_row(_("Name:"), 'value_name', null, 50, 100);
textarea_row(_("Description:"), 'value_description', null, 50, 2);
amount_row('Display Order', 'display_order', null, null, null, 0);

if(isset($list['list_id']) && $list['list_id'] > 0){
    if(strlen($list['group_list_code']) > 2){
        value_list_row("Group :", 'group_value_id', $list['group_list_code'], null, '');
    }
    else{
        hidden("group_value_id", 0);
    }

    if($list['text1_active']){
        text_row($list['text1_name'], 'text1', null, 50, $list['text1_length']);
    }

    if($list['text2_active']){
        text_row($list['text2_name'], 'text2', null, 50, $list['text2_length']);
    }

    if($list['amount1_active']){
        amount_row($list['amount1_name'], 'amount1', null, null, null, $list['amount1_decimals']);
    }

    if($list['date1_active']){
        date_row($list['date1_name'], 'date1');
    }
}

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(2);
end_page();

?>