-
Notifications
You must be signed in to change notification settings - Fork 3
Bugfix/ogc 3406 surcharges or discounts zeros invoice #2650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Tschuppi81
merged 16 commits into
master
from
bugfix/ogc-3406-surcharges-or-discounts-zeros-invoice
Aug 24, 2026
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8d20ca2
Add upgrade task to refresh invoices zeroed by backfill
Tschuppi81 c75daf6
Recover corrupted cases
Tschuppi81 0c4f756
Fix backfill db upgrade task
Tschuppi81 e553f6c
Adding test
Tschuppi81 a806ce0
Reservation: Harden pricing fallback and order the invoice refresh
Tschuppi81 2aa4fb5
Add fixme comment
Tschuppi81 692426c
Compact comments
Tschuppi81 05c4a9f
Remove unused runtime fix
Tschuppi81 da4d7b7
Rework back to original upgrade and fix
Tschuppi81 69605ab
Adjust tests
Tschuppi81 0ef73ca
Add all permutation test
Tschuppi81 0ba30f6
Simplify, remove logging, fix wrong free and adjust tests
Tschuppi81 e666140
Limit to updated invoices after rollout date
Tschuppi81 55e030d
Simplify
Tschuppi81 0d726b4
Add 'free' to the allocation filter
Tschuppi81 039f966
quota mirror tset and more
Tschuppi81 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import transaction | ||
|
|
||
| from datetime import datetime | ||
| from decimal import Decimal | ||
| from freezegun import freeze_time | ||
| from sqlalchemy.orm.attributes import flag_modified | ||
| from onegov.core.utils import Bunch | ||
| from onegov.org.upgrade import refresh_zeroed_reservation_invoices | ||
| from onegov.reservation import ResourceCollection | ||
| from onegov.ticket import TicketCollection | ||
|
|
||
| from typing import cast, TYPE_CHECKING | ||
| if TYPE_CHECKING: | ||
| from onegov.core.upgrade import UpgradeContext | ||
| from onegov.org.models.ticket import ReservationHandler | ||
| from tests.onegov.org.conftest import Client | ||
|
|
||
|
|
||
| @freeze_time('2026-08-20', tick=True) | ||
| def test_refresh_zeroed_reservation_invoices(client: Client) -> None: | ||
| """ A reservation invoice whose line was zeroed (before the price data was | ||
| corrected) is recomputed by the upgrade, restoring the price and the | ||
| payment. | ||
| """ | ||
| resources = ResourceCollection(client.app.libres_context) | ||
|
|
||
| transaction.begin() | ||
| resource = resources.add( | ||
| 'Parktower Panorama 24', 'Europe/Zurich', type='room') | ||
| resource.pricing_method = 'per_item' | ||
| resource.price_per_item = 200.00 | ||
| resource.payment_method = 'manual' | ||
| resource.currency = 'CHF' | ||
| scheduler = resource.get_scheduler(client.app.libres_context) | ||
| allocations = scheduler.allocate( | ||
| dates=(datetime(2026, 8, 20), datetime(2026, 8, 20)), | ||
| whole_day=True, | ||
| quota=4, | ||
| ) | ||
| reserve = client.bound_reserve(allocations[0]) | ||
| transaction.commit() | ||
|
|
||
| reserve(quota=1, whole_day=True) | ||
| page = client.get('/resource/parktower-panorama-24/form') | ||
| page.form['email'] = 'info@example.org' | ||
| ticket_page = page.form.submit().follow().form.submit().follow() | ||
| assert 'RSV-' in ticket_page.text | ||
|
|
||
| client.login_editor() | ||
| invoice = ( | ||
| client.get('/tickets/ALL/open') | ||
| .click('Annehmen').follow() | ||
| .click('Rechnung anzeigen') | ||
| ) | ||
| assert '200.00' in invoice | ||
|
|
||
| # already-zeroed state: line at 0, payment dropped, price data still 200 | ||
| transaction.begin() | ||
| session = client.app.session() | ||
| ticket = TicketCollection(session).query().filter_by( | ||
| handler_code='RSV').one() | ||
| handler = cast('ReservationHandler', ticket.handler) | ||
| payment = handler.payment | ||
| assert ticket.invoice is not None | ||
| for item in ticket.invoice.items: | ||
| if item.group == 'reservation': | ||
| item.unit = Decimal('0') | ||
| item.payments = [] | ||
| if payment is not None: | ||
| for reservation in handler.reservations: | ||
| reservation.payment = None | ||
| ticket.payment = None | ||
| ticket.payment_id = None | ||
| session.delete(payment) | ||
| session.flush() | ||
| ticket_id = ticket.id | ||
| transaction.commit() | ||
|
|
||
| # sanity: the invoice is collapsed and has no payment | ||
| session = client.app.session() | ||
| ticket = TicketCollection(session).query().filter_by(id=ticket_id).one() | ||
| assert ticket.invoice is not None | ||
| assert ticket.invoice.total_amount == Decimal('0') | ||
| assert ticket.handler.payment is None | ||
|
|
||
| # run the upgrade task | ||
| context = Bunch( | ||
| has_table=lambda table: True, | ||
| session=session, | ||
| app=Bunch(org=Bunch(price_rounding=None)), | ||
| request=Bunch(session=session, translate=lambda text: text), | ||
| ) | ||
| refresh_zeroed_reservation_invoices(cast('UpgradeContext', context)) | ||
| transaction.commit() | ||
|
|
||
| # the reservation line and the payment are restored | ||
| session = client.app.session() | ||
| ticket = TicketCollection(session).query().filter_by(id=ticket_id).one() | ||
| assert ticket.invoice is not None | ||
| reservation_items = [ | ||
| item for item in ticket.invoice.items if item.group == 'reservation' | ||
| ] | ||
| assert reservation_items | ||
| assert all(item.unit == Decimal('200') for item in reservation_items) | ||
| assert ticket.invoice.total_amount == Decimal('200') | ||
| assert ticket.handler.payment is not None | ||
| assert ticket.handler.payment.amount == Decimal('200') | ||
|
|
||
|
|
||
| @freeze_time('2026-08-20', tick=True) | ||
| def test_refresh_zeroed_reservation_invoices_allocation_priced( | ||
| client: Client, | ||
| ) -> None: | ||
| """ Same recovery, but the price comes from a per_item allocation override | ||
| on an otherwise free resource. Exercises the allocation branch of the | ||
| refresh scoping (not the per_item resource branch) (OGC-3406). | ||
| """ | ||
| resources = ResourceCollection(client.app.libres_context) | ||
|
|
||
| transaction.begin() | ||
| resource = resources.add( | ||
| 'Free Room', 'Europe/Zurich', type='room') | ||
| resource.pricing_method = 'free' # resource branch must not match | ||
| resource.payment_method = 'manual' | ||
| resource.currency = 'CHF' | ||
| scheduler = resource.get_scheduler(client.app.libres_context) | ||
| allocations = scheduler.allocate( | ||
| dates=(datetime(2026, 8, 20), datetime(2026, 8, 20)), | ||
| whole_day=True, | ||
| quota=4, | ||
| ) | ||
| allocations[0].data = { | ||
| 'pricing_method': 'per_item', 'price_per_item': 200.0, | ||
| 'price_per_hour': 0.0, 'currency': 'CHF', | ||
| } | ||
| flag_modified(allocations[0], 'data') | ||
| reserve = client.bound_reserve(allocations[0]) | ||
| transaction.commit() | ||
|
|
||
| reserve(quota=1, whole_day=True) | ||
| page = client.get('/resource/free-room/form') | ||
| page.form['email'] = 'info@example.org' | ||
| ticket_page = page.form.submit().follow().form.submit().follow() | ||
| assert 'RSV-' in ticket_page.text | ||
|
|
||
| client.login_editor() | ||
| invoice = ( | ||
| client.get('/tickets/ALL/open') | ||
| .click('Annehmen').follow() | ||
| .click('Rechnung anzeigen') | ||
| ) | ||
| assert '200.00' in invoice | ||
|
|
||
| # already-zeroed state: line at 0, payment dropped | ||
| transaction.begin() | ||
| session = client.app.session() | ||
| ticket = TicketCollection(session).query().filter_by( | ||
| handler_code='RSV').one() | ||
| handler = cast('ReservationHandler', ticket.handler) | ||
| payment = handler.payment | ||
| assert ticket.invoice is not None | ||
| for item in ticket.invoice.items: | ||
| if item.group == 'reservation': | ||
| item.unit = Decimal('0') | ||
| item.payments = [] | ||
| if payment is not None: | ||
| for reservation in handler.reservations: | ||
| reservation.payment = None | ||
| ticket.payment = None | ||
| ticket.payment_id = None | ||
| session.delete(payment) | ||
| session.flush() | ||
| ticket_id = ticket.id | ||
| transaction.commit() | ||
|
|
||
| session = client.app.session() | ||
| context = Bunch( | ||
| has_table=lambda table: True, | ||
| session=session, | ||
| app=Bunch(org=Bunch(price_rounding=None)), | ||
| request=Bunch(session=session, translate=lambda text: text), | ||
| ) | ||
| refresh_zeroed_reservation_invoices(cast('UpgradeContext', context)) | ||
| transaction.commit() | ||
|
|
||
| # restored via the allocation branch | ||
| session = client.app.session() | ||
| ticket = TicketCollection(session).query().filter_by(id=ticket_id).one() | ||
| assert ticket.invoice is not None | ||
| reservation_items = [ | ||
| item for item in ticket.invoice.items if item.group == 'reservation' | ||
| ] | ||
| assert reservation_items | ||
| assert all(item.unit == Decimal('200') for item in reservation_items) | ||
| assert ticket.invoice.total_amount == Decimal('200') | ||
| assert ticket.handler.payment is not None | ||
| assert ticket.handler.payment.amount == Decimal('200') |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.