D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home3
/
encodto1
/
inventory.tapaslights.com
/
Filename :
add_material.php
back
Copy
<?php require 'config.php'; ?> <!DOCTYPE html> <html lang="en" data-layout="vertical" data-topbar="light" data-sidebar="light" data-sidebar-size="lg" data-sidebar-image="none"> <head> <?php include 'style.php'; ?> </head> <body> <div class="main-wrapper"> <?php include 'header.php'; ?> <?php include 'sidebar.php'; ?> <div class="page-wrapper"> <div class="content container-fluid"> <div class="row"> <div class="col-lg-12"> <div class="card"> <div class="card-header"> <h5 class="card-title">Add Material</h5> </div> <div class="card-body"> <form method="POST" action=""> <div class="input-block mb-3 row"> <label class="col-form-label col-md-2">Product Name</label> <div class="col-md-10"> <input type="text" name="product_name" class="form-control" placeholder="Enter Product Name" required> </div> </div> <div class="input-block mb-3 row"> <label class="col-form-label col-md-2">Material Type</label> <div class="col-md-10"> <input type="text" name="material_type" class="form-control" placeholder="Enter Material Type" required> </div> </div> <div class="input-block mb-3 row"> <label class="col-form-label col-md-2">Unit</label> <div class="col-md-10"> <select name="unit" class="form-control" required> <option value="">Select Unit</option> <option value="kg">kg</option> <option value="pcs">pcs</option> <option value="litre">litre</option> <option value="meter">meter</option> <option value="box">box</option> <option value="pack">pack</option> </select> </div> </div> <div class="input-block mb-3 row"> <label class="col-form-label col-md-2">Price/Unit</label> <div class="col-md-10"> <input type="number" name="price_per_unit" class="form-control" placeholder="Enter Price per Unit" step="0.01" required> </div> </div> <div class="input-block mb-3 row"> <label class="col-form-label col-md-2">Seller</label> <div class="col-md-10"> <select name="seller_id" class="form-control" required> <option value="">Select Seller</option> <?php $sellers = $conn->query("SELECT id, seller_name FROM sellers"); while ($row = $sellers->fetch_assoc()) { echo "<option value='{$row['id']}'>" . htmlspecialchars($row['seller_name']) . "</option>"; } ?> </select> </div> </div> <div class="input-block mb-3 row"> <label class="col-form-label col-md-2">Stock Quantity</label> <div class="col-md-10"> <input type="number" name="stock_quantity" class="form-control" placeholder="Enter Quantity" required> </div> </div> <div class="input-block mb-3 mb-0 row"> <div class="col-md-10 offset-md-2"> <div class="input-group mb-3 d-flex justify-content-end"> <button class="btn btn-success m-2" type="submit">Add Material</button> <button class="btn btn-danger m-2" type="reset">Reset</button> </div> </div> </div> </form> <?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Collect inputs $product_name = $_POST['product_name']; $material_type = $_POST['material_type']; $unit = $_POST['unit']; $price_per_unit = $_POST['price_per_unit']; $seller_id = $_POST['seller_id']; $stock_quantity = $_POST['stock_quantity']; $total_price = $price_per_unit * $stock_quantity; // Generate custom ID (c_id) $last = $conn->query("SELECT c_id FROM materials ORDER BY id DESC LIMIT 1"); if ($last && $last->num_rows > 0) { $last_cid = $last->fetch_assoc()['c_id']; $num = intval(substr($last_cid, 4)) + 1; $new_cid = 'tps_' . str_pad($num, 5, '0', STR_PAD_LEFT); } else { $new_cid = 'tps_00001'; } // Insert into materials table $stmt = $conn->prepare("INSERT INTO materials (c_id, product_name, material_type, unit, price_per_unit, seller_id, stock_quantity) VALUES (?, ?, ?, ?, ?, ?, ?)"); $stmt->bind_param("ssssddi", $new_cid, $product_name, $material_type, $unit, $price_per_unit, $seller_id, $stock_quantity); if ($stmt->execute()) { $material_id = $conn->insert_id; // Insert into material_transactions table $log_stmt = $conn->prepare("INSERT INTO material_transactions (material_id, c_id, material_name, transaction_type, quantity, unit, price_per_unit, total_price) VALUES (?, ?, ?, 'add', ?, ?, ?, ?)"); $log_stmt->bind_param("issdssd", $material_id, $new_cid, $product_name, $stock_quantity, $unit, $price_per_unit, $total_price); $log_stmt->execute(); echo "<script>alert('Material added and transaction logged successfully!');</script>"; } else { echo "<script>alert('Error: " . $stmt->error . "');</script>"; } } ?> </div> </div> </div> </div> </div> </div> </div> <?php include 'footer.php'; ?> </body> </html>