Cart

Your cart is currently empty.

Return to shop

(function () {
function formatPrice(amount, minorUnit, prefix, suffix) {
return (prefix || ”) + (amount / Math.pow(10, minorUnit)).toFixed(minorUnit) + (suffix || ”);
}

function fetchLiveCart() {
return fetch(‘/wp-json/wc/store/v1/cart’, { credentials: ‘same-origin’, cache: ‘no-store’ })
.then(function (res) { return res.json(); });
}

document.getElementById(‘downloadQuotationBtn’).addEventListener(‘click’, function () {
fetchLiveCart().then(generatePdf).catch(function (err) {
alert(‘Gagal ambil data cart. Cuba refresh page dan try lagi.’);
console.error(err);
});
});

function generatePdf(data) {
if (!data.items || data.items.length === 0) {
alert(‘Cart kosong, takde apa nak di-quote.’);
return;
}

var jsPDF = window.jspdf.jsPDF;
var doc = new jsPDF();
var curr = data.totals.currency_minor_unit;
var prefix = data.totals.currency_prefix;
var suffix = data.totals.currency_suffix;
var quotationNo = ‘Q-‘ + Date.now().toString().slice(-8);
var today = new Date().toLocaleDateString(‘en-GB’);

doc.setFontSize(16);
doc.text(‘TAY SCIENTIFIC INSTRUMENTS SDN BHD’, 14, 18);
doc.setFontSize(11);
doc.text(‘Quotation’, 14, 26);
doc.setFontSize(9);
doc.text(‘No: ‘ + quotationNo, 14, 32);
doc.text(‘Date: ‘ + today, 14, 37);

var y = 48;
doc.setFontSize(10);
doc.setFont(undefined, ‘bold’);
doc.text(‘Product’, 14, y);
doc.text(‘Qty’, 130, y);
doc.text(‘Price’, 150, y);
doc.text(‘Subtotal’, 175, y);
doc.setFont(undefined, ‘normal’);
y += 3;
doc.line(14, y, 196, y);
y += 7;

data.items.forEach(function (item) {
var price = formatPrice(item.prices.price, curr, prefix, suffix);
var subtotal = formatPrice(item.totals.line_subtotal, curr, prefix, suffix);
var nameLines = doc.splitTextToSize(item.name, 110);
doc.text(nameLines, 14, y);
doc.text(String(item.quantity), 130, y);
doc.text(price, 150, y);
doc.text(subtotal, 175, y);
y += (nameLines.length * 5) + 4;
});

y += 4;
doc.line(14, y, 196, y);
y += 8;
doc.setFont(undefined, ‘bold’);
doc.text(‘Total: ‘ + formatPrice(data.totals.total_price, curr, prefix, suffix), 150, y);

y += 20;
doc.setFontSize(8);
doc.setFont(undefined, ‘normal’);
doc.text(‘This quotation is valid for 14 days. Prices are subject to change without notice.’, 14, y);

doc.save(‘Quotation-‘ + quotationNo + ‘.pdf’);
}
})();