Skip to content

Commit

Permalink
finance: Move bank account import to action button
Browse files Browse the repository at this point in the history
  • Loading branch information
FestplattenSchnitzel committed Sep 27, 2024
1 parent 1846e57 commit a1f5a45
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 34 deletions.
54 changes: 23 additions & 31 deletions web/blueprints/finance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,17 @@ def bank_accounts_list_json() -> ResponseReturnValue:
def actions(bank_account: BankAccount) -> list[BtnColResponse]:
return [
BtnColResponse(
href=url_for('.accounts_show', account_id=bank_account.account_id),
href=url_for(".accounts_show", account_id=bank_account.account_id),
title="",
btn_class="btn-primary",
btn_class="btn-primary btn-sm",
icon="fa-eye",
)
),
BtnColResponse(
href=url_for(".bank_accounts_import", bank_account_id=bank_account.id),
title="",
btn_class="btn-primary btn-sm",
icon="fa-file-import",
),
]

return TableResponse[BankAccountRow](
Expand All @@ -182,7 +188,6 @@ def actions(bank_account: BankAccount) -> list[BtnColResponse]:
name=bank_account.name,
bank=bank_account.bank,
iban=bank_account.iban,
bic=bank_account.bic,
balance=money_filter(bank_account.balance),
last_imported_at=(
str(datetime.date(i))
Expand Down Expand Up @@ -270,14 +275,11 @@ def flash_fints_errors() -> t.Iterator[None]:
nicht erreicht werden.', 'error')
raise PycroftException from e

@bp.route('/bank-accounts/import', methods=['GET', 'POST'])
@access.require('finance_change')
@nav.navigate("Bankkontobewegungen importieren", icon='fa-file-import')
def bank_accounts_import() -> ResponseReturnValue:

@bp.route("/bank-accounts/<int:bank_account_id>/import", methods=["GET", "POST"])
@access.require("finance_change")
def bank_accounts_import(bank_account_id: int) -> ResponseReturnValue:
form = BankAccountActivitiesImportForm()
form.account.choices = [
(acc.id, acc.name) for acc in get_all_bank_accounts(session) if not acc.account.legacy
]
imported = ImportedTransactions([], [], [])

def display_form_response(
Expand All @@ -290,45 +292,35 @@ def display_form_response(
doubtful_transactions=imported.doubtful,
)

bank_account = session.get(BankAccount, bank_account_id)

if not form.is_submitted():
del (form.start_date)
form.start_date.data = (
datetime.date(i)
if (i := bank_account.last_imported_at) is not None
else date(2018, 1, 1)
)
form.end_date.data = date.today() - timedelta(days=1)
form.account.data = config.membership_fee_bank_account_id

return display_form_response(imported)

if not form.validate():
return display_form_response(imported)

bank_account = session.get(BankAccount, form.account.data)

# set start_date, end_date
if form.start_date.data is None:
# NB: start date default depends on `bank_account`
form.start_date.data = (
datetime.date(i)
if (i := bank_account.last_imported_at) is not None
else date(2018, 1, 1)
)
if form.end_date.data is None:
form.end_date.data = date.today()
start_date = form.start_date.data
end_date = form.end_date.data

try:
with flash_fints_errors():
statement, errors = get_fints_transactions(
product_id=config.fints_product_id,
user_id=form.user.data,
secret_pin=form.secret_pin.data,
bank_account=bank_account,
start_date=start_date,
end_date=end_date,
start_date=form.start_date.data,
end_date=form.end_date.data,
)
except PycroftException:
return display_form_response(imported)

flash(f"Transaktionen vom {start_date} bis {end_date}.")
flash(f"Transaktionen vom {form.start_date.data} bis {form.end_date.data}.")
if errors:
flash(
f"{len(errors)} Statements enthielten fehlerhafte Daten und müssen "
Expand Down
1 change: 0 additions & 1 deletion web/blueprints/finance/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ class BankAccountActivityEditForm(BankAccountActivityReadForm):


class BankAccountActivitiesImportForm(Form):
account = SelectField("Bankkonto", coerce=int)
user = StringField("Loginname", validators=[DataRequired()])
secret_pin = PasswordField("PIN", validators=[DataRequired()])
start_date = DateField("Startdatum")
Expand Down
2 changes: 0 additions & 2 deletions web/blueprints/finance/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ class BankAccountTable(BootstrapTable):
name = Column("Name")
bank = Column("Bank")
iban = IbanColumn("IBAN")
bic = Column("SWIFT-BIC")
balance = Column("Saldo")
last_imported_at = Column("Zuletzt importiert")
actions = MultiBtnColumn("Aktionen")
Expand Down Expand Up @@ -219,7 +218,6 @@ class BankAccountRow(BaseModel):
name: str
bank: str
iban: str
bic: str
balance: str
last_imported_at: str # TODO perhaps date
actions: list[BtnColResponse]
Expand Down

0 comments on commit a1f5a45

Please sign in to comment.