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