Skip to content

Commit

Permalink
perf: huge number of serial no creation (backport #42522) (#42544)
Browse files Browse the repository at this point in the history
perf: huge number of serial no creation (#42522)

(cherry picked from commit 1c7f7c8)

Co-authored-by: rohitwaghchaure <rohitw1991@gmail.com>
  • Loading branch information
mergify[bot] and rohitwaghchaure authored Jul 30, 2024
1 parent 61280e6 commit 6840f6c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ def validate(self):
if self.type_of_transaction == "Maintenance":
return

self.validate_serial_nos_duplicate()
self.check_future_entries_exists()
if not self.flags.ignore_validate_serial_batch or frappe.flags.in_test:
self.validate_serial_nos_duplicate()
self.check_future_entries_exists()

self.set_is_outward()
self.calculate_total_qty()
self.set_warehouse()
Expand Down Expand Up @@ -340,6 +342,9 @@ def set_incoming_rate_for_inward_transaction(self, row=None, save=False):
rate = frappe.db.get_value(child_table, self.voucher_detail_no, valuation_field)

for d in self.entries:
if (d.incoming_rate == rate) and d.qty and d.stock_value_difference:
continue

d.incoming_rate = flt(rate, precision)
if d.qty:
d.stock_value_difference = flt(d.qty) * flt(d.incoming_rate)
Expand Down Expand Up @@ -841,6 +846,9 @@ def on_submit(self):
self.validate_serial_nos_inventory()

def set_purchase_document_no(self):
if self.flags.ignore_validate_serial_batch:
return

if not self.has_serial_no:
return

Expand Down
3 changes: 1 addition & 2 deletions erpnext/stock/doctype/stock_settings/stock_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,6 @@
},
{
"default": "1",
"depends_on": "use_serial_batch_fields",
"description": "If enabled, do not update serial / batch values in the stock transactions on creation of auto Serial \n / Batch Bundle. ",
"fieldname": "do_not_update_serial_batch_on_creation_of_auto_bundle",
"fieldtype": "Check",
Expand All @@ -460,7 +459,7 @@
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
"modified": "2024-07-15 17:18:23.872161",
"modified": "2024-07-29 14:55:19.093508",
"modified_by": "Administrator",
"module": "Stock",
"name": "Stock Settings",
Expand Down
17 changes: 16 additions & 1 deletion erpnext/stock/serial_batch_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def make_serial_batch_no_bundle(self):
"type_of_transaction": "Inward" if self.sle.actual_qty > 0 else "Outward",
"company": self.company,
"is_rejected": self.is_rejected_entry(),
"make_bundle_from_sle": 1,
}
).make_serial_and_batch_bundle()

Expand Down Expand Up @@ -160,12 +161,13 @@ def validate_item(self):

if msg:
error_msg = (
f"Serial and Batch Bundle not set for item {self.item_code} in warehouse {self.warehouse}."
f"Serial and Batch Bundle not set for item {self.item_code} in warehouse {self.warehouse}"
+ msg
)
frappe.throw(_(error_msg))

def set_serial_and_batch_bundle(self, sn_doc):
self.sle.auto_created_serial_and_batch_bundle = 1
self.sle.db_set({"serial_and_batch_bundle": sn_doc.name, "auto_created_serial_and_batch_bundle": 1})

if sn_doc.is_rejected:
Expand Down Expand Up @@ -324,6 +326,9 @@ def submit_serial_and_batch_bundle(self):
def set_warehouse_and_status_in_serial_nos(self):
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos as get_parsed_serial_nos

if self.sle.auto_created_serial_and_batch_bundle and self.sle.actual_qty > 0:
return

serial_nos = get_serial_nos(self.sle.serial_and_batch_bundle)
if not self.sle.serial_and_batch_bundle and self.sle.serial_no:
serial_nos = get_parsed_serial_nos(self.sle.serial_no)
Expand Down Expand Up @@ -928,6 +933,10 @@ def make_serial_and_batch_bundle(self):
if doc.voucher_no and frappe.get_cached_value(doc.voucher_type, doc.voucher_no, "docstatus") == 2:
doc.voucher_no = ""

doc.flags.ignore_validate_serial_batch = False
if self.get("make_bundle_from_sle") and self.type_of_transaction == "Inward":
doc.flags.ignore_validate_serial_batch = True

doc.save()
self.validate_qty(doc)

Expand Down Expand Up @@ -1120,6 +1129,10 @@ def get_auto_created_serial_nos(self):
msg = f"Please set Serial No Series in the item {self.item_code} or create Serial and Batch Bundle manually."
frappe.throw(_(msg))

voucher_no = ""
if self.get("voucher_no"):
voucher_no = self.get("voucher_no")

for _i in range(abs(cint(self.actual_qty))):
serial_no = make_autoname(self.serial_no_series, "Serial No")
sr_nos.append(serial_no)
Expand All @@ -1137,6 +1150,7 @@ def get_auto_created_serial_nos(self):
self.item_name,
self.description,
"Active",
voucher_no,
self.batch_no,
)
)
Expand All @@ -1155,6 +1169,7 @@ def get_auto_created_serial_nos(self):
"item_name",
"description",
"status",
"purchase_document_no",
"batch_no",
]

Expand Down

0 comments on commit 6840f6c

Please sign in to comment.