1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2016 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 /* = = = = = = = = = = = = = = = = = = = = */
45 Archive
*da_archive_malloc(void)
47 return rc_alloc(sizeof(Archive
));
50 Archive
*da_archive_clone(Archive
*src_item
)
52 Archive
*new_item
= rc_dup(src_item
, sizeof(Archive
));
56 //duplicate the string
57 new_item
->wording
= g_strdup(src_item
->wording
);
59 if( da_splits_clone(src_item
->splits
, new_item
->splits
) > 0)
60 new_item
->flags
|= OF_SPLIT
; //Flag that Splits are active
65 void da_archive_free(Archive
*item
)
69 if(item
->wording
!= NULL
)
70 g_free(item
->wording
);
72 da_splits_free(item
->splits
);
73 //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
);
92 static gint
da_archive_glist_compare_func(Archive
*a
, Archive
*b
)
94 return hb_string_utf8_compare(a
->wording
, b
->wording
);
98 GList
*da_archive_sort(GList
*list
)
100 return g_list_sort(list
, (GCompareFunc
)da_archive_glist_compare_func
);
103 guint
da_archive_length(void)
105 return g_list_length(GLOBALS
->arc_list
);
108 void da_archive_consistency(Archive
*item
)
114 // check category exists
115 cat
= da_cat_get(item
->kcat
);
118 g_warning("arc consistency: fixed invalid cat %d", item
->kcat
);
120 GLOBALS
->changes_count
++;
123 split_cat_consistency(item
->splits
);
125 // check payee exists
126 pay
= da_pay_get(item
->kpay
);
129 g_warning("arc consistency: fixed invalid pay %d", item
->kpay
);
131 GLOBALS
->changes_count
++;
134 // reset dst acc for non xfer transaction
135 if( item
->paymode
!= PAYMODE_INTXFER
)
138 // delete automation if dst_acc not exists
139 if(item
->paymode
== PAYMODE_INTXFER
)
141 acc
= da_acc_get(item
->kxferacc
);
144 item
->flags
&= ~(OF_AUTO
); //delete flag
150 /* = = = = = = = = = = = = = = = = = = = = */
152 Archive
*da_archive_init_from_transaction(Archive
*arc
, Transaction
*txn
)
155 arc
->amount
= txn
->amount
;
156 arc
->kacc
= txn
->kacc
;
157 arc
->kxferacc
= txn
->kxferacc
;
158 arc
->paymode
= txn
->paymode
;
159 arc
->flags
= txn
->flags
& (OF_INCOME
);
160 arc
->status
= txn
->status
;
161 arc
->kpay
= txn
->kpay
;
162 arc
->kcat
= txn
->kcat
;
163 if(txn
->wording
!= NULL
)
164 arc
->wording
= g_strdup(txn
->wording
);
166 arc
->wording
= g_strdup(_("(new archive)"));
168 if( da_splits_clone(txn
->splits
, arc
->splits
) > 0)
169 arc
->flags
|= OF_SPLIT
; //Flag that Splits are active
177 static guint32
_sched_date_get_next_post(Archive
*arc
, guint32 nextdate
)
180 guint32 nextpostdate
= nextdate
;
182 DB( g_print("\n[scheduled] _sched_date_get_next_post\n") );
184 tmpdate
= g_date_new_julian(nextpostdate
);
188 g_date_add_days(tmpdate
, arc
->every
);
191 g_date_add_days(tmpdate
, 7 * arc
->every
);
193 case AUTO_UNIT_MONTH
:
194 g_date_add_months(tmpdate
, arc
->every
);
197 g_date_add_years(tmpdate
, arc
->every
);
201 /* get the final post date and free */
202 nextpostdate
= g_date_get_julian(tmpdate
);
203 g_date_free(tmpdate
);
209 gboolean
scheduled_is_postable(Archive
*arc
)
213 value
= hb_amount_round(arc
->amount
, 2);
214 if( (arc
->flags
& OF_AUTO
) && (arc
->kacc
> 0) && (value
!= 0.0) )
221 guint32
scheduled_get_postdate(Archive
*arc
, guint32 postdate
)
225 guint32 finalpostdate
;
228 DB( g_print("\n[scheduled] scheduled_get_postdate\n") );
231 finalpostdate
= postdate
;
233 tmpdate
= g_date_new_julian(finalpostdate
);
234 /* manage weekend exception */
235 if( arc
->weekend
> 0 )
237 wday
= g_date_get_weekday(tmpdate
);
239 DB( g_print(" %s wday=%d\n", arc
->wording
, wday
) );
241 if( wday
>= G_DATE_SATURDAY
)
245 case 1: /* shift before : sun 7-5=+2 , sat 6-5=+1 */
246 shift
= wday
- G_DATE_FRIDAY
;
247 DB( g_print("sub=%d\n", shift
) );
248 g_date_subtract_days (tmpdate
, shift
);
251 case 2: /* shift after : sun 8-7=1 , sat 8-6=2 */
253 DB( g_print("add=%d\n", shift
) );
254 g_date_add_days (tmpdate
, shift
);
260 /* get the final post date and free */
261 finalpostdate
= g_date_get_julian(tmpdate
);
262 g_date_free(tmpdate
);
264 return finalpostdate
;
268 guint32
scheduled_get_latepost_count(Archive
*arc
, guint32 jrefdate
)
270 guint32 curdate
= jrefdate
- arc
->nextdate
;
277 nbpost = (curdate / arc->every);
278 g_print("debug d: %d => %f\n", curdate, nbpost);
282 nbpost = (curdate / ( 7 * arc->every));
283 g_print("debug w: %d => %f\n", curdate, nbpost);
286 case AUTO_UNIT_MONTH:
287 //approximate is sufficient
288 nbpost = (curdate / (( 365.2425 / 12) * arc->every));
289 g_print("debug m: %d => %f\n", curdate, nbpost);
293 //approximate is sufficient
294 nbpost = (curdate / ( 365.2425 * arc->every));
295 g_print("debug y: %d => %f\n", curdate, nbpost);
299 nblate = floor(nbpost);
301 if(arc->flags & OF_LIMIT)
302 nblate = MIN(nblate, arc->limit);
304 nblate = MIN(nblate, 11);
309 curdate
= arc
->nextdate
;
310 while(curdate
<= jrefdate
)
312 curdate
= _sched_date_get_next_post(arc
, curdate
);
314 // break if over limit or at 11 max (to display +10)
315 if(nblate
>= arc
->limit
|| nblate
>= 11)
323 /* return 0 is max number of post is reached */
324 guint32
scheduled_date_advance(Archive
*arc
)
326 arc
->nextdate
= _sched_date_get_next_post(arc
, arc
->nextdate
);
329 /* check limit, update and maybe break */
330 if(arc
->flags
& OF_LIMIT
)
335 arc
->flags
^= (OF_LIMIT
| OF_AUTO
); // invert flags
340 return arc
->nextdate
;
345 * return the maximum date a scheduled txn can be posted to
347 guint32
scheduled_date_get_post_max(void)
350 GDate
*today
, *maxdate
;
352 DB( g_print("\n[scheduled] date_get_post_max\n") );
354 //add until xx of the next month (excluded)
355 if(GLOBALS
->auto_smode
== 0)
357 DB( g_print(" - max is %d of next month\n", GLOBALS
->auto_weekday
) );
359 today
= g_date_new_julian(GLOBALS
->today
);
361 //we compute user xx weekday of next month
362 maxdate
= g_date_new_julian(GLOBALS
->today
);
363 g_date_set_day(maxdate
, GLOBALS
->auto_weekday
);
364 if(g_date_get_day (today
) >= GLOBALS
->auto_weekday
)
365 g_date_add_months(maxdate
, 1);
367 nbdays
= g_date_days_between(today
, maxdate
);
369 g_date_free(maxdate
);
374 nbdays
= GLOBALS
->auto_nbdays
;
377 DB( hb_print_date(GLOBALS
->today
, "today") );
378 DB( g_print(" - %d nbdays\n", nbdays
) );
379 DB( hb_print_date(GLOBALS
->today
+ nbdays
, "maxpostdate") );
381 return GLOBALS
->today
+ nbdays
;
385 gint
scheduled_post_all_pending(void)
392 DB( g_print("\n[scheduled] post_all_pending\n") );
396 maxpostdate
= scheduled_date_get_post_max();
398 txn
= da_transaction_malloc();
400 list
= g_list_first(GLOBALS
->arc_list
);
403 Archive
*arc
= list
->data
;
405 DB( g_print("\n eval %d for '%s'\n", scheduled_is_postable(arc
), arc
->wording
) );
407 if(scheduled_is_postable(arc
) == TRUE
)
409 DB( g_print(" - every %d limit %d (to %d)\n", arc
->every
, arc
->flags
& OF_LIMIT
, arc
->limit
) );
410 DB( hb_print_date(arc
->nextdate
, "next post") );
412 if(arc
->nextdate
< maxpostdate
)
414 guint32 mydate
= arc
->nextdate
;
416 while(mydate
< maxpostdate
)
418 DB( hb_print_date(mydate
, arc
->wording
) );
420 da_transaction_init_from_template(txn
, arc
);
421 txn
->date
= scheduled_get_postdate(arc
, mydate
);
422 /* todo: ? fill in cheque number */
424 transaction_add(txn
, NULL
, 0);
425 GLOBALS
->changes_count
++;
428 da_transaction_clean(txn
);
430 mydate
= scheduled_date_advance(arc
);
432 //DB( hb_print_date(mydate, "next on") );
441 list
= g_list_next(list
);
444 da_transaction_free (txn
);