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"
24 /****************************************************************************/
26 /****************************************************************************/
35 /* our global datas */
36 extern struct HomeBank
*GLOBALS
;
39 Archive
*da_archive_malloc(void)
43 item
= g_malloc0(sizeof(Archive
));
49 Archive
*da_archive_clone(Archive
*src_item
)
51 Archive
*new_item
= g_memdup(src_item
, sizeof(Archive
));
55 //duplicate the string
56 new_item
->memo
= g_strdup(src_item
->memo
);
59 //no g_free here to avoid free the src tags (memdup copie dthe ptr)
60 new_item
->tags
= tags_clone(src_item
->tags
);
63 //no g_free here to avoid free the src tags (memdup copie dthe ptr)
64 new_item
->splits
= da_splits_clone(src_item
->splits
);
65 if( da_splits_length (new_item
->splits
) > 0 )
66 new_item
->flags
|= OF_SPLIT
; //Flag that Splits are active
72 void da_archive_free(Archive
*item
)
76 if(item
->memo
!= NULL
)
78 if(item
->splits
!= NULL
)
79 da_split_destroy(item
->splits
);
85 void da_archive_destroy(GList
*list
)
87 GList
*tmplist
= g_list_first(list
);
89 while (tmplist
!= NULL
)
91 Archive
*item
= tmplist
->data
;
92 da_archive_free(item
);
93 tmplist
= g_list_next(tmplist
);
99 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
102 static gint
da_archive_glist_compare_func(Archive
*a
, Archive
*b
)
104 return hb_string_utf8_compare(a
->memo
, b
->memo
);
108 GList
*da_archive_sort(GList
*list
)
110 return g_list_sort(list
, (GCompareFunc
)da_archive_glist_compare_func
);
114 guint
da_archive_length(void)
116 return g_list_length(GLOBALS
->arc_list
);
120 /* append a fav with an existing key (from xml file only) */
122 da_archive_append(Archive
*item
)
124 GLOBALS
->arc_list
= g_list_append(GLOBALS
->arc_list
, item
);
130 da_archive_append_new(Archive
*item
)
132 item
->key
= da_archive_get_max_key() + 1;
133 GLOBALS
->arc_list
= g_list_append(GLOBALS
->arc_list
, item
);
139 da_archive_get_max_key(void)
141 GList
*tmplist
= g_list_first(GLOBALS
->arc_list
);
144 while (tmplist
!= NULL
)
146 Archive
*item
= tmplist
->data
;
148 max_key
= MAX(item
->key
, max_key
);
149 tmplist
= g_list_next(tmplist
);
157 da_archive_get(guint32 key
)
160 Archive
*retval
= NULL
;
162 tmplist
= g_list_first(GLOBALS
->arc_list
);
163 while (tmplist
!= NULL
)
165 Archive
*item
= tmplist
->data
;
172 tmplist
= g_list_next(tmplist
);
178 void da_archive_consistency(Archive
*item
)
185 // check category exists
186 cat
= da_cat_get(item
->kcat
);
189 g_warning("arc consistency: fixed invalid cat %d", item
->kcat
);
191 GLOBALS
->changes_count
++;
194 //#1340142 check split category
195 if( item
->splits
!= NULL
)
197 nbsplit
= da_splits_consistency(item
->splits
);
198 //# 1416624 empty category when split
199 if(nbsplit
> 0 && item
->kcat
> 0)
201 g_warning("txn consistency: fixed invalid cat on split txn");
203 GLOBALS
->changes_count
++;
207 // check payee exists
208 pay
= da_pay_get(item
->kpay
);
211 g_warning("arc consistency: fixed invalid pay %d", item
->kpay
);
213 GLOBALS
->changes_count
++;
216 // reset dst acc for non xfer transaction
217 if( item
->paymode
!= PAYMODE_INTXFER
)
220 // delete automation if dst_acc not exists
221 if(item
->paymode
== PAYMODE_INTXFER
)
223 acc
= da_acc_get(item
->kxferacc
);
226 item
->flags
&= ~(OF_AUTO
); //delete flag
232 /* = = = = = = = = = = = = = = = = = = = = */
234 Archive
*da_archive_init_from_transaction(Archive
*arc
, Transaction
*txn
)
236 DB( g_print("\n[scheduled] init from txn\n") );
239 arc
->amount
= txn
->amount
;
240 arc
->kacc
= txn
->kacc
;
241 arc
->kxferacc
= txn
->kxferacc
;
242 arc
->paymode
= txn
->paymode
;
243 arc
->flags
= txn
->flags
& (OF_INCOME
);
244 arc
->status
= txn
->status
;
245 arc
->kpay
= txn
->kpay
;
246 arc
->kcat
= txn
->kcat
;
247 if(txn
->memo
!= NULL
)
248 arc
->memo
= g_strdup(txn
->memo
);
250 arc
->memo
= g_strdup(_("(new archive)"));
252 arc
->tags
= tags_clone(txn
->tags
);
253 arc
->splits
= da_splits_clone(txn
->splits
);
254 if( da_splits_length (arc
->splits
) > 0 )
255 arc
->flags
|= OF_SPLIT
; //Flag that Splits are active
261 static guint32
_sched_date_get_next_post(GDate
*tmpdate
, Archive
*arc
, guint32 nextdate
)
263 guint32 nextpostdate
= nextdate
;
265 //DB( g_print("\n[scheduled] date_get_next_post\n") );
267 g_date_set_julian(tmpdate
, nextpostdate
);
269 //DB( g_print("in : %2d-%2d-%4d\n", g_date_get_day(tmpdate), g_date_get_month (tmpdate), g_date_get_year(tmpdate) ) );
274 g_date_add_days(tmpdate
, arc
->every
);
277 g_date_add_days(tmpdate
, 7 * arc
->every
);
279 case AUTO_UNIT_MONTH
:
280 g_date_add_months(tmpdate
, arc
->every
);
283 g_date_add_years(tmpdate
, arc
->every
);
287 //DB( g_print("out: %2d-%2d-%4d\n", g_date_get_day(tmpdate), g_date_get_month (tmpdate), g_date_get_year(tmpdate) ) );
290 /* get the final post date and free */
291 nextpostdate
= g_date_get_julian(tmpdate
);
297 gboolean
scheduled_is_postable(Archive
*arc
)
301 value
= hb_amount_round(arc
->amount
, 2);
302 if( (arc
->flags
& OF_AUTO
) && (arc
->kacc
> 0) && (value
!= 0.0) )
309 guint32
scheduled_get_postdate(Archive
*arc
, guint32 postdate
)
313 guint32 finalpostdate
;
316 DB( g_print("\n[scheduled] get_postdate\n") );
319 finalpostdate
= postdate
;
321 tmpdate
= g_date_new_julian(finalpostdate
);
322 /* manage weekend exception */
323 if( arc
->weekend
> 0 )
325 wday
= g_date_get_weekday(tmpdate
);
327 DB( g_print(" %s wday=%d\n", arc
->memo
, wday
) );
329 if( wday
>= G_DATE_SATURDAY
)
333 case 1: /* shift before : sun 7-5=+2 , sat 6-5=+1 */
334 shift
= wday
- G_DATE_FRIDAY
;
335 DB( g_print("sub=%d\n", shift
) );
336 g_date_subtract_days (tmpdate
, shift
);
339 case 2: /* shift after : sun 8-7=1 , sat 8-6=2 */
341 DB( g_print("add=%d\n", shift
) );
342 g_date_add_days (tmpdate
, shift
);
348 /* get the final post date and free */
349 finalpostdate
= g_date_get_julian(tmpdate
);
350 g_date_free(tmpdate
);
352 return finalpostdate
;
356 guint32
scheduled_get_latepost_count(Archive
*arc
, guint32 jrefdate
)
362 //DB( g_print("\n[scheduled] get_latepost_count\n") );
365 curdate = jrefdate - arc->nextdate;
369 nbpost = (curdate / arc->every);
370 g_print("debug d: %d => %f\n", curdate, nbpost);
374 nbpost = (curdate / ( 7 * arc->every));
375 g_print("debug w: %d => %f\n", curdate, nbpost);
378 case AUTO_UNIT_MONTH:
379 //approximate is sufficient
380 nbpost = (curdate / (( 365.2425 / 12) * arc->every));
381 g_print("debug m: %d => %f\n", curdate, nbpost);
385 //approximate is sufficient
386 nbpost = (curdate / ( 365.2425 * arc->every));
387 g_print("debug y: %d => %f\n", curdate, nbpost);
391 nblate = floor(nbpost);
393 if(arc->flags & OF_LIMIT)
394 nblate = MIN(nblate, arc->limit);
396 nblate = MIN(nblate, 11);
401 post_date
= g_date_new();
402 curdate
= arc
->nextdate
;
403 while(curdate
<= jrefdate
)
405 curdate
= _sched_date_get_next_post(post_date
, arc
, curdate
);
407 // break if over limit or at 11 max (to display +10)
408 if( nblate
>= 11 || ( (arc
->flags
& OF_LIMIT
) && (nblate
>= arc
->limit
) ) )
412 //DB( g_print(" nblate=%d\n", nblate) );
414 g_date_free(post_date
);
420 /* return 0 is max number of post is reached */
421 guint32
scheduled_date_advance(Archive
*arc
)
426 DB( g_print("\n[scheduled] date_advance\n") );
428 DB( g_print(" arc: '%s'\n", arc
->memo
) );
430 post_date
= g_date_new();
431 g_date_set_julian(post_date
, arc
->nextdate
);
432 // saved the current day number
433 lastday
= g_date_get_day(post_date
);
435 arc
->nextdate
= _sched_date_get_next_post(post_date
, arc
, arc
->nextdate
);
437 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
) ) );
439 //for day > 28 we might have a gap to compensate later
440 if( (arc
->unit
==AUTO_UNIT_MONTH
) || (arc
->unit
==AUTO_UNIT_YEAR
) )
444 DB( g_print(" lastday:%d, daygap:%d\n", lastday
, arc
->daygap
) );
445 if( arc
->daygap
> 0 )
447 g_date_add_days (post_date
, arc
->daygap
);
448 arc
->nextdate
= g_date_get_julian (post_date
);
449 lastday
+= arc
->daygap
;
450 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
) ) );
453 arc
->daygap
= CLAMP(lastday
- g_date_get_day(post_date
), 0, 3);
455 DB( g_print(" daygap is %d\n", arc
->daygap
) );
463 /* check limit, update and maybe break */
464 if(arc
->flags
& OF_LIMIT
)
469 arc
->flags
^= (OF_LIMIT
| OF_AUTO
); // invert flags
474 g_date_free(post_date
);
476 return arc
->nextdate
;
481 * return the maximum date a scheduled txn can be posted to
483 guint32
scheduled_date_get_post_max(void)
486 GDate
*today
, *maxdate
;
488 DB( g_print("\n[scheduled] date_get_post_max\n") );
490 //add until xx of the next month (excluded)
491 if(GLOBALS
->auto_smode
== 0)
493 DB( g_print(" - max is %d of next month\n", GLOBALS
->auto_weekday
) );
495 today
= g_date_new_julian(GLOBALS
->today
);
497 //we compute user xx weekday of next month
498 maxdate
= g_date_new_julian(GLOBALS
->today
);
499 g_date_set_day(maxdate
, GLOBALS
->auto_weekday
);
500 if(g_date_get_day (today
) >= GLOBALS
->auto_weekday
)
501 g_date_add_months(maxdate
, 1);
503 nbdays
= g_date_days_between(today
, maxdate
);
505 g_date_free(maxdate
);
510 nbdays
= GLOBALS
->auto_nbdays
;
513 DB( hb_print_date(GLOBALS
->today
, "today") );
514 DB( g_print(" - %d nbdays\n", nbdays
) );
515 DB( hb_print_date(GLOBALS
->today
+ nbdays
, "maxpostdate") );
517 return GLOBALS
->today
+ nbdays
;
521 gint
scheduled_post_all_pending(void)
528 DB( g_print("\n[scheduled] post_all_pending\n") );
532 maxpostdate
= scheduled_date_get_post_max();
534 txn
= da_transaction_malloc();
536 list
= g_list_first(GLOBALS
->arc_list
);
539 Archive
*arc
= list
->data
;
541 DB( g_print("\n eval %d for '%s'\n", scheduled_is_postable(arc
), arc
->memo
) );
543 if(scheduled_is_postable(arc
) == TRUE
)
545 DB( g_print(" - every %d limit %d (to %d)\n", arc
->every
, arc
->flags
& OF_LIMIT
, arc
->limit
) );
546 DB( hb_print_date(arc
->nextdate
, "next post") );
548 if(arc
->nextdate
< maxpostdate
)
550 guint32 mydate
= arc
->nextdate
;
552 while(mydate
< maxpostdate
)
554 DB( hb_print_date(mydate
, arc
->memo
) );
556 da_transaction_init_from_template(txn
, arc
);
557 txn
->date
= scheduled_get_postdate(arc
, mydate
);
558 /* todo: ? fill in cheque number */
560 transaction_add(NULL
, txn
);
561 GLOBALS
->changes_count
++;
564 da_transaction_clean(txn
);
566 mydate
= scheduled_date_advance(arc
);
568 //DB( hb_print_date(mydate, "next on") );
577 list
= g_list_next(list
);
580 da_transaction_free (txn
);