mirror of
https://github.com/frappe/erpnext.git
synced 2026-08-24 20:46:38 +00:00
fix: work order finish dialog with process loss qty from job card (#58256)
* fix(manufacturing): cap job card completed qty by previous operation and show process loss on finish dialog * fix: revert job card finish dialog changes
This commit is contained in:
@@ -159,6 +159,8 @@ class JobCard(Document):
|
||||
)
|
||||
self.set_onload("work_order_closed", self.is_work_order_closed())
|
||||
self.set_onload("has_stock_entry", self.has_stock_entry())
|
||||
if self.docstatus == 0:
|
||||
self.set_onload("max_completable_qty", self.get_max_completable_qty())
|
||||
|
||||
def on_discard(self):
|
||||
self.db_set("status", "Cancelled")
|
||||
@@ -1510,6 +1512,20 @@ class JobCard(Document):
|
||||
|
||||
return current_operation_qty + flt(self.total_completed_qty)
|
||||
|
||||
def get_max_completable_qty(self):
|
||||
if self.is_corrective_job_card or not (self.work_order and self.sequence_id):
|
||||
return None
|
||||
|
||||
previous_operations = self.get_previous_operations()
|
||||
if not previous_operations:
|
||||
return None
|
||||
|
||||
qty_field = "manufactured_qty" if self.track_semi_finished_goods else "completed_qty"
|
||||
min_completed_qty = min(flt(row.get(qty_field)) for row in previous_operations)
|
||||
|
||||
precision = self.precision("total_completed_qty")
|
||||
return flt(min_completed_qty - self.get_current_operation_completed_qty(), precision)
|
||||
|
||||
def validate_previous_operation(self, row, current_operation_qty):
|
||||
if not row.completed_qty or (row.status != "Completed" and row.completed_qty < current_operation_qty):
|
||||
frappe.throw(
|
||||
|
||||
@@ -1186,6 +1186,110 @@ class TestJobCard(ERPNextTestSuite):
|
||||
self.assertEqual(wo_doc.process_loss_qty, 2)
|
||||
self.assertEqual(wo_doc.status, "Completed")
|
||||
|
||||
def make_two_operation_work_order(self, qty=10):
|
||||
from erpnext.manufacturing.doctype.routing.test_routing import (
|
||||
create_routing,
|
||||
setup_bom,
|
||||
setup_operations,
|
||||
)
|
||||
from erpnext.stock.doctype.item.test_item import make_item
|
||||
from erpnext.stock.doctype.warehouse.test_warehouse import create_warehouse
|
||||
|
||||
operations = [
|
||||
{"operation": "Test Operation A1", "workstation": "Test Workstation A", "time_in_mins": 30},
|
||||
{"operation": "Test Operation B1", "workstation": "Test Workstation A", "time_in_mins": 20},
|
||||
]
|
||||
|
||||
warehouse = create_warehouse("Test Warehouse 123 for Job Card")
|
||||
setup_operations(operations)
|
||||
|
||||
item_code = "Test Job Card Process Qty Item"
|
||||
for item in [item_code, item_code + "RM 1", item_code + "RM 2"]:
|
||||
if not frappe.db.exists("Item", item):
|
||||
make_item(item, {"item_name": item, "stock_uom": "Nos", "is_stock_item": 1})
|
||||
|
||||
routing_doc = create_routing(routing_name="Testing Route", operations=operations)
|
||||
bom_doc = setup_bom(
|
||||
item_code=item_code,
|
||||
routing=routing_doc.name,
|
||||
raw_materials=[item_code + "RM 1", item_code + "RM 2"],
|
||||
source_warehouse=warehouse,
|
||||
)
|
||||
|
||||
for row in bom_doc.items:
|
||||
make_stock_entry(item_code=row.item_code, target=row.source_warehouse, qty=qty, basic_rate=100)
|
||||
|
||||
return make_wo_order_test_record(
|
||||
production_item=item_code,
|
||||
bom_no=bom_doc.name,
|
||||
qty=qty,
|
||||
skip_transfer=1,
|
||||
wip_warehouse=warehouse,
|
||||
source_warehouse=warehouse,
|
||||
)
|
||||
|
||||
def test_completion_qty_capped_by_previous_operation(self):
|
||||
wo_doc = self.make_two_operation_work_order()
|
||||
job_cards = frappe.get_all(
|
||||
"Job Card",
|
||||
filters={"work_order": wo_doc.name},
|
||||
fields=["name", "sequence_id"],
|
||||
order_by="sequence_id",
|
||||
)
|
||||
|
||||
jc1 = frappe.get_doc("Job Card", job_cards[0].name)
|
||||
self.assertIsNone(jc1.get_max_completable_qty())
|
||||
|
||||
jc1.append(
|
||||
"time_logs",
|
||||
{"from_time": now(), "to_time": add_to_date(now(), minutes=30), "completed_qty": 8},
|
||||
)
|
||||
jc1.save()
|
||||
jc1.submit()
|
||||
self.assertEqual(jc1.process_loss_qty, 2)
|
||||
|
||||
jc2 = frappe.get_doc("Job Card", job_cards[1].name)
|
||||
self.assertEqual(jc2.get_max_completable_qty(), 8)
|
||||
|
||||
jc2.append("time_logs", {"from_time": add_to_date(now(), minutes=40)})
|
||||
jc2.save()
|
||||
|
||||
self.assertRaises(
|
||||
frappe.ValidationError,
|
||||
jc2.complete_job_card,
|
||||
qty=10,
|
||||
for_quantity=10,
|
||||
pending_qty=0,
|
||||
process_loss_qty=0,
|
||||
end_time=add_to_date(now(), minutes=70),
|
||||
)
|
||||
|
||||
self.complete_second_operation_and_finish(wo_doc, jc2.name)
|
||||
|
||||
def complete_second_operation_and_finish(self, wo_doc, job_card):
|
||||
from erpnext.manufacturing.doctype.work_order.mapper import (
|
||||
make_stock_entry as make_stock_entry_for_wo,
|
||||
)
|
||||
|
||||
jc2 = frappe.get_doc("Job Card", job_card)
|
||||
jc2.time_logs[0].completed_qty = 7
|
||||
jc2.time_logs[0].to_time = add_to_date(now(), minutes=70)
|
||||
jc2.save()
|
||||
self.assertEqual(jc2.process_loss_qty, 3)
|
||||
jc2.submit()
|
||||
|
||||
se = frappe.get_doc(make_stock_entry_for_wo(wo_doc.name, "Manufacture", 10))
|
||||
se.submit()
|
||||
|
||||
self.assertEqual(se.process_loss_qty, 3)
|
||||
fg_qty = sum(d.qty for d in se.items if d.is_finished_item)
|
||||
self.assertEqual(flt(fg_qty), 7)
|
||||
|
||||
wo_doc.reload()
|
||||
self.assertEqual(wo_doc.produced_qty, 7)
|
||||
self.assertEqual(wo_doc.process_loss_qty, 3)
|
||||
self.assertEqual(wo_doc.status, "Completed")
|
||||
|
||||
def get_first_job_card(self, work_order):
|
||||
return frappe.get_doc(
|
||||
"Job Card",
|
||||
|
||||
@@ -1053,6 +1053,15 @@ erpnext.work_order = {
|
||||
return flt(max, precision("qty"));
|
||||
},
|
||||
|
||||
get_pending_operation_process_loss: (frm) => {
|
||||
if (!(frm.doc.operations || []).length) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const total_loss = Math.max(...frm.doc.operations.map((row) => flt(row.process_loss_qty)));
|
||||
return flt(Math.max(total_loss - flt(frm.doc.process_loss_qty), 0), precision("qty"));
|
||||
},
|
||||
|
||||
get_max_requestable_qty: (frm) => {
|
||||
const required = {};
|
||||
const covered = {};
|
||||
@@ -1129,6 +1138,11 @@ erpnext.work_order = {
|
||||
|
||||
show_prompt_for_qty_input: function (frm, purpose, { qty, additional_transfer_entry, target } = {}) {
|
||||
let max = qty == null ? this.get_max_transferable_qty(frm, purpose) : qty;
|
||||
if (purpose === "Manufacture") {
|
||||
max = flt(Math.max(max - flt(frm.doc.process_loss_qty), 0), precision("qty"));
|
||||
}
|
||||
const pending_process_loss =
|
||||
purpose === "Manufacture" ? this.get_pending_operation_process_loss(frm) : 0;
|
||||
|
||||
let fields = [
|
||||
{
|
||||
@@ -1137,23 +1151,36 @@ erpnext.work_order = {
|
||||
fieldname: "qty",
|
||||
description: __("Max: {0}", [max]),
|
||||
default: max,
|
||||
onchange: function () {
|
||||
if (pending_process_loss && frm.qty_prompt) {
|
||||
frm.qty_prompt.set_value(
|
||||
"finished_good_qty",
|
||||
flt(Math.max(flt(this.value) - pending_process_loss, 0), precision("qty"))
|
||||
);
|
||||
}
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
if (!additional_transfer_entry && !target) {
|
||||
fields.push({
|
||||
fieldtype: "Check",
|
||||
label: __("Consider Process Loss"),
|
||||
fieldname: "consider_process_loss",
|
||||
default: 0,
|
||||
onchange: function () {
|
||||
if (this.value) {
|
||||
frm.qty_prompt.set_value("qty", max - frm.doc.process_loss_qty);
|
||||
} else {
|
||||
frm.qty_prompt.set_value("qty", max);
|
||||
}
|
||||
if (pending_process_loss) {
|
||||
fields.push(
|
||||
{
|
||||
fieldtype: "Float",
|
||||
label: __("Process Loss Qty"),
|
||||
fieldname: "process_loss_qty",
|
||||
default: pending_process_loss,
|
||||
read_only: 1,
|
||||
description: __("Process loss booked against the operations of this work order."),
|
||||
},
|
||||
});
|
||||
{
|
||||
fieldtype: "Float",
|
||||
label: __("Finished Good Qty"),
|
||||
fieldname: "finished_good_qty",
|
||||
default: flt(Math.max(max - pending_process_loss, 0), precision("qty")),
|
||||
read_only: 1,
|
||||
description: __("Actual quantity of the finished good that will be manufactured."),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
Reference in New Issue
Block a user