Viewing file: SendWorkflowSMS.php (8.61 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Log; use App\Models\CustomerNumber; use App\Models\SenderId; use App\Models\Coverage; use App\Models\Report; use App\Models\Contact; use App\Models\Customer;
class SendWorkflowSMS implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public $sms; public $number; public $from; public $customer_id; public $type; public $schedule_datetime; /** * Create a new job instance. */ public function __construct($data) { $this->sms = $data['smsData']; $this->number = $data['to']; $this->from = $data['from']; $this->customer_id = $data['customer_id']; $this->type = $data['type']; $this->schedule_datetime = $data['schedule_datetime']; }
/** * Execute the job. */ public function handle(): void { try {
$customer = Customer::where('id',$this->customer_id)->first();
if ($this->type == 'phone_number') { $customerNumber= CustomerNumber::where('customer_id', $customer->id)->where('number', $this->from)->orWhere('number',"+".str_replace('+','',$this->from))->first(); if(!$customerNumber){ return; } $number_form = $this->from; } else if ($this->type == 'sender_id') { $sender_id = SenderId::where('sender_id', $this->from)->where('status', 'approved')->first(); if(!$sender_id){ return; } $number_form = $sender_id->sender_id; }
$allToNumbers = []; foreach($this->number as $to_number){ $allToNumbers[]=$to_number; }
$allToNumbers = array_unique($allToNumbers);
$to = $allToNumbers; $numbers = json_encode(['from' => $number_form, 'to' => $allToNumbers]); $type = 'sent';
$current_plan=$customer->plan;
if(!$current_plan){ Log::warning("Don\'t have any plan right now"); return; }
$wallet = $customer->wallet()->first();
$totalCount=1; $requestCharacters=$this->sms['message']; $characters=mb_strlen($requestCharacters, "UTF-8"); if (strlen($requestCharacters) != strlen(utf8_decode($requestCharacters))) { if($characters && $characters > 70){ $grandTotal=ceil($characters / 70); if($grandTotal > 1) $totalCount= $grandTotal; } }else { if($characters && $characters > 160){ $grandTotal=ceil($characters / 160); if($grandTotal > 1) $totalCount= $grandTotal; } }
$totalToNumbers= count($allToNumbers) * $totalCount; if ($wallet->credit < $totalToNumbers) { Log::warning("Not enough SMS credits to send message."); return; }
if ($this->type == 'phone_number') { $number = Number::where('number', $number_form)->orWhere('number',"+".str_replace('+','',$number_form))->first(); } else if ($this->type == 'sender_id') { $number = SenderId::where('sender_id', $number_form)->first(); }
if (!$number){ Log::warning("Number not found please contact with administrator"); return; }
$plain_sms=$current_plan->plain_sms;
if ($this->type == 'phone_number') { $numb = $number->number; $fromType='number'; } else if ($this->type == 'sender_id') { $numb = $number->sender_id; $fromType='sender_id'; }
$gateway=$number->gateway; if (!$gateway) { Log::warning("Gateway not found"); return; }
$newMessage = $customer->messages()->create([ 'body' => $this->sms['message'], 'numbers' => $numbers, 'customer_id' => $customer->id, 'type' => $this->type, ]);
$coverages=Coverage::whereIn('id', json_decode($current_plan->coverage_ids))->get(); $coverage_rate = []; if ($coverages) { foreach ($coverages as $coverage) { $coverage_rate[$coverage->country_code] = $coverage->plain_sms; } }
$totalSmsAmount=0; foreach ($this->number as $recipient_number) { $number_code = getCountryDialCode($recipient_number); $number_without_code = getPhoneNumberWithoutDialCode($recipient_number); // dd($number_code); if (isset($coverage_rate[str_replace('+', '', $number_code)])) { $totalSmsAmount = $totalSmsAmount + $coverage_rate[str_replace('+', '', $number_code)]; } }
if(!$totalSmsAmount || $totalSmsAmount <= 0){ Log::warning("Select Valid Recipient To Send Message"); return; } // $totalCredit=$totalToNumbers * $plain_sms; $totalCredit=$totalToNumbers * $totalSmsAmount;
if ($this->type == 'phone_number') { $subType='non_masking'; } else if ($this->type == 'sender_id') { $subType='masking'; }
//SMS Credit Managment $wallet->credit = $wallet->credit - $totalCredit; $wallet->save();
$report= new Report(); $report->customer_id=$newMessage->customer_id; $report->ref_id=$newMessage->id; $report->type='message'; $report->sub_type=$subType; $report->amount='-'.$totalCredit; $report->save();
$sms_queue = [];
foreach ($allToNumbers as $to) {
$contact = Contact::where('customer_id', $customer->id)->where('number', getPhoneNumberWithoutDialCode($to))->orderBy('number')->first(); $templates = $this->sms['message']; $templateBody = $templates; $templateBody = str_replace('contact__id', $contact->id, $templateBody); if ($contact->first_name) { $templateBody = str_replace('{first_name}', $contact->first_name, $templateBody); } else { $templateBody = str_replace('{first_name}', ' ', $templateBody); } if ($contact->last_name) { $templateBody = str_replace('{last_name}', $contact->last_name, $templateBody); } else { $templateBody = str_replace('{last_name}', ' ', $templateBody); } if ($contact->address) { $templateBody = str_replace('{address}', $contact->address, $templateBody); } else { $templateBody = str_replace('{address}', ' ', $templateBody); } if ($contact->city) { $templateBody = str_replace('{city}', $contact->city, $templateBody); } else { $templateBody = str_replace('{city}', ' ', $templateBody); } if ($contact->state) { $templateBody = str_replace('{state}', $contact->state, $templateBody); } else { $templateBody = str_replace('{state}', ' ', $templateBody); } if ($contact->zip_code) { $templateBody = str_replace('{zip_code}', $contact->zip_code, $templateBody); } else { $templateBody = str_replace('{zip_code}', ' ', $templateBody); } if ($contact->email) { $templateBody = str_replace('{email}', $contact->email, $templateBody); } else { $templateBody = str_replace('{email}', ' ', $templateBody); }
$sms_queue[] = [ 'message_id' => $newMessage->id, 'from' => $numb, 'to' => $to, 'from_type' => $fromType, 'schedule_datetime' => $this->schedule_datetime, 'body' => $templateBody, 'dynamic_gateway_id' => $gateway->id, 'created_at' => now(), 'updated_at' => now(), 'type' => 'sent', 'staff_id' => $customer->id, ]; }
$customer->sms_queues()->createMany($sms_queue); $customer->message_logs()->createMany($sms_queue);
Log::info('Workflow SMS saved successfully.'); } catch (\Exception $e) { Log::error('Failed to send Workflow SMS'); } }
}
|