Rapport-Laravel/app/Console/Commands/SendNeblogisticEmail.php

161 lines
6.1 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use App\Mail\NeblogisticAbm;
use App\Mail\Neblogistic;
class SendNeblogisticEmail extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'neblogistic:send-email
{--record_id= : Send for a specific neblogistic ID, even if a previous attempt failed}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send neblogistic ABM email for a specific record by ID';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$recordId = $this->option('record_id');
if (!empty($recordId)) {
$record = DB::table('neblogistic')->where('id', $recordId)->first();
if (!$record) {
$this->error("Record not found for ID: {$recordId}");
return 1;
}
$this->info("Sending request emails for record ID {$recordId} (email_sent={$record->email_sent}).");
return $this->sendEmail($record) ? 0 : 1;
}
$records = DB::table('neblogistic')
->where('email_sent', 0)
->where('status', 'Requested')
->whereNotNull('submission_id')
->where('id', '>', 202910)
->orderBy('created_at', 'asc')
->limit(5)
->get();
foreach ($records as $record) {
$this->sendEmail($record);
}
return 0;
}
protected function sendEmail($record) {
$post = array_merge((array) $record, [
'emp_id' => $record->emp_id,
'user_id' => $record->user_id,
'abm_user_id' => $record->abm_user_id ?? '',
'hierarchy_id' => $record->hierarchy_id,
'employee_name' => $record->emp_name,
'employee_phone' => $record->emp_phone,
'employee_email' => $record->emp_email,
'abm_name' => $record->abm_name,
'abm_email' => $record->abm_email,
'rbm_name' => $record->rbm_name,
'msl_code' => $record->msl_code,
'form_submit_type' => $record->form_submit_type,
'dr_name' => $record->doctor_name,
'doctor_name' => $record->doctor_name,
'doctor_mobile_number' => $record->doctor_phone,
'speciality' => $record->doctor_speciality,
'for_what_purpose_nebulisers_will_be_used_by_dr' => $record->nebulizer_purpose,
'type_of_nebuliser' => $record->type_of_nebulizer,
'no_of_nebulizer' => $record->no_of_nebulizer,
'value_of_the_parcel' => $record->value_of_parcel,
'address_type' => $record->address_type,
'building_name' => $record->address_building,
'address_line_1' => $record->address_line_1,
'address_line_2' => $record->address_line_2,
'state' => $record->address_state,
'city' => $record->address_city,
'pin_code' => $record->address_pin_code,
'clinic_hospital_home_name' => $record->clinic_hospital_name,
'status' => $record->status,
'team' => $record->team,
'depot_id' => $record->depot_id,
'depot_name' => $record->depot_name,
'old_employee_code' => $record->old_employee_code,
'remarks' => $record->remarks,
'paramedic_alternate_contact_number' => $record->paramedic_alternate_contact_number,
'request_letter' => $record->request_letter ?? '',
'created_at' => $record->created_at,
'updated_at' => $record->updated_at,
'id' => $record->id,
'submission_id' => $record->submission_id,
'form_id' => '1750914801-153443b9-94d0-4dcb-b3f3-4fa11588a0a9',
'partition' => 'rapport-team3'
]);
$abmOk = null;
$tmOk = null;
// if (!empty($record->abm_email)) {
// try {
// Mail::to($record->abm_email)->cc('ravigulhane1983@gmail.com')->send(new NeblogisticAbm($post));
// $abmOk = true;
// $this->info("ABM email sent to {$record->abm_email} for record ID {$record->id}");
// } catch (\Exception $e) {
// $abmOk = false;
// $this->error("Failed ABM email for record ID {$record->id}: " . $e->getMessage());
// Log::error('Neblogistic request ABM email failed', [
// 'id' => $record->id,
// 'abm_email' => $record->abm_email,
// 'error' => $e->getMessage(),
// ]);
// }
// } else {
// $this->warn("Skipping ABM email for record ID {$record->id}: no abm_email.");
// }
if (!empty($record->emp_email)) {
try {
Mail::to($record->emp_email)->cc('ravigulhane1983@gmail.com')->send(new Neblogistic($post));
$tmOk = true;
$this->info("TM email sent to {$record->emp_email} for record ID {$record->id}");
} catch (\Exception $e) {
$tmOk = false;
$this->error("Failed TM email for record ID {$record->id}: " . $e->getMessage());
Log::error('Neblogistic request TM email failed', [
'id' => $record->id,
'emp_email' => $record->emp_email,
'error' => $e->getMessage(),
]);
}
} else {
$this->warn("Skipping TM email for record ID {$record->id}: no emp_email.");
}
$attempted = array_filter([$abmOk, $tmOk], function ($v) {
return $v !== null;
});
$allSucceeded = count($attempted) > 0 && !in_array(false, $attempted, true);
DB::table('neblogistic')->where('id', $record->id)->update([
'email_sent' => $allSucceeded ? 1 : 2,
]);
return $allSucceeded;
}
}