/
dialog-payment.c
903 lines (757 loc) · 27 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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
/*
* 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 "gncOwner.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;
Transaction *pre_existing_txn;
};
void gnc_ui_payment_window_set_num (PaymentWindow *pw, const char* num)
{
g_assert(pw);
gtk_entry_set_text(GTK_ENTRY (pw->num_entry), num);
}
void gnc_ui_payment_window_set_memo (PaymentWindow *pw, const char* memo)
{
g_assert(pw);
gtk_entry_set_text(GTK_ENTRY (pw->memo_entry), memo);
}
void gnc_ui_payment_window_set_date (PaymentWindow *pw, const GDate *date)
{
g_assert(pw);
g_assert(date);
gnc_date_edit_set_gdate (GNC_DATE_EDIT (pw->date_edit), date);
}
void gnc_ui_payment_window_set_amount (PaymentWindow *pw, gnc_numeric amount)
{
g_assert(pw);
gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT(pw->amount_edit), amount);
}
void gnc_ui_payment_window_set_postaccount (PaymentWindow *pw, const Account* account)
{
g_assert(pw);
g_assert(account);
{
gchar *acct_string = gnc_account_get_full_name (account);
gnc_cbe_set_by_string(GTK_COMBO_BOX_ENTRY(pw->post_combo), acct_string);
g_free(acct_string);
}
}
void gnc_ui_payment_window_set_xferaccount (PaymentWindow *pw, const Account* account)
{
g_assert(pw);
g_assert(account);
gnc_tree_view_account_set_selected_account(GNC_TREE_VIEW_ACCOUNT(pw->acct_tree),
(Account*)account);
}
static gboolean has_pre_existing_txn(const PaymentWindow* pw)
{
return pw->pre_existing_txn != NULL;
}
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;
// Ignore the amount of the invoice in case this payment is from a
// pre-existing txn
if (has_pre_existing_txn(pw))
return;
/* 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_ui_payment_window_set_amount(pw, val);
}
static void
gnc_payment_dialog_owner_changed(PaymentWindow *pw)
{
Account *last_acct = NULL;
GncGUID *guid = NULL;
KvpValue* value;
KvpFrame* slots;
GncOwner *owner = &pw->owner;
/* 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(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 = gncOwnerGetAccountTypesList(owner);
if (gncOwnerIsValid(owner))
pw->acct_commodities = gncOwnerGetCommoditiesList (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, but only if we didn't
* create this dialog from a pre-existing transaction. */
if (last_acct && !has_pre_existing_txn(pw))
{
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 (!gncOwnerIsValid(&pw->owner))
{
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);
}
if (!has_pre_existing_txn(pw))
{
/* Now apply the payment */
gncOwnerApplyPayment (&pw->owner, pw->invoice,
post, acc, amount, exch, date, memo, num);
}
else
{
// New implementation: Allow the user to have a
// pre-selected transaction
Transaction *txn = pw->pre_existing_txn;
GncOwner *owner = &pw->owner;
Split *xfer_split = xaccTransFindSplitByAccount(txn, acc);
if (xaccTransGetCurrency(txn) != gncOwnerGetCurrency (owner))
{
g_message("Uh oh, mismatching currency/commodity between selected transaction and owner. We fall back to manual creation of a new transaction.");
xfer_split = NULL;
}
if (!xfer_split)
{
g_message("Huh? Asset account not found anymore. Fully deleting old txn and now creating a new one.");
xaccTransBeginEdit (txn);
xaccTransDestroy (txn);
xaccTransCommitEdit (txn);
pw->pre_existing_txn = NULL;
gncOwnerApplyPayment (owner, pw->invoice,
post, acc, amount, exch, date, memo, num);
}
else
{
int i = 0;
xaccTransBeginEdit (txn);
while (i < xaccTransCountSplits(txn))
{
Split *split = xaccTransGetSplit (txn, i);
if (split == xfer_split)
{
++i;
}
else
{
xaccSplitDestroy(split);
}
}
// We have opened the txn for editing, and we have deleted all the other splits.
gncOwnerAssignPaymentTxn (owner, txn,
post, pw->invoice);
xaccTransCommitEdit (txn);
}
}
}
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) // FIXME: Does that mean gncOwnerIsValid(owner->owner)???
gnc_payment_set_owner (pw, owner);
// Reset the setting about the pre-existing TXN
pw->pre_existing_txn = NULL;
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 = gncOwnerGetAccountTypesList (owner);
if (gncOwnerIsValid(owner))
pw->acct_commodities = gncOwnerGetCommoditiesList (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);
if (invoice)
{
Account *postacct = gncInvoiceGetPostedAcc (invoice);
if (postacct)
{
gchar *acct_string = gnc_account_get_full_name (postacct);
gnc_cbe_set_by_string(GTK_COMBO_BOX_ENTRY(pw->post_combo), acct_string);
g_free(acct_string);
}
}
/* 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 (const GncOwner *owner, QofBook *book,
GncInvoice *invoice)
{
GncOwner owner_def;
if (!book) return NULL;
if (owner)
{
/* Figure out the company */
gncOwnerCopy (gncOwnerGetEndOwner (owner), &owner_def);
}
else
{
gncOwnerInitCustomer (&owner_def, NULL);
}
return new_payment_window (&owner_def, book, invoice);
}
PaymentWindow *
gnc_ui_payment_new (GncOwner *owner, QofBook *book)
{
return gnc_ui_payment_new_with_invoice (owner, book, NULL);
}
// ////////////////////////////////////////////////////////////
static gboolean isAssetLiabType(GNCAccountType t)
{
switch (t)
{
case ACCT_TYPE_RECEIVABLE:
case ACCT_TYPE_PAYABLE:
return FALSE;
default:
return (xaccAccountTypesCompatible(ACCT_TYPE_ASSET, t)
|| xaccAccountTypesCompatible(ACCT_TYPE_LIABILITY, t));
}
}
static gboolean isAPARType(GNCAccountType t)
{
switch (t)
{
case ACCT_TYPE_RECEIVABLE:
case ACCT_TYPE_PAYABLE:
return TRUE;
default:
return FALSE;
}
}
static void increment_if_asset_account (gpointer data,
gpointer user_data)
{
int *r = user_data;
const Split *split = data;
const Account *account = xaccSplitGetAccount(split);
if (isAssetLiabType(xaccAccountGetType(account)))
++(*r);
}
static int countAssetAccounts(SplitList* slist)
{
int result = 0;
g_list_foreach(slist, &increment_if_asset_account, &result);
return result;
}
static gint predicate_is_asset_account(gconstpointer a,
gconstpointer user_data)
{
const Split *split = a;
const Account *account = xaccSplitGetAccount(split);
if (isAssetLiabType(xaccAccountGetType(account)))
return 0;
else
return -1;
}
static gint predicate_is_apar_account(gconstpointer a,
gconstpointer user_data)
{
const Split *split = a;
const Account *account = xaccSplitGetAccount(split);
if (isAPARType(xaccAccountGetType(account)))
return 0;
else
return -1;
}
static Split *getFirstAssetAccountSplit(SplitList* slist)
{
GList *r = g_list_find_custom(slist, NULL, &predicate_is_asset_account);
if (r)
return r->data;
else
return NULL;
}
static Split *getFirstAPARAccountSplit(SplitList* slist)
{
GList *r = g_list_find_custom(slist, NULL, &predicate_is_apar_account);
if (r)
return r->data;
else
return NULL;
}
static gint predicate_not_equal(gconstpointer a,
gconstpointer user_data)
{
return (a != user_data);
}
static Split *getFirstOtherSplit(SplitList* slist, const Split *postaccount_split)
{
GList *r = g_list_find_custom(slist, postaccount_split, &predicate_not_equal);
if (r)
return r->data;
else
return NULL;
}
// ///////////////
gboolean gnc_ui_payment_is_customer_payment(const Transaction *txn)
{
SplitList *slist;
gboolean result = TRUE;
if (!txn)
return result;
// We require the txn to have one split in an A/R or A/P account.
slist = xaccTransGetSplitList(txn);
if (!slist)
return result;
if (countAssetAccounts(slist) == 0)
{
g_message("No asset splits in txn \"%s\"; cannot use this for assigning a payment.",
xaccTransGetDescription(txn));
return result;
}
{
Split *assetaccount_split = getFirstAssetAccountSplit(slist);
gnc_numeric amount = xaccSplitGetValue(assetaccount_split);
gboolean result = gnc_numeric_positive_p(amount); // positive amounts == customer
//g_message("Amount=%s", gnc_numeric_to_string(amount));
return result;
}
}
// ///////////////
PaymentWindow * gnc_ui_payment_new_with_txn (GncOwner *owner, Transaction *txn)
{
SplitList *slist;
if (!txn)
return NULL;
// We require the txn to have one split in an A/R or A/P account.
slist = xaccTransGetSplitList(txn);
if (!slist)
return NULL;
if (countAssetAccounts(slist) == 0)
{
g_message("No asset splits in txn \"%s\"; cannot use this for assigning a payment.",
xaccTransGetDescription(txn));
return NULL;
}
{
Split *assetaccount_split = getFirstAssetAccountSplit(slist);
Split *postaccount_split = getFirstAPARAccountSplit(slist); // watch out: Might be NULL
gnc_numeric amount = xaccSplitGetValue(assetaccount_split);
PaymentWindow *pw = gnc_ui_payment_new(owner,
qof_instance_get_book(QOF_INSTANCE(txn)));
g_assert(assetaccount_split); // we can rely on this because of the countAssetAccounts() check above
//g_message("Amount=%s", gnc_numeric_to_string(amount));
// Fill in the values from the given txn
pw->pre_existing_txn = txn;
gnc_ui_payment_window_set_num(pw, xaccTransGetNum(txn));
gnc_ui_payment_window_set_memo(pw, xaccTransGetDescription(txn));
{
GDate txn_date = xaccTransGetDatePostedGDate (txn);
gnc_ui_payment_window_set_date(pw, &txn_date);
}
gnc_ui_payment_window_set_amount(pw, gnc_numeric_abs(amount));
gnc_ui_payment_window_set_xferaccount(pw, xaccSplitGetAccount(assetaccount_split));
if (postaccount_split)
gnc_ui_payment_window_set_postaccount(pw, xaccSplitGetAccount(postaccount_split));
return pw;
}
}