select([ 'employee_polices.*', 'policy_type.policy_type as policy_name', 'im.short_name as insurer_short_name', 'ib.branch_name as insurer_branch_name', 'ib.branch_code as insurer_branch_code', 'tpam.name as tpa_name', 'tpam.short_name as tpa_short_name', 'tpab.branch_code as tpa_branch_code', 'cm.client_name', 'cm.short_name as client_short_name', 'emp.relationship', 'emp.relationship_code', 'emp.change_event', 'emp.emp_code', 'emp.name', 'emp.email_corporate', 'emp.dob', 'emp.gender', 'emp.emp_status', 'emp.is_active as emp_is_active', 'emp.mobile as mobile', 'emp.doj', 'emp.basic_pay', 'emp.band as grade', 'policy_type.policy_type', 'client_branch.branch_name as client_branch_name', 'client_branch.branch_code as client_branch_code', 'cp.policy_no', ]) ->join('employees emp', 'employee_polices.employee_id = emp.id') ->join('client_policy cp', 'employee_polices.client_policy_id = cp.id') //cp - client policy ->join('policies pm', 'cp.policy_id = pm.id', 'left') //pm - policy master ->join('policy_type', 'policy_type.id = cp.policy_type_id') ->join('insurers im', 'cp.insurer_id = im.id') //im - insurer master ->join('insurer_branch ib', 'cp.insurer_branch_id = ib.id') //ib - insurer branch ->join('tpa tpam', 'cp.tpa_id = tpam.id', 'left') //tpam - tpa master ->join('tpa_branch tpab', 'cp.tpa_branch_id = tpab.id', 'left') //tpab - tpa branch ->join('clients cm', 'cp.client_id = cm.id') //cm - client master ->join('client_branch', 'emp.client_branch_id = client_branch.id') //cm - client master ->orderBy('emp.emp_code', 'ASC') ->orderBy('employee_polices.employee_id', 'ASC'); // Conditionally add where clauses if ($client_id !=0 && !empty($client_id)) { $result->where('emp.client_id', $client_id); } if ($branch_id !=0 && !empty($branch_id)) { $result->where('emp.client_branch_id', $branch_id); } if ($policy_id !=0 && !empty($policy_id)) { $result->where('employee_polices.client_policy_id', $policy_id); } if ($status !=0 && !empty($status)) { if($status == 'active'){ $result->where('employee_polices.tpa_id IS NOT NULL'); $result->where('employee_polices.uhid IS NOT NULL'); }else if($status == 'pending'){ $status = 'active'; $result->where('employee_polices.status', $status); }else{ $result->where('employee_polices.status', $status); } } if (!empty($emp_code)) { $result->where('emp.emp_code', $emp_code); } if (!empty($emp_name)) { $result->like('emp.name', $emp_name); } // Always check these conditions $result->where('employee_polices.is_active', 1) ->where('emp.is_active', 1); return $result->findAll(); } //for emp onboard do not change public function checkExistingEmpPolicy($arr) { return $this->where('employee_id',$arr['employee_id']) ->where('client_policy_id',$arr['client_policy_id']) ->where('is_active',1) ->find(); } //------------------------------------------------------------------------------------------------------- public function getInceptionEmployeeDataForExportExcel($ref_data) { $client_policy_id = $ref_data['client_policy_id']; $client_branch_id = $ref_data['client_branch_id']; $insurer_or_tpa = $ref_data['insurer_or_tpa']; $event = $ref_data['event_type']; if($insurer_or_tpa == 'tpa'){ $id = 'tpa_id'; }else if($insurer_or_tpa == 'insurer'){ $id = 'uhid'; } $datas = ''; if($event === 'inception'){ $datas = 'I'; }else if($event === 'dependent_addition'){ $datas = 'DA'; }else if($event === 'addition'){ $datas = 'A'; } $sql = " SELECT employees.name AS emp_name, employees.emp_code AS emp_code, employees.dob AS emp_dob, employees.gender AS emp_gender, employees.relationship AS emp_relationship, employees.relationship_code AS emp_relationship_code, '$datas' as event_type_data, employees.change_event AS change_event, employees.doj AS emp_doj, employees.mobile AS emp_mobile, employees.email_corporate AS emp_email_c, employees.email_personal AS emp_email_p, employees.band AS emp_grade, employees.designation AS emp_designation, employees.basic_pay AS emp_basic_pay, TIMESTAMPDIFF(YEAR, employees.dob, CURDATE()) AS emp_age, employees.emp_type as emp_type, employee_polices.id as primaryKey, employee_polices.tpa_id, employee_polices.uhid, employee_polices.pre_existing_alignments, employee_polices.basic_cover_si, employee_polices.date_coverage, employee_polices.policy_end_date, employee_polices.days as no_of_days, employee_polices.premium, employee_polices.rata_premimum as pro_rata_premium, employee_polices.gst, (employee_polices.rata_premimum + employee_polices.gst) AS total, batch_data.emp_policy_id, batch_data.bl AS batch_list_batch_code, batch_data.bf AS batch_files_batch_code, CASE WHEN client_branch.sez = 0 THEN 'NON-SEZ' WHEN client_branch.sez = 1 THEN 'SEZ' END as sez FROM employee_polices LEFT JOIN employees ON employees.id = employee_polices.employee_id LEFT JOIN client_branch ON client_branch.id = employees.client_branch_id LEFT JOIN ( SELECT batch_list.emp_policy_id, batch_list.batch_code as bl, batch_files.batch_code as bf FROM batch_files LEFT JOIN batch_list ON batch_files.batch_code = batch_list.batch_code WHERE batch_files.event_type = '{$event}' AND batch_files.actions = 'export' AND batch_files.insurer_or_tpa = '{$insurer_or_tpa}' ) as batch_data ON employee_polices.id = batch_data.emp_policy_id WHERE employee_polices.client_policy_id = '{$client_policy_id}' AND (employee_polices.{$id} IS NULL OR employee_polices.{$id} = '') AND employees.client_branch_id = '{$client_branch_id}' AND employee_polices.is_active = 1 AND employee_polices.status = 'active' AND employees.is_active = 1 AND employees.emp_status = 'active' "; // Get the result set $query = $this->db->query($sql); $results = $query->getResult(); return $results; } public function getCorrectionEmployeesDataForExportExcel($ref_data) { $client_id = $ref_data['client_id']; $client_policy_id = $ref_data['client_policy_id']; $client_branch_id = $ref_data['client_branch_id']; $insurer_or_tpa = $ref_data['insurer_or_tpa']; $sql = " SELECT DISTINCT emp_endorsement.id, emp_endorsement.pk, emp_endorsement.emp_code, emp_endorsement.endorsement_id, emp_endorsement.old_value, emp_endorsement.new_value, emp_endorsement.field_name, emp_endorsement.remarks, emp_endorsement.actions, employees.id AS primaryKey, employees.name AS emp_name, employees.dob AS emp_dob, employees.gender AS emp_gender, employees.client_id AS emp_client_id, employees.emp_type as emp_type, employee_polices.uhid, employee_polices.tpa_id, employees.relationship_code as emp_relationship_code, employees.relationship as emp_relationship, 'C' as event_type_data, employees.doj AS emp_doj, employees.mobile AS emp_mobile, employees.email_corporate AS emp_email_c, employees.email_personal AS emp_email_p, employees.band AS emp_grade, employees.designation AS emp_designation, employees.basic_pay AS emp_basic_pay, batch_data.emp_policy_id, batch_data.bl AS batch_list_batch_code, batch_data.bf AS batch_files_batch_code FROM emp_endorsement LEFT JOIN employees ON employees.id = emp_endorsement.pk LEFT JOIN employee_polices ON employees.id = employee_polices.employee_id LEFT JOIN ( SELECT batch_list.emp_policy_id, batch_list.batch_code AS bl, batch_files.batch_code AS bf FROM batch_files LEFT JOIN batch_list ON batch_files.batch_code = batch_list.batch_code WHERE batch_files.event_type = 'correction' AND batch_files.actions = 'export' AND batch_files.insurer_or_tpa = '{$insurer_or_tpa}' ) AS batch_data ON emp_endorsement.pk = batch_data.emp_policy_id WHERE employees.client_id = '{$client_id}' AND employee_polices.client_policy_id = '{$client_policy_id}' AND employees.client_branch_id = '{$client_branch_id}' AND emp_endorsement.actions = 'c' AND employee_polices.is_active = 1 AND employee_polices.status = 'active' AND employees.is_active = 1 AND employees.emp_status = 'active' AND (employee_polices.tpa_id IS NOT NULL AND employee_polices.tpa_id != '') AND (employee_polices.uhid IS NOT NULL AND employee_polices.uhid != '') AND (emp_endorsement.endorsement_id IS NULL OR emp_endorsement.endorsement_id = '')"; // Execute the raw query $query = $this->db->query($sql); // Get the result set $result = $query->getResult(); return $result; } public function getSIEnhancementEmployeesDataForExportExcel($ref_data) { $client_id = $ref_data['client_id']; $client_policy_id = $ref_data['client_policy_id']; $client_branch_id = $ref_data['client_branch_id']; $insurer_or_tpa = $ref_data['insurer_or_tpa']; $query = $this->db->query(" SELECT a.id as endorsement_primarykey, a.group_key, employee_polices.id AS primaryKey, employees.name AS emp_name, employees.emp_code AS emp_code, employees.dob AS emp_dob, employees.gender AS emp_gender, employees.relationship_code AS emp_relationship_code, employees.relationship AS emp_relationship, employees.emp_type as emp_type, 'SI' as event_type_data, employees.doj AS emp_doj, employees.mobile AS emp_mobile, employees.email_corporate AS emp_email_c, employees.email_personal AS emp_email_p, employees.band AS emp_grade, employees.designation AS emp_designation, employees.basic_pay AS emp_basic_pay, employee_polices.uhid AS uhid, employee_polices.pre_existing_alignments, employee_polices.policy_end_date, employee_polices.basic_cover_si as old_basic_cover_si, employee_polices.rata_premimum as old_si_premium, batch_data.emp_policy_id, batch_data.bl AS batch_list_batch_code, batch_data.bf AS batch_files_batch_code, sidata.new_basic_cover_si, sidata.new_si_premium, sidata.date_of_coverage, DATEDIFF(employee_polices.policy_end_date, sidata.date_of_coverage) + 1 AS no_of_days, sidata.new_si_premium - employee_polices.rata_premimum AS difference_premium, ROUND((sidata.new_si_premium - employee_polices.rata_premimum) * (DATEDIFF(employee_polices.policy_end_date, sidata.date_of_coverage) + 1) / 365, 2) AS pro_rata_premium, ROUND(((sidata.new_si_premium - employee_polices.rata_premimum) * (DATEDIFF(employee_polices.policy_end_date, sidata.date_of_coverage) + 1) / 365) * 0.18, 2) AS gst, ROUND( ((sidata.new_si_premium - employee_polices.rata_premimum) * (DATEDIFF(employee_polices.policy_end_date, sidata.date_of_coverage) + 1) / 365) + ROUND( ((sidata.new_si_premium - employee_polices.rata_premimum) * (DATEDIFF(employee_polices.policy_end_date, sidata.date_of_coverage) + 1) / 365) * 0.18, 2 ), 2 ) AS total FROM emp_endorsement a LEFT JOIN employees ON employees.emp_code = a.emp_code LEFT JOIN employee_polices ON employees.id = employee_polices.employee_id LEFT JOIN ( SELECT aa.emp_code, aa.new_value as 'new_basic_cover_si', bb.new_value as 'new_si_premium', cc.new_value as 'date_of_coverage' FROM ( SELECT a1.emp_code, a1.field_name, a1.new_value FROM emp_endorsement as a1 WHERE a1.field_name = 'basic_cover_si' ) aa LEFT JOIN ( SELECT b1.emp_code, b1.field_name, b1.new_value FROM emp_endorsement as b1 WHERE b1.field_name = 'premium' ) bb ON aa.emp_code = bb.emp_code LEFT JOIN ( SELECT c1.emp_code, c1.field_name, c1.new_value FROM emp_endorsement as c1 WHERE c1.field_name = 'si_enhancement_date' ) cc ON aa.emp_code = cc.emp_code ) as sidata ON a.emp_code = sidata.emp_code LEFT JOIN ( SELECT DISTINCT batch_list.emp_policy_id, batch_list.batch_code AS bl, batch_files.batch_code AS bf FROM batch_files LEFT JOIN batch_list ON batch_files.batch_code = batch_list.batch_code WHERE batch_files.event_type = 'si_enhancement' AND batch_files.actions = 'export' AND batch_files.insurer_or_tpa = '{$insurer_or_tpa}' ) AS batch_data ON employee_polices.id = batch_data.emp_policy_id WHERE employee_polices.client_policy_id = '{$client_policy_id}' AND employees.client_branch_id = '{$client_branch_id}' AND (a.endorsement_id IS NULL OR a.endorsement_id = '') AND a.actions = 'si' AND employee_polices.is_active = 1 AND employee_polices.status = 'active' AND employees.is_active = 1 AND employees.emp_status = 'active' group by group_key "); // Get the result set $results = $query->getResult(); return $results; } public function getDeletionEmployeeDataForExportExcel($ref_data) { $client_id = $ref_data['client_id']; $client_policy_id = $ref_data['client_policy_id']; $client_branch_id = $ref_data['client_branch_id']; $insurer_or_tpa = $ref_data['insurer_or_tpa']; $query = $this->db->query(" SELECT DISTINCT a.id as endorsement_primarykey, a.group_key, employee_polices.id as primaryKey, employees.name AS emp_name, employees.emp_code AS emp_code, employees.dob AS emp_dob, employees.gender AS emp_gender, employees.relationship AS emp_relationship, employees.relationship_code AS emp_relationship_code, employees.emp_type as emp_type, 'D' as event_type_data, TIMESTAMPDIFF(YEAR, employees.dob, CURDATE()) AS emp_age, employees.doj AS emp_doj, employees.mobile AS emp_mobile, employees.email_corporate AS emp_email_c, employees.email_personal AS emp_email_p, employees.band AS emp_grade, employees.designation AS emp_designation, employees.basic_pay AS emp_basic_pay, employee_polices.basic_cover_si, employee_polices.uhid as uhid, employee_polices.policy_end_date, employee_polices.premium, employee_polices.rata_premimum as pro_rata_premium, batch_data.emp_policy_id AS emp_policy_id, batch_data.bl AS batch_list_batch_code, batch_data.bf AS batch_files_batch_code, deletiondata.empstatus, deletiondata.changeevent, deletiondata.dateofexit, deletiondata.reasonforexit, deletiondata.status, DATEDIFF(employee_polices.policy_end_date, deletiondata.dateofexit) AS no_of_days, ROUND((employee_polices.rata_premimum * DATEDIFF(employee_polices.policy_end_date, deletiondata.dateofexit)) / 365, 2) AS pro_rata_premium, ROUND(((employee_polices.rata_premimum * DATEDIFF(employee_polices.policy_end_date, deletiondata.dateofexit)) / 365) * 0.18, 2) AS gst, ROUND(((employee_polices.rata_premimum * DATEDIFF(employee_polices.policy_end_date, deletiondata.dateofexit)) / 365) + (((employee_polices.rata_premimum * DATEDIFF(employee_polices.policy_end_date, deletiondata.dateofexit)) / 365) * 0.18), 2) AS total FROM emp_endorsement a LEFT JOIN employees ON a.emp_code = employees.emp_code and a.pk = employees.id LEFT JOIN employee_polices ON employees.id = employee_polices.employee_id LEFT JOIN( select aa.emp_code, aa.new_value as 'empstatus', bb.new_value as 'changeevent', cc.new_value as 'dateofexit', dd.new_value as 'reasonforexit', ee.new_value as 'status' from ( SELECT a1.emp_code, a1.field_name, a1.new_value from emp_endorsement as a1 where a1.field_name = 'emp_status') aa left join ( SELECT b1.emp_code, b1.field_name, b1.new_value from emp_endorsement as b1 where b1.field_name = 'change_event') bb on aa.emp_code = bb.emp_code left join ( SELECT c1.emp_code, c1.field_name, c1.new_value from emp_endorsement as c1 where c1.field_name = 'date_of_exit') cc on aa.emp_code = cc.emp_code left join ( SELECT d1.emp_code, d1.field_name, d1.new_value from emp_endorsement as d1 where d1.field_name = 'reason_for_exit') dd on aa.emp_code = dd.emp_code left JOIN ( SELECT e1.emp_code, e1.field_name, e1.new_value from emp_endorsement as e1 where e1.field_name = 'status') ee on aa.emp_code = ee.emp_code ) as deletiondata on a.emp_code = deletiondata.emp_code LEFT JOIN ( SELECT batch_list.emp_policy_id, batch_list.batch_code AS bl, batch_files.batch_code AS bf FROM batch_files LEFT JOIN batch_list ON batch_files.batch_code = batch_list.batch_code WHERE batch_files.event_type = 'deletion' AND batch_files.actions = 'export' AND batch_files.insurer_or_tpa = '{$insurer_or_tpa}' ) AS batch_data ON employee_polices.id = batch_data.emp_policy_id WHERE employee_polices.client_policy_id = {$client_policy_id} AND employees.client_branch_id = {$client_branch_id} AND (a.endorsement_id IS NULL OR a.endorsement_id = '') AND a.actions = 'd' AND employee_polices.is_active = 1 AND employee_polices.status = 'active' AND employees.is_active = 1 AND employees.emp_status = 'active' group by group_key "); $result = $query->getResult(); return $result; } public function fetchEmpEndorsementData($fetch_data) { // dd($fetch_data); $client_policy_id = $fetch_data['client_policy_id']; $client_branch_id = $fetch_data['client_branch_id']; $client_id = $fetch_data['client_id']; $emp_name = $fetch_data['emp_name']; $emp_code = $fetch_data['emp_code']; // Your raw SQL query $sql = " SELECT ee.id as emp_endorsement_primarykey, ep.id as emp_policy_primarykey, e.id as employees_primarykey, ee.group_key, ( SELECT employees.id FROM employees WHERE client_id = '$client_id' AND client_branch_id = '$client_branch_id' AND is_active = 1 AND emp_status = 'active' AND emp_code = '$emp_code' AND name = '$emp_name' LIMIT 1 ) AS employees_id, MAX( CASE WHEN ee.field_name = 'date_of_exit' THEN ee.new_value END ) AS date_of_exit, MAX( CASE WHEN ee.field_name = 'reason_for_exit' THEN ee.new_value END ) AS reason_for_exit, MAX( CASE WHEN ee.field_name = 'status' THEN ee.new_value END ) AS status FROM emp_endorsement AS ee JOIN employee_polices AS ep ON ep.id = ee.pk JOIN employees AS e ON e.emp_code = ee.emp_code WHERE ee.emp_code = '$emp_code' AND ep.client_policy_id = '$client_policy_id' AND e.client_branch_id = '$client_branch_id' AND ee.name = '$emp_name' AND ee.field_name IN ('date_of_exit', 'reason_for_exit', 'status') AND ep.is_active = 1 AND ep.status = 'active' AND e.is_active = 1 AND e.emp_status = 'active' GROUP BY ee.group_key "; // dd($sql); // Execute the raw SQL query $query = $this->db->query($sql); // Fetch and return results $row = $query->getRowArray(); return $row; } //for emp onboard do not change // public function checkExistingEmpPolicy($arr) // { // return $this->where('employee_id',$arr['employee_id']) // ->where('client_policy_id',$arr['client_policy_id']) // ->find(); // } public function updateSiAndPremium( $client_policy_id, $employee_id,$basic_cover_si,$premium = 0) { $query = "UPDATE employee_polices SET employee_polices.basic_cover_si = '{$basic_cover_si}' , employee_polices.premium = '{$premium}' WHERE employee_polices.employee_id = '{$employee_id}' AND employee_polices.client_policy_id = '{$client_policy_id}'"; $this->query($query); } public function getEmployeeEndorsementList($client_id, $policy_id, $status, $branch_id) { $query1 = $this->db->table('emp_endorsement e'); $query1->select(' e.id, e.pk, e.emp_code, e.name, e.actions, e.status, e.remarks, e.endorsement_id, ep.client_policy_id, policies.name as policy_name, insurers.short_name as insurer_short_name, client_policy.policy_no, policy_type.policy_type '); $query1->distinct(); $query1->join('employee_polices ep', 'ep.id = e.pk'); $query1->join('employees', 'employees.id = ep.employee_id'); $query1->join('client_policy', 'client_policy.id = ep.client_policy_id'); $query1->join('client_branch', 'client_branch.id = employees.client_branch_id'); $query1->join('policies', 'policies.id = client_policy.policy_id'); $query1->join('policy_type', 'policy_type.id = policies.policy_type_id'); $query1->join('insurers', 'insurers.id = policies.insurer_id'); $query1->whereIn('e.actions', ['c']); $query1->where('ep.client_policy_id', $policy_id); $query1->where('employees.client_id', $client_id); $query1->where('employees.client_branch_id', $branch_id); if($status != 0 && !empty($status)){ $query1->where('e.status', $status); } $query1->orderBy('e.id', 'desc'); $results1 = $query1->get()->getResultArray(); $query2 = $this->db->table('emp_endorsement e'); $query2->select(' e.id, e.pk, e.emp_code, e.name, e.actions, e.status, e.endorsement_id, e.remarks, ep.client_policy_id, policies.name as policy_name, insurers.short_name as insurer_short_name, client_policy.policy_no, policy_type.policy_type '); $query2->join('employee_polices ep', 'ep.id = e.pk'); $query2->join('employees', 'employees.id = ep.employee_id'); $query2->join('client_policy', 'client_policy.id = ep.client_policy_id'); $query2->join('client_branch', 'client_branch.id = employees.client_branch_id'); $query2->join('policies', 'policies.id = client_policy.policy_id'); $query2->join('policy_type', 'policy_type.id = policies.policy_type_id'); $query2->join('insurers', 'insurers.id = policies.insurer_id'); $query2->whereIn('e.actions', ['si', 'd']); $query2->where('ep.client_policy_id', $policy_id); $query2->where('employees.client_id', $client_id); $query2->where('employees.client_branch_id', $branch_id); if($status != 0 && !empty($status)){ $query2->where('e.status', $status); } $query2->groupBy('e.emp_code, e.name, e.actions'); $query2->orderBy('e.id', 'desc'); $results2 = $query2->get()->getResultArray(); $results = array_merge($results1, $results2); return $results; } //---------------------------------------------------------------------------------------------- public function getEmployeePolicyForFileList($client_id, $policy_id) { $result = $this->select(['employee_polices.*','pm.name as policy_name','im.short_name as insurer_short_name','ib.branch_name as insurer_branch_name','ib.branch_code as insurer_branch_code','tpam.name as tpa_name','tpam.short_name as tpa_short_name','tpab.branch_code as tpa_branch_code','cm.client_name','cm.short_name as client_short_name','emp.relationship','emp.relationship_code','emp.change_event','emp.emp_code','emp.name','emp.email_corporate','emp.dob','emp.gender','emp.emp_status','emp.is_active as emp_is_active','emp.mobile as mobile']) ->join('employees emp', 'employee_polices.employee_id = emp.id') ->join('client_policy cp', 'employee_polices.client_policy_id = cp.id') //cp - client policy ->join('policies pm', 'cp.policy_id = pm.id') //pm - policy master ->join('insurers im', 'cp.insurer_id = im.id') //im - insurar master ->join('insurer_branch ib', 'cp.insurer_branch_id = ib.id') //ib - insurar branch ->join('tpa tpam', 'cp.tpa_id = tpam.id') //tpam - tpa master ->join('tpa_branch tpab', 'cp.tpa_branch_id = tpab.id') //tpab - tpa brach ->join('clients cm', 'cp.client_id = cm.id') //cm - client master ->where('emp.client_id',$client_id) ->where('employee_polices.client_policy_id',$policy_id); $result->whereIn('employee_polices.status', ['draft', 'enrolled']); $result->whereIn('emp.emp_status', ['draft', 'enrolled']); $result = $result->findAll(); return ($result); } public function getViewEmpSuccessList($file_id) { return $this->db->table('employees emp') ->select(['employee_polices.*','pm.name as policy_name','im.short_name as insurer_short_name','ib.branch_name as insurer_branch_name','ib.branch_code as insurer_branch_code','tpam.name as tpa_name','tpam.short_name as tpa_short_name','tpab.branch_code as tpa_branch_code','cm.client_name','cm.short_name as client_short_name','emp.relationship','emp.relationship_code','emp.change_event','emp.emp_code','emp.name','emp.email_corporate','emp.dob','emp.gender','emp.emp_status','emp.is_active as emp_is_active','emp.mobile as mobile']) ->join('employee_polices', 'employee_polices.employee_id = emp.id') ->join('client_policy cp', 'employee_polices.client_policy_id = cp.id') //cp - client policy ->join('policies pm', 'cp.policy_id = pm.id') //pm - policy master ->join('insurers im', 'cp.insurer_id = im.id') //im - insurar master ->join('insurer_branch ib', 'cp.insurer_branch_id = ib.id') //ib - insurar branch ->join('tpa tpam', 'cp.tpa_id = tpam.id') //tpam - tpa master ->join('tpa_branch tpab', 'cp.tpa_branch_id = tpab.id') //tpab - tpa brach ->join('clients cm', 'cp.client_id = cm.id') //cm - client master ->where('emp.file_id',$file_id) ->whereIn('employee_polices.status', ['draft', 'enrolled']) ->get()->getResultArray(); } public function getECardDataUsingMd5($client_policy_id, $emp_code,$client_id){ $result = $this->db->table('employees e') ->select('e.id, e.name, e.mobile, e.relationship, e.relationship_code, e.emp_code, e.email_personal, e.email_corporate, e.gender, e.dob, e.doj, e.band, TIMESTAMPDIFF(YEAR, e.dob, CURDATE()) AS emp_age, (SELECT name FROM employees WHERE relationship = "Self" and emp_code = ' . $this->db->escape($emp_code) . ' and client_id = '.$client_id.') AS self, ep.tpa_id, ep.uhid, ep.policy_end_date, clients.client_name, clients.short_name AS client_short_name, cp.policy_start_date, cp.policy_no, policies.name AS policy_name, insurers.name AS insurer_name, insurers.short_name AS insurer_short_name, insurers.insurer_logo, insurer_branch.branch_name, insurer_branch.branch_code, insurer_branch.city as insurer_branch_city, tpa.name AS tpa_name, tpa.tpa_logo AS tpa_logo, tpa.front_card, tpa.back_card, tpa.short_name AS tpa_short_name' ) ->join('employee_polices ep', 'ep.employee_id = e.id') ->join('client_policy cp', 'cp.id = ep.client_policy_id') ->join('clients', 'clients.id = cp.client_id') ->join('policies', 'policies.id = cp.policy_id') ->join('insurers', 'insurers.id = cp.insurer_id') ->join('insurer_branch', 'insurer_branch.id = cp.insurer_branch_id') ->join('tpa', 'tpa.id = cp.tpa_id') ->where("ep.tpa_id IS NOT NULL AND ep.tpa_id <> ''") ->where('e.emp_status', 'active') ->where('e.is_active', '1') ->where('ep.status', 'active') ->where('ep.is_active', '1') ->where('e.emp_code', $emp_code) ->where('ep.client_policy_id', $client_policy_id) ->get() ->getResultArray(); return $result; } public function getECardSingleData($rand_string, $emp_code,$client_id){ $result = $this->db->table('employees e') ->select('e.id, e.name, e.mobile, e.relationship, e.relationship_code, e.emp_code, e.email_personal, e.email_corporate, e.gender, e.dob, e.doj, e.band, TIMESTAMPDIFF(YEAR, e.dob, CURDATE()) AS emp_age, (SELECT name FROM employees WHERE relationship = "Self" and emp_code = ' . $this->db->escape($emp_code) . ' and client_id = '.$client_id.') AS self, ep.tpa_id, ep.uhid, ep.policy_end_date, clients.client_name, clients.short_name AS client_short_name, cp.policy_start_date, cp.policy_no, policies.name AS policy_name, insurers.name AS insurer_name, insurers.short_name AS insurer_short_name, insurers.insurer_logo, insurer_branch.branch_name, insurer_branch.branch_code, insurer_branch.city as insurer_branch_city, tpa.name AS tpa_name, tpa.tpa_logo AS tpa_logo, tpa.front_card, tpa.back_card, tpa.short_name AS tpa_short_name' ) ->join('employee_polices ep', 'ep.employee_id = e.id') ->join('client_policy cp', 'cp.id = ep.client_policy_id') ->join('clients', 'clients.id = cp.client_id') ->join('policies', 'policies.id = cp.policy_id') ->join('insurers', 'insurers.id = cp.insurer_id') ->join('insurer_branch', 'insurer_branch.id = cp.insurer_branch_id') ->join('tpa', 'tpa.id = cp.tpa_id') ->where("ep.tpa_id IS NOT NULL AND ep.tpa_id <> ''") ->where('e.emp_status', 'active') ->where('e.is_active', '1') ->where('ep.status', 'active') ->where('ep.is_active', '1') ->where('ep.rand_string', $rand_string) ->get() ->getResultArray(); return $result; } public function getExisitingMobileNos(string $client_policy_id) { $result = $this->select(['employee_polices.id','emp.id as emp_id','emp.relationship','emp.emp_code','emp.name','emp.email_corporate','emp.mobile as mobile']) ->join('employees emp', 'employee_polices.employee_id = emp.id') ->join('client_policy cp', 'employee_polices.client_policy_id = cp.id') ->where('employee_polices.client_policy_id',$client_policy_id) ->whereIn('employee_polices.status', ['Active']) ->whereIn('emp.emp_status', ['Active']) ->where('emp.is_active', 1) ->where('emp.relationship', 'Self') ->where('emp.mobile is not null') ->where('employee_polices.is_active', 1) ->findAll(); return ($result); } public function bulkUpdate($emp_details) { // Extract IDs, tpa_ids, and uhids $ids = array_column($emp_details, 'id'); $tpa_ids = array_column($emp_details, 'tpa_id'); $uhids = array_column($emp_details, 'uhid'); // Escape values for SQL $escapedIds = array_map([$this->db, 'escape'], $ids); $escapedTpaIds = array_map([$this->db, 'escape'], $tpa_ids); $escapedUhids = array_map([$this->db, 'escape'], $uhids); // Construct the CASE statements $caseTpaId = array_map(function($id, $tpa_id) { return "WHEN id = $id THEN $tpa_id"; }, $escapedIds, $escapedTpaIds); $caseUhid = array_map(function($id, $uhid) { return "WHEN id = $id THEN $uhid"; }, $escapedIds, $escapedUhids); // Convert cases to a string $caseTpaIdString = implode(' ', $caseTpaId); $caseUhidString = implode(' ', $caseUhid); // Convert ids to a string $idsString = implode(', ', $escapedIds); // Construct the SQL query $sql = " UPDATE {$this->table} SET tpa_id = CASE {$caseTpaIdString} END, uhid = CASE {$caseUhidString} END WHERE id IN ({$idsString}) "; // Begin a transaction $this->db->transBegin(); try { // Execute the query $this->db->query($sql); // Commit the transaction if ($this->db->transStatus() === FALSE) { // If something went wrong, rollback $this->db->transRollback(); throw new \Exception('Bulk update failed.'); } else { // Otherwise, commit $this->db->transCommit(); } return $this->db->getLastQuery(); } catch (\Exception $e) { // Rollback the transaction on error $this->db->transRollback(); throw $e; } } public function bulkUpdateForEndorsement($endorsement_details) { // Extract IDs, endorsement_ids, and statuses $ids = array_column($endorsement_details, 'group_key'); $endorsement_ids = array_column($endorsement_details, 'endorsement_id'); $statuses = array_column($endorsement_details, 'status'); // Escape values for SQL $escapedIds = array_map([$this->db, 'escape'], $ids); $escapedEndorsementIds = array_map([$this->db, 'escape'], $endorsement_ids); $escapedStatuses = array_map([$this->db, 'escape'], $statuses); // Construct the CASE statements $caseEndorsementId = array_map(function ($id, $endorsement_id) { return "WHEN group_key = $id THEN $endorsement_id"; }, $escapedIds, $escapedEndorsementIds); $caseStatus = array_map(function ($id, $status) { return "WHEN status = $id THEN $status"; }, $escapedIds, $escapedStatuses); // Convert cases to a string $caseEndorsementIdString = implode(' ', $caseEndorsementId); $caseStatusString = implode(' ', $caseStatus); // Convert ids to a string $idsString = implode(', ', $escapedIds); // Construct the SQL query $sql = " UPDATE emp_endorsement SET endorsement_id = CASE {$caseEndorsementIdString} END, status = CASE {$caseStatusString} END WHERE group_key IN ({$idsString}) "; // Begin a transaction $this->db->transBegin(); try { // Execute the query $this->db->query($sql); // Commit the transaction if ($this->db->transStatus() === FALSE) { // If something went wrong, rollback $this->db->transRollback(); throw new \Exception('Bulk update failed.'); } else { // Otherwise, commit $this->db->transCommit(); } return $this->db->getLastQuery(); } catch (\Exception $e) { // Rollback the transaction on error $this->db->transRollback(); throw $e; } } public function bulkUpdateForCorrection($emp_details){ foreach ($emp_details as $employee) { $id = $this->db->escape($employee['id']); $ids[] = $id; foreach ($employee as $field => $value) { if ($field === 'id') continue; $escapedValue = $this->db->escape($value); if (!isset($caseStatements[$field])) { $caseStatements[$field] = []; } $caseStatements[$field][] = "WHEN id = $id THEN $escapedValue"; } } // Construct the CASE strings $caseStrings = []; foreach ($caseStatements as $field => $cases) { $caseStrings[] = "$field = CASE " . implode(' ', $cases) . " END"; } // Convert ids to a string $idsString = implode(', ', $ids); // Construct the SQL query $sql = " UPDATE employees SET " . implode(', ', $caseStrings) . " WHERE id IN ($idsString) "; // Begin a transaction $this->db->transBegin(); try { // Execute the query $this->db->query($sql); // Commit the transaction if ($this->db->transStatus() === FALSE) { // If something went wrong, rollback $this->db->transRollback(); throw new \Exception('Bulk update failed.'); } else { // Otherwise, commit $this->db->transCommit(); } return $this->db->getLastQuery(); } catch (\Exception $e) { // Rollback the transaction on error $this->db->transRollback(); throw $e; } } }