pumppal-pos-pro / script.js
ConcernedStudent's picture
Add user authentication with PIN-based login for cashiers, including role-based access control (Cashier, Supervisor, Manager) with restricted actions for refunds and voids. Change the currency to ZAR. Add a reporting dashboard with daily sales charts, fuel vs store breakdown, payment method analytics, and top-selling products visualization.
49cd667 verified
Raw History Blame Contribute Delete
5.9 kB
// User roles and authentication
const users = {
cashier: {
pin: '1234',
name: 'Cashier',
canVoid: false,
canRefund: false,
canAccessReports: false
},
supervisor: {
pin: '5678',
name: 'Supervisor',
canVoid: true,
canRefund: false,
canAccessReports: true
},
manager: {
pin: '9012',
name: 'Manager',
canVoid: true,
canRefund: true,
canAccessReports: true,
canManageInventory: true
}
};
let currentUser = null;
let cart = [];
document.addEventListener('DOMContentLoaded', function() {
// Check if user is logged in
if (!currentUser && !window.location.pathname.endsWith('login.html')) {
window.location.href = 'login.html';
return;
}
// Initialize cart
// Sample product data
const products = [
{ id: 1, name: "Premium Gas", price: 3.99, category: "Fuel", stock: 1000, barcode: "123456789" },
{ id: 2, name: "Energy Drink", price: 2.49, category: "Drinks", stock: 50, barcode: "987654321" },
{ id: 3, name: "Chips", price: 1.99, category: "Snacks", stock: 30, barcode: "456123789" },
{ id: 4, name: "Cigarettes", price: 8.99, category: "Tobacco", stock: 20, barcode: "789123456" },
{ id: 5, name: "Car Wash", price: 9.99, category: "Services", stock: 999, barcode: "321654987" }
];
// Add to cart functionality
document.addEventListener('add-to-cart', function(e) {
const product = e.detail;
const existingItem = cart.find(item => item.id === product.id);
if (existingItem) {
existingItem.quantity += 1;
} else {
cart.push({
...product,
quantity: 1
// Handle login
if (window.location.pathname.endsWith('login.html')) {
const pinInput = document.getElementById('pin-input');
const roleSelect = document.getElementById('role-select');
const loginBtn = document.getElementById('login-btn');
const clearBtn = document.getElementById('clear-pin');
const backspaceBtn = document.getElementById('backspace');
const errorMsg = document.getElementById('error-message');
// PIN pad functionality
document.querySelectorAll('.pin-btn').forEach(btn => {
btn.addEventListener('click', () => {
if (pinInput.value.length < 4) {
pinInput.value += btn.textContent;
}
});
});
clearBtn.addEventListener('click', () => {
pinInput.value = '';
});
backspaceBtn.addEventListener('click', () => {
pinInput.value = pinInput.value.slice(0, -1);
});
loginBtn.addEventListener('click', () => {
const role = roleSelect.value;
const pin = pinInput.value;
if (users[role] && users[role].pin === pin) {
currentUser = {
role: role,
name: users[role].name
};
// Update UI based on role
if (role === 'cashier') {
const voidBtn = document.getElementById('void-btn');
if (voidBtn) voidBtn.disabled = true;
}
// Enable void button if user has permission
if (users[role].canVoid) {
const voidBtn = document.getElementById('void-btn');
if (voidBtn) voidBtn.disabled = false;
}
// Redirect to POS
window.location.href = 'index.html';
} else {
errorMsg.textContent = 'Invalid PIN for selected role';
errorMsg.classList.remove('hidden');
pinInput.value = '';
}
});
}
// Handle reports access
if (window.location.pathname.endsWith('reports.html') && currentUser && !users[currentUser.role].canAccessReports) {
window.location.href = 'index.html';
}
});
}
updateCartDisplay();
});
// Update cart display
function updateCartDisplay() {
const cartContainer = document.getElementById('cart-items');
cartContainer.innerHTML = '';
let subtotal = 0;
cart.forEach(item => {
const cartItem = document.createElement('custom-cart-item');
cartItem.setAttribute('name', item.name);
cartItem.setAttribute('price', item.price);
cartItem.setAttribute('quantity', item.quantity);
cartContainer.appendChild(cartItem);
subtotal += item.price * item.quantity;
});
const tax = subtotal * 0.15; // 15% VAT for South Africa
const total = subtotal + tax;
// Update totals
document.querySelectorAll('.flex.justify-between')[0].lastElementChild.textContent = `R${subtotal.toFixed(2)}`;
document.querySelectorAll('.flex.justify-between')[1].lastElementChild.textContent = `R${tax.toFixed(2)}`;
document.querySelectorAll('.flex.justify-between')[2].lastElementChild.textContent = `R${total.toFixed(2)}`;
}
// Search functionality
const searchInput = document.querySelector('input[type="text"]');
searchInput.addEventListener('input', function() {
const searchTerm = this.value.toLowerCase();
const productCards = document.querySelectorAll('custom-product-card');
productCards.forEach(card => {
const name = card.getAttribute('name').toLowerCase();
if (name.includes(searchTerm)) {
card.style.display = 'block';
} else {
card.style.display = 'none';
}
});
});
});