ldc-legaulois/app/Jobs/SendOrderToArchadia.php
2023-06-26 16:02:23 +02:00

83 lines
2.9 KiB
PHP

<?php
namespace App\Jobs;
use App\Enums\OrderType;
use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
use Kwaadpepper\ExceptionHandler\Exceptions\ExceptionHandler;
use LDC\LDCCRM\Enums\ArcadiaOrderType;
use LDC\LDCCRM\Models\ArcadiaMailDefault;
class SendOrderToArchadia implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
/** @var Order */
private $order;
/**
* Create a new job instance.
*
* @param \App\Models\Order $order
* @return void
*/
public function __construct(Order $order)
{
$this->order = $order;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$deliver = $this->order->deliveryAddress()->exists() ?
$this->order->deliveryAddress : $this->order;
foreach ($this->order->sellItems as $sellItem) {
for ($i = 0; $i < $sellItem->pivot->quantity; $i++) {
try {
// * init CRM API
app('crmAPI');
$mail = new ArcadiaMailDefault([
// * mettre valeur dans .env
'operation' => \intval(env('BOUTIQUE_ARCHADIA_OP_CODE', 16)),
'support_code' => $sellItem->archadia_id,
'label_type_support' => $sellItem->name,
// * format : DD/MM/YYYY
'order_date' => Carbon::parse($this->order->created_at)->format('Y/m/d H:i:s'),
// * format 9 numeric characters
'order_num' => $this->order->id,
'state_order' => $this->order->payementType->equals(OrderType::cb()) ?
ArcadiaOrderType::cb() : ArcadiaOrderType::check(),
'package_unit_weight' => $sellItem->archadia_weight,
'email' => $deliver->email,
'firstname' => $deliver->firstname,
'lastname' => $deliver->lastname,
'address' => $deliver->address,
'city' => $deliver->city,
'postcode' => $deliver->postcode,
'phone' => $deliver->phone
]);
$mail->send();
} catch (ValidationException $e) {
ExceptionHandler::sendExceptionByEmailStatic($e);
} //end try
}//end for
}//end foreach
Log::debug(sprintf('Order %d sent to archadia', $this->order->id));
}
}