94 lines
3.3 KiB
Java
94 lines
3.3 KiB
Java
package service;
|
|
|
|
import model.*;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.util.ArrayList;
|
|
import java.time.LocalDate;
|
|
import java.util.List;
|
|
|
|
public class Ledger {
|
|
private final ArrayList<LedgerEntry> entries = new ArrayList<>();
|
|
|
|
public void post(Operation operation) {
|
|
|
|
if (operation == null) {
|
|
throw new IllegalArgumentException("Не указан документ");
|
|
}
|
|
|
|
OperationType operationType = operation.getType();
|
|
|
|
switch (operationType) {
|
|
case OPENING_BALANCE:
|
|
postOpeningBalance(operation);
|
|
break;
|
|
case OUTFLOW:
|
|
postOutflow(operation);
|
|
break;
|
|
case INFLOW:
|
|
//do something
|
|
break;
|
|
case MOVEMENT:
|
|
postMovement(operation);
|
|
break;
|
|
default:
|
|
//do something
|
|
}
|
|
}
|
|
|
|
private void postOutflow(Operation operation) {
|
|
|
|
if (operation.getLines().isEmpty()) {
|
|
throw new IllegalArgumentException("Документ расхода не содержит строк");
|
|
}
|
|
ArrayList<LedgerEntry> newEntries = new ArrayList<>();
|
|
List<OperationLine> operationLines = operation.getLines();
|
|
for (OperationLine operationLine : operationLines) {
|
|
LedgerEntry ledgerEntry = new LedgerEntry(operation.getDate(), operation.getId(),
|
|
operation.getSourceAccount(), operationLine.getCategory(), operationLine.getAmount().negate());
|
|
newEntries.add(ledgerEntry);
|
|
}
|
|
entries.removeIf(entry -> entry.getOperationId().equals(operation.getId()));
|
|
entries.addAll(newEntries);
|
|
}
|
|
|
|
private void postOpeningBalance(Operation operation) {
|
|
|
|
ArrayList<LedgerEntry> newEntries = new ArrayList<>();
|
|
LedgerEntry ledgerEntry = new LedgerEntry(operation.getDate(), operation.getId(),
|
|
operation.getDestinationAccount(), null, operation.getTotal());
|
|
newEntries.add(ledgerEntry);
|
|
entries.removeIf(entry -> entry.getOperationId().equals(operation.getId()));
|
|
entries.addAll(newEntries);
|
|
}
|
|
|
|
private void postMovement(Operation operation) {
|
|
|
|
ArrayList<LedgerEntry> newEntries = new ArrayList<>();
|
|
LedgerEntry ledgerEntryExpense = new LedgerEntry(operation.getDate(), operation.getId(),
|
|
operation.getSourceAccount(), null, operation.getTotal().negate());
|
|
newEntries.add(ledgerEntryExpense);
|
|
LedgerEntry ledgerEntryIncome = new LedgerEntry(operation.getDate(), operation.getId(),
|
|
operation.getDestinationAccount(), null, operation.getTotal());
|
|
newEntries.add(ledgerEntryIncome);
|
|
|
|
entries.removeIf(entry -> entry.getOperationId().equals(operation.getId()));
|
|
entries.addAll(newEntries);
|
|
}
|
|
|
|
public BigDecimal balance(Account account, LocalDate date) {
|
|
|
|
BigDecimal result = BigDecimal.ZERO;
|
|
|
|
for (LedgerEntry entry : entries) {
|
|
boolean sameAccount = entry.getAccount().equals(account);
|
|
boolean withinPeriod = !entry.getDate().isAfter(date);
|
|
|
|
if (sameAccount && withinPeriod) {
|
|
result = result.add(entry.getDeltaAmount());
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|