V.AISTUDIO / export-functions.js
bep40's picture
Upload export-functions.js
6ef1b39 verified
Raw
History Blame
5.31 kB
// Export functions for quote system
// These functions are required by quote-ui.js
// --- Delivery Export Functions ---
function exportDeliveryExcel() {
console.log('[V.AI STUDIO] Exporting delivery to Excel...');
if (!window.cartItems || !window.cartItems.length) {
alert('❌ Giỏ hàng trống!');
return;
}
// Create CSV content
let csv = 'STT,Tên sản phẩm,Mã SP,Số lượng,Đơn giá,Thành tiền,Ghi chú\n';
let total = 0;
window.cartItems.forEach((item, idx) => {
let price = item.priceNum || 0;
let qty = item.qty || 1;
let subtotal = price * qty;
total += subtotal;
csv += `${idx + 1},"${item.name}","${item.sku || ''}",${qty},${price},${subtotal},"${item.note || ''}"\n`;
});
csv += `,,,,,,"${total}",Tổng cộng\n`;
// Download
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `giao-hang-${new Date().toISOString().slice(0,10)}.csv`;
a.click();
URL.revokeObjectURL(url);
alert('✅ Đã xuất file Excel (CSV) thành công!');
}
function exportDeliveryPDF() {
console.log('[V.AI STUDIO] Exporting delivery to PDF...');
if (!window.cartItems || !window.cartItems.length) {
alert('❌ Giỏ hàng trống!');
return;
}
// Simple PDF export using jsPDF if available
if (typeof jsPDF !== 'undefined') {
const doc = new jsPDF();
doc.setFontSize(16);
doc.text('PHIẾU GIAO HÀNG', 14, 20);
doc.setFontSize(10);
doc.text(`Ngày: ${new Date().toLocaleDateString('vi-VN')}`, 14, 30);
doc.text(`Tổng số: ${window.cartItems.length} sản phẩm`, 14, 36);
let y = 50;
window.cartItems.forEach((item, idx) => {
if (y > 280) { doc.addPage(); y = 20; }
doc.text(`${idx + 1}. ${item.name}`, 14, y);
y += 7;
});
doc.save(`giao-hang-${new Date().toISOString().slice(0,10)}.pdf`);
alert('✅ Đã xuất file PDF thành công!');
} else {
alert('⚠️ Thư viện jsPDF chưa được tải. Vui lòng thử xuất Excel.');
}
}
// --- Quote Export Functions ---
function exportExcel() {
console.log('[V.AI STUDIO] Exporting quote to Excel...');
const table = document.getElementById('quoteTableBody');
if (!table || !table.rows.length) {
alert('❌ Báo giá trống!');
return;
}
let csv = 'STT,Hình ảnh,Tên sản phẩm,Mã SP,Thông tin,SL,Đơn giá,Đơn giá CK,Thành tiền,Ghi chú\n';
// Implementation here...
alert('✅ Đã xuất báo giá Excel (đang triển khai...)');
}
function exportPDF() {
console.log('[V.AI STUDIO] Exporting quote to PDF...');
alert('✅ Đã xuất báo giá PDF (đang triển khai...)');
}
function printQuotation() {
console.log('[V.AI STUDIO] Printing quotation...');
window.print();
}
function shareQuoteImage() {
console.log('[V.AI STUDIO] Sharing quote as image...');
alert('✅ Chia sẻ ảnh báo giá (đang triển khai...)');
}
function shareQuoteLink() {
console.log('[V.AI STUDIO] Sharing quote link...');
const url = window.location.href;
if (navigator.share) {
navigator.share({ title: 'Báo giá V.AI STUDIO', url: url });
} else {
prompt('Copy link báo giá:', url);
}
}
// --- VAI_ORDERS Module ---
window.VAI_ORDERS = window.VAI_ORDERS || {
save: function() {
console.log('[V.AI STUDIO] Saving order...');
const orderData = {
date: new Date().toISOString(),
customer: {
name: document.getElementById('qcName')?.value || '',
phone: document.getElementById('qcPhone')?.value || '',
email: document.getElementById('qcEmail')?.value || '',
address: document.getElementById('qcAddr')?.value || ''
},
items: window.cartItems || [],
total: window.cartTotal || 0
};
// Save to localStorage
let orders = JSON.parse(localStorage.getItem('VAI_ORDERS') || '[]');
orders.push(orderData);
localStorage.setItem('VAI_ORDERS', JSON.stringify(orders));
alert('✅ Đã lưu đơn hàng thành công!');
console.log('[V.AI STUDIO] Order saved:', orderData);
},
openSearch: function() {
console.log('[V.AI STUDIO] Opening order search...');
const orders = JSON.parse(localStorage.getItem('VAI_ORDERS') || '[]');
if (!orders.length) {
alert('📋 Chưa có đơn hàng nào được lưu.');
return;
}
let msg = `📋 Tìm thấy ${orders.length} đơn hàng:\n\n`;
orders.slice(-5).forEach((order, idx) => {
msg += `${idx + 1}. ${order.customer.name || 'Không tên'} - ${order.customer.phone || ''}\n`;
});
alert(msg);
}
};
// --- Initialize cart items if not exists ---
window.cartItems = window.cartItems || [];
window.cartTotal = window.cartTotal || 0;
console.log('[V.AI STUDIO] Export functions loaded successfully!');