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 Archive
*da_archive_malloc(void)
46 item
= rc_alloc(sizeof(Archive
));
52 Archive
*da_archive_clone(Archive
*src_item
)
54 Archive
*new_item
= rc_dup(src_item
, sizeof(Archive
));
58 //duplicate the string
59 new_item
->memo
= g_strdup(src_item
->memo
);
62 //no g_free here to avoid free the src tags (memdup copie dthe ptr)
63 new_item
->tags
= tags_clone(src_item
->tags
);
66 //no g_free here to avoid free the src tags (memdup copie dthe ptr)
67 new_item
->splits
= da_splits_clone(src_item
->splits
);
68 if( da_splits_length (new_item
->splits
) > 0 )
69 new_item
->flags
|= OF_SPLIT
; //Flag that Splits are active
75 void da_archive_free(Archive
*item
)
79 if(item
->memo
!= NULL
)
81 if(item
->splits
!= NULL
)
82 da_split_destroy(item
->splits
);
88 void da_archive_destroy(GList
*list
)
90 GList
*tmplist
= g_list_first(list
);
92 while (tmplist
!= NULL
)
94 Archive
*item
= tmplist
->data
;
95 da_archive_free(item
);
96 tmplist
= g_list_next(tmplist
);
102 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
105 static gint
da_archive_glist_compare_func(Archive
*a
, Archive
*b
)
107 return hb_string_utf8_compare(a
->memo
, b
->memo
);
111 GList
*da_archive_sort(GList
*list
)
113 return g_list_sort(list
, (GCompareFunc
)da_archive_glist_compare_func
);
117 guint
da_archive_length(void)
119 return g_list_length(GLOBALS
->arc_list
);
123 /* append a fav with an existing key (from xml file only) */
125 da_archive_append(Archive
*item
)
127 GLOBALS
->arc_list
= g_list_append(GLOBALS
->arc_list
, item
);
133 da_archive_append_new(Archive
*item
)
135 item
->key
= da_archive_get_max_key() + 1;
136 GLOBALS
->arc_list
= g_list_append(GLOBALS
->arc_list
, item
);
142 da_archive_get_max_key(void)
144 GList
*tmplist
= g_list_first(GLOBALS
->arc_list
);
147 while (tmplist
!= NULL
)
149 Archive
*item
= tmplist
->data
;
151 max_key
= MAX(item
->key
, max_key
);
152 tmplist
= g_list_next(tmplist
);
160 da_archive_get(guint32 key
)
163 Archive
*retval
= NULL
;
165 tmplist
= g_list_first(GLOBALS
->arc_list
);
166 while (tmplist
!= NULL
)
168 Archive
*item
= tmplist
->data
;
175 tmplist
= g_list_next(tmplist
);
181 void da_archive_consistency(Archive
*item
)
188 // check category exists
189 cat
= da_cat_get(item
->kcat
);
192 g_warning("arc consistency: fixed invalid cat %d", item
->kcat
);
194 GLOBALS
->changes_count
++;
197 //#1340142 check split category
198 if( item
->splits
!= NULL
)
200 nbsplit
= da_splits_consistency(item
->splits
);
201 //# 1416624 empty category when split
202 if(nbsplit
> 0 && item
->kcat
> 0)
204 g_warning("txn consistency: fixed invalid cat on split txn");
206 GLOBALS
->changes_count
++;
210 // check payee exists
211 pay
= da_pay_get(item
->kpay
);
214 g_warning("arc consistency: fixed invalid pay %d", item
->kpay
);
216 GLOBALS
->changes_count
++;
219 // reset dst acc for non xfer transaction
220 if( item
->paymode
!= PAYMODE_INTXFER
)
223 // delete automation if dst_acc not exists
224 if(item
->paymode
== PAYMODE_INTXFER
)
226 acc
= da_acc_get(item
->kxferacc
);
229 item
->flags
&= ~(OF_AUTO
); //delete flag
235 /* = = = = = = = = = = = = = = = = = = = = */
237 Archive
*da_archive_init_from_transaction(Archive
*arc
, Transaction
*txn
)
239 DB( g_print("\n[scheduled] init from txn\n") );
242 arc
->amount
= txn
->amount
;
243 arc
->kacc
= txn
->kacc
;
244 arc
->kxferacc
= txn
->kxferacc
;
245 arc
->paymode
= txn
->paymode
;
246 arc
->flags
= txn
->flags
& (OF_INCOME
);
247 arc
->status
= txn
->status
;
248 arc
->kpay
= txn
->kpay
;
249 arc
->kcat
= txn
->kcat
;
250 if(txn
->memo
!= NULL
)
251 arc
->memo
= g_strdup(txn
->memo
);
253 arc
->memo
= g_strdup(_("(new archive)"));
255 arc
->tags
= tags_clone(txn
->tags
);
256 arc
->splits
= da_splits_clone(txn
->splits
);
257 if( da_splits_length (arc
->splits
) > 0 )
258 arc
->flags
|= OF_SPLIT
; //Flag that Splits are active
264 static guint32
_sched_date_get_next_post(GDate
*tmpdate
, Archive
*arc
, guint32 nextdate
)
266 guint32 nextpostdate
= nextdate
;
268 //DB( g_print("\n[scheduled] date_get_next_post\n") );
270 g_date_set_julian(tmpdate
, nextpostdate
);
272 //DB( g_print("in : %2d-%2d-%4d\n", g_date_get_day(tmpdate), g_date_get_month (tmpdate), g_date_get_year(tmpdate) ) );
277 g_date_add_days(tmpdate
, arc
->every
);
280 g_date_add_days(tmpdate
, 7 * arc
->every
);
282 case AUTO_UNIT_MONTH
:
283 g_date_add_months(tmpdate
, arc
->every
);
286 g_date_add_years(tmpdate
, arc
->every
);
290 //DB( g_print("out: %2d-%2d-%4d\n", g_date_get_day(tmpdate), g_date_get_month (tmpdate), g_date_get_year(tmpdate) ) );
293 /* get the final post date and free */
294 nextpostdate
= g_date_get_julian(tmpdate
);
300 gboolean
scheduled_is_postable(Archive
*arc
)
304 value
= hb_amount_round(arc
->amount
, 2);
305 if( (arc
->flags
& OF_AUTO
) && (arc
->kacc
> 0) && (value
!= 0.0) )
312 guint32
scheduled_get_postdate(Archive
*arc
, guint32 postdate
)
316 guint32 finalpostdate
;
319 DB( g_print("\n[scheduled] get_postdate\n") );
322 finalpostdate
= postdate
;
324 tmpdate
= g_date_new_julian(finalpostdate
);
325 /* manage weekend exception */
326 if( arc
->weekend
> 0 )
328 wday
= g_date_get_weekday(tmpdate
);
330 DB( g_print(" %s wday=%d\n", arc
->memo
, wday
) );
332 if( wday
>= G_DATE_SATURDAY
)
336 case 1: /* shift before : sun 7-5=+2 , sat 6-5=+1 */
337 shift
= wday
- G_DATE_FRIDAY
;
338 DB( g_print("sub=%d\n", shift
) );
339 g_date_subtract_days (tmpdate
, shift
);
342 case 2: /* shift after : sun 8-7=1 , sat 8-6=2 */
344 DB( g_print("add=%d\n", shift
) );
345 g_date_add_days (tmpdate
, shift
);
351 /* get the final post date and free */
352 finalpostdate
= g_date_get_julian(tmpdate
);
353 g_date_free(tmpdate
);
355 return finalpostdate
;
359 guint32
scheduled_get_latepost_count(Archive
*arc
, guint32 jrefdate
)
365 //DB( g_print("\n[scheduled] get_latepost_count\n") );
368 curdate = jrefdate - arc->nextdate;
372 nbpost = (curdate / arc->every);
373 g_print("debug d: %d => %f\n", curdate, nbpost);
377 nbpost = (curdate / ( 7 * arc->every));
378 g_print("debug w: %d => %f\n", curdate, nbpost);
381 case AUTO_UNIT_MONTH:
382 //approximate is sufficient
383 nbpost = (curdate / (( 365.2425 / 12) * arc->every));
384 g_print("debug m: %d => %f\n", curdate, nbpost);
388 //approximate is sufficient
389 nbpost = (curdate / ( 365.2425 * arc->every));
390 g_print("debug y: %d => %f\n", curdate, nbpost);
394 nblate = floor(nbpost);
396 if(arc->flags & OF_LIMIT)
397 nblate = MIN(nblate, arc->limit);
399 nblate = MIN(nblate, 11);
404 post_date
= g_date_new();
405 curdate
= arc
->nextdate
;
406 while(curdate
<= jrefdate
)
408 curdate
= _sched_date_get_next_post(post_date
, arc
, curdate
);
410 // break if over limit or at 11 max (to display +10)
411 if( nblate
>= 11 || ( (arc
->flags
& OF_LIMIT
) && (nblate
>= arc
->limit
) ) )
415 //DB( g_print(" nblate=%d\n", nblate) );
417 g_date_free(post_date
);
423 /* return 0 is max number of post is reached */
424 guint32
scheduled_date_advance(Archive
*arc
)
429 DB( g_print("\n[scheduled] date_advance\n") );
431 DB( g_print(" arc: '%s'\n", arc
->memo
) );
433 post_date
= g_date_new();
434 g_date_set_julian(post_date
, arc
->nextdate
);
435 // saved the current day number
436 lastday
= g_date_get_day(post_date
);
438 arc
->nextdate
= _sched_date_get_next_post(post_date
, arc
, arc
->nextdate
);
440 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
) ) );
442 //for day > 28 we might have a gap to compensate later
443 if( (arc
->unit
==AUTO_UNIT_MONTH
) || (arc
->unit
==AUTO_UNIT_YEAR
) )
447 DB( g_print(" lastday:%d, daygap:%d\n", lastday
, arc
->daygap
) );
448 if( arc
->daygap
> 0 )
450 g_date_add_days (post_date
, arc
->daygap
);
451 arc
->nextdate
= g_date_get_julian (post_date
);
452 lastday
+= arc
->daygap
;
453 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
) ) );
456 arc
->daygap
= CLAMP(lastday
- g_date_get_day(post_date
), 0, 3);
458 DB( g_print(" daygap is %d\n", arc
->daygap
) );
466 /* check limit, update and maybe break */
467 if(arc
->flags
& OF_LIMIT
)
472 arc
->flags
^= (OF_LIMIT
| OF_AUTO
); // invert flags
477 g_date_free(post_date
);
479 return arc
->nextdate
;
484 * return the maximum date a scheduled txn can be posted to
486 guint32
scheduled_date_get_post_max(void)
489 GDate
*today
, *maxdate
;
491 DB( g_print("\n[scheduled] date_get_post_max\n") );
493 //add until xx of the next month (excluded)
494 if(GLOBALS
->auto_smode
== 0)
496 DB( g_print(" - max is %d of next month\n", GLOBALS
->auto_weekday
) );
498 today
= g_date_new_julian(GLOBALS
->today
);
500 //we compute user xx weekday of next month
501 maxdate
= g_date_new_julian(GLOBALS
->today
);
502 g_date_set_day(maxdate
, GLOBALS
->auto_weekday
);
503 if(g_date_get_day (today
) >= GLOBALS
->auto_weekday
)
504 g_date_add_months(maxdate
, 1);
506 nbdays
= g_date_days_between(today
, maxdate
);
508 g_date_free(maxdate
);
513 nbdays
= GLOBALS
->auto_nbdays
;
516 DB( hb_print_date(GLOBALS
->today
, "today") );
517 DB( g_print(" - %d nbdays\n", nbdays
) );
518 DB( hb_print_date(GLOBALS
->today
+ nbdays
, "maxpostdate") );
520 return GLOBALS
->today
+ nbdays
;
524 gint
scheduled_post_all_pending(void)
531 DB( g_print("\n[scheduled] post_all_pending\n") );
535 maxpostdate
= scheduled_date_get_post_max();
537 txn
= da_transaction_malloc();
539 list
= g_list_first(GLOBALS
->arc_list
);
542 Archive
*arc
= list
->data
;
544 DB( g_print("\n eval %d for '%s'\n", scheduled_is_postable(arc
), arc
->memo
) );
546 if(scheduled_is_postable(arc
) == TRUE
)
548 DB( g_print(" - every %d limit %d (to %d)\n", arc
->every
, arc
->flags
& OF_LIMIT
, arc
->limit
) );
549 DB( hb_print_date(arc
->nextdate
, "next post") );
551 if(arc
->nextdate
< maxpostdate
)
553 guint32 mydate
= arc
->nextdate
;
555 while(mydate
< maxpostdate
)
557 DB( hb_print_date(mydate
, arc
->memo
) );
559 da_transaction_init_from_template(txn
, arc
);
560 txn
->date
= scheduled_get_postdate(arc
, mydate
);
561 /* todo: ? fill in cheque number */
563 transaction_add(NULL
, txn
);
564 GLOBALS
->changes_count
++;
567 da_transaction_clean(txn
);
569 mydate
= scheduled_date_advance(arc
);
571 //DB( hb_print_date(mydate, "next on") );
580 list
= g_list_next(list
);
583 da_transaction_free (txn
);