Revised the redundant part in the fill_transaction_from_form() utility in the accounting application.

This commit is contained in:
依瑪貓 2020-07-29 09:22:56 +08:00
parent de7abdfc7e
commit 3873f131ef

View File

@ -306,46 +306,36 @@ def fill_transaction_from_form(transaction, form):
if "notes" in form: if "notes" in form:
transaction.notes = form["notes"] transaction.notes = form["notes"]
# The records # The records
max_debit_no = 0 max_no = {
max_credit_no = 0 "debit": 0,
"credit": 0,
}
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)$",
key)
if m is not None: if m is not None:
no = int(m.group(1)) rec_type = m.group(1)
if max_debit_no < no: no = int(m.group(2))
max_debit_no = no if max_no[rec_type] < no:
m = re.match( max_no[rec_type] = no
"^credit-([1-9][0-9]*)-(sn|ord|account|summary|amount)", key)
if m is not None:
no = int(m.group(1))
if max_credit_no < no:
max_credit_no = no
records = [] records = []
for i in range(max_debit_no): for rec_type in max_no.keys():
no = i + 1 for i in range(max_no[rec_type]):
record = Record(ord=no, is_credit=False, transaction=transaction) no = i + 1
if F"debit-{no}-sn" in form: record = Record(
record.pk = form[F"debit-{no}-sn"] ord=no,
if F"debit-{no}-account" in form: is_credit=(rec_type == "credit"),
record.account = Account(pk=form[F"debit-{no}-account"]) transaction=transaction)
if F"debit-{no}-summary" in form: if F"{rec_type}-{no}-sn" in form:
record.summary = form[F"debit-{no}-summary"] record.pk = form[F"{rec_type}-{no}-sn"]
if F"debit-{no}-amount" in form: if F"{rec_type}-{no}-account" in form:
record.amount = form[F"debit-{no}-amount"] record.account = Account(pk=form[F"{rec_type}-{no}-account"])
records.append(record) if F"{rec_type}-{no}-summary" in form:
for i in range(max_credit_no): record.summary = form[F"{rec_type}-{no}-summary"]
no = i + 1 if F"{rec_type}-{no}-amount" in form:
record = Record(ord=no, is_credit=False, transaction=transaction) record.amount = form[F"{rec_type}-{no}-amount"]
if F"credit-{no}-sn" in form: records.append(record)
record.pk = form[F"credit-{no}-sn"]
if F"credit-{no}-account" in form:
record.account = Account(pk=form[F"credit-{no}-account"])
if F"credit-{no}-summary" in form:
record.summary = form[F"credit-{no}-summary"]
if F"credit-{no}-amount" in form:
record.amount = form[F"credit-{no}-amount"]
records.append(record)
transaction.records = records transaction.records = records