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