D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home3
/
encodto1
/
inventory.tapaslights.com
/
Filename :
expense_management.php
back
Copy
<?php include 'config.php'; // Calculate total expense (all deduct transactions) $expenseQuery = "SELECT SUM(total_price) AS total_expense FROM material_transactions WHERE transaction_type = 'deduct'"; $expenseResult = $conn->query($expenseQuery); $total_expense = 0; if ($expenseResult && $row = $expenseResult->fetch_assoc()) { $total_expense = (float)$row['total_expense']; } ?> <!DOCTYPE html> <html lang="en"> <head> <?php include 'style.php'; ?> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> </head> <body> <div class="main-wrapper"> <?php include 'header.php'; ?> <?php include 'sidebar.php'; ?> <div class="page-wrapper"> <div class="content container-fluid"> <!-- Add Manual Transaction --> <div class="row"> <div class="col-lg-12"> <div class="card mb-4"> <div class="card-header"> <h5 class="card-title">Add Manual Transaction</h5> </div> <div class="card-body"> <form method="POST" action=""> <div class="row mb-3"> <div class="col-md-4"> <input type="text" name="material_name" class="form-control" placeholder="Material Name" required> </div> <div class="col-md-2"> <select name="transaction_type" class="form-control" required> <option value="">Type</option> <option value="add">Add</option> <option value="deduct">Deduct</option> </select> </div> <div class="col-md-2"> <input type="number" name="quantity" class="form-control" placeholder="Qty" min="1" required> </div> <div class="col-md-2"> <input type="text" name="unit" class="form-control" placeholder="Unit (e.g. kg, pcs)" required> </div> <div class="col-md-2"> <input type="number" name="price_per_unit" class="form-control" placeholder="Price/Unit" step="0.01" required> </div> </div> <div class="text-end"> <button class="btn btn-primary" type="submit" name="add_transaction">Add Transaction</button> </div> </form> <?php if (isset($_POST['add_transaction'])) { $material_name = $conn->real_escape_string($_POST['material_name']); $transaction_type = $_POST['transaction_type']; $quantity = (int)$_POST['quantity']; $unit = $conn->real_escape_string($_POST['unit']); $price_per_unit = (float)$_POST['price_per_unit']; $total_price = $quantity * $price_per_unit; $stmt = $conn->prepare("INSERT INTO material_transactions (material_name, transaction_type, quantity, unit, price_per_unit, total_price, created_at) VALUES (?, ?, ?, ?, ?, ?, NOW())"); $stmt->bind_param("ssissd", $material_name, $transaction_type, $quantity, $unit, $price_per_unit, $total_price); if ($stmt->execute()) { echo "<script>location.href='expense_management.php';</script>"; } else { echo "<div class='alert alert-danger mt-2'>Failed to add transaction: " . $stmt->error . "</div>"; } } ?> </div> </div> </div> </div> <!-- All Transactions --> <div class="row"> <div class="col-lg-12"> <div class="card mt-4"> <div class="card-header"> <h5 class="card-title">All Material Transactions</h5> </div> <div class="card-body table-responsive"> <?php $transactionQuery = $conn->query("SELECT * FROM material_transactions ORDER BY created_at DESC"); ?> <table class="table table-bordered table-hover"> <thead> <tr> <th>Date</th> <th>Material Name</th> <th>Type</th> <th>Quantity</th> <th>Unit</th> <th>Price/Unit</th> <th>Total Price</th> </tr> </thead> <tbody> <?php while ($row = $transactionQuery->fetch_assoc()): ?> <tr> <td><?= date('d M Y, h:i A', strtotime($row['created_at'])) ?></td> <td><?= htmlspecialchars($row['material_name']) ?></td> <td> <span class="badge bg-<?= $row['transaction_type'] == 'add' ? 'success' : 'danger' ?>"> <?= ucfirst($row['transaction_type']) ?> </span> </td> <td><?= $row['quantity'] ?></td> <td><?= htmlspecialchars($row['unit']) ?></td> <td>₹<?= number_format($row['price_per_unit'], 2) ?></td> <td>₹<?= number_format($row['total_price'], 2) ?></td> </tr> <?php endwhile; ?> </tbody> </table> </div> </div> </div> </div> <!-- Daily Expenditure Report --> <div class="row"> <div class="col-lg-12"> <div class="card mt-4"> <div class="card-header"> <h5 class="card-title">Daily Expenditure Report</h5> </div> <div class="card-body table-responsive"> <?php $dailyQuery = " SELECT DATE(created_at) AS trans_date, SUM(CASE WHEN transaction_type = 'deduct' THEN total_price ELSE 0 END) AS expenditure FROM material_transactions GROUP BY DATE(created_at) ORDER BY trans_date DESC "; $dailyResult = $conn->query($dailyQuery); ?> <table class="table table-bordered table-hover"> <thead> <tr> <th>Date</th> <th>Expenditure</th> </tr> </thead> <tbody> <?php while ($row = $dailyResult->fetch_assoc()): ?> <tr> <td><?= date('d M Y', strtotime($row['trans_date'])) ?></td> <td class="text-danger">₹<?= number_format($row['expenditure'], 2) ?></td> </tr> <?php endwhile; ?> </tbody> </table> </div> </div> </div> </div> <!-- Daily Expenditure Chart --> <div class="row"> <div class="col-lg-12"> <div class="card mt-4"> <div class="card-header d-flex justify-content-between align-items-center"> <h5 class="card-title mb-0">Daily Expenditure Overview</h5> </div> <div class="card-body"> <?php $expChartQuery = " SELECT DATE(created_at) AS trans_date, SUM(total_price) AS total FROM material_transactions WHERE transaction_type = 'deduct' GROUP BY DATE(created_at) ORDER BY trans_date "; $expResult = $conn->query($expChartQuery); $expDates = []; $expValues = []; while ($row = $expResult->fetch_assoc()) { $expDates[] = date('d M', strtotime($row['trans_date'])); $expValues[] = round($row['total'], 2); } ?> <?php if (count($expDates) > 0): ?> <div style="height: 350px;"> <canvas id="expenditureBarChart"></canvas> </div> <script> document.addEventListener("DOMContentLoaded", function() { const ctx = document.getElementById('expenditureBarChart').getContext('2d'); new Chart(ctx, { type: 'bar', data: { labels: <?= json_encode($expDates) ?>, datasets: [{ label: 'Expenditure (₹)', data: <?= json_encode($expValues) ?>, backgroundColor: 'rgba(255, 99, 132, 0.6)', borderColor: 'rgba(255, 99, 132, 1)', borderWidth: 1, borderRadius: 5, barPercentage: 0.6, categoryPercentage: 0.5 }] }, options: { responsive: true, plugins: { legend: { display: false }, tooltip: { callbacks: { label: function(context) { return '₹' + context.parsed.y.toLocaleString(); } } } }, scales: { x: { grid: { display: false }, title: { display: true, text: 'Date' } }, y: { beginAtZero: true, title: { display: true, text: 'Expenditure (₹)' }, ticks: { callback: function(value) { return '₹' + value; } } } } } }); }); </script> <?php else: ?> <div class="text-center text-muted">No expenditure data found to display chart.</div> <?php endif; ?> </div> </div> </div> </div> <!-- Raw Material Stock --> <div class="row"> <div class="col-lg-12"> <div class="card mt-4"> <div class="card-header"> <h5 class="card-title">Current Raw Material Stock</h5> </div> <div class="card-body"> <canvas id="stockChart" height="100"></canvas> <?php $stockQuery = " SELECT material_name, SUM(CASE WHEN transaction_type = 'add' THEN quantity ELSE 0 END) - SUM(CASE WHEN transaction_type = 'deduct' THEN quantity ELSE 0 END) AS current_stock FROM material_transactions GROUP BY material_name HAVING current_stock > 0 ORDER BY material_name "; $stockResult = $conn->query($stockQuery); $stockLabels = []; $stockValues = []; while ($row = $stockResult->fetch_assoc()) { $stockLabels[] = $row['material_name']; $stockValues[] = $row['current_stock']; } ?> <script> const stockCtx = document.getElementById('stockChart').getContext('2d'); new Chart(stockCtx, { type: 'bar', data: { labels: <?= json_encode($stockLabels) ?>, datasets: [{ label: 'Stock Qty', data: <?= json_encode($stockValues) ?>, backgroundColor: 'rgba(54, 162, 235, 0.6)', borderColor: 'rgba(54, 162, 235, 1)', borderWidth: 1 }] }, options: { scales: { y: { beginAtZero: true } } } }); </script> </div> </div> </div> </div> </div> </div> <?php include 'footer.php'; ?> </div> </body> </html>