239 lines
8.2 KiB
PHP
239 lines
8.2 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
use CodeIgniter\RESTful\ResourceController;
|
|
use App\Models\PolicyModel;
|
|
use App\Models\EnquiryModel;
|
|
use App\Models\QuotationModel;
|
|
use App\Models\InvoiceModel;
|
|
use App\Models\InvoiceItemModel;
|
|
use App\Models\InvoiceUtrModel;
|
|
use CodeIgniter\Database\Exceptions\DataException;
|
|
|
|
class InvoiceController extends ResourceController
|
|
{
|
|
protected $PolicyModel;
|
|
protected $QuotationModel;
|
|
protected $EnquiryModel;
|
|
protected $InvoiceModel;
|
|
protected $InvoiceItemModel;
|
|
protected $InvoiceUtrModel;
|
|
protected $db;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->PolicyModel = new PolicyModel();
|
|
$this->QuotationModel = new QuotationModel();
|
|
$this->EnquiryModel = new EnquiryModel();
|
|
$this->InvoiceModel = new InvoiceModel();
|
|
$this->InvoiceItemModel = new InvoiceItemModel();
|
|
$this->InvoiceUtrModel = new InvoiceUtrModel();
|
|
$this->db = \Config\Database::connect();
|
|
}
|
|
|
|
public function invoiceList()
|
|
{
|
|
try {
|
|
|
|
$data = $this->InvoiceModel->where('is_active', 1)->orderBy('id', 'DESC')->findAll();
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200,'data' => $data ], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function findInvoiceWithItems()
|
|
{
|
|
try {
|
|
$id = $this->request->getGet('id');
|
|
|
|
if (!$id) {
|
|
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'ID is required'], 400);
|
|
}
|
|
|
|
$invoice = $this->InvoiceModel->where('id', $id)->first();
|
|
|
|
if (!$invoice) {
|
|
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'Invoice not found'], 404);
|
|
}
|
|
|
|
$items = $this->InvoiceItemModel->select('partner_invoice_items.* , pp.issued_date , pe.name as customer_name , pq.premium_amount')
|
|
->join('partner_policy pp','pp.id = partner_invoice_items.policy_id ', 'left')
|
|
->join('partner_enquiry pe','pe.id = pp.enquiry_id ', 'left')
|
|
->join('partner_quotation pq','pq.id = pp.quotation_id ', 'left')
|
|
->where('partner_invoice_items.invoice_id', $id)
|
|
->where('partner_invoice_items.is_active', 1)
|
|
->findAll();
|
|
|
|
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'code' => 200,
|
|
'data' => [
|
|
'invoice' => $invoice,
|
|
'items' => $items
|
|
]
|
|
], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function createOrUpdateInvoice()
|
|
{
|
|
$this->db->transBegin();
|
|
|
|
try {
|
|
$input = $this->request->getJSON(true);
|
|
|
|
// Basic invoice data
|
|
$invoiceData = [
|
|
'invoice_no' => $input['invoice_no'] ?? '',
|
|
'invoice_amount' => $input['invoice_amount'] ?? 0,
|
|
'agent_id' => $input['agent_id'] ?? '',
|
|
'invoice_date' => $input['invoice_date'] ?? '',
|
|
'payout_status' => $input['payout_status'] ?? 0,
|
|
'is_active' => 1,
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
|
'updated_by' => $input['updated_by'] ?? 0
|
|
];
|
|
|
|
$invoiceId = $input['id'] ?? null;
|
|
|
|
if (!$invoiceId) {
|
|
|
|
$invoiceData['created_at'] = date('Y-m-d H:i:s');
|
|
$invoiceData['created_by'] = $input['created_by'] ?? 0;
|
|
|
|
$invoiceId = $this->InvoiceModel->insert($invoiceData);
|
|
|
|
if (!$invoiceId) { throw new DataException("Failed to create invoice"); }
|
|
|
|
} else {
|
|
$this->InvoiceModel->update($invoiceId, $invoiceData);
|
|
}
|
|
|
|
// Insert or update items
|
|
if (!empty($input['items']) && is_array($input['items'])) {
|
|
|
|
foreach ($input['items'] as $item) {
|
|
|
|
$itemData = [
|
|
'invoice_id' => $invoiceId,
|
|
'policy_id' => $item['policy_id'],
|
|
'policy_no' => $item['policy_no'],
|
|
'commission_amount' => $item['commission_amount'] ?? 0,
|
|
'is_active' => $item['is_active'] ?? 1,
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
|
'updated_by' => $input['updated_by'] ?? 0
|
|
];
|
|
|
|
// UPDATE (if id exists)
|
|
if (!empty($item['id'])) {
|
|
|
|
$this->InvoiceItemModel->update($item['id'], $itemData);
|
|
|
|
}else { // INSERT (if id missing)
|
|
|
|
$itemData['created_at'] = date('Y-m-d H:i:s');
|
|
$itemData['created_by'] = $input['created_by'] ?? 0;
|
|
$this->InvoiceItemModel->insert($itemData);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if ($this->db->transStatus() === false) {
|
|
$this->db->transRollback();
|
|
throw new DataException("Transaction failed");
|
|
}
|
|
|
|
$this->db->transCommit();
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'code' => 200,
|
|
'data' => ['invoice_id' => $invoiceId]
|
|
], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
$this->db->transRollback();
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function deleteInvoice()
|
|
{
|
|
try {
|
|
$id = $this->request->getGet('id');
|
|
|
|
if (!$id) {
|
|
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'ID is required'], 400);
|
|
}
|
|
|
|
$this->InvoiceModel->update($id, ['is_active' => 0]);
|
|
$this->InvoiceItemModel->where('invoice_id', $id)->set(['is_active' => 0])->update();
|
|
|
|
return $this->respond(['status' => 'success', 'code' => 200, 'data' => 'Invoice deleted'], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function getCommissionRateList()
|
|
{
|
|
try {
|
|
$input = $this->request->getJSON(true);
|
|
|
|
if (empty($input['agent_id']) || empty($input['issued_date'])) {
|
|
return $this->respond([
|
|
'status' => 'failed',
|
|
'code' => 400,
|
|
'message'=> 'agent_id and issued_date are required'
|
|
], 400);
|
|
}
|
|
|
|
|
|
$data = $this->PolicyModel
|
|
->select('partner_policy.policy_number as policy_no,partner_policy.id as policy_id , partner_policy.issued_date, partner_policy.commission_amount, pe.name as customer_name , pq.premium_amount')
|
|
->join(
|
|
'partner_enquiry pe',
|
|
'pe.id = partner_policy.enquiry_id AND pe.broker_id = ' . (int)$input['broker_id'],
|
|
'left'
|
|
)
|
|
->join('partner_quotation pq','pq.id = partner_policy.quotation_id ', 'left')
|
|
->where('partner_policy.agent_id', $input['agent_id'])
|
|
->where('partner_policy.issued_date <=', date('Y-m-d', strtotime($input['issued_date'])))
|
|
->where('partner_policy.is_active', 1)
|
|
->findAll();
|
|
|
|
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'code' => 200,
|
|
'data' => $data
|
|
], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->respond([
|
|
'status' => 'failed',
|
|
'code' => 500,
|
|
'message'=> $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|