Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
StockIOProductResource
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
4 / 4
11
100.00% covered (success)
100.00%
1 / 1
 toArray
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
2
 purchaseSolicitationData
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 requestItemObservation
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 hasSolicitationIdentifier
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace Domain\StockIO\Resources;
4
5use App\Models\Integration\Integration;
6use Domain\Integration\Resources\IntegrationResource;
7use Domain\PickingOrder\Resources\PickedItemResource;
8use Domain\PickingOrder\Resources\PickingOrderProductResource;
9use Domain\StockIO\Resources\StockOIResource;
10use Domain\StockIO\Services\StockIOProductMissingAmountService;
11use Domain\Product\Resources\ProductResource;
12use Domain\Request\Resources\RequestDeliveryResource;
13use Domain\Request\Resources\RequestDevolutionResource;
14use Illuminate\Database\Eloquent\Collection;
15use Illuminate\Http\Request;
16use Illuminate\Http\Resources\Json\JsonResource;
17
18/**
19 * @property float $amount
20 * @property bool $picked
21 * @property int $id
22 * @property int $type
23 * @property Integration $integration
24 * @property Collection $pickedItems
25 */
26class StockIOProductResource extends JsonResource
27{
28    public function toArray(Request $request): array
29    {
30        $amountService = new StockIOProductMissingAmountService();
31        $missingAmount = $amountService->resolve($this->resource);
32        $stockAmount = $amountService->resolveStockAmount($this->resource);
33
34        $data = array_filter([
35            'id'                       => $this->id,
36            'amount'                   => $this->amount,
37            'stock_amount'             => $stockAmount,
38            'picked'                   => $this->picked,
39            'status'                   => $this->status->value,
40            'request_item_observation' => $this->requestItemObservation(),
41            'type'                     => isset($this->pivot->type) ? $this->pivot->type : null,
42            'integration'              => IntegrationResource::make($this->whenLoaded('integration')),
43            'product'                  => ProductResource::make($this->whenLoaded('product')),
44            'picked_items'             => PickedItemResource::collection($this->whenLoaded('pickedItems')),
45            'stock_io'                 => StockOIResource::make($this->whenLoaded('stockIO')),
46            'request_delivery'         => RequestDeliveryResource::collection($this->whenLoaded('requestDeliveries')),
47            'request_devolution'       => RequestDevolutionResource::collection($this->whenLoaded('requestDevolution')),
48            'picking_order_product'    => PickingOrderProductResource::make($this->whenLoaded('pickingOrderProduct')),
49            'picking_order_product_id' => $this->pickingOrderProduct?->id,
50            'purchase_solicitation'    => $this->purchaseSolicitationData(),
51        ], fn ($value) => !is_null($value));
52
53        $data['quantity_missing'] = $missingAmount;
54
55        return $data;
56    }
57
58    private function purchaseSolicitationData(): ?array
59    {
60        $number = data_get($this->integration?->parameters, 'purchase_solicitation_number');
61
62        if (!$this->hasSolicitationIdentifier($number)) {
63            return null;
64        }
65
66        return [
67            'branch'   => data_get($this->integration?->parameters, 'purchase_solicitation_branch'),
68            'number'   => (string) $number,
69            'sequence' => data_get($this->integration?->parameters, 'purchase_solicitation_sequence'),
70            'quantity' => is_null(data_get($this->integration?->parameters, 'purchase_solicitation_quantity'))
71                ? null
72                : (float) data_get($this->integration?->parameters, 'purchase_solicitation_quantity'),
73        ];
74    }
75
76    private function requestItemObservation(): ?string
77    {
78        $observation = data_get($this->integration?->parameters, 'obsEme');
79
80        if (is_null($observation)) {
81            return null;
82        }
83
84        $normalized = trim((string) $observation);
85
86        return $normalized === '' ? null : $normalized;
87    }
88
89    private function hasSolicitationIdentifier(mixed $value): bool
90    {
91        if (is_null($value)) {
92            return false;
93        }
94
95        $normalized = trim((string) $value);
96        return $normalized !== '' && $normalized !== '0';
97    }
98}