GnuCash  5.6-150-g038405b370+
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
window-autoclear.c
1 /********************************************************************\
2  * window-autoclear.c -- the autoclear window *
3  * Copyright (C) 2010 Cristian KLEIN *
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 
28 #include "dialog-utils.h"
29 #include "gnc-amount-edit.h"
30 #include "gnc-event.h"
31 #include "gnc-gnome-utils.h"
32 #include "gnc-main-window.h"
34 #include "gnc-ui.h"
35 #include "gnc-autoclear.h"
36 #include "window-autoclear.h"
37 
38 #define WINDOW_AUTOCLEAR_CM_CLASS "window-autoclear"
39 
40 __attribute__((unused)) static QofLogModule log_module = GNC_MOD_GUI;
41 
43 struct _AutoClearWindow
44 {
45  Account *account; /* The account that we are auto-clearing */
46 
47  gint component_id; /* id of component */
48 
49  GtkWidget *window; /* The auto-clear window */
50  GNCAmountEdit *end_value;/* The ending value */
51  GtkWidget *ok_button;
52  GtkWidget *cancel_button;
53  GtkWidget *show_cleared_splits_button;
54  GtkLabel *status_label;
55 };
56 
58 void gnc_autoclear_window_ok_cb (GtkWidget *widget,
59  AutoClearWindow *data);
60 void gnc_autoclear_window_cancel_cb (GtkWidget *widget,
61  AutoClearWindow *data);
62 
63 /********************************************************************\
64  * gnc_ui_autoclear_window_raise *
65  * shows and raises an auto-clear window *
66  * *
67  * Args: autoClearData - the auto-clear window structure *
68 \********************************************************************/
69 void
70 gnc_ui_autoclear_window_raise(AutoClearWindow * autoClearData)
71 {
72  if (autoClearData == NULL)
73  return;
74 
75  if (autoClearData->window == NULL)
76  return;
77 
78  gtk_window_present(GTK_WINDOW(autoClearData->window));
79 }
80 
81 static char *
82 gnc_autoclear_make_window_name(Account *account)
83 {
84  char *fullname;
85  char *title;
86 
87  fullname = gnc_account_get_full_name(account);
88  title = g_strconcat(fullname, " - ", _("Auto-clear"), NULL);
89 
90  g_free(fullname);
91 
92  return title;
93 }
94 
95 static void
96 show_cleared_splits (GList *splits)
97 {
98  GNCLedgerDisplay *ledger;
99  GncPluginPage *page;
100  Query *book_query, *guid_query;
101 
102  book_query = qof_query_create_for (GNC_ID_SPLIT);
103  guid_query = qof_query_create_for (GNC_ID_SPLIT);
104  qof_query_set_book (book_query, gnc_get_current_book ());
105 
106  for (GList *iter = splits; iter; iter = iter->next)
107  {
108  GncGUID guid = iter->data ? *xaccSplitGetGUID (iter->data) : *guid_null() ;
109  xaccQueryAddGUIDMatch (guid_query, &guid, GNC_ID_SPLIT, QOF_QUERY_OR);
110  }
111  book_query = qof_query_merge (book_query, guid_query, QOF_QUERY_AND);
112  ledger = gnc_ledger_display_query (book_query, SEARCH_LEDGER, REG_STYLE_JOURNAL);
114  page = gnc_plugin_page_register_new_ledger (ledger);
115  main_window_update_page_name (page, _("Cleared Transactions"));
116  gnc_main_window_open_page (NULL, page);
117  qof_query_destroy (book_query);
118  qof_query_destroy (guid_query);
119 }
120 
121 void
122 gnc_autoclear_window_ok_cb (GtkWidget *widget,
123  AutoClearWindow *data)
124 {
125  GList *toclear_list = NULL;
126  gnc_numeric toclear_value = gnc_numeric_error (GNC_ERROR_ARG);
127  GError* error = NULL;
128 
129  g_return_if_fail (widget && data);
130 
131  /* test for valid value */
132  if (gnc_amount_edit_evaluate (GNC_AMOUNT_EDIT(data->end_value), &error))
133  {
134  toclear_value = gnc_amount_edit_get_amount(data->end_value);
135 
136  if (gnc_reverse_balance(data->account))
137  toclear_value = gnc_numeric_neg (toclear_value);
138 
139  toclear_value = gnc_numeric_convert
140  (toclear_value, xaccAccountGetCommoditySCU(data->account), GNC_HOW_RND_ROUND);
141 
142  gnc_autoclear_get_splits (data->account, toclear_value, INT64_MAX,
143  &toclear_list, &error, data->status_label);
144  }
145 
146  if (error && error->message)
147  {
148  GtkWidget *entry = gnc_amount_edit_gtk_entry (GNC_AMOUNT_EDIT(data->end_value));
149  gtk_label_set_text (data->status_label, error->message);
150  if (gnc_numeric_check (toclear_value) == 0)
151  gnc_amount_edit_set_amount (data->end_value, toclear_value);
152  gtk_widget_grab_focus (GTK_WIDGET(entry));
153  gnc_amount_edit_select_region (GNC_AMOUNT_EDIT(data->end_value), 0, -1);
154  g_error_free (error);
155  }
156  else
157  {
158  xaccAccountBeginEdit (data->account);
159  for (GList *node = toclear_list; node; node = node->next)
160  xaccSplitSetReconcile (node->data, CREC);
161  xaccAccountCommitEdit (data->account);
162 
163  if (gtk_toggle_button_get_active
164  (GTK_TOGGLE_BUTTON (data->show_cleared_splits_button)))
165  show_cleared_splits (toclear_list);
166 
167  g_list_free (toclear_list);
168 
169  /* Close window */
170  gtk_widget_destroy (data->window);
171  g_free (data);
172  }
173 }
174 
175 void
176 gnc_autoclear_window_cancel_cb (GtkWidget *widget,
177  AutoClearWindow *data)
178 {
179  /* Close window */
180  gtk_widget_destroy(data->window);
181  g_free(data);
182 }
183 
184 static void clear_status_label_cb (GtkEditable *editable, AutoClearWindow *data)
185 {
186  gtk_label_set_text (data->status_label, "");
187 }
188 
189 
190 /********************************************************************\
191  * autoClearWindow *
192  * opens up the window to auto-clear an account *
193  * *
194  * Args: parent - the parent of this window *
195  * account - the account to auto-clear *
196  * Return: autoClearData - the instance of this AutoClearWindow *
197 \********************************************************************/
198 AutoClearWindow *
199 autoClearWindow (GtkWidget *parent, Account *account)
200 {
201  GtkBox *box;
202  GtkWidget *label;
203  GtkBuilder *builder;
204  AutoClearWindow *data;
205  char *title;
206  gnc_numeric after;
207  GNCPrintAmountInfo print_info;
208  gnc_commodity *currency;
209 
210  data = g_new0 (AutoClearWindow, 1);
211  data->account = account;
212 
213  /* Create the dialog box */
214  builder = gtk_builder_new();
215  gnc_builder_add_from_file (builder, "window-autoclear.glade", "auto_clear_start_dialog");
216  data->window = GTK_WIDGET(gtk_builder_get_object (builder, "auto_clear_start_dialog"));
217  title = gnc_autoclear_make_window_name (account);
218  gtk_window_set_title(GTK_WINDOW(data->window), title);
219  g_free (title);
220 
221  // Set the name for this dialog so it can be easily manipulated with css
222  gtk_widget_set_name (GTK_WIDGET(data->window), "gnc-id-auto-clear");
223 
224  data->show_cleared_splits_button =
225  GTK_WIDGET (gtk_builder_get_object (builder, "show_cleared_splits_button"));
226 
227  /* Add amount edit box */
228  data->end_value = GNC_AMOUNT_EDIT(gnc_amount_edit_new());
229 
230  currency = xaccAccountGetCommodity (account);
231  print_info = gnc_commodity_print_info (currency, FALSE);
232  gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT(data->end_value), print_info);
233  gnc_amount_edit_set_fraction (GNC_AMOUNT_EDIT(data->end_value),
234  gnc_commodity_get_fraction (currency));
235 
236  g_signal_connect(GTK_WIDGET(data->end_value), "activate",
237  G_CALLBACK(gnc_autoclear_window_ok_cb), data);
238 
239  box = GTK_BOX(gtk_builder_get_object (builder, "end_value_box"));
240  gtk_box_pack_start(box, GTK_WIDGET(data->end_value), TRUE, TRUE, 0);
241 
242  label = GTK_WIDGET(gtk_builder_get_object (builder, "end_label"));
243  gnc_amount_edit_make_mnemonic_target (GNC_AMOUNT_EDIT(data->end_value), label);
244 
245  /* pre-fill with current balance */
246  after = xaccAccountGetClearedBalance (data->account);
247  if (gnc_reverse_balance(data->account))
248  after = gnc_numeric_neg(after);
249  gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT (data->end_value), after);
250  gtk_widget_grab_focus(GTK_WIDGET(data->end_value));
251  gnc_amount_edit_select_region (GNC_AMOUNT_EDIT (data->end_value), 0, -1);
252 
253  data->status_label = GTK_LABEL(gtk_builder_get_object (builder, "status_label"));
254 
255  g_signal_connect (GTK_WIDGET(data->end_value), "changed",
256  G_CALLBACK(clear_status_label_cb), data);
257 
258  if (parent != NULL)
259  gtk_window_set_transient_for (GTK_WINDOW (data->window), GTK_WINDOW (parent));
260 
261  gtk_builder_connect_signals(builder, data);
262  g_object_unref(G_OBJECT(builder));
263 
264  return data;
265 }
266 
void gnc_ledger_display_refresh(GNCLedgerDisplay *ld)
redisplay/redraw only the indicated window.
int gnc_commodity_get_fraction(const gnc_commodity *cm)
Retrieve the fraction for the specified commodity.
The instance data structure for a content plugin.
int xaccAccountGetCommoditySCU(const Account *acc)
Return the SCU for the account.
Definition: Account.cpp:2716
gnc_numeric gnc_numeric_neg(gnc_numeric a)
Returns a newly created gnc_numeric that is the negative of the given gnc_numeric value...
STRUCTS.
void xaccSplitSetReconcile(Split *split, char recn)
Set the reconcile flag.
gnc_numeric xaccAccountGetClearedBalance(const Account *acc)
Get the current balance of the account, only including cleared transactions.
Definition: Account.cpp:3437
void gnc_main_window_open_page(GncMainWindow *window, GncPluginPage *page)
Display a data plugin page in a window.
Functions for adding content to a window.
GncPluginPage * gnc_plugin_page_register_new_ledger(GNCLedgerDisplay *ledger)
Create a new "register" plugin page, given a pointer to an already created ledger.
gchar * gnc_account_get_full_name(const Account *account)
The gnc_account_get_full_name routine returns the fully qualified name of the account using the given...
Definition: Account.cpp:3275
Functions providing a register page for the GnuCash UI.
gnc_numeric gnc_numeric_convert(gnc_numeric n, gint64 denom, gint how)
Change the denominator of a gnc_numeric value to the specified denominator under standard arguments &#39;...
void qof_query_destroy(QofQuery *query)
Frees the resources associate with a Query object.
gnc_numeric gnc_numeric_error(GNCNumericErrorCode error_code)
Create a gnc_numeric object that signals the error condition noted by error_code, rather than a numbe...
void qof_query_set_book(QofQuery *query, QofBook *book)
Set the book to be searched.
Argument is not a valid number.
Definition: gnc-numeric.h:224
QofQuery * qof_query_merge(QofQuery *q1, QofQuery *q2, QofQueryOp op)
Combine two queries together using the Boolean set (logical) operator &#39;op&#39;.
Definition: qofquery.cpp:1129
Gnome specific utility functions.
Additional event handling code.
#define xaccSplitGetGUID(X)
Definition: Split.h:552
#define CREC
The Split has been cleared.
Definition: Split.h:73
void xaccAccountBeginEdit(Account *acc)
The xaccAccountBeginEdit() subroutine is the first phase of a two-phase-commit wrapper for account up...
Definition: Account.cpp:1477
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account&#39;s commodity.
Definition: Account.cpp:3371
const GncGUID * guid_null(void)
Returns a GncGUID which is guaranteed to never reference any entity.
Definition: guid.cpp:130
GNCLedgerDisplay * gnc_ledger_display_query(Query *query, SplitRegisterType type, SplitRegisterStyle style)
display a general ledger for an arbitrary query
GNCNumericErrorCode gnc_numeric_check(gnc_numeric a)
Check for error signal in value.
Use unbiased ("banker&#39;s") rounding.
Definition: gnc-numeric.h:172
The type used to store guids in C.
Definition: guid.h:75
void main_window_update_page_name(GncPluginPage *page, const gchar *name_in)
Update the name of the page in the main window.
A Query.
Definition: qofquery.cpp:74
void xaccAccountCommitEdit(Account *acc)
ThexaccAccountCommitEdit() subroutine is the second phase of a two-phase-commit wrapper for account u...
Definition: Account.cpp:1518