From 6f17cf93c5198bd979500ef6205fa62c4d1cc281 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Wed, 17 Sep 2014 14:11:22 +0530 Subject: [PATCH] Party balance --- .../journal_voucher/journal_voucher.js | 33 +- .../journal_voucher/journal_voucher.py | 21 +- .../journal_voucher_detail.json | 336 +++++++++--------- erpnext/accounts/utils.py | 42 ++- 4 files changed, 233 insertions(+), 199 deletions(-) diff --git a/erpnext/accounts/doctype/journal_voucher/journal_voucher.js b/erpnext/accounts/doctype/journal_voucher/journal_voucher.js index ad83607de5..69269d07e9 100644 --- a/erpnext/accounts/doctype/journal_voucher/journal_voucher.js +++ b/erpnext/accounts/doctype/journal_voucher/journal_voucher.js @@ -77,15 +77,18 @@ erpnext.accounts.JournalVoucher = frappe.ui.form.Controller.extend({ }, setup_balance_formatter: function() { - var df = frappe.meta.get_docfield("Journal Voucher Detail", "balance", this.frm.doc.name); - df.formatter = function(value, df, options, doc) { - var currency = frappe.meta.get_field_currency(df, doc); - var dr_or_cr = value ? ('') : ""; - return "
" - + ((value==null || value==="") ? "" : format_currency(Math.abs(value), currency)) - + " " + dr_or_cr - + "
"; - } + var me = this; + $.each(["balance", "party_balance"], function(i, field) { + var df = frappe.meta.get_docfield("Journal Voucher Detail", field, me.frm.doc.name); + df.formatter = function(value, df, options, doc) { + var currency = frappe.meta.get_field_currency(df, doc); + var dr_or_cr = value ? ('') : ""; + return "
" + + ((value==null || value==="") ? "" : format_currency(Math.abs(value), currency)) + + " " + dr_or_cr + + "
"; + } + }) }, against_voucher: function(doc, cdt, cdn) { @@ -259,19 +262,15 @@ cur_frm.cscript.voucher_type = function(doc, cdt, cdn) { } frappe.ui.form.on("Journal Voucher Detail", "party", function(frm, cdt, cdn) { - var d = locals[cdt][cdn]; + var d = frappe.get_doc(cdt, cdn); if(!d.account && d.party_type && d.party) { - return frappe.call({ - method: "erpnext.accounts.party.get_party_account", + return frm.call({ + method: "erpnext.accounts.doctype.journal_voucher.journal_voucher.get_party_account_and_balance", + child: d, args: { company: frm.doc.company, party_type: d.party_type, party: d.party - }, - callback: function(r) { - if(!r.exc && r.message) { - frappe.model.set_value(cdt, cdn, "account", r.message); - } } }); } diff --git a/erpnext/accounts/doctype/journal_voucher/journal_voucher.py b/erpnext/accounts/doctype/journal_voucher/journal_voucher.py index 4f8fa9a647..ff8b2e1d5b 100644 --- a/erpnext/accounts/doctype/journal_voucher/journal_voucher.py +++ b/erpnext/accounts/doctype/journal_voucher/journal_voucher.py @@ -7,6 +7,8 @@ from frappe.utils import cstr, flt, fmt_money, formatdate, getdate, money_in_wor from frappe import msgprint, _, scrub from erpnext.setup.utils import get_company_currency from erpnext.controllers.accounts_controller import AccountsController +from erpnext.accounts.utils import get_balance_on + class JournalVoucher(AccountsController): def __init__(self, arg1, arg2=None): @@ -400,7 +402,6 @@ class JournalVoucher(AccountsController): @frappe.whitelist() def get_default_bank_cash_account(company, voucher_type): - from erpnext.accounts.utils import get_balance_on account = frappe.db.get_value("Company", company, voucher_type=="Bank Voucher" and "default_bank_account" or "default_cash_account") if account: @@ -421,6 +422,7 @@ def get_payment_entry_from_sales_invoice(sales_invoice): jv.get("entries")[0].party_type = "Customer" jv.get("entries")[0].party = si.customer jv.get("entries")[0].balance = get_balance_on(si.debit_to) + jv.get("entries")[0].party_balance = get_balance_on(si.customer, "Customer") jv.get("entries")[0].credit = si.outstanding_amount jv.get("entries")[0].against_invoice = si.name @@ -431,7 +433,6 @@ def get_payment_entry_from_sales_invoice(sales_invoice): @frappe.whitelist() def get_payment_entry_from_purchase_invoice(purchase_invoice): - from erpnext.accounts.utils import get_balance_on pi = frappe.get_doc("Purchase Invoice", purchase_invoice) jv = get_payment_entry(pi) jv.remark = 'Payment against Purchase Invoice {0}. {1}'.format(pi.name, pi.remarks) @@ -441,6 +442,7 @@ def get_payment_entry_from_purchase_invoice(purchase_invoice): jv.get("entries")[0].party_type = "Supplier" jv.get("entries")[0].party = pi.supplier jv.get("entries")[0].balance = get_balance_on(pi.credit_to) + jv.get("entries")[0].party_balance = get_balance_on(pi.supplier, "Supplier") jv.get("entries")[0].debit = pi.outstanding_amount jv.get("entries")[0].against_voucher = pi.name @@ -469,7 +471,6 @@ def get_payment_entry(doc): @frappe.whitelist() def get_opening_accounts(company): """get all balance sheet accounts for opening entry""" - from erpnext.accounts.utils import get_balance_on accounts = frappe.db.sql_list("""select name from tabAccount where group_or_ledger='Ledger' and report_type='Balance Sheet' and company=%s""", company) @@ -508,3 +509,17 @@ def get_outstanding(args): return { "debit": flt(frappe.db.get_value("Purchase Invoice", args["docname"], "outstanding_amount")) } + +@frappe.whitelist() +def get_party_account_and_balance(company, party_type, party): + from erpnext.accounts.party import get_party_account + account = get_party_account(company, party, party_type) + + account_balance = get_balance_on(account=account) + party_balance = get_balance_on(party_type=party_type, party=party) + + return { + "account": account, + "balance": account_balance, + "party_balance": party_balance + } diff --git a/erpnext/accounts/doctype/journal_voucher_detail/journal_voucher_detail.json b/erpnext/accounts/doctype/journal_voucher_detail/journal_voucher_detail.json index de1577290b..9834c2e1be 100644 --- a/erpnext/accounts/doctype/journal_voucher_detail/journal_voucher_detail.json +++ b/erpnext/accounts/doctype/journal_voucher_detail/journal_voucher_detail.json @@ -1,197 +1,207 @@ { - "autoname": "JVD.######", - "creation": "2013-02-22 01:27:39", - "docstatus": 0, - "doctype": "DocType", + "autoname": "JVD.######", + "creation": "2013-02-22 01:27:39", + "docstatus": 0, + "doctype": "DocType", "fields": [ { - "fieldname": "account", - "fieldtype": "Link", - "in_filter": 1, - "in_list_view": 1, - "label": "Account", - "oldfieldname": "account", - "oldfieldtype": "Link", - "options": "Account", - "permlevel": 0, - "print_width": "250px", - "reqd": 1, - "search_index": 1, + "fieldname": "account", + "fieldtype": "Link", + "in_filter": 1, + "in_list_view": 1, + "label": "Account", + "oldfieldname": "account", + "oldfieldtype": "Link", + "options": "Account", + "permlevel": 0, + "print_width": "250px", + "reqd": 1, + "search_index": 1, "width": "250px" - }, + }, { - "default": ":Company", - "description": "If Income or Expense", - "fieldname": "cost_center", - "fieldtype": "Link", - "in_filter": 1, - "in_list_view": 1, - "label": "Cost Center", - "oldfieldname": "cost_center", - "oldfieldtype": "Link", - "options": "Cost Center", - "permlevel": 0, - "print_hide": 1, - "print_width": "180px", - "search_index": 0, - "width": "180px" - }, - { - "fieldname": "col_break1", - "fieldtype": "Column Break", - "permlevel": 0 - }, - { - "fieldname": "party_type", - "fieldtype": "Link", - "label": "Party Type", - "options": "DocType", - "permlevel": 0 - }, - { - "fieldname": "party", - "fieldtype": "Dynamic Link", - "label": "Party", - "options": "party_type", - "permlevel": 0 - }, - { - "fieldname": "balance", - "fieldtype": "Currency", - "in_list_view": 1, - "label": "Account Balance", - "no_copy": 1, - "oldfieldname": "balance", - "oldfieldtype": "Data", - "options": "Company:company:default_currency", - "permlevel": 0, - "print_hide": 1, + "fieldname": "balance", + "fieldtype": "Currency", + "in_list_view": 1, + "label": "Account Balance", + "no_copy": 1, + "oldfieldname": "balance", + "oldfieldtype": "Data", + "options": "Company:company:default_currency", + "permlevel": 0, + "print_hide": 1, "read_only": 1 - }, + }, { - "fieldname": "sec_break1", - "fieldtype": "Section Break", + "default": ":Company", + "description": "If Income or Expense", + "fieldname": "cost_center", + "fieldtype": "Link", + "in_filter": 1, + "in_list_view": 1, + "label": "Cost Center", + "oldfieldname": "cost_center", + "oldfieldtype": "Link", + "options": "Cost Center", + "permlevel": 0, + "print_hide": 1, + "print_width": "180px", + "search_index": 0, + "width": "180px" + }, + { + "fieldname": "col_break1", + "fieldtype": "Column Break", "permlevel": 0 - }, + }, { - "fieldname": "debit", - "fieldtype": "Currency", - "in_list_view": 1, - "label": "Debit", - "oldfieldname": "debit", - "oldfieldtype": "Currency", - "options": "Company:company:default_currency", + "fieldname": "party_type", + "fieldtype": "Link", + "label": "Party Type", + "options": "DocType", "permlevel": 0 - }, + }, { - "fieldname": "col_break2", - "fieldtype": "Column Break", + "fieldname": "party", + "fieldtype": "Dynamic Link", + "label": "Party", + "options": "party_type", "permlevel": 0 - }, + }, { - "fieldname": "credit", - "fieldtype": "Currency", - "in_list_view": 1, - "label": "Credit", - "oldfieldname": "credit", - "oldfieldtype": "Currency", - "options": "Company:company:default_currency", + "fieldname": "party_balance", + "fieldtype": "Currency", + "label": "Party Balance", + "options": "Company:company:default_currency", + "permlevel": 0, + "precision": "", + "read_only": 1 + }, + { + "fieldname": "sec_break1", + "fieldtype": "Section Break", + "label": "Amount", "permlevel": 0 - }, + }, { - "fieldname": "reference", - "fieldtype": "Section Break", - "label": "Reference", + "fieldname": "debit", + "fieldtype": "Currency", + "in_list_view": 1, + "label": "Debit", + "oldfieldname": "debit", + "oldfieldtype": "Currency", + "options": "Company:company:default_currency", "permlevel": 0 - }, + }, { - "fieldname": "against_invoice", - "fieldtype": "Link", - "in_filter": 1, - "label": "Against Sales Invoice", - "no_copy": 1, - "oldfieldname": "against_invoice", - "oldfieldtype": "Link", - "options": "Sales Invoice", - "permlevel": 0, - "print_hide": 0, + "fieldname": "col_break2", + "fieldtype": "Column Break", + "permlevel": 0 + }, + { + "fieldname": "credit", + "fieldtype": "Currency", + "in_list_view": 1, + "label": "Credit", + "oldfieldname": "credit", + "oldfieldtype": "Currency", + "options": "Company:company:default_currency", + "permlevel": 0 + }, + { + "fieldname": "reference", + "fieldtype": "Section Break", + "label": "Reference", + "permlevel": 0 + }, + { + "fieldname": "against_invoice", + "fieldtype": "Link", + "in_filter": 1, + "label": "Against Sales Invoice", + "no_copy": 1, + "oldfieldname": "against_invoice", + "oldfieldtype": "Link", + "options": "Sales Invoice", + "permlevel": 0, + "print_hide": 0, "search_index": 1 - }, + }, { - "fieldname": "against_voucher", - "fieldtype": "Link", - "in_filter": 1, - "in_list_view": 1, - "label": "Against Purchase Invoice", - "no_copy": 1, - "oldfieldname": "against_voucher", - "oldfieldtype": "Link", - "options": "Purchase Invoice", - "permlevel": 0, - "print_hide": 0, + "fieldname": "against_voucher", + "fieldtype": "Link", + "in_filter": 1, + "in_list_view": 1, + "label": "Against Purchase Invoice", + "no_copy": 1, + "oldfieldname": "against_voucher", + "oldfieldtype": "Link", + "options": "Purchase Invoice", + "permlevel": 0, + "print_hide": 0, "search_index": 1 - }, + }, { - "fieldname": "against_jv", - "fieldtype": "Link", - "in_filter": 1, - "label": "Against Journal Voucher", - "no_copy": 1, - "oldfieldname": "against_jv", - "oldfieldtype": "Link", - "options": "Journal Voucher", - "permlevel": 0, - "print_hide": 0, + "fieldname": "against_jv", + "fieldtype": "Link", + "in_filter": 1, + "label": "Against Journal Voucher", + "no_copy": 1, + "oldfieldname": "against_jv", + "oldfieldtype": "Link", + "options": "Journal Voucher", + "permlevel": 0, + "print_hide": 0, "search_index": 1 - }, + }, { - "fieldname": "col_break3", - "fieldtype": "Column Break", + "fieldname": "col_break3", + "fieldtype": "Column Break", "permlevel": 0 - }, + }, { - "fieldname": "against_sales_order", - "fieldtype": "Link", - "label": "Against Sales Order", - "options": "Sales Order", + "fieldname": "against_sales_order", + "fieldtype": "Link", + "label": "Against Sales Order", + "options": "Sales Order", "permlevel": 0 - }, + }, { - "fieldname": "against_purchase_order", - "fieldtype": "Link", - "label": "Against Purchase Order", - "options": "Purchase Order", + "fieldname": "against_purchase_order", + "fieldtype": "Link", + "label": "Against Purchase Order", + "options": "Purchase Order", "permlevel": 0 - }, + }, { - "fieldname": "is_advance", - "fieldtype": "Select", - "label": "Is Advance", - "no_copy": 1, - "oldfieldname": "is_advance", - "oldfieldtype": "Select", - "options": "No\nYes", - "permlevel": 0, + "fieldname": "is_advance", + "fieldtype": "Select", + "label": "Is Advance", + "no_copy": 1, + "oldfieldname": "is_advance", + "oldfieldtype": "Select", + "options": "No\nYes", + "permlevel": 0, "print_hide": 1 - }, + }, { - "fieldname": "against_account", - "fieldtype": "Text", - "hidden": 1, - "label": "Against Account", - "no_copy": 1, - "oldfieldname": "against_account", - "oldfieldtype": "Text", - "permlevel": 0, + "fieldname": "against_account", + "fieldtype": "Text", + "hidden": 1, + "label": "Against Account", + "no_copy": 1, + "oldfieldname": "against_account", + "oldfieldtype": "Text", + "permlevel": 0, "print_hide": 1 } - ], - "idx": 1, - "istable": 1, - "modified": "2014-09-11 18:33:53.705093", - "modified_by": "Administrator", - "module": "Accounts", - "name": "Journal Voucher Detail", - "owner": "Administrator", + ], + "idx": 1, + "istable": 1, + "modified": "2014-09-17 13:02:42.012069", + "modified_by": "Administrator", + "module": "Accounts", + "name": "Journal Voucher Detail", + "owner": "Administrator", "permissions": [] -} +} \ No newline at end of file diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 7b40c3eff2..cf57a61fe5 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -40,13 +40,15 @@ def validate_fiscal_year(date, fiscal_year, label="Date"): throw(_("{0} '{1}' not in Fiscal Year {2}").format(label, formatdate(date), fiscal_year)) @frappe.whitelist() -def get_balance_on(account=None, date=None): +def get_balance_on(account=None, date=None, party_type=None, party=None): if not account and frappe.form_dict.get("account"): account = frappe.form_dict.get("account") + if not date and frappe.form_dict.get("date"): date = frappe.form_dict.get("date") - - acc = frappe.get_doc("Account", account) - acc.check_permission("read") + if not party_type and frappe.form_dict.get("party_type"): + party_type = frappe.form_dict.get("party_type") + if not party and frappe.form_dict.get("party"): + party = frappe.form_dict.get("party") cond = [] if date: @@ -67,19 +69,27 @@ def get_balance_on(account=None, date=None): # hence, assuming balance as 0.0 return 0.0 - # for pl accounts, get balance within a fiscal year - if acc.report_type == 'Profit and Loss': - cond.append("posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" \ - % year_start_date) + if account: + acc = frappe.get_doc("Account", account) + acc.check_permission("read") - # different filter for group and ledger - improved performance - if acc.group_or_ledger=="Group": - cond.append("""exists ( - select * from `tabAccount` ac where ac.name = gle.account - and ac.lft >= %s and ac.rgt <= %s - )""" % (acc.lft, acc.rgt)) - else: - cond.append("""gle.account = "%s" """ % (account.replace('"', '\\"'), )) + # for pl accounts, get balance within a fiscal year + if acc.report_type == 'Profit and Loss': + cond.append("posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" \ + % year_start_date) + + # different filter for group and ledger - improved performance + if acc.group_or_ledger=="Group": + cond.append("""exists ( + select * from `tabAccount` ac where ac.name = gle.account + and ac.lft >= %s and ac.rgt <= %s + )""" % (acc.lft, acc.rgt)) + else: + cond.append("""gle.account = "%s" """ % (account.replace('"', '\\"'), )) + + if party_type and party: + cond.append("""gle.party_type = "%s" and gle.party = "%s" """ % + (party_type.replace('"', '\\"'), party.replace('"', '\\"'))) bal = frappe.db.sql(""" SELECT sum(ifnull(debit, 0)) - sum(ifnull(credit, 0))