/
dialog-sx-editor.c
1820 lines (1533 loc) · 61.1 KB
/
dialog-sx-editor.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
/********************************************************************\
* dialog-sx-editor.c : dialog for scheduled transaction editing *
* Copyright (C) 2001,2002,2006 Joshua Sled <jsled@asynchronous.org>*
* Copyright (C) 2011 Robert Fewell *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of version 2 and/or version 3 of the *
* GNU General Public License as published by the Free Software *
* Foundation. *
* *
* As a special exception, permission is granted to link the binary *
* module resultant from this code with the OpenSSL project's *
* "OpenSSL" library (or modified versions of it that use the same *
* license as the "OpenSSL" library), and distribute the linked *
* executable. You must obey the GNU General Public License in all *
* respects for all of the code used other than "OpenSSL". If you *
* modify this file, you may extend this exception to your version *
* of the file, but you are not obligated to do so. If you do not *
* wish to do so, delete this exception statement from your version *
* of this file. *
* *
* 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 <gtk/gtk.h>
#include <glib/gi18n.h>
#include <locale.h>
#include <gnc-gdate-utils.h>
#include "qof.h"
#include "Account.h"
#include "SchedXaction.h"
#include "SX-book.h"
#include "dialog-preferences.h"
#include "dialog-sx-editor.h"
#include "dialog-utils.h"
#include "gnc-component-manager.h"
#include "gnc-date.h"
#include "gnc-date-edit.h"
#include "gnc-dense-cal.h"
#include "gnc-dense-cal-store.h"
#include "gnc-embedded-window.h"
#include "gnc-engine.h"
#include "gnc-frequency.h"
#include "gnc-gui-query.h"
#include "gnc-hooks.h"
#include "gnc-ledger-display.h"
#include "gnc-plugin-page.h"
#include "gnc-plugin-page-register.h"
#include "gnc-prefs.h"
#include "gnc-ui.h"
#include "gnc-ui-util.h"
#include "gnucash-sheet.h"
#include "gnc-split-reg.h"
#include "gnc-sx-instance-model.h"
#include "dialog-sx-since-last-run.h"
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "gnc.gui.sx.editor"
static QofLogModule log_module = GNC_MOD_GUI_SX;
static gint _sx_engine_event_handler_id = -1;
#define END_NEVER_OPTION 0
#define END_DATE_OPTION 1
#define NUM_OCCUR_OPTION 2
#define NUM_LEDGER_LINES_DEFAULT 6
#define EX_CAL_NUM_MONTHS 6
#define EX_CAL_MO_PER_COL 3
#define GNC_D_WIDTH 25
#define GNC_D_BUF_WIDTH 26
/** Datatypes ***********************************************************/
typedef enum _EndTypeEnum
{
END_NEVER,
END_DATE,
END_OCCUR,
} EndType;
typedef enum { NO_END, DATE_END, COUNT_END } END_TYPE;
struct _GncSxEditorDialog
{
GtkWidget *dialog;
GtkBuilder *builder;
GtkNotebook *notebook;
SchedXaction *sx;
/* If this is a new scheduled transaction or not. */
int newsxP;
/* The various widgets in the dialog */
GNCLedgerDisplay *ledger;
GncFrequency *gncfreq;
GncDenseCalStore *dense_cal_model;
GncDenseCal *example_cal;
GtkEditable *nameEntry;
GtkLabel *lastOccurLabel;
GtkToggleButton *enabledOpt;
GtkToggleButton *autocreateOpt;
GtkToggleButton *notifyOpt;
GtkToggleButton *advanceOpt;
GtkSpinButton *advanceSpin;
GtkToggleButton *remindOpt;
GtkSpinButton *remindSpin;
GtkToggleButton *optEndDate;
GtkToggleButton *optEndNone;
GtkToggleButton *optEndCount;
GtkEntry *endCountSpin;
GtkEntry *endRemainSpin;
GNCDateEdit *endDateEntry;
char *sxGUIDstr;
GncEmbeddedWindow *embed_window;
GncPluginPage *plugin_page;
};
/** Prototypes **********************************************************/
static void schedXact_editor_create_freq_sel( GncSxEditorDialog *sxed );
static void schedXact_editor_create_ledger( GncSxEditorDialog *sxed );
static void schedXact_editor_populate( GncSxEditorDialog * );
static void endgroup_rb_toggled_cb( GtkButton *b, gpointer d );
static void set_endgroup_toggle_states( GncSxEditorDialog *sxed, EndType t );
static void advance_toggled_cb( GtkButton *b, GncSxEditorDialog *sxed );
static void remind_toggled_cb( GtkButton *b, GncSxEditorDialog *sxed );
static gboolean gnc_sxed_check_consistent( GncSxEditorDialog *sxed );
static gboolean gnc_sxed_check_changed( GncSxEditorDialog *sxed );
static void gnc_sxed_save_sx( GncSxEditorDialog *sxed );
static void gnc_sxed_freq_changed( GncFrequency *gf, gpointer ud );
static void sxed_excal_update_adapt_cb( GtkWidget *o, gpointer ud );
static void gnc_sxed_update_cal(GncSxEditorDialog *sxed);
void on_sx_check_toggled_cb (GtkWidget *togglebutton, gpointer user_data);
static void gnc_sxed_reg_check_close(GncSxEditorDialog *sxed);
static gboolean sxed_delete_event( GtkWidget *widget, GdkEvent *event, gpointer ud );
static gboolean sxed_confirmed_cancel( GncSxEditorDialog *sxed );
static gboolean editor_component_sx_equality( gpointer find_data,
gpointer user_data );
static GtkActionEntry gnc_sxed_menu_entries [] =
{
{ "EditAction", NULL, N_("_Edit"), NULL, NULL, NULL },
{ "TransactionAction", NULL, N_("_Transaction"), NULL, NULL, NULL },
{ "ViewAction", NULL, N_("_View"), NULL, NULL, NULL },
{ "ActionsAction", NULL, N_("_Actions"), NULL, NULL, NULL },
};
static guint gnc_sxed_menu_n_entries = G_N_ELEMENTS (gnc_sxed_menu_entries);
/** Implementations *****************************************************/
static void
sxed_close_handler(gpointer user_data)
{
GncSxEditorDialog *sxed = user_data;
gnc_sxed_reg_check_close(sxed);
gnc_save_window_size( GNC_PREFS_GROUP_SXED, GTK_WINDOW(sxed->dialog) );
gtk_widget_destroy(sxed->dialog);
/* The data will be cleaned up in the destroy handler. */
}
/**
* @return TRUE if the user does want to cancel, FALSE if not. If TRUE is
* returned, the register's changes have been cancelled.
**/
static gboolean
sxed_confirmed_cancel(GncSxEditorDialog *sxed)
{
SplitRegister *reg;
reg = gnc_ledger_display_get_split_register( sxed->ledger );
/* check for changes */
if ( gnc_sxed_check_changed( sxed ) )
{
const char *sx_changed_msg =
_( "This Scheduled Transaction has changed; are you "
"sure you want to cancel?" );
if (!gnc_verify_dialog(sxed->dialog, FALSE, "%s", sx_changed_msg))
{
return FALSE;
}
}
/* cancel ledger changes */
gnc_split_register_cancel_cursor_trans_changes( reg );
return TRUE;
}
/**********************************
* Dialog Action Button functions *
*********************************/
static void
editor_cancel_button_clicked_cb( GtkButton *b, GncSxEditorDialog *sxed )
{
/* close */
if (!sxed_confirmed_cancel(sxed))
return;
gnc_close_gui_component_by_data( DIALOG_SCHEDXACTION_EDITOR_CM_CLASS,
sxed );
}
static void
editor_help_button_clicked_cb(GtkButton *b, GncSxEditorDialog *sxed)
{
gnc_gnome_help(HF_HELP, HL_SXEDITOR);
}
static void
editor_ok_button_clicked_cb( GtkButton *b, GncSxEditorDialog *sxed )
{
QofBook *book;
SchedXactions *sxes;
if ( !gnc_sxed_check_consistent( sxed ) )
return;
gnc_sxed_save_sx( sxed );
/* add to list */
// @@fixme -- forget 'new'-flag: check for existance of the SX [?]
if ( sxed->newsxP )
{
book = gnc_get_current_book ();
sxes = gnc_book_get_schedxactions(book);
gnc_sxes_add_sx(sxes, sxed->sx);
sxed->newsxP = FALSE;
}
/* cleanup */
gnc_close_gui_component_by_data( DIALOG_SCHEDXACTION_EDITOR_CM_CLASS,
sxed );
}
static gboolean
gnc_sxed_check_name_changed (GncSxEditorDialog *sxed)
{
char *name = gtk_editable_get_chars (GTK_EDITABLE(sxed->nameEntry), 0, -1);
if (strlen(name) == 0)
return TRUE;
if (xaccSchedXactionGetName(sxed->sx) == NULL ||
strcmp(xaccSchedXactionGetName(sxed->sx), name) != 0)
return TRUE;
return FALSE;
}
static gboolean
gnc_sxed_check_end_date_changed (GncSxEditorDialog *sxed)
{
GDate sxEndDate, dlgEndDate;
if (! xaccSchedXactionHasEndDate (sxed->sx))
return TRUE;
sxEndDate = *xaccSchedXactionGetEndDate (sxed->sx);
gnc_gdate_set_time64 (&dlgEndDate,
gnc_date_edit_get_date (sxed-> endDateEntry));
if (g_date_compare (&sxEndDate, &dlgEndDate) != 0)
return TRUE;
return FALSE;
}
static gboolean
gnc_sxed_check_num_occurs_changed(GncSxEditorDialog *sxed)
{
gint sxNumOccur, sxNumRem, dlgNumOccur, dlgNumRem;
if (! xaccSchedXactionGetNumOccur(sxed->sx))
return TRUE;
dlgNumOccur =
gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(sxed->endCountSpin));
dlgNumRem =
gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(sxed->endRemainSpin));
sxNumOccur = xaccSchedXactionGetNumOccur (sxed->sx);
sxNumRem = xaccSchedXactionGetRemOccur (sxed->sx);
if (dlgNumOccur != sxNumOccur || dlgNumRem != sxNumRem)
return TRUE;
return FALSE;
}
static gboolean
gnc_sxed_check_creation_changed (GncSxEditorDialog *sxed)
{
gboolean sxAutoCreate, sxNotify;
gint dlgAdvance = 0;
gint dlgRemind = 0;
gboolean dlgEnabled =
gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(sxed->enabledOpt) );
gboolean dlgAutoCreate =
gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(sxed->autocreateOpt) );
gboolean dlgNotify =
gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(sxed->notifyOpt) );
if (dlgEnabled != xaccSchedXactionGetEnabled (sxed->sx))
return TRUE;
xaccSchedXactionGetAutoCreate (sxed->sx, &sxAutoCreate, &sxNotify);
if (dlgAutoCreate != sxAutoCreate || dlgNotify != sxNotify)
return TRUE;
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(sxed->advanceOpt)))
dlgAdvance = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(sxed->advanceSpin));
if (dlgAdvance != xaccSchedXactionGetAdvanceCreation (sxed->sx))
return TRUE;
if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(sxed->remindOpt)))
dlgRemind = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(sxed->remindSpin));
if (dlgRemind != xaccSchedXactionGetAdvanceReminder (sxed->sx))
return TRUE;
return FALSE;
}
static gboolean
gnc_sxed_check_dates_changed (GncSxEditorDialog *sxed)
{
GList *dialog_schedule = NULL;
GDate dialog_start_date, sx_start_date;
gchar *dialog_schedule_str, *sx_schedule_str;
gboolean schedules_are_the_same, start_dates_are_the_same;
g_date_clear(&dialog_start_date, 1);
gnc_frequency_save_to_recurrence (sxed->gncfreq, &dialog_schedule,
&dialog_start_date);
dialog_schedule_str = recurrenceListToString(dialog_schedule);
recurrenceListFree(&dialog_schedule);
sx_start_date = *xaccSchedXactionGetStartDate (sxed->sx);
sx_schedule_str = recurrenceListToString (gnc_sx_get_schedule(sxed->sx));
g_debug ("dialog schedule [%s], sx schedule [%s]",
dialog_schedule_str, sx_schedule_str);
schedules_are_the_same = (strcmp(dialog_schedule_str,
sx_schedule_str) == 0);
g_free(dialog_schedule_str);
g_free(sx_schedule_str);
start_dates_are_the_same = (g_date_compare(&dialog_start_date,
&sx_start_date) == 0);
if (schedules_are_the_same && start_dates_are_the_same)
return FALSE;
return TRUE;
}
/*************************************************************************
* Checks to see if the SX has been modified from it's previously-saved
* state.
* @return TRUE if this is a 'new' SX, or if the SX has changed from it's
* previous configuration.
************************************************************************/
static gboolean
gnc_sxed_check_changed (GncSxEditorDialog *sxed)
{
SplitRegister *sr = NULL;
if (sxed->newsxP)
return TRUE;
/* name */
if (gnc_sxed_check_name_changed(sxed))
return TRUE;
/* end options */
/* dialog says... no end */
if (gtk_toggle_button_get_active (sxed->optEndNone) &&
(xaccSchedXactionHasEndDate (sxed->sx) ||
xaccSchedXactionHasOccurDef (sxed->sx)))
return TRUE;
/* dialog says... end date */
if (gtk_toggle_button_get_active( sxed->optEndDate ) &&
gnc_sxed_check_end_date_changed(sxed))
return TRUE;
/* dialog says... num occur */
if (gtk_toggle_button_get_active (sxed->optEndCount) &&
gnc_sxed_check_num_occurs_changed (sxed))
return TRUE;
/* SX options [autocreate, notify, reminder, advance] */
if (gnc_sxed_check_creation_changed (sxed))
return TRUE;
/* Dates and Schedules */
if (gnc_sxed_check_dates_changed (sxed))
return TRUE;
/* template transactions */
sr = gnc_ledger_display_get_split_register( sxed->ledger );
if ( gnc_split_register_changed( sr ) )
return TRUE;
return FALSE;
}
/*****************************************************************************
* Holds the credit- and debit-sum for a given Transaction, as used in
* gnc_sxed_check_consistent.
****************************************************************************/
typedef struct _txnCreditDebitSums
{
gnc_numeric creditSum;
gnc_numeric debitSum;
} txnCreditDebitSums;
static txnCreditDebitSums *
tcds_new (void)
{
txnCreditDebitSums *tcds = g_new0 (txnCreditDebitSums, 1);
tcds->creditSum = tcds->debitSum = gnc_numeric_zero();
return tcds;
}
static
void
set_sums_to_zero( gpointer key,
gpointer val,
gpointer ud )
{
txnCreditDebitSums *tcds = (txnCreditDebitSums*)val;
tcds->creditSum = gnc_numeric_zero();
tcds->debitSum = gnc_numeric_zero();
}
inline static gnc_numeric
tcds_difference (txnCreditDebitSums *tcds)
{
return gnc_numeric_sub_fixed (tcds->debitSum, tcds->creditSum);
}
static void
check_credit_debit_balance (gpointer key, gpointer val, gpointer ud)
{
txnCreditDebitSums *tcds = (txnCreditDebitSums*)val;
gboolean *unbalanced = (gboolean*)ud;
gnc_numeric diff = tcds_difference (tcds);
const char *result = gnc_numeric_zero_p (diff) ? "true" : "false";
*unbalanced |= !(gnc_numeric_zero_p(diff));
DEBUG ("%p | %s [%s - %s = %s]", key, result,
gnc_numeric_to_string (tcds->debitSum),
gnc_numeric_to_string (tcds->creditSum),
gnc_numeric_to_string (diff));
}
static gboolean
gnc_sxed_check_names (GncSxEditorDialog *sxed)
{
gchar *name, *nameKey;
gboolean nameExists, nameHasChanged;
GList *sxList;
name = gtk_editable_get_chars( GTK_EDITABLE(sxed->nameEntry), 0, -1 );
if ( strlen(name) == 0 )
{
const char *sx_has_no_name_msg =
_( "Please name the Scheduled Transaction." );
gnc_error_dialog( sxed->dialog, "%s", sx_has_no_name_msg );
g_free( name );
return FALSE;
}
nameExists = FALSE;
nameKey = g_utf8_collate_key(name, -1);
nameHasChanged =
(xaccSchedXactionGetName(sxed->sx) == NULL)
|| (strcmp (xaccSchedXactionGetName(sxed->sx), name) != 0);
for (sxList = gnc_book_get_schedxactions(gnc_get_current_book())->sx_list;
nameHasChanged && !nameExists && sxList;
sxList = sxList->next)
{
char *existingName, *existingNameKey;
existingName = xaccSchedXactionGetName ((SchedXaction*)sxList->data);
existingNameKey = g_utf8_collate_key(existingName, -1);
nameExists |= (strcmp(nameKey, existingNameKey) == 0 );
g_free (existingNameKey);
}
g_free (nameKey);
if (nameHasChanged && nameExists)
{
const char *sx_has_existing_name_msg =
_("A Scheduled Transaction with the name \"%s\" already exists. "
"Are you sure you want to name this one the same?");
if (!gnc_verify_dialog (sxed->dialog, FALSE,
sx_has_existing_name_msg, name))
{
g_free (name);
return FALSE;
}
}
g_free (name);
return TRUE;
}
static gboolean
gnc_sxed_check_endpoint (GncSxEditorDialog *sxed)
{
GDate startDate, endDate, nextDate;
GList *schedule = NULL;
if ( !gtk_toggle_button_get_active(sxed->optEndDate)
&& !gtk_toggle_button_get_active(sxed->optEndCount)
&& !gtk_toggle_button_get_active(sxed->optEndNone) )
{
const char *sx_end_spec_msg =
_("Please provide a valid end selection.");
gnc_error_dialog (sxed->dialog, "%s", sx_end_spec_msg);
return FALSE;
}
if (gtk_toggle_button_get_active (sxed->optEndCount))
{
gint occur =
gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(sxed->endCountSpin));
gint rem =
gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(sxed->endRemainSpin));
if (occur == 0)
{
const char *sx_occur_count_zero_msg =
_("There must be some number of occurrences.");
gnc_error_dialog (sxed->dialog, "%s", sx_occur_count_zero_msg);
return FALSE;
}
if (rem > occur)
{
const char *sx_occur_counts_wrong_msg =
_("The number of remaining occurrences (%d) is greater than "
"the number of total occurrences (%d).");
gnc_error_dialog (sxed->dialog, sx_occur_counts_wrong_msg,
rem, occur);
return FALSE;
}
return TRUE;
}
g_date_clear (&endDate, 1);
if (gtk_toggle_button_get_active (sxed->optEndDate))
{
gnc_gdate_set_time64 (&endDate,
gnc_date_edit_get_date (sxed-> endDateEntry));
}
g_date_clear (&nextDate, 1);
gnc_frequency_save_to_recurrence (sxed->gncfreq, &schedule, &startDate);
if (g_list_length(schedule) > 0)
{
g_date_subtract_days (&startDate, 1);
recurrenceListNextInstance (schedule, &startDate, &nextDate);
}
recurrenceListFree (&schedule);
if (!g_date_valid(&nextDate) ||
(g_date_valid(&endDate) && (g_date_compare(&nextDate, &endDate) > 0)))
{
const char *invalid_sx_check_msg =
_("You have attempted to create a Scheduled Transaction which "
"will never run. Do you really want to do this?");
if (!gnc_verify_dialog(sxed->dialog, FALSE,
"%s", invalid_sx_check_msg))
return FALSE;
}
return TRUE;
}
static gboolean
gnc_sxed_check_autocreate (GncSxEditorDialog *sxed, int ttVarCount,
int splitCount, gboolean multi_commodity)
{
gboolean autocreateState;
autocreateState =
gtk_toggle_button_get_active (
GTK_TOGGLE_BUTTON(sxed->autocreateOpt));
if (((ttVarCount > 0) || multi_commodity) && autocreateState)
{
gnc_warning_dialog(sxed->dialog, "%s",
_("Scheduled Transactions with variables "
"or involving more than one commodity "
"cannot be automatically created."));
return FALSE;
}
/* Fix for part of Bug#121740 -- auto-create transactions are
* only valid if there's actually a transaction to create. */
if (autocreateState && splitCount == 0)
{
gnc_warning_dialog(sxed->dialog, "%s",
_("Scheduled Transactions without a template "
"transaction cannot be automatically created.") );
return FALSE;
}
return TRUE;
}
static gboolean
gnc_sxed_split_check_account (GncSxEditorDialog *sxed, Split *s,
gnc_commodity *base_cmdty, gboolean *multi_cmdty)
{
gnc_commodity *split_cmdty = NULL;
gnc_numeric split_amount = gnc_numeric_zero ();
Account *acct;
KvpFrame *f = xaccSplitGetSlots (s);
KvpValue *v = kvp_frame_get_slot_path(f, GNC_SX_ID, GNC_SX_ACCOUNT, NULL);
GncGUID *acct_guid = kvp_value_get_guid (v);
acct = xaccAccountLookup( acct_guid, gnc_get_current_book ());
if (acct == NULL)
return FALSE;
split_cmdty = xaccAccountGetCommodity(acct);
split_amount = xaccSplitGetAmount(s);
if (!gnc_numeric_zero_p(split_amount) && base_cmdty == NULL)
{
base_cmdty = split_cmdty;
}
*multi_cmdty |= (!gnc_numeric_zero_p(split_amount) &&
!gnc_commodity_equal(split_cmdty, base_cmdty));
return TRUE;
}
static gboolean
gnc_sxed_split_calculate_formula (GncSxEditorDialog *sxed, Split *s,
GHashTable *vars, const char *key,
txnCreditDebitSums *tcds)
{
KvpFrame *f = xaccSplitGetSlots (s);
KvpValue *v = kvp_frame_get_slot_path (f, GNC_SX_ID, key, NULL);
gnc_numeric tmp = gnc_numeric_zero ();
char *str = NULL;
if (v == NULL || (str = kvp_value_get_string(v)) == NULL ||
strlen(str) == 0)
return TRUE; /* No formula no foul */
if (gnc_sx_parse_vars_from_formula (str, vars, &tmp) < 0)
{
gchar *err = g_strdup_printf (_("Couldn't parse %s for split \"%s\"."),
key, xaccSplitGetMemo (s));
gnc_error_dialog (GTK_WIDGET(sxed->dialog), "%s", err);
g_free (err);
return FALSE;
}
if (g_strcmp0 (key, GNC_SX_CREDIT_FORMULA) == 0)
tcds->creditSum = gnc_numeric_add(tcds->creditSum, tmp, 100,
GNC_DENOM_AUTO | GNC_HOW_DENOM_LCD);
else
tcds->debitSum = gnc_numeric_add (tcds->debitSum, tmp, 100,
GNC_DENOM_AUTO | GNC_HOW_DENOM_LCD);
return TRUE;
}
typedef struct
{
GncSxEditorDialog *sxed;
GHashTable *txns;
GHashTable *vars;
txnCreditDebitSums *tcds;
gboolean multi_commodity;
gboolean err;
} CheckTxnSplitData;
static void
split_error_warning_dialog (GtkWidget *parent, const gchar *title,
gchar *message)
{
GtkWidget *dialog = gtk_message_dialog_new (GTK_WINDOW (parent), 0,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
"%s", title);
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
"%s", message);
gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
g_signal_connect_swapped (dialog, "response",
G_CALLBACK(gtk_widget_destroy), dialog);
gtk_dialog_run (GTK_DIALOG (dialog));
}
static gboolean
check_transaction_splits (Transaction *txn, gpointer data)
{
GList *splitList = xaccTransGetSplitList (txn);
CheckTxnSplitData *sd = (CheckTxnSplitData*)data;
for ( ; splitList; splitList = splitList->next )
{
gnc_commodity *base_cmdty = NULL;
txnCreditDebitSums *tcds;
Split *s = (Split*)splitList->data;
tcds = (txnCreditDebitSums*)g_hash_table_lookup (sd->txns,
(gpointer)txn);
if (sd->tcds == NULL)
{
sd->tcds = tcds_new ();
g_hash_table_insert (sd->txns, (gpointer)txn, (gpointer)(sd->tcds));
}
if (!gnc_sxed_split_check_account (sd->sxed, s, base_cmdty,
&sd->multi_commodity))
{
gchar *message = g_strdup_printf
(_("Split with memo %s has an invalid account."),
xaccSplitGetMemo (s));
split_error_warning_dialog (sd->sxed->dialog,
_("Invalid Account in Split"),
message);
g_free (message);
sd->err = TRUE;
return FALSE;
}
if (!gnc_sxed_split_calculate_formula (sd->sxed, s, sd->vars,
GNC_SX_CREDIT_FORMULA,
sd->tcds))
{
gchar *message = g_strdup_printf
(_("Split with memo %s has an unparseable Credit Formula."),
xaccSplitGetMemo (s));
split_error_warning_dialog (sd->sxed->dialog,
_("Unparsable Formula in Split"),
message);
g_free (message);
sd->err = TRUE;
return FALSE;
}
if (!gnc_sxed_split_calculate_formula (sd->sxed, s, sd->vars,
GNC_SX_DEBIT_FORMULA,
sd->tcds))
{
gchar *message = g_strdup_printf
(_("Split with memo %s has an unparseable Debit Formula."),
xaccSplitGetMemo (s));
split_error_warning_dialog (sd->sxed->dialog,
_("Unparsable Formula in Split"),
message);
g_free (message);
sd->err = TRUE;
return FALSE;
}
}
return TRUE;
}
/*******************************************************************************
* Checks to make sure that the SX is in a reasonable state to save.
* @return true if checks out okay, false otherwise.
******************************************************************************/
static gboolean
gnc_sxed_check_consistent( GncSxEditorDialog *sxed )
{
/* Do checks on validity and such, interrupting the user if
* things aren't right.
*
* Features...
* X support formulas [?!]
* X balancing the SX if contain numeric-only formula data.
* X agreement with create-automagically/notification controls
* X the 'will ever be valid' check should take num-occur vals into
* account.
* X SX name is unique
* X SX has a name
* X "weekly" FS has some days set.
* X "once" with reasonable start/end dates.
* X This doesn't work at the time the 'weekly' one was fixed with
* user-confirmation, below; the once SX is always valid.
* [X more generically, creating a "not scheduled" SX is probably not
* right... ]
*/
gboolean multi_commodity = FALSE;
gint ttVarCount = 0, splitCount = 0;
static const int NUM_ITERS_WITH_VARS = 5;
static const int NUM_ITERS_NO_VARS = 1;
int numIters = NUM_ITERS_NO_VARS, i;
gboolean unbalanceable = FALSE;
gpointer unusedKey, unusedValue;
GHashTable *vars = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
(GDestroyNotify)gnc_sx_variable_free);
GHashTable *txns = g_hash_table_new_full (g_direct_hash, g_direct_equal,
NULL, g_free);
CheckTxnSplitData sd = {sxed, txns, vars, NULL, FALSE, FALSE};
/**
* Plan:
* . Do a first pass to get the variables.
* . Set each variable to random values.
* . see if we balance after that
* . true: all good
* . false: indicate to user, allow decision.
*/
/* FIXME: This is probably superfluous. */
gnc_split_register_save (
gnc_ledger_display_get_split_register(sxed->ledger), FALSE);
/* numeric-formulas-get-balanced determination */
gnc_sx_get_variables (sxed->sx, vars);
ttVarCount = g_hash_table_size (vars);
if (ttVarCount != 0)
{
/* balance with random variable bindings some number of times in an
* attempt to ferret out un-balanceable transactions.
*/
numIters = NUM_ITERS_WITH_VARS;
}
for ( i = 0; i < numIters && !unbalanceable; i++ )
{
GList *splitList = xaccSchedXactionGetSplits (sxed->sx);
Account *tmpl_acct = gnc_sx_get_template_transaction_account (sxed->sx);
gnc_sx_randomize_variables(vars);
g_hash_table_foreach( txns, set_sums_to_zero, NULL );
splitCount += g_list_length( splitList );
xaccAccountForEachTransaction(tmpl_acct, check_transaction_splits, &sd);
if (sd.err)
return FALSE;
g_hash_table_foreach (txns, check_credit_debit_balance, &unbalanceable);
}
/* Subtract out pre-defined vars */
if (g_hash_table_lookup_extended(vars, "i", &unusedKey, &unusedValue))
ttVarCount -= 1;
g_hash_table_destroy(vars);
g_hash_table_destroy(txns);
if (unbalanceable)
{
const char *msg =
_("The Scheduled Transaction Editor cannot automatically "
"balance this transaction. Should it still be entered?");
if (!gnc_verify_dialog (sxed->dialog, FALSE, "%s", msg))
return FALSE;
}
if (!gnc_sxed_check_names (sxed))
return FALSE;
if (!gnc_sxed_check_autocreate (sxed, ttVarCount,
splitCount, sd.multi_commodity))
return FALSE;
if (!gnc_sxed_check_endpoint (sxed))
return FALSE;
return TRUE;
}
/******************************************************************************
* Saves the contents of the SX. This assumes that gnc_sxed_check_consistent
* has returned true.
*****************************************************************************/
static void
gnc_sxed_save_sx( GncSxEditorDialog *sxed )
{
gnc_sx_begin_edit( sxed->sx );
/* name */
{
char *name;
name = gtk_editable_get_chars( sxed->nameEntry, 0, -1 );
xaccSchedXactionSetName( sxed->sx, name );
g_free( name );
}
/* date */
{
GDate gdate;
if ( gtk_toggle_button_get_active(sxed->optEndDate) )
{
/* get the end date data */
gnc_gdate_set_time64( &gdate,
gnc_date_edit_get_date(
sxed->endDateEntry ) );
xaccSchedXactionSetEndDate( sxed->sx, &gdate );
/* set the num occurrences data */
xaccSchedXactionSetNumOccur( sxed->sx, 0 );
}
else if ( gtk_toggle_button_get_active(sxed->optEndCount) )
{
gint num;
/* get the occurrences data */
num =
gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(sxed->endCountSpin) );
xaccSchedXactionSetNumOccur( sxed->sx, num );
num =
gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(sxed->endRemainSpin) );
xaccSchedXactionSetRemOccur( sxed->sx, num );
g_date_clear( &gdate, 1 );
xaccSchedXactionSetEndDate( sxed->sx, &gdate );
}
else if ( gtk_toggle_button_get_active( sxed->optEndNone ) )
{
xaccSchedXactionSetNumOccur( sxed->sx, 0 );
g_date_clear( &gdate, 1 );
xaccSchedXactionSetEndDate( sxed->sx, &gdate );
}
else
{
g_critical("no valid end specified\n");
}
}
/* Enabled states */
{
gboolean enabledState;
enabledState = gtk_toggle_button_get_active( sxed->enabledOpt );
xaccSchedXactionSetEnabled( sxed->sx, enabledState );
}
/* Auto-create/notification states */
{
gboolean autocreateState, notifyState;
autocreateState = gtk_toggle_button_get_active( sxed->autocreateOpt );
notifyState = gtk_toggle_button_get_active( sxed->notifyOpt );
/* "Notify" only makes sense if AutoCreate is actived;
* enforce that here. */
xaccSchedXactionSetAutoCreate( sxed->sx,
autocreateState,
(autocreateState & notifyState) );
}
/* days in advance */
{
int daysInAdvance;
daysInAdvance = 0;
if ( gtk_toggle_button_get_active( sxed->advanceOpt ) )
{
daysInAdvance =
gtk_spin_button_get_value_as_int( sxed->advanceSpin );
}
xaccSchedXactionSetAdvanceCreation( sxed->sx, daysInAdvance );
daysInAdvance = 0;
if ( gtk_toggle_button_get_active( sxed->remindOpt ) )
{
daysInAdvance =
gtk_spin_button_get_value_as_int( sxed->remindSpin );
}
xaccSchedXactionSetAdvanceReminder( sxed->sx, daysInAdvance );
}
/* start date and freq spec */
{
GDate gdate;
GList *schedule = NULL;