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

page(_($help_context = "Petty Cash Expense Accounts"));

//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 . "/petty_cash/includes/db/petty_cash_expense_accounts_db.inc");


simple_page_mode(true);


function can_commit($selected_id)
{
    if(!($_POST['type_id'] > 0))
    {
        display_error("Type is mandatory");
        set_focus('type_id');           
        return false;
    }    
    
    if($selected_id  == -1){
        $splc_result = get_petty_cash_expense_account_record($_POST['type_id']);       
    
        if($splc_result['type_id'] == $_POST['type_id'])
        {
            display_error("Entered type already exists.");
            set_focus('type_id');
            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_commit($selected_id)) 
	{
            if ($selected_id != -1) 
            {
                update_petty_cash_expense_account($selected_id, $_POST['debit_account'], $_POST['coc_account']);
                                     
                display_notification(_('Selected subcontractor project invoice line category has been updated'));
            } 
            else 
            {
                add_petty_cash_expense_account($_POST['type_id'], $_POST['debit_account'], $_POST['coc_account']);
                                 
                display_notification(_('New subcontractor project invoice line category has been added'));
                               
            }             
	}
       $Mode = 'RESET';    
} 
 
//-----------------------------------------------------------------------------------

if ($Mode == 'Delete')
{
    delete_petty_cash_expense_account($selected_id);
    display_notification(_('Petty cash expense account 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_petty_cash_expense_accounts();
//global $asset_types;
start_form();
start_table(TABLESTYLE, "width=60%");

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

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

    label_cell($myrow["category_name"]); 
    label_cell($myrow["debit_account"]." - ".$myrow["debit_account_name"]);   
    label_cell($myrow["coc_account"]." - ".$myrow["coc_account_name"]);   
//    label_cell(get_yes_no_name($myrow["budget_required"]), 'align=center');
    
    edit_button_cell("Edit".$myrow['type_id'], _("Edit"));
    delete_button_cell("Delete".$myrow['type_id'], _("Delete"));
    end_row();
}

end_table(1);

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

start_table(TABLESTYLE2);

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

            $myrow = get_petty_cash_expense_account($selected_id);

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

    }

    hidden('selected_id', $selected_id);
} 

if($selected_id > 0){
    $scpi_line_details = get_petty_cash_expense_account_detail($selected_id);
    label_row("Type:", $scpi_line_details['type_name']);
    hidden('type_id', $scpi_line_details['type_id']);
}else{
    value_list_row("Type:", 'type_id', "PETTY_CASH_EXPENSE_TYPES", null, "Select a line category");  
}


gl_all_accounts_list_row(_("Expense Account:"), 'debit_account', null, false, false, 'Select an Account');
gl_all_accounts_list_row(_("C.O.C. Account:"), 'coc_account', null, false, false, 'Select an Account');

//yesno_list_row("Budget Required:", 'budget_required', null);

end_table(1);

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

end_form();

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

end_page();



?>
