මොඩියුලය 8

ඉන්වෙන්ටරි සහ තොග කළමනාකරණය (Inventory & Stock Management)


1 තොග අඩු බවට ඇඟවීම් (Low-Stock Alerts) පෙන්වීම

ව්‍යාපාරයකට "භාණ්ඩ තොගයේ නොමැත" (Out of Stock) යනු අහිමි වූ ආදායමකි. මෙය වළක්වා ගැනීමට, භාණ්ඩයක් අවසන් වීමට පෙර ඒ බව කළමනාකරුට දැනුම් දිය යුතුය. මේ සඳහා අපි එක් එක් නිෂ්පාදනයට "නැවත ඇණවුම් කිරීමේ මට්ටමක්" (Re-order Level) හඳුන්වා දෙමු.

උදාහරණයක් ලෙස, කිරි පැකට් සඳහා re-order level එක 20 ලෙස සැකසූ විට, තොගයේ ප්‍රමාණය 20 හෝ ඊට අඩුවූ සැනින්, පද්ධතිය ඒ බව ඇඟවීමක් ලෙස පෙන්විය යුතුය.

දත්ත සමුදාය වෙනස් කිරීම:

පළමුව, අපගේ `products` වගුවට `reorder_level` නමින් නව තීරුවක් එක් කළ යුතුය.

ALTER TABLE `products` ADD `reorder_level` INT(11) NOT NULL DEFAULT 10 AFTER `quantity`;
SQL විමසුම:

තොග අඩු භාණ්ඩ සොයාගැනීමට මෙම සරල විමසුම භාවිතා කළ හැක.

SELECT name, quantity, reorder_level FROM products WHERE quantity <= reorder_level;
2 තොග නැවත පිරවීම (Restocking) සහ අතින් සකස් කිරීම

විකිණීමකදී තොගය ස්වයංක්‍රීයව අඩුවන බව අපි 6 වන මොඩියුලයේදී සකස් කළෙමු. එලෙසම, සැපයුම්කරුවන්ගෙන් නව තොග ලැබුණු විට, එම ප්‍රමාණය පද්ධතියට අතින් (manually) ඇතුළත් කිරීමට ක්‍රමයක් තිබිය යුතුය.

මෙම ක්‍රියාවලියේදී අදාළ නිෂ්පාදනයේ දැනට පවතින තොග ප්‍රමාණයට, 새로 ලැබුණු ප්‍රමාණය එකතු කරනු ලැබේ.

SQL විමසුම:

නිෂ්පාදනයක තොගය යාවත්කාලීන කිරීමට `UPDATE` විධානය භාවිතා කරයි.

-- Example: Adding 50 units to product with ID 1 UPDATE products SET quantity = quantity + 50 WHERE id = 1;

⭐ ප්‍රායෝගික පැවරුම: "ඉන්වෙන්ටරි තත්ත්ව" පිටුව සහ ඇඟවීම් නිර්මාණය කිරීම

මෙම කාර්යයේදී අපි තොග අඩු භාණ්ඩ පෙන්වන සහ නව තොග පහසුවෙන් එකතු කළ හැකි `inventory.php` නමින් පිටුවක් නිර්මාණය කරමු.

පියවර 1: දත්ත සමුදාය යාවත්කාලීන කිරීම

phpMyAdmin හි SQL ටැබ් එකට ගොස්, ඉහත පෙන්වා ඇති `ALTER TABLE` විධානය ක්‍රියාත්මක කර `products` වගුවට `reorder_level` තීරුව එක් කරන්න.

පියවර 2: `inventory.php` ගොනුව නිර්මාණය කිරීම

මෙම ගොනුවේ කොටස් දෙකක් ඇත: තොග අඩු භාණ්ඩ සඳහා විශේෂ ඇඟවීම් පුවරුවක් සහ සියලුම භාණ්ඩවල තොග කළමනාකරණයට වගුවක්.

<?php // inventory.php session_start(); // ... (Check login) ... include 'includes/db_connect.php'; // Fetch low stock items $low_stock_sql = "SELECT * FROM products WHERE quantity <= reorder_level"; $low_stock_result = mysqli_query($conn, $low_stock_sql); // Fetch all items for general management $all_products_sql = "SELECT * FROM products ORDER BY name"; $all_products_result = mysqli_query($conn, $all_products_sql); ?> <!DOCTYPE html> <html> <head><title>Inventory Management</title><!-- Bootstrap CSS --></head> <body> <div class="container"> <h1 class="my-4">Inventory & Stock Management</h1> <!-- Low Stock Alerts Section --> <div class="card low-stock-alert mb-4"> <div class="card-header"><h4>⚠️ Low Stock Alerts</h4></div> <div class="card-body"> <table class="table table-sm"> <!-- Table showing items from $low_stock_result --> </table> </div> </div> <!-- All Products Stock Management --> <h3>Manage All Products</h3> <table class="table table-bordered"> <thead><tr><th>Product</th><th>Current Stock</th><th>Add Stock</th></tr></thead> <tbody> <?php while($row = mysqli_fetch_assoc($all_products_result)): ?> <tr> <td><?php echo $row['name']; ?></td> <td><?php echo $row['quantity']; ?></td> <td> <form action="handle_add_stock.php" method="POST" class="d-flex"> <input type="hidden" name="product_id" value="<?php echo $row['id']; ?>"> <input type="number" name="qty_to_add" class="form-control form-control-sm me-2" style="width: 80px;" min="1" required> <button type="submit" class="btn btn-primary btn-sm">Add</button> </form> </td> </tr> <?php endwhile; ?> </tbody> </table> </div> </body> </html>

පියවර 3: තොග එකතු කිරීම හැසිරවීමට `handle_add_stock.php` ගොනුව

මෙම ගොනුව `inventory.php` හි ඇති ෆෝරමයෙන් එවන දත්ත ලබාගෙන, අදාළ නිෂ්පාදනයේ තොග ප්‍රමාණය යාවත්කාලීන කර නැවත `inventory.php` පිටුවට යොමු කරයි.

<?php // handle_add_stock.php session_start(); // ... (Check login) ... include 'includes/db_connect.php'; if ($_SERVER["REQUEST_METHOD"] == "POST") { $product_id = $_POST['product_id']; $qty_to_add = $_POST['qty_to_add']; if (!empty($product_id) && is_numeric($qty_to_add) && $qty_to_add > 0) { $sql = "UPDATE products SET quantity = quantity + ? WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("ii", $qty_to_add, $product_id); if ($stmt->execute()) { // Optional: Add to inventory_logs table here header("location: inventory.php?status=success"); } else { header("location: inventory.php?status=error"); } $stmt->close(); } } $conn->close(); ?>

මෙම පිටු නිර්මාණය කිරීමෙන්, කළමනාකරුවෙකුට තොග තත්ත්වය පිළිබඳව පැහැදිලි චිත්‍රයක් ලබා ගැනීමටත්, අවශ්‍ය ක්‍රියාමාර්ග ඉක්මනින් ගැනීමටත් හැකි වනු ඇත.

‹‹ පෙර මොඩියුලය (Module 7) ඊළඟ මොඩියුලය (Module 9) ››