FIX_BDS_REPORT
This commit is contained in:
parent
611ce09e51
commit
82edf33eca
@ -2430,7 +2430,7 @@
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getBDSReportList($start_date = 0, $end_date = 0, $client_id = 0, $insurer_id = 0, $policy_type_id = 0, $date_type = 0, $issuer = 0, $client_branch_id = 0, $insurer_branch_id = 0, $client_policy_id = 0, $user_id = 0, $where = [])
|
||||
public function getBDSReportListSingleUnion($start_date = 0, $end_date = 0, $client_id = 0, $insurer_id = 0, $policy_type_id = 0, $date_type = 0, $issuer = 0, $client_branch_id = 0, $insurer_branch_id = 0, $client_policy_id = 0, $user_id = 0, $where = [])
|
||||
{
|
||||
|
||||
$statement_month_condition = '';
|
||||
@ -2688,6 +2688,576 @@
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT
|
||||
pt.id AS id,
|
||||
pt.endorsement_no,
|
||||
pt.ref,
|
||||
|
||||
pt.data_received_date,
|
||||
pt.renewal_date,
|
||||
pt.installment,
|
||||
nhance_branch.branch_name as nhance_branch,
|
||||
|
||||
DATE_FORMAT(pt.policy_issue_date, '%d %b %Y') AS policy_issue_date,
|
||||
DATE_FORMAT(IF(insq.month IS NULL, pcsd.pt_policy_issue_date, insq.month),'%b %Y') AS policy_issue_month,
|
||||
insq.month as statement_month,
|
||||
|
||||
'statement uploaded' AS statement_uploaded,
|
||||
|
||||
CASE
|
||||
WHEN c.client_type = 1 THEN 'Group'
|
||||
WHEN c.client_type = 2 THEN 'Retail'
|
||||
ELSE '-'
|
||||
END AS client_type,
|
||||
|
||||
CASE
|
||||
WHEN pt.revenue_type = 'NA' THEN 'Fresh'
|
||||
ELSE 'Renewal'
|
||||
END AS revenue_type,
|
||||
|
||||
CASE
|
||||
WHEN
|
||||
IFNULL(cssd.actual_tep_brokerage_amt, 0) = 0
|
||||
AND IFNULL(cssd.actual_tp_brokerage_amt, 0) = 0
|
||||
AND IFNULL(cssd.actual_bp_brokerage_amt, 0) = 0
|
||||
AND IFNULL(cssd.reward, 0) != 0
|
||||
THEN 'Rewards'
|
||||
|
||||
WHEN pt.action_type = 'inception'
|
||||
THEN 'Policy'
|
||||
|
||||
ELSE 'Endorsement'
|
||||
END AS action_type,
|
||||
|
||||
|
||||
pt.action_type as action_type_string,
|
||||
|
||||
c.client_name,
|
||||
c.short_name AS client_short_name,
|
||||
cb.branch_name AS client_branch_name,
|
||||
cb.address1 AS client_address,
|
||||
ptype.policy_type,
|
||||
ptype.bap,
|
||||
ins.id AS insurer_id,
|
||||
ins.name AS insurer_name,
|
||||
ins.short_name AS insurer_short_name,
|
||||
ib.branch_name AS insurer_branch_name,
|
||||
ib.branch_code AS insurer_branch_code,
|
||||
|
||||
created_user.first_name as user_name,
|
||||
|
||||
v.vehicle_no,
|
||||
tpa.name AS tpa_name,
|
||||
|
||||
pcsd.remark AS remarks,
|
||||
pcsd.cop_amt AS bp_amt,
|
||||
pcsd.exp_amt,
|
||||
pcsd.id AS pt_id,
|
||||
|
||||
su.first_name AS salse_person_name,
|
||||
se.first_name AS service_person_name,
|
||||
|
||||
ROUND(pcsd.cop_amt + pcsd.cotp_amt + pcsd.cotep_amt,2) AS premium_wo_gst,
|
||||
ROUND((pcsd.cop_amt + pcsd.cotp_amt + pcsd.cotep_amt) * 0.18,2) AS gst_amount,
|
||||
ROUND((pcsd.cop_amt + pcsd.cotp_amt + pcsd.cotep_amt) * 1.18,2) AS total_premium,
|
||||
|
||||
ROUND(pcsd.cotp_amt + pcsd.cotep_amt,2) AS tp_or_ter,
|
||||
DATEDIFF(pt.policy_end_date, CURDATE()) AS days,
|
||||
(pcsd.agreed_tp_per + pcsd.agreed_tep_per) AS agreed_tp_or_ter_per,
|
||||
|
||||
CASE
|
||||
WHEN insq.month = pt.month THEN pcsd.exp_amt
|
||||
ELSE 0
|
||||
END AS total_irda_amt_2,
|
||||
|
||||
COALESCE((
|
||||
SELECT SUM(cs.reward)
|
||||
FROM co_share_stmt_details cs
|
||||
JOIN insurer_statements i ON cs.statement_id = i.id
|
||||
WHERE cs.co_share_id = pcsd.id AND i.month = insq.month
|
||||
AND i.is_active = 1
|
||||
AND cs.is_active = 1
|
||||
GROUP BY pt.policy_no, i.month, pcsd.insurer_id
|
||||
),0) AS reward,
|
||||
|
||||
COALESCE((
|
||||
SELECT SUM(
|
||||
COALESCE(cs.actual_bp_brokerage_amt,0) +
|
||||
COALESCE(cs.actual_tp_brokerage_amt,0) +
|
||||
COALESCE(cs.actual_tep_brokerage_amt,0) +
|
||||
COALESCE(cs.reward,0)
|
||||
)
|
||||
FROM co_share_stmt_details cs
|
||||
JOIN insurer_statements i ON cs.statement_id = i.id
|
||||
WHERE cs.co_share_id = pcsd.id
|
||||
AND i.invoice_status IS NOT NULL
|
||||
AND i.month = insq.month
|
||||
AND i.is_active = 1
|
||||
AND cs.is_active = 1
|
||||
GROUP BY pt.policy_no, i.month, pcsd.insurer_id
|
||||
),0) AS billed_amt,
|
||||
|
||||
CASE
|
||||
WHEN pcsd.co_share_type IN (0,1) THEN pt.policy_no
|
||||
WHEN pcsd.co_share_type > 1
|
||||
THEN IFNULL(NULLIF(pcsd.follower_policy_no,''), pt.policy_no)
|
||||
ELSE pt.policy_no
|
||||
END AS policy_no,
|
||||
|
||||
CASE
|
||||
WHEN pt.co_share = 1 THEN 'Yes'
|
||||
ELSE 'No'
|
||||
END AS co_share,
|
||||
|
||||
CASE
|
||||
WHEN pt.bro_payable_by = 1 THEN 'Yes'
|
||||
ELSE 'No'
|
||||
END AS bro_payable_by,
|
||||
|
||||
pcsd.non_comm_per_amt,
|
||||
|
||||
pcsd.bp_igst,
|
||||
pcsd.bp_sgst,
|
||||
pcsd.bp_cgst,
|
||||
|
||||
pcsd.agreed_bp_per,
|
||||
ROUND(pcsd.agreed_tp_per + pcsd.agreed_tep_per,2) AS agreed_tp_per,
|
||||
|
||||
pcsd.standerd_bp_per,
|
||||
ROUND(pcsd.standerd_tp_per + pcsd.standerd_tep_per,2) AS standerd_tp_per,
|
||||
|
||||
cssd.actual_bp_amt,
|
||||
ROUND(cssd.actual_tp_amt + cssd.actual_tep_amt,2) AS actual_tp_amt,
|
||||
|
||||
cssd.actual_bp_per,
|
||||
ROUND(cssd.actual_tp_per + cssd.actual_tep_per,2) AS actual_tp_per,
|
||||
|
||||
cssd.actual_tep_brokerage_amt,
|
||||
ROUND(cssd.actual_tp_brokerage_amt + cssd.actual_bp_brokerage_amt,2) AS actual_tp_brokerage_amt,
|
||||
|
||||
service_branch.branch_name as service_branch,
|
||||
|
||||
salse_manager.first_name as salse_manager_name,
|
||||
service_manager.first_name as service_manager_name,
|
||||
cd_master.cd_ac_no,
|
||||
pt.rollover_date,
|
||||
pt.policy_holder_name,
|
||||
|
||||
CASE
|
||||
WHEN pt.same_as_proposer = 1 THEN 'Yes'
|
||||
ELSE 'No'
|
||||
END AS same_as_proposer,
|
||||
|
||||
pcsd.follower_policy_no,
|
||||
pcsd.co_share_per,
|
||||
pcsd.stamp_duty
|
||||
|
||||
FROM policy_transaction pt
|
||||
LEFT JOIN pt_co_share_details pcsd ON pt.id = pcsd.pt_id
|
||||
LEFT JOIN co_share_stmt_details cssd ON pcsd.id = cssd.co_share_id
|
||||
LEFT JOIN insurer_statements insq ON cssd.statement_id = insq.id
|
||||
JOIN clients c ON c.id = pt.client_id
|
||||
LEFT JOIN client_branch cb ON pt.client_branch_id = cb.id
|
||||
LEFT JOIN client_policy cp ON pt.client_policy_id = cp.id
|
||||
LEFT JOIN user_profiles up ON pt.created_by = up.id
|
||||
LEFT JOIN vehicle v ON pt.vehicle_id = v.id
|
||||
LEFT JOIN policy_type ptype ON pt.policy_type_id = ptype.id
|
||||
LEFT JOIN insurers ins ON pcsd.insurer_id = ins.id
|
||||
LEFT JOIN insurer_branch ib ON pcsd.insurer_branch_id = ib.id
|
||||
LEFT JOIN tpa ON pt.tpa_id = tpa.id
|
||||
LEFT JOIN tpa_branch tb ON pt.tpa_branch_id = tb.id
|
||||
LEFT JOIN user_profiles su ON pt.sales_generated_by = su.id
|
||||
LEFT JOIN user_profiles se ON pt.serviced_by = se.id
|
||||
LEFT JOIN user_profiles created_user ON pt.created_by = created_user.id
|
||||
LEFT JOIN nhance_branch ON pt.issuer_branch = nhance_branch.id
|
||||
|
||||
LEFT JOIN nhance_branch service_branch ON pt.service_person_branch_id = service_branch.id
|
||||
LEFT JOIN user_profiles salse_manager ON pt.salse_person_manager_id = salse_manager.id
|
||||
LEFT JOIN user_profiles service_manager ON pt.service_person_manager_id = service_manager.id
|
||||
LEFT JOIN cd_master ON pt.cd_ac_pk = cd_master.id
|
||||
|
||||
|
||||
WHERE pt.is_active = 1
|
||||
AND pcsd.is_active = 1
|
||||
AND cssd.is_active = 1
|
||||
AND insq.is_active = 1
|
||||
$default_date_filter
|
||||
$conditions
|
||||
|
||||
GROUP BY pt.policy_no, pt.endorsement_no, pcsd.insurer_id, insq.month
|
||||
|
||||
) AS final_result
|
||||
|
||||
$statement_month_condition
|
||||
-- GROUP BY statement_month, insurer_name, policy_no
|
||||
-- WHERE id = 6063
|
||||
|
||||
ORDER BY
|
||||
id DESC,
|
||||
policy_no ASC,
|
||||
insurer_branch_name ASC,
|
||||
statement_uploaded ASC,
|
||||
statement_month ASC,
|
||||
STR_TO_DATE(policy_issue_month, '%b %Y') ASC
|
||||
";
|
||||
$query = $this->db->query($sql);
|
||||
$result = $query->getResultArray();
|
||||
// dd($result);
|
||||
// dd($this->db->getLastQuery());
|
||||
|
||||
$keys = [];
|
||||
$filtered = [];
|
||||
|
||||
foreach ($result as $row) {
|
||||
|
||||
$endorsement_number = "-";
|
||||
if(!empty($row['endorsement_no'])){
|
||||
$endorsement_number = $row['endorsement_no'];
|
||||
}
|
||||
|
||||
$key = $row['statement_month'].'|'.$row['insurer_name'].'|'.$row['policy_no'] . '|' . $endorsement_number;
|
||||
|
||||
// If uploaded record exists, always keep it and override previous
|
||||
if ($row['statement_uploaded'] === 'statement uploaded') {
|
||||
$filtered[$key] = $row; // overwrite no-statement row if it exists
|
||||
$keys[$key] = true;
|
||||
|
||||
}
|
||||
// Keep "no statement uploaded" only if uploaded one not added yet
|
||||
elseif (!isset($keys[$key])) {
|
||||
$filtered[$key] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
$result = array_values($filtered);
|
||||
// dd($result);
|
||||
|
||||
// $runningBilled = [];
|
||||
// $totalIrdaMap = [];
|
||||
// $final = [];
|
||||
|
||||
// foreach ($result as $row) {
|
||||
// $ptId = $row['pt_id'];
|
||||
|
||||
// // Store total_irda_amt only once (first non-zero value)
|
||||
// if (!isset($totalIrdaMap[$ptId]) && $row['total_irda_amt'] > 0) {
|
||||
// $totalIrdaMap[$ptId] = $row['total_irda_amt'];
|
||||
// }
|
||||
|
||||
// // Initialize running sum
|
||||
// if (!isset($runningBilled[$ptId])) {
|
||||
// $runningBilled[$ptId] = 0;
|
||||
// }
|
||||
|
||||
// // Add billed amount to running total
|
||||
// $runningBilled[$ptId] += (float)$row['billed_amt'];
|
||||
|
||||
// // Calculate unbilled amount
|
||||
// $row['unbilled_amount'] = ($totalIrdaMap[$ptId] ?? 0) - $runningBilled[$ptId];
|
||||
|
||||
// $final[] = $row;
|
||||
// }
|
||||
|
||||
$totalBilled = [];
|
||||
$totalIrdaMap = [];
|
||||
|
||||
foreach ($result as $row) {
|
||||
$key = $row['pt_id'].'-'.$row['insurer_id'];
|
||||
|
||||
// Sum billed amount
|
||||
$totalBilled[$key] = ($totalBilled[$key] ?? 0)
|
||||
+ (float) ($row['billed_amt'] ?? 0);
|
||||
|
||||
// Store total_irda_amt once
|
||||
if (!isset($totalIrdaMap[$key]) && ($row['total_irda_amt'] ?? 0) > 0) {
|
||||
$totalIrdaMap[$key] = (float) $row['total_irda_amt'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$ptSeen = [];
|
||||
$final = [];
|
||||
|
||||
foreach ($result as $row) {
|
||||
$ptId = $row['pt_id'].'-'.$row['insurer_id'];
|
||||
if (!isset($ptSeen[$ptId])) {
|
||||
// First entry → set unbilled amount
|
||||
$row['unbilled_amount'] = ($totalIrdaMap[$ptId] ?? 0) - ($totalBilled[$ptId] ?? 0);
|
||||
|
||||
$ptSeen[$ptId] = true;
|
||||
} else {
|
||||
// Other entries → zero
|
||||
$row['unbilled_amount'] = 0;
|
||||
}
|
||||
|
||||
$final[] = $row;
|
||||
}
|
||||
|
||||
$result = $final;
|
||||
|
||||
// print_rr($result); die;
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getBDSReportList($start_date = 0, $end_date = 0, $client_id = 0, $insurer_id = 0, $policy_type_id = 0, $date_type = 0, $issuer = 0, $client_branch_id = 0, $insurer_branch_id = 0, $client_policy_id = 0, $user_id = 0, $where = [])
|
||||
{
|
||||
|
||||
$statement_month_condition = '';
|
||||
if ($date_type == 'statement_month' && $start_date != 0 && $end_date != 0) {
|
||||
$statement_month_condition = "
|
||||
WHERE statement_month >= '" . $start_date . "'
|
||||
AND statement_month <= '" . $end_date . "'
|
||||
";
|
||||
}
|
||||
|
||||
$default_date_filter = '';
|
||||
if ($client_id == 0 && $insurer_id == 0 && $policy_type_id == 0 && $date_type == 0 && $issuer == 0) {
|
||||
|
||||
$fromDate = date('Y-m-d', strtotime('-90 days'));
|
||||
$toDate = date('Y-m-d 23:59:59');
|
||||
|
||||
if (empty($where)) {
|
||||
|
||||
$default_date_filter = "
|
||||
AND pt.created_at >= '" . $fromDate . "'
|
||||
AND pt.created_at <= '" . $toDate . "'
|
||||
";
|
||||
}
|
||||
}
|
||||
|
||||
$conditions = ""; // start safely
|
||||
|
||||
// 1. Role-based restrictions
|
||||
if (
|
||||
(!in_array(get_role_id(), [1, 5])) &&
|
||||
!(
|
||||
in_array(MANAGEMENT_TEAM_ID, user_team()) ||
|
||||
in_array(FINANCE_TEAM_ID, user_team()) ||
|
||||
in_array(BUSINESS_TEAM_ID, user_team())
|
||||
)
|
||||
) {
|
||||
if (get_role_id() == 4 && in_array(POS_TEAM_ID, user_team())) {
|
||||
$conditions .= " AND pt.created_by = " . get_session_userid();
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Dynamic $where array
|
||||
if (!empty($where)) {
|
||||
foreach ($where as $column => $value) {
|
||||
$value = addslashes($value);
|
||||
$conditions .= " AND `$column` = '$value' ";
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Date condition (except statement_month)
|
||||
if ($start_date != 0 && $end_date != 0 && $date_type != 0 && $date_type != 'statement_month') {
|
||||
|
||||
$startDate = date('Y-m-d 00:00:00', strtotime($start_date));
|
||||
$endDate = date('Y-m-d 23:59:59', strtotime($end_date));
|
||||
|
||||
if ($date_type === "policy_issue_date") {
|
||||
$conditions .= " AND pcsd.pt_policy_issue_date >= '$startDate' ";
|
||||
$conditions .= " AND pcsd.pt_policy_issue_date <= '$endDate' ";
|
||||
} else {
|
||||
$conditions .= " AND pt.$date_type >= '$startDate' ";
|
||||
$conditions .= " AND pt.$date_type <= '$endDate' ";
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Common filters
|
||||
if ($client_id != 0) {
|
||||
$conditions .= " AND pt.client_id = $client_id ";
|
||||
}
|
||||
|
||||
if ($insurer_id != 0) {
|
||||
$conditions .= " AND pt.insurer_id = $insurer_id ";
|
||||
}
|
||||
|
||||
if ($client_branch_id != 0) {
|
||||
$conditions .= " AND pt.client_branch_id = $client_branch_id ";
|
||||
}
|
||||
|
||||
if ($insurer_branch_id != 0) {
|
||||
$conditions .= " AND pt.insurer_branch_id = $insurer_branch_id ";
|
||||
}
|
||||
|
||||
if ($client_policy_id != 0) {
|
||||
$conditions .= " AND pt.client_policy_id = $client_policy_id ";
|
||||
}
|
||||
|
||||
if ($user_id != 0) {
|
||||
$conditions .= " AND pt.created_by = $user_id ";
|
||||
}
|
||||
|
||||
if ($policy_type_id != 0) {
|
||||
$conditions .= " AND pt.policy_type_id = $policy_type_id ";
|
||||
}
|
||||
|
||||
if ($issuer != 0) {
|
||||
$conditions .= " AND pt.issuer = $issuer ";
|
||||
}
|
||||
|
||||
|
||||
$sql = "
|
||||
SELECT * FROM (
|
||||
|
||||
SELECT
|
||||
pt.id AS id,
|
||||
pt.endorsement_no,
|
||||
pt.ref,
|
||||
|
||||
pt.data_received_date,
|
||||
pt.renewal_date,
|
||||
pt.installment,
|
||||
nhance_branch.branch_name as nhance_branch,
|
||||
|
||||
DATE_FORMAT(pt.policy_issue_date, '%d %b %Y') AS policy_issue_date,
|
||||
DATE_FORMAT(pcsd.pt_policy_issue_date, '%b %Y') AS policy_issue_month,
|
||||
pt.month as statement_month,
|
||||
|
||||
'no statement uploaded' AS statement_uploaded,
|
||||
|
||||
CASE
|
||||
WHEN c.client_type = 1 THEN 'Group'
|
||||
WHEN c.client_type = 2 THEN 'Retail'
|
||||
ELSE '-'
|
||||
END AS client_type,
|
||||
|
||||
CASE
|
||||
WHEN pt.revenue_type = 'NA' THEN 'Fresh'
|
||||
ELSE 'Renewal'
|
||||
END AS revenue_type,
|
||||
|
||||
CASE
|
||||
WHEN pt.action_type = 'inception' THEN 'Policy'
|
||||
ELSE 'Endorsement'
|
||||
END AS action_type,
|
||||
|
||||
pt.action_type as action_type_string,
|
||||
|
||||
c.client_name,
|
||||
c.short_name AS client_short_name,
|
||||
cb.branch_name AS client_branch_name,
|
||||
cb.address1 AS client_address,
|
||||
ptype.policy_type,
|
||||
ptype.bap,
|
||||
ins.id AS insurer_id,
|
||||
ins.name AS insurer_name,
|
||||
ins.short_name AS insurer_short_name,
|
||||
ib.branch_name AS insurer_branch_name,
|
||||
ib.branch_code AS insurer_branch_code,
|
||||
|
||||
created_user.first_name AS user_name,
|
||||
v.vehicle_no,
|
||||
tpa.name AS tpa_name,
|
||||
|
||||
pcsd.remark AS remarks,
|
||||
pcsd.cop_amt AS bp_amt,
|
||||
pcsd.exp_amt,
|
||||
pcsd.id AS pt_id,
|
||||
|
||||
su.first_name AS salse_person_name,
|
||||
se.first_name AS service_person_name,
|
||||
|
||||
ROUND(pcsd.cop_amt + pcsd.cotp_amt + pcsd.cotep_amt, 2) AS premium_wo_gst,
|
||||
ROUND((pcsd.cop_amt + pcsd.cotp_amt + pcsd.cotep_amt) * 0.18, 2) AS gst_amount,
|
||||
ROUND((pcsd.cop_amt + pcsd.cotp_amt + pcsd.cotep_amt) * 1.18, 2) AS total_premium,
|
||||
|
||||
ROUND(pcsd.cotp_amt + pcsd.cotep_amt, 2) AS tp_or_ter,
|
||||
DATEDIFF(pt.policy_end_date, CURDATE()) AS days,
|
||||
(pcsd.agreed_tp_per + pcsd.agreed_tep_per) AS agreed_tp_or_ter_per,
|
||||
|
||||
ROUND(pcsd.exp_amt, 2) AS total_irda_amt,
|
||||
0.00 AS reward,
|
||||
0.00 AS billed_amt,
|
||||
|
||||
CASE
|
||||
WHEN pcsd.co_share_type IN (0,1) THEN pt.policy_no
|
||||
WHEN pcsd.co_share_type > 1 THEN
|
||||
IFNULL(NULLIF(pcsd.follower_policy_no,''), pt.policy_no)
|
||||
ELSE pt.policy_no
|
||||
END AS policy_no,
|
||||
|
||||
CASE
|
||||
WHEN pt.co_share = 1 THEN 'Yes'
|
||||
ELSE 'No'
|
||||
END AS co_share,
|
||||
|
||||
CASE
|
||||
WHEN pt.bro_payable_by = 1 THEN 'Yes'
|
||||
ELSE 'No'
|
||||
END AS bro_payable_by,
|
||||
|
||||
pcsd.non_comm_per_amt,
|
||||
|
||||
pcsd.bp_igst,
|
||||
pcsd.bp_sgst,
|
||||
pcsd.bp_cgst,
|
||||
|
||||
pcsd.agreed_bp_per,
|
||||
ROUND(pcsd.agreed_tp_per + pcsd.agreed_tep_per,2) AS agreed_tp_per,
|
||||
|
||||
pcsd.standerd_bp_per,
|
||||
ROUND(pcsd.standerd_tp_per + pcsd.standerd_tep_per,2) AS standerd_tp_per,
|
||||
|
||||
0 AS actual_bp_amt,
|
||||
ROUND(0, 2) AS actual_tp_amt,
|
||||
|
||||
0 AS actual_bp_per,
|
||||
ROUND(0, 2) AS actual_tp_per,
|
||||
|
||||
0 AS actual_tep_brokerage_amt,
|
||||
ROUND(0, 2) AS actual_tp_brokerage_amt,
|
||||
|
||||
service_branch.branch_name as service_branch,
|
||||
|
||||
salse_manager.first_name as salse_manager_name,
|
||||
service_manager.first_name as service_manager_name,
|
||||
cd_master.cd_ac_no,
|
||||
pt.rollover_date,
|
||||
pt.policy_holder_name,
|
||||
|
||||
CASE
|
||||
WHEN pt.same_as_proposer = 1 THEN 'Yes'
|
||||
ELSE 'No'
|
||||
END AS same_as_proposer,
|
||||
|
||||
pcsd.follower_policy_no,
|
||||
pcsd.co_share_per,
|
||||
pcsd.stamp_duty,
|
||||
'first' as query
|
||||
|
||||
FROM policy_transaction pt
|
||||
LEFT JOIN pt_co_share_details pcsd ON pt.id = pcsd.pt_id
|
||||
JOIN clients c ON c.id = pt.client_id
|
||||
LEFT JOIN client_branch cb ON pt.client_branch_id = cb.id
|
||||
LEFT JOIN client_policy cp ON pt.client_policy_id = cp.id
|
||||
LEFT JOIN user_profiles up ON pt.created_by = up.id
|
||||
LEFT JOIN vehicle v ON pt.vehicle_id = v.id
|
||||
LEFT JOIN policy_type ptype ON pt.policy_type_id = ptype.id
|
||||
LEFT JOIN insurers ins ON pcsd.insurer_id = ins.id
|
||||
LEFT JOIN insurer_branch ib ON pcsd.insurer_branch_id = ib.id
|
||||
LEFT JOIN tpa ON pt.tpa_id = tpa.id
|
||||
LEFT JOIN tpa_branch tb ON pt.tpa_branch_id = tb.id
|
||||
LEFT JOIN user_profiles su ON pt.sales_generated_by = su.id
|
||||
LEFT JOIN user_profiles se ON pt.serviced_by = se.id
|
||||
LEFT JOIN user_profiles created_user ON pt.created_by = created_user.id
|
||||
LEFT JOIN nhance_branch ON pt.issuer_branch = nhance_branch.id
|
||||
|
||||
LEFT JOIN nhance_branch service_branch ON pt.service_person_branch_id = service_branch.id
|
||||
LEFT JOIN user_profiles salse_manager ON pt.salse_person_manager_id = salse_manager.id
|
||||
LEFT JOIN user_profiles service_manager ON pt.service_person_manager_id = service_manager.id
|
||||
LEFT JOIN cd_master ON pt.cd_ac_pk = cd_master.id
|
||||
|
||||
WHERE pt.is_active = 1
|
||||
AND pcsd.is_active = 1
|
||||
$default_date_filter
|
||||
$conditions
|
||||
|
||||
GROUP BY pt.policy_no, pt.endorsement_no, pcsd.insurer_id
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT
|
||||
pt.id AS id,
|
||||
pt.endorsement_no,
|
||||
@ -2831,7 +3401,8 @@
|
||||
|
||||
pcsd.follower_policy_no,
|
||||
pcsd.co_share_per,
|
||||
pcsd.stamp_duty
|
||||
pcsd.stamp_duty,
|
||||
'second' as query
|
||||
|
||||
FROM policy_transaction pt
|
||||
LEFT JOIN pt_co_share_details pcsd ON pt.id = pcsd.pt_id
|
||||
@ -3007,7 +3578,8 @@
|
||||
|
||||
pcsd.follower_policy_no,
|
||||
pcsd.co_share_per,
|
||||
pcsd.stamp_duty
|
||||
pcsd.stamp_duty,
|
||||
'third' as query
|
||||
|
||||
FROM policy_transaction pt
|
||||
LEFT JOIN pt_co_share_details pcsd ON pt.id = pcsd.pt_id
|
||||
@ -3056,120 +3628,34 @@
|
||||
";
|
||||
$query = $this->db->query($sql);
|
||||
$result = $query->getResultArray();
|
||||
// $countofalldata = count($result);
|
||||
// dd($result);
|
||||
// dd($this->db->getLastQuery());
|
||||
|
||||
|
||||
// foreach ($result as $key => &$row) {
|
||||
|
||||
// // If both rewards and billed amount are zero, remove the row
|
||||
// if (
|
||||
// ($row['reward'] ?? 0) == 0.00 &&
|
||||
// ($row['billed_amt'] ?? 0) == 0.00
|
||||
// ) {
|
||||
// unset($result[$key]); // ✅ correct way
|
||||
// }
|
||||
|
||||
// // If rewards exist, zero the IRDA amount
|
||||
// // if (($row['reward'] ?? 0) != 0.00) {
|
||||
// // $row['total_irda_amt'] = '0.00';
|
||||
// // $row['billed_amt'] = $row['reward'];
|
||||
// // }
|
||||
|
||||
// $reward = (float) ($row['reward'] ?? 0);
|
||||
|
||||
// if ($reward > 0) {
|
||||
// $row['total_irda_amt'] = '0.00';
|
||||
// $row['billed_amt'] = $reward;
|
||||
// }
|
||||
|
||||
// }
|
||||
// unset($row);
|
||||
|
||||
// // $result = array_values($result);
|
||||
|
||||
// usort($result, function ($a, $b) {
|
||||
|
||||
// // First: group by policy_issue_date
|
||||
// if ($a['policy_issue_month'] === $b['policy_issue_month']) {
|
||||
|
||||
// // Priority order for action_type
|
||||
// $priority = [
|
||||
// 'Policy' => 1,
|
||||
// 'Endorsement' => 2,
|
||||
// 'Rewards' => 3
|
||||
// ];
|
||||
|
||||
// $aPriority = $priority[$a['action_type']] ?? 99;
|
||||
// $bPriority = $priority[$b['action_type']] ?? 99;
|
||||
|
||||
// return $aPriority <=> $bPriority;
|
||||
// }
|
||||
|
||||
// // Otherwise sort by policy_issue_date
|
||||
// return strtotime($a['policy_issue_month']) <=> strtotime($b['policy_issue_month']);
|
||||
// });
|
||||
|
||||
// dd($result);
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------
|
||||
|
||||
// $keys = [];
|
||||
// $filtered = [];
|
||||
|
||||
// foreach ($result as $row) {
|
||||
|
||||
// $endorsement_number = "-";
|
||||
// if(!empty($row['endorsement_no'])){
|
||||
// $endorsement_number = $row['endorsement_no'];
|
||||
// }
|
||||
|
||||
// $key = $row['statement_month'].'|'.$row['insurer_name'].'|'.$row['policy_no'] . '|' . $endorsement_number;
|
||||
|
||||
// // If uploaded record exists, always keep it and override previous
|
||||
// if ($row['statement_uploaded'] === 'statement uploaded') {
|
||||
// $filtered[$key] = $row; // overwrite no-statement row if it exists
|
||||
// $keys[$key] = true;
|
||||
|
||||
// }
|
||||
// // Keep "no statement uploaded" only if uploaded one not added yet
|
||||
// elseif (!isset($keys[$key])) {
|
||||
// $filtered[$key] = $row;
|
||||
// }
|
||||
// }
|
||||
|
||||
// $result = array_values($filtered);
|
||||
// dd($result);
|
||||
|
||||
$keys = [];
|
||||
$filtered = [];
|
||||
|
||||
foreach ($result as $row) {
|
||||
|
||||
// Normalize endorsement number
|
||||
$endorsement_number = !empty($row['endorsement_no'])? $row['endorsement_no'] : '-';
|
||||
$endorsement_number = !empty($row['endorsement_no']) ? $row['endorsement_no'] : '-';
|
||||
|
||||
// $month = $row['policy_issue_month'];
|
||||
$reward = (float) ($row['reward'] ?? 0);
|
||||
// $billed_amt = (float) ($row['billed_amt'] ?? 0);
|
||||
|
||||
// if (
|
||||
// ($monthCount[$month] ?? 0) > 1 &&
|
||||
// $reward == 0.00 &&
|
||||
// $billed_amt == 0.00
|
||||
// ) {
|
||||
// continue;
|
||||
// }
|
||||
$billed_amt = (float) ($row['billed_amt'] ?? 0);
|
||||
|
||||
if ($reward > 0 && ($row['billed_amt'] ?? 0) == 0.00) {
|
||||
$row['total_irda_amt'] = '0.00';
|
||||
$row['billed_amt'] = $reward;
|
||||
$rewardFlag = 'R'; // Reward row
|
||||
} else {
|
||||
$rewardFlag = 'N'; // Normal row
|
||||
if($reward == 0.00 && $billed_amt == 0.00 && $row['statement_uploaded'] === 'statement uploaded'){
|
||||
continue;
|
||||
}
|
||||
|
||||
// ✅ Stable & deterministic key
|
||||
if ($reward > 0 && ($row['billed_amt'] ?? 0) == 0.00) {
|
||||
$row['total_irda_amt'] = '0.00';
|
||||
$row['billed_amt'] =
|
||||
$reward; $rewardFlag = 'R'; // Reward row
|
||||
} else {
|
||||
$rewardFlag = 'N'; // Normal row
|
||||
}
|
||||
|
||||
// ✅ Stable key (NO reward flag)
|
||||
$key = implode('|', [
|
||||
$row['statement_month'],
|
||||
$row['insurer_name'],
|
||||
@ -3189,37 +3675,10 @@
|
||||
}
|
||||
}
|
||||
|
||||
// Reindex for output
|
||||
// Reindex final output
|
||||
$result = array_values($filtered);
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// $runningBilled = [];
|
||||
// $totalIrdaMap = [];
|
||||
// $final = [];
|
||||
|
||||
// foreach ($result as $row) {
|
||||
// $ptId = $row['pt_id'];
|
||||
|
||||
// // Store total_irda_amt only once (first non-zero value)
|
||||
// if (!isset($totalIrdaMap[$ptId]) && $row['total_irda_amt'] > 0) {
|
||||
// $totalIrdaMap[$ptId] = $row['total_irda_amt'];
|
||||
// }
|
||||
|
||||
// // Initialize running sum
|
||||
// if (!isset($runningBilled[$ptId])) {
|
||||
// $runningBilled[$ptId] = 0;
|
||||
// }
|
||||
|
||||
// // Add billed amount to running total
|
||||
// $runningBilled[$ptId] += (float)$row['billed_amt'];
|
||||
|
||||
// // Calculate unbilled amount
|
||||
// $row['unbilled_amount'] = ($totalIrdaMap[$ptId] ?? 0) - $runningBilled[$ptId];
|
||||
|
||||
// $final[] = $row;
|
||||
// }
|
||||
// dd($countofalldata, $result);
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------
|
||||
|
||||
@ -3239,9 +3698,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
// dd($totalBilled, $totalIrdaMap);
|
||||
|
||||
|
||||
$ptSeen = [];
|
||||
$final = [];
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user