93 lines
3.5 KiB
PHP
93 lines
3.5 KiB
PHP
<?php
|
|
// models/Department.php
|
|
require_once dirname(__DIR__) . '/config/database.php';
|
|
|
|
class Department {
|
|
private $conn;
|
|
private $table_name = "departments";
|
|
|
|
public function __construct() {
|
|
$this->conn = Database::getInstance();
|
|
$this->createTableIfNotExists();
|
|
}
|
|
|
|
private function createTableIfNotExists() {
|
|
$query = "CREATE TABLE IF NOT EXISTS `" . $this->table_name . "` (
|
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
|
`code` varchar(50) NOT NULL UNIQUE,
|
|
`name` varchar(255) NOT NULL,
|
|
`status` enum('active','inactive') DEFAULT 'active',
|
|
`building` varchar(255) DEFAULT NULL,
|
|
`floor` varchar(50) DEFAULT NULL,
|
|
`created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
|
|
PRIMARY KEY (`id`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
|
|
$this->conn->exec($query);
|
|
|
|
// Ensure status column exists for older database versions
|
|
try {
|
|
$this->conn->exec("ALTER TABLE `" . $this->table_name . "` ADD COLUMN `status` ENUM('active','inactive') DEFAULT 'active'");
|
|
} catch (PDOException $e) {
|
|
// Column already exists, ignore
|
|
}
|
|
|
|
// Add default data if empty
|
|
$stmt = $this->conn->query("SELECT COUNT(*) FROM " . $this->table_name);
|
|
if ($stmt->fetchColumn() == 0) {
|
|
$this->conn->exec("INSERT INTO `" . $this->table_name . "` (`name`) VALUES ('ER (ฉุกเฉิน)'), ('OPD1 (อายุรกรรม)'), ('IPD1 (ผู้ป่วยในชาย)'), ('X-Ray (รังสีวิทยา)'), ('OR (ห้องผ่าตัด)')");
|
|
}
|
|
}
|
|
|
|
public function getAllActive() {
|
|
$query = "SELECT * FROM " . $this->table_name . " WHERE status = 'active' ORDER BY name ASC";
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->execute();
|
|
return $stmt->fetchAll();
|
|
}
|
|
|
|
public function getById($id) {
|
|
if (!$id) return null;
|
|
$query = "SELECT * FROM " . $this->table_name . " WHERE id = :id LIMIT 1";
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->bindParam(':id', $id);
|
|
$stmt->execute();
|
|
|
|
if ($stmt->rowCount() > 0) {
|
|
return $stmt->fetch(PDO::FETCH_ASSOC);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function getAll() {
|
|
$query = "SELECT * FROM " . $this->table_name . " ORDER BY name ASC";
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->execute();
|
|
return $stmt->fetchAll();
|
|
}
|
|
|
|
public function add($name) {
|
|
$code = 'D' . strtoupper(substr(uniqid(), -5)); // Generate random code
|
|
$query = "INSERT INTO " . $this->table_name . " (code, name, status) VALUES (:code, :name, 'active')";
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->bindParam(':code', $code);
|
|
$stmt->bindParam(':name', $name);
|
|
return $stmt->execute();
|
|
}
|
|
|
|
public function update($id, $name, $status) {
|
|
$query = "UPDATE " . $this->table_name . " SET name = :name, status = :status WHERE id = :id";
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->bindParam(':name', $name);
|
|
$stmt->bindParam(':status', $status);
|
|
$stmt->bindParam(':id', $id);
|
|
return $stmt->execute();
|
|
}
|
|
|
|
public function delete($id) {
|
|
$query = "DELETE FROM " . $this->table_name . " WHERE id = :id";
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->bindParam(':id', $id);
|
|
return $stmt->execute();
|
|
}
|
|
}
|