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

92 lines
3.4 KiB
PHP

<?php
namespace App\Controllers;
use App\Models\BranchDetails;
use App\Models\CustomerDetails;
use App\Models\Invoice;
use App\Models\InvoiceItem;
use App\Models\InvoiceType;
use App\Models\PaymentMethod;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class InvoiceController extends ResourceController
{
private $db;
private $invoice;
private $invoiceType;
private $invoiceItem;
private $customerDetails;
private $paymentMethod;
private $branchDetails;
public function __construct()
{
$this->invoice = new Invoice();
$this->invoiceType = new InvoiceType();
$this->invoiceItem = new InvoiceItem();
$this->customerDetails = new CustomerDetails();
$this->paymentMethod = new PaymentMethod();
$this->branchDetails = new BranchDetails();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->invoice->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No invoices found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->invoice->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Invoice 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->invoice->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Invoice created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Invoice'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->invoice->find($id)) return $this->respond(['status' => 404, 'message' => 'Invoice not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->invoice->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Invoice updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Invoice'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->invoice->find($id)) return $this->respond(['status' => 404, 'message' => 'Invoice not found'], 404);
if ($this->invoice->delete($id)) return $this->respond(['status' => 200, 'message' => 'Invoice deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Invoice'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}