GnuCashew ~ GnuCash Enabled Web
GCW
Splits.cpp
Go to the documentation of this file.
1 #line 2 "src/Dbo/Splits.cpp"
2 
3 #include "../App.h"
4 
5 #include "Splits.h"
6 #include "../Transactions/Transactions.h"
7 
8 const char * GCW::Dbo::Splits::s_tableName = "splits";
9 
10 namespace {
11 
12 /*!
13 ** \brief Splits Sorter
14 **
15 ** This is a little (private) vector sorter used to sort
16 ** the items pulled from the database. The database can
17 ** produce items in random order, and the 'date' is
18 ** contained on the 'transaction' so sorting by date
19 ** requires a lookup in to the transaction.
20 **
21 ** The sorted vector_of_splits is used in the Account
22 ** Register editor, whereby the items need to be in
23 ** correct chronological order so as to be able to
24 ** calculate a running balance.
25 **
26 */
27 void sort( GCW::Dbo::Splits::Item::Vector & _splitItems )
28 {
29  /*!
30  ** Sort the vector of splits by transaction date so that they can be loaded
31  ** in to the model in proper sequential order.
32  **
33  */
34  std::sort
35  (
36  _splitItems.begin(),
37  _splitItems.end(),
38  []( const GCW::Dbo::Splits::Item::Ptr item1,
39  const GCW::Dbo::Splits::Item::Ptr item2
40  )
41  {
42  auto trans1 = GCW::Dbo::Transactions::byGuid( item1-> tx_guid() );
43  auto trans2 = GCW::Dbo::Transactions::byGuid( item2-> tx_guid() );
44 
45  /*
46  ** return .bool. if the .trans1. date is .less than. the .trans2. date
47  **
48  ** note: it is possible to string-compare these date values, as they are
49  ** represented as ISO dates (YYYY-mm-DD HH:MM:ss) which is
50  ** sortable. Alternatively, we can convert this string to an
51  ** internal WDate element, but it's an unnecessary step.
52  */
53  return trans1-> post_date()
54  < trans2-> post_date();
55 // return trans1-> post_date_as_date()
56 // < trans2-> post_date_as_date();
57  }
58  );
59 
60 } // endvoid sort( GCW::Dbo::Splits::Item::Vector & _splitItems )
61 
62 } // endnamespace {
63 
64 auto
66 load( const std::string & _splitGuid )-> GCW::Dbo::Splits::Item::Ptr
67 {
69 
70  if( _splitGuid != "" )
71  {
72  Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
73 
74  retVal =
75  GCW::app()-> gnucashew_session().load< GCW::Dbo::Splits::Item >( _splitGuid )
76  ;
77  }
78 
79  return retVal;
80 
81 } // endload( const std::string & _splitGuid )
82 
83 auto
85 find( const std::string & _splitGuid )-> GCW::Dbo::Splits::Item::Ptr
86 {
88 
89  if( _splitGuid != "" )
90  {
91  Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
92 
93  /*
94  ** grab the raw data items out of the storage
95  */
96  auto results =
97  GCW::app()-> gnucashew_session().find< Item >()
98  .where( "guid = ?" )
99  .bind( _splitGuid )
100  .resultList()
101  ;
102 
103  /*
104  ** We should find only one item
105  **
106  */
107  if( results.size() == 1 )
108  retVal = *results.begin();
109 
110  } // endif( _splitGuid != "" )
111 
112  return retVal;
113 
114 } // endfind( const std::string & _splitGuid )-> GCW::Dbo::Splits::Item::Ptr
115 
116 auto
118 add( const std::string & _splitGuid )-> Item::Ptr
119 {
120  Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
121 
122  return
123  GCW::app()-> gnucashew_session().addNew< Item >( _splitGuid );
124 
125 } // endadd( const std::string & _splitGuid )-> Item::Ptr
126 
127 auto
129 byAccount( const std::string & _accountGuid )-> GCW::Dbo::Splits::Item::Vector
130 {
132 
133  if( _accountGuid != "" )
134  {
135  Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
136 
137  /*
138  ** grab the raw data items out of the storage
139  */
140  auto results =
141  GCW::app()-> gnucashew_session().find< GCW::Dbo::Splits::Item >()
142  .where( "account_guid = ?" )
143  .bind( _accountGuid )
144  .resultList()
145  ;
146 
147  /*
148  ** vector everything
149  */
150  for( auto result : results )
151  retVal.push_back( result );
152 
153  /*
154  ** sort the vector
155  */
156  sort( retVal );
157 
158  } // endif( _accountGuid != "" )
159 
160  return retVal;
161 
162 } // endbyAccount( const std::string & _accountGuid )-> GCW::Dbo::Splits::Item::Vector
163 
164 auto
166 bySplit( const std::string & _splitGuid )-> GCW::Dbo::Splits::Item::Vector
167 {
169 
170  auto splitItem = GCW::Dbo::Splits::load( _splitGuid );
171 
172  /*
173  ** If we don't have a splitItem then we can't do nuthin.
174  **
175  */
176  if( splitItem )
177  {
178  Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
179 
180  auto results =
181  GCW::app()-> gnucashew_session().find< GCW::Dbo::Splits::Item >()
182  .where( "tx_guid = ?" )
183  .bind( splitItem-> tx_guid() )
184  .resultList()
185  ;
186 
187  /*
188  ** Load the vector, but skip the one that
189  ** matches our incoming split guid.
190  **
191  */
192  for( auto result : results )
193  if( result-> guid() != _splitGuid )
194  retVal.push_back( result );
195 
196  /*!
197  ** The vector is sorted by transction-date before
198  ** returning to the caller.
199  **
200  */
201  sort( retVal );
202 
203  } // endif( GCW::app()-> gnucashew_session().isOpen() )
204 
205  return retVal;
206 
207 } // endbySplit( const std::string & _splitGuid )-> GCW::Dbo::Splits::Item::Vector
208 
209 auto
211 byTransaction( const std::string & _txGuid )-> GCW::Dbo::Splits::Item::Vector
212 {
214 
215  Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
216 
217  auto results =
218  GCW::app()-> gnucashew_session().find< GCW::Dbo::Splits::Item >()
219  .where( "tx_guid = ?" )
220  .bind( _txGuid )
221  .resultList()
222  ;
223 
224  for( auto result : results )
225  retVal.push_back( result );
226 
227  /*!
228  ** The vector is sorted by transction-date before
229  ** returning to the caller.
230  **
231  */
232  sort( retVal );
233 
234  return retVal;
235 
236 } // endbyTransaction( const std::string & _txGuid )-> GCW::Dbo::Splits::Item::Vector
237 
238 auto
240 set_value( GCW_NUMERIC _value )-> void
241 {
242  m_value_num = (_value * 100).getAsInteger();
243  m_value_denom = 100;
244 
245 } // endset_value( GCW_NUMERIC _value )-> void
246 
247 auto
249 set_quantity( GCW_NUMERIC _value )-> void
250 {
251  m_quantity_num = (_value * 100).getAsInteger();
252  m_quantity_denom = 100;
253 
254 } // endset_quantity( GCW_NUMERIC _value )-> void
255 
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 set_quantity(GCW_NUMERIC _value) -> void
Definition: Splits.cpp:249
auto set_value(GCW_NUMERIC _value) -> void
Definition: Splits.cpp:240
#define GCW_NUMERIC
Internal Numeric Type.
Definition: gcwglobal.h:37
const Wt::WFormModel::Field guid
Definition: Accounts.cpp:46
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
App * app()
Definition: App.cpp:67