251 lines
13 KiB
PHP
251 lines
13 KiB
PHP
<?php
|
||
require_once 'db/config.php';
|
||
|
||
$success_message = '';
|
||
$error_message = '';
|
||
|
||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||
try {
|
||
$pdo = db_connect();
|
||
|
||
// Basic validation
|
||
$required_fields = [
|
||
'acquisition_date', 'item_code', 'company_origin', 'category', 'sub_category',
|
||
'po_number', 'item_name', 'acquisition_price', 'item_user_name', 'user_division',
|
||
'user_department', 'location_city', 'location_building', 'location_area',
|
||
'current_condition', 'condition_at_acquisition', 'asset_status'
|
||
];
|
||
|
||
foreach ($required_fields as $field) {
|
||
if (empty($_POST[$field])) {
|
||
throw new Exception("Error: Field '{$field}' is required.");
|
||
}
|
||
}
|
||
|
||
$sql = "INSERT INTO inventory_items (
|
||
acquisition_date, item_code, company_origin, category, sub_category, po_number,
|
||
item_name, acquisition_price, item_user_name, user_division, user_department,
|
||
location_city, location_building, location_area, current_condition,
|
||
condition_at_acquisition, item_information, asset_status, transfer_status
|
||
) VALUES (
|
||
:acquisition_date, :item_code, :company_origin, :category, :sub_category, :po_number,
|
||
:item_name, :acquisition_price, :item_user_name, :user_division, :user_department,
|
||
:location_city, :location_building, :location_area, :current_condition,
|
||
:condition_at_acquisition, :item_information, :asset_status, :transfer_status
|
||
)";
|
||
|
||
$stmt = $pdo->prepare($sql);
|
||
|
||
$stmt->execute([
|
||
':acquisition_date' => $_POST['acquisition_date'],
|
||
':item_code' => $_POST['item_code'],
|
||
':company_origin' => $_POST['company_origin'],
|
||
':category' => $_POST['category'],
|
||
':sub_category' => $_POST['sub_category'],
|
||
':po_number' => $_POST['po_number'],
|
||
':item_name' => $_POST['item_name'],
|
||
':acquisition_price' => $_POST['acquisition_price'],
|
||
':item_user_name' => $_POST['item_user_name'],
|
||
':user_division' => $_POST['user_division'],
|
||
':user_department' => $_POST['user_department'],
|
||
':location_city' => $_POST['location_city'],
|
||
':location_building' => $_POST['location_building'],
|
||
':location_area' => $_POST['location_area'],
|
||
':current_condition' => $_POST['current_condition'],
|
||
':condition_at_acquisition' => $_POST['condition_at_acquisition'],
|
||
':item_information' => $_POST['item_information'] ?? '',
|
||
':asset_status' => $_POST['asset_status'],
|
||
':transfer_status' => $_POST['transfer_status'] ?? 'N/A',
|
||
]);
|
||
|
||
$success_message = "Inventory item added successfully!";
|
||
|
||
} catch (Exception $e) {
|
||
$error_message = "Error: " . $e->getMessage();
|
||
// Check for duplicate item_code
|
||
if ($e instanceof PDOException && $e->errorInfo[1] == 1062) {
|
||
$error_message = "Error: An item with this Item Code already exists.";
|
||
}
|
||
}
|
||
}
|
||
|
||
// Static options for dropdowns
|
||
$categories = ['Electronics', 'Furniture', 'Software', 'Office Supplies'];
|
||
$conditions = ['Good', 'Minor Damage', 'Major Damage', 'Broken'];
|
||
$conditions_acq = ['New', 'Used', 'Refurbished'];
|
||
$asset_statuses = ['Active', 'In Use', 'In Storage', 'Transferred', 'Disposed'];
|
||
$transfer_statuses = ['Pending', 'Approved', 'Rejected', 'Completed', 'N/A'];
|
||
$divisions = ['IT', 'HR', 'Finance', 'Operations'];
|
||
$departments = ['Help Desk', 'Recruitment', 'Accounting', 'Logistics'];
|
||
$cities = ['New York', 'London', 'Tokyo', 'Sydney'];
|
||
|
||
?>
|
||
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>Add New Inventory Item</title>
|
||
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&family=Roboto+Slab:wght@700&display=swap" rel="stylesheet">
|
||
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
||
</head>
|
||
<body>
|
||
<header class="app-header">
|
||
<h1>Inventory Management System</h1>
|
||
<nav>
|
||
<a href="add_item.php" class="active">Add New Item</a>
|
||
<a href="inventory_list.php">View Inventory</a>
|
||
</nav>
|
||
</header>
|
||
|
||
<main class="container">
|
||
<div class="form-container card">
|
||
<h2>Add a New Inventory Item</h2>
|
||
<p>Fill out the form below to add a new asset to the inventory.</p>
|
||
|
||
<?php if ($success_message): ?>
|
||
<div class="toast success"><?php echo htmlspecialchars($success_message); ?></div>
|
||
<?php endif; ?>
|
||
<?php if ($error_message): ?>
|
||
<div class="toast error"><?php echo htmlspecialchars($error_message); ?></div>
|
||
<?php endif; ?>
|
||
|
||
<form action="add_item.php" method="POST" id="add-item-form">
|
||
<div class="form-grid">
|
||
<!-- Column 1 -->
|
||
<div class="form-column">
|
||
<div class="form-group">
|
||
<label for="acquisition_date">Acquisition Date</label>
|
||
<input type="date" id="acquisition_date" name="acquisition_date" required>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="item_code">Item Code (Unique)</label>
|
||
<input type="text" id="item_code" name="item_code" required>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="company_origin">Company Origin (PT)</label>
|
||
<input type="text" id="company_origin" name="company_origin" required>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="category">Category</label>
|
||
<select id="category" name="category" required>
|
||
<?php foreach ($categories as $cat): ?>
|
||
<option value="<?php echo htmlspecialchars($cat); ?>"><?php echo htmlspecialchars($cat); ?></option>
|
||
<?php endforeach; ?>
|
||
</select>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="sub_category">Sub Category</label>
|
||
<input type="text" id="sub_category" name="sub_category" required>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="po_number">Purchase Order Number</label>
|
||
<input type="text" id="po_number" name="po_number" required>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Column 2 -->
|
||
<div class="form-column">
|
||
<div class="form-group">
|
||
<label for="item_name">Inventory Item Name</label>
|
||
<input type="text" id="item_name" name="item_name" required>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="acquisition_price">Acquisition Price</label>
|
||
<input type="number" step="0.01" id="acquisition_price" name="acquisition_price" required>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="item_user_name">Item User Name</label>
|
||
<input type="text" id="item_user_name" name="item_user_name" required>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="user_division">User Division</label>
|
||
<select id="user_division" name="user_division" required>
|
||
<?php foreach ($divisions as $div): ?>
|
||
<option value="<?php echo htmlspecialchars($div); ?>"><?php echo htmlspecialchars($div); ?></option>
|
||
<?php endforeach; ?>
|
||
</select>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="user_department">User Department</label>
|
||
<select id="user_department" name="user_department" required>
|
||
<?php foreach ($departments as $dep): ?>
|
||
<option value="<?php echo htmlspecialchars($dep); ?>"><?php echo htmlspecialchars($dep); ?></option>
|
||
<?php endforeach; ?>
|
||
</select>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="asset_status">Asset Status</label>
|
||
<select id="asset_status" name="asset_status" required>
|
||
<?php foreach ($asset_statuses as $status): ?>
|
||
<option value="<?php echo htmlspecialchars($status); ?>"><?php echo htmlspecialchars($status); ?></option>
|
||
<?php endforeach; ?>
|
||
</select>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Column 3 -->
|
||
<div class="form-column">
|
||
<div class="form-group">
|
||
<label for="location_city">Location – City</label>
|
||
<select id="location_city" name="location_city" required>
|
||
<?php foreach ($cities as $city): ?>
|
||
<option value="<?php echo htmlspecialchars($city); ?>"><?php echo htmlspecialchars($city); ?></option>
|
||
<?php endforeach; ?>
|
||
</select>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="location_building">Location – Building</label>
|
||
<input type="text" id="location_building" name="location_building" required>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="location_area">Location – Area / Room</label>
|
||
<input type="text" id="location_area" name="location_area" required>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="current_condition">Current Condition</label>
|
||
<select id="current_condition" name="current_condition" required>
|
||
<?php foreach ($conditions as $cond): ?>
|
||
<option value="<?php echo htmlspecialchars($cond); ?>"><?php echo htmlspecialchars($cond); ?></option>
|
||
<?php endforeach; ?>
|
||
</select>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="condition_at_acquisition">Condition at Acquisition</label>
|
||
<select id="condition_at_acquisition" name="condition_at_acquisition" required>
|
||
<?php foreach ($conditions_acq as $cond): ?>
|
||
<option value="<?php echo htmlspecialchars($cond); ?>"><?php echo htmlspecialchars($cond); ?></option>
|
||
<?php endforeach; ?>
|
||
</select>
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="transfer_status">Transfer Status</label>
|
||
<select id="transfer_status" name="transfer_status" required>
|
||
<?php foreach ($transfer_statuses as $status): ?>
|
||
<option value="<?php echo htmlspecialchars($status); ?>"><?php echo htmlspecialchars($status); ?></option>
|
||
<?php endforeach; ?>
|
||
</select>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="form-group form-full-width">
|
||
<label for="item_information">Item Information (Notes)</label>
|
||
<textarea id="item_information" name="item_information" rows="4"></textarea>
|
||
</div>
|
||
|
||
<div class="form-actions">
|
||
<button type="submit" class="btn btn-primary">Add Item</button>
|
||
<button type="reset" class="btn btn-secondary">Clear Form</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
</main>
|
||
|
||
<footer class="app-footer">
|
||
<p>© <?php echo date("Y"); ?> Inventory Management System</p>
|
||
</footer>
|
||
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
||
</body>
|
||
</html>
|