<?php

namespace App\Http\Controllers\master;

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

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

    public function index(){
    	return view('master.Upahlist');
    }

    function upah_borongan(){

        return view('master.UpahBoronganList');   
    }

    function get_upahborongan(){
        DB::statement(DB::raw('set @rownum=0'));
        $d_data = DB::table('ms_upah_borongan as mub')->join('ms_proses as mp', 'mp.id_proses', 'mub.id_proses')->leftjoin('ms_motif as mm','mub.id_motif','mm.id_motif')->leftjoin('ms_motif_barang as mmb', 'mub.id_motif_barang', 'mmb.id_motif_barang')->leftjoin('ms_barang as mb', 'mub.id_barang', 'mb.id')->leftjoin('ms_satuan as ms', 'mub.id_satuan', 'ms.id_satuan')->leftjoin('ms_lokasi_produksi as ml', 'mub.id_lokasi', 'ml.id_lokasi')->select(DB::raw('mub.*, mp.nama_proses, CASE WHEN mm.nama_motif IS NULL THEN "" ELSE mm.nama_motif END AS nama_motif, CASE WHEN mmb.motif_keterangan IS NULL THEN "" ELSE mmb.motif_keterangan END AS motif_keterangan, mb.kode_barang, mb.nama_barang, ms.nama_satuan, ms.satuan, ml.nama_lokasi'))->orderBy('mub.tanggal', 'DESC')->orderBy('mp.nama_proses', 'ASC');

        foreach($d_data->get() as $d){
            $id_barang = '';
            $id_motif = '';
            $id_motif_barang = '';

            if($d->id_barang != 0){
                $id_barang = $d->id_barang;
            }

            if($d->id_motif != 0){
                $id_motif = $d->id_motif;
            }

            if($d->id_motif_barang != 0){
                $id_motif_barang = $d->id_motif_barang;
            }

            $arr[] = array('id_upah_borongan' => $d->id_upah_borongan,
                            'id_proses' => $d->id_proses,
                            'id_motif' => $d->id_motif,
                            'id_motif_barang' => $d->id_motif_barang,
                            'id_barang' => $d->id_barang,
                            'id_satuan' => $d->id_satuan,
                            'id_lokasi' => $d->id_lokasi,
                            'tanggal' => tgl_full($d->tanggal, ''),
                            'nominal' => $d->nominal,
                            'nama_proses' => $d->nama_proses,
                            'motif' => $d->nama_motif,
                            'motif_keterangan' => $d->motif_keterangan,
                            'kode_barang' => $d->kode_barang,
                            'nama_barang' => $d->nama_barang,
                            'nama_satuan' => $d->nama_satuan,
                            'nama_lokasi' => $d->nama_lokasi,
                            'satuan' => $d->satuan,
                            'aksi' => '<button class="btn bg-warning-400" onclick="edit('.$d->id_upah_borongan.')"><i class="icon-pencil"></i> </button> <button class="btn bg-danger-800" type="button" onclick="hapus('.$d->id_upah_borongan.')"><i class="icon-eraser"></i></button>'.
                        '<input type="hidden" id="table_idupahborongan'.$d->id_upah_borongan.'" value="'.$d->id_upah_borongan.'">'.
                        '<input type="hidden" id="table_proses'.$d->id_upah_borongan.'" value="'.$d->id_proses.'">'.
                        '<input type="hidden" id="table_motif'.$d->id_upah_borongan.'" value="'.$id_motif.'">'.
                        '<input type="hidden" id="table_motifbarang'.$d->id_upah_borongan.'" value="'.$id_motif_barang.'">'.
                        '<input type="hidden" id="table_satuan'.$d->id_upah_borongan.'" value="'.$d->id_satuan.'">'.
                        '<input type="hidden" id="table_lokasi'.$d->id_upah_borongan.'" value="'.$d->id_lokasi.'">'.
                        '<input type="hidden" id="table_barang'.$d->id_upah_borongan.'" value="'.$id_barang.'">'.
                        '<input type="hidden" id="table_tanggal'.$d->id_upah_borongan.'" value="'.tgl_full($d->tanggal, '').'">'.
                        '<input type="hidden" id="table_nominal'.$d->id_upah_borongan.'" value="'.$d->nominal.'">');
        }

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

    }

    function get_barang(){
        DB::statement(DB::raw('set @rownum=0'));
    	$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'))->whereIn('mkb.induk_kb', [5, 13]);
		if($d_data->count() > 0){
			$arr['barang'] = $d_data->orderBy('nama_kategori', 'asc')->orderBy('nama_sub', 'asc')->orderBy('kode_barang', 'asc')->orderBy('nama_barang', 'asc')->get();
		}else{
			$arr['barang'] = array();
		}

        $d_data = DB::table('ms_proses')->where('parent', '!=', '0');
        if($d_data->count() > 0){
            $arr['proses'] = $d_data->get();
        }else{
            $arr['proses'] = array();
        }

        $d_data = DB::table('ms_motif')->orderBy('nama_motif', 'asc');
        if($d_data->count() > 0){
            $arr['motif'] = $d_data->get();
        }else{
            $arr['motif'] = array();
        }

        $d_data = DB::table('ms_motif_barang')->orderBy('motif_keterangan', 'asc');
        if($d_data->count() > 0){
            $arr['corak'] = $d_data->get();
        }else{
            $arr['corak'] = array();
        }

        $d_data = DB::table('ms_satuan')->orderBy('nama_satuan', 'asc');
        if($d_data->count() > 0){
            $arr['satuan'] = $d_data->get();
        }else{
            $arr['satuan'] = array();
        }

        $d_data = DB::table('ms_lokasi_produksi')->orderBy('nama_lokasi', 'asc');
        if($d_data->count() > 0){
            $arr['lokasi'] = $d_data->get();
        }else{
            $arr['lokasi'] = array();
        }

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

    
    function simpan(Request $request){
    	$id = $request->get('id_upah');
    	$data['id_barang'] = $request->get('popup_barang');
        $data['tanggal'] = tgl_full($request->get('popup_tanggal'), '2');
        $data['id_proses'] = $request->get('popup_proses');
        $data['id_motif'] = $request->get('popup_motif');
        $data['id_motif_barang'] = $request->get('popup_corak');
        $data['id_satuan'] = $request->get('popup_satuan');
        $data['nominal'] = $request->get('popup_nominal');
        $data['id_lokasi'] = $request->get('popup_lokasi');

    	if($id == ""){
    		DB::table('ms_upah_borongan')->insert($data);
    	}else{
    		DB::table('ms_upah_borongan')->where(array('id_upah_borongan'=>$id))->update($data);
    	}

    }

    function hapus(Request $request){
    	$id_upah = $request->get('id');
    	DB::table('ms_upah_borongan')->where('id_upah_borongan',$id_upah)->delete();
    }


    //------------------------------ Upah Tenun --------------------------------------------------------------------------------\\

    function upah_tenun(){
        return view('master.UpahTenunList');
    }

    function get_upahtenun(){
        DB::statement(DB::raw('set @rownum=0'));
        $d_data = DB::table('ms_upahtenun as mu')->join('ms_rekanan as mr', 'mu.id_rekanan', 'mr.id_rekanan')->join('ms_rekanan_kelompok as mrk', 'mu.id_makelar', 'mrk.id_kelompok')->select(DB::raw('mu.*, mr.nama as nama_rekanan, mrk.nama as nama_makelar'))->orderBy('mu.tanggal', 'DESC')->orderBy('mrk.nama', 'ASC');
        if($d_data->count() > 0){
        foreach($d_data->get() as $d){
            $arr[] = array('id_upahtenun' => $d->id_upahtenun,
                            'tanggal' => tgl_full($d->tanggal,''),
                            'nomor' => $d->nomor,
                            'id_rekanan' => $d->id_rekanan,
                            'id_makelar' => $d->id_makelar,
                            'nama_rekanan' => $d->nama_rekanan,
                            'nama_makelar' => $d->nama_makelar,
                            'aksi' => '<a class="btn  btn-xs bg-warning-400" type="button" href="'.url('upah_tenun/edit/'.$d->id_upahtenun).'"><i class="icon-pencil"></i> </a> <button class="btn btn-xs bg-danger-800" type="button" onclick="hapus('.$d->id_upahtenun.')"><i class="icon-eraser"></i></button>');
        }
    }else{
        $arr = array();
    }

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

    }

    function edit_upahtenun($id){
        $d_data = DB::table('ms_upahtenun')->where('id_upahtenun','=',$id)->first();;
        $data['data'] = $this->data($d_data);
        $data['rekanan'] = DB::table('ms_rekanan')->get();
        $data['makelar'] = DB::table('ms_rekanan_kelompok')->get();
        $data['produksi'] = DB::table('ms_lokasi_produksi')->get();
        return view('master.UpahTenunTambah')->with('data', $data);
    }

    function upahtenun_tambah(){
        $data['data'] = $this->data(array());
        $data['rekanan'] = DB::table('ms_rekanan')->get();
        $data['makelar'] = DB::table('ms_rekanan_kelompok')->get();
        $data['produksi'] = DB::table('ms_lokasi_produksi')->get();
        return view('master.UpahTenunTambah')->with('data', $data);
    }

    function get_motifket_upahtenun(){
        $d_data = DB::table('ms_motif')->orderBy('nama_motif', 'asc');
        if($d_data->count() > 0){
            $arr['data'] = $d_data->get();
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

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

    function get_kualitas_upahtenun(){
        $d_data = DB::table('ms_kualitas')->where('jenis', '1')->orderBy('id_kualitas','asc');
        if($d_data->count() > 0){
            $arr['data'] = $d_data->get();
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

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

    function data($data = array()){
        if(count($data) > 0){
            $store['id_upahtenun'] = $data->id_upahtenun;
            $store['tanggal'] = tgl_full($data->tanggal,'');
            $store['makelar'] = $data->id_makelar;
            $store['nomor'] = $data->nomor;
            $store['rekanan'] = $data->id_rekanan;
            $store['penambahan1'] = $data->penambahan1;
            $store['penambahan2'] = $data->penambahan2;
            $store['penambahan3'] = $data->penambahan3;
            $store['pas'] = $data->pas;
            $store['pengurangan1'] = $data->pengurangan1;
            $store['pengurangan2'] = $data->pengurangan2;
            $store['pengurangan3'] = $data->pengurangan3;
            $store['jenis'] = $data->id_lokasi;
        }else{
            $store['id_upahtenun'] = '';
            $store['tanggal'] = '';
            $store['makelar'] = '';
            $store['nomor'] = '';
            $store['rekanan'] = '';
            $store['penambahan1'] = '';
            $store['penambahan2'] = '';
            $store['penambahan3'] = '';
            $store['pas'] = '';
            $store['pengurangan1'] = '';
            $store['pengurangan2'] = '';
            $store['pengurangan3'] = '';
            $store['jenis'] = '';
        }
        return $store;
    }

    function simpan_upahtenun(Request $request){
        $id_upahtenun = $request->get('id_upahtenun');
        $data['id_makelar'] = $request->get('makelar');
        $data['id_rekanan'] = $request->get('rekanan');
        $data['tanggal'] = tgl_full($request->get('tanggal'),'2');
        $data['nomor'] = $request->get('nomor');
        $data['penambahan1'] = $request->get('penambahan1');
        $data['penambahan2'] = $request->get('penambahan2');
        $data['penambahan3'] = $request->get('penambahan3');
        $data['pas'] = $request->get('pas');
        $data['pengurangan1'] = $request->get('pengurangan1');
        $data['pengurangan2'] = $request->get('pengurangan2');
        $data['pengurangan3'] = $request->get('pengurangan3');
        $data['id_lokasi'] = $request->get('id_jenis');

        $tabel_id = $request->get('tabel_id');
        $tabel_idmotif = $request->get('tabel_idmotif');
        $tabel_nominal = $request->get('tabel_nominal');
        $tabel_tabungan = $request->get('tabel_tabungan');
        $tabel_penambahan1 = $request->get('tabel_penambahan1');
        $tabel_penambahan2 = $request->get('tabel_penambahan2');
        $tabel_penambahan3 = $request->get('tabel_penambahan3');
        $tabel_pas = $request->get('tabel_pas');
        $tabel_pengurangan1 = $request->get('tabel_pengurangan1');
        $tabel_pengurangan2 = $request->get('tabel_pengurangan2');
        $tabel_pengurangan3 = $request->get('tabel_pengurangan3');

        if($id_upahtenun == ''){
            $id = DB::table('ms_upahtenun')->insertGetId($data);
            for($i=0; $i < count($tabel_id); $i++){
                $upah['id_upahtenun'] = $id;
                $upah['id_motif'] = $tabel_idmotif[$i];
                $upah['nominal'] = $tabel_nominal[$i];
                $upah['tabungan'] = $tabel_tabungan[$i];
                $upah['penambahan1'] = $tabel_penambahan1[$i];
                $upah['penambahan2'] = $tabel_penambahan2[$i];
                $upah['penambahan3'] = $tabel_penambahan3[$i];
                $upah['pas'] = $tabel_pas[$i];
                $upah['pengurangan1'] = $tabel_pengurangan1[$i];
                $upah['pengurangan2'] = $tabel_pengurangan2[$i];
                $upah['pengurangan3'] = $tabel_pengurangan3[$i];

                DB::table('ms_upahtenun_barang')->insert($upah);
            }
        }else{
            DB::table('ms_upahtenun')->where('id_upahtenun', $id_upahtenun)->update($data);
            $id_del = array();
            $id_store = array();
            $d_barang = DB::table('ms_upahtenun_barang')->where('id_upahtenun', '=', $id_upahtenun);
            foreach($d_barang->get() as $d){
                $id_store[] = $d->id_upahtenun_barang;
            }
            foreach($id_store as $d){
                if(count($tabel_id) > 0){
                    if(!in_array($d, $tabel_id)){
                        $id_del[] = $d;
                    }
                }else{
                    $id_del[] = $d;
                }
            }

            for($i = 0; $i < count($tabel_id); $i++){
                $upah['id_upahtenun'] = $id_upahtenun;
                $upah['id_motif'] = $tabel_idmotif[$i];
                $upah['nominal'] = $tabel_nominal[$i];
                $upah['tabungan'] = $tabel_tabungan[$i];
                $upah['penambahan1'] = $tabel_penambahan1[$i];
                $upah['penambahan2'] = $tabel_penambahan2[$i];
                $upah['penambahan3'] = $tabel_penambahan3[$i];
                $upah['pas'] = $tabel_pas[$i];
                $upah['pengurangan1'] = $tabel_pengurangan1[$i];
                $upah['pengurangan2'] = $tabel_pengurangan2[$i];
                $upah['pengurangan3'] = $tabel_pengurangan3[$i];

                if($tabel_id[$i] == ''){
                    DB::table('ms_upahtenun_barang')->insert($upah);
                }else if($tabel_id != ''){
                    $where = 
                    DB::table('ms_upahtenun_barang')->where(array('id_upahtenun_barang' => $tabel_id[$i]))->update($upah);
                }
            }
            
            if(count($id_del) > 0){
                DB::table('ms_upahtenun_barang')->whereIn('id_upahtenun_barang', $id_del)->delete();
            }
        }

            return redirect('upah_tenun');
    }

    function get_edit_upahtenun(Request $request){
        $id = $request->get('id');
        $d_data = DB::table('ms_upahtenun_barang as mub')->join('ms_motif as mm','mub.id_motif','mm.id_motif')->join('ms_kualitas as mk','mub.pengurangan3','mk.id_kualitas')->select(DB::raw('mub.*, mm.nama_motif, mk.nama_kualitas as nama_pengurangan3'))->where(array('id_upahtenun'=>$id));
        if($d_data->count() > 0){
            foreach ($d_data->get() as $d) {
                # code...
                $arr[] = array("id_upahtenun_barang" => $d->id_upahtenun_barang,
                                "id_motif" => $d->id_motif,
                                "nama_motif" => $d->nama_motif,
                                "nominal" => $d->nominal,
                                "tabungan" => $d->tabungan,
                                "penambahan1" => $d->penambahan1,
                                "penambahan2" => $d->penambahan2,
                                "penambahan3" => $d->penambahan3,
                                "pas" => $d->pas,
                                "pengurangan1" => $d->pengurangan1,
                                "pengurangan2" => $d->pengurangan2,
                                "pengurangan3" => $d->pengurangan3,
                                "nama_pengurangan3" => $d->nama_pengurangan3);
            }
        }else{
            $arr = array();
        }

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

    function hapus_upahtenun(Request $request){
        $id = $request->get('id');
        DB::table('ms_upahtenun')->where(array('id_upahtenun' => $id))->delete();
        DB::table('ms_upahtenun_barang')->where(array('id_upahtenun' => $id))->delete();
    }

}
