Skip to content

Commit

Permalink
bugfix: recalculate reservation total price if there are tax-exempt c…
Browse files Browse the repository at this point in the history
…ategories
  • Loading branch information
cbellone committed Mar 30, 2023
1 parent b1be0f4 commit 27fc690
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/main/java/alfio/manager/ReverseChargeManager.java
Expand Up @@ -132,7 +132,9 @@ public void checkAndApplyVATRules(PurchaseContext purchaseContext,
handleSplitPayment(purchaseContext, reservationId, contactAndTicketsForm, country, optionalReservation.get(), categoriesList, noTaxCategories);
} else if (optionalReservation.isPresent() && !noTaxCategories.isEmpty()) {
// if there is at least one category with custom tax policy, we need to update its tickets
updateTicketsWithNoTaxSetting(purchaseContext, reservationId, optionalReservation.get(), categoriesList, noTaxCategories);
var reservation = optionalReservation.get();
updateTicketsWithNoTaxSetting(purchaseContext, reservationId, reservation, categoriesList, noTaxCategories);
updateBillingData(contactAndTicketsForm, purchaseContext, country, trimToNull(contactAndTicketsForm.getVatNr()), reservation, reservation.getVatStatus());
}
} catch (IllegalStateException ise) {//vat checker failure
bindingResult.rejectValue("vatNr", "error.vatVIESDown");
Expand Down Expand Up @@ -176,7 +178,7 @@ private void updateTicketsWithNoTaxSetting(PurchaseContext purchaseContext,
private void handleSplitPayment(PurchaseContext purchaseContext, String reservationId, ContactAndTicketsForm contactAndTicketsForm, String country, TicketReservation reservation, List<TicketCategory> categoriesList, List<Integer> noTaxCategories) {
updateTicketsWithNoTaxSetting(purchaseContext, reservationId, reservation, categoriesList, noTaxCategories);
var vatStatus = purchaseContext.getVatStatus() == INCLUDED ? INCLUDED_NOT_CHARGED : NOT_INCLUDED_NOT_CHARGED;
updateBillingData(reservationId, contactAndTicketsForm, purchaseContext, country, trimToNull(contactAndTicketsForm.getVatNr()), reservation, vatStatus);
updateBillingData(contactAndTicketsForm, purchaseContext, country, trimToNull(contactAndTicketsForm.getVatNr()), reservation, vatStatus);
}

private void handleValidationResult(PurchaseContext purchaseContext, String reservationId, ContactAndTicketsForm contactAndTicketsForm, BindingResult bindingResult, String country, boolean isEvent, boolean reverseChargeInPerson, boolean reverseChargeOnline, List<TicketCategory> categoriesList, List<Integer> noTaxCategories, VatDetail vatValidation) {
Expand All @@ -202,7 +204,7 @@ private void handleValidationResult(PurchaseContext purchaseContext, String rese
ticketRepository.updateVatStatusForReservation(reservationId, vatStatus);
updateTicketPricesByCategory(reservationId, currencyCode, event, priceContainers);
}
updateBillingData(reservationId, contactAndTicketsForm, purchaseContext, country, trimToNull(vatValidation.getVatNr()), reservation, vatStatus);
updateBillingData(contactAndTicketsForm, purchaseContext, country, trimToNull(vatValidation.getVatNr()), reservation, vatStatus);
} else {
var event = purchaseContext.event().orElseThrow();
var eventFormat = event.getFormat();
Expand All @@ -216,7 +218,7 @@ private void handleValidationResult(PurchaseContext purchaseContext, String rese

updateTicketPricesByCategory(reservationId, currencyCode, event, matchingCategories);
// update billing data for the reservation, using the original VatStatus from reservation
updateBillingData(reservationId, contactAndTicketsForm, purchaseContext, country, trimToNull(vatValidation.getVatNr()), reservation, reservation.getVatStatus());
updateBillingData(contactAndTicketsForm, purchaseContext, country, trimToNull(vatValidation.getVatNr()), reservation, reservation.getVatStatus());
}
vatChecker.logSuccessfulValidation(vatValidation, reservationId, purchaseContext);
}
Expand Down Expand Up @@ -257,7 +259,7 @@ public void applyCustomTaxPolicy(PurchaseContext purchaseContext,
.collect(Collectors.toList());
updateTicketPrices(priceMapping, currencyCode, event);
// update billing data for the reservation, using the original VatStatus from reservation
updateBillingData(reservationId, contactAndTicketsForm, purchaseContext, reservation.getVatCountryCode(), trimToNull(reservation.getVatNr()), reservation, reservation.getVatStatus());
updateBillingData(contactAndTicketsForm, purchaseContext, reservation.getVatCountryCode(), trimToNull(reservation.getVatNr()), reservation, reservation.getVatStatus());
}
}

Expand Down Expand Up @@ -358,7 +360,8 @@ private Predicate<TicketCategory> findReverseChargeCategory(boolean reverseCharg
};
}

private void updateBillingData(String reservationId, ContactAndTicketsForm contactAndTicketsForm, PurchaseContext purchaseContext, String country, String vatNr, TicketReservation reservation, PriceContainer.VatStatus vatStatus) {
private void updateBillingData(ContactAndTicketsForm contactAndTicketsForm, PurchaseContext purchaseContext, String country, String vatNr, TicketReservation reservation, PriceContainer.VatStatus vatStatus) {
var reservationId = reservation.getId();
var discount = getDiscountOrNull(reservation);
var additionalServiceItems = additionalServiceItemRepository.findByReservationUuid(reservation.getId());
var tickets = ticketReservationManager.findTicketsInReservation(reservation.getId());
Expand Down
Expand Up @@ -161,6 +161,7 @@ secondUuid, updateTicketOwnerForm("example@example1.org")
assertEquals(PriceContainer.VatStatus.INCLUDED, ticketRepository.findByUUID(secondUuid).getVatStatus());
totalPrice = ticketReservationManager.totalReservationCostWithVAT(reservationId).getLeft();
assertEquals(19901, totalPrice.priceWithVAT());
assertEquals(19901, ticketReservationManager.findById(reservationId).orElseThrow().getFinalPriceCts());
}

@Test
Expand Down Expand Up @@ -200,6 +201,7 @@ secondUuid, updateTicketOwnerForm("example@example1.org")
assertEquals(PriceContainer.VatStatus.NOT_INCLUDED, ticketRepository.findByUUID(secondUuid).getVatStatus());
totalPrice = ticketReservationManager.totalReservationCostWithVAT(reservationId).getLeft();
assertEquals(20100, totalPrice.priceWithVAT());
assertEquals(20100, ticketReservationManager.findById(reservationId).orElseThrow().getFinalPriceCts());
}

private static UpdateTicketOwnerForm updateTicketOwnerForm(String email) {
Expand Down

0 comments on commit 27fc690

Please sign in to comment.