Bug 498033

Summary: Unclear definition for investment transaction ‘addition of shares’
Product: [Applications] kmymoney Reporter: Ralf Habacker <ralf.habacker>
Component: reportsAssignee: KMyMoney Devel Mailing List <kmymoney-devel>
Status: ASSIGNED ---    
Severity: normal    
Priority: NOR    
Version: 5.1.90   
Target Milestone: ---   
Platform: Other   
OS: All   
Latest Commit: Version Fixed In:
Sentry Crash Report:
Bug Depends on: 498356    
Bug Blocks:    
Attachments: Screenshot 1
Screenshot 2
Screenshot 3
test file
Screenshot 4 - report showing add shares only

Description Ralf Habacker 2024-12-29 17:57:48 UTC
Created attachment 176957 [details]
Screenshot 1

STEPS TO REPRODUCE
1. download kmymoney snapshot from https://cdn.kde.org/ci-builds/office/kmymoney/master/
2. start kmymoney
3. open appended test file
4. Show report "Investment transactions (Customized)"

OBSERVED RESULT
Screenshot 1 shows the transactions for a security that contains an ‘Add shares’ transaction. 

In the input mask for ‘Add shares’ there is an input field ‘shares’, which displays ‘Number of shares’ as a tooltip.  (see screenshot 2)

In the ‘Investment Transactions (Customised)’ report, a ‘*** UNASSIGNED ***’ is displayed for the transaction from 2022-08-01 and the number of shares entered is displayed in the Amount column (screenshot 3)

EXPECTED RESULT
If the input is really ‘shares’, then the value in the Amount column should be calculated with a price (additional field in the input mask or most matching online price) or if the value entered is an amount, an account should be able to be entered.

SOFTWARE/OS VERSIONS
Operating System: openSUSE Leap 15.6
KDE Plasma Version: 5.27.11
KDE Frameworks Version: 5.115.0
Qt Version: 5.15.12
Comment 1 Ralf Habacker 2024-12-29 17:58:07 UTC
Created attachment 176958 [details]
Screenshot 2
Comment 2 Ralf Habacker 2024-12-29 17:58:26 UTC
Created attachment 176959 [details]
Screenshot 3
Comment 3 Ralf Habacker 2024-12-29 17:59:25 UTC
Created attachment 176960 [details]
test file
Comment 4 Thomas Baumgart 2024-12-30 13:28:41 UTC
Please be aware, that the add and remove shares activity actually forces a sort of an imbalance of the books since they modify the number of shares in an account but do not change any other account. They are a single split transactions. There is no counter account involved (resulting in the UNASSIGNED text). Therefore, the value has to be zero and that is achieved by a price of zero (shares * price = value). We can change the *** UNASSIGNED *** text to something else or simply leave the category column blank but I would not want to change anything else.
Comment 5 Jack 2024-12-30 16:15:42 UTC
I thought I had posted a similar response, but it never showed up.  I would prefer to see this handled in the report, by leaving the category and price fields black for add and remove shares transactions.
I also see problems with the third/report screenshot.  If nothing else, the last two transactions are not the same as the ones in the first screenshot - different dates.  It is not clear to me if that last column is "value" (as shown in an Investment account ledger) or the cash value of the investment account.  The upper transactions have that column vary by the shares times price of buy/sell/reinvest and by amount of dividend.  But it then varies by the number of shares for the add/remove transactions, although all prices used are otherwise the same (and not 1.)
I have not yet worked with the sample data file, which should answer some of my questions.
Comment 6 Thomas Baumgart 2024-12-30 16:30:51 UTC
Git commit 49d5e90dc13feb79277f6a037a5079b49fe76cf6 by Thomas Baumgart.
Committed on 30/12/2024 at 16:30.
Pushed by tbaumgart into branch 'master'.

Don't show UNASSIGNED marker for add/remove shares activity

M  +1    -1    kmymoney/plugins/views/reports/core/querytable.cpp

https://invent.kde.org/office/kmymoney/-/commit/49d5e90dc13feb79277f6a037a5079b49fe76cf6
Comment 7 Ralf Habacker 2024-12-31 09:41:37 UTC
(In reply to Thomas Baumgart from comment #4)
> ..  they modify the number of shares in an account

But there is a discrepancy between the ledger and the report.  In the ledger (screenshot 1) the shares are shown in the ‘Quantity’ column and in the report (screenshot 3) in the ‘Amount’ column. Shouldn't it appear in the ‘Shares’ column ?
Comment 8 Ralf Habacker 2024-12-31 11:31:13 UTC
(In reply to Ralf Habacker from comment #7)

>  in the report (screenshot 3) in the ‘Amount’ column. 

which affects the balance column if visible.
Comment 9 Ralf Habacker 2024-12-31 15:31:44 UTC
Created attachment 176998 [details]
Screenshot 4 - report showing add shares only

(In reply to Ralf Habacker from comment #7)
> But there is a discrepancy between the ledger and the report.  In the ledger
> (screenshot 1) the shares are shown in the ‘Quantity’ column and in the
> report (screenshot 3) in the ‘Amount’ column. Shouldn't it appear in the
> ‘Shares’ column ?

According to “Screenshot 4 - Report showing only added shares”, the number of shares added is included in the total shares, which is okay.

However, the displayed value should be moved from the “Value” column to the “Shares” column.
Comment 10 Bug Janitor Service 2024-12-31 16:31:12 UTC
A possibly relevant merge request was started @ https://invent.kde.org/office/kmymoney/-/merge_requests/252
Comment 11 Ralf Habacker 2025-01-01 06:22:07 UTC
(In reply to Thomas Baumgart from comment #4)
> Therefore, the value has to be zero and that is achieved by a price of zero (shares * price = value). 

I traced into the generating of this report and found https://invent.kde.org/office/kmymoney/-/blob/master/kmymoney/plugins/views/reports/core/querytable.cpp#L991

 qS[ctValue] = ((*it_split).shares() * xr).convert(fraction).toString();

At this point, xr is equal to 1.0, which is used as the price and generates a value != 0.0
Comment 12 Ralf Habacker 2025-01-01 08:54:10 UTC
(In reply to Ralf Habacker from comment #11)
> (In reply to Thomas Baumgart from comment #4)
> > Therefore, the value has to be zero and that is achieved by a price of zero (shares * price = value). 
> 
> I traced into the generating of this report and found
> https://invent.kde.org/office/kmymoney/-/blob/master/kmymoney/plugins/views/
> reports/core/querytable.cpp#L991
> 
>  qS[ctValue] = ((*it_split).shares() * xr).convert(fraction).toString();
> 
> At this point, xr is equal to 1.0, which is used as the price and generates
> a value != 0.0

Saving an “Add shares” transaction in a kmymoney file results in this xml fragment:

    <TRANSACTION id=“T000000000000000008” postdate=“2022-08-01” memo=“” entrydate=“2024-12-27” commodity=“AUD”>
      <SPLITS>
        <SPLIT id=“S0001” payee=“” reconciledate=“” action=“Add” reconcileflag=“0” value=“0/1” shares=“5/1” price=“1/1” memo=“” account=“A000003” number=“” bankid=“”/>
      </SPLITS>
    </TRANSACTION>

with a price set to 1.0, which is not correct.  After looking into the implementation of the editor for investment transactions, it turned out that regardless of the call to d->currentActivity->adjustStockSplit(d->stockSplit); from the instance of the Invest::Add class, which clears the price and value, retrieving the price from the split always returns 1.0. The cause of this problem lies in 

MyMoneyMoney MyMoneySplit::price() const
{
    Q_D(const MyMoneySplit);
    if (!d->m_Price.isZero())
        returns d->m_Price;
    if (!d->m_value.isZero() && !d->m_shares.isZero())
        return d->m_value / d->m_shares;
    return MyMoneyMoney::ONE;
}

where the corresponding branch in this case is 

    return MyMoneyMoney:::ONE;

and does not support the case that d->m_value.isZero() can be zero.
Comment 13 Ralf Habacker 2025-01-01 09:26:16 UTC
The second problem is that in InvestTransactionEditor::saveTransaction() the price is explicitly set to a value != 0.0. after 

    if (!d->stockSplit.shares().isZero()) {

The third problem is in QueryTable::constructTransactionTable(), where in this case the qS[ctPrice] map key is not set, which means that the actual price used is not displayed hiding issue one and two.

                ...
                if ((m_config.includes(splitAcc) && use_transfers &&
                        !(splitAcc.isInvest() && include_me)) || splits.count() == 1) { // otherwise stock split is displayed twice in report
                    if (! splitAcc.isIncomeExpense()) {
Comment 14 Ralf Habacker 2025-01-02 22:14:21 UTC
Git commit d3bd1a3b04282ed043050fffa5004fcc8e2ce89c by Ralf Habacker.
Committed on 02/01/2025 at 22:14.
Pushed by habacker into branch 'master'.

Extend adding debug information for generating xml export

The ListTable class receives a new method addSourceLine() for simple
output of source text lines of the associated map keys in the XML
output as TableRow attribute “ctSourceLines”.
This makes it possible to recognize at which source text positions
outputs are defined, whether a key is output multiple times or
not at all.

Corresponding outputs are currently generated for the ctValue
and ctPrice keys.

M  +7    -1    kmymoney/plugins/views/reports/core/listtable.h
M  +1    -1    kmymoney/plugins/views/reports/core/objectinfotable.cpp
M  +28   -10   kmymoney/plugins/views/reports/core/querytable.cpp

https://invent.kde.org/office/kmymoney/-/commit/d3bd1a3b04282ed043050fffa5004fcc8e2ce89c
Comment 15 Bug Janitor Service 2025-01-05 17:03:38 UTC
A possibly relevant merge request was started @ https://invent.kde.org/office/kmymoney/-/merge_requests/255
Comment 16 Thomas Baumgart 2025-01-06 06:53:43 UTC
Git commit 1f70255428af807093f26dd231e41b4140ee7390 by Thomas Baumgart, on behalf of Ralf Habacker.
Committed on 06/01/2025 at 06:53.
Pushed by tbaumgart into branch 'master'.

Refactor eDialogs::PriceMode

Preparation for !252

M  +0    -6    kmymoney/dialogs/dialogenums.h
M  +5    -16   kmymoney/dialogs/knewaccountdlg.cpp
M  +10   -0    kmymoney/mymoney/mymoneyaccount.cpp
M  +18   -0    kmymoney/mymoney/mymoneyaccount.h
M  +9    -0    kmymoney/mymoney/mymoneyenums.h
M  +19   -0    kmymoney/mymoney/tests/mymoneyaccount-test.cpp
M  +2    -0    kmymoney/mymoney/tests/mymoneyaccount-test.h
M  +13   -15   kmymoney/views/investactivities.cpp
M  +3    -6    kmymoney/views/investactivities.h
M  +1    -1    kmymoney/views/investtransactioneditor.cpp
M  +6    -3    kmymoney/wizards/newinvestmentwizard/knewinvestmentwizard.cpp

https://invent.kde.org/office/kmymoney/-/commit/1f70255428af807093f26dd231e41b4140ee7390