ldc-legaulois/app/Jobs/SendOrderToArchadia.php

124 lines
4.8 KiB
PHP
Raw Normal View History

2021-03-08 12:30:16 +01:00
<?php
namespace App\Jobs;
use App\Enums\OrderType;
use App\Exceptions\CrmException;
use App\Models\Order;
use Exception;
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;
class SendOrderToArchadia implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
/** @var Order */
private $order;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Order $order)
{
$this->order = $order;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try {
2021-05-17 18:04:26 +02:00
$deliver = $this->order->deliveryAddress()->exists() ?
$this->order->deliveryAddress : $this->order;
2021-03-08 12:30:16 +01:00
/** @var \App\Lib\LDCCRM\ApiV1 */
$crm = app('crmApi');
foreach ($this->order->sellItems as $sellItem) {
for ($i = 0; $i < $sellItem->pivot->quantity; $i++) {
2021-03-21 22:51:19 +01:00
$response = $crm->arcadiaOutgoingMail($this->order->customer->crm_id, [
2021-03-08 12:30:16 +01:00
'root' => [
[
'fieldName' => 'order_num',
//format 9 numeric characters
'fieldValue' => str_pad((string)$this->order->id, 9, '0', \STR_PAD_LEFT),
2021-03-08 12:30:16 +01:00
],
[
'fieldName' => 'order_date',
//format : DD/MM/YYYY
'fieldValue' => Carbon::parse($this->order->created_at)->format('d/m/Y'),
],
[
'fieldName' => 'state_order_cb',
// format 1 ou Null (oui Null et pas NULL)
'fieldValue' => $this->order->payementType->equals(OrderType::cb()) ? 1 : 'Null',
],
[
'fieldName' => 'state_order_check',
// format 1 ou Null (oui Null et pas NULL)
'fieldValue' => $this->order->payementType->equals(OrderType::check()) ? 1 : 'Null'
],
[
'fieldName' => 'operation',
// mettre valeur dans .env
'fieldValue' => env('BOUTIQUE_ARCHADIA_OP_CODE', 16)
]
],
'orderLine' => [
[
'fieldName' => 'package_unit_weight',
'fieldValue' => $sellItem->archadia_weight
],
[
'fieldName' => 'qty',
// forced to 1 faire un appel par produit et pour chaque quantité
'fieldValue' => '1'
],
[
'fieldName' => 'support_code',
'fieldValue' => $sellItem->archadia_id
],
[
'fieldName' => 'label_type_support',
'fieldValue' => $sellItem->name
]
]
2021-05-17 17:51:41 +02:00
], 'default', [
2021-05-17 18:04:26 +02:00
"firstname" => $deliver->firstname,
"lastname" => $deliver->lastname,
"address" => $deliver->address,
"city" => $deliver->city,
"postcode" => $deliver->postcode,
"phone" => $deliver->phone
2021-05-17 17:51:41 +02:00
]);
2021-03-08 12:30:16 +01:00
if ($response->status == 'KO') {
throw new CrmException(sprintf('Crm Api response is KO %s', json_encode((array)$response)));
} else {
Log::debug(sprintf('Order %d: Sell Item %d sent to archadia', $this->order->id, $sellItem->id));
}
}
}
Log::debug(sprintf('Order %d sent to archadia', $this->order->id));
} catch (Exception $e) {
report($e);
Log::critical(sprintf(
2021-03-21 22:51:19 +01:00
'Could not end customer transaction crmApi->arcadiaOutgoingMail in boutique for order %d, %s',
2021-03-08 12:30:16 +01:00
$this->order->id,
$e->getMessage()
));
}
}
}