Skip to content

Commit 9f77b78

Browse files
committed
ix calculations
1 parent 1806da6 commit 9f77b78

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

src/app/component/income-components/saving/saving.component.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,34 @@ export class SavingComponent implements OnInit {
6262

6363
calculateStats() {
6464
this.totalSavedAmount = this.savings.reduce((sum, s) => sum + Number(s.amount || 0), 0);
65-
this.goalTarget = Number(this.currentGoal?.target_amount || 0);
65+
this.goalTarget = this.goalTargetFunction();
6666

67-
const progress = this.goalTarget > 0 ? (this.totalSavedAmount / this.goalTarget) * 100 : 0;
67+
const totalSavedBetweenGoalRange = this.savingsService.gettotalSavingsBetweenDates(this.currentGoal?.start_date || null, this.currentGoal?.target_date || null);
68+
69+
const progress = this.goalTarget > 0 ? (totalSavedBetweenGoalRange / this.goalTarget) * 100 : 0;
6870
this.progressPercentage = Math.min(Math.max(progress, 0), 100);
6971
this.remainingPercentage = Math.max(100 - this.progressPercentage, 0);
72+
this.goalRemaining = (this.goalTarget - totalSavedBetweenGoalRange) > 0 ? this.goalTarget - totalSavedBetweenGoalRange : 0;
7073

71-
this.goalRemaining = (this.goalTarget - this.totalSavedAmount) > 0 ? this.goalTarget - this.totalSavedAmount : 0;
72-
73-
if (this.currentGoal?.start_date && this.totalSavedAmount > 0) {
74+
if (this.currentGoal?.start_date && totalSavedBetweenGoalRange > 0) {
7475
const start = new Date(this.currentGoal.start_date).getTime();
7576
const today = new Date().getTime();
7677
let days = Math.ceil((today - start) / (1000 * 3600 * 24));
77-
this.averageSavedPerDay = this.totalSavedAmount / (days <= 0 ? 1 : days);
78+
this.averageSavedPerDay = totalSavedBetweenGoalRange / (days <= 0 ? 1 : days);
7879
} else {
7980
this.averageSavedPerDay = 0;
8081
}
8182
this.allowedPerDay = this.allowedPerDayFunction();
8283
this.suggestedPerDay = this.suggestedPerDayFunction();
8384
}
8485

86+
87+
goalTargetFunction(): number {
88+
if (this.currentGoal) {
89+
return Number(this.currentGoal.target_amount);
90+
}
91+
return 0;
92+
}
8593
allowedPerDayFunction(): number {
8694
if (this.currentGoal) {
8795
const days = this.utilService.calculateDaysBetween(this.currentGoal.start_date, this.currentGoal.target_date);

src/app/service/localStorage/savings.service.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,21 @@ export class SavingsService {
117117
const all: Saving[] = this.getAll();
118118
return all.filter(item => item.is_from_income).reduce((acc, item) => acc + item.amount, 0);
119119
}
120+
121+
gettotalSavingsBetweenDates(from: string | null, to: string | null): number {
122+
if (!this.isBrowser()) return 0;
123+
if (from && to) {
124+
const all: Saving[] = this.getAll();
125+
return all.filter(item => {
126+
const itemDate = new Date(item.date);
127+
itemDate.setHours(0, 0, 0, 0);
128+
const fromDate = new Date(from);
129+
const toDate = new Date(to);
130+
fromDate.setHours(0, 0, 0, 0);
131+
toDate.setHours(23, 59, 59, 999);
132+
return itemDate >= fromDate && itemDate <= toDate;
133+
}).reduce((acc, item) => acc + item.amount, 0);
134+
}
135+
return 0;
136+
}
120137
}

0 commit comments

Comments
 (0)