Update index.html

Ryan Danenberg committed commit4325c6f2f3540d8c1423d7e72841c44aef3be7ce Browse files
1 file changed+51−0
index.html+51−0View file
@@ -2363,6 +2363,57 @@
// -------------------------------------------------------------
// Invoice Attachment Handling
// -------------------------------------------------------------
+ let currentPreviewFile = null;
+ let currentPreviewBlobUrl = null;
+
+ function dataUrlToBlob(dataUrl, fallbackMime) {
+ if (!dataUrl || typeof dataUrl !== 'string') return null;
+ try {
+ const commaIdx = dataUrl.indexOf(',');
+ if (commaIdx === -1) return null;
+ const semiIdx = dataUrl.indexOf(';');
+ const mime = (semiIdx > 5 && semiIdx < commaIdx) ? dataUrl.substring(5, semiIdx) : (fallbackMime || 'application/pdf');
+ const bstr = atob(dataUrl.substring(commaIdx + 1));
+ let n = bstr.length;
+ const u8arr = new Uint8Array(n);
+ while (n--) {
+ u8arr[n] = bstr.charCodeAt(n);
+ }
+ return new Blob([u8arr], { type: mime });
+ } catch (err) {
+ return null;
+ }
+ }
+
+ function downloadInvoiceFile(fileObj) {
+ if (!fileObj) return;
+ const filename = fileObj.name || 'invoice.pdf';
+ const mime = fileObj.type || (filename.toLowerCase().endsWith('.pdf') ? 'application/pdf' : 'application/octet-stream');
+ let blob = fileObj.blob;
+ if (!blob && fileObj.data) {
+ blob = dataUrlToBlob(fileObj.data, mime);
+ }
+ if (blob) {
+ const url = URL.createObjectURL(blob);
+ const a = document.createElement('a');
+ a.href = url;
+ a.download = filename;
+ document.body.appendChild(a);
+ a.click();
+ setTimeout(function() {
+ document.body.removeChild(a);
+ URL.revokeObjectURL(url);
+ }, 2000);
+ } else if (fileObj.data) {
+ const a = document.createElement('a');
+ a.href = fileObj.data;
+ a.download = filename;
+ document.body.appendChild(a);
+ a.click();
+ setTimeout(function() { document.body.removeChild(a); }, 1000);
+ }
+ }
+
function handleInvoiceFileUpload(event) {
const file = event.target.files[0];
if (!file) return;