ldc-legaulois/app/Jobs/SendOrderToArchadia.php
2021-03-21 22:51:19 +01:00

117 lines
4.4 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 {
/** @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',
'fieldValue' => $this->order->id, //format 9 numeric characters
],
[
'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' => 'order_date',
'fieldValue' => date('d/m/Y')
],
[
'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');
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()
));
}
}
}