GWM: payout report api
This commit is contained in:
parent
d2ecb2459e
commit
994b0fdbfb
@ -1644,9 +1644,12 @@ class PolicyController extends ResourceController
|
||||
/**
|
||||
* GET: commission / payout rows with filters. Invoice "raised" = active row in partner_invoice_items for policy_id.
|
||||
*
|
||||
* Query: from_date, to_date (issued_date), agent_id (partner_policy.agent_id), payout_raised=yes|no
|
||||
* Query: from_date, to_date (issued_date), agent_id (partner_policy.agent_id), payout_raised=yes|no|all
|
||||
* payout_raised=all: include both invoice-raised and not-raised policies (no EXISTS filter).
|
||||
* At least one filter required among: from_date, to_date, agent_id, payout_raised.
|
||||
*
|
||||
* Response includes summary: total / raised / pending commission sums (commission_amount).
|
||||
*
|
||||
* List columns also include: reg_no, weight, fuel_type, vehicle_type, utr_no (from partner_invoice_utr via invoice).
|
||||
*/
|
||||
public function commissionPayoutReport()
|
||||
@ -1667,11 +1670,11 @@ class PolicyController extends ResourceController
|
||||
|
||||
if ($payoutRaised !== null && $payoutRaised !== '') {
|
||||
$payoutRaised = strtolower(trim((string) $payoutRaised));
|
||||
if (! in_array($payoutRaised, ['yes', 'no'], true)) {
|
||||
if (! in_array($payoutRaised, ['yes', 'no', 'all'], true)) {
|
||||
return $this->respond([
|
||||
'status' => 'failed',
|
||||
'code' => 400,
|
||||
'data' => 'payout_raised must be yes or no',
|
||||
'data' => 'payout_raised must be yes, no, or all',
|
||||
], 400);
|
||||
}
|
||||
} else {
|
||||
@ -1679,7 +1682,8 @@ class PolicyController extends ResourceController
|
||||
}
|
||||
|
||||
$hasDateFilter = $fromDate !== null || $toDate !== null;
|
||||
if (! $hasDateFilter && $agentId === null && $payoutRaised === null) {
|
||||
$hasPayoutFilter = $payoutRaised !== null;
|
||||
if (! $hasDateFilter && $agentId === null && ! $hasPayoutFilter) {
|
||||
return $this->respond([
|
||||
'status' => 'failed',
|
||||
'code' => 400,
|
||||
@ -1710,17 +1714,36 @@ class PolicyController extends ResourceController
|
||||
} elseif ($payoutRaised === 'no') {
|
||||
$builder->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 = pp.id AND pii2.is_active = 1)', null, false);
|
||||
}
|
||||
// payout_raised=all: no EXISTS / NOT EXISTS filter (both cases).
|
||||
|
||||
$builder->orderBy('pa.agent_code', 'ASC');
|
||||
$builder->orderBy('pp.policy_number', 'ASC');
|
||||
|
||||
$rows = $builder->get()->getResultArray();
|
||||
|
||||
$sumTotal = 0.0;
|
||||
$sumRaised = 0.0;
|
||||
$sumPending = 0.0;
|
||||
foreach ($rows as $row) {
|
||||
$amt = (float) ($row['commission_amount'] ?? 0);
|
||||
$sumTotal += $amt;
|
||||
if (($row['received_or_not'] ?? '') === 'yes') {
|
||||
$sumRaised += $amt;
|
||||
} else {
|
||||
$sumPending += $amt;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->respond([
|
||||
'status' => 'success',
|
||||
'code' => 200,
|
||||
'data' => $rows,
|
||||
'count' => count($rows),
|
||||
'summary' => [
|
||||
'total' => round($sumTotal, 2),
|
||||
'raised' => round($sumRaised, 2),
|
||||
'pending' => round($sumPending, 2),
|
||||
],
|
||||
], 200);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->respond([
|
||||
|
||||
Loading…
Reference in New Issue
Block a user