]>
Dogcows Code - chaz/homebank/blob - src/hb-filter.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2014 Maxime DOYEN
4 * This file is part of HomeBank.
6 * HomeBank is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * HomeBank is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "hb-filter.h"
23 /****************************************************************************/
25 /****************************************************************************/
34 /* our global datas */
35 extern struct HomeBank
*GLOBALS
;
36 extern struct Preferences
*PREFS
;
41 /* = = = = = = = = = = = = = = = = = = = = */
44 Filter
*da_filter_malloc(void)
46 return g_malloc0(sizeof(Filter
));
49 void da_filter_free(Filter
*flt
)
60 /* = = = = = = = = = = = = = = = = = = = = */
62 gchar
*filter_daterange_text_get(Filter
*flt
)
68 date
= g_date_new_julian(flt
->mindate
);
69 g_date_strftime (buffer1
, 128-1, PREFS
->date_format
, date
);
70 g_date_set_julian(date
, flt
->maxdate
);
71 g_date_strftime (buffer2
, 128-1, PREFS
->date_format
, date
);
74 return g_strdup_printf(_("<i>from</i> %s <i>to</i> %s"), buffer1
, buffer2
);
79 static void filter_default_date_set(Filter
*flt
)
81 flt
->mindate
= 693596; //01/01/1900
82 flt
->maxdate
= 803533; //31/12/2200
86 static void filter_clear(Filter
*flt
)
90 for(i
=0;i
<FILTER_MAX
;i
++)
106 void filter_default_all_set(Filter
*flt
)
110 DB( g_print("(filter) reset %p\n", flt
) );
114 flt
->range
= FLT_RANGE_LAST12MONTHS
;
115 flt
->type
= FLT_TYPE_ALL
;
116 flt
->status
= FLT_STATUS_ALL
;
118 flt
->option
[FILTER_DATE
] = 1;
119 filter_default_date_set(flt
);
121 for(i
=0;i
<NUM_PAYMODE_MAX
;i
++)
122 flt
->paymode
[i
] = TRUE
;
124 filter_preset_daterange_set(flt
, flt
->range
);
129 void filter_preset_daterange_set(Filter
*flt
, gint range
)
133 guint32 refjuliandate
, month
, year
, qnum
;
135 // any date :: todo : get date of current accout only when account
137 if(g_list_length(GLOBALS
->ope_list
) > 0) // get all transaction date bound
139 GLOBALS
->ope_list
= da_transaction_sort(GLOBALS
->ope_list
);
140 list
= g_list_first(GLOBALS
->ope_list
);
141 flt
->mindate
= ((Transaction
*)list
->data
)->date
;
142 list
= g_list_last(GLOBALS
->ope_list
);
143 flt
->maxdate
= ((Transaction
*)list
->data
)->date
;
146 filter_default_date_set(flt
);
149 // by default refjuliandate is today
150 // but we adjust if to max transaction date found
151 refjuliandate
= GLOBALS
->today
;
152 if(flt
->maxdate
< refjuliandate
)
153 refjuliandate
= flt
->maxdate
;
155 date
= g_date_new_julian(refjuliandate
);
156 month
= g_date_get_month(date
);
157 year
= g_date_get_year(date
);
158 qnum
= ((month
- 1) / 3) + 1;
160 DB( g_print("m=%d, y=%d, qnum=%d\n", month
, year
, qnum
) );
164 case FLT_RANGE_THISMONTH
:
165 g_date_set_day(date
, 1);
166 flt
->mindate
= g_date_get_julian(date
);
167 g_date_add_days(date
, g_date_get_days_in_month(month
, year
)-1);
168 flt
->maxdate
= g_date_get_julian(date
);
171 case FLT_RANGE_LASTMONTH
:
172 g_date_set_day(date
, 1);
173 g_date_subtract_months(date
, 1);
174 flt
->mindate
= g_date_get_julian(date
);
175 month
= g_date_get_month(date
);
176 year
= g_date_get_year(date
);
177 g_date_add_days(date
, g_date_get_days_in_month(month
, year
)-1);
178 flt
->maxdate
= g_date_get_julian(date
);
181 case FLT_RANGE_THISQUARTER
:
182 g_date_set_day(date
, 1);
183 g_date_set_month(date
, (qnum
-1)*3+1);
184 flt
->mindate
= g_date_get_julian(date
);
185 g_date_add_months(date
, 3);
186 g_date_subtract_days(date
, 1);
187 flt
->maxdate
= g_date_get_julian(date
);
190 case FLT_RANGE_LASTQUARTER
:
191 g_date_set_day(date
, 1);
192 g_date_set_month(date
, (qnum
-1)*3+1);
193 g_date_subtract_months(date
, 3);
194 flt
->mindate
= g_date_get_julian(date
);
195 g_date_add_months(date
, 3);
196 g_date_subtract_days(date
, 1);
197 flt
->maxdate
= g_date_get_julian(date
);
200 case FLT_RANGE_THISYEAR
:
201 g_date_set_dmy(date
, PREFS
->fisc_year_day
, PREFS
->fisc_year_month
, year
);
202 if( refjuliandate
>= g_date_get_julian (date
))
204 flt
->mindate
= g_date_get_julian(date
);
208 g_date_set_dmy(date
, PREFS
->fisc_year_day
, PREFS
->fisc_year_month
, year
-1);
209 flt
->mindate
= g_date_get_julian(date
);
211 g_date_add_years (date
, 1);
212 g_date_subtract_days (date
, 1);
213 flt
->maxdate
= g_date_get_julian(date
);
216 case FLT_RANGE_LASTYEAR
:
217 g_date_set_dmy(date
, PREFS
->fisc_year_day
, PREFS
->fisc_year_month
, year
);
218 if( refjuliandate
>= g_date_get_julian (date
))
220 g_date_set_dmy(date
, PREFS
->fisc_year_day
, PREFS
->fisc_year_month
, year
-1);
221 flt
->mindate
= g_date_get_julian(date
);
225 g_date_set_dmy(date
, PREFS
->fisc_year_day
, PREFS
->fisc_year_month
, year
-2);
226 flt
->mindate
= g_date_get_julian(date
);
228 g_date_add_years (date
, 1);
229 g_date_subtract_days (date
, 1);
230 flt
->maxdate
= g_date_get_julian(date
);
233 case FLT_RANGE_LAST30DAYS
:
234 flt
->mindate
= refjuliandate
- 30;
235 flt
->maxdate
= refjuliandate
;
238 case FLT_RANGE_LAST60DAYS
:
239 flt
->mindate
= refjuliandate
- 60;
240 flt
->maxdate
= refjuliandate
;
243 case FLT_RANGE_LAST90DAYS
:
244 flt
->mindate
= refjuliandate
- 90;
245 flt
->maxdate
= refjuliandate
;
248 case FLT_RANGE_LAST12MONTHS
:
249 g_date_subtract_months(date
, 12);
250 flt
->mindate
= g_date_get_julian(date
);
251 flt
->maxdate
= refjuliandate
;
254 // case FLT_RANGE_OTHER:
256 // case FLT_RANGE_ALLDATE:
264 void filter_preset_type_set(Filter
*flt
, gint type
)
269 flt
->option
[FILTER_AMOUNT
] = 0;
270 flt
->minamount
= G_MINDOUBLE
;
271 flt
->maxamount
= G_MINDOUBLE
;
275 case FLT_TYPE_EXPENSE
:
276 flt
->option
[FILTER_AMOUNT
] = 1;
277 flt
->minamount
= -G_MAXDOUBLE
;
278 flt
->maxamount
= G_MINDOUBLE
;
281 case FLT_TYPE_INCOME
:
282 flt
->option
[FILTER_AMOUNT
] = 1;
283 flt
->minamount
= G_MINDOUBLE
;
284 flt
->maxamount
= G_MAXDOUBLE
;
291 void filter_preset_status_set(Filter
*flt
, gint status
)
297 flt
->status
= status
;
298 flt
->option
[FILTER_STATUS
] = 0;
299 flt
->reconciled
= TRUE
;
300 flt
->reminded
= TRUE
;
301 flt
->forceadd
= FALSE
;
302 flt
->forcechg
= FALSE
;
304 flt
->option
[FILTER_CATEGORY
] = 0;
305 lcat
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
308 catitem
= list
->data
;
309 catitem
->filter
= FALSE
;
310 list
= g_list_next(list
);
316 case FLT_STATUS_UNCATEGORIZED
:
317 flt
->option
[FILTER_CATEGORY
] = 1;
318 catitem
= da_cat_get(0); // no category
319 catitem
->filter
= TRUE
;
322 case FLT_STATUS_UNRECONCILED
:
323 flt
->option
[FILTER_STATUS
] = 2;
324 flt
->reconciled
= TRUE
;
326 flt
->reminded
= FALSE
;
333 static gint
filter_text_compare(gchar
*txntext
, gchar
*searchtext
, gboolean exact
)
339 if( g_strstr_len(txntext
, -1, searchtext
) != NULL
)
341 DB( g_print(" found case '%s'\n", searchtext
) );
347 gchar
*word
= g_utf8_casefold(txntext
, -1);
348 gchar
*needle
= g_utf8_casefold(searchtext
, -1);
350 if( g_strrstr(word
, needle
) != NULL
)
352 DB( g_print(" found nocase '%s'\n", needle
) );
363 /* used for quicksearch text into transaction */
364 gboolean
filter_txn_search_match(gchar
*needle
, Transaction
*txn
, gint flags
)
366 gboolean retval
= FALSE
;
371 if(flags
& FLT_QSEARCH_MEMO
)
375 retval
|= filter_text_compare(txn
->wording
, needle
, FALSE
);
380 if(flags
& FLT_QSEARCH_INFO
)
384 retval
|= filter_text_compare(txn
->info
, needle
, FALSE
);
389 if(flags
& FLT_QSEARCH_PAYEE
)
391 payitem
= da_pay_get(txn
->kpay
);
394 retval
|= filter_text_compare(payitem
->name
, needle
, FALSE
);
399 if(flags
& FLT_QSEARCH_CATEGORY
)
401 catitem
= da_cat_get(txn
->kcat
);
404 gchar
*fullname
= da_cat_get_fullname (catitem
);
406 retval
|= filter_text_compare(fullname
, needle
, FALSE
);
412 if(flags
& FLT_QSEARCH_TAGS
)
414 tags
= transaction_tags_tostring(txn
);
417 retval
|= filter_text_compare(tags
, needle
, FALSE
);
420 //if(retval) goto end;
429 gint
filter_test(Filter
*flt
, Transaction
*txn
)
436 //DB( g_print("(filter) test\n") );
440 /*** start filtering ***/
442 /* add/change force */
443 if(flt
->forceadd
== TRUE
&& (txn
->flags
& OF_ADDED
))
446 if(flt
->forcechg
== TRUE
&& (txn
->flags
& OF_CHANGED
))
450 if(flt
->option
[FILTER_DATE
]) {
451 insert
= ( (txn
->date
>= flt
->mindate
) && (txn
->date
<= flt
->maxdate
) ) ? 1 : 0;
452 if(flt
->option
[FILTER_DATE
] == 2) insert
^= 1;
454 if(!insert
) goto end
;
457 if(flt
->option
[FILTER_ACCOUNT
]) {
458 accitem
= da_acc_get(txn
->kacc
);
461 insert
= ( accitem
->filter
== TRUE
) ? 1 : 0;
462 if(flt
->option
[FILTER_ACCOUNT
] == 2) insert
^= 1;
465 if(!insert
) goto end
;
468 if(flt
->option
[FILTER_PAYEE
]) {
469 payitem
= da_pay_get(txn
->kpay
);
472 insert
= ( payitem
->filter
== TRUE
) ? 1 : 0;
473 if(flt
->option
[FILTER_PAYEE
] == 2) insert
^= 1;
476 if(!insert
) goto end
;
479 if(flt
->option
[FILTER_CATEGORY
]) {
480 if(txn
->flags
& OF_SPLIT
)
485 insert
= 0; //fix: 1151259
486 count
= da_transaction_splits_count(txn
);
491 split
= txn
->splits
[i
];
492 catitem
= da_cat_get(split
->kcat
);
495 tmpinsert
= ( catitem
->filter
== TRUE
) ? 1 : 0;
496 if(flt
->option
[FILTER_CATEGORY
] == 2) tmpinsert
^= 1;
503 catitem
= da_cat_get(txn
->kcat
);
506 insert
= ( catitem
->filter
== TRUE
) ? 1 : 0;
507 if(flt
->option
[FILTER_CATEGORY
] == 2) insert
^= 1;
511 if(!insert
) goto end
;
514 if(flt
->option
[FILTER_STATUS
]) {
515 gint insert1
= 0, insert2
= 0;
518 insert1
= ( txn
->flags
& OF_VALID
) ? 1 : 0;
520 insert2
= ( txn
->flags
& OF_REMIND
) ? 1 : 0;
522 insert
= insert1
| insert2
;
523 if(flt
->option
[FILTER_STATUS
] == 2) insert
^= 1;
525 if(!insert
) goto end
;
528 if(flt
->option
[FILTER_PAYMODE
]) {
529 insert
= ( flt
->paymode
[txn
->paymode
] == TRUE
) ? 1 : 0;
530 if(flt
->option
[FILTER_PAYMODE
] == 2) insert
^= 1;
532 if(!insert
) goto end
;
535 if(flt
->option
[FILTER_AMOUNT
]) {
536 insert
= ( (txn
->amount
>= flt
->minamount
) && (txn
->amount
<= flt
->maxamount
) ) ? 1 : 0;
538 if(flt
->option
[FILTER_AMOUNT
] == 2) insert
^= 1;
540 if(!insert
) goto end
;
542 /* info/wording/tag */
543 if(flt
->option
[FILTER_TEXT
])
546 gint insert1
, insert2
, insert3
;
548 insert1
= insert2
= insert3
= 0;
553 insert1
= filter_text_compare(txn
->info
, flt
->info
, flt
->exact
);
563 insert2
= filter_text_compare(txn
->wording
, flt
->wording
, flt
->exact
);
571 tags
= transaction_tags_tostring(txn
);
574 insert3
= filter_text_compare(tags
, flt
->tag
, flt
->exact
);
581 insert
= insert1
&& insert2
&& insert3
? 1 : 0;
583 if(flt
->option
[FILTER_TEXT
] == 2) insert
^= 1;
586 if(!insert
) goto end
;
589 // DB( g_print(" %d :: %d :: %d\n", flt->mindate, txn->date, flt->maxdate) );
590 // DB( g_print(" [%d] %s => %d (%d)\n", txn->account, txn->wording, insert, count) );
This page took 0.056276 seconds and 5 git commands to generate.