From 8a27cf3785f25fdbc2f5c14d62f79351fe79aea8 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 16 May 2017 12:43:00 +0530 Subject: [PATCH] Fix status of invoices with negative outstanding, if no return entry (#8829) * Move allowance field in Item to the first section to apply it for both stock and non-stock items * Fix status of invoices with negative outstanding, if no return entry * get_value included in safe_eval --- erpnext/controllers/status_updater.py | 11 +-- erpnext/patches.txt | 3 +- ..._for_invoices_with_negative_outstanding.py | 23 +++++++ erpnext/stock/doctype/item/item.json | 68 +++++++++---------- 4 files changed, 65 insertions(+), 40 deletions(-) create mode 100644 erpnext/patches/v8_0/fix_status_for_invoices_with_negative_outstanding.py diff --git a/erpnext/controllers/status_updater.py b/erpnext/controllers/status_updater.py index d58ba4b841..403430606f 100644 --- a/erpnext/controllers/status_updater.py +++ b/erpnext/controllers/status_updater.py @@ -46,8 +46,8 @@ status_map = { ["Draft", None], ["Submitted", "eval:self.docstatus==1"], ["Return", "eval:self.is_return==1 and self.docstatus==1"], - ["Credit Note Issued", "eval:self.outstanding_amount < 0 and self.docstatus==1"], - ["Paid", "eval:self.outstanding_amount==0 and self.docstatus==1 and self.is_return==0"], + ["Paid", "eval:self.outstanding_amount<=0 and self.docstatus==1 and self.is_return==0"], + ["Credit Note Issued", "eval:self.outstanding_amount < 0 and self.docstatus==1 and self.is_return==0 and get_value('Sales Invoice', {'is_return': 1, 'return_against': self.name, 'docstatus': 1})"], ["Unpaid", "eval:self.outstanding_amount > 0 and getdate(self.due_date) >= getdate(nowdate()) and self.docstatus==1"], ["Overdue", "eval:self.outstanding_amount > 0 and getdate(self.due_date) < getdate(nowdate()) and self.docstatus==1"], ["Cancelled", "eval:self.docstatus==2"], @@ -56,8 +56,8 @@ status_map = { ["Draft", None], ["Submitted", "eval:self.docstatus==1"], ["Return", "eval:self.is_return==1 and self.docstatus==1"], - ["Debit Note Issued", "eval:self.outstanding_amount < 0 and self.docstatus==1"], - ["Paid", "eval:self.outstanding_amount==0 and self.docstatus==1 and self.is_return==0"], + ["Paid", "eval:self.outstanding_amount<=0 and self.docstatus==1 and self.is_return==0"], + ["Debit Note Issued", "eval:self.outstanding_amount < 0 and self.docstatus==1 and self.is_return==0 and get_value('Purchase Invoice', {'is_return': 1, 'return_against': self.name, 'docstatus': 1})"], ["Unpaid", "eval:self.outstanding_amount > 0 and getdate(self.due_date) >= getdate(nowdate()) and self.docstatus==1"], ["Overdue", "eval:self.outstanding_amount > 0 and getdate(self.due_date) < getdate(nowdate()) and self.docstatus==1"], ["Cancelled", "eval:self.docstatus==2"], @@ -119,7 +119,8 @@ class StatusUpdater(Document): self.status = s[0] break elif s[1].startswith("eval:"): - if frappe.safe_eval(s[1][5:], None, { "self": self.as_dict(), "getdate": getdate, "nowdate": nowdate }): + if frappe.safe_eval(s[1][5:], None, { "self": self.as_dict(), "getdate": getdate, + "nowdate": nowdate, "get_value": frappe.db.get_value }): self.status = s[0] break elif getattr(self, s[1])(): diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 81367937a5..ac91f6038b 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -394,4 +394,5 @@ erpnext.patches.v8_0.revert_manufacturers_table_from_item erpnext.patches.v8_0.disable_instructor_role erpnext.patches.v8_0.merge_student_batch_and_student_group erpnext.patches.v8_0.rename_total_margin_to_rate_with_margin # 11-05-2017 -erpnext.patches.v8_0.make_payments_table_blank_for_non_pos_invoice \ No newline at end of file +erpnext.patches.v8_0.fix_status_for_invoices_with_negative_outstanding +erpnext.patches.v8_0.make_payments_table_blank_for_non_pos_invoice diff --git a/erpnext/patches/v8_0/fix_status_for_invoices_with_negative_outstanding.py b/erpnext/patches/v8_0/fix_status_for_invoices_with_negative_outstanding.py new file mode 100644 index 0000000000..2e7f360c97 --- /dev/null +++ b/erpnext/patches/v8_0/fix_status_for_invoices_with_negative_outstanding.py @@ -0,0 +1,23 @@ +# Copyright (c) 2017, Frappe and Contributors +# License: GNU General Public License v3. See license.txt + +from __future__ import unicode_literals +import frappe + +def execute(): + for dt, status in [["Sales Invoice", "Credit Note Issued"], ["Purchase Invoice", "Debit Note Issued"]]: + invoices = frappe.db.sql(""" + select name + from `tab{0}` + where + status = %s + and outstanding_amount < 0 + and docstatus=1 + and is_return=0 + """.format(dt), status) + + for inv in invoices: + return_inv = frappe.db.sql("""select name from `tab{0}` + where is_return=1 and return_against=%s and docstatus=1""".format(dt), inv[0]) + if not return_inv: + frappe.db.sql("update `tab{0}` set status='Paid' where name = %s".format(dt), inv[0]) \ No newline at end of file diff --git a/erpnext/stock/doctype/item/item.json b/erpnext/stock/doctype/item/item.json index d42e60d1bf..25f30552b5 100644 --- a/erpnext/stock/doctype/item/item.json +++ b/erpnext/stock/doctype/item/item.json @@ -513,6 +513,39 @@ "set_only_once": 0, "unique": 0 }, + { + "allow_bulk_edit": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "columns": 0, + "depends_on": "eval:!doc.__islocal", + "description": "", + "fieldname": "tolerance", + "fieldtype": "Float", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_global_search": 0, + "in_list_view": 0, + "in_standard_filter": 0, + "label": "Allow over delivery or receipt upto this percent", + "length": 0, + "no_copy": 0, + "oldfieldname": "tolerance", + "oldfieldtype": "Currency", + "permlevel": 0, + "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 0, + "remember_last_selected_value": 0, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, { "allow_bulk_edit": 0, "allow_on_submit": 0, @@ -800,39 +833,6 @@ "unique": 0, "width": "50%" }, - { - "allow_bulk_edit": 0, - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "depends_on": "is_stock_item", - "description": "", - "fieldname": "tolerance", - "fieldtype": "Float", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "Allow over delivery or receipt upto this percent", - "length": 0, - "no_copy": 0, - "oldfieldname": "tolerance", - "oldfieldtype": "Currency", - "permlevel": 0, - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "unique": 0 - }, { "allow_bulk_edit": 0, "allow_on_submit": 0, @@ -3143,7 +3143,7 @@ "issingle": 0, "istable": 0, "max_attachments": 1, - "modified": "2017-05-03 09:55:11.624283", + "modified": "2017-05-15 11:49:47.525859", "modified_by": "Administrator", "module": "Stock", "name": "Item",