1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2019 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-archive.h"
27 /****************************************************************************/
29 /****************************************************************************/
38 /* our global datas */
39 extern struct HomeBank
*GLOBALS
;
42 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
46 da_archive_clean(Archive
*item
)
50 if(item
->memo
!= NULL
)
60 //5.3 added as it was a leak
61 if(item
->tags
!= NULL
)
66 if(item
->splits
!= NULL
)
68 da_split_destroy(item
->splits
);
70 item
->flags
&= ~(OF_SPLIT
); //Flag that Splits are cleared
76 void da_archive_free(Archive
*item
)
80 da_archive_clean(item
);
86 Archive
*da_archive_malloc(void)
90 item
= rc_alloc(sizeof(Archive
));
96 Archive
*da_archive_clone(Archive
*src_item
)
98 Archive
*new_item
= rc_dup(src_item
, sizeof(Archive
));
102 //duplicate the string
103 new_item
->memo
= g_strdup(src_item
->memo
);
106 //no g_free here to avoid free the src tags (memdup copie dthe ptr)
107 new_item
->tags
= tags_clone(src_item
->tags
);
110 //no g_free here to avoid free the src tags (memdup copie dthe ptr)
111 new_item
->splits
= da_splits_clone(src_item
->splits
);
112 if( da_splits_length (new_item
->splits
) > 0 )
113 new_item
->flags
|= OF_SPLIT
; //Flag that Splits are active
119 void da_archive_destroy(GList
*list
)
121 GList
*tmplist
= g_list_first(list
);
123 while (tmplist
!= NULL
)
125 Archive
*item
= tmplist
->data
;
126 da_archive_free(item
);
127 tmplist
= g_list_next(tmplist
);
133 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
136 static gint
da_archive_glist_compare_func(Archive
*a
, Archive
*b
)
138 return hb_string_utf8_compare(a
->memo
, b
->memo
);
142 GList
*da_archive_sort(GList
*list
)
144 return g_list_sort(list
, (GCompareFunc
)da_archive_glist_compare_func
);
148 guint
da_archive_length(void)
150 return g_list_length(GLOBALS
->arc_list
);
154 /* append a fav with an existing key (from xml file only) */
156 da_archive_append(Archive
*item
)
158 GLOBALS
->arc_list
= g_list_append(GLOBALS
->arc_list
, item
);
164 da_archive_append_new(Archive
*item
)
166 item
->key
= da_archive_get_max_key() + 1;
167 GLOBALS
->arc_list
= g_list_append(GLOBALS
->arc_list
, item
);
173 da_archive_get_max_key(void)
175 GList
*tmplist
= g_list_first(GLOBALS
->arc_list
);
178 while (tmplist
!= NULL
)
180 Archive
*item
= tmplist
->data
;
182 max_key
= MAX(item
->key
, max_key
);
183 tmplist
= g_list_next(tmplist
);
191 da_archive_get(guint32 key
)
194 Archive
*retval
= NULL
;
196 tmplist
= g_list_first(GLOBALS
->arc_list
);
197 while (tmplist
!= NULL
)
199 Archive
*item
= tmplist
->data
;
206 tmplist
= g_list_next(tmplist
);
212 void da_archive_consistency(Archive
*item
)
219 // check category exists
220 cat
= da_cat_get(item
->kcat
);
223 g_warning("arc consistency: fixed invalid cat %d", item
->kcat
);
225 GLOBALS
->changes_count
++;
228 //#1340142 check split category
229 if( item
->splits
!= NULL
)
231 nbsplit
= da_splits_consistency(item
->splits
);
232 //# 1416624 empty category when split
233 if(nbsplit
> 0 && item
->kcat
> 0)
235 g_warning("txn consistency: fixed invalid cat on split txn");
237 GLOBALS
->changes_count
++;
241 // check payee exists
242 pay
= da_pay_get(item
->kpay
);
245 g_warning("arc consistency: fixed invalid pay %d", item
->kpay
);
247 GLOBALS
->changes_count
++;
250 // reset dst acc for non xfer transaction
251 if( item
->paymode
!= PAYMODE_INTXFER
)
254 // delete automation if dst_acc not exists
255 if(item
->paymode
== PAYMODE_INTXFER
)
257 acc
= da_acc_get(item
->kxferacc
);
260 item
->flags
&= ~(OF_AUTO
); //delete flag
266 /* = = = = = = = = = = = = = = = = = = = = */
268 Archive
*da_archive_init_from_transaction(Archive
*arc
, Transaction
*txn
)
270 DB( g_print("\n[scheduled] init from txn\n") );
273 arc
->amount
= txn
->amount
;
274 arc
->kacc
= txn
->kacc
;
275 arc
->kxferacc
= txn
->kxferacc
;
276 arc
->paymode
= txn
->paymode
;
277 arc
->flags
= txn
->flags
& (OF_INCOME
);
278 arc
->status
= txn
->status
;
279 arc
->kpay
= txn
->kpay
;
280 arc
->kcat
= txn
->kcat
;
281 if(txn
->memo
!= NULL
)
282 arc
->memo
= g_strdup(txn
->memo
);
284 arc
->memo
= g_strdup(_("(new archive)"));
286 arc
->tags
= tags_clone(txn
->tags
);
287 arc
->splits
= da_splits_clone(txn
->splits
);
288 if( da_splits_length (arc
->splits
) > 0 )
289 arc
->flags
|= OF_SPLIT
; //Flag that Splits are active
295 static guint32
_sched_date_get_next_post(GDate
*tmpdate
, Archive
*arc
, guint32 nextdate
)
297 guint32 nextpostdate
= nextdate
;
299 //DB( g_print("\n[scheduled] date_get_next_post\n") );
301 g_date_set_julian(tmpdate
, nextpostdate
);
303 //DB( g_print("in : %2d-%2d-%4d\n", g_date_get_day(tmpdate), g_date_get_month (tmpdate), g_date_get_year(tmpdate) ) );
308 g_date_add_days(tmpdate
, arc
->every
);
311 g_date_add_days(tmpdate
, 7 * arc
->every
);
313 case AUTO_UNIT_MONTH
:
314 g_date_add_months(tmpdate
, arc
->every
);
317 g_date_add_years(tmpdate
, arc
->every
);
321 //DB( g_print("out: %2d-%2d-%4d\n", g_date_get_day(tmpdate), g_date_get_month (tmpdate), g_date_get_year(tmpdate) ) );
324 /* get the final post date and free */
325 nextpostdate
= g_date_get_julian(tmpdate
);
331 gboolean
scheduled_is_postable(Archive
*arc
)
335 value
= hb_amount_round(arc
->amount
, 2);
336 if( (arc
->flags
& OF_AUTO
) && (arc
->kacc
> 0) && (value
!= 0.0) )
343 guint32
scheduled_get_postdate(Archive
*arc
, guint32 postdate
)
347 guint32 finalpostdate
;
350 DB( g_print("\n[scheduled] get_postdate\n") );
353 finalpostdate
= postdate
;
355 tmpdate
= g_date_new_julian(finalpostdate
);
356 /* manage weekend exception */
357 if( arc
->weekend
> 0 )
359 wday
= g_date_get_weekday(tmpdate
);
361 DB( g_print(" %s wday=%d\n", arc
->memo
, wday
) );
363 if( wday
>= G_DATE_SATURDAY
)
367 case 1: /* shift before : sun 7-5=+2 , sat 6-5=+1 */
368 shift
= wday
- G_DATE_FRIDAY
;
369 DB( g_print("sub=%d\n", shift
) );
370 g_date_subtract_days (tmpdate
, shift
);
373 case 2: /* shift after : sun 8-7=1 , sat 8-6=2 */
375 DB( g_print("add=%d\n", shift
) );
376 g_date_add_days (tmpdate
, shift
);
382 /* get the final post date and free */
383 finalpostdate
= g_date_get_julian(tmpdate
);
384 g_date_free(tmpdate
);
386 return finalpostdate
;
390 guint32
scheduled_get_latepost_count(Archive
*arc
, guint32 jrefdate
)
396 //DB( g_print("\n[scheduled] get_latepost_count\n") );
399 curdate = jrefdate - arc->nextdate;
403 nbpost = (curdate / arc->every);
404 g_print("debug d: %d => %f\n", curdate, nbpost);
408 nbpost = (curdate / ( 7 * arc->every));
409 g_print("debug w: %d => %f\n", curdate, nbpost);
412 case AUTO_UNIT_MONTH:
413 //approximate is sufficient
414 nbpost = (curdate / (( 365.2425 / 12) * arc->every));
415 g_print("debug m: %d => %f\n", curdate, nbpost);
419 //approximate is sufficient
420 nbpost = (curdate / ( 365.2425 * arc->every));
421 g_print("debug y: %d => %f\n", curdate, nbpost);
425 nblate = floor(nbpost);
427 if(arc->flags & OF_LIMIT)
428 nblate = MIN(nblate, arc->limit);
430 nblate = MIN(nblate, 11);
435 post_date
= g_date_new();
436 curdate
= arc
->nextdate
;
437 while(curdate
<= jrefdate
)
439 curdate
= _sched_date_get_next_post(post_date
, arc
, curdate
);
441 // break if over limit or at 11 max (to display +10)
442 if( nblate
>= 11 || ( (arc
->flags
& OF_LIMIT
) && (nblate
>= arc
->limit
) ) )
446 //DB( g_print(" nblate=%d\n", nblate) );
448 g_date_free(post_date
);
454 /* return 0 is max number of post is reached */
455 guint32
scheduled_date_advance(Archive
*arc
)
460 DB( g_print("\n[scheduled] date_advance\n") );
462 DB( g_print(" arc: '%s'\n", arc
->memo
) );
464 post_date
= g_date_new();
465 g_date_set_julian(post_date
, arc
->nextdate
);
466 // saved the current day number
467 lastday
= g_date_get_day(post_date
);
469 arc
->nextdate
= _sched_date_get_next_post(post_date
, arc
, arc
->nextdate
);
471 DB( g_print(" raw next post date: %2d-%2d-%4d\n", g_date_get_day(post_date
), g_date_get_month (post_date
), g_date_get_year(post_date
) ) );
473 //for day > 28 we might have a gap to compensate later
474 if( (arc
->unit
==AUTO_UNIT_MONTH
) || (arc
->unit
==AUTO_UNIT_YEAR
) )
478 DB( g_print(" lastday:%d, daygap:%d\n", lastday
, arc
->daygap
) );
479 if( arc
->daygap
> 0 )
481 g_date_add_days (post_date
, arc
->daygap
);
482 arc
->nextdate
= g_date_get_julian (post_date
);
483 lastday
+= arc
->daygap
;
484 DB( g_print(" adjusted post date: %2d-%2d-%4d\n", g_date_get_day(post_date
), g_date_get_month (post_date
), g_date_get_year(post_date
) ) );
487 arc
->daygap
= CLAMP(lastday
- g_date_get_day(post_date
), 0, 3);
489 DB( g_print(" daygap is %d\n", arc
->daygap
) );
497 /* check limit, update and maybe break */
498 if(arc
->flags
& OF_LIMIT
)
503 arc
->flags
^= (OF_LIMIT
| OF_AUTO
); // invert flags
508 g_date_free(post_date
);
510 return arc
->nextdate
;
515 * return the maximum date a scheduled txn can be posted to
517 guint32
scheduled_date_get_post_max(void)
520 GDate
*today
, *maxdate
;
522 DB( g_print("\n[scheduled] date_get_post_max\n") );
524 //add until xx of the next month (excluded)
525 if(GLOBALS
->auto_smode
== 0)
527 DB( g_print(" - max is %d of next month\n", GLOBALS
->auto_weekday
) );
529 today
= g_date_new_julian(GLOBALS
->today
);
531 //we compute user xx weekday of next month
532 maxdate
= g_date_new_julian(GLOBALS
->today
);
533 g_date_set_day(maxdate
, GLOBALS
->auto_weekday
);
534 if(g_date_get_day (today
) >= GLOBALS
->auto_weekday
)
535 g_date_add_months(maxdate
, 1);
537 nbdays
= g_date_days_between(today
, maxdate
);
539 g_date_free(maxdate
);
544 nbdays
= GLOBALS
->auto_nbdays
;
547 DB( hb_print_date(GLOBALS
->today
, "today") );
548 DB( g_print(" - %d nbdays\n", nbdays
) );
549 DB( hb_print_date(GLOBALS
->today
+ nbdays
, "maxpostdate") );
551 return GLOBALS
->today
+ nbdays
;
555 gint
scheduled_post_all_pending(void)
562 DB( g_print("\n[scheduled] post_all_pending\n") );
566 maxpostdate
= scheduled_date_get_post_max();
568 txn
= da_transaction_malloc();
570 list
= g_list_first(GLOBALS
->arc_list
);
573 Archive
*arc
= list
->data
;
575 DB( g_print("\n eval %d for '%s'\n", scheduled_is_postable(arc
), arc
->memo
) );
577 if(scheduled_is_postable(arc
) == TRUE
)
579 DB( g_print(" - every %d limit %d (to %d)\n", arc
->every
, arc
->flags
& OF_LIMIT
, arc
->limit
) );
580 DB( hb_print_date(arc
->nextdate
, "next post") );
582 if(arc
->nextdate
< maxpostdate
)
584 guint32 mydate
= arc
->nextdate
;
586 while(mydate
< maxpostdate
)
588 DB( hb_print_date(mydate
, arc
->memo
) );
590 da_transaction_init_from_template(txn
, arc
);
591 txn
->date
= scheduled_get_postdate(arc
, mydate
);
592 /* todo: ? fill in cheque number */
594 transaction_add(NULL
, txn
);
595 GLOBALS
->changes_count
++;
598 da_transaction_clean(txn
);
600 mydate
= scheduled_date_advance(arc
);
602 //DB( hb_print_date(mydate, "next on") );
611 list
= g_list_next(list
);
614 da_transaction_free (txn
);