<?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\Crypt;
use Illuminate\Contracts\Encryption\DecryptException;
use App\Pegawai;
use App\Rekanan;
use App\ProsesProduksi;
use Illuminate\Support\Facades\Session;


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

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

        DB::statement(DB::raw('set @rownum=0'));
        // DB::enableQueryLog();
         $d_data = DB::table('a_pro_upahproduksi AS pu')
                ->join('pro_barangmasuk as pbm','pu.id_barangmasuk','pbm.id_barangmasuk')
                ->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')
                ->join('ms_proses AS mpr', 'pb.id_proses', 'mpr.id_proses')
                ->leftJoin('ms_rekanan AS mr', 'pb.id_rekanan','mr.id_rekanan')
                ->leftJoin('ms_pegawai AS mp', 'pb.id_pegawai', 'mp.id_pegawai')
                ->leftJoin('pem_workorder AS pw', 'pb.id_workorder', 'pw.id_workorder')
                ->leftJoin('pem_workorder_barang AS pwb', 'pb.id_workorder_barang', 'pwb.id_workorder_barang')
                ->leftjoin('ms_rekanan AS mk', 'pu.id_rekanan', 'mk.id_rekanan')
                ->leftjoin('ms_rekanan_kelompok AS rk', 'mk.id_kelompok', 'rk.id_kelompok')
                ->where('pb.id_proses', '!=', 37)
                ->orderBy('pb.tanggal', 'desc')->orderBy('pb.status', 'asc')
                ->selectRaw("pu.*, pb.id_barangkeluar, pb.id_rekanan, mr.nama nama_rekanan, rk.nama as makelar, rk.id_kelompok, pb.id_pegawai, mp.nama_pegawai, pb.id_proses, mpr.nama_proses, pb.id_workorder, pw.nomor no_workorder, pb.id_workorder_barang, pwb.no_workorder no_workorder_barang, mpr.id_proses, mpr.nama_proses, pb.status, pbm.noAuto, pbm.noAuto_ext");
            if($id_proses != 0){
                $d_data->whereIn('pu.id_proses', explode(",", $id_proses));
            }

            if($id_lokasi != 0){
                $d_data->where('pu.id_lokasi', $id_lokasi);
            }
         if($d_data->count() > 0){
            $param = "";
            foreach($d_data->get() as $d){
                if($d->id_workorder != '' || $d->id_workorder != 0){
                    $workorder = $d->no_workorder.' (Nomor SPK)';
                }else{
                    $workorder = $d->no_workorder_barang. ' (Nomor Order)';
                }
                if($d->nama_rekanan != '' || $d->nama_rekanan != 0){
                    $pm = 'Rekanan';
                    $pemroses = $d->nama_rekanan.' (Rekanan)';
                    $jeniss=0;
                    $param = "rekanan=".$d->id_rekanan;
                }else{
                    $pm = 'Pegawai';
                    $pemroses = $d->nama_pegawai.' (Pegawai)';
                    $jeniss=1;
                    $param = "pegawai=".$d->id_pegawai;
                }

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


                $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'>";
                if ($pm == 'Pegawai') {
                    $button .= "<li><a href='".url('upahproduksi/edit/'.$d->id_upahproduksi)."'><i class='icon-pencil'></i> Edit</a></li>
                                <li><a href='javascript:;' onclick='hapus(".$d->id_upahproduksi.")'><i class='icon-eraser'></i> Hapus</a></li>";
                } else {
                    $prosesTenun = 37;
                    if($d->id_proses==$prosesTenun){
                        $button .= "<li><a href='".url('upahproduksi/rekanan_tenun/edit/'.$d->id_upahproduksi)."'><i class='icon-pencil'></i> Edit</a></li>";

                    }else{
                        $button .= "<li><a href='".url('upahproduksi/edit-rekanan/'.$d->id_upahproduksi)."'><i class='icon-pencil'></i> Edit</a></li>";

                    }
                    $button .= "<li><a href='javascript:;' onclick='hapus(".$d->id_upahproduksi.")'><i class='icon-eraser'></i> Hapus</a></li>";
                }
                

                // $button .= '<a class="btn  btn-xs bg-warning-400" type="button" href="'.url('upahproduksi/edit/'.$d->id_upahproduksi).'"><i class="icon-pencil"></i> </a> <button class="btn btn-xs bg-danger-800" type="button" onclick="hapus('.$d->id_upahproduksi.')"><i class="icon-eraser"></i></button> ';

                if($d->status_bayar==0){
                    $tgl_bayar="<span class='text-warning'><i class='fa fa-info-circle'></i> Belum Diambil</span>";
                    // $button .='<a  target="_blank" class="btn btn-xs bg-success-400" type="button" href="'.url('upahproduksi/bayar_upah/'.$d->id_upahproduksi).'"><i class="icon-cash2"></i></a>';
                    // $button .='<a class="btn btn-xs bg-success-400" type="button" href="'.url('upahproduksi/multi_bayar_upah?'.$param).'"><i class="icon-cash2"></i> Multi</a>';
                    if ($pm == 'Pegawai') {
                        $button .= "<li><a href='".url('upahproduksi/bayar_upah/'.$d->id_upahproduksi)."'><i class='icon-cash2'></i> Bayar</a></li>";
                        $button .= "<li><a href='".url('upahproduksi/multi_bayar_upah?'.$param)."'><i class='icon-cash2'></i> Bayar Multi</a></li>";
                    }else {
                        $button .= "<li><a href='".url('upahproduksi/rekanan/bayar_upah/'.$d->id_upahproduksi)."'><i class='icon-cash2'></i> Bayar</a></li>";
                        $button .= "<li><a href='".url('upahproduksi/rekanan/multi_bayar_upah?'.$param)."'><i class='icon-cash2'></i> Bayar Multi</a></li>";
                    }
                    

                    $button .= "</ul>";
                }else{
                    $tgl_bayar=tgl_full($d->tanggal_bayar,'');
                    $button = "<div class='btn-group'>";
                    if($d->id_proses==37){
                        $button .='<a class="btn btn-xs bg-success-400" type="button" href="javascript:void(0)" onclick="cetakUpahTenun(\''. Crypt::encrypt($d->id_upahproduksi) . '\', \''. $d->nomor . '\', \''. $pm . '\', \''. $pemroses . '\', \''. $d->makelar . '\', \''. Crypt::encrypt($d->id_kelompok) . '\', \''. Crypt::encrypt($d->id_rekanan) . '\', \''. Crypt::encrypt($d->id_pegawai) . '\')"><i class="icon-printer"></i></a>';
                    }else{
                        $button .='<a class="btn btn-xs bg-success-400" type="button" href="javascript:void(0)" onclick="cetakUpah(\''. Crypt::encrypt($d->id_upahproduksi) . '\', \''. $d->nomor . '\', \''. $pm . '\', \''. $pemroses . '\', \''. Crypt::encrypt($d->id_rekanan) . '\', \''. Crypt::encrypt($d->id_pegawai) . '\')"><i class="icon-printer"></i></a>';

                        // $button .= '<button type="button" class="btn btn-xs bg-info-400"><i class="icon-cash"></i></button>';
                    }
                    
                    // if ($pm == 'Pegawai') {
                        // $button .='<a target="_blank" class="btn btn-xs bg-success-400" type="button" href="'.url('upahproduksi/cetak_resi/'.$d->id_upahproduksi.'/'.$jeniss).'"><i class="icon-printer"></i></a>';
                        // $button .='<a class="btn btn-xs bg-success-400" type="button" href="javascript:void(0)" onclick="cetakUpah(\''. Crypt::encrypt($d->id_upahproduksi) . '\', \''. $d->nomor . '\', \''. $pm . '\', \''. $pemroses . '\')"><i class="icon-printer"></i></a>';
                    // } else {
                        // $button .='<a target="_blank" class="btn btn-xs bg-success-400" type="button" href="'.url('upahproduksi/rekanan/cetak_resi/'.$d->id_upahproduksi.'/'.$jeniss).'"><i class="icon-printer"></i></a>';
                        // $button .='<a class="btn btn-xs bg-success-400" type="button" href="javascript:void(0)" onclick="cetakUpah(\''. Crypt::encrypt($d->id_upahproduksi) . '\', \''. $d->nomor . '\', \''. $pm . '\', \''. $pemroses . '\')"><i class="icon-printer"></i></a>';
                    // }                    
                }
                $button .= "</div>";

                $ppn = 0;
                $total = 0;
                if ($d->pajak == "1") {
                    $ppn = ($d->total_upah+$d->total_upah_harian+$d->total_lain)*(10/100);
                }
                $total = $d->total_upah+$d->total_upah_harian+$d->total_lain+$ppn;

                $arr[] = array('id_barangmasuk' => $d->id_barangmasuk,
                                'nomor_upah' => $d->noAuto.$d->noAuto_ext,
                                'workorder' => $workorder,
                                'pemroses' => $pemroses,
                                'tanggal' => tgl_full($d->tanggal,''),
                                'tanggal_bayar'=> $tgl_bayar,
                                'nomor' => $d->nomor,
                                'proses' => $d->nama_proses,
                                'total' => $total,
                                'aksi' => $button);
            }
          }else{
                $arr = array();
          }
         // dd(DB::getQueryLog());
          /*<a class="btn  btn-xs bg-warning-400" type="button" href="'.url('upahproduksi/edit/'.$d->id_upahproduksi).'"><i class="icon-pencil"></i> </a>*/
        $data = new Collection($arr);
        return Datatables::of($data)
                ->make(true);
    
    }

    public function get_data_pegawai()
    {
        $data_pegawai = Pegawai::orderByRaw("nomor_pegawai")->get();
        return response()->json($data_pegawai);
    }

    public function get_data_rekanan()
    {
        $data_rekanan = Rekanan::orderByRaw("nama")->get();
        return response()->json($data_rekanan);
    }

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

    public function bayar_upah($id){
        $d_data = DB::table('pro_upahproduksi as pu')
            ->join('pro_barangmasuk as pbm','pu.id_barangmasuk','pbm.id_barangmasuk')
            ->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')
            ->join('ms_proses as mp','pu.id_proses','mp.id_proses')
            ->leftJoin('ms_rekanan as mr','pu.id_rekanan','mr.id_rekanan')
            ->leftjoin('ms_pegawai as mpe','pu.id_pegawai','mpe.id_pegawai')
            ->leftjoin('pem_workorder as pw','pu.id_workorder','pw.id_workorder')
            ->leftjoin('pem_workorder_barang as pwb','pu.id_workorder_barang','pwb.id_workorder_barang')
            ->select(DB::raw('
                pu.*,
                mp.nama_proses,
                mr.nama as nama_rekanan,
                mpe.nama_pegawai,
                pw.nomor as no_workorder,
                pwb.no_workorder as no_workorder_barang
            '))
            ->where('pu.id_upahproduksi','=', $id)->first();
        
        // dd(DB::getQueryLog());
        $data['barangkeluar'] = DB::table('pro_barangkeluar AS pb')->join('ms_proses AS mpr', 'pb.id_proses', 'mpr.id_proses')->leftJoin('ms_rekanan AS mr', 'pb.id_rekanan','mr.id_rekanan')->leftJoin('ms_pegawai AS mp', 'pb.id_pegawai', 'mp.id_pegawai')->leftJoin('pem_workorder AS pw', 'pb.id_workorder', 'pw.id_workorder')->leftJoin('pem_workorder_barang AS pwb', 'pb.id_workorder_barang', 'pwb.id_workorder_barang')->where('pb.status', '=', '2')->orderBy('pb.tanggal', 'desc')->orderBy('pb.status', 'asc')->select(DB::raw('pb.id_barangkeluar, pb.id_rekanan, mr.nama nama_rekanan, pb.id_pegawai, mp.nama_pegawai, pb.id_proses, mpr.nama_proses, pb.id_workorder, pw.nomor no_workorder, pb.id_workorder_barang, pwb.no_workorder no_workorder_barang, mpr.id_proses, mpr.nama_proses, pb.status'))->get();
        $data['rekanan'] = DB::table('ms_rekanan')->get();
        $data['pegawai'] = DB::table('ms_pegawai')->get();
        $data['id_trans']=$id;
        $data['jenis_trx']=1;
        $data['id_tabel']=1;
        $data['produksi'] = DB::table('ms_lokasi_produksi')->get();
        $data['data'] = $this->data($d_data);


        $akun = DB::table("ms_perkiraan")->whereIn("parent", [3, 7])->orderByRaw("kd_rek_1, kode_perkiraan")->get();
        $data['akun2'] = array_reduce($akun->toArray(), function($carry, $item){
            $carry[ $item->id ] = format_akun_perkiraan($item);
            return $carry;
        });
        
        // dd($data);
        
        return view('produksi.UpahProduksiBayar')->with('data',$data);

    }

    public function bayar_upah_rekanan($id){
        $d_data = DB::table('pro_upahproduksi as pu')->join('pro_barangmasuk as pbm','pu.id_barangmasuk','pbm.id_barangmasuk')->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')->join('ms_proses as mp','pu.id_proses','mp.id_proses')->leftJoin('ms_rekanan as mr','pu.id_rekanan','mr.id_rekanan')->leftjoin('ms_pegawai as mpe','pu.id_pegawai','mpe.id_pegawai')->leftjoin('pem_workorder as pw','pu.id_workorder','pw.id_workorder')->leftjoin('pem_workorder_barang as pwb','pu.id_workorder_barang','pwb.id_workorder_barang')->select(DB::raw('pu.*, mp.nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai, pw.nomor as no_workorder, pwb.no_workorder as no_workorder_barang'))->where('pu.id_upahproduksi','=', $id)->first();

        // echo '<pre>';print_r($d_data);exit();
        // dd(DB::getQueryLog());
        $data['barangkeluar'] = DB::table('pro_barangkeluar AS pb')->join('ms_proses AS mpr', 'pb.id_proses', 'mpr.id_proses')->leftJoin('ms_rekanan AS mr', 'pb.id_rekanan','mr.id_rekanan')->leftJoin('ms_pegawai AS mp', 'pb.id_pegawai', 'mp.id_pegawai')->leftJoin('pem_workorder AS pw', 'pb.id_workorder', 'pw.id_workorder')->leftJoin('pem_workorder_barang AS pwb', 'pb.id_workorder_barang', 'pwb.id_workorder_barang')->where('pb.status', '=', '2')->orderBy('pb.tanggal', 'desc')->orderBy('pb.status', 'asc')->select(DB::raw('pb.id_barangkeluar, pb.id_rekanan, mr.nama nama_rekanan, pb.id_pegawai, mp.nama_pegawai, pb.id_proses, mpr.nama_proses, pb.id_workorder, pw.nomor no_workorder, pb.id_workorder_barang, pwb.no_workorder no_workorder_barang, mpr.id_proses, mpr.nama_proses, pb.status'))->get();
        $data['rekanan'] = DB::table('ms_rekanan')->get();
        $data['pegawai'] = DB::table('ms_pegawai')->get();
        $data['id_trans']=$id;
        $data['jenis_trx']=1;
        $data['id_tabel']=1;
        $data['produksi'] = DB::table('ms_lokasi_produksi')->get();
        $data['data'] = $this->data($d_data);


        $akun = DB::table("ms_perkiraan")->whereIn("parent", [3, 7])->orderByRaw("kd_rek_1, kode_perkiraan")->get();
        $data['akun2'] = array_reduce($akun->toArray(), function($carry, $item){
            $carry[ $item->id ] = format_akun_perkiraan($item);
            return $carry;
        });
        
        // echo '<pre>';print_r($data);exit();
        
        return view('produksi.rekanan.UpahProduksiBayar')->with('data',$data);

    }

    public function multi_bayar_upah(Request $request)
    {
        $input = $request->all();

        // jika tidak ada parameter yg didapat
        if(sizeof($input) == 0){
            return redirect(url('upahproduksi'));

        }

        // apakah parameter sesuai yg diinginkan
        if( !in_array("pegawai", array_keys($input)) && !in_array("rekanan", array_keys($input)) ){
            return redirect(url('upahproduksi'));

        }

        // setting data where
        if(isset($input['pegawai'])){
            $where = [
                    [ "id_pegawai", "=", $input['pegawai'] ],
                ];
            $data['pegawai_rekanan'] = DB::table("ms_pegawai")->where($where)->first();
            $data['jenis'] = "id_pegawai";

        }else if(isset($input['rekanan'])){
            $where = [
                    [ "id_rekanan", "=", $input['rekanan'] ],
                ];
            $data['pegawai_rekanan'] = DB::table("ms_rekanan")->where($where)->first();
            $data['jenis'] = "id_rekanan";

        }else{
            return redirect(url('upahproduksi'));

        }

        $akun = dataAkunKasBank(); // helper SumberdanaHelper utk akun kas/bank
        $data['akun'] = array_reduce($akun->toArray(), function($carry, $item){
            $carry[ $item['id'] ] = format_akun_perkiraan($item);
            return $carry;
        });
        // $data['upah_produksi'] = DB::table("a_pro_upahproduksi")->where($where)->get();

        return view('produksi.UpahProduksiMultiBayar', $data);
    }

    public function multi_bayar_upah_rekanan(Request $request)
    {
        $input = $request->all();

        // jika tidak ada parameter yg didapat
        if(sizeof($input) == 0){
            return redirect(url('upahproduksi'));

        }

        // apakah parameter sesuai yg diinginkan
        if( !in_array("pegawai", array_keys($input)) && !in_array("rekanan", array_keys($input)) ){
            return redirect(url('upahproduksi'));

        }

        // setting data where
        if(isset($input['pegawai'])){
            $where = [
                    [ "id_pegawai", "=", $input['pegawai'] ],
                ];
            $data['pegawai_rekanan'] = DB::table("ms_pegawai")->where($where)->first();
            $data['jenis'] = "id_pegawai";

        }else if(isset($input['rekanan'])){
            $where = [
                    [ "id_rekanan", "=", $input['rekanan'] ],
                ];
            $data['pegawai_rekanan'] = DB::table("ms_rekanan")->where($where)->first();
            $data['jenis'] = "id_rekanan";

        }else{
            return redirect(url('upahproduksi'));

        }

        $akun = dataAkunKasBank(); // helper SumberdanaHelper utk akun kas/bank
        $data['akun'] = array_reduce($akun->toArray(), function($carry, $item){
            $carry[ $item['id'] ] = format_akun_perkiraan($item);
            return $carry;
        });
        // $data['upah_produksi'] = DB::table("a_pro_upahproduksi")->where($where)->get();

        return view('produksi.rekanan.UpahProduksiMultiBayar', $data);
    }

    public function get_multi_upah_produksi(Request $request)
    {
        $input = $request->all();
        
        if(isset($input['id_pegawai'])){
            $where = [
                    [ "up.id_pegawai", "=", $input['id_pegawai'] ],
                    // [ "tanggal", "<=", date_db($input['tanggal_bayar'])],
                    [ "up.tanggal", ">=", date_db($input['tanggal_awal'])],
                    [ "up.tanggal", "<=", date_db($input['tanggal_akhir'])],
                    [ "up.status_bayar", "=", 0],
                ];

        }else if(isset($input['id_rekanan'])){
            $where = [
                    [ "up.id_rekanan", "=", $input['id_rekanan'] ],
                    // [ "tanggal", "<=", date_db($input['tanggal_bayar'])],
                    [ "up.tanggal", ">=", date_db($input['tanggal_awal'])],
                    [ "up.tanggal", "<=", date_db($input['tanggal_akhir'])],
                    [ "up.status_bayar", "=", 0],
                ];

        }else{
            return redirect(url('upahproduksi'));

        }

        $upahproduksi = DB::table("a_pro_upahproduksi AS up")
                    ->join("pro_barangmasuk AS lh", "lh.id_barangmasuk", "=", "up.id_barangmasuk")
                    ->selectRaw("up.*, lh.noAuto, lh.noAuto_ext, lh.tanggal AS tanggal_laporan_hasil")
                    ->where($where)->get();
        return response()->json($upahproduksi);
    }

    public function get_multi_upah_produksi_rekanan(Request $request)
    {
        $input = $request->all();
        
        if(isset($input['id_pegawai'])){
            $where = [
                    [ "id_pegawai", "=", $input['id_pegawai'] ],
                    [ "tanggal", "<=", date_db($input['tanggal_bayar'])],
                    [ "status_bayar", "=", 0],
                ];

        }else if(isset($input['id_rekanan'])){
            $where = [
                    [ "id_rekanan", "=", $input['id_rekanan'] ],
                    [ "tanggal", "<=", date_db($input['tanggal_bayar'])],
                    [ "status_bayar", "=", 0],
                ];

        }else{
            return redirect(url('upahproduksi'));

        }

        $upahproduksi = DB::table("a_pro_upahproduksi")->where($where)->get();
        return response()->json($upahproduksi);
    }

    public function simpan_multi_bayar_upah(Request $request)
    {
        $input = $request->all();
        $id_upahproduksi = $input['id_upahproduksi'];
        $jumlah_bayar = $input['jumlah_bayar'];

        if(sizeof($id_upahproduksi) > 0){
            DB::beginTransaction();

            try {
                foreach ($id_upahproduksi as $key => $value) {
                    $data = [
                            "status_bayar"  => 1,
                            "akun_bayar"    => $input['akun_bayar'],
                            "tanggal_bayar" => date_db($input['tanggal_bayar']),
                            "jumlah_bayar"  => $jumlah_bayar[$key],
                        ];
                    DB::table("pro_upahproduksi")->where([ ["id_upahproduksi", "=", $value] ])->update($data);

                }
                DB::commit();
            } catch (Exception $e) {
                DB::rollBack();
            }
        }

        return redirect( url("upahproduksi") );
    }

    public function simpan_multi_bayar_upah_rekanan(Request $request)
    {
        $input = $request->all();
        $id_upahproduksi = $input['id_upahproduksi'];
        $jumlah_bayar = $input['jumlah_bayar'];

        if(sizeof($id_upahproduksi) > 0){
            DB::beginTransaction();

            try {
                foreach ($id_upahproduksi as $key => $value) {
                    $data = [
                            "status_bayar"  => 1,
                            "akun_bayar"    => $input['akun_bayar'],
                            "tanggal_bayar" => date_db($input['tanggal_bayar']),
                            "jumlah_bayar"  => $jumlah_bayar[$key],
                        ];
                    DB::table("pro_upahproduksi")->where([ ["id_upahproduksi", "=", $value] ])->update($data);

                }
                DB::commit();
                return redirect("upahproduksi");
            } catch (Exception $e) {
                DB::rollBack();
                return redirect()->back();
            }
        }
    }

    public function cetak_rekap_upah(Request $request)
    {
        $input = $request->all();
        $tanggal_awal = date_db($input['tanggal_awal']);
        $tanggal_akhir = date_db($input['tanggal_akhir']);
        

        $filter_id = $input['filter_id'];
        $set_where = "";
        if($input[$filter_id]==0){
            $set_where = "up.$filter_id != '$input[$filter_id]' AND ";
        }else if($input[$filter_id]!=0){
            $set_where = "up.$filter_id = '$input[$filter_id]' AND ";
        }

        /*---- Tak edit dewe ---*/
        $proses = $input['id_proses'];
        $set_where_proses = "";
        $nama_proses = "Semua Proses";
        if($proses!=0){
            $set_where_proses = "up.id_proses = '$proses' AND";
            $nama_proses = DB::table('ms_proses')->where('id_proses',$proses)->first()->nama_proses;
        }
        $pemroses = "";
        if($input['filter_id']=='id_rekanan' AND $input['id_rekanan']!=0){
            $pemroses = DB::table('ms_rekanan_kelompok')->where('id_rekanan',$input['id_rekanan'])->first()->nama.' (Rekanan)';
        }else if($input['filter_id']=='id_rekanan' AND $input['id_rekanan']==0){
            $pemroses = "Semua Rekanan";
        }else if($input['filter_id']=='id_pegawai' AND $input['id_pegawai']!=0){
            $d_pegawai = DB::table('ms_pegawai')->where('id_pegawai',$input['id_pegawai'])->first();
            $pemroses = $d_pegawai->nomor_pegawai." - ".$d_pegawai->nama_pegawai." (Pegawai)";
        }else if($input['filter_id']=='id_pegawai' AND $input['id_pegawai']==0){
            $pemroses = "Semua Pegawai";
        }
        /*---- END Tak edit dewe ---*/

        /*$q_upah_barang = "SELECT
                up.id_upahproduksi,
                up.id_barangmasuk,
                up.id_barangkeluar,
                up.id_proses,
                up.id_pegawai,
                up.id_rekanan,
                up.nomor,
                up.id_workorder,
                up.id_workorder_barang,
                up.id_lokasi,
                up.tanggal,
                up.id_jenis,
                up.status_bayar,
                up.akun_bayar,
                up.tanggal_bayar,
                up.jumlah_bayar,
                `ut`.*, 
                `mb`.`id` AS `id_barang`,
                `mb`.`kode_barang` AS `kode_barang`,
                `mkb`.`id_kelompok` AS `id_kelompok`,
                `mkb2`.`id_kb` AS `id_kategori`,
                `mkb2`.`nama` AS `kategori`,
                `mkb`.`id_kb` AS `id_sub_kategori`,
                `mkb`.`nama` AS `sub_kategori`,
                `mb`.`nama_barang` AS `nama_barang`,
                `ms`.`nama_satuan` AS `nama_satuan`,
                `ms`.`satuan` AS `satuan`,
                mf.nama_motif,
                mfb.motif_keterangan,
                pr.nama_proses,
                rk.nama AS nama_rekanan,
                pg.nomor_pegawai,
                pg.nama_pegawai
            FROM
                `pro_upah_total` `ut`
            LEFT JOIN pro_upahproduksi up ON up.id_upahproduksi = ut.id_upah_borongan
            JOIN `ms_barang` `mb` ON `ut`.`id_barang` = `mb`.`id`
            JOIN `ms_satuan` `ms` ON `mb`.`id_satuan` = `ms`.`id_satuan`
            JOIN `ms_kategori_barang` `mkb` ON `mb`.`id_kb` = `mkb`.`id_kb`
            JOIN `ms_kategori_barang` `mkb2` ON `mkb`.`induk_kb` = `mkb2`.`id_kb`
            LEFT JOIN ms_motif mf ON mf.id_motif = ut.id_motif
            LEFT JOIN ms_motif_barang mfb ON mfb.id_motif_barang = ut.id_motif_barang
            JOIN ms_proses pr ON pr.id_proses = up.id_proses
            LEFT JOIN ms_rekanan rk ON rk.id_rekanan = up.id_rekanan
            LEFT JOIN ms_pegawai pg ON pg.id_pegawai = up.id_pegawai
            WHERE 
                $set_where
                up.status_bayar = 1 AND
                up.tanggal_bayar BETWEEN '$tanggal_awal' AND '$tanggal_akhir'
            ORDER BY up.tanggal_bayar";*/
            $q_upah_barang = "SELECT
                up.id_upahproduksi,
                up.id_barangmasuk,
                up.id_barangkeluar,
                up.id_proses,
                up.id_pegawai,
                up.id_rekanan,
                up.nomor,
                up.id_workorder,
                up.id_workorder_barang,
                up.id_lokasi,
                up.tanggal,
                up.id_jenis,
                up.status_bayar,
                up.akun_bayar,
                up.tanggal_bayar,
                up.jumlah_bayar,
                `ut`.*, 
                SUM(`ut`.jumlah_barang) AS jumlah_barang_sum,
                SUM(`ut`.jumlah_barang*`ut`.upah_satuan) AS upah_total_sum,
                `mb`.`id` AS `id_barang`,
                `mb`.`kode_barang` AS `kode_barang`,
                `mkb`.`id_kelompok` AS `id_kelompok`,
                `mkb2`.`id_kb` AS `id_kategori`,
                `mkb2`.`nama` AS `kategori`,
                `mkb`.`id_kb` AS `id_sub_kategori`,
                `mkb`.`nama` AS `sub_kategori`,
                `mb`.`nama_barang` AS `nama_barang`,
                `ms`.`nama_satuan` AS `nama_satuan`,
                `ms`.`satuan` AS `satuan`,
                mf.nama_motif,
                mfb.motif_keterangan,
                pr.nama_proses,
                rk.nama AS nama_rekanan,
                pg.nomor_pegawai,
                pg.nama_pegawai
            FROM
                `pro_upah_total` `ut`
            LEFT JOIN pro_upahproduksi up ON up.id_upahproduksi = ut.id_upah_borongan
            JOIN `ms_barang` `mb` ON `ut`.`id_barang` = `mb`.`id`
            JOIN `ms_satuan` `ms` ON `mb`.`id_satuan` = `ms`.`id_satuan`
            JOIN `ms_kategori_barang` `mkb` ON `mb`.`id_kb` = `mkb`.`id_kb`
            JOIN `ms_kategori_barang` `mkb2` ON `mkb`.`induk_kb` = `mkb2`.`id_kb`
            LEFT JOIN ms_motif mf ON mf.id_motif = ut.id_motif
            LEFT JOIN ms_motif_barang mfb ON mfb.id_motif_barang = ut.id_motif_barang
            JOIN ms_proses pr ON pr.id_proses = up.id_proses
            LEFT JOIN ms_rekanan rk ON rk.id_rekanan = up.id_rekanan
            LEFT JOIN ms_pegawai pg ON pg.id_pegawai = up.id_pegawai
            WHERE 
                $set_where $set_where_proses
                up.status_bayar = 1 AND
                up.tanggal_bayar BETWEEN '$tanggal_awal' AND '$tanggal_akhir'
            GROUP BY 
                up.$filter_id, 
                up.id_proses, 
                mb.id, 
                ut.id_motif,
                ut.id_motif_barang
            ORDER BY up.tanggal_bayar";
        // DB::enableQueryLog();
        $data_upah_barang = DB::select($q_upah_barang);
        $data['upah_barang'] = array_reduce($data_upah_barang, function($carry, $item){
            $carry[$item->id_upahproduksi][] = $item;
            return $carry;
        });
        // dd(DB::getQueryLog());

        /*$q_upah_lain = "SELECT
                up.id_upahproduksi,
                up.id_barangmasuk,
                up.id_barangkeluar,
                up.id_proses,
                up.id_pegawai,
                up.id_rekanan,
                up.nomor,
                up.id_workorder,
                up.id_workorder_barang,
                up.id_lokasi,
                up.tanggal,
                up.id_jenis,
                up.status_bayar,
                up.akun_bayar,
                up.tanggal_bayar,
                up.jumlah_bayar,
                `ul`.*,
                pr.nama_proses,
                rk.nama AS nama_rekanan,
                pg.nomor_pegawai,
                pg.nama_pegawai
            FROM
                `pro_upah_borongan_lain_lain` `ul`
            LEFT JOIN pro_upahproduksi up ON up.id_upahproduksi = ul.id_upah_borongan
            JOIN ms_proses pr ON pr.id_proses = up.id_proses
            LEFT JOIN ms_rekanan rk ON rk.id_rekanan = up.id_rekanan
            LEFT JOIN ms_pegawai pg ON pg.id_pegawai = up.id_pegawai
            WHERE 
                $set_where
                up.status_bayar = 1 AND
                up.tanggal_bayar BETWEEN '$tanggal_awal' AND '$tanggal_akhir'
            ORDER BY up.tanggal_bayar";*/
        $q_upah_lain = "SELECT
                up.id_upahproduksi,
                up.id_barangmasuk,
                up.id_barangkeluar,
                up.id_proses,
                up.id_pegawai,
                up.id_rekanan,
                up.nomor,
                up.id_workorder,
                up.id_workorder_barang,
                up.id_lokasi,
                up.tanggal,
                up.id_jenis,
                up.status_bayar,
                up.akun_bayar,
                up.tanggal_bayar,
                up.jumlah_bayar,
                `ul`.*,
                pr.nama_proses,
                rk.nama AS nama_rekanan,
                pg.nomor_pegawai,
                pg.nama_pegawai
            FROM
                `pro_upah_borongan_lain_lain` `ul`
            LEFT JOIN pro_upahproduksi up ON up.id_upahproduksi = ul.id_upah_borongan
            JOIN ms_proses pr ON pr.id_proses = up.id_proses
            LEFT JOIN ms_rekanan rk ON rk.id_rekanan = up.id_rekanan
            LEFT JOIN ms_pegawai pg ON pg.id_pegawai = up.id_pegawai
            WHERE 
                $set_where $set_where_proses
                up.status_bayar = 1 AND
                up.tanggal_bayar BETWEEN '$tanggal_awal' AND '$tanggal_akhir'
            ORDER BY up.tanggal_bayar";

        $data_upah_lain = DB::select($q_upah_lain);
        $data['upah_lain'] = array_reduce($data_upah_lain, function($carry, $item){
            // $carry[$item->id_upahproduksi][] = $item;
            $carry[] = $item;
            return $carry;
        });

        $data['judul'] = "Rekap Pembayaran Upah Produksi ".$nama_proses;
        $data['periode'] = date_db($tanggal_awal, true)." s/d ".date_db($tanggal_akhir, true);
        $data['pemroses'] = $pemroses;
        // dd($data);
        require('fpdf181/Mc_table.php');
        $html = view("produksi.UpahRekapCetakPortrait", $data);
        return response($html)->header('Content-Type', 'application/pdf');

    }

    public function cetakUpahTenun(Request $request)
    {
        //
    }

    public function cetakUpah(Request $request)
    {
        $input          = $request->all();
        $tanggal_awal   = date_db($input['tgl_awal']);
        $tanggal_akhir  = date_db($input['tgl_akhir']);
        $filter_cetak   = isset($input['filter_cetak']) ? $input['filter_cetak'] : $input['filter_cetak_tenun'];
        $jenis          = $input['jenis'];
        $id             = $input['id_upahproduksi'];
        $nomor          = $input['nomor'];
        $rekananID      = $input['idrekanan'];
        $pegawaiID      = $input['idpegawai'];
        $kelompokID     = isset($input['idkelompok']) ? $input['idkelompok'] : Crypt::encrypt(0);
        $idProses       = $input['id_proses'];
        $tipeLaporan    = $input['tipe_laporan'];

        try {
            $id        = Crypt::decrypt($id);
            $rekananID = Crypt::decrypt($rekananID);
            $pegawaiID = Crypt::decrypt($pegawaiID);
            $kelompokID = Crypt::decrypt($kelompokID);
        } catch (DecryptException $e) {
            return view('errors/503');
        }
        $arr = array();
        if ($filter_cetak == "nomor") {
            if($jenis == "Rekanan"){
               $d_data = DB::table('pro_upahproduksi as pu')
                    ->join('pro_barangmasuk as pbm','pu.id_barangmasuk','pbm.id_barangmasuk')
                    ->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')
                    ->join('ms_proses as mp','pu.id_proses','mp.id_proses')
                    ->leftJoin('ms_rekanan as mr','pu.id_rekanan','mr.id_rekanan')
                    ->leftJoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')
                    ->leftjoin('ms_pegawai as mpe','pu.id_pegawai','mpe.id_pegawai')
                    ->leftjoin('pem_workorder as pw','pu.id_workorder','pw.id_workorder')
                    ->leftjoin('pem_workorder_barang as pwb','pu.id_workorder_barang','pwb.id_workorder_barang')
                    ->select(DB::raw('pu.*, mrk.nama as nama_makelar, mp.nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai, pw.nomor as no_workorder, pwb.no_workorder as no_workorder_barang'))
                    ->where('pu.id_upahproduksi','=', $id)
                    ->where('pu.nomor','=', $nomor)
                    ->first();

                $arr['data_penerima'] = $d_data;

                $id_barangmasuk       = $d_data->id_barangmasuk;
                $id_barangkeluar      = $d_data->id_barangkeluar;
                $id_workorder         = $d_data->id_workorder;
                $id_workorder_barang  = $d_data->id_workorder_barang;
                $id_proses            = $d_data->id_proses;
                $id_rekanan           = $d_data->id_rekanan;
                $id_pegawai           = $d_data->id_pegawai;
                $id_upah_borongan     = $id;
                $id_jenis             = $d_data->id_jenis;

                if($id_workorder != '' || $id_workorder != 0){
                    $order       = 'id_workorder';
                    $where_order = $id_workorder;
                }else{
                    $order       = 'id_workorder_barang';
                    $where_order = $id_workorder_barang;
                }

                if($id_rekanan != '' || $id_rekanan != 0){
                    $pemroses       = 'pbk.id_rekanan';
                    $where_pemroses = $id_rekanan;
                }else{
                    $pemroses       = 'pbk.id_pegawai';
                    $where_pemroses = 'pbk.id_pegawai';
                }

                $d_hasil = DB::table('pro_barangmasuk AS pb')
                            ->join('pro_barangkeluar as pbk','pb.id_barangkeluar','pbk.id_barangkeluar')
                            ->join('pro_barangmasuk_barang as pbb', 'pb.id_barangmasuk', 'pbb.id_barangmasuk')
                            ->join('ms_gudang as mg', 'pbb.id_gudang', 'mg.id_gudang')
                            ->join('ms_barang as mb', 'pbb.id_barang', 'mb.id')
                            ->leftjoin('ms_warnakomposisi as mwk', 'pbb.id_komposisi', 'mwk.id_komposisi')
                            ->join('ms_satuan AS ms', 'pbb.id_satuan', 'ms.id_satuan')
                            ->leftJoin('ms_motif_barang as mmb', 'pbb.id_motif_barang', 'mmb.id_motif_barang')
                            ->leftJoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')
                            ->leftJoin('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')
                            ->leftJoin('ms_barang as mb2', 'pbb.id_label', 'mb2.id')
                            ->leftJoin('ms_barang as mb3', 'pbb.id_kemasan', 'mb3.id')
                            ->leftJoin('ms_barang as mb4', 'pbb.id_kemasansatuan', 'mb4.id')
                            ->leftJoin('ms_barang as mb5', 'pbb.id_kemasansepuluh', 'mb5.id')
                            ->leftJoin('ms_barang as mb6', 'pbb.id_kemasanseratus', 'mb6.id')
                            ->select(DB::raw('pbb.*, pbk.id_proses, pbk.id_rekanan, mb.kode_barang, mb.nama_barang, mg.id_gudang, mg.nama_gudang, pbb.id_satuan, ms.nama_satuan, 0 AS upah,
                                CASE WHEN mwk.id_komposisi IS NULL THEN "" ELSE  mwk.id_komposisi END AS id_komposisi, 
                                CASE WHEN mwk.nama IS NULL THEN "" ELSE  mwk.nama END AS nama_komposisi, 
                                CASE WHEN mmb.id_motif_barang IS NULL THEN "" ELSE mmb.id_motif_barang END AS id_motif_barang, 
                                CASE WHEN mmb.motif_keterangan IS NULL THEN "" ELSE mmb.motif_keterangan END AS motif_keterangan, 
                                CASE WHEN mm.id_motif IS NULL THEN "" ELSE mm.id_motif END AS id_motif, 
                                CASE WHEN mm.nama_motif IS NULL THEN "" ELSE mm.nama_motif END AS nama_motif, 
                                CASE WHEN mk.id_kualitas IS NULL THEN "" ELSE mk.id_kualitas END AS id_kualitas, 
                                CASE WHEN mk.nama_kualitas IS NULL THEN "" ELSE mk.nama_kualitas END AS nama_kualitas,
                                CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" ELSE pbb.id_kualitas_dudul END AS id_kualitas_dudul,
                                CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" WHEN pbb.id_kualitas_dudul = 1 THEN "Baik" ELSE "Jelek" END AS kualitas_dudul,
                                CASE WHEN pbb.id_label = 0 THEN "" ELSE pbb.id_label END AS id_label, CASE WHEN mb2.nama_barang IS NULL THEN "" ELSE mb2.nama_barang END AS nama_label,
                                CASE WHEN pbb.id_kemasan = 0 THEN "" ELSE pbb.id_kemasan END AS id_kemasan,
                                CASE WHEN mb3.nama_barang IS NULL THEN "" ELSE mb3.nama_barang END AS nama_kemasan,
                                CASE WHEN pbb.id_kemasansatuan = 0 THEN "" ELSE pbb.id_kemasansatuan END AS id_kemasansatuan,
                                CASE WHEN mb4.nama_barang IS NULL THEN "" ELSE mb4.nama_barang END AS nama_kemasansatuan,
                                CASE WHEN pbb.id_kemasansepuluh = 0 THEN "" ELSE pbb.id_kemasansepuluh END AS id_kemasansepuluh,
                                CASE WHEN mb5.nama_barang IS NULL THEN "" ELSE mb5.nama_barang END AS nama_kemasansepuluh,
                                CASE WHEN pbb.id_kemasanseratus = 0 THEN "" ELSE pbb.id_kemasanseratus END AS id_kemasanseratus,
                                CASE WHEN mb6.nama_barang IS NULL THEN "" ELSE mb6.nama_barang END AS nama_kemasanseratus'))
                            ->where('pb.id_barangmasuk', $id_barangmasuk)
                            ->where('pb.id_barangkeluar', $id_barangkeluar)
                            ->where('pbk.id_rekanan',$id_rekanan)
                            ->where($order,$where_order)
                            ->where('pbk.id_proses',$id_proses);

                $d_lain2 = DB::table('pro_upah_borongan_lain_lain')
                            ->where('id_upah_borongan', '=', $id_upah_borongan);

                $d_upah_bayar = DB::table('pro_upah_total')
                                ->where('id_upah_borongan','=',$id_upah_borongan);

                if($id_jenis == '1'){
                    $d_upah =  DB::table('ms_upah_borongan as mub')
                                ->join('ms_proses as mp', 'mp.id_proses', 'mub.id_proses')
                                ->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')
                                ->select(DB::raw('mub.*, mp.nama_proses, 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'))
                                ->where('mub.id_proses',$id_proses);

                    $status_upah = '1';
                }else{
                    $d_upah      =  DB::table('ms_upah_pegawai as mup')
                                    ->where('mup.id_pegawai',$id_pegawai);
                    $status_upah = '2';
                }
                    
                if($d_hasil->count() > 0){
                    $arr['hasil']        = $d_hasil->get();
                    $arr['status_hasil'] = array('1');
                }else{
                    $arr['status_hasil'] = array('0');
                }

                if($d_upah->count() > 0){
                    $arr['upah']        = $d_upah->get();
                    $arr['s_upah']      = $status_upah;
                    $arr['status_upah'] = array('1');
                }else{
                    $arr['status_upah'] = array('0');
                }

                if ($d_lain2->count()>0) {
                    $arr['lain2']        = $d_lain2->get();
                    $arr['status_lain2'] = array('1');
                }else{
                    $arr['status_lain2'] = array('1');                    
                    $arr['lain2']        = '';
                }

                if($d_upah_bayar->count()>0){
                    $arr['upah_bayar'] = $d_upah_bayar->get();
                }else{
                    $arr['upah_bayar'] = '';
                }
                $arr['author'] = Session::get('nama');

                require('fpdf181/Mc_table.php');
                if ($d_data->id_proses == 37) {
                    $html = view('produksi.cetak.CetakResiRekananTenun')->with('data',$arr);
                } else {
                    $html = view('produksi.cetak.CetakResiRekanan')->with('data',$arr);
                }
                
                return response($html)->header('Content-Type', 'application/pdf');
            }else if($jenis == "Pegawai"){
                $d_data = DB::table('pro_upahproduksi as pu')
                        ->join('pro_barangmasuk as pbm','pu.id_barangmasuk','pbm.id_barangmasuk')
                        ->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')
                        ->join('ms_proses as mp','pu.id_proses','mp.id_proses')
                        ->leftJoin('ms_rekanan as mr','pu.id_rekanan','mr.id_rekanan')
                        ->leftjoin('ms_pegawai as mpe','pu.id_pegawai','mpe.id_pegawai')
                        ->leftjoin('pem_workorder as pw','pu.id_workorder','pw.id_workorder')
                        ->leftjoin('pem_workorder_barang as pwb','pu.id_workorder_barang','pwb.id_workorder_barang')
                        ->select(DB::raw('pu.*, mp.nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai, pw.nomor as no_workorder, pwb.no_workorder as no_workorder_barang'))
                        ->where('pu.id_upahproduksi','=', $id)
                        ->where('pu.nomor','=', $nomor)
                        ->first();

                $arr['data_penerima'] = $d_data;

                $id_barangmasuk      = $d_data->id_barangmasuk;
                $id_barangkeluar     = $d_data->id_barangkeluar;
                $id_workorder        = $d_data->id_workorder;
                $id_workorder_barang = $d_data->id_workorder_barang;
                $id_proses           = $d_data->id_proses;
                $id_pegawai          = $d_data->id_pegawai;
                $id_jenis            = $d_data->id_jenis;
                $id_upah_borongan    = $id;

                if($id_workorder != '' || $id_workorder != 0){
                    $order       = 'id_workorder';
                    $where_order = $id_workorder;
                }else{
                    $order       = 'id_workorder_barang';
                    $where_order = $id_workorder_barang;
                }
                DB::enableQueryLog();

                $d_hasil = DB::table('pro_barangmasuk AS pb')
                            ->join('pro_barangkeluar as pbk','pb.id_barangkeluar','pbk.id_barangkeluar')
                            ->join('pro_barangmasuk_barang as pbb', 'pb.id_barangmasuk', 'pbb.id_barangmasuk')
                            ->join('ms_gudang as mg', 'pbb.id_gudang', 'mg.id_gudang')
                            ->join('ms_barang as mb', 'pbb.id_barang', 'mb.id')
                            ->leftjoin('ms_warnakomposisi as mwk', 'pbb.id_komposisi', 'mwk.id_komposisi')
                            ->join('ms_satuan AS ms', 'pbb.id_satuan', 'ms.id_satuan')
                            ->leftJoin('ms_motif_barang as mmb', 'pbb.id_motif_barang', 'mmb.id_motif_barang')
                            ->leftJoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')
                            ->leftJoin('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')
                            ->leftJoin('ms_barang as mb2', 'pbb.id_label', 'mb2.id')
                            ->leftJoin('ms_barang as mb3', 'pbb.id_kemasan', 'mb3.id')
                            ->leftJoin('ms_barang as mb4', 'pbb.id_kemasansatuan', 'mb4.id')
                            ->leftJoin('ms_barang as mb5', 'pbb.id_kemasansepuluh', 'mb5.id')
                            ->leftJoin('ms_barang as mb6', 'pbb.id_kemasanseratus', 'mb6.id')
                            ->select(DB::raw('pbb.*, pbk.id_proses , mb.kode_barang, mb.nama_barang, mg.id_gudang, mg.nama_gudang, pbb.id_satuan, ms.nama_satuan, 0 AS upah, 
                                CASE WHEN mwk.id_komposisi IS NULL THEN "" ELSE  mwk.id_komposisi END AS id_komposisi, 
                                CASE WHEN mwk.nama IS NULL THEN "" ELSE  mwk.nama END AS nama_komposisi, 
                                CASE WHEN mmb.id_motif_barang IS NULL THEN "" ELSE mmb.id_motif_barang END AS id_motif_barang, 
                                CASE WHEN mmb.motif_keterangan IS NULL THEN "" ELSE mmb.motif_keterangan END AS motif_keterangan, 
                                CASE WHEN mm.id_motif IS NULL THEN "" ELSE mm.id_motif END AS id_motif, 
                                CASE WHEN mm.nama_motif IS NULL THEN "" ELSE mm.nama_motif END AS nama_motif, 
                                CASE WHEN mk.id_kualitas IS NULL THEN "" ELSE mk.id_kualitas END AS id_kualitas, 
                                CASE WHEN mk.nama_kualitas IS NULL THEN "" ELSE mk.nama_kualitas END AS nama_kualitas,
                                CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" ELSE pbb.id_kualitas_dudul END AS id_kualitas_dudul,
                                CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" WHEN pbb.id_kualitas_dudul = 1 THEN "Baik" ELSE "Jelek" END AS kualitas_dudul,
                                CASE WHEN pbb.id_label = 0 THEN "" ELSE pbb.id_label END AS id_label, CASE WHEN mb2.nama_barang IS NULL THEN "" ELSE mb2.nama_barang END AS nama_label,
                                CASE WHEN pbb.id_kemasan = 0 THEN "" ELSE pbb.id_kemasan END AS id_kemasan,
                                CASE WHEN mb3.nama_barang IS NULL THEN "" ELSE mb3.nama_barang END AS nama_kemasan,
                                CASE WHEN pbb.id_kemasansatuan = 0 THEN "" ELSE pbb.id_kemasansatuan END AS id_kemasansatuan,
                                CASE WHEN mb4.nama_barang IS NULL THEN "" ELSE mb4.nama_barang END AS nama_kemasansatuan,
                                CASE WHEN pbb.id_kemasansepuluh = 0 THEN "" ELSE pbb.id_kemasansepuluh END AS id_kemasansepuluh,
                                CASE WHEN mb5.nama_barang IS NULL THEN "" ELSE mb5.nama_barang END AS nama_kemasansepuluh,
                                CASE WHEN pbb.id_kemasanseratus = 0 THEN "" ELSE pbb.id_kemasanseratus END AS id_kemasanseratus,
                                CASE WHEN mb6.nama_barang IS NULL THEN "" ELSE mb6.nama_barang END AS nama_kemasanseratus'))
                            ->where('pb.id_barangmasuk', $id_barangmasuk)
                            ->where('pb.id_barangkeluar', $id_barangkeluar)
                            ->where('pbk.id_pegawai',$id_pegawai)
                            ->where($order,$where_order)
                            ->where('pbk.id_proses',$id_proses);

                $d_lain2 = DB::table('pro_upah_borongan_lain_lain')->where('id_upah_borongan', '=', $id_upah_borongan);

                // $upah_total = DB::table('pro_upah_total AS up')->selectRaw("id_pbb")->where("id_upah_borongan", "=", $id_upah_borongan)->groupBy("id_upah_borongan")->first();
                // $view_masuk_all = "SELECT * FROM view_masuk_all WHERE id=$upah_total->id_pbb";
                $d_upah_bayar=DB::table('pro_upah_total AS up')
                        ->join(DB::raw("view_masuk_all AS pbb"), function($join) {
                            $join->on("pbb.id_detail", "=", "up.id_pbb");
                        })
                        ->selectRaw("pbb.*, up.upah_satuan, pbb.jum_barang AS jumlah")
                        ->where('up.id_upah_borongan','=',$id_upah_borongan);

                if($id_jenis == '1'){
                    $d_upah =  DB::table('ms_upah_borongan as mub')
                                ->join('ms_proses as mp', 'mp.id_proses', 'mub.id_proses')
                                ->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')
                                ->select(DB::raw('mub.*, mp.nama_proses, 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'))
                                ->where('mub.id_proses',$id_proses);

                    $status_upah = '1';    
                }else{    
                    $d_upah =  DB::table('ms_upah_pegawai as mup')->where('mup.id_pegawai',$id_pegawai);
                    $status_upah = '2';        
                }
                
                if($d_hasil->count() > 0){
                    $arr['hasil']        = $d_hasil->get();
                    $arr['status_hasil'] = array('1');
                }else{
                    $arr['status_hasil'] = array('0');
                }

                if($d_upah->count() > 0){
                    $arr['upah']        = $d_upah->get();
                    $arr['s_upah']      = $status_upah;
                    $arr['status_upah'] = array('1');
                }else{
                    $arr['status_upah'] = array('0');
                }
                if ($d_lain2->count()>0) {
                    $arr['lain2']        = $d_lain2->get();
                    $arr['status_lain2'] = array('1');
                }else{
                    $arr['lain2']        = '';
                    $arr['status_lain2'] = array('0');
                }
                if($d_upah_bayar->count()>0){
                    $arr['upah_bayar'] = $d_upah_bayar->get();
                }else{
                    $arr['upah_bayar'] = '';
                }
                // dd(DB::getQueryLog());
                // dd($arr);
                require('fpdf181/Mc_table.php');
                $html = view('produksi.cetak.CetakResi')->with('data',$arr);
                return response($html)->header('Content-Type', 'application/pdf');
            }
        } else if ($filter_cetak == "pemroses") { // opsi pemroses
            if ($jenis == "Rekanan") {
                $d_data = DB::table('pro_upahproduksi as pu')
                    ->join('pro_barangmasuk as pbm','pu.id_barangmasuk','pbm.id_barangmasuk')
                    ->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')
                    ->join('ms_proses as mp','pu.id_proses','mp.id_proses')
                    ->leftJoin('ms_rekanan as mr','pu.id_rekanan','mr.id_rekanan')
                    ->leftJoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')
                    ->leftjoin('ms_pegawai as mpe','pu.id_pegawai','mpe.id_pegawai')
                    ->leftjoin('pem_workorder as pw','pu.id_workorder','pw.id_workorder')
                    ->leftjoin('pem_workorder_barang as pwb','pu.id_workorder_barang','pwb.id_workorder_barang')
                    ->select(DB::raw('pu.*, mrk.nama as nama_makelar, mp.nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai, pw.nomor as no_workorder, pwb.no_workorder as no_workorder_barang'))
                    ->where('pu.id_rekanan', '=', $rekananID)
                    ->where('status_bayar', '=', "1")
                    ->whereBetween('pu.tanggal', [$tanggal_awal, $tanggal_akhir]);

                if ($d_data->count() > 0) {
                    foreach ($d_data->get() as $key => $value) {
                        $id_barangmasuk       = $value->id_barangmasuk;
                        $id_barangkeluar      = $value->id_barangkeluar;
                        $id_workorder         = $value->id_workorder;
                        $id_workorder_barang  = $value->id_workorder_barang;
                        $id_proses            = $value->id_proses;
                        $id_rekanan           = $value->id_rekanan;
                        $id_pegawai           = $value->id_pegawai;
                        $id_upah_borongan     = $value->id_upahproduksi;
                        $id_jenis             = $value->id_jenis;
                        $tanggal              = $value->tanggal;

                        if($id_workorder != '' || $id_workorder != 0){
                            $order       = 'pbk.id_workorder';
                            $where_order = $id_workorder;
                        }else{
                            $order       = 'pbk.id_workorder_barang';
                            $where_order = $id_workorder_barang;
                        }

                        if($id_rekanan != '' || $id_rekanan != 0){
                            $pemroses       = 'pbk.id_rekanan';
                            $where_pemroses = $id_rekanan;
                        }else{
                            $pemroses       = 'pbk.id_pegawai';
                            $where_pemroses = 'pbk.id_pegawai';
                        }

                        $d_hasil = DB::table('pro_barangmasuk AS pb')
                                    ->join('pro_barangkeluar as pbk','pb.id_barangkeluar','pbk.id_barangkeluar')
                                    ->join('pro_barangmasuk_barang as pbb', 'pb.id_barangmasuk', 'pbb.id_barangmasuk')
                                    ->join('pro_upahproduksi as pu','pu.id_barangmasuk','pb.id_barangmasuk')
                                    ->join('ms_gudang as mg', 'pbb.id_gudang', 'mg.id_gudang')
                                    ->join('ms_barang as mb', 'pbb.id_barang', 'mb.id')
                                    ->leftjoin('ms_warnakomposisi as mwk', 'pbb.id_komposisi', 'mwk.id_komposisi')
                                    ->join('ms_satuan AS ms', 'pbb.id_satuan', 'ms.id_satuan')
                                    ->leftJoin('ms_motif_barang as mmb', 'pbb.id_motif_barang', 'mmb.id_motif_barang')
                                    ->leftJoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')
                                    ->leftJoin('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')
                                    ->leftJoin('ms_barang as mb2', 'pbb.id_label', 'mb2.id')
                                    ->leftJoin('ms_barang as mb3', 'pbb.id_kemasan', 'mb3.id')
                                    ->leftJoin('ms_barang as mb4', 'pbb.id_kemasansatuan', 'mb4.id')
                                    ->leftJoin('ms_barang as mb5', 'pbb.id_kemasansepuluh', 'mb5.id')
                                    ->leftJoin('ms_barang as mb6', 'pbb.id_kemasanseratus', 'mb6.id')
                                    ->select(DB::raw('pbb.*, pbk.id_proses, mb.kode_barang, mb.nama_barang, mg.id_gudang, mg.nama_gudang, pbb.id_satuan, ms.nama_satuan, 0 AS upah,
                                        CASE WHEN mwk.id_komposisi IS NULL THEN "" ELSE  mwk.id_komposisi END AS id_komposisi, 
                                        CASE WHEN mwk.nama IS NULL THEN "" ELSE  mwk.nama END AS nama_komposisi, 
                                        CASE WHEN mmb.id_motif_barang IS NULL THEN "" ELSE mmb.id_motif_barang END AS id_motif_barang, 
                                        CASE WHEN mmb.motif_keterangan IS NULL THEN "" ELSE mmb.motif_keterangan END AS motif_keterangan, 
                                        CASE WHEN mm.id_motif IS NULL THEN "" ELSE mm.id_motif END AS id_motif, 
                                        CASE WHEN mm.nama_motif IS NULL THEN "" ELSE mm.nama_motif END AS nama_motif, 
                                        CASE WHEN mk.id_kualitas IS NULL THEN "" ELSE mk.id_kualitas END AS id_kualitas, 
                                        CASE WHEN mk.nama_kualitas IS NULL THEN "" ELSE mk.nama_kualitas END AS nama_kualitas,
                                        CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" ELSE pbb.id_kualitas_dudul END AS id_kualitas_dudul,
                                        CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" WHEN pbb.id_kualitas_dudul = 1 THEN "Baik" ELSE "Jelek" END AS kualitas_dudul,
                                        CASE WHEN pbb.id_label = 0 THEN "" ELSE pbb.id_label END AS id_label, CASE WHEN mb2.nama_barang IS NULL THEN "" ELSE mb2.nama_barang END AS nama_label,
                                        CASE WHEN pbb.id_kemasan = 0 THEN "" ELSE pbb.id_kemasan END AS id_kemasan,
                                        CASE WHEN mb3.nama_barang IS NULL THEN "" ELSE mb3.nama_barang END AS nama_kemasan,
                                        CASE WHEN pbb.id_kemasansatuan = 0 THEN "" ELSE pbb.id_kemasansatuan END AS id_kemasansatuan,
                                        CASE WHEN mb4.nama_barang IS NULL THEN "" ELSE mb4.nama_barang END AS nama_kemasansatuan,
                                        CASE WHEN pbb.id_kemasansepuluh = 0 THEN "" ELSE pbb.id_kemasansepuluh END AS id_kemasansepuluh,
                                        CASE WHEN mb5.nama_barang IS NULL THEN "" ELSE mb5.nama_barang END AS nama_kemasansepuluh,
                                        CASE WHEN pbb.id_kemasanseratus = 0 THEN "" ELSE pbb.id_kemasanseratus END AS id_kemasanseratus,
                                        CASE WHEN mb6.nama_barang IS NULL THEN "" ELSE mb6.nama_barang END AS nama_kemasanseratus, pu.tanggal as tanggal, pu.nomor as nomor, pu.pajak as pajak'))
                                    ->where('pb.id_barangmasuk', $id_barangmasuk)
                                    ->where('pb.id_barangkeluar', $id_barangkeluar)
                                    ->where('pbk.id_rekanan',$id_rekanan)
                                    ->where($order,$where_order)
                                    ->where('pbk.id_proses',$id_proses);

                        $d_lain2 = DB::table('pro_upah_borongan_lain_lain as publ')
                                    ->join('pro_upahproduksi as pu','pu.id_upahproduksi','publ.id_upah_borongan')
                                    ->where('publ.id_upah_borongan', '=', $id_upah_borongan);

                        $d_upah_bayar = DB::table('pro_upah_total')
                                        ->where('id_upah_borongan','=',$id_upah_borongan);

                        if($id_jenis == '1'){
                            $d_upah =  DB::table('ms_upah_borongan as mub')
                                        ->join('ms_proses as mp', 'mp.id_proses', 'mub.id_proses')
                                        ->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')
                                        ->select(DB::raw('mub.*, mp.nama_proses, 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'))
                                        ->where('mub.id_proses',$id_proses);

                            $status_upah = '1';
                        }else{
                            $d_upah      =  DB::table('ms_upah_pegawai as mup')
                                            ->where('mup.id_pegawai',$id_pegawai);
                            $status_upah = '2';
                        }
                            
                        if($d_hasil->count() > 0){
                            $arr['hasil'][]        = $d_hasil->get();
                            $arr['status_hasil'][] = array('1');
                        }else{
                            $arr['status_hasil'][] = array('0');
                        }

                        if($d_upah->count() > 0){
                            $arr['upah'][]        = $d_upah->get();
                            $arr['s_upah'][]      = $status_upah;
                            $arr['status_upah'][] = array('1');
                        }else{
                            $arr['status_upah'][] = array('0');
                        }

                        if ($d_lain2->count()>0) {
                            $arr['lain2'][]        = $d_lain2->get();
                            $arr['status_lain2'][] = array('1');
                        }else{
                            $arr['status_lain2'][] = array('1');                    
                            $arr['lain2'][]        = '';
                        }

                        if($d_upah_bayar->count()>0){
                            $arr['upah_bayar'][] = $d_upah_bayar->get();
                        }else{
                            $arr['upah_bayar'][] = '';
                        }
                    }
                    $arr['nama_rekanan'] = $d_data->get()[0]->nama_rekanan;
                    $arr['nama_makelar'] = $d_data->get()[0]->nama_makelar;
                    $arr['proses'] = $id_proses;
                    $arr['author']       = Session::get('nama');
                }                        
                
                require('fpdf181/Mc_table.php');
                if ($d_data->first()->id_proses == 37) {
                    $html = view('produksi.cetak.CetakResiRekananTenunRange')->with('data', $arr);
                } else {
                    $html = view('produksi.cetak.CetakResiRekananRange')->with('data', $arr);
                }
                
                return response($html)->header('Content-Type', 'application/pdf');
            } else if ($jenis == "Pegawai") {
                $d_data = DB::table('pro_upahproduksi as pu')
                        ->join('pro_barangmasuk as pbm','pu.id_barangmasuk','pbm.id_barangmasuk')
                        ->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')
                        ->join('ms_proses as mp','pu.id_proses','mp.id_proses')
                        ->leftJoin('ms_rekanan as mr','pu.id_rekanan','mr.id_rekanan')
                        ->leftjoin('ms_pegawai as mpe','pu.id_pegawai','mpe.id_pegawai')
                        ->leftjoin('pem_workorder as pw','pu.id_workorder','pw.id_workorder')
                        ->leftjoin('pem_workorder_barang as pwb','pu.id_workorder_barang','pwb.id_workorder_barang')
                        ->select(DB::raw('pu.*, mp.nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai, pw.nomor as no_workorder, pwb.no_workorder as no_workorder_barang'))
                        ->where('pu.id_pegawai', '=', $pegawaiID)
                        // ->where('status_bayar', '=', "1")
                        ->whereBetween('pu.tanggal', [$tanggal_awal, $tanggal_akhir])
                        ->orderByRaw("pu.tanggal, pu.id_upahproduksi");

                if($idProses != 0){
                    $d_data = $d_data->where('pu.id_proses', '=', $idProses);
                }

                // ambil data id upah produksi untuk parameter barang
                $idUpahProduksi = array_reduce($d_data->get()->toArray(), function($carry, $item){
                    $carry[] = $item->id_upahproduksi;
                    return $carry;
                });

                if(sizeof($idUpahProduksi) > 0){
                // dd($idUpahProduksi);
                // end : ambil data id upah produksi untuk parameter barang

                // data barang dari keu_upah_total (detail barang)
                // $upah_total = DB::table('pro_upah_total')->whereIn("id_upah_borongan", $idUpahProduksi)->groupBy("id_upah_borongan")->get()->pluck("id_pbb");
                // $param = implode(",", $upah_total->toArray());
                // $view_masuk_all = "SELECT * FROM view_masuk_all WHERE id IN ($param)";
                $detailBarang = DB::table('pro_upah_total AS up')
                                ->join("pro_upahproduksi AS pu", "pu.id_upahproduksi", "=", "up.id_upah_borongan")
                                ->join(DB::raw("view_masuk_all AS pbb"), function($join) {
                                    $join->on("pbb.id_detail", "=", "up.id_pbb");
                                })
                                ->whereIn('up.id_upah_borongan', $idUpahProduksi);
                // end : data barang dari keu_upah_total (detail barang)

                if($tipeLaporan==1){
                    $detailBarang = $detailBarang->selectRaw("pbb.*, pu.*, IFNULL(SUM(pbb.jum_barang), 0) AS jum_barang_total, up.upah_satuan")
                                    ->groupBy(DB::raw("pu.id_pegawai, pbb.id_proses, pbb.id_barang, pbb.id_motif, pbb.id_corak, up.upah_satuan"));
                }else{
                    $detailBarang = $detailBarang->selectRaw("pbb.*, pu.*, up.upah_satuan");

                }

                if($detailBarang->count() > 0){
                    $arr['hasil'][]        = $detailBarang->orderByRaw("pbb.tanggal")->get();
                    $arr['status_hasil'][] = array('1');
                }else{
                    $arr['status_hasil'][] = array('0');
                }

                // dd($arr);

                foreach ($idUpahProduksi as $key => $value) {
                    $d_lain2        = DB::table('pro_upah_borongan_lain_lain as publ')
                                ->join('pro_upahproduksi as pu','pu.id_upahproduksi','publ.id_upah_borongan')
                                ->where('publ.id_upah_borongan', "=", $value);

                    $d_upah_bayar   = DB::table('pro_upah_total')
                                    ->where('id_upah_borongan', "=", $value);

                    $dataUpah = DB::table("pro_upahproduksi")->where([ ["id_upahproduksi", '=', $value] ])->first();
                    if($dataUpah->id_jenis == '1'){
                        $d_upah =  DB::table('ms_upah_borongan as mub')
                                    ->join('ms_proses as mp', 'mp.id_proses', 'mub.id_proses')
                                    ->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')
                                    ->select(DB::raw('mub.*, mp.nama_proses, 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'))
                                    ->where('mub.id_proses',$dataUpah->id_proses);

                        $status_upah = '1';    
                    }else{    
                        $d_upah      =  DB::table('ms_upah_pegawai as mup')
                                        ->where('mup.id_pegawai',$dataUpah->id_pegawai);
                        $status_upah = '2';        
                    }

                    if($d_upah->count() > 0){
                        $arr['upah'][]        = $d_upah->get();
                        $arr['s_upah'][]      = $status_upah;
                        $arr['status_upah'] = array('1');
                    }else{
                        $arr['status_upah'][] = array('0');
                    }
                    if ($d_lain2->count()>0) {
                        $arr['lain2'][]        = $d_lain2->get();
                        $arr['status_lain2'][] = array('1');
                    }else{
                        $arr['lain2'][]        = '';
                        $arr['status_lain2'][] = array('0');
                    }
                    if($d_upah_bayar->count()>0){
                        $arr['upah_bayar'][] = $d_upah_bayar->get();
                    }else{
                        $arr['upah_bayar'][] = '';
                    }
                }
            }else{
                $arr['hasil'][]         = [];
                $arr['status_hasil'][]  = ['0'];

                $arr['upah'][]          = [];
                $arr['status_upah']     = ['0'];

                $arr['lain2'][]         = '';
                $arr['status_lain2'][]  = ['0'];

                $arr['upah_bayar'][]    = '';
            }
                
                $arr['proses'] = [];
                if($idProses != 0){
                    $arr['proses'] = DB::table('ms_proses')->where("id_proses", "=", $idProses)->first();
                }

                $data_pegawai = DB::table("ms_pegawai")->where([ ["id_pegawai", "=", $pegawaiID] ])->first();
                $arr['nama_pegawai'] = $data_pegawai->nama_pegawai;
                $arr['author']       = Session::get('nama');

                if($tipeLaporan==1){
                    $html = view('produksi.cetak.CetakResiRangeRekap')->with('data',$arr);

                }else{
                    $html = view('produksi.cetak.CetakResiRange')->with('data',$arr);

                }
                // dd($arr);
                require('fpdf181/Mc_table.php');
                return response($html)->header('Content-Type', 'application/pdf');
            }
        } else if ($filter_cetak == "makelar") {
            if ($jenis == "Rekanan") {
                $d_data = DB::table('pro_upahproduksi as pu')
                    ->join('pro_barangmasuk as pbm','pu.id_barangmasuk','pbm.id_barangmasuk')
                    ->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')
                    ->join('ms_proses as mp','pu.id_proses','mp.id_proses')
                    ->leftJoin('ms_rekanan as mr','pu.id_rekanan','mr.id_rekanan')
                    ->leftJoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')
                    ->leftjoin('ms_pegawai as mpe','pu.id_pegawai','mpe.id_pegawai')
                    ->leftjoin('pem_workorder as pw','pu.id_workorder','pw.id_workorder')
                    ->leftjoin('pem_workorder_barang as pwb','pu.id_workorder_barang','pwb.id_workorder_barang')
                    ->leftjoin('ms_rekanan AS mk', 'pu.id_rekanan', 'mk.id_rekanan')
                    ->leftjoin('ms_rekanan_kelompok AS rk', 'mk.id_kelompok', 'rk.id_kelompok')
                    ->select(DB::raw('pu.*, mrk.nama as nama_makelar, mp.nama_proses, mr.nama as nama_rekanan, rk.nama as makelar, mpe.nama_pegawai, pw.nomor as no_workorder, pwb.no_workorder as no_workorder_barang'))
                    ->where('pu.id_rekanan', '=', $rekananID)
                    ->where('rk.id_kelompok', '=', $kelompokID)
                    ->where('status_bayar', '=', "1")
                    ->whereBetween('pu.tanggal', [$tanggal_awal, $tanggal_akhir]);

                if ($d_data->count() > 0) {
                    foreach ($d_data->get() as $key => $value) {
                        $id_barangmasuk       = $value->id_barangmasuk;
                        $id_barangkeluar      = $value->id_barangkeluar;
                        $id_workorder         = $value->id_workorder;
                        $id_workorder_barang  = $value->id_workorder_barang;
                        $id_proses            = $value->id_proses;
                        $id_rekanan           = $value->id_rekanan;
                        $id_pegawai           = $value->id_pegawai;
                        $id_upah_borongan     = $value->id_upahproduksi;
                        $id_jenis             = $value->id_jenis;
                        $tanggal              = $value->tanggal;

                        if($id_workorder != '' || $id_workorder != 0){
                            $order       = 'pbk.id_workorder';
                            $where_order = $id_workorder;
                        }else{
                            $order       = 'pbk.id_workorder_barang';
                            $where_order = $id_workorder_barang;
                        }

                        if($id_rekanan != '' || $id_rekanan != 0){
                            $pemroses       = 'pbk.id_rekanan';
                            $where_pemroses = $id_rekanan;
                        }else{
                            $pemroses       = 'pbk.id_pegawai';
                            $where_pemroses = 'pbk.id_pegawai';
                        }

                        $d_hasil = DB::table('pro_barangmasuk AS pb')
                                    ->join('pro_barangkeluar as pbk','pb.id_barangkeluar','pbk.id_barangkeluar')
                                    ->join('pro_barangmasuk_barang as pbb', 'pb.id_barangmasuk', 'pbb.id_barangmasuk')
                                    ->join('pro_upahproduksi as pu','pu.id_barangmasuk','pb.id_barangmasuk')
                                    ->join('ms_gudang as mg', 'pbb.id_gudang', 'mg.id_gudang')
                                    ->join('ms_barang as mb', 'pbb.id_barang', 'mb.id')
                                    ->leftjoin('ms_warnakomposisi as mwk', 'pbb.id_komposisi', 'mwk.id_komposisi')
                                    ->join('ms_satuan AS ms', 'pbb.id_satuan', 'ms.id_satuan')
                                    ->leftJoin('ms_motif_barang as mmb', 'pbb.id_motif_barang', 'mmb.id_motif_barang')
                                    ->leftJoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')
                                    ->leftJoin('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')
                                    ->leftJoin('ms_barang as mb2', 'pbb.id_label', 'mb2.id')
                                    ->leftJoin('ms_barang as mb3', 'pbb.id_kemasan', 'mb3.id')
                                    ->leftJoin('ms_barang as mb4', 'pbb.id_kemasansatuan', 'mb4.id')
                                    ->leftJoin('ms_barang as mb5', 'pbb.id_kemasansepuluh', 'mb5.id')
                                    ->leftJoin('ms_barang as mb6', 'pbb.id_kemasanseratus', 'mb6.id')
                                    ->select(DB::raw('pbb.*, pbk.id_proses, mb.kode_barang, mb.nama_barang, mg.id_gudang, mg.nama_gudang, pbb.id_satuan, ms.nama_satuan, 0 AS upah,
                                        CASE WHEN mwk.id_komposisi IS NULL THEN "" ELSE  mwk.id_komposisi END AS id_komposisi, 
                                        CASE WHEN mwk.nama IS NULL THEN "" ELSE  mwk.nama END AS nama_komposisi, 
                                        CASE WHEN mmb.id_motif_barang IS NULL THEN "" ELSE mmb.id_motif_barang END AS id_motif_barang, 
                                        CASE WHEN mmb.motif_keterangan IS NULL THEN "" ELSE mmb.motif_keterangan END AS motif_keterangan, 
                                        CASE WHEN mm.id_motif IS NULL THEN "" ELSE mm.id_motif END AS id_motif, 
                                        CASE WHEN mm.nama_motif IS NULL THEN "" ELSE mm.nama_motif END AS nama_motif, 
                                        CASE WHEN mk.id_kualitas IS NULL THEN "" ELSE mk.id_kualitas END AS id_kualitas, 
                                        CASE WHEN mk.nama_kualitas IS NULL THEN "" ELSE mk.nama_kualitas END AS nama_kualitas,
                                        CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" ELSE pbb.id_kualitas_dudul END AS id_kualitas_dudul,
                                        CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" WHEN pbb.id_kualitas_dudul = 1 THEN "Baik" ELSE "Jelek" END AS kualitas_dudul,
                                        CASE WHEN pbb.id_label = 0 THEN "" ELSE pbb.id_label END AS id_label, CASE WHEN mb2.nama_barang IS NULL THEN "" ELSE mb2.nama_barang END AS nama_label,
                                        CASE WHEN pbb.id_kemasan = 0 THEN "" ELSE pbb.id_kemasan END AS id_kemasan,
                                        CASE WHEN mb3.nama_barang IS NULL THEN "" ELSE mb3.nama_barang END AS nama_kemasan,
                                        CASE WHEN pbb.id_kemasansatuan = 0 THEN "" ELSE pbb.id_kemasansatuan END AS id_kemasansatuan,
                                        CASE WHEN mb4.nama_barang IS NULL THEN "" ELSE mb4.nama_barang END AS nama_kemasansatuan,
                                        CASE WHEN pbb.id_kemasansepuluh = 0 THEN "" ELSE pbb.id_kemasansepuluh END AS id_kemasansepuluh,
                                        CASE WHEN mb5.nama_barang IS NULL THEN "" ELSE mb5.nama_barang END AS nama_kemasansepuluh,
                                        CASE WHEN pbb.id_kemasanseratus = 0 THEN "" ELSE pbb.id_kemasanseratus END AS id_kemasanseratus,
                                        CASE WHEN mb6.nama_barang IS NULL THEN "" ELSE mb6.nama_barang END AS nama_kemasanseratus, pu.tanggal as tanggal, pu.nomor as nomor, pu.pajak as pajak'))
                                    ->where('pb.id_barangmasuk', $id_barangmasuk)
                                    ->where('pb.id_barangkeluar', $id_barangkeluar)
                                    ->where('pbk.id_rekanan',$id_rekanan)
                                    ->where($order,$where_order)
                                    ->where('pbk.id_proses',$id_proses);

                        $d_lain2 = DB::table('pro_upah_borongan_lain_lain as publ')
                                    ->join('pro_upahproduksi as pu','pu.id_upahproduksi','publ.id_upah_borongan')
                                    ->where('publ.id_upah_borongan', '=', $id_upah_borongan);

                        $d_upah_bayar = DB::table('pro_upah_total')
                                        ->where('id_upah_borongan','=',$id_upah_borongan);

                        if($id_jenis == '1'){
                            $d_upah =  DB::table('ms_upah_borongan as mub')
                                        ->join('ms_proses as mp', 'mp.id_proses', 'mub.id_proses')
                                        ->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')
                                        ->select(DB::raw('mub.*, mp.nama_proses, 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'))
                                        ->where('mub.id_proses',$id_proses);

                            $status_upah = '1';
                        }else{
                            $d_upah      =  DB::table('ms_upah_pegawai as mup')
                                            ->where('mup.id_pegawai',$id_pegawai);
                            $status_upah = '2';
                        }
                            
                        if($d_hasil->count() > 0){
                            $arr['hasil'][]        = $d_hasil->get();
                            $arr['status_hasil'][] = array('1');
                        }else{
                            $arr['status_hasil'][] = array('0');
                        }

                        if($d_upah->count() > 0){
                            $arr['upah'][]        = $d_upah->get();
                            $arr['s_upah'][]      = $status_upah;
                            $arr['status_upah'][] = array('1');
                        }else{
                            $arr['status_upah'][] = array('0');
                        }

                        if ($d_lain2->count()>0) {
                            $arr['lain2'][]        = $d_lain2->get();
                            $arr['status_lain2'][] = array('1');
                        }else{
                            $arr['status_lain2'][] = array('1');                    
                            $arr['lain2'][]        = '';
                        }

                        if($d_upah_bayar->count()>0){
                            $arr['upah_bayar'][] = $d_upah_bayar->get();
                        }else{
                            $arr['upah_bayar'][] = '';
                        }
                    }
                    $arr['nama_rekanan'] = $d_data->get()[0]->nama_rekanan;
                    $arr['makelar'] = $d_data->get()[0]->makelar;
                    $arr['proses'] = $id_proses;
                    $arr['author']       = Session::get('nama');
                }                        
                
                require('fpdf181/Mc_table.php');
                if ($d_data->first()->id_proses == 37) {
                    $html = view('produksi.cetak.CetakResiRekananTenunMakelar')->with('data', $arr);
                } else {
                    $html = view('produksi.cetak.CetakResiMakelar')->with('data', $arr);
                }
                
                return response($html)->header('Content-Type', 'application/pdf');
            }
        }
    }

    public function cetakUpahRekanan(Request $request)
    {
        $input          = $request->all();
        $tanggal_awal   = date_db($input['tgl_awal']);
        $tanggal_akhir  = date_db($input['tgl_akhir']);
        $filter_cetak   = $input['filter_cetak'];
        $jenis          = $input['jenis'];
        $id             = $input['id_upahproduksi'];
        $nomor          = $input['nomor'];
        try {
            $id = Crypt::decrypt($id);
        } catch (DecryptException $e) {
            return view('errors/503');
        }

        if ($filter_cetak == "nomor") {
            if($jenis == "Rekanan"){
               $d_data = DB::table('pro_upahproduksi as pu')
                           ->join('pro_barangmasuk as pbm','pu.id_barangmasuk','pbm.id_barangmasuk')
                           ->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')
                           ->join('ms_proses as mp','pu.id_proses','mp.id_proses')
                           ->leftJoin('ms_rekanan as mr','pu.id_rekanan','mr.id_rekanan')
                           ->leftjoin('ms_pegawai as mpe','pu.id_pegawai','mpe.id_pegawai')
                           ->leftjoin('pem_workorder as pw','pu.id_workorder','pw.id_workorder')
                           ->leftjoin('pem_workorder_barang as pwb','pu.id_workorder_barang','pwb.id_workorder_barang')
                           ->select(DB::raw('pu.*, mp.nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai, pw.nomor as no_workorder, pwb.no_workorder as no_workorder_barang'))
                           ->where('pu.id_upahproduksi','=', $id)
                           ->where('pu.nomor','=', $nomor)
                           ->first();

                $arr['data_penerima']=$d_data;

                $id_barangmasuk      = $d_data->id_barangmasuk;
                $id_barangkeluar     = $d_data->id_barangkeluar;
                $id_workorder        = $d_data->id_workorder;
                $id_workorder_barang = $d_data->id_workorder_barang;
                $id_proses           = $d_data->id_proses;
                $id_rekanan          = $d_data->id_rekanan;
                $id_pegawai          = $d_data->id_pegawai;
                $id_upah_borongan    = $id;
                $id_jenis            = $d_data->id_jenis;
            
                if($id_workorder != '' || $id_workorder != 0){
                    $order       = 'id_workorder';
                    $where_order = $id_workorder;
                }else{
                    $order       = 'id_workorder_barang';
                    $where_order = $id_workorder_barang;
                }

                if($id_rekanan != '' || $id_rekanan != 0){
                    $pemroses       = 'pbk.id_rekanan';
                    $where_pemroses = $id_rekanan;
                }else{
                    $pemroses       = 'pbk.id_pegawai';
                    $where_pemroses = 'pbk.id_pegawai';
                }

                $d_hasil = DB::table('pro_barangmasuk AS pb')
                            ->join('pro_barangkeluar as pbk','pb.id_barangkeluar','pbk.id_barangkeluar')
                            ->join('pro_barangmasuk_barang as pbb', 'pb.id_barangmasuk', 'pbb.id_barangmasuk')
                            ->join('ms_gudang as mg', 'pbb.id_gudang', 'mg.id_gudang')
                            ->join('ms_barang as mb', 'pbb.id_barang', 'mb.id')
                            ->leftjoin('ms_warnakomposisi as mwk', 'pbb.id_komposisi', 'mwk.id_komposisi')
                            ->join('ms_satuan AS ms', 'pbb.id_satuan', 'ms.id_satuan')
                            ->leftJoin('ms_motif_barang as mmb', 'pbb.id_motif_barang', 'mmb.id_motif_barang')
                            ->leftJoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')
                            ->leftJoin('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')
                            ->leftJoin('ms_barang as mb2', 'pbb.id_label', 'mb2.id')
                            ->leftJoin('ms_barang as mb3', 'pbb.id_kemasan', 'mb3.id')
                            ->leftJoin('ms_barang as mb4', 'pbb.id_kemasansatuan', 'mb4.id')
                            ->leftJoin('ms_barang as mb5', 'pbb.id_kemasansepuluh', 'mb5.id')
                            ->leftJoin('ms_barang as mb6', 'pbb.id_kemasanseratus', 'mb6.id')
                            ->select(DB::raw('pbb.*, pbk.id_proses, mb.kode_barang, mb.nama_barang, mg.id_gudang, mg.nama_gudang, pbb.id_satuan, ms.nama_satuan, 0 AS upah,
                                CASE WHEN mwk.id_komposisi IS NULL THEN "" ELSE  mwk.id_komposisi END AS id_komposisi, 
                                CASE WHEN mwk.nama IS NULL THEN "" ELSE  mwk.nama END AS nama_komposisi, 
                                CASE WHEN mmb.id_motif_barang IS NULL THEN "" ELSE mmb.id_motif_barang END AS id_motif_barang, 
                                CASE WHEN mmb.motif_keterangan IS NULL THEN "" ELSE mmb.motif_keterangan END AS motif_keterangan, 
                                CASE WHEN mm.id_motif IS NULL THEN "" ELSE mm.id_motif END AS id_motif, 
                                CASE WHEN mm.nama_motif IS NULL THEN "" ELSE mm.nama_motif END AS nama_motif, 
                                CASE WHEN mk.id_kualitas IS NULL THEN "" ELSE mk.id_kualitas END AS id_kualitas, 
                                CASE WHEN mk.nama_kualitas IS NULL THEN "" ELSE mk.nama_kualitas END AS nama_kualitas,
                                CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" ELSE pbb.id_kualitas_dudul END AS id_kualitas_dudul,
                                CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" WHEN pbb.id_kualitas_dudul = 1 THEN "Baik" ELSE "Jelek" END AS kualitas_dudul,
                                 CASE WHEN pbb.id_label = 0 THEN "" ELSE pbb.id_label END AS id_label, CASE WHEN mb2.nama_barang IS NULL THEN "" ELSE mb2.nama_barang END AS nama_label,
                                CASE WHEN pbb.id_kemasan = 0 THEN "" ELSE pbb.id_kemasan END AS id_kemasan,
                                CASE WHEN mb3.nama_barang IS NULL THEN "" ELSE mb3.nama_barang END AS nama_kemasan,
                                CASE WHEN pbb.id_kemasansatuan = 0 THEN "" ELSE pbb.id_kemasansatuan END AS id_kemasansatuan,
                                CASE WHEN mb4.nama_barang IS NULL THEN "" ELSE mb4.nama_barang END AS nama_kemasansatuan,
                                CASE WHEN pbb.id_kemasansepuluh = 0 THEN "" ELSE pbb.id_kemasansepuluh END AS id_kemasansepuluh,
                                CASE WHEN mb5.nama_barang IS NULL THEN "" ELSE mb5.nama_barang END AS nama_kemasansepuluh,
                                CASE WHEN pbb.id_kemasanseratus = 0 THEN "" ELSE pbb.id_kemasanseratus END AS id_kemasanseratus,
                                CASE WHEN mb6.nama_barang IS NULL THEN "" ELSE mb6.nama_barang END AS nama_kemasanseratus'))
                            ->where('pb.id_barangmasuk', $id_barangmasuk)
                            ->where('pb.id_barangkeluar', $id_barangkeluar)
                            ->where('pbk.id_rekanan',$id_rekanan)
                            ->where($order,$where_order)
                            ->where('pbk.id_proses',$id_proses);

                $d_lain2 = DB::table('pro_upah_borongan_lain_lain')->where('id_upah_borongan', '=', $id_upah_borongan);
                $d_upah_bayar=DB::table('pro_upah_total')->where('id_upah_borongan','=',$id_upah_borongan);

                if($id_jenis == '1'){
                    $d_upah =  DB::table('ms_upah_borongan as mub')
                                ->join('ms_proses as mp', 'mp.id_proses', 'mub.id_proses')
                                ->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')
                                ->select(DB::raw('mub.*, mp.nama_proses, 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'))
                                ->where('mub.id_proses',$id_proses);
                    $status_upah = '1';
                }else{
                   $d_upah =  DB::table('ms_upah_pegawai as mup')->where('mup.id_pegawai',$id_pegawai);
                   $status_upah = '2';
                }
                
                if($d_hasil->count() > 0){
                    $arr['hasil']        = $d_hasil->get();
                    $arr['status_hasil'] = array('1');
                }else{
                    $arr['status_hasil'] = array('0');
                }

                if($d_upah->count() > 0){
                    $arr['upah']        = $d_upah->get();
                    $arr['s_upah']      = $status_upah;
                    $arr['status_upah'] = array('1');
                }else{
                    $arr['status_upah'] = array('0');
                }

                if ($d_lain2->count()>0) {
                    $arr['lain2']=$d_lain2->get();
                    $arr['status_lain2'] = array('1');
                }else{
                    $arr['status_lain2'] = array('1');                    
                    $arr['lain2']        = '';
                }

                if($d_upah_bayar->count()>0){
                    $arr['upah_bayar'] = $d_upah_bayar->get();
                }else{
                    $arr['upah_bayar'] = '';
                }
            }else if($jenis == "Pegawai"){
                $d_data = DB::table('pro_upahproduksi as pu')
                        ->join('pro_barangmasuk as pbm','pu.id_barangmasuk','pbm.id_barangmasuk')
                        ->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')
                        ->join('ms_proses as mp','pu.id_proses','mp.id_proses')
                        ->leftJoin('ms_rekanan as mr','pu.id_rekanan','mr.id_rekanan')
                        ->leftjoin('ms_pegawai as mpe','pu.id_pegawai','mpe.id_pegawai')
                        ->leftjoin('pem_workorder as pw','pu.id_workorder','pw.id_workorder')
                        ->leftjoin('pem_workorder_barang as pwb','pu.id_workorder_barang','pwb.id_workorder_barang')
                        ->select(DB::raw('pu.*, mp.nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai, pw.nomor as no_workorder, pwb.no_workorder as no_workorder_barang'))
                        ->where('pu.id_upahproduksi','=', $id)
                        ->where('pu.nomor','=', $nomor)
                        ->first();

                $arr['data_penerima'] = $d_data;

                $id_barangmasuk      = $d_data->id_barangmasuk;
                $id_barangkeluar     = $d_data->id_barangkeluar;
                $id_workorder        = $d_data->id_workorder;
                $id_workorder_barang = $d_data->id_workorder_barang;
                $id_proses           = $d_data->id_proses;
                $id_pegawai          = $d_data->id_pegawai;
                $id_jenis            = $d_data->id_jenis;
                $id_upah_borongan    = $id;

                if($id_workorder != '' || $id_workorder != 0){
                    $order       = 'id_workorder';
                    $where_order = $id_workorder;
                }else{
                    $order       = 'id_workorder_barang';
                    $where_order = $id_workorder_barang;
                }

                $d_hasil = DB::table('pro_barangmasuk AS pb')
                            ->join('pro_barangkeluar as pbk','pb.id_barangkeluar','pbk.id_barangkeluar')
                            ->join('pro_barangmasuk_barang as pbb', 'pb.id_barangmasuk', 'pbb.id_barangmasuk')
                            ->join('ms_gudang as mg', 'pbb.id_gudang', 'mg.id_gudang')
                            ->join('ms_barang as mb', 'pbb.id_barang', 'mb.id')
                            ->leftjoin('ms_warnakomposisi as mwk', 'pbb.id_komposisi', 'mwk.id_komposisi')
                            ->join('ms_satuan AS ms', 'pbb.id_satuan', 'ms.id_satuan')
                            ->leftJoin('ms_motif_barang as mmb', 'pbb.id_motif_barang', 'mmb.id_motif_barang')
                            ->leftJoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')
                            ->leftJoin('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')
                            ->leftJoin('ms_barang as mb2', 'pbb.id_label', 'mb2.id')
                            ->leftJoin('ms_barang as mb3', 'pbb.id_kemasan', 'mb3.id')
                            ->leftJoin('ms_barang as mb4', 'pbb.id_kemasansatuan', 'mb4.id')
                            ->leftJoin('ms_barang as mb5', 'pbb.id_kemasansepuluh', 'mb5.id')
                            ->leftJoin('ms_barang as mb6', 'pbb.id_kemasanseratus', 'mb6.id')
                            ->select(DB::raw('pbb.*, pbk.id_proses , mb.kode_barang, mb.nama_barang, mg.id_gudang, mg.nama_gudang, pbb.id_satuan, ms.nama_satuan, 0 AS upah, 
                                CASE WHEN mwk.id_komposisi IS NULL THEN "" ELSE  mwk.id_komposisi END AS id_komposisi, 
                                CASE WHEN mwk.nama IS NULL THEN "" ELSE  mwk.nama END AS nama_komposisi, 
                                CASE WHEN mmb.id_motif_barang IS NULL THEN "" ELSE mmb.id_motif_barang END AS id_motif_barang, 
                                CASE WHEN mmb.motif_keterangan IS NULL THEN "" ELSE mmb.motif_keterangan END AS motif_keterangan, 
                                CASE WHEN mm.id_motif IS NULL THEN "" ELSE mm.id_motif END AS id_motif, 
                                CASE WHEN mm.nama_motif IS NULL THEN "" ELSE mm.nama_motif END AS nama_motif, 
                                CASE WHEN mk.id_kualitas IS NULL THEN "" ELSE mk.id_kualitas END AS id_kualitas, 
                                CASE WHEN mk.nama_kualitas IS NULL THEN "" ELSE mk.nama_kualitas END AS nama_kualitas,
                                CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" ELSE pbb.id_kualitas_dudul END AS id_kualitas_dudul,
                                CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" WHEN pbb.id_kualitas_dudul = 1 THEN "Baik" ELSE "Jelek" END AS kualitas_dudul,
                                CASE WHEN pbb.id_label = 0 THEN "" ELSE pbb.id_label END AS id_label, CASE WHEN mb2.nama_barang IS NULL THEN "" ELSE mb2.nama_barang END AS nama_label,
                                CASE WHEN pbb.id_kemasan = 0 THEN "" ELSE pbb.id_kemasan END AS id_kemasan,
                                CASE WHEN mb3.nama_barang IS NULL THEN "" ELSE mb3.nama_barang END AS nama_kemasan,
                                CASE WHEN pbb.id_kemasansatuan = 0 THEN "" ELSE pbb.id_kemasansatuan END AS id_kemasansatuan,
                                CASE WHEN mb4.nama_barang IS NULL THEN "" ELSE mb4.nama_barang END AS nama_kemasansatuan,
                                CASE WHEN pbb.id_kemasansepuluh = 0 THEN "" ELSE pbb.id_kemasansepuluh END AS id_kemasansepuluh,
                                CASE WHEN mb5.nama_barang IS NULL THEN "" ELSE mb5.nama_barang END AS nama_kemasansepuluh,
                                CASE WHEN pbb.id_kemasanseratus = 0 THEN "" ELSE pbb.id_kemasanseratus END AS id_kemasanseratus,
                                CASE WHEN mb6.nama_barang IS NULL THEN "" ELSE mb6.nama_barang END AS nama_kemasanseratus'))
                            ->where('pb.id_barangmasuk', $id_barangmasuk)
                            ->where('pb.id_barangkeluar', $id_barangkeluar)
                            ->where('pbk.id_pegawai',$id_pegawai)
                            ->where($order,$where_order)
                            ->where('pbk.id_proses',$id_proses);

                $d_lain2 = DB::table('pro_upah_borongan_lain_lain')->where('id_upah_borongan', '=', $id_upah_borongan);
                $d_upah_bayar=DB::table('pro_upah_total')->where('id_upah_borongan','=',$id_upah_borongan);
        
                if($id_jenis == '1'){
                    $d_upah =  DB::table('ms_upah_borongan as mub')
                                ->join('ms_proses as mp', 'mp.id_proses', 'mub.id_proses')
                                ->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')
                                ->select(DB::raw('mub.*, mp.nama_proses, 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'))
                                ->where('mub.id_proses',$id_proses);

                    $status_upah = '1';    
                }else{    
                    $d_upah =  DB::table('ms_upah_pegawai as mup')->where('mup.id_pegawai',$id_pegawai);
                    $status_upah = '2';        
                }
                
                if($d_hasil->count() > 0){
                    $arr['hasil']        = $d_hasil->get();
                    $arr['status_hasil'] = array('1');
                }else{
                    $arr['status_hasil'] = array('0');
                }

                if($d_upah->count() > 0){
                    $arr['upah']        = $d_upah->get();
                    $arr['s_upah']      = $status_upah;
                    $arr['status_upah'] = array('1');
                }else{
                    $arr['status_upah'] = array('0');
                }
                if ($d_lain2->count()>0) {
                    $arr['lain2']        = $d_lain2->get();
                    $arr['status_lain2'] = array('1');
                }else{
                    $arr['lain2']        = '';
                    $arr['status_lain2'] = array('0');
                }
                if($d_upah_bayar->count()>0){
                    $arr['upah_bayar'] = $d_upah_bayar->get();
                }else{
                    $arr['upah_bayar'] = '';
                }
            }

            require('fpdf181/Mc_table.php');
            $html = view('produksi.cetak.CetakResi')->with('data',$arr);
            return response($html)->header('Content-Type', 'application/pdf');
        }
    }

    public function cetak_resi($id,$jenis){
        if($jenis==0){
           $d_data = DB::table('pro_upahproduksi as pu')->join('pro_barangmasuk as pbm','pu.id_barangmasuk','pbm.id_barangmasuk')->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')->join('ms_proses as mp','pu.id_proses','mp.id_proses')->leftJoin('ms_rekanan as mr','pu.id_rekanan','mr.id_rekanan')->leftjoin('ms_pegawai as mpe','pu.id_pegawai','mpe.id_pegawai')->leftjoin('pem_workorder as pw','pu.id_workorder','pw.id_workorder')->leftjoin('pem_workorder_barang as pwb','pu.id_workorder_barang','pwb.id_workorder_barang')->select(DB::raw('pu.*, mp.nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai, pw.nomor as no_workorder, pwb.no_workorder as no_workorder_barang'))->where('pu.id_upahproduksi','=', $id)->first();

                $arr['data_penerima']=$d_data;
           // echo '<pre>';print_r($d_data);exit();
            $id_barangmasuk = $d_data->id_barangmasuk;
            $id_barangkeluar = $d_data->id_barangkeluar;
            $id_workorder = $d_data->id_workorder;
            $id_workorder_barang = $d_data->id_workorder_barang;
            $id_proses = $d_data->id_proses;
            $id_rekanan = $d_data->id_rekanan;
            $id_pegawai = $d_data->id_pegawai;

            $id_upah_borongan=$id;
            $id_jenis=$d_data->id_jenis;

            
                 if($id_workorder != '' || $id_workorder != 0){
                    $order = 'id_workorder';
                    $where_order = $id_workorder;
                }else{
                    $order = 'id_workorder_barang';
                    $where_order = $id_workorder_barang;
                }

                if($id_rekanan != '' || $id_rekanan != 0){
                    $pemroses = 'pbk.id_rekanan';
                    $where_pemroses = $id_rekanan;
                }else{
                    $pemroses = 'pbk.id_pegawai';
                    $where_pemroses = 'pbk.id_pegawai';
                }

                // DB::enableQueryLog();
                $d_hasil = DB::table('pro_barangmasuk AS pb')->join('pro_barangkeluar as pbk','pb.id_barangkeluar','pbk.id_barangkeluar')->join('pro_barangmasuk_barang as pbb', 'pb.id_barangmasuk', 'pbb.id_barangmasuk')->join('ms_gudang as mg', 'pbb.id_gudang', 'mg.id_gudang')->join('ms_barang as mb', 'pbb.id_barang', 'mb.id')->leftjoin('ms_warnakomposisi as mwk', 'pbb.id_komposisi', 'mwk.id_komposisi')->join('ms_satuan AS ms', 'pbb.id_satuan', 'ms.id_satuan')->leftJoin('ms_motif_barang as mmb', 'pbb.id_motif_barang', 'mmb.id_motif_barang')->leftJoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')->leftJoin('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')->leftJoin('ms_barang as mb2', 'pbb.id_label', 'mb2.id')->leftJoin('ms_barang as mb3', 'pbb.id_kemasan', 'mb3.id')->leftJoin('ms_barang as mb4', 'pbb.id_kemasansatuan', 'mb4.id')->leftJoin('ms_barang as mb5', 'pbb.id_kemasansepuluh', 'mb5.id')->leftJoin('ms_barang as mb6', 'pbb.id_kemasanseratus', 'mb6.id')
                // ->leftJoin('ms_upah_borongan AS mub',DB::raw('mub.id_proses=pbk.id_proses AND CASE WHEN pbk.id_proses'),DB::raw('23 THEN mub.id_barang =  pbb.id_barang ELSE mub.id_motif=mm.id_motif AND mub.id_motif_barang=pbb.id_motif_barang  END'))
                ->select(DB::raw('pbb.*, pbk.id_proses, mb.kode_barang, mb.nama_barang, mg.id_gudang, mg.nama_gudang, pbb.id_satuan, ms.nama_satuan, 0 AS upah,
                    CASE WHEN mwk.id_komposisi IS NULL THEN "" ELSE  mwk.id_komposisi END AS id_komposisi, 
                    CASE WHEN mwk.nama IS NULL THEN "" ELSE  mwk.nama END AS nama_komposisi, 
                    CASE WHEN mmb.id_motif_barang IS NULL THEN "" ELSE mmb.id_motif_barang END AS id_motif_barang, 
                    CASE WHEN mmb.motif_keterangan IS NULL THEN "" ELSE mmb.motif_keterangan END AS motif_keterangan, 
                    CASE WHEN mm.id_motif IS NULL THEN "" ELSE mm.id_motif END AS id_motif, 
                    CASE WHEN mm.nama_motif IS NULL THEN "" ELSE mm.nama_motif END AS nama_motif, 
                    CASE WHEN mk.id_kualitas IS NULL THEN "" ELSE mk.id_kualitas END AS id_kualitas, 
                    CASE WHEN mk.nama_kualitas IS NULL THEN "" ELSE mk.nama_kualitas END AS nama_kualitas,
                    CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" ELSE pbb.id_kualitas_dudul END AS id_kualitas_dudul,
                    CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" WHEN pbb.id_kualitas_dudul = 1 THEN "Baik" ELSE "Jelek" END AS kualitas_dudul,
                     CASE WHEN pbb.id_label = 0 THEN "" ELSE pbb.id_label END AS id_label, CASE WHEN mb2.nama_barang IS NULL THEN "" ELSE mb2.nama_barang END AS nama_label,
                    CASE WHEN pbb.id_kemasan = 0 THEN "" ELSE pbb.id_kemasan END AS id_kemasan,
                    CASE WHEN mb3.nama_barang IS NULL THEN "" ELSE mb3.nama_barang END AS nama_kemasan,
                    CASE WHEN pbb.id_kemasansatuan = 0 THEN "" ELSE pbb.id_kemasansatuan END AS id_kemasansatuan,
                    CASE WHEN mb4.nama_barang IS NULL THEN "" ELSE mb4.nama_barang END AS nama_kemasansatuan,
                    CASE WHEN pbb.id_kemasansepuluh = 0 THEN "" ELSE pbb.id_kemasansepuluh END AS id_kemasansepuluh,
                    CASE WHEN mb5.nama_barang IS NULL THEN "" ELSE mb5.nama_barang END AS nama_kemasansepuluh,
                    CASE WHEN pbb.id_kemasanseratus = 0 THEN "" ELSE pbb.id_kemasanseratus END AS id_kemasanseratus,
                    CASE WHEN mb6.nama_barang IS NULL THEN "" ELSE mb6.nama_barang END AS nama_kemasanseratus'))->where('pb.id_barangmasuk', $id_barangmasuk)->where('pb.id_barangkeluar', $id_barangkeluar)->where('pbk.id_rekanan',$id_rekanan)->where($order,$where_order)->where('pbk.id_proses',$id_proses);
                $d_lain2 = DB::table('pro_upah_borongan_lain_lain')->where('id_upah_borongan', '=', $id_upah_borongan);
                $d_upah_bayar=DB::table('pro_upah_total')->where('id_upah_borongan','=',$id_upah_borongan);

                
                // dd(DB::getQueryLog());
                // echo "<pre>";print_r($d_hasil->get());exit();
                if($id_jenis == '1'){
                    $d_upah =  DB::table('ms_upah_borongan as mub')->join('ms_proses as mp', 'mp.id_proses', 'mub.id_proses')->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')->select(DB::raw('mub.*, mp.nama_proses, 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'))->where('mub.id_proses',$id_proses);
                    $status_upah = '1';
                }else{
                   $d_upah =  DB::table('ms_upah_pegawai as mup')->where('mup.id_pegawai',$id_pegawai);
                   $status_upah = '2';
                }
                

                if($d_hasil->count() > 0){
                    $arr['hasil'] = $d_hasil->get();
                    $arr['status_hasil'] = array('1');
                }else{
                    $arr['status_hasil'] = array('0');
                }

                if($d_upah->count() > 0){
                    $arr['upah'] = $d_upah->get();
                    $arr['s_upah'] = $status_upah;
                    $arr['status_upah'] = array('1');
                }else{
                    $arr['status_upah'] = array('0');
                }

                if ($d_lain2->count()>0) {
                    $arr['lain2']=$d_lain2->get();
                    $arr['status_lain2'] = array('1');
                }else{
                    $arr['status_lain2'] = array('1');                    
                    $arr['lain2']='';
                }

                if($d_upah_bayar->count()>0){
                    $arr['upah_bayar'] = $d_upah_bayar->get();
                }else{
                    $arr['upah_bayar'] = '';
                }

            }else if($jenis==1){
                $d_data = DB::table('pro_upahproduksi as pu')->join('pro_barangmasuk as pbm','pu.id_barangmasuk','pbm.id_barangmasuk')->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')->join('ms_proses as mp','pu.id_proses','mp.id_proses')->leftJoin('ms_rekanan as mr','pu.id_rekanan','mr.id_rekanan')->leftjoin('ms_pegawai as mpe','pu.id_pegawai','mpe.id_pegawai')->leftjoin('pem_workorder as pw','pu.id_workorder','pw.id_workorder')->leftjoin('pem_workorder_barang as pwb','pu.id_workorder_barang','pwb.id_workorder_barang')->select(DB::raw('pu.*, mp.nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai, pw.nomor as no_workorder, pwb.no_workorder as no_workorder_barang'))->where('pu.id_upahproduksi','=', $id)->first();
                $arr['data_penerima']=$d_data;

           // echo '<pre>';print_r($d_data);exit();
                $id_barangmasuk = $d_data->id_barangmasuk;
                $id_barangkeluar = $d_data->id_barangkeluar;
                $id_workorder = $d_data->id_workorder;
                $id_workorder_barang = $d_data->id_workorder_barang;
                $id_proses = $d_data->id_proses;
                // $id_rekanan = $request->get('id_rekanan');
                $id_pegawai = $d_data->id_pegawai;

                $id_jenis = $d_data->id_jenis;
                $id_upah_borongan=$id;
                // $tanggal_awal = $request->get('tanggal_awal');
                // $tanggal_akhir = $request->get('tanggal_akhir');


                if($id_workorder != '' || $id_workorder != 0){
                    $order = 'id_workorder';
                    $where_order = $id_workorder;
                }else{
                    $order = 'id_workorder_barang';
                    $where_order = $id_workorder_barang;
                }
                 DB::enableQueryLog();
                $d_hasil = DB::table('pro_barangmasuk AS pb')->join('pro_barangkeluar as pbk','pb.id_barangkeluar','pbk.id_barangkeluar')->join('pro_barangmasuk_barang as pbb', 'pb.id_barangmasuk', 'pbb.id_barangmasuk')->join('ms_gudang as mg', 'pbb.id_gudang', 'mg.id_gudang')->join('ms_barang as mb', 'pbb.id_barang', 'mb.id')->leftjoin('ms_warnakomposisi as mwk', 'pbb.id_komposisi', 'mwk.id_komposisi')->join('ms_satuan AS ms', 'pbb.id_satuan', 'ms.id_satuan')->leftJoin('ms_motif_barang as mmb', 'pbb.id_motif_barang', 'mmb.id_motif_barang')->leftJoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')->leftJoin('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')->leftJoin('ms_barang as mb2', 'pbb.id_label', 'mb2.id')->leftJoin('ms_barang as mb3', 'pbb.id_kemasan', 'mb3.id')->leftJoin('ms_barang as mb4', 'pbb.id_kemasansatuan', 'mb4.id')->leftJoin('ms_barang as mb5', 'pbb.id_kemasansepuluh', 'mb5.id')
                    // ->leftJoin('ms_upah_borongan AS mub',DB::raw('mub.id_proses=pbk.id_proses AND CASE WHEN pbk.id_proses'),DB::raw('23 THEN mub.id_barang =  mb.id ELSE mub.id_motif_barang = mmb.id_motif_barang END'))
                    ->leftJoin('ms_barang as mb6', 'pbb.id_kemasanseratus', 'mb6.id')->select(DB::raw('pbb.*, pbk.id_proses , mb.kode_barang, mb.nama_barang, mg.id_gudang, mg.nama_gudang, pbb.id_satuan, ms.nama_satuan, 0 AS upah, 
                    CASE WHEN mwk.id_komposisi IS NULL THEN "" ELSE  mwk.id_komposisi END AS id_komposisi, 
                    CASE WHEN mwk.nama IS NULL THEN "" ELSE  mwk.nama END AS nama_komposisi, 
                    CASE WHEN mmb.id_motif_barang IS NULL THEN "" ELSE mmb.id_motif_barang END AS id_motif_barang, 
                    CASE WHEN mmb.motif_keterangan IS NULL THEN "" ELSE mmb.motif_keterangan END AS motif_keterangan, 
                    CASE WHEN mm.id_motif IS NULL THEN "" ELSE mm.id_motif END AS id_motif, 
                    CASE WHEN mm.nama_motif IS NULL THEN "" ELSE mm.nama_motif END AS nama_motif, 
                    CASE WHEN mk.id_kualitas IS NULL THEN "" ELSE mk.id_kualitas END AS id_kualitas, 
                    CASE WHEN mk.nama_kualitas IS NULL THEN "" ELSE mk.nama_kualitas END AS nama_kualitas,
                    CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" ELSE pbb.id_kualitas_dudul END AS id_kualitas_dudul,
                    CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" WHEN pbb.id_kualitas_dudul = 1 THEN "Baik" ELSE "Jelek" END AS kualitas_dudul,
                    CASE WHEN pbb.id_label = 0 THEN "" ELSE pbb.id_label END AS id_label, CASE WHEN mb2.nama_barang IS NULL THEN "" ELSE mb2.nama_barang END AS nama_label,
                    CASE WHEN pbb.id_kemasan = 0 THEN "" ELSE pbb.id_kemasan END AS id_kemasan,
                    CASE WHEN mb3.nama_barang IS NULL THEN "" ELSE mb3.nama_barang END AS nama_kemasan,
                    CASE WHEN pbb.id_kemasansatuan = 0 THEN "" ELSE pbb.id_kemasansatuan END AS id_kemasansatuan,
                    CASE WHEN mb4.nama_barang IS NULL THEN "" ELSE mb4.nama_barang END AS nama_kemasansatuan,
                    CASE WHEN pbb.id_kemasansepuluh = 0 THEN "" ELSE pbb.id_kemasansepuluh END AS id_kemasansepuluh,
                    CASE WHEN mb5.nama_barang IS NULL THEN "" ELSE mb5.nama_barang END AS nama_kemasansepuluh,
                    CASE WHEN pbb.id_kemasanseratus = 0 THEN "" ELSE pbb.id_kemasanseratus END AS id_kemasanseratus,
                    CASE WHEN mb6.nama_barang IS NULL THEN "" ELSE mb6.nama_barang END AS nama_kemasanseratus'))->where('pb.id_barangmasuk', $id_barangmasuk)->where('pb.id_barangkeluar', $id_barangkeluar)->where('pbk.id_pegawai',$id_pegawai)->where($order,$where_order)->where('pbk.id_proses',$id_proses);


                    $d_lain2 = DB::table('pro_upah_borongan_lain_lain')->where('id_upah_borongan', '=', $id_upah_borongan);
                    $d_upah_bayar=DB::table('pro_upah_total')->where('id_upah_borongan','=',$id_upah_borongan);
        

                // dd(DB::getQueryLog());
            if($id_jenis == '1'){
                $d_upah =  DB::table('ms_upah_borongan as mub')->join('ms_proses as mp', 'mp.id_proses', 'mub.id_proses')->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')->select(DB::raw('mub.*, mp.nama_proses, 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'))->where('mub.id_proses',$id_proses);

                $status_upah = '1';    
            }else{    
                $d_upah =  DB::table('ms_upah_pegawai as mup')->where('mup.id_pegawai',$id_pegawai);
                $status_upah = '2';        
            }
                
                if($d_hasil->count() > 0){
                    $arr['hasil'] = $d_hasil->get();
                    $arr['status_hasil'] = array('1');
                }else{
                    $arr['status_hasil'] = array('0');
                }

                if($d_upah->count() > 0){
                    $arr['upah'] = $d_upah->get();
                    $arr['s_upah'] = $status_upah;
                    $arr['status_upah'] = array('1');
                }else{
                    $arr['status_upah'] = array('0');
                }
                if ($d_lain2->count()>0) {
                    $arr['lain2']=$d_lain2->get();
                    $arr['status_lain2'] = array('1');
                }else{
                    $arr['lain2']='';
                    $arr['status_lain2'] = array('0');
                }
                if($d_upah_bayar->count()>0){
                    $arr['upah_bayar'] = $d_upah_bayar->get();
                }else{
                    $arr['upah_bayar'] = '';
                }
            }
            // echo "<pre>";print_r($arr);exit();

            require('fpdf181/Mc_table.php');
            $html = view('produksi.cetak.CetakResi')->with('data',$arr);
            return response($html)->header('Content-Type', 'application/pdf');

    }

    public function cetak_resi_rekanan($id,$jenis){
        if($jenis==0){
           $d_data = DB::table('pro_upahproduksi as pu')
                    ->join('pro_barangmasuk as pbm','pu.id_barangmasuk','pbm.id_barangmasuk')
                    ->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')
                    ->join('ms_proses as mp','pu.id_proses','mp.id_proses')
                    ->leftJoin('ms_rekanan as mr','pu.id_rekanan','mr.id_rekanan')
                    ->leftjoin('ms_pegawai as mpe','pu.id_pegawai','mpe.id_pegawai')
                    ->leftjoin('pem_workorder as pw','pu.id_workorder','pw.id_workorder')
                    ->leftjoin('pem_workorder_barang as pwb','pu.id_workorder_barang','pwb.id_workorder_barang')
                    ->select(DB::raw('pu.*, mp.nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai, pw.nomor as no_workorder, pwb.no_workorder as no_workorder_barang'))
                    ->where('pu.id_upahproduksi','=', $id)
                    ->first();

            $arr['data_penerima'] = $d_data;

            $id_barangmasuk       = $d_data->id_barangmasuk;
            $id_barangkeluar      = $d_data->id_barangkeluar;
            $id_workorder         = $d_data->id_workorder;
            $id_workorder_barang  = $d_data->id_workorder_barang;
            $id_proses            = $d_data->id_proses;
            $id_rekanan           = $d_data->id_rekanan;
            $id_pegawai           = $d_data->id_pegawai;
            $id_upah_borongan     = $id;
            $id_jenis             = $d_data->id_jenis;

            if($id_workorder != '' || $id_workorder != 0){
                $order       = 'id_workorder';
                $where_order = $id_workorder;
            }else{
                $order       = 'id_workorder_barang';
                $where_order = $id_workorder_barang;
            }

            if($id_rekanan != '' || $id_rekanan != 0){
                $pemroses       = 'pbk.id_rekanan';
                $where_pemroses = $id_rekanan;
            }else{
                $pemroses       = 'pbk.id_pegawai';
                $where_pemroses = 'pbk.id_pegawai';
            }

            $d_hasil = DB::table('pro_barangmasuk AS pb')
                        ->join('pro_barangkeluar as pbk','pb.id_barangkeluar','pbk.id_barangkeluar')
                        ->join('pro_barangmasuk_barang as pbb', 'pb.id_barangmasuk', 'pbb.id_barangmasuk')
                        ->join('ms_gudang as mg', 'pbb.id_gudang', 'mg.id_gudang')
                        ->join('ms_barang as mb', 'pbb.id_barang', 'mb.id')
                        ->leftjoin('ms_warnakomposisi as mwk', 'pbb.id_komposisi', 'mwk.id_komposisi')
                        ->join('ms_satuan AS ms', 'pbb.id_satuan', 'ms.id_satuan')
                        ->leftJoin('ms_motif_barang as mmb', 'pbb.id_motif_barang', 'mmb.id_motif_barang')
                        ->leftJoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')
                        ->leftJoin('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')
                        ->leftJoin('ms_barang as mb2', 'pbb.id_label', 'mb2.id')
                        ->leftJoin('ms_barang as mb3', 'pbb.id_kemasan', 'mb3.id')
                        ->leftJoin('ms_barang as mb4', 'pbb.id_kemasansatuan', 'mb4.id')
                        ->leftJoin('ms_barang as mb5', 'pbb.id_kemasansepuluh', 'mb5.id')
                        ->leftJoin('ms_barang as mb6', 'pbb.id_kemasanseratus', 'mb6.id')
                        ->select(DB::raw('pbb.*, pbk.id_proses, mb.kode_barang, mb.nama_barang, mg.id_gudang, mg.nama_gudang, pbb.id_satuan, ms.nama_satuan, 0 AS upah,
                            CASE WHEN mwk.id_komposisi IS NULL THEN "" ELSE  mwk.id_komposisi END AS id_komposisi, 
                            CASE WHEN mwk.nama IS NULL THEN "" ELSE  mwk.nama END AS nama_komposisi, 
                            CASE WHEN mmb.id_motif_barang IS NULL THEN "" ELSE mmb.id_motif_barang END AS id_motif_barang, 
                            CASE WHEN mmb.motif_keterangan IS NULL THEN "" ELSE mmb.motif_keterangan END AS motif_keterangan, 
                            CASE WHEN mm.id_motif IS NULL THEN "" ELSE mm.id_motif END AS id_motif, 
                            CASE WHEN mm.nama_motif IS NULL THEN "" ELSE mm.nama_motif END AS nama_motif, 
                            CASE WHEN mk.id_kualitas IS NULL THEN "" ELSE mk.id_kualitas END AS id_kualitas, 
                            CASE WHEN mk.nama_kualitas IS NULL THEN "" ELSE mk.nama_kualitas END AS nama_kualitas,
                            CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" ELSE pbb.id_kualitas_dudul END AS id_kualitas_dudul,
                            CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" WHEN pbb.id_kualitas_dudul = 1 THEN "Baik" ELSE "Jelek" END AS kualitas_dudul,
                            CASE WHEN pbb.id_label = 0 THEN "" ELSE pbb.id_label END AS id_label, CASE WHEN mb2.nama_barang IS NULL THEN "" ELSE mb2.nama_barang END AS nama_label,
                            CASE WHEN pbb.id_kemasan = 0 THEN "" ELSE pbb.id_kemasan END AS id_kemasan,
                            CASE WHEN mb3.nama_barang IS NULL THEN "" ELSE mb3.nama_barang END AS nama_kemasan,
                            CASE WHEN pbb.id_kemasansatuan = 0 THEN "" ELSE pbb.id_kemasansatuan END AS id_kemasansatuan,
                            CASE WHEN mb4.nama_barang IS NULL THEN "" ELSE mb4.nama_barang END AS nama_kemasansatuan,
                            CASE WHEN pbb.id_kemasansepuluh = 0 THEN "" ELSE pbb.id_kemasansepuluh END AS id_kemasansepuluh,
                            CASE WHEN mb5.nama_barang IS NULL THEN "" ELSE mb5.nama_barang END AS nama_kemasansepuluh,
                            CASE WHEN pbb.id_kemasanseratus = 0 THEN "" ELSE pbb.id_kemasanseratus END AS id_kemasanseratus,
                            CASE WHEN mb6.nama_barang IS NULL THEN "" ELSE mb6.nama_barang END AS nama_kemasanseratus'))
                        ->where('pb.id_barangmasuk', $id_barangmasuk)
                        ->where('pb.id_barangkeluar', $id_barangkeluar)
                        ->where('pbk.id_rekanan',$id_rekanan)
                        ->where($order,$where_order)
                        ->where('pbk.id_proses',$id_proses);

            $d_lain2 = DB::table('pro_upah_borongan_lain_lain')
                        ->where('id_upah_borongan', '=', $id_upah_borongan);

            $d_upah_bayar = DB::table('pro_upah_total')
                            ->where('id_upah_borongan','=',$id_upah_borongan);

            if($id_jenis == '1'){
                $d_upah =  DB::table('ms_upah_borongan as mub')
                            ->join('ms_proses as mp', 'mp.id_proses', 'mub.id_proses')
                            ->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')
                            ->select(DB::raw('mub.*, mp.nama_proses, 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'))
                            ->where('mub.id_proses',$id_proses);

                $status_upah = '1';
            }else{
                $d_upah      =  DB::table('ms_upah_pegawai as mup')
                                ->where('mup.id_pegawai',$id_pegawai);
                $status_upah = '2';
            }
                

            if($d_hasil->count() > 0){
                $arr['hasil']        = $d_hasil->get();
                $arr['status_hasil'] = array('1');
            }else{
                $arr['status_hasil'] = array('0');
            }

            if($d_upah->count() > 0){
                $arr['upah']        = $d_upah->get();
                $arr['s_upah']      = $status_upah;
                $arr['status_upah'] = array('1');
            }else{
                $arr['status_upah'] = array('0');
            }

            if ($d_lain2->count()>0) {
                $arr['lain2']        = $d_lain2->get();
                $arr['status_lain2'] = array('1');
            }else{
                $arr['status_lain2'] = array('1');                    
                $arr['lain2']        = '';
            }

            if($d_upah_bayar->count()>0){
                $arr['upah_bayar'] = $d_upah_bayar->get();
            }else{
                $arr['upah_bayar'] = '';
            }
            $arr['author'] = Session::get('nama');
        } else {
            $arr = [];
        }
        require('fpdf181/Mc_table.php');
        $html = view('produksi.cetak.CetakResiRekanan')->with('data',$arr);
        return response($html)->header('Content-Type', 'application/pdf');

    }

    public function simpan_bayar(Request $request){
        $id_upahproduksi        = $request->get('id_upahproduksi');
        $data['akun_bayar']     = $request->get('akun_bayar');
        $data['status_bayar']   = '1';
        $data['tanggal_bayar']  = tgl_full($request->get('tanggal'),'2');
        $data['jumlah_bayar']   = $request->get('jumlah_bayar');

        DB::table('pro_upahproduksi')->where('id_upahproduksi', $id_upahproduksi)->update($data);

        return redirect('upahproduksi');
    }

    public function simpan_bayar_rekanan(Request $request){
        $id_upahproduksi        = $request->get('id_upahproduksi');
        $data['akun_bayar']     = $request->get('akun_bayar');
        $data['status_bayar']   = '1';
        $data['tanggal_bayar']  = tgl_full($request->get('tanggal'),'2');
        $data['jumlah_bayar']   = $request->get('jumlah_bayar');

        DB::beginTransaction();
        try{
            DB::table('pro_upahproduksi')->where('id_upahproduksi', $id_upahproduksi)->update($data);
            DB::commit();
            return redirect('upahproduksi');
        }catch (Exception $e){
            DB::rollBack();
            return redirect()->back();
        }
    }

    public function getNoWorker($id,$params){
        DB::statement(DB::raw('set @rownum=0'));
        // DB::enableQueryLog();
        if($params==1){
            $barangkeluar = DB::table('pro_barangkeluar AS pb')->join('ms_proses AS mpr', 'pb.id_proses', 'mpr.id_proses')->leftJoin('ms_rekanan AS mr', 'pb.id_rekanan','mr.id_rekanan')->leftJoin('ms_pegawai AS mp', 'pb.id_pegawai', 'mp.id_pegawai')->leftjoin('pro_barangmasuk AS pbm','pbm.id_barangkeluar','pb.id_barangkeluar')->leftJoin('pem_workorder AS pw', 'pb.id_workorder', 'pw.id_workorder')->leftJoin('pem_workorder_barang AS pwb', 'pb.id_workorder_barang', 'pwb.id_workorder_barang')->where('pb.status', '!=', '0')->where('pb.id_barangkeluar','=',$id)->orderBy('pb.tanggal', 'desc')->orderBy('pb.status', 'asc')->select(DB::raw('pbm.id_barangmasuk id_barangmasuk, pb.id_barangkeluar, pb.id_rekanan, mr.nama nama_rekanan, pb.id_pegawai, mp.nama_pegawai, pb.id_proses, mpr.nama_proses, pb.id_workorder, pw.nomor no_workorder, pb.id_workorder_barang, pwb.no_workorder no_workorder_barang, mpr.id_proses, mpr.nama_proses, pb.tanggal, pb.status,pb.id_lokasi, @rownum  := @rownum  + 1 AS rownum'));
        }else if($params==2){
            $barangkeluar = DB::table('pro_barangkeluar AS pb')->join('ms_proses AS mpr', 'pb.id_proses', 'mpr.id_proses')->leftJoin('ms_rekanan AS mr', 'pb.id_rekanan','mr.id_rekanan')->leftJoin('ms_pegawai AS mp', 'pb.id_pegawai', 'mp.id_pegawai')->leftjoin('pro_barangmasuk AS pbm','pbm.id_barangkeluar','pb.id_barangkeluar')->leftJoin('pem_workorder AS pw', 'pb.id_workorder', 'pw.id_workorder')->leftJoin('pem_workorder_barang AS pwb', 'pb.id_workorder_barang', 'pwb.id_workorder_barang')->where('pb.status', '!=', '0')->where('pbm.id_barangmasuk','=',$id)->orderBy('pb.tanggal', 'desc')->orderBy('pb.status', 'asc')->select(DB::raw('pbm.id_barangmasuk id_barangmasuk, pb.id_barangkeluar, pb.id_rekanan, mr.nama nama_rekanan, pb.id_pegawai, mp.nama_pegawai, pb.id_proses, mpr.nama_proses, pb.id_workorder, pw.nomor no_workorder, pb.id_workorder_barang, pwb.no_workorder no_workorder_barang, mpr.id_proses, mpr.nama_proses, pb.tanggal, pb.status,pb.id_lokasi, @rownum  := @rownum  + 1 AS rownum'));

        }
        // dd(DB::getQueryLog());
        if($barangkeluar->count() > 0){
            foreach($barangkeluar->get() as $b){
                $pekerja = '';
                $stat_pekerja = '';
                $jenis = 0;
                $status = 0;
                $nomor = 0;
                $nomor_jenis = '';

                $id_produksi = 0;
                if($b->nama_rekanan != '' || !is_null($b->nama_rekanan)){
                    $pekerja = $b->nama_rekanan;
                    $stat_pekerja = 'Rekanan';
                }else{
                    $pekerja = $b->nama_pegawai;
                    $stat_pekerja = 'Pegawai';
                }

                if($b->no_workorder != ''){
                    $nomor = $b->no_workorder;
                    $nomor_jenis = "Nomor SPK";
                }else{
                    $nomor = $b->no_workorder_barang;
                    $nomor_jenis = "Nomor Order";
                }
                if($b->status == '1'){
                    $status = $b->status;
                    $jenis = 0;
                    $id_produksi = $b->id_barangkeluar;
                    $arr[] = array('id_barangkeluar' => $b->id_barangkeluar,
                                'no_workorder' => $nomor.' ('.$nomor_jenis.')',
                                'tanggal' => date('d-m-Y', strtotime($b->tanggal)),
                                'nama_proses' => $b->nama_proses,
                                'id_rekanan'=>$b->id_rekanan,
                                'id_pegawai' => $b->id_pegawai,
                                'pekerja' => $pekerja,
                                'idPros'=>$b->id_barangmasuk,
                                'stat_pekerja' => $stat_pekerja,
                                'proses' => $b->nama_proses,
                                'status_id' => $b->status,
                                'status' => $this->proses($jenis, $status),
                                'rownum' => $b->rownum,
                                'id_lokasi'=>$b->id_lokasi,
                                'posisi' => $pekerja.' ('.$stat_pekerja.')',
                                'aksi' => $this->aksi($jenis, $status, $id_produksi));
                }else{
                    $d_masuk = DB::table('pro_barangmasuk')->where('id_barangkeluar', $b->id_barangkeluar);
                    
                    foreach($d_masuk->get() as $masuk) {
                        if($b->status == '2'){
                            $status = $masuk->status;
                            $id_produksi = $masuk->id_barangmasuk;
                            $jenis = 1;
                        }else if($b->status == '3'){

                            $status = $masuk->status;
                            $id_produksi = $masuk->id_barangmasuk;
                            $jenis = 1;
                        }

                    //    print_r($status.' '.$id_produksi."\n");

                        $arr[] = array('id_barangkeluar' => $b->id_barangkeluar,
                                'no_workorder' => $nomor.' ('.$nomor_jenis.')',
                                'tanggal' => date('d-m-Y', strtotime($b->tanggal)),
                                'nama_proses' => $b->nama_proses,
                                'id_pegawai' => $b->id_pegawai,                                
                                'id_rekanan'=>$b->id_rekanan,
                                'idPros'=>$b->id_barangmasuk,
                                'pekerja' => $pekerja,
                                'stat_pekerja' => $stat_pekerja,
                                'proses' => $b->nama_proses,
                                'status_id' => $b->status,
                                'status' => $this->proses($jenis, $status),
                                'rownum' => $b->rownum,
                                'id_lokasi'=>$b->id_lokasi,
                                'posisi' => $pekerja.' ('.$stat_pekerja.')',
                                'aksi' => $this->aksi($jenis, $status, $id_produksi)
                            );  
                    } 

                    if($b->status == '3'){
                        $status = '1';
                        $jenis = 0;
                        $id_produksi = $b->id_barangkeluar;
                        $arr[] = array('id_barangkeluar' => $b->id_barangkeluar,
                                    'no_workorder' => $nomor.' ('.$nomor_jenis.')',
                                    'tanggal' => date('d-m-Y', strtotime($b->tanggal)),
                                    'nama_proses' => $b->nama_proses,
                                    'id_pegawai' => $b->id_pegawai,
                                    'id_rekanan'=>$b->id_rekanan,
                                    'idPros'=>$b->id_barangmasuk,
                                    'pekerja' => $pekerja,
                                    'stat_pekerja' => $stat_pekerja,
                                    'proses' => $b->nama_proses,
                                    'status_id' => $b->status,
                                    'status' => $this->proses($jenis, $status),
                                    'rownum' => $b->rownum,
                                    'posisi' => $pekerja.' ('.$stat_pekerja.')',
                                    'aksi' => $this->aksi($jenis, $status, $id_produksi));
                    }
                }
            }
        }else{
            $arr = array();
        }
        // print_r($d_data->get());
        $data = new Collection($arr);
        return response()->json($data);
    }
    function proses($jenis, $status){
        switch ($jenis) {
            case '0':
                if($status == 1){
                    $d = '<label class="label bg-warning-400">Belum Diterima</label>';
                }else if($status == 2){
                    $d = '<label class="label bg-warning-400">Belum Diterima</label>';
                }
                break;
            case '1':
                if($status == 0){
                    $d = '<label class="label bg-warning-400">Belum Diterima Gudang</label>';
                }else{
                    $d = '<label class="label bg-success-400">Selesai</label>';
                }
                break;
            default:
                $d = '<label class="label bg-info-400">Belum Verifikasi</label>';
                break;
        }

        return $d;
    }
    function aksi($jenis, $status,$id){
        switch ($jenis) {
            case '0':
                if($status == 1){
                    $d = '<a class="btn bg-info-400" type="button" href="'.url('barangmasuk/tambah/').'/'.$id.'"><i class="icon-checkbox-checked2"></i></a>';
                }else if($status == 2){
                    $d = '<a class="btn bg-info-400" type="button" href="'.url('barangmasuk/tambah/').'/'.$id.'"><i class="icon-checkbox-checked2"></i></a>';
                }
                
                break;
            case '1':
                if($status == 0){
                    $d = '<a class="btn bg-warning-400" type="button" href="'.url('barangmasuk/edit/').'/'.$id.'"><i class="icon-pencil3"></i></a> ';
                }else if($status == 1){
                    // <a class="btn bg-warning-400" type="button" href="'.url('barangmasuk/edit/').'/'.$id.'"><i class="icon- icon-pencil3"></i></a>
                    $d = ' <a class="btn bg-info-400" type="button" href="'.url('barangmasuk/detail/').'/'.$id.'"><i class="icon-notification2"></i></a>';
                }
                break;
            default:
                $d = '';
                break;
        }

        return $d;
    }

    public function rekananTambah($id_trans, $id_table)
    {
        // dd(DB::getQueryLog());
        $data['jenis_trx']=1; // opsi form pilihan tabel lama atau baru
        $data['rekanan'] = DB::table('ms_rekanan as mr')->join('ms_rekanan_kelompok as mrk','mr.id_kelompok','mrk.id_kelompok')->select(DB::raw('mr.*, mrk.nama as nama_kelompok'))->get();
        $data['pegawai'] = DB::table('ms_pegawai')->get();
        $data['produksi'] = DB::table('ms_lokasi_produksi')->get();
        $data['id_trans']=$id_trans;
        $data['id_tabel']=$id_table;
        $akun_upah  = DB::table("ms_perkiraan")->where([ ["kd_rek_1", '=', '5'] ])->get();
        $data['akun_upah'] = array_reduce($akun_upah->toArray(), function($carry, $item){
            $carry[ $item->id ] = format_akun_perkiraan($item);
            return $carry;
        });
        $data['satuan'] = DB::table('ms_satuan')->whereIn("id_satuan", [99, 100])->get();
        $data['keterangan_upah_harian'] = DB::table('ms_upahket')->get();

        $data['data'] = $this->data(null);

        $lap_hasil = DB::table('pro_barangkeluar AS pb')
        ->join('ms_proses AS mpr', 'pb.id_proses', 'mpr.id_proses')
        ->leftJoin('ms_rekanan AS mr', 'pb.id_rekanan','mr.id_rekanan')
        ->leftJoin('ms_pegawai AS mp', 'pb.id_pegawai', 'mp.id_pegawai')
        ->where('pb.status', '!=', '0')
        // ->where('pb.id_proses', '!=', '37')
        ->where('pb.id_lokasi', '=', '1')
        ->orderBy('pb.tanggal', 'desc')
        ->orderBy('pb.status', 'asc')
        ->select('pb.id_barangkeluar', 'pb.id_rekanan', 'pb.noAuto', 'pb.noAuto_ext');

        if ($data['data']['id_rekanan'] != 0) {
            $data['laporan_hasil'] = $lap_hasil->where('pb.id_rekanan', '=', $data['data']['id_rekanan'])->get();
        } else if($data['data']['id_pegawai'] != 0) {
            $data['laporan_hasil'] = $lap_hasil->where('pb.id_pegawai', '=', $data['data']['id_pegawai'])->get();
        } else {
            $data['laporan_hasil'] = [];
        }

        return view('produksi.rekanan.UpahproduksiTambah')->with('data',$data);
        // return view('produksi.rekanan.UpahproduksiTambah');
    }

    
    public function tambah($id_laporan_hasil){
        $data['jenis_trx']  = 1; // opsi form pilihan tabel lama atau baru
        $data['rekanan']    = DB::table('ms_rekanan as mr')->join('ms_rekanan_kelompok as mrk','mr.id_kelompok','mrk.id_kelompok')->select(DB::raw('mr.*, mrk.nama as nama_kelompok'))->get();
        $data['pegawai']    = DB::table('ms_pegawai')->get();
        $data['produksi']   = DB::table('ms_lokasi_produksi')->get();
        $data['id_trans']   = $id_laporan_hasil; // id_laporan_hasil => id_barangmasuk
        $data['id_tabel']   = 2;
        $akun_upah  = DB::table("ms_perkiraan")->where([ ["kd_rek_1", '=', '5'] ])->get();
        $data['akun_upah'] = array_reduce($akun_upah->toArray(), function($carry, $item){
            $carry[ $item->id ] = format_akun_perkiraan($item);
            return $carry;
        });
        $data['satuan'] = DB::table('ms_satuan')->whereIn("id_satuan", [99, 100])->get();
        $data['keterangan_upah_harian'] = DB::table('ms_upahket')->get();
        $q_barangmasuk = DB::table("pro_barangmasuk AS bm")
                        ->join("pro_barangkeluar AS bk", "bk.id_barangkeluar", "=", "bm.id_barangkeluar")
                        ->leftJoin("ms_pegawai AS pg", "pg.id_pegawai", "=", "bk.id_pegawai")
                        ->leftJoin("ms_rekanan AS rk", "rk.id_rekanan", "=", "bk.id_rekanan")
                        ->where([ ['bm.id_barangmasuk', '=', $id_laporan_hasil] ])
                        ->selectRaw("
                                bm.*,
                                bk.id_rekanan,
                                bk.id_pegawai,
                                bk.id_mesin,
                                bk.id_proses,
                                bk.id_proses_sebelum,
                                bk.id_workorder,
                                bk.id_workorder_barang,
                                bk.id_lokasi,
                                pg.nomor_pegawai,
                                pg.nama_pegawai,
                                rk.nama AS nama_rekanan
                            ")
                        ->first();
        // dd(Session::all());
        $data['laporan_hasil'] = $q_barangmasuk;

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

    public function tambah_old($id_trans,$id_table){
        // DB::enableQueryLog();
        $data['barangkeluar'] = DB::table('pro_barangkeluar AS pb')->join('ms_proses AS mpr', 'pb.id_proses', 'mpr.id_proses')->leftJoin('ms_rekanan AS mr', 'pb.id_rekanan','mr.id_rekanan')->leftJoin('ms_pegawai AS mp', 'pb.id_pegawai', 'mp.id_pegawai')->leftJoin('pem_workorder AS pw', 'pb.id_workorder', 'pw.id_workorder')->leftJoin('pem_workorder_barang AS pwb', 'pb.id_workorder_barang', 'pwb.id_workorder_barang')->where('pb.status', '=', '2')->orderBy('pb.tanggal', 'desc')->orderBy('pb.status', 'asc')->select(DB::raw('pb.id_barangkeluar, pb.id_rekanan, mr.nama nama_rekanan, pb.id_pegawai, mp.nama_pegawai, pb.id_proses, mpr.nama_proses, pb.id_workorder, pw.nomor no_workorder, pb.id_workorder_barang, pwb.no_workorder no_workorder_barang, mpr.id_proses, mpr.nama_proses, pb.tanggal, pb.status'))->get();

        // dd(DB::getQueryLog());
        $data['jenis_trx']=1; // opsi form pilihan tabel lama atau baru
        $data['rekanan'] = DB::table('ms_rekanan as mr')->join('ms_rekanan_kelompok as mrk','mr.id_kelompok','mrk.id_kelompok')->select(DB::raw('mr.*, mrk.nama as nama_kelompok'))->get();
        $data['pegawai'] = DB::table('ms_pegawai')->get();
        $data['produksi'] = DB::table('ms_lokasi_produksi')->get();
        $data['id_trans']=$id_trans;
        $data['id_tabel']=$id_table;
        $data['data'] = $this->data(array());

        $lap_hasil = DB::table('pro_barangkeluar AS pb')
        ->join('ms_proses AS mpr', 'pb.id_proses', 'mpr.id_proses')
        ->leftJoin('ms_rekanan AS mr', 'pb.id_rekanan','mr.id_rekanan')
        ->leftJoin('ms_pegawai AS mp', 'pb.id_pegawai', 'mp.id_pegawai')
        ->where('pb.status', '!=', '0')
        ->where('pb.id_proses', '!=', '37')
        ->where('pb.id_lokasi', '=', '1')
        ->orderBy('pb.tanggal', 'desc')
        ->orderBy('pb.status', 'asc')
        ->select('pb.id_barangkeluar', 'pb.id_rekanan', 'pb.noAuto', 'pb.noAuto_ext');

        if ($data['data']['id_rekanan'] != 0) {
            $data['laporan_hasil'] = $lap_hasil->where('pb.id_rekanan', '=', $data['data']['id_rekanan'])->get();
        } else if($data['data']['id_pegawai'] != 0) {
            $data['laporan_hasil'] = $lap_hasil->where('pb.id_pegawai', '=', $data['data']['id_pegawai'])->get();
        } else {
            $data['laporan_hasil'] = [];
        }

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

    public function getLaporanHasil($param, $owner, $id_proses=0){
        $lap_hasil = DB::table('pro_barangkeluar AS pb')
        ->join('pro_barangmasuk as pm', function($pm){
            $pm->on('pb.id_barangkeluar', '=', 'pm.id_barangkeluar');
        })
        ->join('ms_proses AS mpr', 'pb.id_proses', 'mpr.id_proses')
        ->leftJoin('ms_rekanan AS mr', 'pb.id_rekanan','mr.id_rekanan')
        ->leftJoin('ms_pegawai AS mp', 'pb.id_pegawai', 'mp.id_pegawai')
        ->where('pb.status', '!=', '0')
        // ->where('pb.id_proses', '!=', '37')
        ->where('pb.id_lokasi', '=', '1')
        ->orderBy('pb.tanggal', 'desc')
        ->orderBy('pb.status', 'asc')
        ->select('pm.id_barangmasuk', 'pm.id_barangkeluar', 'pm.noAuto', 'pm.noAuto_ext');

        $prosesKhusus = [37];
        if(in_array($id_proses, $prosesKhusus)){
            $lap_hasil->where([ ["pb.id_proses", "=", $id_proses] ]);
        }else{
            $lap_hasil->whereNotIn("pb.id_proses", $prosesKhusus);
        }

        if ($owner == "rekanan") {
            $data = $lap_hasil->where('pb.id_rekanan', '=', $param)->get();
        } else if($owner == "pegawai") {
            $data = $lap_hasil->where('pb.id_pegawai', '=', $param)->get();
        } else {
            $data = [];
        }

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

    public function editRekanan($id){
        // DB::enableQueryLog();
        $d_data = DB::table('pro_upahproduksi as pu')
        ->join('pro_barangmasuk as pbm','pu.id_barangmasuk','pbm.id_barangmasuk')
        ->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')
        ->join('ms_proses as mp','pu.id_proses','mp.id_proses')
        ->leftJoin('ms_rekanan as mr','pu.id_rekanan','mr.id_rekanan')
        ->leftjoin('ms_pegawai as mpe','pu.id_pegawai','mpe.id_pegawai')
        ->leftjoin('pem_workorder as pw','pu.id_workorder','pw.id_workorder')
        ->leftjoin('pem_workorder_barang as pwb','pu.id_workorder_barang','pwb.id_workorder_barang')
        ->select(DB::raw('pu.*, mp.nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai, pw.nomor as no_workorder, pwb.no_workorder as no_workorder_barang'))
        ->where('pu.id_upahproduksi','=', $id)
        ->first();
        // dd(DB::getQueryLog());
        $data['barangkeluar'] = DB::table('pro_barangkeluar AS pb')->join('ms_proses AS mpr', 'pb.id_proses', 'mpr.id_proses')->leftJoin('ms_rekanan AS mr', 'pb.id_rekanan','mr.id_rekanan')->leftJoin('ms_pegawai AS mp', 'pb.id_pegawai', 'mp.id_pegawai')->leftJoin('pem_workorder AS pw', 'pb.id_workorder', 'pw.id_workorder')->leftJoin('pem_workorder_barang AS pwb', 'pb.id_workorder_barang', 'pwb.id_workorder_barang')->where('pb.status', '=', '2')->orderBy('pb.tanggal', 'desc')->orderBy('pb.status', 'asc')->select(DB::raw('pb.id_barangkeluar, pb.id_rekanan, mr.nama nama_rekanan, pb.id_pegawai, mp.nama_pegawai, pb.id_proses, mpr.nama_proses, pb.id_workorder, pw.nomor no_workorder, pb.id_workorder_barang, pwb.no_workorder no_workorder_barang, mpr.id_proses, mpr.nama_proses, pb.status'))->get();
        $data['rekanan'] = DB::table('ms_rekanan')->get();
        $data['pegawai'] = DB::table('ms_pegawai')->get();
        $data['id_trans']=$id;
        $data['jenis_trx']=1;
        $data['id_tabel']=1;
        $data['produksi'] = DB::table('ms_lokasi_produksi')->get();
        $akun_upah  = DB::table("ms_perkiraan")->where([ ["kd_rek_1", '=', '5'] ])->get();
        $data['akun_upah'] = array_reduce($akun_upah->toArray(), function($carry, $item){
            $carry[ $item->id ] = format_akun_perkiraan($item);
            return $carry;
        });
        $data['satuan'] = DB::table('ms_satuan')->whereIn("id_satuan", [99, 100])->get();
        $data['keterangan_upah_harian'] = DB::table('ms_upahket')->get();
        $data['data'] = $this->data($d_data);
        
        //print_r($d_data);
        
        return view('produksi.rekanan.UpahproduksiTambah')->with('data',$data);

    }

    public function edit($id_upahproduksi){
        $d_data = DB::table('pro_upahproduksi as pu')
            ->join('ms_proses as mp','pu.id_proses','mp.id_proses')
            ->leftJoin('ms_rekanan as mr','pu.id_rekanan','mr.id_rekanan')
            ->leftjoin('ms_pegawai as mpe','pu.id_pegawai','mpe.id_pegawai')
            ->leftjoin('pem_workorder as pw','pu.id_workorder','pw.id_workorder')
            ->leftjoin('pem_workorder_barang as pwb','pu.id_workorder_barang','pwb.id_workorder_barang')
            ->select(DB::raw('pu.*, mp.nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai, pw.nomor as no_workorder, pwb.no_workorder as no_workorder_barang'))->where('pu.id_upahproduksi','=', $id_upahproduksi)->first();

        $data['jenis_trx']  = 1; // opsi form pilihan tabel lama atau baru
        $data['rekanan']    = DB::table('ms_rekanan as mr')->join('ms_rekanan_kelompok as mrk','mr.id_kelompok','mrk.id_kelompok')->select(DB::raw('mr.*, mrk.nama as nama_kelompok'))->get();
        $data['pegawai']    = DB::table('ms_pegawai')->get();
        $data['produksi']   = DB::table('ms_lokasi_produksi')->get();
        $data['id_trans']   = $id_upahproduksi;
        $data['id_tabel']   = 2;
        $akun_upah  = DB::table("ms_perkiraan")->where([ ["kd_rek_1", '=', '5'] ])->get();
        $data['akun_upah'] = array_reduce($akun_upah->toArray(), function($carry, $item){
            $carry[ $item->id ] = format_akun_perkiraan($item);
            return $carry;
        });
        $data['satuan'] = DB::table('ms_satuan')->whereIn("id_satuan", [99, 100])->get();
        $data['keterangan_upah_harian'] = DB::table('ms_upahket')->get();
        $data['edit'] = 1;

        $data['data']       = $this->data($d_data);
        // dd($data['data']);
        return view('produksi.UpahproduksiTambah2')->with('data',$data);
    }

    public function edit_old($id){
        // DB::enableQueryLog();
        $d_data = DB::table('pro_upahproduksi as pu')->join('pro_barangmasuk as pbm','pu.id_barangmasuk','pbm.id_barangmasuk')->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')->join('ms_proses as mp','pu.id_proses','mp.id_proses')->leftJoin('ms_rekanan as mr','pu.id_rekanan','mr.id_rekanan')->leftjoin('ms_pegawai as mpe','pu.id_pegawai','mpe.id_pegawai')->leftjoin('pem_workorder as pw','pu.id_workorder','pw.id_workorder')->leftjoin('pem_workorder_barang as pwb','pu.id_workorder_barang','pwb.id_workorder_barang')->select(DB::raw('pu.*, mp.nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai, pw.nomor as no_workorder, pwb.no_workorder as no_workorder_barang'))->where('pu.id_upahproduksi','=', $id)->first();
        // dd(DB::getQueryLog());
        $data['barangkeluar'] = DB::table('pro_barangkeluar AS pb')->join('ms_proses AS mpr', 'pb.id_proses', 'mpr.id_proses')->leftJoin('ms_rekanan AS mr', 'pb.id_rekanan','mr.id_rekanan')->leftJoin('ms_pegawai AS mp', 'pb.id_pegawai', 'mp.id_pegawai')->leftJoin('pem_workorder AS pw', 'pb.id_workorder', 'pw.id_workorder')->leftJoin('pem_workorder_barang AS pwb', 'pb.id_workorder_barang', 'pwb.id_workorder_barang')->where('pb.status', '=', '2')->orderBy('pb.tanggal', 'desc')->orderBy('pb.status', 'asc')->select(DB::raw('pb.id_barangkeluar, pb.id_rekanan, mr.nama nama_rekanan, pb.id_pegawai, mp.nama_pegawai, pb.id_proses, mpr.nama_proses, pb.id_workorder, pw.nomor no_workorder, pb.id_workorder_barang, pwb.no_workorder no_workorder_barang, mpr.id_proses, mpr.nama_proses, pb.status'))->get();
        $data['rekanan'] = DB::table('ms_rekanan')->get();
        $data['pegawai'] = DB::table('ms_pegawai')->get();
        $data['id_trans']=$id;
        $data['jenis_trx']=1;
        $data['id_tabel']=1;
        $data['produksi'] = DB::table('ms_lokasi_produksi')->get();
        $data['data'] = $this->data($d_data);
        
        //print_r($d_data);
        
        return view('produksi.UpahproduksiTambah')->with('data',$data);

    }


    function data($data = array()){
        if(count($data) > 0){
            $store['id_upahproduksi'] = $data->id_upahproduksi;
            $store['id_barangmasuk'] = $data->id_barangmasuk;
            $store['id_barangkeluar'] = $data->id_barangkeluar;
            $store['id_proses'] = $data->id_proses;
            $store['nama_proses'] = $data->nama_proses;
            $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['tanggal'] = tgl_full($data->tanggal,'2');
            $store['id_jenis'] = $data->id_jenis;
            $store['id_akun_upah'] = $data->id_akun_upah;
            $store['nomor'] = $data->nomor;
            $store['lap_hasil'] = $data->lap_hasil;
            $store['id_workorder'] = $data->id_workorder;
            $store['no_workorder'] = $data->no_workorder;
            $store['id_workorder_barang'] = $data->id_workorder_barang;
            $store['no_workorder_barang'] = $data->no_workorder_barang;
            $store['jenis'] = $data->id_lokasi;

            $store['termasuk_pajak'] = '0';
            $store['pajak'] = $data->pajak;
            $store['potongan'] = '0';
            $store['nominal_ongkir'] = '0';
        }else{
            $store['id_upahproduksi'] = '';
            $store['id_barangmasuk'] = '';
            $store['id_barangkeluar'] = '';
            $store['id_proses'] = '';
            $store['nama_proses'] = '';
            $store['id_pegawai'] = '';
            $store['nama_pegawai'] = '';
            $store['id_rekanan'] = '';
            $store['nama_rekanan'] = '';
            $store['tanggal'] = '';
            $store['id_jenis'] = '1';
            $store['id_akun_upah'] = '103';
            $store['nomor'] = '';
            $store['lap_hasil'] = '0';
            $store['id_workorder'] = '';
            $store['no_workorder'] = '';
            $store['id_workorder_barang'] = '';
            $store['no_workorder_barang'] = '';
            $store['jenis'] = Session::get("id_lokasi");

            $store['termasuk_pajak'] = '0';
            $store['pajak'] = '0';
            $store['potongan'] = '0';
            $store['nominal_ongkir'] = '0';
        }
        return $store;
    }

    function get_satuan(){
        $d_data = DB::table('ms_satuan')->whereIn("id_satuan", [99, 100]);

        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_ket(){
        $d_data = DB::table('ms_upahket');

        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_perkiraan(){
        $d_data = DB::table('ms_perkiraan')->where('used','=','1');
        if($d_data->count() > 0){
            $arr['data'] = $d_data->get();
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

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

    function getRekananLapHasil(Request $request)
    {
        $lap_hasil           = $request->get('lap_hasil');
        $id_barangmasuk      = $request->get('id_barangmasuk');
        $id_barangkeluar     = $request->get('id_barangkeluar');
        $id_workorder        = $request->get('id_workorder');
        $id_workorder_barang = $request->get('id_workorder_barang');
        $id_proses           = $request->get('id_proses');
        $id_rekanan          = $request->get('id_rekanan');
        $id_pegawai          = $request->get('id_pegawai');
        $id_upah_borongan    = $request->get('id_upah_borongan');
        $id_jenis            = $request->get('id_jenis');
        $tanggal_awal        = $request->get('tanggal_awal');
        $tanggal_akhir       = $request->get('tanggal_akhir');
        array_push($lap_hasil, $id_barangkeluar);
        
        if($id_workorder != '' || $id_workorder != 0){
            $order = 'id_workorder';
            $where_order = $id_workorder;
        }else{
            $order = 'id_workorder_barang';
            $where_order = $id_workorder_barang;
        }

        if($id_rekanan != '' || $id_rekanan != 0){
            $pemroses = 'pbk.id_rekanan';
            $where_pemroses = $id_rekanan;
        }else{
            $pemroses = 'pbk.id_pegawai';
            $where_pemroses = 'pbk.id_pegawai';
        }

        $d_hasil = DB::table('pro_barangmasuk AS pb')->join('pro_barangkeluar as pbk','pb.id_barangkeluar','pbk.id_barangkeluar')->join('pro_barangmasuk_barang as pbb', 'pb.id_barangmasuk', 'pbb.id_barangmasuk')->join('ms_gudang as mg', 'pbb.id_gudang', 'mg.id_gudang')->join('ms_barang as mb', 'pbb.id_barang', 'mb.id')->leftjoin('ms_warnakomposisi as mwk', 'pbb.id_komposisi', 'mwk.id_komposisi')->join('ms_satuan AS ms', 'pbb.id_satuan', 'ms.id_satuan')->leftJoin('ms_motif_barang as mmb', 'pbb.id_motif_barang', 'mmb.id_motif_barang')->leftJoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')->leftJoin('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')->leftJoin('ms_barang as mb2', 'pbb.id_label', 'mb2.id')->leftJoin('ms_barang as mb3', 'pbb.id_kemasan', 'mb3.id')->leftJoin('ms_barang as mb4', 'pbb.id_kemasansatuan', 'mb4.id')->leftJoin('ms_barang as mb5', 'pbb.id_kemasansepuluh', 'mb5.id')->leftJoin('ms_barang as mb6', 'pbb.id_kemasanseratus', 'mb6.id')
            // ->leftJoin('ms_upah_borongan AS mub',DB::raw('mub.id_proses=pbk.id_proses AND CASE WHEN pbk.id_proses'),DB::raw('23 THEN mub.id_barang =  pbb.id_barang ELSE mub.id_motif=mm.id_motif AND mub.id_motif_barang=pbb.id_motif_barang  END'))
        ->select(DB::raw('pbb.*,pbk.id_proses, mb.kode_barang, mb.nama_barang, mg.id_gudang, mg.nama_gudang, pbb.id_satuan, ms.nama_satuan, 0 AS upah,
            CASE WHEN mwk.id_komposisi IS NULL THEN "" ELSE  mwk.id_komposisi END AS id_komposisi, 
            CASE WHEN mwk.nama IS NULL THEN "" ELSE  mwk.nama END AS nama_komposisi, 
            CASE WHEN mmb.id_motif_barang IS NULL THEN "" ELSE mmb.id_motif_barang END AS id_motif_barang, 
            CASE WHEN mmb.motif_keterangan IS NULL THEN "" ELSE mmb.motif_keterangan END AS motif_keterangan, 
            CASE WHEN mm.id_motif IS NULL THEN "" ELSE mm.id_motif END AS id_motif, 
            CASE WHEN mm.nama_motif IS NULL THEN "" ELSE mm.nama_motif END AS nama_motif, 
            CASE WHEN mk.id_kualitas IS NULL THEN "" ELSE mk.id_kualitas END AS id_kualitas, 
            CASE WHEN mk.nama_kualitas IS NULL THEN "" ELSE mk.nama_kualitas END AS nama_kualitas,
            CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" ELSE pbb.id_kualitas_dudul END AS id_kualitas_dudul,
            CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" WHEN pbb.id_kualitas_dudul = 1 THEN "Baik" ELSE "Jelek" END AS kualitas_dudul,
             CASE WHEN pbb.id_label = 0 THEN "" ELSE pbb.id_label END AS id_label, CASE WHEN mb2.nama_barang IS NULL THEN "" ELSE mb2.nama_barang END AS nama_label,
            CASE WHEN pbb.id_kemasan = 0 THEN "" ELSE pbb.id_kemasan END AS id_kemasan,
            CASE WHEN mb3.nama_barang IS NULL THEN "" ELSE mb3.nama_barang END AS nama_kemasan,
            CASE WHEN pbb.id_kemasansatuan = 0 THEN "" ELSE pbb.id_kemasansatuan END AS id_kemasansatuan,
            CASE WHEN mb4.nama_barang IS NULL THEN "" ELSE mb4.nama_barang END AS nama_kemasansatuan,
            CASE WHEN pbb.id_kemasansepuluh = 0 THEN "" ELSE pbb.id_kemasansepuluh END AS id_kemasansepuluh,
            CASE WHEN mb5.nama_barang IS NULL THEN "" ELSE mb5.nama_barang END AS nama_kemasansepuluh,
            CASE WHEN pbb.id_kemasanseratus = 0 THEN "" ELSE pbb.id_kemasanseratus END AS id_kemasanseratus,
            CASE WHEN mb6.nama_barang IS NULL THEN "" ELSE mb6.nama_barang END AS nama_kemasanseratus'))
        ->where('pb.id_barangmasuk', $id_barangmasuk)
        ->whereIn('pb.id_barangkeluar', $lap_hasil)
        ->where('pbk.id_rekanan',$id_rekanan)
        ->where($order,$where_order)
        ->where('pbk.id_proses',$id_proses);

        $d_lain2        = DB::table('pro_upah_borongan_lain_lain')->where('id_upah_borongan', '=', $id_upah_borongan);
        $d_upah_bayar   = DB::table('pro_upah_total')->where('id_upah_borongan','=',$id_upah_borongan);

        if($id_jenis == '1'){
            $d_upah      = DB::table('ms_upah_borongan as mub')
                            ->join('ms_proses as mp', 'mp.id_proses', 'mub.id_proses')
                            ->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')
                            ->select(DB::raw('mub.*, mp.nama_proses, 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'))
                            ->where('mub.id_proses',$id_proses);
            $status_upah = '1';
        }else{
           $d_upah      = DB::table('ms_upah_pegawai as mup')->where('mup.id_pegawai',$id_pegawai);
           $status_upah = '2';
        }

        if($d_hasil->count() > 0){
            $arr['hasil'] = $d_hasil->get();
            $arr['status_hasil'] = array('1');
        }else{
            $arr['status_hasil'] = array('0');
        }

        if($d_upah->count() > 0){
            $arr['upah'] = $d_upah->get();
            $arr['s_upah'] = $status_upah;
            $arr['status_upah'] = array('1');
        }else{
            $arr['status_upah'] = array('0');
        }

        if ($d_lain2->count()>0) {
            $arr['lain2']=$d_lain2->get();
            $arr['status_lain2'] = array('1');
        }else{
            $arr['status_lain2'] = array('1');
        }

        if($d_upah_bayar->count()>0){
            $arr['upah_bayar'] = $d_upah_bayar->get();
        }else{
            $arr['upah_bayar'] = '';
        }

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

    function get_rekanan(Request $request){
        $lap_hasil           = $request->get('lap_hasil');
        $id_barangmasuk      = $request->get('id_barangmasuk');
        $id_barangkeluar     = $request->get('id_barangkeluar');
        $id_workorder        = $request->get('id_workorder');
        $id_workorder_barang = $request->get('id_workorder_barang');
        $id_proses           = $request->get('id_proses');
        $id_rekanan          = $request->get('id_rekanan');
        $id_pegawai          = $request->get('id_pegawai');
        $id_upah_borongan    = $request->get('id_upah_borongan');
        $id_jenis            = $request->get('id_jenis');
        $tanggal_awal        = $request->get('tanggal_awal');
        $tanggal_akhir       = $request->get('tanggal_akhir');

        if($id_workorder != '' || $id_workorder != 0){
            $order = 'id_workorder';
            $where_order = $id_workorder;
        }else{
            $order = 'id_workorder_barang';
            $where_order = $id_workorder_barang;
        }

        if($id_rekanan != '' || $id_rekanan != 0){
            $pemroses = 'pbk.id_rekanan';
            $where_pemroses = $id_rekanan;
        }else{
            $pemroses = 'pbk.id_pegawai';
            $where_pemroses = 'pbk.id_pegawai';
        }

        // DB::enableQueryLog();
        $d_hasil = DB::table('pro_barangmasuk AS pb')->join('pro_barangkeluar as pbk','pb.id_barangkeluar','pbk.id_barangkeluar')->join('pro_barangmasuk_barang as pbb', 'pb.id_barangmasuk', 'pbb.id_barangmasuk')->join('ms_gudang as mg', 'pbb.id_gudang', 'mg.id_gudang')->join('ms_barang as mb', 'pbb.id_barang', 'mb.id')->leftjoin('ms_warnakomposisi as mwk', 'pbb.id_komposisi', 'mwk.id_komposisi')->join('ms_satuan AS ms', 'pbb.id_satuan', 'ms.id_satuan')->leftJoin('ms_motif_barang as mmb', 'pbb.id_motif_barang', 'mmb.id_motif_barang')->leftJoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')->leftJoin('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')->leftJoin('ms_barang as mb2', 'pbb.id_label', 'mb2.id')->leftJoin('ms_barang as mb3', 'pbb.id_kemasan', 'mb3.id')->leftJoin('ms_barang as mb4', 'pbb.id_kemasansatuan', 'mb4.id')->leftJoin('ms_barang as mb5', 'pbb.id_kemasansepuluh', 'mb5.id')->leftJoin('ms_barang as mb6', 'pbb.id_kemasanseratus', 'mb6.id')
            // ->leftJoin('ms_upah_borongan AS mub',DB::raw('mub.id_proses=pbk.id_proses AND CASE WHEN pbk.id_proses'),DB::raw('23 THEN mub.id_barang =  pbb.id_barang ELSE mub.id_motif=mm.id_motif AND mub.id_motif_barang=pbb.id_motif_barang  END'))
        ->select(DB::raw('pbb.*,pbk.id_proses, mb.kode_barang, mb.nama_barang, mg.id_gudang, mg.nama_gudang, pbb.id_satuan, ms.nama_satuan, 0 AS upah,
            CASE WHEN mwk.id_komposisi IS NULL THEN "" ELSE  mwk.id_komposisi END AS id_komposisi, 
            CASE WHEN mwk.nama IS NULL THEN "" ELSE  mwk.nama END AS nama_komposisi, 
            CASE WHEN mmb.id_motif_barang IS NULL THEN "" ELSE mmb.id_motif_barang END AS id_motif_barang, 
            CASE WHEN mmb.motif_keterangan IS NULL THEN "" ELSE mmb.motif_keterangan END AS motif_keterangan, 
            CASE WHEN mm.id_motif IS NULL THEN "" ELSE mm.id_motif END AS id_motif, 
            CASE WHEN mm.nama_motif IS NULL THEN "" ELSE mm.nama_motif END AS nama_motif, 
            CASE WHEN mk.id_kualitas IS NULL THEN "" ELSE mk.id_kualitas END AS id_kualitas, 
            CASE WHEN mk.nama_kualitas IS NULL THEN "" ELSE mk.nama_kualitas END AS nama_kualitas,
            CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" ELSE pbb.id_kualitas_dudul END AS id_kualitas_dudul,
            CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" WHEN pbb.id_kualitas_dudul = 1 THEN "Baik" ELSE "Jelek" END AS kualitas_dudul,
             CASE WHEN pbb.id_label = 0 THEN "" ELSE pbb.id_label END AS id_label, CASE WHEN mb2.nama_barang IS NULL THEN "" ELSE mb2.nama_barang END AS nama_label,
            CASE WHEN pbb.id_kemasan = 0 THEN "" ELSE pbb.id_kemasan END AS id_kemasan,
            CASE WHEN mb3.nama_barang IS NULL THEN "" ELSE mb3.nama_barang END AS nama_kemasan,
            CASE WHEN pbb.id_kemasansatuan = 0 THEN "" ELSE pbb.id_kemasansatuan END AS id_kemasansatuan,
            CASE WHEN mb4.nama_barang IS NULL THEN "" ELSE mb4.nama_barang END AS nama_kemasansatuan,
            CASE WHEN pbb.id_kemasansepuluh = 0 THEN "" ELSE pbb.id_kemasansepuluh END AS id_kemasansepuluh,
            CASE WHEN mb5.nama_barang IS NULL THEN "" ELSE mb5.nama_barang END AS nama_kemasansepuluh,
            CASE WHEN pbb.id_kemasanseratus = 0 THEN "" ELSE pbb.id_kemasanseratus END AS id_kemasanseratus,
            CASE WHEN mb6.nama_barang IS NULL THEN "" ELSE mb6.nama_barang END AS nama_kemasanseratus'))
        // ->where('pb.id_barangmasuk', $id_barangmasuk)
        ->where('pb.id_barangkeluar', $id_barangkeluar)
        ->where('pbk.id_rekanan',$id_rekanan)
        ->where($order,$where_order)
        ->where('pbk.id_proses',$id_proses);

        if ($lap_hasil == "" || $lap_hasil == null) {
            $d_hasil->where('pb.id_barangmasuk', $id_barangmasuk);
        } else {
            array_push($lap_hasil, $id_barangmasuk);
            $d_hasil->whereIn('pb.id_barangmasuk', $lap_hasil);
        }

        // dd(DB::getQueryLog());
        $d_lain2 = DB::table('pro_upah_borongan_lain_lain')->where('id_upah_borongan', '=', $id_upah_borongan);
        $d_upah_bayar=DB::table('pro_upah_total')->where('id_upah_borongan','=',$id_upah_borongan);
        
    if($id_jenis == '1'){
        $d_upah =  DB::table('ms_upah_borongan as mub')->join('ms_proses as mp', 'mp.id_proses', 'mub.id_proses')->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')->select(DB::raw('mub.*, mp.nama_proses, 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'))->where('mub.id_proses',$id_proses);

        $status_upah = '1';
    }else{
       $d_upah =  DB::table('ms_upah_pegawai as mup')->where('mup.id_pegawai',$id_pegawai);
       $status_upah = '2';
    }
        

        if($d_hasil->count() > 0){
            $arr['hasil'] = $d_hasil->get();
            $arr['status_hasil'] = array('1');
        }else{
            $arr['status_hasil'] = array('0');
        }

        if($d_upah->count() > 0){
            $arr['upah'] = $d_upah->get();
            $arr['s_upah'] = $status_upah;
            $arr['status_upah'] = array('1');
        }else{
            $arr['status_upah'] = array('0');
        }

        if ($d_lain2->count()>0) {
            $arr['lain2']=$d_lain2->get();
            $arr['status_lain2'] = array('1');
        }else{
            $arr['status_lain2'] = array('1');
        }

        if($d_upah_bayar->count()>0){
            $arr['upah_bayar'] = $d_upah_bayar->get();
        }else{
            $arr['upah_bayar'] = '';
        }


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

    function getPick($id_rekanan, $id_motif, $value_pick, $param)
    {
        $rekanan = DB::table('ms_rekanan')->where('id_rekanan', $id_rekanan)->first();
        $maping_tenun = DB::table('ms_mapingtenun')->where('id_kelompok', $rekanan->id_kelompok)->get();

        if ($maping_tenun == null) {
            if ($param == 'value') {
                return null;
            } else if ($param == 'jenis') {
                return null;
            }
        } else {
            foreach ($maping_tenun as $key => $maping) {
                $pick = DB::table('ms_pick as p')
                        ->join('ms_pick_barang as pb', 'pb.id_pick', '=','p.id_pick')
                        ->where('p.id_pick', $maping->id_pick)
                        ->where('p.id_motif', $id_motif)
                        ->get();
                foreach ($pick as $key => $value) {
                    if ($value_pick >= $value->pick_bawah && $value_pick <= $value->pick_atas) {
                        if ($param == 'value') {
                            return $value->upah;
                        } else if ($param == 'jenis') {
                            return $value->jenis_pick;
                        }
                    }
                }
            }
        }
    }

    function getIdUpahTenun($id_rekanan)
    {
        $rekanan = DB::table('ms_rekanan')->where('id_rekanan', $id_rekanan)->first();
        $maping_tenun = DB::table('ms_mapingtenun')->where('id_kelompok', $rekanan->id_kelompok)->select('id_upahtenun')->get();
        $arr_upahtenun = array_reduce($maping_tenun->toArray(), function($carry, $item){
            $carry[] = $item->id_upahtenun;
            return $carry;
        });
        return $arr_upahtenun;
    }

    public function get_rekanan2(Request $request)
    {
        $f                   = $request->get('f');
        $lap_hasil           = $request->get('lap_hasil');
        $id_barangmasuk      = $request->get('id_barangmasuk');
        $id_barangkeluar     = $request->get('id_barangkeluar');
        $id_workorder        = $request->get('id_workorder');
        $id_workorder_barang = $request->get('id_workorder_barang');
        $id_proses           = $request->get('id_proses');
        $id_rekanan          = $request->get('id_rekanan');
        $id_pegawai          = $request->get('id_pegawai');
        $id_upah_borongan    = $request->get('id_upah_borongan');
        $id_jenis            = $request->get('id_jenis');
        $tanggal_awal        = $request->get('tanggal_awal');
        $tanggal_akhir       = $request->get('tanggal_akhir');

        if($id_workorder != '' && $id_workorder != 0){
            $order = 'pb.id_workorder';
            $where_order = $id_workorder;
        }else{
            $order = 'pb.id_workorder_barang';
            $where_order = $id_workorder_barang;
        }

        if($id_rekanan != '' && $id_rekanan != 0){
            $pemroses = 'pbk.id_rekanan';
            $where_pemroses = $id_rekanan;
        }else{
            $pemroses = 'pbk.id_pegawai';
            $where_pemroses = 'pbk.id_pegawai';
        }

        // DB::enableQueryLog();
        /*
            upah_satuan => upah pegawai / rekanan
            upah_satuan_2 => upah makelar / kelompok rekanan
            potongan_upah_kualitas => potongan upah berdasarkan kualitas hasil
        */
        $d_hasil = DB::table("view_edit_masuk AS pb")
            ->join('pro_barangmasuk AS pbm', 'pb.id_barangmasuk', 'pbm.id_barangmasuk')
            ->join('pro_barangkeluar as pbk','pbm.id_barangkeluar','pbk.id_barangkeluar')
            // ->where('pb.id_barangmasuk', $id_barangmasuk)
            ->where('pbm.id_barangkeluar', $id_barangkeluar)
            ->where('pbk.id_rekanan',$id_rekanan)
            ->where($order,$where_order)
            ->where('pbk.id_proses',$id_proses);

        if($f=="edit"){
            $d_hasil->join("pro_upahproduksi AS up", function($join){
                $join->on("up.id_barangmasuk", "=", "pbm.id_barangmasuk");
                $join->on("up.id_barangkeluar", "=", "pbm.id_barangkeluar");
            });
            $d_hasil->join("pro_upah_total AS upt", function($join){
                $join->on("upt.id_upah_borongan", "=", "up.id_upahproduksi");
                $join->on("upt.id_pbb", "=",  "pb.id");
            });
            $d_hasil->selectRaw('pb.*,pbk.id_proses, 
                upt.upah_total AS upah, 
                upt.upah_satuan AS upah_satuan,
                upt.upah_satuan_2 AS upah_satuan_2, 
                upt.potongan_upah_kualitas AS potongan_upah_kualitas,
                upt.upah_pick AS upah_pick');
        }else{
            $d_hasil->selectRaw('pb.*,pbk.id_proses, 
                0 AS upah, 
                0 AS upah_satuan,
                0 AS upah_satuan_2, 
                0 AS potongan_upah_kualitas,
                0 AS upah_pick');
        }

        if ($lap_hasil == "" || $lap_hasil == null) {
            // $d_hasil->where('pb.id_barangmasuk', $id_barangmasuk);
            
            $arr['status_hasil'] = array('0');
            $arr['status_upah'] = array('0');
            $arr['status_lain2'] = array('0');
            return response()->json($arr);

        } else {
            array_push($lap_hasil, $id_barangmasuk);
            $d_hasil->whereIn('pb.id_barangmasuk', $lap_hasil);
        }
        $d_hasil2 = $d_hasil->get();
        // dd($d_hasil2);
        // dd(DB::getQueryLog());
        $d_lain2 = DB::table('pro_upah_borongan_lain_lain')->where('id_upah_borongan', '=', $id_upah_borongan);
        $d_upah_bayar=DB::table('pro_upah_total')->where('id_upah_borongan','=',$id_upah_borongan);
        
        if($id_jenis == '1'){
            $prosesTenun = 37;
            if($id_proses == $prosesTenun){
                if($f=="add"){
                    // data master upah tenun
                    //$rekanan = DB::table('ms_rekanan')->where('id_rekanan', $id_rekanan)->first();
                    //$maping_tenun = DB::table('ms_mapingtenun')->where('id_kelompok', $rekanan->id_kelompok)->first();

                    $id_upah_tenun = $this->getIdUpahTenun($id_rekanan);
                    $upahTenun = DB::table("ms_upahtenun_baru")->whereIn('id', $id_upah_tenun)->get();
                    
                    // $upahTenun = DB::table("ms_upahtenun_baru")->get();
                    $arr_upahtenun = array_reduce($upahTenun->toArray(), function($carry, $item){
                        $carry[ $item->id_motif ] = $item;
                        return $carry;
                    });

                    // $pick = DB::table('ms_pick as p')
                    //         ->join('ms_pick_barang as pb', 'pb.id_pick', '=','p.id_pick')
                    //         ->where('p.id_pick', $maping_tenun->id_pick)
                    //         ->get();
                    // $arr_pick = array_reduce($pick->toArray(), function($carry, $item){
                    //     $carry[] = $item;
                    //     return $carry;
                    // });
                    // set upah tenun ke data hasil

                    foreach ($d_hasil2 as $key => $value) {
                        if(isset($arr_upahtenun[$value->id_motif])){
                            $arr_upah = $arr_upahtenun[$value->id_motif];
                            $d_hasil2[$key]->jenis_pick = $this->getPick($id_rekanan, $value->id_motif, $value->pick, 'jenis') == null ? "D" : $this->getPick($id_rekanan, $value->id_motif, $value->pick, 'jenis');
                            $d_hasil2[$key]->upah_pick = $this->getPick($id_rekanan, $value->id_motif, $value->pick, 'value') == null ? 0 : $this->getPick($id_rekanan, $value->id_motif, $value->pick, 'value');
                            $d_hasil2[$key]->upah_satuan = $arr_upah->nominal_rekanan;
                            $d_hasil2[$key]->upah_satuan_2 = $arr_upah->nominal_rekanan_kelompok;
                            if($value->nama_kualitas != ""){
                                $nm_kualitas = strtolower($value->nama_kualitas);
                                if(isset($arr_upah->{'potongan_'.$nm_kualitas})){
                                    $d_hasil2[$key]->potongan_upah_kualitas = $arr_upah->{'potongan_'.$nm_kualitas};

                                }
                            }
                        }
                    }
                } else {
                    $id_upah_tenun = $this->getIdUpahTenun($id_rekanan);
                    $upahTenun = DB::table("ms_upahtenun_baru")->whereIn('id', $id_upah_tenun)->get();

                    $arr_upahtenun = array_reduce($upahTenun->toArray(), function($carry, $item){
                        $carry[ $item->id_motif ] = $item;
                        return $carry;
                    });

                    foreach ($d_hasil2 as $key => $value) {
                        if(isset($arr_upahtenun[$value->id_motif])){
                            $arr_upah = $arr_upahtenun[$value->id_motif];
                            $d_hasil2[$key]->jenis_pick = $this->getPick($id_rekanan, $value->id_motif, $value->pick, 'jenis') == null ? "D" : $this->getPick($id_rekanan, $value->id_motif, $value->pick, 'jenis');
                            $d_hasil2[$key]->upah_pick = $this->getPick($id_rekanan, $value->id_motif, $value->pick, 'value') == null ? 0 : $this->getPick($id_rekanan, $value->id_motif, $value->pick, 'value');
                            $d_hasil2[$key]->upah_satuan = $arr_upah->nominal_rekanan;
                            $d_hasil2[$key]->upah_satuan_2 = $arr_upah->nominal_rekanan_kelompok;
                            if($value->nama_kualitas != ""){
                                $nm_kualitas = strtolower($value->nama_kualitas);
                                if(isset($arr_upah->{'potongan_'.$nm_kualitas})){
                                    $d_hasil2[$key]->potongan_upah_kualitas = $arr_upah->{'potongan_'.$nm_kualitas};

                                }
                            }
                        }
                    }
                }
                $d_upah = DB::table("ms_upahtenun_baru");

            }else{
                $d_upah =  DB::table('ms_upah_borongan as mub')->join('ms_proses as mp', 'mp.id_proses', 'mub.id_proses')->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')->select(DB::raw('mub.*, mp.nama_proses, 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'))->where('mub.id_proses',$id_proses);

            }
            $status_upah = '1';
        }else{
           $d_upah =  DB::table('ms_upah_pegawai as mup')->where('mup.id_pegawai',$id_pegawai);
           $status_upah = '2';
        }
        // dd($d_hasil2);
        if(sizeof($d_hasil2) > 0){
            $arr['hasil'] = $d_hasil2;
            $arr['status_hasil'] = array('1');
        }else{
            $arr['status_hasil'] = array('0');
        }

        if($d_upah->count() > 0){
            $arr['upah'] = $d_upah->get();
            $arr['s_upah'] = $status_upah;
            $arr['status_upah'] = array('1');
        }else{
            $arr['status_upah'] = array('0');
        }

        if ($d_lain2->count()>0) {
            $arr['lain2']=$d_lain2->get();
            $arr['status_lain2'] = array('1');
        }else{
            $arr['status_lain2'] = array('0');
        }

        if($d_upah_bayar->count()>0){
            $arr['upah_bayar'] = $d_upah_bayar->get();
        }else{
            $arr['upah_bayar'] = '';
        }


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

    function getPegawaiLapHasil(Request $request)
    {
        $lap_hasil           = $request->get('lap_hasil');
        $id_upah_borongan    = $request->get('id_upah_borongan');
        $id_barangmasuk      = $request->get('id_barangmasuk');
        $id_barangkeluar     = $request->get('id_barangkeluar');
        $id_workorder        = $request->get('id_workorder');
        $id_workorder_barang = $request->get('id_workorder_barang');
        $id_proses           = $request->get('id_proses');
        $id_rekanan          = $request->get('id_rekanan');
        $id_pegawai          = $request->get('id_pegawai');
        $id_jenis            = $request->get('id_jenis');
        $tanggal_awal        = $request->get('tanggal_awal');
        $tanggal_akhir       = $request->get('tanggal_akhir');
        array_push($lap_hasil, $id_barangkeluar);

        if($id_workorder != '' || $id_workorder != 0){
            $order = 'id_workorder';
            $where_order = $id_workorder;
        }else{
            $order = 'id_workorder_barang';
            $where_order = $id_workorder_barang;
        }

        $d_hasil = DB::table('pro_barangmasuk AS pb')
                    ->join('pro_barangkeluar as pbk','pb.id_barangkeluar','pbk.id_barangkeluar')
                    ->join('pro_barangmasuk_barang as pbb', 'pb.id_barangmasuk', 'pbb.id_barangmasuk')
                    ->join('ms_gudang as mg', 'pbb.id_gudang', 'mg.id_gudang')
                    ->join('ms_barang as mb', 'pbb.id_barang', 'mb.id')
                    ->leftjoin('ms_warnakomposisi as mwk', 'pbb.id_komposisi', 'mwk.id_komposisi')
                    ->join('ms_satuan AS ms', 'pbb.id_satuan', 'ms.id_satuan')
                    ->leftJoin('ms_motif_barang as mmb', 'pbb.id_motif_barang', 'mmb.id_motif_barang')
                    ->leftJoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')
                    ->leftJoin('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')
                    ->leftJoin('ms_barang as mb2', 'pbb.id_label', 'mb2.id')
                    ->leftJoin('ms_barang as mb3', 'pbb.id_kemasan', 'mb3.id')
                    ->leftJoin('ms_barang as mb4', 'pbb.id_kemasansatuan', 'mb4.id')
                    ->leftJoin('ms_barang as mb5', 'pbb.id_kemasansepuluh', 'mb5.id')
                    // ->leftJoin('ms_upah_borongan AS mub',DB::raw('mub.id_proses=pbk.id_proses AND CASE WHEN pbk.id_proses'),DB::raw('23 THEN mub.id_barang =  mb.id ELSE mub.id_motif_barang = mmb.id_motif_barang END'))
                    ->leftJoin('ms_barang as mb6', 'pbb.id_kemasanseratus', 'mb6.id')
                    ->select(DB::raw('pbb.*, pbk.id_proses , mb.kode_barang, mb.nama_barang, mg.id_gudang, mg.nama_gudang, pbb.id_satuan, ms.nama_satuan, 0 AS upah, CASE WHEN mwk.id_komposisi IS NULL THEN "" ELSE  mwk.id_komposisi END AS id_komposisi, CASE WHEN mwk.nama IS NULL THEN "" ELSE  mwk.nama END AS nama_komposisi, CASE WHEN mmb.id_motif_barang IS NULL THEN "" ELSE mmb.id_motif_barang END AS id_motif_barang, CASE WHEN mmb.motif_keterangan IS NULL THEN "" ELSE mmb.motif_keterangan END AS motif_keterangan, CASE WHEN mm.id_motif IS NULL THEN "" ELSE mm.id_motif END AS id_motif, CASE WHEN mm.nama_motif IS NULL THEN "" ELSE mm.nama_motif END AS nama_motif, CASE WHEN mk.id_kualitas IS NULL THEN "" ELSE mk.id_kualitas END AS id_kualitas, CASE WHEN mk.nama_kualitas IS NULL THEN "" ELSE mk.nama_kualitas END AS nama_kualitas, CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" ELSE pbb.id_kualitas_dudul END AS id_kualitas_dudul, CASE WHEN pbb.id_kualitas_dudul = 0 THEN "" WHEN pbb.id_kualitas_dudul = 1 THEN "Baik" ELSE "Jelek" END AS kualitas_dudul, CASE WHEN pbb.id_label = 0 THEN "" ELSE pbb.id_label END AS id_label, CASE WHEN mb2.nama_barang IS NULL THEN "" ELSE mb2.nama_barang END AS nama_label, CASE WHEN pbb.id_kemasan = 0 THEN "" ELSE pbb.id_kemasan END AS id_kemasan, CASE WHEN mb3.nama_barang IS NULL THEN "" ELSE mb3.nama_barang END AS nama_kemasan, CASE WHEN pbb.id_kemasansatuan = 0 THEN "" ELSE pbb.id_kemasansatuan END AS id_kemasansatuan, CASE WHEN mb4.nama_barang IS NULL THEN "" ELSE mb4.nama_barang END AS nama_kemasansatuan, CASE WHEN pbb.id_kemasansepuluh = 0 THEN "" ELSE pbb.id_kemasansepuluh END AS id_kemasansepuluh, CASE WHEN mb5.nama_barang IS NULL THEN "" ELSE mb5.nama_barang END AS nama_kemasansepuluh, CASE WHEN pbb.id_kemasanseratus = 0 THEN "" ELSE pbb.id_kemasanseratus END AS id_kemasanseratus, CASE WHEN mb6.nama_barang IS NULL THEN "" ELSE mb6.nama_barang END AS nama_kemasanseratus'))
                    ->where('pb.id_barangmasuk', $id_barangmasuk)
                    ->whereIn('pb.id_barangkeluar', $lap_hasil)
                    ->where('pbk.id_pegawai',$id_pegawai)
                    ->where($order,$where_order)
                    ->where('pbk.id_proses',$id_proses);
        $d_lain2      = DB::table('pro_upah_borongan_lain_lain')
                        ->where('id_upah_borongan', '=', $id_upah_borongan);
        $d_upah_bayar = DB::table('pro_upah_total')
                        ->where('id_upah_borongan','=',$id_upah_borongan);

        if($id_jenis == '1'){
            $d_upah      =  DB::table('ms_upah_borongan as mub')
                            ->join('ms_proses as mp', 'mp.id_proses', 'mub.id_proses')
                            ->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')
                            ->select(DB::raw('mub.*, mp.nama_proses, 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'))
                            ->where('mub.id_proses',$id_proses);
            $status_upah = '1';    
        }else{    
            $d_upah =  DB::table('ms_upah_pegawai as mup')->where('mup.id_pegawai',$id_pegawai);
            $status_upah = '2';        
        }

        if($d_hasil->count() > 0){
            $arr['hasil'] = $d_hasil->get();
            $arr['status_hasil'] = array('1');
        }else{
            $arr['status_hasil'] = array('0');
        }

        if($d_upah->count() > 0){
            $arr['upah'] = $d_upah->get();
            $arr['s_upah'] = $status_upah;
            $arr['status_upah'] = array('1');
        }else{
            $arr['status_upah'] = array('0');
        }

        if ($d_lain2->count()>0) {
            $arr['lain2']=$d_lain2->get();
            $arr['status_lain2'] = array('1');
        }else{
            $arr['status_lain2'] = array('0');
            $arr['lain2']='';
        }

        if($d_upah_bayar->count()>0){
            $arr['upah_bayar'] = $d_upah_bayar->get();
        }else{
            $arr['upah_bayar'] = '';
        }

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

    function get_pegawai(Request $request){
        $id_upah_borongan    = $request->get('id_upah_borongan');
        $id_barangmasuk      = $request->get('id_barangmasuk');
        $id_barangkeluar     = $request->get('id_barangkeluar');
        $id_workorder        = $request->get('id_workorder');
        $id_workorder_barang = $request->get('id_workorder_barang');
        $id_proses           = $request->get('id_proses');
        $id_rekanan          = $request->get('id_rekanan');
        $id_pegawai          = $request->get('id_pegawai');
        $id_jenis            = $request->get('id_jenis');
        // dd($lap_hasil);
        if($id_workorder != '' && $id_workorder != 0){
            $order = 'pbb.id_workorder';
            $where_order = $id_workorder;
        }

        if($id_workorder_barang != '' && $id_workorder_barang != 0){
            $order = 'pbb.id_workorder_barang';
            $where_order = $id_workorder_barang;
        }

        // DB::enableQueryLog();
        $d_hasil = DB::table('pro_barangmasuk AS pb')
        ->join('pro_barangkeluar as pbk','pb.id_barangkeluar','pbk.id_barangkeluar')
        ->join('view_masuk_all as pbb', 'pb.id_barangmasuk', 'pbb.id')
        ->join('pro_barangmasuk_barang as pbbg', 'pbb.id_detail', 'pbbg.id')
        ->leftjoin('ms_proses as mp', 'pbbg.id_jahit', 'mp.id_proses')
        ->select(DB::raw('pbb.*, pbk.id_proses, 0 AS upah, jum_barang AS jumlah, pbbg.id_jahit, CASE WHEN mp.nama_proses IS NULL THEN "" ELSE mp.nama_proses END AS nama_proses_, pbk.id_lokasi'))
        ->where('pb.id_barangmasuk', $id_barangmasuk)
        ->where('pb.id_barangkeluar', $id_barangkeluar)
        ->where('pbk.id_pegawai',$id_pegawai)
        ->where('pbk.id_proses',$id_proses);

        if(($id_workorder != '' && $id_workorder != 0) || ($id_workorder_barang != '' && $id_workorder_barang != 0)){
            $d_hasil->where($order,$where_order);
        }

        $d_upah_harian = DB::table('pro_upahproduksi_harian AS h')
                        ->selectRaw('h.*, k.nama_upahket AS nama_keterangan, s.nama_satuan')
                        ->join('ms_upahket AS k', 'k.id_upahket', 'h.keterangan')
                        ->join('ms_satuan AS s', 's.id_satuan', 'h.satuan_waktu')
                        ->where('id_upahproduksi', '=', $id_upah_borongan);
        $d_lain2 = DB::table('pro_upah_borongan_lain_lain')->where('id_upah_borongan', '=', $id_upah_borongan);
        $d_upah_bayar=DB::table('pro_upah_total')->where('id_upah_borongan','=',$id_upah_borongan);

        if($id_jenis == '1'){ // data master upah borongan
            $d_upah =  DB::table('ms_upah_borongan as mub')->join('ms_proses as mp', 'mp.id_proses', 'mub.id_proses')->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')->select(DB::raw('mub.*, mp.nama_proses, 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'));
            if($id_proses == 45){
                $d_upah->whereIn('mub.id_proses', [66, 69, 88, 89, 90]);    
            }else{
                $d_upah->where('mub.id_proses',$id_proses);
            }
            $status_upah = '1';    
        }else{ // data master upah pegawai
            $d_upah =  DB::table('ms_upah_pegawai as mup')->where('mup.id_pegawai',$id_pegawai);
            $status_upah = '2';        
        }

        // daftar hasil setor
        if($d_hasil->count() > 0){
            $arr['hasil'] = $d_hasil->get();
            $arr['status_hasil'] = array('1');
        }else{
            $arr['status_hasil'] = array('0');
        }

        // master upah borongan
        if($d_upah->count() > 0){
            $arr['upah'] = $d_upah->get();
            $arr['s_upah'] = $status_upah;
            $arr['status_upah'] = array('1');
        }else{
            $arr['status_upah'] = array('0');
        }

        if ($d_upah_harian->count()>0) {
            $arr['upah_harian']=$d_upah_harian->get();
            $arr['status_upah_harian'] = array('1');
        }else{
            $arr['upah_harian']='';
            $arr['status_upah_harian'] = array('0');
        }

        if ($d_lain2->count()>0) {
            $arr['lain2']=$d_lain2->get();
            $arr['status_lain2'] = array('1');
        }else{
            $arr['status_lain2'] = array('0');
            $arr['lain2']='';
        }

        // upah borongan yg telah dibayar
        if($d_upah_bayar->count()>0){
            $arr['upah_bayar'] = $d_upah_bayar->get();
        }else{
            $arr['upah_bayar'] = '';
        }

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

    function get_proses(Request $request){
        $rekanan = $request->get('rekanan');
        $pegawai = $request->get('pegawai');
        // DB::enableQueryLog();

        if($rekanan != '' || $rekanan != 0){
            $d_data = DB::table('pro_barangmasuk as pbm')->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')->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('pbm.id_barangmasuk, pbm.noAuto, pbm.noAuto_ext, pb.*, pob.no_workorder as no_workorder_barang, pw.nomor as no_workorder, mp.nama_proses, mr.nama as nama_rekanan, mpg.nama_pegawai'))->where('pbm.status','=','1')->where('pb.id_rekanan','=',$rekanan);        
            if($d_data->count() > 0){
                $arr['data'] = $d_data->get();
                $arr['status'] = array('1');
            }else{
                $arr['status'] = array('0');
            }
        }
        // dd(DB::getQueryLog());

        if($pegawai != '' || $pegawai != 0){
            // DB::enableQueryLog();
            $d_data = DB::table('pro_barangmasuk as pbm')->join('pro_barangkeluar as pb','pbm.id_barangkeluar','pb.id_barangkeluar')->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('pbm.id_barangmasuk, pbm.noAuto AS masukNoAuto, pbm.noAuto_ext AS masukNoAuto_ext, pb.*, pob.no_workorder as no_workorder_barang, pw.nomor as no_workorder, mp.nama_proses, mr.nama, mpg.nama_pegawai'))->where('pbm.status','=','1')->where('pb.id_pegawai','=',$pegawai);
            if($d_data->count() > 0){
                $arr['data'] = $d_data->get();
                $arr['status'] = array('1');
            }else{
                $arr['status'] = array('0');
            }
            // dd(DB::getQueryLog());
        }

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

    function simpanRekanan(Request $request){
        $id_upahproduksi = $request->get('id_upahproduksi');
        $id_pegawai = '';
        $id_rekanan = '';
        $id_workorder = '';
        $id_workorder_barang = '';
        if($request->get('pegawai') != "" || $request->get('pegawai') != 0){
            $id_pegawai = $request->get('pegawai');
        }
        if($request->get('rekanan') != "" || $request->get('rekanan') != 0){
            $id_rekanan = $request->get('rekanan');
        }
        if($request->get('spk') != "" || $request->get('spk') != 0){
            $id_workorder = $request->get('spk');
        }
        if($request->get('workorder') != "" || $request->get('workorder') != 0){
            $id_workorder_barang = $request->get('workorder');
        }
        $data['id_barangmasuk'] = $request->get('proses');
        $data['id_barangkeluar'] = $request->get('id_barangkeluar');
        $data['id_rekanan'] = $id_rekanan;
        $data['id_pegawai'] = $id_pegawai;
        $data['tanggal'] = tgl_full($request->get('tanggal'),'2');
        $data['nomor'] = $request->get('nomor');
        if (is_array($request->get('lap_hasil'))) {
            $data['lap_hasil'] = implode(',',$request->get('lap_hasil'));
        } else {
            $data['lap_hasil'] = ($request->get('lap_hasil') == "") ? "" : $request->get('lap_hasil');
        }
        $data['id_proses'] = $request->get('id_proses');
        $data['id_workorder'] = $id_workorder;
        $data['id_workorder_barang'] = $id_workorder_barang;
        $data['id_jenis'] = $request->get('jenis');
        $data['id_lokasi'] = $request->get('id_jenis');

        // $data['termasuk_pajak'] = $request->get('termasuk_pajak') ? $request->get('termasuk_pajak') : '0';
        $data['pajak'] = $request->get('pajak');
        // $data['nominal_ongkir'] = $request->get('td_ongkir');

        $data['id_akun_upah'] = $request->get('id_akun_upah');
        $data['tanggal'] = tgl_full($request->get('tanggal'),'2');
        $data['nomor'] = $request->get('nomor');

        // table_barang - upah harian
        $tabel_id = $request->get('tabel_id');
        $tabel_ket = $request->get('tabel_ket');
        $tabel_perkiraan = $request->get('tabel_perkiraan');
        $tabel_satuan = $request->get('tabel_satuan');
        $tabel_satuanwaktu = $request->get('tabel_satuanwaktu');
        $tabel_upah = $request->get('tabel_upah');
        $tabel_total = $request->get('tabel_total');
        // end : table_barang - upah harian

        // tabel_lain_lain
        $tabel_id2= $request->get('tabel_id2');
        $table_jenis=$request->get('tabel_jenis');
        $table_jumlah=$request->get('tabel_upahll');
        // end : tabel_lain_lain

        // tabel_hasil
        $id_pbb= $request->get('id_pbb_');
        $id_proses__ = $request->get('id_proses_');
        $id_motif_barang__ = $request->get('id_motif_barang_');
        $id_motif__ = $request->get('id_motif_');
        $id_barang__ = $request->get('id_barang_');
        $jumlah__ = $request->get('jumlah_barang_');
        $upah_satuan__= $request->get('upah_satuan_');
        $total_upah__ = $request->get('upah_total_');
        // end : tabel_hasil

        // echo '<pre>';print_r($harga);exit();

        DB::beginTransaction();
        try{
            if($id_upahproduksi == ''){
                $id = DB::table('pro_upahproduksi')->insertGetId($data);
                if ($tabel_id != null) {
                    for($i=0; $i < count($tabel_id); $i++){
                        $upah['id_upahproduksi'] = $id;
                        $upah['id_perkiraan'] = $tabel_perkiraan[$i];
                        $upah['keterangan'] = $tabel_ket[$i];
                        $upah['satuan'] = $tabel_satuan[$i];
                        $upah['satuan_waktu'] = $tabel_satuanwaktu[$i];
                        $upah['upah'] = $tabel_upah[$i];

                        DB::table('pro_upahproduksi_barang')->insert($upah);
                    }
                }
                
                if ($tabel_id2 != null) {
                    for($i=0;$i<count($tabel_id2);$i++){
                        $lain['id_upah_borongan']=$id;
                        $lain['jenis'] = $table_jenis[$i];
                        $lain['jumlah'] = $table_jumlah[$i];

                        DB::table('pro_upah_borongan_lain_lain')->insert($lain);
                    }
                }
                
                for($i=0;$i < count($id_proses__);$i++){
                    $harga['id_upah_borongan']  = $id;
                    $harga['id_pbb']            = $id_pbb[$i];
                    $harga['id_proses']         = $id_proses__[$i];
                    $harga['id_motif_barang']   = $id_motif_barang__[$i];
                    $harga['id_motif']          = $id_motif__[$i];
                    $harga['id_barang']         = $id_barang__[$i];
                    $harga['jumlah_barang']     = $jumlah__[$i];
                    $harga['upah_satuan']       = $upah_satuan__[$i];
                    $harga['upah_total']        = $total_upah__[$i];

                    DB::table('pro_upah_total')->insert($harga);
                }
            }else{
                DB::table('pro_upahproduksi')->where('id_upahproduksi', $id_upahproduksi)->update($data);
                $id_del = array();
                $id_del_lain=array();
                $id_store = array();
                $id_store_lain2=array();
                $d_barang = DB::table('pro_upahproduksi_barang')->where('id_upahproduksi', '=', $id_upahproduksi);
                $d_lain22 = DB::table('pro_upah_borongan_lain_lain')->where('id_upah_borongan','=',$id_upahproduksi);
                if ($d_barang->count() > 0) {
                    foreach($d_barang->get() as $d){
                        $id_store[] = $d->id_upahproduksi_barang;
                    }

                    foreach($id_store as $d){
                        if(count($tabel_id) > 0){
                            if(!in_array($d, $tabel_id)){
                                $id_del[] = $d;
                            }
                        }else{
                            $id_del[] = $d;
                        }
                    }
                }
                
                if ($d_lain22->count() > 0) {
                    foreach ($d_lain22->get() as $d) {
                        $id_store_lain2[]=$d->id_upah;
                    }
                    
                    foreach ($id_store_lain2 as $d) {
                        if ($tabel_id2 != null) {
                            if (count($tabel_id2)>0) {
                                if(!in_array($d, $tabel_id2)){
                                    $id_del_lain[]=$d;
                                }
                            }
                        }else{
                            $id_del_lain[]=$d;
                        }
                    }
                }
                
                if ($tabel_id != null) {
                    for($i = 0; $i < count($tabel_id); $i++){
                        $upah['id_upahproduksi'] = $id_upahproduksi;
                        $upah['id_perkiraan'] = $tabel_perkiraan[$i];
                        $upah['keterangan'] = $tabel_ket[$i];
                        $upah['satuan'] = $tabel_satuan[$i];
                        $upah['satuan_waktu'] = $tabel_satuanwaktu[$i];
                        $upah['upah'] = $tabel_upah[$i];

                        if($tabel_id[$i] == ''){
                            DB::table('pro_upahproduksi_barang')->insert($upah);
                        }else if($tabel_id != ''){
                            DB::table('pro_upahproduksi_barang')->where(array('id_upahproduksi_barang' => $tabel_id[$i]))->update($upah);
                        }
                    }
                }
                
                if ($tabel_id2 != null) {
                    for ($i=0; $i < count($tabel_id2); $i++) { 
                        $lain['id_upah_borongan']=$id_upahproduksi;
                        $lain['jenis'] = $table_jenis[$i];
                        $lain['jumlah'] = $table_jumlah[$i];
                        if($tabel_id2[$i]==''){
                            DB::table('pro_upah_borongan_lain_lain')->insert($lain);
                        }else if($tabel_id2 != ''){
                            DB::table('pro_upah_borongan_lain_lain')->where(array('id_upah' => $tabel_id2[$i]))->update($lain);
                        }
                    }
                }
                
                
                if(count($id_del) > 0){
                    DB::table('pro_upahproduksi_barang')->whereIn('id_upahproduksi_barang', $id_del)->delete();
                }
                if(count($id_del_lain)>0){
                    DB::table('pro_upah_borongan_lain_lain')->whereIn('id_upah',$id_del_lain)->delete();
                }
            }
            DB::commit();
            return redirect('upahproduksi');
        }catch (Exception $e){
            return redirect()->back();
        }
    }

    function simpan(Request $request){
        // dd($request->all());
        $id_upahproduksi = $request->get('id_upahproduksi');
        $id_pegawai = '';
        $id_rekanan = '';
        $id_workorder = '';
        $id_workorder_barang = '';
        if($request->get('pegawai') != "" || $request->get('pegawai') != 0){
            $id_pegawai = $request->get('pegawai');
        }
        if($request->get('rekanan') != "" || $request->get('rekanan') != 0){
            $id_rekanan = $request->get('rekanan');
        }
        if($request->get('spk') != "" || $request->get('spk') != 0){
            $id_workorder = $request->get('spk');
        }
        if($request->get('workorder') != "" || $request->get('workorder') != 0){
            $id_workorder_barang = $request->get('workorder');
        }
        $data['id_barangmasuk'] = $request->get('proses');
        $data['id_barangkeluar'] = $request->get('id_barangkeluar');
        $data['id_rekanan'] = $id_rekanan;
        $data['id_pegawai'] = $id_pegawai;
        $data['id_proses'] = $request->get('id_proses');
        $data['id_workorder'] = $id_workorder;
        $data['id_workorder_barang'] = $id_workorder_barang;
        $data['id_jenis'] = $request->get('jenis');
        $data['id_lokasi'] = $request->get('id_jenis');

        $data['id_akun_upah'] = $request->get('id_akun_upah');
        $data['tanggal'] = tgl_full($request->get('tanggal'),'2');
        $data['nomor'] = $request->get('nomor');

        // table_barang - upah harian
        $tabel_id = $request->get('tabel_id');
        $tabel_ket = $request->get('tabel_ket');
        $tabel_perkiraan = $request->get('tabel_perkiraan');
        $tabel_satuan = $request->get('tabel_satuan');
        $tabel_satuanwaktu = $request->get('tabel_satuanwaktu');
        $tabel_upah = $request->get('tabel_upah');
        $tabel_total = $request->get('tabel_total');
        // end : table_barang - upah harian

        // tabel_lain_lain
        $tabel_id2= $request->get('tabel_id2');
        $table_jenis=$request->get('tabel_jenis');
        $table_jenis_transaksi=$request->get('tabel_jenis_transaksi');
        $table_jumlah=$request->get('tabel_upahll');
        // end : tabel_lain_lain

        // tabel_hasil
        $id_pbb= $request->get('id_pbb_');
        $id_proses__ = $request->get('id_proses_');
        $id_motif_barang__ = $request->get('id_motif_barang_');
        $id_motif__ = $request->get('id_motif_');
        $id_barang__ = $request->get('id_barang_');
        
        $id_gudang__ = $request->get('id_gudang_');
        $id_satuan__ = $request->get('id_satuan_');
        $id_komposisi__ = $request->get('id_komposisi_');
        $id_kualitas__ = $request->get('id_kualitas_');
        $id_kualitas_dudul__ = $request->get('id_kualitas_dudul_');
        $id_kualitas_proses__ = $request->get('id_kualitas_proses_');
        $id_kualitas_periksa__ = $request->get('id_kualitas_periksa_');
        $id_label__ = $request->get('id_label_');
        $id_cap__ = $request->get('id_cap_');
        $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_plat__ = $request->get('id_plat_');

        $jumlah__ = $request->get('jumlah_barang_');
        $upah_satuan__= $request->get('upah_satuan_');
        $total_upah__ = $request->get('upah_total_');
        // end : tabel_hasil

        
        // echo '<pre>';print_r($harga);exit();
        
        if($id_upahproduksi == ''){ // simpan utk data upah produksi baru
            DB::beginTransaction();
            try {
                $id = DB::table('pro_upahproduksi')->insertGetId($data);
                if($data['id_jenis']==1){ // upah borongan
                    for($i=0;$i < count($id_proses__);$i++){
                        $harga['id_upah_borongan']  = $id;
                        $harga['id_pbb']            = $id_pbb[$i];
                        $harga['id_proses']         = $id_proses__[$i];
                        $harga['id_motif_barang']   = $id_motif_barang__[$i];
                        $harga['id_motif']          = $id_motif__[$i];
                        $harga['id_barang']         = $id_barang__[$i];
                        /* === */
                        $harga['id_gudang'] = $id_gudang__[$i];
                        $harga['id_satuan'] = $id_satuan__[$i];
                        $harga['id_komposisi'] = $id_komposisi__[$i];
                        $harga['id_kualitas'] = $id_kualitas__[$i];
                        $harga['id_kualitas_dudul'] = $id_kualitas_dudul__[$i];
                        $harga['id_kualitas_proses'] = $id_kualitas_proses__[$i];
                        $harga['id_kualitas_periksa'] = $id_kualitas_periksa__[$i];
                        $harga['id_label'] = $id_label__[$i];
                        $harga['id_cap'] = $id_cap__[$i];
                        $harga['id_kemasan'] = $id_kemasan__[$i];
                        $harga['id_kemasansatuan'] = $id_kemasansatuan__[$i];
                        $harga['id_kemasansepuluh'] = $id_kemasansepuluh__[$i];
                        $harga['id_kemasanseratus'] = $id_kemasanseratus__[$i];
                        $harga['id_plat'] = $id_plat__[$i];
                        /* === */
                        $harga['jumlah_barang']     = $jumlah__[$i];
                        $harga['upah_satuan']       = $upah_satuan__[$i];
                        $harga['upah_satuan_2']         = 0;
                        $harga['potongan_upah_kualitas']= 0;
                        $harga['upah_total']        = $total_upah__[$i];

                        DB::table('pro_upah_total')->insert($harga);
                    }

                }else if($data['id_jenis']==2){ // upah harian
                    for($i=0; $i < count($tabel_id); $i++){
                        $upah['id_upahproduksi'] = $id;
                        $upah['id_perkiraan'] = $data['id_akun_upah'];
                        $upah['keterangan'] = $tabel_ket[$i];
                        $upah['satuan'] = $tabel_satuan[$i];
                        $upah['satuan_waktu'] = $tabel_satuanwaktu[$i];
                        $upah['upah'] = $tabel_upah[$i];

                        DB::table('pro_upahproduksi_harian')->insert($upah);
                    }

                }
                for($i=0;$i<count($tabel_id2);$i++){ // upah lain-lain
                    $lain['id_upah_borongan']=$id;
                    $lain['id_akun_upah_lain']=$data['id_akun_upah'];
                    $lain['jenis'] = $table_jenis[$i];
                    $lain['jenis_transaksi'] = $table_jenis_transaksi[$i];
                    $lain['jumlah'] = $table_jumlah[$i];

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

                DB::commit();
            } catch (Exception $e) {
                DB::rollBack();
            }
            
        }else{ // simpan utk edit data upah produksi
            DB::beginTransaction();
            try {
                DB::table('pro_upahproduksi')->where('id_upahproduksi', $id_upahproduksi)->update($data);
                $id_del_borongan = array();
                $id_del = array();
                $id_del_lain=array();
                $id_store_borongan = array();
                $id_store = array();
                $id_store_lain2=array();

                // proses array data id yg akan di update dan delete
                if($data['id_jenis']==1){ // upah borongan
                    $d_borongan = DB::table('pro_upah_total')->where('id_upah_borongan', '=', $id_upahproduksi);
                    
                    foreach($d_borongan->get() as $d){
                        $id_store_borongan[] = $d->id;
                    }

                    foreach($id_store_borongan as $d){
                        if(count($id_pbb) > 0){
                            if(!in_array($d, $id_pbb)){
                                $id_del_borongan[] = $d;
                            }
                        }else{
                            $id_del_borongan[] = $d;
                        }
                    }

                }else if($data['id_jenis']==2){ // upah harian
                    $d_harian = DB::table('pro_upahproduksi_harian')->where('id_upahproduksi', '=', $id_upahproduksi);
                    
                    foreach($d_harian->get() as $d){
                        $id_store[] = $d->id;
                    }

                    foreach($id_store as $d){
                        if(count($tabel_id) > 0){
                            if(!in_array($d, $tabel_id)){
                                $id_del[] = $d;
                            }
                        }else{
                            $id_del[] = $d;
                        }
                    }

                }

                // upah lain-lain
                $d_lain22 = DB::table('pro_upah_borongan_lain_lain')->where('id_upah_borongan','=',$id_upahproduksi);
                foreach ($d_lain22->get() as $d) {
                    $id_store_lain2[]=$d->id_upah;
                }

                foreach ($id_store_lain2 as $d) {
                    if (count($tabel_id2)>0) {
                        if(!in_array($d, $tabel_id2)){
                            $id_del_lain[]=$d;
                        }
                    }else{
                        $id_del_lain[]=$d;
                    }
                }
                // end : proses array data id yg akan di update dan delete

                // proses insert dan update data
                if($data['id_jenis']==1){ // upah borongan
                    for($i=0;$i < count($id_pbb);$i++){
                        $harga['upah_satuan']       = $upah_satuan__[$i];
                        $harga['upah_total']        = $total_upah__[$i];

                        // if($id_pbb[$i] == ''){
                        //     DB::table('pro_upah_total')->insert($harga);
                        // }else 
                        if($id_pbb != ''){
                            DB::table('pro_upah_total')->where(array('id' => $id_pbb[$i]))->update($harga);
                        }
                    }

                }else if($data['id_jenis']==2){ // upah harian

                    for($i = 0; $i < count($tabel_id); $i++){
                        $upah['id_upahproduksi'] = $id_upahproduksi;
                        $upah['id_perkiraan'] = $data['id_akun_upah'];
                        $upah['keterangan'] = $tabel_ket[$i];
                        $upah['satuan'] = $tabel_satuan[$i];
                        $upah['satuan_waktu'] = $tabel_satuanwaktu[$i];
                        $upah['upah'] = $tabel_upah[$i];

                        if($tabel_id[$i] == ''){
                            DB::table('pro_upahproduksi_harian')->insert($upah);
                        }else if($tabel_id != ''){
                            DB::table('pro_upahproduksi_harian')->where(array('id' => $tabel_id[$i]))->update($upah);
                        }
                    }

                }

                for ($i=0; $i < count($tabel_id2); $i++) { 
                    $lain['id_upah_borongan']=$id_upahproduksi;
                    $lain['jenis'] = $table_jenis[$i];
                    $lain['jenis_transaksi'] = $table_jenis_transaksi[$i];
                    $lain['jumlah'] = $table_jumlah[$i];
                    if($tabel_id2[$i]==''){
                        $lain['id_akun_upah_lain']=$data['id_akun_upah'];
                        DB::table('pro_upah_borongan_lain_lain')->insert($lain);
                    }else if($tabel_id2 != ''){
                        DB::table('pro_upah_borongan_lain_lain')->where(array('id_upah' => $tabel_id2[$i]))->update($lain);
                    }
                }
                // end : proses insert dan update data
                
                // proses delete data
                if(count($id_del_borongan) > 0){
                    DB::table('pro_upah_total')->whereIn('id', $id_del_borongan)->delete();
                }
                
                if(count($id_del) > 0){
                    DB::table('pro_upahproduksi_harian')->whereIn('id', $id_del)->delete();
                }
                if(count($id_del_lain)>0){
                    DB::table('pro_upah_borongan_lain_lain')->whereIn('id_upah',$id_del_lain)->delete();
                }
                // end : proses delete data
            
                DB::commit();
            } catch (Exception $e) {
                DB::rollBack();
            }

        }

        return redirect('upahproduksi');
    }

    function get_edit(Request $request){
        $id = $request->get('id');
        $d_data = DB::table('pro_upahproduksi_barang as pub')->join('ms_perkiraan as mp','pub.id_perkiraan','mp.id')->join('ms_satuan as ms','pub.satuan_waktu','ms.id_satuan')->join('ms_upahket as mu','pub.keterangan','mu.id_upahket')->select(DB::raw('pub.*,mp.kode_perkiraan,mp.nama_perkiraan,ms.nama_satuan as nama_satuanwaktu, mu.nama_upahket'))->where(array('id_upahproduksi'=>$id));
        if($d_data->count() > 0){
            foreach ($d_data->get() as $d) {
                # code...
                $arr[] = array("id_upahproduksi_barang" => $d->id_upahproduksi_barang,
                                "id_keterangan" => $d->keterangan,
                                "keterangan" => $d->nama_upahket,
                                "perkiraan" => $d->kode_perkiraan." - ".$d->nama_perkiraan,
                                "id_perkiraan" => $d->id_perkiraan,
                                "satuan" => $d->satuan,
                                "satuan_waktu" => $d->satuan_waktu,
                                "nama_satuanwaktu" => $d->nama_satuanwaktu,
                                "upah" => $d->upah);
            }
        }else{
            $arr = array();
        }

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

    /* rekanan tenun */
    public function rekanan_tenun_tambah($id_barangmasuk)
    {
        $data['f'] = 'add'; // jenis function controller
        $data['rekanan']    = DB::table('ms_rekanan as mr')->join('ms_rekanan_kelompok as mrk','mr.id_kelompok','mrk.id_kelompok')->select(DB::raw('mr.*, mrk.nama as nama_kelompok'))->get();
        $data['pegawai']    = DB::table('ms_pegawai')->get();
        $data['produksi']   = DB::table('ms_lokasi_produksi')->get();
        $akun_upah  = DB::table("ms_perkiraan")->where([ ["kd_rek_1", '=', '5'] ])->get();
        $data['akun_upah'] = array_reduce($akun_upah->toArray(), function($carry, $item){
            $carry[ $item->id ] = format_akun_perkiraan($item);
            return $carry;
        });
        $data['barang_masuk'] = \App\BarangMasuk::with("dataBarangkeluar")->find($id_barangmasuk);
        $data['id_trans'] = "";
        $data['url_referrer'] = url('barangmasuk');
        $data['data'] = $this->data([]);
        // dd($data['barang_masuk']->dataBarangkeluar->id_rekanan);
        return view("produksi.rekanan.UpahproduksiTenunTambah")->with("data", $data);
    }

    public function rekanan_tenun_edit(Request $request, $id_upahproduksi)
    {
        $data['f'] = 'edit'; // jenis function controller
        $upahProduksiTenun = DB::table('pro_upahproduksi as pu')
            ->join('ms_proses as mp','pu.id_proses','mp.id_proses')
            ->leftJoin('ms_rekanan as mr','pu.id_rekanan','mr.id_rekanan')
            ->leftjoin('ms_pegawai as mpe','pu.id_pegawai','mpe.id_pegawai')
            ->leftjoin('pem_workorder as pw','pu.id_workorder','pw.id_workorder')
            ->leftjoin('pem_workorder_barang as pwb','pu.id_workorder_barang','pwb.id_workorder_barang')
            ->select(DB::raw('pu.*, mp.nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai, pw.nomor as no_workorder, pwb.no_workorder as no_workorder_barang'))->where('pu.id_upahproduksi','=', $id_upahproduksi)->first();
        // dd($upahProduksiTenun);
        $data['rekanan']    = DB::table('ms_rekanan as mr')->join('ms_rekanan_kelompok as mrk','mr.id_kelompok','mrk.id_kelompok')->select(DB::raw('mr.*, mrk.nama as nama_kelompok'))->get();
        $data['pegawai']    = DB::table('ms_pegawai')->get();
        $data['produksi']   = DB::table('ms_lokasi_produksi')->get();
        $akun_upah  = DB::table("ms_perkiraan")->where([ ["kd_rek_1", '=', '5'] ])->get();
        $data['akun_upah'] = array_reduce($akun_upah->toArray(), function($carry, $item){
            $carry[ $item->id ] = format_akun_perkiraan($item);
            return $carry;
        });
        $data['barang_masuk'] = \App\BarangMasuk::with("dataBarangkeluar")->find($upahProduksiTenun->id_barangmasuk);
        $data['id_trans'] = $id_upahproduksi;
        $data['url_referrer'] = url('upahproduksi');
        $data['data'] = $this->data($upahProduksiTenun);
        // dd($data['barang_masuk']->dataBarangkeluar->id_rekanan);
        return view("produksi.rekanan.UpahproduksiTenunTambah")->with("data", $data);
    }

    function rekanan_tenun_simpan(Request $request)
    {
        $input = $request->all();
        $session = session()->all();

        $id_upahproduksi = $request->get('id_upahproduksi');
        $id_pegawai = 0;
        $id_rekanan = 0;
        $id_workorder = '';
        $id_workorder_barang = '';
        if($request->get('pegawai') != "" && $request->get('pegawai') != 0){
            $id_pegawai = $request->get('pegawai');
        }
        if($request->get('rekanan') != "" && $request->get('rekanan') != 0){
            $id_rekanan = $request->get('rekanan');
        }
        if($request->get('spk') != "" && $request->get('spk') != 0){
            $id_workorder = $request->get('spk');
        }
        if($request->get('workorder') != "" && $request->get('workorder') != 0){
            $id_workorder_barang = $request->get('workorder');
        }
        $data['id_barangmasuk'] = $request->get('proses');
        $data['id_barangkeluar'] = $request->get('id_barangkeluar');
        $data['id_rekanan'] = $id_rekanan;
        $data['id_pegawai'] = $id_pegawai;
        $data['tanggal'] = tgl_full($request->get('tanggal'),'2');
        $data['nomor'] = $request->get('nomor');
        if (is_array($request->get('lap_hasil'))) {
            $data['lap_hasil'] = implode(',',$request->get('lap_hasil'));
        } else {
            $data['lap_hasil'] = ($request->get('lap_hasil') == "") ? "" : $request->get('lap_hasil');
        }
        $data['id_proses'] = $request->get('id_proses');
        $data['id_workorder'] = $id_workorder;
        $data['id_workorder_barang'] = $id_workorder_barang;
        $data['id_jenis'] = $request->get('jenis');
        $data['id_lokasi'] = $request->get('id_jenis');

        // $data['termasuk_pajak'] = $request->get('termasuk_pajak') ? $request->get('termasuk_pajak') : '0';
        $data['pajak'] = $request->get('pajak');
        // $data['nominal_ongkir'] = $request->get('td_ongkir');

        $data['id_akun_upah'] = $request->get('id_akun_upah');
        $data['tanggal'] = tgl_full($request->get('tanggal'),'2');
        $data['nomor'] = $request->get('nomor');

        // tabel_lain_lain
        $tabel_id2= $request->get('tabel_id2');
        $table_jenis=$request->get('tabel_jenis');
        $table_jumlah=$request->get('tabel_upahll');
        // end : tabel_lain_lain

        // tabel_hasil
        $id_pbb= $request->get('id_pbb_');
        $id_proses__ = $request->get('id_proses_');
        $id_motif_barang__ = $request->get('id_motif_barang_');
        $id_motif__ = $request->get('id_motif_');
        $id_barang__ = $request->get('id_barang_');
        
        $id_gudang__ = $request->get('id_gudang_');
        $id_satuan__ = $request->get('id_satuan_');
        $id_komposisi__ = $request->get('id_komposisi_');
        $id_kualitas__ = $request->get('id_kualitas_');
        $id_kualitas_dudul__ = $request->get('id_kualitas_dudul_');
        $id_kualitas_proses__ = $request->get('id_kualitas_proses_');
        $id_kualitas_periksa__ = $request->get('id_kualitas_periksa_');
        $id_label__ = $request->get('id_label_');
        $id_cap__ = $request->get('id_cap_');
        $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_plat__ = $request->get('id_plat_');

        $jumlah__ = $request->get('jumlah_barang_');
        $upah_satuan__= $request->get('upah_satuan_');
        $upah_satuan_2__= $request->get('upah_satuan_2_');
        $upah_pick__= $request->get('upah_pick_');
        $total_upah__ = $request->get('upah_total_');
        // end : tabel_hasil

        DB::beginTransaction();
        try{
            if($id_upahproduksi == ''){
                $id = DB::table('pro_upahproduksi')->insertGetId($data);
                
                if ($tabel_id2 != null) {
                    for($i=0;$i<count($tabel_id2);$i++){
                        $lain['id_upah_borongan']   = $id;
                        $lain['id_akun_upah_lain']=$data['id_akun_upah'];
                        $lain['jenis']              = $table_jenis[$i];
                        $lain['jumlah']             = $table_jumlah[$i];

                        DB::table('pro_upah_borongan_lain_lain')->insert($lain);
                    }
                }
                
                for($i=0;$i < count($id_proses__);$i++){
                    $harga['id_upah_borongan']  = $id;
                    $harga['id_pbb']            = $id_pbb[$i];
                    $harga['id_proses']         = $id_proses__[$i];
                    $harga['id_motif_barang']   = $id_motif_barang__[$i];
                    $harga['id_motif']          = $id_motif__[$i];
                    $harga['id_barang']         = $id_barang__[$i];
                    /* === */
                    $harga['id_gudang']             = $id_gudang__[$i];
                    $harga['id_satuan']             = $id_satuan__[$i];
                    $harga['id_komposisi']          = $id_komposisi__[$i];
                    $harga['id_kualitas']           = $id_kualitas__[$i];
                    $harga['id_kualitas_dudul']     = $id_kualitas_dudul__[$i];
                    $harga['id_kualitas_proses']    = $id_kualitas_proses__[$i];
                    $harga['id_kualitas_periksa']   = $id_kualitas_periksa__[$i];
                    $harga['id_label']              = $id_label__[$i];
                    $harga['id_cap']                = $id_cap__[$i];
                    $harga['id_kemasan']            = $id_kemasan__[$i];
                    $harga['id_kemasansatuan']      = $id_kemasansatuan__[$i];
                    $harga['id_kemasansepuluh']     = $id_kemasansepuluh__[$i];
                    $harga['id_kemasanseratus']     = $id_kemasanseratus__[$i];
                    $harga['id_plat']               = $id_plat__[$i];
                    /* === */
                    $harga['jumlah_barang']     = $jumlah__[$i];
                    $harga['upah_satuan']       = $upah_satuan__[$i];
                    $harga['upah_satuan_2']     = $upah_satuan_2__[$i];
                    //$harga['potongan_upah_kualitas']    = $potongan_upah_kualitas__[$i];
                    $harga['potongan_upah_kualitas']    = 0;
                    $harga['upah_pick']                 = $upah_pick__[$i];
                    $harga['upah_total']                = $total_upah__[$i];

                    DB::table('pro_upah_total')->insert($harga);
                }
            }else{
                DB::table('pro_upahproduksi')->where('id_upahproduksi', $id_upahproduksi)->update($data);
                $id_del = array();
                $id_del_lain=array();
                $id_store = array();
                $id_store_lain2=array();

                $d_lain22 = DB::table('pro_upah_borongan_lain_lain')->where('id_upah_borongan','=',$id_upahproduksi);
                
                for($i=0;$i < count($id_proses__);$i++){
                    $harga['upah_satuan']               = $upah_satuan__[$i];
                    $harga['upah_satuan_2']             = $upah_satuan_2__[$i];
                    //$harga['potongan_upah_kualitas']    = $potongan_upah_kualitas__[$i];
                    $harga['potongan_upah_kualitas']    = 0;
                    $harga['upah_pick']                 = $upah_pick__[$i];
                    $harga['upah_total']                = $total_upah__[$i];

                    if($id_pbb != ''){
                        DB::table('pro_upah_total')->where(array('id_pbb' => $id_pbb[$i]))->update($harga);
                    }
                }
                
                if ($d_lain22->count() > 0) {
                    foreach ($d_lain22->get() as $d) {
                        $id_store_lain2[]=$d->id_upah;
                    }
                    
                    foreach ($id_store_lain2 as $d) {
                        if ($tabel_id2 != null) {
                            if (count($tabel_id2)>0) {
                                if(!in_array($d, $tabel_id2)){
                                    $id_del_lain[]=$d;
                                }
                            }
                        }else{
                            $id_del_lain[]=$d;
                        }
                    }
                }
                
                if ($tabel_id2 != null) {
                    for ($i=0; $i < count($tabel_id2); $i++) { 
                        $lain['id_upah_borongan']=$id_upahproduksi;
                        $lain['id_akun_upah_lain']=$data['id_akun_upah'];
                        $lain['jenis'] = $table_jenis[$i];
                        $lain['jumlah'] = $table_jumlah[$i];
                        if($tabel_id2[$i]==''){
                            DB::table('pro_upah_borongan_lain_lain')->insert($lain);
                        }else if($tabel_id2 != ''){
                            DB::table('pro_upah_borongan_lain_lain')->where(array('id_upah' => $tabel_id2[$i]))->update($lain);
                        }
                    }
                }

                if(count($id_del_lain)>0){
                    DB::table('pro_upah_borongan_lain_lain')->whereIn('id_upah',$id_del_lain)->delete();
                }
            }
            DB::commit();
            return redirect('upahproduksi');
        }catch (Exception $e){
            return redirect()->back();
        }
    }
    /* end : rekanan tenun */


    function hapus(Request $request){
        $id = $request->get('id');
        DB::table('pro_upahproduksi')->where('id_upahproduksi',$id)->delete();
        DB::table('pro_upah_total')->where(array('id_upah_borongan'=>$id))->delete();
        DB::table('pro_upah_borongan_lain_lain')->where(array('id_upah_borongan'=>$id))->delete();
        DB::table('pro_upahproduksi_harian')->where(array('id_upahproduksi'=>$id))->delete();
    }

    public function get_nomor_upah(Request $request)
    {
        $input = $request->all();
        $digit = 4;
        $substr_digit = -1*$digit;

        // $tanggal = explode("/", $input['tanggal']);
        if($input['id_upahproduksi']!=""){
            $upahproduksi = DB::table("pro_upahproduksi")->where([ ["id_upahproduksi", "=",$input['id_upahproduksi']] ])->first();
            if($upahproduksi->nomor==""){
                $upahproduksi = DB::table("pro_upahproduksi")->max(DB::raw("CAST(SUBSTRING(nomor, $substr_digit) AS UNSIGNED)"), "nomor");
                $upahproduksi = (int) preg_replace("/[^0-9]/", '', $upahproduksi);
                if($upahproduksi==0){
                    $upahproduksi = sprintf("%0{$digit}d", 1);
                }
                
                $result = [
                        "nomor"=>sprintf("%0{$digit}d", (int)$upahproduksi+1),
                    ];
            }else{
                $result = [
                        "nomor"=>$upahproduksi->nomor,
                    ];   
            }
        }else{
            $upahproduksi = DB::table("pro_upahproduksi")->max(DB::raw("CAST(SUBSTRING(nomor, $substr_digit) AS UNSIGNED)"), "nomor");
            $upahproduksi = (int) preg_replace("/[^0-9]/", '', $upahproduksi);
            if($upahproduksi==0){
                $upahproduksi = 1;
            }

            $result = [
                    "nomor"=>sprintf("%0{$digit}d", (int)$upahproduksi+1),
                ];

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