Skip to content

Commit

Permalink
refactor: fetching of account balances when node is expanded
Browse files Browse the repository at this point in the history
  • Loading branch information
nextchamp-saqib committed Sep 28, 2021
1 parent 3111a66 commit 4519a85
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 33 deletions.
67 changes: 42 additions & 25 deletions erpnext/accounts/doctype/account/account_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,48 @@ frappe.treeview_settings["Account"] = {
],
root_label: "Accounts",
get_tree_nodes: 'erpnext.accounts.utils.get_children',
on_get_node: function(nodes, deep=false) {
if (frappe.boot.user.can_read.indexOf("GL Entry") == -1) return;

if (deep) {
// in case of `get_all_nodes`
accounts = nodes.reduce((acc, node) => [...acc, ...node.data], []);
} else {
accounts = nodes;
}

const get_balances = frappe.call({
method: 'erpnext.accounts.utils.get_account_balances',
args: {
accounts: accounts,
company: cur_tree.args.company
},
});

get_balances.then(r => {
if (!r.message || r.message.length == 0) return;

for (let account of r.message) {

const node = cur_tree.nodes && cur_tree.nodes[account.value];
if (!node || node.is_root) continue;

// show Dr if positive since balance is calculated as debit - credit else show Cr
const balance = account.balance_in_account_currency || account.balance;
const dr_or_cr = balance > 0 ? "Dr": "Cr";
const format = (value, currency) => format_currency(Math.abs(value), currency);

if (account.balance!==undefined) {
$('<span class="balance-area pull-right">'
+ (account.balance_in_account_currency ?
(format(account.balance_in_account_currency, account.account_currency) + " / ") : "")
+ format(account.balance, account.company_currency)
+ " " + dr_or_cr
+ '</span>').insertBefore(node.$ul);
}
}
});
},
add_tree_node: 'erpnext.accounts.utils.add_ac',
menu_items:[
{
Expand Down Expand Up @@ -122,31 +164,6 @@ frappe.treeview_settings["Account"] = {
}
}, "add");
},
onrender: function(node) {
if (frappe.boot.user.can_read.indexOf("GL Entry") !== -1 && !node.is_root && node.data) {

frappe.call({
method: 'erpnext.accounts.utils.get_account_balance',
args: {account: node.data, ...cur_tree.args},
callback: function(r) {
let account = r.message;
// show Dr if positive since balance is calculated as debit - credit else show Cr
let balance = account.balance_in_account_currency || account.balance;
let dr_or_cr = balance > 0 ? "Dr": "Cr";
let format = (value, currency) => format_currency(Math.abs(value), currency);

if (account.balance!==undefined) {
$('<span class="balance-area pull-right">'
+ (account.balance_in_account_currency ?
(format(account.balance_in_account_currency, account.account_currency) + " / ") : "")
+ format(account.balance, account.company_currency)
+ " " + dr_or_cr
+ '</span>').insertBefore(node.$ul);
}
}
});
}
},
toolbar: [
{
label:__("Add Child"),
Expand Down
22 changes: 14 additions & 8 deletions erpnext/accounts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,17 +794,23 @@ def get_children(doctype, parent, company, is_root=False):
return acc

@frappe.whitelist()
def get_account_balance(account, company):
if isinstance(account, string_types):
account = loads(account)
def get_account_balances(accounts, company):

if isinstance(accounts, string_types):
accounts = loads(accounts)

if not accounts:
return []

company_currency = frappe.get_cached_value("Company", company, "default_currency")
account["company_currency"] = company_currency
account["balance"] = flt(get_balance_on(account["value"], in_account_currency=False, company=company))
if account["account_currency"] and account["account_currency"] != company_currency:
account["balance_in_account_currency"] = flt(get_balance_on(account["value"], company=company))

return account
for account in accounts:
account["company_currency"] = company_currency
account["balance"] = flt(get_balance_on(account["value"], in_account_currency=False, company=company))
if account["account_currency"] and account["account_currency"] != company_currency:
account["balance_in_account_currency"] = flt(get_balance_on(account["value"], company=company))

return accounts

def create_payment_gateway_account(gateway, payment_channel="Email"):
from erpnext.setup.setup_wizard.operations.install_fixtures import create_bank_account
Expand Down

0 comments on commit 4519a85

Please sign in to comment.