diff --git a/src/main/java/alfio/manager/EuVatChecker.java b/src/main/java/alfio/manager/EuVatChecker.java index 575c6160e4..984355dd33 100644 --- a/src/main/java/alfio/manager/EuVatChecker.java +++ b/src/main/java/alfio/manager/EuVatChecker.java @@ -36,13 +36,17 @@ import org.springframework.stereotype.Component; import java.io.IOException; -import java.util.*; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.function.BiFunction; import java.util.function.Predicate; import java.util.function.Supplier; import static alfio.model.Audit.EntityType.RESERVATION; +import static alfio.model.Audit.EventType.VAT_FORMAL_VALIDATION_SUCCESSFUL; import static alfio.model.Audit.EventType.VAT_VALIDATION_SUCCESSFUL; import static alfio.model.system.Configuration.getSystemConfiguration; import static alfio.model.system.ConfigurationKeys.APPLY_VAT_FOREIGN_BUSINESS; @@ -55,6 +59,7 @@ public class EuVatChecker { private final ConfigurationManager configurationManager; private final AuditingRepository auditingRepository; + private final ExtensionManager extensionManager; private final OkHttpClient client = new OkHttpClient(); private static final Cache, Map> validationCache = Caffeine.newBuilder() @@ -167,12 +172,18 @@ public boolean test(String vatNr) { } Map result = validateEUVat(vatNr, organizerCountry, checker.configurationManager, checker.client); - boolean valid = result != null && isValid(result); + boolean validStrict = result != null && isValid(result); + boolean valid = validStrict; + + if(!valid && StringUtils.isNotBlank(vatNr)) { + valid = checker.extensionManager.handleTaxIdValidation(eventId, vatNr, organizerCountry); + } + if(valid && StringUtils.isNotEmpty(ticketReservationId)) { Map data = new HashMap<>(); data.put("vatNumber", vatNr); data.put("country", organizerCountry); - checker.auditingRepository.insert(ticketReservationId, null, eventId, VAT_VALIDATION_SUCCESSFUL, new Date(), RESERVATION, ticketReservationId, singletonList(data)); + checker.auditingRepository.insert(ticketReservationId, null, eventId, validStrict ? VAT_VALIDATION_SUCCESSFUL : VAT_FORMAL_VALIDATION_SUCCESSFUL, new Date(), RESERVATION, ticketReservationId, singletonList(data)); } return valid; } diff --git a/src/main/java/alfio/manager/ExtensionManager.java b/src/main/java/alfio/manager/ExtensionManager.java index 6c055ebb73..736d3e315f 100644 --- a/src/main/java/alfio/manager/ExtensionManager.java +++ b/src/main/java/alfio/manager/ExtensionManager.java @@ -43,6 +43,7 @@ public enum ExtensionEvent { TICKET_ASSIGNED, WAITING_QUEUE_SUBSCRIBED, INVOICE_GENERATION, + TAX_ID_NUMBER_VALIDATION, // STUCK_RESERVATIONS, OFFLINE_RESERVATIONS_WILL_EXPIRE, @@ -155,6 +156,14 @@ public Optional handleInvoiceGeneration(Event event, String r return Optional.ofNullable(syncCall(ExtensionEvent.INVOICE_GENERATION, event, event.getOrganizationId(), payload, InvoiceGeneration.class)); } + public boolean handleTaxIdValidation(int eventId, String taxIdNumber, String countryCode) { + Event event = eventRepository.findById(eventId); + Map payload = new HashMap<>(); + payload.put("taxIdNumber", taxIdNumber); + payload.put("countryCode", countryCode); + return Optional.ofNullable(syncCall(ExtensionEvent.TAX_ID_NUMBER_VALIDATION, event, event.getOrganizationId(), payload, Boolean.class)).orElse(false); + } + public void handleTicketCheckedIn(Ticket ticket) { Map payload = new HashMap<>(); Event event = eventRepository.findById(ticket.getEventId()); diff --git a/src/main/java/alfio/model/Audit.java b/src/main/java/alfio/model/Audit.java index 7bf6ee18df..8fd7d4efd1 100644 --- a/src/main/java/alfio/model/Audit.java +++ b/src/main/java/alfio/model/Audit.java @@ -50,7 +50,8 @@ public enum EventType { FORCED_UPDATE_INVOICE, TERMS_CONDITION_ACCEPTED, PRIVACY_POLICY_ACCEPTED, - VAT_VALIDATION_SUCCESSFUL + VAT_VALIDATION_SUCCESSFUL, + VAT_FORMAL_VALIDATION_SUCCESSFUL } private final String reservationId;