Files
star-erp/app/Console/Commands/NotifyUtilityFeeStatus.php
sky121113 07b7d9b327
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 56s
[FEAT] 實作公共事業費逾期提醒、租戶自訂通知設定及發送測試信功能
2026-03-05 16:01:00 +08:00

91 lines
3.4 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class NotifyUtilityFeeStatus extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'finance:notify-utility-fees';
/**
* The console command description.
*
* @var string
*/
protected $description = '檢查公共事業費狀態並寄送 Email 通知管理員';
/**
* Execute the console command.
*/
public function handle()
{
$this->info("正在掃描公共事業費狀態...");
// 1. 更新逾期狀態 (pending -> overdue)
\App\Modules\Finance\Models\UtilityFee::where('payment_status', \App\Modules\Finance\Models\UtilityFee::STATUS_PENDING)
->whereNotNull('due_date')
->where('due_date', '<', now()->startOfDay())
->update(['payment_status' => \App\Modules\Finance\Models\UtilityFee::STATUS_OVERDUE]);
// 2. 獲取需要處理的單據 (pending 或 overdue)
$unpaidFees = \App\Modules\Finance\Models\UtilityFee::whereIn('payment_status', [
\App\Modules\Finance\Models\UtilityFee::STATUS_PENDING,
\App\Modules\Finance\Models\UtilityFee::STATUS_OVERDUE
])
->orderBy('due_date', 'asc')
->get();
if ($unpaidFees->isEmpty()) {
$this->info("目前沒有需要繳納的公共事業費。");
return 0;
}
// 3. 讀取系統設定
$senderEmail = \App\Modules\Core\Models\SystemSetting::getVal('notification.utility_fee_sender_email');
$senderPassword = \App\Modules\Core\Models\SystemSetting::getVal('notification.utility_fee_sender_password');
$recipientEmailsStr = \App\Modules\Core\Models\SystemSetting::getVal('notification.utility_fee_recipient_emails');
if (empty($senderEmail) || empty($senderPassword) || empty($recipientEmailsStr)) {
$this->warn("系統設定中缺乏完整的 Email 通知參數,跳過寄送通知。請至「系統設定」->「通知設定」完善資料。");
return 0;
}
// 4. 動態覆寫應用程式名稱與 SMTP Config
$tenantName = tenant('name') ?? config('app.name');
config([
'app.name' => $tenantName,
'mail.mailers.smtp.username' => $senderEmail,
'mail.mailers.smtp.password' => $senderPassword,
'mail.from.address' => $senderEmail,
'mail.from.name' => $tenantName . ' (系統通知)'
]);
// 清理原先可能的 Mailer 實例,確保使用新的 Config
\Illuminate\Support\Facades\Mail::purge();
// 5. 解析收件者並寄送 Email
$recipients = array_map('trim', explode(',', $recipientEmailsStr));
$validRecipients = array_filter($recipients, fn($e) => filter_var($e, FILTER_VALIDATE_EMAIL));
if (empty($validRecipients)) {
$this->warn("無效的收件者 Email 格式,跳過寄送通知。");
return 0;
}
try {
\Illuminate\Support\Facades\Mail::to($validRecipients)->send(new \App\Mail\PaymentReminderMail($unpaidFees));
$this->info("通知郵件已成功寄送至: " . implode(', ', $validRecipients));
} catch (\Exception $e) {
$this->error("Email 寄送失敗: " . $e->getMessage());
}
return 0;
}
}