Search in sources :

Example 1 with SpecialPrice

use of alfio.model.SpecialPrice in project alf.io by alfio-event.

the class SpecialPriceManagerTest method init.

@Before
public void init() {
    List<SpecialPrice> specialPrices = asList(new SpecialPrice(0, "123", 0, 0, "FREE", null, null, null, null), new SpecialPrice(0, "456", 0, 0, "FREE", null, null, null, null));
    when(i18nManager.getEventLanguages(anyInt())).thenReturn(Collections.singletonList(ContentLanguage.ITALIAN));
    when(messageSource.getMessage(anyString(), any(), any())).thenReturn("text");
    when(eventManager.getSingleEvent(anyString(), anyString())).thenReturn(event);
    when(eventManager.loadTicketCategories(eq(event))).thenReturn(Collections.singletonList(ticketCategory));
    when(ticketCategory.getId()).thenReturn(0);
    when(specialPriceRepository.findActiveByCategoryId(eq(0))).thenReturn(specialPrices);
    when(eventManager.getEventUrl(eq(event))).thenReturn("http://my-event");
    when(eventManager.loadOrganizer(eq(event), anyString())).thenReturn(organization);
    when(event.getShortName()).thenReturn("eventName");
    when(event.getDisplayName()).thenReturn("Event Name");
    when(event.getLocales()).thenReturn(1);
    when(event.getZoneId()).thenReturn(ZoneId.systemDefault());
    when(specialPriceRepository.markAsSent(any(), anyString(), anyString(), anyString())).thenReturn(1);
    setRestricted(ticketCategory, true);
    specialPriceManager = new SpecialPriceManager(eventManager, notificationManager, specialPriceRepository, templateManager, messageSource, i18nManager);
}
Also used : SpecialPrice(alfio.model.SpecialPrice) Before(org.junit.Before)

Example 2 with SpecialPrice

use of alfio.model.SpecialPrice in project alf.io by alfio-event.

the class ReservationForm method validate.

public Optional<Pair<List<TicketReservationWithOptionalCodeModification>, List<ASReservationWithOptionalCodeModification>>> validate(Errors bindingResult, TicketReservationManager tickReservationManager, AdditionalServiceRepository additionalServiceRepository, EventManager eventManager, Event event) {
    int selectionCount = ticketSelectionCount();
    if (selectionCount <= 0) {
        bindingResult.reject(ErrorsCode.STEP_1_SELECT_AT_LEAST_ONE);
        return Optional.empty();
    }
    List<Pair<TicketReservationModification, Integer>> maxTicketsByTicketReservation = selected().stream().map(r -> Pair.of(r, tickReservationManager.maxAmountOfTicketsForCategory(event.getOrganizationId(), event.getId(), r.getTicketCategoryId()))).collect(toList());
    Optional<Pair<TicketReservationModification, Integer>> error = maxTicketsByTicketReservation.stream().filter(p -> p.getKey().getAmount() > p.getValue()).findAny();
    if (error.isPresent()) {
        bindingResult.reject(ErrorsCode.STEP_1_OVER_MAXIMUM, new Object[] { error.get().getValue() }, null);
        return Optional.empty();
    }
    final List<TicketReservationModification> categories = selected();
    final List<AdditionalServiceReservationModification> additionalServices = selectedAdditionalServices();
    final boolean validCategorySelection = categories.stream().allMatch(c -> {
        TicketCategory tc = eventManager.getTicketCategoryById(c.getTicketCategoryId(), event.getId());
        return OptionalWrapper.optionally(() -> eventManager.findEventByTicketCategory(tc)).isPresent();
    });
    final boolean validAdditionalServiceSelected = additionalServices.stream().allMatch(asm -> {
        AdditionalService as = eventManager.getAdditionalServiceById(asm.getAdditionalServiceId(), event.getId());
        ZonedDateTime now = ZonedDateTime.now(event.getZoneId());
        return as.getInception(event.getZoneId()).isBefore(now) && as.getExpiration(event.getZoneId()).isAfter(now) && asm.getQuantity() >= 0 && ((as.isFixPrice() && asm.isQuantityValid(as, selectionCount)) || (!as.isFixPrice() && asm.getAmount() != null && asm.getAmount().compareTo(BigDecimal.ZERO) >= 0)) && OptionalWrapper.optionally(() -> eventManager.findEventByAdditionalService(as)).isPresent();
    });
    if (!validCategorySelection || !validAdditionalServiceSelected) {
        bindingResult.reject(ErrorsCode.STEP_1_TICKET_CATEGORY_MUST_BE_SALEABLE);
        return Optional.empty();
    }
    List<TicketReservationWithOptionalCodeModification> res = new ArrayList<>();
    // 
    Optional<SpecialPrice> specialCode = Optional.ofNullable(StringUtils.trimToNull(promoCode)).flatMap((trimmedCode) -> tickReservationManager.getSpecialPriceByCode(trimmedCode));
    // 
    final ZonedDateTime now = ZonedDateTime.now(event.getZoneId());
    maxTicketsByTicketReservation.forEach((pair) -> validateCategory(bindingResult, tickReservationManager, eventManager, event, pair.getRight(), res, specialCode, now, pair.getLeft()));
    return bindingResult.hasErrors() ? Optional.empty() : Optional.of(Pair.of(res, additionalServices.stream().map(as -> new ASReservationWithOptionalCodeModification(as, specialCode)).collect(Collectors.toList())));
}
Also used : Errors(org.springframework.validation.Errors) TicketReservationModification(alfio.model.modification.TicketReservationModification) ZonedDateTime(java.time.ZonedDateTime) SaleableTicketCategory(alfio.controller.decorator.SaleableTicketCategory) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Pair(org.apache.commons.lang3.tuple.Pair) ASReservationWithOptionalCodeModification(alfio.model.modification.ASReservationWithOptionalCodeModification) AdditionalServiceRepository(alfio.repository.AdditionalServiceRepository) TicketReservationWithOptionalCodeModification(alfio.model.modification.TicketReservationWithOptionalCodeModification) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) ErrorsCode(alfio.util.ErrorsCode) OptionalWrapper(alfio.util.OptionalWrapper) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) TicketReservationManager(alfio.manager.TicketReservationManager) TicketCategory(alfio.model.TicketCategory) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) EventManager(alfio.manager.EventManager) AdditionalService(alfio.model.AdditionalService) SpecialPrice(alfio.model.SpecialPrice) Data(lombok.Data) Optional(java.util.Optional) Event(alfio.model.Event) AdditionalServiceReservationModification(alfio.model.modification.AdditionalServiceReservationModification) SpecialPrice(alfio.model.SpecialPrice) AdditionalService(alfio.model.AdditionalService) ArrayList(java.util.ArrayList) SaleableTicketCategory(alfio.controller.decorator.SaleableTicketCategory) TicketCategory(alfio.model.TicketCategory) ASReservationWithOptionalCodeModification(alfio.model.modification.ASReservationWithOptionalCodeModification) AdditionalServiceReservationModification(alfio.model.modification.AdditionalServiceReservationModification) TicketReservationWithOptionalCodeModification(alfio.model.modification.TicketReservationWithOptionalCodeModification) ZonedDateTime(java.time.ZonedDateTime) TicketReservationModification(alfio.model.modification.TicketReservationModification) Pair(org.apache.commons.lang3.tuple.Pair)

Example 3 with SpecialPrice

use of alfio.model.SpecialPrice in project alf.io by alfio-event.

the class SpecialPriceManager method loadSentCodes.

public List<SpecialPrice> loadSentCodes(String eventName, int categoryId, String username) {
    final Event event = eventManager.getSingleEvent(eventName, username);
    checkOwnership(categoryId, event, username);
    Predicate<SpecialPrice> p = SpecialPrice::notSent;
    return specialPriceRepository.findAllByCategoryId(categoryId).stream().filter(p.negate()).collect(toList());
}
Also used : SpecialPrice(alfio.model.SpecialPrice) Event(alfio.model.Event)

Example 4 with SpecialPrice

use of alfio.model.SpecialPrice in project alf.io by alfio-event.

the class SpecialPriceManager method checkCodeAssignment.

private List<String> checkCodeAssignment(Set<SendCodeModification> input, int categoryId, Event event, String username) {
    final TicketCategory category = checkOwnership(categoryId, event, username);
    List<String> availableCodes = new ArrayList<>(specialPriceRepository.findActiveByCategoryId(category.getId()).stream().filter(SpecialPrice::notSent).map(SpecialPrice::getCode).collect(toList()));
    Validate.isTrue(input.size() <= availableCodes.size(), "Requested codes: " + input.size() + ", available: " + availableCodes.size() + ".");
    List<String> requestedCodes = input.stream().filter(IS_CODE_PRESENT).map(SendCodeModification::getCode).collect(toList());
    Validate.isTrue(requestedCodes.stream().distinct().count() == requestedCodes.size(), "Cannot assign the same code twice. Please fix the input file.");
    Validate.isTrue(requestedCodes.stream().allMatch(availableCodes::contains), "some requested codes don't exist.");
    return availableCodes;
}
Also used : SpecialPrice(alfio.model.SpecialPrice) TicketCategory(alfio.model.TicketCategory)

Aggregations

SpecialPrice (alfio.model.SpecialPrice)4 Event (alfio.model.Event)2 TicketCategory (alfio.model.TicketCategory)2 SaleableTicketCategory (alfio.controller.decorator.SaleableTicketCategory)1 EventManager (alfio.manager.EventManager)1 TicketReservationManager (alfio.manager.TicketReservationManager)1 AdditionalService (alfio.model.AdditionalService)1 ASReservationWithOptionalCodeModification (alfio.model.modification.ASReservationWithOptionalCodeModification)1 AdditionalServiceReservationModification (alfio.model.modification.AdditionalServiceReservationModification)1 TicketReservationModification (alfio.model.modification.TicketReservationModification)1 TicketReservationWithOptionalCodeModification (alfio.model.modification.TicketReservationWithOptionalCodeModification)1 AdditionalServiceRepository (alfio.repository.AdditionalServiceRepository)1 ErrorsCode (alfio.util.ErrorsCode)1 OptionalWrapper (alfio.util.OptionalWrapper)1 Serializable (java.io.Serializable)1 BigDecimal (java.math.BigDecimal)1 ZonedDateTime (java.time.ZonedDateTime)1 ArrayList (java.util.ArrayList)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1