35171-vm/products.php
Flatlogic Bot b88fb2e6d7 son
2025-10-24 09:40:09 +00:00

162 lines
7.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once 'db/config.php';
// Form gönderildi mi kontrolü
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$pdo = db();
// Yeni ürün ekleme işlemi
if (isset($_POST['name'])) {
$name = $_POST['name'] ?? '';
$type = $_POST['type'] ?? '';
$stock_quantity = $_POST['stock_quantity'] ?? 0;
$purchase_price = $_POST['purchase_price'] ?? 0.0;
$sale_price = $_POST['sale_price'] ?? 0.0;
$low_stock_threshold = $_POST['low_stock_threshold'] ?? 5;
if (!empty($name) && !empty($type) && is_numeric($stock_quantity) && is_numeric($purchase_price) && is_numeric($sale_price)) {
try {
$stmt = $pdo->prepare("INSERT INTO products (name, type, stock_quantity, purchase_price, sale_price, low_stock_threshold) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute([$name, $type, $stock_quantity, $purchase_price, $sale_price, $low_stock_threshold]);
$_SESSION['notification'] = "Ürün başarıyla eklendi!";
} catch (PDOException $e) {
$_SESSION['error'] = "Veritabanı hatası: " . $e->getMessage();
}
} else {
$_SESSION['error'] = "Lütfen tüm alanları doğru bir şekilde doldurun.";
}
}
// Ürün silme işlemi
if (isset($_POST['action']) && $_POST['action'] === 'delete') {
$product_id = $_POST['product_id'] ?? null;
if ($product_id) {
try {
$stmt = $pdo->prepare("DELETE FROM products WHERE id = ?");
$stmt->execute([$product_id]);
$_SESSION['notification'] = "Ürün başarıyla silindi.";
} catch (PDOException $e) {
$_SESSION['error'] = "Hata: Ürün silinemedi. İlişkili satış kayıtları olabilir.";
}
}
}
// PRG Pattern: Sayfayı yeniden yönlendirerek formun tekrar gönderilmesini engelle
header("Location: products.php");
exit();
}
// Ürünleri veritabanından çek
$products = [];
try {
$pdo = db();
$stmt = $pdo->query("SELECT * FROM products ORDER BY created_at DESC");
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
// Hata mesajını oturuma kaydet ve sayfayı yenilemekten kaçın
$_SESSION['error'] = "Ürünler çekilirken hata oluştu: " . $e->getMessage();
}
require_once 'partials/header.php';
?>
<h1 class="mb-4">Ürün Yönetimi</h1>
<!-- Yeni Ürün Ekleme Formu -->
<div class="card mb-4">
<div class="card-header">
Yeni Ürün Ekle
</div>
<div class="card-body">
<form action="products.php" method="POST">
<div class="row">
<div class="col-md-6 mb-3">
<label for="name" class="form-label">Ürün Adı</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="col-md-6 mb-3">
<label for="type" class="form-label">Ürün Tipi</label>
<select class="form-select" id="type" name="type" required>
<option value="jant">Jant</option>
<option value="lastik">Lastik</option>
<option value="akü">Akü</option>
</select>
</div>
</div>
<div class="row">
<div class="col-md-3 mb-3">
<label for="stock_quantity" class="form-label">Stok Miktarı</label>
<input type="number" class="form-control" id="stock_quantity" name="stock_quantity" required>
</div>
<div class="col-md-3 mb-3">
<label for="purchase_price" class="form-label">Alış Fiyatı</label>
<input type="number" step="0.01" class="form-control" id="purchase_price" name="purchase_price" required>
</div>
<div class="col-md-3 mb-3">
<label for="sale_price" class="form-label">Satış Fiyatı</label>
<input type="number" step="0.01" class="form-control" id="sale_price" name="sale_price" required>
</div>
<div class="col-md-3 mb-3">
<label for="low_stock_threshold" class="form-label">Düşük Stok Uyarısı</label>
<input type="number" class="form-control" id="low_stock_threshold" name="low_stock_threshold" value="5" required>
</div>
</div>
<button type="submit" class="btn btn-primary">Ürünü Ekle</button>
</form>
</div>
</div>
<!-- Ürün Listesi -->
<div class="card">
<div class="card-header">
Mevcut Ürünler
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Ad</th>
<th>Tip</th>
<th>Stok</th>
<th>Alış Fiyatı</th>
<th>Satış Fiyatı</th>
<th>Eklendiği Tarih</th>
<th>İşlemler</th>
</tr>
</thead>
<tbody>
<?php if (empty($products)):
<tr>
<td colspan="8" class="text-center">Henüz hiç ürün eklenmemiş.</td>
</tr>
<?php else: ?>
<?php foreach ($products as $product): ?>
<tr>
<td><?php echo htmlspecialchars($product['id']); ?></td>
<td><?php echo htmlspecialchars($product['name']); ?></td>
<td><?php echo htmlspecialchars($product['type']); ?></td>
<td><?php echo htmlspecialchars($product['stock_quantity']); ?></td>
<td><?php echo htmlspecialchars($product['purchase_price']); ?></td>
<td><?php echo htmlspecialchars($product['sale_price']); ?></td>
<td><?php echo htmlspecialchars($product['created_at']); ?></td>
<td>
<a href="edit-product.php?id=<?php echo $product['id']; ?>" class="btn btn-sm btn-warning">Düzenle</a>
<form action="products.php" method="POST" class="d-inline" onsubmit="return confirm('Bu ürünü silmek istediğinizden emin misiniz?');">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="product_id" value="<?php echo $product['id']; ?>">
<button type="submit" class="btn btn-sm btn-danger">Sil</button>
</form>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php require_once 'partials/footer.php'; ?>