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; } }