Line data Source code
1 : /********************************************************************\
2 : * SX-ttinfo.h -- Template Transaction manipulation functions *
3 : * for scheduled transactions *
4 : * Copyright (C) 2001 Robert Merkel <rgmerk@mira.net> *
5 : * *
6 : * This program is free software; you can redistribute it and/or *
7 : * modify it under the terms of the GNU General Public License as *
8 : * published by the Free Software Foundation; either version 2 of *
9 : * the License, or (at your option) any later version. *
10 : * *
11 : * This program is distributed in the hope that it will be useful, *
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 : * GNU General Public License for more details. *
15 : * *
16 : * You should have received a copy of the GNU General Public License*
17 : * along with this program; if not, contact: *
18 : * *
19 : * Free Software Foundation Voice: +1-617-542-5942 *
20 : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21 : * Boston, MA 02110-1301, USA gnu@gnu.org *
22 : * *
23 : \********************************************************************/
24 :
25 : #ifndef GNC_SX_TTINFO_H
26 : #define GNC_SX_TTINFO_H
27 :
28 : #include <glib.h>
29 : #include "qof.h"
30 : #include "SchedXaction.h"
31 : #include "Account.h"
32 : #include "gnc-commodity.h"
33 : #include "gnc-numeric.hpp"
34 :
35 : #include <vector>
36 : #include <optional>
37 : #include <string>
38 : #include <algorithm>
39 : #include <memory>
40 :
41 : struct OptionalString
42 : {
43 44 : const char* operator* () const { return m_str ? m_str->c_str() : nullptr; };
44 0 : void operator= (const char* str) { if (str) m_str = str; else reset(); };
45 0 : void reset () { m_str = std::nullopt; };
46 : protected:
47 : std::optional<std::string> m_str;
48 : };
49 :
50 : struct OptionalStringFromNumeric : public OptionalString
51 : {
52 : using OptionalString::operator=;
53 : void operator= (std::optional<gnc_numeric> num)
54 : { if (num) m_str = GncNumeric (*num).to_string(); else reset(); }
55 : };
56 :
57 : struct TTSplitInfo
58 : {
59 : OptionalString m_action;
60 : OptionalString m_memo;
61 : OptionalStringFromNumeric m_credit_formula, m_debit_formula;
62 : Account *m_acc = nullptr;
63 :
64 8 : const char* get_action () const { return *m_action; };
65 8 : const char* get_memo () const { return *m_memo; };
66 8 : const Account* get_account () const { return m_acc; };
67 8 : const char* get_credit_formula () const { return *m_credit_formula; };
68 8 : const char* get_debit_formula () const { return *m_debit_formula; };
69 :
70 0 : void set_action (const char *action) { m_action = action; };
71 0 : void set_memo (const char *memo) { m_memo = memo; };
72 0 : void set_account (Account *acc) { m_acc = acc; };
73 0 : void set_credit_formula (const char *credit_formula) { set_formulas (nullptr, credit_formula); };
74 0 : void set_debit_formula (const char *debit_formula) { set_formulas (debit_formula, nullptr); };
75 : void set_credit_formula_numeric (gnc_numeric num) { set_formulas_numeric ({}, num); };
76 : void set_debit_formula_numeric (gnc_numeric num) { set_formulas_numeric (num, {}); };
77 :
78 : private:
79 0 : void set_formulas (const char* debit, const char *credit)
80 : {
81 0 : m_debit_formula = debit;
82 0 : m_credit_formula = credit;
83 0 : }
84 : void set_formulas_numeric (std::optional<gnc_numeric> debit, std::optional<gnc_numeric> credit)
85 : {
86 : m_debit_formula = debit;
87 : m_credit_formula = credit;
88 : }
89 : };
90 :
91 : using TTSplitInfoPtr = std::shared_ptr<TTSplitInfo>;
92 : using TTSplitInfoVec = std::vector<TTSplitInfoPtr>;
93 :
94 : struct TTInfo
95 : {
96 : OptionalString m_description;
97 : OptionalString m_num;
98 : OptionalString m_notes;
99 : gnc_commodity *m_common_currency = nullptr;
100 : TTSplitInfoVec m_splits;
101 :
102 4 : const char* get_description () const { return *m_description; };
103 4 : const char* get_num () const { return *m_num; };
104 4 : const char* get_notes () const { return *m_notes; };
105 4 : gnc_commodity* get_currency () const { return m_common_currency; };
106 4 : const TTSplitInfoVec& get_template_splits () const { return m_splits; };
107 :
108 0 : void set_description (const char *description) { m_description = description; };
109 0 : void set_num (const char *num) { m_num = num; };
110 0 : void set_notes (const char *notes) { m_notes = notes; };
111 0 : void set_currency (gnc_commodity *currency) { m_common_currency = currency; };
112 0 : void clear_template_splits () { m_splits.clear(); };
113 0 : void append_template_split (TTSplitInfoPtr& ttsi) { m_splits.push_back (ttsi); };
114 : ;
115 : };
116 :
117 : using TTInfoPtr = std::shared_ptr<TTInfo>;
118 : using TTInfoVec = std::vector<TTInfoPtr>;
119 :
120 : #endif
|