~siretart/gnucash/ubuntu-fullsource

« back to all changes in this revision

Viewing changes to src/gnome-utils/dialog-book-close.c

  • Committer: Reinhard Tartler
  • Date: 2008-08-03 07:25:46 UTC
  • Revision ID: siretart@tauware.de-20080803072546-y6p8xda8zpfi62ys
import gnucash_2.2.4.orig.tar.gz

The original tarball had the md5sum: 27e660297dc5b8ce574515779d05a5a5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/********************************************************************\
 
2
 * dialog-book-close.c -- dialog for helping the user close the     *
 
3
 *                        book at the end of the year by adding     *
 
4
 *                        zero-izing splits to all Income and       *
 
5
 *                        Expense accounts                          *
 
6
 *                                                                  *
 
7
 * Copyright (C) 2007-8 Derek Atkins <derek@ihtfp.com>              *
 
8
 *                                                                  *
 
9
 * This program is free software; you can redistribute it and/or    *
 
10
 * modify it under the terms of the GNU General Public License as   *
 
11
 * published by the Free Software Foundation; either version 2 of   *
 
12
 * the License, or (at your option) any later version.              *
 
13
 *                                                                  *
 
14
 * This program is distributed in the hope that it will be useful,  *
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
 
17
 * GNU General Public License for more details.                     *
 
18
 *                                                                  *
 
19
 * You should have received a copy of the GNU General Public License*
 
20
 * along with this program; if not, contact:                        *
 
21
 *                                                                  *
 
22
 * Free Software Foundation           Voice:  +1-617-542-5942       *
 
23
 * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
 
24
 * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
 
25
\********************************************************************/
 
26
 
 
27
#include "config.h"
 
28
 
 
29
#include <gtk/gtk.h>
 
30
#include <glib/gi18n.h>
 
31
#include <glade/glade.h>
 
32
 
 
33
#include "dialog-utils.h"
 
34
#include "gnc-engine.h"
 
35
#include "Transaction.h"
 
36
#include "Split.h"
 
37
#include "Account.h"
 
38
#include "gnc-ui.h"
 
39
#include "gnc-gui-query.h"
 
40
#include "dialog-book-close.h"
 
41
#include "gnc-account-sel.h"
 
42
#include "gnc-component-manager.h"
 
43
#include "gnc-date-edit.h"
 
44
#include "gnc-session.h"
 
45
 
 
46
#define DIALOG_BOOK_CLOSE_CM_CLASS "dialog-book-close"
 
47
 
 
48
void gnc_book_close_response_cb(GtkDialog *, gint, GtkDialog *);
 
49
 
 
50
struct CloseBookWindow
 
51
{
 
52
  /* Passed in by the creator */
 
53
  QofBook* book;
 
54
 
 
55
  /* Parts of the dialog */
 
56
  GtkWidget* dialog;
 
57
  GtkWidget* close_date_widget;
 
58
  GtkWidget* income_acct_widget;
 
59
  GtkWidget* expense_acct_widget;
 
60
  GtkWidget* desc_widget;
 
61
 
 
62
  /* The final settings */
 
63
  time_t close_date;
 
64
  const char* desc;
 
65
 
 
66
  /* Component registration */
 
67
  gint component_manager_id;
 
68
};
 
69
 
 
70
struct CloseAccountsCB
 
71
{
 
72
  struct CloseBookWindow* cbw;
 
73
  Account* base_acct;
 
74
  GNCAccountType acct_type;
 
75
  GHashTable* txns;
 
76
  guint hash_size;
 
77
};
 
78
 
 
79
struct CACBTransactionList
 
80
{
 
81
  gnc_commodity* cmdty;
 
82
  Transaction* txn;
 
83
  gnc_numeric total;
 
84
};
 
85
 
 
86
static struct CACBTransactionList*
 
87
find_or_create_txn(struct CloseAccountsCB* cacb, gnc_commodity* cmdty)
 
88
{
 
89
  struct CACBTransactionList* txn;
 
90
 
 
91
  g_return_val_if_fail(cacb, NULL);
 
92
  g_return_val_if_fail(cmdty, NULL);
 
93
 
 
94
  txn = g_hash_table_lookup(cacb->txns, cmdty);
 
95
  if (!txn)
 
96
  {
 
97
    txn = g_new0(struct CACBTransactionList, 1);
 
98
    txn->cmdty = cmdty;
 
99
    txn->total = gnc_numeric_zero();
 
100
    txn->txn = xaccMallocTransaction(cacb->cbw->book);
 
101
    xaccTransBeginEdit(txn->txn);
 
102
    xaccTransSetDateEnteredSecs(txn->txn, time(NULL));
 
103
    xaccTransSetDatePostedSecs(txn->txn, cacb->cbw->close_date);
 
104
    xaccTransSetDescription(txn->txn, cacb->cbw->desc);
 
105
    xaccTransSetCurrency(txn->txn, cmdty);
 
106
 
 
107
    g_hash_table_insert(cacb->txns, cmdty, txn);
 
108
  }
 
109
 
 
110
  return txn;
 
111
}
 
112
 
 
113
/* Make sure that the account is of the correct type.
 
114
 * then make sure the account has a balance as of the closing date.
 
115
 * then get the account commodity and find the appropriate
 
116
 * balancing transaction for that commodity and add this balance
 
117
 * to it.
 
118
 */
 
119
static void close_accounts_cb(Account *a, gpointer data)
 
120
{
 
121
  struct CloseAccountsCB* cacb = data;
 
122
  struct CACBTransactionList* txn;
 
123
  gnc_commodity* acct_commodity;
 
124
  Split* split;
 
125
  gnc_numeric bal;
 
126
 
 
127
  g_return_if_fail(a);
 
128
  g_return_if_fail(cacb);
 
129
  g_return_if_fail(cacb->cbw);
 
130
  g_return_if_fail(cacb->txns);
 
131
 
 
132
  if (cacb->acct_type != xaccAccountGetType(a))
 
133
    return;
 
134
 
 
135
  bal = xaccAccountGetBalanceAsOfDate(a, cacb->cbw->close_date+1);
 
136
  if (gnc_numeric_zero_p(bal))
 
137
    return;
 
138
 
 
139
  acct_commodity = xaccAccountGetCommodity(a);
 
140
  g_assert(acct_commodity);
 
141
 
 
142
  txn = find_or_create_txn(cacb, acct_commodity);
 
143
  g_assert(txn);
 
144
  
 
145
  split = xaccMallocSplit(cacb->cbw->book);
 
146
  xaccSplitSetParent(split, txn->txn);
 
147
  xaccAccountBeginEdit(a);
 
148
  xaccAccountInsertSplit(a, split);
 
149
  xaccSplitSetBaseValue(split, gnc_numeric_neg(bal), acct_commodity);
 
150
  xaccAccountCommitEdit(a);
 
151
  txn->total = gnc_numeric_add(txn->total, bal, GNC_DENOM_AUTO,
 
152
                               GNC_HOW_DENOM_FIXED | GNC_HOW_RND_NEVER);
 
153
}
 
154
 
 
155
 
 
156
static void finish_txn_cb(gnc_commodity* cmdty,
 
157
                          struct CACBTransactionList* txn,
 
158
                          struct CloseAccountsCB* cacb)
 
159
{
 
160
  Account* acc;
 
161
  Split* split;
 
162
 
 
163
  g_return_if_fail(cmdty);
 
164
  g_return_if_fail(txn);
 
165
  g_return_if_fail(cacb);
 
166
  g_return_if_fail(cacb->hash_size);
 
167
 
 
168
  /* If we only have one currency and the base account uses
 
169
   * that currency, then we can use that account.  Otherwise,
 
170
   * create a subaccount for each currency.
 
171
   */
 
172
  if (cacb->hash_size == 1 &&
 
173
      gnc_commodity_equal(cmdty, xaccAccountGetCommodity(cacb->base_acct)))
 
174
    acc = cacb->base_acct;
 
175
  else
 
176
  {
 
177
    /* See if we already have an account by that name */
 
178
    acc = gnc_account_lookup_by_name(cacb->base_acct,
 
179
                                     gnc_commodity_get_mnemonic(cmdty));
 
180
 
 
181
    /* If not, then create one */
 
182
    if (!acc)
 
183
    {
 
184
      acc = xaccMallocAccount(cacb->cbw->book);
 
185
      xaccAccountBeginEdit(acc);
 
186
      xaccAccountSetType(acc, ACCT_TYPE_EQUITY);
 
187
      xaccAccountSetName(acc, gnc_commodity_get_mnemonic(cmdty));
 
188
      xaccAccountSetDescription(acc, gnc_commodity_get_mnemonic(cmdty));
 
189
      xaccAccountSetCommodity(acc, cmdty);
 
190
      gnc_account_append_child(cacb->base_acct, acc);
 
191
      xaccAccountCommitEdit(acc);
 
192
    }
 
193
  }
 
194
  /* Make sure the account exists and is of the correct commodity */
 
195
  g_assert(acc);
 
196
  g_assert(gnc_commodity_equal(cmdty, xaccAccountGetCommodity(acc)));
 
197
 
 
198
  /* Create the split for the Equity account to balance out
 
199
   * all the accounts of this.  Use the "total".
 
200
   */
 
201
  split = xaccMallocSplit(cacb->cbw->book);
 
202
  xaccSplitSetParent(split, txn->txn);
 
203
  xaccAccountBeginEdit(acc);
 
204
  xaccAccountInsertSplit(acc, split);
 
205
  xaccSplitSetBaseValue(split, txn->total, cmdty);
 
206
  xaccAccountCommitEdit(acc);
 
207
  xaccTransCommitEdit(txn->txn);
 
208
}
 
209
 
 
210
static void close_accounts_of_type(struct CloseBookWindow* cbw,
 
211
                                   Account* acct,
 
212
                                   GNCAccountType acct_type)
 
213
{
 
214
  struct CloseAccountsCB cacb;
 
215
  Account* root_acct;
 
216
 
 
217
  g_return_if_fail(cbw);
 
218
  g_return_if_fail(acct);
 
219
 
 
220
  cacb.cbw = cbw;
 
221
  cacb.base_acct = acct;
 
222
  cacb.acct_type = acct_type;
 
223
  cacb.txns = g_hash_table_new_full(g_direct_hash,
 
224
                                    (GEqualFunc)gnc_commodity_equal,
 
225
                                    NULL, g_free);
 
226
 
 
227
  /* Iterate through all accounts and set up the balancing splits */
 
228
  root_acct = gnc_book_get_root_account(cbw->book);
 
229
  gnc_account_foreach_descendant(root_acct, close_accounts_cb, &cacb);
 
230
 
 
231
  /* now iterate through the transactions and handle each currency */
 
232
  cacb.hash_size = g_hash_table_size(cacb.txns);
 
233
  if (cacb.hash_size)
 
234
    g_hash_table_foreach(cacb.txns, (GHFunc)finish_txn_cb, &cacb);
 
235
 
 
236
  /* Destroy the table, freeing the used memory */
 
237
  g_hash_table_destroy(cacb.txns);
 
238
}
 
239
 
 
240
static void close_handler(gpointer data)
 
241
{
 
242
  GtkWidget *dialog = data;
 
243
 
 
244
  gtk_widget_destroy(dialog);
 
245
}
 
246
 
 
247
static void destroy_cb(GtkObject *object, gpointer data)
 
248
{
 
249
  struct CloseBookWindow *cbw;
 
250
 
 
251
  cbw = g_object_get_data(G_OBJECT(object), "CloseBookWindow");
 
252
 
 
253
  if (cbw->component_manager_id) {
 
254
    gnc_unregister_gui_component(cbw->component_manager_id);
 
255
    cbw->component_manager_id = 0;
 
256
  }
 
257
}
 
258
 
 
259
 
 
260
void
 
261
gnc_book_close_response_cb(GtkDialog *dialog, gint response, GtkDialog *unused)
 
262
{
 
263
  struct CloseBookWindow* cbw;
 
264
  Account* income_acct;
 
265
  Account* expense_acct;
 
266
 
 
267
  g_return_if_fail(dialog);
 
268
 
 
269
  cbw = g_object_get_data(G_OBJECT(dialog), "CloseBookWindow");
 
270
  g_return_if_fail(cbw);
 
271
 
 
272
  switch (response)
 
273
  {
 
274
  case GTK_RESPONSE_HELP:
 
275
     gnc_gnome_help(HF_HELP, HL_GLOBPREFS);
 
276
     break;
 
277
  case GTK_RESPONSE_OK:
 
278
    cbw->close_date = gnc_date_edit_get_date(GNC_DATE_EDIT(cbw->close_date_widget));
 
279
    cbw->close_date += (3600 * 12);  /* Add 12 hours to the timestamp */
 
280
    cbw->desc = gtk_entry_get_text(GTK_ENTRY(cbw->desc_widget));
 
281
 
 
282
    income_acct = gnc_account_sel_get_account(GNC_ACCOUNT_SEL(cbw->income_acct_widget));
 
283
    expense_acct = gnc_account_sel_get_account(GNC_ACCOUNT_SEL(cbw->expense_acct_widget));
 
284
 
 
285
    if (!income_acct)
 
286
    {
 
287
      gnc_error_dialog(cbw->dialog, "%s",
 
288
                       _("Please select an Equity account to hold the total Period Income."));
 
289
      break;
 
290
    }
 
291
 
 
292
    if (!expense_acct)
 
293
    {
 
294
      gnc_error_dialog(cbw->dialog, "%s",
 
295
                       _("Please select an Equity account to hold the total Period Expense."));
 
296
      break;
 
297
    }
 
298
 
 
299
    close_accounts_of_type(cbw, income_acct, ACCT_TYPE_INCOME);
 
300
    close_accounts_of_type(cbw, expense_acct, ACCT_TYPE_EXPENSE);
 
301
 
 
302
    /* FALLTHROUGH */ 
 
303
  default:
 
304
     gtk_widget_destroy(GTK_WIDGET(dialog));
 
305
     break;
 
306
  }
 
307
}
 
308
 
 
309
void gnc_ui_close_book (QofBook* book)
 
310
{
 
311
  struct CloseBookWindow *cbw;
 
312
  GladeXML* xml;
 
313
  GtkWidget* box;
 
314
  GList* equity_list = NULL;
 
315
 
 
316
  g_return_if_fail(book);
 
317
 
 
318
  cbw = g_new0(struct CloseBookWindow, 1);
 
319
  g_return_if_fail(cbw);
 
320
  cbw->book = book;
 
321
 
 
322
  /* Open the dialog */
 
323
  xml = gnc_glade_xml_new("dialog-book-close.glade", "Close Book");
 
324
  cbw->dialog = glade_xml_get_widget(xml, "Close Book");
 
325
 
 
326
  /* close date */
 
327
  box = glade_xml_get_widget(xml, "date_box");
 
328
  cbw->close_date_widget = gnc_date_edit_new(time(NULL), FALSE, FALSE);
 
329
  gtk_box_pack_start(GTK_BOX(box), cbw->close_date_widget, TRUE, TRUE, 0);
 
330
 
 
331
  /* income acct */
 
332
  equity_list = g_list_prepend(equity_list, GINT_TO_POINTER(ACCT_TYPE_EQUITY));
 
333
  box = glade_xml_get_widget(xml, "income_acct_box");
 
334
  cbw->income_acct_widget = gnc_account_sel_new();
 
335
  gnc_account_sel_set_acct_filters(GNC_ACCOUNT_SEL(cbw->income_acct_widget),
 
336
                                   equity_list);
 
337
  gnc_account_sel_set_new_account_ability(GNC_ACCOUNT_SEL(cbw->income_acct_widget), TRUE);
 
338
  gtk_box_pack_start(GTK_BOX(box), cbw->income_acct_widget, TRUE, TRUE, 0);
 
339
 
 
340
  /* expense acct */
 
341
  box = glade_xml_get_widget(xml, "expense_acct_box");
 
342
  cbw->expense_acct_widget = gnc_account_sel_new();
 
343
  gnc_account_sel_set_acct_filters(GNC_ACCOUNT_SEL(cbw->expense_acct_widget),
 
344
                                   equity_list);
 
345
  gnc_account_sel_set_new_account_ability(GNC_ACCOUNT_SEL(cbw->expense_acct_widget), TRUE);
 
346
  gtk_box_pack_start(GTK_BOX(box), cbw->expense_acct_widget, TRUE, TRUE, 0);
 
347
 
 
348
  /* desc */
 
349
  cbw->desc_widget = glade_xml_get_widget(xml, "desc_entry");
 
350
 
 
351
  /* Autoconnect signals */
 
352
  glade_xml_signal_autoconnect_full(xml, gnc_glade_autoconnect_full_func,
 
353
                                    cbw->dialog);
 
354
 
 
355
  /* Register dialog with component manager */
 
356
  cbw->component_manager_id =
 
357
    gnc_register_gui_component(DIALOG_BOOK_CLOSE_CM_CLASS, NULL, close_handler,
 
358
                               cbw->dialog);
 
359
  gnc_gui_component_set_session(cbw->component_manager_id,
 
360
                                gnc_get_current_session());
 
361
  g_signal_connect(cbw->dialog, "destroy", G_CALLBACK(destroy_cb), NULL);
 
362
 
 
363
  /* Clean up the xml data structure when the dialog is destroyed */
 
364
  g_object_set_data_full(G_OBJECT(cbw->dialog), "dialog-book-close.glade",
 
365
                         xml, g_object_unref);
 
366
  g_object_set_data_full(G_OBJECT(cbw->dialog), "CloseBookWindow", cbw,
 
367
                         g_free);
 
368
 
 
369
  /* Run the dialog */
 
370
  gtk_widget_show_all(cbw->dialog);
 
371
 
 
372
  g_list_free(equity_list);
 
373
}
 
374