Skip to content

Commit

Permalink
Merge 4d38016 into ab0e496
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Aug 20, 2019
2 parents ab0e496 + 4d38016 commit 2c08861
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
public class EventWithAdditionalInfo implements DateValidity {
private final Event event;
private final String mapUrl;
private final Organization organization;
private final Organization.OrganizationContact organization;
private final Map<String, String> description;
private final Map<PaymentMethod, PaymentProxyWithParameters> activePaymentMethods;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
import alfio.repository.user.OrganizationRepository;
import alfio.util.*;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -84,7 +84,6 @@ public class EventApiV2Controller {
private final TicketCategoryDescriptionRepository ticketCategoryDescriptionRepository;
private final PaymentManager paymentManager;
private final MessageSourceManager messageSourceManager;
private final EuVatChecker vatChecker;
private final AdditionalServiceRepository additionalServiceRepository;
private final AdditionalServiceTextRepository additionalServiceTextRepository;
private final WaitingQueueManager waitingQueueManager;
Expand Down Expand Up @@ -128,13 +127,11 @@ public ResponseEntity<EventWithAdditionalInfo> getEvent(@PathVariable("eventName

var messageSource = messageSourceManager.getMessageSourceForEvent(event);

var descriptions = applyCommonMark(eventDescriptionRepository.findByEventIdAndType(event.getId(), EventDescription.EventDescriptionType.DESCRIPTION)
.stream()
.collect(Collectors.toMap(EventDescription::getLocale, EventDescription::getDescription)));
var descriptions = applyCommonMark(eventDescriptionRepository.findDescriptionByEventIdAsMap(event.getId()));

var organization = organizationRepository.getById(event.getOrganizationId());
var organization = organizationRepository.getContactById(event.getOrganizationId());

var configurationsValues = configurationManager.getFor(Arrays.asList(
var configurationsValues = configurationManager.getFor(List.of(
MAPS_PROVIDER,
MAPS_CLIENT_API_KEY,
MAPS_HERE_APP_ID,
Expand All @@ -151,7 +148,19 @@ public ResponseEntity<EventWithAdditionalInfo> getEvent(@PathVariable("eventName
DISPLAY_DISCOUNT_CODE_BOX,
USE_PARTNER_CODE_INSTEAD_OF_PROMOTIONAL,
GOOGLE_ANALYTICS_KEY,
GOOGLE_ANALYTICS_ANONYMOUS_MODE
GOOGLE_ANALYTICS_ANONYMOUS_MODE,
// captcha
ENABLE_CAPTCHA_FOR_TICKET_SELECTION,
RECAPTCHA_API_KEY,
ENABLE_CAPTCHA_FOR_OFFLINE_PAYMENTS,
//
GENERATE_ONLY_INVOICE,
//
INVOICE_ADDRESS,
VAT_NR,
// required by EuVatChecker.reverseChargeEnabled
ENABLE_EU_VAT_DIRECTIVE,
COUNTRY_OF_BUSINESS
), ConfigurationLevel.event(event));

var geoInfoConfiguration = Map.of(
Expand All @@ -171,13 +180,13 @@ public ResponseEntity<EventWithAdditionalInfo> getEvent(@PathVariable("eventName
});

//
boolean captchaForTicketSelection = configurationManager.isRecaptchaForTicketSelectionEnabled(event);
boolean captchaForTicketSelection = isRecaptchaForTicketSelectionEnabled(configurationsValues);
String recaptchaApiKey = null;
if (captchaForTicketSelection) {
recaptchaApiKey = configurationsValues.get(RECAPTCHA_API_KEY).getValueOrDefault(null);
}
//
boolean captchaForOfflinePaymentAndFreeEnabled = configurationManager.isRecaptchaForOfflinePaymentAndFreeEnabled(ConfigurationLevel.event(event));
boolean captchaForOfflinePaymentAndFreeEnabled = configurationManager.isRecaptchaForOfflinePaymentAndFreeEnabled(configurationsValues);
var captchaConf = new EventWithAdditionalInfo.CaptchaConfiguration(captchaForTicketSelection, captchaForOfflinePaymentAndFreeEnabled, recaptchaApiKey);


Expand All @@ -192,10 +201,10 @@ public ResponseEntity<EventWithAdditionalInfo> getEvent(@PathVariable("eventName
var formattedEndTime = Formatters.getFormattedDate(event, event.getEnd(), "common.event.time-format", messageSource);

//invoicing information
boolean canGenerateReceiptOrInvoiceToCustomer = configurationManager.canGenerateReceiptOrInvoiceToCustomer(event);
boolean euVatCheckingEnabled = vatChecker.isReverseChargeEnabledFor(event);
boolean invoiceAllowed = configurationManager.hasAllConfigurationsForInvoice(event) || euVatCheckingEnabled;
boolean onlyInvoice = invoiceAllowed && configurationManager.isInvoiceOnly(event);
boolean canGenerateReceiptOrInvoiceToCustomer = configurationManager.canGenerateReceiptOrInvoiceToCustomer(configurationsValues);
boolean euVatCheckingEnabled = EuVatChecker.reverseChargeEnabled(configurationsValues);
boolean invoiceAllowed = configurationManager.hasAllConfigurationsForInvoice(configurationsValues) || euVatCheckingEnabled;
boolean onlyInvoice = invoiceAllowed && configurationManager.isInvoiceOnly(configurationsValues);
boolean customerReferenceEnabled = configurationsValues.get(ENABLE_CUSTOMER_REFERENCE).getValueAsBooleanOrDefault(false);
boolean enabledItalyEInvoicing = configurationsValues.get(ENABLE_ITALY_E_INVOICING).getValueAsBooleanOrDefault(false);
boolean vatNumberStrictlyRequired = configurationsValues.get(VAT_NUMBER_IS_REQUIRED).getValueAsBooleanOrDefault(false);
Expand Down Expand Up @@ -443,9 +452,11 @@ public ResponseEntity<ValidatedResponse<String>> reserveTickets(@PathVariable("e
}

Optional<String> promoCodeDiscount = codeCheck.map(ValidatedResponse::getValue).flatMap(Pair::getRight).map(PromoCodeDiscount::getPromoCode);
var configurationValues = configurationManager.getFor(List.of(
ENABLE_CAPTCHA_FOR_TICKET_SELECTION,
RECAPTCHA_API_KEY), ConfigurationLevel.event(event));


if (isCaptchaInvalid(reservation.getCaptcha(), request.getRequest(), event)) {
if (isCaptchaInvalid(reservation.getCaptcha(), request.getRequest(), configurationValues)) {
bindingResult.reject(ErrorsCode.STEP_2_CAPTCHA_VALIDATION_FAILED);
}

Expand Down Expand Up @@ -654,8 +665,14 @@ private static HttpHeaders getCorsHeaders() {
return headers;
}

private boolean isCaptchaInvalid(String recaptchaResponse, HttpServletRequest request, EventAndOrganizationId event) {
return configurationManager.isRecaptchaForTicketSelectionEnabled(event)
private boolean isRecaptchaForTicketSelectionEnabled(Map<ConfigurationKeys, ConfigurationManager.MaybeConfiguration> configurationValues) {
Validate.isTrue(configurationValues.containsKey(ENABLE_CAPTCHA_FOR_TICKET_SELECTION) && configurationValues.containsKey(RECAPTCHA_API_KEY));
return configurationValues.get(ENABLE_CAPTCHA_FOR_TICKET_SELECTION).getValueAsBooleanOrDefault(false) &&
configurationValues.get(RECAPTCHA_API_KEY).getValueOrDefault(null) != null;
}

private boolean isCaptchaInvalid(String recaptchaResponse, HttpServletRequest request, Map<ConfigurationKeys, ConfigurationManager.MaybeConfiguration> configurationValues) {
return isRecaptchaForTicketSelectionEnabled(configurationValues)
&& !recaptchaService.checkRecaptcha(recaptchaResponse, request);
}

Expand Down
16 changes: 13 additions & 3 deletions src/main/java/alfio/manager/EuVatChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.stereotype.Component;

Expand All @@ -40,7 +41,7 @@

import static alfio.model.Audit.EntityType.RESERVATION;
import static alfio.model.Audit.EventType.*;
import static alfio.model.system.ConfigurationKeys.APPLY_VAT_FOREIGN_BUSINESS;
import static alfio.model.system.ConfigurationKeys.*;

@Component
@Log4j2
Expand Down Expand Up @@ -144,8 +145,17 @@ static String organizerCountry(ConfigurationManager configurationManager, EventA
}

private static boolean reverseChargeEnabled(ConfigurationManager configurationManager, EventAndOrganizationId eventAndOrganizationId) {
var res = configurationManager.getFor(Set.of(ConfigurationKeys.ENABLE_EU_VAT_DIRECTIVE, ConfigurationKeys.COUNTRY_OF_BUSINESS), ConfigurationLevel.event(eventAndOrganizationId));
return res.get(ConfigurationKeys.ENABLE_EU_VAT_DIRECTIVE).getValueAsBooleanOrDefault(false) &&
var res = configurationManager.getFor(Set.of(ENABLE_EU_VAT_DIRECTIVE, ConfigurationKeys.COUNTRY_OF_BUSINESS), ConfigurationLevel.event(eventAndOrganizationId));
return reverseChargeEnabled(res);
}

/**
* @param res require the keys ENABLE_EU_VAT_DIRECTIVE, COUNTRY_OF_BUSINESS
* @return
*/
public static boolean reverseChargeEnabled(Map<ConfigurationKeys, ConfigurationManager.MaybeConfiguration> res) {
Validate.isTrue(res.containsKey(ENABLE_EU_VAT_DIRECTIVE) && res.containsKey(COUNTRY_OF_BUSINESS));
return res.get(ENABLE_EU_VAT_DIRECTIVE).getValueAsBooleanOrDefault(false) &&
res.get(ConfigurationKeys.COUNTRY_OF_BUSINESS).isPresent();
}

Expand Down
48 changes: 37 additions & 11 deletions src/main/java/alfio/manager/system/ConfigurationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -429,34 +429,60 @@ public String getPublicReservationID(EventAndOrganizationId event, TicketReserva

public boolean hasAllConfigurationsForInvoice(EventAndOrganizationId event) {
var r = getFor(Set.of(INVOICE_ADDRESS, VAT_NR), ConfigurationLevel.event(event));
return r.get(INVOICE_ADDRESS).isPresent() && r.get(VAT_NR).isPresent();
return hasAllConfigurationsForInvoice(r);
}

/**
* @param configurationValues note: require keys INVOICE_ADDRESS, VAT_NR
* @return
*/
public boolean hasAllConfigurationsForInvoice(Map<ConfigurationKeys, MaybeConfiguration> configurationValues) {
Validate.isTrue(configurationValues.containsKey(INVOICE_ADDRESS) && configurationValues.containsKey(VAT_NR));
return configurationValues.get(INVOICE_ADDRESS).isPresent() && configurationValues.get(VAT_NR).isPresent();
}

public boolean isRecaptchaForOfflinePaymentAndFreeEnabled(ConfigurationLevel configurationLevel) {
var conf = getFor(Set.of(ENABLE_CAPTCHA_FOR_OFFLINE_PAYMENTS, RECAPTCHA_API_KEY), configurationLevel);
return conf.get(ENABLE_CAPTCHA_FOR_OFFLINE_PAYMENTS).getValueAsBooleanOrDefault(false) &&
conf.get(RECAPTCHA_API_KEY).getValueOrDefault(null) != null;
public boolean isRecaptchaForOfflinePaymentAndFreeEnabled(Map<ConfigurationKeys, MaybeConfiguration> configurationValues) {
Validate.isTrue(configurationValues.containsKey(ENABLE_CAPTCHA_FOR_OFFLINE_PAYMENTS) && configurationValues.containsKey(RECAPTCHA_API_KEY));
return configurationValues.get(ENABLE_CAPTCHA_FOR_OFFLINE_PAYMENTS).getValueAsBooleanOrDefault(false) &&
configurationValues.get(RECAPTCHA_API_KEY).getValueOrDefault(null) != null;
}

public boolean isRecaptchaForTicketSelectionEnabled(EventAndOrganizationId event) {
var res = getFor(Set.of(ENABLE_CAPTCHA_FOR_TICKET_SELECTION, RECAPTCHA_API_KEY), ConfigurationLevel.event(event));
return res.get(ENABLE_CAPTCHA_FOR_TICKET_SELECTION).getValueAsBooleanOrDefault(false) &&
res.get(RECAPTCHA_API_KEY).getValueOrDefault(null) != null;
public boolean isRecaptchaForOfflinePaymentAndFreeEnabled(ConfigurationLevel configurationLevel) {
var conf = getFor(Set.of(ENABLE_CAPTCHA_FOR_OFFLINE_PAYMENTS, RECAPTCHA_API_KEY), configurationLevel);
return isRecaptchaForOfflinePaymentAndFreeEnabled(conf);
}

// https://github.com/alfio-event/alf.io/issues/573
public boolean canGenerateReceiptOrInvoiceToCustomer(EventAndOrganizationId event) {
return !isItalianEInvoicingEnabled(event);
}

public boolean canGenerateReceiptOrInvoiceToCustomer(Map<ConfigurationKeys, MaybeConfiguration> configurationValues) {
return !isItalianEInvoicingEnabled(configurationValues);
}

public boolean isInvoiceOnly(EventAndOrganizationId event) {
var res = getFor(Set.of(GENERATE_ONLY_INVOICE, ENABLE_ITALY_E_INVOICING), ConfigurationLevel.event(event));
return res.get(GENERATE_ONLY_INVOICE).getValueAsBooleanOrDefault(false) || res.get(ENABLE_ITALY_E_INVOICING).getValueAsBooleanOrDefault(false);
return isInvoiceOnly(res);
}

/**
* @param configurationValues note: require the key GENERATE_ONLY_INVOICE and ENABLE_ITALY_E_INVOICING to be present
* @return
*/
public boolean isInvoiceOnly(Map<ConfigurationKeys, MaybeConfiguration> configurationValues) {
Validate.isTrue(configurationValues.containsKey(GENERATE_ONLY_INVOICE) && configurationValues.containsKey(ENABLE_ITALY_E_INVOICING));
return configurationValues.get(GENERATE_ONLY_INVOICE).getValueAsBooleanOrDefault(false) || configurationValues.get(ENABLE_ITALY_E_INVOICING).getValueAsBooleanOrDefault(false);
}

public boolean isItalianEInvoicingEnabled(EventAndOrganizationId event) {
return getFor(ENABLE_ITALY_E_INVOICING, ConfigurationLevel.event(event)).getValueAsBooleanOrDefault(false);
var res = getFor(List.of(ENABLE_ITALY_E_INVOICING), ConfigurationLevel.event(event));
return isItalianEInvoicingEnabled(res);
}

public boolean isItalianEInvoicingEnabled(Map<ConfigurationKeys, MaybeConfiguration> configurationValues) {
Validate.isTrue(configurationValues.containsKey(ENABLE_ITALY_E_INVOICING));
return configurationValues.get(ENABLE_ITALY_E_INVOICING).getValueAsBooleanOrDefault(false);
}

//
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/alfio/model/EventDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,15 @@ public EventDescription(@Column("event_id_fk") Integer eventId, @Column("locale"
this.eventDescriptionType = eventDescriptionType;
this.description = description;
}

@Getter
public static class LocaleDescription {
private final String locale;
private final String description;

public LocaleDescription(@Column("locale") String locale, @Column("description") String description) {
this.locale = locale;
this.description = description;
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/alfio/model/user/Organization.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,15 @@ public int hashCode() {
.append(id)
.toHashCode();
}

@Getter
public static class OrganizationContact {
private final String name;
private final String email;

public OrganizationContact(@Column("name") String name, @Column("email") String email) {
this.name = name;
this.email = email;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ public interface EventDescriptionRepository {
@Query("select * from event_description_text where event_id_fk = :eventId")
List<EventDescription> findByEventId(@Bind("eventId") int eventId);

@Query("select * from event_description_text where event_id_fk = :eventId and type = :type")
List<EventDescription> findByEventIdAndType(@Bind("eventId") int eventId, @Bind("type")EventDescription.EventDescriptionType type);
@Query("select locale, description from event_description_text where event_id_fk = :eventId and type = 'DESCRIPTION'")
List<EventDescription.LocaleDescription> findDescriptionByEventId(@Bind("eventId") int eventId);

default Map<String, String> findByEventIdAsMap(int eventId) {
return findByEventId(eventId).stream().collect(Collectors.toMap(EventDescription::getLocale, EventDescription::getDescription));
}

default Map<String, String> findDescriptionByEventIdAsMap(int eventId) {
return findDescriptionByEventId(eventId).stream().collect(Collectors.toMap(EventDescription.LocaleDescription::getLocale, EventDescription.LocaleDescription::getDescription));
}

@Query("select description from event_description_text where event_id_fk = :eventId and type = :type and locale = :locale")
Optional<String> findDescriptionByEventIdTypeAndLocale(@Bind("eventId") int eventId, @Bind("type") EventDescription.EventDescriptionType type, @Bind("locale") String locale);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public interface OrganizationRepository {
@Query("SELECT * FROM organization where id = :id")
Organization getById(@Bind("id") int id);

@Query("select name, email from organization where id = :id")
Organization.OrganizationContact getContactById(@Bind("id") int id);

@Query("SELECT * FROM organization where name = :name")
Optional<Organization> findByName(@Bind("name") String name);

Expand Down

0 comments on commit 2c08861

Please sign in to comment.