Skip to content

Commit

Permalink
#483 - implement taxIdValidation extension
Browse files Browse the repository at this point in the history
  • Loading branch information
cbellone committed Jul 3, 2018
1 parent 32c56de commit fc59f61
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/main/java/alfio/manager/EuVatChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Pair<String, String>, Map<String, String>> validationCache = Caffeine.newBuilder()
Expand Down Expand Up @@ -167,12 +172,18 @@ public boolean test(String vatNr) {
}

Map<String, String> 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<String, Object> 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;
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/alfio/manager/ExtensionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public enum ExtensionEvent {
TICKET_ASSIGNED,
WAITING_QUEUE_SUBSCRIBED,
INVOICE_GENERATION,
TAX_ID_NUMBER_VALIDATION,
//
STUCK_RESERVATIONS,
OFFLINE_RESERVATIONS_WILL_EXPIRE,
Expand Down Expand Up @@ -155,6 +156,14 @@ public Optional<InvoiceGeneration> 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<String, Object> 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<String, Object> payload = new HashMap<>();
Event event = eventRepository.findById(ticket.getEventId());
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/alfio/model/Audit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit fc59f61

Please sign in to comment.