nhance/app/Models/PolicyTransactionModel.php

210 lines
9.0 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class PolicyTransactionModel extends Model
{
protected $table = 'policy_transaction';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
'id',
'issuer',
'client_id',
'client_branch_id',
'insurer_id',
'insurer_branch_id',
'tpa_id',
'tpa_branch_id',
'client_policy_id',
'vehicle_id',
'issue_type',
'source_client_policy_id',
'policy_no',
'cd_ac_no',
'policy_issue_date',
'policy_start_date',
'policy_end_date',
'action_type',
'endorsement_no',
'data_received_date',
'closure_date',
'emp_count',
'dependent_count',
'status',
'created_by',
'updated_by',
'created_at',
'updated_at',
'is_active',
'invoice_status',
'invoice_no',
'revenue_type',
'tsi',
'co_share',
'pre_payable_by',
'bro_payable_by',
'ct_status',
'co_broking_status',
'installment',
'installment_data',
'location',
'links',
'stage',
'etat',
'etat_band',
'edate',
'renewal_date',
'rollover_date',
'policy_holder_name',
'same_as_proposer',
'ref',
'spl',
'fund_received',
'listed_insurers',
'ppteam',
'policy_type_id',
'endorse_eff_date',
'sales_generated_by',
'serviced_by',
];
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = ["checkAndADDCreatedByValue"];
protected $afterInsert = [];
protected $beforeUpdate = ["checkAndUpdateUpdatedByValue"];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
protected function checkAndADDCreatedByValue(array $data)
{
// Check if 'updated_by' value is null or empty
if (empty($data['data']['created_by'])) {
// Set 'updated_by' value to the current session user ID
$data['data']['created_by'] = get_session_userid();
}
return $data;
}
protected function checkAndUpdateUpdatedByValue(array $data)
{
// Check if 'updated_by' value is null or empty
if (empty($data['data']['updated_by'])) {
// Set 'updated_by' value to the current session user ID
$data['data']['updated_by'] = get_session_userid();
}
return $data;
}
// public function getBDSReportList($client_id, $policy_id, $branch_id, $issuer)
public function getBDSReportList($start_date = 0, $end_date = 0, $client_id = 0, $insurer_id = 0, $policy_type_id = 0, $date_type = 0, $issuer = 0)
{
$builder = $this->db->table('policy_transaction')
->select("
policy_transaction.*,
DATE_FORMAT(policy_transaction.policy_issue_date, '%d %b %Y') AS policy_issue_date,
DATE_FORMAT(policy_transaction.policy_issue_date, '%b') AS policy_issue_month,
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.bp_amt,
sales_user.first_name as salse_person_name,
service_user.first_name as service_person_name,
ROUND((pt_co_share_details.bp_amt + pt_co_share_details.tp_amt + pt_co_share_details.tep_amt), 2) AS premium_wo_gst,
ROUND((ROUND((pt_co_share_details.bp_amt + pt_co_share_details.tp_amt + pt_co_share_details.tep_amt), 2) * 18 / 100), 2) AS gst_amount,
ROUND((ROUND((pt_co_share_details.bp_amt + pt_co_share_details.tp_amt + pt_co_share_details.tep_amt), 2) + ROUND((ROUND((pt_co_share_details.bp_amt + pt_co_share_details.tp_amt + pt_co_share_details.tep_amt), 2) * 18 / 100), 2)), 2) AS total_premium,
ROUND((pt_co_share_details.tp_amt + pt_co_share_details.tep_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,
ROUND((pt_co_share_details.bp_amt * pt_co_share_details.agreed_bp_per + (pt_co_share_details.tp_amt + pt_co_share_details.tep_amt) * (pt_co_share_details.agreed_tp_per + pt_co_share_details.agreed_tep_per)), 2) AS total_irda,
ROUND((ROUND((pt_co_share_details.bp_amt * pt_co_share_details.agreed_bp_per + (pt_co_share_details.tp_amt + pt_co_share_details.tep_amt) * (pt_co_share_details.agreed_tp_per + pt_co_share_details.agreed_tep_per)), 2) * 18 / 100), 2) AS gst_on_revenue,
ROUND(
(ROUND((pt_co_share_details.bp_amt * pt_co_share_details.agreed_bp_per + (pt_co_share_details.tp_amt + pt_co_share_details.tep_amt) * (pt_co_share_details.agreed_tp_per + pt_co_share_details.agreed_tep_per)), 2) +
ROUND((ROUND((pt_co_share_details.bp_amt * pt_co_share_details.agreed_bp_per + (pt_co_share_details.tp_amt + pt_co_share_details.tep_amt) * (pt_co_share_details.agreed_tp_per + pt_co_share_details.agreed_tep_per)), 2) * 18 / 100), 2)),
2) AS total_amt_received
")
->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', 'client_policy.policy_type_id = policy_type.id', 'left')
->join('insurers', 'policy_transaction.insurer_id = insurers.id', 'left')
->join('insurer_branch', 'policy_transaction.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')
->where('policy_transaction.is_active', 1);
// Check if the start date and end date are provided
if ($start_date != 0 && $end_date != 0 && $date_type != 0) {
$startDate = date('Y-m-d 00:00:00', strtotime($start_date));
$endDate = date('Y-m-d 23:59:59', strtotime($end_date));
$builder->where('policy_transaction.'.$date_type.'>=', $startDate)
->where('policy_transaction.'.$date_type.'<=', $endDate);
}
if ($client_id != 0) {
$builder->where('policy_transaction.client_id', $client_id);
}
if ($insurer_id != 0) {
$builder->where('policy_transaction.insurer_id', $insurer_id);
}
if ($policy_type_id != 0) {
$builder->where('client_policy.policy_type_id', $policy_type_id);
}
if ($issuer != 0) {
$builder->where('policy_transaction.issuer', $issuer);
}
return $builder->get()->getResultArray();
}
}