Skip to content

Commit

Permalink
Add VAT to treasurer tab (#456)
Browse files Browse the repository at this point in the history
* Added VAT rate column to treasurer tab

* change to layout of treasurer tab

* added VAT column to treasurer tab test
  • Loading branch information
JulesFleuren authored Nov 5, 2023
1 parent 5e09f88 commit 7b5fc5d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import ch.wisv.events.core.repository.OrderRepository;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
Expand Down Expand Up @@ -57,37 +59,40 @@ public String index(Model model) {
* This method uses an intensive SQL query in the order repository. It filters order such that
* all returned orders use IDEAL/SOFORT and were actually paid.
*
* The treasurerRepository returns a list of TreasurerData that contains 4 parameters:
* The treasurerRepository returns a list of TreasurerData that contains 5 parameters:
* - the PaidAt date
* - The title of the product
* - The price of the product
* - The amount bought of the product
* - The vatRate of the product
*
* This is all the data required to create the treasurer data page.
*
* @return HashMap
*/
private Map<LocalDate, Map<String, Pair<Double, Integer>>> generateProductMap() {
private Map<LocalDate, Map<String, Triple<Double, Integer, String>>> generateProductMap() {
List<TreasurerData> treasurerData = orderRepository.findallPayments();
ListIterator<TreasurerData> listIterator = treasurerData.listIterator(treasurerData.size());
Map<LocalDate, Map<String, Pair<Double, Integer>>> map = new TreeMap<>();
Map<LocalDate, Map<String, Triple<Double, Integer, String>>> map = new TreeMap<>();

while (listIterator.hasPrevious()) {
TreasurerData data = listIterator.previous();
LocalDateTime paidAt = data.getPaidAt();
if (paidAt == null) {
continue;
}


// Set date to first of the month,
// in this way all orders that are paid in the same month will have the same date
LocalDate date = paidAt.toLocalDate();
date = LocalDate.of(date.getYear(), date.getMonthValue(), 1);
date = LocalDate.of(date.getYear(), date.getMonthValue(), 1);

Map<String, Pair<Double, Integer>> list = map.getOrDefault(date, new HashMap<>());
Map<String, Triple<Double, Integer, String>> list = map.getOrDefault(date, new HashMap<>());
if (!list.containsKey(data.getTitle())) {
list.put(data.getTitle(), new ImmutablePair<>(data.getPrice(), data.getAmount()));
list.put(data.getTitle(), new ImmutableTriple<>(data.getPrice(), data.getAmount(), data.getVatRate()));
} else {
list.put(data.getTitle(),
new ImmutablePair<>(data.getPrice(), list.get(data.getTitle()).getRight()+data.getAmount())
new ImmutableTriple<>(data.getPrice(), list.get(data.getTitle()).getMiddle()+data.getAmount(), data.getVatRate())
);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/ch/wisv/events/core/admin/TreasurerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public interface TreasurerData {
double getPrice();
int getAmount();
LocalDateTime getPaidAt();
String getVatRate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public interface OrderRepository extends JpaRepository<Order, Integer> {
List<Order> findAllByOrderProducts(OrderProduct orderProduct);

@Query(value =
"SELECT B.TITLE AS title,B.PRICE AS price,B.AMOUNT AS amount,O.PAID_AT AS paidAt " +
"SELECT B.TITLE AS title,B.PRICE AS price,B.AMOUNT AS amount,B.VAT_RATE AS vatRate, O.PAID_AT AS paidAt " +
"FROM " +
"( SELECT * " +
"FROM " +
Expand All @@ -91,6 +91,7 @@ public interface OrderRepository extends JpaRepository<Order, Integer> {
"P.TITLE," +
"OP.AMOUNT," +
"OP.PRICE," +
"OP.VAT_RATE," +
"OP.ID AS OOPID " +
"FROM PRODUCT P " +
"INNER JOIN ORDER_PRODUCT OP ON P.ID = OP.PRODUCT_ID " +
Expand Down
12 changes: 8 additions & 4 deletions src/main/resources/templates/admin/treasurer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,20 @@ <h1>Mollie Overview</h1>
<th>Name</th>
<th class="text-center">Number</th>
<th class="text-center">Price</th>
<th class="text-right">Total</th>
<th class="text-center">Total</th>
<th class="text-center">VAT</th>
</tr>
</thead>
<tbody>
<tr th:each="product : ${date.value}">
<td th:text="${product.key}"></td>
<td th:text="${product.value.getRight()}" class="text-center"></td>
<td th:text="${product.value.getMiddle()}" class="text-center"></td>
<td th:text="'€ ' + ${#numbers.formatDecimal(product.value.getLeft(), 1, 'POINT', 2, 'COMMA')}"
class="text-right">
<td th:text="'€ ' + ${#numbers.formatDecimal(product.value.getLeft() * product.value.getRight(), 1, 'POINT', 2, 'COMMA')}"
<td th:text="'€ ' + ${#numbers.formatDecimal(product.value.getLeft() * product.value.getMiddle(), 1, 'POINT', 2, 'COMMA')}"
class="text-right"></td>
<td th:text="${product.value.getRight()}"
class="text-center"></td>
</tr>
</tbody>
<tfoot>
Expand All @@ -80,6 +83,7 @@ <h1>Mollie Overview</h1>
<th class="sum text-center"></th>
<th class="sum currency text-right"></th>
<th class="sum currency text-right"></th>
<th></th>
</tr>
</tfoot>
</table>
Expand Down Expand Up @@ -129,7 +133,7 @@ <h1>Mollie Overview</h1>
paging: false,
"bInfo": false,
columnDefs: [
{width: "100px", targets: [1, 2, 3]}
{width: "100px", targets: [1, 2, 3, 4]}
]
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.apache.commons.lang3.tuple.Triple;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -40,10 +42,10 @@ public void testIndex() throws Exception {
Order order1 = this.createOrder(customer, ImmutableList.of(product), OrderStatus.PENDING, "test");
orderService.updateOrderStatus(order1, OrderStatus.PAID);

Map<LocalDate, Map<String, Pair<Double, Integer>>> map = new TreeMap<>();
Map<LocalDate, Map<String, Triple<Double, Integer, String>>> map = new TreeMap<>();
LocalDate date = order.getPaidAt().toLocalDate();
map.put(LocalDate.of(date.getYear(), date.getMonthValue(), 1), ImmutableMap.of(product.title,
new ImmutablePair<>(product.cost, 2)));
new ImmutableTriple<>(product.cost, 2, product.vatRate.name())));

mockMvc.perform(get("/administrator/treasurer"))
.andExpect(status().is2xxSuccessful())
Expand Down

0 comments on commit 7b5fc5d

Please sign in to comment.