101 lines
4.0 KiB
PHP
101 lines
4.0 KiB
PHP
<?php
|
|
/**
|
|
* Reminder Scheduler Cron Script
|
|
* This script should be run via CLI/Cron periodically (e.g., once daily).
|
|
*/
|
|
|
|
require_once __DIR__ . '/../db/config.php';
|
|
require_once __DIR__ . '/../mail/MailService.php';
|
|
require_once __DIR__ . '/../includes/DocumentService.php';
|
|
|
|
echo "Starting Reminder Scheduler..." . PHP_EOL;
|
|
|
|
// 1. Invoice Payment Reminders
|
|
$overdue_invoices = db()->query("
|
|
SELECT i.*, c.name as customer_name, c.email as customer_email
|
|
FROM invoices i
|
|
JOIN customers c ON i.customer_id = c.id
|
|
WHERE i.status IN ('Unpaid', 'Partial', 'Overdue')
|
|
AND i.due_date < CURDATE()
|
|
AND (i.last_reminded_at IS NULL OR i.last_reminded_at < DATE_SUB(NOW(), INTERVAL 7 DAY))
|
|
AND i.deleted_at IS NULL
|
|
")->fetchAll();
|
|
|
|
foreach ($overdue_invoices as $invoice) {
|
|
echo "Sending reminder for Invoice #{$invoice['invoice_number']} to {$invoice['customer_email']}..." . PHP_EOL;
|
|
|
|
$subject = "Payment Reminder: Invoice #{$invoice['invoice_number']}";
|
|
$body = "
|
|
<h2>Payment Reminder</h2>
|
|
<p>Dear {$invoice['customer_name']},</p>
|
|
<p>This is a friendly reminder that your invoice <strong>#{$invoice['invoice_number']}</strong> with an amount of <strong>" . format_currency($invoice['total_amount']) . "</strong> was due on <strong>{$invoice['due_date']}</strong>.</p>
|
|
<p>Please kindly arrange for payment at your earliest convenience. We have attached the invoice for your reference.</p>
|
|
<p>Thank you for your business!</p>
|
|
";
|
|
|
|
$pdf_content = DocumentService::generateInvoicePDF($invoice['id'], 'S');
|
|
$opts = [
|
|
'attachments' => [
|
|
[
|
|
'data' => $pdf_content,
|
|
'name' => "Invoice_{$invoice['invoice_number']}.pdf"
|
|
]
|
|
]
|
|
];
|
|
|
|
$res = MailService::sendMail($invoice['customer_email'], $subject, $body, null, $opts);
|
|
|
|
if ($res['success']) {
|
|
$stmt = db()->prepare("UPDATE invoices SET last_reminded_at = NOW(), status = 'Overdue' WHERE id = ?");
|
|
$stmt->execute([$invoice['id']]);
|
|
echo "Successfully reminded." . PHP_EOL;
|
|
} else {
|
|
echo "Failed to send: " . $res['error'] . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
// 2. Quotation Follow-up Reminders
|
|
$pending_quotations = db()->query("
|
|
SELECT q.*, c.name as customer_name, c.email as customer_email
|
|
FROM quotations q
|
|
JOIN customers c ON q.customer_id = c.id
|
|
WHERE q.status = 'Sent'
|
|
AND q.expiry_date >= CURDATE()
|
|
AND (q.last_reminded_at IS NULL OR q.last_reminded_at < DATE_SUB(NOW(), INTERVAL 5 DAY))
|
|
AND q.deleted_at IS NULL
|
|
")->fetchAll();
|
|
|
|
foreach ($pending_quotations as $quote) {
|
|
echo "Sending follow-up for Quotation #{$quote['quotation_number']} to {$quote['customer_email']}..." . PHP_EOL;
|
|
|
|
$subject = "Follow-up: Quotation #{$quote['quotation_number']}";
|
|
$body = "
|
|
<h2>Quotation Follow-up</h2>
|
|
<p>Dear {$quote['customer_name']},</p>
|
|
<p>We're following up on the quotation <strong>#{$quote['quotation_number']}</strong> we sent you on <strong>{$quote['issue_date']}</strong>.</p>
|
|
<p>The quotation is valid until <strong>{$quote['expiry_date']}</strong>. Please let us know if you have any questions or if you're ready to proceed. We have attached the quotation for your convenience.</p>
|
|
<p>Best regards,<br>The Sales Team</p>
|
|
";
|
|
|
|
$pdf_content = DocumentService::generateQuotationPDF($quote['id'], 'S');
|
|
$opts = [
|
|
'attachments' => [
|
|
[
|
|
'data' => $pdf_content,
|
|
'name' => "Quotation_{$quote['quotation_number']}.pdf"
|
|
]
|
|
]
|
|
];
|
|
|
|
$res = MailService::sendMail($quote['customer_email'], $subject, $body, null, $opts);
|
|
|
|
if ($res['success']) {
|
|
$stmt = db()->prepare("UPDATE quotations SET last_reminded_at = NOW() WHERE id = ?");
|
|
$stmt->execute([$quote['id']]);
|
|
echo "Successfully sent follow-up." . PHP_EOL;
|
|
} else {
|
|
echo "Failed to send: " . $res['error'] . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
echo "Reminder Scheduler finished." . PHP_EOL; |