<?php
/**********************************************************************
	Copyright (C) Evolve Techniologies
	Author : Evolve Technologies
	Email : info@evolve-sl.com
	Company : Evolve Technologies(pvt)Ltd
***********************************************************************/
$page_security = 'SA_CHEQUEBOOK';
$path_to_root = "../..";
include($path_to_root . "/includes/session.inc");

page(_($help_context = "Cheque Book Management"));

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


simple_page_mode();
//-----------------------------------------------------------------------------------

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

	//initialise no input errors assumed initially before we test
	$input_error = 0;

	if ($input_error != 1){
    	if ($selected_id != -1){    		
    		update_cheque_book($_POST['selected_id'], $_POST['bank_account'], $_POST['cheque_book_no'], $_POST['bank_code'], $_POST['branch_code'], $_POST['starting_no'], $_POST['ending_no'], 1);
			display_notification(_('Cheque Book Details has been updated'));
    	}else{
    		    
    		add_cheque_book($_POST['bank_account'], $_POST['cheque_book_no'], $_POST['bank_code'], $_POST['branch_code'], $_POST['starting_no'], $_POST['ending_no'], 1);
			display_notification(_('New Cheque Book Details has been added'));
    	}
 		$Mode = 'RESET';
	}
}/*elseif( $Mode == 'Delete'){
	//the link to delete a selected record was clicked instead of the submit button

	$cancel_delete = 0;
	// PREVENT DELETES IF DEPENDENT RECORDS IN 'bank_trans'

	if (key_in_foreign_table($selected_id, 'bank_trans', 'bank_act') || key_in_foreign_table(get_post('account_code'), 'gl_trans', 'account'))
	{
		$cancel_delete = 1;
		display_error(_("Cannot delete this bank account because transactions have been created using this account."));
	}

	if (key_in_foreign_table($selected_id, 'sales_pos', 'pos_account'))
	{
		$cancel_delete = 1;
		display_error(_("Cannot delete this bank account because POS definitions have been created using this account."));
	}
	if (!$cancel_delete) 
	{
		delete_cheque_book($selected_id);
		display_notification(_('Selected bank account has been deleted'));
	} //end if Delete bank account
	$Mode = 'RESET';
} */

if ($Mode == 'RESET')
{
 	$selected_id = -1;
	$_POST['bank_name']  = 	$_POST['bank_account_name']  = '';
	$_POST['bank_account_number'] = $_POST['bank_address'] = '';
	$_POST['bank_charge_act'] = get_company_pref('bank_charge_act');
}
if (!isset($_POST['bank_charge_act']))
	$_POST['bank_charge_act'] = get_company_pref('bank_charge_act');

/* Always show the list of cheque Books */

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

start_form();
start_table(TABLESTYLE, "width='80%'");

$th = array(_("Bank Name"), _("Cheque Book No"), _("Bank Code"), _("Branch Code"), _("Starting Cheque No"), _("Ending Cheque No"), '');
inactive_control_column($th);
table_header($th);	

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

        label_cell($myrow["bank_account_name"], "nowrap");
        label_cell($myrow["cheqeu_book_no"], "nowrap");
	label_cell($myrow["bank_code"], "nowrap");
	label_cell($myrow["branch_code"], "nowrap");
        label_cell($myrow["chque_start_no"], "nowrap");
        label_cell($myrow["chque_end_no"], "nowrap");

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

inactive_control_row($th);
end_table(1);

$is_used = $selected_id != -1 && key_in_foreign_table($selected_id, 'bank_trans', 'bank_act');

start_table(TABLESTYLE2);

if ($selected_id != -1) 
{
  if ($Mode == 'Edit') {	
	$myrow = get_cheque_book($selected_id);

	$_POST['bank_account'] 		= $myrow["bank_id"];
	$_POST['cheque_book_no'] 	= $myrow["cheqeu_book_no"];
	$_POST['bank_code'] 		= $myrow["bank_code"];
	$_POST['branch_code'] 		= $myrow["branch_code"];
	$_POST['starting_no'] 		= $myrow["chque_start_no"];
	$_POST['ending_no']  		= $myrow["chque_end_no"];
  }
	hidden('selected_id', $selected_id);
/*	hidden('account_code');
	hidden('account_type');
	hidden('BankAccountCurrency', $_POST['BankAccountCurrency']);	
	hidden('bank_short_code', $_POST['bank_short_code']);//-----@widanage add new field for bank short code----10.04.2013------	
*/	set_focus('bank_account_name');
} 


bank_accounts_list_row( _("Bank Name :"), 'bank_account', null, true);
text_row(_("Cheque Book no:"), 'cheque_book_no', null, 10, 10);
text_row(_("Bank Code :"), 'bank_code', null, 10, 10);
text_row(_("Branch Code:"), 'branch_code', null, 10, 10);
text_row(_("Cheque Starting No:"), 'starting_no', null, 10, 10);
text_row(_("Cheque Ending No:"), 'ending_no', null, 10, 10);
end_table(1);

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

end_form();

end_page();
