GnuCashew ~ Web Application compatible with GnuCash sql data files.
GCW
Loading...
Searching...
No Matches
Splits.h
Go to the documentation of this file.
1#line 2 "src/Dbo/Splits/Splits.h"
2
3#ifndef __DBO_SPLITS_H___
4#define __DBO_SPLITS_H___
5
6#include <Wt/WDate.h>
7
8#include "../../Glb/gcwglobal.h"
9#include "../../GnuCashew.h"
10#include "../BaseItem.h"
11
12/*
13** Predefine the Account class that fixin to come up.
14*/
15namespace GCW {
16 namespace Dbo {
17 namespace Splits {
18 class Item;
19 }
20 }
21}
22
23/*
24** Define these dbo_traits to prevent the system from
25** automatically generating an ID field or a VERSION
26** field, and instead substitute the guid field
27** as the primary key.
28*/
29template<> struct Wt::Dbo::dbo_traits< GCW::Dbo::Splits::Item >
31{
32 using IdType = std::string;
33 static IdType invalidId() { return std::string(); }
34 static const char * surrogateIdField() { return nullptr; }
35 static const char * versionField() { return nullptr; }
36};
37
38template<> struct Wt::Dbo::dbo_traits< const GCW::Dbo::Splits::Item > : Wt::Dbo::dbo_traits< GCW::Dbo::Splits::Item > {};
39
40/*
41** Now we can start building our class!
42*/
43namespace GCW {
44 namespace Dbo {
45 namespace Splits {
46
47/*!
48** \brief Split Item Class
49**
50** This class represents a 'split' within gnucash
51**
52** \code
53**
54** CREATE TABLE splits
55** (
56** guid text(32) PRIMARY KEY NOT NULL,
57** tx_guid text(32) NOT NULL,
58** account_guid text(32) NOT NULL,
59** memo text(2048) NOT NULL,
60** action text(2048) NOT NULL,
61** reconcile_state text(1) NOT NULL,
62** reconcile_date text(19),
63** value_num bigint NOT NULL,
64** value_denom bigint NOT NULL,
65** quantity_num bigint NOT NULL,
66** quantity_denom bigint NOT NULL,
67** lot_guid text(32)
68** );
69** CREATE INDEX splits_tx_guid_index ON splits(tx_guid);
70** CREATE INDEX splits_account_guid_index ON splits(account_guid);
71**
72** sqlite> select * from accounts;
73** guid name account_type commodity_guid scu non parent_guid code description h p
74** 822a857c5f484affa5a6a3e62f4b700f|Checking Account|BANK |10b24d11b4b94b8789d1830da2695bbb|100|0 |b61b07c024fc463489f5db031135a29e| |Checking Account|0|0
75** d934f2e5606c4da6b25c703c4661d747|Opening Balances|EQUITY |10b24d11b4b94b8789d1830da2695bbb|100|0 |745365afce5946b19d4189c593b9f6aa| |Opening Balances|0|0
76** sqlite> select * from transactions;
77** guid currency_guid num post_date enter_date description
78** 4b2259ef3fbb486bad1b42f28ec84346|10b24d11b4b94b8789d1830da2695bbb| |2023-05-28 10:59:00|2023-05-28 21:46:09|
79** sqlite> select * from splits;
80** guid tx_guid account_guid memo action rs reconcile_date value_num value_denom q_num q_den lot_guid
81** c68c279c3ef64d00ae682f5573bb55bc|4b2259ef3fbb486bad1b42f28ec84346|822a857c5f484affa5a6a3e62f4b700f| | |n |1970-01-01 00:00:00|100 |100 |100 |100 |
82** a67a975bc58a4fa1bd00421b37fed115|4b2259ef3fbb486bad1b42f28ec84346|d934f2e5606c4da6b25c703c4661d747| | |n |1970-01-01 00:00:00|-100 |100 |-100 |100 |
83**
84** \endcode
85*/
86class Item
87: public GCW::Dbo::BaseItem< Item >
88{
89 public:
90
91 Item(){}
92
93 Item( const std::string & _splitGuid ): m_guid( _splitGuid ) {}
94
95 /*!
96 ** Split GUID
97 */
98 auto guid() const-> const std::string & { return m_guid; }
99
100 /*!
101 ** Set Split GUID
102 */
103 auto set_guid( const std::string & _guid )-> void { m_guid = _guid; }
104
105 /*!
106 ** Transaction GUID
107 */
108 auto tx_guid() const-> const std::string & { return m_tx_guid; }
109
110 /*!
111 ** Set Transaction GUID
112 */
113 auto set_tx_guid( const std::string & _guid )-> void { m_tx_guid = _guid; }
114
115 /*!
116 ** Account GUID
117 */
118 auto account_guid() const-> const std::string & { return m_account_guid; }
119
120 /*!
121 ** Set Account GUID
122 */
123 auto set_account_guid( const std::string & _guid )-> void { m_account_guid = _guid; }
124
125 /*!
126 ** Memo field
127 */
128 auto memo() const-> const std::string & { return m_memo; }
129
130 /*!
131 ** Set Memo field
132 */
133 auto set_memo( const std::string & _value )-> void { m_memo = _value; }
134
135 /*!
136 ** Action field
137 */
138 auto action() const-> const std::string & { return m_action; }
139
140 /*!
141 ** Set Action
142 */
143 auto set_action( const std::string & _value )-> void { m_action = _value; }
144
145 /*!
146 ** Reconcile State field
147 */
148 auto reconcile_state() const-> const std::string & { return m_reconcile_state; }
149
150 /*!
151 ** Set Reconcile State field
152 */
153 auto set_reconcile_state( const std::string & _state )-> void { m_reconcile_state = _state; }
154
155 /*!
156 ** Return true/false if the split is reconciled or not
157 */
158 auto isReconciled() const-> bool { return m_reconcile_state == GCW_RECONCILE_YES ; }
159
160 /*!
161 ** Reconcile Date field
162 */
163 auto reconcile_date() const-> const std::string & { return m_reconcile_date; }
164
165 /*!
166 ** Set Reconcile Date field
167 */
168 auto set_reconcile_date( const std::string & _date )-> void { m_reconcile_date = _date; }
169
170 /*!
171 ** Value Number field
172 */
173 auto value_num() const-> int { return m_value_num; }
174
175 /*!
176 ** Value Denominator field
177 */
178 auto value_denom() const-> int { return m_value_denom; }
179
180 /*!
181 ** Quantity Number field
182 */
183 auto quantity_num() const-> int { return m_quantity_num; }
184
185 /*!
186 ** Quantity Denominator field
187 */
188 auto quantity_denom() const-> int { return m_quantity_denom; }
189
190 /*!
191 ** Lot GUID field
192 */
193 auto lot_guid() const-> const std::string & { return m_lot_guid; }
194
195 /*!
196 ** \brief Return 'value' as a decimal.h number.
197 **
198 ** This converts the stored number in to a proper decimal<>
199 ** number type. It can then be used in regular accounting
200 ** calculations. The inversion flag can be used to 'reset'
201 ** the sign of the value for the purpose of displaying
202 ** (otherwise negative) numbers with positive values only.
203 */
204 auto value( bool invert = false ) const-> GCW_NUMERIC
205 {
206 auto inv = invert? -1:1;
207 GCW_NUMERIC retVal( value_num()*inv );
208 if( value_denom() > 0 )
209 retVal /= value_denom();
210 return retVal;
211 }
212
213 /*!
214 ** \brief Test for Negative
215 **
216 ** This returns 'true' if the number is considered less
217 ** than zero.
218 */
219 bool valueIsNegative() const
220 {
221 return value() < 0;
222 }
223
224 /*!
225 ** \brief Return Value as a formatted String
226 **
227 ** This uses the decimal.h library to format the number
228 ** as a std::string.
229 **
230 ** \sa GCW::Cfg::decimal_format()
231 */
232 std::string valueAsString( bool negate = false ) const
233 {
234 return toString( value(negate), GCW::Cfg::decimal_format() );
235 }
236
237 auto set_value( GCW_NUMERIC _value )-> void ;
238
239 /*!
240 ** \brief Return 'quantity' as a decimal.h number.
241 **
242 ** This converts the stored number in to a proper decimal<>
243 ** number type. It can then be used in regular accounting
244 ** calculations.
245 */
247 {
248 GCW_NUMERIC retVal( quantity_num() );
249 retVal /= quantity_denom();
250 return retVal;
251 }
252
253 std::string quantityAsString() const
254 {
255 return toString( quantity(), GCW::Cfg::decimal_format() );
256 }
257
258 auto quantityIsNegative() const-> bool
259 {
260 return quantity() < 0;
261 }
262
263 auto set_quantity( GCW_NUMERIC _value )-> void ;
264
265 template< class Action > void persist( Action & action )
266 {
267 Wt::Dbo::id ( action, m_guid , "guid" , 32 ); // text(32) PRIMARY KEY NOT NULL,
268 Wt::Dbo::field( action, m_tx_guid , "tx_guid" , 32 ); // text(32) NOT NULL,
269 Wt::Dbo::field( action, m_account_guid , "account_guid" , 32 ); // text(32) NOT NULL,
270 Wt::Dbo::field( action, m_memo , "memo" , 2048 ); // text(2048) NOT NULL,
271 Wt::Dbo::field( action, m_action , "action" , 2048 ); // text(2048) NOT NULL,
272 Wt::Dbo::field( action, m_reconcile_state , "reconcile_state" , 1 ); // text(1) NOT NULL,
273 Wt::Dbo::field( action, m_reconcile_date , "reconcile_date" , 19 ); // text(19),
274 Wt::Dbo::field( action, m_value_num , "value_num" ); // bigint NOT NULL,
275 Wt::Dbo::field( action, m_value_denom , "value_denom" ); // bigint NOT NULL,
276 Wt::Dbo::field( action, m_quantity_num , "quantity_num" ); // bigint NOT NULL,
277 Wt::Dbo::field( action, m_quantity_denom , "quantity_denom" ); // bigint NOT NULL,
278 Wt::Dbo::field( action, m_lot_guid , "lot_guid" , 32 ); // text(32)
279
280 } // endtemplate< class Action > void persist( Action & action )
281
282 private:
283
284 /*
285 ** local storage
286 */
287 std::string m_guid ; // text(32) PRIMARY KEY NOT NULL,
288 std::string m_tx_guid ; // text(32) NOT NULL,
289 std::string m_account_guid ; // text(32) NOT NULL,
290 std::string m_memo ; // text(2048) NOT NULL,
291 std::string m_action ; // text(2048) NOT NULL,
292 std::string m_reconcile_state = GCW_RECONCILE_NO ; // text(1) NOT NULL,
293 std::string m_reconcile_date ; // text(19),
294 int m_value_num = 0 ; // bigint NOT NULL,
295 int m_value_denom = 0 ; // bigint NOT NULL,
296 int m_quantity_num = 0 ; // bigint NOT NULL,
297 int m_quantity_denom = 0 ; // bigint NOT NULL,
298 std::string m_lot_guid ; // text(32)
299
300}; // endclass Item
301
302extern const char * s_tableName;
303
304/*!
305** \brief Load a single split
306**
307** This function returns a split based on the GUID.
308*/
309auto load( const std::string & _splitGuid )-> Item::Ptr ;
310
311/*!
312** \brief Find a single split
313**
314** This function returns a split based on the GUID. If the
315** split is not found, a simple empty object is returned.
316*/
317auto find( const std::string & _splitGuid )-> Item::Ptr ;
318
319/*!
320** \brief Add a single split
321**
322** This adds a new split item containing the guid requested
323*/
324auto add( const std::string & _splitGuid )-> Item::Ptr ;
325
326/*!
327** \brief Load a single split
328**
329** This function returns a split based on the GUID.
330*/
331inline auto byGuid( const std::string & _splitGuid )-> Item::Ptr { return load( _splitGuid ); }
332
333/*!
334** \brief Load Splits by Account
335**
336** This function returns a vector of Split items, sorted
337** by transaction date. The result includes ~all~ splits
338** associated with a single account.
339**
340** \return Vector of Items sorted by Transction Date
341*/
342auto byAccount( const std::string & _accountGuid )-> Item::Vector ;
343
344/*!
345** \brief Load Splits by Split
346**
347** This function returns a vector of Split items, sorted
348** by transaction date. The result includes ~all~ splits
349** associated with a transaction except for the split
350** ID used to identify the transaction. This function
351** acts as a convenience function for a split to quickly
352** identify all of the 'other' splits in the transaction.
353**
354** \return Vector of Items sorted by Transction Date
355*/
356auto bySplitExcept( const std::string & _splitGuid )-> Item::Vector ;
357
358/*!
359** \brief Load Splits by Transaction
360**
361** This function returns a vector of Split items, sorted
362** by transaction date. The result includes ~all~ splits
363** associated with a transaction
364**
365** \return Vector of Items sorted by Transction Date
366*/
367auto byTransaction( const std::string & _txGuid )-> Item::Vector ;
368
369 } // endnamespace Splits {
370 } // endnamespace Dbo {
371} // endnamespace GCW {
372
373#endif // end#ifndef __SPLITS_H___
374
std::vector< Ptr > Vector
Definition BaseItem.h:41
Wt::Dbo::ptr< Item > Ptr
Definition BaseItem.h:39
Split Item Class.
Definition Splits.h:88
auto memo() const -> const std::string &
Definition Splits.h:128
auto quantity_denom() const -> int
Definition Splits.h:188
std::string m_tx_guid
Definition Splits.h:288
auto lot_guid() const -> const std::string &
Definition Splits.h:193
auto set_reconcile_date(const std::string &_date) -> void
Definition Splits.h:168
auto set_account_guid(const std::string &_guid) -> void
Definition Splits.h:123
auto set_guid(const std::string &_guid) -> void
Definition Splits.h:103
void persist(Action &action)
Definition Splits.h:265
std::string m_guid
Definition Splits.h:287
auto isReconciled() const -> bool
Definition Splits.h:158
std::string m_memo
Definition Splits.h:290
Item(const std::string &_splitGuid)
Definition Splits.h:93
bool valueIsNegative() const
Test for Negative.
Definition Splits.h:219
auto set_quantity(GCW_NUMERIC _value) -> void
Definition Splits.cpp:281
auto value(bool invert=false) const -> GCW_NUMERIC
Return 'value' as a decimal.h number.
Definition Splits.h:204
auto value_num() const -> int
Definition Splits.h:173
auto quantityIsNegative() const -> bool
Definition Splits.h:258
auto quantity_num() const -> int
Definition Splits.h:183
std::string m_reconcile_date
Definition Splits.h:293
std::string m_reconcile_state
Definition Splits.h:292
auto set_memo(const std::string &_value) -> void
Definition Splits.h:133
auto set_value(GCW_NUMERIC _value) -> void
Definition Splits.cpp:272
GCW_NUMERIC quantity() const
Return 'quantity' as a decimal.h number.
Definition Splits.h:246
auto set_tx_guid(const std::string &_guid) -> void
Definition Splits.h:113
std::string m_account_guid
Definition Splits.h:289
auto set_reconcile_state(const std::string &_state) -> void
Definition Splits.h:153
auto guid() const -> const std::string &
Definition Splits.h:98
std::string valueAsString(bool negate=false) const
Return Value as a formatted String.
Definition Splits.h:232
auto account_guid() const -> const std::string &
Definition Splits.h:118
std::string quantityAsString() const
Definition Splits.h:253
std::string m_action
Definition Splits.h:291
auto value_denom() const -> int
Definition Splits.h:178
auto action() const -> const std::string &
Definition Splits.h:138
auto tx_guid() const -> const std::string &
Definition Splits.h:108
auto reconcile_date() const -> const std::string &
Definition Splits.h:163
std::string m_lot_guid
Definition Splits.h:298
auto reconcile_state() const -> const std::string &
Definition Splits.h:148
auto set_action(const std::string &_value) -> void
Definition Splits.h:143
#define GCW_RECONCILE_NO
Definition gcwglobal.h:27
#define GCW_RECONCILE_YES
Definition gcwglobal.h:28
#define GCW_NUMERIC
Internal Numeric Type.
Definition gcwglobal.h:38
void id(Action &action, V &value, const std::string &name="id", int size=-1)
void field(Action &action, V &value, const std::string &name, int size=-1)
DECIMAL::decimal_format decimal_format()
Decimal Format Specifier.
Definition GnuCashew.cpp:21
auto byTransaction(const std::string &_txGuid) -> Item::Vector
Load Splits by Transaction.
Definition Splits.cpp:244
auto byAccount(const std::string &_accountGuid) -> Item::Vector
Load Splits by Account.
Definition Splits.cpp:165
const char * s_tableName
Definition Splits.cpp:8
auto byGuid(const std::string &_splitGuid) -> Item::Ptr
Load a single split.
Definition Splits.h:331
auto add(const std::string &_splitGuid) -> Item::Ptr
Add a single split.
Definition Splits.cpp:154
auto bySplitExcept(const std::string &_splitGuid) -> Item::Vector
Load Splits by Split.
Definition Splits.cpp:202
auto load(const std::string &_splitGuid) -> Item::Ptr
Load a single split.
Definition Splits.cpp:103
auto find(const std::string &_splitGuid) -> Item::Ptr
Find a single split.
Definition Splits.cpp:122
Definition App.h:18