-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_total_per_range_days.js
More file actions
126 lines (101 loc) · 4.2 KB
/
get_total_per_range_days.js
File metadata and controls
126 lines (101 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const { Pool } = require('pg');
// Create a new instance of the Pool class
const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_DB,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
});
/*
async function calculateFinancialBalanceByDay(memberName, startDate, endDate) {
try {
const member = await getFamilyMemberByName(memberName);
if (!member) {
throw new Error(`Family member '${memberName}' not found.`);
}
const expenses = await getMonthlyExpensesByMemberId(member.id);
const transactions = await getIncomeTransactionsByMemberId(member.id);
const balanceByDay = [];
let currentBalance = 0;
// Iterate over each day in the range
const currentDate = new Date(startDate);
while (currentDate <= endDate) {
const formattedDate = currentDate.toISOString().split('T')[0];
// Calculate total expenses for the current day
const expensesForDay = expenses.filter(expense => expense.recurring_day === currentDate.getDate());
const totalExpenses = expensesForDay.reduce((sum, expense) => sum + expense.amount, 0);
// Calculate total income for the current day
const transactionsForDay = transactions.filter(transaction => transaction.transaction_date === formattedDate);
const totalIncome = transactionsForDay.reduce((sum, transaction) => sum + transaction.amount, 0);
// Update the current balance
currentBalance += totalIncome - totalExpenses;
// Store the balance for the current day
balanceByDay.push({
date: formattedDate,
financialBalance: currentBalance
});
// Move to the next day
currentDate.setDate(currentDate.getDate() + 1);
}
return balanceByDay;
} catch (error) {
throw new Error('Error calculating financial balance: ' + error.message);
}
}*/
async function logFinancialBalanceByDay(memberName, startDate, endDate) {
try {
const member = await pool.query('SELECT id FROM family_members WHERE name = $1', [memberName]);
if (member.rows.length === 0) {
console.log(`Family member ${memberName} not found.`);
return;
}
const memberId = member.rows[0].id;
const start = new Date(startDate);
const end = new Date(endDate);
const balanceByDay = [];
// Iterate over each day in the date range
for (let date = start; date <= end; date.setDate(date.getDate() + 1)) {
const expenses = await pool.query(
'SELECT amount FROM monthly_expenses WHERE member_id = $1 AND recurring_day <= $2',
[memberId, date.getDate()]
);
const transactions = await pool.query(
'SELECT amount FROM income_transactions WHERE member_id = $1 AND transaction_date = $2',
[memberId, date]
);
let totalExpenses = 0;
let totalIncome = 0;
// Calculate the total expenses for the day
for (const expense of expenses.rows) {
totalExpenses += parseFloat(expense.amount);
}
// Calculate the total income for the day
for (const transaction of transactions.rows) {
totalIncome += parseFloat(transaction.amount);
}
const financialBalance = totalIncome - totalExpenses;
balanceByDay.push({
date: date.toISOString().split('T')[0],
financialBalance
});
}
console.log(`Financial balance for ${memberName}:`);
balanceByDay.forEach(balance => {
console.log(`${balance.date}: $${balance.financialBalance.toFixed(2)}`);
});
} catch (error) {
console.error('Error calculating financial balance:', error.message);
} finally {
pool.end(); // Close the database connection
}
}
// Specify the member name and date range
const memberName = process.argv[2];
const startDate = process.argv[3];
const endDate = process.argv[4];
// Call the function to log the financial balance
logFinancialBalanceByDay(memberName, startDate, endDate);