-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
185 lines (164 loc) · 6.92 KB
/
index.html
File metadata and controls
185 lines (164 loc) · 6.92 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Shared Expense App</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/tailwindcss@latest/dist/tailwind.min.css">
</head>
<body class="bg-gray-100">
<div class="container mx-auto p-4" x-data="app()" x-init="mounted()">
<div class="mb-4 md:flex items-center md:space-x-4 space-y-4 md:space-y-0">
<div class="flex-1">
<label for="description" class="block text-gray-700 text-sm font-bold mb-2">Description:</label>
<input id="description" type="text"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
x-model.trim="description" placeholder="Enter a description of the expense">
</div>
<div class="flex-1">
<label for="payer" class="block text-gray-700 text-sm font-bold mb-2">Payer:</label>
<select id="payer"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
x-model="selectedPayer">
<option value="" disabled selected>Select the person who paid the expense</option>
<template x-for="(person, index) in peopleList" :key="index">
<option x-text="person" :value="person"></option>
</template>
</select>
</div>
<div class="flex-1">
<label for="amount" class="block text-gray-700 text-sm font-bold mb-2">Amount (€):</label>
<input id="amount" type="number" min="0" step="0.01"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
x-model.number="amount" placeholder="Enter the amount of the expense">
</div>
</div>
<div class="mb-4">
<button type="button"
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline"
@click.prevent="addExpense">Add Expense</button>
</div>
<div class="mb-4">
<h2 class="text-gray-700 text-lg font-bold mb-2">Expenses:</h2>
<ul class="list-disc list-inside space-y-2">
<template x-for="(expense, index) in expenses" :key="index">
<li x-text="'€' + expense.amount.toFixed(2) + ' paid by ' + expense.payer + ' for ' + expense.description">
</li>
</template>
</ul>
</div>
<div class="mb-4">
<h2 class="text-gray-700 text-lg font-bold mb-2">Balance:</h2>
<ul class="list-disc list-inside space-y-2">
<li x-text="'Total amount ' + totalAmount.toFixed(2) + '€, ' + averageAmount.toFixed(2) + '€ per person'"></li>
<template x-for="(payment, index) in payments" :key="index">
<li x-text="'€' + payment.amount.toFixed(2) + ' from ' + payment.from + ' to ' + payment.to"></li>
</template>
</ul>
</div>
<div class="mb-4">
<button type="button"
class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline"
@click="recalculateBalance">Recalculate Balance</button>
</div>
</div>
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
<script>
function app() {
return {
amount: '',
selectedPayer: '',
description: '',
expenses: [],
payments: [],
totalAmount: 0, // Add this line
averageAmount: 0, // Add this line
mounted() {
if (localStorage.expenses) {
this.expenses = JSON.parse(localStorage.expenses);
}
if (localStorage.payments) {
this.payments = JSON.parse(localStorage.payments);
}
if (localStorage.people) {
this.peopleList = JSON.parse(localStorage.people);
}
this.recalculateBalance()
},
updated() {
localStorage.expenses = JSON.stringify(this.expenses);
localStorage.payments = JSON.stringify(this.payments);
localStorage.people = JSON.stringify(this.peopleList);
},
addExpense() {
if (this.amount <= 0) {
alert('Please enter a positive amount');
return;
}
if (this.selectedPayer === '') {
alert('Please select a payer');
return;
}
if (this.description === '') {
alert('Please enter a description');
return;
}
this.expenses.push({
amount: parseFloat(this.amount), // Parse float
payer: this.selectedPayer,
description: this.description
});
this.amount = '';
this.selectedPayer = '';
this.description = '';
this.updatePayments();
this.updateLocalStorage();
},
updateLocalStorage() {
localStorage.expenses = JSON.stringify(this.expenses);
localStorage.payments = JSON.stringify(this.payments);
localStorage.people = JSON.stringify(this.peopleList);
},
recalculateBalance() {
this.updatePayments();
this.updateLocalStorage(); // Update local storage with recalculated balance
},
updatePayments() {
this.payments = []; // Reset payments
let balances = new Map();
for (let person of this.peopleList) {
balances.set(person, 0);
}
for (let expense of this.expenses) {
balances.set(expense.payer, balances.get(expense.payer) + parseFloat(expense.amount));
}
let totalAmount = this.expenses.reduce((sum, expense) => sum + parseFloat(expense.amount), 0);
let averageAmount = totalAmount / this.peopleList.length;
for (let [person, balance] of balances) {
balances.set(person, balance - averageAmount);
}
let debtors = [...balances.entries()].filter(([person, balance]) => balance < 0).sort((a, b) => a[1] - b[1]);
let creditors = [...balances.entries()].filter(([person, balance]) => balance > 0).sort((a, b) => b[1] - a[1]);
let i = 0; // Index for debtors
let j = 0; // Index for creditors
while (i < debtors.length && j < creditors.length) {
let debtor = debtors[i];
let creditor = creditors[j];
let amount = Math.min(-debtor[1], creditor[1]);
this.payments.push({
from: debtor[0],
to: creditor[0],
amount: amount
});
debtor[1] += amount;
creditor[1] -= amount;
if (debtor[1] === 0) i++; // Move to the next debtor
if (creditor[1] === 0) j++; // Move to the next creditor
}
this.totalAmount = totalAmount; // Set totalAmount
this.averageAmount = averageAmount; // Set averageAmount
},
}
}
</script>
</body>
</html>