From 96bb070781a42a5e82d3bf7dd8c46119b9acdf2b Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Mon, 7 Sep 2015 15:51:40 +0530 Subject: [PATCH 1/2] Payment against invoices where party not specified Conflicts: erpnext/accounts/doctype/gl_entry/gl_entry.py --- erpnext/accounts/doctype/gl_entry/gl_entry.py | 19 +++++++++------ .../doctype/journal_entry/journal_entry.js | 24 ++++++++++++------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/erpnext/accounts/doctype/gl_entry/gl_entry.py b/erpnext/accounts/doctype/gl_entry/gl_entry.py index 43c421352c..c459628482 100644 --- a/erpnext/accounts/doctype/gl_entry/gl_entry.py +++ b/erpnext/accounts/doctype/gl_entry/gl_entry.py @@ -4,7 +4,7 @@ from __future__ import unicode_literals import frappe -from frappe.utils import flt, fmt_money, getdate, formatdate, cstr +from frappe.utils import flt, fmt_money, getdate, formatdate from frappe import _ from frappe.model.document import Document @@ -91,7 +91,7 @@ class GLEntry(Document): if self.cost_center and _get_cost_center_company() != self.company: frappe.throw(_("Cost Center {0} does not belong to Company {1}").format(self.cost_center, self.company)) - + def validate_party(self): if self.party_type and self.party: frozen_accounts_modifier = frappe.db.get_value( 'Accounts Settings', None,'frozen_accounts_modifier') @@ -124,12 +124,18 @@ def check_freezing_date(posting_date, adv_adj=False): frappe.throw(_("You are not authorized to add or update entries before {0}").format(formatdate(acc_frozen_upto))) def update_outstanding_amt(account, party_type, party, against_voucher_type, against_voucher, on_cancel=False): + if party_type and party: + party_condition = " and ifnull(party_type, '')='{0}' and ifnull(party, '')='{1}'"\ + .format(frappe.db.escape(party_type), frappe.db.escape(party)) + else: + party_condition = "" + # get final outstanding amt bal = flt(frappe.db.sql("""select sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) from `tabGL Entry` where against_voucher_type=%s and against_voucher=%s - and account = %s and ifnull(party_type, '')=%s and ifnull(party, '')=%s""", - (against_voucher_type, against_voucher, account, party_type, party))[0][0] or 0.0) + and account = %s {0}""".format(party_condition), + (against_voucher_type, against_voucher, account))[0][0] or 0.0) if against_voucher_type == 'Purchase Invoice': bal = -bal @@ -137,9 +143,8 @@ def update_outstanding_amt(account, party_type, party, against_voucher_type, aga against_voucher_amount = flt(frappe.db.sql(""" select sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) from `tabGL Entry` where voucher_type = 'Journal Entry' and voucher_no = %s - and account = %s and ifnull(party_type, '')=%s and ifnull(party, '')=%s - and ifnull(against_voucher, '') = ''""", - (against_voucher, account, cstr(party_type), cstr(party)))[0][0]) + and account = %s and ifnull(against_voucher, '') = '' {0}""" + .format(party_condition), (against_voucher, account))[0][0]) if not against_voucher_amount: frappe.throw(_("Against Journal Entry {0} is already adjusted against some other voucher") diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.js b/erpnext/accounts/doctype/journal_entry/journal_entry.js index ff1ace49d2..1e77422a25 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.js +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.js @@ -59,7 +59,6 @@ erpnext.accounts.JournalEntry = frappe.ui.form.Controller.extend({ // journal entry if(jvd.reference_type==="Journal Entry") { frappe.model.validate_missing(jvd, "account"); - return { query: "erpnext.accounts.doctype.journal_entry.journal_entry.get_against_jv", filters: { @@ -69,23 +68,32 @@ erpnext.accounts.JournalEntry = frappe.ui.form.Controller.extend({ }; } - // against party - - frappe.model.validate_missing(jvd, "party_type"); - frappe.model.validate_missing(jvd, "party"); - var out = { filters: [ - [jvd.reference_type, jvd.reference_type.indexOf("Sales")===0 ? "customer" : "supplier", "=", jvd.party], - [jvd.reference_type, "docstatus", "=", 1], + [jvd.reference_type, "docstatus", "=", 1] ] }; if(in_list(["Sales Invoice", "Purchase Invoice"], jvd.reference_type)) { out.filters.push([jvd.reference_type, "outstanding_amount", "!=", 0]); + + // account filter + frappe.model.validate_missing(jvd, "account"); + + party_account_field = jvd.reference_type==="Sales Invoice" ? "debit_to": "credit_to"; + out.filters.push([jvd.reference_type, party_account_field, "=", jvd.account]); } else { + // party_type and party mandatory + frappe.model.validate_missing(jvd, "party_type"); + frappe.model.validate_missing(jvd, "party"); + out.filters.push([jvd.reference_type, "per_billed", "<", 100]); } + + if(jvd.party_type && jvd.party) { + out.filters.push([jvd.reference_type, + (jvd.reference_type.indexOf("Sales")===0 ? "customer" : "supplier"), "=", jvd.party]); + } return out; }); From 83a358afc1c7029a030ba040d33c0341f2ccf552 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Mon, 7 Sep 2015 15:22:24 +0530 Subject: [PATCH 2/2] Fix invoice outstanding where party missing Conflicts: erpnext/accounts/doctype/gl_entry/gl_entry.py erpnext/patches.txt --- erpnext/patches.txt | 1 + erpnext/patches/v6_0/fix_outstanding_amount.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 erpnext/patches/v6_0/fix_outstanding_amount.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 298c48cac8..b5fa71bd16 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -204,3 +204,4 @@ erpnext.patches.v6_0.set_default_title # 2015-09-03 erpnext.patches.v6_0.default_activity_rate execute:frappe.db.set_value("Stock Settings", None, "automatically_set_serial_nos_based_on_fifo", 1) execute:frappe.db.sql("""update `tabProject` set percent_complete=round(percent_complete, 2) where percent_complete is not null""") +erpnext.patches.v6_0.fix_outstanding_amount diff --git a/erpnext/patches/v6_0/fix_outstanding_amount.py b/erpnext/patches/v6_0/fix_outstanding_amount.py new file mode 100644 index 0000000000..0de689074f --- /dev/null +++ b/erpnext/patches/v6_0/fix_outstanding_amount.py @@ -0,0 +1,16 @@ +# 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 +from erpnext.accounts.doctype.gl_entry.gl_entry import update_outstanding_amt + +def execute(): + for dt, party_field, account_field in (("Sales Invoice", "customer", "debit_to"), + ("Purchase Invoice", "supplier", "credit_to")): + + wrong_invoices = frappe.db.sql("""select name, {0} as account from `tab{1}` + where docstatus=1 and ifnull({2}, '')=''""".format(account_field, dt, party_field)) + + for invoice, account in wrong_invoices: + update_outstanding_amt(account, party_field.title(), None, dt, invoice) \ No newline at end of file