From 0e46a9b86e44d79db4217654fe1be19c3902f59e Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 18 Aug 2015 18:19:27 +0530 Subject: [PATCH] Journal Entry: client side triggers --- .../doctype/journal_entry/journal_entry.js | 115 ++++++++++++------ .../doctype/journal_entry/journal_entry.py | 40 +++--- .../journal_entry_account.json | 86 ++++++------- 3 files changed, 144 insertions(+), 97 deletions(-) diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.js b/erpnext/accounts/doctype/journal_entry/journal_entry.js index 1107826218..e0d580c595 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.js +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.js @@ -5,8 +5,6 @@ frappe.provide("erpnext.accounts"); frappe.provide("erpnext.journal_entry"); frappe.require("assets/erpnext/js/utils.js"); -cur_frm.add_fetch("account", "currency", "currency"); - frappe.ui.form.on("Journal Entry", { refresh: function(frm) { erpnext.toggle_naming_series(); @@ -189,8 +187,10 @@ erpnext.accounts.JournalEntry = frappe.ui.form.Controller.extend({ // set difference if(doc.difference) { if(doc.difference > 0) { + row.credit_in_account_currency = doc.difference; row.credit = doc.difference; } else { + row.debit_in_account_currency = -doc.difference; row.debit = -doc.difference; } } @@ -211,10 +211,10 @@ cur_frm.cscript.posting_date = function(doc, cdt, cdn){ cur_frm.cscript.update_totals = function(doc) { var td=0.0; var tc =0.0; - var el = doc.accounts || []; - for(var i in el) { - td += flt(el[i].debit, precision("debit", el[i])); - tc += flt(el[i].credit, precision("credit", el[i])); + var accounts = doc.accounts || []; + for(var i in accounts) { + td += flt(accounts[i].debit, precision("debit", accounts[i])); + tc += flt(accounts[i].credit, precision("credit", accounts[i])); } var doc = locals[doc.doctype][doc.name]; doc.total_debit = td; @@ -223,32 +223,12 @@ cur_frm.cscript.update_totals = function(doc) { refresh_many(['total_debit','total_credit','difference']); } -cur_frm.cscript.debit = function(doc,dt,dn) { cur_frm.cscript.update_totals(doc); } -cur_frm.cscript.credit = function(doc,dt,dn) { cur_frm.cscript.update_totals(doc); } - cur_frm.cscript.get_balance = function(doc,dt,dn) { cur_frm.cscript.update_totals(doc); return $c_obj(cur_frm.doc, 'get_balance', '', function(r, rt){ cur_frm.refresh(); }); } -// Get balance -// ----------- - -cur_frm.cscript.account = function(doc,dt,dn) { - var d = locals[dt][dn]; - if(d.account) { - return frappe.call({ - method: "erpnext.accounts.doctype.journal_entry.journal_entry.get_account_balance_and_party_type", - args: {account: d.account, date: doc.posting_date}, - callback: function(r) { - $.extend(d, r.message); - refresh_field('balance', d.name, 'accounts'); - refresh_field('party_type', d.name, 'accounts'); - } - }); - } -} cur_frm.cscript.validate = function(doc,cdt,cdn) { cur_frm.cscript.update_totals(doc); @@ -313,22 +293,77 @@ cur_frm.cscript.voucher_type = function(doc, cdt, cdn) { } } -frappe.ui.form.on("Journal Entry Account", "party", function(frm, cdt, cdn) { - var d = frappe.get_doc(cdt, cdn); - if(!d.account && d.party_type && d.party) { - return frm.call({ - method: "erpnext.accounts.doctype.journal_entry.journal_entry.get_party_account_and_balance", - child: d, - args: { - company: frm.doc.company, - party_type: d.party_type, - party: d.party - } - }); +frappe.ui.form.on("Journal Entry Account", { + party: function(frm, cdt, cdn) { + var d = frappe.get_doc(cdt, cdn); + if(!d.account && d.party_type && d.party) { + return frm.call({ + method: "erpnext.accounts.doctype.journal_entry.journal_entry.get_party_account_and_balance", + child: d, + args: { + company: frm.doc.company, + party_type: d.party_type, + party: d.party + } + }); + } + }, + + account: function(frm, dt, dn) { + var d = locals[dt][dn]; + if(d.account) { + return frappe.call({ + method: "erpnext.accounts.doctype.journal_entry.journal_entry.get_account_balance_and_party_type", + args: { + account: d.account, + date: frm.doc.posting_date, + company: frm.doc.company + }, + callback: function(r) { + if(r.message) { + $.extend(d, r.message[0]); + refresh_field('balance', d.name, 'accounts'); + refresh_field('party_type', d.name, 'accounts'); + refresh_field('currency', d.name, 'accounts'); + + if(r.message[1] && (!frm.doc.exchange_rate || frm.doc.exchange_rate == 1.0)) { + frm.set_value("exchange_rate", r.message[1]) + } + } + } + }); + } + }, + + debit_in_account_currency: function(frm, dt, dn) { + var company_currency = erpnext.get_currency(frm.doc.company); + var row = locals[dt][dn]; + + var exchange_rate = (row.currency==company_currency) ? 1 : frm.doc.exchange_rate; + + frappe.model.set_value(dt, dn, "debit", + flt(flt(row.debit_in_account_currency)*exchange_rate), precision("debit", row)); + }, + + credit_in_account_currency: function(frm, dt, dn) { + var company_currency = erpnext.get_currency(frm.doc.company); + var row = locals[dt][dn]; + + var exchange_rate = (row.currency==company_currency) ? 1 : frm.doc.exchange_rate; + + frappe.model.set_value(dt, dn, "credit", + flt(flt(row.credit_in_account_currency)*exchange_rate), precision("credit", row)); + }, + + debit: function(frm, dt, dn) { + cur_frm.cscript.update_totals(frm.doc); + }, + + credit: function(frm, dt, dn) { + cur_frm.cscript.update_totals(frm.doc); } }) frappe.ui.form.on("Journal Entry Account", "accounts_remove", function(frm) { cur_frm.cscript.update_totals(frm.doc); -}); - +}); \ No newline at end of file diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index 8ea1e58e17..315685da8f 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -5,7 +5,7 @@ from __future__ import unicode_literals import frappe from frappe.utils import cstr, flt, fmt_money, formatdate, getdate, date_diff from frappe import msgprint, _, scrub -from erpnext.setup.utils import get_company_currency +from erpnext.setup.utils import get_company_currency, get_exchange_rate from erpnext.controllers.accounts_controller import AccountsController from erpnext.accounts.utils import get_balance_on @@ -27,8 +27,8 @@ class JournalEntry(AccountsController): self.validate_party() self.validate_cheque_info() self.validate_entries_for_advance() - self.validate_debit_and_credit() self.validate_multi_currency() + self.validate_debit_and_credit() self.validate_against_jv() self.validate_reference_doc() self.set_against_account() @@ -259,8 +259,8 @@ class JournalEntry(AccountsController): if d.debit and d.credit: frappe.throw(_("You cannot credit and debit same account at the same time")) - self.total_debit = flt(self.total_debit) + flt(d.debit, self.precision("debit", "accounts")) - self.total_credit = flt(self.total_credit) + flt(d.credit, self.precision("credit", "accounts")) + self.total_debit = flt(self.total_debit) + flt(d.debit, d.precision("debit")) + self.total_credit = flt(self.total_credit) + flt(d.credit, d.precision("credit")) self.difference = flt(self.total_debit, self.precision("total_debit")) - \ flt(self.total_credit, self.precision("total_credit")) @@ -285,12 +285,10 @@ class JournalEntry(AccountsController): if not d.currency: d.currency = frappe.db.get_value("Account", d.account, "currency") or self.company_currency - exchange_rate = self.exchange_rate - if d.currency != self.company_currency: - exchange_rate = 1 + exchange_rate = self.exchange_rate if d.currency != self.company_currency else 1 - d.debit_in_account_currency = flt(flt(d.debit)*exchange_rate, d.precision("debit_in_account_currency")) - d.credit_in_account_currency = flt(flt(d.credit)*exchange_rate, d.precision("credit_in_account_currency")) + d.debit = flt(flt(d.debit_in_account_currency)*exchange_rate, d.precision("debit")) + d.credit = flt(flt(d.credit_in_account_currency)*exchange_rate, d.precision("credit")) def create_remarks(self): @@ -681,14 +679,28 @@ def get_party_account_and_balance(company, party_type, party): } @frappe.whitelist() -def get_account_balance_and_party_type(account, date): +def get_account_balance_and_party_type(account, date, company): """Returns dict of account balance and party type to be set in Journal Entry on selection of account.""" if not frappe.has_permission("Account"): frappe.msgprint(_("No Permission"), raise_exception=1) - account_type = frappe.db.get_value("Account", account, "account_type") - return { + company_currency = get_company_currency(company) + account_details = frappe.db.get_value("Account", account, ["account_type", "currency"], as_dict=1) + + if account_details.account_type == "Receivable": + party_type = "Customer" + elif account_details.account_type == "Payable": + party_type = "Supplier" + else: + party_type = "" + + exchange_rate = None + if account_details.currency != company_currency: + exchange_rate = get_exchange_rate(account_details.currency, company_currency) + + grid_values = { "balance": get_balance_on(account, date), - "party_type": {"Receivable":"Customer", "Payable":"Supplier"}.get(account_type, "") + "party_type": party_type, + "currency": account_details.currency or company_currency, } - + return grid_values, exchange_rate \ No newline at end of file diff --git a/erpnext/accounts/doctype/journal_entry_account/journal_entry_account.json b/erpnext/accounts/doctype/journal_entry_account/journal_entry_account.json index 5cd7216989..253c267b8a 100644 --- a/erpnext/accounts/doctype/journal_entry_account/journal_entry_account.json +++ b/erpnext/accounts/doctype/journal_entry_account/journal_entry_account.json @@ -241,6 +241,27 @@ "set_only_once": 0, "unique": 0 }, + { + "allow_on_submit": 0, + "fieldname": "debit_in_account_currency", + "fieldtype": "Currency", + "hidden": 1, + "ignore_user_permissions": 0, + "in_filter": 0, + "in_list_view": 0, + "label": "Debit in Account Currency", + "no_copy": 0, + "options": "currency", + "permlevel": 0, + "precision": "", + "print_hide": 0, + "read_only": 0, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, { "allow_on_submit": 0, "fieldname": "debit", @@ -263,27 +284,6 @@ "set_only_once": 0, "unique": 0 }, - { - "allow_on_submit": 0, - "fieldname": "debit_in_account_currency", - "fieldtype": "Currency", - "hidden": 1, - "ignore_user_permissions": 0, - "in_filter": 0, - "in_list_view": 0, - "label": "Debit in Account Currency", - "no_copy": 0, - "options": "currency", - "permlevel": 0, - "precision": "", - "print_hide": 0, - "read_only": 1, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "unique": 0 - }, { "allow_on_submit": 0, "fieldname": "col_break2", @@ -302,6 +302,27 @@ "set_only_once": 0, "unique": 0 }, + { + "allow_on_submit": 0, + "fieldname": "credit_in_account_currency", + "fieldtype": "Currency", + "hidden": 1, + "ignore_user_permissions": 0, + "in_filter": 0, + "in_list_view": 0, + "label": "Credit in Account Currency", + "no_copy": 0, + "options": "currency", + "permlevel": 0, + "precision": "", + "print_hide": 0, + "read_only": 0, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, { "allow_on_submit": 0, "fieldname": "credit", @@ -324,27 +345,6 @@ "set_only_once": 0, "unique": 0 }, - { - "allow_on_submit": 0, - "fieldname": "credit_in_account_currency", - "fieldtype": "Currency", - "hidden": 1, - "ignore_user_permissions": 0, - "in_filter": 0, - "in_list_view": 0, - "label": "Credit in Account Currency", - "no_copy": 0, - "options": "currency", - "permlevel": 0, - "precision": "", - "print_hide": 0, - "read_only": 1, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "unique": 0 - }, { "allow_on_submit": 0, "fieldname": "reference", @@ -476,7 +476,7 @@ "is_submittable": 0, "issingle": 0, "istable": 1, - "modified": "2015-08-18 15:17:42.583090", + "modified": "2015-08-18 17:23:28.378231", "modified_by": "Administrator", "module": "Accounts", "name": "Journal Entry Account",