File size: 5,901 Bytes
008dde1 49cd667 008dde1 49cd667 008dde1 49cd667 008dde1 | 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 |
// 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';
}
});
});
}); |