<?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 KembalibarangkeseluruhanController 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.KembalibarangkeseluruhanList');
	}

	function get_data(){
		$id_lokasi = $this->session['id_lokasi'];
        $id_gudang = $this->session['id_gudang'];

        $d_data = DB::table('pro_kembali AS pk')->leftjoin("ms_lokasi_produksi AS mlp", "pk.id_lokasi", "mlp.id_lokasi")->whereRaw("is_all = 1")->select(DB::raw("pk.*, mlp.nama_lokasi"));

        if($id_lokasi != 0){
        	$d_data->where('pk.id_lokasi', $id_lokasi);
        }

        if($d_data->count() > 0){
        	foreach($d_data->get() as $d){
        		$arr[] = array("id_kembali" => $d->id_kembali,
        					"tanggal" => tgl_full($d->tanggal, ''),
        					"nama_lokasi" => $d->nama_lokasi,
        					"keterangan" => $d->keterangan,
        					"status" => $this->get_status($d->status),
        					"aksi" => '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('kembaliseluruh/detail').'/'.$d->id_kembali.'"><i class="icon-notification2"></i></a> <a class="btn bg-warning-400" type="button" href="'.url('kembaliseluruh/edit/').'/'.$d->id_kembali.'"><i class="icon-pencil3"></i></a><button class="btn bg-danger-800" type="button" onclick="hapus($(this))" id_hasil="'.$d->id_kembali.'" jenis="0"><i class="icon-eraser"></i></button></div>');
        	}
        }else{
        	$arr = array();
        }

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

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

    	$data['lokasi'] = DB::table('ms_lokasi_produksi');
        if($id_lokasi != 0){
            $data['lokasi']->where('id_lokasi', $id_lokasi);
        }
        $data['lokasi'] = $data['lokasi']->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 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');

        if($id_gudang != 0){
            $data['gudang']->whereIn('id_gudang', explode(',', $id_gudang));
        }

        $data['gudang'] = $data['gudang']->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');

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

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

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

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

    	$data['lokasi'] = DB::table('ms_lokasi_produksi');
        if($id_lokasi != 0){
            $data['lokasi']->where('id_lokasi', $id_lokasi);
        }
        $data['lokasi'] = $data['lokasi']->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 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');

        if($id_gudang != 0){
            $data['gudang']->whereIn('id_gudang', explode(',', $id_gudang));
        }

        $data['gudang'] = $data['gudang']->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');

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

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

        $d_data = DB::table('pro_kembali')->where('id_kembali', $id);

        if($d_data->count() > 0){
        	$data['form_data'] = $this->form_data($d_data->first());
        	return view('produksi.KembalibarangkeseluruhanTambah')->with('data', $data);
        }else{
        	return redirect('kembaliseluruh');
        }
	}

	function form_data($data = array()){
        if(count($data) > 0){
            $store['id_kembali'] = $data->id_kembali;
            $store['tanggal'] = tgl_full($data->tanggal, '');
            $store['id_lokasi'] = $data->id_lokasi;
            $store['keterangan'] = $data->keterangan;
        }else{
            $store['id_kembali'] = '';
            $store['tanggal'] = '';
            $store['id_lokasi'] = '';
            $store['keterangan'] = "";
        }

        return $store;
    }

    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['jenis'] = 1;
        $data['id_user'] = $this->session['kode_user'];
        $data['id_lokasi'] = $req['lokasi'];
        $data['keterangan'] = $req['keterangan'];
        $data['is_all'] = 1;

        $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'];
        


        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];

                DB::table('pro_kembali_barang')->insert($insert);
            }
            
        }else{
            $id_del = array();
            $id_store = array();

            DB::table('pro_kembali')->where('id_kembali', $id_kembali)->update($data);
            
            $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];

                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('kembaliseluruh');
    }


    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;
    }

    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_edit(Request $request){
        $req = $request->all();
        $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"))->where('v.id_kembali', $id);

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

    function detail($id){
        $data['induk'] = DB::table('pro_kembali AS pk')->leftjoin("ms_lokasi_produksi AS mlp", "pk.id_lokasi", "mlp.id_lokasi")->select(DB::raw("pk.*, mlp.nama_lokasi"))->first();

        $data['detail'] = DB::table('view_edit_kembali')->where('id_kembali', $id)->get();

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

}