diff --git a/src/main/java/ch/wisv/events/core/model/order/Order.java b/src/main/java/ch/wisv/events/core/model/order/Order.java index b2351e2a..7042b40d 100644 --- a/src/main/java/ch/wisv/events/core/model/order/Order.java +++ b/src/main/java/ch/wisv/events/core/model/order/Order.java @@ -61,6 +61,12 @@ public class Order { @NotNull private Double vat = 0.0; + /** + * Administration costs of the order. + */ + @NotNull + private Double administrationCosts = 0.0; + /** * Field products list of Products in the Order. */ @@ -137,6 +143,7 @@ public void updateOrderAmount() { this.getOrderProducts().stream() .mapToDouble(orderProduct -> orderProduct.getProduct().getCost() * orderProduct.getAmount()) .sum() + + this.administrationCosts ); this.setVat(Math.round(this.getOrderProducts().stream() diff --git a/src/main/java/ch/wisv/events/core/service/order/OrderServiceImpl.java b/src/main/java/ch/wisv/events/core/service/order/OrderServiceImpl.java index 80ff391c..12974310 100644 --- a/src/main/java/ch/wisv/events/core/service/order/OrderServiceImpl.java +++ b/src/main/java/ch/wisv/events/core/service/order/OrderServiceImpl.java @@ -37,6 +37,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; @@ -79,6 +80,12 @@ public class OrderServiceImpl implements OrderService { */ private final TicketService ticketService; + /** + * Possible administration costs value + */ + @Value("${administrationCosts}") + private double administrationCosts; + /** * Constructor OrderServiceImpl creates a new OrderServiceImpl instance. * @@ -155,6 +162,10 @@ public Order createOrderByOrderProductDto(OrderProductDto orderProductDto) throw if (values.getValue() > 0) { Product product = productService.getByKey(values.getKey()); + if(product.getCost() > 0){ + order.setAdministrationCosts(administrationCosts); + } + order.addOrderProduct(new OrderProduct(product, product.getCost(), values.getValue())); } } diff --git a/src/main/java/ch/wisv/events/core/service/order/OrderValidationServiceImpl.java b/src/main/java/ch/wisv/events/core/service/order/OrderValidationServiceImpl.java index bf77002c..dfd04770 100644 --- a/src/main/java/ch/wisv/events/core/service/order/OrderValidationServiceImpl.java +++ b/src/main/java/ch/wisv/events/core/service/order/OrderValidationServiceImpl.java @@ -18,6 +18,7 @@ import java.time.LocalDateTime; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; /** @@ -38,6 +39,12 @@ public class OrderValidationServiceImpl implements OrderValidationService { /** EventService. */ private final EventService eventService; + /** + * Possible administration costs value + */ + @Value("${administrationCosts}") + private double administrationCosts; + /** * OrderValidationServiceImpl constructor. * @@ -133,10 +140,18 @@ private void assertDefaultOrderChecks(Order order) throws OrderInvalidException throw new OrderInvalidException("Order amount can not be null"); } + Double administrationCostShouldBe = order.getOrderProducts().stream() + .mapToDouble(orderProduct -> orderProduct.getProduct().getCost() * orderProduct.getAmount()) + .anyMatch(c -> c > 0.0) ? administrationCosts : 0.0; + + if (!order.getAdministrationCosts().equals(administrationCostShouldBe)) { + throw new OrderInvalidException("Order administration costs does not match"); + } + Double amountShouldBe = order.getOrderProducts() .stream() .mapToDouble(orderProduct -> orderProduct.getPrice() * orderProduct.getAmount()) - .sum(); + .sum() + order.getAdministrationCosts(); if (!order.getAmount().equals(amountShouldBe)) { throw new OrderInvalidException("Order amount does not match"); diff --git a/src/main/java/ch/wisv/events/utils/dev/data/OrderTestDataRunner.java b/src/main/java/ch/wisv/events/utils/dev/data/OrderTestDataRunner.java index 23bf469f..37a10fde 100644 --- a/src/main/java/ch/wisv/events/utils/dev/data/OrderTestDataRunner.java +++ b/src/main/java/ch/wisv/events/utils/dev/data/OrderTestDataRunner.java @@ -18,6 +18,7 @@ import java.util.UUID; import org.apache.commons.lang3.RandomStringUtils; import org.json.simple.JSONObject; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Profile; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @@ -48,6 +49,12 @@ public class OrderTestDataRunner extends TestDataRunner { /** TicketRepository. */ private final TicketRepository ticketRepository; + /** + * Possible administration costs value + */ + @Value("${administrationCosts}") + private double administrationCosts; + /** * Constructor EventTestDataRunner creates a new EventTestDataRunner instance. * @@ -102,6 +109,10 @@ private ch.wisv.events.core.model.order.Order createOrder(JSONObject jsonObject) orderProductRepository.saveAndFlush(orderProduct); + if(orderProduct.getPrice() > 0.0){ + order.setAdministrationCosts(administrationCosts); + } + order.addOrderProduct(orderProduct); order.getOrderProducts().forEach(x -> { x.getProduct().increaseSold(1); diff --git a/src/main/java/ch/wisv/events/webshop/controller/WebshopIndexController.java b/src/main/java/ch/wisv/events/webshop/controller/WebshopIndexController.java index a6c94c3d..59af9c36 100644 --- a/src/main/java/ch/wisv/events/webshop/controller/WebshopIndexController.java +++ b/src/main/java/ch/wisv/events/webshop/controller/WebshopIndexController.java @@ -32,6 +32,9 @@ public class WebshopIndexController extends WebshopController { /** Model attr of the OrderProductDTO. */ private static final String MODEL_ATTR_ORDER_PRODUCT = "orderProduct"; + /** Model attr administrationCosts. */ + private static final String MODEL_ATTR_ADMINISTRATION_COSTS = "administrationCosts"; + /** EventService. */ private final EventService eventService; @@ -45,6 +48,13 @@ public class WebshopIndexController extends WebshopController { @NotNull private String linkGTC; + /** + * Possible administration costs value + */ + @Value("${administrationCosts}") + @NotNull + private double administrationCosts; + /** * WebshopController constructor. * @@ -77,6 +87,7 @@ public String index(Model model) { model.addAttribute(MODEL_ATTR_CUSTOMER, authenticationService.getCurrentCustomer()); model.addAttribute(MODEL_ATTR_EVENTS, webshopService.filterEventProductNotSalable(upcoming)); model.addAttribute(MODEL_ATTR_ORDER_PRODUCT, new OrderProductDto()); + model.addAttribute(MODEL_ATTR_ADMINISTRATION_COSTS, administrationCosts); model.addAttribute("linkGTC", linkGTC); return "webshop/index"; @@ -96,6 +107,7 @@ public String index(Model model, @PathVariable String key) { model.addAttribute(MODEL_ATTR_CUSTOMER, authenticationService.getCurrentCustomer()); model.addAttribute(MODEL_ATTR_EVENT, webshopService.filterEventProductNotSalable(eventService.getByKey(key))); model.addAttribute(MODEL_ATTR_ORDER_PRODUCT, new OrderProductDto()); + model.addAttribute(MODEL_ATTR_ADMINISTRATION_COSTS, administrationCosts); return "webshop/event"; } catch (EventNotFoundException e) { diff --git a/src/main/java/ch/wisv/events/webshop/service/PaymentsServiceImpl.java b/src/main/java/ch/wisv/events/webshop/service/PaymentsServiceImpl.java index bbc7939f..6307542f 100644 --- a/src/main/java/ch/wisv/events/webshop/service/PaymentsServiceImpl.java +++ b/src/main/java/ch/wisv/events/webshop/service/PaymentsServiceImpl.java @@ -131,9 +131,7 @@ protected PaymentRequest createMolliePaymentRequestFromOrder(Order order) { String returnUrl = clientUri + "/return/" + order.getPublicReference(); String webhookUrl = clientUri + "/api/v1/orders/status"; - double value = order.getOrderProducts().stream() - .mapToDouble(op -> op.getPrice() * op.getAmount()) - .sum(); + double value = order.getAmount(); value = order.getPaymentMethod().calculateCostIncludingTransaction(value); Amount paymentAmount = Amount.builder().value(BigDecimal.valueOf(value).setScale(2, RoundingMode.CEILING)).currency("EUR").build(); diff --git a/src/main/resources/static/js/webshop/webshop.js b/src/main/resources/static/js/webshop/webshop.js index dcca7275..02cbf98a 100644 --- a/src/main/resources/static/js/webshop/webshop.js +++ b/src/main/resources/static/js/webshop/webshop.js @@ -75,6 +75,7 @@ var ShoppingBasket; var shoppingBasketTable = ""; var shoppingBasketTotal = 0; var countItems = 0; + var administrationCosts = 0; $.each(ShoppingBasket.shoppingBasket, function (index, product) { var rowBlueprint = "