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