-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-transaction.js
More file actions
45 lines (41 loc) · 1.64 KB
/
add-transaction.js
File metadata and controls
45 lines (41 loc) · 1.64 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
window.addEventListener('DOMContentLoaded', function () {
const dateInput = document.getElementById('date');
if (dateInput) {
const today = new Date();
const yyyy = today.getFullYear();
const mm = String(today.getMonth() + 1).padStart(2, '0');
const dd = String(today.getDate()).padStart(2, '0');
dateInput.value = `${yyyy}-${mm}-${dd}`;
}
});
// ฟังก์ชันบันทึกรายการลง Firestore
function saveTransaction(event) {
event.preventDefault();
const type = document.getElementById('type').value;
const name = document.getElementById('name').value.trim();
const amount = parseFloat(document.getElementById('amount').value);
const date = document.getElementById('date').value;
if (!type || !name || !amount || amount <= 0 || !date) {
alert("กรุณากรอกข้อมูลให้ครบถ้วน");
return;
}
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
const transaction = {
type, name, amount, date,
createdAt: firebase.firestore.FieldValue.serverTimestamp()
};
db.collection("users").doc(user.uid).collection("transactions").add(transaction)
.then(() => {
alert("บันทึกรายการสำเร็จ!");
window.location.href = "dashboard.html";
})
.catch(err => {
alert("เกิดข้อผิดพลาด: " + err.message);
});
} else {
alert("กรุณาเข้าสู่ระบบใหม่");
window.location.href = "login.html";
}
});
}