diff --git a/lib/api/apis/goal_investment_api.dart b/lib/api/apis/goal_investment_api.dart index 9f8c4ac..f9aff9a 100644 --- a/lib/api/apis/goal_investment_api.dart +++ b/lib/api/apis/goal_investment_api.dart @@ -9,10 +9,10 @@ class GoalInvestmentApi { Future create( {required final int goalId, required final int investmentId, - required final double split}) async { + required final double splitPercentage}) async { return _db.into(_db.goalInvestmentTable).insert( GoalInvestmentTableCompanion.insert( - goalId: goalId, investmentId: investmentId, split: split)); + goalId: goalId, investmentId: investmentId, splitPercentage: splitPercentage)); } Future> getBy( @@ -38,12 +38,12 @@ class GoalInvestmentApi { {required final int id, required final int goalId, required final int investmentId, - required final double split}) async { + required final double splitPercentage}) async { return (_db.update(_db.goalInvestmentTable)..where((t) => t.id.equals(id))) .write(GoalInvestmentTableCompanion( investmentId: Value(investmentId), goalId: Value(goalId), - split: Value(split))); + splitPercentage: Value(splitPercentage))); } Future deleteBy( diff --git a/lib/api/db/app_database.dart b/lib/api/db/app_database.dart index d4cca4d..4cc5697 100644 --- a/lib/api/db/app_database.dart +++ b/lib/api/db/app_database.dart @@ -107,7 +107,7 @@ class GoalInvestmentTable extends Table { IntColumn get investmentId => integer().named('INVESTMENT_ID').references(GoalTable, #id)(); - RealColumn get split => real().named('SPLIT')(); + RealColumn get splitPercentage => real().named('SPLIT_PERCENTAGE')(); } @DataClassName('InvestmentEnrichedDO') diff --git a/lib/api/db/app_database.g.dart b/lib/api/db/app_database.g.dart index 71fc3b4..6e494e9 100644 --- a/lib/api/db/app_database.g.dart +++ b/lib/api/db/app_database.g.dart @@ -682,11 +682,11 @@ late final GeneratedColumn goalId = GeneratedColumn('GOAL_ID', aliased static const VerificationMeta _investmentIdMeta = const VerificationMeta('investmentId'); @override late final GeneratedColumn investmentId = GeneratedColumn('INVESTMENT_ID', aliasedName, false, type: DriftSqlType.int, requiredDuringInsert: true, defaultConstraints: GeneratedColumn.constraintIsAlways('REFERENCES goal_table (ID)')); -static const VerificationMeta _splitMeta = const VerificationMeta('split'); +static const VerificationMeta _splitPercentageMeta = const VerificationMeta('splitPercentage'); @override -late final GeneratedColumn split = GeneratedColumn('SPLIT', aliasedName, false, type: DriftSqlType.double, requiredDuringInsert: true); +late final GeneratedColumn splitPercentage = GeneratedColumn('SPLIT_PERCENTAGE', aliasedName, false, type: DriftSqlType.double, requiredDuringInsert: true); @override -List get $columns => [id, goalId, investmentId, split]; +List get $columns => [id, goalId, investmentId, splitPercentage]; @override String get aliasedName => _alias ?? actualTableName; @override @@ -705,16 +705,16 @@ if (data.containsKey('INVESTMENT_ID')) { context.handle(_investmentIdMeta, investmentId.isAcceptableOrUnknown(data['INVESTMENT_ID']!, _investmentIdMeta));} else if (isInserting) { context.missing(_investmentIdMeta); } -if (data.containsKey('SPLIT')) { -context.handle(_splitMeta, split.isAcceptableOrUnknown(data['SPLIT']!, _splitMeta));} else if (isInserting) { -context.missing(_splitMeta); +if (data.containsKey('SPLIT_PERCENTAGE')) { +context.handle(_splitPercentageMeta, splitPercentage.isAcceptableOrUnknown(data['SPLIT_PERCENTAGE']!, _splitPercentageMeta));} else if (isInserting) { +context.missing(_splitPercentageMeta); } return context; } @override Set get $primaryKey => {id}; @override GoalInvestmentDO map(Map data, {String? tablePrefix}) { -final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';return GoalInvestmentDO(id: attachedDatabase.typeMapping.read(DriftSqlType.int, data['${effectivePrefix}ID'])!, goalId: attachedDatabase.typeMapping.read(DriftSqlType.int, data['${effectivePrefix}GOAL_ID'])!, investmentId: attachedDatabase.typeMapping.read(DriftSqlType.int, data['${effectivePrefix}INVESTMENT_ID'])!, split: attachedDatabase.typeMapping.read(DriftSqlType.double, data['${effectivePrefix}SPLIT'])!, ); +final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';return GoalInvestmentDO(id: attachedDatabase.typeMapping.read(DriftSqlType.int, data['${effectivePrefix}ID'])!, goalId: attachedDatabase.typeMapping.read(DriftSqlType.int, data['${effectivePrefix}GOAL_ID'])!, investmentId: attachedDatabase.typeMapping.read(DriftSqlType.int, data['${effectivePrefix}INVESTMENT_ID'])!, splitPercentage: attachedDatabase.typeMapping.read(DriftSqlType.double, data['${effectivePrefix}SPLIT_PERCENTAGE'])!, ); } @override $GoalInvestmentTableTable createAlias(String alias) { @@ -722,44 +722,44 @@ return $GoalInvestmentTableTable(attachedDatabase, alias);}}class GoalInvestment final int id; final int goalId; final int investmentId; -final double split; -const GoalInvestmentDO({required this.id, required this.goalId, required this.investmentId, required this.split});@override +final double splitPercentage; +const GoalInvestmentDO({required this.id, required this.goalId, required this.investmentId, required this.splitPercentage});@override Map toColumns(bool nullToAbsent) { final map = {};map['ID'] = Variable(id); map['GOAL_ID'] = Variable(goalId); map['INVESTMENT_ID'] = Variable(investmentId); -map['SPLIT'] = Variable(split); +map['SPLIT_PERCENTAGE'] = Variable(splitPercentage); return map; } GoalInvestmentTableCompanion toCompanion(bool nullToAbsent) { -return GoalInvestmentTableCompanion(id: Value(id),goalId: Value(goalId),investmentId: Value(investmentId),split: Value(split),); +return GoalInvestmentTableCompanion(id: Value(id),goalId: Value(goalId),investmentId: Value(investmentId),splitPercentage: Value(splitPercentage),); } factory GoalInvestmentDO.fromJson(Map json, {ValueSerializer? serializer}) { serializer ??= driftRuntimeOptions.defaultSerializer; -return GoalInvestmentDO(id: serializer.fromJson(json['id']),goalId: serializer.fromJson(json['goalId']),investmentId: serializer.fromJson(json['investmentId']),split: serializer.fromJson(json['split']),);} +return GoalInvestmentDO(id: serializer.fromJson(json['id']),goalId: serializer.fromJson(json['goalId']),investmentId: serializer.fromJson(json['investmentId']),splitPercentage: serializer.fromJson(json['splitPercentage']),);} @override Map toJson({ValueSerializer? serializer}) { serializer ??= driftRuntimeOptions.defaultSerializer; return { -'id': serializer.toJson(id),'goalId': serializer.toJson(goalId),'investmentId': serializer.toJson(investmentId),'split': serializer.toJson(split),};}GoalInvestmentDO copyWith({int? id,int? goalId,int? investmentId,double? split}) => GoalInvestmentDO(id: id ?? this.id,goalId: goalId ?? this.goalId,investmentId: investmentId ?? this.investmentId,split: split ?? this.split,);@override -String toString() {return (StringBuffer('GoalInvestmentDO(')..write('id: $id, ')..write('goalId: $goalId, ')..write('investmentId: $investmentId, ')..write('split: $split')..write(')')).toString();} +'id': serializer.toJson(id),'goalId': serializer.toJson(goalId),'investmentId': serializer.toJson(investmentId),'splitPercentage': serializer.toJson(splitPercentage),};}GoalInvestmentDO copyWith({int? id,int? goalId,int? investmentId,double? splitPercentage}) => GoalInvestmentDO(id: id ?? this.id,goalId: goalId ?? this.goalId,investmentId: investmentId ?? this.investmentId,splitPercentage: splitPercentage ?? this.splitPercentage,);@override +String toString() {return (StringBuffer('GoalInvestmentDO(')..write('id: $id, ')..write('goalId: $goalId, ')..write('investmentId: $investmentId, ')..write('splitPercentage: $splitPercentage')..write(')')).toString();} @override - int get hashCode => Object.hash(id, goalId, investmentId, split);@override -bool operator ==(Object other) => identical(this, other) || (other is GoalInvestmentDO && other.id == this.id && other.goalId == this.goalId && other.investmentId == this.investmentId && other.split == this.split); + int get hashCode => Object.hash(id, goalId, investmentId, splitPercentage);@override +bool operator ==(Object other) => identical(this, other) || (other is GoalInvestmentDO && other.id == this.id && other.goalId == this.goalId && other.investmentId == this.investmentId && other.splitPercentage == this.splitPercentage); }class GoalInvestmentTableCompanion extends UpdateCompanion { final Value id; final Value goalId; final Value investmentId; -final Value split; -const GoalInvestmentTableCompanion({this.id = const Value.absent(),this.goalId = const Value.absent(),this.investmentId = const Value.absent(),this.split = const Value.absent(),}); -GoalInvestmentTableCompanion.insert({this.id = const Value.absent(),required int goalId,required int investmentId,required double split,}): goalId = Value(goalId), investmentId = Value(investmentId), split = Value(split); +final Value splitPercentage; +const GoalInvestmentTableCompanion({this.id = const Value.absent(),this.goalId = const Value.absent(),this.investmentId = const Value.absent(),this.splitPercentage = const Value.absent(),}); +GoalInvestmentTableCompanion.insert({this.id = const Value.absent(),required int goalId,required int investmentId,required double splitPercentage,}): goalId = Value(goalId), investmentId = Value(investmentId), splitPercentage = Value(splitPercentage); static Insertable custom({Expression? id, Expression? goalId, Expression? investmentId, -Expression? split, +Expression? splitPercentage, }) { -return RawValuesInsertable({if (id != null)'ID': id,if (goalId != null)'GOAL_ID': goalId,if (investmentId != null)'INVESTMENT_ID': investmentId,if (split != null)'SPLIT': split,}); -}GoalInvestmentTableCompanion copyWith({Value? id, Value? goalId, Value? investmentId, Value? split}) { -return GoalInvestmentTableCompanion(id: id ?? this.id,goalId: goalId ?? this.goalId,investmentId: investmentId ?? this.investmentId,split: split ?? this.split,); +return RawValuesInsertable({if (id != null)'ID': id,if (goalId != null)'GOAL_ID': goalId,if (investmentId != null)'INVESTMENT_ID': investmentId,if (splitPercentage != null)'SPLIT_PERCENTAGE': splitPercentage,}); +}GoalInvestmentTableCompanion copyWith({Value? id, Value? goalId, Value? investmentId, Value? splitPercentage}) { +return GoalInvestmentTableCompanion(id: id ?? this.id,goalId: goalId ?? this.goalId,investmentId: investmentId ?? this.investmentId,splitPercentage: splitPercentage ?? this.splitPercentage,); } @override Map toColumns(bool nullToAbsent) { @@ -769,12 +769,12 @@ if (goalId.present) { map['GOAL_ID'] = Variable(goalId.value);} if (investmentId.present) { map['INVESTMENT_ID'] = Variable(investmentId.value);} -if (split.present) { -map['SPLIT'] = Variable(split.value);} +if (splitPercentage.present) { +map['SPLIT_PERCENTAGE'] = Variable(splitPercentage.value);} return map; } @override -String toString() {return (StringBuffer('GoalInvestmentTableCompanion(')..write('id: $id, ')..write('goalId: $goalId, ')..write('investmentId: $investmentId, ')..write('split: $split')..write(')')).toString();} +String toString() {return (StringBuffer('GoalInvestmentTableCompanion(')..write('id: $id, ')..write('goalId: $goalId, ')..write('investmentId: $investmentId, ')..write('splitPercentage: $splitPercentage')..write(')')).toString();} } class InvestmentEnrichedDO extends DataClass { final int id; diff --git a/lib/domain/models/goal.dart b/lib/domain/models/goal.dart index e6e0c54..cee246b 100644 --- a/lib/domain/models/goal.dart +++ b/lib/domain/models/goal.dart @@ -4,6 +4,7 @@ import 'package:wealth_wave/api/db/app_database.dart'; import 'package:wealth_wave/contract/goal_importance.dart'; import 'package:wealth_wave/domain/models/investment.dart'; import 'package:wealth_wave/domain/models/irr_calculator.dart'; +import 'package:wealth_wave/utils/utils.dart'; class Goal { final int id; @@ -44,7 +45,7 @@ class Goal { .then((investmentDO) => Investment.from(investmentDO: investmentDO)) .then((investment) => - MapEntry(investment, goalInvestment.split))))) + MapEntry(investment, goalInvestment.splitPercentage))))) .then((entries) => Map.fromEntries(entries)); } @@ -57,8 +58,10 @@ class Goal { .then((investmentDO) => Investment.from(investmentDO: investmentDO)) .then((investment) => investment.getTotalInvestedAmount()) - .then((amount) => amount * goalInvestment.split)))) - .then((amounts) => amounts.isNotEmpty ? amounts.reduce((value, element) => value + element): 0); + .then((amount) => amount * goalInvestment.splitPercentage)))) + .then((amounts) => amounts.isNotEmpty + ? amounts.reduce((value, element) => value + element) + : 0); } Future getMaturityAmount() { @@ -79,14 +82,18 @@ class Goal { Investment.from(investmentDO: investmentDO)) .then((investment) => investment.getValueOn( date: maturityDate, considerFuturePayments: true)) - .then((amount) => amount * goalInvestment.split)))) - .then((amounts) => amounts.isNotEmpty ? amounts.reduce((value, element) => value + element): 0); + .then((amount) => calculatePercentageOfValue( + value: amount, + percentage: goalInvestment.splitPercentage))))) + .then((amounts) => amounts.isNotEmpty + ? amounts.reduce((value, element) => value + element) + : 0); } Future tagInvestment( {required final int investmentId, required final double split}) async { return _goalInvestmentApi - .create(goalId: id, investmentId: investmentId, split: split) + .create(goalId: id, investmentId: investmentId, splitPercentage: split) .then((goalInvestmentDO) => {}); } @@ -95,7 +102,11 @@ class Goal { required final int investmentId, required final double split}) async { return _goalInvestmentApi - .update(id: id, goalId: id, investmentId: investmentId, split: split) + .update( + id: id, + goalId: id, + investmentId: investmentId, + splitPercentage: split) .then((goalInvestmentDO) => {}); } diff --git a/lib/domain/models/investment.dart b/lib/domain/models/investment.dart index f0f6b71..accb26a 100644 --- a/lib/domain/models/investment.dart +++ b/lib/domain/models/investment.dart @@ -117,7 +117,7 @@ class Investment { (goalInvestment) => _goalApi .getBy(id: goalInvestment.goalId) .then((goalDO) => Goal.from(goalDO: goalDO)) - .then((goal) => MapEntry(goal, goalInvestment.split))))) + .then((goal) => MapEntry(goal, goalInvestment.splitPercentage))))) .then((entries) => Map.fromEntries(entries)); } @@ -211,7 +211,7 @@ class Investment { Future tagGoal( {required final int goalId, required final double split}) async { return _goalInvestmentApi - .create(goalId: goalId, investmentId: id, split: split) + .create(goalId: goalId, investmentId: id, splitPercentage: split) .then((goalInvestmentDO) => {}); } @@ -220,7 +220,7 @@ class Investment { required final int goalId, required final double split}) async { return _goalInvestmentApi - .update(id: id, goalId: goalId, investmentId: id, split: split) + .update(id: id, goalId: goalId, investmentId: id, splitPercentage: split) .then((goalInvestmentDO) => {}); } diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart new file mode 100644 index 0000000..24a2cde --- /dev/null +++ b/lib/utils/utils.dart @@ -0,0 +1,4 @@ +double calculatePercentageOfValue( + {required final double value, required final double percentage}) { + return value * percentage / 100; +}