GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dialog-find-transactions.c
1 /********************************************************************\
2  * dialog-find-transactions.c : locate transactions and show them *
3  * Copyright (C) 2000 Bill Gribble <[email protected]> *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA [email protected] *
21 \********************************************************************/
22 
23 #include "config.h"
24 
25 #include <gtk/gtk.h>
26 #include <glib/gi18n.h>
27 #include <stdio.h>
28 
29 #include "gnc-ui-util.h"
30 #include "Query.h"
31 #include "qof.h"
32 #include "SX-book.h"
33 #include "Transaction.h"
34 #include "dialog-find-transactions.h"
35 #include "gnc-main-window.h"
37 #include "search-param.h"
38 
39 #define GNC_PREFS_GROUP_SEARCH "dialogs.find"
40 
41 struct _ftd_data
42 {
43  QofQuery * q;
44  QofQuery * ledger_q;
45  GNCSearchWindow * sw;
46 };
47 
48 static void
49 do_find_cb (QofQuery *query, gpointer user_data, gpointer *result)
50 {
51  struct _ftd_data *ftd = user_data;
52  GNCLedgerDisplay *ledger;
53  gboolean new_ledger = FALSE;
54  GncPluginPage *page;
55 
56  ledger = gnc_ledger_display_find_by_query (ftd->ledger_q);
57  if (!ledger)
58  {
59  new_ledger = TRUE;
60  ledger = gnc_ledger_display_query (query, SEARCH_LEDGER,
61  REG_STYLE_JOURNAL);
62  }
63  else
64  gnc_ledger_display_set_query (ledger, query);
65 
66  gnc_ledger_display_refresh (ledger);
67 
68  if (new_ledger)
69  {
71  gnc_main_window_open_page (NULL, page);
72  }
73 
74  qof_query_destroy (ftd->q);
75 
76  gnc_search_dialog_destroy (ftd->sw);
77 }
78 
79 static void
80 free_ftd_cb (gpointer user_data)
81 {
82  struct _ftd_data *ftd = user_data;
83 
84  if (!ftd)
85  return;
86 
87  g_free (ftd);
88 }
89 
91 gnc_ui_find_transactions_dialog_create(GNCLedgerDisplay * orig_ledg)
92 {
93  QofIdType type = GNC_ID_SPLIT;
94  struct _ftd_data *ftd;
95  static GList *params = NULL;
96  QofQuery *start_q, *show_q = NULL;
97  gboolean num_action =
98  qof_book_use_split_action_for_num_field(gnc_get_current_book());
99 
100  /* Build parameter list in reverse order */
101  if (params == NULL)
102  {
103  params = gnc_search_param_prepend (params, N_("All Accounts"),
105  type, SPLIT_TRANS, TRANS_SPLITLIST,
106  SPLIT_ACCOUNT_GUID, NULL);
107  params = gnc_search_param_prepend (params, N_("Account"), GNC_ID_ACCOUNT,
108  type, SPLIT_ACCOUNT, QOF_PARAM_GUID,
109  NULL);
110  params = gnc_search_param_prepend (params, N_("Balanced"), NULL,
111  type, SPLIT_TRANS, TRANS_IS_BALANCED,
112  NULL);
113  params = gnc_search_param_prepend (params, N_("Closing Entries"), NULL,
114  type, SPLIT_TRANS, TRANS_IS_CLOSING,
115  NULL);
116  params = gnc_search_param_prepend (params, N_("Reconcile"), RECONCILED_MATCH_TYPE,
117  type, SPLIT_RECONCILE, NULL);
118  params = gnc_search_param_prepend (params, N_("Share Price"), NULL,
119  type, SPLIT_SHARE_PRICE, NULL);
120  params = gnc_search_param_prepend (params, N_("Shares"), NULL,
121  type, SPLIT_AMOUNT, NULL);
122  params = gnc_search_param_prepend (params, N_("Value"), NULL,
123  type, SPLIT_VALUE, NULL);
124  params = gnc_search_param_prepend (params, N_("Date Posted"), NULL,
125  type, SPLIT_TRANS, TRANS_DATE_POSTED,
126  NULL);
127  params = gnc_search_param_prepend (params, N_("Notes"), NULL,
128  type, SPLIT_TRANS, TRANS_NOTES, NULL);
129  params = gnc_search_param_prepend (params, (num_action
130  ? N_("Number/Action")
131  : N_("Action")), NULL,
132  type, SPLIT_ACTION, NULL);
133  params = gnc_search_param_prepend (params, (num_action
134  ? N_("Transaction Number")
135  : N_("Number")), NULL,
136  type, SPLIT_TRANS, TRANS_NUM, NULL);
137  params = gnc_search_param_prepend (params, N_("Memo"), NULL,
138  type, SPLIT_MEMO, NULL);
139  params = gnc_search_param_prepend (params, N_("Description"), NULL,
140  type, SPLIT_TRANS, TRANS_DESCRIPTION,
141  NULL);
142  }
143  else
144  {
145  GList *l;
146  for (l = params; l; l = l->next)
147  {
148  GNCSearchParam *param = l->data;
149 
150  if (num_action)
151  {
152  if (strcmp (param->title, N_("Action")) == 0)
153  gnc_search_param_set_title (param, N_("Number/Action"));
154  if (strcmp (param->title, N_("Number")) == 0)
155  gnc_search_param_set_title (param, N_("Transaction Number"));
156  }
157  else
158  {
159  if (strcmp (param->title, N_("Number/Action")) == 0)
160  gnc_search_param_set_title (param, N_("Action"));
161  if (strcmp (param->title, N_("Transaction Number")) == 0)
162  gnc_search_param_set_title (param, N_("Number"));
163  }
164  }
165  }
166 
167  ftd = g_new0 (struct _ftd_data, 1);
168 
169  if (orig_ledg)
170  {
171  ftd->ledger_q = gnc_ledger_display_get_query (orig_ledg);
172  start_q = show_q = qof_query_copy (ftd->ledger_q);
173  }
174  else
175  {
176  start_q = qof_query_create ();
177  qof_query_set_book (start_q, gnc_get_current_book ());
178 
179  /* In lieu of not "mis-using" some portion of the infrastructure by writing
180  * a bunch of new code, we just filter out the accounts of the template
181  * transactions. While these are in a seperate Account trees just for this
182  * reason, the query engine makes no distinction between Account trees.
183  * See Gnome Bug 86302.
184  * -- jsled
185  *
186  * copied from gnc-ledger-display.c:gnc_ledger_display_gl() -- warlord
187  *
188  * <jsled> Alternatively, you could look for a GNC_SX_ACCOUNT [SchedAction.h]
189  * key in the KVP frame of the split.
190  */
191  {
192  Account *tRoot;
193  GList *al;
194 
195  tRoot = gnc_book_get_template_root( gnc_get_current_book() );
196  al = gnc_account_get_descendants( tRoot );
197  xaccQueryAddAccountMatch( start_q, al, QOF_GUID_MATCH_NONE, QOF_QUERY_AND );
198  g_list_free (al);
199  al = NULL;
200  tRoot = NULL;
201  }
202 
203  ftd->q = start_q; /* save this to destroy it later */
204  }
205 
206  ftd->sw = gnc_search_dialog_create (type, _("Find Transaction"),
207  params, NULL, start_q, show_q,
208  NULL, do_find_cb, NULL,
209  ftd, free_ftd_cb, GNC_PREFS_GROUP_SEARCH, NULL);
210 
211  if (!ftd->sw)
212  {
213  free_ftd_cb (ftd);
214  return NULL;
215  }
216 
217  return ftd->sw;
218 }
utility functions for the GnuCash UI
gboolean qof_book_use_split_action_for_num_field(const QofBook *book)
QofQuery * qof_query_copy(QofQuery *q)
Account * gnc_book_get_template_root(const QofBook *book)
Definition: SX-book.c:65
struct _QofQuery QofQuery
Definition: qofquery.h:90
void gnc_main_window_open_page(GncMainWindow *window, GncPluginPage *page)
Functions for adding content to a window.
GncPluginPage * gnc_plugin_page_register_new_ledger(GNCLedgerDisplay *ledger)
const gchar * QofIdType
Definition: qofid.h:85
Functions providing a register page for the GnuCash UI.
void qof_query_destroy(QofQuery *q)
#define ACCOUNT_MATCH_ALL_TYPE
Definition: Account.h:1438
void qof_query_set_book(QofQuery *q, QofBook *book)
#define SPLIT_ACCOUNT_GUID
Definition: Split.h:513
Anchor Scheduled Transaction info in a book. See src/doc/books.txt for design overview.
GList * gnc_account_get_descendants(const Account *account)
Definition: Account.c:2755
API for Transactions and Splits (journal entries)
QofQuery * qof_query_create(void)