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