<?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
$page_security = 'SA_BANKACCOUNT';
$path_to_root="../..";
include($path_to_root . "/includes/session.inc");

page(_($help_context = "Payment Modes"));

include($path_to_root . "/includes/ui.inc");
include($path_to_root . "/gl/includes/db/gl_db_payment_modes.inc");

simple_page_mode(true);

//------------------------------
//	Helper to translate record content to more intuitive form
//
function get_bank_names($ids){
    
}

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

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{

	$input_error = 0;

	if (strlen($_POST['name']) == 0) 
	{
		$input_error = 1;
		display_error( _("The Mode Name must be entered."));
		set_focus('name');
	}

	if (count($_POST['banks']) == 0) 
	{
		$input_error = 1;
		display_error( _("Atleast one bank is required for this Payment Mode."));
		set_focus('banks');
	}
        
        if ($selected_id != -1) 
    	{
            update_payment_modes($selected_id, $_POST['name'],  implode(",", $_POST['banks'])); 
 			$note = _('Selected payment mode have been updated');
    	} 
    	else 
    	{
                add_payment_modes($_POST['name'],implode(",", $_POST['banks']));
			$note = _('New payment mode have been added');
    	}
}

if ($Mode == 'Delete')
{
	// PREVENT DELETES IF DEPENDENT RECORDS IN debtors_master
	if (key_in_foreign_table($selected_id, 'bank_trans', 'payment_mode'))
	{
		display_error(_("Cannot delete this payment term, because customer accounts have been created referring to this term."));
	} 
	else 
	{
            delete_payment_modes($selected_id);
			display_notification(_('Selected payment mode have been deleted'));
		}
	$Mode = 'RESET';
}

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

$result = get_payment_modes_all(check_value('show_inactive'));

start_form();
start_table(TABLESTYLE);
$th = array(_("Name"), _("Banks"), _(""), _(""));
inactive_control_column($th);
table_header($th);

$k = 0; //row colour counter
while ($myrow = db_fetch($result)) 
{

	alt_table_row_color($k);
    label_cell($myrow["name"]);
    label_cell(get_bank_names($myrow['banks']));
	inactive_control_cell($myrow["id"], $myrow["inactive"], 'payment_modes', "id");
 	edit_button_cell("Edit".$myrow["id"], _("Edit"));
 	delete_button_cell("Delete".$myrow["id"], _("Delete"));
    end_row();

}

inactive_control_row($th);
end_table(1);

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

div_start('edits');

start_table(TABLESTYLE2);
$selected_banks = 0;
if ($selected_id != -1) 
{
	if ($Mode == 'Edit') {
		//editing an existing payment terms
		$myrow = get_payment_modes($selected_id);
		$_POST['name']  = $myrow["name"];
		$_POST['banks'] = $myrow['banks'];
                $selected_banks = explode(",", $myrow['banks']);
	}
	hidden('selected_id', $selected_id);
}

text_row(_("Name:"), 'name', null, 40, 40);

$bank_accounts = db_fetch(get_bank_accounts());
$sql = "SELECT id,bank_account_name,inactive 
		FROM ".TB_PREF."bank_accounts  
		WHERE  !inactive ";
start_row();
start_th();
echo 'Banks';
end_th();
start_cell();
//display_warning(" count: ".count($bank_accounts));
echo combo_input("banks", $selected_banks, $sql, 'id', 'bank_account_name', array(
        'multi' => TRUE,
        'height' => count($bank_accounts),
        'category' => FALSE,
        'select_submit' => FALSE,
        'async' => true,
        'where' => Array()
    ));
    end_cell();
    end_row();

end_table(1);
div_end();

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

end_form();

end_page();

function start_cell()
{
    echo "<td>";
}

function end_cell()
{
    echo "</td>";
}

function start_th()
{
    echo "<th>";
}

function end_th()
{
    echo "</th>";
}
