Viewing file: CheckExpiredNumber.php (2.82 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command; use App\Events\SendMail; use App\Models\Notification; use App\Models\User; use App\Models\Number; use App\Models\CustomerNumber; use Carbon\Carbon; class CheckExpiredNumber extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'app:check-expired-number';
/** * The console command description. * * @var string */ protected $description = 'Command description';
/** * Execute the console command. */ public function handle() { $customerNumbers = CustomerNumber::with(['customer'])->whereNotNull('expire_date') ->where('expire_date', '<', now()->addWeek()) ->get();
foreach ($customerNumbers as $customerNumber) {
$customer = $customerNumber->customer;
if ($customerNumber->expire_date > now()->subDay(1)) { if (!$customerNumber->notifications_date || $customerNumber->notifications_date < now()) { $template = get_email_template('number_expire'); $subject = $template->subject ?? 'Number Expired'; $templateBody = ''; if ($template && isset($template->body) && $template->body) { $templateBody = str_replace('{customer_name}', $customer->first_name . ' ' . $customer->last_name, $template->body); $templateBody = str_replace('{number}', $customerNumber->number, $templateBody); $templateBody = str_replace('{date}', $customerNumber->expire_date, $templateBody); }else{ $templateBody = "Dear {$customer->first_name} {$customer->last_name} your number will be expired in {$customerNumber->expire_date}"; }
SendMail::dispatch($customer->email, $subject, $templateBody);
$notification = new Notification(); $notification->customer_id = $customer->id; $notification->subject = $subject; $notification->details = $templateBody; $notification->save();
$expireDate = Carbon::parse($customerNumber->expire_date); $now = Carbon::now(); $daysDiff = $expireDate->diffInDays($now);
$customerNumber->notifications_date = now()->addDays($daysDiff+1); $customerNumber->save(); } }else { if ($customerNumber->notifications_date) { $customerNumber->notifications_date = null; $customerNumber->save(); } }
}
return Command::SUCCESS; } }
|