GnuCashew ~ Web Application compatible with GnuCash sql data files.
GCW
Loading...
Searching...
No Matches
Transactions.cpp
Go to the documentation of this file.
1#line 2 "src/Dbo/Transactions.cpp"
2
3#include "../App.h"
4#include "Transactions.h"
5
6const char * GCW::Dbo::Transactions::s_tableName = "transactions";
7
8auto
10set_num( const std::string & _value )-> void
11{
12 m_num = _value;
13
14}
15
16auto
18set_enter_date ( const std::string & _value )-> void
19{
20 m_enter_date = _value;
21}
22
23auto
25set_enter_date ( const Wt::WDate & _value )-> void
26{
27 auto dateTime = Wt::WDateTime( _value, GCW_DATE_DEFAULT_TIME );
28 set_enter_date( dateTime.toString( GCW_DATETIME_FORMAT_STORAGE ).toUTF8() );
29}
30
31auto
33set_enter_date ( const Wt::WDateTime & _value )-> void
34{
35 set_enter_date ( _value.toString( GCW_DATETIME_FORMAT_STORAGE ).toUTF8() );
36}
37
38auto
40set_post_date( const std::string & _value )-> void
41{
42 m_post_date = _value;
43}
44
45auto
47set_post_date( const Wt::WDate & _value )-> void
48{
49 auto dateTime = Wt::WDateTime( _value, GCW_DATE_DEFAULT_TIME );
50
51 set_post_date( dateTime.toString( GCW_DATETIME_FORMAT_STORAGE ).toUTF8() );
52
53} // endset_post_date( const Wt::WDate & _value )-> void
54
55auto
57set_post_date( const Wt::WDateTime & _value )-> void
58{
59 /*
60 ** BUGBUG: This is a very weird thing! The gnucash system seems to post transactions
61 ** with a post_date of "yyyy-MM-dd 10:59:00" with the 'hours and minutes'
62 ** set to that value accordingly. If the record is posted as '00:00:00'
63 ** hours, gnucash will interpret the date as being in the day before.
64 **
65 ** So, therefore, when posting the 'post_date' of a transaction, it is
66 ** a requirement that the hours be set like this.
67 */
68 set_post_date( _value.toString( "yyyy-MM-dd 10:59:00" ).toUTF8() );
69
70} // endset_post_date( const Wt::WDateTime & _value )-> void
71
72auto
74set_description( const std::string & _value )-> void
75{
76 m_description = _value;
77}
78
79auto
81load( const std::string & _txGuid )-> GCW::Dbo::Transactions::Item::Ptr
82{
84
85 /*
86 ** If the session isn't open then there's nothing to load.
87 **
88 */
89 if( GCW::app()-> gnucashew_session().isOpen() )
90 {
91
92 Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
93
94 retVal =
95 GCW::app()-> gnucashew_session().find< GCW::Dbo::Transactions::Item >()
96 .where( "guid = ?" )
97 .bind( _txGuid )
98 ;
99
100 } // endif( GCW::app()-> gnucashew_session().isOpen() )
101
102
103 return retVal;
104
105} // endGCW::Dbo::Transactions::Item::Ptr GCW::Dbo::Transactions::byGuid( const std::string & _txGuid )
106
107auto
109byGuid( const std::string & _txGuid )-> GCW::Dbo::Transactions::Item::Ptr
110{
111 return load( _txGuid );
112
113} // endGCW::Dbo::Transactions::Item::Ptr GCW::Dbo::Transactions::byGuid( const std::string & _txGuid )
114
115auto
117add( const std::string & _txGuid )-> Item::Ptr
118{
119 Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
120
121 return
122 GCW::app()-> gnucashew_session().addNew< Item >( _txGuid );
123
124} // endadd( const std::string & _txGuid )-> Item::Ptr
125
126auto
128byAccount( const std::string & _accountGuid )-> GCW::Dbo::Transactions::Item::Vector
129{
131
132 /*
133 ** If the session isn't open then there's nothing to load.
134 **
135 */
136 if( GCW::app()-> gnucashew_session().isOpen() )
137 {
138
139 Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
140
141 /*
142 ** Pull all transactions that have a split that matches this
143 ** account. Sort the results by post_date:ascending, so that any
144 ** subsequent views can process the items in sequential order.
145 **
146 */
147 auto results =
148 GCW::app()-> gnucashew_session().find< GCW::Dbo::Transactions::Item >()
149 .where( "guid in (select tx_guid from splits where account_guid = ?)" )
150 .bind( _accountGuid )
151 .orderBy( "post_date" )
152 .resultList();
153 ;
154
155 for( auto result : results )
156 retVal.push_back( result );
157
158 } // endif( GCW::app()-> gnucashew_session().isOpen() )
159
160 return retVal;
161
162} // endGCW::Dbo::Transactions::Item::Vector GCW::Dbo::Transactions::byAccount( const std::string & _accountGuid )
163
164auto
166byAccountMonth( const std::string & _accountGuid, int _month )-> GCW::Dbo::Transactions::Item::Vector
167{
169
170 auto thisYear = Wt::WDate::currentDate().year();
171
172 /*
173 ** if the transaction item is within this month and year
174 ** then it's a keeper
175 */
176 for( auto item : byAccount( _accountGuid ) )
177 if( item-> post_date_as_date().date().month() == _month
178 && item-> post_date_as_date().date().year() == thisYear
179 )
180 retVal.push_back( item );
181
182 return retVal;
183
184} // endbyAccountMonth( const std::string & _accountGuid, int _month )-> GCW::Dbo::Transactions::Item::Vector
185
186auto
188byNumMonth( const std::string & _num, int _month )-> GCW::Dbo::Transactions::Item::Vector
189{
191
192 auto date = Wt::WDate( Wt::WDate::currentDate().year(), _month, 1 ).toString( "yyyy-MM" );
193
194 Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
195 auto results =
196 GCW::app()-> gnucashew_session().find< GCW::Dbo::Transactions::Item >()
197 .where( "num = ? and post_date LIKE ?" )
198 .bind( _num )
199 .bind( date + "%" )
200 .orderBy( "post_date" )
201 .resultList();
202 ;
203
204 for( auto item : results )
205 retVal.push_back( item );
206
207 return retVal;
208
209} // endbyNumMonth( const std::string & _num, int _month )-> GCW::Dbo::Transactions::Item::Vector
210
auto set_num(const std::string &_value) -> void
auto set_post_date(const Wt::WDate &_value) -> void
std::vector< Ptr > Vector
auto set_enter_date(const Wt::WDate &_value) -> void
auto set_description(const std::string &_value) -> void
WString toString() const
static WDate currentDate()
int year() const
#define GCW_DATETIME_FORMAT_STORAGE
Definition gcwglobal.h:14
#define GCW_DATE_DEFAULT_TIME
Default Time.
Definition gcwglobal.h:27
auto add(const std::string &_txGuid) -> Item::Ptr
Add Transaction with Guid.
auto byGuid(const std::string &_txGuid) -> Item::Ptr
Load Transaction by Guid.
auto byAccount(const std::string &_accountGuid) -> Item::Vector
Load Transactions for Account Guid.
auto byNumMonth(const std::string &_num, int _month) -> Item::Vector
Load Transactions for 'num' and Month.
auto byAccountMonth(const std::string &_accountGuid, int _month) -> Item::Vector
auto load(const std::string &_txGuid) -> Item::Ptr
Load Transaction by Guid.
App * app()
Definition App.cpp:75