AMS_Backend/app/Controllers/BranchDetailsController.php
2025-11-06 13:41:06 +08:00

86 lines
3.2 KiB
PHP

<?php
namespace App\Controllers;
use App\Models\BranchCategory;
use App\Models\BranchDetails;
use App\Models\Invoice;
use App\Models\Letterhead;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class BranchDetailsController extends ResourceController
{
private $db;
private $branchDetails;
private $branchCategory;
private $invoice;
private $letterhead;
public function __construct()
{
$this->branchDetails = new BranchDetails();
$this->branchCategory = new BranchCategory();
$this->invoice = new Invoice();
$this->letterhead = new Letterhead();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->branchDetails->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No branch details found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->branchDetails->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Branch not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->branchDetails->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Branch created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Branch'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->branchDetails->find($id)) return $this->respond(['status' => 404, 'message' => 'Branch not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->branchDetails->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Branch updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Branch'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->branchDetails->find($id)) return $this->respond(['status' => 404, 'message' => 'Branch not found'], 404);
if ($this->branchDetails->delete($id)) return $this->respond(['status' => 200, 'message' => 'Branch deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Branch'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}