<?php

namespace App\Http\Controllers\produksi;

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

class BarangkeluarkemasanController 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'));
    }

	public function index(){
       
    	return view('produksi.BarangkeluarkemasanList');
    }

    function get_data(){
    	$arr = array();

    	$d_data = DB::table('pro_barangkeluar_kemasan as pbk')->join('pro_barangkeluar_kemasan_barang as pbkb', 'pbk.id_barangkeluar_kemasan', 'pbkb.id_barangkeluar_kemasan')->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"))->get();
      
      if($this->session['id_proses'] != 0 && $this->session['id_lokasi'] != 0){
        $d_data = DB::table('pro_barangkeluar_kemasan as pbk')->join('pro_barangkeluar_kemasan_barang as pbkb', 'pbk.id_barangkeluar_kemasan', 'pbkb.id_barangkeluar_kemasan')->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')->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"))->get();
      }

    	foreach($d_data as $d){
    		$pemroses = '';

    		if($d->id_rekanan != '0'){
    			$pemroses = $d->nama_rekanan.' - '.$d->nama_kelompok.' ( Rekanan )';
    		}else{
    			$pemroses = $d->nama_pegawai.' ( Pegawai ) ';
    		}



    		$arr[] = array('id_barangkeluar_kemasan' => $d->id_barangkeluar_kemasan,
                'nomor' => $d->noAuto.$d->noAuto_ext,
    						'tanggal' => tgl_full($d->tanggal, ''),
    						'pemroses' => $pemroses,
    						'proses' => $d->nama_proses,
    						'status' => status_bon($d->status),
    						'aksi' => $this->aksi_perubahan($d->id_barangkeluar_kemasan, $d->status));
    	}

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

    function tambah(){
    	//$data['proses'] = DB::table('ms_proses')->where('nama_proses', 'like', '%pengemasan%')->get();
      $id_lokasi = $this->session['id_lokasi'];
      $id_proses = $this->session['id_proses'];

      $data['proses'] = DB::table('ms_proses')->where('jenis', '1');
      if ($id_proses != 0) {
        $data['proses']->whereIn('id_proses', explode(",", $id_proses));
      }
      $data['proses'] = $data['proses']->get();

    	$data['lokasi'] = DB::table('ms_lokasi_produksi')->orderBy('nama_lokasi', 'asc');
      if ($id_lokasi != 0) {
        $data['lokasi']->where('id_lokasi', $id_lokasi);
      }
      $data['lokasi'] = $data['lokasi']->get();

    	$data['pegawai'] = DB::table('ms_pegawai')->orderBy('nama_pegawai', 'asc')->get();
    	$data['rekanan'] = DB::table('ms_rekanan as mr')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->select(DB::raw('mr.*, mrk.nama nama_kelompok'))->orderby('mr.nama', 'asc')->get();
    	$data['proses_sebelum'] = DB::table('ms_proses')->where('parent', '!=', '0')->get();
    	$data['data'] = $this->data();
    	return view('produksi.BarangkeluarkemasanTambah')->with('data', $data);
    }

    function edit($id){
     	$d_data = DB::table('pro_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id)->first();
    	$data['proses'] = DB::table('ms_proses')->where('jenis', '1')->get();
    	$data['lokasi'] = DB::table('ms_lokasi_produksi')->orderBy('nama_lokasi', 'asc')->get();
    	$data['pegawai'] = DB::table('ms_pegawai')->orderBy('nama_pegawai', 'asc')->get();
    	$data['rekanan'] = DB::table('ms_rekanan as mr')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->select(DB::raw('mr.*, mrk.nama nama_kelompok'))->orderby('mr.nama', 'asc')->get();
    	$data['proses_sebelum'] = DB::table('ms_proses')->where('parent', '!=', '0')->get();
    	$data['data'] = $this->data($d_data);

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

    function detail($id){
      $d_data = DB::table('pro_barangkeluar_kemasan as pbk')->leftjoin('ms_proses as mp', 'pbk.id_proses', 'mp.id_proses')->leftjoin('ms_pegawai as mpg', 'pbk.id_pegawai', 'mpg.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_lokasi_produksi as mlp', 'pbk.id_lokasi', 'mlp.id_lokasi')->where('pbk.id_barangkeluar_kemasan', $id)->select(DB::raw('pbk.id_barangkeluar_kemasan, pbk.tanggal, pbk.tanggal_perkiraan, pbk.id_proses, mp.nama_proses, mpg.id_pegawai, mpg.nama_pegawai, mr.id_rekanan, mr.nama nama_rekanan, mrk.id_kelompok, mrk.nama nama_kelompok, mlp.id_lokasi, mlp.nama_lokasi'))->first();
      $data['proses'] = DB::table('ms_proses')->where('jenis', '1')->get();
      $data['lokasi'] = DB::table('ms_lokasi_produksi')->orderBy('nama_lokasi', 'asc')->get();
      $data['pegawai'] = DB::table('ms_pegawai')->orderBy('nama_pegawai', 'asc')->get();
      $data['rekanan'] = DB::table('ms_rekanan as mr')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->select(DB::raw('mr.*, mrk.nama nama_kelompok'))->orderby('mr.nama', 'asc')->get();
      $data['proses_sebelum'] = DB::table('ms_proses')->where('parent', '!=', '0')->get();
      $data['data'] = $d_data;

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

    function get_barang_proses(Request $request){
        //$id_lokasi = $this->session['id_lokasi'];

        $id_proses = $request->get("id_proses");
        $id_lokasi = $request->get("id_lokasi");
        
        $id_gudang = $this->session["id_gudang"];

		    $where = " WHERE B.id_proses = '$id_proses'";
        
        if($id_lokasi != 0){
          $d_gudang = DB::table('ms_gudang')->where('id_lokasi', $id_lokasi)->pluck('id_gudang');
          
          if($id_gudang != 0){
            $d_gudang->whereIn('id_gudang', explode(",", $id_gudang));
          }

          $d_gudang = $d_gudang->toArray();
          $d_gudang = implode(",", $d_gudang);
          $where .= " AND B.id_gudang IN ($d_gudang)";
        }

        $PDO = DB::connection('mysql')->getPdo();
        
        $billingStmt = $PDO->prepare("SELECT B.*, mkb.nama kategori, mkb2.nama sub_kategori, CASE WHEN mwk.id_komposisi IS NULL THEN '' ELSE mwk.inisial END AS inisial_komposisi
                                     FROM (
                                         ".base_stok($where)."
                                     ) B
                                     JOIN ms_barang mb ON B.id_barang = mb.id
                                     JOIN ms_kategori_barang mkb ON mb.id_kb = mkb.id_kb
                                     JOIN ms_kategori_barang mkb2 ON mkb.induk_kb = mkb2.id_kb
                                     LEFT JOIN ms_warnakomposisi as mwk ON mwk.id_komposisi = B.id_komposisi
                                     $where ");
    
        $billingStmt->execute();
        $usersBills = $billingStmt->fetchAll((\PDO::FETCH_ASSOC));

        return response()->json(barang_byproses($usersBills, 6, "asdf"));

      //  print_r($usersBills);

        
   }

   function get_barang(Request $request){
        
            $id = $request->get('id');
            $id_barang = $request->get('id_barang');
            $id_proses = $request->get("id_proses");
            $id_komposisi= $request->get("id_komposisi");
            $id_corak = $request->get('id_corak');
            $spk = $request->get('spk');
            $workorder = $request->get('workorder'); 
            $id_kualitas = $request->get('id_kualitas');
            $id_dudul = $request->get('id_dudul');
            $id_motif = $request->get('id_motif');
            $id_label = $request->get('id_label');
            $id_kemasan = $request->get('id_kemasan');
            $id_kemasansatuan = $request->get('id_kemasansatuan');
            $id_kemasansepuluh = $request->get('id_kemasansepuluh');
            $id_kemasanseratus = $request->get('id_kemasanseratus');
            $id_cap = $request->get('id_cap');
            $id_kualitasproses = $request->get('id_kualitasproses');
            $id_kualitasperiksa = $request->get('id_kualitasperiksa');
            $id_rencana = $request->get('id_rencana');
            $jumlah_rencana = $request->get('jumlah_rencana');

            $where = "WHERE B.id_gudang = '$id'";

            

            $where .= " AND B.id_barang = '$id_barang'";
      			$where .= " AND B.id_komposisi = '$id_komposisi'";
      			$where .= " AND B.id_corak ='$id_corak'";
      			$where .= " AND B.id_motif ='$id_motif'";
      			$where .= " AND B.id_kualitas = '$id_kualitas'";
      			$where .= " AND B.id_kualitas_dudul = '$id_dudul'";
      			$where .= " AND B.id_label = '$id_label'";
      			$where .= " AND B.id_kemasan = '$id_kemasan'";
      			$where .= " AND B.id_kemasansatuan = '$id_kemasansatuan'";
      			$where .= " AND B.id_kemasansepuluh = '$id_kemasansepuluh'";
      			$where .= " AND B.id_kemasanseratus = '$id_kemasanseratus'";
      			$where .= " AND B.id_proses = '$id_proses'";
      			$where .= " AND B.id_workorder = '$spk'";
      			$where .= " AND B.id_workorder_barang = '$workorder'";
      			$where .= " AND B.id_cap = '$id_cap'";
      			$where .= " AND B.id_kualitas_periksa = '$id_kualitasperiksa'";
      			$where .= " AND B.id_kualitas_proses = '$id_kualitasproses'";
            $where .= " AND B.id_perubahansatuan = '$id_rencana'";
            $where .= " AND B.jumlah_rencana = '$jumlah_rencana'";

        $PDO = DB::connection('mysql')->getPdo();
        
        $billingStmt = $PDO->prepare("SELECT B.*, mkb.nama kategori, mkb2.nama sub_kategori, CASE WHEN mwk.id_komposisi IS NULL THEN '' ELSE mwk.inisial END AS inisial_komposisi
                                     FROM (
                                         ".base_stok($where)."
                                     ) B
                                     JOIN ms_barang mb ON B.id_barang = mb.id
                                     JOIN ms_kategori_barang mkb ON mb.id_kb = mkb.id_kb
                                     JOIN ms_kategori_barang mkb2 ON mkb.induk_kb = mkb2.id_kb
                                     LEFT JOIN ms_warnakomposisi as mwk ON mwk.id_komposisi = B.id_komposisi
                                     $where ");
    
        $billingStmt->execute();
        $usersBills = $billingStmt->fetchAll((\PDO::FETCH_ASSOC));

       return response()->json(barang_byproses($usersBills, 4, "asdf"));
   }

   	function simpan(Request $request){
   		$id_bonbahan = request()->get('id_bonbahan');

   		$data['tanggal'] = tgl_full(request()->get('tanggal_ambil'), 2);
      $data['tanggal_full'] = tgl_full(request()->get('tanggal_ambil'), 5);
   		$data['id_proses'] = request()->get('proses_produksi');
      // $data['noAuto'] = $request->get('nomor');
      // $data['noAuto_ext'] = $request->get('nomor_ext');
      
      $data = $data+$this->validasi_nomor($id_bonbahan, $request->get('nomor'), $request->get('nomor_ext'), request()->get('id_lokasi'));

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

   		$rekanan = '0';
   		$pegawai = '0';

   		if(request()->get('pegawai') == ''){
   			$pegawai = '0';
   			$rekanan = request()->get('rekanan');
   		}else{
   			$pegawai = request()->get('pegawai');
   			$rekanan = '0';
   		}

   		$data['id_pegawai'] = $pegawai;
   		$data['id_rekanan'] = $rekanan;
   		
   		$data['tanggal_perkiraan'] = tgl_full(request()->get('tanggal_selesai'), 2);
   		$data['id_lokasi'] = request()->get('id_lokasi');

   		$tabel_id = $request->get('tabel_id');
        $tabel_satuan = $request->get('tabel_idsatuan');
        $tabel_kode = $request->get('tabel_kode');
        $tabel_nama = $request->get('tabel_nama');
        $tabel_idgudang = $request->get('tabel_idgudang');
        $tabel_idproses = $request->get('tabel_idproses');
        $tabel_idkomposisi = $request->get('tabel_idwarna');
        $tabel_idcorak = $request->get('tabel_idcorak');
        $tabel_idmotif = $request->get('tabel_idmotif');
        $tabel_idlabel = $request->get('tabel_idlabel');
        $tabel_idkualitas = $request->get('tabel_idkualitas');
        $tabel_idkualitas_dudul = $request->get('tabel_idkualitas_dudul');
        $tabel_idkemasan = $request->get('tabel_idkemasan');
        $tabel_idkemasan_satuan = $request->get('tabel_idkemasan_satuan');
        $tabel_idkemasan_sepuluh = $request->get('tabel_idkemasan_sepuluh');
        $tabel_idkemasan_seratus = $request->get('tabel_idkemasan_seratus');
        $tabel_jumlah = $request->get('tabel_jumlah');
        $tabel_idcap = $request->get('tabel_idcap');
        $tabel_idkualitasproses = $request->get('tabel_idkualitas_proses');
        $tabel_idkualitasperiksa = $request->get('tabel_idkualitas_periksa');
        $tabel_spk = $request->get('tabel_spk');
        $tabel_workorder = $request->get('tabel_workorder');
        $tabel_idrencana = $request->get('tabel_idrencana');
        $tabel_jumlahrencana = $request->get('tabel_rencanajumlah');
        $tabel_keterangan = $request->get('tabel_keterangan');

   		if($id_bonbahan == ''){

   			$id = DB::table('pro_barangkeluar_kemasan')->insertGetId($data);

   			for($i = 0; $i < count($tabel_id); $i++){
            	$barang['id_barangkeluar_kemasan'] = $id;
                $barang['id_barang'] = $tabel_kode[$i];
                $barang['nama_barang'] = $tabel_nama[$i];
                $barang['id_satuan'] = $tabel_satuan[$i];
                $barang['id_gudang'] = $tabel_idgudang[$i];
                $barang['id_proses_sebelum'] = $tabel_idproses[$i];
                $barang['id_komposisi'] = $tabel_idkomposisi[$i];
                $barang['id_motif'] = $tabel_idmotif[$i];
                $barang['id_motif_barang'] = $tabel_idcorak[$i];
                $barang['id_kualitas'] = $tabel_idkualitas[$i];
                $barang['id_kualitas_dudul'] = $tabel_idkualitas_dudul[$i];
                $barang['id_kemasan'] = $tabel_idkemasan[$i];
                $barang['id_label'] = $tabel_idlabel[$i];
                $barang['id_cap'] = $tabel_idcap[$i];
                $barang['id_kemasansatuan'] = $tabel_idkemasan_satuan[$i];
                $barang['id_kemasansepuluh'] = $tabel_idkemasan_sepuluh[$i];
                $barang['id_kemasanseratus'] = $tabel_idkemasan_seratus[$i];
                $barang['jumlah'] = $tabel_jumlah[$i];
                $barang['id_kualitas_proses'] = $tabel_idkualitasproses[$i];
                $barang['id_kualitas_periksa'] = $tabel_idkualitasperiksa[$i];
                $barang['id_workorder'] = $tabel_spk[$i];
                $barang['id_workorder_barang'] = $tabel_workorder[$i];
                $barang['id_perubahansatuan'] = $tabel_idrencana[$i];
                $barang['jumlah_rencana'] = $tabel_jumlahrencana[$i];
                $barang['keterangan'] = $tabel_keterangan[$i];

                print_r("\n");
   				print_r($barang);

                DB::table('pro_barangkeluar_kemasan_barang')->insert($barang);
            }
   		}else{
   			DB::table('pro_barangkeluar_kemasan')->where(array('id_barangkeluar_kemasan' => $id_bonbahan))->update($data);
            $id_del = array();
            $id_store = array();
            $d_barang = DB::table('pro_barangkeluar_kemasan_barang')->where('id_barangkeluar_kemasan', '=', $id_bonbahan);
            foreach($d_barang->get() as $d){
                $id_store[] = $d->id_barangkeluar_kb;
            }

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

            for($i = 0; $i < count($tabel_id); $i++){
                $barang['id_barangkeluar_kemasan'] = $id_bonbahan;
                $barang['id_barang'] = $tabel_kode[$i];
                $barang['nama_barang'] = $tabel_nama[$i];
                $barang['id_satuan'] = $tabel_satuan[$i];
                $barang['id_gudang'] = $tabel_idgudang[$i];
                $barang['id_proses_sebelum'] = $tabel_idproses[$i];
                $barang['id_komposisi'] = $tabel_idkomposisi[$i];
                $barang['id_motif'] = $tabel_idmotif[$i];
                $barang['id_motif_barang'] = $tabel_idcorak[$i];
                $barang['id_kualitas'] = $tabel_idkualitas[$i];
                $barang['id_kualitas_dudul'] = $tabel_idkualitas_dudul[$i];
                $barang['id_kemasan'] = $tabel_idkemasan[$i];
                $barang['id_label'] = $tabel_idlabel[$i];
                $barang['id_cap'] = $tabel_idcap[$i];
                $barang['id_kemasansatuan'] = $tabel_idkemasan_satuan[$i];
                $barang['id_kemasansepuluh'] = $tabel_idkemasan_sepuluh[$i];
                $barang['id_kemasanseratus'] = $tabel_idkemasan_seratus[$i];
                $barang['jumlah'] = $tabel_jumlah[$i];
                $barang['id_kualitas_proses'] = $tabel_idkualitasproses[$i];
                $barang['id_kualitas_periksa'] = $tabel_idkualitasperiksa[$i];
                $barang['id_workorder'] = $tabel_spk[$i];
                $barang['id_workorder_barang'] = $tabel_workorder[$i];
                $barang['id_perubahansatuan'] = $tabel_idrencana[$i];
                $barang['jumlah_rencana'] = $tabel_jumlahrencana[$i];
                $barang['keterangan'] = $tabel_keterangan[$i];

                if($tabel_id[$i] == ''){
                    DB::table('pro_barangkeluar_kemasan_barang')->insert($barang);
                }else if($tabel_id != ''){
                    $where = 
                    DB::table('pro_barangkeluar_kemasan_barang')->where(array('id_barangkeluar_kb' => $tabel_id[$i]))->update($barang);
                }
            }

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

   		
   		return redirect('barangkeluar_kemasan');
   	}

   	function data($data = array()){
   		if(count($data) > 0){
   			$store['id_barangkeluar_kemasan'] = $data->id_barangkeluar_kemasan;
   			$store['tanggal'] = tgl_full($data->tanggal, '');
   			$store['id_pegawai'] = $data->id_pegawai;
   			$store['id_rekanan'] = $data->id_rekanan;
   			$store['id_proses'] = $data->id_proses;
   			$store['tanggal_perkiraan'] = tgl_full($data->tanggal_perkiraan, '');
   			$store['id_barangkeluar_kemasan'] = $data->id_barangkeluar_kemasan;
   			$store['id_lokasi'] = $data->id_lokasi;
   		}else{
   			$store['id_barangkeluar_kemasan'] = '';
   			$store['tanggal'] = '';
   			$store['id_pegawai'] = '';
   			$store['id_rekanan'] = '';
   			$store['id_proses'] = '';
   			$store['tanggal_perkiraan'] = '';
   			$store['id_barangkeluar_kemasan'] = '';
   			$store['id_lokasi'] = '';
   		}

   		return $store;
   	}

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

        DB::table('pro_barangkeluar_kemasan')->where(array('id_barangkeluar_kemasan' => $id))->delete();
        DB::table('pro_barangkeluar_kemasan_barang')->where(array('id_barangkeluar_kemasan' => $id))->delete();
    }

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

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

      if($d_data->count() > 0){
        $arr = $d_data->get();

        foreach ($arr as $k => $data) {
          $arr[$k]->status_div = status_approve($data->status);               
        }
      }else{
        $arr= array();
      }
      
      
    	return response()->json($arr);
    }

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

        if($data['id'] != ''){
            $d_data = DB::table('pro_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $data['id']);
            if($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_barangkeluar_kemasan")->select(DB::raw("noAuto, noAuto_ext"));
            if($id_lokasi != ''){
                $d_kemasan->where('id_lokasi', $id_lokasi);
            }
            $d_data = DB::table('pro_barangkeluar')->unionAll($d_kemasan)->select(DB::raw("noAuto, noAuto_ext"));
            if($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' => "BB.",
                        '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_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id);
                if($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_barangkeluar')->where('id_barangkeluar', $id)->update(['noAuto' => '', 'noAuto_ext' => '']);
                        return $this->validasi_nomor(0, $noAuto, $noAuto_ext, $id_lokasi);
                    }
                }else{
                    $arr = array('noAuto' => $noAuto,
                            'noAuto_ext' => $noAuto_ext); 
                }

            }else{
                $no = 1;
                $where = '';
            
                $d_data[] = "SELECT noAuto, noAuto_ext, id_lokasi FROM pro_barangkeluar";
                $d_data[] = "SELECT noAuto, noAuto_ext, id_lokasi FROM pro_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 cetak(Request $request, $id){
      require('fpdf181/Mc_table.php');

      $req = $request->all();

      $data['data'] = get_keluarkemasan_byid()->where('pbk.id_barangkeluar_kemasan', $id)->first();
      $data['detail'] = DB::table('view_edit_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id)->get();

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

      $html = view('produksi.cetak.'.$view)->with('data', $data);
      return response($html)->header('Content-Type', 'application/pdf');
    }

    function aksi_perubahan($id, $status){
        $d_data = DB::table('pro_barangkeluar_kemasan_barang')->where('id_barangkeluar_kemasan', $id)->pluck('status')->toArray();

        if($status == '0'){
          if(in_array(1, $d_data)){
            $ret = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangkeluar_kemasan/detail/').'/'.$id.'"><i class="icon-notification2"></i></a> <button class="btn bg-danger-800" type="button" onclick="hapus('.$id.')"><i class="icon-eraser"></i></button> <button type="button" class="btn btn-success 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('barangkeluar_kemasan/cetak/'.$id).'?kertas=1'.'" target="_blank"><i class="icon-printer4"></i> Cetak Sedang</a></li> <li><a href="'.url('barangkeluar_kemasan/cetak/'.$id).'?kertas=0'.'" target="_blank"><i class="icon-printer4"></i> Cetak Kecil</a></li> </ul> </div>';
          }else{
            $ret = '<div class="btn-group"> <a class="btn bg-warning-400" type="button" href="'.url('barangkeluar_kemasan/edit').'/'.$id.'"><i class="icon-pencil"></i></a> <button class="btn bg-danger-800" type="button" onclick="hapus('.$id.')"><i class="icon-eraser"></i></button> <button type="button" class="btn btn-success 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('barangkeluar_kemasan/cetak/'.$id).'?kertas=1'.'" target="_blank"><i class="icon-printer4"></i> Cetak Sedang</a></li> <li><a href="'.url('barangkeluar_kemasan/cetak/'.$id).'?kertas=0'.'" target="_blank"><i class="icon-printer4"></i> Cetak Kecil</a></li> </ul> </div>';
          }
        }else if($status == '1'){
          $ret = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangkeluar_kemasan/detail/').'/'.$id.'"><i class="icon-notification2"></i></a> <a class="btn bg-warning-400" type="button" href="'.url('barangkeluar_kemasan/edit').'/'.$id.'"><i class="icon-pencil"></i></a> <button class="btn bg-danger-800" type="button" onclick="hapus('.$id.')"><i class="icon-eraser"></i></button> <button type="button" class="btn btn-success 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('barangkeluar_kemasan/cetak/'.$id).'?kertas=1'.'" target="_blank"><i class="icon-printer4"></i> Cetak Sedang</a></li> <li><a href="'.url('barangkeluar_kemasan/cetak/'.$id).'?kertas=0'.'" target="_blank"><i class="icon-printer4"></i> Cetak Kecil</a></li> </ul></div>';
        }
        else{
          $ret = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangkeluar_kemasan/detail/').'/'.$id.'"><i class="icon-notification2"></i></a>  <button type="button" class="btn btn-success 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('barangkeluar_kemasan/cetak/'.$id).'?kertas=1'.'" target="_blank"><i class="icon-printer4"></i> Cetak Sedang</a></li> <li><a href="'.url('barangkeluar_kemasan/cetak/'.$id).'?kertas=0'.'" target="_blank"><i class="icon-printer4"></i> Cetak Kecil</a></li> </ul></div>';
        }

        return $ret;
    }

}