opposite transaction
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package com.sasiedzi.event.service;
|
||||
|
||||
public enum Account {
|
||||
Boisko,
|
||||
Skarbiec,
|
||||
}
|
||||
@@ -168,8 +168,6 @@ public class EventService {
|
||||
transactionRepository.save(transaction);
|
||||
}
|
||||
if (event.getRegistrations() != null && !event.getRegistrations().isEmpty()) {
|
||||
String fieldServiceAccountName = "Boisko";
|
||||
String vaultAccountName = "Skarbiec";
|
||||
List<UserAccount> accounts = userAccountRepository.findAll();
|
||||
Map<String, UserAccount> accountsByLogin = new HashMap<>();
|
||||
accounts.forEach(acc -> {
|
||||
@@ -182,17 +180,17 @@ public class EventService {
|
||||
Map<String, UserAccount> accountsByAccountName = accounts
|
||||
.stream()
|
||||
.collect(Collectors.toMap(UserAccount::getName, Function.identity(), (existing, replacement) -> existing));
|
||||
if (!accountsByAccountName.containsKey(fieldServiceAccountName)) {
|
||||
if (!accountsByAccountName.containsKey(Account.Boisko.name())) {
|
||||
UserAccount entity = new UserAccount();
|
||||
entity.setName(fieldServiceAccountName);
|
||||
entity.setName(Account.Boisko.name());
|
||||
entity = userAccountRepository.save(entity);
|
||||
accountsByAccountName.put(fieldServiceAccountName, entity);
|
||||
accountsByAccountName.put(Account.Boisko.name(), entity);
|
||||
}
|
||||
if (!accountsByAccountName.containsKey(vaultAccountName)) {
|
||||
if (!accountsByAccountName.containsKey(Account.Skarbiec.name())) {
|
||||
UserAccount entity = new UserAccount();
|
||||
entity.setName(vaultAccountName);
|
||||
entity.setName(Account.Skarbiec.name());
|
||||
entity = userAccountRepository.save(entity);
|
||||
accountsByAccountName.put(vaultAccountName, entity);
|
||||
accountsByAccountName.put(Account.Skarbiec.name(), entity);
|
||||
}
|
||||
List<UserAccount> accountsToCharge = new ArrayList<>();
|
||||
event
|
||||
@@ -209,7 +207,7 @@ public class EventService {
|
||||
TransactionItem fieldServiceItem = new TransactionItem();
|
||||
fieldServiceItem.setEvent(event);
|
||||
fieldServiceItem.setAmount(event.getCost());
|
||||
fieldServiceItem.setUserAccount(accountsByAccountName.get(fieldServiceAccountName));
|
||||
fieldServiceItem.setUserAccount(accountsByAccountName.get(Account.Boisko.name()));
|
||||
fieldServiceItem.setTransaction(transaction);
|
||||
transaction.getTransactionItems().add(fieldServiceItem);
|
||||
accountsToCharge.forEach(userAccount -> {
|
||||
@@ -230,7 +228,7 @@ public class EventService {
|
||||
TransactionItem vaultItem = new TransactionItem();
|
||||
vaultItem.setEvent(event);
|
||||
vaultItem.setAmount(vaultValue);
|
||||
vaultItem.setUserAccount(accountsByAccountName.get(vaultAccountName));
|
||||
vaultItem.setUserAccount(accountsByAccountName.get(Account.Skarbiec.name()));
|
||||
vaultItem.setTransaction(transaction);
|
||||
transaction.getTransactionItems().add(vaultItem);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
package com.sasiedzi.event.service;
|
||||
|
||||
import com.sasiedzi.event.domain.Transaction;
|
||||
import com.sasiedzi.event.domain.TransactionItem;
|
||||
import com.sasiedzi.event.domain.UserAccount;
|
||||
import com.sasiedzi.event.domain.enumeration.TransactionType;
|
||||
import com.sasiedzi.event.repository.TransactionRepository;
|
||||
import com.sasiedzi.event.repository.UserAccountRepository;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class TransactionService {
|
||||
|
||||
@Autowired
|
||||
TransactionRepository transactionRepository;
|
||||
|
||||
@Autowired
|
||||
UserAccountRepository userAccountRepository;
|
||||
|
||||
public Transaction save(Transaction transaction) {
|
||||
Set<TransactionItem> items = transaction.getTransactionItems();
|
||||
Set<TransactionItem> vaults = items
|
||||
.stream()
|
||||
.filter(item -> Account.Skarbiec.name().equals(item.getUserAccount().getName()))
|
||||
.collect(Collectors.toSet());
|
||||
Set<TransactionItem> nonVaults = items
|
||||
.stream()
|
||||
.filter(item -> !Account.Skarbiec.name().equals(item.getUserAccount().getName()))
|
||||
.collect(Collectors.toSet());
|
||||
BigDecimal vaultValue = nonVaults
|
||||
.stream()
|
||||
.map(TransactionItem::getAmount)
|
||||
.map(BigDecimal::negate)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
boolean vaultIsNeeded = vaultValue.compareTo(BigDecimal.ZERO) != 0;
|
||||
|
||||
HashSet<TransactionItem> itemsToBeRemoved = new HashSet<>();
|
||||
|
||||
if (vaultIsNeeded) {
|
||||
if (vaults.isEmpty()) {
|
||||
TransactionItem transactionItem = new TransactionItem();
|
||||
UserAccount vaultAccount = userAccountRepository
|
||||
.findAll()
|
||||
.stream()
|
||||
.filter(item -> Account.Skarbiec.name().equals(item.getName()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
transactionItem.setUserAccount(vaultAccount);
|
||||
transactionItem.setAmount(vaultValue);
|
||||
transactionItem.setTransaction(transaction);
|
||||
transaction.getTransactionItems().add(transactionItem);
|
||||
} else {
|
||||
TransactionItem first = null;
|
||||
for (TransactionItem item : vaults) {
|
||||
if (first == null) {
|
||||
first = item;
|
||||
} else {
|
||||
itemsToBeRemoved.add(item);
|
||||
}
|
||||
}
|
||||
first.setAmount(vaultValue);
|
||||
}
|
||||
} else {
|
||||
itemsToBeRemoved.addAll(vaults);
|
||||
}
|
||||
|
||||
itemsToBeRemoved.forEach(transactionItem -> {
|
||||
transaction.getTransactionItems().remove(transactionItem);
|
||||
});
|
||||
|
||||
if (transaction.getDate() == null) transaction.setDate(LocalDate.now());
|
||||
return transactionRepository.save(transaction);
|
||||
}
|
||||
|
||||
public Transaction createOpposite(Long id) {
|
||||
Transaction existing = transactionRepository.findOneWithEagerRelationships(id).orElseGet(Transaction::new);
|
||||
Transaction newTransaction = new Transaction();
|
||||
newTransaction.setType(TransactionType.INTERNALTRANSFER);
|
||||
newTransaction.setEvent(existing.getEvent());
|
||||
for (TransactionItem item : existing.getTransactionItems()) {
|
||||
TransactionItem newItem = new TransactionItem();
|
||||
newItem.setAmount(item.getAmount().negate());
|
||||
newItem.setTransaction(newTransaction);
|
||||
newItem.setUserAccount(item.getUserAccount());
|
||||
newItem.setEvent(item.getEvent());
|
||||
newItem.setRegistration(item.getRegistration());
|
||||
newItem.setComment(item.getComment());
|
||||
newTransaction.getTransactionItems().add(newItem);
|
||||
}
|
||||
return newTransaction;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.sasiedzi.event.web.rest;
|
||||
import com.sasiedzi.event.domain.*;
|
||||
import com.sasiedzi.event.repository.TransactionRepository;
|
||||
import com.sasiedzi.event.security.AuthoritiesConstants;
|
||||
import com.sasiedzi.event.service.TransactionService;
|
||||
import com.sasiedzi.event.web.rest.errors.BadRequestAlertException;
|
||||
import java.math.BigDecimal;
|
||||
import java.net.URI;
|
||||
@@ -11,6 +12,7 @@ import java.util.*;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.annotation.Secured;
|
||||
@@ -54,7 +56,7 @@ public class TransactionResource {
|
||||
if (transaction.getId() != null) {
|
||||
throw new BadRequestAlertException("A new transaction cannot already have an ID", ENTITY_NAME, "idexists");
|
||||
}
|
||||
transaction = transactionRepository.save(transaction);
|
||||
transaction = transactionService.save(transaction);
|
||||
return ResponseEntity.created(new URI("/api/transactions/" + transaction.getId()))
|
||||
.headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, transaction.getId().toString()))
|
||||
.body(transaction);
|
||||
@@ -88,7 +90,7 @@ public class TransactionResource {
|
||||
throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
|
||||
}
|
||||
|
||||
transaction = transactionRepository.save(transaction);
|
||||
transaction = transactionService.save(transaction);
|
||||
return ResponseEntity.ok()
|
||||
.headers(HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, transaction.getId().toString()))
|
||||
.body(transaction);
|
||||
@@ -279,6 +281,21 @@ public class TransactionResource {
|
||||
return ResponseUtil.wrapOrNotFound(transaction);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
TransactionService transactionService;
|
||||
|
||||
/**
|
||||
* {@code GET /transactions/:id} : get the "id" transaction.
|
||||
*
|
||||
* @param id the id of the transaction to retrieve.
|
||||
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the transaction, or with status {@code 404 (Not Found)}.
|
||||
*/
|
||||
@GetMapping("/opposing/{id}")
|
||||
public Transaction getTransactionOpposite(@PathVariable("id") Long id) {
|
||||
LOG.debug("REST request to get Transaction : {}", id);
|
||||
return transactionService.createOpposite(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code DELETE /transactions/:id} : delete the "id" transaction.
|
||||
*
|
||||
|
||||
@@ -44,6 +44,17 @@
|
||||
<font-awesome-icon icon="pencil-alt"></font-awesome-icon> <span>Edit</span>
|
||||
</button>
|
||||
</router-link>
|
||||
<router-link
|
||||
v-if="transaction.id"
|
||||
:to="{ name: 'TransactionCreateOpposite', params: { opposingTransactionId: transaction.id } }"
|
||||
custom
|
||||
v-slot="{ navigate }"
|
||||
>
|
||||
<button @click="navigate" class="btn btn-primary" v-if="hasAnyAuthority('ROLE_ADMIN')">
|
||||
<font-awesome-icon icon="pencil-alt"></font-awesome-icon> <span>Create opposite transaction</span>
|
||||
</button>
|
||||
</router-link>
|
||||
<!-- <router-link :to="{ name: 'TransactionCreateOpposite', params: { opposingTransactionId: transaction.id } }">Create opposite transaction</router-link>-->
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div class="table-responsive">
|
||||
|
||||
@@ -38,14 +38,14 @@ export default defineComponent({
|
||||
|
||||
const previousState = () => router.go(-1);
|
||||
|
||||
const retrieveTransaction = async transactionId => {
|
||||
const retrieveTransaction = async (transactionId, opposing) => {
|
||||
try {
|
||||
if (dictUserAccounts.value.length == 0) {
|
||||
const res2 = await userAccountService().retrieve();
|
||||
console.log('got accounts' + res2);
|
||||
dictUserAccounts.value = res2.data;
|
||||
}
|
||||
const res = await transactionService().find(transactionId);
|
||||
const res = await transactionService().find(transactionId, opposing);
|
||||
transaction.value = res;
|
||||
// console.log('len'+transaction.value.transactionItems?.length);
|
||||
// console.log('len'+dictUserAccounts.value.length);
|
||||
@@ -76,7 +76,12 @@ export default defineComponent({
|
||||
retrieveUserAccounts();
|
||||
|
||||
if (route.params?.transactionId) {
|
||||
retrieveTransaction(route.params.transactionId);
|
||||
retrieveTransaction(route.params.transactionId, false);
|
||||
}
|
||||
|
||||
if (route.params?.opposingTransactionId) {
|
||||
console.log('getting opposite transaction for ' + route.params?.opposingTransactionId);
|
||||
retrieveTransaction(route.params.opposingTransactionId, true);
|
||||
}
|
||||
|
||||
const initRelationships = () => {
|
||||
|
||||
@@ -5,10 +5,14 @@ import { type ITransaction } from '@/shared/model/transaction.model';
|
||||
const baseApiUrl = 'api/transactions';
|
||||
|
||||
export default class TransactionService {
|
||||
public find(id: number): Promise<ITransaction> {
|
||||
public find(id: number, opposing: boolean): Promise<ITransaction> {
|
||||
return new Promise<ITransaction>((resolve, reject) => {
|
||||
let url = `${baseApiUrl}/${id}`;
|
||||
if (opposing) {
|
||||
url = `${baseApiUrl}/opposing/${id}`;
|
||||
}
|
||||
axios
|
||||
.get(`${baseApiUrl}/${id}`)
|
||||
.get(url)
|
||||
.then(res => {
|
||||
resolve(res.data);
|
||||
})
|
||||
|
||||
@@ -120,6 +120,12 @@ export default {
|
||||
component: TransactionUpdate,
|
||||
meta: { authorities: [Authority.ADMIN] },
|
||||
},
|
||||
{
|
||||
path: 'transaction/opposing/:opposingTransactionId/new',
|
||||
name: 'TransactionCreateOpposite',
|
||||
component: TransactionUpdate,
|
||||
meta: { authorities: [Authority.ADMIN] },
|
||||
},
|
||||
{
|
||||
path: 'transaction/:transactionId/edit',
|
||||
name: 'TransactionEdit',
|
||||
|
||||
Reference in New Issue
Block a user