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"
27 /****************************************************************************/
29 /****************************************************************************/
38 /* our global datas */
39 extern struct HomeBank
*GLOBALS
;
42 Archive
*da_archive_malloc(void)
44 return rc_alloc(sizeof(Archive
));
48 Archive
*da_archive_clone(Archive
*src_item
)
50 Archive
*new_item
= rc_dup(src_item
, sizeof(Archive
));
54 //duplicate the string
55 new_item
->memo
= g_strdup(src_item
->memo
);
57 if( da_splits_clone(src_item
->splits
, new_item
->splits
) > 0)
58 new_item
->flags
|= OF_SPLIT
; //Flag that Splits are active
64 void da_archive_free(Archive
*item
)
68 if(item
->memo
!= NULL
)
71 da_splits_free(item
->splits
);
72 //item->flags &= ~(OF_SPLIT); //Flag that Splits are cleared
79 void da_archive_destroy(GList
*list
)
81 GList
*tmplist
= g_list_first(list
);
83 while (tmplist
!= NULL
)
85 Archive
*item
= tmplist
->data
;
86 da_archive_free(item
);
87 tmplist
= g_list_next(tmplist
);
93 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
96 static gint
da_archive_glist_compare_func(Archive
*a
, Archive
*b
)
98 return hb_string_utf8_compare(a
->memo
, b
->memo
);
102 GList
*da_archive_sort(GList
*list
)
104 return g_list_sort(list
, (GCompareFunc
)da_archive_glist_compare_func
);
108 guint
da_archive_length(void)
110 return g_list_length(GLOBALS
->arc_list
);
114 void da_archive_consistency(Archive
*item
)
120 // check category exists
121 cat
= da_cat_get(item
->kcat
);
124 g_warning("arc consistency: fixed invalid cat %d", item
->kcat
);
126 GLOBALS
->changes_count
++;
129 split_cat_consistency(item
->splits
);
131 // check payee exists
132 pay
= da_pay_get(item
->kpay
);
135 g_warning("arc consistency: fixed invalid pay %d", item
->kpay
);
137 GLOBALS
->changes_count
++;
140 // reset dst acc for non xfer transaction
141 if( item
->paymode
!= PAYMODE_INTXFER
)
144 // delete automation if dst_acc not exists
145 if(item
->paymode
== PAYMODE_INTXFER
)
147 acc
= da_acc_get(item
->kxferacc
);
150 item
->flags
&= ~(OF_AUTO
); //delete flag
156 /* = = = = = = = = = = = = = = = = = = = = */
158 Archive
*da_archive_init_from_transaction(Archive
*arc
, Transaction
*txn
)
160 DB( g_print("\n[scheduled] init from txn\n") );
163 arc
->amount
= txn
->amount
;
164 arc
->kacc
= txn
->kacc
;
165 arc
->kxferacc
= txn
->kxferacc
;
166 arc
->paymode
= txn
->paymode
;
167 arc
->flags
= txn
->flags
& (OF_INCOME
);
168 arc
->status
= txn
->status
;
169 arc
->kpay
= txn
->kpay
;
170 arc
->kcat
= txn
->kcat
;
171 if(txn
->memo
!= NULL
)
172 arc
->memo
= g_strdup(txn
->memo
);
174 arc
->memo
= g_strdup(_("(new archive)"));
176 if( da_splits_clone(txn
->splits
, arc
->splits
) > 0)
177 arc
->flags
|= OF_SPLIT
; //Flag that Splits are active
183 static guint32
_sched_date_get_next_post(GDate
*tmpdate
, Archive
*arc
, guint32 nextdate
)
185 guint32 nextpostdate
= nextdate
;
187 //DB( g_print("\n[scheduled] date_get_next_post\n") );
189 g_date_set_julian(tmpdate
, nextpostdate
);
191 //DB( g_print("in : %2d-%2d-%4d\n", g_date_get_day(tmpdate), g_date_get_month (tmpdate), g_date_get_year(tmpdate) ) );
196 g_date_add_days(tmpdate
, arc
->every
);
199 g_date_add_days(tmpdate
, 7 * arc
->every
);
201 case AUTO_UNIT_MONTH
:
202 g_date_add_months(tmpdate
, arc
->every
);
205 g_date_add_years(tmpdate
, arc
->every
);
209 //DB( g_print("out: %2d-%2d-%4d\n", g_date_get_day(tmpdate), g_date_get_month (tmpdate), g_date_get_year(tmpdate) ) );
212 /* get the final post date and free */
213 nextpostdate
= g_date_get_julian(tmpdate
);
219 gboolean
scheduled_is_postable(Archive
*arc
)
223 value
= hb_amount_round(arc
->amount
, 2);
224 if( (arc
->flags
& OF_AUTO
) && (arc
->kacc
> 0) && (value
!= 0.0) )
231 guint32
scheduled_get_postdate(Archive
*arc
, guint32 postdate
)
235 guint32 finalpostdate
;
238 DB( g_print("\n[scheduled] get_postdate\n") );
241 finalpostdate
= postdate
;
243 tmpdate
= g_date_new_julian(finalpostdate
);
244 /* manage weekend exception */
245 if( arc
->weekend
> 0 )
247 wday
= g_date_get_weekday(tmpdate
);
249 DB( g_print(" %s wday=%d\n", arc
->memo
, wday
) );
251 if( wday
>= G_DATE_SATURDAY
)
255 case 1: /* shift before : sun 7-5=+2 , sat 6-5=+1 */
256 shift
= wday
- G_DATE_FRIDAY
;
257 DB( g_print("sub=%d\n", shift
) );
258 g_date_subtract_days (tmpdate
, shift
);
261 case 2: /* shift after : sun 8-7=1 , sat 8-6=2 */
263 DB( g_print("add=%d\n", shift
) );
264 g_date_add_days (tmpdate
, shift
);
270 /* get the final post date and free */
271 finalpostdate
= g_date_get_julian(tmpdate
);
272 g_date_free(tmpdate
);
274 return finalpostdate
;
278 guint32
scheduled_get_latepost_count(Archive
*arc
, guint32 jrefdate
)
284 //DB( g_print("\n[scheduled] get_latepost_count\n") );
287 curdate = jrefdate - arc->nextdate;
291 nbpost = (curdate / arc->every);
292 g_print("debug d: %d => %f\n", curdate, nbpost);
296 nbpost = (curdate / ( 7 * arc->every));
297 g_print("debug w: %d => %f\n", curdate, nbpost);
300 case AUTO_UNIT_MONTH:
301 //approximate is sufficient
302 nbpost = (curdate / (( 365.2425 / 12) * arc->every));
303 g_print("debug m: %d => %f\n", curdate, nbpost);
307 //approximate is sufficient
308 nbpost = (curdate / ( 365.2425 * arc->every));
309 g_print("debug y: %d => %f\n", curdate, nbpost);
313 nblate = floor(nbpost);
315 if(arc->flags & OF_LIMIT)
316 nblate = MIN(nblate, arc->limit);
318 nblate = MIN(nblate, 11);
323 post_date
= g_date_new();
324 curdate
= arc
->nextdate
;
325 while(curdate
<= jrefdate
)
327 curdate
= _sched_date_get_next_post(post_date
, arc
, curdate
);
329 // break if over limit or at 11 max (to display +10)
330 if(nblate
>= arc
->limit
|| nblate
>= 11)
334 //DB( g_print(" nblate=%d\n", nblate) );
336 g_date_free(post_date
);
342 /* return 0 is max number of post is reached */
343 guint32
scheduled_date_advance(Archive
*arc
)
348 DB( g_print("\n[scheduled] date_advance\n") );
350 DB( g_print(" arc: '%s'\n", arc
->memo
) );
352 post_date
= g_date_new();
353 g_date_set_julian(post_date
, arc
->nextdate
);
354 // saved the current day number
355 lastday
= g_date_get_day(post_date
);
357 arc
->nextdate
= _sched_date_get_next_post(post_date
, arc
, arc
->nextdate
);
359 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
) ) );
361 //for day > 28 we might have a gap to compensate later
362 if( (arc
->unit
==AUTO_UNIT_MONTH
) || (arc
->unit
==AUTO_UNIT_YEAR
) )
366 DB( g_print(" lastday:%d, daygap:%d\n", lastday
, arc
->daygap
) );
367 if( arc
->daygap
> 0 )
369 g_date_add_days (post_date
, arc
->daygap
);
370 arc
->nextdate
= g_date_get_julian (post_date
);
371 lastday
+= arc
->daygap
;
372 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
) ) );
375 arc
->daygap
= CLAMP(lastday
- g_date_get_day(post_date
), 0, 3);
377 DB( g_print(" daygap is %d\n", arc
->daygap
) );
385 /* check limit, update and maybe break */
386 if(arc
->flags
& OF_LIMIT
)
391 arc
->flags
^= (OF_LIMIT
| OF_AUTO
); // invert flags
396 g_date_free(post_date
);
398 return arc
->nextdate
;
403 * return the maximum date a scheduled txn can be posted to
405 guint32
scheduled_date_get_post_max(void)
408 GDate
*today
, *maxdate
;
410 DB( g_print("\n[scheduled] date_get_post_max\n") );
412 //add until xx of the next month (excluded)
413 if(GLOBALS
->auto_smode
== 0)
415 DB( g_print(" - max is %d of next month\n", GLOBALS
->auto_weekday
) );
417 today
= g_date_new_julian(GLOBALS
->today
);
419 //we compute user xx weekday of next month
420 maxdate
= g_date_new_julian(GLOBALS
->today
);
421 g_date_set_day(maxdate
, GLOBALS
->auto_weekday
);
422 if(g_date_get_day (today
) >= GLOBALS
->auto_weekday
)
423 g_date_add_months(maxdate
, 1);
425 nbdays
= g_date_days_between(today
, maxdate
);
427 g_date_free(maxdate
);
432 nbdays
= GLOBALS
->auto_nbdays
;
435 DB( hb_print_date(GLOBALS
->today
, "today") );
436 DB( g_print(" - %d nbdays\n", nbdays
) );
437 DB( hb_print_date(GLOBALS
->today
+ nbdays
, "maxpostdate") );
439 return GLOBALS
->today
+ nbdays
;
443 gint
scheduled_post_all_pending(void)
450 DB( g_print("\n[scheduled] post_all_pending\n") );
454 maxpostdate
= scheduled_date_get_post_max();
456 txn
= da_transaction_malloc();
458 list
= g_list_first(GLOBALS
->arc_list
);
461 Archive
*arc
= list
->data
;
463 DB( g_print("\n eval %d for '%s'\n", scheduled_is_postable(arc
), arc
->memo
) );
465 if(scheduled_is_postable(arc
) == TRUE
)
467 DB( g_print(" - every %d limit %d (to %d)\n", arc
->every
, arc
->flags
& OF_LIMIT
, arc
->limit
) );
468 DB( hb_print_date(arc
->nextdate
, "next post") );
470 if(arc
->nextdate
< maxpostdate
)
472 guint32 mydate
= arc
->nextdate
;
474 while(mydate
< maxpostdate
)
476 DB( hb_print_date(mydate
, arc
->memo
) );
478 da_transaction_init_from_template(txn
, arc
);
479 txn
->date
= scheduled_get_postdate(arc
, mydate
);
480 /* todo: ? fill in cheque number */
482 transaction_add(txn
);
483 GLOBALS
->changes_count
++;
486 da_transaction_clean(txn
);
488 mydate
= scheduled_date_advance(arc
);
490 //DB( hb_print_date(mydate, "next on") );
499 list
= g_list_next(list
);
502 da_transaction_free (txn
);