snackmaster-delight / script.js
ama0192's picture
انزع كلمة المرور
467e23a verified
Raw History Blame Contribute Delete
13 kB
// Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyDummyKeyReplaceWithYours",
authDomain: "snackmaster-pro.firebaseapp.com",
projectId: "snackmaster-pro",
storageBucket: "snackmaster-pro.appspot.com",
messagingSenderId: "1234567890",
appId: "1:1234567890:web:abcdef1234567890"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
const auth = firebase.auth();
// DOM Elements
const loginSection = document.getElementById('login-section');
const appContent = document.getElementById('app-content');
const loginForm = document.getElementById('login-form');
const salesSection = document.getElementById('sales-section');
const expensesSection = document.getElementById('expenses-section');
const recordsSection = document.getElementById('records-section');
const salesTab = document.getElementById('sales-tab');
const expensesTab = document.getElementById('expenses-tab');
const recordsTab = document.getElementById('records-tab');
const ordersList = document.getElementById('orders-list');
const expensesList = document.getElementById('expenses-list');
const recordsList = document.getElementById('records-list');
const totalSales = document.getElementById('total-sales');
const totalExpenses = document.getElementById('total-expenses');
const dailySales = document.getElementById('daily-sales');
const dailyExpenses = document.getElementById('daily-expenses');
const dailyProfit = document.getElementById('daily-profit');
const expenseForm = document.getElementById('expense-form');
const modal = document.getElementById('confirmation-modal');
const modalTitle = document.getElementById('modal-title');
const modalMessage = document.getElementById('modal-message');
const modalCancel = document.getElementById('modal-cancel');
const modalConfirm = document.getElementById('modal-confirm');
// Global variables
let currentUser = null;
let today = new Date().toISOString().split('T')[0];
let currentAction = null;
let currentItemId = null;
// Initialize the app
document.addEventListener('DOMContentLoaded', () => {
// Auto-login as guest
auth.signInAnonymously()
.then(() => {
currentUser = auth.currentUser;
appContent.classList.remove('hidden');
loadTodayData();
})
.catch(error => {
showNotification('خطأ في بدء التشغيل: ' + error.message, 'error');
});
});
// Sales buttons
document.querySelectorAll('[data-item]').forEach(button => {
button.addEventListener('click', () => {
const item = button.getAttribute('data-item');
const price = parseInt(button.getAttribute('data-price'));
addOrder(item, price);
});
});
// Expense form submission
expenseForm.addEventListener('submit', e => {
e.preventDefault();
const name = document.getElementById('expense-name').value;
const amount = parseInt(document.getElementById('expense-amount').value);
if (name && amount) {
addExpense(name, amount);
expenseForm.reset();
}
});
// Tab navigation
salesTab.addEventListener('click', () => showTab('sales'));
expensesTab.addEventListener('click', () => showTab('expenses'));
recordsTab.addEventListener('click', () => showTab('records'));
// Modal buttons
modalCancel.addEventListener('click', () => {
modal.classList.add('hidden');
currentAction = null;
currentItemId = null;
});
modalConfirm.addEventListener('click', () => {
if (currentAction === 'delete-order') {
deleteOrder(currentItemId);
} else if (currentAction === 'delete-expense') {
deleteExpense(currentItemId);
}
modal.classList.add('hidden');
});
});
// Functions
function showTab(tab) {
salesSection.classList.add('hidden');
expensesSection.classList.add('hidden');
recordsSection.classList.add('hidden');
salesTab.classList.remove('bg-indigo-100', 'text-indigo-700');
expensesTab.classList.remove('bg-indigo-100', 'text-indigo-700');
recordsTab.classList.remove('bg-indigo-100', 'text-indigo-700');
if (tab === 'sales') {
salesSection.classList.remove('hidden');
salesTab.classList.add('bg-indigo-100', 'text-indigo-700');
} else if (tab === 'expenses') {
expensesSection.classList.remove('hidden');
expensesTab.classList.add('bg-indigo-100', 'text-indigo-700');
} else if (tab === 'records') {
recordsSection.classList.remove('hidden');
recordsTab.classList.add('bg-indigo-100', 'text-indigo-700');
}
}
function addOrder(item, price) {
const order = {
item,
price,
date: new Date(),
user: currentUser.uid,
userName: currentUser.displayName || currentUser.email
};
db.collection('orders').add(order)
.then(() => {
showNotification('تم إضافة الطلب بنجاح', 'success');
})
.catch(error => {
showNotification('خطأ في إضافة الطلب: ' + error.message, 'error');
});
}
function addExpense(name, amount) {
const expense = {
name,
amount,
date: new Date(),
user: currentUser.uid,
userName: currentUser.displayName || currentUser.email
};
db.collection('expenses').add(expense)
.then(() => {
showNotification('تم إضافة المصروف بنجاح', 'success');
})
.catch(error => {
showNotification('خطأ في إضافة المصروف: ' + error.message, 'error');
});
}
function deleteOrder(id) {
db.collection('orders').doc(id).delete()
.then(() => {
showNotification('تم حذف الطلب بنجاح', 'success');
})
.catch(error => {
showNotification('خطأ في حذف الطلب: ' + error.message, 'error');
});
}
function deleteExpense(id) {
db.collection('expenses').doc(id).delete()
.then(() => {
showNotification('تم حذف المصروف بنجاح', 'success');
})
.catch(error => {
showNotification('خطأ في حذف المصروف: ' + error.message, 'error');
});
}
function loadTodayData() {
const todayStart = new Date(today);
const todayEnd = new Date(today);
todayEnd.setDate(todayEnd.getDate() + 1);
// Load orders
db.collection('orders')
.where('date', '>=', todayStart)
.where('date', '<', todayEnd)
.orderBy('date', 'desc')
.onSnapshot(snapshot => {
let total = 0;
ordersList.innerHTML = '';
snapshot.forEach(doc => {
const order = doc.data();
total += order.price;
const orderElement = document.createElement('div');
orderElement.className = 'py-3 flex justify-between items-center';
orderElement.innerHTML = `
<div>
<span class="font-medium">${order.item}</span>
<span class="text-sm text-gray-500 rtl:ml-2">${formatTime(order.date)} - ${order.userName}</span>
</div>
<div class="flex items-center">
<span class="font-medium">${order.price} دج</span>
<button data-id="${doc.id}" class="text-red-500 hover:text-red-700 rtl:mr-2 ml-2 delete-order">
<i data-feather="trash-2" class="w-4 h-4"></i>
</button>
</div>
`;
ordersList.appendChild(orderElement);
// Add event listener to delete button
orderElement.querySelector('.delete-order').addEventListener('click', (e) => {
e.stopPropagation();
currentAction = 'delete-order';
currentItemId = doc.id;
modalTitle.textContent = 'تأكيد حذف الطلب';
modalMessage.textContent = 'هل أنت متأكد أنك تريد حذف هذا الطلب؟';
modal.classList.remove('hidden');
});
});
totalSales.textContent = `${total} دج`;
dailySales.textContent = `${total} دج`;
calculateProfit();
feather.replace();
});
// Load expenses
db.collection('expenses')
.where('date', '>=', todayStart)
.where('date', '<', todayEnd)
.orderBy('date', 'desc')
.onSnapshot(snapshot => {
let total = 0;
expensesList.innerHTML = '';
snapshot.forEach(doc => {
const expense = doc.data();
total += expense.amount;
const expenseElement = document.createElement('div');
expenseElement.className = 'py-3 flex justify-between items-center';
expenseElement.innerHTML = `
<div>
<span class="font-medium">${expense.name}</span>
<span class="text-sm text-gray-500 rtl:ml-2">${formatTime(expense.date)} - ${expense.userName}</span>
</div>
<div class="flex items-center">
<span class="font-medium">${expense.amount} دج</span>
<button data-id="${doc.id}" class="text-red-500 hover:text-red-700 rtl:mr-2 ml-2 delete-expense">
<i data-feather="trash-2" class="w-4 h-4"></i>
</button>
</div>
`;
expensesList.appendChild(expenseElement);
// Add event listener to delete button
expenseElement.querySelector('.delete-expense').addEventListener('click', (e) => {
e.stopPropagation();
currentAction = 'delete-expense';
currentItemId = doc.id;
modalTitle.textContent = 'تأكيد حذف المصروف';
modalMessage.textContent = 'هل أنت متأكد أنك تريد حذف هذا المصروف؟';
modal.classList.remove('hidden');
});
});
totalExpenses.textContent = `${total} دج`;
dailyExpenses.textContent = `${total} دج`;
calculateProfit();
feather.replace();
});
// Load records
db.collection('records')
.where('date', '>=', todayStart)
.where('date', '<', todayEnd)
.orderBy('date', 'desc')
.onSnapshot(snapshot => {
recordsList.innerHTML = '';
snapshot.forEach(doc => {
const record = doc.data();
const recordElement = document.createElement('div');
recordElement.className = 'py-3';
recordElement.innerHTML = `
<div class="flex justify-between">
<span>${record.action} - ${record.userName}</span>
<span class="text-sm text-gray-500">${formatTime(record.date)}</span>
</div>
<div class="text-sm text-gray-600">${record.details}</div>
`;
recordsList.appendChild(recordElement);
});
});
}
function calculateProfit() {
const sales = parseInt(dailySales.textContent) || 0;
const expenses = parseInt(dailyExpenses.textContent) || 0;
const profit = sales - expenses;
dailyProfit.textContent = `${profit} دج`;
}
function formatTime(date) {
const d = date.toDate ? date.toDate() : new Date(date);
return d.toLocaleTimeString('ar-EG', { hour: '2-digit', minute: '2-digit' });
}
function showNotification(message, type) {
const notification = document.createElement('div');
notification.className = `fixed bottom-4 right-4 p-4 rounded-lg shadow-lg text-white notification ${
type === 'success' ? 'bg-green-500' : 'bg-red-500'
}`;
notification.textContent = message;
document.body.appendChild(notification);
setTimeout(() => {
notification.style.opacity = '0';
setTimeout(() => {
notification.remove();
}, 300);
}, 3000);
}
// Logout function (can be called from navbar)
function logout() {
auth.signOut()
.then(() => {
showNotification('تم تسجيل الخروج بنجاح', 'success');
})
.catch(error => {
showNotification('خطأ في تسجيل الخروج: ' + error.message, 'error');
});
}