From 97ea1f5123b55f30d07418485302f68bae1d81ef Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 11 May 2022 14:33:58 +0000 Subject: [PATCH] fix: per_billed for return DN (backport #30868) (#30971) This is a semi-automatic backport of pull request #30868 done by [Mergify](https://mergify.com). --- erpnext/patches.txt | 1 + .../set_per_billed_in_return_delivery_note.py | 29 ++++++++++++++ .../delivery_note/test_delivery_note.py | 38 +++++++++++++++++++ .../delivery_note_item.json | 6 ++- 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 erpnext/patches/v13_0/set_per_billed_in_return_delivery_note.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 5d95f824dce3..94e7bfc16d25 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -364,3 +364,4 @@ erpnext.patches.v13_0.copy_custom_field_filters_to_website_item erpnext.patches.v13_0.set_available_for_use_date_if_missing erpnext.patches.v13_0.education_deprecation_warning erpnext.patches.v13_0.create_accounting_dimensions_in_orders +erpnext.patches.v13_0.set_per_billed_in_return_delivery_note diff --git a/erpnext/patches/v13_0/set_per_billed_in_return_delivery_note.py b/erpnext/patches/v13_0/set_per_billed_in_return_delivery_note.py new file mode 100644 index 000000000000..a4d701244925 --- /dev/null +++ b/erpnext/patches/v13_0/set_per_billed_in_return_delivery_note.py @@ -0,0 +1,29 @@ +# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors +# For license information, please see license.txt + +import frappe + + +def execute(): + dn = frappe.qb.DocType("Delivery Note") + dn_item = frappe.qb.DocType("Delivery Note Item") + + dn_list = ( + frappe.qb.from_(dn) + .inner_join(dn_item) + .on(dn.name == dn_item.parent) + .select(dn.name) + .where(dn.docstatus == 1) + .where(dn.is_return == 1) + .where(dn.per_billed < 100) + .where(dn_item.returned_qty > 0) + .run(as_dict=True) + ) + + frappe.qb.update(dn_item).inner_join(dn).on(dn.name == dn_item.parent).set( + dn_item.returned_qty, 0 + ).where(dn.is_return == 1).where(dn_item.returned_qty > 0).run() + + for d in dn_list: + dn_doc = frappe.get_doc("Delivery Note", d.get("name")) + dn_doc.run_method("update_billing_status") diff --git a/erpnext/stock/doctype/delivery_note/test_delivery_note.py b/erpnext/stock/doctype/delivery_note/test_delivery_note.py index f97e7ca9c680..0738bfbd8fc0 100644 --- a/erpnext/stock/doctype/delivery_note/test_delivery_note.py +++ b/erpnext/stock/doctype/delivery_note/test_delivery_note.py @@ -962,6 +962,44 @@ def test_payment_terms_are_fetched_when_creating_sales_invoice(self): automatically_fetch_payment_terms(enable=0) + def test_returned_qty_in_return_dn(self): + # SO ---> SI ---> DN + # | + # |---> DN(Partial Sales Return) ---> SI(Credit Note) + # | + # |---> DN(Partial Sales Return) ---> SI(Credit Note) + + from erpnext.accounts.doctype.sales_invoice.sales_invoice import make_delivery_note + from erpnext.selling.doctype.sales_order.sales_order import make_sales_invoice + + so = make_sales_order(qty=10) + si = make_sales_invoice(so.name) + si.insert() + si.submit() + dn = make_delivery_note(si.name) + dn.insert() + dn.submit() + self.assertEqual(dn.items[0].returned_qty, 0) + self.assertEqual(dn.per_billed, 100) + + from erpnext.stock.doctype.delivery_note.delivery_note import make_sales_invoice + + dn1 = create_delivery_note(is_return=1, return_against=dn.name, qty=-3) + si1 = make_sales_invoice(dn1.name) + si1.insert() + si1.submit() + dn1.reload() + self.assertEqual(dn1.items[0].returned_qty, 0) + self.assertEqual(dn1.per_billed, 100) + + dn2 = create_delivery_note(is_return=1, return_against=dn.name, qty=-4) + si2 = make_sales_invoice(dn2.name) + si2.insert() + si2.submit() + dn2.reload() + self.assertEqual(dn2.items[0].returned_qty, 0) + self.assertEqual(dn2.per_billed, 100) + def create_delivery_note(**args): dn = frappe.new_doc("Delivery Note") diff --git a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json index e2eb2a4bbb27..2d7abc8a0d68 100644 --- a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json +++ b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json @@ -737,7 +737,9 @@ "depends_on": "returned_qty", "fieldname": "returned_qty", "fieldtype": "Float", - "label": "Returned Qty in Stock UOM" + "label": "Returned Qty in Stock UOM", + "no_copy": 1, + "read_only": 1 }, { "fieldname": "incoming_rate", @@ -778,7 +780,7 @@ "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2022-03-31 18:36:24.671913", + "modified": "2022-05-02 12:09:39.610075", "modified_by": "Administrator", "module": "Stock", "name": "Delivery Note Item",