GWM : getAgentUnusedCommissionList
This commit is contained in:
parent
994b0fdbfb
commit
9bec6292f5
@ -485,41 +485,40 @@ class InvoiceController extends ResourceController
|
||||
try {
|
||||
|
||||
$manager_id = $this->request->getGet('manager_id');
|
||||
// $broker_id = $this->request->getGet('broker_id');
|
||||
|
||||
// if (empty($broker_id)) {
|
||||
// return $this->respond([
|
||||
// 'status' => 'failed',
|
||||
// 'code' => 400,
|
||||
// 'message'=> 'broker_id is required'
|
||||
// ], 400);
|
||||
// }
|
||||
if ($manager_id === null || $manager_id === '') {
|
||||
return $this->respond([
|
||||
'status' => 'failed',
|
||||
'code' => 400,
|
||||
'message'=> 'manager_id is required',
|
||||
], 400);
|
||||
}
|
||||
|
||||
// Unused = policy has no active invoice line on an active invoice (same idea as commissionPayoutReport pending).
|
||||
// NOT EXISTS avoids false "invoiced" when only inactive items/invoices exist, and avoids join fan-out.
|
||||
$query = $this->PolicyModel
|
||||
->select([
|
||||
'pe.agent_id',
|
||||
'pa.name as agent_name',
|
||||
'MAX(pa.name) as agent_name',
|
||||
'MAX(pa.agent_code) as agent_code',
|
||||
'SUM(partner_policy.commission_amount) as unused_commission_amount',
|
||||
'COUNT(partner_policy.id) as total_policies'
|
||||
'COUNT(partner_policy.id) as total_policies',
|
||||
])
|
||||
// ->join(
|
||||
// 'partner_enquiry pe',
|
||||
// 'pe.id = partner_policy.enquiry_id AND pe.broker_id = ' . (int)$broker_id,
|
||||
// 'left'
|
||||
// )
|
||||
->join('partner_enquiry pe','pe.id = partner_policy.enquiry_id','left')
|
||||
->join('partner_enquiry pe', 'pe.id = partner_policy.enquiry_id', 'left')
|
||||
->join('partner_agent pa', 'pa.id = pe.agent_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')
|
||||
->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', $manager_id)
|
||||
->where('pii.policy_id IS NULL') // ❗ unused commission
|
||||
->where('partner_policy.commission_amount >', 0)
|
||||
->where('partner_policy.is_data_accuracy_checked', 1)
|
||||
->where('partner_policy.manager_id', (int) $manager_id)
|
||||
->where('pe.agent_id IS NOT NULL', null, false)
|
||||
->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
|
||||
)
|
||||
->groupBy('pe.agent_id');
|
||||
|
||||
|
||||
|
||||
$data = $query->findAll();
|
||||
|
||||
// Grand total (optional but useful for FE)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user