Skip to content

Commit

Permalink
(PC-31957)[API] feat: include booking with partial finance incident i…
Browse files Browse the repository at this point in the history
…n the beneficiary's credits
  • Loading branch information
ataib-pass committed Sep 20, 2024
1 parent 2c7a847 commit 07361c2
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 8 deletions.
6 changes: 5 additions & 1 deletion api/src/pcapi/core/bookings/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from pcapi.core.bookings.models import ExternalBooking
from pcapi.core.bookings.utils import convert_booking_dates_utc_to_venue_timezone
from pcapi.core.categories import subcategories_v2 as subcategories
from pcapi.core.finance.models import BookingFinanceIncident
from pcapi.core.geography.models import Address
from pcapi.core.offerers.models import Offerer
from pcapi.core.offerers.models import OffererAddress
Expand Down Expand Up @@ -278,7 +279,10 @@ def get_bookings_from_deposit(deposit_id: int) -> list[Booking]:
Booking.depositId == deposit_id,
Booking.status != BookingStatus.CANCELLED,
)
.options(joinedload(Booking.stock).joinedload(Stock.offer))
.options(
joinedload(Booking.stock).joinedload(Stock.offer),
joinedload(Booking.incidents).joinedload(BookingFinanceIncident.incident),
)
.all()
)

Expand Down
3 changes: 2 additions & 1 deletion api/src/pcapi/core/external/attributes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,8 @@ def get_user_bookings(user: users_models.User) -> list[bookings_models.Booking]:
offers_models.Offer.subcategoryId,
offers_models.Offer.name,
offers_models.Offer.extraData,
)
),
joinedload(bookings_models.Booking.incidents).joinedload(finance_models.BookingFinanceIncident.incident),
)
.filter(
bookings_models.Booking.userId == user.id,
Expand Down
18 changes: 15 additions & 3 deletions api/src/pcapi/core/users/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,15 @@ def add_comment_to_user(user: models.User, author_user: models.User, comment: st
db.session.commit()


def _get_booking_credit(booking: bookings_models.Booking) -> Decimal:
# Get only partial incidents
for booking_finance_incident in booking.incidents:
if booking_finance_incident.is_partial:
if booking_finance_incident.incident.status == finance_models.IncidentStatus.VALIDATED:
return Decimal(booking_finance_incident.newTotalAmount) / Decimal("100")
return booking.total_amount


def get_domains_credit(
user: models.User, user_bookings: list[bookings_models.Booking] | None = None
) -> models.DomainsCredit | None:
Expand All @@ -794,7 +803,10 @@ def get_domains_credit(
all=models.Credit(
initial=user.deposit.amount,
remaining=(
max(user.deposit.amount - sum(booking.total_amount for booking in deposit_bookings), Decimal("0"))
max(
user.deposit.amount - sum(_get_booking_credit(booking) for booking in deposit_bookings),
Decimal("0"),
)
if user.has_active_deposit
else Decimal("0")
),
Expand All @@ -804,7 +816,7 @@ def get_domains_credit(

if specific_caps.DIGITAL_CAP:
digital_bookings_total = sum(
booking.total_amount
_get_booking_credit(booking)
for booking in deposit_bookings
if specific_caps.digital_cap_applies(booking.stock.offer)
)
Expand All @@ -820,7 +832,7 @@ def get_domains_credit(

if specific_caps.PHYSICAL_CAP:
physical_bookings_total = sum(
booking.total_amount
_get_booking_credit(booking)
for booking in deposit_bookings
if specific_caps.physical_cap_applies(booking.stock.offer)
)
Expand Down
10 changes: 7 additions & 3 deletions api/src/pcapi/routes/backoffice/accounts/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pcapi.core.bookings import models as bookings_models
from pcapi.core.external.attributes import api as external_attributes_api
from pcapi.core.finance import api as finance_api
from pcapi.core.finance import models as finance_models
from pcapi.core.fraud import api as fraud_api
from pcapi.core.fraud import models as fraud_models
from pcapi.core.history import api as history_api
Expand Down Expand Up @@ -284,9 +285,12 @@ def render_public_account_details(
users_models.User.query.filter_by(id=user_id)
.options(
sa.orm.joinedload(users_models.User.deposits),
sa.orm.subqueryload(users_models.User.userBookings)
.joinedload(bookings_models.Booking.stock)
.joinedload(offers_models.Stock.offer),
sa.orm.subqueryload(users_models.User.userBookings).options(
sa.orm.joinedload(bookings_models.Booking.stock).joinedload(offers_models.Stock.offer),
sa.orm.joinedload(bookings_models.Booking.incidents).joinedload(
finance_models.BookingFinanceIncident.incident
),
),
sa.orm.subqueryload(users_models.User.userBookings)
.joinedload(bookings_models.Booking.offerer)
.load_only(offerers_models.Offerer.name),
Expand Down
Loading

0 comments on commit 07361c2

Please sign in to comment.