Changed around line 1
+ // Sample inventory data
+ const inventory = [
+ { name: "Green Tea", quantity: 50, expiry: "2023-12-01" },
+ { name: "Black Tea", quantity: 30, expiry: "2023-11-15" },
+ { name: "Herbal Tea", quantity: 20, expiry: "2023-10-30" },
+ { name: "Oolong Tea", quantity: 10, expiry: "2023-11-20" }
+ ];
+
+ // Calculate inventory metrics
+ function calculateMetrics() {
+ const totalStock = inventory.reduce((sum, item) => sum + item.quantity, 0);
+ const expiringSoon = inventory.filter(item => {
+ const expiryDate = new Date(item.expiry);
+ const today = new Date();
+ const diffTime = expiryDate - today;
+ const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
+ return diffDays <= 30;
+ }).length;
+
+ const lowStock = inventory.filter(item => item.quantity <= 15).length;
+
+ document.getElementById('total-stock').textContent = totalStock;
+ document.getElementById('expiring-soon').textContent = expiringSoon;
+ document.getElementById('low-stock').textContent = lowStock;
+ }
+
+ // Populate inventory table
+ function populateTable() {
+ const tbody = document.getElementById('inventory-table');
+ tbody.innerHTML = inventory.map(item => `
+
+
${item.name} | +
${item.quantity} | +
${item.expiry} | +
${getStatus(item)} | +
+ `).join('');
+ }
+
+ // Get item status
+ function getStatus(item) {
+ const expiryDate = new Date(item.expiry);
+ const today = new Date();
+ const diffTime = expiryDate - today;
+ const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
+
+ if (diffDays <= 0) return 'Expired';
+ if (diffDays <= 30) return 'Expiring Soon';
+ if (item.quantity <= 15) return 'Low Stock';
+ return 'Good';
+ }
+
+ // Initialize expiry forecast chart
+ function initChart() {
+ const ctx = document.getElementById('expiryChart').getContext('2d');
+ const expiryData = inventory.map(item => {
+ const expiryDate = new Date(item.expiry);
+ const today = new Date();
+ return Math.ceil((expiryDate - today) / (1000 * 60 * 60 * 24));
+ });
+
+ new Chart(ctx, {
+ type: 'line',
+ data: {
+ labels: inventory.map(item => item.name),
+ datasets: [{
+ label: 'Days Until Expiry',
+ data: expiryData,
+ backgroundColor: 'rgba(76, 175, 80, 0.2)',
+ borderColor: 'rgba(76, 175, 80, 1)',
+ borderWidth: 2
+ }]
+ },
+ options: {
+ scales: {
+ y: {
+ beginAtZero: true,
+ title: {
+ display: true,
+ text: 'Days Remaining'
+ }
+ }
+ }
+ }
+ });
+ }
+
+ // Initialize everything
+ function init() {
+ calculateMetrics();
+ populateTable();
+ initChart();
+ }
+
+ // Run initialization when DOM is loaded
+ document.addEventListener('DOMContentLoaded', init);