Party balance

This commit is contained in:
Nabin Hait 2014-09-17 14:11:22 +05:30
parent 997a7a1d3e
commit 6f17cf93c5
4 changed files with 233 additions and 199 deletions

View File

@ -77,7 +77,9 @@ erpnext.accounts.JournalVoucher = frappe.ui.form.Controller.extend({
}, },
setup_balance_formatter: function() { setup_balance_formatter: function() {
var df = frappe.meta.get_docfield("Journal Voucher Detail", "balance", this.frm.doc.name); 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) { df.formatter = function(value, df, options, doc) {
var currency = frappe.meta.get_field_currency(df, doc); var currency = frappe.meta.get_field_currency(df, doc);
var dr_or_cr = value ? ('<label>' + (value > 0.0 ? __("Dr") : __("Cr")) + '</label>') : ""; var dr_or_cr = value ? ('<label>' + (value > 0.0 ? __("Dr") : __("Cr")) + '</label>') : "";
@ -86,6 +88,7 @@ erpnext.accounts.JournalVoucher = frappe.ui.form.Controller.extend({
+ " " + dr_or_cr + " " + dr_or_cr
+ "</div>"; + "</div>";
} }
})
}, },
against_voucher: function(doc, cdt, cdn) { 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) { 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) { if(!d.account && d.party_type && d.party) {
return frappe.call({ return frm.call({
method: "erpnext.accounts.party.get_party_account", method: "erpnext.accounts.doctype.journal_voucher.journal_voucher.get_party_account_and_balance",
child: d,
args: { args: {
company: frm.doc.company, company: frm.doc.company,
party_type: d.party_type, party_type: d.party_type,
party: d.party party: d.party
},
callback: function(r) {
if(!r.exc && r.message) {
frappe.model.set_value(cdt, cdn, "account", r.message);
}
} }
}); });
} }

View File

@ -7,6 +7,8 @@ from frappe.utils import cstr, flt, fmt_money, formatdate, getdate, money_in_wor
from frappe import msgprint, _, scrub from frappe import msgprint, _, scrub
from erpnext.setup.utils import get_company_currency from erpnext.setup.utils import get_company_currency
from erpnext.controllers.accounts_controller import AccountsController from erpnext.controllers.accounts_controller import AccountsController
from erpnext.accounts.utils import get_balance_on
class JournalVoucher(AccountsController): class JournalVoucher(AccountsController):
def __init__(self, arg1, arg2=None): def __init__(self, arg1, arg2=None):
@ -400,7 +402,6 @@ class JournalVoucher(AccountsController):
@frappe.whitelist() @frappe.whitelist()
def get_default_bank_cash_account(company, voucher_type): def get_default_bank_cash_account(company, voucher_type):
from erpnext.accounts.utils import get_balance_on
account = frappe.db.get_value("Company", company, account = frappe.db.get_value("Company", company,
voucher_type=="Bank Voucher" and "default_bank_account" or "default_cash_account") voucher_type=="Bank Voucher" and "default_bank_account" or "default_cash_account")
if 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_type = "Customer"
jv.get("entries")[0].party = si.customer jv.get("entries")[0].party = si.customer
jv.get("entries")[0].balance = get_balance_on(si.debit_to) 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].credit = si.outstanding_amount
jv.get("entries")[0].against_invoice = si.name jv.get("entries")[0].against_invoice = si.name
@ -431,7 +433,6 @@ def get_payment_entry_from_sales_invoice(sales_invoice):
@frappe.whitelist() @frappe.whitelist()
def get_payment_entry_from_purchase_invoice(purchase_invoice): 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) pi = frappe.get_doc("Purchase Invoice", purchase_invoice)
jv = get_payment_entry(pi) jv = get_payment_entry(pi)
jv.remark = 'Payment against Purchase Invoice {0}. {1}'.format(pi.name, pi.remarks) 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_type = "Supplier"
jv.get("entries")[0].party = pi.supplier jv.get("entries")[0].party = pi.supplier
jv.get("entries")[0].balance = get_balance_on(pi.credit_to) 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].debit = pi.outstanding_amount
jv.get("entries")[0].against_voucher = pi.name jv.get("entries")[0].against_voucher = pi.name
@ -469,7 +471,6 @@ def get_payment_entry(doc):
@frappe.whitelist() @frappe.whitelist()
def get_opening_accounts(company): def get_opening_accounts(company):
"""get all balance sheet accounts for opening entry""" """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 accounts = frappe.db.sql_list("""select name from tabAccount
where group_or_ledger='Ledger' and report_type='Balance Sheet' and company=%s""", company) where group_or_ledger='Ledger' and report_type='Balance Sheet' and company=%s""", company)
@ -508,3 +509,17 @@ def get_outstanding(args):
return { return {
"debit": flt(frappe.db.get_value("Purchase Invoice", args["docname"], "outstanding_amount")) "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
}

View File

@ -19,6 +19,19 @@
"search_index": 1, "search_index": 1,
"width": "250px" "width": "250px"
}, },
{
"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
},
{ {
"default": ":Company", "default": ":Company",
"description": "If Income or Expense", "description": "If Income or Expense",
@ -56,21 +69,18 @@
"permlevel": 0 "permlevel": 0
}, },
{ {
"fieldname": "balance", "fieldname": "party_balance",
"fieldtype": "Currency", "fieldtype": "Currency",
"in_list_view": 1, "label": "Party Balance",
"label": "Account Balance",
"no_copy": 1,
"oldfieldname": "balance",
"oldfieldtype": "Data",
"options": "Company:company:default_currency", "options": "Company:company:default_currency",
"permlevel": 0, "permlevel": 0,
"print_hide": 1, "precision": "",
"read_only": 1 "read_only": 1
}, },
{ {
"fieldname": "sec_break1", "fieldname": "sec_break1",
"fieldtype": "Section Break", "fieldtype": "Section Break",
"label": "Amount",
"permlevel": 0 "permlevel": 0
}, },
{ {
@ -188,7 +198,7 @@
], ],
"idx": 1, "idx": 1,
"istable": 1, "istable": 1,
"modified": "2014-09-11 18:33:53.705093", "modified": "2014-09-17 13:02:42.012069",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Accounts", "module": "Accounts",
"name": "Journal Voucher Detail", "name": "Journal Voucher Detail",

View File

@ -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)) throw(_("{0} '{1}' not in Fiscal Year {2}").format(label, formatdate(date), fiscal_year))
@frappe.whitelist() @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"): if not account and frappe.form_dict.get("account"):
account = 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") date = frappe.form_dict.get("date")
if not party_type and frappe.form_dict.get("party_type"):
acc = frappe.get_doc("Account", account) party_type = frappe.form_dict.get("party_type")
acc.check_permission("read") if not party and frappe.form_dict.get("party"):
party = frappe.form_dict.get("party")
cond = [] cond = []
if date: if date:
@ -67,6 +69,10 @@ def get_balance_on(account=None, date=None):
# hence, assuming balance as 0.0 # hence, assuming balance as 0.0
return 0.0 return 0.0
if account:
acc = frappe.get_doc("Account", account)
acc.check_permission("read")
# for pl accounts, get balance within a fiscal year # for pl accounts, get balance within a fiscal year
if acc.report_type == 'Profit and Loss': if acc.report_type == 'Profit and Loss':
cond.append("posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" \ cond.append("posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" \
@ -81,6 +87,10 @@ def get_balance_on(account=None, date=None):
else: else:
cond.append("""gle.account = "%s" """ % (account.replace('"', '\\"'), )) 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(""" bal = frappe.db.sql("""
SELECT sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) SELECT sum(ifnull(debit, 0)) - sum(ifnull(credit, 0))
FROM `tabGL Entry` gle FROM `tabGL Entry` gle