<?php

namespace App\Http\Controllers\inventori;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use DB;
use Illuminate\Http\Request;
use Yajra\Datatables\Datatables;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Session;

class PersetujuanController extends Controller{

	public function __construct(){     
        $this->middleware(function ($request, $next) {
        $url = request()->segment(1);
        $kode_group = Session::get('kode_group');
        $d_menu = DB::table("tb_menu as a")->join("tb_role as b","a.kode_menu","b.kode_menu")->where([['a.link_menu',$url],['b.kode_group',$kode_group],['b.status_role','1'],['a.active','1']])->orderBy('a.sort_menu','asc')->count();
        if(Session::get('decode') <> 1)
         {
            Session::flash('message', 'Silahkan login dengan username dan password anda.!!');
             return redirect()->to('login');
         }
         if($d_menu == 0){
            Session::flush();
            Session::flash('message', 'Anda Tidak Punya Akses ke Halaman ini.!!');
             return redirect()->to('login');
         }
        return $next($request); 
        });

        $this->session = array('id_gudang'=>Session::get('id_gudang'),'id_proses'=>Session::get('id_proses'),'id_lokasi'=>Session::get('id_lokasi'), 'kode_group' => Session::get('kode_group'), 'kode_user' => Session::get('kode_user'), 'id_proses' => Session::get('id_proses'));
    }

	function index(){
		return view('inventori.PersetujuankeluarList');
	}

	function get_keluar(){
        $id_gudang = $this->session['id_gudang'];
        $id_proses = $this->session['id_proses'];

		DB::statement(DB::raw('set @rownum=0'));
		$barangkeluar = DB::table('pro_barangkeluar as pb')->join('pro_barangkeluar_barang as pbk', 'pb.id_barangkeluar', 'pbk.id_barangkeluar')->join('ms_proses as mp', 'pb.id_proses', 'mp.id_proses')->leftJoin('ms_rekanan as mr', 'pb.id_rekanan', 'mr.id_rekanan')->leftJoin('ms_pegawai as mpg', 'pb.id_pegawai', 'mpg.id_pegawai')->leftJoin('pem_workorder as pw', 'pb.id_workorder', 'pw.id_workorder')->leftJoin('pem_workorder_barang as pwb', 'pb.id_workorder_barang', 'pwb.id_workorder_barang')->leftjoin('ms_mesin as mm', 'pb.id_mesin', 'mm.id_mesin')->orderBy('pb.status', 'asc')->orderBy('pb.tanggal', 'desc')->groupby('pb.id_barangkeluar')->select(DB::raw('pb.*, mp.nama_proses, mr.nama, mpg.nama_pegawai, pw.id_workorder, pw.nomor nomor_workorder, pwb.id_workorder_barang, pwb.no_workorder nomor_workorder_barang, mm.nama nama_mesin, GROUP_CONCAT(pb.status) AS group_status, LOCATE("0", GROUP_CONCAT(pbk.status)) AS is_0, @rownum  := @rownum  + 1 AS rownum'));

        if($id_gudang != 0 || $id_gudang != ''){
            $barangkeluar->whereIn('pbk.id_gudang', explode(",", $id_gudang))->groupby('pb.id_barangkeluar');
        }

        if($id_proses != 0 || $id_proses != ''){
            $barangkeluar->whereIn('pb.id_proses', explode(",", $id_proses));
        }


        $keluarkemasan = DB::table("pro_barangkeluar_kemasan as pbk")->leftjoin('pro_barangkeluar_kemasan_barang as pbkb', 'pbk.id_barangkeluar_kemasan', 'pbkb.id_barangkeluar_kemasan')->leftjoin("ms_proses as mp", "pbk.id_proses", "mp.id_proses")->leftjoin("ms_rekanan as mr", "pbk.id_rekanan", "mr.id_rekanan")->leftjoin("ms_pegawai as mpg", "pbk.id_pegawai", "mpg.id_pegawai")->orderBy('pbk.status', 'asc')->orderBy('pbk.tanggal', 'desc')->groupby('pbk.id_barangkeluar_kemasan')->select(DB::raw("pbk.*, mp.nama_proses, mr.nama nama_rekanan, mpg.nama_pegawai, GROUP_CONCAT(pbk.status) AS group_status, LOCATE('0', GROUP_CONCAT(pbkb.status)) AS is_0"));

        if($id_gudang != 0 || $id_proses != ''){
            $keluarkemasan->whereIn('pbkb.id_gudang', explode(",", $id_gudang))->groupby('pbk.id_barangkeluar_kemasan');
        }

        $barangkeluar->havingRaw("LOCATE('0', GROUP_CONCAT(pbk.status)) != 0");
        $keluarkemasan->havingRaw("LOCATE('0', GROUP_CONCAT(pbkb.status)) != 0");
        // print_r($barangkeluar->get()); exit();

        

        if($barangkeluar->count() > 0){
            foreach($barangkeluar->get() as $b){
                $pekerja = '';
                $stat_pekerja = '';
                if($b->nama != '' || !is_null($b->nama)){
                    $pekerja = $b->nama;
                    $stat_pekerja = 'Rekanan';
                }else if($b->nama_pegawai != '' || !is_null($b->nama_pegawai)){
                    $pekerja = $b->nama_pegawai;
                    $stat_pekerja = 'Pegawai';
                }else if($b->nama_mesin != '' || !is_null($b->nama_mesin)){
                    $pekerja = $b->nama_mesin;
                    $stat_pekerja = 'Mesin';
                }

                $nomor = "";
                $no_ket = "";
                if($b->nomor_workorder_barang != '' || !is_null($b->nomor_workorder_barang)){
                    $nomor = $b->nomor_workorder_barang;
                    $no_ket = "(No. Order)";
                }else if($b->nomor_workorder != '' || !is_null($b->nomor_workorder)){
                    $nomor = $b->nomor_workorder;
                    $no_ket = "(No. SPK)";
                }

                // if(in_array(0, $this->get_status($b->id_barangkeluar, 0))){
                //     $status = 0;
                // }else{
                //     $status = 1;
                // }

                if($b->is_0 == 0){
                    $status = 0;
                }else{
                    $status = 1;
                }

                $arr[] = array('id_barangkeluar' => $b->id_barangkeluar,
                                'nomor' => $b->noAuto.$b->noAuto_ext,
                				'no_workorder' => $nomor.' '.$no_ket,
                                'tanggal' => date('d-m-Y', strtotime($b->tanggal)),
                                'tanggal_perkiraan' => date('d-m-Y', strtotime($b->tanggal_perkiraan)),
                                'nama_proses' => $b->nama_proses,
                                'pekerja' => $pekerja,
                                'stat_pekerja' => $stat_pekerja,
                                'proses' => $b->nama_proses,
                                'status_id' => $status,
                            	// 'status' => $this->proses($this->get_status($b->id_barangkeluar, 0)),
                                'status' => $this->text_status($b->is_0),
                            	'rownum' => '1',
                            	'posisi' => $pekerja.' ('.$stat_pekerja.')',
                            	'aksi' => '<a class="btn bg-info-400" type="button" href="'.url('persetujuankeluar/verifikasikeluar/').'/'.$b->id_barangkeluar.'/1'.'"><i class="icon-checkbox-checked2"></i></a>',
                                'checkbox' => status_checkbox($status, $b->id_barangkeluar, '1'));
            }
        }else{
            $arr = array();
        }

        if($keluarkemasan->count() > 0){
            foreach ($keluarkemasan->get() as $k) {
                $pekerja = '';
                $stat_pekerja = '';
                if($k->id_rekanan != '0'){
                    $pekerja = $k->nama_rekanan;
                    $stat_pekerja = 'Rekanan';
                }else{
                    $pekerja = $k->nama_pegawai;
                    $stat_pekerja = 'Pegawai';
                } 

                //  if(in_array(0, $this->get_status($k->id_barangkeluar_kemasan, 1))){
                //     $status = 0;
                // }else{
                //     $status = 1;
                // }

                if($k->is_0 == 0){
                    $status = 0;
                }else{
                    $status = 1;
                }

                $arr[] = array('id_barangkeluar' => $k->id_barangkeluar_kemasan,
                                'no_workorder' => '',
                                'nomor' => $k->noAuto.$k->noAuto_ext,
                                'tanggal' => date('d-m-Y', strtotime($k->tanggal)),
                                'tanggal_perkiraan' => date('d-m-Y', strtotime($k->tanggal_perkiraan)),
                                'nama_proses' => $k->nama_proses,
                                'pekerja' => $pekerja,
                                'stat_pekerja' => $stat_pekerja,
                                'proses' => $k->nama_proses,
                                'status_id' => $status,
                                // 'status' => $this->proses($this->get_status($k->id_barangkeluar_kemasan, 1)),
                                'status' => $this->text_status($k->is_0),
                                'rownum' => '2',
                                'posisi' => $pekerja.' ('.$stat_pekerja.')',
                                'aksi' => '<a class="btn bg-info-400" type="button" href="'.url('persetujuankeluar/verifikasikeluar/').'/'.$k->id_barangkeluar_kemasan.'/2'.'"><i class="icon-checkbox-checked2"></i></a>',
                                'checkbox' => status_checkbox($status, $k->id_barangkeluar_kemasan, '2'));
            }
        }

		$data = new Collection($arr);
        return Datatables::of($data)
                ->make(true);
	}

	function tampilmasuk(){
		return view('inventori.PersetujuanmasukList');
	}

	function get_masuk(){
        $id_gudang = $this->session['id_gudang'];
        $id_proses = $this->session['id_proses'];

		DB::statement(DB::raw('set @rownum=0'));
		
        $barangkeluar = get_barangkeluar_verif()->join('pro_barangmasuk_barang as pbb', 'pbm.id_barangmasuk', 'pbb.id_barangmasuk')->where('pbk.id_proses', '!=', '37')->groupby('pbm.id_barangmasuk');

        

        $masukkemasan = DB::table("pro_barangmasuk_kemasan as pbk")->join('pro_barangmasuk_kemasan_barang as pbkb', 'pbk.id_barangmasuk_kemasan', 'pbkb.id_barangmasuk_kemasan')->join('pro_barangkeluar_kemasan as pbkel', 'pbk.id_barangkeluar_kemasan', 'pbkel.id_barangkeluar_kemasan')->leftjoin('ms_proses as mp', 'pbkel.id_proses', 'mp.id_proses')->leftjoin('ms_rekanan as mr', 'pbkel.id_rekanan', 'mr.id_rekanan')->leftjoin('ms_pegawai as mpg', 'pbkel.id_pegawai', 'mpg.id_pegawai')->orderBy('pbk.status', 'asc')->orderBy('pbk.tanggal', 'desc')->select(DB::raw("pbk.*, mp.nama_proses, mr.nama nama_rekanan, mpg.nama_pegawai, pbkel.id_rekanan, pbkel.id_pegawai, pbkel.tanggal_perkiraan, GROUP_CONCAT(pbkb.status) AS group_status, LOCATE('0', GROUP_CONCAT(pbkb.status)) AS is_0"))->groupby('pbk.id_barangmasuk_kemasan');        


        $barangkeluar_tenun = get_barangkeluar_verif()->join('pro_barangmasuk_barang as pbb', 'pbm.id_barangmasuk', 'pbb.id_barangmasuk')->where('pbk.id_proses', '37')->groupby('pbm.id_barangmasuk');

        if($id_gudang != 0 || $id_gudang != ''){
            $barangkeluar->whereIn('pbb.id_gudang', explode(",", $id_gudang));
            $masukkemasan->whereIn('pbkb.id_gudang', explode(",", $id_gudang));
            $barangkeluar_tenun->whereIn('pbb.id_gudang', explode(",", $id_gudang));
        }

        if($id_proses != 0 || $id_proses != ''){
            $barangkeluar->whereIn('pbk.id_proses', explode(",", $id_proses));
            $masukkemasan->whereIn('pbkel.id_proses', explode(",", $id_proses));
            $barangkeluar_tenun->whereIn('pbk.id_proses', explode(",", $id_proses));
        }

        $barangkeluar->havingRaw("LOCATE('0', GROUP_CONCAT(pbb.status)) != 0");
        $masukkemasan->havingRaw("LOCATE('0', GROUP_CONCAT(pbkb.status)) != 0");
        $barangkeluar_tenun->havingRaw("LOCATE('0', GROUP_CONCAT(pbb.status)) != 0");

        $arr = array();
        
        if($barangkeluar->count() > 0){
            foreach($barangkeluar->get() as $b){
                $pekerja = '';
                $stat_pekerja = '';
                if($b->nama_rekanan != '' || !is_null($b->nama_rekanan)){
                    $pekerja = $b->nama_rekanan;
                    $stat_pekerja = 'Rekanan';
                }else if($b->nama_pegawai != '' || !is_null($b->nama_pegawai)){
                    $pekerja = $b->nama_pegawai;
                    $stat_pekerja = 'Pegawai';
                }else if($b->nama_mesin != '' || !is_null($b->nama_mesin)){
                    $pekerja = $b->nama_mesin;
                    $stat_pekerja = 'Mesin';
                }

                $nomor = "";
                $no_ket = "";
                if($b->nomor_workorder_barang != '' || !is_null($b->nomor_workorder_barang)){
                    $nomor = $b->nomor_workorder_barang;
                    $no_ket = "(No. Order)";
                }else if($b->nomor_workorder != '' || !is_null($b->nomor_workorder)){
                    $nomor = $b->nomor_workorder;
                    $no_ket = "(No. SPK)";
                }

                // if(in_array(0, $this->get_status($b->id_barangmasuk, 2))){
                //     $status = 0;
                // }else{
                //     $status = 1;
                // }

                if($b->is_0 == 0){
                    $status = 0;
                }else{
                    $status = 1;
                }



                $arr[] = array('id_barangkeluar' => $b->id_barangkeluar,
                                'id_barangmasuk' => $b->id_barangmasuk,
                                'nomor' => $b->noAuto.$b->noAuto_ext,
                				'no_workorder' => $nomor.' '.$no_ket,
                                'tanggal' => date('d-m-Y', strtotime($b->tanggal)),
                                'tanggal_perkiraan' => date('d-m-Y', strtotime($b->tanggal_perkiraan)),
                                'nama_proses' => $b->nama_proses,
                                'pekerja' => $pekerja,
                                'stat_pekerja' => $stat_pekerja,
                                'proses' => $b->nama_proses,
                                'status_id' => $b->status,
                            	// 'status' => $this->proses($this->get_status($b->id_barangmasuk, 2)),
                                'status' => $this->text_status($b->is_0),
                            	'rownum' => '1',
                            	'posisi' => $pekerja.' ('.$stat_pekerja.')',
                            	'aksi' => '<a class="btn bg-info-400" type="button" href="'.url('persetujuanmasuk/verifikasimasuk/').'/'.$b->id_barangmasuk.'/1'.'"><i class="icon-checkbox-checked2"></i></a>',
                                'checkbox' => status_checkbox($status, $b->id_barangmasuk, 1));
            }
        }else{
            
        }

        if($masukkemasan->count() > 0){
            foreach ($masukkemasan->get() as $k) {
                $pekerja = '';
                $stat_pekerja = '';
                if($k->id_rekanan != '0'){
                    $pekerja = $k->nama_rekanan;
                    $stat_pekerja = 'Rekanan';
                }else{
                    $pekerja = $k->nama_pegawai;
                    $stat_pekerja = 'Pegawai';
                } 

                // if(in_array(0, $this->get_status($k->id_barangmasuk_kemasan, 3))){
                //     $status = 0;
                // }else{
                //     $status = 1;
                // }

                if($k->is_0 == 0){
                    $status = 0;
                }else{
                    $status = 1;
                }

                

                $arr[] = array('id_barangkeluar' => $k->id_barangkeluar_kemasan,
                                'id_barangmasuk' => $k->id_barangmasuk_kemasan,
                                'no_workorder' => '',
                                'nomor' => $k->noAuto.$k->noAuto_ext,
                                'tanggal' => date('d-m-Y', strtotime($k->tanggal)),
                                'tanggal_perkiraan' => date('d-m-Y', strtotime($k->tanggal_perkiraan)),
                                'nama_proses' => $k->nama_proses,
                                'pekerja' => $pekerja,
                                'stat_pekerja' => $stat_pekerja,
                                'proses' => $k->nama_proses,
                                'status_id' => $k->status,
                                // 'status' => $this->proses($this->get_status($k->id_barangmasuk_kemasan, 3)),
                                'status' => $this->text_status($b->is_0),
                                'rownum' => '2',
                                'posisi' => $pekerja.' ('.$stat_pekerja.')',
                                'aksi' => '<a class="btn bg-info-400" type="button" href="'.url('persetujuanmasuk/verifikasimasuk/').'/'.$k->id_barangmasuk_kemasan.'/2'.'"><i class="icon-checkbox-checked2"></i></a>',
                                'checkbox' => status_checkbox($status, $k->id_barangmasuk_kemasan, 2));
            }
        }

        if($barangkeluar_tenun->count() > 0){
            foreach($barangkeluar_tenun->get() as $b){
                $pekerja = '';
                $stat_pekerja = '';
                if($b->nama_rekanan != '' || !is_null($b->nama_rekanan)){
                    $pekerja = $b->nama_rekanan;
                    $stat_pekerja = 'Rekanan';
                }else if($b->nama_pegawai != '' || !is_null($b->nama_pegawai)){
                    $pekerja = $b->nama_pegawai;
                    $stat_pekerja = 'Pegawai';
                }else if($b->nama_mesin != '' || !is_null($b->nama_mesin)){
                    $pekerja = $b->nama_mesin;
                    $stat_pekerja = 'Mesin';
                }

                $nomor = "";
                $no_ket = "";
                if($b->nomor_workorder_barang != '' || !is_null($b->nomor_workorder_barang)){
                    $nomor = $b->nomor_workorder_barang;
                    $no_ket = "(No. Order)";
                }else if($b->nomor_workorder != '' || !is_null($b->nomor_workorder)){
                    $nomor = $b->nomor_workorder;
                    $no_ket = "(No. SPK)";
                }

                // if(in_array(0, $this->get_status($b->id_barangmasuk, 2))){
                //     $status = 0;
                // }else{
                //     $status = 1;
                // }

                if($b->is_0 == 0){
                    $status = 0;
                }else{
                    $status = 1;
                }

                $arr[] = array('id_barangkeluar' => $b->id_barangkeluar,
                                'id_barangmasuk' => $b->id_barangmasuk,
                                'no_workorder' => $nomor.' '.$no_ket,
                                'nomor' => $b->noAuto.$b->noAuto_ext,
                                'tanggal' => date('d-m-Y', strtotime($b->tanggal)),
                                'tanggal_perkiraan' => date('d-m-Y', strtotime($b->tanggal_perkiraan)),
                                'nama_proses' => $b->nama_proses,
                                'pekerja' => $pekerja,
                                'stat_pekerja' => $stat_pekerja,
                                'proses' => $b->nama_proses,
                                'status_id' => $b->status,
                                // 'status' => $this->proses($this->get_status($b->id_barangmasuk, 2)),
                                'status' => $this->text_status($b->is_0),
                                'rownum' => '1',
                                'posisi' => $pekerja.' ('.$stat_pekerja.')',
                                'aksi' => '<a class="btn bg-info-400" type="button" href="'.url('persetujuanmasuk/verifikasimasuk/').'/'.$b->id_barangmasuk.'/3'.'"><i class="icon-checkbox-checked2"></i></a>',
                                'checkbox' => status_checkbox($status, $b->id_barangmasuk, 1));
            }
        }else{
            
        }

       
        
        $data = new Collection($arr);
        return Datatables::of($data)
                ->make(true);
	}

	function verifikasikeluar($id){
		$d_data = DB::table('pro_barangkeluar AS pb')->leftjoin('ms_rekanan AS mr', 'pb.id_rekanan', 'mr.id_rekanan')->leftjoin('ms_pegawai as mpe','pb.id_pegawai','mpe.id_pegawai')->join('ms_proses AS mp', 'pb.id_proses', 'mp.id_proses')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->select(DB::raw('pb.*, mp.nama_proses AS nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai as nama_pegawai, mrk.nama nama_kelompok'))->where('pb.id_barangkeluar', $id);

       

        $id_gudang = $this->session['id_gudang'];

        $d_barang = DB::table('view_edit_keluar')->where('id', $id);
        if($id_gudang != 0){
            $d_barang->whereIn('id_gudang', explode(",", $id_gudang));
        }

        $data['status'] = $this->get_status($id, 0);

		$data['data'] = $d_data->first();
		$data['barang'] = $d_barang->get();
        $data['jenis'] = '1';
		return view('inventori.PersetujuankeluarAdd')->with('data', $data);
	}

	function verifikasimasuk($id){
		$id_gudang = $this->session['id_gudang'];

		$barangkeluar = DB::table('pro_barangmasuk AS pbm')->join('pro_barangkeluar AS pbk', 'pbm.id_barangkeluar', 'pbk.id_barangkeluar')->leftJoin('ms_rekanan AS mr', 'pbk.id_rekanan', 'mr.id_rekanan')->leftJoin('ms_pegawai AS mp', 'pbk.id_pegawai', 'mp.id_pegawai')->join('ms_proses AS mpr', 'pbk.id_proses', 'mpr.id_proses')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->leftjoin('ms_mesin as mmn','pbk.id_mesin','mmn.id_mesin')->select(DB::raw('pbm.*, pbk.tanggal_perkiraan, pbk.tanggal as tanggal_pengambilan, pbm.status, pbk.id_barangkeluar, mr.nama nama_rekanan, mp.nama_pegawai, mpr.nama_proses, mrk.nama nama_kelompok, mmn.nama nama_mesin'))->where('pbm.id_barangmasuk',$id)->first();
		$id_barangkeluar = $barangkeluar->id_barangkeluar;
		
        //$d_barang = barangkeluar_barang($id);
        $d_barang = DB::table('view_edit_keluar')->where('id', $id_barangkeluar);
		
		$d_setor = DB::table('pro_barangmasuk_barang AS pbb')->join('ms_barang AS mb', 'pbb.id_barang', 'mb.id')->join('ms_gudang AS mg', 'pbb.id_gudang', 'mg.id_gudang')->join('ms_kualitas AS mk','pbb.id_kualitas','mk.id_kualitas')->select(DB::raw('pbb.*, mb.kode_barang AS kode_barang, mb.nama_barang AS nama_barang, mg.nama_gudang, mk.nama_kualitas'))->where('pbb.id_barangmasuk', $id);

        
        $d_data = DB::table('view_edit_masuk')->where('id_barangmasuk', $id);

        // --- Kembali 
        // $data['d_kembali'] = DB::table('view_edit_kembali as v')->join('pro_kembali as p', 'v.id_kembali', 'p.id_kembali')->select('v.*')->where('id_barangkeluar', $id_barangkeluar)->where('p.status', '0')->where('p.jenis', '1');
        
        // if($data['d_kembali']->count() == 0 && $barangkeluar->status != 0){
        //     $data['d_kembali'] = DB::table('view_edit_kembali as v')->join('pro_kembali as p', 'v.id_kembali', 'p.id_kembali')->select('v.*')->where('id_barangmasuk', $id)->where('p.jenis', '1');
        // }
        // --- Kembali 

        if($id_gudang != 0){
            $gd = explode(",", $id_gudang);

            $d_data->whereIn('id_gudang', $gd);
        }

        $data['status'] = $this->get_status($id, 2);
		$data['data'] = $barangkeluar;
		$data['barang'] = $d_barang->get();
		$data['setor'] = $d_data->get();
        $data['jenis'] = '1';
		return view('inventori.PersetujuanmasukAdd')->with('data',$data);
	   //print_r($data['d_kembali']->get());
	}

	function simpankeluar(Request $request){
		$id_barangkeluar = $request->get('id_barangkeluar');
		$data['status'] = $request->get('status');
        $id_barang = $request->get('id');

		DB::table('pro_barangkeluar_barang')->whereIn('id', $id_barang)->update($data);

        $this->set_status_all($id_barangkeluar, 0);



		return redirect('persetujuankeluar');

	}

	function simpanmasuk(Request $request){
		$id_barangmasuk = $request->get('id_barangmasuk');
		$data['status'] =  $request->get('status');
        $id_barang = $request->get('id');

		DB::table('pro_barangmasuk_barang')->whereIn('id', $id_barang)->update($data);
        $this->set_status_all($id_barangmasuk, 2);
        
        // $barangkeluar = DB::table('pro_barangmasuk AS pbm')->where('pbm.id_barangmasuk',$id_barangmasuk)->first();
        // $id_barangkeluar = $barangkeluar->id_barangkeluar;

        // $update['status'] =  $request->get('status');
        // $update['id_barangmasuk'] = $id_barangmasuk;
        // $d_kembali = DB::table('pro_kembali')->where('id_barangkeluar', $id_barangkeluar)->where('jenis', '1')->update($update);

		return redirect('persetujuanmasuk');
	}

    function simpan_keluarmulti(Request $request){
        $id = $request->get('id');
        $jenis = $request->get('jenis');
        $id_gudang = $this->session['id_gudang'];

        $data = array();

        for($i = 0; $i < count($id); $i++){
            $data[$jenis[$i]][] = array($id[$i]);
        }

        if(array_key_exists('1', $data)){
            $d = DB::table('pro_barangkeluar_barang')->whereIn('id_barangkeluar', $data[1]);
            
            if($id_gudang != 0){
                $d->whereIn('id_gudang', explode(",", $id_gudang));
            }

            $d->update(array('status' => '1'));

            foreach($data[1] as $d => $d1){
                $this->set_status_all($d1[0], 0);

            }

            //print_r($data[1]);
        }

        if(array_key_exists('2', $data)){
            $d = DB::table('pro_barangkeluar_kemasan_barang')->whereIn('id_barangkeluar_kemasan', $data[2]);
            if($id_gudang != 0){
                $d->whereIn('id_gudang', explode(",", $id_gudang));
            }

            $d->update(array('status' => '1'));

            foreach($data[2] as $d => $d1){
                $this->set_status_all($d1[0], 1);
            }
        }

        return response()->json(array('status' => '1'));
    }

    function simpan_masukmulti(Request $request){
        $id = $request->get('id');
        $jenis = $request->get('jenis');
        $id_gudang = $this->session['id_gudang'];

        for($i = 0; $i < count($id); $i++){
            $data[$jenis[$i]][] = array($id[$i]);
        }

        //DB::table('pro_barangmasuk')->whereIn('id_barangmasuk', $id)->update(array('status' => '1'));

        if(array_key_exists('1', $data)){
            $d = DB::table('pro_barangmasuk_barang')->whereIn('id_barangmasuk', $data[1]);
            
            if($id_gudang != 0){
                $d->whereIn('id_gudang', explode(",", $id_gudang));
            }

            $d->update(array('status' => '1'));

            foreach($data[1] as $d => $d1){
                $this->set_status_all($d1[0], 2);
            }
            // foreach($data[1] as $d => $e){
            //     $barangkeluar = DB::table('pro_barangmasuk AS pbm')->where('pbm.id_barangmasuk', $e)->first();

            //     $id_barangkeluar = $barangkeluar->id_barangkeluar;

            //     $update['status'] =  '1';
            //     $update['id_barangmasuk'] = $barangkeluar->id_barangmasuk;
            //     $d_kembali = DB::table('pro_kembali')->where('id_barangkeluar', $id_barangkeluar)->where('jenis', '1')->update($update);
            // }
        }

        if(array_key_exists('2', $data)){
            $d = DB::table('pro_barangmasuk_kemasan_barang')->whereIn('id_barangmasuk_kemasan', $data[2]);

            if($id_gudang != 0){
                $d->whereIn('id_gudang', explode(",", $id_gudang));
            }

            $d->update(array('status' => '1'));

            foreach($data[2] as $d => $d1){
                $this->set_status_all($d1[0], 3);
            }

            // foreach($data[2] as $d => $e){
            //     $barangkeluar = DB::table('pro_barangmasuk_kemasan AS pbm')->where('pbm.id_barangmasuk_kemasan', $e)->first();

            //     $id_barangkeluar = $barangkeluar->id_barangkeluar_kemasan;

            //     $update['status'] =  '1';
            //     $update['id_barangmasuk'] = $barangkeluar->id_barangmasuk_kemasan;
            //     $d_kembali = DB::table('pro_kembali')->where('id_barangkeluar', $id_barangkeluar)->where('jenis', '2')->update($update);
            // }
        }

        return response()->json(array('status' => '1'));
    }

    function get_status($id, $jenis, $id_gudang = ''){
        if($id_gudang != ''){
            $id_gudang = 0;
        }else{
            $id_gudang = $this->session['id_gudang'];
        }

        if($jenis == 0){
            $d_data = DB::table('pro_barangkeluar_barang')->where('id_barangkeluar', $id);
        }else if($jenis == 1){
            $d_data = DB::table('pro_barangkeluar_kemasan_barang')->where('id_barangkeluar_kemasan', $id);
        }else if($jenis == 2){
            $d_data = DB::table('pro_barangmasuk_barang')->where('id_barangmasuk', $id);
        }else if($jenis == 3){
            $d_data = DB::table('pro_barangmasuk_kemasan_barang')->where('id_barangmasuk_kemasan', $id);
        }else if($jenis == 4){
            $d_data = DB::table('pro_kembali_barang')->where('id_kembali', $id);
        }

        if($id_gudang != 0){
            $d_data->whereIn('id_gudang', explode(",", $id_gudang));
        }

        if($d_data->count() > 0){
            $arr = $d_data->pluck('status')->toArray();
        }else{
            $arr = array();
        }

        return $arr;
    }

	function proses($id){
        if(in_array(0, $id)){
            $d = '<label class="label bg-warning-400">Belum Verifikasi</label>';
        }else{
            $d = '<label class="label bg-primary-400">Terverifikasi</label>';
        }
		return $d;
	}

    function text_status($id){
        if($id > 0){
            $d = '<label class="label bg-warning-400">Belum Verifikasi</label>';
        }else{
            $d = '<label class="label bg-primary-400">Terverifikasi</label>';
        }
        return $d;
    }

    function set_status_all($id, $jenis){
        $id_gudang = $this->session['id_gudang'];
        $status_1 = array('status' => 1);
        $status_0 = array('status' => 0);

        if($jenis == 0){ //pro_barangkeluar
            $d_data = DB::table("pro_barangkeluar_barang")->where("id_barangkeluar", $id)->groupby("id_barangkeluar")->select(DB::raw("GROUP_CONCAT(STATUS), LOCATE('0', GROUP_CONCAT(STATUS)) is_0"));
            if($id_gudang != 0){
                $d_data->whereIn('id_gudang', explode(",", $id_gudang));
            }
            $d_data = $d_data->get();
            $d_count = $d_data->count();
            if($d_count > 0){
                $d_keluar = DB::table("pro_barangkeluar")->where("id_barangkeluar", $id)->get();
                $d_kelcount = $d_keluar->count();
                if($d_kelcount > 0){
                    if($d_keluar->first()->status == 0 || $d_keluar->first()->status == 1){
                        if($d_data->first()->is_0 == 0){
                        DB::table('pro_barangkeluar')->where('id_barangkeluar', $id)->update($status_1);
                        }else{
                            DB::table('pro_barangkeluar')->where('id_barangkeluar', $id)->update($status_0);
                        }
                    }
                }
            }
        }else if($jenis == 1){ //pro_barangkeluar_kemasan
            //$d_data = DB::table('pro_barangkeluar_kemasan_barang')->where('id_barangkeluar_kemasan', $id);
            $d_status = $this->get_status($id, 1, 1);
            if(!in_array(0, $d_status)){
                DB::table('pro_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id)->update($status_1);            
            }else{
                DB::table('pro_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id)->update($status_0);
            }
        }else if($jenis == 2){
            $d_data = DB::table("pro_barangmasuk_barang")->where("id_barangmasuk", $id)->groupby("id_barangmasuk")->select(DB::raw("GROUP_CONCAT(STATUS), LOCATE('0', GROUP_CONCAT(STATUS)) is_0"));
            if($id_gudang != 0){
                $d_data->whereIn('id_gudang', explode(",", $id_gudang));
            }
            $d_data = $d_data->get();
            $d_count = $d_data->count();
            if($d_count > 0){
                $d_masuk = DB::table("pro_barangmasuk")->where("id_barangmasuk", $id)->get();
                $d_mascount = $d_masuk->count();
                if($d_mascount > 0){
                    if($d_masuk->first()->status == 0 || $d_masuk->first()->status == 1){
                        if($d_data->first()->is_0 == 0){
                            DB::table('pro_barangmasuk')->where('id_barangmasuk', $id)->update($status_1);
                        }else{
                            DB::table('pro_barangmasuk')->where('id_barangmasuk', $id)->update($status_0);
                        }
                    }
                }
            }
        }else if($jenis == 3){
            $d_status = $this->get_status($id, 3, 1);
            if(!in_array(0, $d_status)){
                DB::table('pro_barangmasuk_kemasan')->where('id_barangmasuk_kemasan', $id)->update($status_1);            
            }else{
                DB::table('pro_barangmasuk_kemasan')->where('id_barangmasuk_kemasan', $id)->update($status_0);
            }
        }else if($jenis == 4){
            // $d_status = $this->get_status($id, 4, 1);
            // if(!in_array(0, $d_status)){
            //     DB::table('pro_kembali')->where('id_kembali', $id)->update($status_1);            
            // }else{
            //     DB::table('pro_kembali')->where('id_kembali', $id)->update($status_0);
            // }
            $d_data = DB::table("pro_kembali_barang")->where("id_kembali", $id)->groupby("id_kembali")->select(DB::raw("GROUP_CONCAT(STATUS), LOCATE('0', GROUP_CONCAT(STATUS)) is_0"));
            if($id_gudang != 0){
                $d_data->whereIn('id_gudang', explode(",", $id_gudang));
            }
            $d_data = $d_data->get();
            $d_count = $d_data->count();
            if($d_count > 0){
                if($d_data->first()->is_0 == 0){
                    DB::table('pro_kembali')->where('id_kembali', $id)->update($status_1);
                }else{
                    DB::table('pro_kembali')->where('id_kembali', $id)->update($status_0);
                }
            }
        }

        
    }


    function verifikasikeluar_jenis($id, $jenis){
        if($jenis == '1'){
            return $this->verifikasikeluar($id);
        }else{
            return $this->verifikasikeluar_kemasan($id);
        }
    }

    function verifikasimasuk_jenis($id, $jenis){
        if($jenis == '1'){
            return $this->verifikasimasuk($id);
        }else if($jenis == '2'){
            return $this->verifikasimasuk_kemasan($id);
        }else if($jenis == '3'){
            return $this->verifikasimasuk_tenun($id);
        }else{
            return redirect('persetujuanmasuk');
        }
    }

    function verifikasikeluar_kemasan($id){
        $d_data = DB::table("pro_barangkeluar_kemasan as pbk")->leftjoin("ms_proses as mp", "pbk.id_proses", "mp.id_proses")->leftjoin("ms_rekanan as mr", "pbk.id_rekanan", "mr.id_rekanan")->leftjoin("ms_pegawai as mpg", "pbk.id_pegawai", "mpg.id_pegawai")->leftjoin("ms_kelompok as mk", "mr.id_kelompok", "mk.id_kelompok")->orderBy('pbk.status', 'asc')->orderBy('pbk.tanggal', 'desc')->select(DB::raw("pbk.*, mp.nama_proses, mr.nama nama_rekanan, mpg.nama_pegawai, mk.nama_kelompok, '-' nama_mesin"));

       

        $d_barang = DB::table('view_edit_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id);

        $id_gudang = $this->session['id_gudang'];

        $d_barang = DB::table('view_edit_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id);
        if($id_gudang != 0){
            $d_barang->whereIn('id_gudang', explode(",", $id_gudang));
        }

        $data['status'] = $this->get_status($id, 1);

        $data['data'] = $d_data->first();
        $data['barang'] = $d_barang->get();
        $data['jenis'] = '2';

    //  print_r($data);
        return view('inventori.PersetujuankeluarAdd')->with('data', $data);
    }

    function verifikasimasuk_kemasan($id){
        
        $barangkeluar = DB::table("pro_barangmasuk_kemasan as pbk")->join('pro_barangkeluar_kemasan as pbkel', 'pbk.id_barangkeluar_kemasan', 'pbkel.id_barangkeluar_kemasan')->leftjoin('ms_proses as mp', 'pbkel.id_proses', 'mp.id_proses')->leftjoin('ms_rekanan as mr', 'pbkel.id_rekanan', 'mr.id_rekanan')->leftjoin('ms_pegawai as mpg', 'pbkel.id_pegawai', 'mpg.id_pegawai')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->orderBy('pbk.status', 'asc')->orderBy('pbk.tanggal', 'desc')->select(DB::raw("pbk.*, mp.nama_proses, mr.nama nama_rekanan, mpg.nama_pegawai, pbkel.id_rekanan, pbkel.id_pegawai, pbkel.tanggal_perkiraan, pbkel.tanggal tanggal_pengambilan, mrk.nama nama_kelompok, '-' nama_mesin"))->where('pbk.id_barangmasuk_kemasan', $id)->first();

        $id_barangkeluar = $barangkeluar->id_barangkeluar_kemasan;
        
        $d_barang = DB::table('view_edit_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id_barangkeluar);
        

        
        $id_gudang = $this->session['id_gudang'];

        $d_data = DB::table('view_edit_barangmasuk_kemasan as v')->join('pro_barangmasuk_kemasan_barang as pb', 'v.id_barangmasuk_kb', 'pb.id_barangmasuk_kb')->where('pb.id_barangmasuk_kemasan', $id)->select(DB::raw('v.*, pb.status'));
        if($id_gudang != 0){
            $d_data->whereIn('v.id_gudang', explode(",", $id_gudang));
        }

        // --- Kembali 
        // $data['d_kembali'] = DB::table('view_edit_kembali as v')->join('pro_kembali as p', 'v.id_kembali', 'p.id_kembali')->select('v.*')->where('id_barangkeluar', $id_barangkeluar)->where('p.status', '0')->where('p.jenis', '2');
        // if($data['d_kembali']->count() == 0 && $barangkeluar->status != 0){
        //     $data['d_kembali'] = DB::table('view_edit_kembali as v')->join('pro_kembali as p', 'v.id_kembali', 'p.id_kembali')->select('v.*')->where('id_barangmasuk', $id)->where('p.jenis', '2');
        // }
        // --- Kembali 

        $data['status'] = $this->get_status($id, 3);
        $data['data'] = $barangkeluar;
        $data['barang'] = $d_barang->get();
        $data['setor'] = $d_data->get();
        $data['jenis'] = '2';

    

        return view('inventori.PersetujuanmasukAdd')->with('data',$data);
    }

    function simpankeluar_kemasan(Request $request){
        $id_barangkeluar_kemasan = $request->get('id_barangkeluar_kemasan');
        $data['status'] = $request->get('status');
        $id_barang = $request->get('id');

        DB::table('pro_barangkeluar_kemasan_barang')->whereIn('id_barangkeluar_kb', $id_barang)->update($data);
        $this->set_status_all($id_barangkeluar_kemasan, 1);

         return redirect('persetujuankeluar');

    }

    function simpanmasuk_kemasan(Request $request){
        $id_barangmasuk_kemasan = $request->get('id_barangmasuk_kemasan');
        $data['status'] = $request->get('status');
        $id_barang = $request->get('id');

        DB::table('pro_barangmasuk_kemasan_barang')->whereIn('id_barangmasuk_kb', $id_barang)->update($data);
        $this->set_status_all($id_barangmasuk_kemasan, 3);

        // $barangkeluar = DB::table('pro_barangmasuk_kemasan AS pbm')->where('pbm.id_barangmasuk_kemasan',$id_barangmasuk_kemasan)->first();
        // $id_barangkeluar_kemasan = $barangkeluar->id_barangkeluar_kemasan;

        // $update['status'] =  $request->get('status');
        // $update['id_barangmasuk'] = $id_barangmasuk_kemasan;
        // $d_kembali = DB::table('pro_kembali')->where('id_barangkeluar', $id_barangkeluar_kemasan)->where('jenis', '2')->update($update);

        return redirect('persetujuanmasuk');

    }

    function verifikasimasuk_tenun($id){
        $id_gudang = $this->session['id_gudang'];

        $barangkeluar = get_detailtenunAll()->where('pbm.id_barangmasuk', $id)->leftjoin('pro_barangmasuk as pbm', 'pb.id_barangkeluar', 'pbm.id_barangkeluar')->select(DB::raw('pb.id_barangkeluar, MIN(pb.tanggal) tanggal_pengambilan, MAX(pb.tanggal_perkiraan) tanggal_perkiraan, pb.id_rekanan, mr.nama nama_rekanan, mrk.nama nama_kelompok, pb.id_pegawai, mpi.nama_pegawai, pb.id_mesin, mp.nama nama_mesin, pb.id_proses, mpr.nama_proses, pb.id_workorder, pw.nomor no_workorder, pb.id_workorder_barang, pwb.no_workorder no_workorder_barang, pb.id_lokasi, mlp.nama_lokasi, pb.status_beem, pb.tanggal_beem, pb.status_workorder, pbm.id_barangmasuk, pbm.tanggal tanggal, pbm.status, pbm.noAuto, pbm.noAuto_ext'))->first();

        $id_barangkeluar = $barangkeluar->id_barangkeluar;

        $d_barangkeluar = DB::table('pro_barangkeluar')->where('id_barangkeluar', $id_barangkeluar)->first();
        $id_proses = $d_barangkeluar->id_proses;
        $id_workorder_barang = $d_barangkeluar->id_workorder_barang;
        $id_rekanan = $d_barangkeluar->id_rekanan;
        $id_pegawai = $d_barangkeluar->id_pegawai;
        $id_mesin = $d_barangkeluar->id_mesin;
        
        $d_barang = get_barangkeluar_byorderAll()->whereRaw('vk.id_workorder_barang = "'.$id_workorder_barang.'" AND pb.id_rekanan = "'.$id_rekanan.'" AND pb.id_pegawai = "'.$id_pegawai.'" AND pb.id_mesin = "'.$id_mesin.'"');
        

        $d_data = DB::table('view_edit_masuk')->where('id_barangmasuk', $id);

        // --- Kembali 
        // $data['d_kembali'] = DB::table('view_edit_kembali as v')->join('pro_kembali as p', 'v.id_kembali', 'p.id_kembali')->select('v.*')->where('id_barangkeluar', $id_barangkeluar)->where('p.status', '0')->where('p.jenis', '1');
        
        // if($data['d_kembali']->count() == 0 && $barangkeluar->status != 0){
        //     $data['d_kembali'] = DB::table('view_edit_kembali as v')->join('pro_kembali as p', 'v.id_kembali', 'p.id_kembali')->select('v.*')->where('id_barangmasuk', $id)->where('p.jenis', '1');
        // }
        // --- Kembali 

        if($id_gudang != 0){
            $gd = explode(",", $id_gudang);

            $d_data->whereIn('id_gudang', $gd);
        }
        $data['status'] = $this->get_status($id, 2);
        $data['data'] = $barangkeluar;
        $data['barang'] = $d_barang->get();
        $data['setor'] = $d_data->get();
        $data['jenis'] = '1';    

        return view('inventori.PersetujuanmasukAdd')->with('data',$data);
    }


    //Pengembalian produksi

    function terimakembali(){
        return view('inventori.PengembalianproduksiList');
    }

    function get_kembali(){
        $id_gudang = $this->session['id_gudang'];

        $kembali = DB::table('pro_kembali as pk')->join('pro_kembali_barang as pkb', 'pk.id_kembali', 'pkb.id_kembali')->leftjoin('pro_barangkeluar as pbk', 'pk.id_barangkeluar', 'pbk.id_barangkeluar')->leftjoin('ms_rekanan as mr', 'pbk.id_rekanan', 'mr.id_rekanan')->leftjoin('ms_pegawai as mp', 'pbk.id_pegawai', 'mp.id_pegawai')->leftjoin('ms_mesin as mm', 'pbk.id_mesin', 'mm.id_mesin')->leftjoin('pem_workorder_barang as pwb', 'pbk.id_workorder_barang', 'pwb.id_workorder_barang')->leftjoin('ms_proses as mpr', 'pbk.id_proses', 'mpr.id_proses')->select(DB::raw('pk.*, pbk.tanggal tanggal_keluar, mp.id_pegawai, mp.nama_pegawai, mr.id_rekanan, mr.nama nama_rekanan, mm.id_mesin, mm.nama nama_mesin, mpr.id_proses, mpr.nama_proses, pbk.noAuto, pbk.noAuto_ext, pbk.tanggal_perkiraan, CASE WHEN ISNULL(pwb.id_workorder_barang) THEN "" ELSE pwb.id_workorder_barang END AS id_workorder_barang, CASE WHEN ISNULL(pwb.no_workorder) THEN "" ELSE pwb.no_workorder END AS no_workorder_barang, pk.keterangan, LOCATE("0", GROUP_CONCAT(pkb.status)) AS is_0'))->groupby('pk.id_kembali')->where('pk.jenis', '1');
        
        $kembali_kemas = DB::table('pro_kembali as pk')->join('pro_barangkeluar_kemasan as pbk', 'pk.id_barangkeluar', 'pbk.id_barangkeluar_kemasan')->join('pro_barangkeluar_kemasan_barang as pbkb', 'pbk.id_barangkeluar_kemasan', 'pbkb.id_barangkeluar_kemasan')->leftjoin('ms_rekanan as mr', 'pbk.id_rekanan', 'mr.id_rekanan')->leftjoin('ms_pegawai as mp', 'pbk.id_pegawai', 'mp.id_pegawai')->join('ms_proses as mpr', 'pbk.id_proses', 'mpr.id_proses')->where('pk.jenis', '2')->groupby('pbk.id_barangkeluar_kemasan');

        if($id_gudang != 0){
            $kembali->whereIn('pkb.id_gudang', explode(",", $id_gudang));
            $kembali_kemas->whereIn('pbkb.id_gudang', explode(",", $id_gudang));
        }

        $kembali->havingRaw("LOCATE('0', GROUP_CONCAT(pkb.status)) != 0");


        if($kembali->count() > 0){
            foreach($kembali->get() as $k){
                
                $pekerja = '';
                $stat_pekerja = '';
                if($k->id_rekanan != '' || !is_null($k->id_rekanan)){
                    $pekerja = $k->nama_rekanan;
                    $stat_pekerja = 'Rekanan';
                }else if($k->id_pegawai != '' || !is_null($k->id_pegawai)){
                    $pekerja = $k->nama_pegawai;
                    $stat_pekerja = 'Pegawai';
                }else if($k->nama_mesin != '' || !is_null($k->nama_mesin)){
                    $pekerja = $k->nama_mesin;
                    $stat_pekerja = 'Mesin';
                }

                // if(in_array(0, $this->get_status($k->id_kembali, 4))){
                //     $status = 0;
                // }else{
                //     $status = 1;
                // }

                if($k->is_0 == 0){
                    $status = 0;
                }else{
                    $status = 1;
                }

                $arr[] = array('id' => $k->id_kembali,
                                'nomor' => $k->noAuto.$k->noAuto_ext,
                                'no_workorder' => $k->no_workorder_barang,
                                'tanggal' => tgl_full($k->tanggal, ''),
                                'nama_proses' => $k->nama_proses,
                                'pekerja' => $pekerja,
                                'stat_pekerja' => $stat_pekerja,
                                'status_id' => $k->status,
                                'status' => $this->text_status($k->is_0),
                                'rownum' => '1',
                                'posisi' => ($pekerja != '' && $stat_pekerja != '') ? $pekerja.' ('.$stat_pekerja.')':"",
                                'keterangan' => $k->keterangan,
                                'aksi' => '<a class="btn bg-info-400" type="button" href="'.url('terimakembali/verifikasikembali/').'/'.$k->id_kembali.'/1'.'"><i class="icon-checkbox-checked2"></i></a>',
                                'checkbox' => status_checkbox($status, $k->id_kembali, '1'));
            }
        }else{
            $arr = array();
        }

        if($kembali_kemas->count() > 0){
            foreach($kembali_kemas->get() as $k){
                $pekerja = '';
                $stat_pekerja = '';
                if($k->id_rekanan != '' || !is_null($k->id_rekanan)){
                    $pekerja = $k->nama_rekanan;
                    $stat_pekerja = 'Rekanan';
                }else if($k->id_pegawai != '' || !is_null($k->id_pegawai)){
                    $pekerja = $k->nama_pegawai;
                    $stat_pekerja = 'Pegawai';
                }else if($k->nama_mesin != '' || !is_null($k->nama_mesin)){
                    $pekerja = $k->nama_mesin;
                    $stat_pekerja = 'Mesin';
                }

                if(in_array(0, $this->get_status($k->id_kembali, 4))){
                    $status = 0;
                }else{
                    $status = 1;
                }

                $arr[] = array('id' => $k->id_kembali,
                                'nomor' => $k->noAuto.$k->noAuto_ext,
                                'no_workorder' => '',
                                'tanggal' => tgl_full($k->tanggal, ''),
                                'nama_proses' => $k->nama_proses,
                                'pekerja' => $pekerja,
                                'stat_pekerja' => $stat_pekerja,
                                'status_id' => $k->status,
                                'status' => $this->proses($this->get_status($k->id_kembali, 4)),
                                'rownum' => '1',
                                'posisi' => $pekerja.' ('.$stat_pekerja.')',
                                'keterangan' => $k->keterangan,
                                'aksi' => '<a class="btn bg-info-400" type="button" href="'.url('terimakembali/verifikasikembali/').'/'.$k->id_kembali.'/1'.'"><i class="icon-checkbox-checked2"></i></a>',
                                'checkbox' => status_checkbox($status, $k->id_kembali, '1'));
            }
        }

        $data = new Collection($arr);
        return Datatables::of($data)
                ->make(true);
    }


    function simpan_terimamulti(Request $request){
        $id = $request->get('id');
        $jenis = $request->get('jenis');
        $id_gudang = $this->session['id_gudang'];

        $data = array();

        for($i = 0; $i < count($id); $i++){
            $data[$jenis[$i]][] = array($id[$i]);
        }

        if(array_key_exists('1', $data)){
            $d = DB::table('pro_kembali_barang')->whereIn('id_kembali', $data[1]);
            
            if($id_gudang != 0){
                $d->whereIn('id_gudang', explode(",", $id_gudang));
            }

            $d->update(array('status' => '1'));

            foreach($data[1] as $d => $d1){
                $this->set_status_all($d1, 4);
                
            }
        }

        return response()->json(array('status' => '1'));
    }

    function verifikasikembali($id, $jenis){
        $id_gudang = $this->session['id_gudang'];
        $id_lokasi = $this->session['id_lokasi'];
        $where = '';
        

        $data['induk'] = DB::select("SELECT * FROM (".get_kembali().") A WHERE A.id_kembali = '$id'");
        $data['detail'] = DB::table('view_edit_kembali')->where('id_kembali', $id);

        if($id_gudang != 0){
            $data['detail']->whereIn('id_gudang', explode(",", $id_gudang));
        }

        $data['barang'] = $data['detail']->get();

        $data['status'] = $this->get_status($id, 4);
        return view('inventori.PersetujuankembaliAdd')->with('data', $data);
    //    print_r("SELECT * FROM (".get_kembali().") A WHERE A.id_kembali = '$id'");
    }

    function simpan_terima(Request $request){
        $id_kembali = $request->get('id_kembali');
        $data['status'] =  $request->get('status');
        $id_barang = $request->get('id');

        DB::table('pro_kembali_barang')->whereIn('id_kembali_barang', $id_barang)->update($data);
        $this->set_status_all($id_kembali, 4);

        return redirect('terimakembali');
    }
	
}