223 lines
7.3 KiB
PHP
223 lines
7.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\StoreInvoiceRequest;
|
|
use App\Http\Requests\UpdateInvoiceRequest;
|
|
use App\Http\Resources\InvoiceResource;
|
|
use App\Repositories\InvoiceRepositoryInterface;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use App\Mail\DocumentMail;
|
|
|
|
class InvoiceController extends Controller
|
|
{
|
|
public function __construct(
|
|
protected InvoiceRepositoryInterface $invoiceRepository
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Display a listing of invoices.
|
|
*/
|
|
public function index(): AnonymousResourceCollection|JsonResponse
|
|
{
|
|
try {
|
|
$invoices = $this->invoiceRepository->all();
|
|
return InvoiceResource::collection($invoices);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error fetching invoices: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la récupération des factures.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Store a newly created invoice.
|
|
*/
|
|
public function store(StoreInvoiceRequest $request): InvoiceResource|JsonResponse
|
|
{
|
|
try {
|
|
$invoice = $this->invoiceRepository->create($request->validated());
|
|
return new InvoiceResource($invoice);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error creating invoice: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
'data' => $request->validated(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la création de la facture.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified invoice.
|
|
*/
|
|
public function show(string $id): InvoiceResource|JsonResponse
|
|
{
|
|
try {
|
|
$invoice = $this->invoiceRepository->find($id);
|
|
|
|
if (! $invoice) {
|
|
return response()->json([
|
|
'message' => 'Facture non trouvée.',
|
|
], 404);
|
|
}
|
|
|
|
return new InvoiceResource($invoice);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error fetching invoice: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
'invoice_id' => $id,
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la récupération de la facture.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update the specified invoice.
|
|
*/
|
|
public function update(UpdateInvoiceRequest $request, string $id): InvoiceResource|JsonResponse
|
|
{
|
|
try {
|
|
$updated = $this->invoiceRepository->update($id, $request->validated());
|
|
|
|
if (! $updated) {
|
|
return response()->json([
|
|
'message' => 'Facture non trouvée ou échec de la mise à jour.',
|
|
], 404);
|
|
}
|
|
|
|
$invoice = $this->invoiceRepository->find($id);
|
|
return new InvoiceResource($invoice);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error updating invoice: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
'invoice_id' => $id,
|
|
'data' => $request->validated(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la mise à jour de la facture.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified invoice.
|
|
*/
|
|
public function destroy(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$deleted = $this->invoiceRepository->delete($id);
|
|
|
|
if (! $deleted) {
|
|
return response()->json([
|
|
'message' => 'Facture non trouvée ou échec de la suppression.',
|
|
], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'message' => 'Facture supprimée avec succès.',
|
|
], 200);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error deleting invoice: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
'invoice_id' => $id,
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la suppression de la facture.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create an invoice from a quote.
|
|
*/
|
|
public function createFromQuote(string $quoteId): InvoiceResource|JsonResponse
|
|
{
|
|
try {
|
|
$invoice = $this->invoiceRepository->createFromQuote($quoteId);
|
|
return new InvoiceResource($invoice);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error creating invoice from quote: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
'quote_id' => $quoteId,
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la création de la facture depuis le devis.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send the invoice by email to the client.
|
|
*/
|
|
public function sendByEmail(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$invoice = $this->invoiceRepository->find($id);
|
|
|
|
if (!$invoice) {
|
|
return response()->json(['message' => 'Facture non trouvée.'], 404);
|
|
}
|
|
|
|
if (!$invoice->client || !$invoice->client->email) {
|
|
return response()->json(['message' => 'Le client n\'a pas d\'adresse email.'], 422);
|
|
}
|
|
|
|
// Load lines to ensure they are available in the view
|
|
$invoice->load('lines');
|
|
|
|
// Generate PDF
|
|
$pdfContent = Pdf::loadView('pdf.invoice_pdf', ['invoice' => $invoice])->output();
|
|
|
|
// Send Email
|
|
Mail::to($invoice->client->email)->send(new DocumentMail($invoice, 'invoice', $pdfContent));
|
|
|
|
return response()->json([
|
|
'message' => 'La facture a été envoyée avec succès à ' . $invoice->client->email,
|
|
], 200);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error sending invoice email: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'invoice_id' => $id,
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de l\'envoi de l\'email.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
}
|