działąjące rozliczenie

This commit is contained in:
2024-11-13 17:14:27 +01:00
parent add69e7999
commit 7ae7cc1d36
9 changed files with 116 additions and 39 deletions
@@ -38,7 +38,7 @@ public class Transaction implements Serializable {
@JsonIgnoreProperties(value = { "registrations", "transactions" }, allowSetters = true) @JsonIgnoreProperties(value = { "registrations", "transactions" }, allowSetters = true)
private Event event; private Event event;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "transaction") @OneToMany(fetch = FetchType.LAZY, mappedBy = "transaction", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnoreProperties(value = { "userAccount", "transaction", "event" }, allowSetters = true) @JsonIgnoreProperties(value = { "userAccount", "transaction", "event" }, allowSetters = true)
private Set<TransactionItem> transactionItems = new HashSet<>(); private Set<TransactionItem> transactionItems = new HashSet<>();
@@ -170,4 +170,20 @@ public class User extends AbstractAuditingEntity<String> implements Serializable
", langKey='" + langKey + '\'' + ", langKey='" + langKey + '\'' +
"}"; "}";
} }
@Transient
public String getName() {
String out = "";
if (firstName != null) {
out = firstName;
}
if (lastName != null) {
out = (out + " " + lastName).trim();
}
if (out.isEmpty()) {
return login;
} else {
return out;
}
}
} }
@@ -25,7 +25,7 @@ public class UserAccount implements Serializable {
@Column(name = "name") @Column(name = "name")
private String name; private String name;
@ManyToMany(fetch = FetchType.LAZY) @ManyToMany(fetch = FetchType.EAGER)
@JoinTable( @JoinTable(
name = "rel_user_account__user", name = "rel_user_account__user",
joinColumns = @JoinColumn(name = "user_account_id"), joinColumns = @JoinColumn(name = "user_account_id"),
@@ -38,6 +38,8 @@ public interface TransactionRepository extends JpaRepository<Transaction, Long>
@Query("select transaction from Transaction transaction left join fetch transaction.event where transaction.id =:id") @Query("select transaction from Transaction transaction left join fetch transaction.event where transaction.id =:id")
Optional<Transaction> findOneWithToOneRelationships(@Param("id") Long id); Optional<Transaction> findOneWithToOneRelationships(@Param("id") Long id);
@Query("select transaction from Transaction transaction left join fetch transaction.transactionItem") @Query(
"select transaction from Transaction transaction left join fetch transaction.transactionItems where transaction.event.id =:eventId"
)
List<Transaction> findAllByEventId(Long eventId); List<Transaction> findAllByEventId(Long eventId);
} }
@@ -1,16 +1,18 @@
package com.sasiedzi.event.service; package com.sasiedzi.event.service;
import com.sasiedzi.event.domain.Event; import com.sasiedzi.event.domain.*;
import com.sasiedzi.event.domain.Transaction;
import com.sasiedzi.event.domain.enumeration.TransactionType; import com.sasiedzi.event.domain.enumeration.TransactionType;
import com.sasiedzi.event.repository.EventRepository; import com.sasiedzi.event.repository.EventRepository;
import com.sasiedzi.event.repository.TransactionRepository; import com.sasiedzi.event.repository.TransactionRepository;
import com.sasiedzi.event.repository.UserAccountRepository;
import com.sasiedzi.event.web.rest.TransactionResource; import com.sasiedzi.event.web.rest.TransactionResource;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Date; import java.util.*;
import java.util.List; import java.util.function.Function;
import java.util.Optional; import java.util.stream.Collectors;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -123,9 +125,16 @@ public class EventService {
@Autowired @Autowired
TransactionRepository transactionRepository; TransactionRepository transactionRepository;
@Autowired
UserAccountRepository userAccountRepository;
public Optional<Event> settle(@Valid Event event) { public Optional<Event> settle(@Valid Event event) {
Long eventId = event.getId(); Long eventId = event.getId();
List<Transaction> allTransactions = transactionRepository.findAllByEventId(eventId); List<Transaction> allTransactions = transactionRepository
.findAllByEventId(eventId)
.stream()
.filter(transaction -> TransactionType.MATCH.equals(transaction.getType()))
.toList();
Transaction transaction; Transaction transaction;
if (allTransactions.isEmpty()) { if (allTransactions.isEmpty()) {
@@ -133,9 +142,85 @@ public class EventService {
transaction.setEvent(event); transaction.setEvent(event);
transaction.setType(TransactionType.MATCH); transaction.setType(TransactionType.MATCH);
transaction.setDate(LocalDate.now()); transaction.setDate(LocalDate.now());
transaction.setTransactionItems(new HashSet<>());
transactionRepository.save(transaction); transactionRepository.save(transaction);
} else { } else {
transaction = allTransactions.get(0); transaction = allTransactions.get(0);
if (transaction.getTransactionItems().isEmpty()) {
transaction.setTransactionItems(new HashSet<>());
}
transaction.getTransactionItems().clear();
transactionRepository.save(transaction);
}
if (event.getRegistrations() != null && !event.getRegistrations().isEmpty()) {
String fieldServiceAccountName = "Boisko";
String vaultAccountName = "Skarbiec";
List<UserAccount> accounts = userAccountRepository.findAllWithEagerRelationships();
Map<String, UserAccount> accountsByLogin = new HashMap<>();
accounts.forEach(acc -> {
acc
.getUsers()
.forEach(user -> {
accountsByLogin.put(user.getLogin(), acc);
});
});
Map<String, UserAccount> accountsByAccountName = accounts
.stream()
.collect(Collectors.toMap(UserAccount::getName, Function.identity(), (existing, replacement) -> existing));
if (!accountsByAccountName.containsKey(fieldServiceAccountName)) {
UserAccount entity = new UserAccount();
entity.setName(fieldServiceAccountName);
entity = userAccountRepository.save(entity);
accountsByAccountName.put(fieldServiceAccountName, entity);
}
if (!accountsByAccountName.containsKey(vaultAccountName)) {
UserAccount entity = new UserAccount();
entity.setName(vaultAccountName);
entity = userAccountRepository.save(entity);
accountsByAccountName.put(vaultAccountName, entity);
}
List<UserAccount> accountsToCharge = new ArrayList<>();
event
.getRegistrations()
.forEach(registration -> {
String login = registration.getUser().getLogin();
UserAccount userAccount = accountsByLogin.get(login);
if (userAccount != null) {
userAccount = new UserAccount();
userAccount.setName(registration.getUser().getName());
userAccount = userAccountRepository.save(userAccount);
}
accountsToCharge.add(userAccount);
});
TransactionItem fieldServiceItem = new TransactionItem();
fieldServiceItem.setEvent(event);
fieldServiceItem.setAmount(event.getCost());
fieldServiceItem.setUserAccount(accountsByAccountName.get(fieldServiceAccountName));
fieldServiceItem.setTransaction(transaction);
transaction.getTransactionItems().add(fieldServiceItem);
accountsToCharge.forEach(userAccount -> {
TransactionItem playerCharge = new TransactionItem();
playerCharge.setEvent(event);
playerCharge.setAmount(event.getCost().divide(BigDecimal.valueOf(-accountsToCharge.size()), 2, RoundingMode.HALF_UP));
playerCharge.setUserAccount(accountsByAccountName.get(fieldServiceAccountName));
playerCharge.setTransaction(transaction);
transaction.getTransactionItems().add(playerCharge);
});
BigDecimal vaultValue = transaction
.getTransactionItems()
.stream()
.map(TransactionItem::getAmount)
.map(BigDecimal::negate)
.reduce(BigDecimal.ZERO, BigDecimal::add);
if (!vaultValue.equals(BigDecimal.ZERO)) {
TransactionItem vaultItem = new TransactionItem();
vaultItem.setEvent(event);
vaultItem.setAmount(vaultValue);
vaultItem.setUserAccount(accountsByAccountName.get(vaultAccountName));
vaultItem.setTransaction(transaction);
transaction.getTransactionItems().add(vaultItem);
}
transactionRepository.save(transaction);
} }
return eventRepository.findById(event.getId()); return eventRepository.findById(event.getId());
@@ -1,11 +1 @@
id;type;date;comment id;type;date;comment
1;MATCH;2024-11-12;hope gad outside
2;FIELDPAYMENT;2024-11-12;fibre
3;INTERNALTRANSFER;2024-11-12;finger
4;MATCH;2024-11-12;fair victorious sniff
5;MATCH;2024-11-12;yuck
6;PURCHASE;2024-11-13;correctly
7;INTERNALTRANSFER;2024-11-12;behind gladly alongside
8;FIELDPAYMENT;2024-11-13;negative
9;FIELDPAYMENT;2024-11-13;unlucky
10;FIELDPAYMENT;2024-11-12;see scorpion
1 id type date comment
1 MATCH 2024-11-12 hope gad outside
2 FIELDPAYMENT 2024-11-12 fibre
3 INTERNALTRANSFER 2024-11-12 finger
4 MATCH 2024-11-12 fair victorious sniff
5 MATCH 2024-11-12 yuck
6 PURCHASE 2024-11-13 correctly
7 INTERNALTRANSFER 2024-11-12 behind gladly alongside
8 FIELDPAYMENT 2024-11-13 negative
9 FIELDPAYMENT 2024-11-13 unlucky
10 FIELDPAYMENT 2024-11-12 see scorpion
@@ -1,11 +1,2 @@
id;amount;comment id;amount;comment
1;11521.38;per cautiously out
2;974.75;since below bah
3;25825.62;outlaw
4;862.08;seriously
5;618.2;faithfully past versus
6;3528.31;because cap ah
7;14487.7;beneath festival grandiose
8;24535.73;roughly wonderfully
9;22363.52;extra-large
10;5071.75;tectonics fortunate
1 id amount comment
2
2 974.75 since below bah
3 25825.62 outlaw
4 862.08 seriously
5 618.2 faithfully past versus
6 3528.31 because cap ah
7 14487.7 beneath festival grandiose
8 24535.73 roughly wonderfully
9 22363.52 extra-large
10 5071.75 tectonics fortunate
@@ -1,11 +1,2 @@
id;name id;name
1;since
2;pfft what jam-packed
3;internalise
4;yum indeed for
5;minion gee
6;likewise impressive concerning
7;notwithstanding
8;furthermore
9;diligently kindly
10;buck
1 id name
2
2 pfft what jam-packed
3 internalise
4 yum indeed for
5 minion gee
6 likewise impressive concerning
7 notwithstanding
8 furthermore
9 diligently kindly
10 buck
@@ -3,6 +3,7 @@ export interface IUser {
login?: string; login?: string;
firstName?: string; firstName?: string;
lastName?: string; lastName?: string;
name?: string;
email?: string; email?: string;
activated?: boolean; activated?: boolean;
langKey?: string; langKey?: string;
@@ -20,6 +21,7 @@ export class User implements IUser {
public login?: string, public login?: string,
public firstName?: string, public firstName?: string,
public lastName?: string, public lastName?: string,
public name?: string,
public email?: string, public email?: string,
public activated?: boolean, public activated?: boolean,
public langKey?: string, public langKey?: string,