Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add administration costs #510

Merged
merged 9 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config/application-devcontainer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,6 @@ googleWallet:
serviceKeyPath: ./googleservicekey.json
issuerId: 3388000000022297569
origin: http://localhost:8080
baseUrl: https://ch.tudelft.nl/events
baseUrl: https://ch.tudelft.nl/events

administrationCosts: 0.35
6 changes: 4 additions & 2 deletions config/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ spring:
username: postgres

# Password to log in with <MyDbPassword> in database <MyDb>
password:
password:

data.jpa.repositories.enabled: true

Expand Down Expand Up @@ -89,4 +89,6 @@ googleWallet:
serviceKeyPath: ./googleservicekey.json
issuerId: 3388000000022297569
origin: https://ch.tudelft.nl/events
baseUrl: https://ch.tudelft.nl/events
baseUrl: https://ch.tudelft.nl/events

administrationCosts: 0.35
4 changes: 3 additions & 1 deletion config/application.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,6 @@ googleWallet:
serviceKeyPath: ./googleservicekey.json
issuerId: 3388000000022297569
origin: https://ch.tudelft.nl/events
baseUrl: https://ch.tudelft.nl/events
baseUrl: https://ch.tudelft.nl/events

administrationCosts: 0.35
7 changes: 7 additions & 0 deletions src/main/java/ch/wisv/events/core/model/order/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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.
*
Expand Down Expand Up @@ -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.getPrice() * 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
*
Expand Down Expand Up @@ -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";
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package db.migration;

import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;

import java.sql.Statement;


/**
* DB migration which adds Transaction cost field.
*/
public class V202410026__Add_administration_costs extends BaseJavaMigration {

/**
* Executes this migration. The execution will automatically take place within a transaction, when the underlying
* database supports it.
*
* @param context of type Context
* @throws Exception when something is wrong
*/
public void migrate(Context context) throws Exception {
try (Statement select = context.getConnection().createStatement()) {
select.execute("ALTER TABLE public.orders ADD COLUMN administration_costs DOUBLE PRECISION NOT NULL DEFAULT 0");
}
}

}
13 changes: 13 additions & 0 deletions src/main/resources/static/js/webshop/webshop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<tr><td>%s</td><td><a href='#' class='decreaseBasketAmount' data-product-key='%s'><i class='fas fa-minus'></i></a><span class='px-4'>%s</span><a href='#' class='increaseBasketAmount' data-product-key='%s'><i class='fas fa-plus'></i></a></td><td>&euro; %s</td></tr>";
Expand All @@ -90,8 +91,20 @@ var ShoppingBasket;
countItems += product.amount;

shoppingBasketTotal += product.amount * product.cost;

if (product.cost > 0) {
administrationCosts = administrationCostsSetting;
}
});

var rowBlueprint = "<tr><td>Administration costs</td><td><td>&euro; %s</td></tr>";

shoppingBasketTable += vsprintf(rowBlueprint, [
parseFloat(Math.round(administrationCosts * 100) / 100).toFixed(2).replace(".", ",")
]);

shoppingBasketTotal += administrationCosts;

$("#shoppingBasketTable").html(shoppingBasketTable);
$("#shoppingBasketCount").html(countItems);
$("#shoppingBasketTotal").html("&euro; " + parseFloat(Math.round(shoppingBasketTotal * 100) / 100).toFixed(2).replace(".", ","));
Expand Down
Loading
Loading