| 
									
										
										
										
											2021-10-26 14:37:13 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | defined('BASEPATH') or exit('No direct script access allowed'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class InsuranceSubscription_model extends CI_Model | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	function __construct() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		// Set table name
 | 
					
						
							|  |  |  | 		$this->table = 'nh_infos_insurances_subscriptions'; | 
					
						
							|  |  |  | 		// Set orderable column fields
 | 
					
						
							| 
									
										
										
										
											2021-11-12 05:09:33 +00:00
										 |  |  | 		$this->column_order = array(null, 'insurance_subscription_id', 'lastname', 'phone', 'number_of_months',  'number_of_beneficiaries', 'bonus_amount', 'state', | 
					
						
							|  |  |  | 			'insurance_action','created_at', null); | 
					
						
							| 
									
										
										
										
											2021-10-26 14:37:13 +00:00
										 |  |  | 		// Set searchable column fields
 | 
					
						
							| 
									
										
										
										
											2021-11-12 05:09:33 +00:00
										 |  |  | 		$this->column_search = array('insurance_subscription_id', 'lastname', 'phone', 'number_of_months',  'number_of_beneficiaries', 'bonus_amount', 'state', | 
					
						
							|  |  |  | 			'insurance_action','created_at'); | 
					
						
							| 
									
										
										
										
											2021-10-26 14:37:13 +00:00
										 |  |  | 		// Set default order
 | 
					
						
							|  |  |  | 		$this->order = array('created_at' => 'desc'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* | 
					
						
							|  |  |  | 	 * Fetch members data from the database | 
					
						
							|  |  |  | 	 * @param $_POST filter data based on the posted parameters | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	public function getRows($postData) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		$this->_get_datatables_query($postData); | 
					
						
							|  |  |  | 		if ($postData['length'] != -1) { | 
					
						
							|  |  |  | 			$this->db->limit($postData['length'], $postData['start']); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		$query = $this->db->get(); | 
					
						
							|  |  |  | 		return $query->result(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* | 
					
						
							|  |  |  | 	 * Count all records | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	public function countAll($postData) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		$this->db->from($this->table); | 
					
						
							|  |  |  | 		$this->db->where('network_id', $postData['id_network']); | 
					
						
							|  |  |  | 		return $this->db->count_all_results(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* | 
					
						
							|  |  |  | 	 * Count records based on the filter params | 
					
						
							|  |  |  | 	 * @param $_POST filter data based on the posted parameters | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	public function countFiltered($postData) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		$this->_get_datatables_query($postData); | 
					
						
							|  |  |  | 		$query = $this->db->get(); | 
					
						
							|  |  |  | 		return $query->num_rows(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* | 
					
						
							|  |  |  | 	 * Perform the SQL queries needed for an server-side processing requested | 
					
						
							|  |  |  | 	 * @param $_POST filter data based on the posted parameters | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	private function _get_datatables_query($postData) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$this->db->from($this->table); | 
					
						
							|  |  |  | 		$this->db->where('network_id', $postData['id_network']); | 
					
						
							|  |  |  | 		if (strlen($postData['startDate']) > 0 && strlen($postData['endDate']) > 0) { | 
					
						
							|  |  |  | 			$this->db->where('created_at >=', date('Y-m-d', strtotime($postData['startDate']))); | 
					
						
							|  |  |  | 			$this->db->where('created_at <', date('Y-m-d', strtotime($postData['endDate']. "+1 day"))); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$i = 0; | 
					
						
							|  |  |  | 		// loop searchable columns
 | 
					
						
							|  |  |  | 		foreach ($this->column_search as $item) { | 
					
						
							|  |  |  | 			// if datatable send POST for search
 | 
					
						
							|  |  |  | 			if ($postData['search']['value']) { | 
					
						
							|  |  |  | 				// first loop
 | 
					
						
							|  |  |  | 				if ($i === 0) { | 
					
						
							|  |  |  | 					// open bracket
 | 
					
						
							|  |  |  | 					$this->db->group_start(); | 
					
						
							|  |  |  | 					$this->db->like($item, $postData['search']['value']); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					$this->db->or_like($item, $postData['search']['value']); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				// last loop
 | 
					
						
							|  |  |  | 				if (count($this->column_search) - 1 == $i) { | 
					
						
							|  |  |  | 					// close bracket
 | 
					
						
							|  |  |  | 					$this->db->group_end(); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			$i++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (isset($postData['order'])) { | 
					
						
							|  |  |  | 			$this->db->order_by($this->column_order[$postData['order']['0']['column']], $postData['order']['0']['dir']); | 
					
						
							|  |  |  | 		} else if (isset($this->order)) { | 
					
						
							|  |  |  | 			$order = $this->order; | 
					
						
							|  |  |  | 			$this->db->order_by(key($order), $order[key($order)]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |