Skip to content

Commit

Permalink
[assistant-stock-transaction] support additional txn_types
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlam authored and jralls committed Jun 11, 2023
1 parent 55046f2 commit 0d9dd3b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 20 deletions.
83 changes: 69 additions & 14 deletions gnucash/gnome/assistant-stock-transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ enum class FieldMask : unsigned
ALLOW_NEGATIVE = 8,
INPUT_NEW_BALANCE = 16, // stock_amt only: instead of amount, get new balance
CAPITALIZE_DEFAULT = 32, // fees only: capitalize by default into stock acct
CAPGAINS_IN_STOCK = 64, // capg only: add a balancing split in stock acct
};

FieldMask operator |(FieldMask lhs, FieldMask rhs)
Expand Down Expand Up @@ -182,7 +183,7 @@ static const TxnTypeVec long_types
FieldMask::ENABLED_DEBIT, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
FieldMask::DISABLED, // dividend_amt
FieldMask::ENABLED_CREDIT | FieldMask::ALLOW_ZERO | FieldMask::ALLOW_NEGATIVE, // capgains_amt
FieldMask::ENABLED_CREDIT | FieldMask::ALLOW_ZERO | FieldMask::ALLOW_NEGATIVE | FieldMask::CAPGAINS_IN_STOCK, // capgains_amt
// Translators: this is a stock transaction describing new
// sale of stock, and recording capital gain/loss
N_("Sell"),
Expand Down Expand Up @@ -213,6 +214,18 @@ reinvested must be subsequently recorded as a regular stock purchase.")
N_("Return of capital"),
N_("Company returns capital, reducing the cost basis without affecting # units.")
},
{
FieldMask::DISABLED, // stock_amt
FieldMask::ENABLED_CREDIT, // stock_val
FieldMask::DISABLED, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO | FieldMask::CAPITALIZE_DEFAULT, // fees_amt
FieldMask::ENABLED_DEBIT, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing return
// of capital, reclassifying a dividend into return of capital
N_("Return of capital (reclassification)"),
N_("Company returns capital, reducing the cost basis without affecting # units. A distribution previously recorded as a dividend is reclassified to return of capital, often due to end-of-year tax information.")
},
{
FieldMask::DISABLED, // stock_amt
FieldMask::ENABLED_DEBIT, // stock_val
Expand All @@ -221,10 +234,22 @@ reinvested must be subsequently recorded as a regular stock purchase.")
FieldMask::ENABLED_CREDIT, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing a
// notional distribution
N_("Notional distribution"),
// notional distribution recorded as dividend
N_("Notional distribution (dividend)"),
N_("Company issues a notional distribution, which is recorded as dividend income and increases the cost basis without affecting # units.")
},
{
FieldMask::DISABLED, // stock_amt
FieldMask::ENABLED_DEBIT, // stock_val
FieldMask::DISABLED, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
FieldMask::DISABLED, // dividend_amt
FieldMask::ENABLED_CREDIT, // capg_amt
// Translators: this is a stock transaction describing a
// notional distribution recorded as capital gain
N_("Notional distribution (capital gain)"),
N_("Company issues a notional distribution, which is recorded as capital gain and increases the cost basis without affecting # units.")
},
{
FieldMask::ENABLED_DEBIT | FieldMask::INPUT_NEW_BALANCE, // stock_amt
FieldMask::DISABLED, // stock_val
Expand Down Expand Up @@ -274,7 +299,7 @@ static const TxnTypeVec short_types
FieldMask::ENABLED_CREDIT, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
FieldMask::DISABLED, // dividend_amt
FieldMask::ENABLED_CREDIT | FieldMask::ALLOW_ZERO | FieldMask::ALLOW_NEGATIVE, // capg_amt
FieldMask::ENABLED_CREDIT | FieldMask::ALLOW_ZERO | FieldMask::ALLOW_NEGATIVE | FieldMask::CAPGAINS_IN_STOCK, // capg_amt
// Translators: this is a stock transaction describing cover
// buying stock, and recording capital gain/loss
N_("Buy to cover short"),
Expand Down Expand Up @@ -304,6 +329,19 @@ static const TxnTypeVec short_types
N_("Compensatory return of capital"),
N_("Company returns capital, and the short stock holder must make a compensatory payment for the returned capital. This reduces the cost basis (less negative, towards 0.00 value) without affecting # units.")
},
{
FieldMask::DISABLED, // stock_amt
FieldMask::ENABLED_DEBIT, // stock_val
FieldMask::DISABLED, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO | FieldMask::CAPITALIZE_DEFAULT, // fees_amt
FieldMask::ENABLED_CREDIT, // dividend_amt
FieldMask::DISABLED, // capg_amt
// Translators: this is a stock transaction describing
// reclassifying a compensatory dividend into compensatory
// return of capital when shorting stock
N_("Compensatory return of capital (reclassification)"),
N_("Company returns capital, and the short stock holder must make a compensatory payment for the returned capital. This reduces the cost basis (less negative, towards 0.00 value) without affecting # units. A distribution previously recorded as a compensatory dividend is reclassified to compensatory return of capital, often due to end-of-year tax information.")
},
{
FieldMask::DISABLED, // stock_amt
FieldMask::ENABLED_CREDIT, // stock_val
Expand All @@ -312,10 +350,24 @@ static const TxnTypeVec short_types
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"),
// notional distribution recorded as dividend when shorting
// stock
N_("Compensatory notional distribution (dividend)"),
N_("Company issues a notional distribution, and the short stock holder must make a compensatory payment for the notional distribution. This is recorded as a loss/negative dividend income amount, and increases the cost basis (more negative, away from 0.00 value) without affecting # units.")
},
{
FieldMask::DISABLED, // stock_amt
FieldMask::ENABLED_CREDIT, // stock_val
FieldMask::DISABLED, // cash_amt
FieldMask::ENABLED_DEBIT | FieldMask::ALLOW_ZERO, // fees_amt
FieldMask::DISABLED, // dividend_amt
FieldMask::ENABLED_DEBIT, // capg_amt
// Translators: this is a stock transaction describing a
// notional distribution recorded as capital gain when
// shorting stock
N_("Compensatory notional distribution (capital gain)"),
N_("Company issues a notional distribution, and the short stock holder must make a compensatory payment for the notional distribution. This is recorded as a capital loss amount, and increases the cost basis (more negative, away from 0.00 value) without affecting # units.")
},
{
FieldMask::ENABLED_CREDIT | FieldMask::INPUT_NEW_BALANCE, // stock_amt
FieldMask::DISABLED, // stock_val
Expand Down Expand Up @@ -756,20 +808,23 @@ to ensure proper recording."), new_date_str, last_split_date_str);
// swap the debit/credit flags.
if (this->capgains_enabled)
{
if (this->txn_type->capgains_value & FieldMask::CAPGAINS_IN_STOCK)
{
line = check_page (debit, credit, errors, this->txn_type->capgains_value ^
(FieldMask::ENABLED_CREDIT | FieldMask::ENABLED_DEBIT),
this->acct, this->capgains_memo, this->capgains_value,
NC_ ("Stock Assistant: Page name", "capital gains"),
this->curr_pinfo);
line.units_numeric = gnc_numeric_zero();
this->list_of_splits.push_back (std::move (line));
}

line = check_page (debit, credit, errors, this->txn_type->capgains_value,
this->capgains_account, this->capgains_memo,
this->capgains_value,
NC_ ("Stock Assistant: Page name", "capital gains"),
this->curr_pinfo);
this->list_of_splits.push_back (std::move (line));

line = check_page (debit, credit, errors, this->txn_type->capgains_value ^
(FieldMask::ENABLED_CREDIT | FieldMask::ENABLED_DEBIT),
this->acct, this->capgains_memo, this->capgains_value,
NC_ ("Stock Assistant: Page name", "capital gains"),
this->curr_pinfo);
line.units_numeric = gnc_numeric_zero();
this->list_of_splits.push_back (std::move (line));
}

if (!gnc_numeric_equal (debit, credit))
Expand Down
12 changes: 6 additions & 6 deletions gnucash/gnome/test/gtest-assistant-stock-transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ TestCase easyTestCase = {
{ 0, 1 , 4 , 2020, "Buy" , 250 , 4200000 , 4200995 , true , 995 , 0 , 0 , 325 },
{ 3, 16, 4 , 2020, "ROC" , 0 , 250000 , 250000 , true , 0 , 0 , 0 , 325 },
{ 0, 2 , 5 , 2020, "Buy" , 125 , 4750000 , 4750000 , true , 0 , 0 , 0 , 450 },
{ 5, 11, 5 , 2020, "Split 2:1" , 900 , 0 , 0 , true , 0 , 0 , 0 , 900 },
{ 7, 11, 5 , 2020, "Split 2:1" , 900 , 0 , 0 , true , 0 , 0 , 0 , 900 },
{ 1, 21, 5 , 2020, "Sell" , 135 , 2150000 , 2149005 , false , 995 , 0 , 574702, 765 },
{ 0, 3 , 6 , 2020, "Buy" , 150 , 2100000 , 2100000 , true , 0 , 0 , 0 , 915 },
{ 1, 10, 6 , 2020, "Sell" , 915 , 12810000, 12809005, false , 995 , 0 , 1783309, 0 },
Expand All @@ -73,11 +73,11 @@ TestCase easyTestCase = {
// bal<0. next lines are short_types
{ 0, 15, 6 , 2020, "Short Sell" , 65 , 1105000 , 1104005 , true , 995 , 0 , 0 , -150 },
{ 1, 16, 6 , 2020, "Cover Buy" , 50 , 500000 , 500995 , false , 995 , 0 ,-264337, -100 },
{ 5, 17, 6 , 2020, "Split 2:1" , -200 , 0 , 0 , false , 0 , 0 , 0 , -200 },
{ 6, 18, 6 , 2020, "Reverse Split" , -100 , 0 , 0 , false , 0 , 0 , 0 , -100 },
{ 7, 17, 6 , 2020, "Split 2:1" , -200 , 0 , 0 , false , 0 , 0 , 0 , -200 },
{ 8, 18, 6 , 2020, "Reverse Split" , -100 , 0 , 0 , false , 0 , 0 , 0 , -100 },
{ 2, 19, 6 , 2020, "Comp Dividend" , 0 , 0 , 50000 , false , 0 , 50000, 0 , -100 },
{ 3, 19, 6 , 2020, "Comp ROC" , 0 , 250000 , 250000 , false , 0 , 0 , 0 , -100 },
{ 4, 19, 6 , 2020, "Comp ND" , 0 , 20000 , 0 , false , 0 , 20000, 0 , -100 },
{ 5, 19, 6 , 2020, "Comp ND" , 0 , 20000 , 0 , false , 0 , 20000, 0 , -100 },
{ 1, 20, 6 , 2020, "Cover Buy" , 100 , 800000 , 800498 , false , 498 , 0 ,-498673, 0 },

// bal=0. next line is "open long".
Expand All @@ -88,8 +88,8 @@ TestCase easyTestCase = {
{ 2, 25, 6 , 2020, "Dividend" , 0 , 0 , 11000 , false , 0 , 11000, 0 , 100 },
{ 0, 25, 6 , 2020, "+ Reinv" , 1 , 10000 , 10000 , false , 0 , 0 , 0 , 101 },
{ 1, 26, 6 , 2020, "Sell remainder" , 1 , 10000 , 10000 , false , 0 , 0 , 1975 , 100 },
{ 6, 26, 6 , 2020, "Reverse Split 1:2", 50 , 0 , 0 , false , 0 , 0 , 0 , 50 },
{ 4, 27, 6 , 2020, "ND" , 0 , 10000 , 0 , false , 0 , 10000, 0 , 50 }
{ 8, 26, 6 , 2020, "Reverse Split 1:2", 50 , 0 , 0 , false , 0 , 0 , 0 , 50 },
{ 5, 27, 6 , 2020, "ND" , 0 , 10000 , 0 , false , 0 , 10000, 0 , 50 }
}
};

Expand Down

0 comments on commit 0d9dd3b

Please sign in to comment.