1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2018 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)
41 return g_malloc0(sizeof(Archive
));
45 Archive
*da_archive_clone(Archive
*src_item
)
47 Archive
*new_item
= g_memdup(src_item
, sizeof(Archive
));
51 //duplicate the string
52 new_item
->memo
= g_strdup(src_item
->memo
);
54 if( da_splits_clone(src_item
->splits
, new_item
->splits
) > 0)
55 new_item
->flags
|= OF_SPLIT
; //Flag that Splits are active
61 void da_archive_free(Archive
*item
)
65 if(item
->memo
!= NULL
)
68 da_splits_free(item
->splits
);
69 //item->flags &= ~(OF_SPLIT); //Flag that Splits are cleared
76 void da_archive_destroy(GList
*list
)
78 GList
*tmplist
= g_list_first(list
);
80 while (tmplist
!= NULL
)
82 Archive
*item
= tmplist
->data
;
83 da_archive_free(item
);
84 tmplist
= g_list_next(tmplist
);
90 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
93 static gint
da_archive_glist_compare_func(Archive
*a
, Archive
*b
)
95 return hb_string_utf8_compare(a
->memo
, b
->memo
);
99 GList
*da_archive_sort(GList
*list
)
101 return g_list_sort(list
, (GCompareFunc
)da_archive_glist_compare_func
);
105 guint
da_archive_length(void)
107 return g_list_length(GLOBALS
->arc_list
);
111 void da_archive_consistency(Archive
*item
)
117 // check category exists
118 cat
= da_cat_get(item
->kcat
);
121 g_warning("arc consistency: fixed invalid cat %d", item
->kcat
);
123 GLOBALS
->changes_count
++;
126 split_cat_consistency(item
->splits
);
128 // check payee exists
129 pay
= da_pay_get(item
->kpay
);
132 g_warning("arc consistency: fixed invalid pay %d", item
->kpay
);
134 GLOBALS
->changes_count
++;
137 // reset dst acc for non xfer transaction
138 if( item
->paymode
!= PAYMODE_INTXFER
)
141 // delete automation if dst_acc not exists
142 if(item
->paymode
== PAYMODE_INTXFER
)
144 acc
= da_acc_get(item
->kxferacc
);
147 item
->flags
&= ~(OF_AUTO
); //delete flag
153 /* = = = = = = = = = = = = = = = = = = = = */
155 Archive
*da_archive_init_from_transaction(Archive
*arc
, Transaction
*txn
)
157 DB( g_print("\n[scheduled] init from txn\n") );
160 arc
->amount
= txn
->amount
;
161 arc
->kacc
= txn
->kacc
;
162 arc
->kxferacc
= txn
->kxferacc
;
163 arc
->paymode
= txn
->paymode
;
164 arc
->flags
= txn
->flags
& (OF_INCOME
);
165 arc
->status
= txn
->status
;
166 arc
->kpay
= txn
->kpay
;
167 arc
->kcat
= txn
->kcat
;
168 if(txn
->memo
!= NULL
)
169 arc
->memo
= g_strdup(txn
->memo
);
171 arc
->memo
= g_strdup(_("(new archive)"));
173 if( da_splits_clone(txn
->splits
, arc
->splits
) > 0)
174 arc
->flags
|= OF_SPLIT
; //Flag that Splits are active
180 static guint32
_sched_date_get_next_post(GDate
*tmpdate
, Archive
*arc
, guint32 nextdate
)
182 guint32 nextpostdate
= nextdate
;
184 //DB( g_print("\n[scheduled] date_get_next_post\n") );
186 g_date_set_julian(tmpdate
, nextpostdate
);
188 //DB( g_print("in : %2d-%2d-%4d\n", g_date_get_day(tmpdate), g_date_get_month (tmpdate), g_date_get_year(tmpdate) ) );
193 g_date_add_days(tmpdate
, arc
->every
);
196 g_date_add_days(tmpdate
, 7 * arc
->every
);
198 case AUTO_UNIT_MONTH
:
199 g_date_add_months(tmpdate
, arc
->every
);
202 g_date_add_years(tmpdate
, arc
->every
);
206 //DB( g_print("out: %2d-%2d-%4d\n", g_date_get_day(tmpdate), g_date_get_month (tmpdate), g_date_get_year(tmpdate) ) );
209 /* get the final post date and free */
210 nextpostdate
= g_date_get_julian(tmpdate
);
216 gboolean
scheduled_is_postable(Archive
*arc
)
220 value
= hb_amount_round(arc
->amount
, 2);
221 if( (arc
->flags
& OF_AUTO
) && (arc
->kacc
> 0) && (value
!= 0.0) )
228 guint32
scheduled_get_postdate(Archive
*arc
, guint32 postdate
)
232 guint32 finalpostdate
;
235 DB( g_print("\n[scheduled] get_postdate\n") );
238 finalpostdate
= postdate
;
240 tmpdate
= g_date_new_julian(finalpostdate
);
241 /* manage weekend exception */
242 if( arc
->weekend
> 0 )
244 wday
= g_date_get_weekday(tmpdate
);
246 DB( g_print(" %s wday=%d\n", arc
->memo
, wday
) );
248 if( wday
>= G_DATE_SATURDAY
)
252 case 1: /* shift before : sun 7-5=+2 , sat 6-5=+1 */
253 shift
= wday
- G_DATE_FRIDAY
;
254 DB( g_print("sub=%d\n", shift
) );
255 g_date_subtract_days (tmpdate
, shift
);
258 case 2: /* shift after : sun 8-7=1 , sat 8-6=2 */
260 DB( g_print("add=%d\n", shift
) );
261 g_date_add_days (tmpdate
, shift
);
267 /* get the final post date and free */
268 finalpostdate
= g_date_get_julian(tmpdate
);
269 g_date_free(tmpdate
);
271 return finalpostdate
;
275 guint32
scheduled_get_latepost_count(Archive
*arc
, guint32 jrefdate
)
281 //DB( g_print("\n[scheduled] get_latepost_count\n") );
284 curdate = jrefdate - arc->nextdate;
288 nbpost = (curdate / arc->every);
289 g_print("debug d: %d => %f\n", curdate, nbpost);
293 nbpost = (curdate / ( 7 * arc->every));
294 g_print("debug w: %d => %f\n", curdate, nbpost);
297 case AUTO_UNIT_MONTH:
298 //approximate is sufficient
299 nbpost = (curdate / (( 365.2425 / 12) * arc->every));
300 g_print("debug m: %d => %f\n", curdate, nbpost);
304 //approximate is sufficient
305 nbpost = (curdate / ( 365.2425 * arc->every));
306 g_print("debug y: %d => %f\n", curdate, nbpost);
310 nblate = floor(nbpost);
312 if(arc->flags & OF_LIMIT)
313 nblate = MIN(nblate, arc->limit);
315 nblate = MIN(nblate, 11);
320 post_date
= g_date_new();
321 curdate
= arc
->nextdate
;
322 while(curdate
<= jrefdate
)
324 curdate
= _sched_date_get_next_post(post_date
, arc
, curdate
);
326 // break if over limit or at 11 max (to display +10)
327 if(nblate
>= arc
->limit
|| nblate
>= 11)
331 //DB( g_print(" nblate=%d\n", nblate) );
333 g_date_free(post_date
);
339 /* return 0 is max number of post is reached */
340 guint32
scheduled_date_advance(Archive
*arc
)
345 DB( g_print("\n[scheduled] date_advance\n") );
347 DB( g_print(" arc: '%s'\n", arc
->memo
) );
349 post_date
= g_date_new();
350 g_date_set_julian(post_date
, arc
->nextdate
);
351 // saved the current day number
352 lastday
= g_date_get_day(post_date
);
354 arc
->nextdate
= _sched_date_get_next_post(post_date
, arc
, arc
->nextdate
);
356 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
) ) );
358 //for day > 28 we might have a gap to compensate later
359 if( (arc
->unit
==AUTO_UNIT_MONTH
) || (arc
->unit
==AUTO_UNIT_YEAR
) )
363 DB( g_print(" lastday:%d, daygap:%d\n", lastday
, arc
->daygap
) );
364 if( arc
->daygap
> 0 )
366 g_date_add_days (post_date
, arc
->daygap
);
367 arc
->nextdate
= g_date_get_julian (post_date
);
368 lastday
+= arc
->daygap
;
369 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
) ) );
372 arc
->daygap
= CLAMP(lastday
- g_date_get_day(post_date
), 0, 3);
374 DB( g_print(" daygap is %d\n", arc
->daygap
) );
382 /* check limit, update and maybe break */
383 if(arc
->flags
& OF_LIMIT
)
388 arc
->flags
^= (OF_LIMIT
| OF_AUTO
); // invert flags
393 g_date_free(post_date
);
395 return arc
->nextdate
;
400 * return the maximum date a scheduled txn can be posted to
402 guint32
scheduled_date_get_post_max(void)
405 GDate
*today
, *maxdate
;
407 DB( g_print("\n[scheduled] date_get_post_max\n") );
409 //add until xx of the next month (excluded)
410 if(GLOBALS
->auto_smode
== 0)
412 DB( g_print(" - max is %d of next month\n", GLOBALS
->auto_weekday
) );
414 today
= g_date_new_julian(GLOBALS
->today
);
416 //we compute user xx weekday of next month
417 maxdate
= g_date_new_julian(GLOBALS
->today
);
418 g_date_set_day(maxdate
, GLOBALS
->auto_weekday
);
419 if(g_date_get_day (today
) >= GLOBALS
->auto_weekday
)
420 g_date_add_months(maxdate
, 1);
422 nbdays
= g_date_days_between(today
, maxdate
);
424 g_date_free(maxdate
);
429 nbdays
= GLOBALS
->auto_nbdays
;
432 DB( hb_print_date(GLOBALS
->today
, "today") );
433 DB( g_print(" - %d nbdays\n", nbdays
) );
434 DB( hb_print_date(GLOBALS
->today
+ nbdays
, "maxpostdate") );
436 return GLOBALS
->today
+ nbdays
;
440 gint
scheduled_post_all_pending(void)
447 DB( g_print("\n[scheduled] post_all_pending\n") );
451 maxpostdate
= scheduled_date_get_post_max();
453 txn
= da_transaction_malloc();
455 list
= g_list_first(GLOBALS
->arc_list
);
458 Archive
*arc
= list
->data
;
460 DB( g_print("\n eval %d for '%s'\n", scheduled_is_postable(arc
), arc
->memo
) );
462 if(scheduled_is_postable(arc
) == TRUE
)
464 DB( g_print(" - every %d limit %d (to %d)\n", arc
->every
, arc
->flags
& OF_LIMIT
, arc
->limit
) );
465 DB( hb_print_date(arc
->nextdate
, "next post") );
467 if(arc
->nextdate
< maxpostdate
)
469 guint32 mydate
= arc
->nextdate
;
471 while(mydate
< maxpostdate
)
473 DB( hb_print_date(mydate
, arc
->memo
) );
475 da_transaction_init_from_template(txn
, arc
);
476 txn
->date
= scheduled_get_postdate(arc
, mydate
);
477 /* todo: ? fill in cheque number */
479 transaction_add(txn
);
480 GLOBALS
->changes_count
++;
483 da_transaction_clean(txn
);
485 mydate
= scheduled_date_advance(arc
);
487 //DB( hb_print_date(mydate, "next on") );
496 list
= g_list_next(list
);
499 da_transaction_free (txn
);