<?php

namespace App\Http\Controllers\produksi;

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 BarangmasukkemasanController extends Controller{

    private $session;
    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_user' => Session::get('kode_user'));
    }
    
	function index(){
		return view('produksi.BarangmasukkemasanList');
	}

	function get_data(){
		$d_data = DB::table('pro_barangkeluar_kemasan as pbk')->leftjoin('ms_pegawai as mp', 'pbk.id_pegawai', 'mp.id_pegawai')->leftjoin('ms_rekanan as mr', 'pbk.id_rekanan', 'mr.id_rekanan')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->leftjoin('ms_proses as mpro', 'pbk.id_proses', 'mpro.id_proses')->leftjoin('ms_lokasi_produksi as mlp', 'pbk.id_lokasi', 'mlp.id_lokasi')->where('pbk.status', '!=', '0')->select(DB::raw("pbk.*, mp.nama_pegawai, mr.nama nama_rekanan, mrk.nama nama_kelompok, mpro.nama_proses, mlp.nama_lokasi, pbk.noAuto, pbk.noAuto_ext"));

        if($this->session['id_proses'] != 0 && $this->session['id_lokasi'] != 0){
        $d_data = DB::table('pro_barangkeluar_kemasan as pbk')->leftjoin('ms_pegawai as mp', 'pbk.id_pegawai', 'mp.id_pegawai')->leftjoin('ms_rekanan as mr', 'pbk.id_rekanan', 'mr.id_rekanan')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->leftjoin('ms_proses as mpro', 'pbk.id_proses', 'mpro.id_proses')->leftjoin('ms_lokasi_produksi as mlp', 'pbk.id_lokasi', 'mlp.id_lokasi')->where('pbk.status', '!=', '0')->where('pbk.id_lokasi', $this->session['id_lokasi'])->whereIn('pbk.id_proses', explode(',', $this->session['id_proses']))->select(DB::raw("pbk.*, mp.nama_pegawai, mr.nama nama_rekanan, mrk.nama nama_kelompok, mpro.nama_proses, mlp.nama_lokasi, pbk.noAuto, pbk.noAuto_ext"));
        }
        $arr = array();

		if($d_data->count() > 0){
            foreach($d_data->get() as $d){
                $pekerja = '';
                $stat_pekerja = '';
                $jenis = 0;
                $status = 0;
                $nomor = '';
                $nomor_jenis = '';
                $noAuto = '';

                $id_produksi = 0;
                if($d->nama_rekanan != '' || !is_null($d->nama_rekanan)){
                    $pekerja = $d->nama_rekanan;
                    $stat_pekerja = 'Rekanan';
                }else{
                    $pekerja = $d->nama_pegawai;
                    $stat_pekerja = 'Pegawai';
                }

                if($d->status == '1'){
                    $status = $d->status;
                    $jenis = 0;
                    $id_produksi = $d->id_barangkeluar_kemasan;

                    $arr[] = array('id_barangkeluar_kemasan' => $d->id_barangkeluar_kemasan,
                                'tanggal' => date('d-m-Y', strtotime($d->tanggal)),
                                'tanggal_laporan' => '',
                                'nomor' => $noAuto,
                                'nomor_bon' => $d->noAuto.$d->noAuto_ext,
                                'nama_proses' => $d->nama_proses,
                                'pekerja' => $pekerja,
                                'stat_pekerja' => $stat_pekerja,
                                'proses' => $d->nama_proses,
                                'status_id' => $d->status,
                                'status' => $this->proses($jenis, $status),
                                'posisi' => $pekerja.' ('.$stat_pekerja.')',
                                'lokasi' => $d->nama_lokasi,
                                'aksi' => $this->aksi($jenis, $status, $id_produksi, $stat_pekerja));
                }else{
                    $d_masuk = DB::table('pro_barangmasuk_kemasan')->where('id_barangkeluar_kemasan', $d->id_barangkeluar_kemasan);

                    if($d_masuk->count() > 0){
                    	foreach($d_masuk->get() as $masuk) {
                    		if($d->status == '2'){
	                            $status = $masuk->status;
	                            $id_produksi = $masuk->id_barangmasuk_kemasan;
                                $noAuto = $masuk->noAuto.$masuk->noAuto_ext;
	                            $jenis = 1;
	                        }else if($d->status == '3'){

	                            $status = $masuk->status;
	                            $id_produksi = $masuk->id_barangmasuk_kemasan;
                                $noAuto = $masuk->noAuto.$masuk->noAuto_ext;
	                            $jenis = 1;
	                        }

	                        $arr[] = array('id_barangkeluar_kemasan' => $d->id_barangkeluar_kemasan,
                                'tanggal' => date('d-m-Y', strtotime($d->tanggal)),
                                'tanggal_laporan' => tgl_full($masuk->tanggal, ''),
                                'nomor' => $noAuto,
                                'nomor_bon' => $d->noAuto.$d->noAuto_ext,
                                'nama_proses' => $d->nama_proses,
                                'pekerja' => $pekerja,
                                'stat_pekerja' => $stat_pekerja,
                                'proses' => $d->nama_proses,
                                'status_id' => $d->status,
                                'status' => $this->proses($jenis, $status),
                                'posisi' => $pekerja.' ('.$stat_pekerja.')',
                                'lokasi' => $d->nama_lokasi,
                                'aksi' => $this->aksi($jenis, $status, $id_produksi, $stat_pekerja));
                    	}

                    	if($d->status == '3'){
                        $status = '3';
                        $jenis = 0;
                        $id_produksi = $d->id_barangkeluar_kemasan;
                        $noAuto = '';
                        $arr[] = array('id_barangkeluar_kemasan' => $d->id_barangkeluar_kemasan,
                                    'tanggal' => date('d-m-Y', strtotime($d->tanggal)),
                                    'tanggal_laporan' => '',
                                    'nomor' => $noAuto,
                                    'nomor_bon' => $d->noAuto.$d->noAuto_ext,
                                    'nama_proses' => $d->nama_proses,
                                    'pekerja' => $pekerja,
                                    'stat_pekerja' => $stat_pekerja,
                                    'proses' => $d->nama_proses,
                                    'status_id' => $d->status,
                                    'status' => $this->proses($jenis, $status),
                                    'posisi' => $pekerja.' ('.$stat_pekerja.')',
                                    'lokasi' => $d->nama_lokasi,
                                    'aksi' => $this->aksi($jenis, $status, $id_produksi, $stat_pekerja));
                    	}

                    }else{

                    }
                }
            }
        }else{
        	$arr = array();
        }

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

	function tambah($id){
        $id_gudang = $this->session['id_gudang'];
        $id_proses = $this->session['id_proses'];
        $id_lokasi = $this->session['id_lokasi'];
        
		$data['barang'] = DB::table('ms_barang AS mb')->join('ms_kategori_barang AS mkb', 'mb.id_kb', 'mkb.id_kb')->join('ms_kategori_barang AS mkb2', 'mkb.induk_kb', 'mkb2.id_kb')->join('ms_satuan AS ms', 'mb.id_satuan', 'ms.id_satuan')->select(DB::raw('mb.*, mkb2.id_kb AS id_kategori, mkb2.nama nama_kategori, mkb.id_kb id_sub, mkb.nama nama_sub, ms.id_satuan, ms.nama_satuan'))->orderBy('nama_kategori', 'asc')->orderBy('nama_sub', 'asc')->orderBy('kode_barang', 'asc')->orderBy('nama_barang', 'asc')->get();

		$data['gudang'] = DB::table('ms_gudang')->orderBy('nama_gudang', 'asc');
		$data['satuan'] = DB::table('ms_satuan')->orderBy('nama_satuan', 'asc')->get();

        if($id_lokasi != 0){
            $data['gudang']->where('id_lokasi', $id_lokasi);
        }

        $data['gudang'] = $data['gudang']->get();
		
		$data['kualitas_proses'] = DB::table('ms_kualitas')->orderBy('nama_kualitas', 'asc')->where('jenis', '2')->get();
		$data['kualitas_tenun'] = DB::table('ms_kualitas')->orderBy('nama_kualitas', 'asc')->where('jenis', '1')->get();
		$data['kualitas_periksa'] = DB::table('ms_kualitas')->orderBy('nama_kualitas', 'asc')->where('jenis', '3')->get();

		$data['barang_label'] = DB::table("ms_barang")->orderby('nama_barang', 'asc')->where('id_kb', '31')->get();
		$data['barang_cap'] = DB::table("ms_barang")->orderby('nama_barang', 'asc')->where('id_kb', '38')->get();
		$data['barang_awal'] = DB::table("ms_barang")->orderby('nama_barang', 'asc')->where('id_kb', '32')->where('nama_barang', 'like', '%plastik%')->get();
		$data['barang_satuan'] = DB::table("ms_barang")->orderby('nama_barang', 'asc')->where('id_kb', '30')->get();
		$data['barang_dos'] = DB::table("ms_barang")->orderby('nama_barang', 'asc')->where('id_kb', '36')->get();

		$data['warna'] = DB::table('ms_warnakomposisi')->orderBy('nama', 'asc')->get();
		$data['corak'] = DB::table('ms_motif as mm')->join('ms_motif_barang as mmb', 'mm.id_motif', 'mmb.id_motif')->select(DB::raw('mm.id_motif, mm.kode_motif, mm.nama_motif, mmb.id_motif_barang, mmb.motif_keterangan'))->orderBy('nama_motif', 'asc')->get();
		$data['motif'] = DB::table('ms_motif')->orderBy('nama_motif', 'asc')->get();
		
		$data['kemasan'] = DB::table('ms_barang as mb')->where('mb.id_kb', '32')->where('mb.nama_barang', 'like', '%plastik%')->orderBy('mb.nama_barang', 'asc')->get();

        $data['workorder'] = $this->get_spk(2);

		$data['barang_keluar'] = DB::table('view_edit_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id)->get();

        $data['wo_barang'] = DB::table('view_edit_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id)->groupBy('id_workorder_barang')->get();
		
		$data['detail'] = DB::table('pro_barangkeluar_kemasan as pbk')->leftjoin('ms_pegawai as mp', 'pbk.id_pegawai', 'mp.id_pegawai')->leftjoin('ms_rekanan as mr', 'pbk.id_rekanan', 'mr.id_rekanan')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->leftjoin('ms_proses as mpro', 'pbk.id_proses', 'mpro.id_proses')->leftjoin('ms_lokasi_produksi as mlp', 'pbk.id_lokasi', 'mlp.id_lokasi')->groupBy('pbk.id_barangkeluar_kemasan')->select(DB::raw("pbk.*, mp.nama_pegawai, mr.nama nama_rekanan, mrk.nama nama_kelompok, mpro.nama_proses, mlp.nama_lokasi"))->where('pbk.id_barangkeluar_kemasan', $id)->first();
		$data['d_kemasan'] = $this->data();
        $data['sisa'] = $this->get_sisa($id);

		return view('produksi.BarangmasukkemasanTambah')->with('data', $data);

    //    print_r($data['detail']);

	
	}

	function edit($id){
        $id_lokasi = $this->session['id_lokasi'];

        
		$data['barang'] = DB::table('ms_barang AS mb')->join('ms_kategori_barang AS mkb', 'mb.id_kb', 'mkb.id_kb')->join('ms_kategori_barang AS mkb2', 'mkb.induk_kb', 'mkb2.id_kb')->join('ms_satuan AS ms', 'mb.id_satuan', 'ms.id_satuan')->select(DB::raw('mb.*, mkb2.id_kb AS id_kategori, mkb2.nama nama_kategori, mkb.id_kb id_sub, mkb.nama nama_sub, ms.id_satuan, ms.nama_satuan'))->orderBy('nama_kategori', 'asc')->orderBy('nama_sub', 'asc')->orderBy('kode_barang', 'asc')->orderBy('nama_barang', 'asc')->get();

		$data['gudang'] = DB::table('ms_gudang')->orderBy('nama_gudang', 'asc');
		$data['satuan'] = DB::table('ms_satuan')->orderBy('nama_satuan', 'asc')->get();

        if($id_lokasi != 0){
            $data['gudang']->where('id_lokasi', $id_lokasi);
        }

        $data['gudang'] = $data['gudang']->get();
		
		$data['kualitas_proses'] = DB::table('ms_kualitas')->orderBy('nama_kualitas', 'asc')->where('jenis', '2')->get();
		$data['kualitas_tenun'] = DB::table('ms_kualitas')->orderBy('nama_kualitas', 'asc')->where('jenis', '1')->get();
		$data['kualitas_periksa'] = DB::table('ms_kualitas')->orderBy('nama_kualitas', 'asc')->where('jenis', '3')->get();

		$data['barang_label'] = DB::table("ms_barang")->orderby('nama_barang', 'asc')->where('id_kb', '31')->get();
		$data['barang_cap'] = DB::table("ms_barang")->orderby('nama_barang', 'asc')->where('id_kb', '38')->get();
		$data['barang_awal'] = DB::table("ms_barang")->orderby('nama_barang', 'asc')->where('id_kb', '32')->where('nama_barang', 'like', '%plastik%')->get();
		$data['barang_satuan'] = DB::table("ms_barang")->orderby('nama_barang', 'asc')->where('id_kb', '30')->get();
		$data['barang_dos'] = DB::table("ms_barang")->orderby('nama_barang', 'asc')->where('id_kb', '36')->get();

		$data['warna'] = DB::table('ms_warnakomposisi')->orderBy('nama', 'asc')->get();
		$data['corak'] = DB::table('ms_motif as mm')->join('ms_motif_barang as mmb', 'mm.id_motif', 'mmb.id_motif')->select(DB::raw('mm.id_motif, mm.kode_motif, mm.nama_motif, mmb.id_motif_barang, mmb.motif_keterangan'))->orderBy('nama_motif', 'asc')->get();
		$data['motif'] = DB::table('ms_motif')->orderBy('nama_motif', 'asc')->get();
		
		$data['kemasan'] = DB::table('ms_barang as mb')->where('mb.id_kb', '32')->where('mb.nama_barang', 'like', '%plastik%')->orderBy('mb.nama_barang', 'asc')->get();

        $data['workorder'] = $this->get_spk(2);

		$id_keluar = DB::table('pro_barangmasuk_kemasan')->where('id_barangmasuk_kemasan', $id)->first();

		$data['barang_keluar'] = DB::table('view_edit_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id_keluar->id_barangkeluar_kemasan)->get();

        $data['wo_barang'] = DB::table('view_edit_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id_keluar->id_barangkeluar_kemasan)->groupBy('id_workorder_barang')->get();
		
		$data['detail'] = DB::table('pro_barangkeluar_kemasan as pbk')->leftjoin('ms_pegawai as mp', 'pbk.id_pegawai', 'mp.id_pegawai')->leftjoin('ms_rekanan as mr', 'pbk.id_rekanan', 'mr.id_rekanan')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->leftjoin('ms_proses as mpro', 'pbk.id_proses', 'mpro.id_proses')->leftjoin('ms_lokasi_produksi as mlp', 'pbk.id_lokasi', 'mlp.id_lokasi')->groupBy('pbk.id_barangkeluar_kemasan')->select(DB::raw("pbk.*, mp.nama_pegawai, mr.nama nama_rekanan, mrk.nama nama_kelompok, mpro.nama_proses, mlp.nama_lokasi"))->where('pbk.id_barangkeluar_kemasan', $id_keluar->id_barangkeluar_kemasan)->first();

		$d_kemasan = DB::table('pro_barangmasuk_kemasan')->where('id_barangmasuk_kemasan', $id)->first();
		$data['d_kemasan'] = $this->data($d_kemasan);
        $data['sisa'] = $this->get_sisa($id_keluar->id_barangkeluar_kemasan);
		return view('produksi.BarangmasukkemasanTambah')->with('data', $data);

		
	}

    function detail($id){
        $id_keluar = DB::table('pro_barangmasuk_kemasan')->where('id_barangmasuk_kemasan', $id)->first();

        $data['barang_keluar'] = DB::table('view_edit_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id_keluar->id_barangkeluar_kemasan)->get();
        
        $data['detail'] = DB::table('pro_barangkeluar_kemasan as pbk')->leftjoin('ms_pegawai as mp', 'pbk.id_pegawai', 'mp.id_pegawai')->leftjoin('ms_rekanan as mr', 'pbk.id_rekanan', 'mr.id_rekanan')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->leftjoin('ms_proses as mpro', 'pbk.id_proses', 'mpro.id_proses')->leftjoin('ms_lokasi_produksi as mlp', 'pbk.id_lokasi', 'mlp.id_lokasi')->groupBy('pbk.id_barangkeluar_kemasan')->select(DB::raw("pbk.*, mp.nama_pegawai, mr.nama nama_rekanan, mrk.nama nama_kelompok, mpro.nama_proses, mlp.nama_lokasi"))->where('pbk.id_barangkeluar_kemasan', $id_keluar->id_barangkeluar_kemasan)->first();

        $d_kemasan = DB::table('pro_barangmasuk_kemasan')->where('id_barangmasuk_kemasan', $id)->first();
        $data['d_kemasan'] = $this->data($d_kemasan);

        return view('produksi.BarangmasukkemasanDetail')->with('data', $data);

        
    }


	function get_proses(Request $request){
		$id = $request->get('id');

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

        $query = "SELECT B.*
                FROM (
                ".base_stok_masuk_multi()."
                ) B WHERE B.id_barangkeluar_kemasan = $id AND B.stok > 0 ORDER BY B.id_barangkeluar_kemasan";

            $d_data = get_rawquery($query);

		return response()->json($d_data);
	}

	function get_edit(Request $request){
		$id = $request->get('id');

		$d_data = DB::table('view_edit_barangmasuk_kemasan')->where('id_barangmasuk_kemasan', $id)->get();

		return response()->json($d_data);
	}


	function simpan(Request $request){
		$id_barangmasuk_kemasan = $request->get('id_barangmasuk');
		$data['id_barangkeluar_kemasan'] = $request->get('id_barangkeluar');
		$data['tanggal'] = tgl_full($request->get('tanggal'), '2');
        $data['tanggal_full'] = tgl_full($request->get('tanggal'), '5');
        // $data['noAuto'] = $request->get('nomor');
        // $data['noAuto_ext'] = $request->get('nomor_ext');

        $data = $data+$this->validasi_nomor($id_barangmasuk_kemasan, $request->get('nomor'), $request->get('nomor_ext'), $request->get('id_lokasi'));

        $data['id_user'] = $this->session['kode_user'];

		$status_proses = $request->get('status_proses');
        if($status_proses == 1){
            $update['status'] = '2';
        }else{
            $update['status'] = '3';
        }

        $table_id = $request->get('tabel_idtable');
        $table_barang = $request->get('tabel_idbarang');
        $table_gudang = $request->get('tabel_idgudang');
        $table_upah = $request->get('tabel_idupah');
        $table_jumlah = $request->get('tabel_jumlah');
        $table_satuan = $request->get('tabel_idsatuan');
        $table_kualitas = $request->get('tabel_idkualitas');
        $table_dudul = $request->get('tabel_iddudul');
        $table_label = $request->get('tabel_idlabel');
        $table_kemasan = $request->get('tabel_idkemasan');
        $table_kemasansatuan = $request->get('tabel_idkemasansatuan');
        $table_kemasansepuluh = $request->get('tabel_idkemasansepuluh');
        $table_kemasanseratus = $request->get('tabel_idkemasanseratus');
        $table_cap = $request->get('tabel_idcap');
        $table_kualitasproses = $request->get('tabel_idkualitasproses');
        $table_kualitasperiksa = $request->get('tabel_idkualitasperiksa');
        $table_corak = $request->get('tabel_idcorak');
		$table_kualitas = $request->get('tabel_idkualitas');
		$table_motif = $request->get('tabel_idmotif');
		$table_komposisi = $request->get('tabel_idkomposisi');
		$table_corak = $request->get('tabel_idcorak');
        $table_workorder = $request->get('tabel_idworkorder');
        $table_idperubahansatuan = $request->get('tabel_idperubahansatuan');
        $table_rencanajumlah = $request->get('tabel_rencanajumlah');
        $table_keterangan = $request->get('tabel_keterangan');

        if($id_barangmasuk_kemasan == ''){
        	$data['status'] = "0";
        	$id = DB::table("pro_barangmasuk_kemasan")->insertGetId($data);

        	for($i=0; $i < count($table_barang); $i++){                
                $barang['id_barangmasuk_kemasan']=$id;
                $barang['id_barang']=$table_barang[$i];
                $barang['id_gudang']=$table_gudang[$i];
                $barang['jumlah'] = $table_jumlah[$i];

                $barang['id_kualitas'] = $table_kualitas[$i];
                $barang['id_motif'] = $table_motif[$i];                
                // $barang['total_upah'] = $table_total[$i];
                $barang['id_komposisi'] = $table_komposisi[$i];
                $barang['id_satuan'] = $table_satuan[$i];
                $barang['id_motif_barang'] = $table_corak[$i];
                $barang['id_kualitas'] = $table_kualitas[$i];
                $barang['id_kualitas_dudul'] = $table_dudul[$i];
                $barang['id_label'] = $table_label[$i];
                $barang['id_kemasan'] = $table_kemasan[$i];
                $barang['id_kemasansatuan'] = $table_kemasansatuan[$i];
                $barang['id_kemasansepuluh'] = $table_kemasansepuluh[$i];
                $barang['id_kemasanseratus'] = $table_kemasanseratus[$i];
                $barang['id_cap'] = $table_cap[$i];
                $barang['id_kualitas_proses'] = $table_kualitasproses[$i];
                $barang['id_kualitas_periksa'] = $table_kualitasperiksa[$i];

                $barang['id_workorder_barang'] = $table_workorder[$i];
                $barang['id_perubahansatuan'] = $table_idperubahansatuan[$i];
                $barang['jumlah_rencana'] = $table_rencanajumlah[$i];
                $barang['keterangan'] = $table_keterangan[$i];

                $wo_barang = $table_workorder[$i];
                if($wo_barang != ''){
                    $wo = DB::table('pem_workorder_barang')->where('id_workorder_barang', $wo_barang)->first()->id_workorder;
                    $barang['id_workorder'] = $wo;
                }else{
                    $barang['id_workorder'] = '';
                }

               DB::table('pro_barangmasuk_kemasan_barang')->insert($barang); 
            }

            
           DB::table('pro_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $request->get('id_barangkeluar'))->update($update);

        }else{
            DB::table('pro_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $request->get('id_barangkeluar'))->update($update);
            DB::table('pro_barangmasuk_kemasan')->where('id_barangmasuk_kemasan', $id_barangmasuk_kemasan)->update($data);

            $id_del = array();
            $id_store = array();
            $d_barang = DB::table('pro_barangmasuk_kemasan_barang')->where('id_barangmasuk_kemasan', $id_barangmasuk_kemasan);
            foreach($d_barang->get() as $d){
                $id_store[] = $d->id_barangmasuk_kb;
            }

            foreach($id_store as $d){
                if(count($table_id) > 0){
                    if(!in_array($d, $table_id)){
                        $id_del[] = $d;
                    }
                }else{
                    $id_del[] = $d;
                }
            }

            for($i=0; $i < count($table_barang); $i++){
                
                $barang['id_barangmasuk_kemasan']=$id_barangmasuk_kemasan;
                $barang['id_barang']=$table_barang[$i];
                $barang['id_gudang']=$table_gudang[$i];
                $barang['jumlah'] = $table_jumlah[$i];

                $barang['id_kualitas'] = $table_kualitas[$i];
                $barang['id_motif'] = $table_motif[$i];                
                // $barang['total_upah'] = $table_total[$i];
                $barang['id_komposisi'] = $table_komposisi[$i];
                $barang['id_satuan'] = $table_satuan[$i];
                $barang['id_motif_barang'] = $table_corak[$i];
                $barang['id_kualitas'] = $table_kualitas[$i];
                $barang['id_kualitas_dudul'] = $table_dudul[$i];
                $barang['id_label'] = $table_label[$i];
                $barang['id_kemasan'] = $table_kemasan[$i];
                $barang['id_kemasansatuan'] = $table_kemasansatuan[$i];
                $barang['id_kemasansepuluh'] = $table_kemasansepuluh[$i];
                $barang['id_kemasanseratus'] = $table_kemasanseratus[$i];
                $barang['id_cap'] = $table_cap[$i];
                $barang['id_kualitas_proses'] = $table_kualitasproses[$i];
                $barang['id_kualitas_periksa'] = $table_kualitasperiksa[$i];
                $barang['id_workorder_barang'] = $table_workorder[$i];
                $barang['id_perubahansatuan'] = $table_idperubahansatuan[$i];
                $barang['jumlah_rencana'] = $table_rencanajumlah[$i];
                $barang['keterangan'] = $table_keterangan[$i];

                $wo_barang = $table_workorder[$i];
                if($wo_barang != ''){
                    $wo = DB::table('pem_workorder_barang')->where('id_workorder_barang', $wo_barang)->first()->id_workorder;
                    $barang['id_workorder'] = $wo;
                }else{
                    $barang['id_workorder'] = '';
                }

               if($table_id[$i] == ''){
            //    echo 'tambah';
                DB::table('pro_barangmasuk_kemasan_barang')->insert($barang);
               }else if($table_id[$i] != ''){
            //    echo 'edit';
                DB::table('pro_barangmasuk_kemasan_barang')->where('id_barangmasuk_kb', $table_id[$i])->update($barang); 
               }
            }

            if(count($id_del) > 0){
               DB::table('pro_barangmasuk_kemasan_barang')->whereIn('id_barangmasuk_kb', $id_del)->delete();
            }
        }

        return redirect('barangmasuk_kemasan');
	}

    function cetak(Request $request, $id){
        require('fpdf181/Mc_table.php');

        $d_masuk = DB::table('pro_barangmasuk_kemasan as pbk')->join('pro_barangkeluar_kemasan as pbkk', 'pbkk.id_barangkeluar_kemasan', 'pbk.id_barangkeluar_kemasan')->leftjoin('ms_pegawai as mp', 'pbkk.id_pegawai', 'mp.id_pegawai')->leftjoin('ms_rekanan as mr', 'pbkk.id_rekanan', 'mr.id_rekanan')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->join('ms_proses as mpr', 'pbkk.id_proses', 'mpr.id_proses')->join('ms_lokasi_produksi as mlp', 'pbkk.id_lokasi', 'mlp.id_lokasi')->select(DB::raw("pbkk.id_barangkeluar_kemasan, pbk.id_barangmasuk_kemasan, pbkk.tanggal, pbkk.tanggal_perkiraan, pbk.tanggal tanggal_selesai, pbkk.id_pegawai, mp.nama_pegawai, pbkk.id_rekanan, mr.nama nama_rekanan, mrk.nama nama_kelompok, mpr.id_proses, mpr.nama_proses, mlp.id_lokasi, mlp.nama_lokasi, pbk.noAuto, pbk.noAuto_ext"))->where('pbk.id_barangmasuk_kemasan', $id);
        
        $req = $request->all();
    
        $data['data'] = $d_masuk->first();
        $data['keluar'] = DB::table('view_edit_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $d_masuk->first()->id_barangkeluar_kemasan)->get();
        $data['detail'] = DB::table('view_edit_barangmasuk_kemasan')->where('id_barangmasuk_kemasan', $id)->get();

        $id_keluar = $d_masuk->first()->id_barangkeluar_kemasan;

        $query = "SELECT B.*
                FROM (
                ".base_stok_masuk_multi()."
                ) B WHERE B.id_barangkeluar_kemasan = $id_keluar AND B.stok > 0 ORDER BY B.id_barangkeluar_kemasan";

        $data['sisa'] = DB::select($query);

        if(isset($req['kertas'])){
            if($req['kertas'] == 0){
                $view = "BarangmasukmultiCetakKecil";
            }else{
                $view = "BarangmasukmultiCetak";
            }
        }else{
                $view = "BarangmasukmultiCetak";
        }

        $html = view('produksi.cetak.'.$view.'')->with('data',$data);

        return response($html)->header('Content-Type', 'application/pdf');
    }

	function data($data = array()){
		if(count($data) > 0){
			$return['id_barangmasuk_kemasan'] = $data->id_barangmasuk_kemasan;
			$return['id_barangkeluar_kemasan'] = $data->id_barangkeluar_kemasan;
			$return['tanggal'] = tgl_full($data->tanggal, '');
            $return['nomor'] = $data->noAuto.$data->noAuto_ext;
		}else{
			$return['id_barangmasuk_kemasan'] = '';
			$return['id_barangkeluar_kemasan'] = '';
			$return['tanggal'] = '';
            $return['nomor'] = '';
		}

		return $return;
	}

	// ---------- Fungsi Lainnya

	function proses($jenis, $status){
		switch ($jenis) {
			case '0':
                if($status == 1){
                    $d = '<label class="label bg-warning-400">Belum Diterima</label>';
                }else if($status == 2){
                    $d = '<label class="label bg-warning-400">Belum Diterima</label>';
                }else if($status == 3){
                    $d = '<label class="label bg-warning-600">Belum Diterima Sebagian</label>';
                }
				break;
			case '1':
				if($status == 0){
                    $d = '<label class="label bg-warning-400">Belum Diterima Gudang</label>';
                }else{
                    $d = '<label class="label bg-success-400">Selesai</label>';
                }
				break;
			default:
				$d = '<label class="label bg-info-400">Belum Verifikasi</label>';
				break;
		}

		return $d;
	}


	function aksi($jenis, $status,$id, $stat_pekerja){

        $d_data = DB::table('pro_barangmasuk_kemasan_barang')->where('id_barangmasuk_kemasan', $id)->pluck('status')->toArray();
		switch ($jenis) {
			case '0':
				if($status == 1){
                    $d = '<a class="btn bg-info-400" type="button" href="'.url('barangmasuk_kemasan/tambah/').'/'.$id.'"><i class="icon-checkbox-checked2"></i></a>';
                }else if($status == 2){
                    $d = '<a class="btn bg-info-400" type="button" href="'.url('barangmasuk_kemasan/tambah/').'/'.$id.'"><i class="icon-checkbox-checked2"></i></a>';
                }else if($status == 3){
                    $d = '<a class="btn bg-info-400" type="button" href="'.url('barangmasuk_kemasan/tambah/').'/'.$id.'"><i class="icon-checkbox-checked2"></i></a>';
                }
                
				break;
			case '1':
                $btn_upah = "";
                $cek_upah = DB::table("pro_upahproduksi_kemasan")->where([["id_barangmasuk_kemasan", $id]]);
                    if($cek_upah->count() == 0){
                        if (strtolower($stat_pekerja) == "pegawai") {
                            $btn_upah ='<a class="btn btn-success" type="button" href="'.url('upahproduksi_kemasan/tambah').'/'.$id.'"><i class="icon-cash2"></i></a>';
                        } else {
                        //    $btn_upah ='<a class="btn btn-success" type="button" href="'.url('upahproduksi_kemasan/rekanan/tambah').'/'.$id.'/'.$id_tabel.'"><i class="icon-cash2"></i></a>'; Error di variabel id_tabel
                        }
                }
                if($status == 0){
                    if(in_array(1, $d_data)){
                        $d = '<div class="btn-group"> <a class="btn bg-info-400" type="button" href="'.url('barangmasuk_kemasan/detail/').'/'.$id.'"><i class="icon-notification2"></i></a> <a class="btn bg-warning-400" type="button" href="'.url('barangmasuk_kemasan/edit/').'/'.$id.'"><i class="icon-pencil3"></i></a> ';
                    }else{
                        $d = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangmasuk_kemasan/detail/').'/'.$id.'"><i class="icon-notification2"></i></a> <a class="btn bg-warning-400" type="button" href="'.url('barangmasuk_kemasan/edit/').'/'.$id.'"><i class="icon-pencil3"></i></a> ';    
                    }

                    $d .= $this->get_hapus($id);

                    $d .= '<button type="button" class="btn bg-success-400 dropdown-toggle" data-toggle="dropdown"><i class="icon-printer"></i><span class="caret"></span></button><ul class="dropdown-menu dropdown-menu-right"> <li><a href="'.url('barangmasuk_kemasan/cetak/'.$id).'?kertas=1'.'" target="_blank"><i class="icon-printer4"></i> Cetak Sedang</a></li> <li><a href="'.url('barangmasuk_kemasan/cetak/'.$id).'?kertas=0'.'" target="_blank"><i class="icon-printer4"></i> Cetak Kecil</a></li></ul>';
                    $d .= "</div>";
                    
                }else if($status == 1){
                    $d = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangmasuk_kemasan/detail/').'/'.$id.'"><i class="icon-notification2"></i></a> <a class="btn bg-warning-400" type="button" href="'.url('barangmasuk_kemasan/edit/').'/'.$id.'"><i class="icon-pencil3"></i></a> '.$this->get_hapus($id).' '.$btn_upah.' ';

                    

                    $d .= '<button type="button" class="btn bg-success-400 dropdown-toggle" data-toggle="dropdown"><i class="icon-printer"></i><span class="caret"></span></button><ul class="dropdown-menu dropdown-menu-right"> <li><a href="'.url('barangmasuk_kemasan/cetak/'.$id).'?kertas=1'.'" target="_blank"><i class="icon-printer4"></i> Cetak Sedang</a></li> <li><a href="'.url('barangmasuk_kemasan/cetak/'.$id).'?kertas=0'.'" target="_blank"><i class="icon-printer4"></i> Cetak Kecil</a></li></ul>';
                    $d .= "</div>";
                }
				break;
			default:
				$d = '';
				break;
		}

		return $d;
	}

    function get_spk($jenis = ''){
        $arr = array();
        $d_list;

        if($jenis == '1'){
            $d_data = DB::table('pem_workorder as pw')->join('pro_barangkeluar as pb', 'pw.id_workorder', 'pb.id_workorder')->where('pb.status_workorder', '1')->groupBy('pw.id_workorder')->select(DB::raw('pw.id_workorder'));
            foreach ($d_data->get() as $d) {
                $arr[] = $d->id_workorder;
            }

            $d_list = DB::table('pem_workorder as pw')->whereNotIn('pw.id_workorder', $arr);
        }else{
            $d_order = DB::table('pro_barangkeluar as pb')->join('pem_workorder_barang as pwk', 'pb.id_workorder_barang', 'pwk.id_workorder_barang')->where('pb.status_beem', '1')->groupBy('pwk.id_workorder_barang')->select(DB::raw('pwk.id_workorder_barang'));
             foreach ($d_order->get() as $d) {
                $arr[] = $d->id_workorder_barang;
            }

            $d_list = DB::table('pem_workorder_barang as pwb')->whereNotIn('pwb.id_workorder_barang', $arr)->join('pem_workorder as pw', 'pw.id_workorder', 'pwb.id_workorder')->where('pw.id_lokasi', '1');
        }

        return $d_list->get();        
    }

    function get_nomor(Request $request){
        $data = $request->all();
        $arr = array();
        $id_lokasi = $data['id_lokasi'];

        if($data['id'] != ''){
            $d_data = DB::table('pro_barangmasuk_kemasan')->where('id_barangmasuk_kemasan', $data['id']);
            if($id_lokasi != ''){
                $d_data->join('pro_barangkeluar_kemasan as pkk', 'pro_barangmasuk_kemasan.id_barangkeluar_kemasan', 'pkk.id_barangkeluar_kemasan')->select(DB::raw("pro_barangmasuk_kemasan.noAuto, pro_barangmasuk_kemasan.noAuto_ext, pkk.id_lokasi"));
                $d_data->where('id_lokasi', $id_lokasi);
            }
            $d_data = $d_data->first();
            $arr = array('nomor' => $d_data->noAuto,
                         'faktur' => $d_data->noAuto_ext);
        }else{
            $tgl = explode('-', $data['tgl']);
            $no = 1;


            $d_kemasan = DB::table("pro_barangmasuk_kemasan")->select(DB::raw("noAuto, noAuto_ext"));
            if($id_lokasi != ''){
                $d_kemasan->join('pro_barangkeluar_kemasan as pkk', 'pro_barangmasuk_kemasan.id_barangkeluar_kemasan', 'pkk.id_barangkeluar_kemasan')->select(DB::raw("pro_barangmasuk_kemasan.noAuto, pro_barangmasuk_kemasan.noAuto_ext, pkk.id_lokasi"));
                $d_kemasan->where('id_lokasi', $id_lokasi);
            }
            $d_data = DB::table('pro_barangmasuk')->unionAll($d_kemasan)->select(DB::raw("noAuto, noAuto_ext"));
            if($id_lokasi != ''){
                $d_data->join('pro_barangkeluar as pb', 'pro_barangmasuk.id_barangkeluar', 'pb.id_barangkeluar')->select(DB::raw("pro_barangmasuk.noAuto, pro_barangmasuk.noAuto_ext, pb.id_lokasi"));
                $d_data->where('id_lokasi', $id_lokasi);
            }

            $d_data = $d_data->get()->max('noAuto_ext');

            if($d_data == 0 || $d_data == ''){
                $no = 1;
            }else{
                $no = $d_data + 1;
            }

            $arr = array('nomor' => "LH.",
                        'faktur' => $no);
        }

        return response()->json($arr);
    }

    function validasi_nomor($id, $noAuto, $noAuto_ext, $id_lokasi = ''){
        $arr = array();

        if($noAuto != '' && $noAuto_ext != ''){
            
            if($id > 0){
                $d_data = DB::table('pro_barangmasuk_kemasan')->where('id_barangmasuk_kemasan', $id);
                if($id_lokasi != ''){
                    $d_data->join('pro_barangkeluar_kemasan as pkk', 'pro_barangmasuk_kemasan.id_barangkeluar_kemasan', 'pkk.id_barangkeluar_kemasan')->select(DB::raw("pro_barangmasuk_kemasan.noAuto, pro_barangmasuk_kemasan.noAuto_ext, pkk.id_lokasi"));
                    $d_data->where('id_lokasi', $id_lokasi);
                }
                if($d_data->count() > 0){
                    if($d_data->first()->noAuto == $noAuto && $d_data->first()->noAuto_ext == $noAuto_ext){
                        $arr = array('noAuto' => $noAuto,
                            'noAuto_ext' => $noAuto_ext); 
                    }else{
                        DB::table('pro_barangmasuk')->where('id_barangmasuk', $id)->update(['noAuto' => '', 'noAuto_ext' => '']);
                        return $this->validasi_nomor(0, $noAuto, $noAuto_ext);
                    }
                }else{
                    $arr = array('noAuto' => $noAuto,
                            'noAuto_ext' => $noAuto_ext); 
                }

            }else{
                $no = 1;
                $where = '';
            
                $d_data[] = "SELECT pm.noAuto, pm.noAuto_ext, pb.id_lokasi FROM pro_barangmasuk as pm JOIN pro_barangkeluar as pb ON pm.id_barangkeluar = pb.id_barangkeluar";
                $d_data[] = "SELECT pbm.noAuto, pbm.noAuto_ext, pkk.id_lokasi FROM pro_barangmasuk_kemasan as pbm JOIN pro_barangkeluar_kemasan as pkk ON pbm.id_barangkeluar_kemasan = pkk.id_barangkeluar_kemasan";

                $union = implode(" UNION ALL ", $d_data);

                if($id_lokasi != ''){
                    $where = " WHERE u.id_lokasi = '".$id_lokasi."' ";
                }
                

                $test = DB::select("SELECT *, max(CAST(u.noAuto_ext as signed)) as nomax FROM ($union) u $where");

                if(count($test) > 0){
                    $nomor = $test[0]->nomax;


                    if($nomor == 0 || $nomor == ''){
                        $no = 1;
                    }else{
                        $no = $nomor + 1;
                    }

                    $arr = array('noAuto' => $noAuto,
                                    'noAuto_ext' => $no);                
                }else{
                    $arr = array('noAuto' => $noAuto,
                                'noAuto_ext' => $noAuto_ext);

                }

            }
        }

        return $arr;
    }

    function get_sisa($id){
        $d_keluar = DB::table('pro_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id)->first();

        $query = "SELECT B.*
                FROM (
                ".base_stok_masuk_multi()."
                ) B WHERE B.id_barangkeluar_kemasan = $id AND B.stok > 0";

            $result = get_rawquery($query);
        

        return $result;
        

    }

    function get_hapus($id){
        $cek_upah = DB::table('pro_upahproduksi_kemasan')->where('id_barangmasuk_kemasan', $id);
        $del = '';
        if($cek_upah->count() > 0){
            if($cek_upah->first()->status_bayar != 1){
                $del = '<button class="btn bg-danger-800" type="button" onclick="hapus($(this))" id_hasil="'.$id.'" jenis="0"><i class="icon-eraser"></i></button>';
            }else{
                $del = '';    
            }           
        }else{
            $del = '<button class="btn bg-danger-800" type="button" onclick="hapus($(this))" id_hasil="'.$id.'" jenis="0"><i class="icon-eraser"></i></button>';
        }

        return $del;
    }

    function hapus_hasil(Request $request){
        $req = $request->all();
        $id = $req['id'];
        $jenis = $req['jenis'];

        if($jenis == 0){
            $d = $this->get_barangstok($id);

            if(in_array(0, $d)){
                $stat = array('status' => '0');
            }else{
                $this->hapus($id);
                $stat = array('status' => '1');
            }
        }else{
            $this->hapus($id);
            $stat = array('status' => '1');
        }

        return $stat;
    }

    function get_barangstok($id){
        $d_masuk = DB::table('view_edit_barangmasuk_kemasan')->where('id_barangmasuk_kemasan', $id);
        $jum = array();

        if($d_masuk->count() > 0){
            foreach($d_masuk->get() as $d){

                $jumlah = $d->jumlah;

                $where = "WHERE B.id_gudang = '$d->id_gudang' AND B.id_barang = '$d->id_barang' AND B.id_komposisi = '$d->id_komposisi' AND B.id_corak ='$d->id_corak' AND B.id_motif ='$d->id_motif' AND B.id_kualitas = '$d->id_kualitas' AND B.id_kualitas_dudul = '$d->id_kualitas_dudul' AND B.id_label = '$d->id_label' AND B.id_kemasan = '$d->id_kemasan' AND B.id_kemasansatuan = '$d->id_kemasansatuan' AND B.id_kemasansepuluh = '$d->id_kemasansepuluh' AND B.id_kemasanseratus = '$d->id_kemasanseratus' AND B.id_proses = '$d->id_proses' AND B.id_workorder = '$d->id_workorder' AND B.id_workorder_barang = '$d->id_workorder_barang' AND B.id_cap = '$d->id_cap' AND B.id_kualitas_periksa = '$d->id_kualitas_periksa' AND B.id_kualitas_proses = '$d->id_kualitas_proses' AND B.id_perubahansatuan = '$d->id_perubahansatuan' AND B.jumlah_rencana = '$d->jumlah_rencana'";

               

                $usersBills = get_rawquery("SELECT B.*
                                             FROM (
                                                 ".base_stok()."
                                             ) B
                                             $where ");

                if(count($usersBills) > 0){
                    if($usersBills[0]['stok'] < $jumlah){
                        $jum[] = 0;
                    }else{
                        $jum[] = 1;
                    }
                }else{
                    $jum[] = 1;
                }

        
            }
        }else{
            $jum[] = 0;
        }

        return $jum;
    }

    function hapus($id){

        $status_0 = array('status' => '0');
        $status_1 = array('status' => '1');
        $status_3 = array('status' => '3');

        $id_barangkeluar = DB::table('pro_barangmasuk_kemasan')->where('id_barangmasuk_kemasan', $id)->first()->id_barangkeluar_kemasan;

        DB::table('pro_upahproduksi_kemasan')->where('id_barangmasuk_kemasan', $id)->delete();
        DB::table('pro_barangmasuk_kemasan')->where('id_barangmasuk_kemasan', $id)->delete();
        DB::table('pro_barangmasuk_kemasan_barang')->where('id_barangmasuk_kemasan', $id)->delete();

        $cek_hasil = DB::table('pro_barangmasuk_kemasan')->where('id_barangkeluar_kemasan', $id_barangkeluar);

        if($cek_hasil->count() > 0){
            DB::table('pro_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id_barangkeluar)->update($status_3);
        }else{
            DB::table('pro_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id_barangkeluar)->update($status_1);
        }

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

}