New Report BDS CHanges - VADIVEL J 2025-10-25

This commit is contained in:
vadivelJ96 2025-10-25 16:53:18 +05:30
parent 9d5cabd588
commit 81bc0b7b8b
3 changed files with 288 additions and 100 deletions

View File

@ -426,7 +426,7 @@ $routes->group("policy_tranction", ["filter" => "authMVC"], function ($routes) {
});
$routes->group("report", ["filter" => "authMVC"], function ($routes) {
$routes->match(['get', 'post'],"list", "PolicyTransactionController::reportBDS");
$routes->match(['get', 'post'],"list", "PolicyTransactionController::reportBDSNew");
$routes->match(['get', 'post'],"listNew", "PolicyTransactionController::reportBDSNew");
$routes->get("report-varience-list", "PolicyTransactionController::reportVarience");
$routes->get("report-business-list", "PolicyTransactionController::reportBusinessList");

View File

@ -2404,8 +2404,8 @@ class PolicyTransactionController extends BaseController
// $client_name = preg_replace('/^\p{Z}+|\p{Z}+$/u', '', $excel_row[3]); //clientname from excel
$endorsement_no = preg_replace('/^\p{Z}+|\p{Z}+$/u', '', $excel_row[2]); //policy_end_date from excel
$endorsement_no = preg_replace( '/[\x{200B}\x{200C}\x{200D}\x{FEFF}\x{00A0}\x{200E}\x{200F}\x{202A}-\x{202E}]/u', '', $excel_row[2]); //policy_end_date from excel
$endorsement_no = preg_replace('/^\p{Z}+|\p{Z}+$/u', '', $excel_row[2]??''); //policy_end_date from excel
$endorsement_no = preg_replace( '/[\x{200B}\x{200C}\x{200D}\x{FEFF}\x{00A0}\x{200E}\x{200F}\x{202A}-\x{202E}]/u', '', $excel_row[2]??''); //policy_end_date from excel
// Kint::dump($excel_key,$policy_no,$endorsement_no,$policy_start_date,$policy_end_date);//die();
foreach ($source_data as $source_key => $source_row) {

View File

@ -1829,24 +1829,35 @@ class PolicyTransactionModel extends Model
// ==============================
if ($date_type == 'statement_month' && $start_date != 0 && $end_date != 0) {
$date_condition = "
AND insurer_statements.month >= '{$start_date}'
AND insurer_statements.month <= '{$end_date}'
AND insurer_statements_oq.month >= '{$start_date}'
AND insurer_statements_oq.month <= '{$end_date}'
";
}
// ==============================
// BASE QUERY
// ==============================
$builder = $this->db->table('policy_transaction')
// ==============================
// Without Statement Upload
// ==============================
$builder2 = $this->db->table('policy_transaction')
->select("
policy_transaction.*,
policy_transaction.id AS id,
policy_transaction.endorsement_no AS endorsement_no,
policy_transaction.ref AS ref,
DATE_FORMAT(policy_transaction.policy_issue_date, '%d %b %Y') AS policy_issue_date,
DATE_FORMAT(
IF(policy_transaction.month IS NULL,
policy_transaction.policy_issue_date,
policy_transaction.month
), '%b %Y'
) AS policy_issue_month,
DATE_FORMAT( policy_transaction.policy_issue_date,, '%b %Y') AS policy_issue_month,
CASE
WHEN 1 = 1 THEN 'no statement uploaded'
ELSE 'statement uploaded'
END AS statement_uploaded ,
CASE
WHEN clients.client_type = 1 THEN 'Group'
@ -1898,89 +1909,15 @@ class PolicyTransactionModel extends Model
-- ==============================
-- SUBQUERY: Total IRDA Amount
-- Alias Agreed Amount ...!!
-- ==============================
ROUND((
SELECT (
SUM(co_share_stmt_details.actual_bp_brokerage_amt) +
SUM(co_share_stmt_details.actual_tp_brokerage_amt) +
SUM(co_share_stmt_details.actual_tep_brokerage_amt) +
SUM(co_share_stmt_details.reward)
)
FROM co_share_stmt_details
JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id
WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id
AND co_share_stmt_details.is_active = 1
AND insurer_statements.is_active = 1
{$date_condition}
), 2) AS total_irda_amt,
ROUND(pt_co_share_details.exp_amt, 2) AS total_irda_amt,
-- Reward Only
ROUND((
SELECT SUM(co_share_stmt_details.reward)
FROM co_share_stmt_details
JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id
WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id
AND co_share_stmt_details.is_active = 1
AND insurer_statements.is_active = 1
{$date_condition}
), 2) AS reward,
ROUND(0, 2) AS reward,
-- Billed Amount
ROUND((
SELECT SUM(
COALESCE(co_share_stmt_details.actual_bp_brokerage_amt, 0) +
COALESCE(co_share_stmt_details.actual_tp_brokerage_amt, 0) +
COALESCE(co_share_stmt_details.actual_tep_brokerage_amt, 0) +
COALESCE(co_share_stmt_details.reward, 0)
)
FROM co_share_stmt_details
JOIN pt_co_share_details AS pt_table ON co_share_stmt_details.co_share_id = pt_table.id
JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id
WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id
AND co_share_stmt_details.is_active = 1
AND pt_table.is_active = 1
AND insurer_statements.is_active = 1
AND insurer_statements.invoice_status IS NOT NULL
{$date_condition}
), 2) AS billed_amt,
-- Unbilled Amount
ROUND(
(
(
SELECT
SUM(
COALESCE(co_share_stmt_details.actual_bp_brokerage_amt, 0) +
COALESCE(co_share_stmt_details.actual_tp_brokerage_amt, 0) +
COALESCE(co_share_stmt_details.actual_tep_brokerage_amt, 0) +
COALESCE(co_share_stmt_details.reward, 0)
)
FROM co_share_stmt_details
JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id
WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id
AND co_share_stmt_details.is_active = 1
{$date_condition}
)
-
(
SELECT
SUM(
COALESCE(co_share_stmt_details.actual_bp_brokerage_amt, 0) +
COALESCE(co_share_stmt_details.actual_tp_brokerage_amt, 0) +
COALESCE(co_share_stmt_details.actual_tep_brokerage_amt, 0) +
COALESCE(co_share_stmt_details.reward, 0)
)
FROM co_share_stmt_details
JOIN pt_co_share_details AS pt_table ON co_share_stmt_details.co_share_id = pt_table.id
JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id
WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id
AND co_share_stmt_details.is_active = 1
AND pt_table.is_active = 1
AND insurer_statements.is_active = 1
AND insurer_statements.invoice_status IS NULL
{$date_condition}
)
), 2) AS unbilled_amt,
ROUND(0, 2) AS billed_amt,
created_user.first_name AS user_name,
@ -2012,9 +1949,255 @@ class PolicyTransactionModel extends Model
->join('tpa_branch', 'policy_transaction.tpa_branch_id = tpa_branch.id', 'left')
->join('user_profiles AS sales_user', 'policy_transaction.sales_generated_by = sales_user.id', 'left')
->join('user_profiles AS service_user', 'policy_transaction.serviced_by = service_user.id', 'left')
->join('user_profiles AS created_user', 'policy_transaction.created_by = created_user.id', 'left')
->join('user_profiles AS created_user', 'policy_transaction.created_by = created_user.id', 'left')
->where('policy_transaction.is_active', 1)
->where('pt_co_share_details.is_active', 1);
->where('pt_co_share_details.is_active', 1)
// ========================================================
// GROUP BY Insurer Statement Months
// =======================================================
->groupBy('policy_transaction.policy_no , pt_co_share_details.insurer_id ');
// ==============================
// ROLE-BASED FILTERS
// ==============================
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())) {
$builder2->where('policy_transaction.created_by', get_session_userid());
}
}
// ==============================
// ADDITIONAL FILTERS
// ==============================
if (!empty($where)) {
log_message('info', 'Where condition: ' . json_encode($where));
$builder2->where($where);
}
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));
$builder2->where("policy_transaction.{$date_type} >=", $startDate)
->where("policy_transaction.{$date_type} <=", $endDate);
}
// ==============================
// FILTER BY IDs
// ==============================
if ($client_id != 0) $builder2->where('policy_transaction.client_id', $client_id);
if ($insurer_id != 0) $builder2->where('policy_transaction.insurer_id', $insurer_id);
if ($client_branch_id != 0) $builder2->where('policy_transaction.client_branch_id', $client_branch_id);
if ($insurer_branch_id != 0) $builder2->where('policy_transaction.insurer_branch_id', $insurer_branch_id);
if ($client_policy_id != 0) $builder2->where('policy_transaction.client_policy_id', $client_policy_id);
if ($user_id != 0) $builder2->where('policy_transaction.created_by', $user_id);
if ($policy_type_id != 0) $builder2->where('client_policy.policy_type_id', $policy_type_id);
if ($issuer != 0) $builder2->where('policy_transaction.issuer', $issuer);
// ==============================
// DEFAULT 90-DAY 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)) {
$builder2->where('policy_transaction.created_at >=', $fromDate)
->where('policy_transaction.created_at <=', $toDate);
}
}
// ==============================
// With Statement Upload
// ==============================
$builder = $this->db->table('policy_transaction')
->select("
policy_transaction.id AS id,
policy_transaction.endorsement_no AS endorsement_no,
policy_transaction.ref AS ref,
DATE_FORMAT(policy_transaction.policy_issue_date, '%d %b %Y') AS policy_issue_date,
DATE_FORMAT(
IF(insurer_statements_oq.month IS NULL,
policy_transaction.policy_issue_date,
insurer_statements_oq.month
), '%b %Y'
) AS policy_issue_month,
CASE
WHEN insurer_statements_oq.month IS NULL THEN 'no statement uploaded'
ELSE 'statement uploaded'
END AS statement_uploaded ,
CASE
WHEN clients.client_type = 1 THEN 'Group'
WHEN clients.client_type = 2 THEN 'Retail'
ELSE '-'
END AS client_type,
CASE
WHEN policy_transaction.revenue_type = 'NA' THEN 'Fresh'
ELSE 'Renewal'
END AS revenue_type,
CASE
WHEN policy_transaction.action_type = 'inception' THEN 'Policy'
ELSE 'Endorsement'
END AS action_type,
clients.client_name AS client_name,
clients.short_name AS client_short_name,
client_branch.branch_name AS client_branch_name,
client_branch.address1 AS client_address,
policy_type.policy_type,
policy_type.bap,
insurers.name AS insurer_name,
insurers.short_name AS insurer_short_name,
insurer_branch.branch_name AS insurer_branch_name,
insurer_branch.branch_code AS insurer_branch_code,
user_profiles.first_name AS user_name,
vehicle.vehicle_no,
tpa.name AS tpa_name,
pt_co_share_details.remark AS remarks,
pt_co_share_details.cop_amt AS bp_amt,
pt_co_share_details.exp_amt,
pt_co_share_details.id AS pt_id,
sales_user.first_name AS salse_person_name,
service_user.first_name AS service_person_name,
ROUND(pt_co_share_details.cop_amt + pt_co_share_details.cotp_amt + pt_co_share_details.cotep_amt, 2) AS premium_wo_gst,
ROUND((ROUND(pt_co_share_details.cop_amt + pt_co_share_details.cotp_amt + pt_co_share_details.cotep_amt, 2) * 18 / 100), 2) AS gst_amount,
ROUND(
ROUND(pt_co_share_details.cop_amt + pt_co_share_details.cotp_amt + pt_co_share_details.cotep_amt, 2) +
ROUND((ROUND(pt_co_share_details.cop_amt + pt_co_share_details.cotp_amt + pt_co_share_details.cotep_amt, 2) * 18 / 100), 2),
2) AS total_premium,
ROUND(pt_co_share_details.cotp_amt + pt_co_share_details.cotep_amt, 2) AS tp_or_ter,
DATEDIFF(policy_transaction.policy_end_date, CURDATE()) AS days,
(pt_co_share_details.agreed_tp_per + pt_co_share_details.agreed_tep_per) AS agreed_tp_or_ter_per,
pt_co_share_details.agreed_bp_per,
-- ==============================
-- SUBQUERY: Total IRDA Amount
-- Alias Agreed Amount ...!!
-- ==============================
ROUND(pt_co_share_details.exp_amt, 2) AS total_irda_amt,
-- Reward Only
ROUND((
SELECT SUM(co_share_stmt_details.reward)
FROM co_share_stmt_details
JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id
WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id
AND co_share_stmt_details.is_active = 1
AND insurer_statements.is_active = 1
AND insurer_statements.month = insurer_statements_oq.month
{$date_condition}
GROUP BY('policy_transaction.policy_no , insurer_statements.month , pt_co_share_details.insurer_id')
), 2) AS reward,
-- Billed Amount
ROUND((
SELECT SUM(
COALESCE(co_share_stmt_details.actual_bp_brokerage_amt, 0) +
COALESCE(co_share_stmt_details.actual_tp_brokerage_amt, 0) +
COALESCE(co_share_stmt_details.actual_tep_brokerage_amt, 0) +
COALESCE(co_share_stmt_details.reward, 0)
)
FROM co_share_stmt_details
JOIN pt_co_share_details AS pt_table ON co_share_stmt_details.co_share_id = pt_table.id
JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id
WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id
AND co_share_stmt_details.is_active = 1
AND pt_table.is_active = 1
AND insurer_statements.is_active = 1
AND insurer_statements.invoice_status IS NOT NULL
AND insurer_statements.month = insurer_statements_oq.month
{$date_condition}
GROUP BY('policy_transaction.policy_no , insurer_statements.month , pt_co_share_details.insurer_id')
), 2) AS billed_amt,
created_user.first_name AS user_name,
CASE
WHEN pt_co_share_details.co_share_type IN (0, 1) THEN policy_transaction.policy_no
WHEN pt_co_share_details.co_share_type > 1 THEN
CASE
WHEN pt_co_share_details.follower_policy_no IS NULL OR pt_co_share_details.follower_policy_no = ''
THEN policy_transaction.policy_no
ELSE pt_co_share_details.follower_policy_no
END
ELSE policy_transaction.policy_no
END AS policy_no
")
// ==============================
// JOINS
// ==============================
->join('pt_co_share_details', 'policy_transaction.id = pt_co_share_details.pt_id', 'left')
->join('clients', 'clients.id = policy_transaction.client_id')
->join('client_branch', 'policy_transaction.client_branch_id = client_branch.id', 'left')
->join('client_policy', 'policy_transaction.client_policy_id = client_policy.id', 'left')
->join('user_profiles', 'policy_transaction.created_by = user_profiles.id', 'left')
->join('vehicle', 'policy_transaction.vehicle_id = vehicle.id', 'left')
->join('policy_type', 'policy_transaction.policy_type_id = policy_type.id', 'left')
->join('insurers', 'pt_co_share_details.insurer_id = insurers.id', 'left')
->join('insurer_branch', 'pt_co_share_details.insurer_branch_id = insurer_branch.id', 'left')
->join('tpa', 'policy_transaction.tpa_id = tpa.id', 'left')
->join('tpa_branch', 'policy_transaction.tpa_branch_id = tpa_branch.id', 'left')
->join('user_profiles AS sales_user', 'policy_transaction.sales_generated_by = sales_user.id', 'left')
->join('user_profiles AS service_user', 'policy_transaction.serviced_by = service_user.id', 'left')
->join('user_profiles AS created_user', 'policy_transaction.created_by = created_user.id', 'left')
->join('co_share_stmt_details', 'pt_co_share_details.id = co_share_stmt_details.co_share_id','left')
->join('insurer_statements insurer_statements_oq','insurer_statements_oq.id = co_share_stmt_details.statement_id','left')
->where('policy_transaction.is_active', 1)
->where('pt_co_share_details.is_active', 1)
->where('insurer_statements_oq.id IS NOT NULL')
// ========================================================
// GROUP BY Insurer Statement Months
// =======================================================
->groupBy('policy_transaction.policy_no , pt_co_share_details.insurer_id ,insurer_statements_oq.month');
// ==============================
// ROLE-BASED FILTERS
@ -2092,17 +2275,22 @@ class PolicyTransactionModel extends Model
}
}
// ==============================
// ORDER & EXECUTION
// ==============================
$builder->orderBy('policy_transaction.id', 'desc');
$result = $builder->get()->getResultArray();
// Uncomment if you need to debug SQL
// dd($this->db->getLastQuery());
$sql1 = $builder2->getCompiledSelect();
$sql2 = $builder->getCompiledSelect();
$finalSql = "($sql1) UNION ALL ($sql2)
ORDER BY policy_no DESC, insurer_branch_name ASC, statement_uploaded ASC,
STR_TO_DATE(policy_issue_month, '%b %Y') ASC";
$result = $this->db->query($finalSql)->getResultArray();
return $result;
}