-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.php
More file actions
87 lines (75 loc) · 2.35 KB
/
Copy pathexport.php
File metadata and controls
87 lines (75 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/functions.php';
// ── Filtres optionnels (même que produits.php) ───────────────────
$search = trim($_GET['q'] ?? '');
$cat_filter = intval_safe($_GET['cat'] ?? 0);
$filter = $_GET['filter'] ?? '';
$where = ['p.actif = 1'];
$params = [];
if ($search) {
$where[] = '(p.nom LIKE ? OR p.reference LIKE ? OR p.description LIKE ?)';
$like = "%$search%";
$params = array_merge($params, [$like, $like, $like]);
}
if ($cat_filter) {
$where[] = 'p.categorie_id = ?';
$params[] = $cat_filter;
}
if ($filter === 'alerte') {
$where[] = 'p.quantite <= p.seuil_alerte';
}
$sql = "
SELECT
p.reference,
p.nom,
c.nom AS categorie,
p.quantite,
p.seuil_alerte,
p.unite,
p.emplacement,
p.fournisseur,
p.description,
p.updated_at
FROM produits p
LEFT JOIN categories c ON c.id = p.categorie_id
WHERE " . implode(' AND ', $where) . "
ORDER BY p.nom ASC
";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$produits = $stmt->fetchAll();
// ── Envoi CSV ─────────────────────────────────────────────────────
$filename = 'stock_it_' . date('Y-m-d_His') . '.csv';
header('Content-Type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Pragma: no-cache');
header('Expires: 0');
// BOM UTF-8 pour Excel
echo "\xEF\xBB\xBF";
$out = fopen('php://output', 'w');
// En-têtes colonnes
fputcsv($out, [
'Référence', 'Nom du produit', 'Catégorie',
'Quantité', 'Seuil alerte', 'Unité',
'État stock', 'Emplacement', 'Fournisseur',
'Description', 'Dernière mise à jour',
], ';');
foreach ($produits as $p) {
fputcsv($out, [
$p['reference'],
$p['nom'],
$p['categorie'] ?? '',
$p['quantite'],
$p['seuil_alerte'],
$p['unite'],
stock_label((int)$p['quantite'], (int)$p['seuil_alerte']),
$p['emplacement'] ?? '',
$p['fournisseur'] ?? '',
$p['description'] ?? '',
date_fr($p['updated_at']),
], ';');
}
fclose($out);
exit;