Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 96
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
WorkService
0.00% covered (danger)
0.00%
0 / 96
0.00% covered (danger)
0.00%
0 / 2
552
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getG3wTasksExecuted
0.00% covered (danger)
0.00%
0 / 95
0.00% covered (danger)
0.00%
0 / 1
506
1<?php
2
3namespace App\Services;
4
5use App\Models\TblCompanies;
6use App\Models\TblQuotations;
7use Illuminate\Support\Facades\DB;
8
9class WorkService extends GestionaService
10{
11    public function __construct()
12    {
13        parent::__construct();
14    }
15
16    public function getG3wTasksExecuted($region, $isUpdate = null)
17    {
18        /**
19         * 27    MDG - BCN - GEN
20         * 28    MDG - MAD - GEN
21         * 29    MDG - VAL - GEN
22         * 47    MDG - ALM
23         * 49    MDG - ZAR
24         * 51    MDG - GUA
25         * 52    MDG - VALL
26         */
27        $sources = [27, 28, 29, 47, 49, 51, 52];
28        $totalFst = 0;
29        $totalG3w = 0;
30        $totalFacturado = 0;
31        $totalCerrado = 0;
32        $totalFinalizado = 0;
33        $totalVerificado = 0;
34        $idsFacturado = null;
35        $idsFinalizado = null;
36        $idsVerificado = null;
37        $idsCerrado = null;
38        $idsTotalExecuted = null;
39
40        $quotations = null;
41        if ($region === 'All') {
42            $quotations = TblQuotations::where('budget_status_id', 3)
43                ->whereIn('source_id', $sources)
44                ->get();
45        } else {
46            $company = TblCompanies::where('region', $region)->first();
47            $quotations = TblQuotations::where('budget_status_id', 3)
48                ->where('company_id', $company->company_id)
49                ->whereIn('source_id', $sources)
50                ->get();
51        }
52
53        $internalIdsGroupByregion = [];
54
55        $totalFst = count($quotations);
56
57        foreach ($quotations as $quotation) {
58            $companyId = $quotation->company_id;
59            $boxWorkG3w = (int) $quotation->box_work_g3w;
60
61            if (! isset($internalIdsGroupByregion[$companyId])) {
62                $internalIdsGroupByregion[$companyId] = [];
63            }
64
65            $internalIdsGroupByregion[$companyId][] = $boxWorkG3w;
66        }
67
68        foreach ($internalIdsGroupByregion as $companyId => $boxWorkG3w) {
69            $regionCall = TblCompanies::where('company_id', $companyId)->where('g3W_active', 1)->first();
70
71            if (! $regionCall) {
72                continue;
73            }
74
75            $dataToSend = [
76                'ids' => $boxWorkG3w,
77            ];
78
79            $worksStatus = $this->request('post', 'trabajo/estados', $regionCall->region, $dataToSend);
80            $totalG3w = $totalG3w + count($worksStatus);
81
82            foreach ($worksStatus as $workstatus) {
83
84                if ($isUpdate != null) {
85                    $exists = DB::table('tbl_box_work_g3w_mapping')
86                        ->where('box_work_g3w', $workstatus['ID'])
87                        ->first();
88
89                    if ($exists != null) {
90                        DB::table('tbl_box_work_g3w_mapping')
91                            ->where('box_work_g3w', $workstatus['ID'])
92                            ->update([
93                                'work_status' => $workstatus['estado'],
94                                'verified_attribute' => $workstatus['verificado'] === 'true' ? 1 : 0,
95                                'closed_attribute' => $workstatus['cerrado'] === 'true' ? 1 : 0,
96                                'updated_at' => now(),
97                            ]);
98                    } else {
99                        DB::table('tbl_box_work_g3w_mapping')->insert([
100                            'box_work_g3w' => $workstatus['ID'],
101                            'work_status' => $workstatus['estado'],
102                            'verified_attribute' => $workstatus['verificado'] === 'true' ? 1 : 0,
103                            'closed_attribute' => $workstatus['cerrado'] === 'true' ? 1 : 0,
104                        ]);
105                    }
106
107                } else {
108                    $idsTotalExecuted[] = TblQuotations::where('box_work_g3w', $workstatus['ID'])
109                        ->where('company_id', $companyId)
110                        ->first()->id;
111
112                    if ($workstatus['estado'] === 'Facturado') {
113                        $totalFacturado++;
114                        $idsFacturado[] = TblQuotations::where('box_work_g3w', $workstatus['ID'])
115                            ->where('company_id', $companyId)
116                            ->first()->id;
117                    }
118
119                    if ($workstatus['estado'] === 'Finalizado') {
120                        $totalFinalizado++;
121                        $idsFinalizado[] = TblQuotations::where('box_work_g3w', $workstatus['ID'])
122                            ->where('company_id', $companyId)
123                            ->first()->id;
124                    }
125
126                    if ($workstatus['verificado'] === 'true') {
127                        $totalVerificado++;
128                        $idsVerificado[] = TblQuotations::where('box_work_g3w', $workstatus['ID'])
129                            ->where('company_id', $companyId)
130                            ->first()->id;
131                    }
132
133                    if ($workstatus['cerrado'] === 'true') {
134                        $totalCerrado++;
135                        $idsCerrado[] = TblQuotations::where('box_work_g3w', $workstatus['ID'])
136                            ->where('company_id', $companyId)
137                            ->first()->id;
138                    }
139                }
140            }
141
142        }
143
144        return response()->json([
145            'totalFst' => $totalFst,
146            'totalG3w' => $totalG3w,
147            'totalFinalizado' => $totalFinalizado,
148            'totalFacturado' => $totalFacturado,
149            'totalCerrado' => $totalCerrado,
150            'totalVerificado' => $totalVerificado,
151            'idsTotalExecuted' => ! empty($idsTotalExecuted) ? implode(',', $idsTotalExecuted) : '',
152            'idsFacturado' => ! empty($idsFacturado) ? implode(',', $idsFacturado) : '',
153            'idsFinalizado' => ! empty($idsFinalizado) ? implode(',', $idsFinalizado) : '',
154            'idsVerificado' => ! empty($idsVerificado) ? implode(',', $idsVerificado) : '',
155            'idsCerrado' => ! empty($idsCerrado) ? implode(',', $idsCerrado) : '',
156        ]);
157    }
158}