Finance manager
Лабораторна робота №2 - Документація коду
Loading...
Searching...
No Matches
Transaction.h
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <vector>
4#include <ctime>
5#include <nlohmann/json.hpp>
6
7using json = nlohmann::json;
8
23public:
24 // --- Основні дані ---
25 std::string id;
26 double amount = 0.0;
27 std::time_t date = 0;
28 std::string description;
29 std::string categoryId;
30 std::string accountId;
31
32 // --- Валюта ---
33 std::string currency = "USD";
34 double exchangeRate = 1.0;
35
36 // --- Метадані ---
37 std::vector<std::string> tags;
38 std::string recurringRule;
39 std::string importedSource;
40 std::string note;
41
42 // --- Часові мітки ---
43 std::time_t createdAt = 0;
44 std::time_t updatedAt = 0;
45
46
50 void addTransaction();
51
61 std::vector<Transaction> splitTransaction(const std::vector<std::pair<double, std::string>>& parts);
62
67 void tagTransaction(const std::string& tag);
68
74 void undoTransaction();
75};
76
77// --- Серіалізація nlohmann/json ---
78
88inline void to_json(json& j, const Transaction& t) {
89 j = json{
90 {"id", t.id},
91 {"amount", t.amount},
92 {"date", t.date},
93 {"description", t.description},
94 {"categoryId", t.categoryId},
95 {"accountId", t.accountId},
96 {"currency", t.currency},
97 {"exchangeRate", t.exchangeRate},
98 {"tags", t.tags},
99 {"recurringRule", t.recurringRule},
100 {"importedSource", t.importedSource},
101 {"note", t.note},
102 {"createdAt", t.createdAt},
103 {"updatedAt", t.updatedAt}
104 };
105}
106
118inline void from_json(const json& j, Transaction& t) {
119 t.id = j.value("id", "");
120 t.amount = j.value("amount", 0.0);
121 t.date = j.value("date", 0);
122 t.description = j.value("description", "");
123 t.categoryId = j.value("categoryId", "");
124 t.accountId = j.value("accountId", "");
125 t.currency = j.value("currency", "USD");
126 t.exchangeRate = j.value("exchangeRate", 1.0);
127 t.tags = j.value("tags", std::vector<std::string>{});
128 t.recurringRule = j.value("recurringRule", "");
129 t.importedSource = j.value("importedSource", "");
130 t.note = j.value("note", "");
131 t.createdAt = j.value("createdAt", 0);
132 t.updatedAt = j.value("updatedAt", 0);
133}
134
135inline void Transaction::addTransaction() { createdAt = std::time(nullptr); }
136
137inline std::vector<Transaction> Transaction::splitTransaction(const std::vector<std::pair<double, std::string>>& parts) {
138 std::vector<Transaction> out;
139 for (const auto &p : parts) {
140 Transaction t = *this;
141 t.amount = p.first;
142 t.categoryId = p.second;
143 out.push_back(t);
144 }
145 return out;
146}
147
148inline void Transaction::tagTransaction(const std::string& tag) { tags.push_back(tag); }
149
150inline void Transaction::undoTransaction() { /* placeholder */ }
nlohmann::json json
Definition CurrencyConverter.cpp:8
void from_json(const json &j, Transaction &t)
Десеріалізує JSON в об'єкт Transaction.
Definition Transaction.h:118
void to_json(json &j, const Transaction &t)
Серіалізує об'єкт Transaction у формат JSON.
Definition Transaction.h:88
Представляє одну фінансову операцію (дохід, витрата тощо).
Definition Transaction.h:22
void tagTransaction(const std::string &tag)
Додає тег до транзакції.
Definition Transaction.h:148
std::vector< Transaction > splitTransaction(const std::vector< std::pair< double, std::string > > &parts)
Розділяє одну транзакцію на декілька менших.
Definition Transaction.h:137
double exchangeRate
Обмінний курс відносно базової валюти (якщо не 1.0).
Definition Transaction.h:34
void undoTransaction()
Скасовує транзакцію (наразі заглушка).
Definition Transaction.h:150
std::string recurringRule
Правило повторення (напр., "monthly:5" - щомісяця 5-го числа).
Definition Transaction.h:38
std::string accountId
Ідентифікатор рахунку, з якого/на який було здійснено платіж.
Definition Transaction.h:30
std::time_t updatedAt
Час останнього оновлення запису (Unix timestamp).
Definition Transaction.h:44
std::string id
Унікальний ідентифікатор транзакції.
Definition Transaction.h:25
double amount
Сума транзакції (додатна - дохід, від'ємна - витрата).
Definition Transaction.h:26
std::string categoryId
Ідентифікатор категорії (напр., "food", "rent").
Definition Transaction.h:29
std::vector< std::string > tags
Список тегів для фільтрації (напр., "work", "vacation").
Definition Transaction.h:37
std::time_t createdAt
Час створення запису (Unix timestamp).
Definition Transaction.h:43
std::string currency
Код валюти
Definition Transaction.h:33
std::string description
Короткий опис, наданий користувачем.
Definition Transaction.h:28
std::string note
Додаткова розширена примітка.
Definition Transaction.h:40
std::string importedSource
Джерело імпорту (напр., "CSV:monobank.csv").
Definition Transaction.h:39
void addTransaction()
Встановлює час створення транзакції. (Зараз - заглушка).
Definition Transaction.h:135
std::time_t date
Дата та час транзакції (у форматі Unix timestamp).
Definition Transaction.h:27