transactions matrix

This commit is contained in:
2024-11-15 19:41:18 +01:00
parent 0e3b17f152
commit 0e18d28ca9
8 changed files with 230 additions and 15 deletions
@@ -34,7 +34,7 @@ public class Transaction implements Serializable {
@Column(name = "comment")
private String comment;
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JsonIgnoreProperties(value = { "registrations", "transactions" }, allowSetters = true)
private Event event;
@@ -0,0 +1,87 @@
package com.sasiedzi.event.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.sasiedzi.event.domain.enumeration.TransactionType;
import jakarta.persistence.*;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
//@Data
//@Builder
public class TransactionDTO implements Serializable {
private Long id;
private TransactionType type;
private LocalDate date;
private String comment;
private Event event;
@JsonIgnoreProperties(value = { "transaction", "event" }, allowSetters = true)
private List<TransactionItem> items = new ArrayList<>();
public TransactionDTO(Long id, TransactionType type, LocalDate date, String comment, Event event) {
this.id = id;
this.type = type;
this.date = date;
this.comment = comment;
this.event = event;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public TransactionType getType() {
return type;
}
public void setType(TransactionType type) {
this.type = type;
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public Event getEvent() {
return event;
}
public void setEvent(Event event) {
this.event = event;
}
public List<TransactionItem> getItems() {
return items;
}
public void setItems(List<TransactionItem> items) {
this.items = items;
}
}
@@ -147,7 +147,7 @@ public class EventService {
} else {
transaction = allTransactions.get(0);
if (transaction.getTransactionItems().isEmpty()) {
transaction.setTransactionItems(new HashSet<>());
// transaction.setTransactionItems(new HashSet<>());
}
transaction.getTransactionItems().clear();
transactionRepository.save(transaction);
@@ -155,7 +155,7 @@ public class EventService {
if (event.getRegistrations() != null && !event.getRegistrations().isEmpty()) {
String fieldServiceAccountName = "Boisko";
String vaultAccountName = "Skarbiec";
List<UserAccount> accounts = userAccountRepository.findAllWithEagerRelationships();
List<UserAccount> accounts = userAccountRepository.findAll();
Map<String, UserAccount> accountsByLogin = new HashMap<>();
accounts.forEach(acc -> {
acc
@@ -214,7 +214,7 @@ public class EventService {
.map(TransactionItem::getAmount)
.map(BigDecimal::negate)
.reduce(BigDecimal.ZERO, BigDecimal::add);
if (!vaultValue.equals(BigDecimal.ZERO)) {
if (vaultValue.compareTo(BigDecimal.ZERO) != 0) {
TransactionItem vaultItem = new TransactionItem();
vaultItem.setEvent(event);
vaultItem.setAmount(vaultValue);
@@ -1,13 +1,13 @@
package com.sasiedzi.event.web.rest;
import com.sasiedzi.event.domain.Transaction;
import com.sasiedzi.event.domain.*;
import com.sasiedzi.event.repository.TransactionRepository;
import com.sasiedzi.event.web.rest.errors.BadRequestAlertException;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
@@ -148,17 +148,103 @@ public class TransactionResource {
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of transactions in body.
*/
@GetMapping("")
public List<Transaction> getAllTransactions(
public List<TransactionDTO> getAllTransactions(
@RequestParam(name = "eagerload", required = false, defaultValue = "true") boolean eagerload
) {
LOG.debug("REST request to get all Transactions");
if (eagerload) {
return transactionRepository.findAllWithEagerRelationships();
} else {
return transactionRepository.findAll();
// LOG.debug("REST request to get all Transactions");
// if (eagerload) {
// return transactionRepository.findAllWithEagerRelationships();
// } else {
// return transactionRepository.findAll();
// }
List<Transaction> all = transactionRepository.findAll();
Map<UserAccount, TransactionItem> accounts = new HashMap<>();
Map<Pair<UserAccount, Transaction>, BigDecimal> items = new HashMap<>();
all.forEach(transaction -> {
transaction
.getTransactionItems()
.forEach(transactionItem -> {
accounts.put(transactionItem.getUserAccount(), transactionItem);
});
});
all.forEach(transaction -> {
transaction
.getTransactionItems()
.forEach(transactionItem -> {
items.put(
Pair.of(transactionItem.getUserAccount(), transaction),
add(items.get(Pair.of(transactionItem.getUserAccount(), transaction)), transactionItem.getAmount())
);
});
});
List<UserAccount> accountsSorted = accounts
.keySet()
.stream()
.sorted(Comparator.comparing(UserAccount::getName).thenComparing(Comparator.comparing(UserAccount::getName)))
.toList();
List<TransactionDTO> out = new ArrayList<>();
for (Transaction transaction : all.stream().sorted(Comparator.comparing(Transaction::getDate)).toList()) {
TransactionDTO transactionDTO = new TransactionDTO(
transaction.getId(),
transaction.getType(),
transaction.getDate(),
transaction.getComment(),
transaction.getEvent()
);
for (UserAccount userAccount : accountsSorted) {
BigDecimal amount = items.get(Pair.of(userAccount, transaction));
TransactionItem item = new TransactionItem();
item.setUserAccount(userAccount);
transactionDTO.getItems().add(item);
item.setAmount(amount);
}
out.add(transactionDTO);
}
return out;
}
BigDecimal add(BigDecimal val, BigDecimal val2) {
if (val == null) return val2;
return val.add(val2);
}
// private Comparator<UserAccount> getMyAccountFirstComparator (final String myAccountName) {
//
// Comparator<UserAccount> customOrder = Comparator.comparing(s -> {
// if (s.getName().equals(myAccountName)) {
// return 1;
// } else if (s.getName().equals("Boisko")) {
// return s.getName();
// } else if (s.getName().equals("Skarbiec")) {
// return 3;
// }
// return 4;
// });
//
// return customOrder.thenComparing(UserAccount::getName);
//
// }
// public class CustomComparator implements Comparator<UserAccount> {
//
// @Override
// public int compare(UserAccount o1, UserAccount o2) {
//
//
// // Najpierw sortujemy według zdefiniowanego porządku
// int typeComparison = customOrder.compare(o1.getType(), o2.getType());
// if (typeComparison != 0) {
// return typeComparison;
// }
//
// // Jeśli typy są takie same, sortujemy według nazwy
// return o1.getName().compareTo(o2.getName());
// }
// }
/**
* {@code GET /transactions/:id} : get the "id" transaction.
*
@@ -30,6 +30,12 @@
<span>Wszystkie wydarzenia</span>
</span>
</b-nav-item>
<b-nav-item to="/transaction" exact>
<span>
<font-awesome-icon icon="th-list" />
<span>Finanse</span>
</span>
</b-nav-item>
<!-- <b-nav-item-dropdown right id="entity-menu" v-if="authenticated" active-class="active" class="pointer" data-cy="entity">-->
<!-- <template #button-content>-->
<!-- <span class="navbar-dropdown-menu">-->
@@ -31,7 +31,11 @@
<th scope="row"><span>Type</span></th>
<th scope="row"><span>Date</span></th>
<th scope="row"><span>Comment</span></th>
<th scope="row"><span>Event</span></th>
<th scope="row" v-for="item in transactions[0].items">
<span>{{ item.userAccount?.name }}</span>
</th>
<th scope="row"></th>
</tr>
</thead>
@@ -50,6 +54,7 @@
}}</router-link>
</div>
</td>
<td v-for="item in transaction.items">{{ item.amount }}</td>
<td class="text-right">
<div class="btn-group">
<router-link :to="{ name: 'TransactionView', params: { transactionId: transaction.id } }" custom v-slot="{ navigate }">
@@ -1,12 +1,14 @@
import { type IEvent } from '@/shared/model/event.model';
import { type TransactionType } from '@/shared/model/enumerations/transaction-type.model';
import type { IUserAccount } from '@/shared/model/user-account.model';
export interface ITransaction {
id?: number;
type?: keyof typeof TransactionType | null;
date?: Date | null;
comment?: string | null;
event?: IEvent | null;
items?: ITransactionItem[] | [];
}
export class Transaction implements ITransaction {
@@ -16,5 +18,24 @@ export class Transaction implements ITransaction {
public date?: Date | null,
public comment?: string | null,
public event?: IEvent | null,
public items?: ITransactionItem[] | [],
) {}
}
export interface ITransactionItem {
id?: number;
comment?: string | null;
event?: IEvent | null;
amount?: number | null;
userAccount?: IUserAccount | null;
}
export class TransactionItem implements ITransactionItem {
constructor(
public id?: number,
public comment?: string | null,
public event?: IEvent | null,
public amount?: number | null,
public userAccount?: IUserAccount | null,
) {}
}