<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Transfer_model extends CI_Model
{
    public function getDataTransfer($tgl_awal, $tgl_akhir, $id_cabang, $category = null, $id_impor = null)
    {
        $this->db->select('
            a.*, 
            b.*, 
            c.nama_barang, 
            u.nama as nm_user, 
            up.id_user as id_user_pemilik, up.nama as nm_user_pemilik, up.jabatan, 
            d.nama_depart,
            s.nama_satuan, 
            dbms.merk, dbms.spesifikasi, dbms.barcode, dbms.sn,
            bm.tanggal_masuk, bm.stts_penggunaan, bm.stts_perolehan,
            cba.nama_cabang AS asal, cbt.nama_cabang AS tujuan,
            j.nama_jenis,
            ');
        $this->db->from('transfer_barang a');
        $this->db->join('detail_transfer_barang b', 'a.id_transfer = b.id_transfer','left');
        $this->db->join('detail_barang_masuk dbms', 'b.id_detail_masuk = dbms.id_detail','left');
        $this->db->join('barang_masuk bm', 'dbms.id_barang_masuk = bm.id_barang_masuk','left');
        $this->db->join('user u', 'a.user_id = u.id_user','left');
        $this->db->join('user up', 'b.user_id_pemilik = up.id_user','left');
        $this->db->join('barang c', 'b.id_barang = c.id_barang','left');
        $this->db->join('departemen d', 'up.id_depart = d.id_depart','left');
        $this->db->join('satuan s', 'c.satuan_id = s.id_satuan','left');
        $this->db->join('cabang cba', 'a.asal_cabang = cba.id_cabang','left');
        $this->db->join('cabang cbt', 'a.tujuan_cabang = cbt.id_cabang','left');
        $this->db->join('jenis j', 'c.jenis_id = j.id_jenis','left');
        if(!empty($tgl_awal) && !empty($tgl_akhir)) {
            $where_id = array('a.tgl_transfer >=' => $tgl_awal, 'a.tgl_transfer <=' => $tgl_akhir);
            $this->db->where($where_id);
        }

        if ($category != null) {
            $this->db->where_in('c.jenis_id', $category);
        }

        if ($id_impor != null) {
            $this->db->where_in('a.id_impor', $id_impor);
        }
        
        $this->db->where('a.asal_cabang', $id_cabang);
        $this->db->or_where('a.tujuan_cabang', $id_cabang);

        return $this->db->get()->result_array();
    }

    public function getDetailBarangTransferById($id)
    {        
        $this->db->select('
            a.*, 
            b.*, 
            c.nama_barang, 
            up.id_user as id_user_pemilik, up.nama as nm_user_pemilik, 
            s.nama_satuan, 
            dbms.merk, dbms.spesifikasi, dbms.barcode, dbms.sn,
            bm.tanggal_masuk,
            ');
        $this->db->from('transfer_barang a');
        $this->db->join('detail_transfer_barang b', 'a.id_transfer = b.id_transfer','left');
        $this->db->join('detail_barang_masuk dbms', 'b.id_detail_masuk = dbms.id_detail','left');
        $this->db->join('barang_masuk bm', 'dbms.id_barang_masuk = bm.id_barang_masuk','left');
        $this->db->join('user up', 'b.user_id_pemilik = up.id_user','left');
        $this->db->join('barang c', 'b.id_barang = c.id_barang','left');
        $this->db->join('satuan s', 'c.satuan_id = s.id_satuan','left');
        $this->db->where('a.id_transfer', $id);
        return $this->db->get()->result_array();
    }

    public function getTransferById($id)
    {
        $query = "SELECT u.nama,c.*,cb.nama_cabang as nama_asal, ct.nama_cabang as nama_tujuan FROM  transfer_barang c,  cabang cb, cabang ct, user u where u.id_user = c.user_id AND cb.id_cabang = c.asal_cabang AND ct.id_cabang = c.tujuan_cabang and c.id_transfer = '$id'";
        return $this->db->query($query)->row_array();
    }

    public function getDetailTransferById($id)
    {
        $query = "SELECT a.*,b.nama_barang,s.nama_satuan,cb.nama_cabang as nama_asal, ct.nama_cabang as nama_tujuan FROM detail_transfer_barang a, barang b, transfer_barang c, satuan s, cabang cb, cabang ct where cb.id_cabang = c.asal_cabang AND ct.id_cabang = c.tujuan_cabang AND b.satuan_id = s.id_satuan AND a.id_barang = b.id_barang and c.id_transfer = a.id_transfer and a.id_transfer = '$id'";
        return $this->db->query($query)->result_array();
    }

    public function insertTransfer()
    {
        $tgl_transfer = $this->input->post('tgl_transfer');
        $keterangan = $this->input->post('keterangan');
        $kode_transfer = $this->input->post('kode_transfer');
        $asal_cabang = $this->input->post('asal_cabang');
        $tujuan_cabang = $this->input->post('tujuan_cabang');

        $data = [
            'kode_transfer' => $kode_transfer,
            'tgl_transfer' => $tgl_transfer,
            'keterangan' => $keterangan,
            'asal_cabang' => $asal_cabang,
            'tujuan_cabang' => $tujuan_cabang,
            'user_id' => $this->input->post('user_id')
        ];

        $this->db->insert('transfer_barang', $data);
        $id_transfer =  $this->db->insert_id();

        $barang = $this->input->post('id_barang[]');
        $jumlah = $this->input->post('jumlah[]');
        $id_detail_masuk = $this->input->post('id_detail_masuk[]');
        $user_id_pemilik = $this->input->post('user_id_pemilik[]');

        $index = 0;
        $data_detail = array();
        foreach ($barang as $id) {
            array_push($data_detail, array(
                'id_transfer' => $id_transfer,
                'id_barang' => $id,
                'id_detail_masuk' => $id_detail_masuk[$index],
                'user_id_pemilik' => $user_id_pemilik[$index],
                'jumlah' => $jumlah[$index],
            ));
            // End Update Stok
            $index++;
        }

        return $this->db->insert_batch('detail_transfer_barang', $data_detail);
    }


    public function insertTransferImpor($id_impor, $data_impor)
    {
        $this->load->model('Admin_model', 'admin');
        // mencari kode id barang masuk
        $kode = 'TR-' . date('ymd');
        $kode_terakhir = $this->admin->getMax('transfer_barang', 'kode_transfer', $kode);
        $kode_tambah = substr($kode_terakhir, -5, 5);
        
        foreach($data_impor as $row) {
            $kode_tambah++;
            $number = str_pad($kode_tambah, 5, '0', STR_PAD_LEFT);
            $id_barang_keluar = $kode . $number;

            // mencari id_barang_masuk
            $dt_impor = [
                "kode_transfer" => $id_barang_keluar,
                "tgl_transfer" => $row['tgl'],
                "asal_cabang" => $row['asal_cabang'],
                "tujuan_cabang" => $row['tujuan_cabang'],
                "keterangan" => $row['ket'],
                "user_id" => $row['id_user'],
                "id_impor" => $id_impor,
            ];

            $this->db->insert('transfer_barang', $dt_impor);
            $id_transfer =  $this->db->insert_id();

            $data_detail = array();
            $i = 0;
            foreach($row['dt_array'] as $row_det) {
                $data_detail[] = array(      
                    'id_transfer' => $id_transfer,     
                    'id_detail_masuk' => $row_det['id_detail'],
                    'user_id_pemilik' => $row_det['id_user_pemilik'],
                    'jumlah' => $row_det['jumlah'],
                    'id_barang' => $row_det['id_barang'],
                );
                $i++;
            };

            return $this->db->insert_batch('detail_transfer_barang', $data_detail);
        }

    }


    public function editTransfer($id)
    {

        $this->db->delete('detail_transfer_barang', ['id_transfer' => $id]);

        $tgl_transfer = $this->input->post('tgl_transfer');
        $keterangan = $this->input->post('keterangan');
        $kode_transfer = $this->input->post('kode_transfer');
        $asal_cabang = $this->input->post('asal_cabang');
        $tujuan_cabang = $this->input->post('tujuan_cabang');

        $data = [
            'kode_transfer' => $kode_transfer,
            'tgl_transfer' => $tgl_transfer,
            'keterangan' => $keterangan,
            'asal_cabang' => $asal_cabang,
            'tujuan_cabang' => $tujuan_cabang
        ];


        $this->db->where('id_transfer', $id);
        $this->db->update('transfer_barang', $data);

        $barang = $this->input->post('id_barang');
        $jumlah = $this->input->post('jumlah');
        $id_detail_masuk = $this->input->post('id_detail_masuk[]');
        $user_id_pemilik = $this->input->post('user_id_pemilik[]');

        $index = 0;
        $data_detail = array();
        foreach ($barang as $id_br) {
            array_push($data_detail, array(
                'id_transfer' => $id,
                'id_barang' => $id_br,
                'id_detail_masuk' => $id_detail_masuk[$index],
                'user_id_pemilik' => $user_id_pemilik[$index],
                'jumlah' => $jumlah[$index],
            ));
            $index++;
        }

        return $this->db->insert_batch('detail_transfer_barang', $data_detail);
    }


    public function delete($table, $pk, $id)
    {

        $this->db->delete('detail_transfer_barang', [$pk => $id]);

        return $this->db->delete($table, [$pk => $id]);
    }
}
