GnuCashew ~ GnuCash Enabled Web
GCW
Splits.h
Go to the documentation of this file.
1 
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 **
15 */
16 namespace GCW {
17  namespace Dbo {
18  namespace Splits {
19  class Item;
20  }
21  }
22 }
23 
24 /*
25 ** Define these dbo_traits to prevent the system from
26 ** automatically generating an ID field or a VERSION
27 ** field, and instead substitute the guid field
28 ** as the primary key.
29 **
30 */
31 template<> struct Wt::Dbo::dbo_traits< GCW::Dbo::Splits::Item >
32 : public Wt::Dbo::dbo_default_traits
33 {
34  using IdType = std::string;
35  static IdType invalidId() { return std::string(); }
36  static const char * surrogateIdField() { return nullptr; }
37  static const char * versionField() { return nullptr; }
38 };
39 
40 template<> struct Wt::Dbo::dbo_traits< const GCW::Dbo::Splits::Item > : Wt::Dbo::dbo_traits< GCW::Dbo::Splits::Item > {};
41 
42 /*
43 ** Now we can start building our class!
44 **
45 */
46 namespace GCW {
47  namespace Dbo {
48  namespace Splits {
49 
50 /*!
51 ** \brief Split Item Class
52 **
53 ** This class represents a 'split' within gnucash
54 **
55 ** \code
56 **
57 ** CREATE TABLE splits
58 ** (
59 ** guid text(32) PRIMARY KEY NOT NULL,
60 ** tx_guid text(32) NOT NULL,
61 ** account_guid text(32) NOT NULL,
62 ** memo text(2048) NOT NULL,
63 ** action text(2048) NOT NULL,
64 ** reconcile_state text(1) NOT NULL,
65 ** reconcile_date text(19),
66 ** value_num bigint NOT NULL,
67 ** value_denom bigint NOT NULL,
68 ** quantity_num bigint NOT NULL,
69 ** quantity_denom bigint NOT NULL,
70 ** lot_guid text(32)
71 ** );
72 ** CREATE INDEX splits_tx_guid_index ON splits(tx_guid);
73 ** CREATE INDEX splits_account_guid_index ON splits(account_guid);
74 **
75 ** sqlite> select * from accounts;
76 ** guid name account_type commodity_guid scu non parent_guid code description h p
77 ** 822a857c5f484affa5a6a3e62f4b700f|Checking Account|BANK |10b24d11b4b94b8789d1830da2695bbb|100|0 |b61b07c024fc463489f5db031135a29e| |Checking Account|0|0
78 ** d934f2e5606c4da6b25c703c4661d747|Opening Balances|EQUITY |10b24d11b4b94b8789d1830da2695bbb|100|0 |745365afce5946b19d4189c593b9f6aa| |Opening Balances|0|0
79 ** sqlite> select * from transactions;
80 ** guid currency_guid num post_date enter_date description
81 ** 4b2259ef3fbb486bad1b42f28ec84346|10b24d11b4b94b8789d1830da2695bbb| |2023-05-28 10:59:00|2023-05-28 21:46:09|
82 ** sqlite> select * from splits;
83 ** guid tx_guid account_guid memo action rs reconcile_date value_num value_denom q_num q_den lot_guid
84 ** c68c279c3ef64d00ae682f5573bb55bc|4b2259ef3fbb486bad1b42f28ec84346|822a857c5f484affa5a6a3e62f4b700f| | |n |1970-01-01 00:00:00|100 |100 |100 |100 |
85 ** a67a975bc58a4fa1bd00421b37fed115|4b2259ef3fbb486bad1b42f28ec84346|d934f2e5606c4da6b25c703c4661d747| | |n |1970-01-01 00:00:00|-100 |100 |-100 |100 |
86 **
87 ** \endcode
88 **
89 */
90 class Item
91 : public GCW::Dbo::BaseItem< Item >
92 {
93  public:
94 
95  Item(){}
96 
97  Item( const std::string & _splitGuid ): m_guid( _splitGuid ) {}
98 
99  /*!
100  ** Split GUID
101  */
102  auto guid() const-> const std::string & { return m_guid; }
103 
104  /*!
105  ** Set Split GUID
106  */
107  auto set_guid( const std::string & _guid )-> void { m_guid = _guid; }
108 
109  /*!
110  ** Transaction GUID
111  */
112  auto tx_guid() const-> const std::string & { return m_tx_guid; }
113 
114  /*!
115  ** Set Transaction GUID
116  */
117  auto set_tx_guid( const std::string & _guid )-> void { m_tx_guid = _guid; }
118 
119  /*!
120  ** Account GUID
121  */
122  auto account_guid() const-> const std::string & { return m_account_guid; }
123 
124  /*!
125  ** Set Account GUID
126  */
127  auto set_account_guid( const std::string & _guid )-> void { m_account_guid = _guid; }
128 
129  /*!
130  ** Memo field
131  */
132  auto memo() const-> const std::string & { return m_memo; }
133 
134  /*!
135  ** Action field
136  */
137  auto action() const-> const std::string & { return m_action; }
138 
139  /*!
140  ** Set Action
141  **
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  */
233  std::string valueAsString( bool negate = false ) const
234  {
235  return toString( value(negate), GCW::Cfg::decimal_format() );
236  }
237 
238  auto set_value( GCW_NUMERIC _value )-> void ;
239 
240  /*!
241  ** \brief Return 'quantity' as a decimal.h number.
242  **
243  ** This converts the stored number in to a proper decimal<>
244  ** number type. It can then be used in regular accounting
245  ** calculations.
246  */
248  {
249  GCW_NUMERIC retVal( quantity_num() );
250  retVal /= quantity_denom();
251  return retVal;
252  }
253 
254  std::string quantityAsString() const
255  {
257  }
258 
259  auto quantityIsNegative() const-> bool
260  {
261  return quantity() < 0;
262  }
263 
264  auto set_quantity( GCW_NUMERIC _value )-> void ;
265 
266  template< class Action > void persist( Action & action )
267  {
268  Wt::Dbo::id ( action, m_guid , "guid" , 32 ); // text(32) PRIMARY KEY NOT NULL,
269  Wt::Dbo::field( action, m_tx_guid , "tx_guid" , 32 ); // text(32) NOT NULL,
270  Wt::Dbo::field( action, m_account_guid , "account_guid" , 32 ); // text(32) NOT NULL,
271  Wt::Dbo::field( action, m_memo , "memo" , 2048 ); // text(2048) NOT NULL,
272  Wt::Dbo::field( action, m_action , "action" , 2048 ); // text(2048) NOT NULL,
273  Wt::Dbo::field( action, m_reconcile_state , "reconcile_state" , 1 ); // text(1) NOT NULL,
274  Wt::Dbo::field( action, m_reconcile_date , "reconcile_date" , 19 ); // text(19),
275  Wt::Dbo::field( action, m_value_num , "value_num" ); // bigint NOT NULL,
276  Wt::Dbo::field( action, m_value_denom , "value_denom" ); // bigint NOT NULL,
277  Wt::Dbo::field( action, m_quantity_num , "quantity_num" ); // bigint NOT NULL,
278  Wt::Dbo::field( action, m_quantity_denom , "quantity_denom" ); // bigint NOT NULL,
279  Wt::Dbo::field( action, m_lot_guid , "lot_guid" , 32 ); // text(32)
280 
281  } // endtemplate< class Action > void persist( Action & action )
282 
283  private:
284 
285  std::string m_guid ; // text(32) PRIMARY KEY NOT NULL,
286  std::string m_tx_guid ; // text(32) NOT NULL,
287  std::string m_account_guid ; // text(32) NOT NULL,
288  std::string m_memo ; // text(2048) NOT NULL,
289  std::string m_action ; // text(2048) NOT NULL,
290  std::string m_reconcile_state = GCW_RECONCILE_NO ; // text(1) NOT NULL,
291  std::string m_reconcile_date ; // text(19),
292  int m_value_num = 0 ; // bigint NOT NULL,
293  int m_value_denom = 0 ; // bigint NOT NULL,
294  int m_quantity_num = 0 ; // bigint NOT NULL,
295  int m_quantity_denom = 0 ; // bigint NOT NULL,
296  std::string m_lot_guid ; // text(32)
297 
298 }; // endclass Item
299 
300 extern const char * s_tableName;
301 
302 /*!
303 ** \brief Load a single split
304 **
305 ** This function returns a split based on the GUID.
306 **
307 */
308 auto load( const std::string & _splitGuid )-> Item::Ptr ;
309 
310 /*!
311 ** \brief Find a single split
312 **
313 ** This function returns a split based on the GUID. If the
314 ** split is not found, a simple empty object is returned.
315 **
316 */
317 auto 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 **
324 */
325 auto add( const std::string & _splitGuid )-> Item::Ptr ;
326 
327 /*!
328 ** \brief Load Splits by Account
329 **
330 ** This function returns a vector of Split items, sorted
331 ** by transaction date. The result includes ~all~ splits
332 ** associated with a single account.
333 **
334 ** \return Vector of Items sorted by Transction Date
335 */
336 auto byAccount( const std::string & _accountGuid )-> Item::Vector ;
337 
338 /*!
339 ** \brief Load Splits by Split
340 **
341 ** This function returns a vector of Split items, sorted
342 ** by transaction date. The result includes ~all~ splits
343 ** associated with a transaction except for the split
344 ** ID used to identify the transaction. This function
345 ** acts as a convenience function for a split to quickly
346 ** identify all of the 'other' splits in the transaction.
347 **
348 ** \return Vector of Items sorted by Transction Date
349 */
350 auto bySplit( const std::string & _splitGuid )-> Item::Vector ;
351 
352 /*!
353 ** \brief Load Splits by Transaction
354 **
355 ** This function returns a vector of Split items, sorted
356 ** by transaction date. The result includes ~all~ splits
357 ** associated with a transaction
358 **
359 ** \return Vector of Items sorted by Transction Date
360 */
361 auto byTransaction( const std::string & _txGuid )-> Item::Vector ;
362 
363  } // endnamespace Splits {
364  } // endnamespace Dbo {
365 } // endnamespace GCW {
366 
367 #endif // end#ifndef __SPLITS_H___
368 
std::vector< Ptr > Vector
Definition: BaseItem.h:41
Wt::Dbo::ptr< Item > Ptr
Definition: BaseItem.h:39
Split Item Class.
Definition: Splits.h:92
auto memo() const -> const std::string &
Definition: Splits.h:132
auto quantity_denom() const -> int
Definition: Splits.h:188
std::string m_tx_guid
Definition: Splits.h:286
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:127
auto set_guid(const std::string &_guid) -> void
Definition: Splits.h:107
void persist(Action &action)
Definition: Splits.h:266
std::string m_guid
Definition: Splits.h:285
auto isReconciled() const -> bool
Definition: Splits.h:158
std::string m_memo
Definition: Splits.h:288
Item(const std::string &_splitGuid)
Definition: Splits.h:97
bool valueIsNegative() const
Test for Negative.
Definition: Splits.h:219
auto set_quantity(GCW_NUMERIC _value) -> void
Definition: Splits.cpp:249
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:259
auto quantity_num() const -> int
Definition: Splits.h:183
std::string m_reconcile_date
Definition: Splits.h:291
std::string m_reconcile_state
Definition: Splits.h:290
auto set_value(GCW_NUMERIC _value) -> void
Definition: Splits.cpp:240
GCW_NUMERIC quantity() const
Return 'quantity' as a decimal.h number.
Definition: Splits.h:247
auto set_tx_guid(const std::string &_guid) -> void
Definition: Splits.h:117
std::string m_account_guid
Definition: Splits.h:287
auto set_reconcile_state(const std::string &_state) -> void
Definition: Splits.h:153
auto guid() const -> const std::string &
Definition: Splits.h:102
std::string valueAsString(bool negate=false) const
Return Value as a formatted String.
Definition: Splits.h:233
auto account_guid() const -> const std::string &
Definition: Splits.h:122
std::string quantityAsString() const
Definition: Splits.h:254
std::string m_action
Definition: Splits.h:289
auto value_denom() const -> int
Definition: Splits.h:178
auto action() const -> const std::string &
Definition: Splits.h:137
auto tx_guid() const -> const std::string &
Definition: Splits.h:112
auto reconcile_date() const -> const std::string &
Definition: Splits.h:163
std::string m_lot_guid
Definition: Splits.h:296
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:26
#define GCW_RECONCILE_YES
Definition: gcwglobal.h:27
#define GCW_NUMERIC
Internal Numeric Type.
Definition: gcwglobal.h:37
DECIMAL::decimal_format decimal_format()
Decimal Format Specifier.
Definition: GnuCashew.cpp:21
const Wt::WFormModel::Field id
Definition: Definition.h:17
auto bySplit(const std::string &_splitGuid) -> Item::Vector
Load Splits by Split.
Definition: Splits.cpp:166
auto byTransaction(const std::string &_txGuid) -> Item::Vector
Load Splits by Transaction.
Definition: Splits.cpp:211
auto byAccount(const std::string &_accountGuid) -> Item::Vector
Load Splits by Account.
Definition: Splits.cpp:129
const char * s_tableName
Definition: Splits.cpp:8
auto add(const std::string &_splitGuid) -> Item::Ptr
Add a single split.
Definition: Splits.cpp:118
auto load(const std::string &_splitGuid) -> Item::Ptr
Load a single split.
Definition: Splits.cpp:66
auto find(const std::string &_splitGuid) -> Item::Ptr
Find a single split.
Definition: Splits.cpp:85
auto toString(int _value) -> std::string
Convert Integer to String.
Definition: BillPay.cpp:41
Definition: App.h:17
Definition: guid.cpp:397
static const char * surrogateIdField()
Definition: Splits.h:36