FOSSredder 0.5.0
Loading...
Searching...
No Matches
DraftFinalizer.cpp
Go to the documentation of this file.
1
5
7
14
15#include <charconv>
16#include <string_view>
17
18namespace {
19
20inline constexpr std::string_view kDefaultImportedStatementName = "Imported";
21inline constexpr std::string_view kGeneratedContractPrefix = "Vertrag ";
22
23int nextGeneratedContractIndex(const std::vector<std::shared_ptr<Contract>>& contracts)
24{
25 int maxIdx = 0;
26 constexpr auto prefix = kGeneratedContractPrefix;
27 for (const auto& contractPtr : contracts) {
28 if (!contractPtr) continue;
29 const std::string& contractName = contractPtr->name();
30 if (contractName.size() <= prefix.size() || contractName.rfind(prefix.data(), 0) != 0) continue;
31 const std::string rest = contractName.substr(prefix.size());
32 int idx = 0;
33 const auto result = std::from_chars(rest.data(), rest.data() + rest.size(), idx);
34 if (result.ec == std::errc{} && result.ptr == rest.data() + rest.size())
35 maxIdx = std::max(maxIdx, idx);
36 }
37 return maxIdx + 1;
38}
39
40}
41
43
45{
46 if (draft.transactions.empty()) return {};
47
48 auto contracts = state.contracts();
49 auto transactions = state.transactions();
50 auto statements = state.statements();
51
52 auto statement = std::make_shared<Statement>();
54 statement->rename(::core::utils::trim(draft.name).empty()
55 ? std::string(kDefaultImportedStatementName)
56 : draft.name);
57
58 std::size_t addedTransactions = 0;
59
60 for (const auto& item : draft.transactions) {
61 auto transaction = std::make_shared<Transaction>();
63 transaction->setName(item.name);
64 transaction->setBookingDate(item.bookingDate);
65 transaction->setValuta(item.valuta);
66 transaction->setAmount(item.amount);
67 transaction->setStatementId(statement->id());
68 transaction->setStatus(item.status);
69 transaction->setActorId(item.actorId);
70 transaction->setContractId(item.contractId);
71 transaction->setAllocatable(item.allocatable);
72 transaction->setPropertyIds(item.propertyIds);
74 transaction->amount(),
75 transaction->statementId())) {
76 continue;
77 }
78 const std::string normalizedContractId = core::domain::policies::transaction::trimCopy(item.contractId);
79 if (!normalizedContractId.empty()) {
80 transaction->setContractId(normalizedContractId);
81 } else {
82 const std::string normalizedType = core::domain::policies::transaction::trimCopy(item.type);
83 if (!normalizedType.empty()) {
84 constexpr auto prefix = kGeneratedContractPrefix;
85 auto contract = std::make_shared<Contract>();
86 contract->setId(core::utils::makeStableId());
87 contract->rename(std::string(prefix) + std::to_string(nextGeneratedContractIndex(contracts)));
88 contract->setType(normalizedType);
89 contract->setPropertyIds(item.propertyIds);
90 contracts.push_back(contract);
91 transaction->setContractId(contract->id());
92 }
93 }
94
95 transactions.push_back(transaction);
96 statement->addTransaction(transaction->id());
97 ++addedTransactions;
98 }
99
100 if (addedTransactions == 0) {
101 return {};
102 }
103
104 statements.push_back(statement);
105 state.setContracts(std::move(contracts));
106 state.setTransactions(std::move(transactions));
107 state.setStatements(std::move(statements));
108 return statement->id();
109}
110
111}
Domain model for contracts.
Declares draft finalization helpers that materialize imported entities.
Declares private stable identifier helpers for core entity creation.
Domain model for statements.
Declares private string helpers shared across core implementation files.
Domain model for transactions.
Shared transaction transition and normalization helpers.
static std::string finalize(core::domain::catalog::WorkspaceCatalog &state, const core::application::importing::draft::StatementDraft &draft)
Finalize a statement draft and append the resulting entities to the workspace state.
void setStatements(StatementList value)
Replaces all statements in the catalog.
const StatementList & statements() const noexcept
Returns all statements in the catalog.
const TransactionList & transactions() const noexcept
Returns all transactions in the catalog.
void setContracts(ContractList value)
Replaces all contracts in the catalog.
const ContractList & contracts() const noexcept
Returns all contracts in the catalog.
void setTransactions(TransactionList value)
Replaces all transactions in the catalog.
bool canFinalizeFromDraft(const std::string &bookingDate, double amount, const std::string &statementId)
Checks whether a draft transaction can be finalized.
std::string trimCopy(std::string value)
Trims leading and trailing whitespace from text.
std::string makeStableId()
Generates a stable hex identifier for persisted entities.
Definition StableId.cpp:21
std::string trim(std::string s)
Removes leading and trailing ASCII whitespace from a string.