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

page(_($help_context = "Bank Branches"));

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 . "/payroll/includes/db/bank_branches_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['bank_id'] == -1) 
    {
            $input_error = 1;
            display_error( _('Select a bank.'));
            set_focus('bank_id');
    }
   	if ($_POST['branch_name'] == '')
   	{
            $input_error = 1;
            display_error( _("Branch name cannot be empty."));
            set_focus('branch_name');
   	}
        
    elseif ($_POST['branch_code'] == '')
   	{
            $input_error = 1;
            display_error( _("Branch code cannot be empty."));
            set_focus('branch_code');
   	}
 
	elseif($selected_id == -1)
	{
            $existing_values = get_existing_bank_branches($_POST['bank_id'], $_POST['branch_code']);

            if ($existing_values != "") {
                    $input_error = 1;
                    display_error( _("This code is already available in the list."));
                    set_focus('branch_code');
		}
   	}
	
	if ($input_error == 0)
	{
     	if ($Mode == 'ADD_ITEM') 
       	{
                add_bank_branch($_POST['bank_id'], $_POST['branch_name'], $_POST['branch_code'], 
                        $_POST['branch_address']); 

    		display_notification(_("New bank branch has been added."));
       	} else
       	{
                update_bank_branch($selected_id, $_POST['bank_id'], $_POST['branch_name'], 
                        $_POST['branch_code'], $_POST['branch_address']); 

    	  	display_notification(_("Selected bank branch has been updated."));
       	}
		$Mode = 'RESET';
	}
}

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

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

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

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

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

start_form();

echo value_list_row(_("Bank Name:"), 'bank_id', "BANKS", '', '', true);

br();

$result = get_branches_list($_POST['bank_id']);

br();

div_start('code_table');
	start_table(TABLESTYLE, "width=60%");

	$th = array(_("ID"), _("Branch Code"), _("Branch Name"), _("Branch Address"), "", "");

        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["bank_name"]);
            label_cell($myrow["branch_code"]);
            label_cell($myrow["branch_name"]);           
            label_cell($myrow["branch_address"]);

            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_branch_details($selected_id);
		$_POST['id'] = $myrow["id"];
		$_POST['bank_id'] = $myrow["bank_id"];
		$_POST['branch_name'] = $myrow["branch_name"];
                $_POST['branch_code'] = $myrow["branch_code"];
		$_POST['branch_address'] = $myrow["branch_address"];
	}
	hidden('selected_id', $selected_id);
} 
else {
	$_POST['id'] = '';
	$_POST['bank_id'] = '';
        $_POST['branch_name'] = '';
	$_POST['branch_address'] = '';
}

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

hidden('id', $selected_id);

text_row(_("Branch Code:"), 'branch_code', null, 20, 20);
text_row(_("Branch Name:"), 'branch_name', null, 35, 100);
textarea_row(_("Branch Address:"), 'branch_address', null, 30, 2);


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

end_table(1);

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

end_form();
end_page();

?>
