diff --git a/site/public/order/dot-compliance/index.html b/site/public/order/dot-compliance/index.html index 07edec9..54d4b98 100644 --- a/site/public/order/dot-compliance/index.html +++ b/site/public/order/dot-compliance/index.html @@ -442,25 +442,57 @@ var checkboxes = document.querySelectorAll("input[data-slug]"); var totalSection = document.getElementById("pw-total-section"); var totalRows = document.getElementById("pw-total-rows"); +// Non-discountable slugs (passthrough costs) +var nonDiscountable = {"dot-drug-alcohol": true}; + +// Cached discount info +var activeDiscount = null; +function fetchDiscount() { + var code = (document.getElementById("pw-promo").value || "").trim().toUpperCase(); + if (!code) { activeDiscount = null; updateTotal(); return; } + fetch(API + "/api/v1/discount/" + encodeURIComponent(code)).then(function(r) { return r.json(); }).then(function(d) { + if (d.valid) { activeDiscount = d; } else { activeDiscount = null; } + updateTotal(); + }).catch(function() { activeDiscount = null; updateTotal(); }); +} +// Fetch on load if promo pre-filled +if (promoFromUrl) { setTimeout(fetchDiscount, 500); } +// Fetch on blur of promo field +document.getElementById("pw-promo").addEventListener("blur", fetchDiscount); + function updateTotal() { var selected = []; var svcTotal = 0; var govTotal = 0; + var discountable = 0; checkboxes.forEach(function(cb) { if (cb.checked) { var price = parseInt(cb.dataset.price) || 0; var gov = parseInt(cb.dataset.govfee) || 0; svcTotal += price; govTotal += gov; + if (!nonDiscountable[cb.dataset.slug]) { discountable += price; } selected.push(cb.dataset.slug); } }); if (selected.length === 0) { totalSection.hidden = true; return; } totalSection.hidden = false; + var discountAmt = 0; + if (activeDiscount && activeDiscount.discount_type === "percent") { + discountAmt = Math.round(discountable * activeDiscount.discount_value / 100); + } else if (activeDiscount && activeDiscount.discount_type === "flat") { + discountAmt = Math.min(activeDiscount.discount_value * 100, discountable); + } + var html = '