<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_UTILITY_EXPENSE_TYPES';
$path_to_root = "../..";
include($path_to_root . "/includes/session.inc");
add_access_extensions();
set_ext_domain('/asset_register');

page(_($help_context = "Utility Expense Types"));

include_once($path_to_root . "/gl/includes/db/utility_expense_types_db.inc");
include_once($path_to_root . "/includes/ui.inc");
//include_once($path_to_root . "/includes/ui/ui_lists_custom.inc");
//include_once($path_to_root . "/transport/includes/db/asset_types_db.inc");
simple_page_mode(true);


function can_commint()
{
    if(strlen($_POST['description']) == 0)
    {
        display_error("Description is mandatory");
        set_focus('category');           
        return false;
    }
    
//    if(strlen($_POST['debit_account']) == 0)
//    {
//        display_error("Debit account is mandatory");
//        set_focus('debit_account');           
//        return false;
//    }
//
//    if(strlen($_POST['coc_account']) == 0)
//    {
//        display_error("Credit account is mandatory");
//        set_focus('coc_account');           
//        return false;
//    }

    return true;
    
}
//-----------------------------------------------------------------------------------

if($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
{
	//initialise no input errors assumed initially before we test
	$input_error = 0;
        
    

	if (can_commint()) 
	{
            if ($selected_id != -1) 
            {
                update_utility_expense_type($selected_id, $_POST['description'], $_POST['debit_account'], $_POST['coc_account']);
                                     
                display_notification(_('Selected utility expense type has been updated'));
            } 
            else 
            {
                add_utility_expense_type($_POST['description'], $_POST['debit_account'], $_POST['coc_account']);
                                 
                display_notification(_('New utility expense type has been added'));
                               
            }    	
             
	}
       $Mode = 'RESET';    
} 
 
//-----------------------------------------------------------------------------------

if ($Mode == 'Delete')
{
    delete_utility_expense_type($selected_id);
    display_notification(_('Selected utility expense type has been deleted'));
	
    $Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
	$sav = get_post('show_inactive');
	unset($_POST);
	$_POST['show_inactive'] = $sav;
}
//-----------------------------------------------------------------------------------

$result = get_all_utility_expense_types();
//global $asset_types;
start_form();
start_table(TABLESTYLE, "width=60%");

$th = array(("Id"),("Description"),_("Expense Account"), "C.O.C. Account", "");
        

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

    label_cell($myrow["id"]); 
    label_cell($myrow["description"]); 
    label_cell($myrow["debit_account"]." - ".$myrow["debit_account_name"]);   
    label_cell($myrow["coc_account"]." - ".$myrow["coc_account_name"]);   

    edit_button_cell("Edit".$myrow['id'], _("Edit"));
//    delete_button_cell("Delete".$myrow['id'], _("Delete"));
    end_row();
}

end_table(1);

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

start_table(TABLESTYLE2);

if ($selected_id != -1) 
{
    if ($Mode == 'Edit') {
            //editing an existing status code

            $myrow = get_utility_expense_type($selected_id);

            $_POST['description'] = $myrow["description"];               
            $_POST['debit_account'] = $myrow["debit_account"];
            $_POST['coc_account'] = $myrow["coc_account"];

    }

    hidden('selected_id', $selected_id);

} 

text_row(("Description:"), 'description', null, 40, 30); 
gl_all_accounts_list_row(_("Expense Account:"), 'debit_account', null, false, false, 'Select anAccount');
gl_all_accounts_list_row(_("C.O.C Account:"), 'coc_account', null, false, false, 'Select anAccount');

end_table(1);

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

end_form();

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

end_page();


?>
