Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 130
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
PresupuestosController
0.00% covered (danger)
0.00%
0 / 130
0.00% covered (danger)
0.00%
0 / 7
552
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 syncByDate
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
20
 syncErrorBudgets
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
20
 syncBudgetsWorks
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
20
 syncById
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getCountFailedBudgets
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
42
 syncByIds
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2namespace App\Http\Controllers;
3
4use App\Models\TblCompanies;
5use App\Models\TblG3WOrdersUpdateLogs;
6use App\Models\TblOrdersUpdateLogs;
7use App\Services\GestionaService;
8use Illuminate\Http\Request;
9use App\Services\PresupuestosService;
10use Illuminate\Support\Facades\Log;
11use Symfony\Component\Process\Process;
12
13class PresupuestosController extends Controller
14{
15    protected $presupuestosService;
16    protected $gestionaService;
17
18    public function __construct(PresupuestosService $presupuestosService, GestionaService  $gestionaService)
19    {
20        $this->presupuestosService = $presupuestosService;
21        $this->gestionaService = $gestionaService;
22    }
23
24    /**
25     * Sincroniza presupuestos a partir de una fecha.
26     *
27     * @param Request $request
28     * @return \Illuminate\Http\JsonResponse
29     */
30    public function syncByDate(Request $request)
31    {
32        $date = escapeshellarg($request->input('fecha'));
33        $name = escapeshellarg(@getallheaders()["Name"]);
34        $region = escapeshellarg(@getallheaders()["Region"]);
35
36        if($region === "Catalunya"){
37            $region="Cataluña";
38        }
39
40        if ($this->gestionaService->getSyncStatus($region) === 1) {
41            return response()->json([
42                'success' => false,
43                'message' => 'Synchronization already in progress.',
44            ], 400);
45        }
46
47        try {
48            $phpBinary = '/usr/bin/php';
49
50            $artisanPath = escapeshellarg(base_path('artisan'));
51
52            $command = sprintf(
53                '%s %s quotations:sync %s %s %s > /dev/null 2>&1 &',
54                $phpBinary,
55                $artisanPath,
56                $date,
57                $name,
58                $region
59            );
60
61            exec($command, $output, $returnVar);
62            /*$comand = "cd /var/www/html && php artisan quotations:sync $date $name $region > /dev/null 2>&1 &";
63            exec($comand, $output, $returnVar);*/
64
65            return response()->json([
66                'success' => true,
67                'message' => 'Synchronization started in background.',
68            ]);
69        } catch (\Exception $e) {
70            /** @disregard P1014 */
71            $e->exceptionCode = 'SYNC_BY_DATE_EXCEPTION'; 
72            report($e);
73            $this->gestionaService->setSyncStatus(0, $region);
74            Log::channel('g3w')->error("Failed to start sync: " . $e->getMessage());
75            return response()->json([
76                'success' => false,
77                'message' => $e->getMessage(),
78            ], 500);
79        }
80    }
81
82    /**
83     * @return \Illuminate\Http\JsonResponse
84     */
85    public function syncErrorBudgets()
86    {
87        $name = escapeshellarg(@getallheaders()["Name"]);
88        $region = escapeshellarg(@getallheaders()["Region"]);
89
90        if($region === "Catalunya"){
91            $region="Cataluña";
92        }
93
94        if ($this->gestionaService->getSyncStatus($region) === 1) {
95            return response()->json([
96                'success' => false,
97                'message' => 'Synchronization already in progress.',
98            ], 400);
99        }
100
101        try {
102            $phpBinary = '/usr/bin/php';
103
104            $artisanPath = escapeshellarg(base_path('artisan'));
105
106            $command = sprintf(
107                '%s %s quotations:retry-failed %s %s > /dev/null 2>&1 &',
108                $phpBinary,
109                $artisanPath,
110                $name,
111                $region
112            );
113
114            exec($command, $output, $returnVar);
115            /*$comand = "cd /var/www/html && php artisan quotations:retry-failed $name $region > /dev/null 2>&1 &";
116            exec($comand, $output, $returnVar);*/
117
118            return response()->json([
119                'success' => true,
120                'message' => 'Synchronization started in background.',
121            ]);
122        } catch (\Exception $e) {
123            /** @disregard P1014 */
124            $e->exceptionCode = 'SYNC_ERROR_BUDGETS_EXCEPTION'; 
125            report($e);
126            $this->gestionaService->setSyncStatus(0, $region);
127            Log::channel('g3w')->error("Failed to start sync: " . $e->getMessage());
128            return response()->json([
129                'success' => false,
130                'message' => $e->getMessage(),
131            ], 500);
132        }
133
134    }
135
136    /**
137     * @return \Illuminate\Http\JsonResponse
138     */
139    public function syncBudgetsWorks()
140    {
141        $name = escapeshellarg(@getallheaders()["Name"]);
142        $region = escapeshellarg(@getallheaders()["Region"]);
143
144        if($region === "Catalunya"){
145            $region="Cataluña";
146        }
147
148        if ($this->gestionaService->getSyncStatus($region) === 1) {
149            return response()->json([
150                'success' => false,
151                'message' => 'Synchronization already in progress.',
152            ], 400);
153        }
154
155        try {
156            $phpBinary = '/usr/bin/php';
157
158            $artisanPath = escapeshellarg(base_path('artisan'));
159
160            $command = sprintf(
161                '%s %s quotations:sync-work %s %s > /dev/null 2>&1 &',
162                $phpBinary,
163                $artisanPath,
164                $name,
165                $region
166            );
167
168            exec($command, $output, $returnVar);
169            /*$comand = "cd /var/www/html && php artisan quotations:sync-work $name $region > /dev/null 2>&1 &";
170            exec($comand, $output, $returnVar);*/
171
172            return response()->json([
173                'success' => true,
174                'message' => 'Synchronization started in background.',
175            ]);
176        } catch (\Exception $e) {
177            /** @disregard P1014 */
178            $e->exceptionCode = 'SYNC_BUDGETS_WORKS_EXCEPTION'; 
179            report($e);
180            $this->gestionaService->setSyncStatus(0, $region);
181            Log::channel('g3w')->error("Failed to start sync: " . $e->getMessage());
182            return response()->json([
183                'success' => false,
184                'message' => $e->getMessage(),
185            ], 500);
186        }
187
188    }
189
190    /**
191     * Sincroniza un presupuesto por su ID.
192     *
193     * @param $id
194     * @return \Illuminate\Http\JsonResponse
195     */
196    public function syncById($id)
197    {
198        $region = urldecode(@getallheaders()["Region"]);
199        if($region === "Catalunya"){ $region = "Cataluña"; }
200        return response()->json($this->presupuestosService->syncById($id, $region));
201    }
202
203    /**
204     * Sincroniza un presupuesto por su ID.
205     *
206     * @param $id
207     * @return \Illuminate\Http\JsonResponse
208     */
209
210    public function getCountFailedBudgets()
211    {
212        $region = @getallheaders()["Region"];
213
214        if($region === "Catalunya" || $region === "'Catalunya'"){
215            $region="Cataluña";
216        }
217
218        $company = TblCompanies::where("region", $region)->first();
219
220        if(!$company){
221            return response()->json([
222                'countWarnings' => 0
223            ], 200);
224        }
225
226        $ids = TblG3WOrdersUpdateLogs::whereNotNull('sync_error_ids')
227            ->where('company_id', $company->company_id)
228            ->get(['sync_error_ids']);
229
230        $allSyncErrorIds = [];
231
232        foreach ($ids as $id) {
233            if (is_string($id->sync_error_ids)) {
234                $id->sync_error_ids = json_decode($id->sync_error_ids, true);
235                $allSyncErrorIds = array_merge($allSyncErrorIds, $id->sync_error_ids);
236            }
237        }
238
239        $allSyncErrorIds = array_unique($allSyncErrorIds);
240
241        return response()->json([
242            'count' => count($allSyncErrorIds)
243        ]);
244    }
245
246    public function syncByIds(Request $request){
247        $ids = $request['ids'];
248        $date = $request['date'];
249        $region = urldecode(@getallheaders()["Region"]);
250        $user = urldecode(@getallheaders()["Name"]);
251
252        if ($region === "Catalunya"){
253            $region = "Cataluña";
254        }
255
256        return response()->json($this->presupuestosService->syncByIds($ids, $region, $date, $user));
257    }
258}