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