2022-08-19 15:27:57 +00:00
|
|
|
<?php
|
|
|
|
|
2023-01-13 14:11:56 +00:00
|
|
|
namespace App\Jobs\WorkOrder;
|
2022-08-19 15:27:57 +00:00
|
|
|
|
2023-01-02 15:43:56 +00:00
|
|
|
use App\Models\WorkOrder\WorkOrder as WorkOrderModel;
|
2022-08-19 15:27:57 +00:00
|
|
|
use Illuminate\Bus\Queueable;
|
2022-11-06 11:28:22 +00:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
2022-08-19 15:27:57 +00:00
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
2022-11-06 11:28:22 +00:00
|
|
|
use Illuminate\Queue\SerializesModels;
|
2023-01-20 11:29:23 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2022-11-06 11:28:22 +00:00
|
|
|
|
2022-09-08 16:12:02 +00:00
|
|
|
// use Illuminate\Contracts\Queue\ShouldBeUnique;
|
2022-08-19 15:27:57 +00:00
|
|
|
|
|
|
|
class WorkOrder implements ShouldQueue
|
|
|
|
{
|
2022-09-08 16:12:02 +00:00
|
|
|
use InteractsWithQueue, Queueable, SerializesModels;
|
2022-08-19 15:27:57 +00:00
|
|
|
|
2023-01-02 15:43:56 +00:00
|
|
|
protected WorkOrderModel $workOrder;
|
2023-01-30 16:14:07 +00:00
|
|
|
|
2023-01-02 15:43:56 +00:00
|
|
|
protected string $type;
|
2022-08-19 15:27:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2023-01-02 15:43:56 +00:00
|
|
|
public function __construct(WorkOrderModel $workOrder, $type = 'post')
|
2022-08-19 15:27:57 +00:00
|
|
|
{
|
2023-01-19 09:25:14 +00:00
|
|
|
$this->workOrder = $workOrder->load(['module']);
|
2022-08-19 15:27:57 +00:00
|
|
|
$this->type = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-12-31 11:28:21 +00:00
|
|
|
public function handle(): void
|
2022-08-19 15:27:57 +00:00
|
|
|
{
|
2023-01-19 09:25:14 +00:00
|
|
|
if ($this->workOrder->isPlatform()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-24 03:44:47 +00:00
|
|
|
if ($this->workOrder->status === 'error' && $this->type !== 'delete') {
|
|
|
|
$this->type = 'post';
|
|
|
|
}
|
|
|
|
|
2023-01-02 15:43:56 +00:00
|
|
|
if ($this->type == 'post') {
|
|
|
|
$response = $this->workOrder->module->http()->post('work-orders', $this->workOrder->toArray());
|
2023-02-01 06:22:30 +00:00
|
|
|
} else if ($this->type == 'put') {
|
|
|
|
$response = $this->workOrder->module->http()->put('work-orders/' . $this->workOrder->id, $this->workOrder->toArray());
|
2023-01-02 15:43:56 +00:00
|
|
|
} else {
|
2023-02-01 06:22:30 +00:00
|
|
|
$response = $this->workOrder->module->http()->delete('work-orders/' . $this->workOrder->id);
|
2022-11-19 03:06:02 +00:00
|
|
|
|
|
|
|
if ($response->successful()) {
|
|
|
|
$this->workOrder->delete();
|
|
|
|
}
|
2022-08-19 15:27:57 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 06:22:30 +00:00
|
|
|
if (!$response->successful()) {
|
2023-01-24 03:37:04 +00:00
|
|
|
Log::debug('WorkOrder push failed', [
|
2023-01-24 03:47:15 +00:00
|
|
|
'type' => $this->type,
|
2023-01-24 03:37:04 +00:00
|
|
|
'response' => $response->json(),
|
|
|
|
'workOrder' => $this->workOrder->toArray(),
|
|
|
|
]);
|
2023-01-20 11:29:23 +00:00
|
|
|
|
2022-10-31 11:03:38 +00:00
|
|
|
$this->workOrder->update([
|
2023-01-30 16:14:07 +00:00
|
|
|
'status' => 'error',
|
2022-10-31 11:03:38 +00:00
|
|
|
]);
|
2022-08-19 15:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|