34959-vm/credit_sales.php
Flatlogic Bot e428ea4534 v1
2025-10-14 17:08:20 +00:00

214 lines
10 KiB
PHP

<?php
require_once 'includes/header.php';
require_once 'db/config.php';
$pdoconn = db();
// Handle form submission for adding a new credit sale
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_credit_sale'])) {
$bunk_id = trim($_POST['bunk_id']);
$customer_id = trim($_POST['customer_id']);
$date = trim($_POST['date']);
$fuel_type_id = trim($_POST['fuel_type_id']);
$quantity = trim($_POST['quantity']);
$rate = trim($_POST['rate']);
$amount = $quantity * $rate;
if (!empty($bunk_id) && !empty($customer_id) && !empty($date) && !empty($fuel_type_id) && !empty($quantity) && !empty($rate)) {
try {
$pdoconn->beginTransaction();
// Insert into credit_sales
$sql = "INSERT INTO credit_sales (bunk_id, customer_id, date, fuel_type_id, quantity, rate, amount) VALUES (:bunk_id, :customer_id, :date, :fuel_type_id, :quantity, :rate, :amount)";
$stmt = $pdoconn->prepare($sql);
$stmt->execute([
':bunk_id' => $bunk_id,
':customer_id' => $customer_id,
':date' => $date,
':fuel_type_id' => $fuel_type_id,
':quantity' => $quantity,
':rate' => $rate,
':amount' => $amount
]);
// Update customer's outstanding balance
$update_sql = "UPDATE credit_customers SET outstanding_balance = outstanding_balance + :amount WHERE id = :customer_id";
$update_stmt = $pdoconn->prepare($update_sql);
$update_stmt->execute([':amount' => $amount, ':customer_id' => $customer_id]);
$pdoconn->commit();
$success_message = "Credit sale recorded successfully!";
} catch (PDOException $e) {
$pdoconn->rollBack();
$error_message = "Error recording sale: " . $e->getMessage();
}
} else {
$error_message = "All fields are required.";
}
}
// Fetch master data for dropdowns
try {
$bunks_stmt = $pdoconn->query("SELECT id, name FROM bunks ORDER BY name");
$bunks = $bunks_stmt->fetchAll(PDO::FETCH_ASSOC);
$customers_stmt = $pdoconn->query("SELECT id, name, bunk_id FROM credit_customers ORDER BY name");
$customers = $customers_stmt->fetchAll(PDO::FETCH_ASSOC);
$fuel_types_stmt = $pdoconn->query("SELECT id, name FROM fuel_types ORDER BY name");
$fuel_types = $fuel_types_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$bunks = [];
$customers = [];
$fuel_types = [];
$page_error = "Error fetching master data: " . $e->getMessage();
}
// Fetch recent credit sales
try {
$sales_stmt = $pdoconn->query("
SELECT cs.*, c.name as customer_name, b.name as bunk_name, ft.name as fuel_type_name
FROM credit_sales cs
JOIN credit_customers c ON cs.customer_id = c.id
JOIN bunks b ON cs.bunk_id = b.id
JOIN fuel_types ft ON cs.fuel_type_id = ft.id
ORDER BY cs.date DESC, cs.id DESC
LIMIT 20
");
$sales = $sales_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$sales = [];
$page_error = "Error fetching sales: " . $e->getMessage();
}
?>
<div class="container-fluid">
<div class="card">
<div class="card-header">
<h4 class="card-title">Record Credit Sale</h4>
</div>
<div class="card-body">
<?php if (isset($success_message)): ?>
<div class="alert alert-success"><?php echo $success_message; ?></div>
<?php endif; ?>
<?php if (isset($error_message)): ?>
<div class="alert alert-danger"><?php echo $error_message; ?></div>
<?php endif; ?>
<?php if (isset($page_error)): ?>
<div class="alert alert-danger"><?php echo $page_error; ?></div>
<?php endif; ?>
<div class="row">
<div class="col-md-4">
<h5>New Sale Entry</h5>
<form method="POST" action="credit_sales.php" id="credit_sale_form">
<div class="mb-3">
<label for="date" class="form-label">Date</label>
<input type="date" class="form-control" id="date" name="date" value="<?php echo date('Y-m-d'); ?>" required>
</div>
<div class="mb-3">
<label for="bunk_id" class="form-label">Bunk</label>
<select class="form-select" id="bunk_id" name="bunk_id" required>
<option value="">Select Bunk</option>
<?php foreach ($bunks as $bunk): ?>
<option value="<?php echo htmlspecialchars($bunk['id']); ?>"><?php echo htmlspecialchars($bunk['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="customer_id" class="form-label">Customer</label>
<select class="form-select" id="customer_id" name="customer_id" required>
<option value="">Select Customer</option>
<?php foreach ($customers as $customer): ?>
<option value="<?php echo htmlspecialchars($customer['id']); ?>" data-bunk-id="<?php echo $customer['bunk_id']; ?>">
<?php echo htmlspecialchars($customer['name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="fuel_type_id" class="form-label">Fuel Type</label>
<select class="form-select" id="fuel_type_id" name="fuel_type_id" required>
<option value="">Select Fuel Type</option>
<?php foreach ($fuel_types as $fuel_type): ?>
<option value="<?php echo htmlspecialchars($fuel_type['id']); ?>"><?php echo htmlspecialchars($fuel_type['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="quantity" class="form-label">Quantity (Ltr)</label>
<input type="number" step="0.01" class="form-control" id="quantity" name="quantity" required>
</div>
<div class="mb-3">
<label for="rate" class="form-label">Rate</label>
<input type="number" step="0.01" class="form-control" id="rate" name="rate" required>
</div>
<button type="submit" name="add_credit_sale" class="btn btn-primary">Record Sale</button>
</form>
</div>
<div class="col-md-8">
<h5>Recent Credit Sales</h5>
<div class="table-responsive">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Date</th>
<th>Customer</th>
<th>Bunk</th>
<th>Fuel</th>
<th>Qty</th>
<th>Rate</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php if (empty($sales)): ?>
<tr>
<td colspan="7" class="text-center">No credit sales recorded yet.</td>
</tr>
<?php else: ?>
<?php foreach ($sales as $sale): ?>
<tr>
<td><?php echo htmlspecialchars($sale['date']); ?></td>
<td><?php echo htmlspecialchars($sale['customer_name']); ?></td>
<td><?php echo htmlspecialchars($sale['bunk_name']); ?></td>
<td><?php echo htmlspecialchars($sale['fuel_type_name']); ?></td>
<td><?php echo htmlspecialchars($sale['quantity']); ?></td>
<td><?.php echo htmlspecialchars(number_format($sale['rate'], 2)); ?></td>
<td><?php echo htmlspecialchars(number_format($sale['amount'], 2)); ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const bunkSelect = document.getElementById('bunk_id');
const customerSelect = document.getElementById('customer_id');
const customerOptions = Array.from(customerSelect.options);
bunkSelect.addEventListener('change', function() {
const selectedBunkId = this.value;
// Clear current options
customerSelect.innerHTML = '<option value="">Select Customer</option>';
// Filter and add relevant customers
customerOptions.forEach(function(option) {
if (option.value && option.dataset.bunkId === selectedBunkId) {
customerSelect.add(option.cloneNode(true));
}
});
});
});
</script>
<?php require_once 'includes/footer.php'; ?>