GnuCashew ~ GnuCash Enabled Web
GCW
SessionGnuCash.cpp
Go to the documentation of this file.
1 #line 2 "src/Dbo/SessionGnuCash.cpp"
2 
3 #ifdef NEVER
4 #define TESTFILE "gnucash-dev.gnucash"
5 
6 #include <iostream>
7 
8 #include "../Glb/Core.h"
9 
10 #include <gcwglobal.h>
11 #include "Accounts.h"
12 #include "Customers.h"
13 #include "Slots.h"
14 #include "Splits.h"
15 #include "Transactions.h"
16 #include "Vars.h"
17 #include "SessionGnuCash.h"
18 
19 GCW::Dbo::GnuCash::Session::
20 ~Session()
21 {
22  if( isOpen() )
23  closeGnuCash();
24 }
25 
26 /*
27  *
28 
29  This is a trace-output from the open() call:
30 
31 src/Dbo/SessionGnuCash.cpp:43 /home/serveradmin/LorimarkSolutions.gnucash
32 src/Dbo/SessionGnuCash.cpp:49 qof_session_begin
33 src/Dbo/SessionGnuCash.cpp:58 error: 0
34 src/Dbo/SessionGnuCash.cpp:66 qof_session_load
35 * 11:24:47 ERROR <gnc.engine> void QofSessionImpl::load(QofPercentageFunc): assertion 'm_book && qof_book_empty(m_book)' failed
36 src/Dbo/SessionGnuCash.cpp:69 error: 0
37 src/Dbo/SessionGnuCash.cpp:76 qof_session_get_book
38 src/Dbo/SessionGnuCash.cpp:78 0x7f9480017070
39 src/Dbo/SessionGnuCash.cpp:80 gnc_book_get_root_account
40 src/Dbo/SessionGnuCash.cpp:82 rootAccount: 0x7f9480020430
41 src/Dbo/SessionGnuCash.cpp:84 count:2277
42 src/Dbo/SessionGnuCash.cpp:88 rootGuid: ba4d0360249c4a848cdf4d2955fff3e2
43 src/Dbo/SessionGnuCash.cpp:89 rootName: Root Account
44 src/Dbo/SessionGnuCash.cpp:90 rootCode: testing
45 src/Dbo/SessionGnuCash.cpp:91 rootDesc: testing 123
46 src/Dbo/SessionGnuCash.cpp:93 size: 8
47 src/Dbo/SessionGnuCash.cpp:99 index:0 guid: 5dc2c6d4adae4f00b19bd5b9a1c74b46 name: Expenses
48 src/Dbo/SessionGnuCash.cpp:99 index:1 guid: a078ee7908764913b4901cdda1401cba name: Liabilities
49 src/Dbo/SessionGnuCash.cpp:99 index:2 guid: 551b39dc9e7e48369b269d7d50b5fc8f name: Assets
50 src/Dbo/SessionGnuCash.cpp:99 index:3 guid: 0ab997bc52ae48998012fa8ed058c7be name: Income
51 src/Dbo/SessionGnuCash.cpp:99 index:4 guid: 1440f4545f164671a63dbf3e2fce5634 name: Equity
52 src/Dbo/SessionGnuCash.cpp:99 index:5 guid: 94282e4852634c8a92c95a80831b5b73 name: Personal
53 src/Dbo/SessionGnuCash.cpp:99 index:6 guid: df47015bd02642f198163f7099a3eddd name: Orphan-USD
54 src/Dbo/SessionGnuCash.cpp:99 index:7 guid: 6938eb9a9c014cf491e87fbba61d4325 name: Imbalance-USD
55 src/Dbo/SessionGnuCash.cpp:103 qof_session_save
56 src/Dbo/SessionGnuCash.cpp:121 open finished.
57 */
58 
59 QofSession *
60 GCW::Dbo::GnuCash::Session::
61 qofSession()
62 {
63  return m_session;
64 }
65 
66 bool
67 GCW::Dbo::GnuCash::Session::
68 open( const std::string & _path )
69 {
70  if( isOpen() )
71  return true;
72 
73  /*
74  ** Call the base class.
75  **
76  */
78 
79  std::cout << __FILE__ << ":" << __LINE__ << " " << path() << std::endl;
80 
81 // auto userdata_migration_msg = gnc_filepath_init();
82  gnc_engine_init( 0, nullptr );
83 // auto s = gnc_get_current_session(); // qof_session_new( qof_book_new() );
84  m_session = qof_session_new( qof_book_new() );
85 
86  std::cout << __FILE__ << ":" << __LINE__ << " qof_session_begin" << std::endl;
87  std::string url = "sqlite3://" + path();
88 
89 // qof_session_begin( s, url.c_str(), SESSION_NORMAL_OPEN );
90 // qof_session_begin( s, url.c_str(), SESSION_NEW_STORE );
91 // qof_session_begin( s, url.c_str(), SESSION_NEW_OVERWRITE );
92  qof_session_begin( m_session, url.c_str(), SESSION_READ_ONLY );
93 // qof_session_begin( s, url.c_str(), SESSION_BREAK_LOCK );
94 
95  std::cout << __FILE__ << ":" << __LINE__ << " error: " << qof_session_get_error( m_session ) << std::endl;
96 
97  if( qof_session_get_error( m_session ) != 0 )
98  {
99  std::cout << __FILE__ << ":" << __LINE__ << " error opening qof_session" << std::endl;
100  return false;
101  }
102 
103  std::cout << __FILE__ << ":" << __LINE__ << " qof_session_load" << std::endl;
104  qof_session_load( m_session, NULL );
105 
106  std::cout << __FILE__ << ":" << __LINE__ << " error: " << qof_session_get_error( m_session ) << std::endl;
107 
108 
109 // std::cout << __FILE__ << ":" << __LINE__ << " checking file path" << std::endl;
110 // std::cout << __FILE__ << ":" << __LINE__ << " filePath:" << qof_session_get_file_path( m_session ) << std::endl;
111 
112 #ifdef NEVER
113  std::cout << __FILE__ << ":" << __LINE__ << " qof_session_get_book" << std::endl;
114  auto book = getBook();
115 
116  std::cout << __FILE__ << ":" << __LINE__ << " gnc_book_get_root_account" << std::endl;
117  auto rootAccount = book.get_root_account(); // gnc_book_get_root_account( book );
118 
119 // std::cout << __FILE__ << ":" << __LINE__ << " gnc_book_count_transactions:" << gnc_book_count_transactions( book ) << std::endl;
120  std::cout << __FILE__ << ":" << __LINE__ << " gnc_book_count_transactions:" << book.count_transactions() << std::endl;
121 
122 // auto guidString = rootAccount.getGUID(); // guid_to_string( xaccAccountGetGUID( rootAccount ) );
123 
124  std::cout << __FILE__ << ":" << __LINE__ << " rootGuid: " << rootAccount.guid() << std::endl; // guidString << std::endl;
125  std::cout << __FILE__ << ":" << __LINE__ << " rootName: " << rootAccount.name() << std::endl; // xaccAccountGetName( rootAccount ) << std::endl;
126  std::cout << __FILE__ << ":" << __LINE__ << " rootCode: " << rootAccount.code() << std::endl; // xaccAccountGetCode( rootAccount ) << std::endl;
127  std::cout << __FILE__ << ":" << __LINE__ << " rootDesc: " << rootAccount.description() << std::endl; // xaccAccountGetDescription( rootAccount ) << std::endl;
128 
129 // std::cout << __FILE__ << ":" << __LINE__ << " size: " << gnc_account_n_children( rootAccount ) << std::endl;
130  std::cout << __FILE__ << ":" << __LINE__ << " size: " << rootAccount.children().size() << std::endl;
131 // for( int i=0; i< gnc_account_n_children( rootAccount ); i++ )
132  int i=0;
133  for( auto child : rootAccount.children() )
134  {
135 // auto child = gnc_account_nth_child( rootAccount, i );
136 // auto guid = guid_to_string( xaccAccountGetGUID( child ) );
137 // std::cout << __FILE__ << ":" << __LINE__
138 // << " index:" << i
139 // << " guid: " << guid
140 // << " name: " << xaccAccountGetName( child )
141 // << std::endl;
142 
143  std::cout << __FILE__ << ":" << __LINE__
144  << " index:" << i++
145  << " guid: " << child.guid()
146  << " name: " << child.name()
147  << std::endl;
148 
149  } // endfor( ..all children.. )
150 
151  std::cout << __FILE__ << ":" << __LINE__ << " qof_session_save" << std::endl;
152  qof_session_save( m_session, NULL );
153 
154 // AccountList *accountList;
155 // gnc_accounts_and_all_descendants( accountList );
156 
157 // auto account = gnc_account_lookup_by_name( nullptr, "Assets" );
158 
159 // std::cout << __FILE__ << ":" << __LINE__ << " " << std::endl;
160 
161 // std::cout << __FILE__ << ":" << __LINE__ << " " << g_list_length( &accountList ) << std::endl;
162 
163 // std::cout << __FILE__ << ":" << __LINE__ << " " << std::endl;
164 
165 // accountList.next;
166 
167 #endif
168 
169  std::cout << __FILE__ << ":" << __LINE__ << " open finished." << std::endl;
170  m_isOpen = true;
171 
172  return isOpen();
173 
174 } // endopen( const std::string & _path )
175 
176 void
177 GCW::Dbo::GnuCash::Session::
178 closeGnuCash()
179 {
180  std::cout << __FILE__ << ":" << __LINE__ << " qof_session_end" << std::endl;
181 
182  qof_session_end( m_session );
183 
184  m_session = nullptr;
185 
186  gnc_engine_shutdown();
187 }
188 
189 
190 GCW::Dbo::GnuCash::Book
191 GCW::Dbo::GnuCash::Session::
192 book()
193 {
194  return Book( this );
195 }
196 
197 
198 
199 
200 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
201 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
202 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
203 
204 
205 GCW::Dbo::GnuCash::Book::
206 Book( GCW::Dbo::GnuCash::Session * _session )
207 : m_session( _session )
208 {
209  m_book = qof_session_get_book( m_session-> qofSession() );
210 }
211 
212 GCW::Dbo::GnuCash::Account
214 rootAccount()
215 {
216  return Account( gnc_book_get_root_account( m_book ), this );
217 }
218 
219 
220 GCW::Dbo::GnuCash::Account
221 GCW::Dbo::GnuCash::Book::
222 accountByGuid( const std::string _guid )
223 {
224  return GCW::Dbo::GnuCash::Account();
225 }
226 
227 guint
228 GCW::Dbo::GnuCash::Book::
229 count_transactions()
230 {
231  return gnc_book_count_transactions( m_book );
232 }
233 
234 
235 
236 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
237 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
238 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
239 
240 namespace {
241 
242 std::string _to_string( const char * s )
243 {
244  std::string retVal;
245 
246  if( s )
247  retVal = s;
248 
249  return retVal;
250 }
251 
252 } // endnamespace {
253 
254 GCW::Dbo::GnuCash::Account::
255 Account( ::Account * _gncAccount, Book * _book )
256 : m_gncAccount( _gncAccount ),
257  m_book( _book )
258 {
259 }
260 
261 GCW::Dbo::GnuCash::Account::
262 Account( const Account & _copy )
263 : m_gncAccount( _copy.m_gncAccount ),
264  m_book( _copy.m_book )
265 {
266 }
267 
268 ::Account *
269 GCW::Dbo::GnuCash::Account::
270 gncAccount()
271 {
272  return m_gncAccount;
273 }
274 
275 GCW::Dbo::GnuCash::Book *
276 GCW::Dbo::GnuCash::Account::
277 book()
278 {
279  return m_book;
280 }
281 
282 std::string
284 guid()
285 {
286  return _to_string( guid_to_string( xaccAccountGetGUID( gncAccount() ) ) );
287 }
288 
289 std::string
291 name()
292 {
293  return _to_string( xaccAccountGetName( gncAccount() ) );
294 }
295 
296 std::string
298 code()
299 {
300  return _to_string( xaccAccountGetCode( gncAccount() ) );
301 }
302 
303 std::string
305 description()
306 {
307  return _to_string( xaccAccountGetDescription( gncAccount() ) );
308 }
309 
310 std::string
312 notes()
313 {
314  return _to_string( xaccAccountGetNotes( gncAccount() ) );
315 }
316 
317 std::vector< GCW::Dbo::GnuCash::Account >
318 GCW::Dbo::GnuCash::Account::
319 children()
320 {
321  std::vector< GCW::Dbo::GnuCash::Account > retVal;
322 
323  for( int i=0; i< gnc_account_n_children( gncAccount() ); i++ )
324  retVal.push_back( GCW::Dbo::GnuCash::Account( gnc_account_nth_child( gncAccount(), i ), book() ) );
325 
326  return retVal;
327 
328 } // endchildren()
329 
330 #endif
331 
332 
auto open(const std::string &_path) -> bool
Open a database.
const Wt::WFormModel::Field name
Definition: Accounts.cpp:47
const Wt::WFormModel::Field guid
Definition: Accounts.cpp:46
const Wt::WFormModel::Field description
Definition: Accounts.cpp:54
const Wt::WFormModel::Field code
Definition: Accounts.cpp:53
auto rootAccount() -> Item::Ptr
Load Root Account.
Definition: Accounts.cpp:126
const Wt::WFormModel::Field notes
Definition: Definition.h:20