diff --git a/public/js/modals/email-modal.js b/public/js/modals/email-modal.js index dfcc79f..173114b 100644 --- a/public/js/modals/email-modal.js +++ b/public/js/modals/email-modal.js @@ -2,7 +2,7 @@ // Modal to review and send invoice emails via AWS SES // With Stripe Payment Link integration -import { showSpinner, hideSpinner } from '../utils/helpers.js'; +import { showSpinner, hideSpinner, formatDate } from '../utils/helpers.js'; let currentInvoice = null; let quillInstance = null; @@ -118,7 +118,8 @@ function renderModalContent() { // Variablen für den Text aufbereiten const invoiceNum = currentInvoice.invoice_number || currentInvoice.id; const totalDue = parseFloat(currentInvoice.balance ?? currentInvoice.total).toFixed(2); - + const customerName = currentInvoice.customer_name || 'Valued Customer'; + // Datum formatieren let dueDateStr = 'Upon Receipt'; if (currentInvoice.due_date) { @@ -135,17 +136,46 @@ function renderModalContent() { } else { paymentText = 'Our terms are Net 30.'; } - const customerName = currentInvoice.customer_name || 'Valued Customer'; - const defaultHtml = ` -
Dear ${customerName},
-Attached is invoice #${invoiceNum} for service performed at your location. The total amount due is $${totalDue}, ${paymentText}
-Please pay at your earliest convenience. We appreciate your continued business.
-If you have any questions about the invoice, feel free to reply to this email.
-Best regards,
-Claudia Knuth
-Bay Area Affiliates, Inc.
-accounting@bayarea-cc.com
- `; + + // Detect overdue: unpaid + older than 30 days + const invoiceDateParsed = currentInvoice.invoice_date + ? new Date(currentInvoice.invoice_date.split('T')[0]) + : null; + const daysSinceInvoice = invoiceDateParsed + ? Math.floor((new Date() - invoiceDateParsed) / 86400000) + : 0; + const isOverdue = !currentInvoice.paid_date && daysSinceInvoice > 30; + + let defaultHtml = ''; + + if (isOverdue) { + // Reminder / Overdue template + defaultHtml = ` +Dear ${customerName},
+We hope this message finds you well. Our records indicate that invoice #${invoiceNum} in the amount of $${totalDue}, dated ${formatDate(currentInvoice.invoice_date)}, remains unpaid.
+This invoice is now ${daysSinceInvoice} days past the invoice date. We kindly request prompt payment at your earliest convenience.
+For your convenience, you can pay securely online using the payment link included below. We accept both Credit Card and ACH bank transfer.
+If payment has already been sent, please disregard this notice. Should you have any questions or need to discuss payment arrangements, please do not hesitate to reply to this email.
+Thank you for your attention to this matter. We value your business and look forward to continuing our partnership.
+Best regards,
+Claudia Knuth
+Bay Area Affiliates, Inc.
+accounting@bayarea-cc.com
+ `; + } else { + // Standard template + defaultHtml = ` +Dear ${customerName},
+Attached is invoice #${invoiceNum} for service performed at your location. The total amount due is $${totalDue}, ${paymentText}
+Please pay at your earliest convenience. We appreciate your continued business.
+If you have any questions about the invoice, feel free to reply to this email.
+Best regards,
+Claudia Knuth
+Bay Area Affiliates, Inc.
+accounting@bayarea-cc.com
+ `; + } + quillInstance.root.innerHTML = defaultHtml; // Bind Submit Handler diff --git a/public/js/views/invoice-view.js b/public/js/views/invoice-view.js index 2869ee1..109959b 100644 --- a/public/js/views/invoice-view.js +++ b/public/js/views/invoice-view.js @@ -269,19 +269,10 @@ function renderInvoiceRow(invoice) { if (hasQbo && !paid && !overdue && invoice.email_status !== 'sent') { sendBtn = ``; } - // if (hasQbo && !paid && !overdue) { - // sendBtn = ` - // - // - // `; } const delBtn = ``; - const stripeEmailBtn = (hasQbo && !paid && !overdue && invoice.email_status !== 'sent') + const stripeEmailBtn = (hasQbo && !paid && (invoice.email_status !== 'sent' || ((invoice.email_status === 'sent' && overdue)))) ? `` : '';