From e2c200a91e9bfc60812ef974f938dc8a58a39c37 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Thu, 28 May 2015 13:00:37 +0530 Subject: [PATCH] Make gl entry for rounded-off amount --- erpnext/accounts/general_ledger.py | 53 ++++++++++++++----- .../leave_application/leave_application.json | 3 +- erpnext/setup/doctype/company/company.json | 20 ++++++- 3 files changed, 62 insertions(+), 14 deletions(-) diff --git a/erpnext/accounts/general_ledger.py b/erpnext/accounts/general_ledger.py index 9b97e4bc6b2..df2a217e967 100644 --- a/erpnext/accounts/general_ledger.py +++ b/erpnext/accounts/general_ledger.py @@ -5,6 +5,8 @@ from __future__ import unicode_literals import frappe from frappe.utils import flt, cstr from frappe import _ +import copy +from frappe.model.meta import get_field_precision from erpnext.accounts.utils import validate_expense_against_budget @@ -64,19 +66,13 @@ def check_if_in_list(gle, gl_map): def save_entries(gl_map, adv_adj, update_outstanding): validate_account_for_auto_accounting_for_stock(gl_map) - - total_debit = total_credit = 0.0 + round_off_debit_credit(gl_map) + for entry in gl_map: make_entry(entry, adv_adj, update_outstanding) # check against budget validate_expense_against_budget(entry) - # update total debit / credit - total_debit += flt(entry.debit) - total_credit += flt(entry.credit) - - validate_total_debit_credit(total_debit, total_credit, gl_map) - def make_entry(args, adv_adj, update_outstanding): args.update({"doctype": "GL Entry"}) gle = frappe.get_doc(args) @@ -85,10 +81,6 @@ def make_entry(args, adv_adj, update_outstanding): gle.run_method("on_update_with_args", adv_adj, update_outstanding) gle.submit() -def validate_total_debit_credit(total_debit, total_credit, gl_map): - if abs(total_debit - total_credit) > 0.005: - frappe.throw(_("Debit and Credit not equal for {0} #{1}. Difference is {2}.").format(gl_map[0].voucher_type, gl_map[0].voucher_no, total_debit - total_credit)) - def validate_account_for_auto_accounting_for_stock(gl_map): if gl_map[0].voucher_type=="Journal Entry": aii_accounts = [d[0] for d in frappe.db.sql("""select name from tabAccount @@ -98,6 +90,43 @@ def validate_account_for_auto_accounting_for_stock(gl_map): if entry.account in aii_accounts: frappe.throw(_("Account: {0} can only be updated via Stock Transactions").format(entry.account), StockAccountInvalidTransaction) +def round_off_debit_credit(gl_map): + precision = get_field_precision(frappe.get_meta("GL Entry").get_field("debit"), + currency=frappe.db.get_value("Company", gl_map[0].company, "default_currency", cache=True)) + + debit_credit_diff = 0.0 + for entry in gl_map: + entry.debit = flt(entry.debit, precision) + entry.credit = flt(entry.credit, precision) + debit_credit_diff += entry.debit - entry.credit + + debit_credit_diff = flt(debit_credit_diff, precision) + if abs(debit_credit_diff) >= (2.0 / (10**precision)): + frappe.throw(_("Debit and Credit not equal for {0} #{1}. Difference is {2}.") + .format(gl_map[0].voucher_type, gl_map[0].voucher_no, debit_credit_diff)) + + elif abs(debit_credit_diff) >= (1.0 / (10**precision)): + make_round_off_gle(gl_map, debit_credit_diff) + +def make_round_off_gle(gl_map, debit_credit_diff): + round_off_account, round_off_cost_center = frappe.db.get_value("Company", gl_map[0].company, + ["round_off_account", "round_off_cost_center"]) or [None, None] + if not round_off_account: + frappe.throw(_("Please mention Round Off Account in Company")) + + if not round_off_cost_center: + frappe.throw(_("Please mention Round Off Cost Center in Company")) + + round_off_gle = copy.deepcopy(gl_map[0]) + round_off_gle.update({ + "account": round_off_account, + "debit": abs(debit_credit_diff) if debit_credit_diff < 0 else 0, + "credit": debit_credit_diff if debit_credit_diff > 0 else 0, + "cost_center": round_off_cost_center + }) + + gl_map.append(round_off_gle) + def delete_gl_entries(gl_entries=None, voucher_type=None, voucher_no=None, adv_adj=False, update_outstanding="Yes"): diff --git a/erpnext/hr/doctype/leave_application/leave_application.json b/erpnext/hr/doctype/leave_application/leave_application.json index cd32b9c6559..de1c2b02494 100644 --- a/erpnext/hr/doctype/leave_application/leave_application.json +++ b/erpnext/hr/doctype/leave_application/leave_application.json @@ -130,6 +130,7 @@ "description": "", "fieldname": "leave_approver", "fieldtype": "Link", + "ignore_user_permissions": 1, "label": "Leave Approver", "options": "User", "permlevel": 0 @@ -216,7 +217,7 @@ "idx": 1, "is_submittable": 1, "max_attachments": 3, - "modified": "2015-04-30 02:19:39.330689", + "modified": "2015-05-27 18:44:36.708614", "modified_by": "Administrator", "module": "HR", "name": "Leave Application", diff --git a/erpnext/setup/doctype/company/company.json b/erpnext/setup/doctype/company/company.json index db71d0f2a24..c2d70735655 100644 --- a/erpnext/setup/doctype/company/company.json +++ b/erpnext/setup/doctype/company/company.json @@ -170,6 +170,15 @@ "permlevel": 0, "read_only": 0 }, + { + "fieldname": "round_off_account", + "fieldtype": "Link", + "ignore_user_permissions": 0, + "label": "Round Off Account", + "options": "Account", + "permlevel": 0, + "precision": "" + }, { "fieldname": "column_break0", "fieldtype": "Column Break", @@ -211,6 +220,15 @@ "options": "Account", "permlevel": 0 }, + { + "fieldname": "round_off_cost_center", + "fieldtype": "Link", + "ignore_user_permissions": 0, + "label": "Round Off Cost Center", + "options": "Cost Center", + "permlevel": 0, + "precision": "" + }, { "fieldname": "section_break_22", "fieldtype": "Section Break", @@ -414,7 +432,7 @@ ], "icon": "icon-building", "idx": 1, - "modified": "2015-05-19 02:00:41.055138", + "modified": "2015-05-28 12:56:18.175509", "modified_by": "Administrator", "module": "Setup", "name": "Company",