430 lines
19 KiB
PHP
430 lines
19 KiB
PHP
<?php
|
|
namespace app\Controllers;
|
|
|
|
use app\Models\IncomeMasterModel;
|
|
use app\Models\DeductionMasterModel;
|
|
use app\Models\SystemSettingModel;
|
|
use app\Models\HrPersonModel;
|
|
use app\Models\OfficerTypeModel;
|
|
|
|
class SettingController extends Controller {
|
|
|
|
public function __construct() {
|
|
if (empty($_SESSION['user_id'])) {
|
|
$_SESSION['error'] = 'กรุณาเข้าสู่ระบบก่อนใช้งาน';
|
|
header('Location: ' . BASE_URL . '/login');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// --- Income Methods ---
|
|
|
|
public function income() {
|
|
$incomeModel = new IncomeMasterModel();
|
|
$incomes = $incomeModel->getAllIncomes();
|
|
|
|
$this->view('settings/income', [
|
|
'title' => 'จัดการรายการรายรับ | ' . APP_NAME,
|
|
'activeMenu' => 'setting_income',
|
|
'incomes' => $incomes
|
|
]);
|
|
}
|
|
|
|
public function storeIncome() {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$data = [
|
|
'code' => $_POST['code'] ?? '',
|
|
'name' => $_POST['name'] ?? '',
|
|
'description' => $_POST['description'] ?? '',
|
|
'is_taxable' => isset($_POST['is_taxable']) ? 1 : 0,
|
|
'is_active' => isset($_POST['is_active']) ? 1 : 0,
|
|
'fund_source' => $_POST['fund_source'] ?? 'HOSPITAL',
|
|
'display_order' => $_POST['display_order'] ?? 0
|
|
];
|
|
|
|
$incomeModel = new IncomeMasterModel();
|
|
try {
|
|
$incomeModel->createIncome($data);
|
|
$_SESSION['success'] = 'เพิ่มรายการรายรับสำเร็จ';
|
|
} catch (\PDOException $e) {
|
|
$_SESSION['error'] = 'ไม่สามารถเพิ่มรายการได้ อาจรหัสซ้ำ (' . $e->getMessage() . ')';
|
|
}
|
|
header('Location: ' . BASE_URL . '/settings/income');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public function updateIncome() {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$id = $_POST['id'] ?? null;
|
|
if (!$id) {
|
|
$_SESSION['error'] = 'ไม่พบรหัสรายการ';
|
|
header('Location: ' . BASE_URL . '/settings/income');
|
|
exit;
|
|
}
|
|
|
|
$data = [
|
|
'code' => $_POST['code'] ?? '',
|
|
'name' => $_POST['name'] ?? '',
|
|
'description' => $_POST['description'] ?? '',
|
|
'is_taxable' => isset($_POST['is_taxable']) ? 1 : 0,
|
|
'is_active' => isset($_POST['is_active']) ? 1 : 0,
|
|
'fund_source' => $_POST['fund_source'] ?? 'HOSPITAL',
|
|
'display_order' => $_POST['display_order'] ?? 0
|
|
];
|
|
|
|
$incomeModel = new IncomeMasterModel();
|
|
try {
|
|
$incomeModel->updateIncome($id, $data);
|
|
$_SESSION['success'] = 'แก้ไขรายการรายรับสำเร็จ';
|
|
} catch (\PDOException $e) {
|
|
$_SESSION['error'] = 'ไม่สามารถแก้ไขรายการได้ (' . $e->getMessage() . ')';
|
|
}
|
|
header('Location: ' . BASE_URL . '/settings/income');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// --- Deduction Methods ---
|
|
|
|
public function deduction() {
|
|
$deductionModel = new DeductionMasterModel();
|
|
$deductions = $deductionModel->getAllDeductions();
|
|
|
|
$this->view('settings/deduction', [
|
|
'title' => 'จัดการรายการรายจ่าย | ' . APP_NAME,
|
|
'activeMenu' => 'setting_deduction',
|
|
'deductions' => $deductions
|
|
]);
|
|
}
|
|
|
|
public function storeDeduction() {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$data = [
|
|
'code' => $_POST['code'] ?? '',
|
|
'name' => $_POST['name'] ?? '',
|
|
'description' => $_POST['description'] ?? '',
|
|
'is_tax_deductible' => isset($_POST['is_tax_deductible']) ? 1 : 0,
|
|
'is_active' => isset($_POST['is_active']) ? 1 : 0,
|
|
'fund_source' => $_POST['fund_source'] ?? 'HOSPITAL',
|
|
'display_order' => $_POST['display_order'] ?? 0
|
|
];
|
|
|
|
$deductionModel = new DeductionMasterModel();
|
|
try {
|
|
$deductionModel->createDeduction($data);
|
|
$_SESSION['success'] = 'เพิ่มรายการรายจ่ายสำเร็จ';
|
|
} catch (\PDOException $e) {
|
|
$_SESSION['error'] = 'ไม่สามารถเพิ่มรายการได้ อาจรหัสซ้ำ (' . $e->getMessage() . ')';
|
|
}
|
|
header('Location: ' . BASE_URL . '/settings/deduction');
|
|
exit;
|
|
}
|
|
}
|
|
public function updateDeduction() {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$id = $_POST['id'] ?? null;
|
|
if (!$id) {
|
|
$_SESSION['error'] = 'ไม่พบรหัสรายการ';
|
|
header('Location: ' . BASE_URL . '/settings/deduction');
|
|
exit;
|
|
}
|
|
|
|
$data = [
|
|
'code' => $_POST['code'] ?? '',
|
|
'name' => $_POST['name'] ?? '',
|
|
'description' => $_POST['description'] ?? '',
|
|
'is_tax_deductible' => isset($_POST['is_tax_deductible']) ? 1 : 0,
|
|
'is_active' => isset($_POST['is_active']) ? 1 : 0,
|
|
'fund_source' => $_POST['fund_source'] ?? 'HOSPITAL',
|
|
'display_order' => $_POST['display_order'] ?? 0
|
|
];
|
|
|
|
$deductionModel = new DeductionMasterModel();
|
|
try {
|
|
$deductionModel->updateDeduction($id, $data);
|
|
$_SESSION['success'] = 'แก้ไขรายการรายจ่ายสำเร็จ';
|
|
} catch (\PDOException $e) {
|
|
$_SESSION['error'] = 'ไม่สามารถแก้ไขรายการได้ (' . $e->getMessage() . ')';
|
|
}
|
|
header('Location: ' . BASE_URL . '/settings/deduction');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// --- System Methods ---
|
|
|
|
public function system() {
|
|
$settingModel = new SystemSettingModel();
|
|
$orgTaxId = $settingModel->getSetting('org_tax_id', '');
|
|
$appVersion = $settingModel->getSetting('app_version', '1.0.0');
|
|
$signatureName = $settingModel->getSetting('signature_name', '');
|
|
$signaturePosition = $settingModel->getSetting('signature_position', '');
|
|
$signatureScale = $settingModel->getSetting('signature_scale', '100');
|
|
$slipFontSize = $settingModel->getSetting('slip_font_size', '11pt');
|
|
$signaturePosX = $settingModel->getSetting('signature_pos_x', '0');
|
|
$signaturePosY = $settingModel->getSetting('signature_pos_y', '0');
|
|
$chartHistoryYears = $settingModel->getSetting('chart_history_years', '3');
|
|
|
|
$this->view('settings/system', [
|
|
'title' => 'ตั้งค่าระบบทั่วไป | ' . APP_NAME,
|
|
'activeMenu' => 'setting_system',
|
|
'orgTaxId' => $orgTaxId,
|
|
'appVersion' => $appVersion,
|
|
'signatureName' => $signatureName,
|
|
'signaturePosition' => $signaturePosition,
|
|
'signatureScale' => $signatureScale,
|
|
'slipFontSize' => $slipFontSize,
|
|
'signaturePosX' => $signaturePosX,
|
|
'signaturePosY' => $signaturePosY,
|
|
'chartHistoryYears' => $chartHistoryYears,
|
|
'taxSignatureName' => $settingModel->getSetting('tax_signature_name', ''),
|
|
'taxSignaturePosition' => $settingModel->getSetting('tax_signature_position', ''),
|
|
'taxSignatureScale' => $settingModel->getSetting('tax_signature_scale', '100'),
|
|
'taxSignaturePosX' => $settingModel->getSetting('tax_signature_pos_x', '0'),
|
|
'taxSignaturePosY' => $settingModel->getSetting('tax_signature_pos_y', '0'),
|
|
'taxFontSize' => $settingModel->getSetting('tax_font_size', '11pt'),
|
|
'orgAddress' => $settingModel->getSetting('org_address', 'ต.อ่างทอง อ.เกาะสมุย จ.สุราษฎร์ธานี 84140'),
|
|
'taxPayeeAddress' => $settingModel->getSetting('tax_payee_address', 'ต.อ่างทอง อ.เกาะสมุย จ.สุราษฎร์ธานี 84140')
|
|
]);
|
|
}
|
|
|
|
public function saveSystemSettings() {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$settingModel = new SystemSettingModel();
|
|
|
|
$settingModel->setSetting('org_tax_id', $_POST['org_tax_id'] ?? '');
|
|
$settingModel->setSetting('app_version', $_POST['app_version'] ?? '1.0.0');
|
|
$settingModel->setSetting('signature_name', $_POST['signature_name'] ?? '');
|
|
$settingModel->setSetting('signature_position', $_POST['signature_position'] ?? '');
|
|
$settingModel->setSetting('signature_scale', $_POST['signature_scale'] ?? '100');
|
|
$settingModel->setSetting('slip_font_size', $_POST['slip_font_size'] ?? '11pt');
|
|
$settingModel->setSetting('signature_pos_x', $_POST['signature_pos_x'] ?? '0');
|
|
$settingModel->setSetting('signature_pos_y', $_POST['signature_pos_y'] ?? '0');
|
|
$settingModel->setSetting('chart_history_years', $_POST['chart_history_years'] ?? '3');
|
|
|
|
$settingModel->setSetting('tax_signature_name', $_POST['tax_signature_name'] ?? '');
|
|
$settingModel->setSetting('tax_signature_position', $_POST['tax_signature_position'] ?? '');
|
|
$settingModel->setSetting('tax_signature_scale', $_POST['tax_signature_scale'] ?? '100');
|
|
$settingModel->setSetting('tax_signature_pos_x', $_POST['tax_signature_pos_x'] ?? '0');
|
|
$settingModel->setSetting('tax_signature_pos_y', $_POST['tax_signature_pos_y'] ?? '0');
|
|
$settingModel->setSetting('tax_font_size', $_POST['tax_font_size'] ?? '11pt');
|
|
$settingModel->setSetting('org_address', $_POST['org_address'] ?? 'ต.อ่างทอง อ.เกาะสมุย จ.สุราษฎร์ธานี 84140');
|
|
$settingModel->setSetting('tax_payee_address', $_POST['tax_payee_address'] ?? 'ต.อ่างทอง อ.เกาะสมุย จ.สุราษฎร์ธานี 84140');
|
|
|
|
$_SESSION['success'] = 'บันทึกการตั้งค่าระบบเรียบร้อยแล้ว';
|
|
header('Location: ' . BASE_URL . '/settings/system');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public function searchOfficer() {
|
|
error_reporting(0); // Supress all errors to prevent breaking JSON
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
$keyword = $_GET['q'] ?? '';
|
|
|
|
$response = [
|
|
'status' => 'error',
|
|
'message' => 'ไม่พบข้อมูล',
|
|
'data' => []
|
|
];
|
|
|
|
if (strlen($keyword) >= 2) {
|
|
try {
|
|
$hrModel = new HrPersonModel();
|
|
$results = $hrModel->searchPersons($keyword);
|
|
|
|
if (!empty($results)) {
|
|
$formattedData = [];
|
|
foreach ($results as $person) {
|
|
// User's formatting logic
|
|
$fullName = ($person['HR_PREFIX_NAME'] ?? '') . ($person['HR_FNAME'] ?? '') . ' ' . ($person['HR_LNAME'] ?? '');
|
|
$department = $person['HR_DEPARTMENT_SUB_SUB_NAME'] ?? 'ไม่ระบุ';
|
|
$position = ($person['HR_POSITION_NAME'] ?? '') . ' ' . ($person['HR_LEVEL_NAME'] ?? '');
|
|
|
|
$formattedData[] = [
|
|
'id' => $person['HR_CID'] ?? '',
|
|
'text' => trim($fullName) . ' (' . trim($position) . ')',
|
|
'name' => trim($fullName),
|
|
'position' => trim($position)
|
|
];
|
|
}
|
|
|
|
$response = [
|
|
'status' => 'success',
|
|
'message' => 'พบข้อมูล',
|
|
'data' => $formattedData
|
|
];
|
|
}
|
|
} catch (\Exception $e) {
|
|
$response['message'] = $e->getMessage();
|
|
}
|
|
}
|
|
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
public function uploadSignature() {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['signature'])) {
|
|
$file = $_FILES['signature'];
|
|
|
|
// Check for errors
|
|
if ($file['error'] !== UPLOAD_ERR_OK) {
|
|
$_SESSION['error'] = 'เกิดข้อผิดพลาดในการอัปโหลดไฟล์ (Error Code: ' . $file['error'] . ')';
|
|
header('Location: ' . BASE_URL . '/settings/system');
|
|
exit;
|
|
}
|
|
|
|
// Validate MIME type
|
|
$allowedTypes = ['image/jpeg', 'image/png'];
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
|
$mimeType = finfo_file($finfo, $file['tmp_name']);
|
|
finfo_close($finfo);
|
|
|
|
if (!in_array($mimeType, $allowedTypes)) {
|
|
$_SESSION['error'] = 'กรุณาอัปโหลดไฟล์รูปภาพ JPG หรือ PNG เท่านั้น';
|
|
header('Location: ' . BASE_URL . '/settings/system');
|
|
exit;
|
|
}
|
|
|
|
// Save as signature.png
|
|
$targetPath = APP_ROOT . '/public/img/signature.png';
|
|
if (move_uploaded_file($file['tmp_name'], $targetPath)) {
|
|
$_SESSION['success'] = 'อัปโหลดรูปลายเซ็นสำเร็จ';
|
|
} else {
|
|
$_SESSION['error'] = 'ไม่สามารถบันทึกไฟล์ได้';
|
|
}
|
|
|
|
header('Location: ' . BASE_URL . '/settings/system');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public function uploadTaxSignature() {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['tax_signature'])) {
|
|
$file = $_FILES['tax_signature'];
|
|
|
|
// Check for errors
|
|
if ($file['error'] !== UPLOAD_ERR_OK) {
|
|
$_SESSION['error'] = 'เกิดข้อผิดพลาดในการอัปโหลดไฟล์ (Error Code: ' . $file['error'] . ')';
|
|
header('Location: ' . BASE_URL . '/settings/system');
|
|
exit;
|
|
}
|
|
|
|
// Validate MIME type
|
|
$allowedTypes = ['image/jpeg', 'image/png'];
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
|
$mimeType = finfo_file($finfo, $file['tmp_name']);
|
|
finfo_close($finfo);
|
|
|
|
if (!in_array($mimeType, $allowedTypes)) {
|
|
$_SESSION['error'] = 'กรุณาอัปโหลดไฟล์รูปภาพ JPG หรือ PNG เท่านั้น';
|
|
header('Location: ' . BASE_URL . '/settings/system');
|
|
exit;
|
|
}
|
|
|
|
// Save as tax_signature.png
|
|
$targetPath = APP_ROOT . '/public/img/tax_signature.png';
|
|
if (move_uploaded_file($file['tmp_name'], $targetPath)) {
|
|
$_SESSION['success'] = 'อัปโหลดรูปลายเซ็นสำหรับใบเสียภาษีสำเร็จ';
|
|
} else {
|
|
$_SESSION['error'] = 'ไม่สามารถบันทึกไฟล์ได้';
|
|
}
|
|
|
|
header('Location: ' . BASE_URL . '/settings/system');
|
|
exit;
|
|
}
|
|
}
|
|
// --- Officer Types ---
|
|
|
|
public function officerTypes() {
|
|
$model = new OfficerTypeModel();
|
|
$officerTypes = $model->getAll();
|
|
|
|
$this->view('settings/officer_types', [
|
|
'title' => 'จัดการประเภทเจ้าหน้าที่ | ' . APP_NAME,
|
|
'activeMenu' => 'setting_officer_types',
|
|
'officerTypes' => $officerTypes
|
|
]);
|
|
}
|
|
|
|
public function storeOfficerType() {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$name = trim($_POST['name'] ?? '');
|
|
$isActive = isset($_POST['is_active']) ? 1 : 0;
|
|
|
|
if (empty($name)) {
|
|
$_SESSION['error'] = 'กรุณากรอกชื่อประเภทเจ้าหน้าที่';
|
|
header('Location: ' . BASE_URL . '/settings/officer-types');
|
|
exit;
|
|
}
|
|
|
|
$model = new OfficerTypeModel();
|
|
if ($model->create($name, $isActive)) {
|
|
$_SESSION['success'] = 'เพิ่มประเภทเจ้าหน้าที่สำเร็จ';
|
|
} else {
|
|
$_SESSION['error'] = 'ไม่สามารถเพิ่มข้อมูลได้ (ชื่ออาจซ้ำ)';
|
|
}
|
|
|
|
header('Location: ' . BASE_URL . '/settings/officer-types');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public function updateOfficerType() {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$id = $_POST['id'] ?? null;
|
|
$name = trim($_POST['name'] ?? '');
|
|
$isActive = isset($_POST['is_active']) ? 1 : 0;
|
|
|
|
if (!$id || empty($name)) {
|
|
$_SESSION['error'] = 'ข้อมูลไม่ครบถ้วน';
|
|
header('Location: ' . BASE_URL . '/settings/officer-types');
|
|
exit;
|
|
}
|
|
|
|
$model = new OfficerTypeModel();
|
|
if ($model->update($id, $name, $isActive)) {
|
|
$_SESSION['success'] = 'แก้ไขประเภทเจ้าหน้าที่สำเร็จ';
|
|
} else {
|
|
$_SESSION['error'] = 'ไม่สามารถแก้ไขข้อมูลได้';
|
|
}
|
|
|
|
header('Location: ' . BASE_URL . '/settings/officer-types');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public function toggleOfficerTypeStatus() {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$id = $_POST['id'] ?? null;
|
|
$status = isset($_POST['status']) && $_POST['status'] == '1' ? 1 : 0;
|
|
|
|
if ($id) {
|
|
$model = new OfficerTypeModel();
|
|
$model->toggleActive($id, $status);
|
|
}
|
|
|
|
header('Location: ' . BASE_URL . '/settings/officer-types');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public function deleteOfficerType() {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$id = $_POST['id'] ?? null;
|
|
|
|
if ($id) {
|
|
$model = new OfficerTypeModel();
|
|
$model->delete($id);
|
|
$_SESSION['success'] = 'ลบประเภทเจ้าหน้าที่สำเร็จ';
|
|
}
|
|
|
|
header('Location: ' . BASE_URL . '/settings/officer-types');
|
|
exit;
|
|
}
|
|
}
|
|
}
|