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