From e0cc87d2d8591330595f906569488925b828d2a3 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Thu, 21 Jul 2016 18:30:03 +0530 Subject: [PATCH] Unlink payment entries on cancellation of Invoice --- .../doctype/journal_entry/journal_entry.py | 4 +- .../purchase_invoice/purchase_invoice.py | 4 +- .../doctype/sales_invoice/sales_invoice.py | 4 +- erpnext/accounts/utils.py | 42 ++++++++++--- .../doctype/sales_order/sales_order.json | 62 ++++++++++--------- 5 files changed, 73 insertions(+), 43 deletions(-) diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index 6f25220ec5..94709a7b72 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -61,8 +61,8 @@ class JournalEntry(AccountsController): frappe.get_doc(voucher_type, voucher_no).set_total_advance_paid() def on_cancel(self): - from erpnext.accounts.utils import remove_against_link_from_jv - remove_against_link_from_jv(self.doctype, self.name) + from erpnext.accounts.utils import unlink_ref_doc_from_payment_entries + unlink_ref_doc_from_payment_entries(self.doctype, self.name) self.make_gl_entries(1) self.update_advance_paid() diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 5be0b69706..b4ae7f886a 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -553,8 +553,8 @@ class PurchaseInvoice(BuyingController): self.update_status_updater_args() if not self.is_return: - from erpnext.accounts.utils import remove_against_link_from_jv - remove_against_link_from_jv(self.doctype, self.name) + from erpnext.accounts.utils import unlink_ref_doc_from_payment_entries + unlink_ref_doc_from_payment_entries(self.doctype, self.name) self.update_prevdoc_status() self.update_billing_status_for_zero_amount_refdoc("Purchase Order") diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 1df86aa49c..b284619b6c 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -134,8 +134,8 @@ class SalesInvoice(SellingController): def on_cancel(self): self.check_close_sales_order("sales_order") - from erpnext.accounts.utils import remove_against_link_from_jv - remove_against_link_from_jv(self.doctype, self.name) + from erpnext.accounts.utils import unlink_ref_doc_from_payment_entries + unlink_ref_doc_from_payment_entries(self.doctype, self.name) if self.is_return: # NOTE status updating bypassed for is_return diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 635197ff81..6efb28813d 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -323,8 +323,19 @@ def update_reference_in_payment_entry(d, payment_entry): payment_entry.set_missing_values() payment_entry.set_amounts() payment_entry.save(ignore_permissions=True) + +def unlink_ref_doc_from_payment_entries(ref_type, ref_no): + remove_ref_doc_link_from_jv(ref_type, ref_no) + remove_ref_doc_link_from_pe(ref_type, ref_no) + + frappe.db.sql("""update `tabGL Entry` + set against_voucher_type=null, against_voucher=null, + modified=%s, modified_by=%s + where against_voucher_type=%s and against_voucher=%s + and voucher_no != ifnull(against_voucher, '')""", + (now(), frappe.session.user, ref_type, ref_no)) -def remove_against_link_from_jv(ref_type, ref_no): +def remove_ref_doc_link_from_jv(ref_type, ref_no): linked_jv = frappe.db.sql_list("""select parent from `tabJournal Entry Account` where reference_type=%s and reference_name=%s and docstatus < 2""", (ref_type, ref_no)) @@ -335,15 +346,30 @@ def remove_against_link_from_jv(ref_type, ref_no): where reference_type=%s and reference_name=%s and docstatus < 2""", (now(), frappe.session.user, ref_type, ref_no)) - frappe.db.sql("""update `tabGL Entry` - set against_voucher_type=null, against_voucher=null, - modified=%s, modified_by=%s - where against_voucher_type=%s and against_voucher=%s - and voucher_no != ifnull(against_voucher, '')""", - (now(), frappe.session.user, ref_type, ref_no)) - frappe.msgprint(_("Journal Entries {0} are un-linked".format("\n".join(linked_jv)))) + +def remove_ref_doc_link_from_pe(ref_type, ref_no): + linked_pe = frappe.db.sql_list("""select parent from `tabPayment Entry Reference` + where reference_doctype=%s and reference_name=%s and docstatus < 2""", (ref_type, ref_no)) + if linked_pe: + frappe.db.sql("""update `tabPayment Entry Reference` + set allocated_amount=0, modified=%s, modified_by=%s + where reference_doctype=%s and reference_name=%s + and docstatus < 2""", (now(), frappe.session.user, ref_type, ref_no)) + + for pe in linked_pe: + pe_doc = frappe.get_doc("Payment Entry", pe) + pe_doc.set_total_allocated_amount() + pe_doc.set_unallocated_amount() + pe_doc.clear_unallocated_reference_document_rows() + + frappe.db.sql("""update `tabPayment Entry` set total_allocated_amount=%s, + base_total_allocated_amount=%s, unallocated_amount=%s, modified=%s, modified_by=%s + where name=%s""", (pe_doc.total_allocated_amount, pe_doc.base_total_allocated_amount, + pe_doc.unallocated_amount, now(), frappe.session.user, pe)) + + frappe.msgprint(_("Payment Entries {0} are un-linked".format("\n".join(linked_pe)))) @frappe.whitelist() def get_company_default(company, fieldname): diff --git a/erpnext/selling/doctype/sales_order/sales_order.json b/erpnext/selling/doctype/sales_order/sales_order.json index 66f6b717f5..3699595cba 100644 --- a/erpnext/selling/doctype/sales_order/sales_order.json +++ b/erpnext/selling/doctype/sales_order/sales_order.json @@ -3,11 +3,13 @@ "allow_import": 1, "allow_rename": 0, "autoname": "naming_series:", + "beta": 0, "creation": "2013-06-18 12:39:59", "custom": 0, "docstatus": 0, "doctype": "DocType", "document_type": "Document", + "editable_grid": 1, "fields": [ { "allow_on_submit": 0, @@ -712,6 +714,34 @@ "set_only_once": 0, "unique": 0 }, + { + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "fieldname": "currency", + "fieldtype": "Link", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_list_view": 0, + "label": "Currency", + "length": 0, + "no_copy": 0, + "oldfieldname": "currency", + "oldfieldtype": "Select", + "options": "Currency", + "permlevel": 0, + "print_hide": 1, + "print_hide_if_no_value": 0, + "read_only": 0, + "report_hide": 0, + "reqd": 1, + "search_index": 0, + "set_only_once": 0, + "unique": 0, + "width": "100px" + }, { "allow_on_submit": 0, "bold": 0, @@ -741,34 +771,6 @@ "unique": 0, "width": "100px" }, - { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "fieldname": "currency", - "fieldtype": "Link", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_list_view": 0, - "label": "Currency", - "length": 0, - "no_copy": 0, - "oldfieldname": "currency", - "oldfieldtype": "Select", - "options": "Currency", - "permlevel": 0, - "print_hide": 1, - "print_hide_if_no_value": 0, - "read_only": 0, - "report_hide": 0, - "reqd": 1, - "search_index": 0, - "set_only_once": 0, - "unique": 0, - "width": "100px" - }, { "allow_on_submit": 0, "bold": 0, @@ -2989,13 +2991,14 @@ "hide_toolbar": 0, "icon": "icon-file-text", "idx": 105, + "image_view": 0, "in_create": 0, "in_dialog": 0, "is_submittable": 1, "issingle": 0, "istable": 0, "max_attachments": 0, - "modified": "2016-04-14 12:34:16.220066", + "modified": "2016-07-21 17:24:25.306923", "modified_by": "Administrator", "module": "Selling", "name": "Sales Order", @@ -3122,6 +3125,7 @@ "write": 1 } ], + "quick_entry": 0, "read_only": 0, "read_only_onload": 1, "search_fields": "status,transaction_date,customer,customer_name, territory,order_type,company",