GnuCashew ~ Web Application compatible with GnuCash sql data files.
GCW
Loading...
Searching...
No Matches
Splits.cpp
Go to the documentation of this file.
1#line 2 "src/Dbo/Splits.cpp"
2
3#include <chrono>
4
5#include "../App.h"
6
7#include "Splits.h"
8#include "../Transactions/Transactions.h"
9
10const char * GCW::Dbo::Splits::s_tableName = "splits";
11
12auto
14load( const std::string & _splitGuid )-> GCW::Dbo::Splits::Item::Ptr
15{
17
18 if( _splitGuid != "" )
19 {
20 Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
21
22 retVal =
23 GCW::app()-> gnucashew_session().load< GCW::Dbo::Splits::Item >( _splitGuid )
24 ;
25 }
26
27 return retVal;
28
29} // endload( const std::string & _splitGuid )
30
31auto
33find( const std::string & _splitGuid )-> GCW::Dbo::Splits::Item::Ptr
34{
36
37 if( _splitGuid != "" )
38 {
39 Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
40
41 /*
42 ** grab the raw data items out of the storage
43 */
44 auto results =
45 GCW::app()-> gnucashew_session().find< Item >()
46 .where( "guid = ?" )
47 .bind( _splitGuid )
48 .resultList()
49 ;
50
51 /*
52 ** We should find only one item
53 */
54 if( results.size() == 1 )
55 retVal = *results.begin();
56
57 } // endif( _splitGuid != "" )
58
59 return retVal;
60
61} // endfind( const std::string & _splitGuid )-> GCW::Dbo::Splits::Item::Ptr
62
63auto
65add( const std::string & _splitGuid )-> Item::Ptr
66{
67 Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
68
69 return
70 GCW::app()-> gnucashew_session().addNew< Item >( _splitGuid );
71
72} // endadd( const std::string & _splitGuid )-> Item::Ptr
73
74auto
76byAccount( const std::string & _accountGuid )-> GCW::Dbo::Splits::Item::Vector
77{
79
80// const auto start = std::chrono::system_clock::now();
81
82 if( _accountGuid != "" )
83 {
84 Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
85
86 /*
87 ** this sql will sort the results of the query by date,value(desc)
88 ** without having to sort in code. this is way freaking faster
89 */
90 std::string sql =
91 "SELECT s "
92 "FROM splits s "
93 "JOIN transactions t ON s.tx_guid = t.guid "
94 "WHERE s.account_guid = ? "
95 "ORDER BY t.post_date, s.value_num desc";
96
97#ifdef SQL_THAT_CAN_RETURN_RUNNING_BALANCE
98
99SELECT s
100 SUM(s.value_num * 1.0 / s.value_denom) OVER (
101 PARTITION BY s.account_guid
102 ORDER BY t.post_date, s.guid
103 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
104 ) AS running_balance,
105FROM splits s
106JOIN transactions t ON s.tx_guid = t.guid
107WHERE s.account_guid = ?
108ORDER BY t.post_date, s.value_num;
109
110#endif
111
112 /*
113 ** grab the raw data items out of the storage
114 */
115 auto results =
116 GCW::app()->
117 gnucashew_session()
118 .query< GCW::Dbo::Splits::Item::Ptr >( sql )
119 .bind( _accountGuid )
120 .resultList()
121 ;
122
123 /*
124 ** vector everything
125 */
126 for( auto result : results )
127 retVal.push_back( result );
128
129 } // endif( _accountGuid != "" )
130
131// std::cout << __FILE__ << ":" << __LINE__
132// << " " << std::chrono::duration_cast< std::chrono::milliseconds >
133// ( std::chrono::system_clock::now() - start ).count()
134// << "mS load time for"
135// << " " << retVal.size() << " items"
136// << std::endl;
137
138 return retVal;
139
140} // endbyAccount( const std::string & _accountGuid )-> GCW::Dbo::Splits::Item::Vector
141
142auto
144bySplitExcept( const std::string & _splitGuid )-> GCW::Dbo::Splits::Item::Vector
145{
147
148 auto splitItem = GCW::Dbo::Splits::load( _splitGuid );
149
150 /*
151 ** If we don't have a splitItem then we can't do nuthin.
152 */
153 if( splitItem )
154 {
155 Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
156
157 std::string sql =
158 "SELECT s "
159 "FROM splits s "
160 "JOIN transactions t ON s.tx_guid = t.guid "
161 "WHERE s.tx_guid = ? "
162 "AND s.guid != ? "
163 "ORDER BY t.post_date, s.value_num desc";
164
165 auto results =
166 GCW::app()-> gnucashew_session().query< GCW::Dbo::Splits::Item::Ptr >( sql )
167 .bind( splitItem-> tx_guid() )
168 .bind( splitItem-> guid() )
169 .resultList()
170 ;
171
172 /*
173 ** Load the vector, but skip the one that
174 ** matches our incoming split guid.
175 */
176 for( auto result : results )
177 retVal.push_back( result );
178
179 } // endif( GCW::app()-> gnucashew_session().isOpen() )
180
181 return retVal;
182
183} // endbySplit( const std::string & _splitGuid )-> GCW::Dbo::Splits::Item::Vector
184
185auto
187byTransaction( const std::string & _txGuid )-> GCW::Dbo::Splits::Item::Vector
188{
190
191 Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
192
193 std::string sql =
194 "SELECT s "
195 "FROM splits s "
196 "JOIN transactions t ON s.tx_guid = t.guid "
197 "WHERE s.tx_guid = ? "
198 "ORDER BY t.post_date, s.value_num desc";
199
200 auto results =
201 GCW::app()-> gnucashew_session().query< GCW::Dbo::Splits::Item::Ptr >( sql )
202 .bind( _txGuid )
203 .resultList()
204 ;
205
206 for( auto result : results )
207 retVal.push_back( result );
208
209 return retVal;
210
211} // endbyTransaction( const std::string & _txGuid )-> GCW::Dbo::Splits::Item::Vector
212
213auto
215set_value( GCW_NUMERIC _value )-> void
216{
217 auto account = GCW::Dbo::Accounts::byGuid( account_guid() );
218
219 m_value_num = (_value * account-> commodity_scu() ).getAsInteger();
220 m_value_denom = account-> commodity_scu();
221
222} // endset_value( GCW_NUMERIC _value )-> void
223
224auto
226set_quantity( GCW_NUMERIC _value )-> void
227{
228 auto account = GCW::Dbo::Accounts::byGuid( account_guid() );
229
230 m_quantity_num = (_value * account-> commodity_scu() ).getAsInteger();
231 m_quantity_denom = account-> commodity_scu() ;
232
233} // endset_quantity( GCW_NUMERIC _value )-> void
234
std::vector< Ptr > Vector
Definition BaseItem.h:41
Split Item Class.
Definition Splits.h:88
auto set_quantity(GCW_NUMERIC _value) -> void
Definition Splits.cpp:226
auto set_value(GCW_NUMERIC _value) -> void
Definition Splits.cpp:215
#define GCW_NUMERIC
Internal Numeric Type.
Definition gcwglobal.h:40
auto byGuid(const std::string &_guid) -> Item::Ptr
Load Account by GUID.
auto byTransaction(const std::string &_txGuid) -> Item::Vector
Load Splits by Transaction.
Definition Splits.cpp:187
auto byAccount(const std::string &_accountGuid) -> Item::Vector
Load Splits by Account.
Definition Splits.cpp:76
const char * s_tableName
Definition Splits.cpp:10
auto add(const std::string &_splitGuid) -> Item::Ptr
Add a single split.
Definition Splits.cpp:65
auto bySplitExcept(const std::string &_splitGuid) -> Item::Vector
Load Splits by Split.
Definition Splits.cpp:144
auto load(const std::string &_splitGuid) -> Item::Ptr
Load a single split.
Definition Splits.cpp:14
auto find(const std::string &_splitGuid) -> Item::Ptr
Find a single split.
Definition Splits.cpp:33
App * app()
Definition App.cpp:75