74 lines
2.1 KiB
Java
74 lines
2.1 KiB
Java
package service;
|
|
|
|
import model.Account;
|
|
import model.Operation;
|
|
import model.OperationJournal;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.time.LocalDate;
|
|
import java.util.Objects;
|
|
|
|
public class OperationService {
|
|
|
|
private final OperationJournal journal;
|
|
private final Ledger ledger;
|
|
private Long counterId = 0L;
|
|
|
|
public OperationService(OperationJournal journal, Ledger ledger) {
|
|
|
|
this.journal = Objects.requireNonNull(
|
|
journal, "Не указан журнал операций"
|
|
);
|
|
this.ledger = Objects.requireNonNull(
|
|
ledger, "Не указан регистр движений"
|
|
);
|
|
}
|
|
|
|
public synchronized void save(Operation operation) {
|
|
journal.saveOperation(operation);
|
|
}
|
|
|
|
public synchronized void post(Long operationId) {
|
|
post(requiredOperation(operationId));
|
|
}
|
|
|
|
public synchronized void post(Operation operation) {
|
|
ledger.post(operation);
|
|
journal.saveOperation(operation);
|
|
}
|
|
|
|
public synchronized void unpost(Long operationId) {
|
|
ledger.unpost(requiredOperation(operationId).getId());
|
|
}
|
|
|
|
public synchronized boolean isPosted(Operation operation) {
|
|
if (operation == null) {
|
|
throw new IllegalArgumentException("Не указан документ");
|
|
}
|
|
return ledger.isPosted(operation.getId());
|
|
}
|
|
|
|
public synchronized Operation getById(Long operationId) {
|
|
return requiredOperation(operationId);
|
|
}
|
|
|
|
public synchronized Operation createOpeningBalance(LocalDate date, Account account, BigDecimal amount) {
|
|
Operation operation = Operation.openingBalance(date, counterId++, account, amount);
|
|
journal.saveOperation(operation);
|
|
return operation;
|
|
}
|
|
|
|
public synchronized BigDecimal balance(Account account, LocalDate date) {
|
|
return ledger.balance(account, date);
|
|
}
|
|
|
|
private Operation requiredOperation(Long operationId) {
|
|
Operation operation = journal.getOperationById(operationId);
|
|
|
|
if (operation == null) {
|
|
throw new OperationNotFoundException(operationId);
|
|
}
|
|
return operation;
|
|
}
|
|
}
|