~mdoyen/homebank/trunk

« back to all changes in this revision

Viewing changes to src/hb-archive.c

  • Committer: Maxime Doyen
  • Date: 2023-01-12 17:58:52 UTC
  • Revision ID: homebank@free.fr-20230112175852-qtqy23vexam04fim
5.6.1 release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*  HomeBank -- Free, easy, personal accounting for everyone.
2
 
 *  Copyright (C) 1995-2022 Maxime DOYEN
 
2
 *  Copyright (C) 1995-2023 Maxime DOYEN
3
3
 *
4
4
 *  This file is part of HomeBank.
5
5
 *
202
202
        return retval;
203
203
}
204
204
 
 
205
//#1968249 build a non empty label, when memo/payee/category are empty
 
206
void da_archive_get_display_label(GString *tpltitle, Archive *item)
 
207
{
 
208
        g_string_truncate(tpltitle, 0);
 
209
        if(item->memo != NULL)
 
210
        {
 
211
                g_string_append(tpltitle, item->memo);
 
212
        }
 
213
        else
 
214
        {
 
215
                g_string_append(tpltitle, _("(no memo)") );
 
216
                if(item->kpay > 0)
 
217
                {
 
218
                Payee *pay = da_pay_get(item->kpay);
 
219
 
 
220
                        if(pay != NULL)
 
221
                        {
 
222
                                g_string_append_c(tpltitle, ' ');
 
223
                                g_string_append(tpltitle, pay->name);
 
224
                        }
 
225
                }
 
226
 
 
227
                if(item->kcat > 0)
 
228
                {
 
229
                Category *cat = da_cat_get(item->kcat);
 
230
 
 
231
                        if(cat != NULL)
 
232
                        {
 
233
                                g_string_append(tpltitle, " / ");
 
234
                                g_string_append(tpltitle, cat->fullname);
 
235
                        }
 
236
                }
 
237
 
 
238
        }
 
239
}
 
240
 
205
241
 
206
242
void da_archive_consistency(Archive *item)
207
243
{
330
366
        //fill it
331
367
        arc->amount             = txn->amount;
332
368
        arc->kacc               = txn->kacc;
 
369
        //#1673260
 
370
        arc->xferamount = txn->xferamount;
333
371
        arc->kxferacc   = txn->kxferacc;
334
372
        arc->paymode    = txn->paymode;
335
373
        arc->flags              = txn->flags;
365
403
}
366
404
 
367
405
 
 
406
/* = = = = = = = = = = = = = = = = = = = = */
 
407
 
 
408
 
 
409
static void _sched_nextdate_weekend_adjust(GDate *tmpdate)
 
410
{
 
411
GDateWeekday wday;
 
412
 
 
413
        wday = g_date_get_weekday(tmpdate);
 
414
        if( wday == G_DATE_SATURDAY )
 
415
                g_date_add_days (tmpdate, 2);
 
416
        else
 
417
        if( wday == G_DATE_SUNDAY )
 
418
                g_date_add_days (tmpdate, 1);
 
419
}
 
420
 
 
421
 
368
422
static guint32 _sched_date_get_next_post(GDate *tmpdate, Archive *arc, guint32 nextdate)
369
423
{
370
424
guint32 nextpostdate = nextdate;
393
447
 
394
448
        //DB( g_print("out: %2d-%2d-%4d\n", g_date_get_day(tmpdate), g_date_get_month (tmpdate), g_date_get_year(tmpdate) ) );
395
449
 
 
450
        //#1906953 add ignore weekend
 
451
        if( arc->weekend == ARC_WEEKEND_SKIP )
 
452
        {
 
453
                _sched_nextdate_weekend_adjust(tmpdate);
 
454
        }
396
455
 
397
456
        /* get the final post date and free */
398
457
        nextpostdate = g_date_get_julian(tmpdate);
401
460
}
402
461
 
403
462
 
 
463
//#1906953 add ignore weekend
 
464
void scheduled_nextdate_weekend_adjust(Archive *arc)
 
465
{
 
466
        if( arc->weekend == ARC_WEEKEND_SKIP )
 
467
        {
 
468
        GDate *tmpdate = g_date_new_julian(arc->nextdate);
 
469
 
 
470
                _sched_nextdate_weekend_adjust(tmpdate);
 
471
                arc->nextdate = g_date_get_julian(tmpdate);
 
472
                g_date_free(tmpdate);
 
473
        }
 
474
}
 
475
 
 
476
 
404
477
gboolean scheduled_is_postable(Archive *arc)
405
478
{
406
479
gdouble value;
422
495
 
423
496
        DB( g_print("\n[scheduled] get_postdate\n") );
424
497
 
425
 
 
426
498
        finalpostdate = postdate;
427
499
        
428
500
        tmpdate = g_date_new_julian(finalpostdate);
 
501
 
429
502
        /* manage weekend exception */
430
503
        if( arc->weekend > 0 )
431
504
        {
532
605
 
533
606
        DB( g_print("\n[scheduled] date_advance\n") );
534
607
 
535
 
        DB( g_print(" arc: '%s'\n", arc->memo ) );
536
 
 
537
608
        post_date = g_date_new();
538
609
        g_date_set_julian(post_date, arc->nextdate);
539
610
        // saved the current day number
541
612
 
542
613
        arc->nextdate = _sched_date_get_next_post(post_date, arc, arc->nextdate);
543
614
 
544
 
        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) ) );
 
615
        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) ) );
545
616
 
546
617
        //for day > 28 we might have a gap to compensate later
547
618
        if( (arc->unit==AUTO_UNIT_MONTH) || (arc->unit==AUTO_UNIT_YEAR) )
565
636
                        arc->daygap = 0;
566
637
        }
567
638
 
568
 
 
569
639
        //#1556289
570
640
        /* check limit, update and maybe break */
571
641
        if(arc->flags & OF_LIMIT)
630
700
/*
631
701
 *  return the maximum date a scheduled txn can be posted to
632
702
 */
633
 
guint32 scheduled_date_get_post_max(void)
 
703
guint32 scheduled_date_get_post_max(guint32 start, gint auto_smode, gint auto_nbdays, gint auto_weekday, gint nbmonth)
634
704
{
635
 
guint nbdays;
 
705
guint nbdays = 0;
636
706
GDate *today, *maxdate;
637
 
 
638
 
        DB( g_print("\n[scheduled] date_get_post_max\n") );
639
 
 
640
 
        //add until xx of the each month (excluded)
641
 
        if(GLOBALS->auto_smode == 0)    
642
 
        {
643
 
                DB( g_print(" - max is %d of next month\n", GLOBALS->auto_weekday) );
644
 
                
645
 
                today = g_date_new_julian(GLOBALS->today);
646
 
 
647
 
                //we compute user xx weekday of next month
648
 
                maxdate = g_date_new_julian(GLOBALS->today);
649
 
                g_date_set_day(maxdate, GLOBALS->auto_weekday);
650
 
                if(g_date_get_day (today) >= GLOBALS->auto_weekday)
651
 
                        g_date_add_months(maxdate, 1);
652
 
                
653
 
                nbdays = g_date_days_between(today, maxdate);
654
 
        
655
 
                g_date_free(maxdate);
656
 
                g_date_free(today);
657
 
        }
658
 
        //add xx days in advance the current date
659
 
        else
660
 
        {
661
 
                nbdays = GLOBALS->auto_nbdays;
662
 
        }
663
 
 
664
 
        DB( hb_print_date(GLOBALS->today, "today") );
665
 
        DB( g_print(" - %d nbdays\n", nbdays) );
666
 
        DB( hb_print_date(GLOBALS->today + nbdays, "maxpostdate") );
667
 
 
668
 
        return GLOBALS->today + nbdays;
 
707
gdouble nxtmonth;
 
708
guint8 daysinmonth;
 
709
 
 
710
        switch( auto_smode)
 
711
        {
 
712
                //add until xx of the yy month (excluded)
 
713
                //if(auto_smode == 0)
 
714
                case ARC_POSTMODE_PAYOUT:
 
715
                {
 
716
                        today = g_date_new_julian(start);
 
717
                        daysinmonth = g_date_get_days_in_month(g_date_get_month(today), g_date_get_year(today));
 
718
                        maxdate = g_date_new_julian(start + (daysinmonth - auto_weekday));
 
719
                        
 
720
                        nxtmonth = ceil((gdouble)g_date_get_month(maxdate)/(gdouble)nbmonth)*(gdouble)nbmonth;
 
721
                        DB( g_print("nxtmonth: %f =  ceil( %d / %d) / %d\n", nxtmonth, g_date_get_month(today), month, month) );
 
722
                        g_date_set_day(maxdate, auto_weekday);
 
723
                        g_date_set_month(maxdate, nxtmonth);
 
724
 
 
725
                        //if( today > g_date_get_julian(maxdate) )
 
726
                        //      g_date_add_months(maxdate, month);
 
727
 
 
728
                        nbdays = g_date_days_between(today, maxdate);
 
729
                
 
730
                        g_date_free(maxdate);
 
731
                        g_date_free(today);
 
732
                }
 
733
                break;
 
734
 
 
735
                //add xx days in advance the current date
 
736
                //if(auto_smode == 1)
 
737
                case ARC_POSTMODE_ADVANCE:
 
738
                        nbdays = auto_nbdays;
 
739
                        break;
 
740
 
 
741
                case ARC_POSTMODE_DUEDATE:
 
742
                default:
 
743
                        nbdays = 0;
 
744
                        break;
 
745
        }
 
746
 
 
747
        return start + nbdays;
669
748
}
670
749
 
671
750
 
680
759
 
681
760
        count = 0;
682
761
 
683
 
        maxpostdate = scheduled_date_get_post_max();
 
762
        maxpostdate = scheduled_date_get_post_max(GLOBALS->today, GLOBALS->auto_smode, GLOBALS->auto_nbdays, GLOBALS->auto_weekday, GLOBALS->auto_nbmonths);
684
763
        
685
764
        list = g_list_first(GLOBALS->arc_list);
686
765
        while (list != NULL)