Merge pull request #29642 from marination/packed-items-new-doc

fix: Regenerate packed items on newly mapped doc
This commit is contained in:
Marica 2022-02-04 23:00:10 +05:30 committed by GitHub
commit 772336bb43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 10 deletions

View File

@ -27,8 +27,7 @@ def make_packing_list(doc):
stale_packed_items_table = get_indexed_packed_items_table(doc)
if not doc.is_new():
reset = reset_packing_list_if_deleted_items_exist(doc)
reset = reset_packing_list(doc)
for item_row in doc.get("items"):
if frappe.db.exists("Product Bundle", {"new_item_code": item_row.item_code}):
@ -64,20 +63,24 @@ def get_indexed_packed_items_table(doc):
return indexed_table
def reset_packing_list_if_deleted_items_exist(doc):
doc_before_save = doc.get_doc_before_save()
def reset_packing_list(doc):
"Conditionally reset the table and return if it was reset or not."
reset_table = False
doc_before_save = doc.get_doc_before_save()
if doc_before_save:
# reset table if:
# 1. items were deleted
# 2. if bundle item replaced by another item (same no. of items but different items)
# we maintain list to maintain repeated item rows as well
# we maintain list to track recurring item rows as well
items_before_save = [item.item_code for item in doc_before_save.get("items")]
items_after_save = [item.item_code for item in doc.get("items")]
reset_table = items_before_save != items_after_save
else:
reset_table = True # reset if via Update Items (cannot determine action)
# reset: if via Update Items OR
# if new mapped doc with packed items set (SO -> DN)
# (cannot determine action)
reset_table = True
if reset_table:
doc.set("packed_items", [])

View File

@ -2,6 +2,7 @@
# License: GNU General Public License v3. See license.txt
from erpnext.selling.doctype.product_bundle.test_product_bundle import make_product_bundle
from erpnext.selling.doctype.sales_order.sales_order import make_delivery_note
from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order
from erpnext.stock.doctype.item.test_item import make_item
from erpnext.tests.utils import ERPNextTestCase, change_settings
@ -22,7 +23,7 @@ class TestPackedItem(ERPNextTestCase):
qty=2
)
def test_sales_order_adding_bundle_item(self):
def test_adding_bundle_item(self):
"Test impact on packed items if bundle item row is added."
so = make_sales_order(item_code = "_Test Product Bundle X", qty=1,
do_not_submit=True)
@ -32,7 +33,7 @@ class TestPackedItem(ERPNextTestCase):
self.assertEqual(so.packed_items[0].item_code, "_Test Bundle Item 1")
self.assertEqual(so.packed_items[0].qty, 2)
def test_sales_order_updating_bundle_item(self):
def test_updating_bundle_item(self):
"Test impact on packed items if bundle item row is updated."
so = make_sales_order(item_code = "_Test Product Bundle X", qty=1,
do_not_submit=True)
@ -49,7 +50,7 @@ class TestPackedItem(ERPNextTestCase):
self.assertEqual(len(so.packed_items), 0)
def test_sales_order_recurring_bundle_item(self):
def test_recurring_bundle_item(self):
"Test impact on packed items if same bundle item is added and removed."
so_items = []
for qty in [2, 4, 6, 8]:
@ -91,7 +92,7 @@ class TestPackedItem(ERPNextTestCase):
self.assertEqual(so.packed_items[3].qty, 12)
@change_settings("Selling Settings", {"editable_bundle_item_rates": 1})
def test_sales_order_bundle_item_cumulative_price(self):
def test_bundle_item_cumulative_price(self):
"Test if Bundle Item rate is cumulative from packed items."
so = make_sales_order(item_code = "_Test Product Bundle X", qty=2,
do_not_submit=True)
@ -102,3 +103,25 @@ class TestPackedItem(ERPNextTestCase):
self.assertEqual(so.items[0].rate, 350)
self.assertEqual(so.items[0].amount, 700)
def test_newly_mapped_doc_packed_items(self):
"Test impact on packed items in newly mapped DN from SO."
so_items = []
for qty in [2, 4]:
so_items.append({
"item_code": "_Test Product Bundle X",
"qty": qty,
"rate": 400,
"warehouse": "_Test Warehouse - _TC"
})
# create SO with recurring bundle item
so = make_sales_order(item_list=so_items)
dn = make_delivery_note(so.name)
dn.items[1].qty = 3 # change second row qty for inserting doc
dn.save()
self.assertEqual(len(dn.packed_items), 4)
self.assertEqual(dn.packed_items[2].qty, 6)
self.assertEqual(dn.packed_items[3].qty, 6)