<?php
session_start();
require_once 'includes/auth.php';
requireTechnician();
require_once 'config/db.php';
date_default_timezone_set('Asia/Dhaka');
// Fetch product names only — NO stock quantity shown to technicians
$all_products = $pdo->query(
"SELECT id, name, unit FROM products ORDER BY name ASC"
)->fetchAll();
$success = '';
$error = '';
$form = ['product_id' => '', 'quantity' => '', 'notes' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$form['product_id'] = (int) ($_POST['product_id'] ?? 0);
$form['quantity'] = trim($_POST['quantity'] ?? '');
$form['notes'] = trim($_POST['notes'] ?? '');
$quantity = (int) $form['quantity'];
if ($form['product_id'] <= 0) {
$error = 'Please select a product.';
} elseif ($quantity <= 0) {
$error = 'Quantity must be a positive number.';
} else {
// Fetch product (validate it exists and has enough stock)
$prod_stmt = $pdo->prepare("SELECT id, name, unit, current_stock FROM products WHERE id = ?");
$prod_stmt->execute([$form['product_id']]);
$product = $prod_stmt->fetch();
if (!$product) {
$error = 'Selected product not found.';
} elseif ($quantity > $product['current_stock']) {
// Stock too low — tell tech to contact admin, but don't reveal exact stock number
$error = 'Not enough stock available for this quantity. Please contact your admin.';
} else {
// Deduct stock
$pdo->prepare("UPDATE products SET current_stock = current_stock - ? WHERE id = ?")
->execute([$quantity, $product['id']]);
// Log transaction with user_id
$pdo->prepare(
"INSERT INTO stock_transactions (product_id, user_id, quantity, type, notes)
VALUES (?, ?, ?, 'deducted', ?)"
)->execute([$product['id'], $_SESSION['user_id'], $quantity, $form['notes']]);
$success = "Usage submitted: {$quantity} {$product['unit']}(s) of \"{$product['name']}\".";
$form = ['product_id' => '', 'quantity' => '', 'notes' => ''];
}
}
}
include 'includes/header.php';
?>
<div class="row mb-3">
<div class="col">
<h4 class="page-title"><i class="bi bi-plus-circle me-2 text-primary"></i>Submit Usage</h4>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="tech_dashboard.php">My Dashboard</a></li>
<li class="breadcrumb-item active">Submit Usage</li>
</ol>
</nav>
</div>
</div>
<div class="row justify-content-center">
<div class="col-md-7 col-lg-6">
<!-- Info box for technicians -->
<div class="alert alert-info d-flex align-items-start gap-2 mb-4">
<i class="bi bi-info-circle-fill mt-1"></i>
<div>
<strong>How to use:</strong> Select the product you used, enter the quantity, and optionally
add a note (e.g., customer name, job location). You can edit a mistaken entry from your dashboard.
</div>
</div>
<div class="card">
<div class="card-header bg-primary text-white">
<i class="bi bi-clipboard-plus me-1"></i> Log Product Usage
</div>
<div class="card-body">
<?php if ($success !== ''): ?>
<div class="alert alert-success d-flex align-items-start gap-2">
<i class="bi bi-check-circle-fill mt-1"></i>
<div>
<?= htmlspecialchars($success) ?>
<div class="mt-2 d-flex gap-2 flex-wrap">
<a href="tech_usage.php" class="btn btn-sm btn-success">Submit Another</a>
<a href="tech_dashboard.php" class="btn btn-sm btn-outline-secondary">My Dashboard</a>
</div>
</div>
</div>
<?php endif; ?>
<?php if ($error !== ''): ?>
<div class="alert alert-danger d-flex align-items-center gap-2">
<i class="bi bi-exclamation-triangle-fill"></i>
<?= htmlspecialchars($error) ?>
</div>
<?php endif; ?>
<?php if (empty($all_products)): ?>
<div class="alert alert-warning">No products are available yet. Contact your admin.</div>
<?php else: ?>
<form method="POST" action="" novalidate>
<div class="mb-3">
<label for="product_id" class="form-label fw-semibold">
Product Used <span class="text-danger">*</span>
</label>
<select class="form-select" id="product_id" name="product_id" required>
<option value="">-- Select Product --</option>
<?php foreach ($all_products as $p): ?>
<option value="<?= $p['id'] ?>"
data-unit="<?= htmlspecialchars($p['unit']) ?>"
<?= (int) $form['product_id'] === (int) $p['id'] ? 'selected' : '' ?>>
<?= htmlspecialchars($p['name']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="quantity" class="form-label fw-semibold">
Quantity Used <span class="text-danger">*</span>
</label>
<div class="input-group">
<input type="number"
class="form-control"
id="quantity"
name="quantity"
value="<?= htmlspecialchars($form['quantity']) ?>"
min="1"
placeholder="e.g., 2"
required>
<span class="input-group-text" id="unit-label">unit(s)</span>
</div>
</div>
<div class="mb-4">
<label for="notes" class="form-label fw-semibold">Notes / Reason</label>
<input type="text"
class="form-control"
id="notes"
name="notes"
value="<?= htmlspecialchars($form['notes']) ?>"
placeholder="e.g., House #5 Mirpur, Customer: Rahim">
<div class="form-text">Optional — helps identify the job this was used for.</div>
</div>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary fw-semibold py-2">
<i class="bi bi-send me-1"></i> Submit Usage
</button>
<a href="tech_dashboard.php" class="btn btn-outline-secondary">Cancel</a>
</div>
</form>
<?php endif; ?>
</div>
</div>
</div>
</div>
<script>
(function () {
const select = document.getElementById('product_id');
const unitLabel = document.getElementById('unit-label');
if (!select || !unitLabel) return;
select.addEventListener('change', function () {
const opt = this.options[this.selectedIndex];
unitLabel.textContent = opt.dataset.unit ? opt.dataset.unit + '(s)' : 'unit(s)';
});
}());
</script>
<?php include 'includes/footer.php'; ?>