/
window-reconcile2.c
2192 lines (1725 loc) · 74.2 KB
/
window-reconcile2.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
/********************************************************************\
* window-reconcile2.c -- the reconcile window *
* Copyright (C) 1997 Robin D. Clark *
* Copyright (C) 1998-2000 Linas Vepstas *
* Copyright (C) 2002 Christian Stimming *
* Copyright (C) 2006 David Hampton *
* *
* 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 *
* *
* Author: Rob Clark *
* Internet: rclark@cs.hmc.edu *
* Address: 609 8th Street *
* Huntington Beach, CA 92648-4632 *
\********************************************************************/
#include <config.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#ifdef __G_IR_SCANNER__
#undef __G_IR_SCANNER__
#endif
#include <gdk/gdkkeysyms.h>
#include "Scrub.h"
#include "Scrub3.h"
#include "dialog-account.h"
#include "dialog-transfer.h"
#include "dialog-utils.h"
#include "gnc-amount-edit.h"
#include "gnc-component-manager.h"
#include "gnc-date.h"
#include "gnc-date-edit.h"
#include "gnc-event.h"
#include "gnc-filepath-utils.h"
#include "gnc-gnome-utils.h"
#include "gnc-plugin-page-register2.h"
#include "gnc-prefs.h"
#include "gnc-ui.h"
#include "gnc-ui-balances.h"
#include "gnc-window.h"
#include "reconcile-view.h"
#include "window-reconcile2.h"
#include "Account.h"
#define WINDOW_RECONCILE_CM_CLASS "window-reconcile"
#define GNC_PREF_AUTO_CC_PAYMENT "auto-cc-payment"
#define GNC_PREF_ALWAYS_REC_TO_TODAY "always-reconcile-to-today"
/** STRUCTS *********************************************************/
struct _RecnWindow2
{
GncGUID account; /* The account that we are reconciling */
gnc_numeric new_ending; /* The new ending balance */
time64 statement_date; /* The statement date */
gint component_id; /* id of component */
GtkWidget *window; /* The reconcile window */
GtkUIManager *ui_merge;
GtkActionGroup *action_group;
GtkWidget *starting; /* The starting balance */
GtkWidget *ending; /* The ending balance */
GtkWidget *recn_date; /* The statement date */
GtkWidget *reconciled; /* The reconciled balance */
GtkWidget *difference; /* Text field, amount left to reconcile */
GtkWidget *total_debit; /* Text field, total debit reconciled */
GtkWidget *total_credit; /* Text field, total credit reconciled */
GtkWidget *debit; /* Debit matrix show unreconciled debit */
GtkWidget *credit; /* Credit matrix, shows credits... */
GtkWidget *debit_frame; /* Frame around debit matrix */
GtkWidget *credit_frame; /* Frame around credit matrix */
gboolean delete_refresh; /* do a refresh upon a window deletion */
};
/* This structure doesn't contain everything involved in the
* startRecnWindow, just pointers that have to be passed in to
* callbacks that need more than one piece of data to operate on.
* This is also used by the interest transfer dialog code.
*/
typedef struct _startRecnWindowData
{
Account *account; /* the account being reconciled */
GNCAccountType account_type; /* the type of the account */
GtkWidget *startRecnWindow; /* the startRecnWindow dialog */
GtkWidget *xfer_button; /* the dialog's interest transfer button */
GtkWidget *date_value; /* the dialog's ending date field */
GNCAmountEdit *end_value; /* the dialog's ending balance amount edit */
gnc_numeric original_value; /* the dialog's original ending balance */
gboolean user_set_value; /* the user changed the ending value */
XferDialog *xferData; /* the interest xfer dialog (if it exists) */
gboolean include_children;
time64 date; /* the interest xfer reconcile date */
} startRecnWindowData;
/** PROTOTYPES ******************************************************/
static gnc_numeric recnRecalculateBalance (RecnWindow2 *recnData);
static void recn_destroy_cb (GtkWidget *w, gpointer data);
static void recn_cancel (RecnWindow2 *recnData);
static gboolean recn_delete_cb (GtkWidget *widget, GdkEvent *event, gpointer data);
static gboolean recn_key_press_cb (GtkWidget *widget, GdkEventKey *event, gpointer data);
static void recnFinishCB (GtkAction *action, RecnWindow2 *recnData);
static void recnPostponeCB (GtkAction *action, gpointer data);
static void recnCancelCB (GtkAction *action, gpointer data);
void gnc_start_recn2_children_changed (GtkWidget *widget, startRecnWindowData *data);
void gnc_start_recn2_interest_clicked_cb (GtkButton *button, startRecnWindowData *data);
static void gnc_reconcile_window_set_sensitivity (RecnWindow2 *recnData);
static char * gnc_recn_make_window_name (Account *account);
static void gnc_recn_set_window_name (RecnWindow2 *recnData);
static gboolean find_by_account (gpointer find_data, gpointer user_data);
/** GLOBALS ************************************************************/
/* This static indicates the debugging module that this .o belongs to. */
G_GNUC_UNUSED static QofLogModule log_module = GNC_MOD_GUI;
static time64 gnc_reconcile_last_statement_date = 0;
/** IMPLEMENTATIONS *************************************************/
/** An array of all of the actions provided by the main window code.
* This includes some placeholder actions for the menus that are
* visible in the menu bar but have no action associated with
* them. */
static GtkActionEntry recnWindow2_actions [];
/** The number of actions provided by the main window. */
static guint recnWindow2_n_actions;
/********************************************************************\
* recnRefresh *
* refreshes the transactions in the reconcile window *
* *
* Args: account - the account of the reconcile window to refresh *
* Return: none *
\********************************************************************/
static void
recnRefresh (RecnWindow2 *recnData)
{
if (recnData == NULL)
return;
gnc_reconcile_view_refresh (GNC_RECONCILE_VIEW (recnData->debit));
gnc_reconcile_view_refresh (GNC_RECONCILE_VIEW (recnData->credit));
gnc_reconcile_window_set_sensitivity (recnData);
gnc_recn_set_window_name (recnData);
recnRecalculateBalance (recnData);
gtk_widget_queue_resize (recnData->window);
}
static Account *
recn_get_account (RecnWindow2 *recnData)
{
if (!recnData)
return NULL;
return xaccAccountLookup (&recnData->account, gnc_get_current_book ());
}
/********************************************************************\
* recnRecalculateBalance *
* refreshes the balances in the reconcile window *
* *
* Args: recnData -- the reconcile window to refresh *
* Return: the difference between the nominal ending balance *
* and the 'effective' ending balance. *
\********************************************************************/
static gnc_numeric
recnRecalculateBalance (RecnWindow2 *recnData)
{
Account *account;
const char *amount;
gnc_numeric debit;
gnc_numeric credit;
gnc_numeric starting;
gnc_numeric ending;
gnc_numeric reconciled;
gnc_numeric diff;
GNCPrintAmountInfo print_info;
gboolean reverse_balance, include_children;
GtkAction *action;
account = recn_get_account (recnData);
if (!account)
return gnc_numeric_zero ();
reverse_balance = gnc_reverse_balance (account);
/* update the starting balance */
include_children = xaccAccountGetReconcileChildrenStatus (account);
starting = gnc_ui_account_get_reconciled_balance (account, include_children);
print_info = gnc_account_print_info (account, TRUE);
/*
* Do not reverse the balance here. It messes up the math in the
* reconciliation window. Also, the balance should show up as a
* positive number in the reconciliation window to match the positive
* number that shows in the register window.
*/
amount = xaccPrintAmount (starting, print_info);
gnc_set_label_color (recnData->starting, starting);
gtk_label_set_text (GTK_LABEL (recnData->starting), amount);
if (reverse_balance)
starting = gnc_numeric_neg (starting);
/* update the statement date */
amount = qof_print_date (recnData->statement_date);
gtk_label_set_text (GTK_LABEL (recnData->recn_date), amount);
/* update the ending balance */
ending = recnData->new_ending;
if (reverse_balance)
ending = gnc_numeric_neg (ending);
amount = xaccPrintAmount (ending, print_info);
gnc_set_label_color (recnData->ending, ending);
gtk_label_set_text (GTK_LABEL (recnData->ending), amount);
if (reverse_balance)
ending = gnc_numeric_neg (ending);
debit = gnc_reconcile_view_reconciled_balance
(GNC_RECONCILE_VIEW (recnData->debit));
credit = gnc_reconcile_view_reconciled_balance
(GNC_RECONCILE_VIEW (recnData->credit));
/* Update the total debit and credit fields */
amount = xaccPrintAmount (debit, print_info);
gtk_label_set_text (GTK_LABEL (recnData->total_debit), amount);
amount = xaccPrintAmount (credit, print_info);
gtk_label_set_text (GTK_LABEL (recnData->total_credit), amount);
/* update the reconciled balance */
reconciled = gnc_numeric_add_fixed (starting,
gnc_numeric_sub_fixed (debit, credit));
if (reverse_balance)
reconciled = gnc_numeric_neg (reconciled);
amount = xaccPrintAmount (reconciled, print_info);
gnc_set_label_color (recnData->reconciled, reconciled);
gtk_label_set_text (GTK_LABEL (recnData->reconciled), amount);
if (reverse_balance)
reconciled = gnc_numeric_neg (reconciled);
/* update the difference */
diff = gnc_numeric_sub_fixed (ending, reconciled);
if (reverse_balance)
diff = gnc_numeric_neg (diff);
amount = xaccPrintAmount (diff, print_info);
gnc_set_label_color (recnData->difference, diff);
gtk_label_set_text (GTK_LABEL (recnData->difference), amount);
if (reverse_balance)
diff = gnc_numeric_neg (diff);
action = gtk_action_group_get_action (recnData->action_group,
"RecnFinishAction");
gtk_action_set_sensitive (action, gnc_numeric_zero_p (diff));
action = gtk_action_group_get_action (recnData->action_group,
"TransBalanceAction");
gtk_action_set_sensitive (action, !gnc_numeric_zero_p (diff));
return diff;
}
static gboolean
gnc_start_recn2_update_cb (GtkWidget *widget, GdkEventFocus *event,
startRecnWindowData *data)
{
gnc_numeric value;
gnc_amount_edit_evaluate (GNC_AMOUNT_EDIT (data->end_value), NULL);
value = gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT (data->end_value));
data->user_set_value = !gnc_numeric_equal (value, data->original_value);
return FALSE;
}
/* If the user changed the date edit widget, update the
* ending balance to reflect the ending balance of the account
* on the date that the date edit was changed to.
*/
static void
gnc_start_recn2_date_changed (GtkWidget *widget, startRecnWindowData *data)
{
GNCDateEdit *gde = GNC_DATE_EDIT (widget);
gnc_numeric new_balance;
time64 new_date;
if (data->user_set_value)
return;
new_date = gnc_date_edit_get_date_end (gde);
/* get the balance for the account as of the new date */
new_balance = gnc_ui_account_get_balance_as_of_date (data->account, new_date,
data->include_children);
/* update the amount edit with the amount */
gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT (data->end_value),
new_balance);
}
void
gnc_start_recn2_children_changed (GtkWidget *widget, startRecnWindowData *data)
{
data->include_children =
gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
/* Force an update of the ending balance */
gnc_start_recn2_date_changed (data->date_value, data);
}
/********************************************************************\
* recnInterestXferWindow *
* opens up a window to prompt the user to enter an interest *
* charge or payment for an account prior to reconciling it. *
* Only to be called for some types of accounts, as defined *
* in the macros at the top of this file. *
* *
* NOTE: This function does not return until the user presses "Ok" *
* or "Cancel", which means that the transaction must be *
* resolved before the startRecnWindow will work. *
* *
* Args: data - jumbo structure containing info *
* about the start of the reconcile *
* process needed by this function. *
* Returns: none. *
\********************************************************************/
/* helper function */
static char *
gnc_recn_make_interest_window_name (Account *account, char *text)
{
char *fullname;
char *title;
fullname = gnc_account_get_full_name (account);
title = g_strconcat (fullname, " - ", text && *text ? _(text) : "", NULL);
g_free (fullname);
return title;
}
/* user clicked button in the interest xfer dialog entitled
* "No Auto Interest Payments for this Account".
*/
static void
gnc_recn_interest_xfer_no_auto_clicked_cb (GtkButton *button,
startRecnWindowData *data)
{
/* shut down the interest xfer dialog */
gnc_xfer_dialog_close (data->xferData);
/* make the button clickable again */
if (data->xfer_button)
gtk_widget_set_sensitive (GTK_WIDGET (data->xfer_button), TRUE);
}
static void
recnInterestXferWindow (startRecnWindowData *data)
{
gchar *title;
if (!account_type_has_auto_interest_xfer (data->account_type)) return;
/* get a normal transfer dialog... */
data->xferData = gnc_xfer_dialog (GTK_WIDGET(data->startRecnWindow),
data->account);
/* ...and start changing things: */
/* change title */
if ( account_type_has_auto_interest_payment (data->account_type))
title = gnc_recn_make_interest_window_name (data->account,
_("Interest Payment"));
else
title = gnc_recn_make_interest_window_name (data->account,
_("Interest Charge"));
gnc_xfer_dialog_set_title (data->xferData, title);
g_free (title);
/* change frame labels */
gnc_xfer_dialog_set_information_label (data->xferData,
_("Payment Information"));
/* Interest accrued is a transaction from an income account
* to a bank account. Interest charged is a transaction from
* a credit account to an expense account. The user isn't allowed
* to change the account (bank or credit) being reconciled.
*/
if (account_type_has_auto_interest_payment (data->account_type))
{
gnc_xfer_dialog_set_from_account_label (data->xferData,
_("Payment From"));
gnc_xfer_dialog_set_from_show_button_active( data->xferData, TRUE);
// XXX: Set "from" account from previous interest payment.
gnc_xfer_dialog_set_to_account_label (data->xferData,
_("Reconcile Account"));
gnc_xfer_dialog_select_to_account (data->xferData, data->account);
gnc_xfer_dialog_lock_to_account_tree (data->xferData );
/* Quickfill based on the reconcile account, which is the "To" acct. */
gnc_xfer_dialog_quickfill_to_account (data->xferData, TRUE);
}
else /* interest charged to account rather than paid to it */
{
gnc_xfer_dialog_set_from_account_label (data->xferData,
_("Reconcile Account"));
gnc_xfer_dialog_select_from_account (data->xferData, data->account);
gnc_xfer_dialog_lock_from_account_tree (data->xferData);
gnc_xfer_dialog_set_to_account_label (data->xferData,
_("Payment To"));
gnc_xfer_dialog_set_to_show_button_active (data->xferData, TRUE);
// XXX: Set "to" account from previous interest payment.
/* Quickfill based on the reconcile account, which is the "From" acct. */
gnc_xfer_dialog_quickfill_to_account (data->xferData, FALSE);
}
/* add a button to disable auto interest payments for this account */
gnc_xfer_dialog_add_user_specified_button (data->xferData,
( account_type_has_auto_interest_payment (data->account_type) ?
_("No Auto Interest Payments for this Account")
: _("No Auto Interest Charges for this Account")),
G_CALLBACK (gnc_recn_interest_xfer_no_auto_clicked_cb),
(gpointer) data );
/* no currency frame */
gnc_xfer_dialog_toggle_currency_table (data->xferData, FALSE);
/* set the reconcile date for the transaction date */
gnc_xfer_dialog_set_date (data->xferData, data->date);
/* Now run the transfer dialog. This blocks until done.
* If the user hit Cancel, make the button clickable so that
* the user can retry if they want. We don't make the button
* clickable if they successfully entered a transaction, since
* the fact that the button was clickable again might make
* the user think that the transaction didn't actually go through.
*/
if (!gnc_xfer_dialog_run_until_done (data->xferData))
if (data->xfer_button)
gtk_widget_set_sensitive (GTK_WIDGET (data->xfer_button), TRUE);
/* done with the XferDialog */
data->xferData = NULL;
}
/* Set up for the interest xfer window, run the window, and update
* the startRecnWindow if the interest xfer changed anything that matters.
*/
static void
gnc_reconcile_interest_xfer_run (startRecnWindowData *data)
{
GtkWidget *entry = gnc_amount_edit_gtk_entry (
GNC_AMOUNT_EDIT(data->end_value));
gnc_numeric before = gnc_amount_edit_get_amount (
GNC_AMOUNT_EDIT(data->end_value));
gnc_numeric after;
recnInterestXferWindow (data);
/* recompute the ending balance */
after = xaccAccountGetBalanceAsOfDate (data->account, data->date);
/* update the ending balance in the startRecnWindow if it has changed. */
if ( gnc_numeric_compare (before, after))
{
if (gnc_reverse_balance (data->account))
after = gnc_numeric_neg (after);
gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT (data->end_value), after);
gtk_widget_grab_focus (GTK_WIDGET (entry));
gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
data->original_value = after;
data->user_set_value = FALSE;
}
}
void
gnc_start_recn2_interest_clicked_cb (GtkButton *button, startRecnWindowData *data)
{
/* make the button unclickable since we're popping up the window */
if (data->xfer_button)
gtk_widget_set_sensitive (GTK_WIDGET (data->xfer_button), FALSE);
/* run the account window */
gnc_reconcile_interest_xfer_run (data);
}
static void
gnc_save_reconcile_interval (Account *account, time64 statement_date)
{
time64 prev_statement_date;
int days = 0, months = 0;
double seconds;
if (!xaccAccountGetReconcileLastDate (account, &prev_statement_date))
return;
/*
* Compute the number of days difference.
*/
seconds = gnc_difftime (statement_date, prev_statement_date);
days = (int)(seconds / 60 / 60 / 24);
/*
* See if we need to remember days(weeks) or months. The only trick
* value is 28 days which could be wither 4 weeks or 1 month.
*/
if (days == 28)
{
int prev_days = 0, prev_months = 1;
/* What was it last time? */
xaccAccountGetReconcileLastInterval (account, &prev_months, &prev_days);
if (prev_months == 1)
{
months = 1;
days = 0;
}
}
else if (days > 28)
{
struct tm current, prev;
gnc_localtime_r (&statement_date, ¤t);
gnc_localtime_r (&prev_statement_date, &prev);
months = ((12 * current.tm_year + current.tm_mon) -
(12 * prev.tm_year + prev.tm_mon));
days = 0;
}
/*
* Remember for next time unless it is negative.
*/
if (months >= 0 && days >= 0)
xaccAccountSetReconcileLastInterval (account, months, days);
}
/********************************************************************\
* startRecnWindow *
* opens up the window to prompt the user to enter the ending *
* balance from bank statement *
* *
* NOTE: This function does not return until the user presses "Ok" *
* or "Cancel" *
* *
* Args: parent - the parent of this window *
* account - the account to reconcile *
* new_ending - returns the amount for ending balance *
* statement_date - returns date of the statement :) *
* Return: True, if the user presses "Ok", else False *
\********************************************************************/
static gboolean
startRecnWindow (GtkWidget *parent, Account *account,
gnc_numeric *new_ending, time64 *statement_date,
gboolean enable_subaccount)
{
GtkWidget *dialog, *end_value, *date_value, *include_children_button;
GtkBuilder *builder;
startRecnWindowData data = { NULL };
gboolean auto_interest_xfer_option;
GNCPrintAmountInfo print_info;
gnc_numeric ending;
char *title;
int result;
/* Initialize the data structure that will be used for several callbacks
* throughout this file with the relevant info. Some initialization is
* done below as well. Note that local storage should be OK for this,
* since any callbacks using it will only work while the startRecnWindow
* is running.
*/
data.account = account;
data.account_type = xaccAccountGetType (account);
data.date = *statement_date;
/* whether to have an automatic interest xfer dialog or not */
auto_interest_xfer_option = xaccAccountGetAutoInterest (account);
data.include_children = xaccAccountGetReconcileChildrenStatus (account);
ending = gnc_ui_account_get_reconciled_balance (account,
data.include_children);
print_info = gnc_account_print_info (account, TRUE);
/*
* Do not reverse the balance here. It messes up the math in the
* reconciliation window. Also, the balance should show up as a
* positive number in the reconciliation window to match the positive
* number that shows in the register window.
*/
/* Create the dialog box */
builder = gtk_builder_new();
gnc_builder_add_from_file (builder, "window-reconcile.glade", "reconcile_start_dialog");
dialog = GTK_WIDGET(gtk_builder_get_object (builder, "reconcile_start_dialog"));
// Set the name for this dialog so it can be easily manipulated with css
gtk_widget_set_name (GTK_WIDGET(dialog), "gnc-id-reconcile2-start");
title = gnc_recn_make_window_name (account);
gtk_window_set_title (GTK_WINDOW (dialog), title);
g_free (title);
data.startRecnWindow = GTK_WIDGET (dialog);
if (parent != NULL)
gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
{
GtkWidget *start_value, *box;
GtkWidget *entry, *label;
GtkWidget *interest = NULL;
start_value = GTK_WIDGET (gtk_builder_get_object (builder, "start_value"));
gtk_label_set_text (GTK_LABEL(start_value), xaccPrintAmount (ending, print_info));
include_children_button = GTK_WIDGET (gtk_builder_get_object (builder, "subaccount_check"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (include_children_button),
data.include_children);
gtk_widget_set_sensitive (include_children_button, enable_subaccount);
date_value = gnc_date_edit_new (*statement_date, FALSE, FALSE);
data.date_value = date_value;
box = GTK_WIDGET (gtk_builder_get_object (builder, "date_value_box"));
gtk_box_pack_start (GTK_BOX (box), date_value, TRUE, TRUE, 0);
label = GTK_WIDGET (gtk_builder_get_object (builder, "date_label"));
gnc_date_make_mnemonic_target (GNC_DATE_EDIT (date_value), label);
end_value = gnc_amount_edit_new ();
data.end_value = GNC_AMOUNT_EDIT (end_value);
data.original_value = *new_ending;
data.user_set_value = FALSE;
box = GTK_WIDGET (gtk_builder_get_object (builder, "ending_value_box"));
gtk_box_pack_start (GTK_BOX (box), end_value, TRUE, TRUE, 0);
label = GTK_WIDGET (gtk_builder_get_object (builder, "end_label"));
gtk_label_set_mnemonic_widget (GTK_LABEL (label), end_value);
gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, &data);
gnc_date_activates_default (GNC_DATE_EDIT (date_value), TRUE);
/* need to get a callback on date changes to update the recn balance */
g_signal_connect ( G_OBJECT (date_value), "date_changed",
G_CALLBACK (gnc_start_recn2_date_changed), (gpointer) &data );
print_info.use_symbol = 0;
gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT (end_value), print_info);
gnc_amount_edit_set_fraction (GNC_AMOUNT_EDIT (end_value),
xaccAccountGetCommoditySCU (account));
gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT (end_value), *new_ending);
entry = gnc_amount_edit_gtk_entry (GNC_AMOUNT_EDIT (end_value));
gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
g_signal_connect (G_OBJECT (entry), "focus-out-event",
G_CALLBACK (gnc_start_recn2_update_cb), (gpointer) &data);
gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
/* if it's possible to enter an interest payment or charge for this
* account, add a button so that the user can pop up the appropriate
* dialog if it isn't automatically popping up.
*/
interest = GTK_WIDGET(gtk_builder_get_object (builder, "interest_button"));
if ( account_type_has_auto_interest_payment( data.account_type ) )
gtk_button_set_label (GTK_BUTTON (interest), _("Enter _Interest Payment...") );
else if ( account_type_has_auto_interest_charge( data.account_type ) )
gtk_button_set_label (GTK_BUTTON (interest), _("Enter _Interest Charge...") );
else
{
gtk_widget_destroy (interest);
interest = NULL;
}
if ( interest )
{
data.xfer_button = interest;
if ( auto_interest_xfer_option )
gtk_widget_set_sensitive (GTK_WIDGET (interest), FALSE);
}
gtk_widget_show_all (dialog);
gtk_widget_grab_focus(gnc_amount_edit_gtk_entry
(GNC_AMOUNT_EDIT (end_value)));
}
/* Allow the user to enter an interest payment
* or charge prior to reconciling */
if (account_type_has_auto_interest_xfer (data.account_type)
&& auto_interest_xfer_option)
{
gnc_reconcile_interest_xfer_run (&data);
}
result = gtk_dialog_run (GTK_DIALOG (dialog));
if (result == GTK_RESPONSE_OK)
{
*new_ending = gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT (end_value));
*statement_date = gnc_date_edit_get_date_end (GNC_DATE_EDIT (date_value));
if (gnc_reverse_balance (account))
*new_ending = gnc_numeric_neg (*new_ending);
xaccAccountSetReconcileChildrenStatus (account, data.include_children);
gnc_save_reconcile_interval (account, *statement_date);
}
gtk_widget_destroy (dialog);
g_object_unref (G_OBJECT (builder));
return (result == GTK_RESPONSE_OK);
}
static void
gnc_reconcile_window_set_sensitivity (RecnWindow2 *recnData)
{
gboolean sensitive = FALSE;
GNCReconcileView *view;
GtkAction *action;
view = GNC_RECONCILE_VIEW (recnData->debit);
if (gnc_reconcile_view_num_selected (view) == 1)
sensitive = TRUE;
view = GNC_RECONCILE_VIEW (recnData->credit);
if (gnc_reconcile_view_num_selected (view) == 1)
sensitive = TRUE;
action = gtk_action_group_get_action (recnData->action_group,
"TransEditAction");
gtk_action_set_sensitive (action, sensitive);
action = gtk_action_group_get_action (recnData->action_group,
"TransDeleteAction");
gtk_action_set_sensitive (action, sensitive);
sensitive = FALSE;
view = GNC_RECONCILE_VIEW (recnData->debit);
if (gnc_reconcile_view_num_selected (view) > 0)
sensitive = TRUE;
view = GNC_RECONCILE_VIEW (recnData->credit);
if (gnc_reconcile_view_num_selected (view) > 0)
sensitive = TRUE;
action = gtk_action_group_get_action (recnData->action_group,
"TransRecAction");
gtk_action_set_sensitive (action, sensitive);
action = gtk_action_group_get_action (recnData->action_group,
"TransUnRecAction");
gtk_action_set_sensitive (action, sensitive);
}
static void
gnc_reconcile_window_toggled_cb (GNCReconcileView *view, Split *split,
gpointer data)
{
RecnWindow2 *recnData = data;
gnc_reconcile_window_set_sensitivity (recnData);
recnRecalculateBalance (recnData);
}
static void
gnc_reconcile_window_row_cb (GNCReconcileView *view, gpointer item,
gpointer data)
{
RecnWindow2 *recnData = data;
gnc_reconcile_window_set_sensitivity (recnData);
}
/** Popup a contextual menu. This function ends up being called when
* the user right-clicks in the context of a window, or uses the
* keyboard context-menu request key combination (Shift-F10 by
* default).
*
* @param recnData This is a data structure describing the
* Reconciliation Window.
*
* @param event The event parameter passed to the "button-press"
* callback. May be null if there was no event (aka keyboard
* request).
*/
static void
do_popup_menu (RecnWindow2 *recnData, GdkEventButton *event)
{
GtkWidget *menu = gtk_ui_manager_get_widget (recnData->ui_merge, "/MainPopup");
if (!menu)
return;
gtk_menu_popup_at_pointer (GTK_MENU(menu), (GdkEvent *) event);
}
/** Callback function invoked when the user requests that Gnucash
* popup the contextual menu via the keyboard context-menu request
* key combination (Shift-F10 by default).
*
* @param recnData This is a data structure describing the
* Reconciliation Window.
*
* @param widget Whatever widget had focus when the user issued the
* keyboard context-menu request.
*
* @return Always returns TRUE to indicate that the menu request was
* handled.
*/
static gboolean
gnc_reconcile_window_popup_menu_cb (GtkWidget *widget,
RecnWindow2 *recnData)
{
do_popup_menu (recnData, NULL);
return TRUE;
}
/* Callback function invoked when the user clicks in the content of
* any Gnucash window. If this was a "right-click" then Gnucash will
* popup the contextual menu.
*/
static gboolean
gnc_reconcile_window_button_press_cb (GtkWidget *widget,
GdkEventButton *event,
RecnWindow2 *recnData)
{
GNCQueryView *qview = GNC_QUERY_VIEW (widget);
GtkTreeSelection *selection;
GtkTreePath *path;
if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
{
/* Get tree path for row that was clicked */
gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (qview),
(gint) event->x,
(gint) event->y,
&path, NULL, NULL, NULL);
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (qview));
gtk_tree_selection_select_path (selection, path);
gtk_tree_path_free (path);
do_popup_menu (recnData, event);
return TRUE;
}
return FALSE;
}
static GNCSplitReg2 *
gnc_reconcile_window_open_register (RecnWindow2 *recnData)
{
Account *account = recn_get_account (recnData);
GncPluginPage *page;
GNCSplitReg2 *gsr;
gboolean include_children;
if (!account)
return(NULL);
include_children = xaccAccountGetReconcileChildrenStatus (account);
page = gnc_plugin_page_register2_new (account, include_children);
gnc_main_window_open_page (NULL, page);
gsr = gnc_plugin_page_register2_get_gsr (page);
gnc_split_reg2_raise (gsr);
return gsr;
}
static void
gnc_reconcile_window_double_click_cb (GNCReconcileView *view, Split *split,
gpointer data)
{
RecnWindow2 *recnData = data;
GNCSplitReg2 *gsr;
/* This should never be true, but be paranoid */
if (split == NULL)
return;
gsr = gnc_reconcile_window_open_register (recnData);
if (gsr == NULL)
return;
gnc_split_reg2_jump_to_split (gsr, split);
}
static void
gnc_reconcile_window_focus_cb (GtkWidget *widget, GdkEventFocus *event,
gpointer data)
{
RecnWindow2 *recnData = data;
GNCReconcileView *this_view, *other_view;
GNCReconcileView *debit, *credit;
this_view = GNC_RECONCILE_VIEW (widget);
debit = GNC_RECONCILE_VIEW (recnData->debit);
credit = GNC_RECONCILE_VIEW (recnData->credit);
other_view = GNC_RECONCILE_VIEW (this_view == debit ? credit : debit);
/* clear the *other* list so we always have no more than one selection */
gnc_reconcile_view_unselect_all (other_view);
}
static gboolean
gnc_reconcile_key_press_cb (GtkWidget *widget, GdkEventKey *event,
gpointer data)
{
RecnWindow2 *recnData = data;
GtkWidget *this_view, *other_view;
GtkWidget *debit, *credit;
switch (event->keyval)
{
case GDK_KEY_Tab:
case GDK_KEY_ISO_Left_Tab:
break;
default:
return FALSE;
}
g_signal_stop_emission_by_name (widget, "key_press_event");
this_view = widget;
debit = recnData->debit;
credit = recnData->credit;
other_view = (this_view == debit ? credit : debit);
gtk_widget_grab_focus (other_view);
return TRUE;
}