1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2014 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"
23 /****************************************************************************/
25 /****************************************************************************/
34 /* our global datas */
35 extern struct HomeBank
*GLOBALS
;
38 /* = = = = = = = = = = = = = = = = = = = = */
41 Archive
*da_archive_malloc(void)
43 return g_malloc0(sizeof(Archive
));
46 Archive
*da_archive_clone(Archive
*src_item
)
48 Archive
*new_item
= g_memdup(src_item
, sizeof(Archive
));
52 //duplicate the string
53 new_item
->wording
= g_strdup(src_item
->wording
);
58 void da_archive_free(Archive
*item
)
62 if(item
->wording
!= NULL
)
63 g_free(item
->wording
);
69 void da_archive_destroy(GList
*list
)
71 GList
*tmplist
= g_list_first(list
);
73 while (tmplist
!= NULL
)
75 Archive
*item
= tmplist
->data
;
76 da_archive_free(item
);
77 tmplist
= g_list_next(tmplist
);
82 static gint
da_archive_glist_compare_func(Archive
*a
, Archive
*b
)
84 return hb_string_utf8_compare(a
->wording
, b
->wording
);
88 GList
*da_archive_sort(GList
*list
)
90 return g_list_sort(list
, (GCompareFunc
)da_archive_glist_compare_func
);
93 guint
da_archive_length(void)
95 return g_list_length(GLOBALS
->arc_list
);
98 void da_archive_consistency(Archive
*item
)
104 // check category exists
105 cat
= da_cat_get(item
->kcat
);
108 g_warning("arc consistency: fixed invalid cat %d", item
->kcat
);
112 // check payee exists
113 pay
= da_pay_get(item
->kpay
);
116 g_warning("arc consistency: fixed invalid pay %d", item
->kpay
);
120 // reset dst acc for non xfer transaction
121 if( item
->paymode
!= PAYMODE_INTXFER
)
124 // remove automation if dst_acc not exists
125 if(item
->paymode
== PAYMODE_INTXFER
)
127 acc
= da_acc_get(item
->kxferacc
);
130 item
->flags
&= ~(OF_AUTO
); //remove flag
136 /* = = = = = = = = = = = = = = = = = = = = */
138 static guint32
_sched_date_get_next_post(Archive
*arc
, guint32 nextdate
)
141 guint32 nextpostdate
= nextdate
;
143 tmpdate
= g_date_new_julian(nextpostdate
);
147 g_date_add_days(tmpdate
, arc
->every
);
150 g_date_add_days(tmpdate
, 7 * arc
->every
);
152 case AUTO_UNIT_MONTH
:
153 g_date_add_months(tmpdate
, arc
->every
);
156 g_date_add_years(tmpdate
, arc
->every
);
160 /* get the final post date and free */
161 nextpostdate
= g_date_get_julian(tmpdate
);
162 g_date_free(tmpdate
);
164 /* check limit, update and maybe break */
165 if(arc
->flags
& OF_LIMIT
)
170 arc
->flags
^= (OF_LIMIT
| OF_AUTO
); // invert flags
179 gboolean
scheduled_is_postable(Archive
*arc
)
183 value
= arrondi(arc
->amount
, 2);
184 if( (arc
->flags
& OF_AUTO
) && (arc
->kacc
> 0) && (value
!= 0.0) )
191 guint32
scheduled_get_postdate(Archive
*arc
, guint32 postdate
)
195 guint32 finalpostdate
;
198 finalpostdate
= postdate
;
200 tmpdate
= g_date_new_julian(finalpostdate
);
201 /* manage weekend exception */
202 if( arc
->weekend
> 0 )
204 wday
= g_date_get_weekday(tmpdate
);
206 DB( g_print(" %s wday=%d\n", arc
->wording
, wday
) );
208 if( wday
>= G_DATE_SATURDAY
)
212 case 1: /* shift before : sun 7-5=+2 , sat 6-5=+1 */
213 shift
= wday
- G_DATE_FRIDAY
;
214 DB( g_print("sub=%d\n", shift
) );
215 g_date_subtract_days (tmpdate
, shift
);
218 case 2: /* shift after : sun 8-7=1 , sat 8-6=2 */
220 DB( g_print("add=%d\n", shift
) );
221 g_date_add_days (tmpdate
, shift
);
227 /* get the final post date and free */
228 finalpostdate
= g_date_get_julian(tmpdate
);
229 g_date_free(tmpdate
);
231 return finalpostdate
;
238 guint32
scheduled_get_latepost_count(Archive
*arc
, guint32 jrefdate
)
241 guint32 curdate
= arc
->nextdate
;
243 while(curdate
<= jrefdate
)
245 curdate
= _sched_date_get_next_post(arc
, curdate
);
247 // break at 11 max (to display +10)
256 /* return 0 is max number of post is reached */
257 guint32
scheduled_date_advance(Archive
*arc
)
259 arc
->nextdate
= _sched_date_get_next_post(arc
, arc
->nextdate
);
260 return arc
->nextdate
;
265 * return the maximum date a scheduled txn can be posted to
267 guint32
scheduled_date_get_post_max(void)
270 GDate
*today
, *maxdate
;
272 DB( g_print("\n[scheduled] date_get_post_max\n") );
274 //add until xx of the next month (excluded)
275 if(GLOBALS
->auto_smode
== 0)
277 DB( g_print(" - max is %d of next month\n", GLOBALS
->auto_weekday
) );
279 today
= g_date_new_julian(GLOBALS
->today
);
281 //we compute user xx weekday of next month
282 maxdate
= g_date_new_julian(GLOBALS
->today
);
283 g_date_set_day(maxdate
, GLOBALS
->auto_weekday
);
284 if(g_date_get_day (today
) >= GLOBALS
->auto_weekday
)
285 g_date_add_months(maxdate
, 1);
287 nbdays
= g_date_days_between(today
, maxdate
);
289 g_date_free(maxdate
);
294 nbdays
= GLOBALS
->auto_nbdays
;
297 DB( hb_print_date(GLOBALS
->today
, "today") );
298 DB( g_print(" - %d nbdays\n", nbdays
) );
299 DB( hb_print_date(GLOBALS
->today
+ nbdays
, "maxpostdate") );
301 return GLOBALS
->today
+ nbdays
;
305 gint
scheduled_post_all_pending(void)
312 DB( g_print("\n[scheduled] post_all_pending\n") );
316 maxpostdate
= scheduled_date_get_post_max();
318 txn
= da_transaction_malloc();
320 list
= g_list_first(GLOBALS
->arc_list
);
323 Archive
*arc
= list
->data
;
325 DB( g_print("\n eval %d for '%s'\n", scheduled_is_postable(arc
), arc
->wording
) );
327 if(scheduled_is_postable(arc
) == TRUE
)
329 DB( g_print(" - every %d limit %d (to %d)\n", arc
->every
, arc
->flags
& OF_LIMIT
, arc
->limit
) );
330 DB( hb_print_date(arc
->nextdate
, "next post") );
332 if(arc
->nextdate
< maxpostdate
)
334 guint32 mydate
= arc
->nextdate
;
336 while(mydate
< maxpostdate
)
338 DB( hb_print_date(mydate
, arc
->wording
) );
340 da_transaction_init_from_template(txn
, arc
);
341 txn
->date
= scheduled_get_postdate(arc
, mydate
);
342 /* todo: ? fill in cheque number */
344 transaction_add(txn
, NULL
, 0);
345 GLOBALS
->changes_count
++;
348 da_transaction_clean(txn
);
350 mydate
= scheduled_date_advance(arc
);
352 //DB( hb_print_date(mydate, "next on") );
361 list
= g_list_next(list
);
364 da_transaction_free (txn
);