<?php

namespace App\Http\Controllers\produksi;

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

class UpahproduksitanpahasilController 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_lokasi'=>Session::get('id_lokasi'), 'kode_user' => Session::get('kode_user'), 'kode_group' => Session::get('kode_group'), 'id_proses'=>Session::get('id_proses'));
    }

    function index(){
        return view('produksi.UpahproduksiLangsungList');
    }

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



        $d_parent = $this->parent_upah();
        if($id_lokasi != 0){
            $d_parent->where('p.id_lokasi', $id_lokasi);
        }

        if($id_proses != ''){
            $d_parent->whereIn('p.id_proses', explode(",", $id_proses));
        }

        $d_parent->leftjoin(DB::raw("(SELECT *, SUM(upah) jum_barang FROM pro_upahproduksi_lainnya_barang GROUP BY id_upahproduksi_lainnya) AS upl"), function($join){
            $join->on("p.id_upahproduksi_lainnya", "upl.id_upahproduksi_lainnya");
        })->leftjoin(DB::raw("(SELECT *, SUM(CASE WHEN tipe = 'D' THEN jumlah ELSE '0' END) - SUM(CASE WHEN tipe = 'K' THEN jumlah ELSE '0' END) jum_lain 
                    FROM pro_upahproduksi_lainnya_lainlain
                    GROUP BY id_upahproduksi_lainnya) as upll"), function($join){
            $join->on("p.id_upahproduksi_lainnya", "upll.id_upahproduksi_lainnya");
        })->select(DB::raw("p.id_upahproduksi_lainnya, CASE WHEN mp.id_pegawai IS NULL THEN '' ELSE mp.id_pegawai END AS id_pegawai, CASE WHEN mp.id_pegawai IS NULL THEN '' ELSE mp.nama_pegawai END AS nama_pegawai, CASE WHEN mr.id_rekanan IS NULL THEN '' ELSE mr.id_rekanan END AS id_rekanan, CASE WHEN mr.id_rekanan IS NULL THEN '' ELSE mr.nama END AS nama_rekanan, mlp.id_lokasi, mlp.nama_lokasi, mpr.id_proses, mpr.nama_proses, p.tanggal_upah, p.tanggal_hasil, p.nomor, p.status_bayar, p.tanggal_bayar, (CASE WHEN upl.jum_barang IS NULL THEN '0' ELSE upl.jum_barang END + CASE WHEN upll.jum_lain IS NULL THEN '0' ELSE upll.jum_lain END) AS upah_total"));

        if($d_parent->count() > 0){
            foreach($d_parent->get() as $d){
                
                $pemroses = '';
                if($d->id_pegawai != ''){
                    $pemroses = $d->nama_pegawai." (Pegawai) ";
                }else if($d->id_rekanan != ''){
                    $pemroses = $d->nama_rekanan." (Rekanan) ";
                }

                $button = "<div class='btn-group'>";

                if($d->status_bayar == 0){

                    $button .= "<button type='button' class='btn btn-default dropdown-toggle' data-toggle='dropdown'><i class='icon-menu7'></i></button>";
                    $button .= "<ul class='dropdown-menu dropdown-menu-right'>";
                    $button .= "<li><a href='".url('upah_produksitanpahasil/bayar_upah/'.$d->id_upahproduksi_lainnya)."'><i class='icon-cash2'></i> Bayar</a></li> <li><a href='javascript:;' onclick='hapus(".$d->id_upahproduksi_lainnya.")'><i class='icon-eraser'></i> Hapus</a></li>";
                }else{
                    $button .='<a class="btn btn-xs bg-success-400" type="button" href="javascript:void(0)" onclick="cetak($(this))" id_upah="'.$d->id_upahproduksi_lainnya.'" pemroses="'.$pemroses.'" nomor_upah="'.$d->nomor.'" id_pegawai="'.$d->id_pegawai.'" id_rekanan="'.$d->id_rekanan.'"><i class="icon-printer"></i></a>';
                }

                $button .= "</div>";

                $arr[] = array('id_upah' => $d->id_upahproduksi_lainnya,
                                'pemroses' => $pemroses,
                                'id_lokasi' => $d->id_lokasi,
                                'lokasi' => $d->nama_lokasi,
                                'id_proses' => $d->id_proses,
                                'nama_proses' => $d->nama_proses,
                                'tanggal_upah' => tgl_full($d->tanggal_upah, ''),
                                'nomor' => $d->nomor,
                                'tanggal_bayar' => ($d->tanggal_bayar == '0000-00-00') ? "<span class='text-warning'><i class='fa fa-info-circle'></i> Belum Diambil</span>":tgl_full($d->tanggal_bayar, ''),
                                'status' => $d->status_bayar,
                                'jumlah' => $d->upah_total,
                                'aksi' => $button);
            }
        }else{
            $arr = array();
        }

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

    function tambah(){
        $id_lokasi = $this->session['id_lokasi'];
        $d_lokasi = DB::table('ms_lokasi_produksi')->orderby('nama_lokasi', 'asc');

        if($id_lokasi != 0 && !is_null($id_lokasi)){
            $d_lokasi->where('id_lokasi', $id_lokasi);
        }
        // if(isset($req['id_lokasi'])){
        //     $id_lokasi = $req['id_lokasi'];
        // }

        $d_pegawai = DB::table('ms_pegawai AS mp')->join("ms_departement AS md", "mp.id_departement", "md.id_departement")->join("ms_lokasi_produksi AS mlp", "md.id_lokasi", "mlp.id_lokasi")->orderby("mp.nama_pegawai", 'asc')->select(DB::raw("mp.*, md.nama nama_departemen, mlp.id_lokasi, mlp.nama_lokasi"));

        if($id_lokasi != '0' && !is_null($id_lokasi)){
            $d_pegawai->where('mlp.id_lokasi', $id_lokasi);
        }

        $d_rekanan = DB::table("ms_rekanan AS mr")->leftjoin("ms_rekanan_kelompok AS mrk", "mrk.id_kelompok", "mr.id_kelompok")->orderBy("mr.nama", 'asc')->orderBy("mrk.nama", 'asc')->select(DB::raw("mr.*, mrk.nama as nama_kelompok"));

        $d_proses = DB::table('ms_proses')->where('parent', '!=', '0')->orderBy('nama_proses', 'asc');
        $akun  = DB::table("ms_perkiraan")->where([['used', '=', 1]])->orderByRaw("kd_rek_1, kode_perkiraan")->get();
        $data['akun'] = array_reduce($akun->toArray(), function($carry, $item){
            $carry[ $item->id ] = format_akun_perkiraan($item);
            return $carry;
        });

        $data['lokasi']       = $d_lokasi->get();
        $data['id_akun_upah'] = '103';
        $data['pegawai']      = $d_pegawai->get();
        $data['proses']       = $d_proses->get();
        $data['rekanan']      = $d_rekanan->get();
        $data['edit']         = false;
        // $data['nomor'] = CodeGenerator::code('pro_upahproduksi_lainnya', 'nomor', 4, 'UP');
        $data['data']       = $this->data();
        //$data['nomor'] = '001';

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

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

        $id_upah = $req['id_upahproduksi'];
        $data['id_pegawai'] = $req['pegawai'];
        $data['id_rekanan'] = 0;
        $data['id_lokasi'] = $req['lokasi'];
        $data['id_proses'] = $req['proses'];
        $data['tanggal_upah'] = tgl_full($req['tanggal'], '2');
        $data['tanggal_hasil'] = tgl_full($req['tanggal_akhir'], '2');
        $data['tanggal_full'] = tgl_full($req['tanggal'], '5');
        $data['nomor'] = CodeGenerator::code('pro_upahproduksi_lainnya', 'nomor', 4, 'UP');
        $data['id_akun_upah'] = $req['akun_upah'];
        $data['status_bayar'] = '0';
        $data['jenis'] = '1';

        $detail_keterangan = $request->get('keterangan_hasil');
        $detail_upah = $request->get('select-upah');
        $detail_jumlah = $request->get('jumlah_hasil');
        $detail_total = $request->get('store_total');
        $detail_satuan = $request->get('store_satuan');

        $lain_akun = $request->get('akun_lain_');
        $lain_keterangan = $request->get('keterangan_lain_');
        $lain_tipe = $request->get('tipe_lain_');
        $lain_jenis_transaksi = $request->get('jenis_transaksi_lain_');
        $lain_jumlah = $request->get('jumlah_lain_');
        
        if($id_upah == ''){
            $id_parent = DB::table('pro_upahproduksi_lainnya')->insertGetId($data);

            for($i = 0; $i < count($detail_keterangan); $i++){
                $detail['id_upahproduksi_lainnya'] = $id_parent;
                $detail['keterangan'] = $detail_keterangan[$i];
                $detail['id_upah'] = $detail_upah[$i];
                $detail['jumlah'] = $detail_jumlah[$i];
                $detail['upah'] = $detail_total[$i];
                $detail['id_satuan'] = $detail_satuan[$i];

                DB::table('pro_upahproduksi_lainnya_barang')->insert($detail);
            }

            for($i = 0; $i < count($lain_keterangan); $i++){
                $lain['id_upahproduksi_lainnya'] = $id_parent;
                // $lain['id_akun'] = $lain_akun[$i];// id_akun sementara tidak terpakai
                $lain['keterangan'] = $lain_keterangan[$i];
                $lain['tipe'] = $lain_tipe[$i];
                $lain['jenis_transaksi'] = $lain_jenis_transaksi[$i];
                $lain['jumlah'] = $lain_jumlah[$i];

                DB::table('pro_upahproduksi_lainnya_lainlain')->insert($lain);
            }
        }else{

        }

        return redirect('upah_produksitanpahasil');
    }

    function get_data_proses()
    {
        $data_proses = ProsesProduksi::orderByRaw("id_proses")->get();
        return response()->json($data_proses);
    }

    function data($data = null){
        if(!is_null($data)){
            $store['id_upahproduksi']   = $data->id_upahproduksi_lainnya;
            $store['id_pegawai']        = $data->id_pegawai;
            $store['nama_pegawai']      = $data->nama_pegawai;
            $store['id_rekanan']        = $data->id_rekanan;
            $store['nama_rekanan']      = $data->nama_rekanan;
            $store['id_lokasi']            = $data->id_lokasi;
            $store['id_proses']         = $data->id_proses;
            $store['nama_proses']       = $data->nama_proses;
            $store['tanggal_upah']      = tgl_full($data->tanggal_upah, null);
            $store['tanggal_hasil']     = tgl_full($data->tanggal_hasil, null);
            $store['nomor']             = $data->nomor;
            $store['id_akun_upah']      = $data->id_akun_upah;
            $store['id_akun_bayar']     = $data->id_akun_bayar;
            $store['status_bayar']      = $data->status_bayar;
            $store['tanggal_bayar']     = ($data->tanggal_bayar == '0000-00-00') ? "" : tgl_full($data->tanggal_bayar, null);
        }else{
            $store['id_upahproduksi']   = '';
            $store['id_pegawai']        = '';
            $store['nama_pegawai']      = '';
            $store['id_rekanan']        = '';
            $store['nama_rekanan']      = '';
            $store['id_lokasi']            = '';
            $store['id_proses']         = '';
            $store['nama_proses']       = '';
            $store['tanggal_upah']      = '';
            $store['tanggal_hasil']     = '';
            $store['nomor']             = '';
            $store['id_akun_upah']      = '';
            $store['id_akun_bayar']     = '';
            $store['status_bayar']      = '';
            $store['tanggal_bayar']     = '';
        }
        return $store;
    }

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

        DB::beginTransaction();
        try{
            $up = DB::table('pro_upahproduksi_lainnya')->where('id_upahproduksi_lainnya', $id_upah);
            $up_detail = DB::table('pro_upahproduksi_lainnya_barang')->where('id_upahproduksi_lainnya', $id_upah);
            $up_lain = DB::table('pro_upahproduksi_lainnya_lainlain')->where('id_upahproduksi_lainnya', $id_upah);

            if ($up->count() > 0) {
                $up->delete();
            }
            if ($up_detail->count() > 0) {
                $up_detail->delete();
            }
            if ($up_lain->count() > 0) {
                $up_lain->delete();
            }
            DB::commit();
            return response()->json(array('status' => 'Success'));
        }catch(Exception $exception){
            DB::rollback();
            return response()->json(array('status' => 'Failed'));
        }
    }

    function parent_upah(){
        $d_data = DB::table('pro_upahproduksi_lainnya AS p')->leftjoin('ms_pegawai AS mp', 'p.id_pegawai', 'mp.id_pegawai')->leftjoin('ms_rekanan AS mr', 'p.id_rekanan', 'mr.id_rekanan')->leftjoin('ms_lokasi_produksi AS mlp', 'p.id_lokasi', 'mlp.id_lokasi')->leftjoin('ms_proses AS mpr', 'p.id_proses', 'mpr.id_proses')->select(DB::raw("p.id_upahproduksi_lainnya, CASE WHEN mp.id_pegawai IS NULL THEN '' ELSE mp.id_pegawai END AS id_pegawai, CASE WHEN mp.id_pegawai IS NULL THEN '' ELSE mp.nama_pegawai END AS nama_pegawai, CASE WHEN mr.id_rekanan IS NULL THEN '' ELSE mr.id_rekanan END AS id_rekanan, CASE WHEN mr.id_rekanan IS NULL THEN '' ELSE mr.nama END AS nama_rekanan, mlp.id_lokasi, mlp.nama_lokasi, mpr.id_proses, mpr.nama_proses, p.tanggal_upah, p.tanggal_hasil, p.nomor, p.status_bayar, p.tanggal_bayar"))->orderBy('p.status_bayar', 'asc')->orderBy('nomor', 'desc')->where("p.jenis", '1');

        return $d_data;
    }

    function get_sisa(Request $request){
        $req = $request->all();
       
        
        $d_upah = DB::table('ms_upah_boronganlain')->where(['id_proses' => $req['proses'], 'id_lokasi' => $req['lokasi'], 'id_satuan' => $req['id_satuan'], 'status' => '1']);
        $arr['upah'] = $d_upah->get();
        

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

    }

    function bayar_upah($id){
        $d_data = DB::table('pro_upahproduksi_lainnya')->where('id_upahproduksi_lainnya', $id);

        if($d_data->count() > 0){
            $data['id_upah'] = $id;
            $akun  = DB::table("ms_perkiraan")->where([['used', '=', 1]])->orderByRaw("kd_rek_1, kode_perkiraan")->get();
            $data['akun'] = array_reduce($akun->toArray(), function($carry, $item){
                $carry[ $item->id ] = format_akun_perkiraan($item);
                return $carry;
            });
            $akun_bayar  = DB::table("ms_perkiraan")->where([['used', '=', 1]])->orderByRaw("kd_rek_1, kode_perkiraan")->get();
            $data['akun_bayar'] = array_reduce($akun_bayar->toArray(), function($carry, $item){
                $carry[ $item->id ] = format_akun_perkiraan($item);
                return $carry;
            });

            $data['parent'] = DB::table('pro_upahproduksi_lainnya AS p')->leftjoin('ms_pegawai AS mp', 'p.id_pegawai', 'mp.id_pegawai')->leftjoin('ms_rekanan AS mr', 'p.id_rekanan', 'mr.id_rekanan')->leftjoin('ms_lokasi_produksi AS mlp', 'p.id_lokasi', 'mlp.id_lokasi')->leftjoin('ms_proses AS mpr', 'p.id_proses', 'mpr.id_proses')->leftjoin("ms_rekanan_kelompok AS mk", "mr.id_kelompok", "mk.id_kelompok")->select(DB::raw("p.id_upahproduksi_lainnya, CASE WHEN mp.id_pegawai IS NULL THEN '' ELSE mp.id_pegawai END AS id_pegawai, CASE WHEN mp.id_pegawai IS NULL THEN '' ELSE mp.nama_pegawai END AS nama_pegawai, CASE WHEN mr.id_rekanan IS NULL THEN '' ELSE mr.id_rekanan END AS id_rekanan, CASE WHEN mr.id_rekanan IS NULL THEN '' ELSE mr.nama END AS nama_rekanan, CASE WHEN mk.id_kelompok IS NULL THEN '' ELSE mk.id_kelompok END AS id_kelompok, CASE WHEN mk.id_kelompok IS NULL THEN '' ELSE mk.nama END AS nama_kelompok, mlp.id_lokasi, mlp.nama_lokasi, mpr.id_proses, mpr.nama_proses, p.tanggal_upah, p.tanggal_hasil, p.nomor, p.status_bayar, p.tanggal_bayar"))->orderBy('p.status_bayar', 'asc')->orderBy('nomor', 'desc')->where('p.id_upahproduksi_lainnya', $id)->first();

            $data['detail'] = DB::table('pro_upahproduksi_lainnya_barang AS pul')->leftjoin('ms_upah_boronganlain AS mub', 'pul.id_upah', 'mub.id_upah_boronganlain')->where('pul.id_upahproduksi_lainnya', $id)->select(DB::raw("pul.*, CASE WHEN mub.nama IS NULL THEN '' ELSE mub.nama END AS nama_upah"));

            $data['lainnya'] = DB::table('pro_upahproduksi_lainnya_lainlain as pull')->leftjoin("ms_perkiraan AS mpe", "pull.id_akun", "mpe.id")->where('id_upahproduksi_lainnya', $id)->select(DB::raw("pull.*, mpe.kode_perkiraan, mpe.nama_perkiraan"));

            return view('produksi.UpahproduksiLangsungBayar')->with('data', $data);       
        }else{
            return redirect('upah_produksitanpahasil');
        }
    }


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

        $id_upah = $req['id_upah'];

        $data['id_akun_bayar'] = $req['akun_bayar'];
        $data['tanggal_bayar'] = tgl_full($req['tanggal_bayar'], '2');
        $data['status_bayar'] = '1';

       DB::table('pro_upahproduksi_lainnya')->where('id_upahproduksi_lainnya', $id_upah)->update($data);

        $store_id = $request->get('store_id');
        $lain_akun = $request->get('akun_lain_');
        $lain_keterangan = $request->get('keterangan_lain_');
        $lain_tipe = $request->get('tipe_lain_');
        $lain_jenis_transaksi = $request->get('jenis_transaksi_lain_');
        $lain_jumlah = $request->get('jumlah_lain_');

        // print_r($store_id);
        // print_r($lain_akun);
        // print_r($lain_keterangan);
        // print_r($lain_tipe);
        // print_r($lain_jumlah);

        if($id_upah != ''){
            for($i = 0; $i < count($store_id); $i++){
                if($store_id[$i] == ''){
                    $lain['id_upahproduksi_lainnya'] = $id_upah;
                    // $lain['id_akun'] = $lain_akun[$i]; //id_akun sementara tidak terpakai
                    $lain['keterangan'] = $lain_keterangan[$i];
                    $lain['tipe'] = $lain_tipe[$i];
                    $lain['jenis_transaksi'] = $lain_jenis_transaksi[$i];
                    $lain['jumlah'] = $lain_jumlah[$i];

                    DB::table('pro_upahproduksi_lainnya_lainlain')->insert($lain);

                    
                }else{

                }
            }
        }

       return redirect('upah_produksitanpahasil');
    }

    function cetak(Request $request){
        $req = $request->all();
        $id = '';
        $filter_cetak = '';
        $tipe_laporan = '';
        $id_rekanan = '';
        $id_pegawai = '';
        $id_proses = '';
        $tgl_awal = date('Y-m-d');
        $tgl_akhir = date('Y-m-d');
        if(isset($req['id_upahproduksi'])){
            $id = $req['id_upahproduksi'];
        }

        if(isset($req['filter_cetak'])){
            $filter_cetak = $req['filter_cetak'];
        }

        if(isset($req['tipe_laporan'])){
            $tipe_laporan = $req['tipe_laporan'];
        }

        if(isset($req['idrekanan'])){
            $id_rekanan = $req['idrekanan'];
        }

        if(isset($req['idpegawai'])){
            $id_pegawai = $req['idpegawai'];
        }

        if(isset($req['tgl_awal'])){
            $tgl_awal = tgl_full($req['tgl_awal'], '2');
        }

        if(isset($req['tgl_akhir'])){
            $tgl_akhir = tgl_full($req['tgl_akhir'], '2');
        }

        if(isset($req['id_proses'])){
            $id_proses = $req['id_proses'];
        }

        if($filter_cetak == 'nomor'){
            $d_parent = $this->parent_upah()->where('p.id_upahproduksi_lainnya', $id);
            if($d_parent->count() > 0){
                $arr['parent'] = $d_parent->first();
            }else{
                $arr['parent'] = array();
            }

            $d_detail = DB::table('pro_upahproduksi_lainnya_barang')->where('id_upahproduksi_lainnya', $id);

            if($d_detail->count() > 0){
                $arr['detail'] = $d_detail->get();
            }else{
                $arr['detail'] = array();
            }

            $d_lain = DB::table('pro_upahproduksi_lainnya_lainlain')->where('id_upahproduksi_lainnya', $id);
            if($d_lain->count() > 0){
                $arr['lain'] = $d_lain->get();
            }else{
                $arr['lain'] = array();
            }

            $html = view('produksi.cetak.CetakResiLainnya')->with('data',$arr);

        }else if($filter_cetak == 'pemroses'){
            if($tipe_laporan == 1){
                $d_parent = $this->parent_upah();
                $arr['date_range'] = date_range($tgl_awal, $tgl_akhir);
                if($id_pegawai != ''){
                    $d_parent->where('p.id_pegawai', $id_pegawai);
                }else if($id_rekanan != ''){
                    $d_parent->where('p.id_rekanan', $id_rekanan);
                }else{
                    $d_parent->where('p.id_pegawai', '');
                }

                $d_parent->whereBetween('p.tanggal_upah', [$tgl_awal, $tgl_akhir])->where('p.id_proses', $id_proses);
                $arr['id_parent'] = $d_parent->pluck('id_upahproduksi_lainnya')->toArray();

                if($d_parent->count() > 0){
                    $arr['parent'] = $d_parent->first();
                }else{
                    $arr['parent'] = array();
                }

                if(count($arr['id_parent']) > 0){
                    $d_detail = DB::table('pro_upahproduksi_lainnya_barang AS pul')->join("pro_upahproduksi_lainnya AS pu", "pu.id_upahproduksi_lainnya", "pul.id_upahproduksi_lainnya")->leftjoin("ms_upah_boronganlain AS mub", "pul.id_upah", "mub.id_upah_boronganlain")->leftjoin("ms_proses AS mp", "pu.id_proses", "mp.id_proses")->whereIn('pu.id_upahproduksi_lainnya', $arr['id_parent'])->select(DB::raw("pul.*, pu.tanggal_upah, pu.tanggal_hasil, pu.nomor, mp.id_proses, mp.nama_proses, mub.id_upah_boronganlain, mub.nama AS nama_upah, SUM(pul.jumlah) AS sum_jumlah, SUM(pul.upah) AS sum_upah"))->orderBy('pu.tanggal_upah', 'asc')->orderBy('pu.nomor', 'asc')->groupBy(DB::raw("pu.id_proses, pul.id_upah, pul.upah/pul.jumlah"));

                    if($d_detail->count() > 0){
                        $arr['detail'] = $d_detail->get();
                    }else{
                        $arr['detail'] = array();
                    }

                    $d_lain = DB::table('pro_upahproduksi_lainnya_lainlain AS pul')->join("pro_upahproduksi_lainnya AS pu", "pu.id_upahproduksi_lainnya", "pul.id_upahproduksi_lainnya")->whereIn('pu.id_upahproduksi_lainnya', $arr['id_parent'])->select(DB::raw("pul.*, pu.tanggal_upah, pu.tanggal_hasil, pu.nomor"))->orderBy('pu.tanggal_upah', 'asc')->orderBy('pu.nomor', 'asc');

                    if($d_lain->count() > 0){
                        $arr['lain'] = $d_lain->get();
                    }else{
                        $arr['lain'] = array();
                    }
                }else{
                    $arr['detail'] = array();
                    $arr['lain'] = array();
                }

                $html = view('produksi.cetak.CetakResiLainnyaRangeRekap')->with('data',$arr);

            }else if($tipe_laporan == 2){ // detail
                $d_parent = $this->parent_upah();
                $arr['date_range'] = date_range($tgl_awal, $tgl_akhir);
                if($id_pegawai != ''){
                    $d_parent->where('p.id_pegawai', $id_pegawai);
                }else if($id_rekanan != ''){
                    $d_parent->where('p.id_rekanan', $id_rekanan);
                }else{
                    $d_parent->where('p.id_pegawai', '');
                }

                $d_parent->whereBetween('p.tanggal_upah', [$tgl_awal, $tgl_akhir])->where('p.id_proses', $id_proses);
                $arr['id_parent'] = $d_parent->pluck('id_upahproduksi_lainnya')->toArray();

                if($d_parent->count() > 0){
                    $arr['parent'] = $d_parent->first();
                }else{
                    $arr['parent'] = array();
                }

                if(count($arr['id_parent']) > 0){
                    $d_detail = DB::table('pro_upahproduksi_lainnya_barang AS pul')->join("pro_upahproduksi_lainnya AS pu", "pu.id_upahproduksi_lainnya", "pul.id_upahproduksi_lainnya")->whereIn('pu.id_upahproduksi_lainnya', $arr['id_parent'])->select(DB::raw("pul.*, pu.tanggal_upah, pu.tanggal_hasil, pu.nomor"))->orderBy('pu.tanggal_upah', 'asc')->orderBy('pu.nomor', 'asc');

                    if($d_detail->count() > 0){
                        $arr['detail'] = $d_detail->get();
                    }else{
                        $arr['detail'] = array();
                    }

                    $d_lain = DB::table('pro_upahproduksi_lainnya_lainlain AS pul')->join("pro_upahproduksi_lainnya AS pu", "pu.id_upahproduksi_lainnya", "pul.id_upahproduksi_lainnya")->whereIn('pu.id_upahproduksi_lainnya', $arr['id_parent'])->select(DB::raw("pul.*, pu.tanggal_upah, pu.tanggal_hasil, pu.nomor"))->orderBy('pu.tanggal_upah', 'asc')->orderBy('pu.nomor', 'asc');

                    if($d_lain->count() > 0){
                        $arr['lain'] = $d_lain->get();
                    }else{
                        $arr['lain'] = array();
                    }
                }else{
                    $arr['detail'] = array();
                    $arr['lain'] = array();
                }

                $html = view('produksi.cetak.CetakResiLainnyaRange')->with('data',$arr);

                

            }else{

            }
        }else{
            return redirect("upah_produksitanpahasil");
        }

        require('fpdf181/Mc_table.php');
        return response($html)->header('Content-Type', 'application/pdf');
    }
}