<?php

namespace App\Http\Controllers\produksi;

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

class KembalibarangController extends Controller{



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

        $this->session = array('id_gudang'=>Session::get('id_gudang'),'id_proses'=>Session::get('id_proses'),'id_lokasi'=>Session::get('id_lokasi'), 'kode_user' => Session::get('kode_user'));
    }
    
	function index(){
		return view('produksi.KembalibarangList');
	}

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

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

        $data['gudang'] = DB::table('ms_gudang')->orderBy('nama_gudang', 'asc')->get();
        $data['kualitas'] = DB::table('ms_kualitas')->get();
        $data['satuan'] = DB::table('ms_satuan')->orderBy('nama_satuan', 'asc')->get();
        $data['warna'] = DB::table('ms_warnakomposisi')->OrderBy('nama', 'asc')->get();
        
        $data['kualitas_proses'] = DB::table('ms_kualitas')->orderBy('nama_kualitas', 'asc')->where('jenis', '2')->get();
        $data['kualitas_tenun'] = DB::table('ms_kualitas')->orderBy('nama_kualitas', 'asc')->where('jenis', '1')->get();
        $data['kualitas_periksa'] = DB::table('ms_kualitas')->orderBy('nama_kualitas', 'asc')->where('jenis', '3')->get();

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

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

        $data['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.parent', '!=', '0')->orderBy('mp.id_proses','ASC')->get();

        $data['bon_bahan'] = get_rawquery(get_barangkeluar_proses());
        $data['form_data'] = $this->form_data(array());
        return view('produksi.KembalibarangTambah')->with('data', $data);
        
    }

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

        $data['gudang'] = DB::table('ms_gudang')->orderBy('nama_gudang', 'asc')->get();
        $data['kualitas'] = DB::table('ms_kualitas')->get();
        $data['satuan'] = DB::table('ms_satuan')->orderBy('nama_satuan', 'asc')->get();
        $data['warna'] = DB::table('ms_warnakomposisi')->OrderBy('nama', 'asc')->get();
        
        $data['kualitas_proses'] = DB::table('ms_kualitas')->orderBy('nama_kualitas', 'asc')->where('jenis', '2')->get();
        $data['kualitas_tenun'] = DB::table('ms_kualitas')->orderBy('nama_kualitas', 'asc')->where('jenis', '1')->get();
        $data['kualitas_periksa'] = DB::table('ms_kualitas')->orderBy('nama_kualitas', 'asc')->where('jenis', '3')->get();

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

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

        $data['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.parent', '!=', '0')->orderBy('mp.id_proses','ASC')->get();

        $data['bon_bahan'] = get_rawquery(get_barangkeluar_proses());
        $data['form_data'] = $this->form_data(DB::table('pro_kembali')->where('id_kembali', $id)->first());

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

    function detail($id){
        $data['induk'] = get_rawquery(get_kembali()." WHERE pk.id_kembali = '$id'");

        $data['detail'] = DB::table('view_edit_kembali as v')->join("pro_kembali_barang AS p", "v.id_kembali_barang", "p.id_kembali_barang")->leftjoin("ms_satuan AS ms", "p.id_satuanhasil", "ms.id_satuan")->where('v.id_kembali', $id)->select(DB::raw("v.*, p.id, p.id_satuanhasil, p.hasil, CASE WHEN ms.id_satuan IS NULL THEN '' ELSE ms.nama_satuan END AS nama_satuanhasil"))->get();

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

    function simpan(Request $request){
        $req = $request->all();

        $id_kembali = $req['id_kembalibarang'];
        $data['tanggal'] = date('Y-m-d');
        $data['tanggal_full'] = tgl_full(date('Y-m-d'), '5');
        $data['id_barangkeluar'] = $req['store_idbarangkeluar'];
        $data['jenis'] = $req['store_jenis'];
        $data['id_user'] = $this->session['kode_user'];

        $tabel_idtable = $req['tabel_idtable'];
        
        $tabel_idbarangkeluar = $req['tabel_idbarangkeluar'];
        $tabel_jenis = $req['tabel_jenis'];
        $tabel_idbarang = $req['tabel_idbarang'];
        $tabel_idgudang = $req['tabel_idgudang'];
        $tabel_proses = $req['tabel_proses'];
        $tabel_jumlah = $req['tabel_jumlah'];
        $tabel_perubahanjumlah = $req['tabel_perubahanjumlah'];
        
        $tabel_idkomposisi = $req['tabel_idkomposisi'];
        $tabel_idsatuan = $req['tabel_idsatuan'];
        $tabel_idsatuan_perubahan = $req['tabel_idsatuan_perubahan'];
        $tabel_idcorak = $req['tabel_idcorak'];
        $tabel_idmotif = $req['tabel_idmotif'];
        $tabel_idkualitas = $req['tabel_idkualitas'];
        $tabel_iddudul = $req['tabel_iddudul'];
        $tabel_idlabel = $req['tabel_idlabel'];
        $tabel_idkemasan = $req['tabel_idkemasan'];
        $tabel_idkemasansatuan = $req['tabel_idkemasansatuan'];
        $tabel_idkemasansepuluh = $req['tabel_idkemasansepuluh'];
        $tabel_idkemasanseratus = $req['tabel_idkemasanseratus'];
        $tabel_spk = $req['tabel_spk'];
        $tabel_workorder = $req['tabel_workorder'];
        $tabel_idcap = $req['tabel_idcap'];
        $tabel_idkualitasproses = $req['tabel_idkualitasproses'];
        $tabel_idkualitasperiksa = $req['tabel_idkualitasperiksa'];
        $tabel_keterangan = $req['tabel_keterangan'];
        $tabel_jumlahhasil = $req['tabel_jumhasil'];
        $tabel_satuanhasil = $req['tabel_sathasil'];

        
        if($id_kembali == ''){
           $id = DB::table('pro_kembali')->insertGetId($data);

            for($i = 0; $i < count($tabel_idtable); $i++){
                $insert['id_kembali'] = $id;
                $insert['id_barang'] = $tabel_idbarang[$i];
                $insert['id'] = $tabel_idbarangkeluar[$i];
                $insert['jumlah'] = $tabel_jumlah[$i];
                $insert['id_gudang'] = $tabel_idgudang[$i];
                $insert['id_satuan'] = $tabel_idsatuan[$i];
                $insert['id_proses'] = $tabel_proses[$i];
                $insert['id_komposisi'] = $tabel_idkomposisi[$i];
                $insert['id_motif'] = $tabel_idmotif[$i];
                $insert['id_motif_barang'] = $tabel_idcorak[$i];
                $insert['id_label'] = $tabel_idlabel[$i];
                $insert['id_kualitas'] = $tabel_idkualitas[$i];
                $insert['id_kualitas_dudul'] = $tabel_iddudul[$i];
                $insert['id_kemasan'] = $tabel_idkemasan[$i];
                $insert['id_kemasansatuan'] = $tabel_idkemasansatuan[$i];
                $insert['id_kemasansepuluh'] = $tabel_idkemasansepuluh[$i];
                $insert['id_kemasanseratus'] = $tabel_idkemasanseratus[$i];
                $insert['id_workorder'] = $tabel_spk[$i];
                $insert['id_workorder_barang'] = $tabel_workorder[$i];
                $insert['id_cap'] = $tabel_idcap[$i];
                $insert['id_kualitas_proses'] = $tabel_idkualitasproses[$i];
                $insert['id_kualitas_periksa'] = $tabel_idkualitasperiksa[$i];
                $insert['id_perubahansatuan'] = $tabel_idsatuan_perubahan[$i];
                $insert['jumlah_rencana'] = $tabel_perubahanjumlah[$i];
                $insert['hasil'] = $tabel_jumlahhasil[$i];
                $insert['id_satuanhasil'] = $tabel_satuanhasil[$i];

                DB::table('pro_kembali_barang')->insert($insert);
            }
            
        }else{
            $id_del = array();
            $id_store = array();
            
            $d_barang = DB::table('pro_kembali_barang')->where('id_kembali',$id_kembali);
            foreach($d_barang->get() as $d){
                $id_store[] = $d->id_kembali_barang;
            }

            foreach($id_store as $d){
                if(!in_array($d, $tabel_idtable)){
                    $id_del[] = $d;
                }
            }

            // print_r($id_store);
            // print_r("\n");
            // print_r($id_del);

            for($i = 0; $i < count($tabel_idtable); $i++){
                $insert['id_kembali'] = $id_kembali;
                $insert['id_barang'] = $tabel_idbarang[$i];
                $insert['id'] = $tabel_idbarangkeluar[$i];
                $insert['jumlah'] = $tabel_jumlah[$i];
                $insert['id_gudang'] = $tabel_idgudang[$i];
                $insert['id_satuan'] = $tabel_idsatuan[$i];
                $insert['id_proses'] = $tabel_proses[$i];
                $insert['id_komposisi'] = $tabel_idkomposisi[$i];
                $insert['id_motif'] = $tabel_idmotif[$i];
                $insert['id_motif_barang'] = $tabel_idcorak[$i];
                $insert['id_label'] = $tabel_idlabel[$i];
                $insert['id_kualitas'] = $tabel_idkualitas[$i];
                $insert['id_kualitas_dudul'] = $tabel_iddudul[$i];
                $insert['id_kemasan'] = $tabel_idkemasan[$i];
                $insert['id_kemasansatuan'] = $tabel_idkemasansatuan[$i];
                $insert['id_kemasansepuluh'] = $tabel_idkemasansepuluh[$i];
                $insert['id_kemasanseratus'] = $tabel_idkemasanseratus[$i];
                $insert['id_workorder'] = $tabel_spk[$i];
                $insert['id_workorder_barang'] = $tabel_workorder[$i];
                $insert['id_cap'] = $tabel_idcap[$i];
                $insert['id_kualitas_proses'] = $tabel_idkualitasproses[$i];
                $insert['id_kualitas_periksa'] = $tabel_idkualitasperiksa[$i];
                $insert['id_perubahansatuan'] = $tabel_idsatuan_perubahan[$i];
                $insert['jumlah_rencana'] = $tabel_perubahanjumlah[$i];
                $insert['hasil'] = $tabel_jumlahhasil[$i];
                $insert['id_satuanhasil'] = $tabel_satuanhasil[$i];

                if($tabel_idtable[$i] == ''){
                    DB::table('pro_kembali_barang')->insert($insert);
                }else if($tabel_idtable[$i] != ''){
                    DB::table('pro_kembali_barang')->where('id_kembali_barang', $tabel_idtable[$i])->update($insert);
                }
                
            }

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


        return redirect('kembalibarang');
    }


    function get_barang(Request $request){
        $req = $request->all();

        $id_keluar = $req['id_keluar'];
        $jenis = $req['jenis'];

        //    $d_data = DB::table('view_edit_masuk as vp')->join('pro_barangmasuk_barang as pbb', 'vp.id', 'pbb.id')->join('pro_barangmasuk as pb', 'pb.id_barangmasuk', 'pbb.id_barangmasuk')->where('pb.id_barangkeluar', $id_keluar)->select('vp.*')->get();
    //    $d_data = DB::table('view_edit_keluar as v')->join('ms_barang as mb', 'v.id_barang', 'mb.id')->leftJoin('ms_kategori_barang as mk', 'mb.id_kb', 'mk.id_kb')->leftJoin('ms_kategori_barang as mk2', 'mk.induk_kb', 'mk2.id_kb')->where('v.id', $id_keluar)->select(DB::raw("v.*, mk2.id_kb, mk2.nama sub_kategori, mk.id_kb, mk.nama kategori, v.id_barangkeluar_barang id_bb"))->get();

        $d_data = get_rawquery(stok_barang_kembali($id_keluar));
        $d_data = barang_byproses($d_data, 4, '');

        if($jenis == '2'){
        //    $d_data = DB::table('view_edit_barangkeluar_kemasan as v')->join('ms_barang as mb', 'v.id_barang', 'mb.id')->leftJoin('ms_kategori_barang as mk', 'mb.id_kb', 'mk.id_kb')->leftJoin('ms_kategori_barang as mk2', 'mk.induk_kb', 'mk2.id_kb')->where('v.id_barangkeluar_kemasan', $id_keluar)->select(DB::raw("v.*, mk2.id_kb, mk2.nama sub_kategori, mk.id_kb, mk.nama kategori, v.id_barangkeluar_kb id_bb"))->get();
            $d_data = get_rawquery(stok_barang_kembali_kel($id_keluar));
            $d_data = barang_byproses($d_data, 4, '');
        }

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

    function get_workorder(Request $request){
        $req = $request->all();

        $id_keluar = $req['id'];
        $jenis = $req['jenis'];

        $data = DB::table('view_edit_keluar')->where('id', $id_keluar)->where('id_workorder_barang', '!=', '')->groupBy('id_workorder_barang')->get();

        if($jenis == '2'){
            $data = DB::table('view_edit_barangkeluar_kemasan')->where('id_barangkeluar_kemasan', $id_keluar)->where('id_workorder_barang', '!=', '')->groupBy('id_workorder_barang')->get();    
        }
        
        return response()->json($data);
    }

    function get_data(){
        $id_lokasi = Session::get('id_lokasi');
        $where = "";
        $where_arr = array();
        if($id_lokasi != 0){
            $where_arr[] = " pbk.id_lokasi = ".$id_lokasi." ";
        }

        $where_arr[] = "is_all = 0";

        $where = " WHERE ".implode(" AND ", $where_arr)." GROUP BY pk.id_kembali";

        $arr = get_rawquery(get_kembali($where));
        $data = array();
        foreach($arr as $a){

            if($a['id_pegawai'] != ''){
                $pro = $a['nama_pegawai']."( Pegawai )";
            }else if($a['id_rekanan'] != ''){
                $pro = $a['nama_rekanan']."( Rekanan )";
            }else if($a['id_mesin'] != ''){
                $pro = $a['nama_mesin']."( Mesin )";
            }else{
                $pro = "";
            }

            $data[] = array('id_kembali' => $a['id_kembali'],
                            'nomor' => $a['noAuto'].$a['noAuto_ext'],
                            'tanggal' => tgl_full($a['tanggal_keluar'], ''),
                            'pemroses' => $pro,
                            'status' => $this->get_status($a['status']),
                            'proses' => $a['nama_proses'],
                            'aksi' => $this->get_aksi($a['id_kembali'], $a['status']));
        }

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

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

        DB::table('pro_kembali')->where(array('id_kembali' => $id))->delete();
        DB::table('pro_kembali_barang')->where(array('id_kembali' => $id))->delete();
    }

    function get_aksi($id, $status){
        $d_data = DB::table('pro_kembali_barang')->where('id_kembali', $id)->pluck('status')->toArray();
        switch ($status) {
            case '0':
            if(in_array(1, $d_data)){
                $ret = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('kembalibarang/detail').'/'.$id.'"><i class="icon-notification2"></i></a> <a class="btn bg-warning-400" type="button" href="'.url('kembalibarang/edit/').'/'.$id.'"><i class="icon-pencil3"></i></a></div>'; //hapus edit
            }else{
                $ret = '<div class="btn-group"> <a class="btn bg-info-400" type="button" href="'.url('kembalibarang/detail').'/'.$id.'"><i class="icon-notification2"></i></a> <a class="btn bg-warning-400" type="button" href="'.url('kembalibarang/edit/').'/'.$id.'"><i class="icon-pencil3"></i></a> <button class="btn bg-danger-800" type="button" onclick="hapus('.$id.')"><i class="icon-eraser"></i></button></div>';
            }
                
                break;
            case '1':
                $ret = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('kembalibarang/detail').'/'.$id.'"><i class="icon-notification2"></i></a>  <a class="btn bg-warning-400" type="button" href="'.url('kembalibarang/edit/').'/'.$id.'"><i class="icon-pencil3"></i></a> <button class="btn bg-danger-800" type="button" onclick="hapus('.$id.')"><i class="icon-eraser"></i></button> </div>';
                break;
            default:
                $ret = '';
                break;
        }


        return $ret;
    }

    function get_status($status){
        switch ($status) {
            case '0':
                $d = '<label class="label bg-warning-400">Belum Diterima</label>';
                break;
            case '1':
                $d = '<label class="label bg-success-400">Diterima</label>';
                break;
            default:
                $d = '<label class="label bg-warning-400"></label>';
                break;
        }

        return $d;
    }

    function form_data($data = array()){
        if(count($data) > 0){
            $store['id_kembali'] = $data->id_kembali;
            $store['id_barangkeluar'] = $data->id_barangkeluar;
            $store['jenis'] = $data->jenis;
        }else{
            $store['id_kembali'] = '';
            $store['id_barangkeluar'] = '';
            $store['jenis'] = '';
        }

        return $store;
    }

    function get_edit(Request $request){
        $req = $request->all();
        $id = $req['id'];
        // if($id == ''){
        //     $id = $req['id'];
        // }

        $d_data = DB::table('view_edit_kembali as v')->join('pro_kembali_barang as p', 'v.id_kembali_barang', 'p.id_kembali_barang')->select(DB::raw("v.*, p.id, p.id_satuanhasil, p.hasil"))->where('v.id_kembali', $id);

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

    function get_perubahansatuan(Request $request){
        $id = $request->get('id_barangkeluar');
        $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 mpe','pb.id_pegawai','mpe.id_pegawai')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->leftjoin('pem_workorder as pw', 'pb.id_workorder', 'pw.id_workorder')->leftjoin('pem_workorder_barang as pwb', 'pwb.id_workorder_barang', 'pb.id_workorder_barang')->select(DB::raw('pb.*, mp.nama_proses AS nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai as nama_pegawai, mrk.nama nama_kelompok, pw.nomor no_workorder, pwb.no_workorder no_workorder_barang'))->where('pb.id_barangkeluar', $id)->first();

        $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'] = $d_data->id_proses;
        $data['id_motif'] = $id_motif;
        $data['id_motif_barang'] = $id_motif_barang;
        $data['id_lokasi'] = $d_data->id_lokasi;

        //-- Jumlah Hasil
        $jumlah_hasil = $request->get('jumlah_hasil');
        $data['id_satuanperubahan'] = $request->get('satuan_hasil');
        $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);
        
    }

}