Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 193 |
|
0.00% |
0 / 14 |
CRAP | |
0.00% |
0 / 1 |
| FacturasController | |
0.00% |
0 / 193 |
|
0.00% |
0 / 14 |
2162 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getInvoices | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| getAllInvoices | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
20 | |||
| getAllInvoicesExceptions | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
12 | |||
| setInvoiceReminderActive | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| getInvoiceReminderActive | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
12 | |||
| addInvoiceReminderException | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getInvoice | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
12 | |||
| addInvoiceNextReminder | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
72 | |||
| uploadToCyC | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| setAllMonthAdministratorsInvoices | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| sendAdministratorsInvoices | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| sendCallCenterInvoices | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |||
| handleGoogleAuthCallback | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers; |
| 4 | |
| 5 | use App\Models\TblCompanies; |
| 6 | use App\Models\TblInvoiceReminders; |
| 7 | use App\Models\TblInvoicesExceptions; |
| 8 | use App\Models\TblInvoicesNextReminders; |
| 9 | use App\Services\FacturasService; |
| 10 | use Illuminate\Http\Request; |
| 11 | use Illuminate\Support\Facades\Log; |
| 12 | |
| 13 | |
| 14 | class FacturasController extends GestionaController |
| 15 | { |
| 16 | private $facturasService; |
| 17 | public function __construct(FacturasService $facturasService) |
| 18 | { |
| 19 | $this->facturasService = $facturasService; |
| 20 | } |
| 21 | |
| 22 | public function getInvoices(Request $request) |
| 23 | { |
| 24 | $region = urldecode(@getallheaders()["Region"]); |
| 25 | |
| 26 | if($region === "Catalunya"){ |
| 27 | $region="Cataluña"; |
| 28 | } |
| 29 | |
| 30 | try { |
| 31 | $result = $this->facturasService->getInvoices($region); |
| 32 | |
| 33 | if(!$result["success"]){ |
| 34 | throw new \Exception($result["error"]); |
| 35 | } |
| 36 | |
| 37 | return response()->json([ |
| 38 | 'success' => true |
| 39 | ], 200); |
| 40 | |
| 41 | } catch (\Exception $e) { |
| 42 | /** @disregard P1014 */ |
| 43 | $e->exceptionCode = 'GET_INVOICES_EXCEPTION'; |
| 44 | report($e); |
| 45 | Log::channel('g3w_invoices')->error("Failed to get invoices: " . $e->getMessage()); |
| 46 | return response()->json([ |
| 47 | 'success' => false, |
| 48 | 'message' => $e->getMessage(), |
| 49 | ], 500); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public function getAllInvoices(Request $request) |
| 54 | { |
| 55 | try { |
| 56 | $region = urldecode(@getallheaders()["Region"]); |
| 57 | |
| 58 | if($region === "Catalunya"){ |
| 59 | $region="Cataluña"; |
| 60 | } |
| 61 | |
| 62 | $result = $this->facturasService->getAllInvoices($region); |
| 63 | |
| 64 | if($result->isEmpty()){ |
| 65 | throw new \Exception($result["error"]); |
| 66 | } |
| 67 | |
| 68 | return response()->json([ |
| 69 | 'success' => true, |
| 70 | 'invoices' => $result->original["invoices"], |
| 71 | 'pagination' => $result->original["pagination"] |
| 72 | ]); |
| 73 | |
| 74 | } catch (\Exception $e) { |
| 75 | /** @disregard P1014 */ |
| 76 | $e->exceptionCode = 'GET_ALL_INVOICES_EXCEPTION'; |
| 77 | report($e); |
| 78 | Log::channel('g3w_invoices')->error("Failed to get invoices: " . $e->getMessage()); |
| 79 | return response()->json([ |
| 80 | 'success' => false, |
| 81 | 'message' => $e->getMessage(), |
| 82 | ], 500); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public function getAllInvoicesExceptions(Request $request) |
| 87 | { |
| 88 | try { |
| 89 | |
| 90 | $result = $this->facturasService->getAllInvoicesExceptions(); |
| 91 | |
| 92 | if($result->isEmpty()){ |
| 93 | throw new \Exception($result["error"]); |
| 94 | } |
| 95 | |
| 96 | return response()->json([ |
| 97 | 'success' => true, |
| 98 | 'invoices' => $result->original["invoices"], |
| 99 | 'pagination' => $result->original["pagination"] |
| 100 | ]); |
| 101 | |
| 102 | } catch (\Exception $e) { |
| 103 | /** @disregard P1014 */ |
| 104 | $e->exceptionCode = 'GET_ALL_INVOICES_EXCEPTIONS_EXCEPTION'; |
| 105 | report($e); |
| 106 | Log::channel('g3w_invoices')->error("Failed to get invoices: " . $e->getMessage()); |
| 107 | return response()->json([ |
| 108 | 'success' => false, |
| 109 | 'message' => $e->getMessage(), |
| 110 | ], 500); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | public function setInvoiceReminderActive($value){ |
| 115 | try { |
| 116 | $region = urldecode(@getallheaders()["Region"]); |
| 117 | |
| 118 | if(!$region){ |
| 119 | throw new \Exception("No region provided"); |
| 120 | } |
| 121 | |
| 122 | TblCompanies::where('region', $region)->update(['invoice_reminder_active' => $value==="true"]); |
| 123 | |
| 124 | return response()->json([ |
| 125 | 'success' => true |
| 126 | ]); |
| 127 | |
| 128 | } catch (\Exception $e) { |
| 129 | /** @disregard P1014 */ |
| 130 | $e->exceptionCode = 'SET_INVOICE_REMINDER_ACTIVE_EXCEPTION'; |
| 131 | report($e); |
| 132 | Log::channel('g3w_invoices')->error("Failed to set invoice reminder active: " . $e->getMessage()); |
| 133 | return response()->json([ |
| 134 | 'success' => false, |
| 135 | 'message' => $e->getMessage(), |
| 136 | ], 500); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | public function getInvoiceReminderActive(){ |
| 141 | try { |
| 142 | $region = urldecode(@getallheaders()["Region"]); |
| 143 | |
| 144 | if(!$region){ |
| 145 | throw new \Exception("No region provided"); |
| 146 | } |
| 147 | |
| 148 | $tblCompanies = TblCompanies::where('region', $region)->first(); |
| 149 | |
| 150 | return response()->json([ |
| 151 | 'success' => true, |
| 152 | 'invoice_reminder_active' => $tblCompanies->invoice_reminder_active, |
| 153 | ]); |
| 154 | |
| 155 | } catch (\Exception $e) { |
| 156 | /** @disregard P1014 */ |
| 157 | $e->exceptionCode = 'GET_INVOICE_REMINDER_ACTIVE_EXCEPTION'; |
| 158 | report($e); |
| 159 | Log::channel('g3w_invoices')->error("Failed to set invoice reminder active: " . $e->getMessage()); |
| 160 | return response()->json([ |
| 161 | 'success' => false, |
| 162 | 'message' => $e->getMessage(), |
| 163 | ], 500); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | public function addInvoiceReminderException(Request $request){ |
| 168 | $data = $request->all(); |
| 169 | |
| 170 | $exception = TblInvoicesExceptions::create($data); |
| 171 | |
| 172 | return $exception; |
| 173 | } |
| 174 | |
| 175 | public function getInvoice($invoiceId){ |
| 176 | if(!$invoiceId){ |
| 177 | return response()->json([ |
| 178 | 'success' => false, |
| 179 | 'message' => "No invoice id provided", |
| 180 | "data" => null |
| 181 | ], 400); |
| 182 | } |
| 183 | |
| 184 | $data = TblInvoiceReminders::where('invoice_number', $invoiceId)->get(); |
| 185 | |
| 186 | if($data->isEmpty()){ |
| 187 | return response()->json([ |
| 188 | 'success' => false, |
| 189 | 'message' => "No invoices found", |
| 190 | "data" => null |
| 191 | ], 404); |
| 192 | } |
| 193 | |
| 194 | return response()->json([ |
| 195 | 'success' => true, |
| 196 | 'message' => "Invoices found", |
| 197 | 'data' => $data, |
| 198 | ]); |
| 199 | } |
| 200 | |
| 201 | public function addInvoiceNextReminder(Request $request){ |
| 202 | $data = $request->all(); |
| 203 | |
| 204 | if(!isset($data["region"]) || $data["region"] === null || $data["region"] === "") { |
| 205 | $data["region"] = urldecode(@getallheaders()["Region"]); |
| 206 | } |
| 207 | |
| 208 | /*$requiredFields = ['invoice_number', 'next_reminders']; |
| 209 | $missingFields = []; |
| 210 | |
| 211 | foreach ($requiredFields as $field) { |
| 212 | if(!isset($data[$field]) || $data[$field] === null || $data[$field] === "") { |
| 213 | $missingFields[] = $field; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if(!empty($missingFields)) { |
| 218 | return response()->json([ |
| 219 | 'success' => false, |
| 220 | 'message' => "Next fields are required: " . implode(', ', $missingFields), |
| 221 | 'data' => null |
| 222 | ], 400); |
| 223 | }*/ |
| 224 | |
| 225 | try { |
| 226 | $dataNewInvoiceNextReminder = TblInvoicesNextReminders::create($data); |
| 227 | |
| 228 | if(isset($data["payment_day"])){ |
| 229 | $idClient = (isset($data["id_client"])) ? $data["id_client"] : 0; |
| 230 | $invoiceNumber = (isset($data["invoice_number"])) ? $data["invoice_number"] : 0; |
| 231 | $this->facturasService->addToSheets($idClient, $invoiceNumber, $data["region"]); |
| 232 | } |
| 233 | |
| 234 | return response()->json([ |
| 235 | 'success' => true, |
| 236 | 'message' => "Invoice next reminder created successfully", |
| 237 | 'data' => $dataNewInvoiceNextReminder |
| 238 | ], 201); |
| 239 | |
| 240 | } catch (\Exception $e) { |
| 241 | /** @disregard P1014 */ |
| 242 | $e->exceptionCode = 'ADD_INVOICE_NEXT_REMINDER_EXCEPTION'; |
| 243 | report($e); |
| 244 | Log::error("Error creating invoice next reminder: " . $e->getMessage()); |
| 245 | |
| 246 | return response()->json([ |
| 247 | 'success' => false, |
| 248 | 'message' => "Error al crear el recordatorio", |
| 249 | 'data' => null |
| 250 | ], 500); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | public function uploadToCyC(Request $request){ |
| 255 | try{ |
| 256 | $region = urldecode(@getallheaders()["Region"]); |
| 257 | if(!$region){ |
| 258 | throw new \Exception("No region provided"); |
| 259 | } |
| 260 | |
| 261 | $result = $this->facturasService->sendCyCInvoices($region); |
| 262 | |
| 263 | if(!$result["success"]){ |
| 264 | throw new \Exception($result["error"]); |
| 265 | } |
| 266 | |
| 267 | return response()->json([ |
| 268 | 'success' => true |
| 269 | ], 200); |
| 270 | |
| 271 | |
| 272 | } catch (\Exception $e) { |
| 273 | /** @disregard P1014 */ |
| 274 | $e->exceptionCode = 'UPLOAD_TO_CYC_EXCEPTION'; |
| 275 | report($e); |
| 276 | Log::channel('third-party')->error("Fail in upload to CyC: " . $e->getMessage()); |
| 277 | return response()->json([ |
| 278 | 'success' => false, |
| 279 | 'message' => $e->getMessage(), |
| 280 | ], 500); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | public function setAllMonthAdministratorsInvoices(Request $request){ |
| 285 | try{ |
| 286 | $region = urldecode(@getallheaders()["Region"]); |
| 287 | if(!$region){ |
| 288 | throw new \Exception("No region provided"); |
| 289 | } |
| 290 | |
| 291 | $result = $this->facturasService->setAllMonthAdministratorsInvoices($region); |
| 292 | |
| 293 | if(!$result["success"]){ |
| 294 | throw new \Exception($result["error"]); |
| 295 | } |
| 296 | |
| 297 | return response()->json([ |
| 298 | 'success' => true |
| 299 | ], 200); |
| 300 | |
| 301 | } catch (\Exception $e) { |
| 302 | /** @disregard P1014 */ |
| 303 | $e->exceptionCode = 'SET_ALL_MONTH_ADMINISTRATORS_INVOICES_EXCEPTION'; |
| 304 | report($e); |
| 305 | Log::channel('third-party')->error("Fail in set all month administrators invoices: " . $e->getMessage()); |
| 306 | return response()->json([ |
| 307 | 'success' => false, |
| 308 | 'message' => $e->getMessage(), |
| 309 | ], 500); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | |
| 314 | |
| 315 | public function sendAdministratorsInvoices(Request $request){ |
| 316 | try{ |
| 317 | $region = urldecode(@getallheaders()["Region"]); |
| 318 | if(!$region){ |
| 319 | throw new \Exception("No region provided"); |
| 320 | } |
| 321 | |
| 322 | $result = $this->facturasService->sendAdministratorsInvoices($region); |
| 323 | |
| 324 | if(!$result["success"]){ |
| 325 | throw new \Exception($result["error"]); |
| 326 | } |
| 327 | |
| 328 | return response()->json([ |
| 329 | 'success' => true |
| 330 | ], 200); |
| 331 | |
| 332 | } catch (\Exception $e) { |
| 333 | /** @disregard P1014 */ |
| 334 | $e->exceptionCode = 'SEND_ADMINISTRATORS_INVOICES_EXCEPTION'; |
| 335 | report($e); |
| 336 | Log::channel('third-party')->error("Fail in send all month administrators invoices: " . $e->getMessage()); |
| 337 | return response()->json([ |
| 338 | 'success' => false, |
| 339 | 'message' => $e->getMessage(), |
| 340 | ], 500); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | public function sendCallCenterInvoices(Request $request){ |
| 345 | try{ |
| 346 | $result = $this->facturasService->sendCallCenterInvoices(); |
| 347 | |
| 348 | if(!$result["success"]){ |
| 349 | throw new \Exception($result["error"]); |
| 350 | } |
| 351 | |
| 352 | return response()->json([ |
| 353 | 'success' => true |
| 354 | ], 200); |
| 355 | |
| 356 | } catch (\Exception $e) { |
| 357 | /** @disregard P1014 */ |
| 358 | $e->exceptionCode = 'SEND_CALL_CENTER_INVOICES_EXCEPTION'; |
| 359 | report($e); |
| 360 | Log::channel('third-party')->error("Fail in send call center invoices: " . $e->getMessage()); |
| 361 | return response()->json([ |
| 362 | 'success' => false, |
| 363 | 'message' => $e->getMessage(), |
| 364 | ], 500); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | public function handleGoogleAuthCallback(Request $request){ |
| 369 | return $this->facturasService->handleGoogleAuthCallback($request); |
| 370 | } |
| 371 | |
| 372 | } |