Spaces:
Running
Running
File size: 12,975 Bytes
990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 467e23a 0d7100b 467e23a 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 7e4200b 0d7100b 7e4200b 0d7100b 7e4200b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a 0d7100b 990bb6a | 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | // 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');
});
} |