| 1 |
/** |
| 2 |
* Document Generator for Easy Invoice |
| 3 |
* Handles PDF and HTML invoice generation |
| 4 |
*/ |
| 5 |
(function($) { |
| 6 |
'use strict'; |
| 7 |
|
| 8 |
// Document Generator object |
| 9 |
window.EasyInvoiceDocument = { |
| 10 |
// Default settings |
| 11 |
settings: { |
| 12 |
invoiceId: 0, |
| 13 |
ajaxUrl: '', |
| 14 |
nonce: '', |
| 15 |
format: 'pdf' // pdf or html |
| 16 |
}, |
| 17 |
|
| 18 |
// Initialize the document generator |
| 19 |
init: function() { |
| 20 |
|
| 21 |
|
| 22 |
// Load settings if available from localized data |
| 23 |
if (typeof easyInvoiceDocument !== 'undefined') { |
| 24 |
this.settings.invoiceId = easyInvoiceDocument.invoiceId || 0; |
| 25 |
this.settings.ajaxUrl = easyInvoiceDocument.ajaxUrl || ''; |
| 26 |
this.settings.nonce = easyInvoiceDocument.nonce || ''; |
| 27 |
this.settings.format = easyInvoiceDocument.format || 'pdf'; |
| 28 |
} |
| 29 |
|
| 30 |
// Set up event handlers |
| 31 |
this.setupEventHandlers(); |
| 32 |
}, |
| 33 |
|
| 34 |
// Set up event handlers for document-related elements |
| 35 |
setupEventHandlers: function() { |
| 36 |
var self = this; |
| 37 |
|
| 38 |
// Generate PDF button |
| 39 |
$('#generate_pdf').on('click', function(e) { |
| 40 |
e.preventDefault(); |
| 41 |
self.generateDocument('pdf'); |
| 42 |
}); |
| 43 |
|
| 44 |
// Generate HTML button |
| 45 |
$('#generate_html').on('click', function(e) { |
| 46 |
e.preventDefault(); |
| 47 |
self.generateDocument('html'); |
| 48 |
}); |
| 49 |
|
| 50 |
// Send invoice button |
| 51 |
$('#send_invoice').on('click', function(e) { |
| 52 |
e.preventDefault(); |
| 53 |
self.sendInvoice(); |
| 54 |
}); |
| 55 |
|
| 56 |
// Preview invoice button |
| 57 |
$('#preview_invoice').on('click', function(e) { |
| 58 |
e.preventDefault(); |
| 59 |
self.previewInvoice(); |
| 60 |
}); |
| 61 |
}, |
| 62 |
|
| 63 |
// Generate document (PDF or HTML) |
| 64 |
generateDocument: function(format) { |
| 65 |
var self = this; |
| 66 |
var invoiceId = this.settings.invoiceId; |
| 67 |
|
| 68 |
// If no invoice ID, try to get it from the form |
| 69 |
if (!invoiceId) { |
| 70 |
invoiceId = $('#invoice_id').val(); |
| 71 |
} |
| 72 |
|
| 73 |
// If still no invoice ID, show error |
| 74 |
if (!invoiceId) { |
| 75 |
this.showMessage('Please save the invoice before generating a document.', 'error'); |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
// Show loading message |
| 80 |
this.showMessage('Generating ' + format.toUpperCase() + ' document...', 'info'); |
| 81 |
|
| 82 |
// Make AJAX request to generate document |
| 83 |
$.ajax({ |
| 84 |
url: this.settings.ajaxUrl, |
| 85 |
type: 'POST', |
| 86 |
data: { |
| 87 |
action: 'easy_invoice_generate_document', |
| 88 |
invoice_id: invoiceId, |
| 89 |
format: format, |
| 90 |
nonce: this.settings.nonce |
| 91 |
}, |
| 92 |
success: function(response) { |
| 93 |
if (response.success) { |
| 94 |
// Show success message |
| 95 |
self.showMessage(response.data.message, 'success'); |
| 96 |
|
| 97 |
// If download URL is provided, open it in a new tab |
| 98 |
if (response.data.download_url) { |
| 99 |
window.open(response.data.download_url, '_blank'); |
| 100 |
} |
| 101 |
} else { |
| 102 |
// Show error message |
| 103 |
self.showMessage(response.data.message || 'An error occurred while generating the document.', 'error'); |
| 104 |
} |
| 105 |
}, |
| 106 |
error: function() { |
| 107 |
// Show error message |
| 108 |
self.showMessage('An error occurred while generating the document.', 'error'); |
| 109 |
} |
| 110 |
}); |
| 111 |
}, |
| 112 |
|
| 113 |
// Send invoice to client via email |
| 114 |
sendInvoice: function() { |
| 115 |
var self = this; |
| 116 |
var invoiceId = this.settings.invoiceId; |
| 117 |
|
| 118 |
// If no invoice ID, try to get it from the form |
| 119 |
if (!invoiceId) { |
| 120 |
invoiceId = $('#invoice_id').val(); |
| 121 |
} |
| 122 |
|
| 123 |
// If still no invoice ID, show error |
| 124 |
if (!invoiceId) { |
| 125 |
this.showMessage('Please save the invoice before sending it.', 'error'); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
// Show confirmation dialog |
| 130 |
if (!confirm('Are you sure you want to send this invoice to the client?')) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
// Show loading message |
| 135 |
this.showMessage('Sending invoice...', 'info'); |
| 136 |
|
| 137 |
// Make AJAX request to send invoice |
| 138 |
$.ajax({ |
| 139 |
url: this.settings.ajaxUrl, |
| 140 |
type: 'POST', |
| 141 |
data: { |
| 142 |
action: 'easy_invoice_send_invoice', |
| 143 |
invoice_id: invoiceId, |
| 144 |
nonce: this.settings.nonce |
| 145 |
}, |
| 146 |
success: function(response) { |
| 147 |
if (response.success) { |
| 148 |
// Show success message |
| 149 |
self.showMessage(response.data.message, 'success'); |
| 150 |
} else { |
| 151 |
// Show error message |
| 152 |
self.showMessage(response.data.message || 'An error occurred while sending the invoice.', 'error'); |
| 153 |
} |
| 154 |
}, |
| 155 |
error: function() { |
| 156 |
// Show error message |
| 157 |
self.showMessage('An error occurred while sending the invoice.', 'error'); |
| 158 |
} |
| 159 |
}); |
| 160 |
}, |
| 161 |
|
| 162 |
// Preview invoice in a modal |
| 163 |
previewInvoice: function() { |
| 164 |
var self = this; |
| 165 |
var invoiceId = this.settings.invoiceId; |
| 166 |
|
| 167 |
// If no invoice ID, try to get it from the form |
| 168 |
if (!invoiceId) { |
| 169 |
invoiceId = $('#invoice_id').val(); |
| 170 |
} |
| 171 |
|
| 172 |
// If still no invoice ID, show error |
| 173 |
if (!invoiceId) { |
| 174 |
this.showMessage('Please save the invoice before previewing it.', 'error'); |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
// Show loading message |
| 179 |
this.showMessage('Loading preview...', 'info'); |
| 180 |
|
| 181 |
// Make AJAX request to get invoice preview |
| 182 |
$.ajax({ |
| 183 |
url: this.settings.ajaxUrl, |
| 184 |
type: 'POST', |
| 185 |
data: { |
| 186 |
action: 'easy_invoice_preview_invoice', |
| 187 |
invoice_id: invoiceId, |
| 188 |
nonce: this.settings.nonce |
| 189 |
}, |
| 190 |
success: function(response) { |
| 191 |
if (response.success) { |
| 192 |
// Show preview in modal |
| 193 |
self.showPreviewModal(response.data.html); |
| 194 |
|
| 195 |
// Clear the info message |
| 196 |
self.clearMessage(); |
| 197 |
} else { |
| 198 |
// Show error message |
| 199 |
self.showMessage(response.data.message || 'An error occurred while loading the preview.', 'error'); |
| 200 |
} |
| 201 |
}, |
| 202 |
error: function() { |
| 203 |
// Show error message |
| 204 |
self.showMessage('An error occurred while loading the preview.', 'error'); |
| 205 |
} |
| 206 |
}); |
| 207 |
}, |
| 208 |
|
| 209 |
// Show preview modal |
| 210 |
showPreviewModal: function(html) { |
| 211 |
// Create modal if it doesn't exist |
| 212 |
if ($('#invoice-preview-modal').length === 0) { |
| 213 |
$('body').append( |
| 214 |
'<div id="invoice-preview-modal" class="modal">' + |
| 215 |
' <div class="modal-content">' + |
| 216 |
' <span class="close">×</span>' + |
| 217 |
' <div id="invoice-preview-content"></div>' + |
| 218 |
' </div>' + |
| 219 |
'</div>' |
| 220 |
); |
| 221 |
|
| 222 |
// Close modal when clicking on close button |
| 223 |
$('#invoice-preview-modal .close').on('click', function() { |
| 224 |
$('#invoice-preview-modal').hide(); |
| 225 |
}); |
| 226 |
|
| 227 |
// Close modal when clicking outside the content |
| 228 |
$(window).on('click', function(e) { |
| 229 |
if ($(e.target).is('#invoice-preview-modal')) { |
| 230 |
$('#invoice-preview-modal').hide(); |
| 231 |
} |
| 232 |
}); |
| 233 |
} |
| 234 |
|
| 235 |
// Set the content |
| 236 |
$('#invoice-preview-content').html(html); |
| 237 |
|
| 238 |
// Show the modal |
| 239 |
$('#invoice-preview-modal').show(); |
| 240 |
}, |
| 241 |
|
| 242 |
// Show message |
| 243 |
showMessage: function(message, type) { |
| 244 |
// Create message container if it doesn't exist |
| 245 |
if ($('#invoice-message').length === 0) { |
| 246 |
$('.easy-invoice-container').prepend('<div id="invoice-message"></div>'); |
| 247 |
} |
| 248 |
|
| 249 |
// Set message class based on type |
| 250 |
var messageClass = 'notice'; |
| 251 |
switch (type) { |
| 252 |
case 'success': |
| 253 |
messageClass = 'notice notice-success'; |
| 254 |
break; |
| 255 |
case 'error': |
| 256 |
messageClass = 'notice notice-error'; |
| 257 |
break; |
| 258 |
case 'warning': |
| 259 |
messageClass = 'notice notice-warning'; |
| 260 |
break; |
| 261 |
case 'info': |
| 262 |
messageClass = 'notice notice-info'; |
| 263 |
break; |
| 264 |
} |
| 265 |
|
| 266 |
// Set the message |
| 267 |
$('#invoice-message').html('<p class="' + messageClass + '">' + message + '</p>'); |
| 268 |
|
| 269 |
// Auto-clear success and info messages after 5 seconds |
| 270 |
if (type === 'success' || type === 'info') { |
| 271 |
setTimeout(function() { |
| 272 |
$('#invoice-message p').fadeOut(500, function() { |
| 273 |
$(this).remove(); |
| 274 |
}); |
| 275 |
}, 5000); |
| 276 |
} |
| 277 |
}, |
| 278 |
|
| 279 |
// Clear message |
| 280 |
clearMessage: function() { |
| 281 |
$('#invoice-message').empty(); |
| 282 |
} |
| 283 |
}; |
| 284 |
|
| 285 |
// Initialize document generator when document is ready |
| 286 |
$(document).ready(function() { |
| 287 |
EasyInvoiceDocument.init(); |
| 288 |
}); |
| 289 |
|
| 290 |
})(jQuery); |