fix: validate available qty for consumption in SCR

This commit is contained in:
Sagar Sharma 2022-09-04 22:41:48 +05:30
parent e424ad5ff2
commit 4a7add2169
3 changed files with 108 additions and 6 deletions

View File

@ -75,6 +75,7 @@ class SubcontractingReceipt(SubcontractingController):
self.get_current_stock() self.get_current_stock()
def on_submit(self): def on_submit(self):
self.validate_available_qty_for_consumption()
self.update_status_updater_args() self.update_status_updater_args()
self.update_prevdoc_status() self.update_prevdoc_status()
self.set_subcontracting_order_status() self.set_subcontracting_order_status()
@ -107,10 +108,42 @@ class SubcontractingReceipt(SubcontractingController):
self.set_missing_values_in_supplied_items() self.set_missing_values_in_supplied_items()
self.set_missing_values_in_items() self.set_missing_values_in_items()
def set_available_qty_for_consumption(self):
supplied_items_details = {}
sco_supplied_item = frappe.qb.DocType("Subcontracting Order Supplied Item")
for item in self.get("items"):
supplied_items = (
frappe.qb.from_(sco_supplied_item)
.select(
sco_supplied_item.rm_item_code,
sco_supplied_item.reference_name,
(sco_supplied_item.total_supplied_qty - sco_supplied_item.consumed_qty).as_("available_qty"),
)
.where(
(sco_supplied_item.parent == item.subcontracting_order)
& (sco_supplied_item.main_item_code == item.item_code)
& (sco_supplied_item.reference_name == item.subcontracting_order_item)
)
).run(as_dict=True)
if supplied_items:
supplied_items_details[item.name] = {}
for supplied_item in supplied_items:
supplied_items_details[item.name][supplied_item.rm_item_code] = supplied_item.available_qty
else:
for item in self.get("supplied_items"):
item.available_qty_for_consumption = supplied_items_details.get(item.reference_name, {}).get(
item.rm_item_code, 0
)
def set_missing_values_in_supplied_items(self): def set_missing_values_in_supplied_items(self):
for item in self.get("supplied_items") or []: for item in self.get("supplied_items") or []:
item.amount = item.rate * item.consumed_qty item.amount = item.rate * item.consumed_qty
self.set_available_qty_for_consumption()
def set_missing_values_in_items(self): def set_missing_values_in_items(self):
rm_supp_cost = {} rm_supp_cost = {}
for item in self.get("supplied_items") or []: for item in self.get("supplied_items") or []:
@ -147,6 +180,17 @@ class SubcontractingReceipt(SubcontractingController):
_("Rejected Warehouse is mandatory against rejected Item {0}").format(item.item_code) _("Rejected Warehouse is mandatory against rejected Item {0}").format(item.item_code)
) )
def validate_available_qty_for_consumption(self):
for item in self.get("supplied_items"):
if (
item.available_qty_for_consumption and item.available_qty_for_consumption < item.consumed_qty
):
frappe.throw(
_(
"Row {0}: Consumed Qty must be less than or equal to Available Qty For Consumption in Consumed Items Table."
).format(item.idx)
)
def set_items_cost_center(self): def set_items_cost_center(self):
if self.company: if self.company:
cost_center = frappe.get_cached_value("Company", self.company, "cost_center") cost_center = frappe.get_cached_value("Company", self.company, "cost_center")

View File

@ -70,6 +70,55 @@ class TestSubcontractingReceipt(FrappeTestCase):
rm_supp_cost = sum(item.amount for item in scr.get("supplied_items")) rm_supp_cost = sum(item.amount for item in scr.get("supplied_items"))
self.assertEqual(scr.get("items")[0].rm_supp_cost, flt(rm_supp_cost)) self.assertEqual(scr.get("items")[0].rm_supp_cost, flt(rm_supp_cost))
def test_available_qty_for_consumption(self):
make_stock_entry(
item_code="_Test Item", qty=100, target="_Test Warehouse 1 - _TC", basic_rate=100
)
make_stock_entry(
item_code="_Test Item Home Desktop 100",
qty=100,
target="_Test Warehouse 1 - _TC",
basic_rate=100,
)
service_items = [
{
"warehouse": "_Test Warehouse - _TC",
"item_code": "Subcontracted Service Item 1",
"qty": 10,
"rate": 100,
"fg_item": "_Test FG Item",
"fg_item_qty": 10,
},
]
sco = get_subcontracting_order(service_items=service_items)
rm_items = [
{
"main_item_code": "_Test FG Item",
"item_code": "_Test Item",
"qty": 5.0,
"rate": 100.0,
"stock_uom": "_Test UOM",
"warehouse": "_Test Warehouse - _TC",
},
{
"main_item_code": "_Test FG Item",
"item_code": "_Test Item Home Desktop 100",
"qty": 10.0,
"rate": 100.0,
"stock_uom": "_Test UOM",
"warehouse": "_Test Warehouse - _TC",
},
]
itemwise_details = make_stock_in_entry(rm_items=rm_items)
make_stock_transfer_entry(
sco_no=sco.name,
rm_items=rm_items,
itemwise_details=copy.deepcopy(itemwise_details),
)
scr = make_subcontracting_receipt(sco.name)
scr.save()
self.assertRaises(frappe.ValidationError, scr.submit)
def test_subcontracting_gle_fg_item_rate_zero(self): def test_subcontracting_gle_fg_item_rate_zero(self):
from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import get_gl_entries from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import get_gl_entries

View File

@ -19,6 +19,7 @@
"col_break2", "col_break2",
"amount", "amount",
"secbreak_2", "secbreak_2",
"available_qty_for_consumption",
"required_qty", "required_qty",
"col_break3", "col_break3",
"consumed_qty", "consumed_qty",
@ -75,8 +76,7 @@
{ {
"fieldname": "required_qty", "fieldname": "required_qty",
"fieldtype": "Float", "fieldtype": "Float",
"in_list_view": 1, "label": "Required Qty",
"label": "Available Qty For Consumption",
"print_hide": 1, "print_hide": 1,
"read_only": 1 "read_only": 1
}, },
@ -85,7 +85,7 @@
"fieldname": "consumed_qty", "fieldname": "consumed_qty",
"fieldtype": "Float", "fieldtype": "Float",
"in_list_view": 1, "in_list_view": 1,
"label": "Qty to be Consumed", "label": "Consumed Qty",
"reqd": 1 "reqd": 1
}, },
{ {
@ -179,12 +179,21 @@
"options": "Subcontracting Order", "options": "Subcontracting Order",
"print_hide": 1, "print_hide": 1,
"read_only": 1 "read_only": 1
},
{
"default": "0",
"fieldname": "available_qty_for_consumption",
"fieldtype": "Float",
"in_list_view": 1,
"label": "Available Qty For Consumption",
"print_hide": 1,
"read_only": 1
} }
], ],
"idx": 1, "idx": 1,
"istable": 1, "istable": 1,
"links": [], "links": [],
"modified": "2022-04-18 10:45:16.538479", "modified": "2022-09-02 22:28:53.392381",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Subcontracting", "module": "Subcontracting",
"name": "Subcontracting Receipt Supplied Item", "name": "Subcontracting Receipt Supplied Item",
@ -193,6 +202,6 @@
"permissions": [], "permissions": [],
"sort_field": "modified", "sort_field": "modified",
"sort_order": "DESC", "sort_order": "DESC",
"track_changes": 1, "states": [],
"states": [] "track_changes": 1
} }