1 | <?php |
||
2 | |||
3 | namespace IproSync\Jobs\Properties; |
||
4 | |||
5 | use Carbon\Carbon; |
||
6 | use Illuminate\Bus\Queueable; |
||
7 | use Illuminate\Contracts\Queue\ShouldQueue; |
||
8 | use Illuminate\Foundation\Bus\Dispatchable; |
||
9 | use Illuminate\Queue\InteractsWithQueue; |
||
10 | use Illuminate\Queue\SerializesModels; |
||
11 | use IproSync\Models\MonthCustomRate; |
||
12 | use IproSync\Models\Property; |
||
13 | use LaravelIproSoftwareApi\IproSoftwareFacade; |
||
14 | |||
15 | class PropertyCustomRatesPull implements ShouldQueue |
||
16 | { |
||
17 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
18 | |||
19 | protected int $iproPropertyId; |
||
20 | |||
21 | public function __construct(int $iproPropertyId) |
||
22 | { |
||
23 | $this->iproPropertyId = $iproPropertyId; |
||
24 | } |
||
25 | |||
26 | public function handle() |
||
27 | { |
||
28 | /** @var Property $property */ |
||
29 | $property = Property::query()->findOrFail($this->iproPropertyId); |
||
30 | |||
31 | $response = IproSoftwareFacade::getPropertyCustomRates($this->iproPropertyId)->onlySuccessful(); |
||
32 | |||
33 | $items = $response->json('Rates'); |
||
34 | |||
35 | $property->monthsCustomRates() |
||
36 | ->whereKeyNot(collect($items)->pluck('Id')->all()) |
||
37 | ->delete(); |
||
38 | |||
39 | foreach ($items as $item) { |
||
40 | self::createOrUpdatePropertyCustomRate($property->getKey(), $item); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | public static function createOrUpdatePropertyCustomRate(int $propertyId, array $item): ?MonthCustomRate |
||
45 | { |
||
46 | if (isset($item['Id'])) { |
||
47 | /** @var MonthCustomRate $model */ |
||
48 | $model = MonthCustomRate::firstOrNew(['id' => $item['Id']], ) |
||
49 | ->fill([ |
||
50 | 'property_id' => $propertyId, |
||
51 | 'month' => Carbon::createFromFormat('Y-m', $item['Month'])->startOfMonth()->format('Y-m-d'), |
||
52 | 'notes' => !empty($item['Notes']) ? (string) $item['Notes'] : null, |
||
53 | 'week_price_list' => !empty($item['WeekPriceList']) ? (array) $item['WeekPriceList'] : null, |
||
54 | 'group_size' => !empty($item['GroupSize']) ? (array) $item['GroupSize'] : null, |
||
55 | ]) |
||
56 | ->fillPulled(); |
||
57 | $model->save(); |
||
58 | |||
59 | return $model; |
||
60 | } |
||
61 | |||
62 | return null; |
||
63 | } |
||
64 | } |
||
65 |