1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2017 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 /* = = = = = = = = = = = = = = = = = = = = */
42 Archive
*da_archive_malloc(void)
44 return g_malloc0(sizeof(Archive
));
47 Archive
*da_archive_clone(Archive
*src_item
)
49 Archive
*new_item
= g_memdup(src_item
, sizeof(Archive
));
53 //duplicate the string
54 new_item
->wording
= g_strdup(src_item
->wording
);
56 if( da_splits_clone(src_item
->splits
, new_item
->splits
) > 0)
57 new_item
->flags
|= OF_SPLIT
; //Flag that Splits are active
62 void da_archive_free(Archive
*item
)
66 if(item
->wording
!= NULL
)
67 g_free(item
->wording
);
69 da_splits_free(item
->splits
);
70 //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
);
89 static gint
da_archive_glist_compare_func(Archive
*a
, Archive
*b
)
91 return hb_string_utf8_compare(a
->wording
, b
->wording
);
95 GList
*da_archive_sort(GList
*list
)
97 return g_list_sort(list
, (GCompareFunc
)da_archive_glist_compare_func
);
100 guint
da_archive_length(void)
102 return g_list_length(GLOBALS
->arc_list
);
105 void da_archive_consistency(Archive
*item
)
111 // check category exists
112 cat
= da_cat_get(item
->kcat
);
115 g_warning("arc consistency: fixed invalid cat %d", item
->kcat
);
117 GLOBALS
->changes_count
++;
120 split_cat_consistency(item
->splits
);
122 // check payee exists
123 pay
= da_pay_get(item
->kpay
);
126 g_warning("arc consistency: fixed invalid pay %d", item
->kpay
);
128 GLOBALS
->changes_count
++;
131 // reset dst acc for non xfer transaction
132 if( item
->paymode
!= PAYMODE_INTXFER
)
135 // delete automation if dst_acc not exists
136 if(item
->paymode
== PAYMODE_INTXFER
)
138 acc
= da_acc_get(item
->kxferacc
);
141 item
->flags
&= ~(OF_AUTO
); //delete flag
147 /* = = = = = = = = = = = = = = = = = = = = */
149 Archive
*da_archive_init_from_transaction(Archive
*arc
, Transaction
*txn
)
152 arc
->amount
= txn
->amount
;
153 arc
->kacc
= txn
->kacc
;
154 arc
->kxferacc
= txn
->kxferacc
;
155 arc
->paymode
= txn
->paymode
;
156 arc
->flags
= txn
->flags
& (OF_INCOME
);
157 arc
->status
= txn
->status
;
158 arc
->kpay
= txn
->kpay
;
159 arc
->kcat
= txn
->kcat
;
160 if(txn
->wording
!= NULL
)
161 arc
->wording
= g_strdup(txn
->wording
);
163 arc
->wording
= g_strdup(_("(new archive)"));
165 if( da_splits_clone(txn
->splits
, arc
->splits
) > 0)
166 arc
->flags
|= OF_SPLIT
; //Flag that Splits are active
174 static guint32
_sched_date_get_next_post(Archive
*arc
, guint32 nextdate
)
177 guint32 nextpostdate
= nextdate
;
179 DB( g_print("\n[scheduled] _sched_date_get_next_post\n") );
181 tmpdate
= g_date_new_julian(nextpostdate
);
185 g_date_add_days(tmpdate
, arc
->every
);
188 g_date_add_days(tmpdate
, 7 * arc
->every
);
190 case AUTO_UNIT_MONTH
:
191 g_date_add_months(tmpdate
, arc
->every
);
194 g_date_add_years(tmpdate
, arc
->every
);
198 /* get the final post date and free */
199 nextpostdate
= g_date_get_julian(tmpdate
);
200 g_date_free(tmpdate
);
206 gboolean
scheduled_is_postable(Archive
*arc
)
210 value
= hb_amount_round(arc
->amount
, 2);
211 if( (arc
->flags
& OF_AUTO
) && (arc
->kacc
> 0) && (value
!= 0.0) )
218 guint32
scheduled_get_postdate(Archive
*arc
, guint32 postdate
)
222 guint32 finalpostdate
;
225 DB( g_print("\n[scheduled] scheduled_get_postdate\n") );
228 finalpostdate
= postdate
;
230 tmpdate
= g_date_new_julian(finalpostdate
);
231 /* manage weekend exception */
232 if( arc
->weekend
> 0 )
234 wday
= g_date_get_weekday(tmpdate
);
236 DB( g_print(" %s wday=%d\n", arc
->wording
, wday
) );
238 if( wday
>= G_DATE_SATURDAY
)
242 case 1: /* shift before : sun 7-5=+2 , sat 6-5=+1 */
243 shift
= wday
- G_DATE_FRIDAY
;
244 DB( g_print("sub=%d\n", shift
) );
245 g_date_subtract_days (tmpdate
, shift
);
248 case 2: /* shift after : sun 8-7=1 , sat 8-6=2 */
250 DB( g_print("add=%d\n", shift
) );
251 g_date_add_days (tmpdate
, shift
);
257 /* get the final post date and free */
258 finalpostdate
= g_date_get_julian(tmpdate
);
259 g_date_free(tmpdate
);
261 return finalpostdate
;
265 guint32
scheduled_get_latepost_count(Archive
*arc
, guint32 jrefdate
)
267 guint32 curdate
= jrefdate
- arc
->nextdate
;
274 nbpost = (curdate / arc->every);
275 g_print("debug d: %d => %f\n", curdate, nbpost);
279 nbpost = (curdate / ( 7 * arc->every));
280 g_print("debug w: %d => %f\n", curdate, nbpost);
283 case AUTO_UNIT_MONTH:
284 //approximate is sufficient
285 nbpost = (curdate / (( 365.2425 / 12) * arc->every));
286 g_print("debug m: %d => %f\n", curdate, nbpost);
290 //approximate is sufficient
291 nbpost = (curdate / ( 365.2425 * arc->every));
292 g_print("debug y: %d => %f\n", curdate, nbpost);
296 nblate = floor(nbpost);
298 if(arc->flags & OF_LIMIT)
299 nblate = MIN(nblate, arc->limit);
301 nblate = MIN(nblate, 11);
306 curdate
= arc
->nextdate
;
307 while(curdate
<= jrefdate
)
309 curdate
= _sched_date_get_next_post(arc
, curdate
);
311 // break if over limit or at 11 max (to display +10)
312 if(nblate
>= arc
->limit
|| nblate
>= 11)
320 /* return 0 is max number of post is reached */
321 guint32
scheduled_date_advance(Archive
*arc
)
323 arc
->nextdate
= _sched_date_get_next_post(arc
, arc
->nextdate
);
326 /* check limit, update and maybe break */
327 if(arc
->flags
& OF_LIMIT
)
332 arc
->flags
^= (OF_LIMIT
| OF_AUTO
); // invert flags
337 return arc
->nextdate
;
342 * return the maximum date a scheduled txn can be posted to
344 guint32
scheduled_date_get_post_max(void)
347 GDate
*today
, *maxdate
;
349 DB( g_print("\n[scheduled] date_get_post_max\n") );
351 //add until xx of the next month (excluded)
352 if(GLOBALS
->auto_smode
== 0)
354 DB( g_print(" - max is %d of next month\n", GLOBALS
->auto_weekday
) );
356 today
= g_date_new_julian(GLOBALS
->today
);
358 //we compute user xx weekday of next month
359 maxdate
= g_date_new_julian(GLOBALS
->today
);
360 g_date_set_day(maxdate
, GLOBALS
->auto_weekday
);
361 if(g_date_get_day (today
) >= GLOBALS
->auto_weekday
)
362 g_date_add_months(maxdate
, 1);
364 nbdays
= g_date_days_between(today
, maxdate
);
366 g_date_free(maxdate
);
371 nbdays
= GLOBALS
->auto_nbdays
;
374 DB( hb_print_date(GLOBALS
->today
, "today") );
375 DB( g_print(" - %d nbdays\n", nbdays
) );
376 DB( hb_print_date(GLOBALS
->today
+ nbdays
, "maxpostdate") );
378 return GLOBALS
->today
+ nbdays
;
382 gint
scheduled_post_all_pending(void)
389 DB( g_print("\n[scheduled] post_all_pending\n") );
393 maxpostdate
= scheduled_date_get_post_max();
395 txn
= da_transaction_malloc();
397 list
= g_list_first(GLOBALS
->arc_list
);
400 Archive
*arc
= list
->data
;
402 DB( g_print("\n eval %d for '%s'\n", scheduled_is_postable(arc
), arc
->wording
) );
404 if(scheduled_is_postable(arc
) == TRUE
)
406 DB( g_print(" - every %d limit %d (to %d)\n", arc
->every
, arc
->flags
& OF_LIMIT
, arc
->limit
) );
407 DB( hb_print_date(arc
->nextdate
, "next post") );
409 if(arc
->nextdate
< maxpostdate
)
411 guint32 mydate
= arc
->nextdate
;
413 while(mydate
< maxpostdate
)
415 DB( hb_print_date(mydate
, arc
->wording
) );
417 da_transaction_init_from_template(txn
, arc
);
418 txn
->date
= scheduled_get_postdate(arc
, mydate
);
419 /* todo: ? fill in cheque number */
421 transaction_add(txn
, NULL
, 0);
422 GLOBALS
->changes_count
++;
425 da_transaction_clean(txn
);
427 mydate
= scheduled_date_advance(arc
);
429 //DB( hb_print_date(mydate, "next on") );
438 list
= g_list_next(list
);
441 da_transaction_free (txn
);