db->table('cd_master') ->select('cd_master.*, clients.client_name, clients.short_name, insurers.name AS insurer_name, user_profiles.first_name AS user_name, IFNULL(cd_ac_counts.cd_ac_no_count, 0) AS cd_ac_no_count, IFNULL(cd_ac_counts_for_cdt.cd_ac_no_count_cd_tranction, 0) AS cd_ac_no_count_cd_tranction, insurer_branch.branch_name as insurer_branch_name') ->join('clients', 'clients.id = cd_master.client_id') ->join('insurers', 'insurers.id = cd_master.insurer_id') ->join('insurer_branch', 'cd_master.insurer_branch_id = insurer_branch.id', "left") ->join('user_profiles', 'user_profiles.id = cd_master.created_by') ->join( '(SELECT cd_master.cd_ac_no, cd_master.id, COUNT(client_policy.cd_ac_no) AS cd_ac_no_count FROM cd_master JOIN client_policy ON client_policy.cd_ac_pk = cd_master.id WHERE client_policy.is_active = 1 AND cd_master.is_active = 1 GROUP BY cd_master.id ) AS cd_ac_counts', 'cd_ac_counts.id = cd_master.id', 'left' ) ->join( '(SELECT cd_master.cd_ac_no, cd_master.id, COUNT(cash_deposit.cd_ac_no) AS cd_ac_no_count_cd_tranction FROM cd_master JOIN cash_deposit ON cash_deposit.cd_ac_pk = cd_master.id WHERE cash_deposit.is_active = 1 AND cd_master.is_active = 1 GROUP BY cd_master.id ) AS cd_ac_counts_for_cdt', 'cd_ac_counts_for_cdt.id = cd_master.id', 'left' ) ->where('cd_master.is_active', 1) ->where('clients.is_active', 1) ->where('insurers.is_active', 1) ->where('user_profiles.is_active', 1) ->orderBy('cd_master.id', "desc") ->get(); $result = $query->getResultArray(); return $result; } /** * Active CD master rows that have a low-balance alert threshold configured. * * @return array> */ public function getCdMastersWithLowAlertThreshold(): array { return $this->db->table('cd_master') ->select('cd_master.*, clients.client_name, clients.short_name, insurers.name AS insurer_name, insurer_branch.branch_name AS insurer_branch_name') ->join('clients', 'clients.id = cd_master.client_id') ->join('insurers', 'insurers.id = cd_master.insurer_id') ->join('insurer_branch', 'insurer_branch.id = cd_master.insurer_branch_id', 'left') ->where('cd_master.is_active', 1) ->where('clients.is_active', 1) ->where('insurers.is_active', 1) ->where('cd_master.cd_balance_low_alert_amount IS NOT NULL', null, false) ->where('cd_master.cd_balance_low_alert_amount >', 0) ->orderBy('cd_master.id', 'desc') ->get() ->getResultArray(); } }