Topic: Opening Balance entry for Customers and Suppliers
I have done like this and would like to know will it be a proper solution?
This solution will accept negative values too , which will be a bank deposit by customer or payment to supplier
1. include item cart class in customer and suppliers
include_once($path_to_root . "/includes/ui/items_cart.inc");
2. added two input fields to customers and suppliers master (amount and date)
a. Customer
table_section(2);
if (!$selected_id) {
table_section_title(_("Opening Balance"));
amount_row(_("Amount:"), 'opening_bal');
if (!isset($_POST['ob_date']))
$_POST['ob_date'] = add_days(begin_fiscalyear(), -1);
date_row(_("As of:"), 'ob_date');
} else {
hidden('opening_bal', 0);
hidden('ob_date', Today());
}
b. Supplier
if (!$supplier_id) {
table_section_title(_("Opening Balance"));
amount_row(_("Amount:"), 'opening_bal');
if (!isset($_POST['ob_date']))
$_POST['ob_date'] = add_days(begin_fiscalyear(), -1);
date_row(_("As of:"), 'ob_date');
} else {
hidden('opening_bal', 0);
hidden('ob_date', Today());
}
It will appear only in new customer or supplier mode
3. Validation of Opening balance entry date in can_process() method
for both Customer and supplier
if (input_num('opening_bal') != 0 && !is_date(get_post('ob_date'))) {
display_error(_("Invalid opening balance entry date."));
set_focus('ob_date');
return false;
}
4. Journal entry in handle_submit method
a. customer
if (input_num('opening_bal') != 0) {
$cart = new items_cart(ST_JOURNAL);
$cart->order_id = 0;
$cart->tran_date = $cart->doc_date = $cart->event_date = get_post('ob_date');
$cart->reference = $Refs->get_next(ST_JOURNAL, null, $cart->tran_date);
$cart->tax_info = false;
$cart->memo_ = _("Opening Balance Entry");
$cart->currency = $_POST['curr_code'];
if ($cart->currency != get_company_pref('curr_default'))
$cart->rate = get_exchange_rate_to_home_currency($cart->currency,get_post('ob_date'));
$cart->clear_items();
if (input_num('opening_bal') > 0) {
$cart->add_gl_item(get_company_pref('debtors_act'), 0,
0, input_num('opening_bal'), _("Opening Balance Entry"), '', $selected_id);
$cart->add_gl_item(get_company_pref('default_inv_sales_act'), 0,
0, input_num('opening_bal') * -1, _("Opening Balance Entry"), '', $selected_id);
} else {
$cart->add_gl_item(get_company_pref('debtors_act'), 0,
0, input_num('opening_bal'), _("Opening Balance Entry"), '', $selected_id);
$bank_account= get_bank_account(get_default_customer_bank_account($selected_id));
$cart->add_gl_item($bank_account['account_code'], 0,
0, input_num('opening_bal')*-1 , _("Opening Balance Entry"), '', $selected_id);
}
$trans_no = write_journal_entries($cart);
}
b. Supplier
if (input_num('opening_bal') != 0) {
$cart = new items_cart(ST_JOURNAL);
$cart->order_id = 0;
$cart->tran_date = $cart->doc_date = $cart->event_date = get_post('ob_date');
$cart->reference = $Refs->get_next(ST_JOURNAL, null, $cart->tran_date);
$cart->tax_info = false;
$cart->memo_ = _("Opening Balance Entry");
$cart->currency = $_POST['curr_code'];
if ($cart->currency != get_company_pref('curr_default'))
$cart->rate = get_exchange_rate_to_home_currency($cart->currency,get_post('ob_date'));
$cart->clear_items();
if (input_num('opening_bal') > 0) {
$cart->add_gl_item($_POST['payable_account'], 0,
0, input_num('opening_bal')* -1, _("Opening Balance Entry"), '', $supplier_id);
$cart->add_gl_item($_POST['purchase_account']!=''?$_POST['purchase_account']:get_company_pref('grn_clearing_act'), 0,
0, input_num('opening_bal') , _("Opening Balance Entry"), '', $supplier_id);
} else {
$cart->add_gl_item($_POST['payable_account'], 0,
0, input_num('opening_bal')* -1, _("Opening Balance Entry"), '', $supplier_id);
$bank_account= get_bank_account(get_default_supplier_bank_account($supplier_id));
$cart->add_gl_item($bank_account['account_code'], 0,
0, input_num('opening_bal') , _("Opening Balance Entry"), '', $supplier_id);
}
$trans_no = write_journal_entries($cart);
}
NB: patch file is available on request
Experience is the name everyone gives to their mistakes!