teditor  1.8.0@@fee5e94
Terminal based editor written in C++
ledger/objects.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <utility>
4 #include <set>
5 #include <string>
6 #include <vector>
7 #include <stdio.h>
8 #include <stdint.h>
9 #include "core/time_utils.h"
10 
11 namespace teditor {
12 namespace ledger {
13 
15 typedef std::set<std::string> Aliases;
16 
18 class Account {
19 public:
26  Account(const std::string& n, const std::string& d="",
27  const Aliases& a=Aliases()): nam(n), descr(d), alias(a), bal(0) {}
28 
29  const std::string& name() const { return nam; }
30  const std::string& desc() const { return descr; }
31  int64_t rawBalance() const { return bal; }
32  double balance() const { return rawBalance() / 100.0; }
33 
34  bool isAnAlias(const std::string& a) const {
35  return alias.find(a) != alias.end();
36  }
37  void insertAlias(const std::string& a) { alias.insert(a); }
38 
39  Account& operator+=(double val) {
40  bal += (int64_t)(val * 100.0);
41  return *this;
42  }
43  Account& operator+=(int64_t val) { bal += val; return *this; }
44 
45 private:
46  std::string nam, descr;
47  Aliases alias;
48  int64_t bal;
49 };
50 
52 class Accounts: public std::vector<Account> {
53 public:
54  Account& find(const std::string& a) {
55  for(size_t i=0;i<size();++i) {
56  auto& itr = at(i);
57  if(itr.name() == a) return itr;
58  if(itr.isAnAlias(a)) return itr;
59  }
60  push_back(Account(a));
61  return back();
62  }
63 };
64 
66 typedef std::pair<std::string, int64_t> AccInfo;
67 
69 typedef std::vector<AccInfo> AccountsInfo;
70 
72 class Transaction {
73 public:
74  Transaction(const std::string& dateStr, const std::string& n):
75  nam(n), dat(timeFromStr(dateStr)), accts() {}
76 
77  void add(const std::string& acct, double val) {
78  accts.push_back(AccInfo(acct, int64_t(val * 100.0)));
79  }
80 
82  void add(const std::string& acct) {
83  int64_t val = 0;
84  for(const auto& a : accts) val += a.second;
85  accts.push_back(AccInfo(acct, -val));
86  }
87 
88  void updateAccounts(Accounts& acc) {
89  for(const auto& a : accts) acc.find(a.first) += a.second;
90  }
91 
92  const std::string& name() const { return nam; }
93  const TimePoint& date() const { return dat; }
94 
96  int64_t rawBalance() const {
97  int64_t val = 0;
98  for(const auto& a : accts) val += a.second;
99  return val;
100  }
101 
102  double balance() const { return rawBalance() / 100.0; }
103 
104 private:
105  std::string nam;
106  TimePoint dat;
107  AccountsInfo accts;
108 };
109 
110 typedef std::vector<Transaction> Transactions;
111 
112 } // end namespace ledger
113 } // end namespace teditor
teditor::todo::RepeatType
RepeatType
Definition: todo/objects.h:10
teditor::ledger::Account::isAnAlias
bool isAnAlias(const std::string &a) const
Definition: ledger/objects.h:34
teditor::ledger::Transaction::name
const std::string & name() const
Definition: ledger/objects.h:92
teditor::todo::Repeat_None
@ Repeat_None
Definition: todo/objects.h:11
teditor::ledger::Transaction::balance
double balance() const
Definition: ledger/objects.h:102
teditor::todo::MatchItem::pt
TimePoint pt
Definition: todo/objects.h:38
teditor::todo::CalendarItem::hasStart
bool hasStart
Definition: todo/objects.h:22
teditor::ledger::Transaction::Transaction
Transaction(const std::string &dateStr, const std::string &n)
Definition: ledger/objects.h:74
teditor::ledger::Account
Definition: ledger/objects.h:18
teditor::todo::CalendarItem::description
std::string description
Definition: todo/objects.h:26
teditor::todo::CalendarItems
std::vector< CalendarItem > CalendarItems
Definition: todo/objects.h:41
teditor::todo::CalendarMatches
std::vector< MatchItem > CalendarMatches
Definition: todo/objects.h:42
teditor::todo::CalendarItem::CalendarItem
CalendarItem()
Definition: todo/objects.h:28
teditor::todo::Repeat_Yearly
@ Repeat_Yearly
Definition: todo/objects.h:12
teditor::ledger::Account::desc
const std::string & desc() const
Definition: ledger/objects.h:30
teditor::ledger::Account::insertAlias
void insertAlias(const std::string &a)
Definition: ledger/objects.h:37
teditor::ledger::Transaction::add
void add(const std::string &acct)
Definition: ledger/objects.h:82
teditor::todo::compareMatches
bool compareMatches(const MatchItem &a, const MatchItem &b)
Definition: objects.cpp:48
objects.h
teditor::ledger::Transaction
Definition: ledger/objects.h:72
teditor::ledger::Account::Account
Account(const std::string &n, const std::string &d="", const Aliases &a=Aliases())
ctor
Definition: ledger/objects.h:26
teditor::todo::strToRepeatType
RepeatType strToRepeatType(const std::string &str)
Definition: objects.cpp:11
utils.h
teditor::ledger::Account::name
const std::string & name() const
Definition: ledger/objects.h:29
teditor::todo::Repeat_Weekly
@ Repeat_Weekly
Definition: todo/objects.h:14
teditor::todo::MatchItem
Definition: todo/objects.h:36
teditor::ledger::AccountsInfo
std::vector< AccInfo > AccountsInfo
Definition: ledger/objects.h:69
teditor::ledger::Account::operator+=
Account & operator+=(int64_t val)
Definition: ledger/objects.h:43
teditor::addYear
void addYear(TimePoint &pt)
Definition: time_utils.cpp:106
teditor::ledger::Transaction::add
void add(const std::string &acct, double val)
Definition: ledger/objects.h:77
ASSERT
#define ASSERT(check, fmt,...)
Macro to assert with runtime_error exception if the check fails.
Definition: utils.h:35
teditor::ledger::Accounts::find
Account & find(const std::string &a)
Definition: ledger/objects.h:54
teditor::todo::CalendarItem::getNextOccurence
TimePoint getNextOccurence(const TimePoint &pt) const
Definition: objects.cpp:38
teditor::todo::Repeat_Monthly
@ Repeat_Monthly
Definition: todo/objects.h:13
teditor::todo::CalendarItem::clear
void clear()
Definition: objects.cpp:31
teditor::TimePoint
std::chrono::system_clock::time_point TimePoint
Definition: time_utils.h:7
teditor::todo::CalendarItem::repeat
RepeatType repeat
Definition: todo/objects.h:25
teditor::ledger::Transactions
std::vector< Transaction > Transactions
Definition: ledger/objects.h:110
teditor::ledger::Accounts
Definition: ledger/objects.h:52
time_utils.h
teditor::addWeek
void addWeek(TimePoint &pt, int times)
Definition: time_utils.cpp:92
teditor::timeFromStr
TimePoint timeFromStr(const std::string &dt)
Definition: time_utils.cpp:50
teditor::ledger::Transaction::date
const TimePoint & date() const
Definition: ledger/objects.h:93
teditor::ledger::Account::operator+=
Account & operator+=(double val)
Definition: ledger/objects.h:39
teditor::ledger::Account::balance
double balance() const
Definition: ledger/objects.h:32
teditor::ledger::Transaction::updateAccounts
void updateAccounts(Accounts &acc)
Definition: ledger/objects.h:88
teditor::ledger::Aliases
std::set< std::string > Aliases
Definition: ledger/objects.h:15
teditor::ledger::Account::rawBalance
int64_t rawBalance() const
Definition: ledger/objects.h:31
teditor::todo::Repeat_Daily
@ Repeat_Daily
Definition: todo/objects.h:15
teditor::ledger::AccInfo
std::pair< std::string, int64_t > AccInfo
Definition: ledger/objects.h:66
teditor::todo::findMatchesIn
CalendarMatches findMatchesIn(const CalendarItems &items, const TimePoint &start, const TimePoint &end)
Definition: objects.cpp:54
teditor::addMonth
void addMonth(TimePoint &pt)
Definition: time_utils.cpp:96
teditor::todo::MatchItem::idx
int idx
Definition: todo/objects.h:37
teditor::todo::CalendarItem::hasEnd
bool hasEnd
Definition: todo/objects.h:24
teditor::ledger::Transaction::rawBalance
int64_t rawBalance() const
Definition: ledger/objects.h:96
teditor::addDay
void addDay(TimePoint &pt, int times)
Definition: time_utils.cpp:88
teditor
Definition: any.hpp:10