refactor!: Item Alternative

This commit is contained in:
Sagar Sharma 2022-05-13 14:50:12 +05:30
parent 92625902ad
commit 6c794afbe7

View File

@ -1,17 +1,15 @@
# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and Contributors # Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and Contributors
# See license.txt # See license.txt
import json
import frappe import frappe
from frappe.tests.utils import FrappeTestCase from frappe.tests.utils import FrappeTestCase
from frappe.utils import flt from frappe.utils import flt
from erpnext.buying.doctype.purchase_order.purchase_order import ( from erpnext.controllers.tests.test_subcontracting_controller import (
make_purchase_receipt, get_subcontracting_order,
make_rm_stock_entry, make_service_item,
set_backflush_based_on,
) )
from erpnext.buying.doctype.purchase_order.test_purchase_order import create_purchase_order
from erpnext.manufacturing.doctype.production_plan.test_production_plan import make_bom from erpnext.manufacturing.doctype.production_plan.test_production_plan import make_bom
from erpnext.manufacturing.doctype.work_order.test_work_order import make_wo_order_test_record from erpnext.manufacturing.doctype.work_order.test_work_order import make_wo_order_test_record
from erpnext.manufacturing.doctype.work_order.work_order import make_stock_entry from erpnext.manufacturing.doctype.work_order.work_order import make_stock_entry
@ -19,6 +17,10 @@ from erpnext.stock.doctype.item.test_item import create_item
from erpnext.stock.doctype.stock_reconciliation.test_stock_reconciliation import ( from erpnext.stock.doctype.stock_reconciliation.test_stock_reconciliation import (
create_stock_reconciliation, create_stock_reconciliation,
) )
from erpnext.subcontracting.doctype.subcontracting_order.subcontracting_order import (
make_rm_stock_entry,
make_subcontracting_receipt,
)
class TestItemAlternative(FrappeTestCase): class TestItemAlternative(FrappeTestCase):
@ -27,9 +29,7 @@ class TestItemAlternative(FrappeTestCase):
make_items() make_items()
def test_alternative_item_for_subcontract_rm(self): def test_alternative_item_for_subcontract_rm(self):
frappe.db.set_value( set_backflush_based_on("BOM")
"Buying Settings", None, "backflush_raw_materials_of_subcontract_based_on", "BOM"
)
create_stock_reconciliation( create_stock_reconciliation(
item_code="Alternate Item For A RW 1", warehouse="_Test Warehouse - _TC", qty=5, rate=2000 item_code="Alternate Item For A RW 1", warehouse="_Test Warehouse - _TC", qty=5, rate=2000
@ -39,15 +39,22 @@ class TestItemAlternative(FrappeTestCase):
) )
supplier_warehouse = "Test Supplier Warehouse - _TC" supplier_warehouse = "Test Supplier Warehouse - _TC"
po = create_purchase_order(
item="Test Finished Goods - A",
is_subcontracted=1,
qty=5,
rate=3000,
supplier_warehouse=supplier_warehouse,
)
rm_item = [ make_service_item("Subcontracted Service Item 1")
service_items = [
{
"warehouse": "_Test Warehouse - _TC",
"item_code": "Subcontracted Service Item 1",
"qty": 5,
"rate": 3000,
"fg_item": "Test Finished Goods - A",
"fg_item_qty": 5,
},
]
sco = get_subcontracting_order(
service_items=service_items, supplier_warehouse=supplier_warehouse
)
rm_items = [
{ {
"item_code": "Test Finished Goods - A", "item_code": "Test Finished Goods - A",
"rm_item_code": "Test FG A RW 1", "rm_item_code": "Test FG A RW 1",
@ -70,14 +77,13 @@ class TestItemAlternative(FrappeTestCase):
}, },
] ]
rm_item_string = json.dumps(rm_item)
reserved_qty_for_sub_contract = frappe.db.get_value( reserved_qty_for_sub_contract = frappe.db.get_value(
"Bin", "Bin",
{"item_code": "Test FG A RW 1", "warehouse": "_Test Warehouse - _TC"}, {"item_code": "Test FG A RW 1", "warehouse": "_Test Warehouse - _TC"},
"reserved_qty_for_sub_contract", "reserved_qty_for_sub_contract",
) )
se = frappe.get_doc(make_rm_stock_entry(po.name, rm_item_string)) se = frappe.get_doc(make_rm_stock_entry(sco.name, rm_items))
se.to_warehouse = supplier_warehouse se.to_warehouse = supplier_warehouse
se.insert() se.insert()
@ -101,22 +107,17 @@ class TestItemAlternative(FrappeTestCase):
after_transfer_reserved_qty_for_sub_contract, flt(reserved_qty_for_sub_contract - 5) after_transfer_reserved_qty_for_sub_contract, flt(reserved_qty_for_sub_contract - 5)
) )
pr = make_purchase_receipt(po.name) scr = make_subcontracting_receipt(sco.name)
pr.save() scr.save()
pr = frappe.get_doc("Purchase Receipt", pr.name) scr = frappe.get_doc("Subcontracting Receipt", scr.name)
status = False status = False
for d in pr.supplied_items: for item in scr.supplied_items:
if d.rm_item_code == "Alternate Item For A RW 1": if item.rm_item_code == "Alternate Item For A RW 1":
status = True status = True
self.assertEqual(status, True) self.assertEqual(status, True)
frappe.db.set_value( set_backflush_based_on("Material Transferred for Subcontract")
"Buying Settings",
None,
"backflush_raw_materials_of_subcontract_based_on",
"Material Transferred for Subcontract",
)
def test_alternative_item_for_production_rm(self): def test_alternative_item_for_production_rm(self):
create_stock_reconciliation( create_stock_reconciliation(