Changeset View
Changeset View
Standalone View
Standalone View
kmymoney/plugins/views/reports/core/querytable.cpp
Show First 20 Lines • Show All 352 Lines • ▼ Show 20 Line(s) | 341 | if (!MyMoneyMoney((*currencyGrp).at(i + 1).value(ctRowsCount)).isZero()) { // if no rows summed up, then no totals row | |||
---|---|---|---|---|---|
353 | } | 353 | } | ||
354 | 354 | | |||
355 | // custom total values calculations | 355 | // custom total values calculations | ||
356 | foreach (auto subtotal, subtotals) { | 356 | foreach (auto subtotal, subtotals) { | ||
357 | if (subtotal == ctReturnInvestment) | 357 | if (subtotal == ctReturnInvestment) | ||
358 | totalsRow[subtotal] = helperROI((*currencyGrp).at(i + 1).value(ctBuys) - (*currencyGrp).at(i + 1).value(ctReinvestIncome), (*currencyGrp).at(i + 1).value(ctSells), | 358 | totalsRow[subtotal] = helperROI((*currencyGrp).at(i + 1).value(ctBuys) - (*currencyGrp).at(i + 1).value(ctReinvestIncome), (*currencyGrp).at(i + 1).value(ctSells), | ||
359 | (*currencyGrp).at(i + 1).value(ctStartingBalance), (*currencyGrp).at(i + 1).value(ctEndingBalance) + (*currencyGrp).at(i + 1).value(ctMarketValue), | 359 | (*currencyGrp).at(i + 1).value(ctStartingBalance), (*currencyGrp).at(i + 1).value(ctEndingBalance) + (*currencyGrp).at(i + 1).value(ctMarketValue), | ||
360 | (*currencyGrp).at(i + 1).value(ctCashIncome)); | 360 | (*currencyGrp).at(i + 1).value(ctCashIncome)); | ||
361 | else if (subtotal == ctPercentageGain) | 361 | else if (subtotal == ctPercentageGain) { | ||
362 | totalsRow[subtotal] = (((*currencyGrp).at(i + 1).value(ctBuys) + (*currencyGrp).at(i + 1).value(ctMarketValue)) / (*currencyGrp).at(i + 1).value(ctBuys).abs()).toString(); | 362 | const MyMoneyMoney denominator = (*currencyGrp).at(i + 1).value(ctBuys).abs(); | ||
363 | else if (subtotal == ctPrice) | 363 | totalsRow[subtotal] = denominator.isZero() ? QString(): | ||
tbaumgart: Change
denominator == 0
to
denominator.isZero()
which is faster. | |||||
364 | (((*currencyGrp).at(i + 1).value(ctBuys) + (*currencyGrp).at(i + 1).value(ctMarketValue)) / denominator).toString(); | ||||
365 | } else if (subtotal == ctPrice) | ||||
364 | totalsRow[subtotal] = MyMoneyMoney((*currencyGrp).at(i + 1).value(ctPrice) / (*currencyGrp).at(i + 1).value(ctRowsCount)).toString(); | 366 | totalsRow[subtotal] = MyMoneyMoney((*currencyGrp).at(i + 1).value(ctPrice) / (*currencyGrp).at(i + 1).value(ctRowsCount)).toString(); | ||
365 | } | 367 | } | ||
366 | 368 | | |||
367 | // total values that aren't calculated here, but are taken untouched from external source, e.g. constructPerformanceRow | 369 | // total values that aren't calculated here, but are taken untouched from external source, e.g. constructPerformanceRow | ||
368 | if (!stashedTotalRows.isEmpty()) { | 370 | if (!stashedTotalRows.isEmpty()) { | ||
369 | for (int j = 0; j < stashedTotalRows.count(); ++j) { | 371 | for (int j = 0; j < stashedTotalRows.count(); ++j) { | ||
370 | if (stashedTotalRows.at(j).value(ctCurrency) != currencyID) | 372 | if (stashedTotalRows.at(j).value(ctCurrency) != currencyID) | ||
371 | continue; | 373 | continue; | ||
▲ Show 20 Lines • Show All 680 Lines • ▼ Show 20 Line(s) | 982 | for (auto it_account = accts.constBegin(); it_account != accts.constEnd(); ++it_account) { | |||
1052 | qA[ctID] = QLatin1Char('Z'); | 1054 | qA[ctID] = QLatin1Char('Z'); | ||
1053 | m_rows += qA; | 1055 | m_rows += qA; | ||
1054 | } | 1056 | } | ||
1055 | } | 1057 | } | ||
1056 | 1058 | | |||
1057 | QString QueryTable::helperROI(const MyMoneyMoney &buys, const MyMoneyMoney &sells, const MyMoneyMoney &startingBal, const MyMoneyMoney &endingBal, const MyMoneyMoney &cashIncome) const | 1059 | QString QueryTable::helperROI(const MyMoneyMoney &buys, const MyMoneyMoney &sells, const MyMoneyMoney &startingBal, const MyMoneyMoney &endingBal, const MyMoneyMoney &cashIncome) const | ||
1058 | { | 1060 | { | ||
1059 | MyMoneyMoney returnInvestment; | 1061 | MyMoneyMoney returnInvestment; | ||
1060 | if (!buys.isZero() || !startingBal.isZero()) { | 1062 | if (!(startingBal - buys).isZero()) { | ||
tbaumgart: Use
if (!(startingBal - buys).isZero()) {
here. | |||||
1061 | returnInvestment = (sells + buys + cashIncome + endingBal - startingBal) / (startingBal - buys); | 1063 | returnInvestment = (sells + buys + cashIncome + endingBal - startingBal) / (startingBal - buys); | ||
1062 | return returnInvestment.convert(10000).toString(); | 1064 | return returnInvestment.convert(10000).toString(); | ||
1063 | } else | 1065 | } else | ||
1064 | return QString(); | 1066 | return QString(); | ||
1065 | } | 1067 | } | ||
1066 | 1068 | | |||
1067 | QString QueryTable::helperIRR(const CashFlowList &all) const | 1069 | QString QueryTable::helperIRR(const CashFlowList &all) const | ||
1068 | { | 1070 | { | ||
▲ Show 20 Lines • Show All 382 Lines • ▼ Show 20 Line(s) | 1428 | { | |||
1451 | 1453 | | |||
1452 | int pricePrecision = file->security(account.currencyId()).pricePrecision(); | 1454 | int pricePrecision = file->security(account.currencyId()).pricePrecision(); | ||
1453 | result[ctBuys] = buysTotal.toString(); | 1455 | result[ctBuys] = buysTotal.toString(); | ||
1454 | result[ctShares] = shList.at(BuysOfOwned).toString(); | 1456 | result[ctShares] = shList.at(BuysOfOwned).toString(); | ||
1455 | result[ctBuyPrice] = (buysTotal.abs() / shList.at(BuysOfOwned)).convertPrecision(pricePrecision).toString(); | 1457 | result[ctBuyPrice] = (buysTotal.abs() / shList.at(BuysOfOwned)).convertPrecision(pricePrecision).toString(); | ||
1456 | result[ctLastPrice] = price.toString(); | 1458 | result[ctLastPrice] = price.toString(); | ||
1457 | result[ctMarketValue] = endingBal.toString(); | 1459 | result[ctMarketValue] = endingBal.toString(); | ||
1458 | result[ctCapitalGain] = (buysTotal + endingBal).toString(); | 1460 | result[ctCapitalGain] = (buysTotal + endingBal).toString(); | ||
1459 | result[ctPercentageGain] = ((buysTotal + endingBal)/buysTotal.abs()).toString(); | 1461 | result[ctPercentageGain] = buysTotal.isZero() ? QString() : | ||
Change buysTotal.abs() == 0 to buysTotal.isZero() and save more CPU cycles. No need to take the absolute value of zero. tbaumgart: Change
buysTotal.abs() == 0
to
buysTotal.isZero()
and save more CPU cycles. No need… | |||||
1462 | ((buysTotal + endingBal)/buysTotal.abs()).toString(); | ||||
1460 | break; | 1463 | break; | ||
1461 | } | 1464 | } | ||
1462 | case eMyMoney::Report::InvestmentSum::Sold: | 1465 | case eMyMoney::Report::InvestmentSum::Sold: | ||
1463 | default: | 1466 | default: | ||
1464 | buysTotal = cfList.at(BuysOfSells).total() - cfList.at(ReinvestIncome).total(); | 1467 | buysTotal = cfList.at(BuysOfSells).total() - cfList.at(ReinvestIncome).total(); | ||
1465 | sellsTotal = cfList.at(Sells).total(); | 1468 | sellsTotal = cfList.at(Sells).total(); | ||
1466 | longTermBuysOfSellsTotal = cfList.at(LongTermBuysOfSells).total(); | 1469 | longTermBuysOfSellsTotal = cfList.at(LongTermBuysOfSells).total(); | ||
1467 | longTermSellsOfBuys = cfList.at(LongTermSellsOfBuys).total(); | 1470 | longTermSellsOfBuys = cfList.at(LongTermSellsOfBuys).total(); | ||
▲ Show 20 Lines • Show All 505 Lines • Show Last 20 Lines |
Change
to
which is faster.