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 = {}
for no in rec_no[rec_type]:
try: try:
debit_orders[no] = int(form[F"debit-{no}-ord"]) orders[no] = int(form[F"{rec_type}-{no}-ord"])
except KeyError: except KeyError:
debit_orders[no] = 9999 orders[no] = 9999
except ValueError: except ValueError:
debit_orders[no] = 9999 orders[no] = 9999
debit_no.sort(key=lambda x: debit_orders[x]) rec_no[rec_type].sort(key=lambda x: orders[x])
credit_orders = {} # Constructs the sorted new form
for no in credit_no:
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])):
old_no = rec_no[rec_type][i]
no = i + 1 no = i + 1
new_form[F"debit-{no}-ord"] = no new_form[F"{rec_type}-{no}-ord"] = no
for attr in ["sn", "account", "summary", "amount"]: for attr in ["sn", "account", "summary", "amount"]:
if F"debit-{old_no}-{attr}" in form: if F"{rec_type}-{old_no}-{attr}" in form:
new_form[F"debit-{no}-{attr}"]\ new_form[F"{rec_type}-{no}-{attr}"]\
= form[F"debit-{old_no}-{attr}"] = form[F"{rec_type}-{old_no}-{attr}"]
for i in range(len(credit_no)):
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)]