Refined the sort_form_transaction_records() utility in the accounting application.

This commit is contained in:
依瑪貓 2020-07-29 09:37:15 +08:00
parent 0b0624b709
commit 7ec25d89a8

View File

@ -363,58 +363,42 @@ def sort_form_transaction_records(form):
form (dict): The POSTed form. form (dict): The POSTed form.
""" """
# Collects the available record numbers # Collects the available record numbers
debit_no = [] rec_no = {
credit_no = [] "debit": [],
"credit": [],
}
for key in form.keys(): for key in form.keys():
m = re.match( m = re.match(
"^debit-([1-9][0-9]*)-(sn|ord|account|summary|amount)", key) "^(debit|credit)-([1-9][0-9]*)-(sn|ord|account|summary|amount)",
if m is not None: key)
no = int(m.group(1)) if m is None:
if no not in debit_no: continue
debit_no.append(no) rec_type = m.group(1)
m = re.match( no = int(m.group(2))
"^credit-([1-9][0-9]*)-(sn|ord|account|summary|amount)", key) if no not in rec_no[rec_type]:
if m is not None: rec_no[rec_type].append(no)
no = int(m.group(1))
if no not in credit_no:
credit_no.append(no)
# Sorts these record numbers by their specified orders # Sorts these record numbers by their specified orders
debit_orders = {} for rec_type in rec_no.keys():
for no in debit_no: orders = {}
try: for no in rec_no[rec_type]:
debit_orders[no] = int(form[F"debit-{no}-ord"]) try:
except KeyError: orders[no] = int(form[F"{rec_type}-{no}-ord"])
debit_orders[no] = 9999 except KeyError:
except ValueError: orders[no] = 9999
debit_orders[no] = 9999 except ValueError:
debit_no.sort(key=lambda x: debit_orders[x]) orders[no] = 9999
credit_orders = {} rec_no[rec_type].sort(key=lambda x: orders[x])
for no in credit_no: # Constructs the sorted new form
try:
credit_orders[no] = int(form[F"credit-{no}-ord"])
except KeyError:
credit_orders[no] = 9999
except ValueError:
credit_orders[no] = 9999
credit_no.sort(key=lambda x: credit_orders[x])
# Constructed the sorted new form
new_form = {} new_form = {}
for i in range(len(debit_no)): for rec_type in rec_no.keys():
old_no = debit_no[i] for i in range(len(rec_no[rec_type])):
no = i + 1 old_no = rec_no[rec_type][i]
new_form[F"debit-{no}-ord"] = no no = i + 1
for attr in ["sn", "account", "summary", "amount"]: new_form[F"{rec_type}-{no}-ord"] = no
if F"debit-{old_no}-{attr}" in form: for attr in ["sn", "account", "summary", "amount"]:
new_form[F"debit-{no}-{attr}"]\ if F"{rec_type}-{old_no}-{attr}" in form:
= form[F"debit-{old_no}-{attr}"] new_form[F"{rec_type}-{no}-{attr}"]\
for i in range(len(credit_no)): = form[F"{rec_type}-{old_no}-{attr}"]
old_no = credit_no[i]
no = i + 1
new_form[F"credit-{no}-ord"] = no
for attr in ["sn", "account", "summary", "amount"]:
if F"credit-{old_no}-{attr}" in form:
new_form[F"credit-{no}-{attr}"]\
= form[F"credit-{old_no}-{attr}"]
# Purges the old form and fills it with the new form # Purges the old form and fills it with the new form
old_keys = [x for x in form.keys() if re.match( old_keys = [x for x in form.keys() if re.match(
"^(debit|credit)-([1-9][0-9]*)-(sn|ord|account|summary|amount)", x)] "^(debit|credit)-([1-9][0-9]*)-(sn|ord|account|summary|amount)", x)]