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