Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 140 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| IfexController | |
0.00% |
0 / 140 |
|
0.00% |
0 / 5 |
702 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
| request | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
110 | |||
| syncQuotations | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
30 | |||
| createQuotation | |
0.00% |
0 / 47 |
|
0.00% |
0 / 1 |
30 | |||
| updateQuotation | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers; |
| 4 | |
| 5 | use App\Models\TblIfexLastUpdate; |
| 6 | use App\Models\TblQuotations; |
| 7 | use App\Services\PresupuestosService; |
| 8 | use Carbon\Carbon; |
| 9 | use Illuminate\Support\Facades\Log; |
| 10 | |
| 11 | class IfexController extends Controller |
| 12 | { |
| 13 | protected $presupuestosService; |
| 14 | |
| 15 | protected $statusNormalize; |
| 16 | |
| 17 | public function __construct(PresupuestosService $presupuestosService) |
| 18 | { |
| 19 | $this->presupuestosService = $presupuestosService; |
| 20 | |
| 21 | $this->statusNormalize = [ |
| 22 | 'Oportunidad' => 1, // Oportunidad => Nuevo |
| 23 | 'Pendiente' => 11, // Pendiente => Listo para enviar |
| 24 | 'Pendiente Cliente' => 2, // Pendiente Cliente => Enviado |
| 25 | 'Aceptado' => 3, // Aceptado => Aceptado |
| 26 | 'En ejecución' => 12, // En ejecución => En proceso |
| 27 | 'Rechazado' => 4, // Rechazado => Rechazado |
| 28 | 'Anulado' => 5, // Anulado => Anulado |
| 29 | 'Finalizado' => 3, // Finalizado => Aceptado |
| 30 | ]; |
| 31 | |
| 32 | } |
| 33 | |
| 34 | protected function request($method, $endpoint, array $data = []) |
| 35 | { |
| 36 | try { |
| 37 | // $this->getCredentials($region); |
| 38 | |
| 39 | /*if (!$this->apiUrl) { |
| 40 | throw new \Exception('API URL is not defined.'); |
| 41 | } |
| 42 | |
| 43 | if (!$this->accessToken) { |
| 44 | $this->auth($region); |
| 45 | }*/ |
| 46 | $apiUrl = 'https://titanapi-h9a2d7bvfqckfzep.spaincentral-01.azurewebsites.net/'; |
| 47 | |
| 48 | $url = "{$apiUrl}{$endpoint}"; |
| 49 | $curl = curl_init(); |
| 50 | |
| 51 | curl_setopt_array($curl, [ |
| 52 | CURLOPT_URL => $url, |
| 53 | CURLOPT_RETURNTRANSFER => true, |
| 54 | CURLOPT_CUSTOMREQUEST => strtoupper($method), |
| 55 | CURLOPT_HTTPHEADER => [ |
| 56 | 'x-api-key: 9E5reQeWEktx3Cd0uODpjTQTZlEEZjDW', |
| 57 | ], |
| 58 | ]); |
| 59 | |
| 60 | if (in_array(strtoupper($method), ['POST', 'PUT', 'PATCH']) && ! empty($data)) { |
| 61 | curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); |
| 62 | } |
| 63 | |
| 64 | $response = curl_exec($curl); |
| 65 | |
| 66 | if (curl_errno($curl)) { |
| 67 | throw new \Exception('cURL error: '.curl_error($curl)); |
| 68 | } |
| 69 | |
| 70 | $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
| 71 | curl_close($curl); |
| 72 | |
| 73 | /*if ($httpCode === 401) { |
| 74 | $this->auth($region); |
| 75 | return $this->request($method, $endpoint, $region, $data); |
| 76 | }*/ |
| 77 | |
| 78 | if ($httpCode < 200 || $httpCode >= 300) { |
| 79 | Log::channel('ifex')->error('API Response Error: '.$response); |
| 80 | |
| 81 | $errorResponse = json_decode($response, true); |
| 82 | $errorMessage = is_array($errorResponse) && isset($errorResponse['message']) |
| 83 | ? $errorResponse['message'] |
| 84 | : 'Request error: '.$response; |
| 85 | |
| 86 | throw new \Exception($errorMessage); |
| 87 | } |
| 88 | |
| 89 | $responseData = json_decode($response, true); |
| 90 | |
| 91 | if (json_last_error() !== JSON_ERROR_NONE) { |
| 92 | throw new \Exception('Invalid JSON response: '.$response); |
| 93 | } |
| 94 | |
| 95 | return $responseData; |
| 96 | } catch (\Exception $e) { |
| 97 | Log::channel('g3w')->error('Error in request: '.$e->getMessage()); |
| 98 | throw $e; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public function syncQuotations() |
| 103 | { |
| 104 | try { |
| 105 | $today = Carbon::now()->format('Y-m-d'); |
| 106 | $requestDate = "presupuestos/creados-editados?fechainicio={$today}&fechafin={$today}"; |
| 107 | |
| 108 | $quotationsToSync = json_decode(json_encode($this->request('GET', $requestDate))); |
| 109 | |
| 110 | foreach ($quotationsToSync as $quotation) { |
| 111 | if (TblQuotations::where('internal_quote_id', $quotation->codigo)->exists()) { |
| 112 | $this->updateQuotation($quotation); |
| 113 | } else { |
| 114 | $this->createQuotation($quotation); |
| 115 | } |
| 116 | |
| 117 | } |
| 118 | |
| 119 | TblIfexLastUpdate::where('id', 1)->first()->update( |
| 120 | [ |
| 121 | 'last_date' => Carbon::now()->format('Y-m-d H:i:s'), |
| 122 | ] |
| 123 | ); |
| 124 | |
| 125 | return ['success' => true, 'quotations' => $quotationsToSync]; |
| 126 | } catch (\Exception $e) { |
| 127 | /** @disregard P1014 */ |
| 128 | $e->exceptionCode = 'SYNC_QUOTATIONS_IFEX_EXCEPTION'; |
| 129 | report($e); |
| 130 | Log::channel('ifex')->error('Error updating quotations: '.$e->getMessage(), [ |
| 131 | 'exception' => $e, |
| 132 | ]); |
| 133 | |
| 134 | return response()->json([ |
| 135 | 'success' => false, |
| 136 | 'message' => 'Failed to update ifex quotations', |
| 137 | 'error' => $e->getMessage(), |
| 138 | 'error_code' => $e->exceptionCode ?: 500, |
| 139 | ], 500); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | public function createQuotation($quotation) |
| 144 | { |
| 145 | // instalaciones 6 25O |
| 146 | // correctivos 3 25C |
| 147 | // mantenimiento 1 25P |
| 148 | $type = null; |
| 149 | if (strpos($quotation->codigo, '25O') !== false) { |
| 150 | $type = 6; |
| 151 | } |
| 152 | |
| 153 | if (strpos($quotation->codigo, '25C') !== false) { |
| 154 | $type = 3; |
| 155 | } |
| 156 | |
| 157 | if (strpos($quotation->codigo, '25P') !== false) { |
| 158 | $type = 1; |
| 159 | } |
| 160 | if (strpos($quotation->codigo, 'G25') !== false) { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | $generateNumber = $this->presupuestosService->generateQuoteId(30); |
| 165 | |
| 166 | $g3wNewId = $generateNumber['id']; |
| 167 | $newQuoteId = $generateNumber['number']; |
| 168 | |
| 169 | $ifexArray = [ |
| 170 | 'internal_quote_id' => $quotation->codigo, |
| 171 | 'quote_id' => $newQuoteId, |
| 172 | 'company_id' => 30, |
| 173 | 'customer_type_id' => 2, |
| 174 | 'segment_id' => null, |
| 175 | 'budget_type_id' => $type, |
| 176 | 'budget_status_id' => $this->statusNormalize[$quotation->estado], |
| 177 | 'source_id' => 55, |
| 178 | 'client' => $quotation->clienteNombre ?? null, |
| 179 | 'phone_number' => $quotation->clienteTelefono ?? null, |
| 180 | 'email' => $quotation->clienteMail ?? null, |
| 181 | 'issue_date' => $quotation->fecha ?? null, |
| 182 | 'request_date' => $quotation->fechaAlta ?? null, |
| 183 | 'acceptance_date' => $quotation->fechaAceptacion ?? null, |
| 184 | 'amount' => $quotation->baseImponible ?? null, |
| 185 | 'last_follow_up_date' => null, |
| 186 | 'commercial' => 'Ifex', |
| 187 | 'created_at' => $quotation->fecha ?? null, |
| 188 | 'created_by' => 'Ifex', |
| 189 | 'has_attachment' => 0, |
| 190 | 'cost_of_labor' => 0, |
| 191 | 'total_cost_of_job' => 0, |
| 192 | 'invoice_margin' => 0, |
| 193 | 'margin_for_the_company' => 0, |
| 194 | 'revenue_per_date_per_worked' => 0, |
| 195 | 'gross_margin' => 100, |
| 196 | 'labor_percentage' => 0, |
| 197 | 'sync_import' => 1, |
| 198 | 'box_work_g3w' => null, |
| 199 | 'for_add' => 0, |
| 200 | 'for_approval' => 0, |
| 201 | 'reason_for_not_following_up_id' => 2, |
| 202 | ]; |
| 203 | |
| 204 | TblQuotations::where('id', $g3wNewId)->update($ifexArray); |
| 205 | } |
| 206 | |
| 207 | public function updateQuotation($quotation) |
| 208 | { |
| 209 | $fstQuotation = TblQuotations::where('internal_quote_id', $quotation->codigo)->first(); |
| 210 | |
| 211 | // instalaciones 6 25O |
| 212 | // correctivos 3 25C |
| 213 | // mantenimiento 1 25P |
| 214 | $type = null; |
| 215 | if (strpos($quotation->codigo, '25O') !== false) { |
| 216 | $type = 6; |
| 217 | } |
| 218 | |
| 219 | if (strpos($quotation->codigo, '25C') !== false) { |
| 220 | $type = 3; |
| 221 | } |
| 222 | |
| 223 | if (strpos($quotation->codigo, '25P') !== false) { |
| 224 | $type = 1; |
| 225 | } |
| 226 | if (strpos($quotation->codigo, 'G25') !== false) { |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | $ifexArray = [ |
| 231 | 'internal_quote_id' => $quotation->codigo, |
| 232 | 'budget_type_id' => $type, |
| 233 | 'budget_status_id' => $this->statusNormalize[$quotation->estado], |
| 234 | 'client' => $quotation->clienteNombre ?? null, |
| 235 | 'phone_number' => $quotation->clienteTelefono ?? null, |
| 236 | 'email' => $quotation->clienteMail ?? null, |
| 237 | 'issue_date' => $quotation->fecha ?? null, |
| 238 | 'request_date' => $quotation->fechaAlta ?? null, |
| 239 | 'acceptance_date' => $quotation->fechaAceptacion ?? null, |
| 240 | 'amount' => $quotation->baseImponible ?? null, |
| 241 | 'updated_at' => Carbon::now()->format('Y-m-d H:i:s'), |
| 242 | 'updated_by' => 'Ifex', |
| 243 | ]; |
| 244 | |
| 245 | $fstQuotation->update($ifexArray); |
| 246 | |
| 247 | } |
| 248 | } |