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

page(_($help_context = "Project Invoice Line Categories"));

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

simple_page_mode(true);


function can_commit($selected_id)
{
    if(!($_POST['category_id'] > 0))
    {
        display_error("Category is mandatory");
        set_focus('category_id');           
        return false;
    }    
    
    
    if($selected_id  == -1){
        $pic_result = get_pi_line_category_detail($_POST['category_id']);       
    
        if($pic_result['category_id'] == $_POST['category_id'])
        {
            display_error("Entered category already exists.");
            set_focus('category_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_pi_line_category($selected_id, $_POST['debit_account'], $_POST['credit_account']);
                                     
                display_notification(_('Selected project invoice line category has been updated'));
            } 
            else 
            {
                add_pi_line_category($_POST['category_id'], $_POST['debit_account'], $_POST['credit_account']);
                                 
                display_notification(_('New project invoice line category has been added'));
                               
            }             
	}
       $Mode = 'RESET';    
} 
 
//-----------------------------------------------------------------------------------

if ($Mode == 'Delete')
{
    delete_pi_line_category($selected_id);
    display_notification(_('Selected project invoice line category 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_pi_line_categories();
//global $asset_types;
start_form();
start_table(TABLESTYLE, "width=60%");

$th = array(("Category"),_("Debit Account"), "Credit 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["credit_account"]." - ".$myrow["credit_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_pi_line_category($selected_id);

            $_POST['category_id'] = $myrow["category_id"];               
            $_POST['debit_account'] = $myrow["debit_account"];
            $_POST['credit_account'] = $myrow["credit_account"];

    }

    hidden('selected_id', $selected_id);

} 

if($selected_id > 0){
    $pi_line_details = get_pi_line_category_record($selected_id);
    label_row("Line Category:", $pi_line_details['category_name']);
    hidden('category_id', $pi_line_details['category_id']);
}else{
    value_list_row("Line Category:", 'category_id', "PROJ_INV_LINE_CATEGORIES", null, "Select a line category");    
}

gl_all_accounts_list_row(_("Debit Account:"), 'debit_account', null, false, false, 'Select an Account');
gl_all_accounts_list_row(_("Credit Account:"), 'credit_account', null, false, false, 'Select an Account');

end_table(1);

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

end_form();

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

end_page();


?>
