refactor!: Subcontracted Item To Be Transferred

This commit is contained in:
Sagar Sharma 2022-05-13 14:46:44 +05:30
parent 3be663b121
commit 05f05ab75b
2 changed files with 55 additions and 48 deletions

View File

@ -46,10 +46,10 @@ def get_columns():
def get_data(filters):
po_rm_item_details = get_po_items_to_supply(filters)
sco_rm_item_details = get_sco_items_to_supply(filters)
data = []
for row in po_rm_item_details:
for row in sco_rm_item_details:
transferred_qty = row.get("transferred_qty") or 0
if transferred_qty < row.get("reqd_qty", 0):
pending_qty = frappe.utils.flt(row.get("reqd_qty", 0) - transferred_qty)
@ -59,23 +59,22 @@ def get_data(filters):
return data
def get_po_items_to_supply(filters):
def get_sco_items_to_supply(filters):
return frappe.db.get_all(
"Purchase Order",
"Subcontracting Order",
fields=[
"name as purchase_order",
"name as subcontracting_order",
"transaction_date as date",
"supplier as supplier",
"`tabPurchase Order Item Supplied`.rm_item_code as rm_item_code",
"`tabPurchase Order Item Supplied`.required_qty as reqd_qty",
"`tabPurchase Order Item Supplied`.supplied_qty as transferred_qty",
"`tabSubcontracting Order Supplied Item`.rm_item_code as rm_item_code",
"`tabSubcontracting Order Supplied Item`.required_qty as reqd_qty",
"`tabSubcontracting Order Supplied Item`.supplied_qty as transferred_qty",
],
filters=[
["Purchase Order", "per_received", "<", "100"],
["Purchase Order", "is_subcontracted", "=", 1],
["Purchase Order", "supplier", "=", filters.supplier],
["Purchase Order", "transaction_date", "<=", filters.to_date],
["Purchase Order", "transaction_date", ">=", filters.from_date],
["Purchase Order", "docstatus", "=", 1],
["Subcontracting Order", "per_received", "<", "100"],
["Subcontracting Order", "supplier", "=", filters.supplier],
["Subcontracting Order", "transaction_date", "<=", filters.to_date],
["Subcontracting Order", "transaction_date", ">=", filters.from_date],
["Subcontracting Order", "docstatus", "=", 1],
],
)

View File

@ -3,24 +3,36 @@
# Compiled at: 2019-05-06 10:24:35
# Decompiled by https://python-decompiler.com
import json
import frappe
from frappe.tests.utils import FrappeTestCase
from erpnext.buying.doctype.purchase_order.purchase_order import make_rm_stock_entry
from erpnext.buying.doctype.purchase_order.test_purchase_order import create_purchase_order
from erpnext.buying.report.subcontracted_raw_materials_to_be_transferred.subcontracted_raw_materials_to_be_transferred import (
execute,
)
from erpnext.controllers.tests.test_subcontracting_controller import (
get_subcontracting_order,
make_service_item,
)
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry
from erpnext.subcontracting.doctype.subcontracting_order.subcontracting_order import (
make_rm_stock_entry,
)
class TestSubcontractedItemToBeTransferred(FrappeTestCase):
def test_pending_and_transferred_qty(self):
po = create_purchase_order(
item_code="_Test FG Item", is_subcontracted=1, supplier_warehouse="_Test Warehouse 1 - _TC"
)
make_service_item("Subcontracted Service Item 1")
service_items = [
{
"warehouse": "_Test Warehouse - _TC",
"item_code": "Subcontracted Service Item 1",
"qty": 10,
"rate": 500,
"fg_item": "_Test FG Item",
"fg_item_qty": 10,
},
]
sco = get_subcontracting_order(service_items=service_items)
# Material Receipt of RMs
make_stock_entry(item_code="_Test Item", target="_Test Warehouse - _TC", qty=100, basic_rate=100)
@ -28,50 +40,47 @@ class TestSubcontractedItemToBeTransferred(FrappeTestCase):
item_code="_Test Item Home Desktop 100", target="_Test Warehouse - _TC", qty=100, basic_rate=100
)
se = transfer_subcontracted_raw_materials(po)
transfer_subcontracted_raw_materials(sco)
col, data = execute(
filters=frappe._dict(
{
"supplier": po.supplier,
"supplier": sco.supplier,
"from_date": frappe.utils.get_datetime(
frappe.utils.add_to_date(po.transaction_date, days=-10)
frappe.utils.add_to_date(sco.transaction_date, days=-10)
),
"to_date": frappe.utils.get_datetime(frappe.utils.add_to_date(po.transaction_date, days=10)),
"to_date": frappe.utils.get_datetime(frappe.utils.add_to_date(sco.transaction_date, days=10)),
}
)
)
po.reload()
sco.reload()
po_data = [row for row in data if row.get("purchase_order") == po.name]
sco_data = [row for row in data if row.get("subcontracting_order") == sco.name]
# Alphabetically sort to be certain of order
po_data = sorted(po_data, key=lambda i: i["rm_item_code"])
sco_data = sorted(sco_data, key=lambda i: i["rm_item_code"])
self.assertEqual(len(po_data), 2)
self.assertEqual(po_data[0]["purchase_order"], po.name)
self.assertEqual(len(sco_data), 2)
self.assertEqual(sco_data[0]["subcontracting_order"], sco.name)
self.assertEqual(po_data[0]["rm_item_code"], "_Test Item")
self.assertEqual(po_data[0]["p_qty"], 8)
self.assertEqual(po_data[0]["transferred_qty"], 2)
self.assertEqual(sco_data[0]["rm_item_code"], "_Test Item")
self.assertEqual(sco_data[0]["p_qty"], 8)
self.assertEqual(sco_data[0]["transferred_qty"], 2)
self.assertEqual(po_data[1]["rm_item_code"], "_Test Item Home Desktop 100")
self.assertEqual(po_data[1]["p_qty"], 19)
self.assertEqual(po_data[1]["transferred_qty"], 1)
se.cancel()
po.cancel()
self.assertEqual(sco_data[1]["rm_item_code"], "_Test Item Home Desktop 100")
self.assertEqual(sco_data[1]["p_qty"], 19)
self.assertEqual(sco_data[1]["transferred_qty"], 1)
def transfer_subcontracted_raw_materials(po):
# Order of supplied items fetched in PO is flaky
def transfer_subcontracted_raw_materials(sco):
# Order of supplied items fetched in SCO is flaky
transfer_qty_map = {"_Test Item": 2, "_Test Item Home Desktop 100": 1}
item_1 = po.supplied_items[0].rm_item_code
item_2 = po.supplied_items[1].rm_item_code
item_1 = sco.supplied_items[0].rm_item_code
item_2 = sco.supplied_items[1].rm_item_code
rm_item = [
rm_items = [
{
"name": po.supplied_items[0].name,
"name": sco.supplied_items[0].name,
"item_code": item_1,
"rm_item_code": item_1,
"item_name": item_1,
@ -82,7 +91,7 @@ def transfer_subcontracted_raw_materials(po):
"stock_uom": "Nos",
},
{
"name": po.supplied_items[1].name,
"name": sco.supplied_items[1].name,
"item_code": item_2,
"rm_item_code": item_2,
"item_name": item_2,
@ -93,8 +102,7 @@ def transfer_subcontracted_raw_materials(po):
"stock_uom": "Nos",
},
]
rm_item_string = json.dumps(rm_item)
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.from_warehouse = "_Test Warehouse - _TC"
se.to_warehouse = "_Test Warehouse - _TC"
se.stock_entry_type = "Send to Subcontractor"