Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.61% |
69 / 77 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| StockIoInputInvoiceResource | |
89.61% |
69 / 77 |
|
50.00% |
2 / 4 |
19.40 | |
0.00% |
0 / 1 |
| toArray | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
3 | |||
| getDetailsReasonDevolution | |
82.14% |
23 / 28 |
|
0.00% |
0 / 1 |
8.36 | |||
| getRequestNumberByIntegrationParameters | |
78.57% |
11 / 14 |
|
0.00% |
0 / 1 |
5.25 | |||
| getDetailsReasonInvoice | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\StockIO\Resources; |
| 4 | |
| 5 | use App\Models\CostCenter\CostCenter; |
| 6 | use App\Models\CostCenter\ResourceCenter; |
| 7 | use App\Models\Integration\Integration; |
| 8 | use App\Models\StockIO\StockIO; |
| 9 | use App\Models\Supplier; |
| 10 | use App\Models\User; |
| 11 | use Domain\CostCenter\Resources\CostCenterResource; |
| 12 | use Domain\CostCenter\Resources\ResourceCenterResource; |
| 13 | use Illuminate\Http\Request; |
| 14 | use Illuminate\Http\Resources\Json\JsonResource; |
| 15 | |
| 16 | use Domain\Integration\Resources\IntegrationInvoiceResource; |
| 17 | use Domain\Integration\Resources\IntegrationResource; |
| 18 | use Domain\StockIO\Enums\StockInputReasonEnum; |
| 19 | use Domain\StockIO\Resources\StockIoProductInputInvoiceResource; |
| 20 | use Domain\User\Resources\UserResource; |
| 21 | |
| 22 | /** |
| 23 | * @property int $id |
| 24 | * @property int $type |
| 25 | * @property string $observation |
| 26 | * @property string $start_date |
| 27 | * @property string $end_date |
| 28 | * @property bool $urgent |
| 29 | * @property int $status |
| 30 | * @property $created_at |
| 31 | * @property string $updated_at |
| 32 | * @property int $reason |
| 33 | * @property int $sector |
| 34 | */ |
| 35 | class StockIoInputInvoiceResource extends JsonResource |
| 36 | { |
| 37 | public function toArray(Request $request): array |
| 38 | { |
| 39 | $details = $this->reason == StockInputReasonEnum::DEVOLUTION->value ? |
| 40 | $this->getDetailsReasonDevolution() : |
| 41 | $this->getDetailsReasonInvoice(); |
| 42 | |
| 43 | return [ |
| 44 | 'id' => $this->id, |
| 45 | 'type' => $this->type, |
| 46 | 'observation' => $this->observation, |
| 47 | 'start_date' => $this->start_date, |
| 48 | 'end_date' => $this->end_date, |
| 49 | 'urgent' => $this->urgent, |
| 50 | 'status' => $this->status, |
| 51 | 'created_at' => (string) $this->created_at, |
| 52 | 'updated_at' => (string) $this->updated_at, |
| 53 | 'reason' => $this->reason, |
| 54 | 'sector' => $this->sector, |
| 55 | 'details' => $details, |
| 56 | 'transferable' => $this->whenLoaded('transferable'), |
| 57 | 'integration' => $this->reason == StockInputReasonEnum::INVOICE->value ? |
| 58 | IntegrationInvoiceResource::make($this->whenLoaded('integration')) : |
| 59 | IntegrationResource::make($this->whenLoaded('integration')), |
| 60 | 'stock_io_products' => StockIoProductInputInvoiceResource::collection( |
| 61 | $this->whenLoaded('stockIOProducts') |
| 62 | ), |
| 63 | ]; |
| 64 | } |
| 65 | |
| 66 | private function getDetailsReasonDevolution(): array |
| 67 | { |
| 68 | $parameters = is_array($this->integration?->parameters) ? |
| 69 | $this->integration->parameters : |
| 70 | []; |
| 71 | |
| 72 | $responsible = null; |
| 73 | $costCenter = null; |
| 74 | $resourceCenter = null; |
| 75 | |
| 76 | if (isset($parameters['responsible_id'])) { |
| 77 | $responsible = User::find($parameters['responsible_id']); |
| 78 | } |
| 79 | |
| 80 | if (isset($parameters['cost_center_id'])) { |
| 81 | $costCenter = CostCenter::query() |
| 82 | ->with(['integration', 'resourceCenter.integration']) |
| 83 | ->find($parameters['cost_center_id']); |
| 84 | } |
| 85 | |
| 86 | if (isset($parameters['resource_center_id'])) { |
| 87 | $resourceCenter = ResourceCenter::query() |
| 88 | ->with('integration') |
| 89 | ->find($parameters['resource_center_id']); |
| 90 | |
| 91 | if (!$resourceCenter && $costCenter) { |
| 92 | $resourceCenter = $costCenter->resourceCenter |
| 93 | ->firstWhere('id', $parameters['resource_center_id']); |
| 94 | } |
| 95 | } elseif ($costCenter) { |
| 96 | $resourceCenter = $costCenter->resourceCenter->first(); |
| 97 | } |
| 98 | |
| 99 | return [ |
| 100 | 'request_number' => $this->getRequestNumberByIntegrationParameters($parameters), |
| 101 | 'source_stock_io_id' => $parameters['stock_io_id'] ?? null, |
| 102 | 'responsible' => UserResource::make($responsible) ?? null, |
| 103 | 'cost_center' => CostCenterResource::make($costCenter) ?? null, |
| 104 | 'resource_center' => ResourceCenterResource::make($resourceCenter) ?? null, |
| 105 | ]; |
| 106 | } |
| 107 | |
| 108 | private function getRequestNumberByIntegrationParameters(array $parameters): ?string |
| 109 | { |
| 110 | $requestNumber = $parameters['request_number'] ?? null; |
| 111 | if ($requestNumber) { |
| 112 | return (string) $requestNumber; |
| 113 | } |
| 114 | |
| 115 | if (empty($parameters['stock_io_id'])) { |
| 116 | return null; |
| 117 | } |
| 118 | |
| 119 | $stockIO = StockIO::query() |
| 120 | ->with('integration') |
| 121 | ->find($parameters['stock_io_id']); |
| 122 | |
| 123 | $sourceParameters = is_array($stockIO?->integration?->parameters) ? |
| 124 | $stockIO->integration->parameters : |
| 125 | []; |
| 126 | |
| 127 | return isset($sourceParameters['request_number']) ? |
| 128 | (string) $sourceParameters['request_number'] : |
| 129 | null; |
| 130 | } |
| 131 | |
| 132 | private function getDetailsReasonInvoice(): array |
| 133 | { |
| 134 | $supplier = null; |
| 135 | if (isset($this->integration->parameters['supplier_code'])) { |
| 136 | $supplierERP = Integration::query() |
| 137 | ->where('integrable_type', Supplier::class) |
| 138 | ->whereRaw("parameters->>'CODFOR' = ?", [(string) $this->integration->parameters['supplier_code']]) |
| 139 | ->first(); |
| 140 | |
| 141 | if (!empty($supplierERP)) { |
| 142 | $supplier = Supplier::find($supplierERP->integrable_id); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return [ |
| 147 | 'supplier' => $supplier, |
| 148 | ]; |
| 149 | } |
| 150 | } |