Skip to content

Commit

Permalink
Merge pull request #25 from pamudusarasith/supplierdetails-page
Browse files Browse the repository at this point in the history
suppliers update function
  • Loading branch information
RSCooray authored Nov 28, 2024
2 parents e39a4a4 + df01225 commit e9479c6
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 21 deletions.
7 changes: 5 additions & 2 deletions app/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
'/users' => ['controller' => App\Controllers\UserController::class],
'/roles' => ['controller' => App\Controllers\RolesController::class],
'/discounts' => ['controller' => App\Controllers\Discounts::class],
'/suppliers' => ['controller' => App\Controllers\Suppliers::class],
'/orders' => ['controller' => App\Controllers\PurchaseOrders::class],
'/orders/add' => ['controller' => App\Controllers\PurchaseOrders::class, 'action' => 'addformview'],
'/orders/details' => ['controller' => App\Controllers\PurchaseOrders::class, 'action' => 'details'],
'/branch/add' => ['controller' => App\Controllers\Branches::class, 'action' => 'addformview'],
'/branches' => ['controller' => App\Controllers\Branches::class],
'/suppliers' => ['controller' => App\Controllers\Suppliers::class],
'/suppliers/edit' => ['controller' => App\Controllers\Suppliers::class, 'action' => 'edit'],
'/suppliers/add' => ['controller' => App\Controllers\Suppliers::class, 'action' => 'add'],
'/suppliers/details' => ['controller' => App\Controllers\Suppliers::class, 'action' => 'details'],
'/reports' => ['controller' => App\Controllers\Dashboard::class],
Expand All @@ -26,6 +27,7 @@
'/pos/search' => ['controller' => App\Controllers\POS::class, 'action' => 'search'],
'/reports' => ['controller' => App\Controllers\Reports::class],
'/categories' => ['controller' => App\Controllers\Categories::class]

],

'POST' => [
Expand All @@ -38,7 +40,8 @@
'/category/new' => ['controller' => App\Controllers\Products::class, 'action' => 'newCategory'],
'/customer/new' => ['controller' => App\Controllers\Customer::class, 'action' => 'newCustomer'],
'/suppliers/add' => ['controller' => App\Controllers\Suppliers::class, 'action' => 'addSupplier'],
'/suppliers/delete' => ['controller' => App\Controllers\Suppliers::class, 'action' => 'deleteSupplier']
'/suppliers/delete' => ['controller' => App\Controllers\Suppliers::class, 'action' => 'deleteSupplier'],
'/suppliers/update' => ['controller' => App\Controllers\Suppliers::class, 'action' => 'updateSupplier'],
]

];
62 changes: 62 additions & 0 deletions app/controllers/Suppliers.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,67 @@ public function deleteSupplier(): void
echo json_encode(['success' => false, 'message' => 'Failed to delete supplier']);
}
}

public function edit(): void
{
Utils::requireAuth();

// Retrieve supplier ID from the query parameter
$supplierID = $_GET['id'] ?? null;

if (!$supplierID) {
// Redirect if no ID is provided
header("Location: /suppliers");
exit;
}

// Fetch existing supplier details
$supplier = new App\Models\Supplier();
$supplierDetails = $supplier->getSupplierDetails($supplierID);

// If supplier not found, redirect or show error
if (!$supplierDetails) {
View::render('Template', [
'title' => 'Supplier Not Found',
'view' => 'Error',
'errorMessage' => 'Supplier not found.'
]);
return;
}

// Render the form with the existing supplier details
View::render('Template', [
'title' => 'Edit Supplier Details',
'view' => 'AddSupplierForm', // Reuse the same form view
'stylesheets' => ['suppliers'],
'supplier' => $supplierDetails // Pass details to populate form
]);
}

public function updateSupplier(): void
{
Utils::requireAuth();

$supplierID = $_POST['supplier-id'] ?? null;

if (!$supplierID) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Missing supplier ID']);
return;
}

$supplier = new App\Models\Supplier();
$result = $supplier->updateSupplier($supplierID, $_POST);

if ($result) {
// Redirect to suppliers page after update
header("Location: /suppliers/details?id=" . $supplierID);
exit;
} else {
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Failed to update supplier']);
}
}

}
?>
31 changes: 31 additions & 0 deletions app/models/Supplier.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,35 @@ public function getSupplierDetails(string $supplierID): ?array
return $supplier ?: null; // Return null if no supplier is found
}


public function updateSupplier(string $supplierID, array $data): bool
{
$stmt = $this->dbh->prepare("UPDATE `supplier_details`
SET `supplierName` = :supplierName,
`productCategories` = :productCategories,
`products` = :products,
`address` = :address,
`contactNo` = :contactNo,
`email` = :email,
`specialNotes` = :specialNotes
WHERE `supplierID` = :supplierID");

try {
return $stmt->execute([
'supplierID' => $supplierID,
'supplierName' => $data["supplier-name"],
'productCategories' => $data["product-categories"],
'products' => $data["products"],
'address' => $data["address"],
'contactNo' => $data["contact-no"],
'email' => $data["email"],
'specialNotes' => $data["special-notes"],
]);
} catch (\PDOException $e) {
error_log("Error updating supplier: " . $e->getMessage());
return false;
}
}


}
73 changes: 55 additions & 18 deletions app/views/AddSupplierForm.view.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
<!-- AddSupplierForm.view.php -->
<div class="container">
<h2>Add New Supplier</h2>
<!-- Dynamically set the title -->
<h2><?= isset($supplier) ? 'Edit Supplier Details' : 'Add New Supplier'; ?></h2>

<form id ="sup-form" class="form-container" action="/suppliers/add" method="POST">
<!-- Dynamically set the form action -->
<form id="sup-form" class="form-container" action="<?= isset($supplier) ? '/suppliers/update' : '/suppliers/add'; ?>" method="POST">
<!-- Supplier ID and Supplier Name -->
<div class="form-row">
<div class="form-group">
<label for="supplier-id">Supplier ID</label>
<input type="text" id="supplier-id" name="supplier-id" required>
<input
type="text"
id="supplier-id"
name="supplier-id"
value="<?= htmlspecialchars($supplier['supplierID'] ?? ''); ?>"
<?= isset($supplier) ? 'readonly' : 'required'; ?>
>
</div>
<div class="form-group">
<label for="supplier-name">Supplier Name</label>
<input type="text" id="supplier-name" name="supplier-name" required>
<input
type="text"
id="supplier-name"
name="supplier-name"
value="<?= htmlspecialchars($supplier['supplierName'] ?? ''); ?>"
required>
</div>
</div>

Expand All @@ -20,53 +32,78 @@
<div class="form-group">
<label for="product-categories">Product Categories</label>
<select id="product-categories" name="product-categories" required>
<option value="" disabled selected>Select a category</option>
<option value="Vegetables">Vegetable</option>
<option value="Fruits">Fruits</option>
<option value="Fish">Fish</option>
<option value="Meats">Meat</option>
<option value="Others">Others</option>
<option value="" disabled <?= empty($supplier['productCategories']) ? 'selected' : ''; ?>>Select a category</option>
<option value="Vegetables" <?= isset($supplier['productCategories']) && $supplier['productCategories'] == 'Vegetables' ? 'selected' : ''; ?>>Vegetables</option>
<option value="Fruits" <?= isset($supplier['productCategories']) && $supplier['productCategories'] == 'Fruits' ? 'selected' : ''; ?>>Fruits</option>
<option value="Fish" <?= isset($supplier['productCategories']) && $supplier['productCategories'] == 'Fish' ? 'selected' : ''; ?>>Fish</option>
<option value="Meats" <?= isset($supplier['productCategories']) && $supplier['productCategories'] == 'Meats' ? 'selected' : ''; ?>>Meats</option>
<option value="Others" <?= isset($supplier['productCategories']) && $supplier['productCategories'] == 'Others' ? 'selected' : ''; ?>>Others</option>
</select>
</div>
<div class="form-group">
<label for="products">Products</label>
<input type="text" id="products" name="products" required>
<input
type="text"
id="products"
name="products"
value="<?= htmlspecialchars($supplier['products'] ?? ''); ?>"
required>
</div>
</div>

<!-- Address -->
<div class="form-row">
<div class="form-group full-width">
<label for="address">Address</label>
<input type="text" id="address" name="address" required>
<input
type="text"
id="address"
name="address"
value="<?= htmlspecialchars($supplier['address'] ?? ''); ?>"
required>
</div>
</div>

<!-- Contact No and Email -->
<div class="form-row">
<div class="form-group">
<label for="contact-no">Contact No</label>
<input type="text" id="contact-no" name="contact-no" required>
<input
type="text"
id="contact-no"
name="contact-no"
value="<?= htmlspecialchars($supplier['contactNo'] ?? ''); ?>"
required>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
<input
type="email"
id="email"
name="email"
value="<?= htmlspecialchars($supplier['email'] ?? ''); ?>"
required>
</div>
</div>

<!-- Special Notes -->
<div class="form-row">
<div class="form-group full-width">
<label for="special-notes">Special Notes</label>
<input type="text" id="special-notes" name="special-notes">
<input
type="text"
id="special-notes"
name="special-notes"
value="<?= htmlspecialchars($supplier['specialNotes'] ?? ''); ?>">
</div>
</div>

<!-- Buttons -->
<div class="form-actions">
<button type="submit" class="save-btn">Save</button>
<button type="submit" class="save-btn">
<?= isset($supplier) ? 'Update Supplier' : 'Save'; ?>
</button>
<a href="/suppliers" class="cancel-btn">Cancel</a>
</div>
</form>
</div>
2 changes: 1 addition & 1 deletion app/views/SupplierDetails.view.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<!-- Action Buttons -->
<div class="action-btn">
<a href="/suppliers" class="btn-update">Update</a>
<a href="/suppliers/edit?id=<?= htmlspecialchars($supplier['supplierID']); ?>" class="btn-update">Update</a>
<a href="/suppliers" class="btn-cancel">Cancel</a>
</div>
</div>
Expand Down

0 comments on commit e9479c6

Please sign in to comment.