działąjące rozliczenie
This commit is contained in:
@@ -38,7 +38,7 @@ public class Transaction implements Serializable {
|
||||
@JsonIgnoreProperties(value = { "registrations", "transactions" }, allowSetters = true)
|
||||
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)
|
||||
private Set<TransactionItem> transactionItems = new HashSet<>();
|
||||
|
||||
|
||||
@@ -170,4 +170,20 @@ public class User extends AbstractAuditingEntity<String> implements Serializable
|
||||
", 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")
|
||||
private String name;
|
||||
|
||||
@ManyToMany(fetch = FetchType.LAZY)
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(
|
||||
name = "rel_user_account__user",
|
||||
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")
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
package com.sasiedzi.event.service;
|
||||
|
||||
import com.sasiedzi.event.domain.Event;
|
||||
import com.sasiedzi.event.domain.Transaction;
|
||||
import com.sasiedzi.event.domain.*;
|
||||
import com.sasiedzi.event.domain.enumeration.TransactionType;
|
||||
import com.sasiedzi.event.repository.EventRepository;
|
||||
import com.sasiedzi.event.repository.TransactionRepository;
|
||||
import com.sasiedzi.event.repository.UserAccountRepository;
|
||||
import com.sasiedzi.event.web.rest.TransactionResource;
|
||||
import jakarta.validation.Valid;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -123,9 +125,16 @@ public class EventService {
|
||||
@Autowired
|
||||
TransactionRepository transactionRepository;
|
||||
|
||||
@Autowired
|
||||
UserAccountRepository userAccountRepository;
|
||||
|
||||
public Optional<Event> settle(@Valid Event event) {
|
||||
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;
|
||||
if (allTransactions.isEmpty()) {
|
||||
@@ -133,9 +142,85 @@ public class EventService {
|
||||
transaction.setEvent(event);
|
||||
transaction.setType(TransactionType.MATCH);
|
||||
transaction.setDate(LocalDate.now());
|
||||
transaction.setTransactionItems(new HashSet<>());
|
||||
transactionRepository.save(transaction);
|
||||
} else {
|
||||
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());
|
||||
|
||||
@@ -1,11 +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
|
||||
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,11 +1,2 @@
|
||||
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
|
||||
|
||||
|
||||
|
@@ -3,6 +3,7 @@ export interface IUser {
|
||||
login?: string;
|
||||
firstName?: string;
|
||||
lastName?: string;
|
||||
name?: string;
|
||||
email?: string;
|
||||
activated?: boolean;
|
||||
langKey?: string;
|
||||
@@ -20,6 +21,7 @@ export class User implements IUser {
|
||||
public login?: string,
|
||||
public firstName?: string,
|
||||
public lastName?: string,
|
||||
public name?: string,
|
||||
public email?: string,
|
||||
public activated?: boolean,
|
||||
public langKey?: string,
|
||||
|
||||
Reference in New Issue
Block a user