<?php

namespace App\Http\Controllers\produksi;

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 Illuminate\Support\Facades\Session;

class BarangkeluarController 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'));
    }
    
    public function index(){
        return view('produksi.BarangkeluarList');
    }

    public function tambah(){
    	$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')->where('parent','=','0')->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','ASC')->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['spk'] = $this->get_spk(1);
        $data['workorder'] = $this->get_spk(2);
    	$data['gudang'] = DB::table('ms_gudang')->get();
        $data['pegawai'] = DB::table('ms_pegawai')->get();
        $data['kualitas'] = DB::table('ms_kualitas')->get();
        $data['warna'] = DB::table('ms_warnakomposisi')->get();
        $data['satuan'] = DB::table('ms_satuan')->orderby('nama_satuan', 'asc')->get();
        $data['produksi'] = DB::table('ms_lokasi_produksi')->get();
    	$data['data'] = $this->data(array());
    	return view('produksi.BarangkeluarTambah')->with('data',$data);
    }

    public function get_jum(Request $request){
        $barang = $request->get('barang');
        $gudang = $request->get('gudang');
    }


   function edit($id){
        $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')->where([['mp.nama_proses','<>','Pengemasan Satuan'],['mp.nama_proses','<>','Pengemasan Sepuluh'],['mp.nama_proses','<>','Pengemasan Seratus'],['mp.nama_proses','<>','Seri'],['mp.nama_proses','<>','Plat'],['mp.nama_proses','<>','Packing Karung']])->orderBy('mp.id_proses')->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','ASC')->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['barang'] = DB::table('ms_barang as mb')->join('ms_kategori_barang as mkb','mb.id_kb','mkb.id_kb')->join('ms_kategori_barang as mkbb','mkb.induk_kb','mkbb.id_kb')->select('mb.*','mkbb.nama as nama_kategori','mkb.nama as nama_subkategori')->get();
        $data['gudang'] = DB::table('ms_gudang')->get();
        $data['spk'] = $this->get_spk(1);
        $data['workorder'] = $this->get_spk(2);
        $data['pegawai'] = DB::table('ms_pegawai')->get();
        $data['kualitas'] = DB::table('ms_kualitas')->get();
        $data['warna'] = DB::table('ms_warnakomposisi')->get();
        $data['satuan'] = DB::table('ms_satuan')->get();
        $data['produksi'] = DB::table('ms_lokasi_produksi')->get();
        $data['data'] = $this->data($d_data);

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

    function detail($id){
        $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')->where('parent','=','0')->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.nama_proses','<>','Pengemasan Satuan'],['mp.nama_proses','<>','Pengemasan Sepuluh'],['mp.nama_proses','<>','Pengemasan Seratus'],['mp.nama_proses','<>','Seri'],['mp.nama_proses','<>','Plat'],['mp.nama_proses','<>','Packing Karung']])->orderBy('mp.id_proses')->get();
        $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 mkbb','mkb.induk_kb','mkbb.id_kb')->select('mb.*','mkbb.nama as nama_kategori','mkb.nama as nama_subkategori')->get();
        $data['gudang'] = DB::table('ms_gudang')->get();
        $data['spk'] = $this->get_spk(1);
        $data['workorder'] = $this->get_spk(2);
        $data['pegawai'] = DB::table('ms_pegawai')->get();
        $data['kualitas'] = DB::table('ms_kualitas')->get();
        $data['warna'] = DB::table('ms_warnakomposisi')->get();
        $data['satuan'] = DB::table('ms_satuan')->get();
        $data['produksi'] = DB::table('ms_lokasi_produksi')->get();
        $data['data'] = $this->data($d_data);

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

    function get_edit(Request $request){
        $id = request()->get('id');
        
        $d_data = DB::table('view_edit_getedit_keluar')->orderBy('id', 'asc')->where('id_barangkeluar', '=', $id)->where('id_kemasansatuan','=','');
        if($d_data->count() > 0){
            foreach($d_data->get() as $d){ 
                $satuan_jumlah = '';
                $id_satuan_jumlah = "";
                if($d->satuan_jumlah != null || $d->satuan_jumlah != ''){
                    $satuan_jumlah = $d->satuan_jumlah;
                    $id_satuan_jumlah = $d->id_satuan_jumlah;
                }
                $satuan_jumlah_konv = '';
                $id_satuan_konv = '';
                if($d->satuan_jumlah_konv != null || $d->satuan_jumlah_konv != ''){
                    $satuan_jumlah_konv = $d->satuan_jumlah_konv;
                    $id_satuan_konv = $d->id_satuan_konv;
                }
                $id_komposisi = '';
                $nama_komposisi = '';
                $inisial_komposisi = '';
                if($d->id_komposisi != null || $d->id_komposisi != ''){
                    $id_komposisi = $d->id_komposisi;
                    $nama_komposisi = $d->nama_komposisi;
                    $inisial_komposisi = $d->inisial_komposisi;
                }

                $nama_dudulan = '';
                $id_dudulan = '';
                if($d->id_kualitas_dudul != null || $d->id_kualitas_dudul != ''){
                    if($d->id_kualitas_dudul == '2'){
                        $nama_dudulan = 'Jelek';
                    }elseif($d->id_kualitas_dudul == '1'){
                        $nama_dudulan = 'Baik';
                    }else{
                        $nama_dudulan = "";
                    }
                    $id_dudulan = $d->id_kualitas_dudul;
                }

                $nama_kualitas = '';
                $id_kualitas = '';
                if($d->id_kualitas != null || $d->id_kualitas != ''){
                    $id_kualitas = $d->id_kualitas;
                    $nama_kualitas = $d->nama_kualitas;
                }

                $nama_label = '';
                $id_label = '';
                if($d->id_label != null || $d->id_label != ''){
                    $nama_label = $d->nama_label;
                    $id_label = '';
                }

                $nama_kemasan = '';
                $id_kemasan = '';
                if($d->id_kemasan != null || $d->id_kemasan != ''){
                    $nama_kemasan = $d->nama_kemasan;
                    $id_kemasan = $id_kemasan;
                }
                
                $nama_kemasansatuan = '';
                $id_kemasansatuan = '';
                if($d->id_kemasansatuan != null || $d->id_kemasansatuan != ''){
                    $nama_kemasansatuan = $d->nama_kemasansatuan;
                    $id_kemasansatuan = $d->id_kemasansatuan;
                }

                $nama_kemasansepuluh = '';
                $id_kemasansepuluh = '';
                if($d->id_kemasansepuluh != null || $d->id_kemasansepuluh != ''){
                    $nama_kemasansepuluh = $d->nama_kemasansepuluh;
                    $id_kemasansepuluh = $d->id_kemasansepuluh;
                }

                $nama_kemasanseratus = '';
                $id_kemasanseratus = '';
                if($d->id_kemasanseratus != null || $d->id_kemasanseratus != ''){
                    $nama_kemasanseratus = $d->nama_kemasanseratus;
                    $id_kemasanseratus = $d->id_kemasanseratus;
                }

                $nama_cap = '';
                $id_cap = '';
                if($d->id_cap != null || $d->id_cap != '' || $d->id_cap != 0){
                    $nama_cap = $d->nama_cap;
                    $id_cap = $d->id_cap;
                }

                $nama_kualitas_proses = '';
                $id_kualitas_proses = '';
                if($d->id_kualitas_proses != null || $d->id_kualitas_proses != '' || $d->id_kualitas_proses != 0){
                    $nama_kualitas_proses = $d->nama_kualitas_proses;
                    $id_kualitas_proses = $d->id_kualitas_proses;
                }

                $nama_kualitas_periksa = '';
                $id_kualitas_periksa = '';
                if($d->id_kualitas_periksa != null || $d->id_kualitas_periksa != '' || $d->id_kualitas_periksa != 0){
                    $nama_kualitas_periksa = $d->nama_kualitas_periksa;
                    $id_kualitas_periksa = $d->id_kualitas_periksa;
                }

                $id_perubahansatuan = '';
                $nama_perubahansatuan = '';
                $jumlah_rencana = '';
                if($d->id_perubahansatuan != null || $d->id_perubahansatuan != '' || $d->id_perubahansatuan != 0){
                    $id_perubahansatuan = $d->id_perubahansatuan;
                    $nama_perubahansatuan = $d->nama_perubahansatuan;
                    $jumlah_rencana = $d->jumlah_rencana;
                }

                $keterangan = '';
                if($d->keterangan != null || $d->keterangan != '' || $d->keterangan != 0){
                    $keterangan =$d->keterangan;
                }

                $arr[] = array('id' => $d->id,
                                'id_barangkeluar' => $d->id_barangkeluar,
                                'id_barang' => $d->id_barang,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang,
                                'namabarang_kategori' => $d->namabarang_kategori.' - '.$d->namabarang_subkategori,
                                'id_gudang' => $d->id_gudang,
                                'nama_gudang' => $d->nama_gudang,
                                'jumlah' => $d->jumlah,
                                'jumlah_konv' => $d->jumlah_konversi,
                                'satuan_jumlah' => $satuan_jumlah,
                                'id_satuan_jumlah' => $id_satuan_jumlah,
                                'satuan_jumlah_konv' => $satuan_jumlah_konv,
                                'id_komposisi' => $id_komposisi,
                                'nama_komposisi' => $nama_komposisi,
                                'inisial_komposisi' => $inisial_komposisi,
                                'id_proses_sebelum' => $d->id_proses_sebelumnya,
                                'id_motif' =>$d->id_motif,
                                'id_corak' => $d->id_corak,
                                'id_kualitas_dudul' => $id_dudulan,
                                'id_kualitas' => $id_kualitas,
                                'id_label' => $id_label,
                                'id_kemasan' => $id_kemasan,
                                'id_kemasansatuan' => $id_kemasansatuan,
                                'id_kemasansepuluh' => $id_kemasansepuluh,
                                'id_kemasanseratus' => $id_kemasanseratus,
                                'id_cap' => $id_cap,
                                'id_kualitas_proses' => $id_kualitas_proses,
                                'id_kualitas_periksa' => $id_kualitas_periksa,
                                'nama_motif' => $d->nama_motif,
                                'nama_corak' => $d->nama_corak,
                                'nama_kualitas' => $nama_kualitas,
                                'nama_kualitas_dudul' => $nama_dudulan,
                                'nama_label' => $nama_label,
                                'nama_kemasan' => $nama_kemasan,
                                'nama_kemasansatuan' => $nama_kemasansatuan,
                                'nama_kemasansepuluh' => $nama_kemasansepuluh,
                                'nama_kemasanseratus' => $nama_kemasanseratus,
                                'nama_cap' => $nama_cap,
                                'nama_kualitas_proses' => $nama_kualitas_proses,
                                'nama_kualitas_periksa' => $nama_kualitas_periksa,
                                'id_spk' => $d->id_workorder,
                                'id_workorder' => $d->id_workorder_barang,
                                'no_spk' => $d->no_workorder,
                                'no_workorder' => $d->no_workorder_barang,
                                'id_perubahansatuan' => $id_perubahansatuan,
                                'nama_perubahansatuan' => $nama_perubahansatuan,
                                'jumlah_rencana' => $jumlah_rencana,
                                'keterangan' => $keterangan,
                                'tanggal_proses' => tgl_full($d->tanggal_proses, ''));
            }
        }else{
            $arr = array();
        }

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

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

        $d_data = DB::table('ms_konversi AS mk')->join('ms_satuan AS ms', 'mk.satuan_awal', 'ms.id_satuan')->join('ms_satuan AS ms2', 'mk.satuan_akhir', 'ms2.id_satuan')->select(DB::raw('mk.* , ms.nama_satuan ns_awal, ms.satuan s_awal, ms2.nama_satuan ns_akhir, ms2.satuan s_akhir'))->where('kode', '=', $id);
        if($d_data->count() > 0){
            foreach($d_data->get() as $d){
                $arr[] = array('id_konversi' => $d->id_konversi,
                            'satuan_awal' => $d->ns_awal,
                            'satuan_akhir' => $d->ns_akhir,
                            'nilai_awal' => $d->nilai_awal,
                            'nilai_akhir' => $d->nilai_akhir);
            }
        }else{
            $d_data = DB::table('ms_barang AS mb')->join('ms_satuan AS ms', 'mb.id_satuan', 'ms.id_satuan')->where('mb.id', $id)->select(DB::raw('mb.*, ms.nama_satuan AS satuan'));
            if($d_data->count() > 0){
                $arr = array('satuan_awal' => $d_data->first()->satuan);
            }else{
                $arr = array('satuan_awal' => '');
            }
        }

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

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

        $d_data = DB::table('ms_konversi AS mk')->join('ms_satuan AS ms', 'mk.satuan_awal', 'ms.id_satuan')->join('ms_satuan AS ms2', 'mk.satuan_akhir', 'ms2.id_satuan')->select(DB::raw('mk.* , ms.nama_satuan ns_awal, ms.satuan s_awal, ms2.nama_satuan ns_akhir, ms2.satuan s_akhir'))->where('kode', '=', $id);
        if($d_data->count() > 0){
            foreach($d_data->get() as $d){
                $arr[] = array('id_konversi' => $d->id_konversi,
                            'satuan_awal' => $d->ns_awal,
                            'satuan_akhir' => $d->ns_akhir,
                            'nilai_awal' => $d->nilai_awal,
                            'nilai_akhir' => $d->nilai_akhir);
            }
        }else{
            /*$d_data = DB::table('ms_barang AS mb')->join('ms_satuan AS ms', 'mb.id_satuan', 'ms.id_satuan')->where('mb.id', $id)->select(DB::raw('mb.*, ms.nama_satuan AS satuan'));
            if($d_data->count() > 0){
                $arr = array('satuan_awal' => $d_data->first()->satuan);
            }else{
                $arr = array('satuan_awal' => '');
            }*/
            $arr = array('id_konversi' => '',
                            'satuan_awal' => '',
                            'satuan_akhir' => '',
                            'nilai_awal' => '',
                            'nilai_akhir' => 0);
        }

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

    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_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;
            $store['noAuto_ext'] = $data->noAuto_ext;
    	}else{
            $store['id_barangkeluar'] = '';
    		$store['id_rekanan'] = '';
    		$store['id_proses'] = '';
            $store['id_pegawai'] = '';
            $store['id_workorder_barang'] = '';
            $store['id_workorder'] = '';
            $store['tanggal'] = '';
            $store['tanggal_perkiraan'] = '';
            $store['status'] = '';
            $store['jenis'] = '';
            $store['noAuto'] = '';
            $store['noAuto_ext'] = '';
    	}

        return $store;


    }

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

        $id_barangkeluar = $request->get('id_barangkeluar');
        $data['tanggal'] = date_format(date_create($request->get('tanggal')),'Y-m-d');
        
        if($request->get('rekanan')!=""){
        $data['id_rekanan'] = $request->get('rekanan');
        $data['id_pegawai'] = "0";   
        }else{
        $data['id_rekanan'] = "0";
        $data['id_pegawai'] = $request->get('pegawai');
        }
        $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');


        $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_jumlah_konv = $request->get('tabel_jumlah_konv');*/
        $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 = '';
        // if($request->get('proses') >= 41){
        // $table_kualitas_dudul = $request->get('tabel_idkualitas_dudul');    
        // }
        $table_kualitas_dudul = $request->get('tabel_idkualitas_dudul');    
        $table_idrencana = $request->get('tabel_idrencana');
        $table_jumlahrencana = $request->get('tabel_rencanajumlah');
        $table_keterangan = $request->get('tabel_keterangan');
        $table_tanggal = $request->get('tabel_tanggal');


        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['jumlah_konversi'] = $table_jumlah_konv[$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];
                /*if($request->get('proses') >= 41){
                $barang['id_kualitas_dudul'] = $table_kualitas_dudul[$i];
                }else{*/
                $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');

                
                DB::table('pro_barangkeluar_barang')->insert($barang);
                
                /*$cek_warna = DB::table('ms_warnakomposisi as mw')->join('ms_warnakomposisi_barang as mwb','mw.id_komposisi','mwb.id_komposisi')->join('ms_barang as mb','mwb.id_barang','mb.id')->leftjoin('ms_konversi as mk','mb.id','mk.kode')->leftjoin('ms_satuan as ms','mk.satuan_awal','ms.id_satuan')->leftJoin('ms_satuan as ms2','mk.satuan_akhir','ms2.id_satuan')->where('mw.id_komposisi','=',$table_warna[$i])->select(DB::raw('mwb.*,ms.nama_satuan as satuan_jumlah, ms.id_satuan as idsatuan_jumlah , ms2.nama_satuan as satuan_konv, ms2.id_satuan as idsatuan_konv'));
                if(count($cek_warna) > 0 && $request->get('proses') == '18' || $request->get('proses') == '22' || $request->get('proses') == '32'){
                foreach($cek_warna->get() as $w){
                    if($w->id_komposisi == $barang['warna']){
                    $warna['id_barangkeluar'] = $id;
                    $warna['id_barang'] = $w->id_barang;
                    $warna['id_gudang'] = '4';
                    $warna['jumlah'] = $barang['jumlah']*$w->jum_konv;
                    $warna['satuan_jumlah'] = $w->idsatuan_konv;
                    $warna['warna'] = '';
                    $warna['id_proses_sebelum'] = $barang['id_proses_sebelum'];
                    $warna['id_motif_barang'] = '';
                    $warna['id_motif'] = '';
                    $warna['id_kualitas'] ='';
                    $warna['id_kualitas_dudul'] = '';
                    $warna['id_label'] = '';
                    $warna['id_kemasan'] = '';
                    $warna['id_kemasansatuan'] = '';
                    $warna['id_kemasansepuluh'] = '';
                    $warna['id_kemasanseratus'] = '';
                    $warna['id_cap'] = '';
                    $warna['id_kualitas_proses'] = '';
                    $warna['id_kualitas_periksa'] = '';
                    DB::table('pro_barangkeluar_barang')->insert($warna);
                        }
                    }
                }*/
            }
            
        }else{
            DB::table('pro_barangkeluar')->where('id_barangkeluar',$id_barangkeluar)->update($data);
           
            $id_del = array();
            $id_store = array();
            $d_barang = DB::table('pro_barangkeluar_barang')->where('id_barangkeluar',$id_barangkeluar);
            foreach($d_barang->get() as $d){
                $id_store[] = $d->id;
            }

            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_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['jumlah_konversi'] = $table_jumlah_konv[$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');
                
                if($table_id[$i] == ''){
                    DB::table('pro_barangkeluar_barang')->insert($barang);
                }else if($table_id != ''){
                    $where = 
                    DB::table('pro_barangkeluar_barang')->where(array('id' => $table_id[$i]))->update($barang);
                }
                
            }

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

        }
        
         return redirect('barangkeluar');

        

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

    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_rencana = $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 != ''){
                $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'";   
            }

            $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_rencana != '' || $jumlah_rencana != 0){
                $jumlah_rencana .= " AND B.jumlah_rencana = '$jumlah_rencana'";
            }





            if($id_proses == '37'){
                $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'";
            }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'){
                    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='' ";
                    }
                }
            
           


        }
        $not_where = '';
    //    $not_where = "AND id_kemasansatuan = ''";
        /*print_r($id_workorder_barang);*/
        $user_gudang = Session::get('id_gudang');
        $PDO = DB::connection('mysql')->getPdo();
        /*if($id_proses == '18'){
        $billingStmt = $PDO->prepare("SELECT B.*, mkb.nama kategori, mkb2.nama sub_kategori
                            FROM (
                                SELECT A.*, sum(masuk - keluar) as stok
                                FROM (
                                  SELECT * FROM view_penerimaan
                                 UNION ALL
                                 SELECT * FROM view_transfer 
                                 UNION ALL
                                 SELECT * FROM view_keluar
                                 UNION ALL
                                 SELECT * FROM view_masuk
                                 UNION ALL
                                 SELECT * FROM view_penjualan
                                 UNION ALL
                                 SELECT * FROM view_opname
                                ) A 
                                GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi
                            ) 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
                            $where $proses");
    
    }else{*/
         $billingStmt = $PDO->prepare("SELECT B.*, mkb.nama kategori, mkb2.nama sub_kategori
                                     FROM (
                                         ".base_stok_user($user_gudang)."
                                     ) 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
                                     $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 $id_perubahansatuan $jumlah_perubahan $not_where");
    /*}*/
        $billingStmt->execute();
        $usersBills = $billingStmt->fetchAll((\PDO::FETCH_ASSOC));

        return response()->json($usersBills);

    //     print_r($where."\n". $proses2."\n". $warna."\n". $corak."\n". $id_workorder."\n". $id_workorder_barang."\n". $motif."\n". $kualitas."\n". $dudulan."\n".$label."\n". $kemasan."\n". $kemasan_satuan."\n". $kemasan_sepuluh."\n". $kemasan_seratus."\n". $cap."\n". $kualitas_proses."\n". $kualitas_periksa."\n". $id_perubahansatuan."\n". $jumlah_perubahan."\n");
        // print_r($d_proses);
        

       
    }

    function get_barang2(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');


            $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 = ''";
            }
            
            $warna = "";
            if($id_komposisi != '' && $id_proses2 != ''){
                $warna .= " AND B.id_komposisi = '$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'";
            }

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

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

            if($id_proses == '37'){
                $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'";
            }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'){
                if($workorder != 0 || $workorder != ''){
                   $id_workorder_barang = " AND B.id_workorder_barang='$workorder'";
                 }
                if($id_proses2 == 0 || $id_proses2 == ''){
                   $id_workorder_barang = '';
                }
                }
            
           
                $not_where = "AND id_kemasansatuan = ''";
                

        }
        
        
        /*print_r($id_workorder_barang);*/

        $PDO = DB::connection('mysql')->getPdo();
        /*if($id_proses == '18'){
        $billingStmt = $PDO->prepare("SELECT B.*, mkb.nama kategori, mkb2.nama sub_kategori
                            FROM (
                                SELECT A.*, sum(masuk - keluar) as stok
                                FROM (
                                  SELECT * FROM view_penerimaan
                                 UNION ALL
                                 SELECT * FROM view_transfer 
                                 UNION ALL
                                 SELECT * FROM view_keluar
                                 UNION ALL
                                 SELECT * FROM view_masuk
                                 UNION ALL
                                 SELECT * FROM view_penjualan
                                 UNION ALL
                                 SELECT * FROM view_opname
                                ) A 
                                GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi
                            ) 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
                            $where $proses");
    
    }else{*/
         $billingStmt = $PDO->prepare("SELECT B.*, mkb.nama kategori, mkb2.nama sub_kategori
                                     FROM (
                                         -- SELECT A.*, sum(masuk - keluar) as stok
                                         --  -- A.id_barang, A.kode_barang, A.nama_barang, A.jumlah, A.masuk, A.keluar, A.id_gudang, A.nama_gudang, 
                                         --  --   A.tanggal, A.id_proses, A.nama_proses, A.id_komposisi, A.nama_komposisi, A.id_satuan, 
                                         --  --   A.nama_satuan, A.id_corak,A.nama_corak, A.id_motif, A.nama_motif,
                                         --  --   A.id_workorder , A.no_workorder, A.id_workorder_barang, A.no_workorder_barang, A.id_kualitas, A.nama_kualitas, A.id_kualitas_dudul, A.kualitas_dudul ,sum(masuk - keluar) as stok,
                                         --  --   A.id_label, A.nama_label, A.id_kemasan, A.nama_kemasan, A.id_kemasansatuan, A.nama_kemasansatuan, A.id_kemasansepuluh, A.nama_kemasansepuluh, A.id_kemasanseratus, A.nama_kemasanseratus 
                                         -- FROM (
                                         --  SELECT * FROM view_penerimaan
                                         --  UNION ALL
                                         --  SELECT * FROM view_transfer 
                                         --  UNION ALL
                                         --  SELECT * FROM view_keluar
                                         --  UNION ALL
                                         --  SELECT * FROM view_masuk
                                         --  UNION ALL
                                         --  SELECT * FROM view_penjualan
                                         --  UNION ALL
                                         --  SELECT * FROM view_opname
                                         -- ) A 
                                         -- GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_corak, A.id_motif,A.id_workorder,A.id_workorder_barang, A.id_kualitas, A.id_kualitas_dudul, A.id_label, A.id_kemasan, A.id_kemasansatuan, A.id_kemasansepuluh, A.id_kemasanseratus
                                         ".base_stok()."
                                     ) 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
                                     $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 $id_perubahansatuan $jumlah_perubahan $not_where");
    /*}*/
        $billingStmt->execute();
        $usersBills = $billingStmt->fetchAll((\PDO::FETCH_ASSOC));
       return response()->json($usersBills);
        

       
    }

    function get_barang_proses(Request $request){
        $id_gudang = $request->get('id_gudang');
        $id_proses = $request->get('id_proses');
        $id_proses2 = $request->get('id_proses2');
        $spk = $request->get('spk');
        $workorder = $request->get('workorder');
        $id_komposisi= $request->get("id_komposisi");
        $id_corak = $request->get('id_corak');

        $where = "";
        if($id_gudang != '' && $id_proses == '18'){
            $where .= "WHERE B.id_gudang ='4' AND B.id_proses = ''";
        }else if($id_gudang != '' && $id_proses > '18'){
            $where .= "WHERE B.id_gudang ='$id_gudang'";
        }

        $proses = "";
        if($id_proses2 != ''){
            $proses .= "AND B.id_proses ='$id_proses2'";
        }

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

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

        if($id_proses == '37'){
                $cek_spk_tenun = DB::table('pem_workorder_barang as pwb')->join('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'";
            }else{
            $id_workorder = "";
            if($spk != 0 || $spk != ''){
                $id_workorder = " AND B.id_workorder='$spk'";
            }
        }

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

        $not_where = "AND id_kemasansatuan = ''";

        $user_gudang = Session::get('id_gudang');
        $PDO = DB::connection('mysql')->getPdo();
        /*$d_data = DB::table("ms_barang");
        $arr = "";
        if($d_data->count() > 0){
            foreach($d_data->get() as $d){
               $arr += $d->id.",";
            }
        }
        $billingStmt = $PDO->prepare("SELECT B.*, mkb.nama kategori, mkb2.nama sub_kategori
                            FROM (
                                SELECT A.*, sum(masuk - keluar) as stok
                                FROM (
                                  SELECT * FROM view_penerimaan
                                 UNION ALL
                                 SELECT * FROM view_transfer 
                                 UNION ALL
                                 SELECT * FROM view_keluar
                                 UNION ALL
                                 SELECT * FROM view_masuk
                                 UNION ALL
                                 SELECT * FROM view_penjualan
                                 UNION ALL
                                 SELECT * FROM view_opname
                                ) A 
                                GROUP BY A.id_barang, A.id_gudang, A.id_proses,A.id_komposisi
                            ) 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
                            where id_barang in ($arr)                             
                            ");
        $billingStmt = $PDO->prepare("SELECT 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 
                                    FROM ms_barang mb join ms_kategori_barang as mkb on mb.id_kb=mkb.id_kb 
                                    JOIN ms_kategori_barang as mkb2 on mkb.induk_kb=mkb2.id_kb 
                                    JOIN ms_satuan as ms on mb.id_satuan=ms.id_satuan");*/

        /*if($id_proses == '18'){
        $billingStmt = $PDO->prepare("SELECT B.*, mkb.nama kategori, mkb2.nama sub_kategori
                            FROM (
                                SELECT A.*, sum(masuk - keluar) as stok
                                FROM (
                                  SELECT * FROM view_penerimaan
                                 UNION ALL
                                 SELECT * FROM view_transfer 
                                 UNION ALL
                                 SELECT * FROM view_keluar
                                 UNION ALL
                                 SELECT * FROM view_masuk
                                 UNION ALL
                                 SELECT * FROM view_penjualan
                                 UNION ALL
                                 SELECT * FROM view_opname
                                ) A 
                                GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi
                            ) 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
                            $where AND B.stok > 0");
    
    }else{*/
         $billingStmt = $PDO->prepare("SELECT B.*, mkb.nama kategori, mkb2.nama sub_kategori
                                     FROM (
                                         ".base_stok_user($user_gudang)."
                                     ) 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
                                     $where $proses $warna $corak $id_workorder $id_workorder_barang $not_where AND B.stok > 0");
    /*}*/
        $billingStmt->execute();
        $usersBills = $billingStmt->fetchAll((\PDO::FETCH_ASSOC));

        return response()->json($usersBills);
        /*$d_data = 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'));

        if($d_data->count() > 0){
            foreach($d_data->orderBy('nama_kategori', 'asc')->orderBy('nama_sub', 'asc')->orderBy('kode_barang', 'asc')->orderBy('nama_barang', 'asc')->get() as $d){
                $arr[] = array('id_barang' => $d->id,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang,
                                'id_kategori' => $d->id_kategori,
                                'kategori' => $d->nama_kategori,
                                'id_sub' => $d->id_sub,
                                'nama_sub' => $d->nama_sub,
                                'satuan' => $d->nama_satuan);
            }
        }else{
            $arr = array();
        }

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

    function get_barang_prosesedit(Request $request){
        $id = $request->get('id');
        $id_gudang = $request->get('id_gudang');
        $id_proses = $request->get('id_proses');
        $id_proses2 = $request->get('id_proses2');
        $spk = $request->get('spk');
        $workorder = $request->get('workorder');
        $id_komposisi= $request->get("id_komposisi");
        $id_corak = $request->get('id_corak');

        $where = "";
        if($id_gudang != '' && $id_proses == '18'){
            $where .= "WHERE B.id_gudang ='4' AND B.id_proses = ''";
        }else if($id_gudang != '' && $id_proses > '18'){
            $where .= "WHERE B.id_gudang ='$id_gudang'";
        }

        $proses = "";
        if($id_proses2 != ''){
            $proses .= "AND B.id_proses ='$id_proses2'";
        }

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

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

        if($id_proses == '37'){
                $cek_spk_tenun = DB::table('pem_workorder_barang as pwb')->join('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'";
            }else{
            $id_workorder = "";
            if($spk != 0 || $spk != ''){
                $id_workorder = " AND B.id_workorder='$spk'";
            }
        }

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

        $not_where = "AND id_kemasansatuan = ''";


        $PDO = DB::connection('mysql')->getPdo();
       /* if($id_proses == '18'){
        $billingStmt = $PDO->prepare("SELECT B.*, mkb.nama kategori, mkb2.nama sub_kategori
                            FROM (
                                SELECT A.*, sum(masuk - keluar) as stok
                                FROM (
                                  SELECT * FROM view_penerimaan
                                 UNION ALL
                                 SELECT * FROM view_transfer 
                                 UNION ALL
                                 SELECT * FROM view_keluar
                                 UNION ALL
                                 SELECT * FROM view_masuk
                                 UNION ALL
                                 SELECT * FROM view_penjualan
                                 UNION ALL
                                 SELECT * FROM view_opname
                                ) A 
                                GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi
                            ) 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
                            $where AND B.stok > 0");
    
    }else{*/
         $billingStmt = $PDO->prepare("SELECT B.*, mkb.nama kategori, mkb2.nama sub_kategori
                                     FROM (
                                         SELECT * FROM view_edit_keluar
                                     ) 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
                                     where id_barangkeluar_barang = '$id' $not_where
                                     /*$where $proses $warna $corak $id_workorder $id_workorder_barang AND B.stok > 0*/ ");
    /*}*/
        $billingStmt->execute();
        $usersBills = $billingStmt->fetchAll((\PDO::FETCH_ASSOC));

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

    function get_warna(Request $request){
        $id_order = $request->get('id_order');
        $tipe = $request->get('tipe');
        $where = "";
        if($tipe != ""){
            $where = "where pwb.id_workorder_barang='$id_order'";
               

        $PDO = DB::connection('mysql')->getPdo();


        $billingStmt = $PDO->prepare("SELECT pwb.*, mw.nama as warna_dasar, mw.inisial as inisial_dasar, 
                                    mw2.nama as warna_streep, mw2.inisial as inisial_streep
                                    FROM pem_workorder_barang pwb join ms_warnakomposisi mw on pwb.dasar=mw.id_komposisi
                                    join ms_warnakomposisi mw2 on pwb.streep=mw2.id_komposisi $where");
        $billingStmt->execute();
        $usersBills = $billingStmt->fetchAll((\PDO::FETCH_ASSOC));
        }else{
        $usersBills = array();    
        }
        return response()->json($usersBills);

        
    }

    function get_komposisi(Request $request){
        $id = request()->get('id');
        $d_data = DB::table('ms_warnakomposisi_barang as mwb')->join('ms_barang as mb','mwb.id_barang','mb.id')->leftJoin('ms_konversi as mk','mwb.id_barang','mk.kode')->leftJoin('ms_satuan as ms','mb.id_satuan','ms.id_satuan')->leftJoin('ms_satuan as ms2','mk.satuan_akhir','ms2.id_satuan')->select(DB::raw("mwb.*, mb.kode_barang, mb.nama_barang, ms.nama_satuan as satuan_jumlah, ms2.nama_satuan as satuan_jumlah_konv"))->where('mwb.id_komposisi', '=', $id);
        $d_gudang = DB::table('ms_gudang')->orderBy('id_gudang','ASC')->first();
        if($d_data->count() > 0){
            foreach($d_data->get() as $d){
                $satuan_jumlah = '';
                if($d->satuan_jumlah != null || $d->satuan_jumlah != ''){
                    $satuan_jumlah = $d->satuan_jumlah;
                }
                $satuan_jumlah_konv = '';
                if($d->satuan_jumlah_konv != null || $d->satuan_jumlah_konv != ''){
                    $satuan_jumlah_konv = $d->satuan_jumlah_konv;
                }
                $arr[] = array('id' => $d->id_komposisi_barang,
                                'id_barang' => $d->id_barang,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang,
                                'id_gudang' => $d_gudang->id_gudang,
                                'nama_gudang' => $d_gudang->nama_gudang,
                                'jumlah' => $d->jumlah,
                                'jumlah_konv' => $d->jumlah_konv,
                                'satuan_jumlah' => $satuan_jumlah,
                                'satuan_jumlah_konv' => $satuan_jumlah_konv,
                                'jum_satuan' => $d->jum_satuan);
            }
        }else{
            $arr = array();
        }

        return response()->json($arr);
    }
    
    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();
    }

    function cek_proses(Request $request){
        $proses = $request->get('id_proses');
        $workorder = $request->get('id_workorder');
        $d_data = DB::table('pro_barangkeluar as pb')->join('ms_proses as mp','pb.id_proses','mp.id_proses')->where(array('pb.id_proses'=>$proses,'pb.id_workorder_barang'=>$workorder))->select('pb.*','mp.nama_proses');
        if($d_data->count() > 0){
            foreach($d_data->get() as $d){
                $arr[] = array('id_barangkeluar'=>$d->id_barangkeluar,
                                'nama_proses' =>$d->nama_proses, 
                                'status' => $d->status);
            }
        }else{
            $arr = array();
        }
        return response()->json($arr);
    }

    function cek_proses_spk(Request $request){
        $proses = $request->get('id_proses');
        $workorder = $request->get('id_workorder');
        $d_data = DB::table('pro_barangkeluar as pb')->join('ms_proses as mp','pb.id_proses','mp.id_proses')->where(array('pb.id_proses'=>$proses,'pb.id_workorder'=>$workorder,'pb.id_workorder_barang'=>''))->select('pb.*','mp.nama_proses');
        if($d_data->count() > 0){
            foreach($d_data->get() as $d){
                $arr[] = array('id_barangkeluar'=>$d->id_barangkeluar,
                                'nama_proses' =>$d->nama_proses, 
                                'status' => $d->status);
            }
        }else{
            $arr = array();
        }
        return response()->json($arr);

    }

    function get_proses_detail(Request $request){
        $id = request()->get('id_proses');
        $d_data = DB::table('ms_maping_barang AS mmb')->join('ms_maping AS mm', 'mmb.id_maping', 'mm.id_maping')->join('ms_proses as mp', 'mm.id_proses', 'mp.id_proses')->join('ms_barang as mb','mmb.id_barang','mb.id')->select(DB::raw("mmb.*, mp.nama_proses, mb.nama_barang, mb.kode_barang"))->where('mm.id_proses', '=', $id)->where('status','0');

        if($d_data->count() > 0){
            foreach($d_data->get() as $d){
                $arr[] = array('id' => $d->id,
                                'id_maping' => $d->id_maping,
                                'id_barang' => $d->id_barang,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang);
            }
        }else{
            $arr = array();
        }

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

    }

    function get_workorder_detail(Request $request){
        $id = request()->get('id_workorder');
        // $d_data = DB::table('pem_workorder_ket AS pwk')->join('ms_barang as mb','pwk.id_barang','mb.id')->select(DB::raw("pwk.*, mb.nama_barang, mb.kode_barang"))->where('pwk.id_workorder', '=', $id);

       DB::statement(DB::raw('set @rownum=0'));
        $d_data = DB::table('view_workorderbarang')->select(DB::raw('*, @rownum  := @rownum  + 1 AS rownum'))->where('id_workorder_barang', '=', $id);
         if($d_data->count() > 0){
            foreach($d_data->get() as $d){
                $arr[] = array('id' => $d->rownum,
                                'id_workorder' => $d->id_workorder,
                                'id_barang' => $d->id_barang,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang,
                                'id_warna' => $d->id_komposisi,
                                'nama_warna' => $d->nama_komposisi,
                                'inisial_warna' => $d->inisial);
            }
        }else{
            $arr = array();
        }

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

    }


    function get_spk_detail(Request $request){
        $id = request()->get('id_workorder');
        DB::statement(DB::raw('set @rownum=0'));
        /*$d_data = DB::table('view_workorderbarang')->select(DB::raw('*, @rownum  := @rownum  + 1 AS rownum'))->where('id_workorder', '=', $id);*/
        $d_data = DB::table('pem_workorder_ket as pwk')->join('ms_barang as mb','pwk.id_barang','mb.id')->select(DB::raw('*, @rownum  := @rownum  + 1 AS rownum'))->where('id_workorder', '=', $id);
         if($d_data->count() > 0){
            foreach($d_data->get() as $d){
                $arr[] = array('id' => $d->rownum,
                                'id_workorder' => $d->id_workorder,
                                'id_barang' => $d->id_barang,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang);
            }
        }else{
            $arr = array();
        }

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

    }

    
    function get_data(){
        DB::statement(DB::raw('set @rownum=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('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, @rownum  := @rownum  + 1 AS rownum'));

         $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('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, @rownum  := @rownum  + 1 AS rownum'))->orderBy('pb.tanggal', 'desc')->orderBy('pb.status', 'asc')->where([['mp.nama_proses','<>','Pengemasan Satuan'],['mp.nama_proses','<>','Pengemasan Sepuluh'],['mp.nama_proses','<>','Pengemasan Seratus'],['mp.nama_proses','<>','Seri'],['mp.nama_proses','<>','Plat'],['mp.nama_proses','<>','Packing Karung']])->where('pb.id_lokasi', '1');

         if($this->session['id_proses'] != 0 && $this->session['id_gudang'] != 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('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, @rownum  := @rownum  + 1 AS rownum'))->orderBy('pb.tanggal', 'desc')->orderBy('pb.status', 'asc')->where([['mp.nama_proses','<>','Pengemasan Satuan'],['mp.nama_proses','<>','Pengemasan Sepuluh'],['mp.nama_proses','<>','Pengemasan Seratus'],['mp.nama_proses','<>','Seri'],['mp.nama_proses','<>','Plat'],['mp.nama_proses','<>','Packing Karung']])->where('pb.id_lokasi', '1')->whereIn('pb.id_proses',[$this->session['id_proses']]);
         }
         
        
        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_pegawai;
                    $stat_pekerja = 'Pegawai';
                }
                // +-
                $aksi = '';
                $status = '';
                if($d->status == '0'){
                    $aksi = '<div class="btn-group"><a class="btn bg-warning-400" type="button" href="'.url('barangkeluar/edit/'.$d->id_barangkeluar).'"><i class="icon-pencil"></i> </a>
                            <button class="btn bg-danger-800" type="button" onclick="hapus('.$d->id_barangkeluar.')"><i class="icon-eraser"></i></button>
                            <a class="btn bg-info-400" type="button" href="'.url('barangkeluar/detail/'.$d->id_barangkeluar).'"><i class="icon-eye"></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/print/'.$d->id_barangkeluar).'?kertas=1'.'" target="_blank"><i class="icon-printer4"></i> Cetak Sedang</a></li>
                                        <li><a href="'.url('barangkeluar/print/'.$d->id_barangkeluar).'?kertas=0'.'" target="_blank"><i class="icon-printer4"></i> Cetak Kecil</a></li>
                                    </ul>
                            </div>';
                    $status = status_bon(0);
                }else if($d->status == 1){
                    $aksi = '<div class="btn-group"><a class="btn bg-warning-400" type="button" href="'.url('barangkeluar/edit/'.$d->id_barangkeluar).'"><i class="icon-pencil"></i> </a>
                            <button class="btn bg-danger-800" type="button" onclick="hapus('.$d->id_barangkeluar.')"><i class="icon-eraser"></i></button>
                            <a class="btn bg-info-400" type="button" href="'.url('barangkeluar/detail/'.$d->id_barangkeluar).'"><i class="icon-eye"></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/print/'.$d->id_barangkeluar).'?kertas=1'.'" target="_blank"><i class="icon-printer4"></i> Cetak Sedang</a></li>
                                        <li><a href="'.url('barangkeluar/print/'.$d->id_barangkeluar).'?kertas=0'.'" target="_blank"><i class="icon-printer4"></i> Cetak Kecil</a></li>
                                    </ul>
                            </div>';
                    $status = status_bon(1);
                }else if($d->status == 3 && $d->id_proses == 37){
                    $aksi = '<div class="btn-group"><a class="btn bg-warning-400" type="button" href="'.url('barangkeluar/edit/'.$d->id_barangkeluar).'"><i class="icon-pencil"></i> </a>
                            <a class="btn bg-info-400" type="button" href="'.url('barangkeluar/detail/'.$d->id_barangkeluar).'"><i class="icon-eye"></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/print/'.$d->id_barangkeluar).'?kertas=1'.'" target="_blank"><i class="icon-printer4"></i> Cetak Sedang</a></li>
                                        <li><a href="'.url('barangkeluar/print/'.$d->id_barangkeluar).'?kertas=0'.'" target="_blank"><i class="icon-printer4"></i> Cetak Kecil</a></li>
                                    </ul>
                            </div>';
                    $status = status_bon(3);
                }
                else{
                    $aksi = '<div class="btn-group"><a class="btn bg-warning-400" type="button" href="'.url('barangkeluar/edit/'.$d->id_barangkeluar).'"><i class="icon-pencil"></i> </a>
                            <a class="btn bg-info-400" type="button" href="'.url('barangkeluar/detail/'.$d->id_barangkeluar).'"><i class="icon-eye"></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/print/'.$d->id_barangkeluar).'?kertas=1'.'" target="_blank"><i class="icon-printer4"></i> Cetak Sedang</a></li>
                                        <li><a href="'.url('barangkeluar/print/'.$d->id_barangkeluar).'?kertas=0'.'" target="_blank"><i class="icon-printer4"></i> Cetak Kecil</a></li>
                                    </ul>
                            </div>';

                            // <a class="btn bg-success-400" type="button" target="_blank" href="'.url('barangkeluar/print/'.$d->id_barangkeluar).'"><i class="icon-printer"></i> </a>
                    $status = status_bon(2);
                }

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

                /*$aksi = '<a class="btn bg-warning-400" type="button" href="'.url('barangkeluar/edit/'.$d->id_barangkeluar).'"><i class="icon-pencil"></i> </a>
                            <button class="btn bg-danger-800" type="button" onclick="hapus('.$d->id_barangkeluar.')"><i class="icon-eraser"></i></button>';*/
                $data['data'][] = array('id_barangkeluar' => $d->id_barangkeluar,
                                        'no_workorder' => $nomor.' '.$no_ket,
                                        'nomor' => $d->noAuto.$d->noAuto_ext,
                                        '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,
                                        'aksi' => $aksi,
                                        'rownum' => $d->rownum);
            }
        }else{
            $data['data'] = array();
        }

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

    }    

    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_spk($jenis = ''){
        $arr = array();
        $arr2 = 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_nomor(Request $request){
        $data = $request->all();
        $arr = array();

        if($data['id'] != ''){
            $d_data = DB::table('pro_barangkeluar')->where('id_barangkeluar', $data['id'])->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"));
            $d_data = DB::table('pro_barangkeluar')->unionAll($d_kemasan)->select(DB::raw("noAuto, noAuto_ext"))->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);
    }


    //--- Perubahan Satuan
    function get_perubahansatuan(Request $request){
        $id_motif = '';
        $id_motif_barang = '';
        if($request->get('motif') != ''){
        $id_motif = $request->get('motif');
        }
        if($request->get('motif_barang') != ''){
        $id_motif_barang = $request->get('motif_barang');
        }
        
        $data['id_proses'] = $request->get('proses');
        $data['id_motif'] = $id_motif;
        $data['id_motif_barang'] = $id_motif_barang;
        $data['id_lokasi'] = $request->get('lokasi');

        //-- Jumlah Hasil
        /*if($request->get('jumlah_hasil') != ''){*/
        $jumlah_hasil = $request->get('jumlah_hasil');
        $data['id_satuanperubahan'] = $request->get('satuan_hasil');
        /*}else{*/
        $jumlah = $request->get('jumlah');
        $data['id_satuanawal'] = $request->get('satuan');
        /*}*/
        $cek = DB::table('ms_perubahansatuan as mps')->join('ms_satuan as ms','mps.id_satuanawal','ms.id_satuan')->join('ms_satuan as ms2','mps.id_satuanperubahan','ms2.id_satuan')->select(DB::raw('mps.*,ms.nama_satuan as nama_satuanawal, ms2.nama_satuan as nama_satuanperubahan'))->where($data);
        if($cek->count() > 0){
            $arr['data'] = $cek->first();
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

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

    function print_barangkeluar(Request $request, $id){
    	require('fpdf181/Mc_table.php');
    	
        $req = $request->all();

        $data['data'] = Barangkeluar::with(['getPegawai'],['getRekanan'],['getProses'],['getOrder'])->find($id);
    	
        $data['detail'] = DB::table('view_edit_keluar')->where('id', $id)->orderby('id_barangkeluar_barang', 'asc')->get(); barangkeluar_barang($id)->get();



        if(isset($req['kertas'])){
            if($req['kertas'] == 0){
                $view = "BarangkeluarCetakKecil";
            }else{
                $view = "BarangkeluarCetak";
            }
        }else{
            $view = "BarangkeluarCetak";
        }
    	$html = view('produksi.cetak.'.$view.'')->with('data',$data);
        return response($html)->header('Content-Type', 'application/pdf');
    }

}
