fix: per_billed for return DN (#30868)
* fix: per_billed for return DN
* chore: move patch from v14 to v13
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index d5b1592..1fef240 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -369,4 +369,5 @@
erpnext.patches.v13_0.change_default_item_manufacturer_fieldtype
erpnext.patches.v14_0.discount_accounting_separation
erpnext.patches.v14_0.delete_employee_transfer_property_doctype
-erpnext.patches.v13_0.create_accounting_dimensions_in_orders
\ No newline at end of file
+erpnext.patches.v13_0.create_accounting_dimensions_in_orders
+erpnext.patches.v13_0.set_per_billed_in_return_delivery_note
\ No newline at end of file
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 0000000..a4d7012
--- /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 f97e7ca..0738bfb 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 @@
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 e2eb2a4..2d7abc8 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",