/
dialog-invoice.c
3755 lines (3185 loc) · 123 KB
/
dialog-invoice.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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* dialog-invoice.c -- Dialog for Invoice entry
* Copyright (C) 2001,2002,2006 Derek Atkins
* Author: Derek Atkins <warlord@MIT.EDU>
*
* Copyright (c) 2005,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 <libguile.h>
#include "swig-runtime.h"
#include "qof.h"
#include "dialog-utils.h"
#include "gnc-component-manager.h"
#include "gnc-ui.h"
#include "gnc-gui-query.h"
#include "gnc-prefs.h"
#include "gnc-ui-util.h"
#include "gnc-date.h"
#include "gnc-date-edit.h"
#include "gnc-amount-edit.h"
#include "gnucash-sheet.h"
#include "gnucash-register.h"
#include "window-report.h"
#include "dialog-search.h"
#include "search-param.h"
#include "gnc-session.h"
#include "gncOwner.h"
#include "gncInvoice.h"
#include "gncInvoiceP.h"
#include "gncEntryLedger.h"
#include "gnc-plugin-page.h"
#include "gnc-general-search.h"
#include "dialog-date-close.h"
#include "dialog-invoice.h"
#include "dialog-job.h"
#include "business-gnome-utils.h"
#include "dialog-payment.h"
#include "dialog-tax-table.h"
#include "dialog-billterms.h"
#include "dialog-account.h"
#include "guile-mappings.h"
#include "dialog-dup-trans.h"
#include "dialog-query-view.h"
#include "gnc-plugin-business.h"
#include "gnc-plugin-page-invoice.h"
#include "gnc-main-window.h"
#include "gnc-state.h"
#include "dialog-doclink.h"
#include "dialog-doclink-utils.h"
#include "dialog-transfer.h"
#include "gnc-uri-utils.h"
/* Disable -Waddress. GCC 4.2 warns (and fails to compile with -Werror) when
* passing the address of a guid on the stack to QOF_BOOK_LOOKUP_ENTITY via
* gncInvoiceLookup and friends. When the macro gets inlined, the compiler
* emits a warning that the guid null pointer test is always true.
*/
#if (__GNUC__ >= 4 && __GNUC_MINOR__ >= 2)
# pragma GCC diagnostic warning "-Waddress"
#endif
#define DIALOG_NEW_INVOICE_CM_CLASS "dialog-new-invoice"
#define DIALOG_VIEW_INVOICE_CM_CLASS "dialog-view-invoice"
#define GNC_PREFS_GROUP_SEARCH "dialogs.business.invoice-search"
#define GNC_PREF_NOTIFY_WHEN_DUE "notify-when-due"
#define GNC_PREF_ACCUM_SPLITS "accumulate-splits"
#define GNC_PREF_DAYS_IN_ADVANCE "days-in-advance"
void gnc_invoice_window_ok_cb (GtkWidget *widget, gpointer data);
void gnc_invoice_window_cancel_cb (GtkWidget *widget, gpointer data);
void gnc_invoice_window_help_cb (GtkWidget *widget, gpointer data);
void gnc_invoice_type_toggled_cb (GtkWidget *widget, gpointer data);
void gnc_invoice_id_changed_cb (GtkWidget *widget, gpointer data);
void gnc_invoice_terms_changed_cb (GtkWidget *widget, gpointer data);
#define ENUM_INVOICE_TYPE(_) \
_(NEW_INVOICE, ) \
_(MOD_INVOICE, ) \
_(DUP_INVOICE, ) \
_(EDIT_INVOICE, ) \
_(VIEW_INVOICE, )
DEFINE_ENUM(InvoiceDialogType, ENUM_INVOICE_TYPE)
AS_STRING_DEC(InvoiceDialogType, ENUM_INVOICE_TYPE)
FROM_STRING_DEC(InvoiceDialogType, ENUM_INVOICE_TYPE)
FROM_STRING_FUNC(InvoiceDialogType, ENUM_INVOICE_TYPE)
AS_STRING_FUNC(InvoiceDialogType, ENUM_INVOICE_TYPE)
typedef enum
{
DUE_FOR_VENDOR, // show bills due
DUE_FOR_CUSTOMER, // show invoices due
} GncWhichDueType;
struct _invoice_select_window
{
QofBook *book;
GncOwner *owner;
QofQuery *q;
GncOwner owner_def;
};
#define UNUSED_VAR __attribute__ ((unused))
static QofLogModule UNUSED_VAR log_module = G_LOG_DOMAIN; //G_LOG_BUSINESS;
/** This data structure does double duty. It is used to maintain
* information for the "New Invoice" dialog, and it is also used to
* maintain information for the "Invoice Entry" page that is embedded
* into a main window. Beware, as not all fields are used by both windows.
*/
struct _invoice_window
{
GtkBuilder * builder;
GtkWidget * dialog; /* Used by 'New Invoice Window' */
GncPluginPage *page; /* Used by 'Edit Invoice' Page */
const gchar * page_state_name; /* Used for loading open state information */
/* Summary Bar Widgets */
GtkWidget * total_label;
GtkWidget * total_cash_label;
GtkWidget * total_charge_label;
GtkWidget * total_subtotal_label;
GtkWidget * total_tax_label;
/* Data Widgets */
GtkWidget * info_label; /*Default in glade is "Invoice Information"*/
GtkWidget * id_label; /* Default in glade is Invoice ID */
GtkWidget * type_label;
GtkWidget * type_label_hbox;
GtkWidget * type_hbox;
GtkWidget * type_choice;
GtkWidget * id_entry;
GtkWidget * notes_text;
GtkWidget * opened_date;
GtkWidget * posted_date_hbox;
GtkWidget * posted_date;
GtkWidget * active_check;
GtkWidget * paid_label;
GtkWidget * doclink_button;
GtkWidget * owner_box;
GtkWidget * owner_label;
GtkWidget * owner_choice;
GtkWidget * job_label;
GtkWidget * job_box;
GtkWidget * job_choice;
GtkWidget * billing_id_entry;
GtkWidget * terms_menu;
/* Project Widgets (used for Bills only) */
GtkWidget * proj_frame;
GtkWidget * proj_cust_box;
GtkWidget * proj_cust_choice;
GtkWidget * proj_job_box;
GtkWidget * proj_job_choice;
/* Expense Voucher Widgets */
GtkWidget * to_charge_frame;
GtkWidget * to_charge_edit;
gint width;
GncBillTerm * terms;
GnucashRegister * reg;
GncEntryLedger * ledger;
invoice_sort_type_t last_sort;
InvoiceDialogType dialog_type;
GncGUID invoice_guid;
gboolean is_credit_note;
gint component_id;
QofBook * book;
GncInvoice * created_invoice;
GncOwner owner;
GncOwner job;
GncOwner proj_cust;
GncOwner proj_job;
/* for Unposting */
gboolean reset_tax_tables;
};
/* Forward definitions for CB functions */
void gnc_invoice_window_active_toggled_cb (GtkWidget *widget, gpointer data);
gboolean gnc_invoice_window_leave_notes_cb (GtkWidget *widget, GdkEventFocus *event, gpointer data);
DialogQueryView *gnc_invoice_show_docs_due (GtkWindow *parent, QofBook *book, double days_in_advance, GncWhichDueType duetype);
#define INV_WIDTH_PREFIX "invoice_reg"
#define BILL_WIDTH_PREFIX "bill_reg"
#define VOUCHER_WIDTH_PREFIX "voucher_reg"
static void gnc_invoice_update_window (InvoiceWindow *iw, GtkWidget *widget);
static InvoiceWindow * gnc_ui_invoice_modify (GtkWindow *parent, GncInvoice *invoice);
/*******************************************************************************/
/* FUNCTIONS FOR ACCESSING DATA STRUCTURE FIELDS */
static GtkWidget *
iw_get_window (InvoiceWindow *iw)
{
if (iw->page)
return gnc_plugin_page_get_window (iw->page);
return iw->dialog;
}
GtkWidget *
gnc_invoice_get_register (InvoiceWindow *iw)
{
if (iw)
return (GtkWidget *)iw->reg;
return NULL;
}
GtkWidget *
gnc_invoice_get_notes (InvoiceWindow *iw)
{
if (iw)
return (GtkWidget *)iw->notes_text;
return NULL;
}
/*******************************************************************************/
/* FUNCTIONS FOR UNPOSTING */
static gboolean
iw_ask_unpost (InvoiceWindow *iw)
{
GtkWidget *dialog;
GtkToggleButton *toggle;
GtkBuilder *builder;
gint response;
const gchar *style_label = NULL;
GncOwnerType owner_type = gncOwnerGetType (&iw->owner);
builder = gtk_builder_new();
gnc_builder_add_from_file (builder, "dialog-invoice.glade", "unpost_message_dialog");
dialog = GTK_WIDGET (gtk_builder_get_object (builder, "unpost_message_dialog"));
toggle = GTK_TOGGLE_BUTTON(gtk_builder_get_object (builder, "yes_tt_reset"));
switch (owner_type)
{
case GNC_OWNER_VENDOR:
style_label = "gnc-class-vendors";
break;
case GNC_OWNER_EMPLOYEE:
style_label = "gnc-class-employees";
break;
default:
style_label = "gnc-class-customers";
break;
}
// Set a secondary style context for this page so it can be easily manipulated with css
gnc_widget_style_context_add_class (GTK_WIDGET(dialog), style_label);
gtk_window_set_transient_for (GTK_WINDOW(dialog),
GTK_WINDOW(iw_get_window(iw)));
iw->reset_tax_tables = FALSE;
gtk_widget_show_all(dialog);
response = gtk_dialog_run(GTK_DIALOG(dialog));
if (response == GTK_RESPONSE_OK)
iw->reset_tax_tables =
gtk_toggle_button_get_active(toggle);
gtk_widget_destroy(dialog);
g_object_unref(G_OBJECT(builder));
return (response == GTK_RESPONSE_OK);
}
/*******************************************************************************/
/* INVOICE WINDOW */
static GncInvoice *
iw_get_invoice (InvoiceWindow *iw)
{
if (!iw)
return NULL;
return gncInvoiceLookup (iw->book, &iw->invoice_guid);
}
GncInvoice *
gnc_invoice_window_get_invoice (InvoiceWindow *iw)
{
if (!iw)
return NULL;
return iw_get_invoice (iw);
}
GtkWidget *
gnc_invoice_window_get_doclink_button (InvoiceWindow *iw)
{
if (!iw)
return NULL;
return iw->doclink_button;
}
static void
set_gncEntry_switch_type (gpointer data, gpointer user_data)
{
GncEntry *entry = data;
//g_warning("Modifying date for entry with desc=\"%s\"", gncEntryGetDescription(entry));
gncEntrySetQuantity (entry, gnc_numeric_neg (gncEntryGetQuantity (entry)));
}
static void
set_gncEntry_date(gpointer data, gpointer user_data)
{
GncEntry *entry = data;
time64 new_date = *(time64*) user_data;
//g_warning("Modifying date for entry with desc=\"%s\"", gncEntryGetDescription(entry));
gncEntrySetDate(entry, gnc_time64_get_day_neutral (new_date));
/*gncEntrySetDateEntered(entry, *new_date); - don't modify this
* because apparently it defines the ordering of the entries,
* which we don't want to change. */
}
static void gnc_ui_to_invoice (InvoiceWindow *iw, GncInvoice *invoice)
{
GtkTextBuffer* text_buffer;
GtkTextIter start, end;
gchar *text;
time64 time;
gboolean is_credit_note = gncInvoiceGetIsCreditNote (invoice);
if (iw->dialog_type == VIEW_INVOICE)
return;
gnc_suspend_gui_refresh ();
gncInvoiceBeginEdit (invoice);
if (iw->active_check)
gncInvoiceSetActive (invoice, gtk_toggle_button_get_active
(GTK_TOGGLE_BUTTON (iw->active_check)));
text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW(iw->notes_text));
gtk_text_buffer_get_bounds (text_buffer, &start, &end);
text = gtk_text_buffer_get_text (text_buffer, &start, &end, FALSE);
gncInvoiceSetNotes (invoice, text);
if (iw->to_charge_edit)
gncInvoiceSetToChargeAmount (invoice,
gnc_amount_edit_get_amount
(GNC_AMOUNT_EDIT (iw->to_charge_edit)));
time = gnc_date_edit_get_date (GNC_DATE_EDIT (iw->opened_date));
/* Only set these values for NEW/MOD INVOICE types */
if (iw->dialog_type != EDIT_INVOICE)
{
gncInvoiceSetID (invoice, gtk_editable_get_chars
(GTK_EDITABLE (iw->id_entry), 0, -1));
gncInvoiceSetBillingID (invoice, gtk_editable_get_chars
(GTK_EDITABLE (iw->billing_id_entry), 0, -1));
gncInvoiceSetTerms (invoice, iw->terms);
gncInvoiceSetDateOpened (invoice, time);
gnc_owner_get_owner (iw->owner_choice, &(iw->owner));
if (iw->job_choice)
gnc_owner_get_owner (iw->job_choice, &(iw->job));
/* Only set the job if we've actually got one */
if (gncOwnerGetJob (&(iw->job)))
gncInvoiceSetOwner (invoice, &(iw->job));
else
gncInvoiceSetOwner (invoice, &(iw->owner));
/* Set the invoice currency based on the owner */
gncInvoiceSetCurrency (invoice, gncOwnerGetCurrency (&iw->owner));
/* Only set the BillTo if we've actually got one */
if (gncOwnerGetJob (&iw->proj_job))
gncInvoiceSetBillTo (invoice, &iw->proj_job);
else
gncInvoiceSetBillTo (invoice, &iw->proj_cust);
}
/* Document type can only be modified for a new or duplicated invoice/credit note */
if (iw->dialog_type == NEW_INVOICE || iw->dialog_type == DUP_INVOICE)
{
/* Update the entry dates to match the invoice date. This only really
* should happen for a duplicate invoice. However as a new invoice has
* no entries we can run this unconditionally. */
g_list_foreach(gncInvoiceGetEntries(invoice),
&set_gncEntry_date, &time);
gncInvoiceSetIsCreditNote (invoice, iw->is_credit_note);
}
/* If the document type changed on a duplicated invoice,
* its entries should be updated
*/
if (iw->dialog_type == DUP_INVOICE && iw->is_credit_note != is_credit_note)
{
g_list_foreach(gncInvoiceGetEntries(invoice),
&set_gncEntry_switch_type, NULL);
}
gncInvoiceCommitEdit (invoice);
gnc_resume_gui_refresh ();
}
static gboolean
gnc_invoice_window_verify_ok (InvoiceWindow *iw)
{
const char *res;
gchar *string;
/* save the current entry in the ledger? */
if (!gnc_entry_ledger_check_close (iw_get_window(iw), iw->ledger))
return FALSE;
/* Check the Owner */
gnc_owner_get_owner (iw->owner_choice, &(iw->owner));
res = gncOwnerGetName (&(iw->owner));
if (res == NULL || g_strcmp0 (res, "") == 0)
{
gnc_error_dialog (GTK_WINDOW (iw_get_window(iw)), "%s",
/* Translators: In this context,
* 'Billing information' maps to the
* label in the frame and means
* e.g. customer i.e. the company being
* invoiced. */
_("You need to supply Billing Information."));
return FALSE;
}
/* Check the ID; set one if necessary */
res = gtk_entry_get_text (GTK_ENTRY (iw->id_entry));
if (g_strcmp0 (res, "") == 0)
{
/* Invoices and bills have separate counters.
Therefore we pass the GncOwer to gncInvoiceNextID
so it knows whether we are creating a bill
or an invoice. */
string = gncInvoiceNextID(iw->book, &(iw->owner));
gtk_entry_set_text (GTK_ENTRY (iw->id_entry), string);
g_free(string);
}
return TRUE;
}
static gboolean
gnc_invoice_window_ok_save (InvoiceWindow *iw)
{
if (!gnc_invoice_window_verify_ok (iw))
return FALSE;
{
GncInvoice *invoice = iw_get_invoice (iw);
if (invoice)
{
gnc_ui_to_invoice (iw, invoice);
}
/* Save the invoice to return it later. */
iw->created_invoice = invoice;
}
return TRUE;
}
void
gnc_invoice_window_ok_cb (GtkWidget *widget, gpointer data)
{
InvoiceWindow *iw = data;
if (!gnc_invoice_window_ok_save (iw))
return;
/* Ok, we don't need this anymore */
iw->invoice_guid = *guid_null ();
/* if this is a new or duplicated invoice, and created_invoice is NON-NULL,
* then open up a new window with the invoice. This used to be done
* in gnc_ui_invoice_new() but cannot be done anymore
*/
if ((iw->dialog_type == NEW_INVOICE || iw->dialog_type == DUP_INVOICE)
&& iw->created_invoice)
gnc_ui_invoice_edit (gnc_ui_get_main_window (iw->dialog), iw->created_invoice);
gnc_close_gui_component (iw->component_id);
}
void
gnc_invoice_window_cancel_cb (GtkWidget *widget, gpointer data)
{
InvoiceWindow *iw = data;
gnc_close_gui_component (iw->component_id);
}
void
gnc_invoice_window_help_cb (GtkWidget *widget, gpointer data)
{
InvoiceWindow *iw = data;
GncOwnerType owner_type = gncOwnerGetType (&iw->owner);
switch(owner_type)
{
case GNC_OWNER_CUSTOMER:
gnc_gnome_help (GTK_WINDOW(iw->dialog), HF_HELP, HL_USAGE_INVOICE);
break;
case GNC_OWNER_VENDOR:
gnc_gnome_help (GTK_WINDOW(iw->dialog), HF_HELP, HL_USAGE_BILL);
break;
default:
gnc_gnome_help (GTK_WINDOW(iw->dialog), HF_HELP, HL_USAGE_VOUCHER);
break;
}
}
static const gchar *
gnc_invoice_window_get_state_group (InvoiceWindow *iw)
{
switch (gncOwnerGetType (gncOwnerGetEndOwner (&iw->owner)))
{
case GNC_OWNER_VENDOR:
return "Vendor documents";
break;
case GNC_OWNER_EMPLOYEE:
return "Employee documents";
break;
default:
return "Customer documents";
break;
}
}
/* Save user state layout information for Invoice/Bill/Voucher
* documents so it can be used for the default user set layout
*/
void
gnc_invoice_window_save_document_layout_to_user_state (InvoiceWindow *iw)
{
Table *table = gnc_entry_ledger_get_table (iw->ledger);
const gchar *group = gnc_invoice_window_get_state_group (iw);
gnc_table_save_state (table, group);
}
/* Removes the user state layout information for Invoice/Bill/Voucher
* documents and also resets the current layout to the built-in defaults
*/
void
gnc_invoice_window_reset_document_layout_and_clear_user_state (InvoiceWindow *iw)
{
GnucashRegister *reg = iw->reg;
const gchar *group = gnc_invoice_window_get_state_group (iw);
gnucash_register_reset_sheet_layout (reg);
gnc_state_drop_sections_for (group);
}
/* Checks to see if there is user state layout information for
* Invoice/Bill/Voucher documents so it can be used for the
* default user layout
*/
gboolean
gnc_invoice_window_document_has_user_state (InvoiceWindow *iw)
{
GKeyFile *state_file = gnc_state_get_current ();
const gchar *group = gnc_invoice_window_get_state_group (iw);
return g_key_file_has_group (state_file, group);
}
void
gnc_invoice_window_destroy_cb (GtkWidget *widget, gpointer data)
{
InvoiceWindow *iw = data;
GncInvoice *invoice = iw_get_invoice (iw);
gnc_suspend_gui_refresh ();
if ((iw->dialog_type == NEW_INVOICE || iw->dialog_type == DUP_INVOICE)
&& invoice != NULL)
{
gncInvoiceRemoveEntries (invoice);
gncInvoiceBeginEdit (invoice);
gncInvoiceDestroy (invoice);
iw->invoice_guid = *guid_null ();
}
gtk_widget_destroy(widget);
gnc_entry_ledger_destroy (iw->ledger);
gnc_unregister_gui_component (iw->component_id);
gnc_resume_gui_refresh ();
g_free (iw);
}
void
gnc_invoice_window_editCB (GtkWindow *parent, gpointer data)
{
InvoiceWindow *iw = data;
GncInvoice *invoice = iw_get_invoice (iw);
if (invoice)
gnc_ui_invoice_modify (parent, invoice);
}
void
gnc_invoice_window_duplicateInvoiceCB (GtkWindow *parent, gpointer data)
{
InvoiceWindow *iw = data;
GncInvoice *invoice = iw_get_invoice (iw);
if (invoice)
gnc_ui_invoice_duplicate (parent, invoice, TRUE, NULL);
}
void gnc_invoice_window_entryUpCB (GtkWidget *widget, gpointer data)
{
InvoiceWindow *iw = data;
if (!iw || !iw->ledger)
return;
gnc_entry_ledger_move_current_entry_updown(iw->ledger, TRUE);
}
void gnc_invoice_window_entryDownCB (GtkWidget *widget, gpointer data)
{
InvoiceWindow *iw = data;
if (!iw || !iw->ledger)
return;
gnc_entry_ledger_move_current_entry_updown(iw->ledger, FALSE);
}
void
gnc_invoice_window_recordCB (GtkWidget *widget, gpointer data)
{
InvoiceWindow *iw = data;
if (!iw || !iw->ledger)
return;
if (!gnc_entry_ledger_commit_entry (iw->ledger))
return;
gnucash_register_goto_next_virt_row (iw->reg);
}
void
gnc_invoice_window_cancelCB (GtkWidget *widget, gpointer data)
{
InvoiceWindow *iw = data;
if (!iw || !iw->ledger)
return;
gnc_entry_ledger_cancel_cursor_changes (iw->ledger);
}
void
gnc_invoice_window_deleteCB (GtkWidget *widget, gpointer data)
{
InvoiceWindow *iw = data;
GncEntry *entry;
if (!iw || !iw->ledger)
return;
/* get the current entry based on cursor position */
entry = gnc_entry_ledger_get_current_entry (iw->ledger);
if (!entry)
{
gnc_entry_ledger_cancel_cursor_changes (iw->ledger);
return;
}
/* deleting the blank entry just cancels */
if (entry == gnc_entry_ledger_get_blank_entry (iw->ledger))
{
gnc_entry_ledger_cancel_cursor_changes (iw->ledger);
return;
}
/* Verify that the user really wants to delete this entry */
{
const char *message = _("Are you sure you want to delete the "
"selected entry?");
const char *order_warn = _("This entry is attached to an order and "
"will be deleted from that as well!");
char *msg;
gboolean result;
if (gncEntryGetOrder (entry))
msg = g_strconcat (message, "\n\n", order_warn, (char *)NULL);
else
msg = g_strdup (message);
result = gnc_verify_dialog (GTK_WINDOW (iw_get_window(iw)), FALSE, "%s", msg);
g_free (msg);
if (!result)
return;
}
/* Yep, let's delete */
gnc_entry_ledger_delete_current_entry (iw->ledger);
return;
}
void
gnc_invoice_window_duplicateCB (GtkWidget *widget, gpointer data)
{
InvoiceWindow *iw = data;
if (!iw || !iw->ledger)
return;
gnc_entry_ledger_duplicate_current_entry (iw->ledger);
}
void
gnc_invoice_window_blankCB (GtkWidget *widget, gpointer data)
{
InvoiceWindow *iw = data;
if (!iw || !iw->ledger)
return;
if (!gnc_entry_ledger_commit_entry (iw->ledger))
return;
{
VirtualCellLocation vcell_loc;
GncEntry *blank;
blank = gnc_entry_ledger_get_blank_entry (iw->ledger);
if (blank == NULL)
return;
if (gnc_entry_ledger_get_entry_virt_loc (iw->ledger, blank, &vcell_loc))
gnucash_register_goto_virt_cell (iw->reg, vcell_loc);
}
}
static void
gnc_invoice_window_print_invoice(GtkWindow *parent, GncInvoice *invoice)
{
SCM func, arg, arg2;
SCM args = SCM_EOL;
int report_id;
const char *reportname = gnc_plugin_business_get_invoice_printreport();
g_return_if_fail (invoice);
if (!reportname)
reportname = "5123a759ceb9483abf2182d01c140e8d"; // fallback if the option lookup failed
func = scm_c_eval_string ("gnc:invoice-report-create");
g_return_if_fail (scm_is_procedure (func));
arg = SWIG_NewPointerObj(invoice, SWIG_TypeQuery("_p__gncInvoice"), 0);
arg2 = scm_from_utf8_string(reportname);
args = scm_cons2 (arg, arg2, args);
/* scm_gc_protect_object(func); */
arg = scm_apply (func, args, SCM_EOL);
g_return_if_fail (scm_is_exact (arg));
report_id = scm_to_int (arg);
/* scm_gc_unprotect_object(func); */
if (report_id >= 0)
reportWindow (report_id, parent);
}
void
gnc_invoice_window_printCB (GtkWindow* parent, gpointer data)
{
InvoiceWindow *iw = data;
gnc_invoice_window_print_invoice (parent, iw_get_invoice (iw));
}
static gboolean
gnc_dialog_post_invoice(InvoiceWindow *iw, char *message,
time64 *ddue, time64 *postdate,
char **memo, Account **acc, gboolean *accumulate)
{
GncInvoice *invoice;
char *ddue_label, *post_label, *acct_label, *question_label;
GList * acct_types = NULL;
GList * acct_commodities = NULL;
QofInstance *owner_inst;
EntryList *entries, *entries_iter;
invoice = iw_get_invoice (iw);
if (!invoice)
return FALSE;
ddue_label = _("Due Date");
post_label = _("Post Date");
acct_label = _("Post to Account");
question_label = _("Accumulate Splits?");
/* Determine the type of account to post to */
acct_types = gncOwnerGetAccountTypesList (&(iw->owner));
/* Determine which commodity we're working with */
acct_commodities = gncOwnerGetCommoditiesList(&(iw->owner));
/* Get the invoice entries */
entries = gncInvoiceGetEntries (invoice);
/* Find the most suitable post date.
* For Customer Invoices that would be today.
* For Vendor Bills and Employee Vouchers
* that would be the date of the most recent invoice entry.
* Failing that, today is used as a fallback */
*postdate = gnc_time(NULL);
if (entries && ((gncInvoiceGetOwnerType (invoice) == GNC_OWNER_VENDOR) ||
(gncInvoiceGetOwnerType (invoice) == GNC_OWNER_EMPLOYEE)))
{
*postdate = gncEntryGetDate ((GncEntry*)entries->data);
for (entries_iter = entries; entries_iter != NULL; entries_iter = g_list_next(entries_iter))
{
time64 entrydate = gncEntryGetDate ((GncEntry*)entries_iter->data);
if (entrydate > *postdate)
*postdate = entrydate;
}
}
/* Get the due date and posted account */
*ddue = *postdate;
*memo = NULL;
{
GncGUID *guid = NULL;
owner_inst = qofOwnerGetOwner (gncOwnerGetEndOwner (&(iw->owner)));
qof_instance_get (owner_inst,
"invoice-last-posted-account", &guid,
NULL);
*acc = xaccAccountLookup (guid, iw->book);
}
/* Get the default for the accumulate option */
*accumulate = gnc_prefs_get_bool(GNC_PREFS_GROUP_INVOICE, GNC_PREF_ACCUM_SPLITS);
if (!gnc_dialog_dates_acct_question_parented (iw_get_window(iw), message, ddue_label,
post_label, acct_label, question_label, TRUE, TRUE,
acct_types, acct_commodities, iw->book, iw->terms,
ddue, postdate, memo, acc, accumulate))
return FALSE;
return TRUE;
}
struct post_invoice_params
{
time64 ddue; /* Due date */
time64 postdate; /* Date posted */
char *memo; /* Memo for posting transaction */
Account *acc; /* Account to post to */
gboolean accumulate; /* Whether to accumulate splits */
GtkWindow *parent;
};
static void
gnc_invoice_post(InvoiceWindow *iw, struct post_invoice_params *post_params)
{
GncInvoice *invoice;
char *message, *memo;
Account *acc = NULL;
time64 ddue, postdate;
gboolean accumulate;
QofInstance *owner_inst;
const char *text;
GHashTable *foreign_currs;
GHashTableIter foreign_currs_iter;
gpointer key,value;
gboolean is_cust_doc, auto_pay;
gboolean show_dialog = TRUE;
gboolean post_ok = TRUE;
/* Make sure the invoice is ok */
if (!gnc_invoice_window_verify_ok (iw))
return;
invoice = iw_get_invoice (iw);
if (!invoice)
return;
/* Check that there is at least one Entry */
if (gncInvoiceGetEntries (invoice) == NULL)
{
gnc_error_dialog (GTK_WINDOW (iw_get_window(iw)), "%s",
_("The Invoice must have at least one Entry."));
return;
}
is_cust_doc = (gncInvoiceGetOwnerType (invoice) == GNC_OWNER_CUSTOMER);
/* Ok, we can post this invoice. Ask for verification, set the due date,
* post date, and posted account
*/
if (post_params)
{
ddue = post_params->ddue;
postdate = post_params->postdate;
// Dup it since it will free it below
memo = g_strdup (post_params->memo);
acc = post_params->acc;
accumulate = post_params->accumulate;
}
else
{
message = _("Do you really want to post the invoice?");
if (!gnc_dialog_post_invoice(iw, message,
&ddue, &postdate, &memo, &acc, &accumulate))
return;
}
/* Yep, we're posting. So, save the invoice...
* Note that we can safely ignore the return value; we checked
* the verify_ok earlier, so we know it's ok.
* Additionally make sure the invoice has the owner's currency
* refer to https://bugs.gnucash.org/show_bug.cgi?id=728074
*/
gnc_suspend_gui_refresh ();
gncInvoiceBeginEdit (invoice);
gnc_invoice_window_ok_save (iw);
gncInvoiceSetCurrency (invoice, gncOwnerGetCurrency (gncInvoiceGetOwner (invoice)));
/* Fill in the conversion prices with feedback from the user */
text = _("One or more of the entries are for accounts different from the invoice/bill currency. You will be asked a conversion rate for each.");
/* Ask the user for conversion rates for all foreign currencies
* (relative to the invoice currency) */
foreign_currs = gncInvoiceGetForeignCurrencies (invoice);
g_hash_table_iter_init (&foreign_currs_iter, foreign_currs);
while (g_hash_table_iter_next (&foreign_currs_iter, &key, &value))
{
GNCPrice *convprice;
gnc_commodity *account_currency = (gnc_commodity*)key;
gnc_numeric *amount = (gnc_numeric*)value;
XferDialog *xfer;
gnc_numeric exch_rate;
/* Explain to the user we're about to ask for an exchange rate.
* Only show this dialog once, right before the first xfer dialog pops up.
*/
if (show_dialog)
{
gnc_info_dialog(GTK_WINDOW (iw_get_window(iw)), "%s", text);
show_dialog = FALSE;
}
/* Note some twisted logic here:
* We ask the exchange rate
* FROM invoice currency
* TO other account currency
* Because that's what happens logically.
* But the internal posting logic works backwards:
* It searches for an exchange rate
* FROM other account currency