From 53822ae9d0b6474437193580dd6954268f8078fc Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Mon, 3 Mar 2014 19:18:17 +0530 Subject: [PATCH] Account balance must be of type debit/credit --- erpnext/accounts/doctype/account/account.txt | 11 ++++---- erpnext/accounts/doctype/gl_entry/gl_entry.py | 25 +++++++++---------- erpnext/accounts/general_ledger.py | 4 +-- erpnext/patches.txt | 3 ++- erpnext/patches/4_0/set_account_details.py | 14 +++++++++++ 5 files changed, 35 insertions(+), 22 deletions(-) create mode 100644 erpnext/patches/4_0/set_account_details.py diff --git a/erpnext/accounts/doctype/account/account.txt b/erpnext/accounts/doctype/account/account.txt index 7b5a951620..62d5934c36 100644 --- a/erpnext/accounts/doctype/account/account.txt +++ b/erpnext/accounts/doctype/account/account.txt @@ -2,7 +2,7 @@ { "creation": "2013-01-30 12:49:46", "docstatus": 0, - "modified": "2014-03-03 17:21:07", + "modified": "2014-03-03 18:43:33", "modified_by": "Administrator", "owner": "Administrator" }, @@ -216,12 +216,11 @@ "options": "[Select]" }, { - "default": "1", - "depends_on": "eval:doc.group_or_ledger==\"Ledger\"", "doctype": "DocField", - "fieldname": "allow_negative_balance", - "fieldtype": "Check", - "label": "Allow Negative Balance" + "fieldname": "balance_must_be", + "fieldtype": "Select", + "label": "Balance must be", + "options": "\nDebit\nCredit" }, { "doctype": "DocField", diff --git a/erpnext/accounts/doctype/gl_entry/gl_entry.py b/erpnext/accounts/doctype/gl_entry/gl_entry.py index c5a7028aa9..141cbebdf3 100644 --- a/erpnext/accounts/doctype/gl_entry/gl_entry.py +++ b/erpnext/accounts/doctype/gl_entry/gl_entry.py @@ -22,7 +22,7 @@ class DocType: self.validate_account_details(adv_adj) validate_frozen_account(self.doc.account, adv_adj) check_freezing_date(self.doc.posting_date, adv_adj) - check_negative_balance(self.doc.account, adv_adj) + validate_balance_type(self.doc.account, adv_adj) # Update outstanding amt on against voucher if self.doc.against_voucher and self.doc.against_voucher_type != "POS" \ @@ -89,19 +89,18 @@ class DocType: frappe.throw(_("Cost Center") + ": " + self.doc.cost_center + _(" does not belong to the company") + ": " + self.doc.company) -def check_negative_balance(account, adv_adj=False): +def validate_balance_type(account, adv_adj=False): if not adv_adj and account: - account_details = frappe.db.get_value("Account", account, - ["allow_negative_balance", "debit_or_credit"], as_dict=True) - if not account_details["allow_negative_balance"]: - balance = frappe.db.sql("""select sum(debit) - sum(credit) from `tabGL Entry` - where account = %s""", account) - balance = account_details["debit_or_credit"] == "Debit" and \ - flt(balance[0][0]) or -1*flt(balance[0][0]) - - if flt(balance) < 0: - frappe.throw(_("Negative balance is not allowed for account ") + account) - + balance_must_be = frappe.db.get_value("Account", account, "balance_must_be") + if balance_must_be: + balance = frappe.db.sql("""select sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) + from `tabGL Entry` where account = %s""", account)[0][0] + + if (balance_must_be=="Debit" and flt(balance) < 0) or \ + (balance_must_be=="Credit" and flt(balance) > 0): + frappe.throw("Credit" if balance_must_be=="Debit" else "Credit" + + _(" balance is not allowed for account ") + account) + def check_freezing_date(posting_date, adv_adj=False): """ Nobody can do GL Entries where posting date is before freezing date diff --git a/erpnext/accounts/general_ledger.py b/erpnext/accounts/general_ledger.py index bfc2d2d147..f2eef285ce 100644 --- a/erpnext/accounts/general_ledger.py +++ b/erpnext/accounts/general_ledger.py @@ -103,7 +103,7 @@ def validate_account_for_auto_accounting_for_stock(gl_map): def delete_gl_entries(gl_entries=None, voucher_type=None, voucher_no=None, adv_adj=False, update_outstanding="Yes"): - from erpnext.accounts.doctype.gl_entry.gl_entry import check_negative_balance, \ + from erpnext.accounts.doctype.gl_entry.gl_entry import validate_balance_type, \ check_freezing_date, update_outstanding_amt, validate_frozen_account if not gl_entries: @@ -117,7 +117,7 @@ def delete_gl_entries(gl_entries=None, voucher_type=None, voucher_no=None, for entry in gl_entries: validate_frozen_account(entry["account"], adv_adj) - check_negative_balance(entry["account"], adv_adj) + validate_balance_type(entry["account"], adv_adj) validate_expense_against_budget(entry) if entry.get("against_voucher") and entry.get("against_voucher_type") != "POS" \ diff --git a/erpnext/patches.txt b/erpnext/patches.txt index fec9bb0844..7bdff4d9ea 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -27,4 +27,5 @@ erpnext.patches.4_0.customer_discount_to_pricing_rule execute:frappe.db.sql("""delete from `tabWebsite Item Group` where ifnull(item_group, '')=''""") erpnext.patches.4_0.remove_module_home_pages erpnext.patches.4_0.split_email_settings -erpnext.patches.4_0.fix_employee_user_id \ No newline at end of file +erpnext.patches.4_0.fix_employee_user_id +erpnext.patches.4_0.set_account_details diff --git a/erpnext/patches/4_0/set_account_details.py b/erpnext/patches/4_0/set_account_details.py new file mode 100644 index 0000000000..36853a6b45 --- /dev/null +++ b/erpnext/patches/4_0/set_account_details.py @@ -0,0 +1,14 @@ +# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors +# License: GNU General Public License v3. See license.txt + +from __future__ import unicode_literals +import frappe + +def execute(): + for d in (('Asset', 'Debit', 'No'), ('Liability', 'Credit', 'No'), ('Expense', 'Debit', 'Yes'), + ('Income', 'Credit', 'Yes')): + frappe.db.sql("""update `tabAccount` set root_type = %s + where debit_or_credit=%s and is_pl_account=%s""", d) + + frappe.db.sql("""update `tabAccount` set balance_must_be=debit_or_credit + where ifnull(allow_negative_balance, 0) = 0""") \ No newline at end of file