Skip to content

Commit

Permalink
Lookup missing BIC when returning bank account activities
Browse files Browse the repository at this point in the history
  • Loading branch information
FestplattenSchnitzel committed Jul 8, 2024
1 parent 865ebcf commit 4fe52b1
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 4 deletions.
3 changes: 2 additions & 1 deletion pycroft/lib/finance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@
get_pid_csv,
)
from .retransfer import (
get_activities_to_return,
attribute_activities_as_returned,
generate_activities_return_sepaxml,
get_activities_to_return,
)
from .transaction_crud import (
simple_transaction,
Expand Down
30 changes: 28 additions & 2 deletions pycroft/lib/finance/retransfer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from collections.abc import Sequence
from datetime import datetime, timedelta

from schwifty import IBAN
from sepaxml import SepaTransfer
from sqlalchemy import select
from sqlalchemy.orm import joinedload, Session
from sqlalchemy.orm import Session, joinedload

from pycroft import config
from pycroft.helpers.utc import ensure_tz
Expand Down Expand Up @@ -33,10 +34,11 @@ def generate_activities_return_sepaxml(activities: list[BankAccountActivity]) ->
sepa = SepaTransfer(transfer_config, clean=False)

for activity in activities:
bic = activity.other_routing_number or IBAN(activity.other_account_number).bic.compact
payment = {
"name": activity.other_name,
"IBAN": activity.other_account_number,
"BIC": activity.other_routing_number,
"BIC": bic,
"amount": int(activity.amount * 100),
"execution_date": datetime.now().date(),
"description": f"Rücküberweisung nicht zuordenbarer Überweisung vom {activity.posted_on} mit Referenz {activity.reference}"[
Expand All @@ -46,3 +48,27 @@ def generate_activities_return_sepaxml(activities: list[BankAccountActivity]) ->
sepa.add_payment(payment)

return sepa.export()


def attribute_activities_as_returned(
session: Session, activities: list[BankAccountActivity]
) -> None:
for activity in activities:
debit_account = config.non_attributable_transactions_account
credit_account = activity.bank_account.account

transaction = finance.simple_transaction(

Check failure on line 60 in pycroft/lib/finance/retransfer.py

View workflow job for this annotation

GitHub Actions / python-lint

Error

Name "finance" is not defined [name-defined]

Check failure on line 60 in pycroft/lib/finance/retransfer.py

View workflow job for this annotation

GitHub Actions / python-lint

Ruff (F821)

pycroft/lib/finance/retransfer.py:60:23: F821 Undefined name `finance`
description=activity.reference,
debit_account=debit_account,
credit_account=credit_account,
amount=activity.amount,
author=current_user,

Check failure on line 65 in pycroft/lib/finance/retransfer.py

View workflow job for this annotation

GitHub Actions / python-lint

Error

Name "current_user" is not defined [name-defined]

Check failure on line 65 in pycroft/lib/finance/retransfer.py

View workflow job for this annotation

GitHub Actions / python-lint

Ruff (F821)

pycroft/lib/finance/retransfer.py:65:20: F821 Undefined name `current_user`
valid_on=activity.valid_on,
confirmed=False,
)
activity.split = next(
split for split in transaction.splits if split.account_id == credit_account.id
)
session.add(activity)

session.commit()
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""add account for non-attributable transfers to config
Revision ID: 2d7e4df39a3b
Revises: bc0e0dd480d4
Create Date: 2024-06-06 20:21:16.972195
"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

Check failure on line 11 in pycroft/model/alembic/versions/2d7e4df39a3b_add_account_for_non_attributable_.py

View workflow job for this annotation

GitHub Actions / python-lint

Ruff (F401)

pycroft/model/alembic/versions/2d7e4df39a3b_add_account_for_non_attributable_.py:11:33: F401 `sqlalchemy.dialects.postgresql` imported but unused

# revision identifiers, used by Alembic.
revision = "2d7e4df39a3b"
down_revision = "bc0e0dd480d4"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"config",
sa.Column(
"non_attributable_transactions_account_id",
sa.Integer(),
nullable=False,
server_default="10",
),
)
op.create_foreign_key(
None, "config", "account", ["non_attributable_transactions_account_id"], ["id"]
)


def downgrade():
op.drop_constraint(None, "config", type_="foreignkey")
op.drop_column("config", "non_attributable_transactions_account_id")
op.create_index(
"bank_account_activity_imported_at", "bank_account_activity", ["imported_at"], unique=False
)
5 changes: 5 additions & 0 deletions pycroft/model/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class Config(IntegerIdModel):
foreign_keys=[membership_fee_bank_account_id]
)

non_attributable_transactions_account_id: Mapped[int] = col(ForeignKey(Account.id))
non_attributable_transactions_account: Mapped[Account] = relationship(
foreign_keys=[non_attributable_transactions_account_id]
)

fints_product_id: Mapped[str | None]

__table_args__ = (CheckConstraint("id = 1"),)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ dependencies = [
"pydantic ~= 2.4.0",
"python-dotenv ~= 0.21.0",
"reportlab ~= 3.6.13", # usersheet generation
"schwifty ~= 2024.06.1",
"sentry-sdk[Flask] ~= 1.29.2",
"simplejson ~= 3.11.1", # decimal serialization
"SQLAlchemy >= 2.0.1",
Expand Down
3 changes: 3 additions & 0 deletions web/blueprints/finance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
match_activities,
get_activities_to_return,
generate_activities_return_sepaxml,
attribute_activities_as_returned,
get_all_bank_accounts,
get_unassigned_bank_account_activities,
get_all_mt940_errors,
Expand Down Expand Up @@ -609,6 +610,8 @@ def bank_account_activities_return_do() -> ResponseReturnValue:

sepa_xml: bytes = generate_activities_return_sepaxml(selected_activities)

attribute_activities_as_returned(session, selected_activities)

return send_file(
BytesIO(sepa_xml),
as_attachment=True,
Expand Down
2 changes: 1 addition & 1 deletion web/templates/finance/bank_account_activities_return.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

<div class="row">
<div class="col-md-12">
<button type="submit" class="btn btn-primary">SEPA-XML generieren</button>
<button type="submit" class="btn btn-primary">Überweisungen unbestätigt als unzuordenbar buchen und SEPA-XML exportieren</button>
</div>
</div>
</form>
Expand Down

0 comments on commit 4fe52b1

Please sign in to comment.