Shipping & Handling Cost Optimizer

Shipping & Handling Cost Optimizer

Find the most cost-effective shipping option for your packages.

${formatCurrency(savings)}

Carrier Cost Comparison

`; } dashboardContent.innerHTML = dashboardHtml; if (bestOption) { const chartCtx = document.getElementById('costComparisonChart').getContext('2d'); if (charts.cost) charts.cost.destroy(); charts.cost = new Chart(chartCtx, { type: 'bar', data: { labels: allOptions.map(opt => opt.name), datasets: [ { label: 'Shipping Cost', data: allOptions.map(opt => opt.shippingCost), backgroundColor: '#2dd4bf' }, { label: 'Handling Cost', data: allOptions.map(opt => opt.handlingCost), backgroundColor: '#99f6e4' } ] }, options: { responsive: true, maintainAspectRatio: false, scales: { x: { stacked: true }, y: { stacked: true, beginAtZero: true, ticks: { callback: value => formatCurrency(value) } } }, plugins: { tooltip: { callbacks: { label: c => `${c.dataset.label}: ${formatCurrency(c.raw)}` } } } } }); document.getElementById('download-pdf-btn').addEventListener('click', generatePdf); } }; const renderDataConfig = () => { const configContent = document.getElementById('content-data-config'); if (!configContent) return; const carriersHtml = appData.carriers.map((carrier, index) => `

Rate per Pound ($) per Zone

${carrier.ratePerPoundPerZone.slice(1).map((rate, zoneIndex) => `
Zone ${zoneIndex + 1}
`).join('')}
`).join(''); configContent.innerHTML = `
Package Details
Handling Costs
Carrier Rates
${carriersHtml}
`; attachConfigListeners(); }; const attachConfigListeners = () => { document.getElementById('add-carrier-btn').addEventListener('click', () => { appData.carriers.push({ name: 'New Carrier', ratePerPoundPerZone: [0,0,0,0,0,0,0,0,0] }); renderDataConfig(); lucide.createIcons(); }); document.getElementById('carriers-container').addEventListener('click', e => { if (e.target.closest('.remove-carrier-btn')) { appData.carriers.splice(parseInt(e.target.closest('.remove-carrier-btn').dataset.index), 1); renderDataConfig(); lucide.createIcons(); } }); document.getElementById('update-data-btn').addEventListener('click', handleConfigUpdate); }; const handleConfigUpdate = () => { appData.package.weight = parseFloat(document.getElementById('pkg-weight').value) || 0; appData.package.length = parseFloat(document.getElementById('pkg-length').value) || 0; appData.package.width = parseFloat(document.getElementById('pkg-width').value) || 0; appData.package.height = parseFloat(document.getElementById('pkg-height').value) || 0; appData.package.destinationZone = parseInt(document.getElementById('pkg-zone').value) || 1; appData.handling.costPerOrder = parseFloat(document.getElementById('handling-per-order').value) || 0; appData.handling.costPerPound = parseFloat(document.getElementById('handling-per-pound').value) || 0; appData.carriers = Array.from(document.querySelectorAll('.carrier-row')).map((row, index) => { const name = row.querySelector('[data-field="name"]').value; const rates = [0]; row.querySelectorAll('input[data-zone]').forEach(input => { rates[parseInt(input.dataset.zone)] = parseFloat(input.value) || 0; }); return { name, ratePerPoundPerZone: rates }; }); optimizationResult = null; // Invalidate old result renderDashboard(); alert('Configuration saved & costs re-optimized!'); switchTab(0); }; const generatePdf = () => { if (!optimizationResult) return; loadingOverlay.style.display = 'flex'; const { jsPDF } = window.jspdf; const pdfContent = document.getElementById('pdf-content-area'); const pdfHeader = document.getElementById('pdf-header'); pdfHeader.classList.remove('hidden'); html2canvas(pdfContent, { scale: 2, useCORS: true, logging: false }) .then(canvas => { pdfHeader.classList.add('hidden'); const imgData = canvas.toDataURL('image/jpeg', 0.95); const pdf = new jsPDF({ orientation: 'landscape', unit: 'px', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const imgProps = pdf.getImageProperties(imgData); const imgHeight = (imgProps.height * pdfWidth) / imgProps.width; pdf.addImage(imgData, 'JPEG', 0, 0, pdfWidth, imgHeight); pdf.save('Shipping-Cost-Report.pdf'); loadingOverlay.style.display = 'none'; }).catch(err => { console.error("PDF generation failed:", err); pdfHeader.classList.add('hidden'); loadingOverlay.style.display = 'none'; alert('An error occurred generating the PDF.'); }); }; // --- TAB NAVIGATION & INITIALIZATION --- const switchTab = (tabIndex) => { activeTabIndex = tabIndex; document.querySelectorAll('.tab-btn').forEach((btn, i) => btn.classList.toggle('active', i === tabIndex)); document.querySelectorAll('.tab-content').forEach((content, i) => content.classList.toggle('hidden', i !== tabIndex)); updateNavButtons(); }; const updateNavButtons = () => { prevTabBtn.disabled = activeTabIndex === 0; nextTabBtn.disabled = activeTabIndex === tabIdentifiers.length - 1; }; const initializeUI = () => { const tabs = [ { name: 'Optimization Dashboard', id: 'dashboard' }, { name: 'Cost Configuration', id: 'data-config' } ]; tabIdentifiers = tabs.map(t => t.id); tabsContainer.innerHTML = tabs.map(tab => ``).join(''); mainContent.innerHTML = tabs.map(tab => `
`).join(''); tabs.forEach((tab, index) => { document.getElementById(`tab-${tab.id}`).addEventListener('click', () => switchTab(index)); }); renderDashboard(); renderDataConfig(); switchTab(0); }; initializeUI(); lucide.createIcons(); prevTabBtn.addEventListener('click', () => { if (activeTabIndex > 0) switchTab(activeTabIndex - 1); }); nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); }); });
Scroll to Top