~mdoyen/homebank/5.7.x

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
/*  HomeBank -- Free, easy, personal accounting for everyone.
 *  Copyright (C) 1995-2024 Maxime DOYEN
 *
 *  This file is part of HomeBank.
 *
 *  HomeBank is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  HomeBank is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "homebank.h"
#include "hb-archive.h"
#include "hb-split.h"

/****************************************************************************/
/* Debug macros                                                             */
/****************************************************************************/
#define MYDEBUG 0

#if MYDEBUG
#define DB(x) (x);
#else
#define DB(x);
#endif

/* our global datas */
extern struct HomeBank *GLOBALS;


/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */


static void
da_archive_clean(Archive *item)
{
	if(item != NULL)
	{
		g_free(item->memo);
		item->memo = NULL;

		g_free(item->info);
		item->info = NULL;

		//5.3 added as it was a leak
		g_free(item->tags);
		item->tags = NULL;

		if(item->splits != NULL)
		{
			da_split_destroy(item->splits);
			item->splits = NULL;
			item->flags &= ~(OF_SPLIT); //Flag that Splits are cleared
		}
	}
}


void
da_archive_free(Archive *item)
{
	if(item != NULL)
	{
		da_archive_clean(item);
		g_free(item);
	}
}


Archive *
da_archive_malloc(void)
{
Archive *item;

	item = g_malloc0(sizeof(Archive));
	item->key = 1;
	return item;
}


Archive *
da_archive_clone(Archive *src_item)
{
Archive *new_item = g_memdup(src_item, sizeof(Archive));

	if(new_item)
	{
		//duplicate the string
		new_item->memo = g_strdup(src_item->memo);
		new_item->info = g_strdup(src_item->info);

		//duplicate tags
		//no g_free here to avoid free the src tags (memdup copie dthe ptr)
		new_item->tags = tags_clone(src_item->tags);

		//duplicate splits
		//no g_free here to avoid free the src tags (memdup copie dthe ptr)
		new_item->splits = da_splits_clone(src_item->splits);
		if( da_splits_length (new_item->splits) > 0 )
			new_item->flags |= OF_SPLIT; //Flag that Splits are active
	}
	return new_item;
}


void
da_archive_destroy(GList *list)
{
GList *tmplist = g_list_first(list);

	while (tmplist != NULL)
	{
	Archive *item = tmplist->data;
		da_archive_free(item);
		tmplist = g_list_next(tmplist);
	}
	g_list_free(list);
}


/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */


guint
da_archive_length(void)
{
	return g_list_length(GLOBALS->arc_list);
}


guint32
da_archive_get_max_key(void)
{
GList *tmplist = g_list_first(GLOBALS->arc_list);
guint32 max_key = 0;

	while (tmplist != NULL)
	{
	Archive *item = tmplist->data;

		max_key = MAX(item->key, max_key);		
		tmplist = g_list_next(tmplist);
	}
	
	return max_key;
}


//delete


//insert


/* append a fav with an existing key (from xml file only) */
gboolean
da_archive_append(Archive *item)
{
	//perf must use preprend, see glib doc
	//GLOBALS->arc_list = g_list_append(GLOBALS->arc_list, item);
	GLOBALS->arc_list = g_list_prepend(GLOBALS->arc_list, item);
	return TRUE;
}


gboolean
da_archive_append_new(Archive *item)
{
	item->key = da_archive_get_max_key() + 1;
	//TODO: perf must use preprend, see glib doc
	GLOBALS->arc_list = g_list_append(GLOBALS->arc_list, item);
	return TRUE;
}


Archive *
da_archive_get(guint32 key)
{
GList *tmplist;
Archive *retval = NULL;

	tmplist = g_list_first(GLOBALS->arc_list);
	while (tmplist != NULL)
	{
	Archive *item = tmplist->data;

		if(item->key == key)
		{
			retval = item;
			break;
		}
		tmplist = g_list_next(tmplist);
	}
	return retval;
}

//#1968249 build a non empty label, when memo/payee/category are empty
void da_archive_get_display_label(GString *tpltitle, Archive *item)
{
	g_string_truncate(tpltitle, 0);
	if(item->memo != NULL)
	{
		g_string_append(tpltitle, item->memo);
	}
	else
	{
		g_string_append(tpltitle, _("(no memo)") );
		if(item->kpay > 0)
		{
		Payee *pay = da_pay_get(item->kpay);

			if(pay != NULL)
			{
				g_string_append_c(tpltitle, ' ');
				g_string_append(tpltitle, pay->name);
			}
		}

		if(item->kcat > 0)
		{
		Category *cat = da_cat_get(item->kcat);

			if(cat != NULL)
			{
				g_string_append(tpltitle, " / ");
				g_string_append(tpltitle, cat->fullname);
			}
		}

	}
}


void da_archive_consistency(Archive *item)
{
Account *acc;
Category *cat;
Payee *pay;
guint nbsplit;

	// check category exists
	cat = da_cat_get(item->kcat);
	if(cat == NULL)
	{
		g_warning("tpl consistency: fixed invalid cat %d", item->kcat);
		item->kcat = 0;
		GLOBALS->changes_count++;
	}

	//#1340142 check split category 	
	if( item->splits != NULL )
	{
		nbsplit = da_splits_consistency(item->splits);
		//# 1416624 empty category when split
		if(nbsplit > 0 && item->kcat > 0)
		{
			g_warning("tpl consistency: fixed invalid cat on split txn");
			item->kcat = 0;
			GLOBALS->changes_count++;
		}
	}
	
	// check payee exists
	pay = da_pay_get(item->kpay);
	if(pay == NULL)
	{
		g_warning("tpl consistency: fixed invalid pay %d", item->kpay);
		item->kpay = 0;
		GLOBALS->changes_count++;
	}

	// 5.3: fix split on intxfer
	if( ((item->flags & OF_INTXFER) || (item->paymode == OLDPAYMODE_INTXFER)) && (item->splits != NULL) )
	{
		g_warning("tpl consistency: fixed invalid split on xfer");
		item->flags &= ~(OF_INTXFER);
		item->paymode = PAYMODE_XFER;
		item->kxferacc = 0;
	}
	
	// reset dst acc for non xfer transaction
	if( !((item->flags & OF_INTXFER) || (item->paymode == OLDPAYMODE_INTXFER)) )
		item->kxferacc = 0;

	// delete automation if dst_acc not exists
	if( (item->flags & OF_INTXFER) || (item->paymode == OLDPAYMODE_INTXFER) )
	{
		acc = da_acc_get(item->kxferacc);
		if(acc == NULL)
		{
			item->flags &= ~(OF_AUTO);	//delete flag
		}
	}

}


/* = = = = = = = = = = = = = = = = = = = = */


static gint
da_archive_glist_name_compare_func(Archive *a, Archive *b)
{
	return hb_string_utf8_compare(a->memo, b->memo);
}


static gint
da_archive_glist_key_compare_func(Archive *a, Archive *b)
{
	return hb_string_utf8_compare(a->memo, b->memo);
}


GList *da_archive_glist_sorted(gint column)
{

	switch(column)
	{
		case HB_GLIST_SORT_NAME:
			GLOBALS->arc_list = g_list_sort(GLOBALS->arc_list, (GCompareFunc)da_archive_glist_name_compare_func);
			break;
		//case HB_GLIST_SORT_KEY:
		default:
			GLOBALS->arc_list = g_list_sort(GLOBALS->arc_list, (GCompareFunc)da_archive_glist_key_compare_func);
			break;
	}

	return GLOBALS->arc_list;
}


void da_archive_stats(gint *nbtpl, gint *nbsch)
{
GList *tmplist = g_list_first(GLOBALS->arc_list);
gint nbt, nbs;

	nbt = nbs = 0;
	while (tmplist != NULL)
	{
	Archive *item = tmplist->data;

		if(item->flags & OF_AUTO)
			nbs++;
		else
			nbt++;

		tmplist = g_list_next(tmplist);
	}

	if( nbtpl != NULL)
		*nbtpl = nbt;
	if( nbsch != NULL)
		*nbsch = nbs;
}


//#1872140 don't prefix if not from register
Archive *da_archive_init_from_transaction(Archive *arc, Transaction *txn, gboolean fromregister)
{
	DB( g_print("\n[scheduled] init from txn\n") );

	da_archive_clean(arc);

	//fill it
	arc->amount		= txn->amount;
	arc->kacc		= txn->kacc;
	//#1673260
	arc->xferamount = txn->xferamount;
	arc->kxferacc	= txn->kxferacc;
	arc->paymode	= txn->paymode;
	arc->flags		= txn->flags;
	arc->status		= txn->status;
	arc->kpay		= txn->kpay;
	arc->kcat		= txn->kcat;

	//5.3.3 prefixed with prefilled
	//#1883063 **PREFILLED** only when fromregister==TRUE, test order was wrogn here
	if( fromregister == FALSE )
	{
		if(txn->memo != NULL)
			arc->memo = g_strdup(txn->memo);	
	}
	else
	{
		arc->flags |= OF_PREFILLED;
		//#2018680
		if(txn->memo != NULL)
			arc->memo = g_strdup( txn->memo );
			//arc->memo = g_strdup_printf("%s %s", _("**PREFILLED**"), txn->memo );
		//else
		//	arc->memo = g_strdup(_("**PREFILLED**"));
	}	

	if(txn->info != NULL)
		arc->info = g_strdup(txn->info);

	arc->tags    = tags_clone(txn->tags);
	arc->splits  = da_splits_clone(txn->splits);

	if( da_splits_length (arc->splits) > 0 )
		arc->flags |= OF_SPLIT; //Flag that Splits are active
	
	return arc;
}


/* = = = = = = = = = = = = = = = = = = = = */


gboolean
template_is_account_used(Archive *arc)
{
GList *lacc, *list;
gboolean retval = FALSE;

	lacc = list = g_hash_table_get_values(GLOBALS->h_acc);
	while (list != NULL)
	{
	Account *acc = list->data;

		if( acc->karc == arc->key )
			retval = TRUE;

		list = g_list_next(list);
	}
	g_list_free(lacc);
	
	return retval;
}


static void _sched_nextdate_weekend_adjust(GDate *tmpdate)
{
GDateWeekday wday;

	wday = g_date_get_weekday(tmpdate);
	if( wday == G_DATE_SATURDAY )
		g_date_add_days (tmpdate, 2);
	else
	if( wday == G_DATE_SUNDAY )
		g_date_add_days (tmpdate, 1);
}


static guint32 _sched_date_get_next_post(GDate *tmpdate, Archive *arc, guint32 nextdate)
{
guint32 nextpostdate = nextdate;

	DB( g_print("\n[scheduled] date_get_next_post\n") );

	g_date_set_julian(tmpdate, nextpostdate);

	DB( g_print("in : %2d-%2d-%4d\n", g_date_get_day(tmpdate), g_date_get_month (tmpdate), g_date_get_year(tmpdate) ) );

	switch(arc->unit)
	{
		case AUTO_UNIT_DAY:
			g_date_add_days(tmpdate, arc->every);
			break;
		case AUTO_UNIT_WEEK:
			g_date_add_days(tmpdate, 7 * arc->every);
			break;
		case AUTO_UNIT_MONTH:
			g_date_add_months(tmpdate, arc->every);
			break;
		case AUTO_UNIT_YEAR:
			g_date_add_years(tmpdate, arc->every);
			break;
	}

	DB( g_print("out: %2d-%2d-%4d\n", g_date_get_day(tmpdate), g_date_get_month (tmpdate), g_date_get_year(tmpdate) ) );

	//#1906953 add ignore weekend
	if( arc->weekend == ARC_WEEKEND_SKIP )
	{
		_sched_nextdate_weekend_adjust(tmpdate);
	}

	/* get the final post date and free */
	nextpostdate = g_date_get_julian(tmpdate);
	
	return nextpostdate;
}


guint32 scheduled_date_get_next_post(GDate *tmpdate, Archive *arc, guint32 nextdate)
{
	return _sched_date_get_next_post(tmpdate, arc, nextdate);
}


//#1906953 add ignore weekend
void scheduled_nextdate_weekend_adjust(Archive *arc)
{
	if( arc->weekend == ARC_WEEKEND_SKIP )
	{
	GDate *tmpdate = g_date_new_julian(arc->nextdate);

		_sched_nextdate_weekend_adjust(tmpdate);
		arc->nextdate = g_date_get_julian(tmpdate);
		g_date_free(tmpdate);
	}
}


gboolean scheduled_is_postable(Archive *arc)
{
gdouble value;

	value = hb_amount_round(arc->amount, 2);
	if( (arc->flags & OF_AUTO) && (arc->kacc > 0) && (value != 0.0) )
		return TRUE;

	return FALSE;
}


guint32 scheduled_get_postdate(Archive *arc, guint32 postdate)
{
GDate *tmpdate;
GDateWeekday wday;
guint32 finalpostdate;
gint shift;

	DB( g_print("\n[scheduled] get_postdate\n") );

	finalpostdate = postdate;
	
	tmpdate = g_date_new_julian(finalpostdate);

	/* manage weekend exception */
	if( arc->weekend > 0 )
	{
		wday = g_date_get_weekday(tmpdate);

		DB( g_print(" %s wday=%d\n", arc->memo, wday) );

		if( wday >= G_DATE_SATURDAY )
		{
			switch(arc->weekend)
			{
				case 1: /* shift before : sun 7-5=+2 , sat 6-5=+1 */
					shift = wday - G_DATE_FRIDAY;
					DB( g_print("sub=%d\n", shift) );
					g_date_subtract_days (tmpdate, shift);
					break;

				case 2: /* shift after : sun 8-7=1 , sat 8-6=2 */
					shift = 8 - wday;
					DB( g_print("add=%d\n", shift) );
					g_date_add_days (tmpdate, shift);
					break;
			}
		}
	}
	
	/* get the final post date and free */
	finalpostdate = g_date_get_julian(tmpdate);
	g_date_free(tmpdate);
	
	return finalpostdate;
}


guint32 scheduled_get_latepost_count(Archive *arc, guint32 jrefdate)
{
GDate *post_date;
guint32 curdate;
guint32 nblate = 0;

	//DB( g_print("\n[scheduled] get_latepost_count\n") );

	/*
	curdate = jrefdate - arc->nextdate;
	switch(arc->unit)
	{
		case AUTO_UNIT_DAY:
			nbpost = (curdate / arc->every);
			g_print("debug d: %d => %f\n", curdate, nbpost);
			break;

		case AUTO_UNIT_WEEK:
			nbpost = (curdate / ( 7 * arc->every));
			g_print("debug w: %d => %f\n", curdate, nbpost);
			break;

		case AUTO_UNIT_MONTH:
			//approximate is sufficient
			nbpost = (curdate / (( 365.2425 / 12) * arc->every));
			g_print("debug m: %d => %f\n", curdate, nbpost);
			break;

		case AUTO_UNIT_YEAR:
			//approximate is sufficient
			nbpost = (curdate / ( 365.2425 * arc->every));
			g_print("debug y: %d => %f\n", curdate, nbpost);
			break;
	}

	nblate = floor(nbpost);

	if(arc->flags & OF_LIMIT)
		nblate = MIN(nblate, arc->limit);
	
	nblate = MIN(nblate, 11);
	*/
	

	// pre 5.1 way
	post_date = g_date_new();
	curdate = arc->nextdate;
	while(curdate <= jrefdate)
	{
		curdate = _sched_date_get_next_post(post_date, arc, curdate);
		nblate++;
		// break if over limit or at 11 max (to display +10)
		if( nblate >= 11 || ( (arc->flags & OF_LIMIT) && (nblate >= arc->limit) ) )
			break;
	}

	//DB( g_print(" nblate=%d\n", nblate) );

	g_date_free(post_date);

	return nblate;
}


/* return 0 is max number of post is reached */
guint32 scheduled_date_advance(Archive *arc)
{
GDate *post_date;
gushort lastday;

	DB( g_print("\n[scheduled] date_advance\n") );

	post_date = g_date_new();
	g_date_set_julian(post_date, arc->nextdate);
	// saved the current day number
	lastday = g_date_get_day(post_date);

	arc->nextdate = _sched_date_get_next_post(post_date, arc, arc->nextdate);

	DB( g_print(" raw next post date: %02d-%02d-%4d\n", g_date_get_day(post_date), g_date_get_month (post_date), g_date_get_year(post_date) ) );

	//for day > 28 we might have a gap to compensate later
	if( (arc->unit==AUTO_UNIT_MONTH) || (arc->unit==AUTO_UNIT_YEAR) )
	{
		if( lastday >= 28 )
		{
			DB( g_print(" lastday:%d, daygap:%d\n", lastday, arc->daygap) );
			if( arc->daygap > 0 )
			{
				g_date_add_days (post_date, arc->daygap);
				arc->nextdate = g_date_get_julian (post_date);
				lastday += arc->daygap;
				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) ) );
			}

			arc->daygap = CLAMP(lastday - g_date_get_day(post_date), 0, 3);
		
			DB( g_print(" daygap is %d\n", arc->daygap) );
		}
		else
			arc->daygap = 0;
	}

	//#1556289
	/* check limit, update and maybe break */
	if(arc->flags & OF_LIMIT)
	{
		arc->limit--;
		if(arc->limit <= 0)
		{
			arc->flags ^= (OF_LIMIT | OF_AUTO);	// invert flags
			arc->nextdate = 0;
		}
	}

	g_date_free(post_date);

	return arc->nextdate;
}


void scheduled_date_get_show_minmax(gint select, guint32 *mindate, guint32 *maxdate)
{
GDate *tmpdate  = g_date_new_julian(GLOBALS->today);
guint16 month, year;

	if( (mindate == NULL) || (maxdate == NULL) )
		return;

	switch( select )
	{
		case FLT_SCHEDULED_THISMONTH:
		case FLT_SCHEDULED_NEXTMONTH:
			g_date_set_day(tmpdate, 1);
			if( select == FLT_SCHEDULED_NEXTMONTH)
				g_date_add_months(tmpdate, 1);
			*mindate = g_date_get_julian(tmpdate);
			month = g_date_get_month(tmpdate);
			year  = g_date_get_year(tmpdate);		
			g_date_add_days(tmpdate, g_date_get_days_in_month(month, year));
			*maxdate = g_date_get_julian(tmpdate) - 1;
			break;
		case FLT_SCHEDULED_NEXT30DAYS:
			*mindate = GLOBALS->today;
			*maxdate = GLOBALS->today + 30;
			break;
		case FLT_SCHEDULED_NEXT60DAYS:
			*mindate = GLOBALS->today;
			*maxdate = GLOBALS->today + 60;
			break;
		case FLT_SCHEDULED_NEXT90DAYS:
			*mindate = GLOBALS->today;
			*maxdate = GLOBALS->today + 90;
			break;
		default:
			*mindate = HB_MINDATE;
			*maxdate = HB_MAXDATE;
			break;	
	}

	g_date_free(tmpdate);
}


/*
 *  return the maximum date a scheduled txn can be posted to
 */
guint32 scheduled_date_get_post_max(guint32 start, gint auto_smode, gint auto_nbdays, gint auto_weekday, gint nbmonth)
{
guint nbdays = 0;
GDate *today, *maxdate;
gdouble nxtmonth;
guint8 daysinmonth;

	switch( auto_smode)
	{
		//add until xx of the yy month (excluded)
		//if(auto_smode == 0)
		case ARC_POSTMODE_PAYOUT:
		{
			today = g_date_new_julian(start);
			daysinmonth = g_date_get_days_in_month(g_date_get_month(today), g_date_get_year(today));
			maxdate = g_date_new_julian(start + (daysinmonth - auto_weekday));
			
			//TODO: 5.7 probably this is false
			nxtmonth = ceil((gdouble)g_date_get_month(maxdate)/(gdouble)nbmonth)*(gdouble)nbmonth;
			DB( g_print("nxtmonth: %f =  ceil( %d / %d) / %d\n", nxtmonth, g_date_get_month(today), month, month) );
			g_date_set_day(maxdate, auto_weekday);
			g_date_set_month(maxdate, nxtmonth);

			//if( today > g_date_get_julian(maxdate) )
			//	g_date_add_months(maxdate, month);

			nbdays = g_date_days_between(today, maxdate);
		
			g_date_free(maxdate);
			g_date_free(today);
		}
		break;

		//add xx days in advance the current date
		//if(auto_smode == 1)
		case ARC_POSTMODE_ADVANCE:
			nbdays = auto_nbdays;
			break;

		case ARC_POSTMODE_DUEDATE:
		default:
			nbdays = 0;
			break;
	}

	return start + nbdays;
}


gint scheduled_post_all_pending(void)
{
GList *list;
gint count;
guint32 maxpostdate;
Transaction *txn;

	DB( g_print("\n[scheduled] post_all_pending\n") );

	count = 0;

	maxpostdate = scheduled_date_get_post_max(GLOBALS->today, GLOBALS->auto_smode, GLOBALS->auto_nbdays, GLOBALS->auto_weekday, GLOBALS->auto_nbmonths);
	
	list = g_list_first(GLOBALS->arc_list);
	while (list != NULL)
	{
	Archive *arc = list->data;

		DB( g_print("--------\n eval post of '%s' %.2f\n", arc->memo, arc->amount) );

		if(scheduled_is_postable(arc) == TRUE)
		{
			DB( g_print(" - every %d limit %d (to %d)\n", arc->every, arc->flags & OF_LIMIT, arc->limit) );
			DB( hb_print_date(arc->nextdate, "next post") );

			if(arc->nextdate < maxpostdate)
			{
			guint32 mydate = arc->nextdate;

				while(mydate < maxpostdate)
				{
					DB( hb_print_date(mydate, arc->memo) );
					//5.5.3 fixed leak as this was outside the loop
					txn = da_transaction_malloc();

					da_transaction_init_from_template(txn, arc);
					txn->date = scheduled_get_postdate(arc, mydate);
					/* todo: ? fill in cheque number */

					DB( g_print(" -- post --\n") );
					transaction_add(NULL, txn);

					da_transaction_free (txn);

					GLOBALS->changes_count++;
					count++;

					mydate = scheduled_date_advance(arc);

					//DB( hb_print_date(mydate, "next on") );

					if(mydate == 0)
						goto nextarchive;
				}

			}
		}
nextarchive:
		list = g_list_next(list);
	}
	
	return count;
}