/** * Document Generator for Easy Invoice * Handles PDF and HTML invoice generation */ (function($) { 'use strict'; // Document Generator object window.EasyInvoiceDocument = { // Default settings settings: { invoiceId: 0, ajaxUrl: '', nonce: '', format: 'pdf' // pdf or html }, // Initialize the document generator init: function() { // Load settings if available from localized data if (typeof easyInvoiceDocument !== 'undefined') { this.settings.invoiceId = easyInvoiceDocument.invoiceId || 0; this.settings.ajaxUrl = easyInvoiceDocument.ajaxUrl || ''; this.settings.nonce = easyInvoiceDocument.nonce || ''; this.settings.format = easyInvoiceDocument.format || 'pdf'; } // Set up event handlers this.setupEventHandlers(); }, // Set up event handlers for document-related elements setupEventHandlers: function() { var self = this; // Generate PDF button $('#generate_pdf').on('click', function(e) { e.preventDefault(); self.generateDocument('pdf'); }); // Generate HTML button $('#generate_html').on('click', function(e) { e.preventDefault(); self.generateDocument('html'); }); // Send invoice button $('#send_invoice').on('click', function(e) { e.preventDefault(); self.sendInvoice(); }); // Preview invoice button $('#preview_invoice').on('click', function(e) { e.preventDefault(); self.previewInvoice(); }); }, // Generate document (PDF or HTML) generateDocument: function(format) { var self = this; var invoiceId = this.settings.invoiceId; // If no invoice ID, try to get it from the form if (!invoiceId) { invoiceId = $('#invoice_id').val(); } // If still no invoice ID, show error if (!invoiceId) { this.showMessage('Please save the invoice before generating a document.', 'error'); return; } // Show loading message this.showMessage('Generating ' + format.toUpperCase() + ' document...', 'info'); // Make AJAX request to generate document $.ajax({ url: this.settings.ajaxUrl, type: 'POST', data: { action: 'easy_invoice_generate_document', invoice_id: invoiceId, format: format, nonce: this.settings.nonce }, success: function(response) { if (response.success) { // Show success message self.showMessage(response.data.message, 'success'); // If download URL is provided, open it in a new tab if (response.data.download_url) { window.open(response.data.download_url, '_blank'); } } else { // Show error message self.showMessage(response.data.message || 'An error occurred while generating the document.', 'error'); } }, error: function() { // Show error message self.showMessage('An error occurred while generating the document.', 'error'); } }); }, // Send invoice to client via email sendInvoice: function() { var self = this; var invoiceId = this.settings.invoiceId; // If no invoice ID, try to get it from the form if (!invoiceId) { invoiceId = $('#invoice_id').val(); } // If still no invoice ID, show error if (!invoiceId) { this.showMessage('Please save the invoice before sending it.', 'error'); return; } // Show confirmation dialog if (!confirm('Are you sure you want to send this invoice to the client?')) { return; } // Show loading message this.showMessage('Sending invoice...', 'info'); // Make AJAX request to send invoice $.ajax({ url: this.settings.ajaxUrl, type: 'POST', data: { action: 'easy_invoice_send_invoice', invoice_id: invoiceId, nonce: this.settings.nonce }, success: function(response) { if (response.success) { // Show success message self.showMessage(response.data.message, 'success'); } else { // Show error message self.showMessage(response.data.message || 'An error occurred while sending the invoice.', 'error'); } }, error: function() { // Show error message self.showMessage('An error occurred while sending the invoice.', 'error'); } }); }, // Preview invoice in a modal previewInvoice: function() { var self = this; var invoiceId = this.settings.invoiceId; // If no invoice ID, try to get it from the form if (!invoiceId) { invoiceId = $('#invoice_id').val(); } // If still no invoice ID, show error if (!invoiceId) { this.showMessage('Please save the invoice before previewing it.', 'error'); return; } // Show loading message this.showMessage('Loading preview...', 'info'); // Make AJAX request to get invoice preview $.ajax({ url: this.settings.ajaxUrl, type: 'POST', data: { action: 'easy_invoice_preview_invoice', invoice_id: invoiceId, nonce: this.settings.nonce }, success: function(response) { if (response.success) { // Show preview in modal self.showPreviewModal(response.data.html); // Clear the info message self.clearMessage(); } else { // Show error message self.showMessage(response.data.message || 'An error occurred while loading the preview.', 'error'); } }, error: function() { // Show error message self.showMessage('An error occurred while loading the preview.', 'error'); } }); }, // Show preview modal showPreviewModal: function(html) { // Create modal if it doesn't exist if ($('#invoice-preview-modal').length === 0) { $('body').append( '