<?
php
namespace App\Http\Controllers;
use App\Models\Vente;
use App\Models\Client;
use App\Models\Produit;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\VentesExport;
use Barryvdh\DomPDF\PDF;
use App\Models\Setting;
//use PDF;
class VenteController extends Controller
{
public function index()
{
$ventes = Vente::with('client')->latest()->paginate(10);
return view('[Link]', compact('ventes'));
}
public function create()
{
$clients = Client::all();
$produits = Produit::where('quantite', '>', 0)->get();
return view('[Link]', compact('clients', 'produits'));
}
public function store(Request $request)
{
$request->validate([
'client_id' => 'required|exists:clients,id',
'produits' => 'required|array',
]);
$total = 0;
foreach ($request->produits as $id => $data) {
if (!isset($data['quantite']) || $data['quantite'] <= 0) {
continue;
}
$produit = Produit::find($id);
if ($data['quantite'] > $produit->quantite) {
return back()->withErrors([
'stock' => "Stock insuffisant pour {$produit->nom}"
]);
}
$total += $data['quantite'] * $produit->prix_vente;
}
$vente = Vente::create([
'client_id' => $request->client_id,
'total' => $total,
]);
foreach ($request->produits as $id => $data) {
if (!isset($data['quantite']) || $data['quantite'] <= 0) {
continue;
}
$produit = Produit::find($id);
$vente->produits()->attach($id, [
'quantite' => $data['quantite'],
'prix_unitaire' => $produit->prix_vente,
]);
$produit->decrement('quantite', $data['quantite']);
}
// C'EST ICI QUE TU METS L’AUDIT
audit('Création vente', $vente);
return redirect()->route('[Link]')
->with('success', 'Vente enregistrée avec succès.');
}
public function show(Vente $vente)
{
$vente->load('client', 'produits');
return view('[Link]', compact('vente'));
}
// On ajoute PDF $pdf en paramètre
public function facturePDF(Vente $vente, PDF $pdf)
{
$vente->load('client', 'produits');
$settings = Setting::first();
$sousTotal = $vente->total;
$tauxTVA = $settings->tva_rate / 100;
$montantTVA = $sousTotal * $tauxTVA;
$totalTTC = $sousTotal + $montantTVA;
$pdf->loadView('[Link]', compact(
'vente',
'settings',
'sousTotal',
'montantTVA',
'totalTTC'
));
return $pdf->download('facture_'.$vente->id.'.pdf');
}
public function export()
{
return Excel::download(new VentesExport, 'ventes_'.now()->format('Y-m-d').'.xlsx');
}
}
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Facture {{ $vente->id }}</title>
<style>
body {
font-family: DejaVu Sans, sans-serif;
font-size: 12px;
margin: 0;
padding: 20px;
}
h1 {
margin: 0;
}
.header {
text-align: center;
border-bottom: 2px solid #333;
padding-bottom: 15px;
margin-bottom: 30px;
}
.info {
width: 100%;
margin-bottom: 30px;
}
.info td {
vertical-align: top;
width: 50%;
}
.box-title {
font-weight: bold;
margin-bottom: 5px;
}
table {
width: 100%;
border-collapse: collapse;
}
th {
background-color: #333;
color: white;
padding: 8px;
text-align: left;
}
td {
padding: 6px;
border-bottom: 1px solid #ddd;
}
.text-right {
text-align: right;
}
.totals-table {
margin-top: 20px;
width: 300px;
float: right;
}
.totals-table td {
border: none;
padding: 5px;
}
.total-final {
font-size: 16px;
font-weight: bold;
}
.signature {
margin-top: 80px;
width: 100%;
}
.signature td {
width: 50%;
text-align: center;
}
.footer {
margin-top: 40px;
font-size: 10px;
text-align: center;
border-top: 1px solid #ddd;
padding-top: 15px;
clear: both;
}
</style>
</head>
<body>
{{-- HEADER --}}
<div class="header">
<h1>FACTURE</h1>
<p>
N° FAC-{{ date('Y') }}-{{ str_pad($vente->id, 5, '0', STR_PAD_LEFT) }}<br>
Date : {{ $vente->created_at->format('d/m/Y à H:i') }}
</p>
</div>
{{-- INFOS ENTREPRISE / CLIENT --}}
<table class="info">
<tr>
<td>
<div class="box-title">ÉMETTEUR</div>
<strong>Mon Entreprise</strong><br>
Adresse de l'entreprise<br>
Téléphone : +243 XX XXX XXX<br>
Email : contact@[Link]
</td>
<td>
<div class="box-title">CLIENT</div>
<strong>{{ $vente->client->nom ?? 'Client inconnu' }}</strong><br>
@if($vente->client->email)
Email : {{ $vente->client->email }}<br>
@endif
@if($vente->client->telephone)
Téléphone : {{ $vente->client->telephone }}
@endif
</td>
</tr>
</table>
{{-- TABLE PRODUITS --}}
<table>
<thead>
<tr>
<th>Produit</th>
<th>Quantité</th>
<th class="text-right">Prix Unitaire</th>
<th class="text-right">Total</th>
</tr>
</thead>
<tbody>
@forelse($vente->produits as $produit)
<tr>
<td>{{ $produit->nom }}</td>
<td>{{ $produit->pivot->quantite }}</td>
<td class="text-right">
{{ number_format($produit->pivot->prix_unitaire, 2, ',', ' ') }}
{{ $settings->currency }}
</td>
<td class="text-right">
{{ number_format($produit->pivot->quantite *
$produit->pivot->prix_unitaire, 2, ',', ' ') }} {{ $settings->currency }}
</td>
</tr>
@empty
<tr>
<td colspan="4" style="text-align:center;">
Aucun produit dans cette vente
</td>
</tr>
@endforelse
</tbody>
</table>
{{-- TOTAUX --}}
<table class="totals-table">
<tr>
<td class="text-right"><strong>Sous-total :</strong></td>
<td class="text-right">
{{ number_format($sousTotal, 2, ',', ' ') }} {{ $settings->currency }}
</td>
</tr>
<tr>
<td class="text-right"><strong>TVA (16%) :</strong></td>
<td class="text-right">
{{ number_format($montantTVA, 2, ',', ' ') }} {{ $settings->currency }}
</td>
</tr>
<tr>
<td class="text-right total-final">Total TTC :</td>
<td class="text-right total-final">
{{ number_format($totalTTC, 2, ',', ' ') }} {{ $settings->currency }}
</td>
</tr>
</table>
<div style="clear: both;"></div>
{{-- SIGNATURE --}}
<table class="signature">
<tr>
<td>
<strong>Signature Client</strong><br><br><br>
__________________________
</td>
<td>
<strong>Cachet & Signature</strong><br><br><br>
__________________________
</td>
</tr>
</table>
{{-- FOOTER --}}
<div class="footer">
Merci de votre confiance.<br>
Tout retard de paiement entraînera des pénalités conformément à la législation
en vigueur.<br>
Document généré le {{ now()->format('d/m/Y à H:i') }}.
</div>
</body>
</html>