diff --git a/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.js b/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.js index 3824853255..1a20f0ccf5 100644 --- a/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.js +++ b/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.js @@ -3,7 +3,7 @@ frappe.provide('erpnext.buying'); -erpnext.landed_cost_taxes_and_charges.setup_triggers("Subcontracting Receipt"); +erpnext.landed_cost_taxes_and_charges.setup_triggers('Subcontracting Receipt'); frappe.ui.form.on('Subcontracting Receipt', { setup: (frm) => { @@ -77,13 +77,13 @@ frappe.ui.form.on('Subcontracting Receipt', { } }); - frm.set_query("serial_and_batch_bundle", "supplied_items", (doc, cdt, cdn) => { + frm.set_query('serial_and_batch_bundle', 'supplied_items', (doc, cdt, cdn) => { let row = locals[cdt][cdn]; return { filters: { 'item_code': row.rm_item_code, 'voucher_type': doc.doctype, - 'voucher_no': ["in", [doc.name, ""]], + 'voucher_no': ['in', [doc.name, '']], 'is_cancelled': 0, } } @@ -180,6 +180,23 @@ frappe.ui.form.on('Subcontracting Receipt', { rejected_warehouse: (frm) => { set_warehouse_in_children(frm.doc.items, 'rejected_warehouse', frm.doc.rejected_warehouse); }, + + get_scrap_items: (frm) => { + frappe.call({ + doc: frm.doc, + method: 'get_scrap_items', + args: { + recalculate_rate: true + }, + freeze: true, + freeze_message: __('Getting Scrap Items'), + callback: (r) => { + if (!r.exc) { + frm.refresh(); + } + } + }); + }, }); frappe.ui.form.on('Landed Cost Taxes and Charges', { @@ -205,6 +222,12 @@ frappe.ui.form.on('Subcontracting Receipt Item', { set_missing_values(frm); }, + recalculate_rate(frm) { + if (frm.doc.recalculate_rate) { + set_missing_values(frm); + } + }, + items_remove: function(frm) { set_missing_values(frm); } diff --git a/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.py b/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.py index 5f64ab1323..09db66f97a 100644 --- a/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.py +++ b/erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.py @@ -148,7 +148,7 @@ class SubcontractingReceipt(SubcontractingController): self.supplied_items = [] @frappe.whitelist() - def get_scrap_items(self): + def get_scrap_items(self, recalculate_rate=False): if ( frappe.db.get_single_value("Buying Settings", "backflush_raw_materials_of_subcontract_based_on") == "BOM" @@ -163,42 +163,43 @@ class SubcontractingReceipt(SubcontractingController): self.append( "items", { + "is_scrap_item": 1, + "reference_name": item.name, "item_code": scrap_item.item_code, "item_name": scrap_item.item_name, "qty": qty, "stock_uom": scrap_item.stock_uom, + "recalculate_rate": 1, "rate": scrap_item.rate, + "rm_cost_per_qty": 0, + "service_cost_per_qty": 0, + "additional_cost_per_qty": 0, + "scrap_cost_per_qty": scrap_item.rate, "amount": qty * scrap_item.rate, - "is_scrap_item": 1, "warehouse": self.set_warehouse, "rejected_warehouse": self.rejected_warehouse, - "service_cost_per_qty": 0, - "reference_name": item.name, - "recalculate_rate": 0, }, ) - else: + + if recalculate_rate: self.calculate_additional_costs() self.calculate_items_qty_and_amount() - def remove_scrap_items(self): + def remove_scrap_items(self, recalculate_rate=False): for item in list(self.items): if item.is_scrap_item: self.remove(item) else: item.scrap_cost_per_qty = 0 + if recalculate_rate: + self.calculate_items_qty_and_amount() + @frappe.whitelist() def set_missing_values(self): - self.calculate_additional_costs() - self.calculate_supplied_items_qty_and_amount() - self.calculate_items_qty_and_amount() - - def calculate_supplied_items_qty_and_amount(self): - for item in self.get("supplied_items") or []: - item.amount = item.rate * item.consumed_qty - self.set_available_qty_for_consumption() + self.calculate_additional_costs() + self.calculate_items_qty_and_amount() def set_available_qty_for_consumption(self): supplied_items_details = {} @@ -233,6 +234,8 @@ class SubcontractingReceipt(SubcontractingController): def calculate_items_qty_and_amount(self): rm_cost_map = {} for item in self.get("supplied_items") or []: + item.amount = flt(item.consumed_qty) * flt(item.rate) + if item.reference_name in rm_cost_map: rm_cost_map[item.reference_name] += item.amount else: @@ -241,15 +244,18 @@ class SubcontractingReceipt(SubcontractingController): scrap_cost_map = {} for item in self.get("items") or []: if item.is_scrap_item: + if item.recalculate_rate: + item.rate = flt(item.scrap_cost_per_qty) + flt(item.additional_cost_per_qty) + + item.amount = flt(item.qty) * flt(item.rate) + if item.reference_name in scrap_cost_map: scrap_cost_map[item.reference_name] += item.amount else: scrap_cost_map[item.reference_name] = item.amount - else: - item.scrap_cost_per_qty = 0 total_qty = total_amount = 0 - for item in self.items: + for item in self.get("items") or []: if not item.is_scrap_item: if item.qty: if item.name in rm_cost_map: @@ -260,6 +266,8 @@ class SubcontractingReceipt(SubcontractingController): if item.name in scrap_cost_map: item.scrap_cost_per_qty = scrap_cost_map[item.name] / item.qty scrap_cost_map.pop(item.name) + else: + item.scrap_cost_per_qty = 0 if item.recalculate_rate: item.rate = ( diff --git a/erpnext/subcontracting/doctype/subcontracting_receipt_item/subcontracting_receipt_item.json b/erpnext/subcontracting/doctype/subcontracting_receipt_item/subcontracting_receipt_item.json index 5442dfddd7..b333627b4a 100644 --- a/erpnext/subcontracting/doctype/subcontracting_receipt_item/subcontracting_receipt_item.json +++ b/erpnext/subcontracting/doctype/subcontracting_receipt_item/subcontracting_receipt_item.json @@ -476,11 +476,9 @@ }, { "default": "1", - "depends_on": "eval: !doc.is_scrap_item", "fieldname": "recalculate_rate", "fieldtype": "Check", - "label": "Recalculate Rate", - "read_only_depends_on": "eval: doc.is_scrap_item" + "label": "Recalculate Rate" }, { "fieldname": "serial_and_batch_bundle", @@ -510,7 +508,6 @@ }, { "default": "0", - "depends_on": "eval: !doc.is_scrap_item", "fieldname": "scrap_cost_per_qty", "fieldtype": "Float", "label": "Scrap Cost Per Qty", @@ -530,7 +527,7 @@ "idx": 1, "istable": 1, "links": [], - "modified": "2023-08-25 09:33:47.232140", + "modified": "2023-08-25 15:42:36.923833", "modified_by": "Administrator", "module": "Subcontracting", "name": "Subcontracting Receipt Item",