Skip to content

Commit

Permalink
fix qty 0 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
praslnx8 committed Aug 9, 2024
1 parent 6dd78bf commit 54c6a02
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
31 changes: 15 additions & 16 deletions lib/domain/models/investment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,8 @@ class Investment {
.fold(0, (value, element) => value + element);
}

double get qty {
if (_qty == null) {
return 1;
} else if (_qty == 0) {
return 1;
} else {
return _qty;
}
double? get qty {
return _qty;
}

DateTime getLastInvestedOn() {
Expand All @@ -78,18 +72,21 @@ class Investment {
final value = this.value;
final irr = this.irr;
if (value != null) {
return value * qty;
return value * (qty ?? 1);
} else if (irr != null) {
final payments = getPayments(till: DateTime.now());
return IRRCalculator().calculateFutureValueOnIRR(
payments: payments, irr: irr, futureDate: DateTime.now());
} else {
throw Exception('Value and IRR are null');
return 0;
}
}

double getValuePerUnit() {
return getValue() / qty;
if (qty == 0) {
return 0;
}
return getValue() / (qty ?? 1);
}

double getValueOn(
Expand All @@ -107,7 +104,7 @@ class Investment {
till: futureDate, considerFuturePayments: considerFuturePayments);
final irr = IRRCalculator().calculateIRR(
payments: paymentTillNow,
value: value * qty,
value: value * (qty ?? 1),
valueUpdatedOn: DateTime.now());
return IRRCalculator().calculateFutureValueOnIRR(
irr: irr,
Expand All @@ -120,7 +117,7 @@ class Investment {
return IRRCalculator().calculateFutureValueOnIRR(
payments: payments, irr: irr, futureDate: futureDate);
} else {
throw Exception('Value and IRR are null');
return 0;
}
}

Expand All @@ -134,10 +131,10 @@ class Investment {

return IRRCalculator().calculateIRR(
payments: payments,
value: value * qty,
value: value * (qty ?? 1),
valueUpdatedOn: DateTime.now());
} else {
throw Exception('Value and IRR are null');
return 0;
}
}

Expand All @@ -153,7 +150,9 @@ class Investment {
riskLevel: investmentDO.riskLevel,
irr: investmentDO.irr,
value: investmentDO.value,
qty: investmentDO.qty,
qty: transactionsDOs.any((element) => element.qty > 0)
? investmentDO.qty
: null,
valueUpdatedOn: investmentDO.valueUpdatedOn,
basketId: investmentDO.basketId,
maturityDate: investmentDO.maturityDate,
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/models/investment_vo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class InvestmentVO {
investedValue: investment.getTotalInvestedAmount(),
currentValue: investment.getValue(),
valueUpdatedDate: investment.valueUpdatedOn,
qty: investment.qty,
qty: investment.qty ?? 1,
valuePerQty: investment.getValuePerUnit(),
basketId: investment.basketId,
basketName: investment.basketName,
Expand Down
4 changes: 3 additions & 1 deletion lib/ui/pages/investments_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class _InvestmentsPage extends PageState<InvestmentsViewState, InvestmentsPage,
{required final BuildContext context,
required final InvestmentVO investmentVO}) {
return Card(
color: investmentVO.qty == 0 ? Colors.grey : null,
margin: const EdgeInsets.all(AppDimen.defaultPadding),
child: InkWell(
onTap: () => {
Expand Down Expand Up @@ -177,7 +178,8 @@ class _InvestmentsPage extends PageState<InvestmentsViewState, InvestmentsPage,
Text(
('Qty ${NumberFormat.compact().format(investmentVO.qty)}'),
),
const Padding(padding: EdgeInsets.all(AppDimen.minPadding)),
const Padding(
padding: EdgeInsets.all(AppDimen.minPadding)),
Text(
('Price ${NumberFormat.compact().format(investmentVO.valuePerQty)}'),
),
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/presentation/investment_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class InvestmentVO {
irr: investment.getIRR(),
investedValue: investment.getTotalInvestedAmount(),
currentValue: investment.getValue(),
qty: investment.qty,
qty: investment.qty ?? 1,
basketId: investment.basketId,
basketName: investment.basketName,
transactions: investment.transactions,
Expand Down
1 change: 0 additions & 1 deletion lib/ui/presentation/investments_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class InvestmentsPresenter extends Presenter<InvestmentsViewState> {
.get()
.then((investments) => investments
.map((investment) => InvestmentVO.from(investment: investment))
.where((element) => element.qty > 0)
.toList())
.then((investmentVOs) {
investmentVOs.sort((a, b) => a.currentValue > b.currentValue ? -1 : 1);
Expand Down

0 comments on commit 54c6a02

Please sign in to comment.