-
Notifications
You must be signed in to change notification settings - Fork 761
/
dialog-payment.c
592 lines (477 loc) · 16.5 KB
/
dialog-payment.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
* dialog-payment.c -- Dialog for payment entry
* Copyright (C) 2002,2006 Derek Atkins
* Author: Derek Atkins <warlord@MIT.EDU>
* Copyright (c) 2006 David Hampton <hampton@employees.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, contact:
*
* Free Software Foundation Voice: +1-617-542-5942
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
* Boston, MA 02110-1301, USA gnu@gnu.org
*/
#include "config.h"
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "dialog-utils.h"
#include "gnc-component-manager.h"
#include "gnc-ui.h"
#include "gnc-gui-query.h"
#include "gnc-ui-util.h"
#include "qof.h"
#include "gnc-date-edit.h"
#include "gnc-amount-edit.h"
#include "gnc-gtk-utils.h"
#include "gnc-tree-view-account.h"
#include "Transaction.h"
#include "Account.h"
#include "gncInvoice.h"
#include "dialog-payment.h"
#include "business-gnome-utils.h"
#include "dialog-transfer.h"
#define DIALOG_PAYMENT_CUSTOMER_CM_CLASS "customer-payment-dialog"
#define DIALOG_PAYMENT_VENDOR_CM_CLASS "vendor-payment-dialog"
struct _payment_window {
GtkWidget * dialog;
GtkWidget * num_entry;
GtkWidget * memo_entry;
GtkWidget * post_combo;
GtkWidget * owner_choice;
GtkWidget * invoice_choice;
GtkWidget * amount_edit;
GtkWidget * date_edit;
GtkWidget * acct_tree;
gint component_id;
QofBook * book;
GncOwner owner;
GncInvoice * invoice;
GList * acct_types;
GList * acct_commodities;
};
void gnc_payment_ok_cb (GtkWidget *widget, gpointer data);
void gnc_payment_cancel_cb (GtkWidget *widget, gpointer data);
void gnc_payment_window_destroy_cb (GtkWidget *widget, gpointer data);
void gnc_payment_acct_tree_row_activated_cb (GtkWidget *widget, GtkTreePath *path,
GtkTreeViewColumn *column, PaymentWindow *pw);
static void
gnc_payment_window_refresh_handler (GHashTable *changes, gpointer data)
{
PaymentWindow *pw = data;
gnc_fill_account_select_combo (pw->post_combo, pw->book, pw->acct_types, pw->acct_commodities);
}
static void
gnc_payment_window_close_handler (gpointer data)
{
PaymentWindow *pw = data;
if (pw)
gtk_widget_destroy (pw->dialog);
}
static void
gnc_payment_dialog_invoice_changed(PaymentWindow *pw)
{
GNCLot *lot;
gnc_numeric val;
/* Set the payment amount in the dialog */
if (pw->invoice) {
lot = gncInvoiceGetPostedLot (pw->invoice);
val = gnc_numeric_abs (gnc_lot_get_balance (lot));
} else {
val = gnc_numeric_zero();
}
gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT(pw->amount_edit), val);
}
static void
gnc_payment_dialog_owner_changed(PaymentWindow *pw)
{
Account *last_acct=NULL;
GUID *guid=NULL;
KvpValue* value;
KvpFrame* slots;
/* If the owner changed, the invoice selection is invalid */
pw->invoice = NULL;
gnc_invoice_set_owner(pw->invoice_choice, &pw->owner);
/* note that set_owner implies ...set_invoice(...,NULL); */
/* in case we don't get the callback */
gnc_payment_dialog_invoice_changed(pw);
/* XXX: We should set the sensitive flag on the invoice_choice
* based on whether 'owner' is NULL or not...
*/
/* Now handle the account tree */
slots = gncOwnerGetSlots(&pw->owner);
if (slots)
{
value = kvp_frame_get_slot_path(slots, "payment", "last_acct", NULL);
if (value)
{
guid = kvp_value_get_guid(value);
}
}
/* refresh the post and acc available accounts, but cleanup first */
if (pw->acct_types)
{
g_list_free(pw->acct_types);
pw->acct_types = NULL;
}
if (pw->acct_commodities)
{
g_list_free(pw->acct_commodities);
pw->acct_commodities = NULL;
}
pw->acct_types = gnc_business_account_types(&pw->owner);
pw->acct_commodities = gnc_business_commodities (&pw->owner);
gnc_fill_account_select_combo (pw->post_combo, pw->book, pw->acct_types, pw->acct_commodities);
if (guid)
{
last_acct = xaccAccountLookup(guid, pw->book);
}
/* Set the last-used transfer account */
if (last_acct) {
gnc_tree_view_account_set_selected_account(GNC_TREE_VIEW_ACCOUNT(pw->acct_tree),
last_acct);
}
}
static void
gnc_payment_dialog_remember_account(PaymentWindow *pw, Account *acc)
{
KvpValue* value;
KvpFrame* slots = gncOwnerGetSlots(&pw->owner);
if (!acc) return;
if (!slots) return;
value = kvp_value_new_guid(xaccAccountGetGUID(acc));
if (!value) return;
kvp_frame_set_slot_path(slots, value, "payment", "last_acct", NULL);
kvp_value_delete(value);
/* XXX: FIXME: Need a commit_edit here to save the data! */
}
static void
gnc_payment_set_owner (PaymentWindow *pw, GncOwner *owner)
{
gnc_owner_set_owner (pw->owner_choice, owner);
gnc_payment_dialog_owner_changed(pw);
}
static int
gnc_payment_dialog_owner_changed_cb (GtkWidget *widget, gpointer data)
{
PaymentWindow *pw = data;
GncOwner owner;
if (!pw) return FALSE;
gncOwnerCopy (&(pw->owner), &owner);
gnc_owner_get_owner (pw->owner_choice, &owner);
/* If this owner really changed, then reset ourselves */
if (!gncOwnerEqual (&owner, &(pw->owner))) {
gncOwnerCopy (&owner, &(pw->owner));
gnc_payment_dialog_owner_changed(pw);
}
return FALSE;
}
static int
gnc_payment_dialog_invoice_changed_cb (GtkWidget *widget, gpointer data)
{
PaymentWindow *pw = data;
GncInvoice *invoice;
if (!pw) return FALSE;
invoice = gnc_invoice_get_invoice (pw->invoice_choice);
/* If this invoice really changed, then reset ourselves */
if (invoice != pw->invoice) {
pw->invoice = invoice;
gnc_payment_dialog_invoice_changed(pw);
}
return FALSE;
}
void
gnc_payment_ok_cb (GtkWidget *widget, gpointer data)
{
PaymentWindow *pw = data;
const char *text;
Account *post, *acc;
gnc_numeric amount;
if (!pw)
return;
/* Verify the amount is non-zero */
amount = gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT (pw->amount_edit));
if (gnc_numeric_check (amount) || !gnc_numeric_positive_p (amount)) {
text = _("You must enter the amount of the payment. "
"The payment amount must be greater than zero.");
gnc_error_dialog (pw->dialog, "%s", text);
return;
}
/* Verify the user has selected an owner */
gnc_owner_get_owner (pw->owner_choice, &(pw->owner));
if (pw->owner.owner.undefined == NULL) {
text = _("You must select a company for payment processing.");
gnc_error_dialog (pw->dialog, "%s", text);
return;
}
/* Verify the user has selected a transfer account */
acc = gnc_tree_view_account_get_selected_account (GNC_TREE_VIEW_ACCOUNT(pw->acct_tree));
if (!acc) {
text = _("You must select a transfer account from the account tree.");
gnc_error_dialog (pw->dialog, "%s", text);
return;
}
/* Verify the "post" account */
text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(pw->post_combo));
if (!text || safe_strcmp (text, "") == 0) {
text = _("You must enter an account name for posting.");
gnc_error_dialog (pw->dialog, "%s", text);
return;
}
post = gnc_account_lookup_by_full_name (gnc_book_get_root_account (pw->book), text);
if (!post) {
char *msg = g_strdup_printf (
_("Your selected post account, %s, does not exist"),
text);
gnc_error_dialog (pw->dialog, "%s", msg);
g_free (msg);
return;
}
/* Ok, now post the damn thing */
gnc_suspend_gui_refresh ();
{
const char *memo, *num;
Timespec date;
gnc_numeric exch = gnc_numeric_create(1,1); //default to "one to one" rate
/* Obtain all our ancillary information */
memo = gtk_entry_get_text (GTK_ENTRY (pw->memo_entry));
num = gtk_entry_get_text (GTK_ENTRY (pw->num_entry));
date = gnc_date_edit_get_date_ts (GNC_DATE_EDIT (pw->date_edit));
/* If the 'acc' account and the post account don't have the same
currency, we need to get the user to specify the exchange rate */
if (!gnc_commodity_equal(xaccAccountGetCommodity(acc), xaccAccountGetCommodity(post)))
{
XferDialog* xfer;
text = _("The transfer and post accounts are associated with different currencies. Please specify the conversion rate.");
xfer = gnc_xfer_dialog(pw->dialog, acc);
gnc_info_dialog(pw->dialog, "%s", text);
gnc_xfer_dialog_select_to_account(xfer,post);
gnc_xfer_dialog_set_amount(xfer, amount);
/* All we want is the exchange rate so prevent the user from thinking
it makes sense to mess with other stuff */
gnc_xfer_dialog_set_from_show_button_active(xfer, FALSE);
gnc_xfer_dialog_set_to_show_button_active(xfer, FALSE);
gnc_xfer_dialog_hide_from_account_tree(xfer);
gnc_xfer_dialog_hide_to_account_tree(xfer);
gnc_xfer_dialog_is_exchange_dialog(xfer, &exch);
gnc_xfer_dialog_run_until_done(xfer);
}
/* Now apply the payment */
gncOwnerApplyPayment (&pw->owner, pw->invoice,
post, acc, amount, exch, date, memo, num);
}
gnc_resume_gui_refresh ();
/* Save the transfer account, acc */
gnc_payment_dialog_remember_account(pw, acc);
gnc_ui_payment_window_destroy (pw);
}
void
gnc_payment_cancel_cb (GtkWidget *widget, gpointer data)
{
PaymentWindow *pw = data;
gnc_ui_payment_window_destroy (pw);
}
void
gnc_payment_window_destroy_cb (GtkWidget *widget, gpointer data)
{
PaymentWindow *pw = data;
if (!pw) return;
gnc_unregister_gui_component (pw->component_id);
g_list_free (pw->acct_types);
g_list_free (pw->acct_commodities);
g_free (pw);
}
void
gnc_payment_acct_tree_row_activated_cb (GtkWidget *widget, GtkTreePath *path,
GtkTreeViewColumn *column, PaymentWindow *pw)
{
GtkTreeView *view;
GtkTreeModel *model;
GtkTreeIter iter;
g_return_if_fail(widget);
view = GTK_TREE_VIEW(widget);
model = gtk_tree_view_get_model(view);
if (gtk_tree_model_get_iter(model, &iter, path))
{
if (gtk_tree_model_iter_has_child(model, &iter))
{
/* There are children,
* just expand or collapse the row. */
if (gtk_tree_view_row_expanded(view, path))
gtk_tree_view_collapse_row(view, path);
else
gtk_tree_view_expand_row(view, path, FALSE);
}
else
/* It's an account without any children, so click the Ok button. */
gnc_payment_ok_cb(widget, pw);
}
}
/* Select the list of accounts to show in the tree */
static void
gnc_payment_set_account_types (GncTreeViewAccount *tree)
{
AccountViewInfo avi;
int i;
gnc_tree_view_account_get_view_info (tree, &avi);
for (i = 0; i < NUM_ACCOUNT_TYPES; i++)
switch (i) {
case ACCT_TYPE_BANK:
case ACCT_TYPE_CASH:
case ACCT_TYPE_CREDIT:
case ACCT_TYPE_ASSET:
case ACCT_TYPE_LIABILITY:
avi.include_type[i] = TRUE;
break;
default:
avi.include_type[i] = FALSE;
break;
}
gnc_tree_view_account_set_view_info (tree, &avi);
}
static gboolean
find_handler (gpointer find_data, gpointer user_data)
{
PaymentWindow *pw = user_data;
return (pw != NULL);
}
static PaymentWindow *
new_payment_window (GncOwner *owner, QofBook *book, GncInvoice *invoice)
{
PaymentWindow *pw;
GladeXML *xml;
GtkWidget *box, *label;
char * cm_class = (gncOwnerGetType (owner) == GNC_OWNER_CUSTOMER ?
DIALOG_PAYMENT_CUSTOMER_CM_CLASS :
DIALOG_PAYMENT_VENDOR_CM_CLASS);
/*
* Find an existing payment window. If found, bring it to
* the front. If we have an actual owner, then set it in
* the window.
*/
pw = gnc_find_first_gui_component (cm_class, find_handler, NULL);
if (pw) {
if (owner->owner.undefined)
gnc_payment_set_owner (pw, owner);
gtk_window_present (GTK_WINDOW(pw->dialog));
return(pw);
}
/* Ok, we need a new window */
pw = g_new0 (PaymentWindow, 1);
pw->book = book;
gncOwnerCopy (owner, &(pw->owner));
/* Compute the post-to account types */
pw->acct_types = gnc_business_account_types (owner);
pw->acct_commodities = gnc_business_commodities (owner);
/* Open and read the XML */
xml = gnc_glade_xml_new ("payment.glade", "Payment Dialog");
pw->dialog = glade_xml_get_widget (xml, "Payment Dialog");
/* Grab the widgets and build the dialog */
pw->num_entry = glade_xml_get_widget (xml, "num_entry");
pw->memo_entry = glade_xml_get_widget (xml, "memo_entry");
pw->post_combo = glade_xml_get_widget (xml, "post_combo");
gnc_cbe_require_list_item(GTK_COMBO_BOX_ENTRY(pw->post_combo));
label = glade_xml_get_widget (xml, "owner_label");
box = glade_xml_get_widget (xml, "owner_box");
pw->owner_choice = gnc_owner_select_create (label, box, book, owner);
label = glade_xml_get_widget (xml, "invoice_label");
box = glade_xml_get_widget (xml, "invoice_box");
pw->invoice_choice = gnc_invoice_select_create (box, book, owner, invoice, label);
box = glade_xml_get_widget (xml, "amount_box");
pw->amount_edit = gnc_amount_edit_new ();
gtk_box_pack_start (GTK_BOX (box), pw->amount_edit, TRUE, TRUE, 0);
gnc_amount_edit_set_evaluate_on_enter (GNC_AMOUNT_EDIT (pw->amount_edit),
TRUE);
gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT (pw->amount_edit), gnc_numeric_zero());
box = glade_xml_get_widget (xml, "date_box");
pw->date_edit = gnc_date_edit_new (time(NULL), FALSE, FALSE);
gtk_box_pack_start (GTK_BOX (box), pw->date_edit, TRUE, TRUE, 0);
box = glade_xml_get_widget (xml, "acct_window");
pw->acct_tree = GTK_WIDGET(gnc_tree_view_account_new (FALSE));
gtk_container_add (GTK_CONTAINER (box), pw->acct_tree);
gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(pw->acct_tree), FALSE);
gnc_payment_set_account_types (GNC_TREE_VIEW_ACCOUNT (pw->acct_tree));
/* Set the dialog for the 'new' owner */
gnc_payment_dialog_owner_changed(pw);
/* Set the dialog for the 'new' invoice */
pw->invoice = invoice;
gnc_payment_dialog_invoice_changed(pw);
/* Setup signals */
glade_xml_signal_autoconnect_full( xml,
gnc_glade_autoconnect_full_func,
pw);
g_signal_connect (G_OBJECT (pw->owner_choice), "changed",
G_CALLBACK (gnc_payment_dialog_owner_changed_cb), pw);
g_signal_connect (G_OBJECT (pw->invoice_choice), "changed",
G_CALLBACK (gnc_payment_dialog_invoice_changed_cb), pw);
g_signal_connect (G_OBJECT (pw->acct_tree), "row-activated",
G_CALLBACK (gnc_payment_acct_tree_row_activated_cb), pw);
/* Register with the component manager */
pw->component_id =
gnc_register_gui_component (cm_class,
gnc_payment_window_refresh_handler,
gnc_payment_window_close_handler,
pw);
/* Watch for any new or changed accounts */
gnc_gui_component_watch_entity_type (pw->component_id,
GNC_ID_ACCOUNT,
QOF_EVENT_CREATE | QOF_EVENT_MODIFY |
QOF_EVENT_DESTROY);
/* Fill in the post_combo and account_tree widgets */
gnc_fill_account_select_combo (pw->post_combo, pw->book, pw->acct_types, pw->acct_commodities);
/* Show it all */
gtk_widget_show_all (pw->dialog);
/* Warn the user if they have no valid post-to accounts */
{
const gchar *text;
const char *acct_type;
text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(pw->post_combo));
if (!text || safe_strcmp (text, "") == 0) {
/* XXX: I know there's only one type here */
acct_type = xaccAccountGetTypeStr(GPOINTER_TO_INT(pw->acct_types->data));
gnc_warning_dialog(pw->dialog,
_("You have no valid \"Post To\" accounts. "
"Please create an account of type \"%s\" "
"before you continue to process this payment. "
"Perhaps you want to create an Invoice or "
"Bill first?"),
acct_type);
}
}
return pw;
}
void
gnc_ui_payment_window_destroy (PaymentWindow *pw)
{
if (!pw) return;
gnc_close_gui_component (pw->component_id);
}
PaymentWindow *
gnc_ui_payment_new_with_invoice (GncOwner *owner, QofBook *book,
GncInvoice *invoice)
{
GncOwner owner_def;
if (!book) return NULL;
if (owner) {
/* Figure out the company */
owner = gncOwnerGetEndOwner (owner);
} else {
gncOwnerInitCustomer (&owner_def, NULL);
owner = &owner_def;
}
return new_payment_window (owner, book, invoice);
}
PaymentWindow *
gnc_ui_payment_new (GncOwner *owner, QofBook *book)
{
return gnc_ui_payment_new_with_invoice (owner, book, NULL);
}