162 lines
6.5 KiB
PHP
162 lines
6.5 KiB
PHP
<?php
|
||
require_once 'db/config.php';
|
||
|
||
// Yeni satış ekleme işlemi
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_sale'])) {
|
||
$product_id = $_POST['product_id'] ?? null;
|
||
$quantity = $_POST['quantity'] ?? 0;
|
||
|
||
if (empty($product_id) || empty($quantity) || !is_numeric($quantity) || $quantity <= 0) {
|
||
$_SESSION['error'] = "Lütfen geçerli bir ürün ve adet girin.";
|
||
header("Location: sales.php");
|
||
exit();
|
||
}
|
||
|
||
$pdo = db();
|
||
$pdo->beginTransaction();
|
||
|
||
try {
|
||
// 1. Ürün bilgilerini ve stok durumunu kontrol et
|
||
$stmt = $pdo->prepare("SELECT name, sale_price, purchase_price, stock_quantity FROM products WHERE id = ? FOR UPDATE");
|
||
$stmt->execute([$product_id]);
|
||
$product = $stmt->fetch(PDO::FETCH_ASSOC);
|
||
|
||
if (!$product) {
|
||
throw new Exception("Ürün bulunamadı.");
|
||
}
|
||
|
||
if ($product['stock_quantity'] < $quantity) {
|
||
throw new Exception("Yetersiz stok! Mevcut stok: " . $product['stock_quantity']);
|
||
}
|
||
|
||
// 2. Satış hesaplamalarını yap
|
||
$total_amount = $product['sale_price'] * $quantity;
|
||
$profit_amount = ($product['sale_price'] - $product['purchase_price']) * $quantity;
|
||
|
||
// 3. 'sales' tablosuna ana satış kaydını ekle
|
||
$stmt = $pdo->prepare("INSERT INTO sales (total_amount, profit_amount) VALUES (?, ?)");
|
||
$stmt->execute([$total_amount, $profit_amount]);
|
||
$sale_id = $pdo->lastInsertId();
|
||
|
||
// 4. 'sale_items' tablosuna satılan ürünü ekle
|
||
$stmt = $pdo->prepare("INSERT INTO sale_items (sale_id, product_id, quantity, unit_price, total_price) VALUES (?, ?, ?, ?, ?)");
|
||
$stmt->execute([$sale_id, $product_id, $quantity, $product['sale_price'], $total_amount]);
|
||
|
||
// 5. Ürün stoğunu güncelle
|
||
$stmt = $pdo->prepare("UPDATE products SET stock_quantity = stock_quantity - ? WHERE id = ?");
|
||
$stmt->execute([$quantity, $product_id]);
|
||
|
||
// Her şey yolundaysa işlemi onayla
|
||
$pdo->commit();
|
||
$_SESSION['notification'] = "Satış başarıyla kaydedildi.";
|
||
|
||
} catch (Exception $e) {
|
||
// Bir hata olursa tüm işlemleri geri al
|
||
$pdo->rollBack();
|
||
$_SESSION['error'] = "Hata: " . $e->getMessage();
|
||
}
|
||
|
||
header("Location: sales.php");
|
||
exit();
|
||
}
|
||
|
||
// Sayfa içeriğini hazırlama
|
||
$pdo = db();
|
||
|
||
// Form için ürünleri çek
|
||
$products_for_form = [];
|
||
try {
|
||
$products_stmt = $pdo->query("SELECT id, name, sale_price, stock_quantity FROM products WHERE stock_quantity > 0 ORDER BY name ASC");
|
||
$products_for_form = $products_stmt->fetchAll(PDO::FETCH_ASSOC);
|
||
} catch (PDOException $e) {
|
||
$_SESSION['error'] = "Ürünler getirilemedi: " . $e->getMessage();
|
||
}
|
||
|
||
// Görüntülemek için geçmiş satışları çek (JOIN ile)
|
||
$sales_list = [];
|
||
try {
|
||
$sales_stmt = $pdo->query("
|
||
SELECT si.id, p.name AS product_name, si.quantity, si.total_price, s.created_at AS sale_date
|
||
FROM sale_items si
|
||
JOIN sales s ON si.sale_id = s.id
|
||
JOIN products p ON si.product_id = p.id
|
||
ORDER BY s.created_at DESC
|
||
");
|
||
$sales_list = $sales_stmt->fetchAll(PDO::FETCH_ASSOC);
|
||
} catch (PDOException $e) {
|
||
$_SESSION['error'] = "Satışlar getirilemedi: " . $e->getMessage();
|
||
}
|
||
|
||
require_once 'partials/header.php';
|
||
?>
|
||
|
||
<h1 class="mb-4">Satış Yönetimi</h1>
|
||
|
||
<!-- Yeni Satış Ekleme Formu -->
|
||
<div class="card mb-4">
|
||
<div class="card-header">Yeni Satış Ekle</div>
|
||
<div class="card-body">
|
||
<form action="sales.php" method="POST">
|
||
<input type="hidden" name="add_sale" value="1">
|
||
<div class="row">
|
||
<div class="col-md-8 mb-3">
|
||
<label for="product_id" class="form-label">Ürün Seçin</label>
|
||
<select class="form-select" id="product_id" name="product_id" required>
|
||
<option value="">-- Bir Ürün Seçin --</option>
|
||
<?php foreach ($products_for_form as $product): ?>
|
||
<option value="<?php echo htmlspecialchars($product['id']); ?>">
|
||
<?php echo htmlspecialchars($product['name']); ?>
|
||
(Stok: <?php echo htmlspecialchars($product['stock_quantity']); ?> |
|
||
Fiyat: <?php echo htmlspecialchars(number_format($product['sale_price'], 2)); ?> TL)
|
||
</option>
|
||
<?php endforeach; ?>
|
||
</select>
|
||
</div>
|
||
<div class="col-md-4 mb-3">
|
||
<label for="quantity" class="form-label">Adet</label>
|
||
<input type="number" class="form-control" id="quantity" name="quantity" min="1" required>
|
||
</div>
|
||
</div>
|
||
<button type="submit" class="btn btn-primary">Satış Ekle</button>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Geçmiş Satışlar -->
|
||
<div class="card">
|
||
<div class="card-header">Geçmiş Satışlar</div>
|
||
<div class="card-body">
|
||
<div class="table-responsive">
|
||
<table class="table table-striped">
|
||
<thead>
|
||
<tr>
|
||
<th>ID</th>
|
||
<th>Ürün Adı</th>
|
||
<th>Adet</th>
|
||
<th>Toplam Fiyat</th>
|
||
<th>Satış Tarihi</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<?php if (empty($sales_list)): ?>
|
||
<tr>
|
||
<td colspan="5" class="text-center">Henüz hiç satış yapılmamış.</td>
|
||
</tr>
|
||
<?php else: ?>
|
||
<?php foreach ($sales_list as $sale): ?>
|
||
<tr>
|
||
<td><?php echo htmlspecialchars($sale['id']); ?></td>
|
||
<td><?php echo htmlspecialchars($sale['product_name']); ?></td>
|
||
<td><?php echo htmlspecialchars($sale['quantity']); ?></td>
|
||
<td><?php echo htmlspecialchars(number_format($sale['total_price'], 2)); ?> TL</td>
|
||
<td><?php echo htmlspecialchars($sale['sale_date']); ?></td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
<?php endif; ?>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<?php require_once 'partials/footer.php'; ?>
|