GnuCashew ~ GnuCash Enabled Web
GCW
TableModel.cpp
Go to the documentation of this file.
1 #line 2 "src/Gui/BillPay/TableModel.cpp"
2 
3 #include <Wt/WStandardItem.h>
4 
5 #include "../../App.h"
6 #include "../Dbo/Vars/Vars.h"
7 #include "BillPay.h"
8 
9 namespace {
10 
12 {
13 // name, width, alignment, toolTip
14  { "accountKey" , "120px", Wt::AlignmentFlag::Left, "Primary Account Identifier" },
15  { "last4" , "60px", Wt::AlignmentFlag::Center, "Last 4 digits of account number" },
16  { "Nickname" , "100px", Wt::AlignmentFlag::Left, "Friendly Nickname for the account" },
17  { "Gp" , "50px", Wt::AlignmentFlag::Center, "Grouping" },
18  { "Dy" , "50px", Wt::AlignmentFlag::Center, "Day that the payment is due" },
19  { "Min" , "50px", Wt::AlignmentFlag::Right, "Minimum Payment Due" },
20  { "Bgt" , "50px", Wt::AlignmentFlag::Right, "Payment Budget" },
21  { "Actual" , "75px", Wt::AlignmentFlag::Right, "Actual payment most recently made" },
22  { "Au" , "50px", Wt::AlignmentFlag::Center, "Automatic Payment Indicator" },
23  { "01" , "35px", Wt::AlignmentFlag::Center, "January" },
24  { "02" , "35px", Wt::AlignmentFlag::Center, "February" },
25  { "03" , "35px", Wt::AlignmentFlag::Center, "March" },
26  { "04" , "35px", Wt::AlignmentFlag::Center, "April" },
27  { "05" , "35px", Wt::AlignmentFlag::Center, "May" },
28  { "06" , "35px", Wt::AlignmentFlag::Center, "June" },
29  { "07" , "35px", Wt::AlignmentFlag::Center, "July" },
30  { "08" , "35px", Wt::AlignmentFlag::Center, "August" },
31  { "09" , "35px", Wt::AlignmentFlag::Center, "September" },
32  { "10" , "35px", Wt::AlignmentFlag::Center, "October" },
33  { "11" , "35px", Wt::AlignmentFlag::Center, "November" },
34  { "12" , "35px", Wt::AlignmentFlag::Center, "December" },
35 };
36 
37 #define COLUMN_COUNT (sizeof(columns)/sizeof(GCW::Gui::BillPay::ColumnDef_t))
38 
39 } // endnamespace {
40 
42 TableModel( int _selectedMonth, const Status _status )
43 : Wt::WStandardItemModel( 0, COLUMN_COUNT ),
44  m_status( _status )
45 {
46 
47  /*
48  ** Load the header _only_ on the 'unpaid' view.
49  **
50  ** The unpaid view is represented first in the widget so that
51  ** bills that are unpaid appear at the top of the browser
52  ** window. The unpaid view, therefore, is the only view
53  ** that includes the header. If the other two remaining
54  ** views (Paid, Disabled) also had a header the gui would
55  ** get too cluttered, so those headers are left blank.
56  */
58  {
59  for( int col = 1; col< COLUMN_COUNT; col++ )
60  {
61  setHeaderData( col, Wt::Orientation::Horizontal, columns[ col ].name , Wt::ItemDataRole::Display );
62  setHeaderData( col, Wt::Orientation::Horizontal, columns[ col ].toolTip , Wt::ItemDataRole::ToolTip );
63  }
64  }
65 
66  /*
67  ** Load the data based on the month selected.
68  */
69  loadData( _selectedMonth );
70 
71 } // endTableModel( const Status _status )
72 
73 auto
75 loadData( int _selectedMonth )-> void
76 {
77  /*!
78  ** On load, the first column-label is set to indicate the
79  ** model type as well as the month selected.
80  **
81  ** \code
82  ** Change the label on the column-0, example;
83  ** "03 Unpaid"
84  ** "06 Paid"
85  ** "12 Disabled"
86  ** \endcode
87  **
88  */
89  setHeaderData
90  (
91  0,
92  Wt::Orientation::Horizontal,
93  toString( _selectedMonth ) + " " + asString( m_status ),
94  Wt::ItemDataRole::Display
95  );
96 
97  /*!
98  ** On load, all existing data in the model is first dumped.
99  */
100  while( rowCount() > 0 )
101  takeRow( 0 );
102 
103  /*!
104  ** Get all the var items that are for the 'managed bill pay item' (mbpi)
105  ** in to a resultList that will then be used to load the
106  ** resulting model.
107  */
108  Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
110 
111  /*
112  ** Calculate our yes/no status for grabbing items.
113  */
114  std::string yesNo = "yes";
115  if( m_status == GCW::Gui::BillPay::Status::Pending )
116  yesNo = "maybe";
117  if( m_status == GCW::Gui::BillPay::Status::Unpaid )
118  yesNo = "no";
119 
120  /*!
121  ** Run the resultList collection through an analyzer that will
122  ** extract billpay items that match the selection criteria of
123  ** paid/unpaid/disabled/yes/no accordingly.
124  */
125  std::vector< GCW::Gui::BillPay::Item > bpItems;
126  for( auto item : items )
127  {
128  auto bpItem = GCW::Gui::BillPay::Item( item );
129 
130  /*
131  ** Calculate these boolean.
132  */
133  auto isActive = bpItem.isActive () == "yes";
134  auto isVisible = bpItem.isVisible() == "yes";
135 
136  /*
137  ** This is for Paid and Unpaid (not Disabled).
138  */
139  if( m_status == GCW::Gui::BillPay::Status::Paid
141  || m_status == GCW::Gui::BillPay::Status::Unpaid
142  )
143  {
144  /*
145  ** The item ~must~ be active ~and~ visible.
146  */
147  if( isActive && isVisible )
148  {
149  /*
150  ** Get the .var. string that has the combo-box values for the monthly
151  ** indicators of what has been paid and what has not. Depending on the
152  ** value of that payment status, it must therefore match the yes/no clause
153  ** we calculated above. If it's a match, we grab it.
154  */
155  if( bpItem.cb( _selectedMonth ) == yesNo )
156 // if( i-> getVarString( "cb" + GCW::Gui::BillPay::toString( _selectedMonth ) ) == yesNo )
157  bpItems.push_back( bpItem );
158 
159  } // endif( isActive && isVisible )
160 
161  } // endif( ..is active.. )
162 
163  /*
164  ** This is for Disabled.
165  */
166  else
167  if( m_status == GCW::Gui::BillPay::Status::Disabled ) // capture disabled items here
168  {
169  /*
170  ** Disabled items are either notActive ~or~ notVisible.
171  */
172  if( !isActive || !isVisible )
173  bpItems.push_back( bpItem );
174 
175  } // endelseif( .disabled. )
176 
177  } // endfor( auto i : items )
178 
179  /*!
180  ** Sort all the items by the account group.dueDay. (The
181  ** user is not allowed to sort these views so we do it) This sorts the items
182  ** with the items that are due first, above those that are due next.
183  */
184  sort( bpItems );
185 
186  /*!
187  ** Each item is processed out of the sorted vector and placed
188  ** in to the item model.
189  */
190  for( auto bpItem : bpItems )
191  {
192  /*
193  ** Grab a few handles.
194  */
195  auto accountName = std::make_unique< Wt::WStandardItem >();
196  auto accountGuid = bpItem.accountGuid();
197 
198  if( accountGuid != "" )
199  {
200  auto accountItem = GCW::Dbo::Accounts::byGuid( accountGuid );
201 
202  /*
203  ** set the bpItem.guid so we can edit this row
204  */
205  accountName-> setData( bpItem.guid(), Wt::ItemDataRole::User );
206 
207  /*
208  ** sometimes the 'account' can get lost, so check first the
209  ** account still exists.
210  */
211  if( accountItem )
212  {
213  /*
214  ** build the account column to;
215  ** display the account name
216  ** carry the account-full-name as a toolTip,
217  ** carry the guid of the originating bpItem
218  */
219  accountName-> setData( accountItem-> name() , Wt::ItemDataRole::Display );
220  accountName-> setToolTip( GCW::Dbo::Accounts::fullName( accountGuid ) );
221  }
222 
223  } // endif( accountGuid != "" )
224 
225  /*
226  ** The columns are pushed in to .columns. variable.
227  */
228  std::vector< std::unique_ptr< Wt::WStandardItem > > columns;
229  columns.push_back( std::move( accountName ) );
230  columns.push_back( std::make_unique< Wt::WStandardItem >( bpItem.last4 () ) );
231  columns.push_back( std::make_unique< Wt::WStandardItem >( bpItem.nickname () ) );
232  columns.push_back( std::make_unique< Wt::WStandardItem >( bpItem.group () ) );
233  columns.push_back( std::make_unique< Wt::WStandardItem >( bpItem.dueDay () ) );
234  columns.push_back( std::make_unique< Wt::WStandardItem >( bpItem.minimum () ) );
235  columns.push_back( std::make_unique< Wt::WStandardItem >( bpItem.budget () ) );
236  columns.push_back( std::make_unique< Wt::WStandardItem >( bpItem.actual () ) );
237  columns.push_back( std::make_unique< Wt::WStandardItem >( bpItem.autoPay () ) );
238 
239  /*
240  ** Load 12-columns, one for each month.
241  */
242  for( int month=1; month<= 12; month++ )
243  {
244  auto cb = std::make_unique< Wt::WStandardItem >( bpItem.cbtr( month ) );
245 
246  /*!
247  ** While building the 'month columns', apply a style class to the
248  ** column of items according to the month selected. This causes
249  ** the current selected column to be highlighted within the table
250  ** view in the browser.
251  **
252  ** \image html BillPayColumnSelector.png
253  */
254  if( _selectedMonth == month )
255  cb-> setStyleClass( "colsel" );
256 
257  columns.push_back( std::move( cb ) );
258 
259  } // endfor( int i=1; i<= 12; i++ )
260 
261  /*
262  ** Push everything in to the model.
263  */
264  appendRow( std::move( columns ) );
265 
266  } // endfor( auto bpItem : bpItems )
267 
268 } // endloadData( int _selectedMonth )-> void
269 
270 auto
273 {
274  return columns[col];
275 }
276 
277 auto
279 sort( std::vector< GCW::Gui::BillPay::Item > & _bpItems )-> void
280 {
281  /*!
282  ** Sort the vector of bpItems by group.dueDay
283  */
284  std::sort
285  (
286  _bpItems.begin(),
287  _bpItems.end(),
288  []( const GCW::Gui::BillPay::Item item1,
289  const GCW::Gui::BillPay::Item item2
290  )
291  {
292 // auto account1 = GCW::Dbo::Accounts::byGuid( item1-> keyField() );
293 // auto account2 = GCW::Dbo::Accounts::byGuid( item2-> keyField() );
294 // auto name1 = account1-> name();
295 // auto name2 = account2-> name();
296 
297 // auto name1 = item1.nickname();
298 // auto name2 = item2.nickname();
299 
300  /*
301  ** return .bool. of the comparison
302  */
303  return item1.sortValue()
304  < item2.sortValue()
305  ;
306  }
307  );
308 
309 } // endsort( std::vector< GCW::Gui::BillPay::Item > & _bpItems )-> void
310 
311 
#define GCW_GUI_BILLPAY_ITEM_CFY
Definition: BillPay.h:40
#define COLUMN_COUNT
Definition: TableModel.cpp:37
Bill Pay Item.
Definition: Item.h:23
auto columnDef(int _col) -> ColumnDef_t
Column Definition.
Definition: TableModel.cpp:272
auto loadData(int _selectedMonth) -> void
Reload the data based on the selected month.
Definition: TableModel.cpp:75
Status m_status
Model Status.
Definition: TableModel.h:123
auto sort(std::vector< GCW::Gui::BillPay::Item > &_bpItems) -> void
Sorter.
Definition: TableModel.cpp:279
TableModel(int _selectedMonth, Status _status)
ctor
Definition: TableModel.cpp:42
const Wt::WFormModel::Field name
Definition: Accounts.cpp:47
auto byGuid(const std::string &_guid) -> Item::Ptr
Load Account by GUID.
Definition: Accounts.cpp:172
auto fullName(const std::string &_guid) -> std::string
Account Fullname via GUID.
Definition: Accounts.cpp:292
auto getByCfy(const std::string &_cfyValue) -> GCW::Dbo::Vars::Item::Vector
Definition: Vars.cpp:75
auto bpItem(const std::string &_guid) -> GCW::Gui::BillPay::Item
Bill Pay Item.
Definition: BillPay.cpp:19
std::string asString(Status _status)
Get Status as String.
Definition: Status.cpp:7
Status
Bill Status.
Definition: Status.h:21
@ Pending
Pending Status.
@ Disabled
Disabled Status.
@ Unpaid
Unpaid Status.
auto toString(int _value) -> std::string
Convert Integer to String.
Definition: BillPay.cpp:41
App * app()
Definition: App.cpp:67
Definition: GncLock.h:6