1 /********************************************************************\
2  * dialog-find-transactions2.c : locate transactions and show them   *
3  * Copyright (C) 2000 Bill Gribble <grib@billgribble.com>           *
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       gnu@gnu.org                   *
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 #include "dialog-utils.h"
39 
40 #define GNC_PREFS_GROUP_SEARCH "dialogs.find"
41 
42 struct _ftd_data
43 {
44     QofQuery        *q;
45     QofQuery        *ledger_q;
46     GNCSearchWindow *sw;
47     GtkWindow       *parent;
48 };
49 
50 static void
do_find_cb(QofQuery * query,gpointer user_data,gpointer * result)51 do_find_cb (QofQuery *query, gpointer user_data, gpointer *result)
52 {
53     struct _ftd_data *ftd = user_data;
54     GNCLedgerDisplay2 *ledger;
55     gboolean new_ledger = FALSE;
56     GncPluginPage *page;
57 
58     ledger = gnc_ledger_display2_find_by_query (ftd->ledger_q);
59 
60     if (!ledger)
61     {
62         new_ledger = TRUE;
63         ledger = gnc_ledger_display2_query (query, SEARCH_LEDGER2,
64                                            REG2_STYLE_JOURNAL);
65     }
66     else
67         gnc_ledger_display2_set_query (ledger, query);
68 
69     gnc_ledger_display2_refresh (ledger);
70 
71     if (new_ledger)
72     {
73         page = gnc_plugin_page_register2_new_ledger (ledger);
74         gnc_main_window_open_page (GNC_MAIN_WINDOW(ftd->parent), page);
75     }
76 
77     qof_query_destroy (ftd->q);
78 
79     gnc_search_dialog_destroy (ftd->sw);
80 }
81 
82 static void
free_ftd_cb(gpointer user_data)83 free_ftd_cb (gpointer user_data)
84 {
85     struct _ftd_data *ftd = user_data;
86 
87     if (!ftd)
88         return;
89 
90     g_free (ftd);
91 }
92 
93 GNCSearchWindow *
gnc_ui_find_transactions_dialog_create2(GNCLedgerDisplay2 * orig_ledg)94 gnc_ui_find_transactions_dialog_create2 (GNCLedgerDisplay2 * orig_ledg)
95 {
96     QofIdType type = GNC_ID_SPLIT;
97     struct _ftd_data *ftd;
98     static GList *params = NULL;
99     QofQuery *start_q, *show_q = NULL;
100     gboolean num_action =
101                 qof_book_use_split_action_for_num_field(gnc_get_current_book());
102     GtkWindow *parent = GTK_WINDOW(gnc_ledger_display2_get_parent (orig_ledg));
103 
104     /* Build parameter list in reverse order */
105     if (params == NULL)
106     {
107         params = gnc_search_param_prepend (params, N_("All Accounts"),
108                                            ACCOUNT_MATCH_ALL_TYPE,
109                                            type, SPLIT_TRANS, TRANS_SPLITLIST,
110                                            SPLIT_ACCOUNT_GUID, NULL);
111         params = gnc_search_param_prepend (params, N_("Account"), GNC_ID_ACCOUNT,
112                                            type, SPLIT_ACCOUNT, QOF_PARAM_GUID,
113                                            NULL);
114         params = gnc_search_param_prepend (params, N_("Balanced"), NULL,
115                                            type, SPLIT_TRANS, TRANS_IS_BALANCED,
116                                            NULL);
117         params = gnc_search_param_prepend (params, N_("Closing Entries"), NULL,
118                                            type, SPLIT_TRANS, TRANS_IS_CLOSING,
119                                            NULL);
120         params = gnc_search_param_prepend (params, N_("Reconcile"), RECONCILED_MATCH_TYPE,
121                                            type, SPLIT_RECONCILE, NULL);
122         params = gnc_search_param_prepend (params, N_("Share Price"), NULL,
123                                            type, SPLIT_SHARE_PRICE, NULL);
124         params = gnc_search_param_prepend (params, N_("Amount"), NULL,
125                                            type, SPLIT_AMOUNT, NULL);
126         params = gnc_search_param_prepend (params, N_("Value"), NULL,
127                                            type, SPLIT_VALUE, NULL);
128         params = gnc_search_param_prepend (params, N_("Date Posted"), NULL,
129                                            type, SPLIT_TRANS, TRANS_DATE_POSTED,
130                                            NULL);
131         params = gnc_search_param_prepend (params, (num_action
132                                                     ? N_("Number/Action")
133                                                     : N_("Action")), NULL,
134                                            type, SPLIT_ACTION, NULL);
135         params = gnc_search_param_prepend (params, (num_action
136                                                     ? N_("Transaction Number")
137                                                     : N_("Number")), NULL,
138                                            type, SPLIT_TRANS, TRANS_NUM, NULL);
139         {
140             GList *params2 = NULL;
141             params2 = gnc_search_param_prepend (params2, "", NULL,
142                                                type, SPLIT_MEMO, NULL);
143             params2 = gnc_search_param_prepend (params2, "", NULL,
144                                                type, SPLIT_TRANS, TRANS_DESCRIPTION,
145                                                NULL);
146             params2 = gnc_search_param_prepend (params2, "", NULL,
147                                                type, SPLIT_TRANS, TRANS_NOTES, NULL);
148             params = gnc_search_param_prepend_compound (params,
149                                                         N_("Description, Notes, or Memo"),
150                                                         params2,
151                                                         GTK_JUSTIFY_LEFT, SEARCH_PARAM_ANY);
152         }
153         params = gnc_search_param_prepend (params, N_("Memo"), NULL,
154                                            type, SPLIT_MEMO, NULL);
155         params = gnc_search_param_prepend (params, N_("Notes"), NULL,
156                                            type, SPLIT_TRANS, TRANS_NOTES, NULL);
157         params = gnc_search_param_prepend (params, N_("Description"), NULL,
158                                            type, SPLIT_TRANS, TRANS_DESCRIPTION,
159                                            NULL);
160     }
161     else
162     {
163         GList *l;
164         for (l = params; l; l = l->next)
165         {
166             GNCSearchParam *param = l->data;
167 
168             if (num_action)
169             {
170                 if (strcmp (param->title, N_("Action")) == 0)
171                     gnc_search_param_set_title (param, N_("Number/Action"));
172                 if (strcmp (param->title, N_("Number")) == 0)
173                     gnc_search_param_set_title (param, N_("Transaction Number"));
174             }
175             else
176             {
177                 if (strcmp (param->title, N_("Number/Action")) == 0)
178                     gnc_search_param_set_title (param, N_("Action"));
179                 if (strcmp (param->title, N_("Transaction Number")) == 0)
180                     gnc_search_param_set_title (param, N_("Number"));
181             }
182         }
183     }
184 
185     ftd = g_new0 (struct _ftd_data, 1);
186 
187     if (orig_ledg)
188     {
189         ftd->ledger_q = gnc_ledger_display2_get_query (orig_ledg);
190         start_q = show_q = qof_query_copy (ftd->ledger_q);
191     }
192     else
193     {
194         start_q = qof_query_create ();
195         qof_query_set_book (start_q, gnc_get_current_book ());
196 
197         /* In lieu of not "mis-using" some portion of the infrastructure by writing
198          * a bunch of new code, we just filter out the accounts of the template
199          * transactions.  While these are in a separate Account trees just for this
200          * reason, the query engine makes no distinction between Account trees.
201          * See Gnome Bug 86302.
202          * 	-- jsled
203          *
204          * copied from gnc-ledger-display2.c:gnc_ledger_display2_gl()  -- warlord
205          *
206          * <jsled> Alternatively, you could look for a GNC_SX_ACCOUNT [SchedAction.h]
207          * key in the KVP frame of the split.
208          */
209         {
210             Account *tRoot;
211             GList *al;
212 
213             tRoot = gnc_book_get_template_root( gnc_get_current_book() );
214             al = gnc_account_get_descendants( tRoot );
215 
216             if (g_list_length(al) != 0)
217                 xaccQueryAddAccountMatch( start_q, al, QOF_GUID_MATCH_NONE, QOF_QUERY_AND );
218 
219             g_list_free (al);
220             al = NULL;
221             tRoot = NULL;
222         }
223 
224         ftd->q = start_q;		/* save this to destroy it later */
225     }
226 
227     ftd->parent = parent;
228 
229     ftd->sw = gnc_search_dialog_create (parent, type, _("Find Transaction"),
230                                         params, NULL, start_q, show_q,
231                                         NULL, do_find_cb, NULL,
232                                         ftd, free_ftd_cb, GNC_PREFS_GROUP_SEARCH, NULL,
233                                         "gnc-class-transactions");
234     if (!ftd->sw)
235     {
236         free_ftd_cb (ftd);
237         return NULL;
238     }
239 
240     return ftd->sw;
241 }
242