<?php 
	class Model_pembelian extends CI_model{
		private $table = 'pembelian'; 
		private $supplier = 'supplier'; 
		private $item = 'item_pembelian'; 
		private $akun = 'akun'; 
		private $jenis = 'jenis_bayar'; 
		function __construct() { 
			// Set table name 
			
			
		} 
		
		public function view_pembelian($where = array()){
			$this->db->where($where);
			return $this->db->get($this->table);
		}

		public function sync_active_stock_items()
		{
			$this->load->model('Pembelian_stok_model', 'pembelian_stok_sync');
			$this->pembelian_stok_sync->sync_stock_items();
		}
		
		public function cek_id($id)
		{
			$query = $this->db->select('id_pembelian');
			$query = $this->db->where('id_pembelian', $id);
			$query = $this->db->get($this->table);
			$result = $query->num_rows();
			
			if ( $result < 1 )
			$result = 0;
			
			return $result;
		}
		
		public function get_detailby_id($id)
		{
			$this->db->select(' `pembelian_detail`.`id_bahan`,
			`pembelian_detail`.`id_supplier`,
			`pembelian_detail`.`id_pembelian`,
			`pembelian_detail`.`id_biaya`,
			`pembelian_detail`.`keterangan`,
			`pembelian_detail`.`jumlah`,
			`pembelian_detail`.`harga`
			');
			$this->db->from('pembelian_detail');
			$this->db->where('pembelian_detail.id_pembelian', $id); 
			$query = $this->db->get();
			$result = $query->num_rows() > 0 ? $query->result_array() : false;
			return $result;
		}
		
		function printPembelian($params = array()){
			// print_r($params);
			$this->db->select('*');
			
			if(array_key_exists("where", $params)){
				foreach($params['where'] as $key => $val){ 
					$this->db->where($key, $val); 
				} 
			}  
			
			if(array_key_exists("search", $params))
			{ 
				if(!empty($params['search']['dari']) AND !empty($params['search']['sampai'])){ 
					$minvalue = $params['search']['dari'];
					$maxvalue = $params['search']['sampai'];
					$this->db->where("tgl_pembelian between '$minvalue' AND '$maxvalue'");
				}
			} 
			
			$query = $this->db->get($this->table); 
			$result = ($query->num_rows() > 0)?$query->result():FALSE; 
			return $result; 
		}
		
		function get_supplier($params = array()){
			$this->db->select('*');
			$this->db->from($this->supplier);
			
			if(array_key_exists("where", $params)){
				foreach($params['where'] as $key => $val){ 
					$this->db->where($key, $val); 
				} 
			}  
			
			if(array_key_exists("search", $params)){
				if(!empty($params['search']['keywords'])){ 
					$this->db->like('nama_perusahaan', $params['search']['keywords']); 
					$this->db->or_like('telp', $params['search']['keywords']); 
				} 
			}
			
			// Sort data by ascending or desceding order 
			if(!empty($params['search']['sortBy'])){ 
				$this->db->order_by('id_supplier', $params['search']['sortBy']); 
			}
			
			if(array_key_exists("returnType",$params) && $params['returnType'] == 'count'){
				$result = $this->db->count_all_results(); 
				}else{
				if(array_key_exists("id", $params) || (array_key_exists("returnType", $params) && $params['returnType'] == 'single')){ 
					
					$query = $this->db->get(); 
					$result = ($query->num_rows() > 0)?$query->result_array():FALSE; 
					}else{
					$this->db->order_by('id_supplier', 'DESC'); 
					if(array_key_exists("start",$params) && array_key_exists("limit",$params)){ 
						
						$this->db->limit($params['limit'],$params['start']); 
						}elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){ 
						
						$this->db->limit($params['limit']); 
					}
					
					$query = $this->db->get(); 
					$result = ($query->num_rows() > 0)?$query->result_array():FALSE; 
				} 
			} 
			
			// Return fetched data 
			return $result; 
		}
		
		//pembelian`
		function getPembelian($params = array()){
			// print_r($params);
			$this->db->select('*');
			$this->db->from($this->table);
			
			if(array_key_exists("where", $params)){
				foreach($params['where'] as $key => $val){ 
					$this->db->where($key, $val); 
				} 
			}  
			
			if(array_key_exists("search", $params)){
				
				if(!empty($params['search']['dari']) AND !empty($params['search']['sampai'])){
					$minvalue = date('Y-m-d', strtotime($params['search']['dari']));
					$maxvalue = date('Y-m-d', strtotime($params['search']['sampai']));
					if($minvalue == $maxvalue){
						$this->db->where("tgl_pembelian = '$minvalue'");
						}else{
						$this->db->where("tgl_pembelian between '$minvalue' AND '$maxvalue'");
					}
				}
			}
			
			
			// Sort data by ascending or desceding order 
			if(!empty($params['search']['sortBy'])){ 
				$this->db->order_by('id_pembelian', $params['search']['sortBy']); 
			}
			
			if(array_key_exists("returnType",$params) && $params['returnType'] == 'count'){
				$result = $this->db->count_all_results(); 
				}else{ 
				if(array_key_exists("id", $params) || (array_key_exists("returnType", $params) && $params['returnType'] == 'single')){ 
					
					$query = $this->db->get(); 
					$result = ($query->num_rows() > 0)?$query->result_array():FALSE; 
					}else{
					
					$this->db->order_by('id_pembelian', 'DESC'); 
					if(array_key_exists("start",$params) && array_key_exists("limit",$params)){ 
						
						$this->db->limit($params['limit'],$params['start']); 
						}elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){ 
						
						$this->db->limit($params['limit']); 
					}
					
					$query = $this->db->get(); 
					$result = ($query->num_rows() > 0)?$query->result_array():FALSE; 
				} 
			} 
			
			// Return fetched data 
			return $result; 
		}
		//pembelian`
		function getPembelianCari($params = array()){
			// print_r($params);
			// $this->db->distinct();
			$this->db->select('*');
			$this->db->select('count(DISTINCT(pembelian.id_pembelian))');  
			$this->db->from($this->table);
			$this->db->join('pembelian_detail', '`pembelian`.`id_pembelian` = `pembelian_detail`.`id_pembelian`');
			
			if(array_key_exists("where", $params)){
				foreach($params['where'] as $key => $val){ 
					$this->db->where($key, $val); 
				} 
			}  
			
			if(array_key_exists("search", $params)){
				
				if(!empty($params['search']['dari']) AND !empty($params['search']['sampai'])){
					$minvalue = date('Y-m-d', strtotime($params['search']['dari']));
					$maxvalue = date('Y-m-d', strtotime($params['search']['sampai']));
					if($minvalue == $maxvalue){
						$this->db->where("tgl_pembelian = '$minvalue'");
						}else{
						$this->db->where("tgl_pembelian between '$minvalue' AND '$maxvalue'");
					}
				}
				if (!empty($params['search']['keywords'])) {
                    $this->db->like('pembelian_detail.keterangan', $params['search']['keywords']);
					
				}
			}
			
			
			// Sort data by ascending or desceding order 
			if(!empty($params['search']['sortBy'])){ 
				$this->db->order_by('pembelian.id_pembelian', $params['search']['sortBy']); 
			}
			
			if(array_key_exists("returnType",$params) && $params['returnType'] == 'count'){
				$result = $this->db->count_all_results(); 
				}else{ 
				if(array_key_exists("id", $params) || (array_key_exists("returnType", $params) && $params['returnType'] == 'single')){ 
					
					$query = $this->db->get(); 
					$result = ($query->num_rows() > 0)?$query->result_array():FALSE; 
					}else{
					
					$this->db->order_by('pembelian.id_pembelian', 'DESC'); 
					if(array_key_exists("start",$params) && array_key_exists("limit",$params)){ 
						
						$this->db->limit($params['limit'],$params['start']); 
						}elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){ 
						
						$this->db->limit($params['limit']); 
					}
					
					$query = $this->db->get(); 
					$result = ($query->num_rows() > 0)?$query->result_array():FALSE; 
				} 
			} 
			
			// Return fetched data 
			return $result; 
		}
		function getPembelianCariSupplier($params = array()){
			// print_r($params);
			$this->db->select('*');
			$this->db->select('count(DISTINCT(pembelian.id_pembelian))');  
			$this->db->from($this->table);
			$this->db->join('pembelian_detail', '`pembelian`.`id_pembelian` = `pembelian_detail`.`id_pembelian`');
			$this->db->join('supplier', '`pembelian_detail`.`id_supplier` = `supplier`.`id_supplier`');
			if(array_key_exists("where", $params)){
				foreach($params['where'] as $key => $val){ 
					$this->db->where($key, $val); 
				} 
			}  
			
			if(array_key_exists("search", $params)){
				
				if(!empty($params['search']['dari']) AND !empty($params['search']['sampai'])){
					$minvalue = date('Y-m-d', strtotime($params['search']['dari']));
					$maxvalue = date('Y-m-d', strtotime($params['search']['sampai']));
					if($minvalue == $maxvalue){
						$this->db->where("tgl_pembelian = '$minvalue'");
						}else{
						$this->db->where("tgl_pembelian between '$minvalue' AND '$maxvalue'");
					}
				}
				if (!empty($params['search']['keywords'])) {
                    $this->db->like('supplier.nama_perusahaan', $params['search']['keywords']);
                    // print_r($params);
				}
			}
			
			
			// Sort data by ascending or desceding order 
			if(!empty($params['search']['sortBy'])){ 
				$this->db->order_by('pembelian.id_pembelian', $params['search']['sortBy']); 
			}
			
			if(array_key_exists("returnType",$params) && $params['returnType'] == 'count'){
				$result = $this->db->count_all_results(); 
				}else{ 
				if(array_key_exists("id", $params) || (array_key_exists("returnType", $params) && $params['returnType'] == 'single')){ 
					
					$query = $this->db->get(); 
					$result = ($query->num_rows() > 0)?$query->result_array():FALSE; 
					}else{
					
					$this->db->order_by('pembelian.id_pembelian', 'DESC'); 
					if(array_key_exists("start",$params) && array_key_exists("limit",$params)){ 
						
						$this->db->limit($params['limit'],$params['start']); 
						}elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){ 
						
						$this->db->limit($params['limit']); 
					}
					
					$query = $this->db->get(); 
					$result = ($query->num_rows() > 0)?$query->result_array():FALSE; 
				} 
			} 
			
			// Return fetched data 
			return $result; 
		}
		
		//item pembelian
		function getItem($params = array()){
			$this->sync_active_stock_items();
			// print_r($params);
			$this->db->select('*');
			$this->db->from($this->item);
			
			if(array_key_exists("where", $params)){
				foreach($params['where'] as $key => $val){ 
					$this->db->where($key, $val); 
				} 
			}  
			if(array_key_exists("search", $params)){
				if(!empty($params['search']['keywords'])){ 
					$this->db->like('title', $params['search']['keywords']); 
				} 
			}
			// Sort data by ascending or desceding order 
			if(!empty($params['search']['sortBy'])){ 
				$this->db->order_by('id', $params['search']['sortBy']); 
			}
			
			if(array_key_exists("returnType",$params) && $params['returnType'] == 'count'){
				$result = $this->db->count_all_results(); 
				}else{ 
				if(array_key_exists("id", $params) || (array_key_exists("returnType", $params) && $params['returnType'] == 'single')){ 
					
					$query = $this->db->get(); 
					$result = ($query->num_rows() > 0)?$query->result_array():FALSE; 
					}else{
					
					$this->db->order_by('id', 'DESC'); 
					if(array_key_exists("start",$params) && array_key_exists("limit",$params)){ 
						
						$this->db->limit($params['limit'],$params['start']); 
						}elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){ 
						
						$this->db->limit($params['limit']); 
					}
					
					$query = $this->db->get(); 
					$result = ($query->num_rows() > 0)?$query->result_array():FALSE; 
				} 
			} 
			
			// Return fetched data 
			return $result; 
		}
		
		public function cek_item_id($id)
		{
			$query = $this->db->select('id');
			$query = $this->db->where('id', $id);
			$query = $this->db->get($this->item);
			$result = $query->num_rows();
			
			if ( $result < 1 )
			$result = 0;
			
			return $result;
		}
		
		public function get_nama($id)
		{
			
			if ( $this->cek_item_id($id) > 0 )
			{
				$query = $this->db->select('title');
				$query = $this->db->where('id', $id);
				$query = $this->db->get($this->item);
				$result = $query->row();
				$title = $result->title;
				return $title;
			}
			else 
			{
				return NULL;
			}
			
		}
		
		public function cek_nama_akun($string)
		{
			
			$query = $this->db->select('nama');
			$query = $this->db->where('nama', $string);
			$query = $this->db->get($this->akun);
			$result = $query->num_rows();
			if ( $result > 0 )
			{
				$result = $query->row();
				$nama = $result->nama;
				return $nama;
				}else{
				return NULL;
			}
			
		}
		
		public function save_item($data){
            if($this->db->insert($this->item, $data))
            {
                $response['status'] =  true;
                $response['id'] = $this->db->insert_id();
                }else{
                $response['status'] =  false;
                $response['id'] =  0;
			}
            return $response;
		}
		
		public function update_item($params = array(), $where = array())
		{
			$this->db->update($this->item, $params, $where);
			return $this->db->affected_rows() > 0 ? true : false;
		}
		
		public function view_item($where = array()){
			$this->sync_active_stock_items();
			$this->db->where($where);
			return $this->db->get($this->item);
		}
		
		public function edit_item($where = array())
		{
			return $this->db->get_where($this->item, $where)->row();
		}
		
		public function delete_item($where = array())
		{
			$this->db->delete($this->item, $where);
			return $this->db->affected_rows() > 0 ? true : false;
		}
		
		public function getMonthPembelian($bulan,$tahun){
			return $this->db->select('*')
			->from($this->table)
			->where('MONTH(tgl_pembelian)',$bulan)
			->where('YEAR(tgl_pembelian)',$tahun)
			->order_by('id_pembelian','ASC')
			->get()
			->result();
		}
		
		// menampilkan data berdasarkan id
		public function getIdAkun($id)
		{
			$query = $this->db->get_where($this->item, ['id' => $id]);
			$result = ($query->num_rows() > 0)?$query->row()->id_akun:0; 
			return $result;
		}
		// menampilkan data berdasarkan id
		public function getIdJenis($id=0)
		{
			
			$query = $this->db->get_where($this->jenis, ['id' => $id]);
			$result = ($query->num_rows() > 0)?$query->row()->id_akun:0; 
			return $result;
		}
	}																																												
