<?php
 class Model_stok extends CI_Model {
 	public function __construct() {
		
	}

	public function dataopname($req,$tipe) {
		$kolom=array('tb_stokopname.tanggal','COUNT(kode_barang)','pegawai','tgl_update');
		$this->db->select('tb_stokopname.id_stokopname,tb_stokopname.tanggal,tgl_update,COUNT(kode_barang) AS barang,pegawai');
		$this->db->from('tb_stokopname');
		$this->db->join('tb_stokopnamemeta','tb_stokopnamemeta.id_stokopname=tb_stokopname.id_stokopname');
		$this->db->join('tb_pegawai','tb_stokopname.kode_pegawai=tb_pegawai.kode_pegawai');
		//pencarian data
		if (@$req['search']['value']) {
			$cari=$req['search']['value'];
			$kunci=array('tb_stokopname.tanggal'=>$cari,'tgl_update'=>$cari,'pegawai'=>$cari);
			$this->db->or_like($kunci);
		}
		$this->db->group_by('tb_stokopname.id_stokopname');
		if ($tipe=='jumlah') {
			$query=$this->db->get();
			return $query->num_rows();
		} else {
			//urutan data
			$this->db->order_by($kolom[$req['order'][0]['column']],$req['order'][0]['dir']);
			$this->db->limit($req['length'],$req['start']);
			$query=$this->db->get();
			return $query->result();
		}
	}

	public function datastokopname($selek,$kunci) {
		if ($selek) {
            $this->db->select($selek);
        }
        $this->db->from('tb_stokopname');
		$this->db->join('tb_stokopnamemeta','tb_stokopnamemeta.id_stokopname=tb_stokopname.id_stokopname');
        $this->db->join('tb_barang','tb_barang.kode_barang=tb_stokopnamemeta.kode_barang');
        if ($kunci) {
            $this->db->where($kunci);
        }
        $this->db->order_by('tb_stokopnamemeta.id_stokopnamemeta','DESC');
		$query=$this->db->get();
		return $query;
	}

	public function datastokopnamegroup($selek,$kunci) {
		if ($selek) {
            $this->db->select($selek);
        }
        $this->db->from('tb_stokopname');
		$this->db->join('tb_stokopnamemeta','tb_stokopnamemeta.id_stokopname=tb_stokopname.id_stokopname');
        $this->db->join('tb_barang','tb_barang.kode_barang=tb_stokopnamemeta.kode_barang');
        if ($kunci) {
            $this->db->where($kunci);
        }
		$this->db->group_by('tb_barang.kode_barang');
		$query=$this->db->get();
		return $query;
	}

	public function datadrafstokopname($selek,$kunci) {
		if ($selek) {
            $this->db->select($selek);
        }
        $this->db->from('tb_drafstokopnamemeta');
        $this->db->join('tb_barang','tb_barang.kode_barang=tb_drafstokopnamemeta.kode_barang');
        if ($kunci) {
            $this->db->where($kunci);
        }
        $this->db->order_by('tb_drafstokopnamemeta.id_drafstokopnamemeta','DESC');
		$query=$this->db->get();
		return $query;
	}

	public function transaksiopname($selek,$kunci) {
		if ($selek) {
			$this->db->select($selek);
		}
		$this->db->from('tb_stokopname');
		$this->db->join('tb_stokopnamemeta','tb_stokopnamemeta.id_stokopname=tb_stokopname.id_stokopname');
		$this->db->join('tb_pegawai','tb_stokopname.kode_pegawai=tb_pegawai.kode_pegawai');
		$this->db->where($kunci);
		$this->db->order_by('tb_stokopname.tanggal','ASC');
		$query=$this->db->get();
		return $query;	
	}

	public function hapus_data($kunci,$tabel) {
		$this->db->where($kunci);
		$this->db->delete($tabel);
	}
}
?>