<?php

namespace App\Http\Controllers\produksi_atm;

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

class BarangkeluarAtmController 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_atm.BarangkeluarList');
    }

    function get_data(){
    	DB::statement(DB::raw('set @rownum=0'));

        
        
        if($this->session['id_proses'] != 0 && $this->session['id_lokasi'] != 0){
         $d_data = DB::table('pro_barangkeluar as pb')->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('ms_mesin as mms','pb.id_mesin','mms.id_mesin')->leftjoin('pem_workorder_barang as pob', 'pb.id_workorder_barang', 'pob.id_workorder_barang')->leftjoin('pem_workorder as pw','pb.id_workorder','pw.id_workorder')->select(DB::raw('pb.*, pob.no_workorder as no_workorder_barang, pw.nomor as no_workorder_spk, mp.nama_proses, mr.nama, mpg.nama_pegawai, mms.nama as nama_mesin, pb.noAuto, pb.noAuto_ext, @rownum  := @rownum  + 1 AS rownum'))->where('pb.id_lokasi','!=','1')->where("pb.id_lokasi", $this->session['id_lokasi'])->whereIn('pb.id_proses', explode(",", $this->session['id_proses']))->orderBy('pb.tanggal', 'desc')->orderBy('pb.status', 'asc');
        }else{
            $d_data = DB::table('pro_barangkeluar as pb')->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('ms_mesin as mms','pb.id_mesin','mms.id_mesin')->leftjoin('pem_workorder_barang as pob', 'pb.id_workorder_barang', 'pob.id_workorder_barang')->leftjoin('pem_workorder as pw','pb.id_workorder','pw.id_workorder')->select(DB::raw('pb.*, pob.no_workorder as no_workorder_barang, pw.nomor as no_workorder_spk, mp.nama_proses, mr.nama, mpg.nama_pegawai, mms.nama as nama_mesin, @rownum  := @rownum  + 1 AS rownum'))->where('pb.id_lokasi','!=','1')->orderBy('pb.tanggal', 'desc')->orderBy('pb.status', 'asc');
        }
        if($d_data->count() > 0){
            foreach($d_data->get() as $d){
                $pekerja = '';
                $stat_pekerja = '';
                if($d->nama != '' || !is_null($d->nama)){
                    $pekerja = $d->nama;
                    $stat_pekerja = 'Rekanan';
                }else{
                    $pekerja = $d->nama_mesin;
                    $stat_pekerja = 'Mesin';
                }

                $nomor = "";
                $no_ket = "";
                if($d->no_workorder_barang != '' || !is_null($d->no_workorder_barang)){
                    $nomor = $d->no_workorder_barang;
                    $no_ket = "(No. Order)";
                }

                $data['data'][] = array('id_barangkeluar' => $d->id_barangkeluar,
                                        'nomor' => $d->noAuto.$d->noAuto_ext,
                                        'no_workorder' => $nomor.' '.$no_ket,
                                        'tanggal' => date('d-m-Y', strtotime($d->tanggal)),
                                        'tanggal_perkiraan' => date('d-m-Y', strtotime($d->tanggal_perkiraan)),
                                        'nama_proses' => $d->nama_proses,
                                        'pekerja' => $pekerja." (".$stat_pekerja.")",
                                        'status' => status_bon($d->status),
                                        'aksi' => $this->get_aksi($d->id_barangkeluar, $d->status),
                                        'rownum' => $d->rownum);
            }
        }else{
            $data['data'] = array();
        }

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

    public function tambah(){
    	$id_proses = $this->session['id_proses'];
        $id_lokasi = $this->session['id_lokasi'];

        $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'] = DB::table('ms_proses as mp')->leftJoin('ms_proses as mp2','mp.parent','mp2.id_proses')->select('mp.nama_proses as nama_proses','mp2.nama_proses as nama_proses2','mp.id_proses')->where('mp.jenis', '0')->orderBy('mp.id_proses');
    	
        $data['mesin'] = DB::table('ms_mesin as mm')->join('ms_lokasi_produksi as mmp','mm.id_lokasi','mmp.id_lokasi')->select('mm.*')->where('mm.id_lokasi','!=','')->orderby('mm.nama','ASC');

        if($id_proses != 0){
            $data['proses']->whereIn('mp.id_proses', explode(",", $id_proses));
        }

        if($id_lokasi != 0 && $id_lokasi != ''){
            $data['mesin']->where("mmp.id_lokasi", $id_lokasi);
        }

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

        $data['proses'] = $data['proses']->get();
        $data['proses_popup'] = DB::table('ms_proses as mp')->leftJoin('ms_proses as mp2','mp.parent','mp2.id_proses')->select('mp.nama_proses as nama_proses','mp2.nama_proses as nama_proses2','mp.id_proses')->orderBy('mp.id_proses','ASC')->get();

        $data['workorder'] = $this->get_spk(2);
    	$data['gudang'] = DB::table('ms_gudang')->where('id_lokasi', '!=', '1')->get();
        $data['pegawai'] = DB::table('ms_pegawai')->orderby('nama_pegawai', 'asc')->get();
        $data['kualitas'] = DB::table('ms_kualitas')->get();
        $data['warna'] = DB::table('ms_warnakomposisi')->orderby('nama', 'asc')->get();
        $data['satuan'] = DB::table('ms_satuan')->orderby('nama_satuan', 'asc')->get();
        $data['produksi'] = DB::table('ms_lokasi_produksi')->where('id_lokasi', '!=', '1');

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

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

        $data['noAuto'] = $this->noauto(); 
    	$data['data'] = $this->data(array());
    	return view('produksi_atm.BarangkeluarTambah')->with('data',$data);
    }

    function noauto(){
        $nama = 'BON.';
        $d_barang = DB::table('pro_barangkeluar')->where('noAuto', 'like', $nama.'%')->orderBy('noAuto', 'desc');
        if($d_barang->get()->count() > 0){
            $kode = $d_barang->first()->noAuto;    
        }else{
            $kode = 0;
        }
        $kode_kat = strlen($nama);
        $kode = substr($kode, ($kode_kat));
        $kode = sprintf('%04d', $kode+1);
        $kode = $nama.$kode;
        return $kode;
    }

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

        $d_data = DB::table('pro_barangkeluar')->where('id_barangkeluar', '=', $id)->first();
        $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'] = DB::table('ms_proses as mp')->leftJoin('ms_proses as mp2','mp.parent','mp2.id_proses')->select('mp.nama_proses as nama_proses','mp2.nama_proses as nama_proses2','mp.id_proses')->orderBy('mp.id_proses');
        $data['mesin'] = DB::table('ms_mesin as mm')->join('ms_lokasi_produksi as mmp','mm.id_lokasi','mmp.id_lokasi')->select('mm.*')->where('mm.id_lokasi','!=','')->orderby('mm.nama','ASC')->get();
        $data['gudang'] = DB::table('ms_gudang')->where('id_lokasi', '!=', '1')->get();
        
        $data['workorder'] = $this->get_spk(2);
        $data['pegawai'] = DB::table('ms_pegawai')->orderby('nama_pegawai', 'asc')->get();
        $data['kualitas'] = DB::table('ms_kualitas')->get();
        $data['warna'] = DB::table('ms_warnakomposisi')->orderby('nama', 'asc')->get();
        $data['satuan'] = DB::table('ms_satuan')->orderby('nama_satuan', 'asc')->get();
        $data['produksi'] = DB::table('ms_lokasi_produksi')->where('id_lokasi', '!=', '1');
        $data['noAuto'] = ''; 
        $data['data'] = $this->data($d_data);
        if($id_proses != 0){
            $data['proses']->whereIn('mp.id_proses', explode(",", $id_proses));
        }
        if($id_lokasi != 0){
            $data['produksi']->where('id_lokasi', $id_lokasi);
        }
        $data['proses'] = $data['proses']->get();
        $data['produksi'] = $data['produksi']->get();
        $data['proses_popup'] = DB::table('ms_proses as mp')->leftJoin('ms_proses as mp2','mp.parent','mp2.id_proses')->select('mp.nama_proses as nama_proses','mp2.nama_proses as nama_proses2','mp.id_proses')->orderBy('mp.id_proses','ASC')->get();

        return view('produksi_atm.BarangkeluarTambah')->with('data', $data);
    }

    function detail($id){
    	$d_data = DB::table('pro_barangkeluar as pb')->leftjoin('pem_workorder_barang as pwb', 'pb.id_workorder_barang', 'pwb.id_workorder_barang')->leftjoin('ms_proses as mp', 'pb.id_proses', 'mp.id_proses')->leftjoin('ms_lokasi_produksi as mlp', 'pb.id_lokasi', 'mlp.id_lokasi')->leftjoin('ms_rekanan as mr', 'pb.id_rekanan', 'mr.id_rekanan')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->leftjoin('ms_mesin as mm', 'pb.id_mesin', 'mm.id_mesin')->select(DB::raw('pb.*, pwb.no_workorder, mp.nama_proses, mlp.nama_lokasi, mr.nama nama_rekanan, mrk.nama nama_kelompok, mm.nama nama_mesin'))->where('id_barangkeluar', '=', $id)->first();
        $data['data'] = $d_data;

        return view('produksi_atm.BarangkeluarDetail')->with('data', $data);
    }
    

    function get_edit(Request $request){
        $id = request()->get('id');
        $d_data = DB::table('view_edit_getedit_keluar as v')->leftjoin('pro_barangkeluar_hasil as pbh', function($join){
            $join->on('v.id_barangkeluar', 'pbh.id_barangkeluar')->on('v.id', 'pbh.id_barangkeluar_barang');
        })->orderBy('v.id', 'asc')->orderBy('status', 'desc')->where('v.id_barangkeluar', '=', $id)->select(DB::raw("v.*, CASE WHEN pbh.id_keluarhasil IS NULL THEN '' ELSE pbh.id_keluarhasil END AS id_keluarhasil, CASE WHEN pbh.id_satuanhasil IS NULL THEN '' ELSE pbh.id_satuanhasil END AS id_satuanhasil, CASE WHEN pbh.hasil IS NULL THEN '' ELSE pbh.hasil END AS hasil"));
        if($d_data->count() > 0){
            $arr = $d_data->get();


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

        return response()->json($arr);
        /*print_r($arr);*/
    }

    function get_barang(Request $request){       
        if($_POST){
            if($request->get("id_proses") == 18 ){
                $d_proses = DB::table('ms_proses')->where('id_proses','<',$request->get("id_proses"))->orderBy('id_proses','DESC')->first();
            }elseif($request->get("id_proses2") == "" || $request->get("id_proses2") == 0){
                $d_proses = DB::table('ms_proses')->where('id_proses','=','17')->orderBy('id_proses','DESC')->first();
            }else{
                $d_proses = DB::table('ms_proses')->where('id_proses',$request->get("id_proses2"))->orderBy('id_proses','DESC')->first();
        	}

            $id = $request->get('id');
            $id_barang = $request->get('id_barang');
            $id_proses = $request->get("id_proses");
            $id_komposisi= $request->get("id_komposisi");
            $id_proses2 = $request->get('id_proses2');
            $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_kualitas_proses = $request->get('id_kualitas_proses');
            $id_kualitas_periksa = $request->get('id_kualitas_periksa');
            $id_perubahansatuan = $request->get('id_rencana');
            $jumlah_rencana = $request->get('jumlah_rencana');

            $where = "WHERE B.id_gudang = '$id'";
            if($id_barang != ''){
                $where .= " AND B.id_barang = '$id_barang'";
            }
            $proses = '';
            $proses2 = '';
            if($id_proses != '' && $d_proses->parent != '0'){

                $proses .= " AND B.id_proses = '$id_proses'";
                $proses2 .= " AND B.id_proses = '$id_proses2'";
            }else{
                $proses2 .= "AND B.id_proses = '$id_proses2'";
            }
            
            $warna = "";
            if($id_komposisi != '' && $id_proses2 != ''){ //&& $id_proses2 != 80
                $warna .= " AND B.id_komposisi = '$id_komposisi'";
            }

            if($id_komposisi != '' && $id_proses2 == '' && $id_proses != 18){
                 $warna .= " AND B.id_komposisi = '$id_komposisi'";   
            }

            if($id_komposisi != '' && $id_proses2 == '' && $id_proses != 18 && $id_proses != 82 && $id_proses != 81){
                 $warna .= " AND B.id_komposisi = '$id_komposisi'";   
            }

            if($id_komposisi == '' && $id_proses2 != '' && $id_proses != 18 && $id_proses != 82 && $id_proses != 81){
                $warna .= " AND B.id_komposisi = ''";
            }

            // if($id_komposisi != '' && $id_proses2 != '' && $id_proses == 80){
            //     $warna .= " AND B.id_komposisi = ''";
            // }

            $corak = "";
            if($id_corak != '' || $id_corak != 0){
                $corak .= " AND B.id_corak ='$id_corak'";
            }

            $motif = "";
            if($id_motif != '' || $id_motif != 0){
                $motif .= " AND B.id_motif ='$id_motif'";
            }

            $kualitas = '';
            if($id_kualitas != '' || $id_kualitas != 0){
                $kualitas .= " AND B.id_kualitas = '$id_kualitas'";
            }

            $dudulan = '';
            if($id_dudul != ''||$id_dudul != 0){
                $dudulan .= " AND B.id_kualitas_dudul = '$id_dudul'";
            }
            
            $label = '';
            if($id_dudul != '' || $id_dudul != 0){
                $label .= " AND B.id_label = '$id_label'";
            }

            $kemasan = '';
            if($id_kemasan != '' || $id_kemasan != 0){
                $kemasan .= " AND B.id_kemasan = '$id_kemasan'";
            }

            $kemasan_satuan = '';
            if($id_kemasansatuan != '' || $id_kemasansatuan != 0){
                $kemasan_satuan .= " AND B.id_kemasansatuan = '$id_kemasansatuan'";
            }

            $kemasan_sepuluh = '';
            if($id_kemasansepuluh != '' || $id_kemasansepuluh != 0){
                $kemasan_sepuluh .= " AND B.id_kemasansepuluh = '$id_kemasansepuluh'";
            }

            $kemasan_seratus = '';
            if($id_kemasanseratus != '' || $id_kemasanseratus != 0){
                $kemasan_seratus .= " AND B.id_kemasanseratus = '$id_kemasanseratus'";
            }

            $cap = '';
            if($id_cap != '' || $id_cap != 0){
                $cap .= " AND B.id_cap = '$id_cap'";
            }

            $kualitas_proses = '';
            if($id_kualitas_proses != '' || $id_kualitas_proses != 0){
                $kualitas_proses .= " AND B.id_kualitas_proses = '$id_kualitas_proses'";
            }

            $kualitas_periksa = '';
            if($id_kualitas_periksa != '' || $id_kualitas_periksa != 0){
                $kualitas_periksa .= " AND B.id_kualitas_periksa = '$id_kualitas_periksa'";
            } 

            
            $perubahansatuan = ''; $rencana_jumlah = '';
            if($id_perubahansatuan != '' || $id_perubahansatuan != 0){
                $perubahansatuan .= " AND B.id_perubahansatuan = '$id_perubahansatuan'";
            }   

            if($jumlah_rencana != '' || $jumlah_rencana != 0){
                $rencana_jumlah .= " AND B.jumlah_rencana = '$jumlah_rencana'";
            }   

            $id_workorder = '';
            if($id_proses == '37'){ // tenun
                $spk_tenun = '';

                if($workorder != ''){
                    $cek_spk_tenun = DB::table('pem_workorder_barang as pwb')->leftjoin('pem_workorder as pb','pwb.id_workorder','pb.id_workorder')->where('pwb.id_workorder_barang','=',$workorder)->select(DB::raw('pb.*,pwb.*'))->first();
                    $spk_tenun = $cek_spk_tenun->id_workorder;
                }
                
                if($id_proses2 == '24'){
                    $id_workorder = " AND B.id_workorder='$spk_tenun'";
                }

            }else if($id_proses == '24'){
                $id_workorder = '';
            } else{
            $id_workorder = "";
                //tak edit dewe
                if($spk != 0 || $spk != ''){
                    if($id_proses2 != ''){
                    $id_workorder = " AND B.id_workorder='$spk'";
                    }
                }
            }
            

            $id_workorder_barang = "";
                if($id_proses != '37' && $id_proses != '24'){
                    if($workorder != 0 || $workorder != ''){
                        $id_workorder_barang = " AND B.id_workorder_barang='$workorder'";
                    }else{
                        $id_workorder_barang = " AND B.id_workorder_barang=''";
                    }
                    
                    if($id_proses2 == 0 || $id_proses2 == ''){
                       $id_workorder_barang = "AND B.id_workorder_barang='$workorder' ";
                    }
                }else{
                    if($workorder != 0 || $workorder != ''){
                        // $id_workorder_barang = " AND B.id_workorder_barang='$workorder'";
                        if($id_proses2 == 24){
                            $id_workorder_barang = " AND B.id_workorder_barang='$workorder'";
                        }
                    }else{
                        $id_workorder_barang = " AND B.id_workorder_barang=''";
                    }
                }
        }
        
        $all_where = " $where $proses2 $warna $corak $id_workorder $id_workorder_barang $motif $kualitas $dudulan 
                                     $label $kemasan $kemasan_satuan $kemasan_sepuluh $kemasan_seratus $cap $kualitas_proses $kualitas_periksa $perubahansatuan $rencana_jumlah ";
        $groupBy = _getgroupbystok($id_proses2, "B");
        $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, SUM(B.stok) stok
                                     FROM (
                                         ".base_stok($all_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
                                     $all_where $groupBy");

        $billingStmt->execute();
        $usersBills = $billingStmt->fetchAll((\PDO::FETCH_ASSOC));

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

    function get_barang_hasil(Request $request){
       
        if($_POST){

            if($request->get("id_proses") == 18 ){
                $d_proses = DB::table('ms_proses')->where('id_proses','<',$request->get("id_proses"))->orderBy('id_proses','DESC')->first();
            }elseif($request->get("id_proses2") == "" || $request->get("id_proses2") == 0){
                $d_proses = DB::table('ms_proses')->where('id_proses','=','17')->orderBy('id_proses','DESC')->first();
            }else{
                $d_proses = DB::table('ms_proses')->where('id_proses',$request->get("id_proses2"))->orderBy('id_proses','DESC')->first();
            }

            $id = $request->get('id');
            $id_barang = $request->get('id_barang');
            $id_proses = $request->get("id_proses");
            $id_komposisi= $request->get("id_komposisi");
            $id_proses2 = $request->get('id_proses2');
            $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_kualitas_proses = $request->get('id_kualitas_proses');
            $id_kualitas_periksa = $request->get('id_kualitas_periksa');
            $id_rencana = $request->get('id_rencana');
            $jumlah_rencana = $request->get('jumlah_rencana');
            $id_satuan = $request->get('id_satuan');

            $where = "WHERE B.id_gudang = '$id'";
            if($id_barang != ''){
                $where .= " AND B.id_barang = '$id_barang'";
            }
            $proses = '';
            $proses2 = '';
            if($id_proses != '' && $d_proses->parent != '0'){

                $proses .= " AND B.id_proses = '$id_proses'";
                $proses2 .= " AND B.id_proses = '$id_proses2'";
            }else{
                $proses2 .= "AND B.id_proses = '$id_proses2'";
            }
            
            $warna = "";
            if($id_komposisi != '' && $id_proses2 != ''){
                $warna .= " AND B.id_komposisi = '$id_komposisi'";
            }

            if($id_komposisi != '' && $id_proses2 == '' && $id_proses != 18 && $id_proses != 82 && $id_proses != 81){
                 $warna .= " AND B.id_komposisi = '$id_komposisi'";   
            }

            if($id_komposisi == '' && $id_proses2 != '' && $id_proses != 18 && $id_proses != 82 && $id_proses != 81){
                $warna .= " AND B.id_komposisi = ''";
            }            

            $corak = " AND B.id_corak = ''";
            if($id_corak != '' || $id_corak != 0){
                $corak = " AND B.id_corak ='$id_corak'";
            }

            $motif = " AND B.id_motif =''";
            if($id_motif != '' || $id_motif != 0){
                $motif = " AND B.id_motif ='$id_motif'";
            }

            $kualitas = " AND B.id_kualitas = ''";
            if($id_kualitas != '' || $id_kualitas != 0){
                $kualitas = " AND B.id_kualitas = '$id_kualitas'";
            }

            $dudulan = '  AND B.id_kualitas_dudul = ""';
            if($id_dudul != ''||$id_dudul != 0){
                $dudulan = " AND B.id_kualitas_dudul = '$id_dudul'";
            }
            
            $label = ' AND B.id_label = "" ';
            if($id_dudul != '' || $id_dudul != 0){
                $label = " AND B.id_label = '$id_label'";
            }

            $kemasan = " AND B.id_kemasan = ''";
            if($id_kemasan != '' || $id_kemasan != 0){
                $kemasan = " AND B.id_kemasan = '$id_kemasan'";
            }

            $kemasan_satuan =  "AND B.id_kemasansatuan = ''";
            if($id_kemasansatuan != '' || $id_kemasansatuan != 0){
                $kemasan_satuan = " AND B.id_kemasansatuan = '$id_kemasansatuan'";
            }

            $kemasan_sepuluh = "  AND B.id_kemasansepuluh = ''";
            if($id_kemasansepuluh != '' || $id_kemasansepuluh != 0){
                $kemasan_sepuluh = " AND B.id_kemasansepuluh = '$id_kemasansepuluh'";
            }

            $kemasan_seratus = "  AND B.id_kemasanseratus = ''";
            if($id_kemasanseratus != '' || $id_kemasanseratus != 0){
                $kemasan_seratus = " AND B.id_kemasanseratus = '$id_kemasanseratus'";
            }

            $cap = " AND B.id_cap = ''";
            if($id_cap != '' || $id_cap != 0){
                $cap = " AND B.id_cap = '$id_cap'";
            }

            $kualitas_proses = " AND B.id_kualitas_proses = ''";
            if($id_kualitas_proses != '' || $id_kualitas_proses != 0){
                $kualitas_proses = " AND B.id_kualitas_proses = '$id_kualitas_proses'";
            }

            $kualitas_periksa = " AND B.id_kualitas_periksa = ''";
            if($id_kualitas_periksa != '' || $id_kualitas_periksa != 0){
                $kualitas_periksa = " AND B.id_kualitas_periksa = '$id_kualitas_periksa'";
            }

            $id_perubahansatuan = " AND B.id_perubahansatuan = ''";
            if($id_rencana != '' || $id_rencana != 0){
                $id_perubahansatuan = " AND B.id_perubahansatuan = '$id_rencana'";
            }

            $jumlah_perubahan = " AND B.jumlah_rencana = ''";
            if($jumlah_perubahan != '' || $jumlah_perubahan != 0){
                $jumlah_perubahan = " AND B.jumlah_rencana = '$jumlah_rencana'";
            }

            $satuan = " AND B.id_satuan = ''";
            if($id_satuan != '' || $id_satuan != 0){
                $satuan = " AND B.id_satuan = '$id_satuan'";
            }




            $id_workorder = '';
            if($id_proses == '37'){
                $spk_tenun = '';

                if($workorder != ''){
                    $cek_spk_tenun = DB::table('pem_workorder_barang as pwb')->leftjoin('pem_workorder as pb','pwb.id_workorder','pb.id_workorder')->where('pwb.id_workorder_barang','=',$workorder)->select(DB::raw('pb.*,pwb.*'))->first();
                    $spk_tenun = $cek_spk_tenun->id_workorder;
                }
                // $id_workorder = " AND B.id_workorder='$spk_tenun'";
                if($id_proses2 == '24'){
                    $id_workorder = " AND B.id_workorder='$spk_tenun'";
                }
            }else if($id_proses == '24'){
                $id_workorder = '';
            }else{
                $id_workorder = "";
                
                if($spk != 0 || $spk != ''){
                    if($id_proses2 != ''){
                    $id_workorder = " AND B.id_workorder='$spk'";
                    }
                }else{

                }
            }
            
            

                $id_workorder_barang = "";
                if($id_proses != '37' && $id_proses != '24'){
                    if($workorder != 0 || $workorder != ''){
                        $id_workorder_barang = " AND B.id_workorder_barang='$workorder'";
                    }else{
                        $id_workorder_barang = " AND B.id_workorder_barang=''";
                    }
                    
                    if($id_proses2 == 0 || $id_proses2 == ''){
                       $id_workorder_barang = "AND B.id_workorder_barang='$workorder' ";
                    }
                }else{
                    if($workorder != 0 || $workorder != ''){
                        // $id_workorder_barang = " AND B.id_workorder_barang='$workorder'";
                        if($id_proses2 == 24){
                            $id_workorder_barang = " AND B.id_workorder_barang='$workorder'";
                        }
                    }else{
                        $id_workorder_barang = " AND B.id_workorder_barang=''";
                    }
                }
            
           


        }
        $not_where = '';
        $PDO = DB::connection('mysql')->getPdo();
        $all_where = " $where $proses2 $warna $corak $id_workorder $id_workorder_barang $motif $kualitas $dudulan 
                                     $label $kemasan $kemasan_satuan $kemasan_sepuluh $kemasan_seratus $cap $kualitas_proses $kualitas_periksa  $not_where "; //$id_perubahansatuan $jumlah_perubahan $satuan
        if($id_proses2 != '33'){
            $all_where .= " $id_perubahansatuan $jumlah_perubahan";
        }
        $groupBy = _getgroupbystok($id_proses2, "B");
         $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, SUM(B.stok) stok
                                     FROM (
                                         ".base_stok($all_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
                                     $all_where $satuan $groupBy");
    
        $billingStmt->execute();
        $usersBills = $billingStmt->fetchAll((\PDO::FETCH_ASSOC));
        return response()->json(barang_byproses($usersBills, 4, "asdf"));   
    }

    function simpan(Request $request){
        if($request->get('rekanan') != "" || $request->get('mesin') != ""){

        $id_barangkeluar = $request->get('id_barangkeluar');
        $data['tanggal'] = date_format(date_create($request->get('tanggal')),'Y-m-d');
        $data['tanggal_full'] = tgl_full($request->get('tanggal'), '5');
        // $data['tanggal_full'] = date_format(date_create($request->get('tanggal')),'Y-m-d')." ".date("H:i:s");
        if($request->get('rekanan')!=""){
        $data['id_rekanan'] = $request->get('rekanan');
        $data['id_mesin'] = "0";   
        }else{
        $data['id_rekanan'] = "0";
        $data['id_mesin'] = $request->get('mesin');
        }
        $data['id_proses'] = $request->get('proses');
        /*$data['id_proses_sebelum'] = $this->get_before($request->get('proses'));*/
        $d_id_proses_sebelum = '17'; 
        if($request->get('tabel_proses2')[0] != '' && $request->get('tabel_proses2')[0] != 0){
            $d_id_proses_sebelum = $request->get('tabel_proses2')[0];
        }
        if($request->get('proses') == 18){
        $data['id_proses_sebelum'] = $this->get_before($request->get('proses'));
        }else{
            $data['id_proses_sebelum'] = $d_id_proses_sebelum;    
        }
        $data['id_workorder'] = "0";
        if($request->get('spk')!=""){
        $data['id_workorder'] = $request->get('spk');
        
        }
        $data['id_workorder_barang'] = "0";
        if($request->get('workorder')!=""){
        $data['id_workorder_barang'] = $request->get('workorder');    
        }
        $data['tanggal_perkiraan'] = date_format(date_create($request->get('tanggal_perkiraan')),'Y-m-d');
        $data['id_lokasi'] = $request->get('id_jenis');

        // $data['noAuto'] = $request->get('nomor');
        // $data['noAuto_ext'] = $request->get('nomor_ext');

        $data = $data+$this->validasi_nomor($id_barangkeluar, $request->get('nomor'), $request->get('nomor_ext'), $request->get('id_jenis'));
        
        $data['id_user'] = $this->session['kode_user'];

        $table_id = $request->get('tabel_id');
        $table_barang = $request->get('tabel_idbarang');
        $table_gudang = $request->get('tabel_idgudang');
        $table_jumlah = $request->get('tabel_jumlah');
        $table_satuan = $request->get('tabel_satuan');
        
        $table_warna = $request->get('tabel_warna');
        $table_sebelum = $request->get('tabel_proses2');
        $table_corak = $request->get('tabel_idcorak');
        $table_motif = $request->get('tabel_idmotif');
        $table_kualitas = $request->get('tabel_idkualitas');
        $table_label = $request->get('tabel_idlabel');
        $table_kemasan = $request->get('tabel_idkemasan');
        $table_kemasansatuan = $request->get('tabel_idkemasan_satuan');
        $table_kemasansepuluh = $request->get('tabel_idkemasan_sepuluh');
        $table_kemasanseratus = $request->get('tabel_idkemasan_seratus');
        $table_cap = $request->get('tabel_idcap');
        $table_kualitas_proses = $request->get('tabel_idkualitas_proses');
        $table_kualitas_periksa = $request->get('tabel_idkualitas_periksa');
        $table_kualitas_dudul = $request->get('tabel_idkualitas_dudul');    
        $table_idrencana = $request->get('tabel_idrencana');
        $table_jumlahrencana = $request->get('tabel_rencanajumlah');
        $table_tanggal = $request->get('tabel_tanggal');
        $table_keterangan = $request->get('tabel_keterangan');
        $table_hasilid = $request->get('tabel_hasilid');
        $table_hasiljumlah = $request->get('tabel_hasiljumlah');
        $table_hasilsatuan = $request->get('tabel_hasilsatuan');

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

            for($i=0; $i < count($table_barang); $i++){                
                $barang['id_barangkeluar']=$id;
                $barang['id_barang']=$table_barang[$i];
                $barang['id_gudang']=$table_gudang[$i];
                $barang['jumlah'] = $table_jumlah[$i];
                $barang['satuan_jumlah'] = $table_satuan[$i];
                
                $barang['warna'] = $table_warna[$i];
                $barang['id_proses_sebelum'] = '17';
                if($table_sebelum[$i] != '' || $table_sebelum[$i] != 0){
                $barang['id_proses_sebelum'] = $table_sebelum[$i];
                }
                $barang['id_motif_barang'] = $table_corak[$i];
                $barang['id_motif'] = $table_motif[$i];
                $barang['id_kualitas'] = $table_kualitas[$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_kualitas_proses[$i];
                $barang['id_kualitas_periksa'] = $table_kualitas_periksa[$i];
                $barang['id_kualitas_dudul'] = $table_kualitas_dudul[$i];
                $barang['id_perubahansatuan'] = $table_idrencana[$i];
                $barang['jumlah_rencana'] = $table_jumlahrencana[$i];
                $barang['keterangan'] = $table_keterangan[$i];
                $barang['tanggal_proses'] = tgl_full($table_tanggal[$i], '2');

                $id_barang = DB::table('pro_barangkeluar_barang')->insertGetId($barang);

                $hasil['id_barangkeluar'] = $id;
                $hasil['id_barangkeluar_barang'] = $id_barang;
                $hasil['id_satuanhasil'] = $table_hasilsatuan[$i];
                $hasil['hasil'] = $table_hasiljumlah[$i];

                DB::table('pro_barangkeluar_hasil')->insert($hasil);
            }
        }else{
            DB::table('pro_barangkeluar')->where('id_barangkeluar',$id_barangkeluar)->update($data);
           
            $id_del = array(); $id_delhasil = array();
            $id_store = array(); $id_storehasil = array();

            $d_barang = DB::table('pro_barangkeluar_barang')->where('id_barangkeluar',$id_barangkeluar);
            $d_hasil = DB::table('pro_barangkeluar_hasil')->where('id_barangkeluar',$id_barangkeluar);

            foreach($d_barang->get() as $d){
                $id_store[] = $d->id;
            }

            foreach($d_hasil->get() as $d){
                $id_storehasil[] = $d->id_keluarhasil;
            }

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

            foreach($id_storehasil as $d){
                if(count($table_hasilid) > 0){
                    if(!in_array($d, $table_hasilid)){
                        $id_delhasil[] = $d;
                    }
                }else{
                    $id_delhasil = $d;
                }
            }

            for($i=0; $i < count($table_barang); $i++){                
                $barang['id_barangkeluar']=$id_barangkeluar;
                $barang['id_barang']=$table_barang[$i];
                $barang['id_gudang']=$table_gudang[$i];
                $barang['jumlah'] = $table_jumlah[$i];
                $barang['satuan_jumlah'] = $table_satuan[$i];
                
                $barang['id_proses_sebelum'] = '17';
                if($table_sebelum[$i] != '' || $table_sebelum[$i] != 0){
                $barang['id_proses_sebelum'] = $table_sebelum[$i];
                }
                $barang['warna'] = $table_warna[$i];
                $barang['id_motif_barang'] = $table_corak[$i];
                $barang['id_motif'] = $table_motif[$i];
                $barang['id_kualitas'] = $table_kualitas[$i];
                $barang['id_kualitas_dudul'] = $table_kualitas_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_kualitas_proses[$i];
                $barang['id_kualitas_periksa'] = $table_kualitas_periksa[$i];
                $barang['id_perubahansatuan'] = $table_idrencana[$i];
                $barang['jumlah_rencana'] = $table_jumlahrencana[$i];
                $barang['keterangan'] = $table_keterangan[$i];
                $barang['tanggal_proses'] = tgl_full($table_tanggal[$i], '2');

                
                $hasil['id_barangkeluar'] = $id_barangkeluar;
                
                $hasil['id_satuanhasil'] = $table_hasilsatuan[$i];
                $hasil['hasil'] = $table_hasiljumlah[$i];

                if($table_id[$i] == ''){
    //                echo 'tambah';

                    $id_barang = DB::table('pro_barangkeluar_barang')->insertGetId($barang);
                    $hasil['id_barangkeluar_barang'] = $id_barang;
                    DB::table('pro_barangkeluar_hasil')->insert($hasil);
                }else if($table_id[$i] != ''){
    //                echo 'edit';
                    DB::table('pro_barangkeluar_barang')->where(array('id' => $table_id[$i]))->update($barang);

                    if($table_hasilid[$i] == ''){
                        $hasil['id_barangkeluar_barang'] = $table_id[$i];
                        DB::table('pro_barangkeluar_hasil')->insert($hasil);
                    }else{
                        DB::table('pro_barangkeluar_hasil')->where(array('id_keluarhasil' => $table_hasilid[$i]))->update($hasil);
                    }
                }
                
            }

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

            if(count($id_delhasil) > 0){
                DB::table('pro_barangkeluar_hasil')->whereIn('id_keluarhasil', $id_delhasil)->delete();
            }

        }
        
         return redirect('barangkeluar_atm');

        

        }else{
            return redirect()->back()->withInput($request->all)->withErrors($request->all);
        }
                
    }

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

        DB::table('pro_barangkeluar')->where(array('id_barangkeluar' => $id))->delete();
        DB::table('pro_barangkeluar_barang')->where(array('id_barangkeluar' => $id))->delete();
        DB::table('pro_barangkeluar_hasil')->where(array('id_barangkeluar' => $id))->delete();
    }


    // Fungsi Lainnya

    function data($data = array()){
        if(count($data) > 0){
            $store['id_barangkeluar'] = $data->id_barangkeluar;
            $store['id_rekanan'] = $data->id_rekanan;
            $store['id_proses'] = $data->id_proses;
            $store['id_pegawai'] = $data->id_pegawai;
            $store['id_mesin'] = $data->id_mesin;
            $store['id_workorder_barang'] = $data->id_workorder_barang;
            $store['tanggal'] = $data->tanggal;
            $store['tanggal_perkiraan'] = $data->tanggal_perkiraan;
            $store['id_workorder'] = $data->id_workorder;
            $store['status'] = $data->status;
            $store['jenis'] = $data->id_lokasi;
            $store['noAuto'] = $data->noAuto;
        }else{
            $store['id_barangkeluar'] = '';
            $store['id_rekanan'] = '';
            $store['id_proses'] = '';
            $store['id_pegawai'] = '';
            $store['id_mesin'] = '';
            $store['id_workorder_barang'] = '';
            $store['id_workorder'] = '';
            $store['tanggal'] = '';
            $store['tanggal_perkiraan'] = '';
            $store['status'] = '';
            $store['jenis'] = '';
            $store['noAuto'] = '';
        }

        return $store;
    }

    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_workorder', '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_before($id){
        $d_data = DB::table("ms_proses")->where('id_proses', '<',$id)->orderBy('id_proses', 'desc')->first();
        $ret = 0;
        if($d_data->parent != 0){
            $ret = $d_data->id_proses;
        }
        return $d_data->id_proses;
    }

    function get_aksi($id_barang, $status){
        $d_data = DB::table('pro_barangkeluar_barang')->where('id_barangkeluar', $id_barang)->pluck('status')->toArray();
        switch ($status) {
            case '0':
                if(in_array(1, $d_data)){
                    $aksi = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangkeluar_atm/detail/'.$id_barang).'"><i class="icon-eye"></i></a> <a class="btn bg-warning-400" type="button" href="'.url('barangkeluar_atm/edit/'.$id_barang).'"><i class="icon-pencil"></i> </a> <button class="btn bg-danger-800" type="button" onclick="hapus('.$id_barang.')"><i class="icon-eraser"></i></button> ';
                }else{
                    $aksi = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangkeluar_atm/detail/'.$id_barang).'"><i class="icon-eye"></i> </a> <a class="btn bg-warning-400" type="button" href="'.url('barangkeluar_atm/edit/'.$id_barang).'"><i class="icon-pencil"></i> </a> <button class="btn bg-danger-800" type="button" onclick="hapus('.$id_barang.')"><i class="icon-eraser"></i></button> ';
                }

                //<a class="btn bg-success-400" type="button" target="_blank" href="'.url('barangkeluar_atm/cetak/'.$id_barang).'"><i class="icon-printer"></i> </a></div>
                
                break;
            case '1':
                $aksi = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangkeluar_atm/detail/'.$id_barang).'"><i class="icon-eye"></i> </a> <a class="btn bg-warning-400" type="button" href="'.url('barangkeluar_atm/edit/'.$id_barang).'"><i class="icon-pencil"></i></a> <button class="btn bg-danger-800" type="button" onclick="hapus('.$id_barang.')"><i class="icon-eraser"></i></button> ';
            break;
            case '2':
                $aksi = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangkeluar_atm/detail/'.$id_barang).'"><i class="icon-eye"></i></a> <a class="btn bg-warning-400" type="button" href="'.url('barangkeluar_atm/edit/'.$id_barang).'"><i class="icon-pencil"></i> </a> ';
            break;
            case '3':
                $aksi = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangkeluar_atm/detail/'.$id_barang).'"><i class="icon-eye"></i></a> <a class="btn bg-warning-400" type="button" href="'.url('barangkeluar_atm/edit/'.$id_barang).'"><i class="icon-pencil"></i> </a> ';
            break;
            default:
                $aksi = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangkeluar_atm/detail/'.$id_barang).'"><i class="icon-eye"></i> </a></div>';
            break;
        }

            $aksi .= '<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_atm/cetak/'.$id_barang).'?kertas=1'.'" target="_blank"><i class="icon-printer4"></i> Cetak Sedang</a></li> <li><a href="'.url('barangkeluar_atm/cetak/'.$id_barang).'?kertas=0'.'" target="_blank"><i class="icon-printer4"></i> Cetak Kecil</a></li> </ul></div>';

        return $aksi;
    }

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

        if($data['id'] != ''){
            $d_data = DB::table('pro_barangkeluar')->where('id_barangkeluar', $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')->where('id_barangkeluar', $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');
        $data['data'] = Barangkeluar::with(['getPegawai'],['getRekanan'],['getProses'],['getMesin'])->find($id);
        $data['kelompok'] = Rekanan::with(['dataKelompok'])->find($data['data']['id_rekanan']);

        $req = $request->all();

        $data['detail'] = DB::table('view_edit_keluar as v')->leftjoin('pro_barangkeluar_hasil AS pbh', function($join){
            $join->on('v.id', 'pbh.id_barangkeluar')->on('v.id_barangkeluar_barang', 'pbh.id_barangkeluar_barang');
        })->leftjoin('ms_satuan AS ms', 'pbh.id_satuanhasil', 'ms.id_satuan')->where('v.id', $id)->orderby('v.id_barangkeluar_barang', 'asc')->select(DB::raw("v.*, CASE WHEN pbh.id_keluarhasil IS NULL THEN '' ELSE pbh.id_keluarhasil END AS id_keluarhasil, CASE WHEN pbh.id_satuanhasil IS NULL OR pbh.id_satuanhasil = 0 THEN '' ELSE pbh.id_satuanhasil END AS id_satuanhasil, CASE WHEN ms.id_satuan IS NULL THEN '' ELSE ms.nama_satuan END AS nama_satuanhasil, CASE WHEN pbh.hasil IS NULL OR pbh.id_satuanhasil = 0 THEN '' ELSE pbh.hasil END AS hasil")); barangkeluar_barang($id)->get();

        $data['jum_hasil'] = 0;
        $arr = array();

        if($data['detail']->count() > 0){
            foreach($data['detail']->get() as $d){
                if($d->hasil != ''){
                    $data['jum_hasil'] += $d->hasil;
                }else{

                }
                /*$arr[] = array('id_keluarhasil' => $d->id_keluarhasil,
                                'id_satuanhasil' => $d->id_satuanhasil,
                                'hasil' => $d->hasil); */
            }
        }

        $data['detail'] = $data['detail']->get();
        if(isset($req['kertas'])){
            if($req['kertas'] == 0){
                $view = "BarangkeluarCetakKecil";
            }else{
                $view = "BarangkeluarCetak";
            }
        }else{
            $view = "BarangkeluarCetak";
        }

        /*$data['detail'] = DB::table('view_edit_getedit_keluar')->where('id_barangkeluar',$id)->get();*/
    //    $data['detail'] = DB::table('view_edit_keluar')->where('id', $id)->orderby('id_barangkeluar_barang', 'asc')->get(); barangkeluar_barang($id)->get();
        $html = view('produksi.cetak.'.$view.'')->with('data',$data);
        return response($html)->header('Content-Type', 'application/pdf');
    }

}
