GWM : getAgentUnusedCommissionList
This commit is contained in:
parent
053e27eb63
commit
3fb7707edc
@ -388,98 +388,135 @@ class InvoiceController extends ResourceController
|
||||
public function getCommissionRateList()
|
||||
{
|
||||
try {
|
||||
|
||||
$input = $this->request->getJSON(true);
|
||||
|
||||
// if (empty($input['broker_id'])) {
|
||||
// return $this->respond([
|
||||
// 'status' => 'failed',
|
||||
// 'code' => 400,
|
||||
// 'message'=> 'broker_id is required'
|
||||
// ], 400);
|
||||
// }
|
||||
|
||||
$invoice_id = isset($input['invoice_id']) ? $input['invoice_id'] : '';
|
||||
|
||||
// Build the main query
|
||||
$query = $this->PolicyModel
|
||||
->select('partner_policy.policy_number as policy_no, partner_policy.id as policy_id, partner_policy.issued_date, partner_policy.commission_amount, partner_policy.insured_name as customer_name, pe.agent_id ,pa.name as agent_name,pa.agent_code, partner_policy.premium_amount, pi.id as invoice_id, pi.invoice_no,
|
||||
COALESCE((
|
||||
SELECT GROUP_CONCAT(piu.utr_no ORDER BY piu.id SEPARATOR ", ")
|
||||
FROM partner_invoice_utr piu
|
||||
WHERE piu.invoice_id = pi.id
|
||||
AND piu.is_active = 1
|
||||
), "") as utr_no', false)
|
||||
// ->join(
|
||||
// 'partner_enquiry pe',
|
||||
// 'pe.id = partner_policy.enquiry_id AND pe.broker_id = ' . (int)$input['broker_id'],
|
||||
// 'left'
|
||||
// )
|
||||
->join('partner_enquiry pe','pe.id = partner_policy.enquiry_id','left')
|
||||
->join('partner_quotation pq', 'pq.id = partner_policy.quotation_id', 'left')
|
||||
->join('partner_invoice_items pii', 'pii.policy_id = partner_policy.id', 'left')
|
||||
->join('partner_invoice pi', 'pi.id = pii.invoice_id AND pi.is_active = 1', 'left')
|
||||
->join('partner_agent pa', 'pa.id = pe.agent_id', 'left')
|
||||
->where('partner_policy.is_active', 1)
|
||||
->where('partner_policy.commission_amount > 0')
|
||||
->where('partner_policy.is_data_accuracy_checked', 1)
|
||||
->where('partner_policy.manager_id', $input['manager_id']);
|
||||
|
||||
// Apply agent_id filter (supports multiple agents)
|
||||
if (!empty($input['agent_id'])) {
|
||||
if (is_array($input['agent_id'])) {
|
||||
$query->whereIn('pe.agent_id', $input['agent_id']);
|
||||
} else {
|
||||
$query->where('pe.agent_id', $input['agent_id']);
|
||||
}
|
||||
if (! is_array($input)) {
|
||||
return $this->respond([
|
||||
'status' => 'failed',
|
||||
'code' => 400,
|
||||
'message'=> 'JSON body is required',
|
||||
], 400);
|
||||
}
|
||||
|
||||
// Apply date range filters
|
||||
if (!empty($input['from_date'])) {
|
||||
$query->where('partner_policy.issued_date >=', date('Y-m-d', strtotime($input['from_date'])));
|
||||
if (empty($input['manager_id'])) {
|
||||
return $this->respond([
|
||||
'status' => 'failed',
|
||||
'code' => 400,
|
||||
'message'=> 'manager_id is required',
|
||||
], 400);
|
||||
}
|
||||
|
||||
if (!empty($input['to_date'])) {
|
||||
$query->where('partner_policy.issued_date <=', date('Y-m-d', strtotime($input['to_date'])));
|
||||
}
|
||||
$managerId = (int) $input['manager_id'];
|
||||
$invoiceIdRaw = $input['invoice_id'] ?? null;
|
||||
$invoiceId = ($invoiceIdRaw !== null && $invoiceIdRaw !== '' && (int) $invoiceIdRaw > 0)
|
||||
? (int) $invoiceIdRaw
|
||||
: 0;
|
||||
|
||||
// Apply POS filter if provided
|
||||
if (!empty($input['pos_id'])) {
|
||||
$query->where('partner_policy.pos_id', $input['pos_id']);
|
||||
}
|
||||
|
||||
// Filter based on invoice_id or policies not yet invoiced
|
||||
if (empty($invoice_id)) {
|
||||
$query->where('pii.policy_id IS NULL');
|
||||
// Unused policies: no active invoice line on an active invoice (same as getAgentUnusedCommissionList).
|
||||
// Do not LEFT JOIN all invoice_items — inactive rows duplicate policies and break "unused" detection.
|
||||
if ($invoiceId === 0) {
|
||||
$query = $this->PolicyModel
|
||||
->select(
|
||||
'partner_policy.policy_number as policy_no, partner_policy.id as policy_id, partner_policy.issued_date, '
|
||||
. 'partner_policy.commission_amount, partner_policy.insured_name as customer_name, pe.agent_id, '
|
||||
. 'pa.name as agent_name, pa.agent_code, partner_policy.premium_amount, '
|
||||
. "NULL AS invoice_id, NULL AS invoice_no, '' AS utr_no",
|
||||
false
|
||||
)
|
||||
->join('partner_enquiry pe', 'pe.id = partner_policy.enquiry_id', 'left')
|
||||
->join('partner_quotation pq', 'pq.id = partner_policy.quotation_id', 'left')
|
||||
->join('partner_agent pa', 'pa.id = pe.agent_id', 'left')
|
||||
->where('partner_policy.is_active', 1)
|
||||
->where('partner_policy.commission_amount >', 0)
|
||||
->where('partner_policy.is_data_accuracy_checked', 1)
|
||||
->where('partner_policy.manager_id', $managerId)
|
||||
->where(
|
||||
'NOT EXISTS (SELECT 1 FROM partner_invoice_items pii2 '
|
||||
. 'INNER JOIN partner_invoice pi2 ON pi2.id = pii2.invoice_id AND pi2.is_active = 1 '
|
||||
. 'WHERE pii2.policy_id = partner_policy.id AND pii2.is_active = 1)',
|
||||
null,
|
||||
false
|
||||
);
|
||||
} else {
|
||||
$query->where('pi.id', $invoice_id);
|
||||
$query = $this->PolicyModel
|
||||
->select(
|
||||
'partner_policy.policy_number as policy_no, partner_policy.id as policy_id, partner_policy.issued_date, '
|
||||
. 'partner_policy.commission_amount, partner_policy.insured_name as customer_name, pe.agent_id, '
|
||||
. 'pa.name as agent_name, pa.agent_code, partner_policy.premium_amount, pi.id as invoice_id, pi.invoice_no, '
|
||||
. 'COALESCE(('
|
||||
. 'SELECT GROUP_CONCAT(piu.utr_no ORDER BY piu.id SEPARATOR ", ") '
|
||||
. 'FROM partner_invoice_utr piu '
|
||||
. 'WHERE piu.invoice_id = pi.id AND piu.is_active = 1'
|
||||
. "), '') as utr_no",
|
||||
false
|
||||
)
|
||||
->join('partner_enquiry pe', 'pe.id = partner_policy.enquiry_id', 'left')
|
||||
->join('partner_quotation pq', 'pq.id = partner_policy.quotation_id', 'left')
|
||||
->join('partner_agent pa', 'pa.id = pe.agent_id', 'left')
|
||||
->join(
|
||||
'partner_invoice_items pii',
|
||||
'pii.policy_id = partner_policy.id AND pii.is_active = 1 AND pii.invoice_id = ' . $invoiceId,
|
||||
'inner'
|
||||
)
|
||||
->join('partner_invoice pi', 'pi.id = pii.invoice_id AND pi.is_active = 1', 'inner')
|
||||
->where('partner_policy.is_active', 1)
|
||||
->where('partner_policy.commission_amount >', 0)
|
||||
->where('partner_policy.is_data_accuracy_checked', 1)
|
||||
->where('partner_policy.manager_id', $managerId)
|
||||
->where('pi.id', $invoiceId)
|
||||
->distinct();
|
||||
}
|
||||
|
||||
$this->applyCommissionRateListPayloadFilters($query, $input);
|
||||
|
||||
$query->orderBy('partner_policy.issued_date', 'DESC');
|
||||
$query->orderBy('partner_policy.id', 'DESC');
|
||||
|
||||
$data = $query->findAll();
|
||||
|
||||
// Calculate total commission
|
||||
$total_commission = 0;
|
||||
foreach ($data as $policy) {
|
||||
$total_commission += $policy['commission_amount'];
|
||||
$total_commission += (float) ($policy['commission_amount'] ?? 0);
|
||||
}
|
||||
|
||||
return $this->respond([
|
||||
'status' => 'success',
|
||||
'code' => 200,
|
||||
'data' => $data,
|
||||
'status' => 'success',
|
||||
'code' => 200,
|
||||
'data' => $data,
|
||||
'total_commission' => $total_commission,
|
||||
'total_policies' => count($data)
|
||||
'total_policies' => count($data),
|
||||
], 200);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return $this->respond([
|
||||
'status' => 'failed',
|
||||
'code' => 500,
|
||||
'message'=> $e->getMessage()
|
||||
'message'=> $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional filters for getCommissionRateList JSON: agent_id, from_date, to_date, pos_id.
|
||||
*/
|
||||
private function applyCommissionRateListPayloadFilters($query, array $input): void
|
||||
{
|
||||
if (! empty($input['agent_id'])) {
|
||||
if (is_array($input['agent_id'])) {
|
||||
$query->whereIn('pe.agent_id', $input['agent_id']);
|
||||
} else {
|
||||
$query->where('pe.agent_id', $input['agent_id']);
|
||||
}
|
||||
}
|
||||
if (! empty($input['from_date'])) {
|
||||
$query->where('partner_policy.issued_date >=', date('Y-m-d', strtotime((string) $input['from_date'])));
|
||||
}
|
||||
if (! empty($input['to_date'])) {
|
||||
$query->where('partner_policy.issued_date <=', date('Y-m-d', strtotime((string) $input['to_date'])));
|
||||
}
|
||||
if (! empty($input['pos_id'])) {
|
||||
$query->where('partner_policy.pos_id', $input['pos_id']);
|
||||
}
|
||||
}
|
||||
|
||||
public function getAgentUnusedCommissionList()
|
||||
{
|
||||
try {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user