Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow to change the batch in the subcontracting receipt #43584

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 45 additions & 19 deletions erpnext/controllers/subcontracting_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,30 +576,56 @@ def __add_supplied_item(self, item_row, bom_item, qty):
self.__set_batch_nos(bom_item, item_row, rm_obj, qty)

if self.doctype == "Subcontracting Receipt" and not use_serial_batch_fields:
args = frappe._dict(
{
"item_code": rm_obj.rm_item_code,
"warehouse": self.supplier_warehouse,
"posting_date": self.posting_date,
"posting_time": self.posting_time,
"qty": -1 * flt(rm_obj.consumed_qty),
"actual_qty": -1 * flt(rm_obj.consumed_qty),
"voucher_type": self.doctype,
"voucher_no": self.name,
"voucher_detail_no": item_row.name,
"company": self.company,
"allow_zero_valuation": 1,
}
)

rm_obj.serial_and_batch_bundle = self.__set_serial_and_batch_bundle(
item_row, rm_obj, rm_obj.consumed_qty
)

if rm_obj.serial_and_batch_bundle:
args["serial_and_batch_bundle"] = rm_obj.serial_and_batch_bundle
self.set_rate_for_supplied_items(rm_obj, item_row)

def update_rate_for_supplied_items(self):
if self.doctype != "Subcontracting Receipt":
return

for row in self.supplied_items:
item_row = None
if row.reference_name:
item_row = self.get_item_row(row.reference_name)

if not item_row:
continue

self.set_rate_for_supplied_items(row, item_row)

def get_item_row(self, reference_name):
for item in self.items:
if item.name == reference_name:
return item

def set_rate_for_supplied_items(self, rm_obj, item_row):
args = frappe._dict(
{
"item_code": rm_obj.rm_item_code,
"warehouse": self.supplier_warehouse,
"posting_date": self.posting_date,
"posting_time": self.posting_time,
"qty": -1 * flt(rm_obj.consumed_qty),
"actual_qty": -1 * flt(rm_obj.consumed_qty),
"voucher_type": self.doctype,
"voucher_no": self.name,
"voucher_detail_no": item_row.name,
"company": self.company,
"allow_zero_valuation": 1,
}
)

if rm_obj.serial_and_batch_bundle:
args["serial_and_batch_bundle"] = rm_obj.serial_and_batch_bundle

if rm_obj.use_serial_batch_fields:
args["batch_no"] = rm_obj.batch_no
args["serial_no"] = rm_obj.serial_no

rm_obj.rate = get_incoming_rate(args)
rm_obj.rate = get_incoming_rate(args)

def __set_batch_nos(self, bom_item, item_row, rm_obj, qty):
key = (rm_obj.rm_item_code, item_row.item_code, item_row.get(self.subcontract_data.order_field))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,15 @@ frappe.ui.form.on("Subcontracting Receipt", {
});

frm.set_query("batch_no", "supplied_items", (doc, cdt, cdn) => {
var row = locals[cdt][cdn];
let row = locals[cdt][cdn];
let filters = {
item_code: row.rm_item_code,
warehouse: doc.supplier_warehouse,
};

return {
filters: {
item: row.rm_item_code,
},
query: "erpnext.controllers.queries.get_batch_no",
filters: filters,
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,14 @@ def reset_supplied_items(self):
frappe.db.get_single_value("Buying Settings", "backflush_raw_materials_of_subcontract_based_on")
== "BOM"
and self.supplied_items
and not any(item.serial_and_batch_bundle for item in self.supplied_items)
):
self.supplied_items = []
if not any(
item.serial_and_batch_bundle or item.batch_no or item.serial_no
for item in self.supplied_items
):
self.supplied_items = []
else:
self.update_rate_for_supplied_items()

@frappe.whitelist()
def get_scrap_items(self, recalculate_rate=False):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,66 @@ def test_subcontracting_receipt_for_batch_materials_without_use_serial_batch_fie

frappe.db.set_single_value("Stock Settings", "use_serial_batch_fields", 1)

def test_change_batch_for_raw_materials(self):
set_backflush_based_on("BOM")

fg_item = make_item(properties={"is_stock_item": 1, "is_sub_contracted_item": 1}).name
rm_item1 = make_item(
properties={
"is_stock_item": 1,
"has_batch_no": 1,
"create_new_batch": 1,
"batch_number_series": "BNGS-.####",
}
).name

bom = make_bom(item=fg_item, raw_materials=[rm_item1])
second_batch_no = None
for row in bom.items:
se = make_stock_entry(
item_code=row.item_code,
qty=1,
target="_Test Warehouse 1 - _TC",
rate=300,
)

se.reload()
se1 = make_stock_entry(
item_code=row.item_code,
qty=1,
target="_Test Warehouse 1 - _TC",
rate=300,
)

se1.reload()

second_batch_no = get_batch_from_bundle(se1.items[0].serial_and_batch_bundle)

service_items = [
{
"warehouse": "_Test Warehouse - _TC",
"item_code": "Subcontracted Service Item 1",
"qty": 1,
"rate": 100,
"fg_item": fg_item,
"fg_item_qty": 1,
},
]
sco = get_subcontracting_order(service_items=service_items)
scr = make_subcontracting_receipt(sco.name)
scr.save()
scr.reload()

scr.supplied_items[0].batch_no = second_batch_no
scr.supplied_items[0].use_serial_batch_fields = 1
scr.submit()
scr.reload()

batch_no = get_batch_from_bundle(scr.supplied_items[0].serial_and_batch_bundle)
self.assertEqual(batch_no, second_batch_no)
self.assertEqual(scr.items[0].rm_cost_per_qty, 300)
self.assertEqual(scr.items[0].service_cost_per_qty, 100)


def make_return_subcontracting_receipt(**args):
args = frappe._dict(args)
Expand Down
Loading