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

class Barang_model extends CI_Model
{
    public function getBarangMasukById($id)
    {
        $this->db->select('a.*, u.nama as nm_user, sp.nama as nm_user_pemilik, cb.nama_cabang');
        $this->db->join('user u', 'a.user_id = u.id_user');
        $this->db->join('user sp', 'a.user_id_pemilik = sp.id_user');
        $this->db->join('cabang cb', 'a.cabang_id = cb.id_cabang');
        $this->db->from('barang_masuk a');
        $this->db->where('a.id_barang_masuk', $id);
        return $this->db->get()->row_array();
    }

    public function getDetailBarangMasukById($id)
    {
        $this->db->select('a.*, b.*, b.id_barang, c.nama_barang, c.stokawal, s.nama_satuan');
        $this->db->from('barang_masuk a');
        $this->db->join('detail_barang_masuk b', 'a.id_barang_masuk = b.id_barang_masuk');
        $this->db->join('barang c', 'b.id_barang = c.id_barang');
        $this->db->join('satuan s', 'c.satuan_id = s.id_satuan');
        $this->db->where('a.id_barang_masuk', $id);
        return $this->db->get()->result_array();
    }

    public function getBarangKeluarById($id)
    {
        $this->db->select('a.*, u.nama as nm_user, cb.nama_cabang');
        $this->db->join('user u', 'a.user_id = u.id_user');
        $this->db->join('cabang cb', 'a.cabang_id = cb.id_cabang');
        $this->db->from('barang_keluar a');
        $this->db->where('a.id_barang_keluar', $id);
        return $this->db->get()->row_array();
    }

    public function getDetailBarangKeluarById($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('barang_keluar a');
        $this->db->join('detail_barang_keluar b', 'a.id_barang_keluar = b.id_barang_keluar','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_barang_keluar', $id);
        return $this->db->get()->result_array();
    }


    public function insertSaldoAwal()
    {
        $data = [
            "tgl" => $this->input->post('tgl', true),
            "barang_id" => $this->input->post('barang_id', true),
            "cabang_id" => $this->input->post('cabang_id', true),
            "user_id" => $this->input->post('user_id', true),
            "saldo" => $this->input->post('stok_awal', true),
            "hpp" => $this->input->post('hpp', true),
        ];

        $save = $this->db->insert('sa_barang', $data);

        return $save;
    }

    public function updateSaldoAwal()
    {
        $data = [
            "tgl" => $this->input->post('tgl', true),
            "barang_id" => $this->input->post('barang_id', true),
            "cabang_id" => $this->input->post('cabang_id', true),
            "user_id" => $this->input->post('user_id', true),
            "saldo" => $this->input->post('stok_awal', true),
            "hpp" => $this->input->post('hpp', true),
        ];

        $this->db->where('id_sa', $this->input->post('id_sa', true));
        $save = $this->db->update('sa_barang', $data);

        return $save;
    }


    public function insertDataBarang()
    {
        $id_barang = $this->input->post('id_barang', true);
        $nama_barang = $this->input->post('nama_barang', true);
        $id_jenis = $this->input->post('jenis_id', true);
        $id_satuan = $this->input->post('satuan_id', true);

        $data = [
            "id_barang" => $id_barang,
            "nama_barang" => $nama_barang,
            "batas" => 0,
            "satuan_id" => $id_satuan,
            "jenis_id" => $id_jenis,
            "tgl" => date('Y-m-d'),
            "stokawal" => 0
        ];

        $save = $this->db->insert('barang', $data);
        return $save;
    }

    public function insertBarang($table, $data, $batch = false)
    {
        $barang_id = $this->input->post('barang_id');
        $id_cabang = $this->input->post('cabang_id');

        // Input data ke cabang jika data barang belum masuk di cabang
        $query = "INSERT INTO cabang_detail (id_cabang, id_barang)
        SELECT * FROM (SELECT '$id_cabang', '$barang_id') AS tmp
        WHERE NOT EXISTS (
            SELECT * FROM cabang_detail WHERE id_cabang = '$id_cabang' AND id_barang = '$barang_id'
        ) LIMIT 1";
        // end input

        $this->db->query($query);
        return $this->db->insert($table, $data);
    }

    public function inputBarangCabang($barang_id, $id_cabang)
    {
        // Input data ke cabang jika data barang belum masuk di cabang
        $query = "INSERT INTO cabang_detail (id_cabang, id_barang)
        SELECT * FROM (SELECT '$id_cabang', '$barang_id') AS tmp
        WHERE NOT EXISTS (
            SELECT * FROM cabang_detail WHERE id_cabang = '$id_cabang' AND id_barang = '$barang_id'
        ) LIMIT 1";
        // $this->db->query($query);
        // end input
    }

    public function insertBarangMasuk()
    {
        $id_barang_masuk = $this->input->post('id_barang_masuk', true);
        $id_cabang = $this->input->post('cabang_id', true);
        $tanggal = $this->input->post('tanggal_masuk', true);
        $user_id_pemilik = $this->input->post('user_id_pemilik', true);
        $stts_perolehan = $this->input->post('stts_perolehan', true);
        $stts_penggunaan = $this->input->post('stts_penggunaan', true);
        $keterangan = $this->input->post('keterangan', true);

        $data = [
            "id_barang_masuk" => $id_barang_masuk,
            "user_id_pemilik" => $user_id_pemilik,
            "user_id" => $this->input->post('user_id'),
            "tanggal_masuk" => $tanggal,
            "cabang_id" => $id_cabang,
            "ket" => $keterangan,
            "stts_perolehan" => $stts_perolehan,
            "stts_penggunaan" => $stts_penggunaan,
        ];

        $this->db->insert('barang_masuk', $data);

        $id_barang = $this->input->post('id_barang[]', true);
        $jumlah = $this->input->post('jumlah[]', true);
        $merk = $this->input->post('merk[]', true);
        $spesifikasi = $this->input->post('spesifikasi[]', true);
        $barcode = $this->input->post('barcode[]', true);
        $sn = $this->input->post('sn[]', true);

        $data_detail = array();
        $i = 0;
        foreach ($id_barang as $id_br) {

            $this->inputBarangCabang($id_br, $id_cabang);

            array_push($data_detail, array(
                'id_barang_masuk' => $id_barang_masuk,
                'jumlah' => $jumlah[$i],
                'merk' => $merk[$i],
                'spesifikasi' => $spesifikasi[$i],
                'barcode' => $barcode[$i],
                'sn' => $sn[$i],
                'id_barang' => $id_br
            ));
            $i++;
        }

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


    public function insertBarangMasukImport($id_impor, $dt_res)
    {   
        $this->load->model('Admin_model', 'admin');
        // mencari kode id barang masuk
        $kode = 'BM-' . date('ymd');
        $kode_terakhir = $this->admin->getMax('barang_masuk', 'id_barang_masuk', $kode);
        $kode_tambah = substr($kode_terakhir, -5, 5);
        


        foreach($dt_res as $row) {
            $kode_tambah++;
            $number = str_pad($kode_tambah, 5, '0', STR_PAD_LEFT);
            $id_brng_msk = $kode . $number;

            // mencari id_barang_masuk
            $dt_impor = [
                "id_barang_masuk" => $id_brng_msk,
                "user_id_pemilik" => $row->id_user_pemilik,
                "user_id" => $row->id_user,
                "tanggal_masuk" => $row->tgl,
                "cabang_id" => $row->cabang_id,
                "ket" => $row->ket,
                "stts_perolehan" => $row->status,
                "stts_penggunaan" => $row->movement,
                "id_impor" => $id_impor,
            ];

            $this->db->insert('barang_masuk', $dt_impor);

            $data_detail = array();
            foreach($row->dt_array as $row_detail) {
                $data_detail[] = array(                    
                    'id_barang_masuk' => $id_brng_msk,
                    'id_barang' => $row_detail->id_barang,
                    'jumlah' => $row_detail->jumlah,
                    'merk' => $row_detail->merk,
                    'spesifikasi' => $row_detail->spesifikasi,
                    'barcode' => $row_detail->barcode,
                    'sn' => $row_detail->sn,
                );
            };

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


    public function editBarangMasuk($id)
    {
        $this->db->delete('detail_barang_masuk', ['id_barang_masuk' => $id]);

        $id_barang_masuk = $id;
        $id_cabang = $this->input->post('cabang_id', true);
        $tanggal = $this->input->post('tanggal_masuk', true);
        $user_id_pemilik = $this->input->post('user_id_pemilik', true);
        $stts_perolehan = $this->input->post('stts_perolehan', true);
        $stts_penggunaan = $this->input->post('stts_penggunaan', true);
        $keterangan = $this->input->post('keterangan', true);

        $data = [
            "user_id_pemilik" => $user_id_pemilik,
            "user_id" => $this->input->post('user_id'),
            "tanggal_masuk" => $tanggal,
            "cabang_id" => $id_cabang,
            "ket" => $keterangan,
            "stts_perolehan" => $stts_perolehan,
            "stts_penggunaan" => $stts_penggunaan,
            "date_updated" => date('Y-m-d H:i:s'),
        ];

        $this->db->where('id_barang_masuk', $id);
        $this->db->update('barang_masuk', $data);

        $id_barang = $this->input->post('id_barang[]', true);
        $jumlah = $this->input->post('jumlah[]', true);
        $merk = $this->input->post('merk', true);
        $spesifikasi = $this->input->post('spesifikasi[]', true);
        $barcode = $this->input->post('barcode[]', true);
        $sn = $this->input->post('sn[]', true);
        $data_detail = array();
        $i = 0;
        foreach ($id_barang as $id_br) {
            $this->inputBarangCabang($id_br, $id_cabang);

            array_push($data_detail, array(
                'id_barang_masuk' => $id_barang_masuk,
                'jumlah' => $jumlah[$i],
                'merk' => $merk[$i],
                'spesifikasi' => $spesifikasi[$i],
                'barcode' => $barcode[$i],
                'sn' => $sn[$i],
                'id_barang' => $id_br
            ));
            $i++;
        }

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

    public function insertBarangKeluar()
    {
        $id_barang_keluar = $this->input->post('id_barang_keluar', true);
        $id_cabang = $this->input->post('cabang_id', true);
        $tanggal = $this->input->post('tanggal_keluar', true);
        $keterangan = $this->input->post('keterangan', true);

        $data = [
            "id_barang_keluar" => $id_barang_keluar,
            "user_id" => $this->input->post('user_id'),
            "tanggal_keluar" => $tanggal,
            "cabang_id" => $id_cabang,
            "ket" => $keterangan,
        ];

        $this->db->insert('barang_keluar', $data);

        $id_detail = $this->input->post('id_detail_masuk[]', true);
        $user_id_pemilik = $this->input->post('user_id_pemilik[]', true);
        $id_barang = $this->input->post('id_barang[]', true);
        $jumlah = $this->input->post('jumlah[]', true);
        $data_detail = array();
        $i = 0;
        foreach ($id_barang as $id_br) {
            array_push($data_detail, array(
                'id_detail_masuk' => $id_detail[$i],
                'user_id_pemilik' => $user_id_pemilik[$i],
                'id_barang_keluar' => $id_barang_keluar,
                'jumlah' => $jumlah[$i],
                'id_barang' => $id_br
            ));
            $i++;
        }

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

    public function insertBarangKeluarImport($id_impor, $data_impor)
    {
        $this->load->model('Admin_model', 'admin');
        // mencari kode id barang masuk
        $kode = 'BK-' . date('ymd');
        $kode_terakhir = $this->admin->getMax('barang_keluar', 'id_barang_keluar', $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 = [
                "id_barang_keluar" => $id_barang_keluar,
                "user_id" => $row['id_user'],
                "tanggal_keluar" => $row['tgl'],
                "cabang_id" => $row['cabang_id'],
                "ket" => $row['ket'],
                "id_impor" => $id_impor,
            ];

            $this->db->insert('barang_keluar', $dt_impor);

            $data_detail = array();
            $i = 0;
            foreach($row['dt_array'] as $row_det) {
                $data_detail[] = array(      
                    'id_barang_keluar' => $id_barang_keluar,     
                    '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_barang_keluar', $data_detail);
        }
    }



    public function editBarangKeluar($id)
    {
        $this->db->delete('detail_barang_keluar', ['id_barang_keluar' => $id]);

        $id_barang_keluar = $id;
        $id_cabang = $this->input->post('cabang_id', true);
        $tanggal = $this->input->post('tanggal_keluar', true);
        $keterangan = $this->input->post('keterangan', true);

        $data = [
            "user_id" => $this->input->post('user_id'),
            "tanggal_keluar" => $tanggal,
            "cabang_id" => $id_cabang,
            "ket" => $keterangan,
            "date_updated" => date('Y-m-d H:i:s'),
        ];

        $this->db->where('id_barang_keluar', $id);
        $this->db->update('barang_keluar', $data);


        $id_detail = $this->input->post('id_detail_masuk[]', true);
        $user_id_pemilik = $this->input->post('user_id_pemilik[]', true);
        $id_barang = $this->input->post('id_barang[]', true);
        $jumlah = $this->input->post('jumlah[]', true);
        $data_detail = array();
        $i = 0;
        foreach ($id_barang as $id_br) {
            array_push($data_detail, array(
                'id_detail_masuk' => $id_detail[$i],
                'user_id_pemilik' => $user_id_pemilik[$i],
                'id_barang_keluar' => $id_barang_keluar,
                'jumlah' => $jumlah[$i],
                'id_barang' => $id_br
            ));
            $i++;
        }

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

    public function getBarangOpt()
    {
        $this->load->model('Admin_model', 'admin');
        $query = $this->admin->getBarang();
        $option = '';
        foreach ($query as $row) {
            $option .= "<option value=" . $row['id_barang'] . ">" . $row['nama_barang'] . "</option>";
        }

        return $option;
    }

    public function getBarangOptTf()
    {
        $this->load->model('Admin_model', 'admin');
        $query = $this->admin->getBarang();
        $option = '';
        foreach ($query as $row) {
            $option .= "<option value=" . $row['id_barang'] . ">" . $row['nama_barang'] . "</option>";
        }

        return $option;
    }

    public function delete($table, $pk, $id)
    {
        $this->load->model('Admin_model', 'admin');
        // $db =  $this->admin->get($table, [$pk => $id]);
        // $id_cabang = $db['cabang_id'];

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

    public function mutasi_stok($barang_id, $table, $operasi, $jumlah, $id_cabang = '')
    {
        $query = "UPDATE $table SET stok = stok $operasi $jumlah WHERE id_barang  = '$barang_id'";
        if ($id_cabang != '') {
            $query = "UPDATE $table SET stok = stok $operasi $jumlah WHERE id_barang  = '$barang_id' AND id_cabang = '$id_cabang'";
        }
        $this->db->query($query);
    }

    public function getMutasiCabang($id)
    {
        $query = "SELECT mt.*,s.nama_satuan, a.nama_barang, c.nama_cabang, d.nama_cabang AS trs_cbg FROM (
            SELECT b.id_barang AS brg, a.tanggal_masuk AS tgl, a.cabang_id AS cbg, 'in' jenis, b.jumlah AS jml, a.cabang_id AS cbg_t FROM barang_masuk a,detail_barang_masuk b
            WHERE a.id_barang_masuk = b.id_barang_masuk

            UNION

            SELECT b.id_barang AS brg, a.tanggal_keluar AS tgl, a.cabang_id AS cbg, 'out' jenis, b.jumlah AS jml, a.cabang_id AS cbg_t FROM barang_keluar a, detail_barang_keluar b 
            WHERE a.id_barang_keluar = b.id_barang_keluar 

            UNION

            SELECT b.id_barang AS brg, a.tgl_transfer AS tgl, a.asal_cabang AS cbg, 'out_tr' jenis, b.jumlah AS jml, a.tujuan_cabang AS cbg_t  FROM transfer_barang a, detail_transfer_barang b
            WHERE a.id_transfer = b.id_transfer


            WHERE a.id_retur = b.id_retur
            ) mt, barang a, cabang c, cabang d,satuan s WHERE mt.brg = a.id_barang AND c.id_cabang = mt.cbg AND d.id_cabang = mt.cbg_t AND a.satuan_id = s.id_satuan AND mt.cbg = '$id' ORDER BY mt.tgl DESC";

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

    public function getMutasiBarang($id = '', $id_cabang = '')
    {
        if ($id == '') {
            $where = '';
        } else {
            $where = 'AND mt.brg = "' . $id . '" AND mt.cbg = "'.$id_cabang.'"';
        }

        $query = "SELECT mt.*,s.nama_satuan, a.nama_barang, c.nama_cabang, d.nama_cabang AS trs_cbg FROM (
            SELECT c.id_barang AS brg, a.id_barang_masuk AS no, a.tanggal_masuk AS tgl, a.cabang_id AS cbg, 'in' jenis, b.jumlah AS jml, a.cabang_id AS cbg_t 
            FROM barang_masuk a
            JOIN detail_barang_masuk b ON a.id_barang_masuk = b.id_barang_masuk
            JOIN barang c ON b.id_barang = c.id_barang 
                UNION
            SELECT c.id_barang AS brg, a.id_barang_keluar AS no, a.tanggal_keluar AS tgl, a.cabang_id AS cbg, 'out' jenis, b.jumlah AS jml, a.cabang_id AS cbg_t 
            FROM barang_keluar a
            JOIN detail_barang_keluar b ON a.id_barang_keluar = b.id_barang_keluar 
            JOIN barang c ON b.id_barang = c.id_barang 
                UNION
            SELECT b.id_barang AS brg, a.kode_transfer AS no, a.tgl_transfer AS tgl, a.asal_cabang AS cbg, 'out_tr' jenis, b.jumlah AS jml, a.tujuan_cabang AS cbg_t  
            FROM transfer_barang a
            JOIN detail_transfer_barang b ON a.id_transfer = b.id_transfer
                UNION
            SELECT b.id_barang AS brg, a.kode_transfer AS no, a.tgl_transfer AS tgl, a.tujuan_cabang AS cbg, 'in_tr' jenis, b.jumlah AS jml, a.asal_cabang AS cbg_t  
            FROM transfer_barang a 
            JOIN detail_transfer_barang b ON a.id_transfer = b.id_transfer
                UNION
            SELECT b.id_barang AS brg, a.id_stock_op_name AS no, a.tanggal_stock_op_name AS tgl, a.cabang_id AS cbg, 'stkpn' jenis, b.jumlah AS jml, a.cabang_id AS cbg_t 
            FROM stock_op_name a
            JOIN detail_stock_op_name b ON a.id_stock_op_name = b.id_stock_op_name 

            ) mt, barang a, cabang c, cabang d,satuan s 
            WHERE mt.brg = a.id_barang AND c.id_cabang = mt.cbg AND d.id_cabang = mt.cbg_t AND a.satuan_id = s.id_satuan $where ORDER BY a.nama_barang desc, mt.tgl asc";

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

    public function getMutasiBarangByDate($id_barang = '', $id_cabang = '', $tgl_awal = null, $tgl_akhir = null)
    {
        if ($id_barang == '') {
            $where = '';
        } else { 
            $where = "AND mt.brg = '" . $id_barang . "' AND mt.cbg = '".$id_cabang."' AND mt.tgl BETWEEN '".$tgl_awal."' AND '".$tgl_akhir."'";
        }

        $query = "SELECT mt.*,s.nama_satuan, a.nama_barang, c.nama_cabang, d.nama_cabang AS trs_cbg FROM (
        
            SELECT c.id_barang AS brg, d.nama as nama_customer, a.id_barang_masuk AS no, a.tanggal_masuk AS tgl, a.cabang_id AS cbg, 'in' jenis, b.jumlah AS jml, a.cabang_id AS cbg_t 
            FROM barang_masuk a
            JOIN detail_barang_masuk b ON a.id_barang_masuk = b.id_barang_masuk
            JOIN user d ON a.user_id_pemilik = d.id_user 
            JOIN barang c ON b.id_barang = c.id_barang 
                UNION
            SELECT c.id_barang AS brg, d.nama as nama_customer, a.id_barang_keluar AS no, a.tanggal_keluar AS tgl, a.cabang_id AS cbg, 'out' jenis, b.jumlah AS jml, a.cabang_id AS cbg_t 
            FROM barang_keluar a
            JOIN detail_barang_keluar b ON a.id_barang_keluar = b.id_barang_keluar 
            JOIN user d ON b.user_id_pemilik = d.id_user 
            JOIN barang c ON b.id_barang = c.id_barang 
                UNION
            SELECT b.id_barang AS brg, d.user_id_pemilik as nama_customer, a.kode_transfer AS no, a.tgl_transfer AS tgl, a.asal_cabang AS cbg, 'out_tr' jenis, b.jumlah AS jml, a.tujuan_cabang AS cbg_t  
            FROM transfer_barang a
            JOIN detail_transfer_barang b ON a.id_transfer = b.id_transfer
            JOIN detail_barang_masuk c ON b.id_detail_masuk = c.id_detail
            JOIN barang_masuk d ON c.id_barang_masuk = d.id_barang_masuk
                UNION
            SELECT b.id_barang AS brg, d.user_id_pemilik as nama_customer, a.kode_transfer AS no, a.tgl_transfer AS tgl, a.tujuan_cabang AS cbg, 'in_tr' jenis, b.jumlah AS jml, a.asal_cabang AS cbg_t  
            FROM transfer_barang a 
            JOIN detail_transfer_barang b ON a.id_transfer = b.id_transfer
            JOIN detail_barang_masuk c ON b.id_detail_masuk = c.id_detail
            JOIN barang_masuk d ON c.id_barang_masuk = d.id_barang_masuk
                UNION
            SELECT b.id_barang AS brg, d.user_id_pemilik as nama_customer, a.id_stock_op_name AS no, a.tanggal_stock_op_name AS tgl, a.cabang_id AS cbg, 'stkpn' jenis, b.jumlah AS jml, a.cabang_id AS cbg_t 
            FROM stock_op_name a
            JOIN detail_stock_op_name b ON a.id_stock_op_name = b.id_stock_op_name 
            JOIN detail_barang_masuk c ON b.id_detail_masuk = c.id_detail
            JOIN barang_masuk d ON c.id_barang_masuk = d.id_barang_masuk
            
            ) mt, barang a, cabang c, cabang d,satuan s 
            WHERE mt.brg = a.id_barang AND c.id_cabang = mt.cbg AND d.id_cabang = mt.cbg_t AND a.satuan_id = s.id_satuan $where ORDER BY a.nama_barang desc, mt.tgl asc";

        return $this->db->query($query)->result_array();
    }
    public function getMutasiBarangByDateAndPemilik($id_pemilik = '', $id_barang = '', $id_cabang = '', $tgl_awal = null, $tgl_akhir = null, $id_detail = null)
    {
        if ($id_barang == '') {
            $where = '';
        } else { 
            $where = " AND mt.id_pemilik = '" .$id_pemilik. "' AND mt.id_detail = '" . $id_detail . "' AND mt.brg = '" . $id_barang . "' AND mt.cbg = '".$id_cabang."' AND mt.tgl BETWEEN '".$tgl_awal."' AND '".$tgl_akhir."'";
        }

        $query = "SELECT mt.*,s.nama_satuan, a.nama_barang, c.nama_cabang, d.nama_cabang AS trs_cbg FROM (
        
            SELECT b.id_detail, c.id_barang AS brg, a.user_id_pemilik as id_pemilik, d.nama as nama_customer, a.id_barang_masuk AS no, a.tanggal_masuk AS tgl, a.cabang_id AS cbg, 'in' jenis, b.jumlah AS jml, a.cabang_id AS cbg_t 
            FROM barang_masuk a
            JOIN detail_barang_masuk b ON a.id_barang_masuk = b.id_barang_masuk
            JOIN user d ON a.user_id_pemilik = d.id_user 
            JOIN barang c ON b.id_barang = c.id_barang 
                UNION
            SELECT b.id_detail_masuk as id_detail, c.id_barang AS brg, b.user_id_pemilik as id_pemilik, d.nama as nama_customer, a.id_barang_keluar AS no, a.tanggal_keluar AS tgl, a.cabang_id AS cbg, 'out' jenis, b.jumlah AS jml, a.cabang_id AS cbg_t 
            FROM barang_keluar a
            JOIN detail_barang_keluar b ON a.id_barang_keluar = b.id_barang_keluar 
            JOIN user d ON b.user_id_pemilik = d.id_user 
            JOIN barang c ON b.id_barang = c.id_barang 
                UNION
            SELECT b.id_detail_masuk as id_detail, b.id_barang AS brg, d.user_id_pemilik as id_pemilik, '' as nama_customer, a.kode_transfer AS no, a.tgl_transfer AS tgl, a.asal_cabang AS cbg, 'out_tr' jenis, b.jumlah AS jml, a.tujuan_cabang AS cbg_t  
            FROM transfer_barang a
            JOIN detail_transfer_barang b ON a.id_transfer = b.id_transfer
            JOIN detail_barang_masuk c ON b.id_detail_masuk = c.id_detail
            JOIN barang_masuk d ON c.id_barang_masuk = d.id_barang_masuk
                UNION
            SELECT b.id_detail_masuk as id_detail, b.id_barang AS brg, d.user_id_pemilik as id_pemilik, '' as nama_customer, a.kode_transfer AS no, a.tgl_transfer AS tgl, a.tujuan_cabang AS cbg, 'in_tr' jenis, b.jumlah AS jml, a.asal_cabang AS cbg_t  
            FROM transfer_barang a 
            JOIN detail_transfer_barang b ON a.id_transfer = b.id_transfer
            JOIN detail_barang_masuk c ON b.id_detail_masuk = c.id_detail
            JOIN barang_masuk d ON c.id_barang_masuk = d.id_barang_masuk
                UNION
            SELECT b.id_detail_masuk as id_detail, b.id_barang AS brg, d.user_id_pemilik as id_pemilik, '' as nama_customer, a.id_stock_op_name AS no, a.tanggal_stock_op_name AS tgl, a.cabang_id AS cbg, 'stkpn' jenis, b.jumlah AS jml, a.cabang_id AS cbg_t 
            FROM stock_op_name a
            JOIN detail_stock_op_name b ON a.id_stock_op_name = b.id_stock_op_name 
            JOIN detail_barang_masuk c ON b.id_detail_masuk = c.id_detail
            JOIN barang_masuk d ON c.id_barang_masuk = d.id_barang_masuk
            
            ) mt, barang a, cabang c, cabang d,satuan s 
            WHERE mt.brg = a.id_barang AND c.id_cabang = mt.cbg AND d.id_cabang = mt.cbg_t AND a.satuan_id = s.id_satuan $where ORDER BY a.nama_barang desc, mt.tgl asc";

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

    public function getMutasiBarangLaporan($limit = null, $id_barang = null, $range = null)
    {
        if ($range != null) {
            $where = 'AND mt.tgl BETWEEN "' . $range['mulai'] . '" AND "' . $range['akhir'] . '"';
        } else {
            $where = '';
        }

        $query = "SELECT mt.*,s.nama_satuan, a.nama_barang, c.nama_cabang, d.nama_cabang AS trs_cbg FROM (
            SELECT b.id_barang AS brg, a.tanggal_masuk AS tgl, a.cabang_id AS cbg, 'in' jenis, b.jumlah AS jml, a.cabang_id AS cbg_t FROM barang_masuk a,detail_barang_masuk b
            WHERE a.id_barang_masuk = b.id_barang_masuk
            UNION
            SELECT b.id_barang AS brg, a.tanggal_keluar AS tgl, a.cabang_id AS cbg, 'out' jenis, b.jumlah AS jml, a.cabang_id AS cbg_t FROM barang_keluar a, detail_barang_keluar b 
            WHERE a.id_barang_keluar = b.id_barang_keluar 
            UNION
            SELECT b.id_barang AS brg, a.tgl_transfer AS tgl, a.asal_cabang AS cbg, 'out_tr' jenis, b.jumlah AS jml, a.tujuan_cabang AS cbg_t  FROM transfer_barang a, detail_transfer_barang b
            WHERE a.id_transfer = b.id_transfer
            UNION
            SELECT b.id_barang AS brg, a.tanggal AS tgl, a.asal_cabang AS cbg, 'in_retur' jenis,b.jumlah AS jml,a.tujuan_cabang AS cbg_t FROM retur a, detail_retur b
            WHERE a.id_retur = b.id_retur
            ) mt, barang a, cabang c, cabang d,satuan s WHERE mt.brg = a.id_barang AND c.id_cabang = mt.cbg AND d.id_cabang = mt.cbg_t AND a.satuan_id = s.id_satuan $where ORDER BY a.nama_barang desc, mt.tgl asc";

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

    public function getStokDataBarang($id)
    {
        $query = 'SELECT * FROM barang a, satuan b where a.satuan_id = b.id_satuan and a.id_barang = "' . $id . '"';
        return $this->db->query($query)->row_array();
    }

    public function _saldo_awal_barang($id_barang, $tgl_awal, $tgl_akhir)
    {
        $dt_user = $this->admin->get('user', ['id_user' => $this->session->userdata('login_session')['user']]);
        $this->load->model('Admin_model', 'admin');
        $getBarangById = $this->admin->get('barang', ['id_barang' => $id_barang]);
        $mutasi_barang = $this->getMutasiBarangByDate($id_barang, $dt_user['cabang_id'], $tgl_awal, $tgl_akhir);

        $sisa_stok = $getBarangById['stokawal'];
        foreach ($mutasi_barang as $b) :

            switch ($b['jenis']) {
                case "in_tr":
                    $jenis = 'Mutasi Barang Masuk ke ' . $b['trs_cbg'];
                    break;
                case "out_tr":
                    $jenis = 'Mutasi Barang Keluar ke ' . $b['trs_cbg'];
                    break;
                case "in":
                    $jenis = 'Barang Masuk';
                    break;
                case "in_retur":
                    $jenis = 'Barang Retur dari ' . $b['nama_cabang'];
                    break;
                case "stkpn":
                    $jenis = 'Stok Op Name dari ' . $b['nama_cabang'];
                    break;
                default:
                    $jenis = 'Barang Keluar';
            }

            if($b['jenis'] == 'in' || $b['jenis'] == 'in_retur') {
                 $sisa_stok += $b['jml']; 
            } else if($b['jenis'] == 'in_tr' ) { 
                 $sisa_stok += $b['jml']; 
            } else if($b['jenis'] == 'stkpn' ) {
                $sisa_stok += $b['jml'];
            } else { 
                $sisa_stok -= $b['jml']; 
            } 
        endforeach;

        return $sisa_stok;
    }

    public function _saldo_awal_barang_by_user_pemilik($id_pemilik, $id_barang, $tgl_awal, $tgl_akhir, $id_detail = null)
    {
        $dt_user = $this->admin->get('user', ['id_user' => $this->session->userdata('login_session')['user']]);
        $this->load->model('Admin_model', 'admin');
        $getBarangById = $this->admin->get('barang', ['id_barang' => $id_barang]);
        $mutasi_barang = $this->getMutasiBarangByDateAndPemilik($id_pemilik, $id_barang, $dt_user['cabang_id'], $tgl_awal, $tgl_akhir, $id_detail);

        $sisa_stok = $getBarangById['stokawal'];
        foreach ($mutasi_barang as $b) :

            switch ($b['jenis']) {
                case "in_tr":
                    $jenis = 'Mutasi Barang Masuk ke ' . $b['trs_cbg'];
                    break;
                case "out_tr":
                    $jenis = 'Mutasi Barang Keluar ke ' . $b['trs_cbg'];
                    break;
                case "in":
                    $jenis = 'Barang Masuk';
                    break;
                case "in_retur":
                    $jenis = 'Barang Retur dari ' . $b['nama_cabang'];
                    break;
                case "stkpn":
                    $jenis = 'Stok Op Name dari ' . $b['nama_cabang'];
                    break;
                default:
                    $jenis = 'Barang Keluar';
            }

            if($b['jenis'] == 'in' || $b['jenis'] == 'in_retur') {
                 $sisa_stok += $b['jml']; 
            } else if($b['jenis'] == 'in_tr' ) { 
                 $sisa_stok += $b['jml']; 
            } else if($b['jenis'] == 'stkpn' ) {
                $sisa_stok += $b['jml'];
            } else { 
                $sisa_stok -= $b['jml']; 
            } 
        endforeach;

        return $sisa_stok;
    }

    public function _stokakhir_barang_by_user_pemilik($id_pemilik, $id_barang, $tgl_awal, $tgl_akhir)
    {
        $dt_user = $this->admin->get('user', ['id_user' => $this->session->userdata('login_session')['user']]);
        $this->load->model('Admin_model', 'admin');
        $getBarangById = $this->admin->get('barang', ['id_barang' => $id_barang]);
        $mutasi_barang = $this->getStokAkhirMutasiBarangByDateAndPemilik($id_pemilik, $id_barang, $dt_user['cabang_id'], $tgl_awal, $tgl_akhir);

        $sisa_stok = $getBarangById['stokawal'];
        foreach ($mutasi_barang as $b) :

            switch ($b['jenis']) {
                case "in_tr":
                    $jenis = 'Mutasi Barang Masuk ke ' . $b['trs_cbg'];
                    break;
                case "out_tr":
                    $jenis = 'Mutasi Barang Keluar ke ' . $b['trs_cbg'];
                    break;
                case "in":
                    $jenis = 'Barang Masuk';
                    break;
                case "in_retur":
                    $jenis = 'Barang Retur dari ' . $b['nama_cabang'];
                    break;
                case "stkpn":
                    $jenis = 'Stok Op Name dari ' . $b['nama_cabang'];
                    break;
                default:
                    $jenis = 'Barang Keluar';
            }

            if($b['jenis'] == 'in' || $b['jenis'] == 'in_retur') {
                 $sisa_stok += $b['jml']; 
            } else if($b['jenis'] == 'in_tr' ) { 
                 $sisa_stok += $b['jml']; 
            } else if($b['jenis'] == 'stkpn' ) {
                $sisa_stok += $b['jml'];
            } else { 
                $sisa_stok -= $b['jml']; 
            } 
        endforeach;

        return $sisa_stok;
    }

    public function getStokAkhirMutasiBarangByDateAndPemilik($id_pemilik = '', $id_barang = '', $id_cabang = '', $tgl_awal = null, $tgl_akhir = null)
    {
        if ($id_barang == '') {
            $where = '';
        } else { 
            $where = " AND mt.id_pemilik = '" .$id_pemilik. "' AND mt.brg = '" . $id_barang . "' AND mt.cbg = '".$id_cabang."' AND mt.tgl BETWEEN '".$tgl_awal."' AND '".$tgl_akhir."'";
        }

        $query = "SELECT mt.*,s.nama_satuan, a.nama_barang, c.nama_cabang, d.nama_cabang AS trs_cbg FROM (
        
            SELECT c.id_barang AS brg, a.user_id_pemilik as id_pemilik, d.nama as nama_customer, a.id_barang_masuk AS no, a.tanggal_masuk AS tgl, a.cabang_id AS cbg, 'in' jenis, b.jumlah AS jml, a.cabang_id AS cbg_t 
            FROM barang_masuk a
            JOIN detail_barang_masuk b ON a.id_barang_masuk = b.id_barang_masuk
            JOIN user d ON a.user_id_pemilik = d.id_user 
            JOIN barang c ON b.id_barang = c.id_barang 
                UNION
            SELECT c.id_barang AS brg, b.user_id_pemilik as id_pemilik, d.nama as nama_customer, a.id_barang_keluar AS no, a.tanggal_keluar AS tgl, a.cabang_id AS cbg, 'out' jenis, b.jumlah AS jml, a.cabang_id AS cbg_t 
            FROM barang_keluar a
            JOIN detail_barang_keluar b ON a.id_barang_keluar = b.id_barang_keluar 
            JOIN user d ON b.user_id_pemilik = d.id_user 
            JOIN barang c ON b.id_barang = c.id_barang 
                UNION
            SELECT b.id_barang AS brg, d.user_id_pemilik as id_pemilik, '' as nama_customer, a.kode_transfer AS no, a.tgl_transfer AS tgl, a.asal_cabang AS cbg, 'out_tr' jenis, b.jumlah AS jml, a.tujuan_cabang AS cbg_t  
            FROM transfer_barang a
            JOIN detail_transfer_barang b ON a.id_transfer = b.id_transfer
            JOIN detail_barang_masuk c ON b.id_detail_masuk = c.id_detail
            JOIN barang_masuk d ON c.id_barang_masuk = d.id_barang_masuk
                UNION
            SELECT b.id_barang AS brg, d.user_id_pemilik as id_pemilik, '' as nama_customer, a.kode_transfer AS no, a.tgl_transfer AS tgl, a.tujuan_cabang AS cbg, 'in_tr' jenis, b.jumlah AS jml, a.asal_cabang AS cbg_t  
            FROM transfer_barang a 
            JOIN detail_transfer_barang b ON a.id_transfer = b.id_transfer
            JOIN detail_barang_masuk c ON b.id_detail_masuk = c.id_detail
            JOIN barang_masuk d ON c.id_barang_masuk = d.id_barang_masuk
                UNION
            SELECT b.id_barang AS brg, d.user_id_pemilik as id_pemilik, '' as nama_customer, a.id_stock_op_name AS no, a.tanggal_stock_op_name AS tgl, a.cabang_id AS cbg, 'stkpn' jenis, b.jumlah AS jml, a.cabang_id AS cbg_t 
            FROM stock_op_name a
            JOIN detail_stock_op_name b ON a.id_stock_op_name = b.id_stock_op_name 
            JOIN detail_barang_masuk c ON b.id_detail_masuk = c.id_detail
            JOIN barang_masuk d ON c.id_barang_masuk = d.id_barang_masuk
            
            ) mt, barang a, cabang c, cabang d,satuan s 
            WHERE mt.brg = a.id_barang AND c.id_cabang = mt.cbg AND d.id_cabang = mt.cbg_t AND a.satuan_id = s.id_satuan $where ORDER BY a.nama_barang desc, mt.tgl asc";

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





    // getData Barang Masuk By Id User
    public function getDetailBarangMasukByIdUser($id_user, $id_cabang, $id_barang = null)
    {
        $this->db->select('
            a.id_detail, a.id_barang_masuk, a.id_barang, a.merk, a.spesifikasi, a.barcode, a.sn,
            b.tanggal_masuk,
            c.nama_barang, 
            s.nama_satuan, 
            j.nama_jenis,
            sp.id_user as id_user_pemilik, sp.nama as nm_user_pemilik
        ');
        $this->db->from('detail_barang_masuk a');
        $this->db->join('barang_masuk b', 'a.id_barang_masuk = b.id_barang_masuk','left');
        $this->db->join('user sp', 'b.user_id_pemilik = sp.id_user','left');
        $this->db->join('barang c', 'a.id_barang = c.id_barang','left');
        $this->db->join('satuan s', 'c.satuan_id = s.id_satuan','left');
        $this->db->join('jenis j', 'c.jenis_id = j.id_jenis','left');
        $this->db->where([
            'b.user_id_pemilik' => $id_user,
            'b.cabang_id'       => $id_cabang
        ]);
        if ($id_barang != null) {
            $this->db->where('a.id_barang', $id_barang);
        }
        $query1 = $this->db->get_compiled_select();


        $this->db->select('
            dbms.id_detail, b.kode_transfer as id_barang_masuk, dbms.id_barang, dbms.merk, dbms.spesifikasi, dbms.barcode, dbms.sn,
            b.tgl_transfer as tanggal_masuk,
            c.nama_barang, 
            s.nama_satuan, 
            j.nama_jenis,
            sp.id_user as id_user_pemilik, sp.nama as nm_user_pemilik
        ');
        $this->db->from('detail_transfer_barang a');
        $this->db->join('transfer_barang b', 'a.id_transfer = b.id_transfer','left');
        $this->db->join('detail_barang_masuk dbms', 'a.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 sp', 'a.user_id_pemilik = sp.id_user');
        $this->db->join('barang c', 'a.id_barang = c.id_barang');
        $this->db->join('satuan s', 'c.satuan_id = s.id_satuan');
        $this->db->join('jenis j', 'c.jenis_id = j.id_jenis','left');
        $this->db->where([
            'a.user_id_pemilik' => $id_user,
            'b.tujuan_cabang'=> $id_cabang
        ]);
        if ($id_barang != null) {
            $this->db->where('a.id_barang', $id_barang);
        }
        $query2 = $this->db->get_compiled_select();

         $sql = "($query1) UNION ($query2)";

        return $this->db->query($sql)->result_array();
    }

    public function getUserPemilikFormBarangMasukByIdBarang($id_barang)
    {
        // $this->db->distinct();
        $this->db->select('b.user_id_pemilik, sp.nama as nm_user_pemilik, c.id_barang, c.nama_barang, s.nama_satuan, a.id_detail');
        $this->db->from('detail_barang_masuk a');
        $this->db->join('barang_masuk b', 'a.id_barang_masuk = b.id_barang_masuk');
        $this->db->join('user sp', 'b.user_id_pemilik = sp.id_user');
        $this->db->join('barang c', 'a.id_barang = c.id_barang');
        $this->db->join('satuan s', 'c.satuan_id = s.id_satuan');
        $this->db->where('a.id_barang', $id_barang);
        return $this->db->get()->result_array();
    }
    // tutup getbarang masuk by id user




    // stock op name
    public function getStockOpNameByCabang($cabang = null, $range = null, $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,
            cb.nama_cabang,
            j.nama_jenis,
        ');

        $this->db->from('detail_stock_op_name a');
        $this->db->join('stock_op_name b', 'a.id_stock_op_name = b.id_stock_op_name');
        $this->db->join('detail_barang_masuk dbms', 'a.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', 'b.user_id = u.id_user','left');
        $this->db->join('user up', 'a.user_id_pemilik = up.id_user','left');
        $this->db->join('barang c', 'a.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 cb', 'b.cabang_id = cb.id_cabang','left');
        $this->db->join('jenis j', 'c.jenis_id = j.id_jenis','left');

        if ($cabang != null) {
            $this->db->where('b.cabang_id', $cabang);
        }

        if ($range != null) {
            $this->db->where('b.tanggal_stock_op_name' . ' >=', $range['mulai']);
            $this->db->where('b.tanggal_stock_op_name' . ' <=', $range['akhir']);
        }

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

        if ($id_impor != null) {
            $this->db->where_in('b.id_impor', $id_impor);
        }
    
        $this->db->order_by('b.id_stock_op_name', 'DESC');
        // $this->db->group_by('bk.id_stock_op_name');
        return $this->db->get()->result_array();
    }
    

    // stok opname
    public function getStockOpNameById($id)
    {
        $this->db->select('*');
        $this->db->from('stock_op_name a');
        $this->db->join('user u', 'a.user_id = u.id_user','left');
        $this->db->join('cabang cb', 'a.cabang_id = cb.id_cabang','left');
        $this->db->where('a.id_stock_op_name', $id);
        return $this->db->get()->row_array();
    }

    public function getDetailStockOpNameById($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('stock_op_name a');
        $this->db->join('detail_stock_op_name b', 'a.id_stock_op_name = b.id_stock_op_name');
        $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_stock_op_name', $id);
        return $this->db->get()->result_array();
    }
    
    public function insertStockOpName()
    {
        $id_stock_op_name = $this->input->post('id_stock_op_name', true);
        $id_cabang = $this->input->post('cabang_id', true);
        $tanggal = $this->input->post('tanggal_stock_op_name', true);
        $keterangan = $this->input->post('keterangan', true);

        $data = [
            "id_stock_op_name" => $id_stock_op_name,
            "user_id" => $this->input->post('user_id'),
            "tanggal_stock_op_name" => $tanggal,
            "cabang_id" => $id_cabang,
            "ket" => $keterangan
        ];

        $this->db->insert('stock_op_name', $data);

        $id_barang = $this->input->post('id_barang[]', true);
        $jumlah = $this->input->post('jumlah[]', true);
        $id_detail_masuk = $this->input->post('id_detail_masuk[]');
        $user_id_pemilik = $this->input->post('user_id_pemilik[]');
        $data_detail = array();
        $i = 0;
        foreach ($id_barang as $id_br) {
            $this->inputBarangCabang($id_br, $id_cabang);

            array_push($data_detail, array(
                'id_stock_op_name' => $id_stock_op_name,
                'jumlah' => $jumlah[$i],
                'id_detail_masuk' => $id_detail_masuk[$i],
                'user_id_pemilik' => $user_id_pemilik[$i],
                'id_barang' => $id_br,
            ));
            $i++;
        }

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

    public function insertStockOpNameImpor($id_impor, $data_impor)
    {
        $this->load->model('Admin_model', 'admin');
        // mencari kode id barang masuk
        $kode = 'SO-' . date('ymd');
        $kode_terakhir = $this->admin->getMax('stock_op_name', 'id_stock_op_name', $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_stock_op_name = $kode . $number;

            // mencari id_barang_masuk
            $dt_impor = [
                "id_stock_op_name" => $id_stock_op_name,
                "user_id" => $row['id_user'],
                "tanggal_stock_op_name" => $row['tgl'],
                "cabang_id" => $row['cabang_id'],
                "ket" => $row['ket'],
                "id_impor" => $id_impor,
            ];

            $this->db->insert('stock_op_name', $dt_impor);

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

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



    public function editStockOpName($id)
    {

        $this->db->delete('detail_stock_op_name', ['id_stock_op_name' => $id]);

        $id_stock_op_name = $id;
        $id_cabang = $this->input->post('cabang_id', true);
        $tanggal = $this->input->post('tanggal_stock_op_name', true);
        $keterangan = $this->input->post('keterangan', true);

        $data = [
            "user_id" => $this->input->post('user_id'),
            "tanggal_stock_op_name" => $tanggal,
            "cabang_id" => $id_cabang,
            "ket" => $keterangan
        ];

        $this->db->where('id_stock_op_name', $id);
        $this->db->update('stock_op_name', $data);


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

        $data_detail = array();
        $i = 0;
        foreach ($id_barang as $id_br) {
            $this->inputBarangCabang($id_br, $id_cabang);

            array_push($data_detail, array(
                'id_stock_op_name' => $id_stock_op_name,
                'id_detail_masuk' => $id_detail_masuk[$i],
                'user_id_pemilik' => $user_id_pemilik[$i],
                'jumlah' => $jumlah[$i],
                'id_barang' => $id_br
            ));
            $i++;
        }

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

}
