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); } } }