GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dialog-find-transactions2.c
1 /********************************************************************\
2  * dialog-find-transactions2.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-transactions2.h"
35 #include "gnc-main-window.h"
36 #include "gnc-plugin-page-register2.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  GNCLedgerDisplay2 *ledger;
53  gboolean new_ledger = FALSE;
54  GncPluginPage *page;
55 
56  ledger = gnc_ledger_display2_find_by_query (ftd->ledger_q);
57 
58  if (!ledger)
59  {
60  new_ledger = TRUE;
61  ledger = gnc_ledger_display2_query (query, SEARCH_LEDGER2,
62  REG2_STYLE_JOURNAL);
63  }
64  else
65  gnc_ledger_display2_set_query (ledger, query);
66 
67  gnc_ledger_display2_refresh (ledger);
68 
69  if (new_ledger)
70  {
72  gnc_main_window_open_page (NULL, page);
73  }
74 
75  qof_query_destroy (ftd->q);
76 
77  gnc_search_dialog_destroy (ftd->sw);
78 }
79 
80 static void
81 free_ftd_cb (gpointer user_data)
82 {
83  struct _ftd_data *ftd = user_data;
84 
85  if (!ftd)
86  return;
87 
88  g_free (ftd);
89 }
90 
92 gnc_ui_find_transactions_dialog_create2 (GNCLedgerDisplay2 * orig_ledg)
93 {
94  QofIdType type = GNC_ID_SPLIT;
95  struct _ftd_data *ftd;
96  static GList *params = NULL;
97  QofQuery *start_q, *show_q = NULL;
98  gboolean num_action =
99  qof_book_use_split_action_for_num_field(gnc_get_current_book());
100 
101  /* Build parameter list in reverse order */
102  if (params == NULL)
103  {
104  params = gnc_search_param_prepend (params, N_("All Accounts"),
106  type, SPLIT_TRANS, TRANS_SPLITLIST,
107  SPLIT_ACCOUNT_GUID, NULL);
108  params = gnc_search_param_prepend (params, N_("Account"), GNC_ID_ACCOUNT,
109  type, SPLIT_ACCOUNT, QOF_PARAM_GUID,
110  NULL);
111  params = gnc_search_param_prepend (params, N_("Balanced"), NULL,
112  type, SPLIT_TRANS, TRANS_IS_BALANCED,
113  NULL);
114  params = gnc_search_param_prepend (params, N_("Closing Entries"), NULL,
115  type, SPLIT_TRANS, TRANS_IS_CLOSING,
116  NULL);
117  params = gnc_search_param_prepend (params, N_("Reconcile"), RECONCILED_MATCH_TYPE,
118  type, SPLIT_RECONCILE, NULL);
119  params = gnc_search_param_prepend (params, N_("Share Price"), NULL,
120  type, SPLIT_SHARE_PRICE, NULL);
121  params = gnc_search_param_prepend (params, N_("Shares"), NULL,
122  type, SPLIT_AMOUNT, NULL);
123  params = gnc_search_param_prepend (params, N_("Value"), NULL,
124  type, SPLIT_VALUE, NULL);
125  params = gnc_search_param_prepend (params, N_("Date Posted"), NULL,
126  type, SPLIT_TRANS, TRANS_DATE_POSTED,
127  NULL);
128  params = gnc_search_param_prepend (params, N_("Notes"), NULL,
129  type, SPLIT_TRANS, TRANS_NOTES, NULL);
130  params = gnc_search_param_prepend (params, (num_action
131  ? N_("Number/Action")
132  : N_("Action")), NULL,
133  type, SPLIT_ACTION, NULL);
134  params = gnc_search_param_prepend (params, (num_action
135  ? N_("Transaction Number")
136  : N_("Number")), NULL,
137  type, SPLIT_TRANS, TRANS_NUM, NULL);
138  params = gnc_search_param_prepend (params, N_("Memo"), NULL,
139  type, SPLIT_MEMO, NULL);
140  params = gnc_search_param_prepend (params, N_("Description"), NULL,
141  type, SPLIT_TRANS, TRANS_DESCRIPTION,
142  NULL);
143  }
144  else
145  {
146  GList *l;
147  for (l = params; l; l = l->next)
148  {
149  GNCSearchParam *param = l->data;
150 
151  if (num_action)
152  {
153  if (strcmp (param->title, N_("Action")) == 0)
154  gnc_search_param_set_title (param, N_("Number/Action"));
155  if (strcmp (param->title, N_("Number")) == 0)
156  gnc_search_param_set_title (param, N_("Transaction Number"));
157  }
158  else
159  {
160  if (strcmp (param->title, N_("Number/Action")) == 0)
161  gnc_search_param_set_title (param, N_("Action"));
162  if (strcmp (param->title, N_("Transaction Number")) == 0)
163  gnc_search_param_set_title (param, N_("Number"));
164  }
165  }
166  }
167 
168  ftd = g_new0 (struct _ftd_data, 1);
169 
170  if (orig_ledg)
171  {
172  ftd->ledger_q = gnc_ledger_display2_get_query (orig_ledg);
173  start_q = show_q = qof_query_copy (ftd->ledger_q);
174  }
175  else
176  {
177  start_q = qof_query_create ();
178  qof_query_set_book (start_q, gnc_get_current_book ());
179 
180  /* In lieu of not "mis-using" some portion of the infrastructure by writing
181  * a bunch of new code, we just filter out the accounts of the template
182  * transactions. While these are in a seperate Account trees just for this
183  * reason, the query engine makes no distinction between Account trees.
184  * See Gnome Bug 86302.
185  * -- jsled
186  *
187  * copied from gnc-ledger-display2.c:gnc_ledger_display2_gl() -- warlord
188  *
189  * <jsled> Alternatively, you could look for a GNC_SX_ACCOUNT [SchedAction.h]
190  * key in the KVP frame of the split.
191  */
192  {
193  Account *tRoot;
194  GList *al;
195 
196  tRoot = gnc_book_get_template_root( gnc_get_current_book() );
197  al = gnc_account_get_descendants( tRoot );
198  xaccQueryAddAccountMatch( start_q, al, QOF_GUID_MATCH_NONE, QOF_QUERY_AND );
199  g_list_free (al);
200  al = NULL;
201  tRoot = NULL;
202  }
203 
204  ftd->q = start_q; /* save this to destroy it later */
205  }
206 
207  ftd->sw = gnc_search_dialog_create (type, _("Find Transaction"),
208  params, NULL, start_q, show_q,
209  NULL, do_find_cb, NULL,
210  ftd, free_ftd_cb, GNC_PREFS_GROUP_SEARCH, NULL);
211 
212  if (!ftd->sw)
213  {
214  free_ftd_cb (ftd);
215  return NULL;
216  }
217 
218  return ftd->sw;
219 }
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.
const gchar * QofIdType
Definition: qofid.h:85
void qof_query_destroy(QofQuery *q)
GncPluginPage * gnc_plugin_page_register2_new_ledger(GNCLedgerDisplay2 *ledger)
#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)