'required|max_length[100]', 'invoice_amount' => 'decimal', 'agent_id' => 'required|integer', 'invoice_date' => 'required|valid_date', ]; protected $validationMessages = []; protected $skipValidation = false; // 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 invoiceList($agent_id = null, $status_id = null, $start_date = null, $end_date = null) { $data = $this->select(" partner_invoice.*, -- Total UTR Amount (SELECT SUM(piu.amount) FROM partner_invoice_utr piu WHERE piu.invoice_id = partner_invoice.id AND piu.is_active = 1 ) AS total_utr_amount, -- Balance Amount (partner_invoice.invoice_amount - IFNULL( (SELECT SUM(piu2.amount) FROM partner_invoice_utr piu2 WHERE piu2.invoice_id = partner_invoice.id AND piu2.is_active = 1 ), 0) ) AS balance_amount, -- Payout status CASE WHEN payout_status = 1 THEN 'Pending' WHEN payout_status = 2 THEN 'Completed' END AS status_text, partner_agent.name as agent_name ") ->join('partner_agent', 'partner_invoice.agent_id = partner_agent.id') ->where('partner_invoice.is_active', 1); if(!empty($agent_id)){ $data->where('partner_invoice.agent_id', $agent_id); } if(!empty($status_id)){ $data->where('partner_invoice.payout_status', $status_id); } if (!empty($start_date) && !empty($end_date)) { $startDate = date('Y-m-d 00:00:00', strtotime($start_date)); $endDate = date('Y-m-d 23:59:59', strtotime($end_date)); $data->where('partner_invoice.invoice_date >=', $startDate) ->where('partner_invoice.invoice_date <=', $endDate); } if(!empty($agent_id) && !empty($status_id) && !empty($start_date) && !empty($end_date)){ $fromDate = date('Y-m-d', strtotime('-60 days')); $toDate = date('Y-m-d 23:59:59'); $data->where('partner_invoice.created_at >=', $fromDate) ->where('partner_invoice.created_at <=', $toDate); } $return_data = $data->orderBy('partner_invoice.id','desc')->findAll(); // print_r($this->db->getLastQuery()); die; return $return_data; } public function agentList($params = []) { if(isset($params['is_active'])){ return $this->db->table('partner_agent')->where('is_active', $params['is_active'])->get()->getResultArray(); } return $this->db->table('partner_agent')->get()->getResultArray(); } public function utrSummary($invoice_id) { $data = $this->select(" partner_invoice.*, -- Total UTR Amount (SELECT SUM(piu.amount) FROM partner_invoice_utr piu WHERE piu.invoice_id = partner_invoice.id AND piu.is_active = 1 ) AS total_utr_amount, -- Balance Amount (partner_invoice.invoice_amount - IFNULL( (SELECT SUM(piu2.amount) FROM partner_invoice_utr piu2 WHERE piu2.invoice_id = partner_invoice.id AND piu2.is_active = 1 ), 0) ) AS balance_amount, -- Payout status CASE WHEN payout_status = 1 THEN 'Pending' WHEN payout_status = 2 THEN 'Completed' END AS status_text, partner_agent.name as agent_name ") ->join('partner_agent', 'partner_invoice.agent_id = partner_agent.id') ->where('partner_invoice.is_active', 1) ->where('partner_invoice.id', $invoice_id) ->first(); return $data; } public function payoutList($flag, $agentId = null, $invoiceId = null) { $builder = $this->db->table('policy_transaction pt') ->select(' pt.id, pt.policy_no AS policyNo, pt.agent_id AS agentId, pp.insured_name AS customer, pp.premium_amount AS premium, COALESCE(pii.commission_amount, pp.commission_amount) AS commission, pp.issued_date AS date_db, DATE_FORMAT(pp.issued_date, "%d/%m/%Y") AS date, pii.id AS invoiceItemId, pii.commission_amount as paid_amount, pi.payout_status, pp.id as partner_policy_id, pp.policy_transaction_id ') ->join('partner_invoice_items pii','pii.policy_no = pt.policy_no','left') ->join('partner_invoice pi','pi.id = pii.invoice_id','left') ->join('partner_policy pp','pt.policy_no = pp.policy_number AND pt.agent_id = pp.agent_id AND pt.id = pp.policy_transaction_id') ->where('pt.is_active',1) ->where('pt.agent_id IS NOT NULL'); if ($flag == 1) { // Add mode // EXCLUDE all policies that exist in partner_invoice_items $builder->where("pt.id NOT IN (SELECT policy_id FROM partner_invoice_items)", null, false); } if ($flag == 2) { // Edit mode // Policies belonging to a specific invoice $builder->where('pii.is_active',1); if ($invoiceId) { $builder->where('pi.id', $invoiceId); // only policies of this invoice } if ($agentId) { $builder->where('pt.agent_id', $agentId); } } if ($flag == 3) { // Extra policies // Policies not assigned to any invoice $builder->where('pii.id IS NULL', null, false); if ($agentId) { $builder->where('pt.agent_id', $agentId); } } return $builder->get()->getResultArray(); } public function agentListById($agentId) { return $this->db->table('partner_agent') ->where('id', $agentId) ->where('is_active', 1) ->get() ->getRowArray(); } }