Skip to content

Commit

Permalink
harbour-watchlist: ISSUE-105 : Add Option to display Capital Gain (WIP)
Browse files Browse the repository at this point in the history
* calculate display current position value and initial position value;
* added testcase
  • Loading branch information
andywuest committed May 16, 2023
1 parent 15bdb26 commit 6b01759
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
29 changes: 29 additions & 0 deletions qml/components/StockDetailsView.qml
Expand Up @@ -173,6 +173,24 @@ SilicaFlickable {
visible: false;
}

// TODO check if displayed and with which label
LabelValueRow {
id: positionValuePurchaseLabelValueRow
//: StockDetailsView page position value purchase
label: qsTr("Position value purchase")
value: ''
visible: false;
}

// TODO check if displayed and with which label
LabelValueRow {
id: positionValueCurrentLabelValueRow
//: StockDetailsView page position value
label: qsTr("Position value current")
value: ''
visible: false;
}

LabelOnlyRow {
id: notesRow
label: ''
Expand Down Expand Up @@ -214,6 +232,17 @@ SilicaFlickable {
piecesLabelValueRow.value = '' + pieces;
piecesLabelValueRow.visible = true;
}
var positionValuePurchase = stock.positionValuePurchase;
if (positionValuePurchase && positionValuePurchase !== 0.0) {
positionValuePurchaseLabelValueRow.value = Functions.renderPrice(positionValuePurchase, currencySymbol);
positionValuePurchaseLabelValueRow.visible = true;
}
var positionValueCurrent = stock.positionValueCurrent;
if (positionValueCurrent && positionValueCurrent !== 0.0) {
positionValueCurrentLabelValueRow.value = Functions.renderPrice(positionValueCurrent, currencySymbol);
positionValueCurrentLabelValueRow.visible = true;
}

}
}

Expand Down
6 changes: 5 additions & 1 deletion qml/js/database.js
Expand Up @@ -186,7 +186,7 @@ function initApplicationTables() {
console.log("Performing DB update from 1.4 to 1.5!")
db.changeVersion("1.4", "1.5", function (tx) {
// add new column to stockdata_ext
tx.executeSql("ALTER TABLE stockdata_ext ADD COLUMN pieces INTEGER");
tx.executeSql("ALTER TABLE stockdata_ext ADD COLUMN pieces INTEGER DEFAULT 0");
})
}

Expand Down Expand Up @@ -554,6 +554,8 @@ function loadStockData(watchListId, sortString, extRefId) {
+ ' COALESCE(se.notes, "") as notes, '
+ ' COALESCE(se.referencePrice, 0.0) as referencePrice, '
+ ' COALESCE(se.pieces, 0) as pieces, '
+ ' COALESCE(se.referencePrice, 0.0) * COALESCE(se.pieces, 0) as positionValuePurchase, '
+ ' COALESCE(price, 0.0) * COALESCE(se.pieces, 0) as positionValueCurrent, '
+ ' COALESCE(ROUND(100 * (price - COALESCE(referencePrice, 0.0)) / COALESCE(referencePrice, 0.0), 2), 0.0) as performanceRelative '
+ ' FROM stockdata s '
+ ' LEFT OUTER JOIN stockdata_ext se '
Expand Down Expand Up @@ -598,6 +600,8 @@ function loadStockData(watchListId, sortString, extRefId) {
entry.referencePrice = row.referencePrice;
entry.pieces = row.pieces;
entry.performanceRelative = row.performanceRelative; // calculated!
entry.positionValuePurchase = row.positionValuePurchase; // calculated!
entry.positionValueCurrent = row.positionValueCurrent; // calculated!
result.push(entry);
}
log("[loadStockData] loading stockdata data from database done");
Expand Down
10 changes: 10 additions & 0 deletions qml/js/functions.js
Expand Up @@ -108,6 +108,16 @@ function calculateWidth(price, change, maxChange, parentWidth) {
}
}

function calculateDepotValue(stocks) {
var sum = 0.0;
if (stocks && stocks.length > 0) {
for (var index = 0; index < stocks.length; index++) {
sum += stocks[index].positionValue;
}
}
return sum;
}

function log(message) {
if (loggingEnabled && message) {
console.log(message);
Expand Down
17 changes: 17 additions & 0 deletions test/qml/tst_functions.qml
Expand Up @@ -38,4 +38,21 @@ TestCase {
compare(Functions.renderPrice(0.541234, 'EUR', Constants.MARKET_DATA_TYPE_NONE), "0,54 EUR")
}

function test_functions_calculateDepotValue() {
// given
var stock1 = {};
stock1.positionValue = 1234.32;
var stock2 = {};
stock2.positionValue = 2453.10;
var stock3 = {};
stock3.positionValue = 4000.00;
var stocks = [];
stocks.push(stock1);
stocks.push(stock2);
stocks.push(stock3);

// when / then
compare(Functions.calculateDepotValue(stocks), 7687.42);
}

}

0 comments on commit 6b01759

Please sign in to comment.