MMCT TEAM
Server IP : 217.21.85.138  /  Your IP : 216.73.216.103
Web Server : LiteSpeed
System : Linux in-mum-web906.main-hosting.eu 4.18.0-553.37.1.lve.el8.x86_64 #1 SMP Mon Feb 10 22:45:17 UTC 2025 x86_64
User : u915722082 ( 915722082)
PHP Version : 7.4.33
Disable Function : system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : OFF  |  Python : OFF
Directory (0755) :  /home/u915722082/.nvm/../public_html/invoicebill/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/u915722082/.nvm/../public_html/invoicebill/billing.php
<?php 
include 'templates/header.php'; 
include 'templates/navbar.php'; 
require_once 'includes/db.php';

function safe_output($data) {
    return htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
}

$msg = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['product_id']) && isset($_POST['quantity']) && isset($_POST['packaging_type']) && isset($_POST['sale_date'])) {
        $product_id = intval($_POST['product_id']);
        $quantity = intval($_POST['quantity']);
        $packaging_type = $_POST['packaging_type'];
        $sale_date = $_POST['sale_date'];

        // Fetch finished good details
        $stmt = $conn->prepare("SELECT quantity FROM finished_goods WHERE id = ?");
        $stmt->bind_param("i", $product_id);
        $stmt->execute();
        $result = $stmt->get_result();
        $product = $result->fetch_assoc();
        $stmt->close();
        
        if ($product && $product['quantity'] >= $quantity) {
            // Deduct finished goods stock
            $stmt = $conn->prepare("UPDATE finished_goods SET quantity = quantity - ? WHERE id = ?");
            $stmt->bind_param("ii", $quantity, $product_id);
            $stmt->execute();
            $stmt->close();

            // Deduct packaging material stock (assuming one unit per sale, adjust as needed)
            $stmt = $conn->prepare("UPDATE packaging_materials SET quantity = quantity - 1 WHERE type = ? LIMIT 1");
            $stmt->bind_param("s", $packaging_type);
            $stmt->execute();
            $stmt->close();

            // Record the sale. Packaging used is stored as JSON.
            $packaging_used = json_encode(["packaging_type" => $packaging_type]);
            $stmt = $conn->prepare("INSERT INTO sales (product_id, quantity, packaging_used, sale_date) VALUES (?, ?, ?, ?)");
            $stmt->bind_param("iiss", $product_id, $quantity, $packaging_used, $sale_date);
            if ($stmt->execute()) {
                $msg = '<div class="alert alert-success">Sale recorded successfully!</div>';
            } else {
                $msg = '<div class="alert alert-danger">Error recording sale: ' . safe_output($conn->error) . '</div>';
            }
            $stmt->close();
        } else {
            $msg = '<div class="alert alert-warning">Insufficient finished goods stock!</div>';
        }
    } else {
        $msg = '<div class="alert alert-warning">Please fill in all required fields.</div>';
    }
}
?>

<div class="container mt-4">
    <h2>Billing & Sales</h2>
    <?php echo $msg; ?>
    <!-- Billing Form -->
    <form method="POST" action="billing.php">
        <div class="form-group">
            <label for="sale_date">Invoice Date</label>
            <input type="date" class="form-control" id="sale_date" name="sale_date" value="<?php echo date('Y-m-d'); ?>" required>
        </div>
        <div class="form-group">
            <label for="product_id">Select Finished Good</label>
            <select class="form-control" id="product_id" name="product_id" required>
                <?php 
                $result = $conn->query("SELECT * FROM finished_goods ORDER BY id DESC");
                while ($row = $result->fetch_assoc()):
                ?>
                <option value="<?php echo $row['id']; ?>">
                    <?php echo safe_output($row['name'] . " - " . $row['form'] . " - " . $row['size']); ?>
                </option>
                <?php endwhile; ?>
            </select>
        </div>
        <div class="form-group">
            <label for="quantity">Quantity Sold</label>
            <input type="number" class="form-control" id="quantity" name="quantity" required>
        </div>
        <div class="form-group">
            <label for="packaging_type">Packaging Type</label>
            <select class="form-control" id="packaging_type" name="packaging_type" required>
                <option value="Carton">Carton</option>
                <option value="Sack">Sack</option>
            </select>
        </div>
        <button type="submit" class="btn btn-primary">Record Sale</button>
    </form>
    
    <hr>
    <h3>Sales Records</h3> 
    <table class="table table-bordered">
        <thead class="thead-dark">
            <tr>
                <th>Sale ID</th>
                <th>Product</th>
                <th>Quantity</th>
                <th>Packaging</th>
                <th>Sale Date</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            $result = $conn->query("SELECT s.*, f.name, f.form, f.size FROM sales s JOIN finished_goods f ON s.product_id = f.id ORDER BY s.id DESC");
            while ($row = $result->fetch_assoc()):
            ?>
            <tr>
                <td><?php echo safe_output($row['id']); ?></td>
                <td><?php echo safe_output($row['name'] . " - " . $row['form'] . " - " . $row['size']); ?></td>
                <td><?php echo safe_output($row['quantity']); ?></td>
                <td><?php 
                      $packaging = json_decode($row['packaging_used'], true);
                      echo safe_output($packaging['packaging_type']);
                    ?>
                </td>
                <td><?php echo safe_output($row['sale_date']); ?></td>
            </tr>
            <?php endwhile; ?>
        </tbody>
    </table>
</div>

<?php include 'templates/footer.php'; ?>

MMCT - 2023