ldc-legaulois/app/Jobs/SendOrderToArchadia.php
2021-05-17 18:04:26 +02:00

123 lines
4.8 KiB
PHP

<?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 {
$deliver = $this->order->deliveryAddress()->exists() ?
$this->order->deliveryAddress : $this->order;
/** @var \App\Lib\LDCCRM\ApiV1 */
$crm = app('crmApi');
foreach ($this->order->sellItems as $sellItem) {
for ($i = 0; $i < $sellItem->pivot->quantity; $i++) {
$response = $crm->arcadiaOutgoingMail($this->order->customer->crm_id, [
'root' => [
[
'fieldName' => 'order_num',
//format 9 numeric characters
'fieldValue' => str_pad((string)$this->order->id, 9, '0', \STR_PAD_LEFT),
],
[
'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
]
]
], 'default', [
"firstname" => $deliver->firstname,
"lastname" => $deliver->lastname,
"address" => $deliver->address,
"city" => $deliver->city,
"postcode" => $deliver->postcode,
"phone" => $deliver->phone
]);
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(
'Could not end customer transaction crmApi->arcadiaOutgoingMail in boutique for order %d, %s',
$this->order->id,
$e->getMessage()
));
}
}
}