use of alfio.model.PriceContainer.VatStatus in project alf.io by alfio-event.
the class DataMigrator method collectASItemMigrationData.
private List<Pair<String, MapSqlParameterSource>> collectASItemMigrationData(String srcPriceCtsParam, final boolean eventVatIncluded, final BigDecimal vatPercentage, final String currencyCode, Map<String, Integer> eventIdParam) {
return jdbc.queryForList("select ai.id as id, ai.paid_price_cts as paid_price_cts, a.vat_type as vat_type from additional_service_item ai, additional_service a where ai.paid_price_cts > 0 and ai.src_price_cts = 0 and ai.event_id_fk = :eventId", eventIdParam).stream().map(item -> {
int oldPrice = (int) item.get("paid_price_cts");
AdditionalService.VatType vatType = AdditionalService.VatType.valueOf((String) item.get("vat_type"));
return Pair.of((Integer) item.get("id"), new PriceContainer() {
@Override
public int getSrcPriceCts() {
return eventVatIncluded ? MonetaryUtil.addVAT(oldPrice, vatPercentage) : oldPrice;
}
@Override
public String getCurrencyCode() {
return currencyCode;
}
@Override
public Optional<BigDecimal> getOptionalVatPercentage() {
return Optional.of(vatPercentage);
}
@Override
public VatStatus getVatStatus() {
if (vatType == AdditionalService.VatType.INHERITED) {
return eventVatIncluded ? INCLUDED : NOT_INCLUDED;
}
// no need to check for other VatTypes. At the time of writing VAT override strategy has not yet been implemented.
return NONE;
}
});
}).map(p -> {
PriceContainer priceContainer = p.getValue();
return Pair.of("additional_service_item", new MapSqlParameterSource(srcPriceCtsParam, priceContainer.getSrcPriceCts()).addValue("finalPriceCts", MonetaryUtil.unitToCents(priceContainer.getFinalPrice())).addValue("vatCts", MonetaryUtil.unitToCents(priceContainer.getVAT())).addValue("additionalServiceItemId", p.getKey()));
}).collect(toList());
}
use of alfio.model.PriceContainer.VatStatus in project alf.io by alfio-event.
the class ReservationApiController method validateEUVat.
@RequestMapping(value = "/event/{eventName}/reservation/{reservationId}/vat-validation", method = RequestMethod.POST)
@Transactional
public ResponseEntity<VatDetail> validateEUVat(@PathVariable("eventName") String eventName, @PathVariable("reservationId") String reservationId, PaymentForm paymentForm, Locale locale, HttpServletRequest request) {
String country = paymentForm.getVatCountryCode();
Optional<Triple<Event, TicketReservation, VatDetail>> vatDetail = eventRepository.findOptionalByShortName(eventName).flatMap(e -> ticketReservationRepository.findOptionalReservationById(reservationId).map(r -> Pair.of(e, r))).filter(e -> EnumSet.of(INCLUDED, NOT_INCLUDED).contains(e.getKey().getVatStatus())).filter(e -> vatChecker.isVatCheckingEnabledFor(e.getKey().getOrganizationId())).flatMap(e -> vatChecker.checkVat(paymentForm.getVatNr(), country, e.getKey().getOrganizationId()).map(vd -> Triple.of(e.getLeft(), e.getRight(), vd)));
vatDetail.filter(t -> t.getRight().isValid()).ifPresent(t -> {
VatDetail vd = t.getRight();
String billingAddress = vd.getName() + "\n" + vd.getAddress();
PriceContainer.VatStatus vatStatus = determineVatStatus(t.getLeft().getVatStatus(), t.getRight().isVatExempt());
ticketReservationRepository.updateBillingData(vatStatus, vd.getVatNr(), country, paymentForm.isInvoiceRequested(), reservationId);
OrderSummary orderSummary = ticketReservationManager.orderSummaryForReservationId(reservationId, t.getLeft(), Locale.forLanguageTag(t.getMiddle().getUserLanguage()));
ticketReservationRepository.addReservationInvoiceOrReceiptModel(reservationId, Json.toJson(orderSummary));
ticketReservationRepository.updateTicketReservation(reservationId, t.getMiddle().getStatus().name(), paymentForm.getEmail(), paymentForm.getFullName(), paymentForm.getFirstName(), paymentForm.getLastName(), locale.getLanguage(), billingAddress, null, Optional.ofNullable(paymentForm.getPaymentMethod()).map(PaymentProxy::name).orElse(null));
paymentForm.getTickets().forEach((ticketId, owner) -> {
if (isNotEmpty(owner.getEmail()) && ((isNotEmpty(owner.getFirstName()) && isNotEmpty(owner.getLastName())) || isNotEmpty(owner.getFullName()))) {
ticketHelper.preAssignTicket(eventName, reservationId, ticketId, owner, Optional.empty(), request, (tr) -> {
}, Optional.empty());
}
});
});
return vatDetail.map(Triple::getRight).map(vd -> {
if (vd.isValid()) {
return ResponseEntity.ok(vd);
} else {
return new ResponseEntity<VatDetail>(HttpStatus.BAD_REQUEST);
}
}).orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
use of alfio.model.PriceContainer.VatStatus in project alf.io by alfio-event.
the class DataMigrator method migratePrices.
private void migratePrices(final int eventId) {
final Map<String, Integer> eventIdParam = Collections.singletonMap("eventId", eventId);
final String srcPriceCtsParam = "srcPriceCts";
Map<String, List<MapSqlParameterSource>> migrationData = jdbc.queryForList("select * from event where type = :type and id = :eventId and regular_price_cts > 0", new MapSqlParameterSource("type", Event.EventType.INTERNAL.name()).addValue("eventId", eventId)).stream().flatMap(event -> {
// fill the event prices
boolean eventVatIncluded = (boolean) event.get("vat_included");
BigDecimal vatPercentage = Optional.ofNullable((BigDecimal) event.get("vat")).orElse(BigDecimal.ZERO);
int price = (int) event.get("regular_price_cts");
String currencyCode = (String) event.get("currency");
int eventSrcPrice = eventVatIncluded ? MonetaryUtil.addVAT(price, vatPercentage) : price;
List<Pair<String, MapSqlParameterSource>> modifications = new ArrayList<>();
if (((int) event.get("src_price_cts")) == 0) {
modifications.add(Pair.of("event", new MapSqlParameterSource(srcPriceCtsParam, eventSrcPrice).addValue("vatStatus", eventVatIncluded ? INCLUDED.name() : NOT_INCLUDED.name()).addValue("eventId", eventId)));
}
// ticket categories
modifications.addAll(collectTicketCategoryMigrationData(srcPriceCtsParam, eventVatIncluded, vatPercentage, eventIdParam));
// tickets
modifications.addAll(collectTicketMigrationData(srcPriceCtsParam, eventVatIncluded, vatPercentage, currencyCode, eventId, eventIdParam));
// additional_service
modifications.addAll(collectASMigrationData(srcPriceCtsParam, eventVatIncluded, vatPercentage, eventIdParam));
// additional_service_item
modifications.addAll(collectASItemMigrationData(srcPriceCtsParam, eventVatIncluded, vatPercentage, currencyCode, eventIdParam));
log.debug("Price migration: got {} modifications for event {}", modifications.size(), event.get("short_name"));
return modifications.stream();
}).collect(groupingBy(Pair::getKey, mapping(Pair::getValue, toList())));
if (migrationData.size() > 0) {
log.debug("Price migration: got modifications for: {}", migrationData.keySet());
PRICE_UPDATE_BY_KEY.entrySet().stream().filter(e -> migrationData.containsKey(e.getKey())).map(e -> Pair.of(e, migrationData.get(e.getKey()))).forEach(p -> {
Map.Entry<String, String> entry = p.getLeft();
log.debug("migrating {} prices...", entry.getKey());
performPriceMigration(entry.getValue(), p.getRight(), jdbc);
});
}
}
Aggregations