GnuCashew ~ GnuCash Enabled Web
GCW
AccountsTreeViewModel.cpp
Go to the documentation of this file.
1 #line 2 "src/Eng/AccountsTreeViewModel.cpp"
2 
3 #include <any>
4 
5 #include <Wt/Json/Array.h>
6 #include <Wt/Json/Parser.h>
7 #include <Wt/Json/Serializer.h>
8 #include <Wt/WText.h>
9 #include <Wt/WTreeTableNode.h>
10 #include <Wt/WVBoxLayout.h>
11 
12 #include "../define.h"
13 #include "../GnuCashew.h"
14 #include "AccountsTreeViewModel.h"
15 
16 auto
18 load( int _columnCount )-> void
19 {
20  m_columnCount = _columnCount;
21 
22  /*
23  ** If the session isn't open then there's nothing to load.
24  **
25  */
26  if( !GCW::app()-> gnucashew_session().isOpen() )
27  return;
28 
29  /*
30  ** load the data in to the model
31  **
32  */
34  if( rootAccount-> guid() != "" )
35  {
36  load( invisibleRootItem(), rootAccount );
37 
38  /*
39  ** define all the columns
40  **
41  */
42  int col = 0;
43 // std::cout << __FILE__ << ":" << __LINE__ << " " << col << " " << m_columnCount << std::endl;
44 
45  if( col < m_columnCount )
46  {
47 // std::cout << __FILE__ << ":" << __LINE__ << " " << col << " " << m_columnCount << std::endl;
48  setHeaderData( col++, TR( "gcw.AccountsTreeView.column.accountname" ) );
49  }
50 
51  if( col < m_columnCount )
52  {
53 // std::cout << __FILE__ << ":" << __LINE__ << " " << col << " " << m_columnCount << std::endl;
54  setHeaderData( col++, TR( "gcw.AccountsTreeView.column.accountcode" ) );
55  }
56 
57  if( col < m_columnCount )
58  {
59 // std::cout << __FILE__ << ":" << __LINE__ << " " << col << " " << m_columnCount << std::endl;
60  setHeaderData( col++, TR( "gcw.AccountsTreeView.column.description" ) );
61  }
62 
63  if( col < m_columnCount )
64  {
65 // std::cout << __FILE__ << ":" << __LINE__ << " " << col << " " << m_columnCount << std::endl;
66  setHeaderData( col++, TR( "gcw.AccountsTreeView.column.taxinfo" ) );
67  }
68 
69  if( col < m_columnCount )
70  {
71 // std::cout << __FILE__ << ":" << __LINE__ << " " << col << " " << m_columnCount << std::endl;
72  setHeaderData( col++, TR( "gcw.AccountsTreeView.column.notes" ) );
73  }
74 
75  if( col < m_columnCount )
76  {
77 // std::cout << __FILE__ << ":" << __LINE__ << " " << col << " " << m_columnCount << std::endl;
78  setHeaderData( col++, TR( "gcw.AccountsTreeView.column.futureminimumusd" ) );
79  }
80 
81  if( col < m_columnCount )
82  {
83 // std::cout << __FILE__ << ":" << __LINE__ << " " << col << " " << m_columnCount << std::endl;
84  setHeaderData( col++, TR( "gcw.AccountsTreeView.column.total" ) );
85  }
86 
87  } // endif( rootAccount-> guid() != "" )
88 
89 // std::cout << __FILE__ << ":" << __LINE__ << " columnCount:" << columnCount() << std::endl;
90 
91 } // endload( int _columnCount )-> void
92 
93 auto
95 load( Wt::WStandardItem * _treeItem, GCW::Dbo::Accounts::Item::Ptr _parentAccount )-> void
96 {
97  Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
98 
99  /*
100  ** This 'builds' a row of account info, with the account
101  ** 'name' on the first column, and other bits about the account
102  ** on the remaining columns.
103  **
104  */
105  auto _append = [=]( Wt::WStandardItem * _item, GCW::Dbo::Accounts::Item::Ptr _accountItem )
106  {
107  std::vector< std::unique_ptr< Wt::WStandardItem > > columns;
108 
109  auto accountName = std::make_unique< Wt::WStandardItem >( _accountItem-> name() );
110  auto retVal = accountName.get();
111  accountName-> setToolTip( _accountItem-> guid() );
112 
113 #ifdef NEVER
114  if( _accountItem-> hasColor() )
115  {
116  auto accountColor = _accountItem-> color();
117  std::cout << __FILE__ << ":" << __LINE__ << " " << accountColor << std::endl;
118  }
119 #endif
120 
121  /*
122  ** set the 'model->data::User' element to contain the guid of the account, so
123  ** we can recover it later.
124  **
125  */
126  accountName-> setData( _accountItem-> guid(), Wt::ItemDataRole::User );
127  int col = 0;
128 // std::cout << __FILE__ << ":" << __LINE__ << " " << col << " " << m_columnCount << std::endl;
129 
130  if( col++ < m_columnCount )
131  {
132 // std::cout << __FILE__ << ":" << __LINE__ << " " << col << " " << m_columnCount << std::endl;
133  columns.push_back( std::move( accountName ) );
134  }
135 
136  if( col++ < m_columnCount )
137  {
138 // std::cout << __FILE__ << ":" << __LINE__ << " " << col << " " << m_columnCount << std::endl;
139  columns.push_back( std::make_unique< Wt::WStandardItem >( _accountItem-> code () ) );
140  }
141 
142  if( col++ < m_columnCount )
143  {
144 // std::cout << __FILE__ << ":" << __LINE__ << " " << col << " " << m_columnCount << std::endl;
145  columns.push_back( std::make_unique< Wt::WStandardItem >( _accountItem-> description () ) );
146  }
147 
148  if( col++ < m_columnCount )
149  {
150 // std::cout << __FILE__ << ":" << __LINE__ << " " << col << " " << m_columnCount << std::endl;
151  columns.push_back( std::make_unique< Wt::WStandardItem >() );
152  }
153 
154  if( col++ < m_columnCount )
155  {
156 // std::cout << __FILE__ << ":" << __LINE__ << " " << col << " " << m_columnCount << std::endl;
157  columns.push_back( std::make_unique< Wt::WStandardItem >() );
158  }
159 
160  if( col++ < m_columnCount )
161  {
162 // std::cout << __FILE__ << ":" << __LINE__ << " " << col << " " << m_columnCount << std::endl;
163  columns.push_back( std::make_unique< Wt::WStandardItem >() );
164  }
165 
166  if( col++ < m_columnCount )
167  {
168 // std::cout << __FILE__ << ":" << __LINE__ << " " << col << " " << m_columnCount << std::endl;
169  columns.push_back( std::make_unique< Wt::WStandardItem >() );
170  }
171 
172  _item-> appendRow( std::move( columns ) );
173 
174  return retVal;
175 
176  }; // end_append()
177 
178  /*
179  ** Pick up all the accounts that are children to the
180  ** specified account.
181  **
182  */
183  auto childAccounts =
184  GCW::app()-> gnucashew_session().find< GCW::Dbo::Accounts::Item >()
185  .where( "parent_guid = ?" )
186  .bind( _parentAccount-> guid() )
187  .resultList()
188  ;
189 
190  /*
191  ** Loop through each child-account for this parent account
192  **
193  */
194  for( auto account : childAccounts )
195  {
196  /*
197  ** Append the tree-item
198  **
199  */
200  auto ti = _append( _treeItem, account );
201 
202  /*
203  ** Perform a recursive-load on this account to pick up
204  ** any more child accounts
205  **
206  */
207  load( ti, account );
208 
209  } // endfor( auto account : accounts )
210 
211 } // endload( Wt::WStandardItem * _treeItem, GCW::Dbo::Accounts::Item::Ptr _parentAccount )-> void
212 
213 
Account Item Class.
Definition: Item.h:89
Wt::Dbo::ptr< Item > Ptr
Definition: BaseItem.h:39
auto load(int _columnCount) -> void
#define TR(X)
Definition: define.h:17
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
auto load(const std::string &_guid) -> Item::Ptr
Load Account by GUID.
Definition: Accounts.cpp:135
App * app()
Definition: App.cpp:67