-
Notifications
You must be signed in to change notification settings - Fork 762
/
SchedXaction.c
1273 lines (1119 loc) · 36.9 KB
/
SchedXaction.c
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************\
* SchedXaction.c -- Scheduled Transaction implementation. *
* Copyright (C) 2001,2007 Joshua Sled <jsled@asynchronous.org> *
* *
* This program 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. *
* *
* This program 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, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
* *
\********************************************************************/
#include <config.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <string.h>
#include <stdint.h>
#include "qof.h"
#include "Account.h"
#include "SX-book.h"
#include "SX-book-p.h"
#include "SX-ttinfo.h"
#include "SchedXaction.h"
#include "Transaction.h"
#include "gnc-engine.h"
#include "engine-helpers.h"
#include "qofinstance-p.h"
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "gnc.engine.sx"
enum
{
PROP_0,
PROP_NAME, /* Table */
PROP_ENABLED, /* Table */
PROP_START_DATE, /* Table */
PROP_END_DATE, /* Table */
PROP_LAST_OCCURANCE_DATE, /* Table */
PROP_NUM_OCCURANCE, /* Table */
PROP_REM_OCCURANCE, /* Table */
PROP_AUTO_CREATE, /* Table */
PROP_AUTO_CREATE_NOTIFY, /* Table */
PROP_ADVANCE_CREATION_DAYS, /* Table */
PROP_ADVANCE_REMINDER_DAYS, /* Table */
PROP_INSTANCE_COUNT, /* Table */
PROP_TEMPLATE_ACCOUNT /* Table */
};
/* GObject initialization */
G_DEFINE_TYPE(SchedXaction, gnc_schedxaction, QOF_TYPE_INSTANCE)
static void
gnc_schedxaction_init(SchedXaction* sx)
{
sx->schedule = NULL;
g_date_clear( &sx->last_date, 1 );
g_date_clear( &sx->start_date, 1 );
g_date_clear( &sx->end_date, 1 );
sx->enabled = 1;
sx->num_occurances_total = 0;
sx->autoCreateOption = FALSE;
sx->autoCreateNotify = FALSE;
sx->advanceCreateDays = 0;
sx->advanceRemindDays = 0;
sx->instance_num = 0;
sx->deferredList = NULL;
}
static void
gnc_schedxaction_dispose(GObject *sxp)
{
G_OBJECT_CLASS(gnc_schedxaction_parent_class)->dispose(sxp);
}
static void
gnc_schedxaction_finalize(GObject* sxp)
{
G_OBJECT_CLASS(gnc_schedxaction_parent_class)->finalize(sxp);
}
/* Note that g_value_set_object() refs the object, as does
* g_object_get(). But g_object_get() only unrefs once when it disgorges
* the object, leaving an unbalanced ref, which leaks. So instead of
* using g_value_set_object(), use g_value_take_object() which doesn't
* ref the object when used in get_property().
*/
static void
gnc_schedxaction_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
SchedXaction *sx;
g_return_if_fail(GNC_IS_SCHEDXACTION(object));
sx = GNC_SCHEDXACTION(object);
switch (prop_id)
{
case PROP_NAME:
g_value_set_string(value, sx->name);
break;
case PROP_ENABLED:
g_value_set_boolean(value, sx->enabled);
break;
case PROP_NUM_OCCURANCE:
g_value_set_int(value, sx->num_occurances_total);
break;
case PROP_REM_OCCURANCE:
g_value_set_int(value, sx->num_occurances_remain);
break;
case PROP_AUTO_CREATE:
g_value_set_boolean(value, sx->autoCreateOption);
break;
case PROP_AUTO_CREATE_NOTIFY:
g_value_set_boolean(value, sx->autoCreateNotify);
break;
case PROP_ADVANCE_CREATION_DAYS:
g_value_set_int(value, sx->advanceCreateDays);
break;
case PROP_ADVANCE_REMINDER_DAYS:
g_value_set_int(value, sx->advanceRemindDays);
break;
case PROP_START_DATE:
g_value_set_boxed(value, &sx->start_date);
break;
case PROP_END_DATE:
/* g_value_set_boxed raises a critical error if sx->end_date
* is invalid */
if (g_date_valid (&sx->end_date))
g_value_set_boxed(value, &sx->end_date);
break;
case PROP_LAST_OCCURANCE_DATE:
/* g_value_set_boxed raises a critical error if sx->last_date
* is invalid */
if (g_date_valid (&sx->last_date))
g_value_set_boxed(value, &sx->last_date);
break;
case PROP_INSTANCE_COUNT:
g_value_set_int(value, sx->instance_num);
break;
case PROP_TEMPLATE_ACCOUNT:
g_value_take_object(value, sx->template_acct);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
gnc_schedxaction_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
SchedXaction *sx;
g_return_if_fail(GNC_IS_SCHEDXACTION(object));
sx = GNC_SCHEDXACTION(object);
g_assert (qof_instance_get_editlevel(sx));
switch (prop_id)
{
case PROP_NAME:
xaccSchedXactionSetName(sx, g_value_get_string(value));
break;
case PROP_ENABLED:
xaccSchedXactionSetEnabled(sx, g_value_get_boolean(value));
break;
case PROP_NUM_OCCURANCE:
xaccSchedXactionSetNumOccur(sx, g_value_get_int(value));
break;
case PROP_REM_OCCURANCE:
xaccSchedXactionSetRemOccur(sx, g_value_get_int(value));
break;
case PROP_AUTO_CREATE:
xaccSchedXactionSetAutoCreate(sx, g_value_get_boolean(value), sx->autoCreateNotify);
break;
case PROP_AUTO_CREATE_NOTIFY:
xaccSchedXactionSetAutoCreate(sx, sx->autoCreateOption, g_value_get_boolean(value));
break;
case PROP_ADVANCE_CREATION_DAYS:
xaccSchedXactionSetAdvanceCreation(sx, g_value_get_int(value));
break;
case PROP_ADVANCE_REMINDER_DAYS:
xaccSchedXactionSetAdvanceReminder(sx, g_value_get_int(value));
break;
case PROP_START_DATE:
/* Note: when passed through a boxed gvalue, the julian value of the date is copied.
The date may appear invalid until a function requiring for dmy calculation is
called. */
xaccSchedXactionSetStartDate(sx, g_value_get_boxed(value));
break;
case PROP_END_DATE:
/* Note: when passed through a boxed gvalue, the julian value of the date is copied.
The date may appear invalid until a function requiring for dmy calculation is
called. */
xaccSchedXactionSetEndDate(sx, g_value_get_boxed(value));
break;
case PROP_LAST_OCCURANCE_DATE:
/* Note: when passed through a boxed gvalue, the julian value of the date is copied.
The date may appear invalid until a function requiring for dmy calculation is
called. */
xaccSchedXactionSetLastOccurDate(sx, g_value_get_boxed(value));
break;
case PROP_INSTANCE_COUNT:
gnc_sx_set_instance_count(sx, g_value_get_int(value));
break;
case PROP_TEMPLATE_ACCOUNT:
sx_set_template_account(sx, g_value_get_object(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
gnc_schedxaction_class_init (SchedXactionClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->dispose = gnc_schedxaction_dispose;
gobject_class->finalize = gnc_schedxaction_finalize;
gobject_class->set_property = gnc_schedxaction_set_property;
gobject_class->get_property = gnc_schedxaction_get_property;
g_object_class_install_property
(gobject_class,
PROP_NAME,
g_param_spec_string ("name",
"Scheduled Transaction Name",
"The name is an arbitrary string "
"assigned by the user. It is intended to "
"a short, 5 to 30 character long string "
"that is displayed by the GUI.",
NULL,
G_PARAM_READWRITE));
g_object_class_install_property
(gobject_class,
PROP_ENABLED,
g_param_spec_boolean ("enabled",
"Enabled",
"TRUE if the scheduled transaction is enabled.",
TRUE,
G_PARAM_READWRITE));
g_object_class_install_property
(gobject_class,
PROP_NUM_OCCURANCE,
g_param_spec_int ("num-occurance",
"Number of occurrences",
"Total number of occurrences for this scheduled transaction.",
0,
G_MAXINT16,
1,
G_PARAM_READWRITE));
g_object_class_install_property
(gobject_class,
PROP_REM_OCCURANCE,
g_param_spec_int ("rem-occurance",
"Number of occurrences remaining",
"Remaining number of occurrences for this scheduled transaction.",
0,
G_MAXINT16,
1,
G_PARAM_READWRITE));
g_object_class_install_property
(gobject_class,
PROP_AUTO_CREATE,
g_param_spec_boolean ("auto-create",
"Auto-create",
"TRUE if the transaction will be automatically "
"created when its time comes.",
FALSE,
G_PARAM_READWRITE));
g_object_class_install_property
(gobject_class,
PROP_AUTO_CREATE_NOTIFY,
g_param_spec_boolean ("auto-create-notify",
"Auto-create-notify",
"TRUE if the the user will be notified when the transaction "
"is automatically created.",
FALSE,
G_PARAM_READWRITE));
g_object_class_install_property
(gobject_class,
PROP_ADVANCE_CREATION_DAYS,
g_param_spec_int ("advance-creation-days",
"Days in advance to create",
"Number of days in advance to create this scheduled transaction.",
0,
G_MAXINT16,
0,
G_PARAM_READWRITE));
g_object_class_install_property
(gobject_class,
PROP_ADVANCE_REMINDER_DAYS,
g_param_spec_int ("advance-reminder-days",
"Days in advance to remind",
"Number of days in advance to remind about this scheduled transaction.",
0,
G_MAXINT16,
0,
G_PARAM_READWRITE));
g_object_class_install_property
(gobject_class,
PROP_START_DATE,
g_param_spec_boxed("start-date",
"Start Date",
"Date for the first occurrence for the scheduled transaction.",
G_TYPE_DATE,
G_PARAM_READWRITE));
g_object_class_install_property
(gobject_class,
PROP_END_DATE,
g_param_spec_boxed("end-date",
"End Date",
"Date for the scheduled transaction to end.",
G_TYPE_DATE,
G_PARAM_READWRITE));
g_object_class_install_property
(gobject_class,
PROP_LAST_OCCURANCE_DATE,
g_param_spec_boxed("last-occurance-date",
"Last Occurrence Date",
"Date for the last occurrence of the scheduled transaction.",
G_TYPE_DATE,
G_PARAM_READWRITE));
g_object_class_install_property
(gobject_class,
PROP_INSTANCE_COUNT,
g_param_spec_int ("instance-count",
"Instance count",
"Number of instances of this scheduled transaction.",
0,
G_MAXINT16,
0,
G_PARAM_READWRITE));
g_object_class_install_property
(gobject_class,
PROP_TEMPLATE_ACCOUNT,
g_param_spec_object("template-account",
"Template account",
"Account which holds the template transactions.",
GNC_TYPE_ACCOUNT,
G_PARAM_READWRITE));
}
static void
xaccSchedXactionInit(SchedXaction *sx, QofBook *book)
{
Account *ra;
const GncGUID *guid;
gchar guidstr[GUID_ENCODING_LENGTH+1];
qof_instance_init_data (&sx->inst, GNC_ID_SCHEDXACTION, book);
/* create a new template account for our splits */
sx->template_acct = xaccMallocAccount(book);
guid = qof_instance_get_guid( sx );
xaccAccountBeginEdit( sx->template_acct );
guid_to_string_buff( guid, guidstr );
xaccAccountSetName( sx->template_acct, guidstr);
xaccAccountSetCommodity
(sx->template_acct,
gnc_commodity_table_lookup( gnc_commodity_table_get_table(book),
GNC_COMMODITY_NS_TEMPLATE, "template") );
xaccAccountSetType( sx->template_acct, ACCT_TYPE_BANK );
xaccAccountCommitEdit( sx->template_acct );
ra = gnc_book_get_template_root( book );
gnc_account_append_child( ra, sx->template_acct );
}
SchedXaction*
xaccSchedXactionMalloc(QofBook *book)
{
SchedXaction *sx;
g_return_val_if_fail (book, NULL);
sx = g_object_new(GNC_TYPE_SCHEDXACTION, NULL);
xaccSchedXactionInit( sx, book );
qof_event_gen( &sx->inst, QOF_EVENT_CREATE , NULL);
return sx;
}
static void
sxprivTransMapDelete( gpointer data, gpointer user_data )
{
Transaction *t = (Transaction *) data;
xaccTransBeginEdit( t );
xaccTransDestroy( t );
xaccTransCommitEdit( t );
return;
}
static void
delete_template_trans(SchedXaction *sx)
{
GList *templ_acct_splits, *curr_split_listref;
Split *curr_split;
Transaction *split_trans;
GList *templ_acct_transactions = NULL;
templ_acct_splits
= xaccAccountGetSplitList(sx->template_acct);
for (curr_split_listref = templ_acct_splits;
curr_split_listref;
curr_split_listref = curr_split_listref->next)
{
curr_split = (Split *) curr_split_listref->data;
split_trans = xaccSplitGetParent(curr_split);
if (! (g_list_find(templ_acct_transactions, split_trans)))
{
templ_acct_transactions
= g_list_prepend(templ_acct_transactions, split_trans);
}
}
g_list_foreach(templ_acct_transactions,
sxprivTransMapDelete,
NULL);
g_list_free (templ_acct_transactions);
return;
}
void
sx_set_template_account (SchedXaction *sx, Account *account)
{
Account *old;
old = sx->template_acct;
sx->template_acct = account;
if (old)
{
xaccAccountBeginEdit(old);
xaccAccountDestroy(old);
}
}
void
xaccSchedXactionDestroy( SchedXaction *sx )
{
qof_instance_set_destroying( QOF_INSTANCE(sx), TRUE );
gnc_sx_commit_edit( sx );
}
static void
xaccSchedXactionFree( SchedXaction *sx )
{
GList *l;
if ( sx == NULL ) return;
qof_event_gen( &sx->inst, QOF_EVENT_DESTROY , NULL);
if ( sx->name )
g_free( sx->name );
/*
* we have to delete the transactions in the
* template account ourselves
*/
delete_template_trans( sx );
xaccAccountBeginEdit( sx->template_acct );
xaccAccountDestroy( sx->template_acct );
for ( l = sx->deferredList; l; l = l->next )
{
gnc_sx_destroy_temporal_state( l->data );
l->data = NULL;
}
if ( sx->deferredList )
{
g_list_free( sx->deferredList );
sx->deferredList = NULL;
}
/* a GList of Recurrences */
g_list_free_full (sx->schedule, g_free);
/* qof_instance_release (&sx->inst); */
g_object_unref( sx );
}
/* ============================================================ */
void
gnc_sx_begin_edit (SchedXaction *sx)
{
qof_begin_edit (&sx->inst);
}
static void sx_free(QofInstance* inst )
{
xaccSchedXactionFree( GNC_SX(inst) );
}
static void commit_err (QofInstance *inst, QofBackendError errcode)
{
g_critical("Failed to commit: %d", errcode);
gnc_engine_signal_commit_error( errcode );
}
static void commit_done(QofInstance *inst)
{
qof_event_gen (inst, QOF_EVENT_MODIFY, NULL);
}
void
gnc_sx_commit_edit (SchedXaction *sx)
{
if (!qof_commit_edit (QOF_INSTANCE(sx))) return;
qof_commit_edit_part2 (&sx->inst, commit_err, commit_done, sx_free);
}
/* ============================================================ */
GList*
gnc_sx_get_schedule(const SchedXaction *sx)
{
return sx->schedule;
}
void
gnc_sx_set_schedule(SchedXaction *sx, GList *schedule)
{
g_return_if_fail(sx);
gnc_sx_begin_edit(sx);
sx->schedule = schedule;
qof_instance_set_dirty(&sx->inst);
gnc_sx_commit_edit(sx);
}
gchar *
xaccSchedXactionGetName( const SchedXaction *sx )
{
return sx->name;
}
void
xaccSchedXactionSetName( SchedXaction *sx, const gchar *newName )
{
g_return_if_fail( newName != NULL );
gnc_sx_begin_edit(sx);
if ( sx->name != NULL )
{
g_free( sx->name );
sx->name = NULL;
}
sx->name = g_strdup( newName );
qof_instance_set_dirty(&sx->inst);
gnc_sx_commit_edit(sx);
}
const GDate*
xaccSchedXactionGetStartDate(const SchedXaction *sx )
{
g_assert (sx);
return &sx->start_date;
}
time64
xaccSchedXactionGetStartDateTT(const SchedXaction *sx )
{
g_assert (sx);
return gdate_to_time64(sx->start_date);
}
void
xaccSchedXactionSetStartDate( SchedXaction *sx, const GDate* newStart )
{
if ( newStart == NULL || !g_date_valid( newStart ))
{
/* XXX: I reject the bad data - is this the right
* thing to do <rgmerk>.
* This warning is only human readable - the caller
* doesn't know the call failed. This is bad
*/
g_critical("Invalid Start Date");
return;
}
gnc_sx_begin_edit(sx);
sx->start_date = *newStart;
qof_instance_set_dirty(&sx->inst);
gnc_sx_commit_edit(sx);
}
void
xaccSchedXactionSetStartDateTT( SchedXaction *sx, const time64 newStart )
{
if ( newStart == INT64_MAX )
{
/* XXX: I reject the bad data - is this the right
* thing to do <rgmerk>.
* This warning is only human readable - the caller
* doesn't know the call failed. This is bad
*/
g_critical("Invalid Start Date");
return;
}
gnc_sx_begin_edit(sx);
gnc_gdate_set_time64(&sx->start_date, newStart);
qof_instance_set_dirty(&sx->inst);
gnc_sx_commit_edit(sx);
}
gboolean
xaccSchedXactionHasEndDate( const SchedXaction *sx )
{
return sx != NULL && g_date_valid( &sx->end_date );
}
const GDate*
xaccSchedXactionGetEndDate(const SchedXaction *sx )
{
g_assert (sx);
return &sx->end_date;
}
void
xaccSchedXactionSetEndDate( SchedXaction *sx, const GDate *newEnd )
{
/* Note that an invalid GDate IS a permissible value: It means that
* the SX is to run "forever". See gnc_sxed_save_sx() and
* schedXact_editor_populate() in dialog-sx-editor.c.
*/
if (newEnd == NULL ||
(g_date_valid(newEnd) && g_date_compare( newEnd, &sx->start_date ) < 0 ))
{
/* XXX: I reject the bad data - is this the right
* thing to do <rgmerk>.
* This warning is only human readable - the caller
* doesn't know the call failed. This is bad
*/
g_critical("Bad End Date: Invalid or before Start Date");
return;
}
gnc_sx_begin_edit(sx);
sx->end_date = *newEnd;
qof_instance_set_dirty(&sx->inst);
gnc_sx_commit_edit(sx);
}
const GDate*
xaccSchedXactionGetLastOccurDate(const SchedXaction *sx )
{
return &sx->last_date;
}
time64
xaccSchedXactionGetLastOccurDateTT(const SchedXaction *sx )
{
return gdate_to_time64(sx->last_date);
}
void
xaccSchedXactionSetLastOccurDate(SchedXaction *sx, const GDate* new_last_occur)
{
g_return_if_fail (new_last_occur != NULL);
if (g_date_valid(&sx->last_date)
&& g_date_compare(&sx->last_date, new_last_occur) == 0)
return;
gnc_sx_begin_edit(sx);
sx->last_date = *new_last_occur;
qof_instance_set_dirty(&sx->inst);
gnc_sx_commit_edit(sx);
}
void
xaccSchedXactionSetLastOccurDateTT(SchedXaction *sx, time64 new_last_occur)
{
GDate last_occur;
g_return_if_fail (new_last_occur != INT64_MAX);
gnc_gdate_set_time64(&last_occur, new_last_occur);
if (g_date_valid(&sx->last_date)
&& g_date_compare(&sx->last_date, &last_occur) == 0)
return;
gnc_sx_begin_edit(sx);
sx->last_date = last_occur;
qof_instance_set_dirty(&sx->inst);
gnc_sx_commit_edit(sx);
}
gboolean
xaccSchedXactionHasOccurDef( const SchedXaction *sx )
{
return ( xaccSchedXactionGetNumOccur( sx ) != 0 );
}
gint
xaccSchedXactionGetNumOccur( const SchedXaction *sx )
{
return sx->num_occurances_total;
}
void
xaccSchedXactionSetNumOccur(SchedXaction *sx, gint new_num)
{
if (sx->num_occurances_total == new_num)
return;
gnc_sx_begin_edit(sx);
sx->num_occurances_remain = sx->num_occurances_total = new_num;
qof_instance_set_dirty(&sx->inst);
gnc_sx_commit_edit(sx);
}
gint
xaccSchedXactionGetRemOccur( const SchedXaction *sx )
{
return sx->num_occurances_remain;
}
void
xaccSchedXactionSetRemOccur(SchedXaction *sx, gint num_remain)
{
/* FIXME This condition can be tightened up */
if (num_remain > sx->num_occurances_total)
{
g_warning("number remaining [%d] > total occurrences [%d]",
num_remain, sx->num_occurances_total);
}
else
{
if (num_remain == sx->num_occurances_remain)
return;
gnc_sx_begin_edit(sx);
sx->num_occurances_remain = num_remain;
qof_instance_set_dirty(&sx->inst);
gnc_sx_commit_edit(sx);
}
}
gint gnc_sx_get_num_occur_daterange(const SchedXaction *sx, const GDate* start_date, const GDate* end_date)
{
gint result = 0;
SXTmpStateData *tmpState;
gboolean countFirstDate;
/* SX still active? If not, return now. */
if ((xaccSchedXactionHasOccurDef(sx)
&& xaccSchedXactionGetRemOccur(sx) <= 0)
|| (xaccSchedXactionHasEndDate(sx)
&& g_date_compare(xaccSchedXactionGetEndDate(sx), start_date) < 0))
{
return result;
}
tmpState = gnc_sx_create_temporal_state (sx);
/* Should we count the first valid date we encounter? Only if the
* SX has not yet occurred so far, or if its last valid date was
* before the start date. */
countFirstDate = !g_date_valid(&tmpState->last_date)
|| (g_date_compare(&tmpState->last_date, start_date) < 0);
/* No valid date? SX has never occurred so far. */
if (!g_date_valid(&tmpState->last_date))
{
/* SX has never occurred so far */
gnc_sx_incr_temporal_state (sx, tmpState);
if (xaccSchedXactionHasOccurDef(sx) && tmpState->num_occur_rem < 0)
{
gnc_sx_destroy_temporal_state (tmpState);
return result;
}
}
/* Increase the tmpState until we are in our interval of
* interest. Only calculate anything if the sx hasn't already
* ended. */
while (g_date_compare(&tmpState->last_date, start_date) < 0)
{
gnc_sx_incr_temporal_state (sx, tmpState);
if (xaccSchedXactionHasOccurDef(sx) && tmpState->num_occur_rem < 0)
{
gnc_sx_destroy_temporal_state (tmpState);
return result;
}
}
/* Now we are in our interval of interest. Increment the
* occurrence date until we are beyond the end of our
* interval. Make sure to check for invalid dates here: It means
* the SX has ended. */
while (g_date_valid(&tmpState->last_date)
&& (g_date_compare(&tmpState->last_date, end_date) <= 0)
&& (!xaccSchedXactionHasEndDate(sx)
|| g_date_compare(&tmpState->last_date, xaccSchedXactionGetEndDate(sx)) <= 0)
&& (!xaccSchedXactionHasOccurDef(sx)
/* The >=0 (i.e. the ==) is important here, otherwise
* we miss the last valid occurrence of a SX which is
* limited by num_occur */
|| tmpState->num_occur_rem >= 0))
{
++result;
gnc_sx_incr_temporal_state (sx, tmpState);
}
/* If the first valid date shouldn't be counted, decrease the
* result number by one. */
if (!countFirstDate && result > 0)
--result;
gnc_sx_destroy_temporal_state (tmpState);
return result;
}
gboolean
xaccSchedXactionGetEnabled( const SchedXaction *sx )
{
return sx->enabled;
}
void
xaccSchedXactionSetEnabled( SchedXaction *sx, gboolean newEnabled)
{
gnc_sx_begin_edit(sx);
sx->enabled = newEnabled;
qof_instance_set_dirty(&sx->inst);
gnc_sx_commit_edit(sx);
}
void
xaccSchedXactionGetAutoCreate( const SchedXaction *sx,
gboolean *outAutoCreate,
gboolean *outNotify )
{
if (outAutoCreate != NULL)
*outAutoCreate = sx->autoCreateOption;
if (outNotify != NULL)
*outNotify = sx->autoCreateNotify;
return;
}
void
xaccSchedXactionSetAutoCreate( SchedXaction *sx,
gboolean newAutoCreate,
gboolean newNotify )
{
gnc_sx_begin_edit(sx);
sx->autoCreateOption = newAutoCreate;
sx->autoCreateNotify = newNotify;
qof_instance_set_dirty(&sx->inst);
gnc_sx_commit_edit(sx);
return;
}
gint
xaccSchedXactionGetAdvanceCreation( const SchedXaction *sx )
{
return sx->advanceCreateDays;
}
void
xaccSchedXactionSetAdvanceCreation( SchedXaction *sx, gint createDays )
{
gnc_sx_begin_edit(sx);
sx->advanceCreateDays = createDays;
qof_instance_set_dirty(&sx->inst);
gnc_sx_commit_edit(sx);
}
gint
xaccSchedXactionGetAdvanceReminder( const SchedXaction *sx )
{
return sx->advanceRemindDays;
}
void
xaccSchedXactionSetAdvanceReminder( SchedXaction *sx, gint reminderDays )
{
gnc_sx_begin_edit(sx);
sx->advanceRemindDays = reminderDays;
qof_instance_set_dirty(&sx->inst);
gnc_sx_commit_edit(sx);
}
GDate
xaccSchedXactionGetNextInstance (const SchedXaction *sx, SXTmpStateData *tsd)
{
GDate prev_occur, next_occur;
g_date_clear( &prev_occur, 1 );
if ( tsd != NULL )
prev_occur = tsd->last_date;
/* If prev_occur is in the "cleared" state and sx->start_date isn't, then
* we're at the beginning. We want to pretend prev_occur is the day before
* the start_date in case the start_date is today so that the SX will fire
* today. If start_date isn't valid either then the SX will fire anyway, no
* harm done. prev_occur cannot be before start_date either.
*/
if (g_date_valid (&sx->start_date) && (!g_date_valid ( &prev_occur ) || g_date_compare (&prev_occur, &sx->start_date)<0))
{
/* We must be at the beginning. */
prev_occur = sx->start_date;
g_date_subtract_days (&prev_occur, 1 );
}
recurrenceListNextInstance(sx->schedule, &prev_occur, &next_occur);
if ( xaccSchedXactionHasEndDate( sx ) )
{
const GDate *end_date = xaccSchedXactionGetEndDate( sx );
if ( g_date_compare( &next_occur, end_date ) > 0 )
{
g_date_clear( &next_occur, 1 );
}
}
else if ( xaccSchedXactionHasOccurDef( sx ) )
{
if ((tsd && tsd->num_occur_rem == 0) ||
(!tsd && sx->num_occurances_remain == 0 ))
{
g_date_clear( &next_occur, 1 );
}
}
return next_occur;
}
gint
gnc_sx_get_instance_count( const SchedXaction *sx, SXTmpStateData *stateData )
{
gint toRet = -1;
SXTmpStateData *tsd;
if ( stateData )
{
tsd = (SXTmpStateData*)stateData;
toRet = tsd->num_inst;
}
else
{
toRet = sx->instance_num;
}
return toRet;
}
void
gnc_sx_set_instance_count(SchedXaction *sx, gint instance_num)
{
g_return_if_fail(sx);
if (sx->instance_num == instance_num)
return;
gnc_sx_begin_edit(sx);
sx->instance_num = instance_num;
qof_instance_set_dirty(&sx->inst);
gnc_sx_commit_edit(sx);
}
GList *
xaccSchedXactionGetSplits( const SchedXaction *sx )
{
g_return_val_if_fail( sx, NULL );
return xaccAccountGetSplitList(sx->template_acct);
}
static Split *
pack_split_info (TTSplitInfo *s_info, Account *parent_acct,
Transaction *parent_trans, QofBook *book)