Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
RequestReportService
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 3
156
0.00% covered (danger)
0.00%
0 / 1
 complete
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
12
 summarized
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
12
 enrichStockIOReportData
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace Domain\Request\Reports;
4
5use App\Events\ServiceProcessed;
6use App\Models\CostCenter\CostCenter;
7use App\Models\PickingOrder\PickingOrder;
8use App\Models\User;
9
10use Carbon\Carbon;
11
12use Illuminate\Support\Facades\Auth;
13
14class RequestReportService
15{
16    /**
17     * @param PickingOrder $pickingOrder
18     * @param User $user
19     * @return array
20     */
21    public function complete(PickingOrder $pickingOrder, User $user): array
22    {
23        $stockIOs = [];
24        foreach ($pickingOrder->pickingOrderProduct()->get() as $pickingOrderProduct) {
25            $stockIO = $pickingOrderProduct->stockIOProduct->stockIO;
26
27            if (!isset($stockIOs[$stockIO->id])) {
28                $stockIO->products = [];
29                $stockIOs[$stockIO->id] = $stockIO;
30            }
31
32            $products = $stockIOs[$stockIO->id]->products;
33            $products[] = $pickingOrderProduct;
34            $stockIOs[$stockIO->id]->products = $products;
35            $this->enrichStockIOReportData($stockIOs[$stockIO->id]);
36        }
37        $stockIOs = array_values($stockIOs);
38
39        // Dispara um evento 'ServiceProcessed' para notificar que o serviço de relatório foi processado.
40        ServiceProcessed::dispatch(self::class, $user);
41        return [
42            'pickingOrder' => $pickingOrder,
43            'stockIOs' => $stockIOs,
44            'now' => Carbon::now()->format('d/m/Y H:i'),
45            'generatedBy' => Auth::user()?->getAttribute('name'),
46            'qrcode' => json_encode([
47                'type' => 'picking_order',
48                'id' => $pickingOrder->getKey()
49            ]),
50            'withItems' => true,
51        ];
52    }
53
54    /**
55     * @param PickingOrder $pickingOrder
56     * @param User $user
57     * @return array
58     */
59    public function summarized(PickingOrder $pickingOrder, User $user): array
60    {
61        $stockIOs = [];
62        foreach ($pickingOrder->pickingOrderProduct()->get() as $pickingOrderProduct) {
63            $stockIO = $pickingOrderProduct->stockIOProduct->stockIO;
64
65            if (!isset($stockIOs[$stockIO->id])) {
66                $stockIO->products = [];
67                $stockIOs[$stockIO->id] = $stockIO;
68            }
69
70            $this->enrichStockIOReportData($stockIOs[$stockIO->id]);
71        }
72        $stockIOs = array_values($stockIOs);
73
74        // Dispara um evento 'ServiceProcessed' para notificar que o serviço de relatório foi processado.
75        ServiceProcessed::dispatch(self::class, $user);
76        return [
77            'pickingOrder' => $pickingOrder,
78            'stockIOs' => $stockIOs,
79            'now' => Carbon::now()->format('d/m/Y H:i'),
80            'generatedBy' => Auth::user()?->getAttribute('name'),
81            'qrcode' => json_encode([
82                'type' => 'picking_order',
83                'id' => $pickingOrder->getKey()
84            ]),
85            'withItems' => false,
86        ];
87    }
88
89    private function enrichStockIOReportData(object $stockIO): void
90    {
91        $costCenterCode = $stockIO->integration?->parameters['cost_center'] ?? null;
92
93        $stockIO->cost_center = CostCenter::whereHas('integration', function ($query) use ($costCenterCode) {
94            $query->where('parameters->cost_center_code', $costCenterCode);
95        })->first();
96
97        $request = $stockIO->relationLoaded('request')
98            ? $stockIO->request
99            : $stockIO->request()->with('requester')->first();
100
101        $requester = $request?->requester;
102
103        $applicant = $request?->applicant;
104        if (is_string($applicant)) {
105            $decodedApplicant = json_decode($applicant, true);
106            $applicant = is_array($decodedApplicant) ? $decodedApplicant : null;
107        }
108
109        $stockIO->requester_code = $requester?->badge;
110        $stockIO->requester_name = $requester?->name;
111        $stockIO->applicant_badge = is_array($applicant) ? ($applicant['badge'] ?? null) : null;
112        $stockIO->applicant_name = is_array($applicant) ? ($applicant['name'] ?? null) : null;
113    }
114}