/
assistant-stock-transaction.cpp
1312 lines (1177 loc) · 53.6 KB
/
assistant-stock-transaction.cpp
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
/********************************************************************\
* assistant-stock-transaction.cpp -- stock assistant for GnuCash *
* Copyright (C) 2022 Christopher Lam *
* *
* 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 <vector>
#include <string>
#include <numeric>
#include <algorithm>
#include <optional>
#include <stdexcept>
extern "C" {
#include "Transaction.h"
#include "engine-helpers.h"
#include "dialog-utils.h"
#include "assistant-stock-transaction.h"
#include "gnc-account-sel.h"
#include "gnc-amount-edit.h"
#include "gnc-component-manager.h"
#include "gnc-date-edit.h"
#include "gnc-tree-view-account.h"
static QofLogModule log_module = GNC_MOD_ASSISTANT;
void stock_assistant_prepare (GtkAssistant *assistant, GtkWidget *page,
gpointer user_data);
void stock_assistant_finish (GtkAssistant *assistant, gpointer user_data);
void stock_assistant_cancel (GtkAssistant *gtkassistant, gpointer user_data);
}
enum class FieldMask : unsigned;
bool operator &(FieldMask lhs, FieldMask rhs);
FieldMask operator |(FieldMask lhs, FieldMask rhs);
FieldMask operator ^(FieldMask lhs, FieldMask rhs);
static const char* ASSISTANT_STOCK_TRANSACTION_CM_CLASS = "assistant-stock-transaction";
enum assistant_pages
{
PAGE_INTRO = 0,
PAGE_TRANSACTION_DETAILS,
PAGE_TRANSACTION_TYPE,
PAGE_STOCK_AMOUNT,
PAGE_STOCK_VALUE,
PAGE_CASH,
PAGE_FEES,
PAGE_DIVIDEND,
PAGE_CAPGAINS,
PAGE_FINISH
};
enum split_cols
{
SPLIT_COL_ACCOUNT = 0,
SPLIT_COL_MEMO,
SPLIT_COL_TOOLTIP,
SPLIT_COL_DEBIT,
SPLIT_COL_CREDIT,
NUM_SPLIT_COLS
};
/** structures *********************************************************/
enum class FieldMask : unsigned
{
DISABLED = 0,
ENABLED_DEBIT,
ENABLED_CREDIT,
ALLOW_ZERO = 4,
ALLOW_NEGATIVE = 8
};
FieldMask operator |(FieldMask lhs, FieldMask rhs)
{
return static_cast<FieldMask> (static_cast<unsigned>(lhs) |
static_cast<unsigned>(rhs));
};
bool operator &(FieldMask lhs, FieldMask rhs)
{
return (static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
};
FieldMask operator ^(FieldMask lhs, FieldMask rhs)
{
return static_cast<FieldMask> (static_cast<unsigned>(lhs) ^
static_cast<unsigned>(rhs));
};
struct TxnTypeInfo
{
FieldMask stock_amount;
FieldMask stock_value;
FieldMask cash_value;
FieldMask fees_value;
bool fees_capitalize;
FieldMask dividend_value;
FieldMask capgains_value;
const char* friendly_name;
const char* explanation;
};
using StringVec = std::vector<std::string>;
using TxnTypeVec = std::vector<TxnTypeInfo>;
using AccountVec = std::vector<Account*>;
static const TxnTypeVec starting_types
{
{
FieldMask::ENABLED_DEBIT, // stock_amt
FieldMask::ENABLED_DEBIT, // stock_val
FieldMask::ENABLED_CREDIT, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
true, // fees_capitalize
FieldMask::DISABLED, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing an
// Initial stock purchase
N_("Open buy"),
N_("Initial stock purchase")
},
{
FieldMask::ENABLED_CREDIT, // stock_amt
FieldMask::ENABLED_CREDIT, // stock_val
FieldMask::ENABLED_DEBIT, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
true, // fees_capitalize
FieldMask::DISABLED, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing an
// initial stock short-sale
N_("Open short"),
N_("Initial stock short-sale")
}
};
static const TxnTypeVec long_types
{
{
FieldMask::ENABLED_DEBIT, // stock_amt
FieldMask::ENABLED_DEBIT, // stock_val
FieldMask::ENABLED_CREDIT, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
true, // fees_capitalize
FieldMask::DISABLED, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing
// purchase of stock.
N_("Buy"),
N_("Buying stock.")
},
{
FieldMask::ENABLED_CREDIT, // stock_amt
FieldMask::ENABLED_CREDIT, // stock_val
FieldMask::ENABLED_DEBIT, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
false, // fees_capitalize
FieldMask::DISABLED, // dividend_amt
FieldMask::ENABLED_CREDIT | FieldMask::ALLOW_ZERO | FieldMask::ALLOW_NEGATIVE, // capgains_amt
// Translators: this is a stock transaction describing sale of
// stock, and recording capital gain/loss
N_("Sell"),
N_("Selling stock, and record capital gain/loss")
},
{
FieldMask::DISABLED, // stock_amt
FieldMask::DISABLED, // stock_val
FieldMask::ENABLED_DEBIT, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
false, // fees_capitalize
FieldMask::ENABLED_CREDIT, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing
// dividends issued to holder
N_("Dividend"),
N_("Company issues dividends to holder")
},
{
FieldMask::ENABLED_DEBIT, // stock_amt
FieldMask::ENABLED_DEBIT, // stock_val
FieldMask::ENABLED_DEBIT, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
true, // fees_capitalize
FieldMask::ENABLED_CREDIT, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing
// dividend issued to holder, and is reinvested. Some
// dividends are distributed as cash.
N_("Dividend reinvestment (with remainder)"),
N_("Company issues dividend which is reinvested. Some dividends are paid to holder")
},
{
FieldMask::ENABLED_DEBIT, // stock_amt
FieldMask::ENABLED_DEBIT, // stock_val
FieldMask::DISABLED, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
true, // fees_capitalize
FieldMask::ENABLED_CREDIT, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing
// dividend which is wholly reinvested.
N_("Dividend reinvestment (without remainder)"),
N_("Company issues dividend which is wholly reinvested.")
},
{
FieldMask::DISABLED, // stock_amt
FieldMask::ENABLED_CREDIT, // stock_val
FieldMask::ENABLED_DEBIT, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
true, // fees_capitalize
FieldMask::DISABLED, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing return
// of capital
N_("Return of Capital"),
N_("Stock returns capital to holder")
},
{
FieldMask::DISABLED, // stock_amt
FieldMask::ENABLED_DEBIT, // stock_val
FieldMask::DISABLED, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
false, // fees_capitalize
FieldMask::ENABLED_CREDIT, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing a
// notional distribution
N_("Notional distribution"),
N_("Stock returns a notional distribution")
},
{
FieldMask::ENABLED_DEBIT, // stock_amt
FieldMask::DISABLED, // stock_val
FieldMask::DISABLED, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
true, // fees_capitalize
FieldMask::DISABLED, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing a stock
// split
N_("Stock split"),
N_("Stock price is fractionally reduced")
},
{
FieldMask::ENABLED_CREDIT, // stock_amt
FieldMask::DISABLED, // stock_val
FieldMask::DISABLED, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
true, // fees_capitalize
FieldMask::DISABLED, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing a reverse split
N_("Reverse split"),
N_("Stock price is fractionally increased.")
},
{
FieldMask::ENABLED_CREDIT, // stock_amt
FieldMask::ENABLED_CREDIT, // stock_val
FieldMask::ENABLED_DEBIT, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
false, // fees_capitalize
FieldMask::DISABLED, // dividend_amt
FieldMask::ENABLED_CREDIT | FieldMask::ALLOW_ZERO | FieldMask::ALLOW_NEGATIVE, // capgains_amt
// Translators: this is a stock transaction describing a
// reverse split. Some fractional stock is returned as cash.
N_("Reverse split with cash in lieu for fractionals"),
N_("Stock price is fractionally increased. Fractional remaining stock is returned as cash.")
},
};
static const TxnTypeVec short_types
{
{
FieldMask::ENABLED_CREDIT, // stock_amt
FieldMask::ENABLED_CREDIT, // stock_val
FieldMask::ENABLED_DEBIT, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
true, // fees_capitalize
FieldMask::DISABLED, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing
// shorting of stock.
N_("Short sell"),
N_("Shorting stock.")
},
{
FieldMask::ENABLED_DEBIT, // stock_amt
FieldMask::ENABLED_DEBIT, // stock_val
FieldMask::ENABLED_CREDIT, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
false, // fees_capitalize
FieldMask::DISABLED, // dividend_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO | FieldMask::ALLOW_NEGATIVE, // capg_amt
// Translators: this is a stock transaction describing cover
// buying stock, and recording capital gain/loss
N_("Buy to cover short"),
N_("Buy back stock to cover short and record capital gain/loss")
},
{
FieldMask::DISABLED, // stock_amt
FieldMask::DISABLED, // stock_val
FieldMask::ENABLED_CREDIT, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
false, // fees_capitalize
FieldMask::ENABLED_DEBIT, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing
// dividends retrieved from holder when shorting stock
N_("Compensatory Dividend"),
N_("Company issues dividends to holder when shorting stock")
},
{
FieldMask::ENABLED_CREDIT, // stock_amt
FieldMask::ENABLED_CREDIT, // stock_val
FieldMask::ENABLED_DEBIT, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
true, // fees_capitalize
FieldMask::ENABLED_DEBIT, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing
// dividends retrieved from holder when shorting stock. Some
// dividends are recovered from holder
N_("Dividend reinvestment (with remainder)"),
N_("Company issues dividends to holder when shorting stock. Some dividends are recovered from holder")
},
{
FieldMask::ENABLED_DEBIT, // stock_amt
FieldMask::ENABLED_DEBIT, // stock_val
FieldMask::DISABLED, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
true, // fees_capitalize
FieldMask::ENABLED_DEBIT, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing
// dividends retrieved from holder when shorting stock,
N_("Dividend reinvestment (without remainder)"),
N_("Company issues dividend when shorting stock, which are wholly recovered from holder.")
},
{
FieldMask::DISABLED, // stock_amt
FieldMask::ENABLED_DEBIT, // stock_val
FieldMask::ENABLED_CREDIT, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
true, // fees_capitalize
FieldMask::DISABLED, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing return
// of capital retrieved from holder when shorting stock
N_("Compensatory Return of Capital"),
N_("Return retrieves capital from holder when shorting stock")
},
{
FieldMask::DISABLED, // stock_amt
FieldMask::ENABLED_CREDIT, // stock_val
FieldMask::DISABLED, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
false, // fees_capitalize
FieldMask::ENABLED_DEBIT, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing a
// notional distribution when shorting stock
N_("Compensatory Notional distribution"),
N_("Stock retrieves a notional distribution")
},
{
FieldMask::ENABLED_CREDIT, // stock_amt
FieldMask::DISABLED, // stock_val
FieldMask::DISABLED, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
true, // fees_capitalize
FieldMask::DISABLED, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing a stock
// split when shorting stock
N_("Stock split"),
N_("Stock price is fractionally reduced when shorting stock")
},
{
FieldMask::ENABLED_DEBIT, // stock_amt
FieldMask::DISABLED, // stock_val
FieldMask::DISABLED, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
true, // fees_capitalize
FieldMask::DISABLED, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing a
// reverse split when shorting stock.
N_("Reverse split"),
N_("Stock price is fractionally increased when shorting stock.")
},
{
FieldMask::ENABLED_DEBIT, // stock_amt
FieldMask::ENABLED_DEBIT, // stock_val
FieldMask::ENABLED_CREDIT, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
false, // fees_capitalize
FieldMask::DISABLED, // dividend_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO | FieldMask::ALLOW_NEGATIVE, // capg_amt
// Translators: this is a stock transaction describing a
// reverse split when shorting stock. Fractional remaining
// stock is retrieved as cash.
N_("Reverse split with cash in lieu for fractionals"),
N_("Stock price is fractionally increased when shorting stock. Fractional remaining stock is retrieved as cash.")
},
};
typedef struct
{
GtkWidget * window;
GtkWidget * assistant;
std::optional<TxnTypeVec> txn_types;
// the following stores date at which the txn_types were set. If
// the GNCDateEdit date is modified, it will trigger recreation of
// the txn_types above.
std::optional<time64> txn_types_date;
Account * acct;
gnc_commodity * currency;
// transaction type page
GtkWidget * transaction_type_page;
GtkWidget * transaction_type_combo;
GtkWidget * transaction_type_explanation;
std::optional<TxnTypeInfo> txn_type;
// transaction details page
GtkWidget * transaction_details_page;
GtkWidget * date_edit;
GtkWidget * transaction_description_entry;
// stock amount page
gnc_numeric balance_at_date;
GtkWidget * stock_amount_page;
GtkWidget * prev_amount;
GtkWidget * next_amount;
GtkWidget * stock_amount_edit;
// stock value page
GtkWidget * stock_value_page;
GtkWidget * stock_value_edit;
GtkWidget * price_value;
GtkWidget * stock_memo_edit;
// cash page
GtkWidget * cash_page;
GtkWidget * cash_account;
GtkWidget * cash_memo_edit;
GtkWidget * cash_value;
// fees page
GtkWidget * fees_page;
GtkWidget * capitalize_fees_checkbox;
GtkWidget * fees_account;
GtkWidget * fees_memo_edit;
GtkWidget * fees_value;
// dividend page
GtkWidget * dividend_page;
GtkWidget * dividend_account;
GtkWidget * dividend_memo_edit;
GtkWidget * dividend_value;
// capgains page
GtkWidget * capgains_page;
GtkWidget * capgains_account;
GtkWidget * capgains_memo_edit;
GtkWidget * capgains_value;
// finish page
GtkWidget * finish_page;
GtkWidget * finish_split_view;
GtkWidget * finish_summary;
} StockTransactionInfo;
/******* implementations ***********************************************/
static void
stock_assistant_window_destroy_cb (GtkWidget *object, gpointer user_data)
{
auto info = static_cast<StockTransactionInfo*>(user_data);
gnc_unregister_gui_component_by_data (ASSISTANT_STOCK_TRANSACTION_CM_CLASS, info);
info->txn_types_date = std::nullopt;
info->txn_types = std::nullopt;
info->txn_type = std::nullopt;
g_free (info);
}
static void
refresh_page_transaction_type (GtkWidget *widget, gpointer user_data)
{
auto info = static_cast<StockTransactionInfo*>(user_data);
auto type_idx = gtk_combo_box_get_active (GTK_COMBO_BOX (widget));
if (type_idx < 0) // combo isn't initialized yet.
return;
if (!info->txn_types)
return;
try
{
info->txn_type = info->txn_types->at (type_idx);
}
catch (const std::out_of_range&)
{
PERR ("out of range type_idx=%d", type_idx);
return;
}
g_return_if_fail (info->txn_type);
gtk_label_set_text (GTK_LABEL (info->transaction_type_explanation),
_(info->txn_type->explanation));
// set default capitalize fees setting
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (info->capitalize_fees_checkbox),
info->txn_type->fees_capitalize);
}
static void
refresh_page_stock_amount (GtkWidget *widget, gpointer user_data)
{
auto info = static_cast<StockTransactionInfo*>(user_data);
g_return_if_fail (info->txn_type);
auto pinfo = gnc_commodity_print_info (xaccAccountGetCommodity (info->acct), true);
auto bal = info->balance_at_date;
gtk_label_set_text (GTK_LABEL(info->prev_amount), xaccPrintAmount (bal, pinfo));
gnc_numeric stock_amount;
if (gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT (info->stock_amount_edit),
&stock_amount, true, nullptr))
gtk_label_set_text (GTK_LABEL(info->next_amount), nullptr);
else
{
if (info->txn_type->stock_amount == FieldMask::ENABLED_CREDIT)
stock_amount = gnc_numeric_neg (stock_amount);
bal = gnc_numeric_add_fixed (bal, stock_amount);
gtk_label_set_text (GTK_LABEL(info->next_amount),
xaccPrintAmount (bal, pinfo));
}
}
static void
refresh_page_stock_value (GtkWidget *widget, gpointer user_data)
{
auto info = static_cast<StockTransactionInfo*>(user_data);
gnc_numeric amount, value;
g_return_if_fail (info->txn_type);
if (info->txn_type->stock_amount == FieldMask::DISABLED ||
info->txn_type->stock_value == FieldMask::DISABLED ||
gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT (info->stock_amount_edit), &amount, true, nullptr) ||
gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT (info->stock_value_edit), &value, true, nullptr) ||
gnc_numeric_zero_p (value))
{
// Translators: StockAssistant: N/A denotes stock price is not computable
const char* na_label = N_("N/A");
gtk_label_set_text (GTK_LABEL (info->price_value), _(na_label));
return;
}
auto price = gnc_numeric_div (value, amount, GNC_DENOM_AUTO, GNC_HOW_RND_ROUND);
auto pinfo = gnc_commodity_print_info (info->currency, true);
gtk_label_set_text (GTK_LABEL (info->price_value), xaccPrintAmount (price, pinfo));
}
static void
refresh_page_cash (GtkWidget *widget, gpointer user_data)
{
return;
}
static void
refresh_page_fees (GtkWidget *widget, gpointer user_data)
{
auto info = static_cast<StockTransactionInfo*>(user_data);
auto capitalize_fees = gtk_toggle_button_get_active
(GTK_TOGGLE_BUTTON (info->capitalize_fees_checkbox));
gtk_widget_set_sensitive (info->fees_account, !capitalize_fees);
}
static void
refresh_page_dividend (GtkWidget *widget, gpointer user_data)
{
return;
}
static void
refresh_page_capgains (GtkWidget *widget, gpointer user_data)
{
return;
}
static void
add_error (StringVec& errors, const char* format_str, const char* arg)
{
gchar *buf = g_strdup_printf (_(format_str),
g_dpgettext2 (nullptr, "Stock Assistant: Page name", arg));
errors.emplace_back (buf);
g_free (buf);
}
static void
add_error_str (StringVec& errors, const char* str)
{
errors.emplace_back (_(str));
}
static void
check_page (GtkListStore *list, gnc_numeric& debit, gnc_numeric& credit,
FieldMask splitfield, Account *acct, GtkWidget *memo, GtkWidget *gae,
gnc_commodity *comm, const char* page, StringVec& errors)
{
const char* missing_str = N_("(missing)");
const gchar* amtstr;
gnc_numeric amount;
bool debit_side = (splitfield & FieldMask::ENABLED_DEBIT);
if (gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT (gae), &amount, true, nullptr))
{
if (splitfield & FieldMask::ALLOW_ZERO)
amtstr = "";
else
{
add_error (errors, N_("Amount for %s is missing."), page);
amtstr = _(missing_str);
}
}
else
{
if (!(splitfield & FieldMask::ALLOW_NEGATIVE))
{
if ((splitfield & FieldMask::ALLOW_ZERO) && gnc_numeric_negative_p (amount))
add_error (errors, N_("Amount for %s must not be negative."), page);
else if (!(splitfield & FieldMask::ALLOW_ZERO) && !gnc_numeric_positive_p (amount))
add_error (errors, N_("Amount for %s must be positive."), page);
}
if (gnc_numeric_negative_p (amount))
{
amount = gnc_numeric_neg (amount);
debit_side = !debit_side;
}
if (debit_side)
debit = gnc_numeric_add_fixed (debit, amount);
else
credit = gnc_numeric_add_fixed (credit, amount);
amtstr = xaccPrintAmount (amount, gnc_commodity_print_info (comm, true));
}
auto memostr = gtk_entry_get_text (GTK_ENTRY (memo));
auto memostr_escaped = g_markup_escape_text (memostr, -1);
const gchar *acctstr;
if (acct)
acctstr = xaccAccountGetName (acct);
else if ((splitfield & FieldMask::ALLOW_ZERO) && gnc_numeric_zero_p (amount))
acctstr = "";
else
{
add_error (errors, N_("Account for %s is missing"), page);
acctstr = _(missing_str);
}
GtkTreeIter iter;
gtk_list_store_append (list, &iter);
gtk_list_store_set (list, &iter,
SPLIT_COL_ACCOUNT, acctstr,
SPLIT_COL_MEMO, memostr,
SPLIT_COL_TOOLTIP, memostr_escaped,
SPLIT_COL_DEBIT, debit_side ? amtstr : "",
SPLIT_COL_CREDIT, !debit_side ? amtstr : "",
-1);
g_free (memostr_escaped);
}
static inline Account*
gas_account (GtkWidget *gas)
{
return gnc_account_sel_get_account (GNC_ACCOUNT_SEL (gas));
}
static void
refresh_page_finish (StockTransactionInfo *info)
{
g_return_if_fail (info->txn_type);
auto view = GTK_TREE_VIEW (info->finish_split_view);
auto list = GTK_LIST_STORE (gtk_tree_view_get_model(view));
gtk_list_store_clear (list);
gnc_numeric debit = gnc_numeric_zero ();
gnc_numeric credit = gnc_numeric_zero ();
StringVec errors, warnings;
// check the stock transaction date. If there are existing stock
// transactions dated after the date specified, it is very likely
// the later stock transactions will be invalidated. warn the user
// to review them.
auto new_date = gnc_date_edit_get_date_end (GNC_DATE_EDIT (info->date_edit));
auto last_split_node = g_list_last (xaccAccountGetSplitList (info->acct));
if (last_split_node)
{
auto last_split = static_cast<const Split*> (last_split_node->data);
auto last_split_date = xaccTransGetDate (xaccSplitGetParent (last_split));
if (new_date <= last_split_date)
{
auto last_split_date_str = qof_print_date (last_split_date);
auto new_date_str = qof_print_date (new_date);
// Translators: the first %s is the new transaction date;
// the second %s is the current stock account's latest
// transaction date.
auto warn_txt = g_strdup_printf (_("You will enter a transaction \
with date %s which is earlier than the latest transaction in this account, \
dated %s. Doing so may affect the cost basis, and therefore capital gains, \
of transactions dated after the new entry. Please review all transactions \
to ensure proper recording."), new_date_str, last_split_date_str);
warnings.push_back (warn_txt);
g_free (warn_txt);
g_free (new_date_str);
g_free (last_split_date_str);
}
}
if (info->txn_type->stock_amount != FieldMask::DISABLED)
{
auto stock_amount = gnc_amount_edit_get_amount
(GNC_AMOUNT_EDIT(info->stock_amount_edit));
if (!gnc_numeric_positive_p (stock_amount))
add_error_str (errors, N_("Stock amount must be positive"));
if (info->txn_type->stock_amount & FieldMask::ENABLED_CREDIT)
stock_amount = gnc_numeric_neg (stock_amount);
auto new_bal = gnc_numeric_add_fixed (info->balance_at_date, stock_amount);
if (gnc_numeric_positive_p (info->balance_at_date) &&
gnc_numeric_negative_p (new_bal))
add_error_str (errors, N_("Cannot sell more units than owned"));
else if (gnc_numeric_negative_p (info->balance_at_date) &&
gnc_numeric_positive_p (new_bal))
add_error_str (errors, N_("Cannot cover buy more units than owed"));
}
if (info->txn_type->stock_value != FieldMask::DISABLED)
check_page (list, debit, credit, info->txn_type->stock_value, info->acct,
info->stock_memo_edit, info->stock_value_edit, info->currency,
NC_ ("Stock Assistant: Page name", "stock value"), errors);
if (info->txn_type->cash_value != FieldMask::DISABLED)
check_page (list, debit, credit, info->txn_type->cash_value,
gas_account (info->cash_account), info->cash_memo_edit,
info->cash_value, info->currency,
NC_ ("Stock Assistant: Page name", "cash"), errors);
if (info->txn_type->fees_value != FieldMask::DISABLED)
{
auto capitalize_fees = gtk_toggle_button_get_active
(GTK_TOGGLE_BUTTON (info->capitalize_fees_checkbox));
check_page (list, debit, credit, info->txn_type->fees_value,
capitalize_fees ? info->acct : gas_account (info->fees_account),
info->fees_memo_edit, info->fees_value, info->currency,
NC_ ("Stock Assistant: Page name", "fees"), errors);
}
if (info->txn_type->dividend_value != FieldMask::DISABLED)
check_page (list, debit, credit, info->txn_type->dividend_value,
gas_account (info->dividend_account),
info->dividend_memo_edit, info->dividend_value, info->currency,
NC_ ("Stock Assistant: Page name", "dividend"), errors);
// the next two checks will involve the two capgains splits:
// income side and stock side. The capgains_value ^
// (FieldMask::ENABLED_CREDIT | FieldMask::ENABLED_DEBIT) will swap the debit/credit
// flags.
if (info->txn_type->capgains_value != FieldMask::DISABLED)
{
check_page (list, debit, credit, info->txn_type->capgains_value,
gas_account (info->capgains_account),
info->capgains_memo_edit, info->capgains_value, info->currency,
NC_ ("Stock Assistant: Page name", "capital gains"), errors);
check_page (list, debit, credit,
info->txn_type->capgains_value ^ (FieldMask::ENABLED_CREDIT | FieldMask::ENABLED_DEBIT),
info->acct, info->capgains_memo_edit, info->capgains_value,
info->currency,
NC_ ("Stock Assistant: Page name", "capital gains"), errors);
}
if (!gnc_numeric_equal (debit, credit))
{
auto imbalance_str = N_("Total Debits of %s does not balance with total Credits of %s.");
auto print_info = gnc_commodity_print_info (info->currency, true);
auto debit_str = g_strdup (xaccPrintAmount (debit, print_info));
auto credit_str = g_strdup (xaccPrintAmount (credit, print_info));
auto error_str = g_strdup_printf (_(imbalance_str), debit_str, credit_str);
errors.emplace_back (error_str);
g_free (error_str);
g_free (credit_str);
g_free (debit_str);
}
// generate final summary message. Collates a header, the errors
// and warnings. Then allow completion if errors is empty.
auto header = errors.empty() ?
N_("No errors found. Click Apply to create transaction.") :
N_("The following errors must be fixed:");
auto summary = std::string { _(header) };
auto summary_add = [&summary](auto a) { summary += "\n• "; summary += a; };
std::for_each (errors.begin(), errors.end(), summary_add);
if (!warnings.empty())
{
auto warnings_header = N_ ("The following warnings exist:");
summary += "\n\n";
summary += _(warnings_header);
std::for_each (warnings.begin(), warnings.end(), summary_add);
}
gtk_label_set_text (GTK_LABEL (info->finish_summary), summary.c_str());
gtk_assistant_set_page_complete (GTK_ASSISTANT (info->window),
info->finish_page, errors.empty());
}
void
stock_assistant_prepare (GtkAssistant *assistant, GtkWidget *page,
gpointer user_data)
{
auto info = static_cast<StockTransactionInfo*>(user_data);
gint currentpage = gtk_assistant_get_current_page(assistant);
switch (currentpage)
{
case PAGE_TRANSACTION_TYPE:;
// initialize transaction types.
gnc_numeric balance;
time64 date;
date = gnc_date_edit_get_date_end (GNC_DATE_EDIT (info->date_edit));
if (info->txn_types_date && (info->txn_types_date == date))
break;
info->txn_types_date = date;
balance = xaccAccountGetBalanceAsOfDate (info->acct, date);
info->txn_types = gnc_numeric_zero_p (balance) ? starting_types
: gnc_numeric_positive_p (balance) ? long_types
: short_types;
gtk_combo_box_text_remove_all (GTK_COMBO_BOX_TEXT (info->transaction_type_combo));
for (auto& it : *(info->txn_types))
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (info->transaction_type_combo),
_(it.friendly_name));
gtk_combo_box_set_active (GTK_COMBO_BOX (info->transaction_type_combo), 0);
refresh_page_transaction_type (info->transaction_type_combo, info);
gtk_widget_grab_focus (info->transaction_type_combo);
break;
case PAGE_STOCK_AMOUNT:
info->balance_at_date = xaccAccountGetBalanceAsOfDate
(info->acct, gnc_date_edit_get_date_end (GNC_DATE_EDIT (info->date_edit)));
refresh_page_stock_amount (info->stock_amount_edit, info);
// fixme: the following doesn't work???
gtk_widget_grab_focus (gnc_amount_edit_gtk_entry
(GNC_AMOUNT_EDIT (info->stock_amount_edit)));
break;
case PAGE_STOCK_VALUE:
refresh_page_stock_value (info->stock_value_edit, info);
// fixme: ditto
gtk_widget_grab_focus (gnc_amount_edit_gtk_entry
(GNC_AMOUNT_EDIT (info->stock_value_edit)));
break;
case PAGE_CASH:
refresh_page_cash (info->cash_value, info);
break;
case PAGE_FEES:
refresh_page_fees (info->fees_value, info);
break;
case PAGE_DIVIDEND:
refresh_page_dividend (info->fees_value, info);
break;
case PAGE_CAPGAINS:
refresh_page_capgains (info->capgains_value, info);
break;
case PAGE_FINISH:
refresh_page_finish (info);
break;
}
}
static gint
forward_page_func (gint current_page, gpointer user_data)
{
auto info = static_cast<StockTransactionInfo*>(user_data);
auto& txn_type = info->txn_type;
current_page++;
if (!txn_type)
return current_page;
if (txn_type->stock_amount == FieldMask::DISABLED && current_page == PAGE_STOCK_AMOUNT)
current_page++;
if (txn_type->stock_value == FieldMask::DISABLED && current_page == PAGE_STOCK_VALUE)
current_page++;
if (txn_type->cash_value == FieldMask::DISABLED && current_page == PAGE_CASH)
current_page++;
if (txn_type->fees_value == FieldMask::DISABLED && current_page == PAGE_FEES)
current_page++;
if (txn_type->dividend_value == FieldMask::DISABLED && current_page == PAGE_DIVIDEND)
current_page++;
if (txn_type->capgains_value == FieldMask::DISABLED && current_page == PAGE_CAPGAINS)
current_page++;
return current_page;
}
static void
create_split (Transaction *trans, FieldMask splitfield,
const gchar *action, Account *account,
AccountVec& account_commits, GtkWidget *memo_entry,
GtkWidget *amount, GtkWidget *value,
bool skip_if_zero)
{
auto amount_numeric = amount ? gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT (amount)) : gnc_numeric_zero ();
auto value_numeric = value ? gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT (value)) : gnc_numeric_zero ();
if (skip_if_zero && gnc_numeric_zero_p (value_numeric))
return;
if (splitfield & FieldMask::ENABLED_CREDIT)
{
amount_numeric = gnc_numeric_neg (amount_numeric);
value_numeric = gnc_numeric_neg (value_numeric);
}
auto split = xaccMallocSplit (gnc_get_current_book ());
xaccSplitSetParent (split, trans);
xaccAccountBeginEdit (account);
account_commits.emplace_back (account);
xaccSplitSetAccount (split, account);
xaccSplitSetMemo (split, gtk_entry_get_text (GTK_ENTRY (memo_entry)));
xaccSplitSetValue (split, value_numeric);
xaccSplitSetAmount (split, amount_numeric);
DEBUG ("creating %s split in Acct(%s): Val(%s), Amt(%s) => Val(%s), Amt(%s)",
action, xaccAccountGetName (account),
gnc_num_dbg_to_string (value_numeric),
gnc_num_dbg_to_string (amount_numeric),
gnc_num_dbg_to_string (xaccSplitGetValue (split)),
gnc_num_dbg_to_string (xaccSplitGetAmount (split)));
gnc_set_num_action (nullptr, split,
nullptr, g_dpgettext2 (nullptr, "Stock Assistant: Action field", action));
}
static void
add_price (GtkWidget *amount, GtkWidget *value,
gnc_commodity *commodity, gnc_commodity *currency, time64 date)
{
auto amt = gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT (amount));
auto val = gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT (value));
auto p = gnc_numeric_div (val, amt, GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT);
auto price = gnc_price_create (gnc_get_current_book ());
gnc_price_begin_edit (price);
gnc_price_set_commodity (price, commodity);
gnc_price_set_currency (price, currency);
gnc_price_set_time64 (price, date);
gnc_price_set_source (price, PRICE_SOURCE_STOCK_TRANSACTION);
gnc_price_set_typestr (price, PRICE_TYPE_UNK);
gnc_price_set_value (price, p);
gnc_price_commit_edit (price);
auto book = gnc_get_current_book ();
auto pdb = gnc_pricedb_get_db (book);
if (!gnc_pricedb_add_price (pdb, price))
PWARN ("error adding price");
gnc_price_unref (price);
}
void
stock_assistant_finish (GtkAssistant *assistant, gpointer user_data)
{
auto info = static_cast<StockTransactionInfo*>(user_data);
AccountVec account_commits;