28 #include <glib/gi18n.h> 42 #include "engine-helpers.h" 43 #include "dialog-utils.h" 44 #include "assistant-stock-transaction.h" 45 #include "gnc-account-sel.h" 46 #include "gnc-amount-edit.h" 48 #include <gnc-date-edit.h> 51 #include "gnc-numeric.hpp" 53 #include "gnc-component-manager.h" 54 #include "gnc-date-edit.h" 59 #include "gnc-split-reg.h" 61 static QofLogModule log_module = GNC_MOD_ASSISTANT;
86 void stock_assistant_prepare_cb (GtkAssistant *assistant, GtkWidget *page,
88 void stock_assistant_finish_cb (GtkAssistant *assistant, gpointer user_data);
89 void stock_assistant_cancel_cb (GtkAssistant *gtkassistant, gpointer user_data);
92 static const char* GNC_PREFS_GROUP =
"dialogs.stock-assistant";
93 static const char* ASSISTANT_STOCK_TRANSACTION_CM_CLASS =
"assistant-stock-transaction";
95 static const char* DIVIDEND_KVP_TAG =
"stock-dividends";
96 static const char* CAPGAINS_KVP_TAG =
"stock-capgains";
97 static const char* PROCEEDS_KVP_TAG =
"stock-cash-proceeds";
98 static const char* FEES_KVP_TAG =
"stock-broker-fees";
102 enum class FieldMask : unsigned
106 ENABLED_CREDIT = 1 << 1,
107 AMOUNT_DEBIT = 1 << 2,
108 AMOUNT_CREDIT = 1 << 3,
109 INPUT_NEW_BALANCE = 1 << 4,
111 ALLOW_NEGATIVE = 1 << 6,
112 CAPITALIZE_DEFAULT = 1 << 7,
113 CAPGAINS_IN_STOCK = 1 << 8,
114 MARKER_SPLIT = 1 << 9,
119 operator |(FieldMask lhs, FieldMask rhs)
121 return static_cast<FieldMask
> (
static_cast<unsigned>(lhs) |
122 static_cast<unsigned>(rhs));
126 operator &(FieldMask lhs, FieldMask rhs)
128 return (static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
139 FieldMask stock_amount;
140 FieldMask cash_value;
141 FieldMask fees_value;
142 FieldMask dividend_value;
143 FieldMask capgains_value;
144 const char* friendly_name;
145 const char* explanation;
148 using StringVec = std::vector<std::string>;
149 using TxnTypeVec = std::vector<TxnTypeInfo>;
150 using AccountVec = std::vector<Account*>;
152 static const TxnTypeVec starting_types
156 FieldMask::ENABLED_DEBIT | FieldMask::AMOUNT_DEBIT,
157 FieldMask::ENABLED_CREDIT,
158 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO | FieldMask::CAPITALIZE_DEFAULT,
164 N_(
"Initial stock long purchase.")
167 FieldMask::ENABLED_CREDIT | FieldMask::AMOUNT_CREDIT,
168 FieldMask::ENABLED_DEBIT,
169 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO | FieldMask::CAPITALIZE_DEFAULT,
175 N_(
"Initial stock short sale.")
179 static const TxnTypeVec long_types
182 FieldMask::ENABLED_DEBIT | FieldMask::AMOUNT_DEBIT,
183 FieldMask::ENABLED_CREDIT,
184 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO | FieldMask::CAPITALIZE_DEFAULT,
190 N_(
"Buying stock long.")
193 FieldMask::ENABLED_CREDIT | FieldMask::AMOUNT_CREDIT,
194 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO,
195 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO,
197 FieldMask::ENABLED_CREDIT | FieldMask::ALLOW_ZERO | FieldMask::ALLOW_NEGATIVE | FieldMask::CAPGAINS_IN_STOCK,
201 N_(
"Selling stock long, and record capital gain/loss." 202 "\n\nIf you are unable to calculate capital gains you can enter a " 203 "placeholder amount and correct it in the transaction later.")
206 FieldMask::MARKER_SPLIT,
207 FieldMask::ENABLED_DEBIT,
208 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO,
209 FieldMask::ENABLED_CREDIT,
214 N_(
"Company issues cash dividends to holder.\n\nAny dividend being " 215 "reinvested must be subsequently recorded as a regular stock purchase.")
218 FieldMask::ENABLED_CREDIT,
219 FieldMask::ENABLED_DEBIT,
220 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO | FieldMask::CAPITALIZE_DEFAULT,
225 N_(
"Return of capital"),
226 N_(
"Company returns capital, reducing the cost basis without affecting # units.")
229 FieldMask::ENABLED_CREDIT,
231 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO | FieldMask::CAPITALIZE_DEFAULT,
232 FieldMask::ENABLED_DEBIT,
236 N_(
"Return of capital (reclassification)"),
237 N_(
"Company returns capital, reducing the cost basis without affecting # units. " 238 "A distribution previously recorded as a dividend is reclassified to return " 239 "of capital, often due to end-of-year tax information.")
242 FieldMask::ENABLED_DEBIT,
244 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO,
245 FieldMask::ENABLED_CREDIT,
249 N_(
"Notional distribution (dividend)"),
250 N_(
"Company issues a notional distribution, which is recorded as dividend " 251 "income and increases the cost basis without affecting # units.")
254 FieldMask::ENABLED_DEBIT,
256 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO,
258 FieldMask::ENABLED_CREDIT,
261 N_(
"Notional distribution (capital gain)"),
262 N_(
"Company issues a notional distribution, which is recorded as capital gain " 263 "and increases the cost basis without affecting # units.")
266 FieldMask::AMOUNT_DEBIT | FieldMask::INPUT_NEW_BALANCE,
267 FieldMask::ENABLED_CREDIT | FieldMask::ALLOW_ZERO,
268 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO | FieldMask::CAPITALIZE_DEFAULT,
274 N_(
"Company issues additional units, thereby reducing the stock price by a divisor, " 275 "while keeping the total monetary value of the overall investment constant.\n\nIf " 276 "the split results in a cash in lieu for remainder units, please " 277 "record the sale using the Stock Transaction Assistant first, then record the split.")
280 FieldMask::AMOUNT_CREDIT | FieldMask::INPUT_NEW_BALANCE,
281 FieldMask::ENABLED_CREDIT | FieldMask::ALLOW_ZERO,
282 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO | FieldMask::CAPITALIZE_DEFAULT,
287 N_(
"Company redeems units, thereby increasing the stock price by a multiple, while " 288 "keeping the total monetary value of the overall investment constant.\n\nIf the " 289 "reverse split results in a cash in lieu for remainder units, please record the " 290 "sale using the Stock Transaction Assistant first, then record the reverse split.")
294 static const TxnTypeVec short_types
297 FieldMask::ENABLED_CREDIT | FieldMask::AMOUNT_CREDIT,
298 FieldMask::ENABLED_DEBIT,
299 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO | FieldMask::CAPITALIZE_DEFAULT,
305 N_(
"Selling stock short.")
308 FieldMask::ENABLED_DEBIT | FieldMask::AMOUNT_DEBIT,
309 FieldMask::ENABLED_CREDIT | FieldMask::ALLOW_ZERO,
310 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO,
312 FieldMask::ENABLED_CREDIT | FieldMask::ALLOW_ZERO | FieldMask::ALLOW_NEGATIVE | FieldMask::CAPGAINS_IN_STOCK,
315 N_(
"Buy to cover short"),
316 N_(
"Buy back stock to cover short position, and record capital gain/loss.\n\nIf " 317 "you are unable to calculate capital gains you can enter a placeholder " 318 "amount and correct it in the transaction later.")
321 FieldMask::MARKER_SPLIT,
322 FieldMask::ENABLED_CREDIT,
323 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO,
324 FieldMask::ENABLED_DEBIT,
328 N_(
"Compensatory dividend"),
329 N_(
"Company issues dividends, and the short stock holder must make a compensatory " 330 "payment for the dividend.")
333 FieldMask::ENABLED_DEBIT,
334 FieldMask::ENABLED_CREDIT,
335 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO | FieldMask::CAPITALIZE_DEFAULT,
340 N_(
"Compensatory return of capital"),
341 N_(
"Company returns capital, and the short stock holder must make a compensatory " 342 "payment for the returned capital. This reduces the cost basis (less negative, " 343 "towards 0.00 value) without affecting # units.")
346 FieldMask::ENABLED_DEBIT,
348 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO | FieldMask::CAPITALIZE_DEFAULT,
349 FieldMask::ENABLED_CREDIT,
354 N_(
"Compensatory return of capital (reclassification)"),
355 N_(
"Company returns capital, and the short stock holder must make a compensatory " 356 "payment for the returned capital. This reduces the cost basis (less negative, " 357 "towards 0.00 value) without affecting # units. A distribution previously recorded " 358 "as a compensatory dividend is reclassified to compensatory return of capital, " 359 "often due to end-of-year tax information.")
362 FieldMask::ENABLED_CREDIT,
364 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO,
365 FieldMask::ENABLED_DEBIT,
370 N_(
"Compensatory notional distribution (dividend)"),
371 N_(
"Company issues a notional distribution, and the short stock holder must make a " 372 "compensatory payment for the notional distribution. This is recorded as a " 373 "loss/negative dividend income amount, and increases the cost basis (more " 374 "negative, away from 0.00 value) without affecting # units.")
377 FieldMask::ENABLED_CREDIT,
379 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO,
381 FieldMask::ENABLED_DEBIT,
385 N_(
"Compensatory notional distribution (capital gain)"),
386 N_(
"Company issues a notional distribution, and the short stock holder must make " 387 "a compensatory payment for the notional distribution. This is recorded as a " 388 "capital loss amount, and increases the cost basis (more negative, away from " 389 "0.00 value) without affecting # units.")
392 FieldMask::AMOUNT_CREDIT | FieldMask::INPUT_NEW_BALANCE,
393 FieldMask::ENABLED_CREDIT | FieldMask::ALLOW_ZERO,
394 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO | FieldMask::CAPITALIZE_DEFAULT,
400 N_(
"Company issues additional units, thereby reducing the stock price by a divisor, " 401 "while keeping the total monetary value of the overall investment constant.\n\nIf " 402 "the split results in a cash in lieu for remainder units, please " 403 "record the cover buy using the Stock Transaction Assistant first, then record the split.")
406 FieldMask::AMOUNT_DEBIT | FieldMask::INPUT_NEW_BALANCE,
407 FieldMask::ENABLED_CREDIT | FieldMask::ALLOW_ZERO,
408 FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO | FieldMask::CAPITALIZE_DEFAULT,
414 N_(
"Company redeems units, thereby increasing the stock price by a multiple, while " 415 "keeping the total monetary value of the overall investment constant.\n\nIf the " 416 "reverse split results in a cash in lieu for remainder units, please record the " 417 "cover buy using the Stock Transaction Assistant first, then record the reverse split.")
421 enum class LogMsgType
431 const std::string m_message;
433 LogMessage(LogMsgType type, std::string&& message) :
434 m_type{type}, m_message(std::move(message)) {}
435 LogMessage(LogMsgType type,
const char* message) :
436 m_type{type}, m_message(message) {}
440 LogMsgType type() {
return m_type; }
441 const std::string& message() {
return m_message; }
450 using Log = std::vector<LogMessage>;
456 void info(
const char* message) { m_log.emplace_back(LogMsgType::info, message); }
457 void warn(
const char* message) { m_log.emplace_back(LogMsgType::warning, message); }
458 void error(
const char* message) { m_log.emplace_back(LogMsgType::error, message); }
459 void clear() { m_log.clear(); }
462 void write_log(std::stringstream& stream, LogMsgType type);
463 void infos(std::stringstream& stream) {
return write_log(stream, LogMsgType::info); }
464 void warnings(std::stringstream& stream) {
return write_log(stream, LogMsgType::warning); }
465 void errors(std::stringstream& stream) {
return write_log(stream, LogMsgType::error); }
475 Logger::write_log(std::stringstream& stream, LogMsgType type)
477 std::for_each(m_log.begin(), m_log.end(),
479 if (msg.type() == type)
480 stream <<
"\n * " << msg.message();
485 Logger::has_warnings()
487 return std::any_of(m_log.begin(), m_log.end(),
488 [](
auto& msg){
return msg.type() == LogMsgType::warning;
495 return std::any_of(m_log.begin(), m_log.end(),
496 [](
auto& msg){
return msg.type() == LogMsgType::error;
503 std::stringstream summary;
506 summary << _(
"No errors found. Click Apply to create transaction.");
511 summary << _(
"The following errors must be fixed:");
516 summary <<
"\n\n" << _(
"The following warnings exist:");
519 return summary.str();
527 void* handler_data, [[maybe_unused]]
void* event_data);
542 bool m_fieldmask_debit_side =
false;
544 bool m_allow_negative =
false;
545 bool m_input_new_balance =
false;
549 const char* m_action;
550 gnc_numeric m_balance = gnc_numeric_zero();
551 const char* m_kvp_tag;
552 int m_qof_event_handler;
555 m_enabled{
false}, m_debit_side{
false}, m_allow_zero{
false}, m_account{
nullptr},
559 m_enabled{
false}, m_debit_side{
false}, m_allow_zero{
false}, m_account{
nullptr},
569 virtual bool enabled()
const {
return m_enabled; }
570 virtual bool debit_side()
const {
return m_debit_side; }
571 virtual void set_capitalize(
bool capitalize) {}
572 virtual bool input_new_balance()
const {
return m_input_new_balance; }
573 virtual bool do_capitalize()
const {
return false; }
574 virtual void set_account(
Account* account) { m_account = account; }
575 virtual Account* account()
const {
return m_account; }
576 virtual const char* print_account()
const;
577 virtual void set_memo(
const char* memo) { m_memo = memo; }
578 virtual const char* get_kvp_tag () {
return m_kvp_tag; }
579 virtual const char* memo()
const {
return m_memo; }
580 virtual void set_value(gnc_numeric amount);
582 virtual void set_amount(gnc_numeric) {}
583 virtual gnc_numeric amount()
const {
return m_value; }
584 virtual bool has_amount()
const {
return false; }
585 virtual bool marker_split()
const {
return false; }
591 virtual void validate_amount(
Logger&)
const;
592 virtual void set_balance(gnc_numeric balance) { m_balance = balance; }
593 virtual gnc_numeric get_balance()
const {
return m_balance; }
601 virtual void create_split(Transaction* trans, AccountVec& commits)
const;
609 virtual const char*
print_amount(gnc_numeric amt)
const;
633 void* handler_data, [[maybe_unused]]
void* event_data)
636 if ((inst && inst != QOF_INSTANCE(entry->account())) || (
event & QOF_EVENT_DESTROY) == 0)
638 entry->set_account(
nullptr);
641 using StockTransactionEntryPtr = std::unique_ptr<StockTransactionEntry>;
646 m_enabled = mask != FieldMask::DISABLED;
647 m_debit_side = mask & FieldMask::ENABLED_DEBIT;
648 m_fieldmask_debit_side = m_debit_side;
649 m_allow_zero = mask & FieldMask::ALLOW_ZERO;
650 m_allow_negative = mask & FieldMask::ALLOW_NEGATIVE;
654 StockTransactionEntry::print_account()
const 656 auto acct_required = m_enabled &&
660 acct_required ? _(
"missing") :
"";
664 StockTransactionEntry::set_value(gnc_numeric amount)
675 PINFO(
"Set %s value to %" PRId64
"/%" PRId64, m_action, m_value.num, m_value.denom);
679 StockTransactionEntry::validate_amount(
Logger& logger)
const 681 auto add_error = [&logger](
const char* format_str,
const char* arg)
683 char *buf = g_strdup_printf (_(format_str),
684 g_dpgettext2 (
nullptr,
"Stock Assistant: Page name", arg));
693 add_error (N_(
"Amount for %s is missing."), m_action);
698 add_error (N_(
"Amount for %s must not be negative."), m_action);
701 add_error (N_(
"Amount for %s must be positive."), m_action);
704 add_error(N_(
"The %s amount has no associated account."), m_action);
724 auto pinfo{gnc_commodity_print_info(currency, TRUE)};
734 auto pinfo{gnc_commodity_print_info(commodity, TRUE)};
739 StockTransactionEntry::create_split(Transaction *trans, AccountVec &account_commits)
const 741 g_return_if_fail(trans);
745 xaccSplitSetParent(split, trans);
747 account_commits.push_back(m_account);
748 xaccSplitSetAccount(split, m_account);
753 PINFO(
"creating %s split in Acct(%s): Val(%s), Amt(%s) => Val(%s), Amt(%s)",
759 gnc_set_num_action(
nullptr, split,
nullptr,
760 g_dpgettext2(
nullptr,
"Stock Assistant: Action field",
772 auto pinfo{gnc_price_print_info(currency, TRUE)};
786 bool m_amount_enabled;
787 gnc_numeric m_amount;
788 bool m_marker =
false;
793 PINFO(
"Stock Entry");
798 PINFO(
"Stock Entry");
801 void set_amount(gnc_numeric amount)
override;
802 gnc_numeric amount()
const override {
return m_amount; }
803 bool has_amount()
const override {
return m_amount_enabled; }
804 void validate_amount(
Logger& logger)
const override;
805 void create_split(Transaction *trans, AccountVec &account_commits)
const override;
808 bool marker_split()
const override {
return m_marker; }
815 m_enabled = mask & (FieldMask::ENABLED_CREDIT | FieldMask::ENABLED_DEBIT);
816 m_amount_enabled = mask & (FieldMask::AMOUNT_CREDIT | FieldMask::AMOUNT_DEBIT);
817 m_debit_side = mask & (FieldMask::ENABLED_DEBIT | FieldMask::AMOUNT_DEBIT);
818 m_fieldmask_debit_side = m_debit_side;
819 m_input_new_balance = mask & FieldMask::INPUT_NEW_BALANCE;
820 m_marker = mask & FieldMask::MARKER_SPLIT;
825 StockTransactionStockEntry::set_amount(gnc_numeric amount)
833 if (m_input_new_balance)
836 m_amount = gnc_numeric_sub_fixed(amount, m_balance);
838 m_amount = gnc_numeric_sub_fixed(m_balance, amount);
850 StockTransactionStockEntry::validate_amount(
Logger& logger)
const 853 StockTransactionEntry::validate_amount(logger);
855 if (!m_amount_enabled)
858 auto add_error_str = [&logger]
859 (
const char* str) { logger.error (_(str)); };
863 add_error_str(_(
"Amount for stock value is missing."));
867 if (m_input_new_balance)
869 auto amount = gnc_numeric_add_fixed(m_debit_side ? m_amount :
gnc_numeric_neg(m_amount), m_balance);
870 auto delta = gnc_numeric_sub_fixed(amount, m_balance);
875 add_error_str(N_(
"Invalid stock new balance."));
877 add_error_str(N_(
"New balance must be higher than old balance."));
879 add_error_str(N_(
"New balance must be lower than old balance."));
881 PINFO(
"Delta %" PRId64
"/%" PRId64
", Ratio %" PRId64
"/%" PRId64, delta.num, delta.denom, ratio.num, ratio.denom);
886 add_error_str(N_(
"Stock amount must be positive."));
888 auto new_bal = gnc_numeric_add_fixed(m_balance, m_amount);
890 add_error_str(N_(
"Cannot sell more units than owned."));
892 add_error_str(N_(
"Cannot cover buy more units than owed."));
903 if (m_input_new_balance)
909 PINFO(
"Computed ratio %" PRId64
"/%" PRId64
"; amount %" PRId64
910 "/%" PRId64
" and balance %" PRId64
"/%" PRId64,
911 ratio.num, ratio.denom, amount.num, amount.denom, m_balance.num, m_balance.denom);
915 std::ostringstream ret;
916 ret << ratio.num <<
':' << ratio.denom;
922 amount = gnc_numeric_add_fixed (amount, m_balance);
931 StockTransactionStockEntry::create_split(Transaction *trans, AccountVec &account_commits)
const 933 g_return_if_fail(trans);
937 xaccSplitSetParent(split, trans);
939 account_commits.push_back(m_account);
940 xaccSplitSetAccount(split, m_account);
944 if (m_amount_enabled)
946 if (m_amount_enabled && !m_enabled)
948 PINFO(
"creating %s split in Acct(%s): Val(%s), Amt(%s) => Val(%s), Amt(%s)",
954 gnc_set_num_action(
nullptr, split,
nullptr,
955 g_dpgettext2(
nullptr,
"Stock Assistant: Action field",
962 if (m_input_new_balance ||
973 auto ainfo{gnc_commodity_print_info (comm,
true)};
974 auto pinfo{gnc_price_print_info (curr,
true)};
975 auto vinfo{gnc_commodity_print_info (curr,
true)};
977 PINFO(
"Calculated price %s from value %s and amount %s",
994 gnc_numeric amount()
const {
return gnc_numeric_zero(); }
997 StockTransactionStockCapGainsEntry::StockTransactionStockCapGainsEntry(
const StockTransactionEntry *cg_entry,
1001 m_debit_side = !m_debit_side;
1002 m_account = stk_entry->account();
1016 void set_capitalize(
bool capitalize)
override { m_capitalize = capitalize; }
1017 bool do_capitalize()
const override {
return m_capitalize; }
1018 void validate_amount(
Logger &logger)
const override;
1019 void create_split(Transaction *trans, AccountVec &commits)
const override;
1026 m_capitalize = mask & FieldMask::CAPITALIZE_DEFAULT;
1030 StockTransactionFeesEntry::validate_amount(
Logger& logger)
const 1032 auto add_error = [&logger](
const char* format_str,
const char* arg)
1034 char *buf = g_strdup_printf (_(format_str),
1035 g_dpgettext2 (
nullptr,
"Stock Assistant: Page name", arg));
1044 add_error (N_(
"Amount for %s is missing."), m_action);
1049 add_error (N_(
"Amount for %s must not be negative."), m_action);
1052 add_error (N_(
"Amount for %s must be positive."), m_action);
1055 add_error(N_(
"The %s amount has no associated account."), m_action);
1059 StockTransactionFeesEntry::create_split(Transaction* trans, AccountVec& commits)
const 1061 g_return_if_fail(trans);
1065 xaccSplitSetParent(split, trans);
1068 xaccSplitSetAccount(split, commits[0]);
1073 commits.push_back(m_account);
1074 xaccSplitSetAccount(split, m_account);
1079 PINFO(
"creating %s split in Acct(%s): Val(%s), Amt(%s) => Val(%s), Amt(%s)",
1085 gnc_set_num_action(
nullptr, split,
nullptr,
1086 g_dpgettext2(
nullptr,
"Stock Assistant: Action field",
1090 using EntryVec = std::vector<StockTransactionEntry*>;
1092 static void stock_assistant_model_date_changed_cb(GtkWidget*,
void*);
1093 static void stock_assistant_model_description_changed_cb(GtkWidget *,
void *);
1104 gnc_commodity* m_currency;
1105 time64 m_transaction_date;
1106 const char* m_transaction_description;
1107 std::optional<TxnTypeVec> m_txn_types;
1109 std::optional<TxnTypeInfo> m_txn_type;
1111 StockTransactionEntryPtr m_stock_entry;
1112 StockTransactionEntryPtr m_cash_entry;
1113 StockTransactionEntryPtr m_fees_entry;
1114 StockTransactionEntryPtr m_dividend_entry;
1115 StockTransactionEntryPtr m_capgains_entry;
1116 StockTransactionEntryPtr m_stock_cg_entry;
1119 std::optional<time64> m_txn_types_date;
1120 bool m_ready_to_create =
false;
1122 EntryVec m_list_of_splits;
1128 m_stock_entry{std::make_unique<StockTransactionStockEntry>(
1129 NC_(
"Stock Assistant: Page name",
"Stock"))},
1130 m_cash_entry{std::make_unique<StockTransactionEntry>(
1131 NC_(
"Stock Assistant: Page name",
"Cash"), PROCEEDS_KVP_TAG)},
1132 m_fees_entry{std::make_unique<StockTransactionFeesEntry>(
1133 NC_(
"Stock Assistant: Page name",
"Fees"), FEES_KVP_TAG)},
1134 m_dividend_entry{std::make_unique<StockTransactionEntry>(
1135 NC_(
"Stock Assistant: Page name",
"Dividend"), DIVIDEND_KVP_TAG)},
1136 m_capgains_entry{std::make_unique<StockTransactionEntry>(
1137 NC_(
"Stock Assistant: Page name",
"Capital Gains"),
1138 CAPGAINS_KVP_TAG)} {
1139 DEBUG (
"StockAssistantModel constructor\n");
1140 m_stock_entry->set_account(m_acct);
1145 DEBUG (
"StockAssistantModel destructor\n");
1185 const std::optional<TxnTypeInfo>&
txn_type() {
return m_txn_type; }
1238 Account* account() {
return m_acct; }
1243 void add_price (
QofBook *book);
1249 auto old_bal = m_stock_entry->get_balance();
1252 if (m_txn_types_date && m_txn_types_date == m_transaction_date &&
1255 m_stock_entry->set_balance(new_bal);
1256 m_txn_types_date = m_transaction_date;
1266 if (!m_txn_types_date || m_txn_types_date != m_transaction_date)
1268 PERR (
"transaction_date has changed. rerun maybe_reset_txn_types!");
1273 m_txn_type = m_txn_types->at (type_idx);
1275 catch (
const std::out_of_range&)
1277 PERR (
"out of range type_idx=%d", type_idx);
1281 m_stock_entry->set_fieldmask(m_txn_type->stock_amount);
1282 m_fees_entry->set_fieldmask(m_txn_type->fees_value);
1283 m_capgains_entry->set_fieldmask(m_txn_type->capgains_value);
1284 m_dividend_entry->set_fieldmask(m_txn_type->dividend_value);
1285 m_cash_entry->set_fieldmask(m_txn_type->cash_value);
1290 check_txn_date(
const Split* last_split,
time64 txn_date,
Logger& logger)
1293 if (txn_date <= last_split_date) {
1299 auto warn_txt = g_strdup_printf(
1300 _(
"You will enter a transaction " 1301 "with date %s which is earlier than the latest transaction in this account, " 1302 "dated %s. Doing so may affect the cost basis, and therefore capital gains, " 1303 "of transactions dated after the new entry. Please review all transactions " 1304 "to ensure proper recording."),
1305 new_date_str, last_split_date_str);
1306 logger.warn(warn_txt);
1308 g_free(new_date_str);
1309 g_free(last_split_date_str);
1313 std::tuple<bool, std::string, EntryVec>
1315 if (!m_txn_types || !m_txn_type)
1316 return {
false,
"Error: txn_type not initialized", {} };
1319 m_list_of_splits.clear();
1328 if (
const auto& splits = xaccAccountGetSplits (m_acct); !splits.empty())
1329 check_txn_date(splits.back(), m_transaction_date, m_logger);
1331 if (m_stock_entry->enabled() || m_stock_entry->has_amount())
1333 m_stock_entry->validate_amount(m_logger);
1334 m_list_of_splits.push_back(m_stock_entry.get());
1336 auto price{m_stock_entry->calculate_price()};
1341 auto tmpl = N_(
"A price of 1 %s = %s on %s will be recorded.");
1343 auto price_msg = g_strdup_printf
1346 m_stock_entry->print_price(), date_str);
1347 m_logger.info(price_msg);
1353 if (m_stock_entry->marker_split())
1354 m_list_of_splits.push_back(m_stock_entry.get());
1356 if (m_cash_entry->enabled())
1358 m_cash_entry->validate_amount(m_logger);
1359 m_list_of_splits.push_back (m_cash_entry.get());
1362 if (m_fees_entry->enabled())
1364 m_fees_entry->validate_amount(m_logger);
1365 if (m_fees_entry->do_capitalize())
1366 m_fees_entry->set_account(m_acct);
1367 m_list_of_splits.push_back (m_fees_entry.get());
1370 if (m_dividend_entry->enabled())
1372 m_dividend_entry->validate_amount(m_logger);
1373 m_list_of_splits.push_back (m_dividend_entry.get());
1376 if (m_capgains_entry->enabled())
1379 std::make_unique<StockTransactionStockCapGainsEntry>(m_capgains_entry.get(),
1380 m_stock_entry.get());
1381 m_stock_cg_entry->validate_amount(m_logger);
1382 m_capgains_entry->validate_amount(m_logger);
1383 m_list_of_splits.push_back(m_stock_cg_entry.get());
1384 m_list_of_splits.push_back (m_capgains_entry.get());
1387 std::for_each(m_list_of_splits.begin(), m_list_of_splits.end(),
1388 [&debit, &credit](
auto& entry) {
1389 if (entry->debit_side())
1390 debit += entry->value();
1392 credit += entry->value();
1397 const char *err_act = NULL, *err_reason = NULL;
1411 const char *balance_str = N_(
"Transaction can't balance, %s is error value %s");
1412 auto err_str = g_strdup_printf (_(balance_str), err_act, err_reason);
1413 m_logger.error(err_str);
1418 auto imbalance_str = N_(
"Total Debits of %s does not balance with total Credits of %s.");
1419 auto pinfo{gnc_commodity_print_info (m_currency,
true)};
1422 auto error_str = g_strdup_printf (_(imbalance_str), debit_str, credit_str);
1423 m_logger.error (error_str);
1425 g_free (credit_str);
1432 m_ready_to_create = !m_logger.has_errors();
1433 return { m_ready_to_create, m_logger.
report(), m_list_of_splits };
1436 std::tuple<bool, Transaction*>
1439 if (!m_ready_to_create)
1441 PERR (
"errors exist. cannot create transaction.");
1442 m_list_of_splits.clear();
1443 return {
false,
nullptr};
1451 AccountVec accounts;
1452 std::for_each (m_list_of_splits.begin(), m_list_of_splits.end(),
1455 entry->create_split (trans, accounts);
1456 if (entry->get_kvp_tag() && entry->account())
1462 m_list_of_splits.clear();
1463 m_ready_to_create =
false;
1464 return {
true, trans};
1468 StockAssistantModel::add_price (
QofBook *book)
1470 auto stock_price{m_stock_entry->calculate_price()};
1475 gnc_price_begin_edit (price);
1477 gnc_price_set_currency (price, m_currency);
1478 gnc_price_set_time64 (price, m_transaction_date);
1479 gnc_price_set_source (price, PRICE_SOURCE_STOCK_TRANSACTION);
1480 gnc_price_set_typestr (price, PRICE_TYPE_UNK);
1481 gnc_price_set_value (price, stock_price);
1482 gnc_price_commit_edit (price);
1486 PWARN (
"error adding price");
1492 stock_assistant_model_date_changed_cb(GtkWidget* widget,
void* data)
1499 stock_assistant_model_description_changed_cb(GtkWidget* widget,
void* data)
1511 entry->set_memo(gtk_entry_get_text (GTK_ENTRY (widget)));
1515 static inline GtkWidget*
1516 get_widget (GtkBuilder *builder,
const gchar * ID)
1518 g_return_val_if_fail (builder && ID,
nullptr);
1519 auto obj = gtk_builder_get_object (builder, ID);
1521 PWARN (
"get_widget ID '%s' not found. it may be a typo?", ID);
1522 return GTK_WIDGET (obj);
1534 m_edit{gnc_date_edit_new(
gnc_time(
nullptr), FALSE, FALSE)} {}
1535 void attach(GtkBuilder *builder,
const char *table_ID,
const char *label_ID,
1537 time64 get_date_time() {
return gnc_date_edit_get_date_end(GNC_DATE_EDIT(m_edit)); }
1538 void connect(GCallback, gpointer);
1542 GncDateEdit::attach(GtkBuilder *builder,
const char *table_ID,
1543 const char *label_ID,
int row)
1545 auto table = get_widget(builder, table_ID);
1546 auto label = get_widget (builder, label_ID);
1547 gtk_grid_attach(GTK_GRID(
table), m_edit, 1, row, 1, 1);
1548 gtk_widget_show(m_edit);
1549 gnc_date_make_mnemonic_target (GNC_DATE_EDIT(m_edit), label);
1553 GncDateEdit::connect(GCallback cb, gpointer data)
1555 g_signal_connect(m_edit,
"date_changed", cb, data);
1565 GncAmountEdit (GtkBuilder *builder, gnc_commodity *commodity);
1566 void attach (GtkBuilder *builder,
const char *table_id,
1567 const char *label_ID,
int row);
1568 GtkWidget* widget() {
1569 return gnc_amount_edit_gtk_entry(GNC_AMOUNT_EDIT(m_edit));
1572 void connect (GCallback cb, gpointer data);
1573 void set_owner (gpointer obj);
1579 g_return_if_fail(GNC_IS_AMOUNT_EDIT(widget));
1581 auto invalid{gnc_amount_edit_expr_is_valid(GNC_AMOUNT_EDIT(widget),
1582 &value,
true,
nullptr)};
1586 GncAmountEdit::GncAmountEdit (GtkBuilder *builder, gnc_commodity *commodity) :
1587 m_edit{gnc_amount_edit_new()}
1590 auto info = gnc_commodity_print_info(commodity,
true);
1591 gnc_amount_edit_set_evaluate_on_enter(GNC_AMOUNT_EDIT(m_edit), TRUE);
1592 gnc_amount_edit_set_print_info(GNC_AMOUNT_EDIT(m_edit), info);
1596 GncAmountEdit::attach (GtkBuilder *builder,
const char *table_ID,
1597 const char* label_ID,
int row)
1599 auto table = get_widget(builder, table_ID);
1600 auto label = get_widget(builder, label_ID);
1601 gtk_grid_attach(GTK_GRID(
table), m_edit, 1, row, 1, 1);
1602 gtk_widget_show(m_edit);
1603 gnc_amount_edit_make_mnemonic_target(GNC_AMOUNT_EDIT(m_edit), label);
1607 GncAmountEdit::get ()
1610 if (!gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT(m_edit), &amt,
true,
nullptr))
1616 GncAmountEdit::connect (GCallback cb, gpointer data)
1618 g_signal_connect(m_edit,
"changed", cb, data);
1622 GncAmountEdit::set_owner(gpointer obj)
1624 g_object_set_data(G_OBJECT (m_edit),
"owner", obj);
1627 using AccountTypeList = std::vector<GNCAccountType>;
1634 GtkWidget* m_selector;
1637 gnc_commodity *currency,
Account *default_acct);
1638 void attach (GtkBuilder *builder,
const char *table_id,
1639 const char *label_ID,
int row);
1641 void set (
Account *acct) { gnc_account_sel_set_account (GNC_ACCOUNT_SEL (m_selector), acct, TRUE); }
1642 void set_sensitive(
bool sensitive);
1643 Account *
get () {
return gnc_account_sel_get_account (GNC_ACCOUNT_SEL (m_selector)); }
1649 g_return_if_fail (GNC_IS_ACCOUNT_SEL (widget));
1650 entry->set_account(gnc_account_sel_get_account (GNC_ACCOUNT_SEL (widget)));
1653 GncAccountSelector::GncAccountSelector (GtkBuilder *builder, AccountTypeList types,
1654 gnc_commodity *currency,
Account *default_acct) :
1655 m_selector{gnc_account_sel_new ()}
1657 auto accum = [](
auto a,
auto b) {
return g_list_prepend(a, (gpointer)b); };
1658 auto null_glist =
static_cast<GList *
>(
nullptr);
1659 auto acct_list = std::accumulate(types.begin(), types.end(), null_glist, accum);
1660 auto curr_list = accum(null_glist, currency);
1661 gnc_account_sel_set_new_account_ability(GNC_ACCOUNT_SEL(m_selector),
true);
1662 gnc_account_sel_set_acct_filters(GNC_ACCOUNT_SEL(m_selector), acct_list, curr_list);
1663 gnc_account_sel_set_default_new_commodity(GNC_ACCOUNT_SEL(m_selector), currency);
1664 gnc_account_sel_set_new_account_modal (GNC_ACCOUNT_SEL(m_selector),
true);
1666 gnc_account_sel_set_account (GNC_ACCOUNT_SEL(m_selector), default_acct,
true);
1667 g_list_free(acct_list);
1668 g_list_free(curr_list);
1672 GncAccountSelector::attach (GtkBuilder *builder,
const char *table_ID,
1673 const char *label_ID,
int row)
1675 auto table = get_widget(builder, table_ID);
1676 auto label = get_widget(builder, label_ID);
1677 gtk_grid_attach(GTK_GRID(
table), m_selector, 1, row, 1, 1);
1678 gtk_widget_show(m_selector);
1679 gtk_label_set_mnemonic_widget(GTK_LABEL(label), m_selector);
1685 g_signal_connect(m_selector,
"account_sel_changed", G_CALLBACK (gnc_account_sel_changed_cb), entry);
1689 GncAccountSelector::set_sensitive(
bool sensitive)
1691 gtk_widget_set_sensitive(m_selector, sensitive);
1707 assistant_page_set_focus(GtkWidget* page, [[maybe_unused]]GtkDirectionType type, GtkWidget* entry)
1709 gtk_widget_grab_focus(entry);
1710 g_signal_handlers_disconnect_by_data(page, entry);
1746 GtkWidget * m_explanation;
1750 int get_transaction_type_index ();
1751 void set_transaction_types (
const TxnTypeVec& txn_types);
1763 PageTransType::PageTransType(GtkBuilder *builder)
1764 : m_page(get_widget(builder,
"transaction_type_page")),
1765 m_type(get_widget(builder,
"transaction_type_page_combobox")),
1766 m_explanation(get_widget(builder,
"transaction_type_page_explanation"))
1768 g_object_set_data(G_OBJECT(m_type),
"owner",
this);
1774 auto me =
static_cast<PageTransType *
>(g_object_get_data (G_OBJECT (widget),
"owner"));
1775 g_return_if_fail (me);
1776 me->change_txn_type (model);
1786 set_transaction_types(txn_types.value());
1787 change_txn_type (model);
1788 g_signal_connect(m_page,
"focus", G_CALLBACK(assistant_page_set_focus), m_type);
1792 PageTransType::get_transaction_type_index ()
1794 return gtk_combo_box_get_active (GTK_COMBO_BOX (m_type));
1798 PageTransType::set_transaction_types (
const TxnTypeVec& txn_types)
1800 auto combo = GTK_COMBO_BOX_TEXT (m_type);
1801 gtk_combo_box_text_remove_all (combo);
1802 std::for_each (txn_types.begin(), txn_types.end(),
1803 [&combo](
const auto& it)
1804 { gtk_combo_box_text_append_text (combo, _(it.friendly_name)); });
1805 gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
1811 gtk_label_set_text (GTK_LABEL (this->m_explanation), txt);
1817 auto type_idx = get_transaction_type_index();
1830 g_signal_connect(m_type,
"changed",
1831 G_CALLBACK (page_trans_type_changed_cb), model);
1844 GtkWidget *m_description;
1847 time64 get_date_time () {
return m_date.get_date_time(); }
1848 const char* get_description () {
return gtk_entry_get_text (GTK_ENTRY (m_description)); }
1853 PageTransDeets::PageTransDeets (GtkBuilder *builder) :
1854 m_page (get_widget (builder,
"transaction_details_page")),
1856 m_description (get_widget (builder,
"transaction_description_entry"))
1858 m_date.attach(builder,
"transaction_details_table",
"transaction_date_label", 0);
1864 m_date.connect(G_CALLBACK (stock_assistant_model_date_changed_cb),
1865 static_cast<void*>(model));
1866 g_signal_connect(m_description,
"changed",
1867 G_CALLBACK (stock_assistant_model_description_changed_cb),
1868 static_cast<void*>(model));
1876 g_signal_connect(m_page,
"focus", G_CALLBACK(assistant_page_set_focus), m_description);
1892 GtkWidget * m_title;
1893 GtkWidget * m_prev_amount;
1894 GtkWidget * m_next_amount;
1895 GtkWidget * m_next_amount_label;
1897 GtkWidget * m_amount_label;
1901 gnc_numeric get_stock_amount () {
return m_amount.get(); }
1902 void set_stock_amount (std::string new_amount_str);
1906 PageStockAmount::PageStockAmount (GtkBuilder *builder,
Account* account) :
1907 m_page (get_widget (builder,
"stock_amount_page")),
1908 m_title (get_widget (builder,
"stock_amount_title")),
1909 m_prev_amount (get_widget (builder,
"prev_balance_amount")),
1910 m_next_amount (get_widget (builder,
"next_balance_amount")),
1911 m_next_amount_label (get_widget (builder,
"next_balance_label")),
1913 m_amount_label (get_widget (builder,
"stock_amount_label"))
1915 m_amount.attach (builder,
"stock_amount_table",
"stock_amount_label", 1);
1921 gtk_label_set_text_with_mnemonic
1922 (GTK_LABEL (m_amount_label),
1923 entry->input_new_balance() ? _(
"Ne_w Balance") : _(
"_Shares"));
1925 (GTK_LABEL (m_next_amount_label),
1926 entry->input_new_balance() ? _(
"Ratio") : _(
"Next Balance"));
1928 (GTK_LABEL (m_title),
1929 entry->input_new_balance() ?
1930 _(
"Enter the new balance of shares after the stock split.") :
1931 _(
"Enter the number of shares you gained or lost in the transaction."));
1932 gtk_label_set_text (GTK_LABEL (m_prev_amount), entry->
print_amount(entry->get_balance()));
1934 entry->set_amount(get_stock_amount());
1936 g_signal_connect(m_page,
"focus", G_CALLBACK(assistant_page_set_focus), m_amount.widget());
1942 auto me =
static_cast<PageStockAmount*
>(g_object_get_data (G_OBJECT (widget),
"owner"));
1943 entry->set_amount(me->get_stock_amount());
1950 m_amount.connect(G_CALLBACK (page_stock_amount_changed_cb), entry);
1951 m_amount.set_owner(static_cast<gpointer>(
this));
1955 PageStockAmount::set_stock_amount (std::string new_amount_str)
1957 gtk_label_set_text (GTK_LABEL(m_next_amount), new_amount_str.c_str());
1969 GtkWidget * m_price;
1973 const char* get_memo ();
1977 void set_price(
const gchar *val);
1983 auto me =
static_cast<PageStockValue*
>(g_object_get_data (G_OBJECT (widget),
"owner"));
1984 entry->set_value (me->value_edit().get());
1988 PageStockValue::PageStockValue(GtkBuilder *builder,
Account* account)
1989 : m_page(get_widget(builder,
"stock_value_page")),
1991 m_price(get_widget(builder,
"stock_price_amount")),
1992 m_memo(get_widget(builder,
"stock_memo_entry"))
1994 m_value.attach(builder,
"stock_value_table",
"stock_value_label", 0);
2000 m_value.connect(G_CALLBACK (page_stock_value_changed_cb), entry);
2001 m_value.set_owner (static_cast<gpointer>(
this));
2002 g_signal_connect (m_memo,
"changed", G_CALLBACK(text_entry_changed_cb), entry);
2008 entry->set_memo(get_memo());
2010 entry->set_value(m_value.get());
2012 g_signal_connect(m_page,
"focus", G_CALLBACK(assistant_page_set_focus), m_value.widget());
2016 PageStockValue::get_memo()
2018 return gtk_entry_get_text(GTK_ENTRY (m_memo));
2022 PageStockValue::set_price (
const gchar *val)
2024 gtk_label_set_text(GTK_LABEL(this->m_price), val);
2043 const char* get_memo();
2046 PageCash::PageCash(GtkBuilder *builder,
Account* account)
2047 : m_page(get_widget(builder,
"cash_details_page")),
2051 m_memo(get_widget(builder,
"cash_memo_entry")),
2054 m_account.attach (builder,
"cash_table",
"cash_account_label", 0);
2055 m_value.attach (builder,
"cash_table",
"cash_label", 1);
2061 m_account.connect(entry);
2062 g_signal_connect(m_memo,
"changed", G_CALLBACK(text_entry_changed_cb), entry);
2063 m_value.connect(G_CALLBACK(value_changed_cb), entry);
2069 entry->set_memo(get_memo());
2071 entry->set_value(m_value.get());
2072 entry->set_account(m_account.get());
2073 g_signal_connect(m_page,
"focus", G_CALLBACK(assistant_page_set_focus), m_value.widget());
2077 PageCash::get_memo()
2079 return gtk_entry_get_text(GTK_ENTRY (m_memo));
2091 GtkWidget * m_capitalize;
2099 bool get_capitalize_fees ();
2100 const char* get_memo();
2101 void set_capitalize_fees (
bool state);
2102 void set_account (
Account *acct) { m_account.set(acct); }
2103 Account* stock_account() {
return m_stock_account; }
2104 void update_fees_acct_sensitive (
bool sensitive);
2108 PageFees::PageFees(GtkBuilder *builder,
Account* account)
2109 : m_page(get_widget(builder,
"fees_details_page")),
2111 get_widget(builder,
"capitalize_fees_checkbutton")),
2114 m_memo(get_widget(builder,
"fees_memo_entry")),
2116 m_stock_account(account)
2118 m_account.attach (builder,
"fees_table",
"fees_account_label", 1);
2119 m_value.attach(builder,
"fees_table",
"fees_label", 2);
2123 PageFees::get_capitalize_fees()
2125 return gtk_toggle_button_get_active(
2126 GTK_TOGGLE_BUTTON(m_capitalize));
2130 PageFees::get_memo()
2132 return gtk_entry_get_text(GTK_ENTRY (m_memo));
2136 PageFees::set_capitalize_fees(
bool state)
2138 gtk_toggle_button_set_active(
2139 GTK_TOGGLE_BUTTON(m_capitalize), state);
2143 PageFees::update_fees_acct_sensitive(
bool sensitive)
2145 m_account.set_sensitive(sensitive);
2151 g_return_if_fail (entry);
2152 auto me =
static_cast<PageFees *
>(g_object_get_data (G_OBJECT (widget),
"owner"));
2153 g_return_if_fail (me);
2154 bool cap = me->get_capitalize_fees();
2155 entry->set_capitalize(cap);
2157 entry->set_account(me->stock_account());
2158 me->update_fees_acct_sensitive(!cap);
2164 m_account.connect(entry);
2165 g_signal_connect(m_memo,
"changed", G_CALLBACK(text_entry_changed_cb), entry);
2166 m_value.connect(G_CALLBACK(value_changed_cb), entry);
2167 g_object_set_data(G_OBJECT (m_capitalize),
"owner",
this);
2168 g_signal_connect (m_capitalize,
"toggled", G_CALLBACK (capitalize_fees_toggled_cb), entry);
2174 set_capitalize_fees (entry->do_capitalize());
2175 entry->set_memo(get_memo());
2177 entry->set_value (m_value.get());
2178 entry->set_account(m_account.get());
2179 g_signal_connect(m_page,
"focus", G_CALLBACK(assistant_page_set_focus), m_value.widget());
2195 const char* get_memo();
2198 PageDividend::PageDividend(GtkBuilder *builder,
Account* account)
2199 : m_page(get_widget(builder,
"dividend_details_page")),
2202 m_memo(get_widget(builder,
"dividend_memo_entry")),
2205 m_account.attach(builder,
"dividend_table",
"dividend_account_label", 0);
2206 m_value.attach(builder,
"dividend_table",
"dividend_label", 1);
2213 m_account.connect(entry);
2214 g_signal_connect(m_memo,
"changed", G_CALLBACK(text_entry_changed_cb), entry);
2215 m_value.connect(G_CALLBACK(value_changed_cb), entry);
2221 entry->set_memo(get_memo());
2223 entry->set_value(m_value.get());
2224 entry->set_account(m_account.get());
2225 g_signal_connect(m_page,
"focus", G_CALLBACK(assistant_page_set_focus), m_value.widget());
2229 PageDividend::get_memo()
2231 return gtk_entry_get_text(GTK_ENTRY (m_memo));
2245 const char* get_memo();
2248 PageCapGain::PageCapGain (GtkBuilder *builder,
Account* account) :
2249 m_page (get_widget (builder,
"capgains_details_page")),
2252 m_memo (get_widget (builder,
"capgains_memo_entry")),
2255 m_account.attach(builder,
"capgains_table",
"capgains_account_label", 0);
2256 m_value.attach(builder,
"capgains_table",
"capgains_label", 1);
2260 PageCapGain::get_memo()
2262 return gtk_entry_get_text(GTK_ENTRY (m_memo));
2269 m_account.connect(entry);
2270 g_signal_connect(m_memo,
"changed", G_CALLBACK(text_entry_changed_cb), entry);
2271 m_value.connect(G_CALLBACK(value_changed_cb), entry);
2277 entry->set_memo(get_memo());
2279 entry->set_value(m_value.get());
2280 entry->set_account(m_account.get());
2281 g_signal_connect(m_page,
"focus", G_CALLBACK(assistant_page_set_focus), m_value.widget());
2287 SPLIT_COL_ACCOUNT = 0,
2293 SPLIT_COL_UNITS_COLOR,
2300 GtkWidget *m_treeview;
2307 void load(
const EntryVec& list_of_splits);
2310 GncFinishTreeview::GncFinishTreeview (GtkBuilder *builder) :
2311 m_treeview{get_widget (builder,
"transaction_view")}
2313 auto view = GTK_TREE_VIEW (m_treeview);
2314 gtk_tree_view_set_grid_lines (GTK_TREE_VIEW(view), gnc_tree_view_get_grid_lines_pref ());
2316 auto store = gtk_list_store_new (NUM_SPLIT_COLS, G_TYPE_STRING, G_TYPE_STRING,
2317 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
2318 G_TYPE_STRING, G_TYPE_STRING);
2319 gtk_tree_view_set_model(view, GTK_TREE_MODEL(store));
2320 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (view),
2321 GTK_SELECTION_NONE);
2322 g_object_unref(store);
2324 auto renderer = gtk_cell_renderer_text_new();
2325 auto column = gtk_tree_view_column_new_with_attributes
2326 (_(
"Account"), renderer,
"text", SPLIT_COL_ACCOUNT,
nullptr);
2327 gtk_tree_view_append_column(view, column);
2329 renderer = gtk_cell_renderer_text_new();
2330 g_object_set (renderer,
"ellipsize", PANGO_ELLIPSIZE_END,
nullptr);
2331 column = gtk_tree_view_column_new_with_attributes
2332 (_(
"Memo"), renderer,
"text", SPLIT_COL_MEMO,
nullptr);
2333 gtk_tree_view_column_set_expand (column,
true);
2334 gtk_tree_view_append_column(view, column);
2336 renderer = gtk_cell_renderer_text_new();
2337 gtk_cell_renderer_set_alignment (renderer, 1.0, 0.5);
2338 gtk_cell_renderer_set_padding (renderer, 5, 0);
2339 column = gtk_tree_view_column_new_with_attributes
2340 (_(
"Debit"), renderer,
"text", SPLIT_COL_DEBIT,
nullptr);
2341 gtk_tree_view_append_column(view, column);
2343 renderer = gtk_cell_renderer_text_new();
2344 gtk_cell_renderer_set_alignment (renderer, 1.0, 0.5);
2345 gtk_cell_renderer_set_padding (renderer, 5, 0);
2346 column = gtk_tree_view_column_new_with_attributes
2347 (_(
"Credit"), renderer,
"text", SPLIT_COL_CREDIT,
nullptr);
2348 gtk_tree_view_append_column(view, column);
2350 renderer = gtk_cell_renderer_text_new();
2351 gtk_cell_renderer_set_alignment (renderer, 1.0, 0.5);
2352 gtk_cell_renderer_set_padding (renderer, 5, 0);
2353 column = gtk_tree_view_column_new_with_attributes
2354 (_(
"Units"), renderer,
2355 "text", SPLIT_COL_UNITS,
2356 "foreground", SPLIT_COL_UNITS_COLOR,
2358 gtk_tree_view_append_column(view, column);
2359 gtk_tree_view_set_tooltip_column(GTK_TREE_VIEW(m_treeview),
2360 SPLIT_COL_TOOLTIP);}
2365 auto gtv = GTK_TREE_VIEW(m_treeview);
2367 GNC_PREF_NEGATIVE_IN_RED);
2368 auto list = GTK_LIST_STORE(gtk_tree_view_get_model(gtv));
2369 gtk_list_store_clear(list);
2370 for (
const auto &entry : list_of_splits) {
2372 auto memo{entry->memo()};
2373 auto tooltip = (memo && *memo ?
2374 g_markup_escape_text(memo, -1) : strdup(
""));
2380 auto char2str{[](
const char* str) -> std::string {
2381 return std::string{ str ? str :
"" }; }};
2383 auto units{char2str(entry->has_amount() ?
2384 entry->
print_amount(entry->debit_side() ? entry->amount() :
2386 auto units_in_red{negative_in_red && !entry->debit_side()};
2387 gtk_list_store_append(list, &iter);
2391 entry->print_account(), SPLIT_COL_MEMO,
2392 entry->memo(), SPLIT_COL_TOOLTIP, tooltip, SPLIT_COL_DEBIT,
2393 entry->debit_side() ? amount.c_str() :
nullptr,
2395 entry->debit_side() ? nullptr : amount.c_str(),
2396 SPLIT_COL_UNITS, units.c_str(),
2397 SPLIT_COL_UNITS_COLOR, units_in_red ?
"red" :
nullptr, -1);
2410 GtkWidget * m_summary;
2416 PageFinish::PageFinish (GtkBuilder *builder) :
2417 m_page (get_widget (builder,
"finish_page")), m_view (builder),
2418 m_summary (get_widget (builder,
"finish_summary")) {}
2425 m_view.
load(list_of_splits);
2426 gtk_label_set_text(GTK_LABEL(m_summary), summary.c_str());
2427 gtk_assistant_set_page_complete(GTK_ASSISTANT(window), m_page, success);
2430 enum assistant_pages
2433 PAGE_TRANSACTION_DETAILS,
2434 PAGE_TRANSACTION_TYPE,
2446 GtkWidget * m_window;
2473 GtkWidget* window() {
return m_window; }
2476 StockAssistantView::StockAssistantView (GtkBuilder *builder,
Account* account, GtkWidget *parent) :
2477 m_window (get_widget (builder,
"stock_transaction_assistant")), m_type_page(builder), m_deets_page(builder),
2478 m_stock_amount_page (builder, account), m_stock_value_page (builder, account), m_cash_page (builder, account),
2479 m_fees_page (builder, account), m_dividend_page (builder, account), m_capgain_page (builder, account),
2480 m_finish_page (builder)
2483 gtk_widget_set_name (GTK_WIDGET(m_window),
"gnc-id-assistant-stock-transaction");
2484 gtk_window_set_transient_for (GTK_WINDOW (m_window), GTK_WINDOW(parent));
2485 gnc_window_adjust_for_screen (GTK_WINDOW(m_window));
2486 gnc_restore_window_size (GNC_PREFS_GROUP, GTK_WINDOW(m_window),
2487 GTK_WINDOW(parent));
2488 gtk_widget_show_all (m_window);
2489 DEBUG (
"StockAssistantView constructor\n");
2492 StockAssistantView::~StockAssistantView()
2494 gnc_save_window_size (GNC_PREFS_GROUP, GTK_WINDOW(m_window));
2495 gtk_widget_destroy (m_window);
2496 DEBUG (
"StockAssistantView destructor\n");
2506 forward_page_func (gint current_page,
void* data)
2511 return current_page;
2513 if (!model->
stock_entry()->has_amount() && current_page == PAGE_STOCK_AMOUNT)
2515 if (!model->
stock_entry()->enabled() && current_page == PAGE_STOCK_VALUE)
2517 if (!model->
cash_entry()->enabled() && current_page == PAGE_CASH)
2519 if (!model->
fees_entry()->enabled() && current_page == PAGE_FEES)
2521 if (!model->
dividend_entry()->enabled() && current_page == PAGE_DIVIDEND)
2523 if (!model->
capgains_entry()->enabled() && current_page == PAGE_CAPGAINS)
2526 return current_page;
2532 m_type_page.connect(model);
2533 m_deets_page.connect(model);
2534 m_stock_amount_page.connect(model->
stock_entry());
2541 gtk_assistant_set_forward_page_func (GTK_ASSISTANT(m_window),
2542 (GtkAssistantPageFunc)forward_page_func,
2549 g_return_if_fail (page < PAGE_STOCK_AMOUNT || model->txn_type_valid());
2552 case PAGE_TRANSACTION_TYPE:
2555 m_type_page.prepare(model);
2557 case PAGE_TRANSACTION_DETAILS:
2558 m_deets_page.prepare(model);
2560 case PAGE_STOCK_AMOUNT:
2562 m_stock_amount_page.prepare(model->
stock_entry());
2565 case PAGE_STOCK_VALUE:
2586 m_finish_page.prepare (m_window, model);
2600 void* handler_data, [[maybe_unused]]
void* event_data);
2604 std::unique_ptr<StockAssistantModel> m_model;
2606 bool m_destroying =
false;
2607 int m_qof_event_handler;
2610 : m_model{std::make_unique<StockAssistantModel>(acct)},
2611 m_view{builder, acct, parent},
2614 connect_signals (builder);
2615 DEBUG (
"StockAssistantController constructor\n");
2618 void connect_signals(GtkBuilder *builder);
2619 void prepare(GtkAssistant* assistant, GtkWidget *page);
2621 bool destroying() {
return m_destroying; }
2622 Account* model_account() {
return m_model->account(); }
2625 static void stock_assistant_window_destroy_cb(GtkWidget *
object, gpointer user_data);
2626 static void close_handler (gpointer user_data);
2628 StockAssistantController::~StockAssistantController()
2630 m_destroying =
true;
2631 gnc_unregister_gui_component_by_data (ASSISTANT_STOCK_TRANSACTION_CM_CLASS,
this);
2636 StockAssistantController::connect_signals (GtkBuilder *builder)
2638 m_view.
connect(m_model.get());
2639 gtk_builder_connect_signals (builder,
this);
2640 g_signal_connect (m_view.window(),
"destroy",
2641 G_CALLBACK (stock_assistant_window_destroy_cb),
this);
2644 auto component_id = gnc_register_gui_component
2645 (ASSISTANT_STOCK_TRANSACTION_CM_CLASS,
nullptr, close_handler,
this);
2646 gnc_gui_component_watch_entity_type (component_id, GNC_ID_ACCOUNT,
2647 QOF_EVENT_MODIFY | QOF_EVENT_DESTROY);
2651 StockAssistantController::prepare(GtkAssistant* assistant, GtkWidget* page)
2653 auto currentpage = gtk_assistant_get_current_page(assistant);
2654 m_view.
prepare(currentpage, m_model.get());
2658 StockAssistantController::finish()
2660 g_return_if_fail (m_model->txn_type_valid());
2662 gnc_suspend_gui_refresh ();
2663 auto [success, trans] = m_model->create_transaction();
2664 gnc_resume_gui_refresh ();
2666 if (success && trans)
2668 auto split = xaccTransFindSplitByAccount (trans, m_model->account());
2674 gnc_split_reg_raise (gsr);
2676 if (gnc_split_reg_clear_filter_for_split (gsr, split))
2679 gnc_split_reg_jump_to_split (gsr, split);
2683 gnc_close_gui_component_by_data (ASSISTANT_STOCK_TRANSACTION_CM_CLASS,
this);
2688 void* handler_data, [[maybe_unused]]
void* event_data)
2691 if ((inst && inst != QOF_INSTANCE(controller->model_account())) || (
event & QOF_EVENT_DESTROY) == 0 ||
2692 controller->destroying())
2700 stock_assistant_prepare_cb (GtkAssistant *assistant, GtkWidget *page,
2704 info->prepare(assistant, page);
2709 stock_assistant_window_destroy_cb (GtkWidget *
object, gpointer user_data)
2712 if (controller->destroying())
2715 gnc_close_gui_component_by_data (ASSISTANT_STOCK_TRANSACTION_CM_CLASS, controller);
2720 stock_assistant_finish_cb (GtkAssistant *assistant, gpointer user_data)
2723 controller->finish();
2728 stock_assistant_cancel_cb (GtkAssistant *assistant, gpointer user_data)
2731 if (controller->destroying())
2733 gnc_close_gui_component_by_data (ASSISTANT_STOCK_TRANSACTION_CM_CLASS, controller);
2738 close_handler (gpointer user_data)
2741 if (controller->destroying())
2755 gnc_stock_transaction_assistant (GtkWidget *parent,
Account *account)
2757 auto builder = gtk_builder_new();
2758 gnc_builder_add_from_file(builder,
"assistant-stock-transaction.glade",
2759 "stock_transaction_assistant");
2762 g_object_unref(builder);
messages for later display to the user.
void xaccSplitSetValue(Split *split, gnc_numeric val)
The xaccSplitSetValue() method sets the value of this split in the transaction's commodity.
GNCPrice * gnc_price_create(QofBook *book)
gnc_price_create - returns a newly allocated and initialized price with a reference count of 1...
GncPluginPage * gnc_plugin_page_register_new(Account *account, gboolean subaccounts)
Create a new "register" plugin page, given a pointer to an account.
bool txn_type_valid()
Accessor.
Transaction * xaccMallocTransaction(QofBook *book)
The xaccMallocTransaction() will malloc memory and initialize it.
gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b)
Equivalence predicate: Returns TRUE (1) if a and b represent the same number.
std::string report()
Compose all of the logged messages into a bullet list, errors first, then warnings, infos last.
void xaccTransSetDatePostedSecsNormalized(Transaction *trans, time64 time)
This function sets the posted date of the transaction, specified by a time64 (see ctime(3))...
Contains the pages and manages displaying them one at a time.
gchar * gnc_num_dbg_to_string(gnc_numeric n)
Convert to string.
void xaccSplitMakeStockSplit(Split *s)
Mark a split to be of type stock split - after this, you shouldn't modify the value anymore...
Page classes generate the several pages of the assistant.
time64 xaccTransGetDate(const Transaction *trans)
Retrieve the posted date of the transaction.
Date and Time handling routines.
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
Retrieve the mnemonic for the specified commodity.
void xaccAccountSetAssociatedAccount(Account *acc, const char *tag, const Account *assoc_acct)
Set the account's associated account e.g.
virtual const char * print_value() const
QofBook * qof_instance_get_book(gconstpointer inst)
Return the book pointer.
utility functions for the GnuCash UI
Expense accounts are used to denote expenses.
StockTransactionEntry * fees_entry()
Accessor.
#define PINFO(format, args...)
Print an informational note.
gnc_numeric gnc_numeric_neg(gnc_numeric a)
Returns a newly created gnc_numeric that is the negative of the given gnc_numeric value...
void set_txn_type_explanation(const gchar *txt)
Sets the explanation text for the selected transaction type, allowing the user to make sure that the ...
std::tuple< bool, Transaction * > create_transaction()
Generate a GnuCash transaction from the active entries.
An exact-rational-number library for gnucash.
gnc_numeric calculate_price() const override
Calculate the price (amount/value) for non-currency accounts.
void gnc_price_unref(GNCPrice *p)
gnc_price_unref - indicate you're finished with a price (i.e.
The Cash page collects the cash account (usually corresponds the broker's cash management account)...
const std::optional< TxnTypeInfo > & txn_type()
Accessor.
#define DEBUG(format, args...)
Print a debugging message.
gboolean gnc_pricedb_add_price(GNCPriceDB *db, GNCPrice *p)
Add a price to the pricedb.
const char * xaccPrintAmount(gnc_numeric val, GNCPrintAmountInfo info)
Make a string representation of a gnc_numeric.
void xaccTransSetDescription(Transaction *trans, const char *desc)
Sets the transaction Description.
gnc_numeric gnc_numeric_add(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Return a+b.
C++ wrapper for GncAmountEdit, see gnucash/gnome-utils/gnc-amount-edit.h.
gboolean gnc_numeric_zero_p(gnc_numeric a)
Returns 1 if the given gnc_numeric is 0 (zero), else returns 0.
Specialized Entry for the stock account's capital gains split.
The primary numeric class for representing amounts and values.
Transaction * xaccSplitGetParent(const Split *split)
Returns the parent transaction of the split.
Use any denominator which gives an exactly correct ratio of numerator to denominator.
virtual gnc_numeric calculate_price() const
Calculate the price (amount/value) for non-currency accounts.
#define PERR(format, args...)
Log a serious error.
C++ wrapper for the GncDateEdit control (see gnucash/gnome-utils/gnc-date-edit.h).
void set_transaction_date(time64 date)
Setter.
StockTransactionEntry * dividend_entry()
Accessor.
GNCPriceDB * gnc_pricedb_get_db(QofBook *book)
Return the pricedb associated with the book.
void gnc_main_window_open_page(GncMainWindow *window, GncPluginPage *page)
Display a data plugin page in a window.
Functions for adding content to a window.
gboolean gnc_numeric_negative_p(gnc_numeric a)
Returns 1 if a < 0, otherwise returns 0.
Logger & logger()
Accessor.
void xaccTransSetCurrency(Transaction *trans, gnc_commodity *curr)
Set a new currency on a transaction.
gint qof_event_register_handler(QofEventHandler handler, gpointer user_data)
Register a handler for events.
#define PWARN(format, args...)
Log a warning.
char * qof_print_date(time64 secs)
Convenience; calls through to qof_print_date_dmy_buff().
void xaccSplitSetAmount(Split *split, gnc_numeric amt)
The xaccSplitSetAmount() method sets the amount in the account's commodity that the split should have...
Functions providing a register page for the GnuCash UI.
Account handling public routines.
gint QofEventId
Define the type of events allowed.
Reduce the result value by common factor elimination, using the smallest possible value for the denom...
GtkTreeView implementation for gnucash account tree.
Income accounts are used to denote income.
Account public routines (C++ api)
Transaction Details page.
class TxnTypeinfo has no functions.
const char * gnc_numeric_errorCode_to_string(GNCNumericErrorCode error_code)
Returns a string representation of the given GNCNumericErrorCode.
void xaccSplitSetMemo(Split *split, const char *memo)
The memo is an arbitrary string associated with a split.
void connect(StockAssistantModel *)
Calls each page's connect function.
gnc_numeric gnc_numeric_error(GNCNumericErrorCode error_code)
Create a gnc_numeric object that signals the error condition noted by error_code, rather than a numbe...
bool maybe_reset_txn_types()
Selects a TxnTypevec for the user to pick from depending on whether the account has a positive...
Holds the configuration information from the fieldmask and the data to create a single split...
void prepare(int page, StockAssistantModel *)
Calls the specified page's prepare function.
The bank account type denotes a savings or checking account held at a bank.
void qof_event_unregister_handler(gint handler_id)
Unregister an event handler.
StockTransactionEntry * capgains_entry()
Accessor.
Argument is not a valid number.
gnc_numeric gnc_numeric_abs(gnc_numeric a)
Returns a newly created gnc_numeric that is the absolute value of the given gnc_numeric value...
available transaction types based on the state of the account, the collection and validation of input...
void xaccTransCommitEdit(Transaction *trans)
The xaccTransCommitEdit() method indicates that the changes to the transaction and its splits are com...
gnc_numeric gnc_numeric_div(gnc_numeric x, gnc_numeric y, gint64 denom, gint how)
Division.
void load(const EntryVec &list_of_splits)
Extract the information from the StockTransactionEntries in the vector created by the model's make_li...
virtual const char * print_price() const
void set_fieldmask(FieldMask mask) override
Set up the state variables from the FieldMask.
StockTransactionEntry * cash_entry()
Accessor.
void xaccTransBeginEdit(Transaction *trans)
The xaccTransBeginEdit() method must be called before any changes are made to a transaction or any of...
asset (and liability) accounts indicate generic, generalized accounts that are none of the above...
std::tuple< bool, std::string, EntryVec > generate_list_of_splits()
Generate the proposed list of splits.
StockTransactionEntry * stock_entry()
Accessor.
void set_transaction_desc(const char *desc)
Setter.
gnc_numeric xaccAccountGetBalanceAsOfDate(Account *acc, time64 date)
Get the balance of the account at the end of the day before the date specified.
All type declarations for the whole Gnucash engine.
gboolean gnc_numeric_positive_p(gnc_numeric a)
Returns 1 if a > 0, otherwise returns 0.
gnc_commodity * gnc_account_get_currency_or_parent(const Account *account)
Returns a gnc_commodity that is a currency, suitable for being a Transaction's currency.
Account * xaccAccountGetAssociatedAccount(const Account *acc, const char *tag)
Get the account's associated account e.g.
virtual const char * print_amount(gnc_numeric amt) const
Split * xaccMallocSplit(QofBook *book)
Constructor.
bool set_txn_type(guint type_idx)
Setter.
Generic api to store and retrieve preferences.
Specialized StockTransactionEntry for the stock split.
gnc_numeric xaccSplitGetValue(const Split *split)
Returns the value of this split in the transaction's commodity.
void xaccAccountBeginEdit(Account *acc)
The xaccAccountBeginEdit() subroutine is the first phase of a two-phase-commit wrapper for account up...
const std::optional< TxnTypeVec > & get_txn_types()
Accessor function.
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account's commodity.
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
Get a boolean value from the preferences backend.
virtual void set_fieldmask(FieldMask mask)
Set up the state variables from the FieldMask.
std::string amount_str_for_display() const override
Generate a string representation of the value.
time64 gnc_time(time64 *tbuf)
get the current time
GNCNumericErrorCode gnc_numeric_check(gnc_numeric a)
Check for error signal in value.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
void gnc_plugin_page_register_clear_current_filter(GncPluginPage *plugin_page)
This function clears the registers current filter.
time64 gnc_time64_get_day_end(time64 time_val)
The gnc_time64_get_day_end() routine will take the given time in seconds and adjust it to the last se...
const char * xaccAccountGetName(const Account *acc)
Get the account's name.
#define GNC_DENOM_AUTO
Values that can be passed as the 'denom' argument.
API for Transactions and Splits (journal entries)
GNCSplitReg * gnc_plugin_page_register_get_gsr(GncPluginPage *plugin_page)
Get the GNCSplitReg data structure associated with this register page.
C++ wrapper for GncAccounSel, see gnucash/gnome-utils/gnc-account-sel.h.
void xaccAccountCommitEdit(Account *acc)
ThexaccAccountCommitEdit() subroutine is the second phase of a two-phase-commit wrapper for account u...
virtual std::string amount_str_for_display() const
Generate a string representation of the value.
Specialized Entry for fees, taxes, commissions, and so on.
void set_fieldmask(FieldMask mask) override
Set up the state variables from the FieldMask.
std::string get_new_amount_str() const
Accessor.
gnc_numeric xaccSplitGetAmount(const Split *split)
Returns the amount of the split in the account's commodity.
Dividend page, collects an amount, an INCOME account, and a memo.