<?php
	class Db_operations{

		public $CI="";
		public function __construct(){

			$this->CI =& get_instance();
		}

		function data_insert($table='',$arr=''){

			$this->CI->db->insert($table,$arr);
			return $this->CI->db->insert_id();
		}

		function get_recordlist($table='',$field='',$orderby=''){

			if(!empty($orderby)){

				$this->CI->db->order_by($field,$orderby);
			}

			$tdata = $this->CI->db->get($table);
			return $tdata->result_array();
		}

		function get_record($table='', $condition=''){

			return $this->CI->db->get_where($table,$condition)->result_array();
		}

		function data_update($table='',$arr='',$field='',$value=''){

			$this->CI->db->where($field,$value);
			return $this->CI->db->update($table,$arr);
		}


		function delete_record($table='',$arr=''){

			return $this->CI->db->delete($table,$arr);
		}

		function get_max_id($table, $field){

			$this->CI->db->select_max($field, 'max_id');
			return $this->CI->db->get($table)->result_array()[0]['max_id'];
		}

		function get_cnt($table, $arr){

			$this->CI->db->where($arr);
			return $this->CI->db->count_all_results($table);
		}

		function empty_table($table){

			return $this->CI->db->empty_table($table); 
		}

		function get_fin_year_max_id($table, $field, $field2)
		{
			$f_year = $this->CI->session->userdata('fin_year');    
			$record = $this->CI->db->query("SELECT MAX($field) as max_id FROM $table WHERE $field2 = '$f_year'")->result_array();

			if(empty($record[0]['max_id']))
			{
				return 1;
			}
			else
			{
				return $record[0]['max_id']+1;
			}

		}
		public function get_single_record($table, $where = [])
		{
			$this->CI->db->where($where);
			$query = $this->CI->db->get($table);
			return $query->row_array();
		}

		function get_fin_year_max_id_custom($table,$field,$field2,$f_year)
		{
			$query = "SELECT MAX($field) as max_id FROM $table WHERE $field2 = '$f_year'";
			// echo "<pre>"; print_r($query); exit;	
			$record = $this->CI->db->query($query)->result_array();
			// echo "<pre>"; print_r($record); exit;	


			if(empty($record[0]['max_id']))
			{
				return 1;
			}
			else
			{
				return $record[0]['max_id']+1;
			}
			// echo "<pre>"; print_r($record); exit;	
			
		}
		
		function get_fin_year_branch_max_id($table,$field,$field2,$f_year, $field3)
		{
			$query = "SELECT MAX($field) as max_id FROM $table WHERE $field2 = '$f_year' ";
			// echo "<pre>"; print_r($query); exit;	
			$record = $this->CI->db->query($query)->result_array();
			// echo "<pre>"; print_r($record); exit;	


			if(empty($record[0]['max_id']))
			{
				return 1;
			}
			else
			{
				return $record[0]['max_id']+1;
			}
			// echo "<pre>"; print_r($record); exit;	
			
		}
	}
?>