Skip to content

Commit

Permalink
refactor: reusable clean_serial_nos
Browse files Browse the repository at this point in the history
  • Loading branch information
ankush committed Jan 29, 2022
1 parent 14e3e16 commit b20df37
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
13 changes: 4 additions & 9 deletions erpnext/controllers/stock_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,12 @@ def validate_serialized_batch(self):
.format(d.idx, get_link_to_form("Batch", d.get("batch_no"))))

def clean_serial_nos(self):
from erpnext.stock.doctype.serial_no.serial_no import clean_serial_no_string

for row in self.get("items"):
if hasattr(row, "serial_no") and row.serial_no:
# replace commas by linefeed
row.serial_no = row.serial_no.replace(",", "\n")

# strip preceeding and succeeding spaces for each SN
# (SN could have valid spaces in between e.g. SN - 123 - 2021)
serial_no_list = row.serial_no.split("\n")
serial_no_list = [sn.strip() for sn in serial_no_list]

row.serial_no = "\n".join(serial_no_list)
# remove extra whitespace and store one serial no on each line
row.serial_no = clean_serial_no_string(row.serial_no)

def get_gl_entries(self, warehouse_account=None, default_expense_account=None,
default_cost_center=None):
Expand Down
7 changes: 7 additions & 0 deletions erpnext/stock/doctype/serial_no/serial_no.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,13 @@ def get_serial_nos(serial_no):
return [s.strip() for s in cstr(serial_no).strip().upper().replace(',', '\n').split('\n')
if s.strip()]

def clean_serial_no_string(serial_no: str) -> str:
if not serial_no:
return ""

serial_no_list = get_serial_nos(serial_no)
return "\n".join(serial_no_list)

def update_args_for_serial_no(serial_no_doc, serial_no, args, is_new=False):
for field in ["item_code", "work_order", "company", "batch_no", "supplier", "location"]:
if args.get(field):
Expand Down

0 comments on commit b20df37

Please sign in to comment.