-
Notifications
You must be signed in to change notification settings - Fork 761
/
assistant-acct-period.c
660 lines (536 loc) · 23.3 KB
/
assistant-acct-period.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
/********************************************************************\
* assistant-acct-period.c - accounting period assistant for GnuCash*
* Copyright (C) 2001 Gnumatic, Inc. *
* Copyright (C) 2001 Dave Peticolas <dave@krondo.com> *
* Copyright (C) 2003 Linas Vepstas <linas@linas.org> *
* Copyright (C) 2011 Robert Fewell *
* *
* 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 *
\********************************************************************/
/*************************************************************************\
* This is still a work in progress so may damage your data, to enable *
* for testing do the following :- *
* Add a define entry to gnc-plugin-basic-commands.c as below *
* #define CLOSE_BOOKS_ACTUALLY_WORKS 1 *
* *
* Add the following to gnc-plugin-basic-commands-ui.xml on line 43 *
* <menuitem name="ActionsCloseBooks" action="ActionsCloseBooksAction"/> *
\*************************************************************************/
#include <config.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "Recurrence.h"
#include "Query.h"
#include "Scrub.h"
#include "Transaction.h"
#include "dialog-utils.h"
#include "assistant-acct-period.h"
#include "gnc-component-manager.h"
#include "qof.h"
#include "gnc-date.h"
#include "gnc-file.h"
#include "gnc-frequency.h"
#include "gnc-gui-query.h"
#include "gnc-ui-util.h"
#include "misc-gnome-utils.h"
#include "gnc-session.h"
#define ASSISTANT_ACCT_PERIOD_CM_CLASS "assistant-acct-period"
static QofLogModule log_module = GNC_MOD_ASSISTANT;
/** structures *********************************************************/
typedef struct
{
GtkWidget * window;
GtkWidget * assistant;
GncFrequency *period_menu;
GtkWidget * period_remarks;
GtkWidget * close_results;
GtkWidget * book_details;
GtkWidget * book_title;
GtkTextView * book_notes;
GtkWidget * apply_label;
GtkWidget * summary;
time64 earliest;
char * earliest_str;
GDate closing_date;
GDate prev_closing_date;
GList *period;
int close_status;
} AcctPeriodInfo;
/* =============================================================== */
void ap_assistant_prepare (GtkAssistant *assistant, GtkWidget *page,
gpointer user_data);
void ap_assistant_menu_prepare (GtkAssistant *assistant, gpointer user_data);
void ap_assistant_book_prepare (GtkAssistant *assistant, gpointer user_data);
void ap_assistant_apply_prepare (GtkAssistant *assistant, gpointer user_data);
void ap_assistant_summary_prepare (GtkAssistant *assistant, gpointer user_data);
gboolean ap_validate_menu (GtkAssistant *assistant, gpointer user_data);
void ap_assistant_finish (GtkAssistant *gtkassistant, gpointer user_data);
void ap_assistant_cancel (GtkAssistant *gtkassistant, gpointer user_data);
void ap_assistant_close (GtkAssistant *gtkassistant, gpointer user_data);
/* =============================================================== */
/* Find the earliest date occurring in the book. Do this by making
* a query and sorting by date. Since the truncated sort returns
* only the *last* search results, sort in decreasing order.
*/
static time64
get_earliest_in_book (QofBook *book)
{
QofQuery *q;
GSList *p1, *p2;
GList *res;
time64 earliest;
q = qof_query_create_for(GNC_ID_SPLIT);
qof_query_set_max_results(q, 1);
qof_query_set_book (q, book);
/* Sort by transaction date */
p1 = g_slist_prepend (NULL, TRANS_DATE_POSTED);
p1 = g_slist_prepend (p1, SPLIT_TRANS);
p2 = g_slist_prepend (NULL, QUERY_DEFAULT_SORT);
qof_query_set_sort_order (q, p1, p2, NULL);
/* Reverse the sort order */
qof_query_set_sort_increasing (q, FALSE, FALSE, FALSE);
/* Run the query, find the earliest transaction date */
res = qof_query_run (q);
if (res)
{
earliest = xaccQueryGetEarliestDateFound (q);
}
else
{
/* If no results, we don't want to bomb totally */
earliest = gnc_time (NULL);
}
qof_query_destroy (q);
return earliest;
}
/* =============================================================== */
/* Find the number of transactions occurring before the indicated date.
* Do this by making a query and counting the results.
*/
static int
get_num_xactions_before_date(QofBook *book, time64 close_date)
{
QofQuery *q;
GSList *param;
QofQueryPredData *pred;
GList *res, *n;
int cnt = 0;
q = qof_query_create_for(GNC_ID_TRANS);
qof_query_set_max_results(q, -1);
qof_query_set_book (q, book);
/* Look for transactions earlier than the closing date */
param = g_slist_prepend (NULL, TRANS_DATE_POSTED);
pred = qof_query_date_predicate (QOF_COMPARE_LTE, QOF_DATE_MATCH_NORMAL, close_date);
qof_query_add_term (q, param, pred, QOF_QUERY_FIRST_TERM);
/* Run the query, find how many transactions there are */
res = qof_query_run (q);
cnt = 0;
for (n = res; n; n = n->next) cnt ++;
qof_query_destroy (q);
return cnt;
}
/* =============================================================== */
static const char *
get_close_status_str (AcctPeriodInfo *info)
{
const char * str;
/* Tell user about how the previous book closing went. */
switch (info->close_status)
{
case -1:
str = "";
break;
case 0:
str = _("The book was closed successfully.");
break;
default:
str = "";
break;
}
return str;
}
/* =============================================================== */
static void
ap_assistant_destroy_cb (GtkWidget *object, gpointer data)
{
AcctPeriodInfo *info = data;
gnc_unregister_gui_component_by_data (ASSISTANT_ACCT_PERIOD_CM_CLASS, info);
// do we need gnc_frequency_destroy or is this automatic ??
recurrenceListFree(&info->period);
g_free (info->earliest_str);
g_free (info);
}
/* =============================================================== */
void
ap_assistant_cancel (GtkAssistant *assistant, gpointer user_data)
{
AcctPeriodInfo *info = user_data;
gnc_close_gui_component_by_data (ASSISTANT_ACCT_PERIOD_CM_CLASS, info);
}
/* =============================================================== */
void
ap_assistant_close (GtkAssistant *assistant, gpointer user_data)
{
AcctPeriodInfo *info = user_data;
gnc_close_gui_component_by_data (ASSISTANT_ACCT_PERIOD_CM_CLASS, info);
}
/* =============================================================== */
void
ap_assistant_prepare (GtkAssistant *assistant, GtkWidget *page,
gpointer user_data)
{
gint currentpage = gtk_assistant_get_current_page(assistant);
switch (currentpage)
{
case 1:
/* Current page is Menu page */
ap_assistant_menu_prepare(assistant, user_data);
break;
case 2:
/* Current page is Book page */
ap_assistant_book_prepare (assistant, user_data);
break;
case 3:
/* Current page is Apply page */
ap_assistant_apply_prepare (assistant, user_data);
break;
case 4:
/* Current page is Summary page */
ap_assistant_summary_prepare (assistant, user_data);
break;
}
}
/* =============================================================== */
void
ap_assistant_menu_prepare (GtkAssistant *assistant, gpointer user_data)
{
int nperiods;
GDate period_begin, period_end, date_now;
char *str, *earliest_str;
AcctPeriodInfo *info = user_data;
ENTER ("info=%p", info);
/* Pull info from widget, push into freq spec */
//gnc_frequency_save_state (info->period_menu, info->period, &info->closing_date);
recurrenceListFree(&info->period);
gnc_frequency_save_to_recurrence(info->period_menu, &info->period, &info->closing_date);
/* Count the number of periods that would be generated. */
g_date_clear (&period_begin, 1);
g_date_clear (&period_end, 1);
g_date_clear (&date_now, 1);
nperiods = 0;
period_end = info->closing_date;
gnc_gdate_set_time64 (&date_now, gnc_time (NULL));
while (0 > g_date_compare(&period_end, &date_now ))
{
nperiods ++;
PINFO ("Period = %d and End date is %d/%d/%d", nperiods,
g_date_get_month(&period_end),
g_date_get_day(&period_end),
g_date_get_year(&period_end));
period_begin = period_end;
recurrenceListNextInstance(info->period, &period_begin, &period_end);
/* FIXME Check for valid period_end, not sure why it won't be!!! */
if (g_date_valid (&period_end) != TRUE)
break;
}
/* Find the date of the earliest transaction in the current book.
* Note that this could have changed since last time, since
* we may have closed books since last time. */
info->earliest = get_earliest_in_book (gnc_get_current_book());
info->earliest_str = qof_print_date(info->earliest);
earliest_str = gnc_ctime (&info->earliest);
PINFO ("Date of earliest transaction is %" G_GINT64_FORMAT " %s",
info->earliest, earliest_str);
g_free (earliest_str);
/* Display the results */
str = g_strdup_printf (
/* Translators: %s is a date string. %d is the number of books
that will be created. This is a ngettext(3) message (but
only for the %d part). */
ngettext("The earliest transaction date found in this book is %s. "
"Based on the selection made above, this book will be split "
"into %d book.",
"The earliest transaction date found in this book is %s. "
"Based on the selection made above, this book will be split "
"into %d books.",
nperiods),
info->earliest_str,
nperiods);
gtk_label_set_text (GTK_LABEL(info->period_remarks), str);
g_free (str);
}
/* =============================================================== */
void
ap_assistant_book_prepare (GtkAssistant *assistant, gpointer user_data)
{
QofBook *currbook;
char close_date_str[MAX_DATE_LENGTH];
char prev_close_date_str[MAX_DATE_LENGTH];
const char *period_text;
char *str;
const char *cstr;
int ntrans, nacc;
GtkTextBuffer *buffer;
AcctPeriodInfo *info = user_data;
ENTER ("info=%p", info);
/* Tell user about how the previous book closing went. */
cstr = get_close_status_str (info);
gtk_label_set_text (GTK_LABEL(info->close_results), cstr);
info->close_status = -1;
/* Pull info from widget, push into freq spec */
//gnc_frequency_save_state (info->period_menu, info->period, &info->closing_date);
recurrenceListFree(&info->period);
gnc_frequency_save_to_recurrence(info->period_menu, &info->period, &info->closing_date);
qof_print_date_dmy_buff (close_date_str, MAX_DATE_LENGTH,
g_date_get_day(&info->closing_date),
g_date_get_month(&info->closing_date),
g_date_get_year(&info->closing_date));
currbook = gnc_get_current_book();
ntrans = get_num_xactions_before_date(currbook,
gnc_time64_get_day_end_gdate (&info->closing_date));
nacc = gnc_account_n_descendants (gnc_book_get_root_account (currbook));
/* Display the book info */
period_text =
/* Translators: Run the assistant in your language to see GTK's translation of the button labels. */
_("You have asked for a book to be created. This book "
"will contain all transactions up to midnight %s "
"(for a total of %d transactions spread over %d accounts).\n\n"
"Amend the Title and Notes or Click on \"Next\" to proceed.\n"
"Click on \"Back\" to adjust the dates or \"Cancel\".");
str = g_strdup_printf (period_text, close_date_str, ntrans, nacc);
gtk_label_set_text (GTK_LABEL(info->book_details), str);
g_free (str);
gtk_widget_show (GTK_WIDGET (info->book_details));
/* Create default settings for the title, notes fields */
qof_print_date_dmy_buff (prev_close_date_str, MAX_DATE_LENGTH,
g_date_get_day(&info->prev_closing_date),
g_date_get_month(&info->prev_closing_date),
g_date_get_year(&info->prev_closing_date));
str = g_strdup_printf (_("Period %s - %s"), prev_close_date_str, close_date_str);
gtk_entry_set_text (GTK_ENTRY(info->book_title), str);
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(info->book_notes));
gtk_text_buffer_set_text(buffer, str, -1);
g_free (str);
}
/* =============================================================== */
void
ap_assistant_apply_prepare (GtkAssistant *assistant, gpointer user_data)
{
AcctPeriodInfo *info = user_data;
const char *btitle;
char *str;
const char *apply_text =
_("The book will be created with the title %s when you "
"click on \"Apply\". Click on \"Back\" to adjust, "
"or \"Cancel\" to not create any book.");
btitle = gtk_entry_get_text (GTK_ENTRY(info->book_title));
str = g_strdup_printf (apply_text, btitle);
gtk_label_set_text (GTK_LABEL(info->apply_label), str);
g_free (str);
}
/* =============================================================== */
static void
ap_assistant_menu_changed_cb (GtkWidget *widget, gpointer user_data)
{
AcctPeriodInfo *info = user_data;
GtkAssistant *assistant = GTK_ASSISTANT(info->window);
gint num = gtk_assistant_get_current_page (assistant);
GtkWidget *page = gtk_assistant_get_nth_page (assistant, num);
ENTER ("info=%p", info);
ap_assistant_menu_prepare (assistant, info);
gtk_assistant_set_page_complete (assistant, page, ap_validate_menu (assistant, user_data));
}
/* =============================================================== */
gboolean
ap_validate_menu (GtkAssistant *assistant, gpointer user_data)
{
GDate date_now;
AcctPeriodInfo *info = user_data;
ENTER("info=%p", info);
/* Pull info from widget, push into freq spec */
//gnc_frequency_save_state (info->period_menu, info->period, &info->closing_date);
recurrenceListFree(&info->period);
gnc_frequency_save_to_recurrence(info->period_menu, &info->period, &info->closing_date);
if (0 <= g_date_compare(&info->prev_closing_date, &info->closing_date))
{
/* Closing date must be greater than closing date of previous book */
return FALSE;
}
g_date_clear (&date_now, 1);
gnc_gdate_set_today (&date_now);
if (0 < g_date_compare(&info->closing_date, &date_now))
{
/* Closing date must be in the future */
return FALSE;
}
return TRUE;
}
/* =============================================================== */
void
ap_assistant_finish (GtkAssistant *assistant, gpointer user_data)
{
AcctPeriodInfo *info = user_data;
GtkTextBuffer * buffer;
GtkTextIter startiter, enditer;
gint len;
const char *btitle;
char *bnotes;
ENTER("info=%p", info);
btitle = gtk_entry_get_text (GTK_ENTRY(info->book_title));
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(info->book_notes));
len = gtk_text_buffer_get_char_count (buffer);
gtk_text_buffer_get_iter_at_offset(buffer, &startiter, 0);
gtk_text_buffer_get_iter_at_offset(buffer, &enditer, len);
bnotes = gtk_text_buffer_get_text(buffer, &startiter, &enditer , 0);
PINFO("Book title is - %s\n", btitle);
g_free(bnotes);
/* Report the status back to the user. */
info->close_status = 0; /* XXX fixme success or failure? */
/* Find the next closing date ... */
info->prev_closing_date = info->closing_date;
recurrenceListNextInstance (info->period, &info->prev_closing_date,
&info->closing_date);
/* FIXME Test for valid closing date, not sure why it won't be!!! */
if (g_date_valid(&info->closing_date) == TRUE)
{
/* If the next closing date is in the future, then we are done. */
if (gnc_time (NULL) >
gnc_time64_get_day_end_gdate (&info->closing_date))
{
/* Load up the GUI for the next closing period. */
gnc_frequency_setup_recurrence (info->period_menu, NULL,
&info->closing_date);
/* Jump back to the Close Book page. */
gtk_assistant_set_current_page (GTK_ASSISTANT(info->window), 1);
}
}
}
/* =============================================================== */
void
ap_assistant_summary_prepare (GtkAssistant *assistant, gpointer user_data)
{
const char *msg;
char *str;
AcctPeriodInfo *info = user_data;
ENTER ("info=%p", info);
/* Translation FIXME: Can this %s-containing message please be
replaced by one single message? Either this closing went
successfully ("success", "congratulations") or something else
should be displayed anyway. */
msg = _("%s\nCongratulations! You are done closing books!\n");
str = g_strdup_printf (msg, get_close_status_str (info));
gtk_label_set_text (GTK_LABEL(info->summary), str);
g_free (str);
}
/* =============================================================== */
static GtkWidget *
ap_assistant_create (AcctPeriodInfo *info)
{
GtkBuilder *builder;
GtkWidget *window;
GtkWidget *box;
gchar *earliest_str;
builder = gtk_builder_new();
gnc_builder_add_from_file (builder , "assistant-acct-period.glade", "account_period_assistant");
window = GTK_WIDGET(gtk_builder_get_object (builder, "account_period_assistant"));
info->window = window;
// Set the name for this assistant so it can be easily manipulated with css
gtk_widget_set_name (GTK_WIDGET(window), "gnc-id-assistant-account-period");
/* Enable all pages except menu page. */
gtk_assistant_set_page_complete (GTK_ASSISTANT (window),
GTK_WIDGET(gtk_builder_get_object(builder, "start_page")),
TRUE);
gtk_assistant_set_page_complete (GTK_ASSISTANT (window),
GTK_WIDGET(gtk_builder_get_object(builder, "book_page")),
TRUE);
gtk_assistant_set_page_complete (GTK_ASSISTANT (window),
GTK_WIDGET(gtk_builder_get_object(builder, "finish_page")),
TRUE);
gtk_assistant_set_page_complete (GTK_ASSISTANT (window),
GTK_WIDGET(gtk_builder_get_object(builder, "summary_page")),
TRUE);
info->close_status = -1;
/* Find the date of the earliest transaction in the book.
* Add a year minus a day as the first guess for book closing,
* and use that to set up the freq spec widget. */
info->earliest = get_earliest_in_book (gnc_get_current_book());
info->earliest_str = qof_print_date(info->earliest);
earliest_str = gnc_ctime (&info->earliest);
PINFO ("date of earliest transaction is %" G_GINT64_FORMAT " %s",
info->earliest, earliest_str);
g_free (earliest_str);
g_date_clear (&info->closing_date, 1);
gnc_gdate_set_time64 (&info->closing_date, info->earliest);
g_date_clear (&info->prev_closing_date, 1);
info->prev_closing_date = info->closing_date;
g_date_add_years (&info->closing_date, 1);
{
Recurrence *r = g_new0(Recurrence, 1);
recurrenceSet(r, 1, PERIOD_MONTH, &info->closing_date, WEEKEND_ADJ_NONE);
info->period = NULL;
info->period = g_list_append(info->period, r);
}
info->period_menu = GNC_FREQUENCY(
gnc_frequency_new_from_recurrence(info->period, &info->closing_date));
/* Change the text so that its more mainingful for this assistant */
gnc_frequency_set_frequency_label_text(info->period_menu, _("Period"));
gnc_frequency_set_date_label_text(info->period_menu, _("Closing Date"));
/* Reparent to the correct location */
box = GTK_WIDGET(gtk_builder_get_object(builder, "period_hbox"));
gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (info->period_menu), TRUE, TRUE, 0);
g_signal_connect (info->period_menu, "changed",
G_CALLBACK (ap_assistant_menu_changed_cb), info);
/* Get handles to all of the other widgets we'll need */
info->period_remarks = GTK_WIDGET(gtk_builder_get_object(builder, "remarks_label"));
info->close_results = GTK_WIDGET(gtk_builder_get_object(builder, "results_label"));
info->book_details = GTK_WIDGET(gtk_builder_get_object(builder, "book_label"));
info->book_title = GTK_WIDGET(gtk_builder_get_object(builder, "book_title_entry"));
info->book_notes = GTK_TEXT_VIEW(gtk_builder_get_object(builder, "book_notes_view"));
info->apply_label = GTK_WIDGET(gtk_builder_get_object(builder, "finish_page"));
info->summary = GTK_WIDGET(gtk_builder_get_object(builder, "summary_label"));
g_signal_connect (G_OBJECT(window), "destroy",
G_CALLBACK (ap_assistant_destroy_cb), info);
gtk_builder_connect_signals(builder, info);
g_object_unref(G_OBJECT(builder));
return window;
}
/* =============================================================== */
static void
ap_close_handler (gpointer user_data)
{
AcctPeriodInfo *info = user_data;
gtk_widget_destroy (info->window);
}
/********************************************************************\
* gnc_acct_period_dialog *
* opens up a assistant to configure accounting periods *
* *
* Args: none *
* Return: nothing *
\********************************************************************/
void
gnc_acct_period_dialog (void)
{
AcctPeriodInfo *info;
info = g_new0 (AcctPeriodInfo, 1);
ap_assistant_create (info);
gnc_register_gui_component (ASSISTANT_ACCT_PERIOD_CM_CLASS,
NULL, ap_close_handler, info);
gtk_widget_show_all (info->window);
gnc_window_adjust_for_screen (GTK_WINDOW(info->window));
}