<?php

namespace App\Http\Controllers\produksi;

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

class BarangmasukController extends Controller{

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


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

        $data['proses'] = DB::table('ms_proses as mp')->leftJoin('ms_proses as mp2','mp.parent','mp2.id_proses')->select('mp.nama_proses as nama_proses','mp2.nama_proses as nama_proses2','mp.id_proses')->where('mp.jenis', '0')->where("mp.parent", "!=", "0")->orderBy('mp.id_proses','ASC');
        if($id_proses != 0){
            $data['proses']->whereIn('mp.id_proses', explode(",", $id_proses));
        }

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

    function get_data(Request $request){
        DB::statement(DB::raw('set @rownum=0'));

        $id_proses = $this->session['id_proses'];
        $id_lokasi = $this->session['id_lokasi'];

        $post_proses = $request->get("proses");
        $post_tglawal = $request->get("tgl_awal");
        $post_tglakhir = $request->get("tgl_akhir");
        $is_semua = $request->get("jenis");

        // $store_proses = '';
        // if($request->get("proses_semua") == 0){
        //     if($id_proses != 0 || $id_proses != '' || !is_null($id_proses)){
        //         if(in_array("37", explode(',', $id_proses))){
        //             $store_proses = '37';
        //         }
        //     }
        // }else{
        //     $store_proses = $request->get("proses_semua");
        // }

        $is_proses = ($request->get("proses_semua") == 0) ? '':$request->get("proses_semua");
        $tgl_awal = '';
        $tgl_akhir = '';

        if($is_semua == '0'){
            $is_proses = $post_proses;
            $tgl_awal = $post_tglawal;
            $tgl_akhir = $post_tglakhir;
        }

        
        
        if($this->session['id_proses'] != 0 && $this->session['id_lokasi'] != 0){
        $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', '!=', '0')->where('pb.id_proses', '!=', '37')->where('pb.id_lokasi', '=', $this->session['id_lokasi'])->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, pb.noAuto, pb.noAuto_ext, @rownum  := @rownum  + 1 AS rownum'));
            //->whereIn('pb.id_proses', explode(",", $this->session['id_proses']))
            
        }else{
            $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', '!=', '0')->where('pb.id_proses', '!=', '37')->where('pb.id_lokasi', '=', '1')->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, pb.noAuto, pb.noAuto_ext, @rownum  := @rownum  + 1 AS rownum'));
        }

        if($is_proses != ''){
            $barangkeluar->where("pb.id_proses", $is_proses);
        }else{
            if(!is_null($id_proses) && $id_proses != 0){
                $barangkeluar->whereIn('pb.id_proses', explode(",", $this->session['id_proses']));
            }
        }

        
        // if($id_proses != 0){
        //     if(in_array('37', explode(',', $id_proses))){
        //         $barangkeluar_tenun = get_detailtenun()->where('pb.id_lokasi', '1')->where('mpr.id_proses', '37');
        //     }else{
        //         $barangkeluar_tenun = get_detailtenun()->where('pb.id_lokasi', '1')->where('mpr.id_proses', '10000000');
        //     }
        // }else{
        //     $barangkeluar_tenun = get_detailtenun()->where('pb.id_lokasi', '1')->where('mpr.id_proses', '37');
        // }

        $barangkeluar_tenun = get_detailtenun()->where('pb.id_lokasi', '1')->where('mpr.id_proses', '10000000');
        if($is_proses != '' && $is_proses == '37'){
            if($id_proses != 0 && !is_null($id_proses) && in_array('37', explode(',', $id_proses))){
                // if(){
                    $barangkeluar_tenun = get_detailtenun()->where('pb.id_lokasi', '1')->where('mpr.id_proses', '37');
                // }
            }else{
                if($is_proses == '37'){
                    $barangkeluar_tenun = get_detailtenun()->where('pb.id_lokasi', '1')->where('mpr.id_proses', '37');
                }
            }
        }else{
            if($id_proses != 0 && !is_null($id_proses)){
                if(in_array('37', explode(',', $id_proses)) && $is_proses == ''){
                    $barangkeluar_tenun = get_detailtenun()->where('pb.id_lokasi', '1')->where('mpr.id_proses', '37');
                }
            }else{
                if($is_proses == '37' || $is_proses == ''){
                    $barangkeluar_tenun = get_detailtenun()->where('pb.id_lokasi', '1')->where('mpr.id_proses', '37');
                }
            }
        }

        if($tgl_awal != '' && $tgl_akhir != ''){
            $tgl_awal = tgl_full($post_tglawal, '2');
            $tgl_akhir = tgl_full($post_tglakhir, '2');
            $barangkeluar->whereBetween("pb.tanggal", [$tgl_awal, $tgl_akhir]);
            $barangkeluar_tenun->whereBetween("pb.tanggal", [$tgl_awal, $tgl_akhir]);
        }



        if($barangkeluar->count() > 0){
            foreach($barangkeluar->get() as $b){
                $pekerja = '';
                $stat_pekerja = '';
                $jenis = 0;
                $status = 0;
                $nomor = '';
                $nomor_jenis = '';
                $noAuto = '';

                $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_barang;
                    $nomor_jenis = '( '."Nomor Order".' )';
                }else if($b->no_workorder_barang){
                    $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,
                                'nomor' => $noAuto,
                                'nomor_bon' => $b->noAuto.$b->noAuto_ext,
                                'tanggal' => date('d-m-Y', strtotime($b->tanggal)),
                                'tanggal_laporan' => '',
                                'nama_proses' => $b->nama_proses,
                                'id_tabel' =>'1',
                                '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, $stat_pekerja, 1));
                }else{
                    $d_masuk = DB::table('pro_barangmasuk as pb')->where('pb.id_barangkeluar', $b->id_barangkeluar)->leftjoin("pro_upahproduksi AS pu", "pb.id_barangmasuk", "pu.id_barangmasuk")->select(DB::raw("pb.*, CASE WHEN pu.id_upahproduksi IS NULL THEN 0 ELSE 1 END AS status_bayar"))->groupBy("pb.id_barangmasuk");
                    
                    foreach($d_masuk->get() as $masuk) {
                        if($b->status == '2'){
                            $status = $masuk->status;
                            $id_produksi = $masuk->id_barangmasuk;
                            $jenis = 1;
                            $noAuto = $masuk->noAuto.$masuk->noAuto_ext;
                        }else if($b->status == '3'){
                            $noAuto = $masuk->noAuto.$masuk->noAuto_ext;
                            $status = $masuk->status;
                            $id_produksi = $masuk->id_barangmasuk;
                            $jenis = 1;
                        }

                        $arr[] = array('id_barangkeluar' => $b->id_barangkeluar,
                                'no_workorder' => $nomor.$nomor_jenis,
                                'nomor' => $noAuto,
                                'nomor_bon' => $b->noAuto.$b->noAuto_ext,
                                'tanggal' => date('d-m-Y', strtotime($b->tanggal)),
                                'tanggal_laporan' => tgl_full($masuk->tanggal, ''),
                                'nama_proses' => $b->nama_proses,
                                'pekerja' => $pekerja,
                                'id_tabel'=>2,
                                '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, $stat_pekerja,2, $masuk->status_bayar)
                            );  
                    } 

                    if($b->status == '3'){
                        $status = '3';
                        $jenis = 0;
                        $id_produksi = $b->id_barangkeluar;
                        $noAuto = '';
                        $arr[] = array('id_barangkeluar' => $b->id_barangkeluar,
                                    'no_workorder' => $nomor.$nomor_jenis,
                                    'nomor' => $noAuto,
                                    'nomor_bon' => $b->noAuto.$b->noAuto_ext,
                                    'tanggal' => date('d-m-Y', strtotime($b->tanggal)),
                                    'tanggal_laporan' => '',
                                    'nama_proses' => $b->nama_proses,
                                    'pekerja' => $pekerja,
                                    'id_tabel'=>1,
                                    '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, $stat_pekerja,1));
                    }
                }
            }
        }else{
            $arr = array();
        }

        if($barangkeluar_tenun->count() > 0){
            foreach($barangkeluar_tenun->get() as $b){
                $pekerja = '';
                $stat_pekerja = '';
                $jenis = 0;
                $status = 0;
                $nomor = '';
                $nomor_jenis = '';
                $noAuto = '';

                $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_barang;
                    $nomor_jenis = '( '."Nomor Order".' )';
                }else if($b->no_workorder_barang){
                    $nomor = $b->no_workorder_barang;
                    $nomor_jenis = '( '."Nomor Order".' )';
                }
                
                if($b->status == '1'){
                    $status = $b->status;
                    $jenis = 2;
                    $id_produksi = $b->id_barangkeluar;
                    $arr[] = array('id_barangkeluar' => $b->id_barangkeluar,
                                'no_workorder' => $nomor.$nomor_jenis,
                                'nomor' => $noAuto,
                                'nomor_bon' => $b->noAuto.$b->noAuto_ext,
                                'tanggal' => date('d-m-Y', strtotime($b->tanggal)),
                                'tanggal_laporan' => '',
                                'nama_proses' => $b->nama_proses,
                                'id_tabel' =>'1',
                                'pekerja' => $pekerja,
                                'stat_pekerja' => $stat_pekerja,
                                'proses' => $b->nama_proses,
                                'status_id' => $b->status,
                                'status' => $this->proses($jenis, $status),
                                'rownum' => $b->status,
                                'posisi' => $pekerja.' ('.$stat_pekerja.')',
                                'aksi' => $this->aksi($jenis, $status, $id_produksi, $stat_pekerja,1));                    
                }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 = 3;
                            $noAuto = $masuk->noAuto.$masuk->noAuto_ext;
                        }else if($b->status == '3'){
                            $noAuto = $masuk->noAuto.$masuk->noAuto_ext;
                            $status = $masuk->status;
                            $id_produksi = $masuk->id_barangmasuk;
                            $jenis = 3;
                        }

                        $arr[] = array('id_barangkeluar' => $b->id_barangkeluar,
                                'no_workorder' => $nomor.$nomor_jenis,
                                'nomor' => $noAuto,
                                'nomor_bon' => $b->noAuto.$b->noAuto_ext,
                                'tanggal' => date('d-m-Y', strtotime($b->tanggal)),
                                'tanggal_laporan' => tgl_full($masuk->tanggal, ''),
                                'nama_proses' => $b->nama_proses,
                                'pekerja' => $pekerja,
                                'id_tabel'=>2,
                                'stat_pekerja' => $stat_pekerja,
                                'proses' => $b->nama_proses,
                                'status_id' => $b->status,
                                'status' => $this->proses($jenis, $status),
                                'rownum' => $b->status,
                                'posisi' => $pekerja.' ('.$stat_pekerja.')',
                                'aksi' => $this->aksi($jenis, $status, $id_produksi, $stat_pekerja,2)
                            );  
                    }
                    
                    if($b->status == '3'){
                        $status = '3';
                        $jenis = 2;
                        $id_produksi = $b->id_barangkeluar;
                        $noAuto = '';
                        $arr[] = array('id_barangkeluar' => $b->id_barangkeluar,
                                    'no_workorder' => $nomor.$nomor_jenis,
                                    'nomor' => $noAuto,
                                    'nomor_bon' => $b->noAuto.$b->noAuto_ext,
                                    'tanggal' => date('d-m-Y', strtotime($b->tanggal)),
                                    'tanggal_laporan' => '',
                                    'nama_proses' => $b->nama_proses,
                                    'pekerja' => $pekerja,
                                    'id_tabel'=>1,
                                    'stat_pekerja' => $stat_pekerja,
                                    'proses' => $b->nama_proses,
                                    'status_id' => $b->status,
                                    'status' => $this->proses($jenis, $status),
                                    'rownum' => $b->status,
                                    'posisi' => $pekerja.' ('.$stat_pekerja.')',
                                    'aksi' => $this->aksi($jenis, $status, $id_produksi, $stat_pekerja,1));
                    }   
                }
            }
        }

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

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

        if($this->session['id_proses'] != 0 && $this->session['id_lokasi'] != 0){
            $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', '!=', '0')
                ->where('pb.id_proses', '!=', '37')
                ->where('pb.id_lokasi', '=', $this->session['id_lokasi'])
                ->whereIn('pb.id_proses', explode(",", $this->session['id_proses']))
                ->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, 
                    pb.tanggal, 
                    NULL AS tanggal_perkiraan, 
                    NULL AS nama_kelompok, 
                    pb.id_lokasi, 
                    pb.status, 
                    pb.noAuto, 
                    pb.noAuto_ext, 
                    "nontenun" AS barang_keluar'));
        }else{
            $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', '!=', '0')
                ->where('pb.id_proses', '!=', '37')
                ->where('pb.id_lokasi', '=', '1')
                ->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, 
                    pb.tanggal, 
                    NULL AS tanggal_perkiraan, 
                    NULL AS nama_kelompok, 
                    pb.id_lokasi, 
                    pb.status, 
                    pb.noAuto, 
                    pb.noAuto_ext, 
                    "nontenun" AS barang_keluar'));
        }

        if($id_proses != 0){
            if(in_array('37', explode(',', $id_proses))){
                $barangkeluar_tenun = DB::table('barangkeluar_tenun')->where('id_lokasi', '1')->where('id_proses', '37')->selectRaw('
                    id_barangkeluar, 
                    id_rekanan, 
                    nama_rekanan, 
                    id_pegawai, 
                    nama_pegawai, 
                    id_proses, 
                    nama_proses, 
                    id_workorder, 
                    no_workorder, 
                    id_workorder_barang, 
                    no_workorder_barang, 
                    tanggal, 
                    tanggal_perkiraan, 
                    nama_kelompok, 
                    id_lokasi,
                    status, 
                    noAuto, 
                    noAuto_ext, 
                    barang_keluar');
            }else{
                $barangkeluar_tenun = DB::table('barangkeluar_tenun')->where('id_lokasi', '1')->where('id_proses', '10000000')->selectRaw('
                    id_barangkeluar, 
                    id_rekanan, 
                    nama_rekanan, 
                    id_pegawai, 
                    nama_pegawai, 
                    id_proses, 
                    nama_proses, 
                    id_workorder, 
                    no_workorder, 
                    id_workorder_barang, 
                    no_workorder_barang, 
                    tanggal, 
                    tanggal_perkiraan, 
                    nama_kelompok, 
                    id_lokasi,
                    status, 
                    noAuto, 
                    noAuto_ext, 
                    barang_keluar');
            }
        }else{
            $barangkeluar_tenun = DB::table('barangkeluar_tenun')->where('id_lokasi', '1')->where('id_proses', '37')->selectRaw('
                    id_barangkeluar, 
                    id_rekanan, 
                    nama_rekanan, 
                    id_pegawai, 
                    nama_pegawai, 
                    id_proses, 
                    nama_proses, 
                    id_workorder, 
                    no_workorder, 
                    id_workorder_barang, 
                    no_workorder_barang, 
                    tanggal, 
                    tanggal_perkiraan, 
                    nama_kelompok, 
                    id_lokasi,
                    status, 
                    noAuto, 
                    noAuto_ext, 
                    barang_keluar');
        }

        $data_barang_keluar = $barangkeluar->union($barangkeluar_tenun)->get();

        if (count($data_barang_keluar) > 0) {
            foreach ($data_barang_keluar as $key => $b) {
                // Barang_keluar_nontenun
                $pekerja = '';
                $stat_pekerja = '';
                $jenis = 0;
                $status = 0;
                $nomor = '';
                $nomor_jenis = '';
                $noAuto = '';

                $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_barang;
                    $nomor_jenis = '( '."Nomor Order".' )';
                }else if($b->no_workorder_barang){
                    $nomor = $b->no_workorder_barang;
                    $nomor_jenis = '( '."Nomor Order".' )';
                }

                if ($b->barang_keluar == "nontenun") {
                    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,
                                    'nomor'            => $noAuto,
                                    'nomor_bon'        => $b->noAuto.$b->noAuto_ext,
                                    'tanggal'          => date('d-m-Y', strtotime($b->tanggal)),
                                    'tanggal_laporan'  => '',
                                    'nama_proses'      => $b->nama_proses,
                                    'id_tabel'         => '1',
                                    'pekerja'          => $pekerja,
                                    'stat_pekerja'     => $stat_pekerja,
                                    'proses'           => $b->nama_proses,
                                    'status_id'        => $b->status,
                                    'status'           => $this->proses($jenis, $status),
                                    'posisi'           => $pekerja.' ('.$stat_pekerja.')',
                                    'aksi'             => $this->aksi($jenis, $status, $id_produksi, $stat_pekerja, 1));
                    }else{
                        $d_masuk = DB::table('pro_barangmasuk as pb')->where('pb.id_barangkeluar', $b->id_barangkeluar)->leftjoin("pro_upahproduksi AS pu", "pb.id_barangmasuk", "pu.id_barangmasuk")->select(DB::raw("pb.status, pb.id_barangmasuk, pb.noAuto, pb.noAuto_ext, pb.tanggal, CASE WHEN pu.id_upahproduksi IS NULL THEN 0 ELSE 1 END AS status_bayar"))->groupBy("pb.id_barangmasuk");
                        
                        foreach($d_masuk->get() as $masuk) {
                            if($b->status == '2'){
                                $status = $masuk->status;
                                $id_produksi = $masuk->id_barangmasuk;
                                $jenis = 1;
                                $noAuto = $masuk->noAuto.$masuk->noAuto_ext;
                            }else if($b->status == '3'){
                                $noAuto = $masuk->noAuto.$masuk->noAuto_ext;
                                $status = $masuk->status;
                                $id_produksi = $masuk->id_barangmasuk;
                                $jenis = 1;
                            }

                            $arr[] = array(
                                    'id_barangkeluar' => $b->id_barangkeluar,
                                    'no_workorder'    => $nomor.$nomor_jenis,
                                    'nomor'           => $noAuto,
                                    'nomor_bon'       => $b->noAuto.$b->noAuto_ext,
                                    'tanggal'         => date('d-m-Y', strtotime($b->tanggal)),
                                    'tanggal_laporan' => tgl_full($masuk->tanggal, ''),
                                    'nama_proses'     => $b->nama_proses,
                                    'pekerja'         => $pekerja,
                                    'id_tabel'        => 2,
                                    'stat_pekerja'    => $stat_pekerja,
                                    'proses'          => $b->nama_proses,
                                    'status_id'       => $b->status,
                                    'status'          => $this->proses($jenis, $status),
                                    'posisi'          => $pekerja.' ('.$stat_pekerja.')',
                                    'aksi'            => $this->aksi($jenis, $status, $id_produksi, $stat_pekerja,2, $masuk->status_bayar)
                                );  
                        } 

                        if($b->status == '3'){
                            $status = '3';
                            $jenis = 0;
                            $id_produksi = $b->id_barangkeluar;
                            $noAuto = '';
                            $arr[] = array(
                                        'id_barangkeluar' => $b->id_barangkeluar,
                                        'no_workorder'    => $nomor.$nomor_jenis,
                                        'nomor'           => $noAuto,
                                        'nomor_bon'       => $b->noAuto.$b->noAuto_ext,
                                        'tanggal'         => date('d-m-Y', strtotime($b->tanggal)),
                                        'tanggal_laporan' => '',
                                        'nama_proses'     => $b->nama_proses,
                                        'pekerja'         => $pekerja,
                                        'id_tabel'        => 1,
                                        'stat_pekerja'    => $stat_pekerja,
                                        'proses'          => $b->nama_proses,
                                        'status_id'       => $b->status,
                                        'status'          => $this->proses($jenis, $status),
                                        'posisi'          => $pekerja.' ('.$stat_pekerja.')',
                                        'aksi'            => $this->aksi($jenis, $status, $id_produksi, $stat_pekerja,1));
                        }
                    }
                } else {
                    if($b->status == '1'){
                        $status = $b->status;
                        $jenis = 2;
                        $id_produksi = $b->id_barangkeluar;
                        $arr[] = array(
                                    'id_barangkeluar' => $b->id_barangkeluar,
                                    'no_workorder'    => $nomor.$nomor_jenis,
                                    'nomor'           => $noAuto,
                                    'nomor_bon'       => $b->noAuto.$b->noAuto_ext,
                                    'tanggal'         => date('d-m-Y', strtotime($b->tanggal)),
                                    'tanggal_laporan' => '',
                                    'nama_proses'     => $b->nama_proses,
                                    'id_tabel'        => '1',
                                    'pekerja'         => $pekerja,
                                    'stat_pekerja'    => $stat_pekerja,
                                    'proses'          => $b->nama_proses,
                                    'status_id'       => $b->status,
                                    'status'          => $this->proses($jenis, $status),
                                    'posisi'          => $pekerja.' ('.$stat_pekerja.')',
                                    'aksi'            => $this->aksi($jenis, $status, $id_produksi, $stat_pekerja,1));                    
                    }else{
                        $d_masuk = DB::table('pro_barangmasuk')->where('id_barangkeluar', $b->id_barangkeluar)->select('status', 'id_barangmasuk', 'noAuto', 'noAuto_ext', 'tanggal');
                        
                        foreach($d_masuk->get() as $masuk) {
                            if($b->status == '2'){
                                $status = $masuk->status;
                                $id_produksi = $masuk->id_barangmasuk;
                                $jenis = 3;
                                $noAuto = $masuk->noAuto.$masuk->noAuto_ext;
                            }else if($b->status == '3'){
                                $noAuto = $masuk->noAuto.$masuk->noAuto_ext;
                                $status = $masuk->status;
                                $id_produksi = $masuk->id_barangmasuk;
                                $jenis = 3;
                            }

                            $arr[] = array(
                                    'id_barangkeluar' => $b->id_barangkeluar,
                                    'no_workorder'    => $nomor.$nomor_jenis,
                                    'nomor'           => $noAuto,
                                    'nomor_bon'       => $b->noAuto.$b->noAuto_ext,
                                    'tanggal'         => date('d-m-Y', strtotime($b->tanggal)),
                                    'tanggal_laporan' => tgl_full($masuk->tanggal, ''),
                                    'nama_proses'     => $b->nama_proses,
                                    'pekerja'         => $pekerja,
                                    'id_tabel'        => 2,
                                    'stat_pekerja'    => $stat_pekerja,
                                    'proses'          => $b->nama_proses,
                                    'status_id'       => $b->status,
                                    'status'          => $this->proses($jenis, $status),
                                    'posisi'          => $pekerja.' ('.$stat_pekerja.')',
                                    'aksi'            => $this->aksi($jenis, $status, $id_produksi, $stat_pekerja,2)
                                );  
                        }
                        
                        if($b->status == '3'){
                            $status = '3';
                            $jenis = 2;
                            $id_produksi = $b->id_barangkeluar;
                            $noAuto = '';
                            $arr[] = array(
                                        'id_barangkeluar' => $b->id_barangkeluar,
                                        'no_workorder'    => $nomor.$nomor_jenis,
                                        'nomor'           => $noAuto,
                                        'nomor_bon'       => $b->noAuto.$b->noAuto_ext,
                                        'tanggal'         => date('d-m-Y', strtotime($b->tanggal)),
                                        'tanggal_laporan' => '',
                                        'nama_proses'     => $b->nama_proses,
                                        'pekerja'         => $pekerja,
                                        'id_tabel'        => 1,
                                        'stat_pekerja'    => $stat_pekerja,
                                        'proses'          => $b->nama_proses,
                                        'status_id'       => $b->status,
                                        'status'          => $this->proses($jenis, $status),
                                        'posisi'          => $pekerja.' ('.$stat_pekerja.')',
                                        'aksi'            => $this->aksi($jenis, $status, $id_produksi, $stat_pekerja,1));
                        }   
                    }
                }
            }
        } else {
            $arr = array();
        }
        
        $data = new Collection($arr);
        return Datatables::of($data)
                ->make(true);
                // ->toJson();
    }

	function get_kualitas($id){
        if($id == ''){
            $id = '1';
        }
    	$d_data = DB::table('ms_kualitas')->where('jenis', $id);

    	if($d_data->count() > 0){
    		foreach($d_data->orderBy('nama_kualitas', 'asc')->get() as $d){
    			$arr[] = array('id_kualitas' => $d->id_kualitas,
    							'nama_kualitas' => $d->nama_kualitas);
    		}
    	}else{
    		$arr = array();
    	}

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

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

    	$d_data = DB::table('ms_barang AS mb')->join('ms_kategori_barang AS mkb', 'mb.id_kb', 'mkb.id_kb')->join('ms_kategori_barang AS mkb2', 'mkb.induk_kb', 'mkb2.id_kb')->join('ms_satuan AS ms', 'mb.id_satuan', 'ms.id_satuan')->select(DB::raw('mb.*, mkb2.id_kb AS id_kategori, mkb2.nama nama_kategori, mkb.id_kb id_sub, mkb.nama nama_sub, ms.id_satuan, ms.nama_satuan'));

        if($d_data->count() > 0){
            foreach($d_data->orderBy('nama_kategori', 'asc')->orderBy('nama_sub', 'asc')->orderBy('kode_barang', 'asc')->orderBy('nama_barang', 'asc')->get() as $d){
                $arr['barang'][] = array('id_barang' => $d->id,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang,
                                'id_kategori' => $d->id_kategori,
                                'kategori' => $d->nama_kategori,
                                'id_sub' => $d->id_sub,
                                'nama_sub' => $d->nama_sub,
                                'satuan' => $d->nama_satuan);
            }
        }else{
            $arr['barang'] = array();
        }


        $d_gudang = DB::table('ms_gudang');

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

        if($d_gudang->count() > 0){
            foreach($d_gudang->orderBy('nama_gudang', 'asc')->get() as $d){
                $arr['gudang'][] = array('id_gudang' => $d->id_gudang,
                                'nama_gudang' => $d->nama_gudang);
            }
        }else{
            $arr['gudang'] = array();
        }


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


        $d_data = DB::table('ms_motif as mm')->join('ms_motif_barang as mmb', 'mm.id_motif', 'mmb.id_motif')->select(DB::raw('mm.id_motif, mm.kode_motif, mm.nama_motif, mmb.id_motif_barang, mmb.motif_keterangan'));
        if($d_data->count() > 0){
            foreach($d_data->orderBy('nama_motif', 'asc')->get() as $d){
                $arr['corak'][] = array('id_corak' => $d->id_motif,
                                'kode_motif' => $d->kode_motif,
                                'nama_motif' => $d->nama_motif,
                                'id_motif_barang' => $d->id_motif_barang,
                                'motif_keterangan' => $d->motif_keterangan);
            }
        }else{
            $arr['corak'] = array();
        }

        $d_data = DB::table('ms_kualitas')->where('jenis', '1');
        if($d_data->count() > 0){
            foreach($d_data->orderBy('nama_kualitas', 'asc')->get() as $d){
                $arr['kualitas'][] = array('id_kualitas' => $d->id_kualitas,
                                'nama_kualitas' => $d->nama_kualitas);
            }
        }else{
            $arr['kualitas'] = array();
        }


        $d_data = DB::table('ms_kualitas')->where('jenis', '2');
        if($d_data->count() > 0){
            foreach($d_data->orderBy('nama_kualitas', 'asc')->get() as $d){
                $arr['kualitas_proses'][] = array('id_kualitas' => $d->id_kualitas,
                                'nama_kualitas' => $d->nama_kualitas);
            }
        }else{
            $arr['kualitas_proses'] = array();
        }

        $d_data = DB::table('ms_kualitas')->where('jenis', '3');
        if($d_data->count() > 0){
            foreach($d_data->orderBy('nama_kualitas', 'asc')->get() as $d){
                $arr['kualitas_periksa'][] = array('id_kualitas' => $d->id_kualitas,
                                'nama_kualitas' => $d->nama_kualitas);
            }
        }else{
            $arr['kualitas_periksa'] = array();
        }


        $d_data = DB::table('ms_barang as mb')->where('mb.id_kb', '38');
        if($d_data->count() > 0){
            foreach($d_data->orderBy('mb.kode_barang', 'asc')->get() as $d){
                $arr['cap'][] = array('id' => $d->id,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang);
            }
        }else{
            $arr['cap'] = array();
        }


        $d_data = DB::table('ms_barang as mb')->where('mb.id_kb', '31');
        if($d_data->count() > 0){
            foreach($d_data->orderBy('mb.kode_barang', 'asc')->get() as $d){
                $arr['labell'][] = array('id' => $d->id,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang);
            }
        }else{
            $arr['labell'] = array();
        }

        $d_data = DB::table('ms_barang as mb')->where('mb.id_kb', '32')->where('mb.nama_barang', 'like', '%plastik%');
        if($d_data->count() > 0){
            foreach($d_data->orderBy('mb.nama_barang', 'asc')->get() as $d){
                $arr['kemasan'][] = array('id' => $d->id,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang);
            }
        }else{
            $arr['kemasan'] = array();
        }

        $d_data = DB::table("ms_barang")->where("id_kb", '30');
        if($d_data->count() > 0){
            foreach ($d_data->get() as $d) {
                $arr['kemasansatuan'][] = array('id_barang' => $d->id,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang);
            }

        }else{
            $arr['kemasansatuan'] = array();
        }

        $d_data = DB::table('ms_barang as mb')->where('mb.id_kb', '30');
        if($d_data->count() > 0){
            foreach($d_data->orderBy('mb.kode_barang', 'asc')->get() as $d){
                $arr['kemasansepuluh'][] = array('id' => $d->id,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang);
            }
        }else{
            $arr['kemasansepuluh'] = array();
        }


        $d_data = DB::table('ms_barang as mb')->where('mb.id_kb', '36');
        if($d_data->count() > 0){
            foreach($d_data->orderBy('mb.kode_barang', 'asc')->get() as $d){
                $arr['kemasanseratus'][] = array('id' => $d->id,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang);
            }
        }else{
            $arr['kemasanseratus'] = array();
        }

        $d_jahit = DB::table('ms_proses')->whereIn('id_proses', ["66", "69", "88", "89", "90"])->orderBy('nama_proses', 'asc');
        if($d_jahit->count() > 0){
            foreach($d_jahit->get() as $d){
                $arr['proses_jahit'][] = array('id_proses' => $d->id_proses,
                                                'nama_proses' => $d->nama_proses);
            }
        }else{
            $arr['proses_jahit'] = array();
        }



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

    function get_gudang(){
    	$d_data = DB::table('ms_gudang');

    	if($d_data->count() > 0){
    		foreach($d_data->orderBy('nama_gudang', 'asc')->get() as $d){
    			$arr[] = array('id_gudang' => $d->id_gudang,
    							'nama_gudang' => $d->nama_gudang);
    		}
    	}else{
    		$arr = array();
    	}

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

    function get_satuan(){
        $d_data = DB::table('ms_satuan');

        if($d_data->count() > 0){
            foreach($d_data->orderBy('nama_satuan', 'asc')->get() as $d){
                $arr[] = array('id_satuan' => $d->id_satuan,
                                'nama_satuan' => $d->nama_satuan);
            }
        }else{
            $arr = array();
        }

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

    function get_komposisi(){
        $d_data = DB::table('ms_warnakomposisi');

        if($d_data->count() > 0){
            foreach($d_data->orderBy('nama', 'asc')->get() as $d){
                $arr[] = array('id_komposisi' => $d->id_komposisi,
                                'nama_komposisi' => $d->nama);
            }
        }else{
            $arr = array();
        }

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

    function get_motif(){
        $d_data = DB::table('ms_motif as mm')->join('ms_motif_barang as mmb', 'mm.id_motif', 'mmb.id_motif')->select(DB::raw('mm.id_motif, mm.kode_motif, mm.nama_motif, mmb.id_motif_barang, mmb.motif_keterangan'));

        if($d_data->count() > 0){
            foreach($d_data->orderBy('nama_motif', 'asc')->get() as $d){
                $arr[] = array('id_motif' => $d->id_motif,
                                'kode_motif' => $d->kode_motif,
                                'nama_motif' => $d->nama_motif,
                                'id_motif_barang' => $d->id_motif_barang,
                                'motif_keterangan' => $d->motif_keterangan);
            }
        }else{
            $arr = array();
        }

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

    function get_barangbykat(Request $request){
        $id = $request->get('id');
        $d_data = DB::table("ms_barang")->where("id_kb", $id);
        if($d_data->count() > 0){
            foreach ($d_data->get() as $d) {
                $arr[] = array('id_barang' => $d->id,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang);
            }

        }else{
            $arr = array();
        }

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

    function get_label(){
        $d_data = DB::table("ms_barang")->where("id_kb",'=','31');
        if($d_data->count() > 0){
            foreach ($d_data->get() as $d) {
                $arr[] = array('id_barang' => $d->id,
                                'kode' => $d->kode_barang,
                                'nama' => $d->nama_barang);
            }

        }else{
            $arr = array();
        }

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

    function get_kemasan(){
        $d_data = DB::table('ms_barang as mb')->where('mb.id_kb', '32')->where('mb.nama_barang', 'like', '%plastik%');

        if($d_data->count() > 0){
            foreach($d_data->orderBy('mb.nama_barang', 'asc')->get() as $d){
                $arr[] = array('id' => $d->id,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang);
            }
        }else{
            $arr = array();
        }

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

    function get_kemasansatuan(){
        $d_data = DB::table('ms_barang as mb')->where('mb.id_kb', '30');

        if($d_data->count() > 0){
            foreach($d_data->orderBy('mb.kode_barang', 'asc')->get() as $d){
                $arr[] = array('id' => $d->id,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang);
            }
        }else{
            $arr = array();
        }

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

	function tambah($id){
		$d_data = DB::table('pro_barangkeluar AS pb')->join('ms_proses AS mp', 'pb.id_proses', 'mp.id_proses')->leftjoin('ms_rekanan AS mr', 'pb.id_rekanan', 'mr.id_rekanan')->leftjoin('ms_pegawai as mpe','pb.id_pegawai','mpe.id_pegawai')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->leftjoin('pem_workorder as pw', 'pb.id_workorder', 'pw.id_workorder')->leftjoin('pem_workorder_barang as pwb', 'pwb.id_workorder_barang', 'pb.id_workorder_barang')->select(DB::raw('pb.*, mp.nama_proses AS nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai as nama_pegawai, mrk.nama nama_kelompok, pw.nomor no_workorder, pwb.no_workorder no_workorder_barang'))->where('pb.id_barangkeluar', $id);

	   //	$d_barang = barangkeluar_barang($id);
        $d_barang = DB::table('view_barangkeluar_barang')->where('id_barangkeluar', $id);

        $d_warna = DB::table('ms_warnakomposisi')->OrderBy('nama', 'asc')->get();
		$data['data2'] = $d_data->first();
		$data['data'] = $this->data(array());
		$data['barang'] = $d_barang->get();
		$data['gudang'] = DB::table('ms_gudang')->orderBy('nama_gudang', 'asc')->get();
		$data['kualitas'] = DB::table('ms_kualitas')->get();
        $data['satuan'] = DB::table('ms_satuan')->orderBy('nama_satuan', 'asc')->get();
        $data['warna'] = $d_warna;
        $data['sisa'] = $this->get_sisa($id);

       // print_r($data['sisa']);
		return view('produksi.BarangmasukTambah')->with('data', $data);
        
	}

    function edit($id){

        $d_masuk = DB::table('pro_barangkeluar AS pbk')->leftJoin('ms_rekanan AS mr', 'pbk.id_rekanan', 'mr.id_rekanan')->leftJoin('ms_pegawai AS mp', 'pbk.id_pegawai', 'mp.id_pegawai')->join('ms_proses AS mpr', 'pbk.id_proses', 'mpr.id_proses')->join('pro_barangmasuk AS pbm', 'pbk.id_barangkeluar', 'pbm.id_barangkeluar')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->leftjoin('pem_workorder as pw', 'pbk.id_workorder', 'pw.id_workorder')->leftjoin('pem_workorder_barang as pwb', 'pwb.id_workorder_barang', 'pbk.id_workorder_barang')->select(DB::raw('pbk.id_barangkeluar, pbm.id_barangmasuk, pbm.tanggal tanggal_selesai, pbk.tanggal tanggal_ambil, pbk.tanggal_perkiraan, pbk.id_rekanan, mr.nama nama_rekanan, pbk.id_pegawai, mp.nama_pegawai, mpr.id_proses, mpr.nama_proses, mrk.nama nama_kelompok, pw.nomor no_workorder, pwb.no_workorder no_workorder_barang, pbk.status, pbk.status_workorder, pbk.id_lokasi'))->where('pbm.id_barangmasuk', $id);

        

        //    $d_barang = barangkeluar_barang($d_masuk->first()->id_barangkeluar);
        $d_barang = DB::table('view_barangkeluar_barang')->where('id_barangkeluar', $d_masuk->first()->id_barangkeluar);

        $d_warna = DB::table('ms_warnakomposisi')->OrderBy('nama', 'asc')->get();
        // $data['data2'] = $d_data->first();
        $data['data'] = $this->data(array());
        $data['barang'] = $d_barang->get();
        $data['gudang'] = DB::table('ms_gudang')->get();
        $data['kualitas'] = DB::table('ms_kualitas')->get();
        $data['satuan'] = DB::table('ms_satuan')->orderBy('nama_satuan', 'asc')->get();
        
        $data['warna'] = $d_warna;
        $data['masuk'] = $d_masuk->first();
        $data['sisa'] = $this->get_sisa($d_masuk->first()->id_barangkeluar);
        /*print_r($d_data->first());*/
        return view('produksi.BarangmasukEdit')->with('data', $data);
    }

	function detail($id){
		$d_masuk = DB::table('pro_barangkeluar AS pbk')->leftJoin('ms_rekanan AS mr', 'pbk.id_rekanan', 'mr.id_rekanan')->leftJoin('ms_pegawai AS mp', 'pbk.id_pegawai', 'mp.id_pegawai')->join('ms_proses AS mpr', 'pbk.id_proses', 'mpr.id_proses')->join('pro_barangmasuk AS pbm', 'pbk.id_barangkeluar', 'pbm.id_barangkeluar')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->leftjoin('pem_workorder as pw', 'pbk.id_workorder', 'pw.id_workorder')->leftjoin('pem_workorder_barang as pwb', 'pwb.id_workorder_barang', 'pbk.id_workorder_barang')->select(DB::raw('pbk.id_barangkeluar, pbm.id_barangmasuk, pbm.tanggal tanggal_selesai, pbk.tanggal tanggal_ambil, pbk.tanggal_perkiraan, pbk.id_rekanan, mr.nama nama_rekanan, pbk.id_pegawai, mp.nama_pegawai, mpr.id_proses, mpr.nama_proses, mrk.nama nama_kelompok, pw.nomor no_workorder, pwb.no_workorder no_workorder_barang, pbk.status, pbk.status_workorder, pbm.noAuto, pbm.noAuto_ext'))->where('pbm.id_barangmasuk', $id);

       // $d_barang = barangkeluar_barang($d_masuk->first()->id_barangkeluar);
        $d_barang = DB::table('view_barangkeluar_barang')->where('id_barangkeluar', $d_masuk->first()->id_barangkeluar);
    
        $d_warna = DB::table('ms_warnakomposisi')->OrderBy('nama', 'asc')->get();
        
        $data['data'] = $this->data(array());
        $data['barang'] = $d_barang->get();
        $data['gudang'] = DB::table('ms_gudang')->get();
        $data['kualitas'] = DB::table('ms_kualitas')->get();
        $data['satuan'] = DB::table('ms_satuan')->orderBy('nama_satuan', 'asc')->get();
        
        $data['warna'] = $d_warna;
        $data['masuk'] = $d_masuk->first();

        // print_r($d_setor->get());
		return view('produksi.BarangmasukDetail')->with('data',$data);
	}

	function simpan(Request $request){
        //   error_reporting(0);
		
        $barangkeluar = $request->get('barangkeluar');
        $id_barangkeluar = $request->get('id_barangkeluar');
		$id_barangmasuk = $request->get('id_barangmasuk');
		$data['id_barangkeluar'] = $request->get('barangkeluar');
        $data['tanggal'] = date_format(date_create($request->get('tanggal')),'Y-m-d');
        $data['tanggal_full'] = tgl_full($request->get('tanggal'), '5');
        // $data['noAuto'] = $request->get('nomor');
        // $data['noAuto_ext'] = $request->get('nomor_ext');

        $data = $data+$this->validasi_nomor($id_barangmasuk, $request->get('nomor'), $request->get('nomor_ext'), $request->get('id_lokasi'));
        $data['id_user'] = $this->session['kode_user'];
    


        $table_id = $request->get('tabel_idtable');
        $table_barang = $request->get('tabel_idbarang');
        $table_gudang = $request->get('tabel_idgudang');
        $table_upah = $request->get('tabel_idupah');
        $table_jumlah = $request->get('tabel_jumlah');
        $table_satuan = $request->get('tabel_idsatuan');
        $table_kualitas = $request->get('tabel_idkualitas');
        $table_dudul = $request->get('tabel_iddudul');
        $table_label = $request->get('tabel_idlabel');
        $table_kemasan = $request->get('tabel_idkemasan');
        $table_kemasansatuan = $request->get('tabel_idkemasansatuan');
        $table_kemasansepuluh = $request->get('tabel_idkemasansepuluh');
        $table_kemasanseratus = $request->get('tabel_idkemasanseratus');
        $table_cap = $request->get('tabel_idcap');
        $table_kualitasproses = $request->get('tabel_idkualitasproses');
        $table_kualitasperiksa = $request->get('tabel_idkualitasperiksa');
        $table_keterangan = $request->get('tabel_keterangan');
        $table_jahit = $request->get('tabel_jahit');

        //---- Perubahan Satuan
        $table_jumlah_perubahan = $request->get('tabel_jumlah_perubahan');
        $table_satuan_perubahan = $request->get('tabel_idsatuan_perubahan');

        $table_corak = "";
        if($request->get('tabel_idcorak') != 0 || $request->get('tabel_idcorak') != ''){
            $table_corak = $request->get('tabel_idcorak');
        }
        $table_kualitas = "";
        if($request->get('tabel_idkualitas') != 0 || $request->get('tabel_idkualitas') != ''){
            $table_kualitas = $request->get('tabel_idkualitas');
        }
        $table_motif = "";
        
        /*if($request->get('tabel_idmotif') != 0 || $request->get('tabel_idmotif') != '' ){
            //$table_motif = $request->get('tabel_idmotif');
            if($c_motif->id_proses == 24){
                $table_motif = $c_motif->id_motif;    
            }else if($c_motif->id_proses == 37){
                $table_motif = $request->get('tabel_idmotif');
            }else{
                $table_motif = $request->get('tabel_idmotif');
            }
            
        }*/
        $table_motif = $request->get('tabel_idmotif');

        $table_komposisi = "";
        if($request->get('tabel_idkomposisi') != 0 || $request->get('tabel_idkomposisi') != ''){
            $table_komposisi = $request->get('tabel_idkomposisi');
        }

        $table_corak = "";
        if($request->get('tabel_idcorak') != 0 || $request->get('tabel_idcorak') != ''){
            $table_corak = $request->get('tabel_idcorak');
        }

        $status_proses = $request->get('status_proses');
        if($status_proses == 1){
            $update['status'] = '2';
        }else{
            $update['status'] = '3';
        }

        $update['status_workorder'] = $request->get('status_workorder');
        
        if($id_barangmasuk == ""){ 
            $data['status'] = "0";

           // $update['status'] = "2";
            $id = DB::table("pro_barangmasuk")->insertGetId($data);
            $arr_barang = array();

            for($i=0; $i < count($table_barang); $i++){                
                $barang['id_barangmasuk']=$id;
                $barang['id_barang']=$table_barang[$i];
                $barang['id_gudang']=$table_gudang[$i];
                $barang['jumlah'] = $table_jumlah[$i];
                // $barang['jumlah_konversi'] = $table_jumlah_konv[$i];
                // $barang['id_upah'] = $table_upah[$i];
                $barang['id_kualitas'] = $table_kualitas[$i];

                /*if($c_motif->id_proses == 24){
                    $barang['id_motif'] = $table_motif;    
                }else if($c_motif->id_proses == 37){
                    $barang['id_motif'] = $table_motif[$i];
                }else{
                    $barang['id_motif'] = $table_motif[$i];
                }*/

                 $barang['id_motif'] = $table_motif[$i];

                
                // $barang['total_upah'] = $table_total[$i];
                $barang['id_komposisi'] = $table_komposisi[$i];
                $barang['id_satuan'] = $table_satuan[$i];
                $barang['id_motif_barang'] = $table_corak[$i];
                $barang['id_kualitas'] = $table_kualitas[$i];
                $barang['id_kualitas_dudul'] = $table_dudul[$i];
                $barang['id_label'] = $table_label[$i];
                $barang['id_kemasan'] = $table_kemasan[$i];
                $barang['id_kemasansatuan'] = $table_kemasansatuan[$i];
                $barang['id_kemasansepuluh'] = $table_kemasansepuluh[$i];
                $barang['id_kemasanseratus'] = $table_kemasanseratus[$i];
                $barang['id_cap'] = $table_cap[$i];
                $barang['id_kualitas_proses'] = $table_kualitasproses[$i];
                $barang['id_kualitas_periksa'] = $table_kualitasperiksa[$i];
                $barang['keterangan'] = $table_keterangan[$i];
                $barang['id_jahit'] = $table_jahit[$i];

                //--- perubahan satuan
                $barang['jumlah_rencana'] = $table_jumlah_perubahan[$i];
                $barang['id_perubahansatuan'] = $table_satuan_perubahan[$i];

               DB::table('pro_barangmasuk_barang')->insert($barang); 
               $arr_barang[] = $barang;
            // print_r($barang);               
            }


            get_barangproduksi('0', $id);

            DB::table('pro_barangkeluar')->where('id_barangkeluar',$request->get('barangkeluar'))->update($update);
        }else{
           // DB::table('pro_barangkeluar')->where('id_barangkeluar',$id_barangkeluar)->update($data);
            $id_del = array();
            $id_store = array();
            // $d_barang = DB::table('pro_barangkeluar_barang')->where('id_barangkeluar',$id_barangkeluar); (tak tutup amergo ora kenek di edit)
            $d_barang = DB::table('pro_barangmasuk_barang')->where('id_barangmasuk',$id_barangmasuk);
            foreach($d_barang->get() as $d){
                $id_store[] = $d->id;
            }

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


            for($i=0; $i < count($table_barang); $i++){                
                
                $barang['id_barangmasuk']=$id_barangmasuk;
                $barang['id_barang']=$table_barang[$i];
                $barang['id_gudang']=$table_gudang[$i];
                $barang['jumlah'] = $table_jumlah[$i];
                // $barang['jumlah_konversi'] = $table_jumlah_konv[$i];
                // $barang['id_upah'] = $table_upah[$i];
                $barang['id_kualitas'] = $table_kualitas[$i];
                
                // if($c_motif->id_proses == 24){
                //     $barang['id_motif'] = $table_motif;    
                // }else if($c_motif->id_proses == 37){
                //     $barang['id_motif'] = $table_motif[$i];
                // }
                $barang['id_motif'] = $table_motif[$i];
                // $barang['total_upah'] = $table_total[$i];
                $barang['id_komposisi'] = $table_komposisi[$i];
                $barang['id_satuan'] = $table_satuan[$i];
                $barang['id_motif_barang'] = $table_corak[$i];
                $barang['id_kualitas'] = $table_kualitas[$i];
                $barang['id_kualitas_dudul'] = $table_dudul[$i];
                $barang['id_label'] = $table_label[$i];
                $barang['id_kemasan'] = $table_kemasan[$i];
                $barang['id_kemasansatuan'] = $table_kemasansatuan[$i];
                $barang['id_kemasansepuluh'] = $table_kemasansepuluh[$i];
                $barang['id_kemasanseratus'] = $table_kemasanseratus[$i];
                $barang['id_cap'] = $table_cap[$i];
                $barang['id_kualitas_proses'] = $table_kualitasproses[$i];
                $barang['id_kualitas_periksa'] = $table_kualitasperiksa[$i];
                $barang['keterangan'] = $table_keterangan[$i];

                //--- perubahan satuan
                $barang['jumlah_rencana'] = $table_jumlah_perubahan[$i];
                $barang['id_perubahansatuan'] = $table_satuan_perubahan[$i];
                $barang['id_jahit'] = $table_jahit[$i];

                if($table_id[$i] == ''){
                    echo 'tambah';
                    DB::table('pro_barangmasuk_barang')->insert($barang);
                }else if($table_id != ''){
                     echo 'edit';
                    DB::table('pro_barangmasuk_barang')->where(array('id' => $table_id[$i]))->update($barang);
                }
                // print_r($barang);         
            }

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

            get_barangproduksi('0', $id_barangmasuk);
            get_barangproduksi('1', $id_barangmasuk);
            
            Db::table('pro_barangmasuk')->where('id_barangmasuk', $id_barangmasuk)->update($data);
            Db::table('pro_barangkeluar')->where('id_barangkeluar',$request->get('barangkeluar'))->update($update);
        }
        
         return redirect('barangmasuk');
       // print_r($id_del);

	}

    function simpan_tenun(Request $request){
       
        $status_beem = $request->get('status_proses');
        if($status_beem == 1){
            $update['status'] = '2';
            $update['status_beem'] = '1';
            $update['tanggal_beem'] = date('Y-m-d');
            $update['status_workorder'] = '1';
        }else{
            $update['status'] = '3';
            $update['status_beem'] = '0';
            $update['tanggal_beem'] = '';
            $update['status_workorder'] = '0';
        }

        // --- Ambil id barang keluar

        $where['id_rekanan'] = request()->get('id_rekanan');
        $where['id_pegawai'] = request()->get('id_pegawai');
        $where['id_workorder_barang'] = request()->get('id_workorder_barang');

        $get_idbarangkeluar = $this->get_barangkeluar($where);
        if(count($get_idbarangkeluar) > 0){
            foreach ($get_idbarangkeluar as $d) {
                DB::table('pro_barangkeluar')->where('id_barangkeluar', $d['id_barangkeluar'])->update($update);
            }
        }

        // --- Akhir Ambil id barang keluar

        $id_barangmasuk = $request->get('id_barangmasuk');
        $data['tanggal'] = date_format(date_create($request->get('tanggal')),'Y-m-d');
        // $data['noAuto'] = $request->get('nomor');
        // $data['noAuto_ext'] = $request->get('nomor_ext');
        $data['id_user'] = $this->session['kode_user'];
        $data['tanggal_full'] = tgl_full($request->get('tanggal'), '5');

        $data = $data+$this->validasi_nomor($id_barangmasuk, $request->get('nomor'), $request->get('nomor_ext'), $request->get('id_lokasi'));

        $table_id = $request->get('tabel_id');
        $table_idtable = $request->get('tabel_idtable');
        $table_barang = $request->get('tabel_idbarang');
        $table_gudang = $request->get('tabel_idgudang');
        $table_jumlah = $request->get('tabel_jumlah');
        $table_satuan = $request->get('tabel_idsatuan');
        $table_kualitas = $request->get('tabel_idkualitas');
        $table_motif = $request->get('tabel_idmotif');
        $table_panjang = $request->get('tabel_panjang');
        $table_lebar = $request->get('tabel_lebar');
        $table_pick = $request->get('tabel_pick');
        $table_idkualitasproses = $request->get('tabel_idkualitas_proses');
        $table_idkomposisi = $request->get('tabel_idkomposisi');

        //
        
        if($id_barangmasuk == ""){ 
            $data['status'] = "0";
            
            if(count($get_idbarangkeluar) > 0){
                    $data['id_barangkeluar'] = $get_idbarangkeluar[0]['id_barangkeluar'];

                    $id = DB::table("pro_barangmasuk")->insertGetId($data);

                    for($i=0; $i < count($table_barang); $i++){                
                        $barang['id_barangmasuk']=$id;
                        $barang['id_barang']=$table_barang[$i];
                        $barang['id_gudang']=$table_gudang[$i];
                        $barang['jumlah'] = $table_jumlah[$i];
                        $barang['id_kualitas'] = $table_kualitas[$i];
                        $barang['id_motif'] = $table_motif[$i];
                        $barang['id_satuan'] = $table_satuan[$i];
                        $barang['panjang'] = $table_panjang[$i];
                        $barang['lebar'] = $table_lebar[$i];
                        $barang['pick'] = $table_pick[$i];
                        $barang['id_komposisi'] = $table_idkomposisi[$i];
                        $barang['id_kualitas_proses'] = $table_idkualitasproses[$i];
                        
                        DB::table('pro_barangmasuk_barang')->insert($barang);
                    }
            }
            
        }else{
           $id_store = array();
            $id_del = array();
            $d_barang = DB::table('pro_barangmasuk_barang')->where('id_barangmasuk',$id_barangmasuk);
            foreach($d_barang->get() as $d){
                $id_store[] = $d->id;
            }

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


            for($i=0; $i < count($table_barang); $i++){                
                
                
                $barang['id_barang']=$table_barang[$i];
                $barang['id_gudang']=$table_gudang[$i];
                $barang['jumlah'] = $table_jumlah[$i];
                $barang['id_kualitas'] = $table_kualitas[$i];
                $barang['id_motif'] = $table_motif[$i];    
                $barang['id_satuan'] = $table_satuan[$i];
                $barang['panjang'] = $table_panjang[$i];
                $barang['lebar'] = $table_lebar[$i];
                $barang['pick'] = $table_pick[$i];
                $barang['id_komposisi'] = $table_idkomposisi[$i];
                $barang['id_kualitas_proses'] = $table_idkualitasproses[$i];

                if($table_idtable[$i] == ''){
                    $barang['id_barangmasuk']=$id_barangmasuk;

                    DB::table('pro_barangmasuk_barang')->insert($barang);
                //    echo 'tambah';
                }else if($table_idtable[$i] != ''){
                //     echo 'edit';
                    DB::table('pro_barangmasuk_barang')->where(array('id' => $table_idtable[$i]))->update($barang);
                }
                
            }

            if(count($id_del) > 0){
            //    print_r($id_del);
                DB::table('pro_barangmasuk_barang')->whereIn('id', $id_del)->delete();
            }

        }
        
        return redirect('barangmasuk');

    }

    function simpan_dudulan(Request $request){
        $update['status'] = '2';
        DB::table('pro_barangkeluar')->where('id_barangkeluar',$request->get('barangkeluar'))->update($update);

        $barangkeluar = $request->get('barangkeluar');
        $id_barangkeluar = $request->get('id_barangkeluar');
        $id_barangmasuk = $request->get('id_barangmasuk');
        $data['id_barangkeluar'] = $request->get('barangkeluar');
        $data['tanggal'] = date_format(date_create($request->get('tanggal')),'Y-m-d');


        $table_id = $request->get('tabel_idtable');
        $table_barang = $request->get('tabel_idbarang');
        $table_gudang = $request->get('tabel_idgudang');
        $table_jumlah = $request->get('tabel_jumlah');
        $table_satuan = $request->get('tabel_idsatuan');
        $table_kualitas = $request->get('tabel_idkualitas');
        $table_motif = $request->get('tabel_idmotif');
        $table_kual = $request->get('tabel_idkual');

        if($id_barangmasuk == ""){ 
            $data['status'] = "0";
        
           $id = DB::table("pro_barangmasuk")->insertGetId($data);

            for($i=0; $i < count($table_barang); $i++){                
                $barang['id_barangmasuk']=$id;
                $barang['id_barang']=$table_barang[$i];
                $barang['id_gudang']=$table_gudang[$i];
                $barang['jumlah'] = $table_jumlah[$i];
                $barang['id_kualitas_dudul'] = $table_kualitas[$i];
                $barang['id_motif'] = $table_motif[$i];
                $barang['id_kualitas'] = $table_kual[$i];
                
                
                $barang['id_satuan'] = $table_satuan[$i];
               DB::table('pro_barangmasuk_barang')->insert($barang); 
            //print_r($barang);               
            }
            
        }else{
           
            $id_del = array();
            $d_barang = DB::table('pro_barangmasuk_barang')->where('id_barangmasuk',$id_barangmasuk);
            foreach($d_barang->get() as $d){
                $id_store[] = $d->id;
            }

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


            for($i=0; $i < count($table_barang); $i++){                
                
                
                $barang['id_barang']=$table_barang[$i];
                $barang['id_gudang']=$table_gudang[$i];
                $barang['jumlah'] = $table_jumlah[$i];
                $barang['id_kualitas_dudul'] = $table_kualitas[$i];
                $barang['id_motif'] = $table_motif[$i];
                $barang['id_barangmasuk'] = $id_barangmasuk;
                $barang['id_kualitas'] = $table_kual[$i];
                $barang['id_satuan'] = $table_satuan[$i];
                if($table_id[$i] == ''){
                    DB::table('pro_barangmasuk_barang')->insert($barang);
                //    echo 'tambah';
                }else if($table_id != ''){
                //    echo 'edit';
                    DB::table('pro_barangmasuk_barang')->where(array('id' => $table_id[$i]))->update($barang);
                }
                
            }

            if(count($id_del) > 0){
                print_r($id_del);
                DB::table('pro_barangmasuk_barang')->whereIn('id', $id_del)->delete();
            }

        }
        
        return redirect('barangmasuk');

    }

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

		$d_data = DB::table('ms_upah')->where('id_barang',$id);
		if($d_data->count() > 0){
			foreach($d_data->get() as $d){
			$arr[] = array('id_upah' => $d->id_upah,
							'nominal' => $d->nominal);
			}
		}else{
			$arr = array();
		}	

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

	function get_proses(Request $request){
    	$id = request()->get('id');
        $proses = request()->get('proses');

        $d_proses = DB::table('ms_proses')->where('id_proses', $proses)->first();
        $where = ["pb.id_barangkeluar = '$id'", "pbk.id_barangkeluar = '$id'"];
        if($proses == '25' || strtolower($d_proses->nama_proses) == 'medang'){
            $PDO = DB::connection('mysql')->getPdo();

            $query = $PDO->prepare("SELECT (@cnt := @cnt + 1) id, ppb.id_barangkeluar, pw.id_workorder, pw.nomor, pw.tanggal, pwk.id_workorder_ket, mmb.id_motif_barang, mmb.motif_keterangan, mb.id id_barang, mb.kode_barang, mb.nama_barang, pwk.jumlah, mw.id_komposisi, mw.nama nama_komposisi, ppb.keterangan
                FROM pem_workorder pw
                JOIN pem_workorder_ket pwk ON pw.id_workorder = pwk.id_workorder
                JOIN ms_barang mb ON pwk.id_barang = mb.id
                LEFT JOIN ms_motif_barang mmb ON pwk.keterangan = mmb.id_motif_barang
                JOIN pro_barangkeluar pbk ON pw.id_workorder = pbk.id_workorder
                JOIN pro_barangkeluar_barang ppb ON pbk.id_barangkeluar = ppb.id_barangkeluar
                LEFT JOIN ms_warnakomposisi mw ON mw.id_komposisi = ppb.warna
                JOIN (SELECT @cnt := 0) AS dummy
                WHERE pbk.id_barangkeluar = '$id'
                GROUP BY pbk.id_barangkeluar, pw.id_workorder, pwk.id_workorder_ket, mmb.id_motif_barang, mb.id  ");
            $query->execute();
            $d_data2 = $query->fetchAll((\PDO::FETCH_ASSOC));
        //    $d_data2 = DB::table('view_workorder_ket')->where('id_barangkeluar', $id);
            if(count($d_data2) > 0){
                foreach($d_data2 as $d){
                    $arr[] = ['id' => $d['id'],
                              'id_barang' => $d['id_barang'],
                              'kode_barang' => $d['kode_barang'],
                              'nama_barang' => $d['nama_barang'],
                              'id_komposisi' => $d['id_komposisi'],
                              'nama_komposisi' => $d['nama_komposisi'],
                              'jumlah' => $d['jumlah'],
                              'id_corak' => $d['id_motif_barang'],
                              'nama_corak' => $d['motif_keterangan'],
                               'id_motif' => '',
                              'nama_motif' => '',
                              'id_kualitas' => '',
                              'nama_kualitas' => '',
                              'id_kualitas_dudul' => '',
                              'kualitas_dudul' => '',
                              'keterangan' => $d['keterangan'],
                              'stok' => ''];
                }
            }else{
                // $data2 = DB::table('view_edit_keluar as v')->where('id', $id)->select(DB::raw('v.*, v.keluar as stok'));
                // if($data2->count() > 0){
                //     $arr = $data2->get();
                // }else{
                //     $arr = array();
                // }

                 $query = "SELECT B.*
                            FROM (
                            ".base_stok_masuk(1, $where)."
                            ) B WHERE B.id_barangkeluar = $id AND B.stok > 0 ORDER BY B.id_barangkeluar_barang";

                $arr = get_rawquery($query);
            }

        }else if($proses == '37' || strtolower($d_proses->nama_proses) == 'tenun'){
            $id_workorder_barang = DB::table('pro_barangkeluar')->where('id_barangkeluar', $id)->first()->id_workorder_barang;
            $data2 = DB::table('view_edit_keluar')->where('id_workorder_barang', $id_workorder_barang)->where('id_motif', '!=', '');

            if($data2->count() > 0){
                $arr = $data2->get();
            }else{
                $arr = array();
            }
        }else if($proses == '24' || strtolower($d_proses->nama_proses) == 'sekir'){
            $data2 = DB::table('view_edit_keluar as v')->leftJoin('pem_workorder_barang as pwb', 'v.id_workorder_barang', 'pwb.id_workorder_barang')->leftjoin('ms_motif as mm', 'pwb.motif', 'mm.id_motif')->select('v.*', 'mm.id_motif', 'v.keluar as stok')->where('id', $id);

            if($data2->count() > 0){
                $arr = $data2->get();
            }else{
                $arr = array();
            }
        }else if($proses == '22' || $proses == '94' || $proses = '97'){
            $query = "SELECT B.*
                FROM (
                ".str_replace("A.id_komposisi,", "", base_stok_masuk('', $where))."
                ) B WHERE B.id_barangkeluar = $id AND B.stok > 0 ORDER BY B.id_barangkeluar_barang";

            $arr = get_rawquery($query);
        }else if($proses == '60'){
            $where = ["pb.id_barangkeluar = '$id'", "pbk.id_barangkeluar = '$id'"];
            $query = "SELECT B.*
                FROM (
                ".str_replace(", A.id_motif", "", base_stok_masuk('', $where))."
                ) B WHERE B.id_barangkeluar = $id AND B.stok > 0 ORDER BY B.id_barangkeluar_barang";
            
            $arr = get_rawquery($query);
        }else if($proses = '97'){
            $query = "SELECT B.*
                FROM (
                ".str_replace("A.id_komposisi,", "", base_stok_masuk('', $where))."
                ) B WHERE B.id_barangkeluar = $id AND B.stok > 0 ORDER BY B.id_barangkeluar_barang";

            $arr = get_rawquery($query);
        } else{
            
            // $data2 = DB::table('view_edit_keluar')->where('id', $id);
            // if($data2->count() > 0){
            //     $arr = $data2->get();
            // }else{
            //     $arr = array();
            // }

            $query = "SELECT B.*
                FROM (
                ".base_stok_masuk('', $where)."
                ) B WHERE B.id_barangkeluar = $id AND B.stok > 0 ORDER BY B.id_barangkeluar_barang";

            $arr = get_rawquery($query);


        }


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

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

        $d_data = DB::table('ms_konversi AS mk')->join('ms_satuan AS ms', 'mk.satuan_awal', 'ms.id_satuan')->join('ms_satuan AS ms2', 'mk.satuan_akhir', 'ms2.id_satuan')->select(DB::raw('mk.* , ms.nama_satuan ns_awal, ms.satuan s_awal, ms2.nama_satuan ns_akhir, ms2.satuan s_akhir'))->where('kode', '=', $id);
        if($d_data->count() > 0){
            foreach($d_data->get() as $d){
                $arr[] = array('id_konversi' => $d->id_konversi,
                            'satuan_awal' => $d->ns_awal,
                            'satuan_akhir' => $d->ns_akhir,
                            'nilai_awal' => $d->nilai_awal,
                            'nilai_akhir' => $d->nilai_akhir);
            }
        }else{
            $arr = array();
        }

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

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



    //    $d_data = DB::table('pro_barangmasuk AS pb')->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_satuan as ms2','pbb.id_perubahansatuan','ms2.id_satuan')->leftJoin('ms_motif_barang as mmb', 'pbb.id_motif_barang', 'mmb.id_motif_barang')->leftJoin('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')->leftjoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')->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_barang as mb7', 'pbb.id_cap', 'mb7.id')->select(DB::raw('pbb.id, pbb.id_barang, mb.kode_barang, mb.nama_barang, mg.id_gudang, mg.nama_gudang, ms.id_satuan, ms.nama_satuan, CASE WHEN ms2.id_satuan IS NULL THEN "" ELSE ms2.id_satuan END AS id_perubahansatuan, CASE WHEN ms2.id_satuan IS NULL THEN "" ELSE ms2.nama_satuan END AS nama_perubahansatuan, CASE WHEN pbb.jumlah_rencana IS NULL OR pbb.jumlah_rencana = 0 THEN "" ELSE pbb.jumlah_rencana END AS jumlah_rencana, pbb.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, sum(pbb.jumlah) jumlah, sum(pbb.jumlah_rencana) jumlah_rencana, 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 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 pbb.id_kualitas_dudul = 0 THEN 0 ELSE pbb.id_kualitas_dudul END AS id_kualitas_dudul, CASE WHEN pbb.id_kualitas_dudul = 0 THEN 0 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, CASE WHEN pbb.id_cap = 0 THEN "" ELSE pbb.id_cap END AS id_cap, CASE WHEN mb7.nama_barang IS NULL THEN "" ELSE mb7.nama_barang END AS nama_cap, pbb.id_kualitas_proses, pbb.id_kualitas_periksa'))->where('pb.id_barangmasuk', $id)->groupBy('pbb.id', 'pbb.id_barang', 'pbb.id_komposisi', 'mmb.id_motif_barang', 'mg.id_gudang');

            $d_data = DB::table('view_edit_masuk as v')->where('v.id_barangmasuk', $id)->join("pro_barangmasuk_barang AS pbp", "v.id", "pbp.id");

        if($d_data->count() > 0){
            foreach($d_data->get() as $d){
                $arr[] = array('id_masuk' => $d->id,
                                'id_barang' => $d->id_barang,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang,
                                'id_komposisi' => $d->id_komposisi,
                                'nama_komposisi' => $d->nama_komposisi,
                                'id_satuan' => $d->id_satuan,
                                'nama_satuan' => $d->nama_satuan,
                                'id_satuanperubahan' => $d->id_perubahansatuan,
                                'nama_satuanperubahan' => $d->nama_perubahansatuan,
                                'id_gudang' => $d->id_gudang,
                                'nama_gudang' => $d->nama_gudang,
                                'jumlah' => $d->jumlah,
                                'jumlah_rencana' => $d->jumlah_rencana,
                                'id_corak' => $d->id_corak,
                                'nama_corak' => $d->nama_corak,
                                'id_motif' => $d->id_motif,
                                'nama_motif' => $d->nama_motif,
                                'id_kualitas' => $d->id_kualitas,
                                'nama_kualitas' => $d->nama_kualitas,
                                'id_kualitas_dudul' => $d->id_kualitas_dudul,
                                'kualitas_dudul' => $d->kualitas_dudul,
                                'id_label' => $d->id_label,
                                'nama_label' => $d->nama_label,
                                'id_cap' => $d->id_cap,
                                'nama_cap' => $d->nama_cap,
                                'id_kemasan' => $d->id_kemasan,
                                'nama_kemasan' => $d->nama_kemasan,
                                'id_kemasansatuan' => $d->id_kemasansatuan,
                                'nama_kemasansatuan' => $d->nama_kemasansatuan,
                                'id_kemasansepuluh' => $d->id_kemasansepuluh,
                                'nama_kemasansepuluh' => $d->nama_kemasansepuluh,
                                'id_kemasanseratus' => $d->id_kemasanseratus,
                                'nama_kemasanseratus' => $d->nama_kemasanseratus,
                                'id_kualitas_proses' => $d->id_kualitas_proses,
                                'id_kualitas_periksa' => $d->id_kualitas_periksa,
                                'keterangan' => $d->keterangan,
                                'id_jahit' => ($d->id_jahit == 0) ? "":$d->id_jahit);
            }
        }else{
            $arr = array();
        }

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

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

        
        $d_data = DB::table('view_edit_masuk as v')->join('pro_barangmasuk_barang AS pbb', 'v.id', 'pbb.id')->where('v.id_barangmasuk', $id)->select(DB::raw("v.*, pbb.panjang, pbb.lebar, pbb.pick"));
        return response()->json($d_data->get());
    }

    // ---------------------- Proses Lanjutan --------------------------------------------


    function proses_lanjutan(Request $request){

    //    print_r(request()->all());
        $id_keluar = ( request()->get('barangkeluar') == '') ?  request()->get('id_barangkeluar'): request()->get('barangkeluar');

        $data['detail'] = array('id_barangmasuk' => request()->get('id_barangmasuk'),
                                'id_barangkeluar' => request()->get('id_barangkeluar'),
                                'barangkeluar' => request()->get('barangkeluar'),
                                'tanggal' => request()->get('tanggal'),
                                'tanggal_pengambilan' => request()->get('tanggal_pengambilan'),
                                'tanggal_perkiraan' => request()->get('tanggal_perkiraan'),
                                'rekanan' => (request()->get('rekanan') == '0') ? '-':request()->get('rekanan'),
                                'pegawai' => (request()->get('pegawai') == '0') ? '-':request()->get('pegawai'),
                                'proses' => request()->get('proses'));

        $d_barang = DB::table('pro_barangkeluar_barang AS pbb')->join('ms_barang AS mb', 'pbb.id_barang', 'mb.id')->join('ms_gudang AS mg', 'pbb.id_gudang', 'mg.id_gudang')->join('ms_warnakomposisi as mwk', 'pbb.warna', 'mwk.id_komposisi')->select(DB::raw('pbb.*, mb.kode_barang, mb.nama_barang, mg.id_gudang, mg.nama_gudang, mwk.id_komposisi, mwk.nama nama_komposisi'))->where('pbb.id_barangkeluar', $id_keluar);
        $data['barang'] = $d_barang->get();
        $setor = array();

        $tabel_idtabel = request()->get('tabel_idtabel');
        $tabel_id = request()->get('tabel_id');
        $tabel_idbarang = request()->get('tabel_idbarang');
        $tabel_idgudang = request()->get('tabel_idgudang');
        $tabel_jumlah = request()->get('tabel_jumlah');
        $tabel_komposisi = request()->get('tabel_komposisi');
        $tabel_idkomposisi = request()->get('tabel_idkomposisi');
        $tabel_idsatuan = request()->get('tabel_idsatuan');
        $tabel_kode = request()->get('tabel_kode');
        $tabel_nama = request()->get('tabel_nama');
        $tabel_namasatuan = request()->get('tabel_namasatuan');
        $tabel_namagudang = request()->get('tabel_namagudang');

        $jum = count($tabel_idbarang);

        
        if(request()->get('id_barangmasuk') == ''){
            for($i = 0; $i < $jum; $i++){
                $setor[] = array('tabel_idtabel' => $tabel_idtabel[$i],
                                'tabel_id' => $tabel_id[$i],
                                'tabel_idbarang' => $tabel_idbarang[$i],
                                'tabel_idgudang' => $tabel_idgudang[$i],
                                'tabel_jumlah' => $tabel_jumlah[$i],
                                'tabel_komposisi' => $tabel_komposisi[$i],
                                'tabel_idkomposisi' => $tabel_idkomposisi[$i],
                                'tabel_idsatuan' => $tabel_idsatuan[$i],
                                'tabel_kode' => $tabel_kode[$i],
                                'tabel_nama' => $tabel_nama[$i],
                                'tabel_namasatuan' => $tabel_namasatuan[$i],
                                'tabel_namagudang' => $tabel_namagudang[$i]);
            }
        }else{

        }

        $data['setor'] = $setor;

       // print_r($setor);
        return view('produksi.BarangMasukLanjutan')->with('data', $data);
    }

    function proses_lanjutan_kualitas(Request $request){

        $id_keluar = ( request()->get('barangkeluar') == '') ?  request()->get('id_barangkeluar'): request()->get('barangkeluar');

        $data['detail'] = array('id_barangmasuk' => request()->get('id_barangmasuk'),
                                'id_barangkeluar' => request()->get('id_barangkeluar'),
                                'barangkeluar' => request()->get('barangkeluar'),
                                'tanggal' => request()->get('tanggal'),
                                'tanggal_pengambilan' => request()->get('tanggal_pengambilan'),
                                'tanggal_perkiraan' => request()->get('tanggal_perkiraan'),
                                'rekanan' => (request()->get('rekanan') == '0') ? '-':request()->get('rekanan'),
                                'pegawai' => (request()->get('pegawai') == '0') ? '-':request()->get('pegawai'),
                                'proses' => request()->get('proses'),
                                'no_workorder' => request()->get('no_workorder'),
                                'no_workorder_barang' => request()->get('no_workorder_barang'),
                                'satuan_proses' => request()->get('status_proses'),
                                'status_workorder' => request()->get('status_workorder'));

       $d_barang = DB::table('pro_barangkeluar_barang AS pbb')->join('ms_barang AS mb', 'pbb.id_barang', 'mb.id')->join('ms_gudang AS mg', 'pbb.id_gudang', 'mg.id_gudang')->leftjoin('ms_warnakomposisi as mwk', 'pbb.warna', 'mwk.id_komposisi')->leftJoin('ms_motif_barang as mmb', 'pbb.id_motif_barang', 'mmb.id_motif_barang')->leftJoin('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')->leftjoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')->select(DB::raw('pbb.*, mb.kode_barang, mb.nama_barang, mg.id_gudang, mg.nama_gudang, mwk.id_komposisi, mwk.nama nama_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 = 0 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'))->where('pbb.id_barangkeluar', $id_keluar);
        $data['barang'] = $d_barang->get();
        $data['barangkeluar'] = DB::table('pro_barangkeluar')->where('id_barangkeluar', $id_keluar)->first();
        $data['motif'] = DB::table('view_tenun_barangmasuk')->where('id_barangkeluar', $id_keluar)->first();
        $setor = array();

        $tabel_idtabel = request()->get('tabel_idtable');
        $tabel_id = request()->get('tabel_id');
        $tabel_idbarang = request()->get('tabel_idbarang');
        $tabel_idgudang = request()->get('tabel_idgudang');
        $tabel_jumlah = request()->get('tabel_jumlah');
        $tabel_komposisi = request()->get('tabel_komposisi');
        $tabel_idkomposisi = request()->get('tabel_idkomposisi');
        $tabel_idsatuan = request()->get('tabel_idsatuan');
        $tabel_kode = request()->get('tabel_kode');
        $tabel_nama = request()->get('tabel_nama');
        $tabel_namasatuan = request()->get('tabel_namasatuan');
        $tabel_namagudang = request()->get('tabel_namagudang');

        $jum = count($tabel_idbarang);

        
        if(request()->get('id_barangmasuk') == ''){
            for($i = 0; $i < $jum; $i++){
                $setor[] = array('tabel_idtabel' => $tabel_idtabel[$i],
                                'tabel_id' => $tabel_id[$i],
                                'tabel_idbarang' => $tabel_idbarang[$i],
                                'tabel_idgudang' => $tabel_idgudang[$i],
                                'tabel_jumlah' => $tabel_jumlah[$i],
                                'tabel_komposisi' => $tabel_komposisi[$i],
                                'tabel_idkomposisi' => $tabel_idkomposisi[$i],
                                'tabel_idsatuan' => $tabel_idsatuan[$i],
                                'tabel_kode' => $tabel_kode[$i],
                                'tabel_nama' => $tabel_nama[$i],
                                'tabel_namasatuan' => $tabel_namasatuan[$i],
                                'tabel_namagudang' => $tabel_namagudang[$i]);
            }
        }else{
            for($i = 0; $i < $jum; $i++){
                $setor[] = array('tabel_idtabel' => $tabel_idtabel[$i],
                                'tabel_id' => $tabel_id[$i],
                                'tabel_idbarang' => $tabel_idbarang[$i],
                                'tabel_idgudang' => $tabel_idgudang[$i],
                                'tabel_jumlah' => $tabel_jumlah[$i],
                                'tabel_komposisi' => $tabel_komposisi[$i],
                                'tabel_idkomposisi' => $tabel_idkomposisi[$i],
                                'tabel_idsatuan' => $tabel_idsatuan[$i],
                                'tabel_kode' => $tabel_kode[$i],
                                'tabel_nama' => $tabel_nama[$i],
                                'tabel_namasatuan' => $tabel_namasatuan[$i],
                                'tabel_namagudang' => $tabel_namagudang[$i]);
            }
        }

        $data['setor'] = $setor;

    //   print_r($setor);
        return view('produksi.BarangMasukKualitas')->with('data', $data);
    }

    function proses_lanjutan_dudulan(Request $request){

        $id_keluar = ( request()->get('barangkeluar') == '') ?  request()->get('id_barangkeluar'): request()->get('barangkeluar');

        $data['detail'] = array('id_barangmasuk' => request()->get('id_barangmasuk'),
                                'id_barangkeluar' => request()->get('id_barangkeluar'),
                                'barangkeluar' => request()->get('barangkeluar'),
                                'tanggal' => request()->get('tanggal'),
                                'tanggal_pengambilan' => request()->get('tanggal_pengambilan'),
                                'tanggal_perkiraan' => request()->get('tanggal_perkiraan'),
                                'rekanan' => (request()->get('rekanan') == '0') ? '-':request()->get('rekanan'),
                                'pegawai' => (request()->get('pegawai') == '0') ? '-':request()->get('pegawai'),
                                'proses' => request()->get('proses'));

        $d_barang = DB::table('pro_barangkeluar_barang AS pbb')->join('ms_barang AS mb', 'pbb.id_barang', 'mb.id')->join('ms_gudang AS mg', 'pbb.id_gudang', 'mg.id_gudang')->leftjoin('ms_warnakomposisi as mwk', 'pbb.warna', 'mwk.id_komposisi')->leftJoin('ms_motif_barang as mmb', 'pbb.id_motif_barang', 'mmb.id_motif_barang')->leftJoin('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')->leftjoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')->select(DB::raw('pbb.*, mb.kode_barang, mb.nama_barang, mg.id_gudang, mg.nama_gudang, mwk.id_komposisi, mwk.nama nama_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 = 0 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'))->where('pbb.id_barangkeluar', $id_keluar);
        $data['barang'] = $d_barang->get();
        $data['barangkeluar'] = DB::table('pro_barangkeluar')->where('id_barangkeluar', $id_keluar)->first();
        $data['motif'] = DB::table('view_tenun_barangmasuk')->where('id_barangkeluar', $id_keluar)->first();
        $setor = array();

        $tabel_idtabel = request()->get('tabel_idtable');
        $tabel_id = request()->get('tabel_id');
        $tabel_idbarang = request()->get('tabel_idbarang');
        $tabel_idgudang = request()->get('tabel_idgudang');
        $tabel_jumlah = request()->get('tabel_jumlah');
        $tabel_komposisi = request()->get('tabel_komposisi');
        $tabel_idkomposisi = request()->get('tabel_idkomposisi');
        $tabel_idsatuan = request()->get('tabel_idsatuan');
        $tabel_kode = request()->get('tabel_kode');
        $tabel_nama = request()->get('tabel_nama');
        $tabel_namasatuan = request()->get('tabel_namasatuan');
        $tabel_namagudang = request()->get('tabel_namagudang');

        $jum = count($tabel_idbarang);

        
        if(request()->get('id_barangmasuk') == ''){
            for($i = 0; $i < $jum; $i++){
                $setor[] = array('tabel_idtabel' => $tabel_idtabel[$i],
                                'tabel_id' => $tabel_id[$i],
                                'tabel_idbarang' => $tabel_idbarang[$i],
                                'tabel_idgudang' => $tabel_idgudang[$i],
                                'tabel_jumlah' => $tabel_jumlah[$i],
                                'tabel_komposisi' => $tabel_komposisi[$i],
                                'tabel_idkomposisi' => $tabel_idkomposisi[$i],
                                'tabel_idsatuan' => $tabel_idsatuan[$i],
                                'tabel_kode' => $tabel_kode[$i],
                                'tabel_nama' => $tabel_nama[$i],
                                'tabel_namasatuan' => $tabel_namasatuan[$i],
                                'tabel_namagudang' => $tabel_namagudang[$i]);
            }
        }else{
            for($i = 0; $i < $jum; $i++){
                $setor[] = array('tabel_idtabel' => $tabel_idtabel[$i],
                                'tabel_id' => $tabel_id[$i],
                                'tabel_idbarang' => $tabel_idbarang[$i],
                                'tabel_idgudang' => $tabel_idgudang[$i],
                                'tabel_jumlah' => $tabel_jumlah[$i],
                                'tabel_komposisi' => $tabel_komposisi[$i],
                                'tabel_idkomposisi' => $tabel_idkomposisi[$i],
                                'tabel_idsatuan' => $tabel_idsatuan[$i],
                                'tabel_kode' => $tabel_kode[$i],
                                'tabel_nama' => $tabel_nama[$i],
                                'tabel_namasatuan' => $tabel_namasatuan[$i],
                                'tabel_namagudang' => $tabel_namagudang[$i]);
            }
        }

        $data['setor'] = $setor;

    //   print_r($d_barang->get());
        return view('produksi.BarangMasukDudulan')->with('data', $data);
    }

    function get_tenunedit(Request $request){
        $id_masuk = request()->get('id');

        $d_data = DB::table('pro_barangmasuk_barang as pbb')->join('ms_barang as mb', 'pbb.id_barang', 'mb.id')->join('ms_gudang as mg', 'pbb.id_gudang', 'mg.id_gudang')->leftjoin('ms_satuan as ms', 'pbb.id_satuan', 'ms.id_satuan')->leftjoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')->leftjoin('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')->leftjoin('ms_warnakomposisi as mwk', 'pbb.id_komposisi', 'mwk.id_komposisi')->leftJoin('ms_kualitas as mk2', 'pbb.id_kualitas_proses', 'mk2.id_kualitas')->select(DB::raw('pbb.id, mb.id id_barang, mb.kode_barang, mb.nama_barang, pbb.id_gudang, mg.nama_gudang, pbb.id_satuan, ms.nama_satuan, pbb.id_kualitas, mk.nama_kualitas, pbb.id_motif, mm.nama_motif, pbb.panjang, pbb.lebar, pbb.pick, pbb.jumlah, 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 pbb.id_kualitas_proses = 0 THEN "" ELSE mk2.id_kualitas END AS id_kualitas_proses, CASE WHEN pbb.id_kualitas_proses = 0 THEN "" ELSE mk2.nama_kualitas END AS nama_kualitas_proses '))->where('id_barangmasuk', $id_masuk);
        return response()->json($d_data->get());
    }

    function get_duduledit(Request $request){
        $id_masuk = request()->get('id');

        $d_data = DB::table('pro_barangmasuk as pbm')->join('pro_barangmasuk_barang as pbb', 'pbm.id_barangmasuk', 'pbb.id_barangmasuk')->join('ms_barang as mb', 'pbb.id_barang', 'mb.id')->join('ms_gudang as mg', 'pbb.id_gudang', 'mg.id_gudang')->join('ms_satuan as ms', 'pbb.id_satuan', 'ms.id_satuan')->join('ms_motif as mm', 'pbb.id_motif', 'mm.id_motif')->leftjoin('ms_kualitas as mk', 'pbb.id_kualitas', 'mk.id_kualitas')->select(DB::raw('pbm.id_barangmasuk, pbb.id, pbb.id_barang, mb.kode_barang, mb.nama_barang, mm.id_motif, mm.nama_motif, pbb.jumlah, pbb.id_satuan, ms.nama_satuan, pbb.id_gudang, mg.nama_gudang, 
            
            CASE WHEN mk.id_kualitas = 0 THEN "" ELSE mk.id_kualitas END AS id_kual, CASE WHEN mk.nama_kualitas IS NULL THEN "" ELSE mk.nama_kualitas END AS nama_kual,
            pbb.id_kualitas_dudul, CASE WHEN pbb.id_kualitas_dudul = 1 THEN "Baik" ELSE "Jelek" END AS nama_kualitas'))->where('pbm.id_barangmasuk', $id_masuk);

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

    function get_workorder_ket(Request $request){
        $id_keluar = request()->get('id');

        $d_data = DB::table('view_workorder_ket')->where('id_barangkeluar', $id_keluar);

        if($d_data->count() > 0){
            $arr = $d_data->get();
        }else{
            $arr = array();
        }

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

    function get_detail_kualitas(Request $request){
        $id_keluar = request()->get('id');
        $d_keluar = DB::table("pro_barangkeluar")->where('id_barangkeluar',$id_keluar)->first();
        $id_wo = "";
        if($d_keluar->id_workorder != 0 || $d_keluar->id_workorder != ''){
            $id_wo = $d_keluar->id_workorder;
        }

        $d_data = DB::table('view_tenun_barangmasuk')->where('id_workorder', $id_wo);

        if($d_data->count() > 0){
            $arr = $d_data->get();
        }else{
            $arr = array();
        }

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

    function get_motif_kualitas(){
        $d_data = DB::table('ms_motif');

        if($d_data->count() > 0){
            foreach($d_data->orderBy('nama_motif', 'asc')->get() as $d){
                $arr[] = array('id_motif' => $d->id_motif,
                                'kode_motif' => $d->kode_motif,
                                'nama_motif' => $d->nama_motif);
            }
        }else{
            $arr = array();
        }

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

    // ------------------------------- Akhir Proses Lanjutan -----------------------


    // ------------------------------- Awal Perubahan Satuan -----------------------
    function get_perubahansatuan(Request $request){
        $id = $request->get('id_barangkeluar');
        $d_data = DB::table('pro_barangkeluar AS pb')->join('ms_proses AS mp', 'pb.id_proses', 'mp.id_proses')->leftjoin('ms_rekanan AS mr', 'pb.id_rekanan', 'mr.id_rekanan')->leftjoin('ms_pegawai as mpe','pb.id_pegawai','mpe.id_pegawai')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->leftjoin('pem_workorder as pw', 'pb.id_workorder', 'pw.id_workorder')->leftjoin('pem_workorder_barang as pwb', 'pwb.id_workorder_barang', 'pb.id_workorder_barang')->select(DB::raw('pb.*, mp.nama_proses AS nama_proses, mr.nama as nama_rekanan, mpe.nama_pegawai as nama_pegawai, mrk.nama nama_kelompok, pw.nomor no_workorder, pwb.no_workorder no_workorder_barang'))->where('pb.id_barangkeluar', $id)->first();

        $id_motif = '';
        $id_motif_barang = '';
        if($request->get('motif') != ''){
        $id_motif = $request->get('motif');
        }
        if($request->get('motif_barang') != ''){
        $id_motif_barang = $request->get('motif_barang');
        }
        
        $data['id_proses'] = $d_data->id_proses;
        $data['id_motif'] = $id_motif;
        $data['id_motif_barang'] = $id_motif_barang;
        $data['id_lokasi'] = $d_data->id_lokasi;

        //-- Jumlah Hasil
        $jumlah_hasil = $request->get('jumlah_hasil');
        $data['id_satuanperubahan'] = $request->get('satuan_hasil');
        $jumlah = $request->get('jumlah');
        $data['id_satuanawal'] = $request->get('satuan');
        $cek = DB::table('ms_perubahansatuan as mps')->join('ms_satuan as ms','mps.id_satuanawal','ms.id_satuan')->join('ms_satuan as ms2','mps.id_satuanperubahan','ms2.id_satuan')->select(DB::raw('mps.*,ms.nama_satuan as nama_satuanawal, ms2.nama_satuan as nama_satuanperubahan'))->where($data);
        if($cek->count() > 0){
            $arr['data'] = $cek->first();
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

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

    function cetak(Request $request, $id){
        require('fpdf181/Mc_table.php');

        $d_masuk = DB::table('pro_barangkeluar AS pbk')->leftJoin('ms_rekanan AS mr', 'pbk.id_rekanan', 'mr.id_rekanan')->leftJoin('ms_pegawai AS mp', 'pbk.id_pegawai', 'mp.id_pegawai')->join('ms_proses AS mpr', 'pbk.id_proses', 'mpr.id_proses')->join('pro_barangmasuk AS pbm', 'pbk.id_barangkeluar', 'pbm.id_barangkeluar')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->leftjoin('pem_workorder as pw', 'pbk.id_workorder', 'pw.id_workorder')->leftjoin('pem_workorder_barang as pwb', 'pwb.id_workorder_barang', 'pbk.id_workorder_barang')->leftjoin('ms_mesin as mm', 'pbk.id_mesin', 'mm.id_mesin')->select(DB::raw('pbk.id_barangkeluar, pbm.id_barangmasuk, pbm.tanggal tanggal_selesai, pbk.tanggal tanggal_ambil, pbk.tanggal_perkiraan, pbk.id_rekanan, mr.nama nama_rekanan, pbk.id_pegawai, mp.nama_pegawai, mpr.id_proses, mpr.nama_proses, mrk.nama nama_kelompok, pw.nomor no_workorder, pwb.no_workorder no_workorder_barang, pbk.status, pbk.status_workorder, mm.id_mesin, mm.nama nama_mesin, pbm.noAuto, pbm.noAuto_ext'))->where('pbm.id_barangmasuk', $id);
        $req = $request->all();

        /*-------------------- Laporan Tenun -------------------------------------*/
    //    $d_barang = barangkeluar_barang($d_masuk->first()->id_barangkeluar);
        // $d_barang = DB::table('view_edit_keluar')->where('id', $d_masuk->first()->id_barangkeluar);
        // $d_barang_tenun = DB::table('pro_barangmasuk_barang as pbb')->join('pro_barangmasuk as pb','pbb.id_barangmasuk','pb.id_barangmasuk')->join('ms_kualitas as mk','pbb.id_kualitas','mk.id_kualitas')->where('pb.id_barangkeluar', $d_masuk->first()->id_barangkeluar);
        // $d_rekanan_kelompok = '';
        // if($d_masuk->first()->id_rekanan != '' || $d_masuk->first()->id_rekanan != null){
        // $d_rekanan_kelompok = DB::table('ms_rekanan as mr')->join('ms_rekanan_kelompok as mrk','mr.id_kelompok','mrk.id_kelompok')->where('mr.id_rekanan','=',$d_masuk->first()->id_rekanan)->select(DB::Raw('mrk.*'))->first();
        // }
        // $d_masuk_isi = get_detailtenun()->where('pbm.id_barangmasuk', $id)->leftjoin('pro_barangmasuk as pbm', 'pb.id_barangkeluar', 'pbm.id_barangkeluar')->select(DB::raw('pb.id_barangkeluar, MIN(pb.tanggal) tanggal_ambil, MAX(pb.tanggal_perkiraan) tanggal_perkiraan, pb.id_rekanan, mr.nama nama_rekanan, mrk.nama nama_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, pb.id_lokasi, mlp.nama_lokasi, pb.status_beem, pb.tanggal_beem, pb.status_workorder, pbm.id_barangmasuk, pbm.tanggal tanggal_selesai, pbm.status'));
        // $d_barangkeluar = DB::table('pro_barangkeluar')->where('id_barangkeluar', $d_masuk->first()->id_barangkeluar)->first();
        // $id_workorder_barang = $d_barangkeluar->id_workorder_barang;
        // $id_rekanan = $d_barangkeluar->id_rekanan;
        // $id_pegawai = $d_barangkeluar->id_pegawai;
        // $d_isi = get_barangkeluar_byorder()->whereRaw('vk.id_workorder_barang = "'.$id_workorder_barang.'" AND pb.id_rekanan = "'.$id_rekanan.'" AND pb.id_pegawai = "'.$id_pegawai.'" AND vk.tanggal <= "'.$d_masuk_isi->first()->tanggal_selesai.'"')->where('id_satuan','=','10');
        /*-------------------- END Laporan Tenun -------------------------------------*/
        
        $d_keluar = DB::table('view_edit_keluar')->where('id', $d_masuk->first()->id_barangkeluar);
        $data['barang'] = $d_keluar;     
        $data['masuk'] = $d_masuk->first();
        $data['setor'] = DB::table('view_edit_masuk as v')->join('pro_barangmasuk as pb', 'v.id_barangmasuk', 'pb.id_barangmasuk')->select(DB::raw("*, pb.tanggal tanggal_setor"))->where('v.id_barangmasuk', $id);
        
        // $data['masuk_tenun'] = $d_barang->first();
        // $data['barang_tenun'] = $d_barang_tenun->get();
        // $data['barang_tenun_keluar'] = $d_isi->get();
        // $data['rekanan_kelompok'] = $d_rekanan_kelompok;
        // $data['isi_sekir'] = $d_isi->first();


        $songket = DB::table('pro_barangkeluar AS pbk')->leftjoin('ms_rekanan AS mr','pbk.id_rekanan','mr.id_rekanan')->leftjoin('ms_pegawai AS mp','pbk.id_pegawai','mp.id_pegawai')->join('ms_proses AS mpr','pbk.id_proses','mpr.id_proses')->where('pbk.id_barangkeluar','=',$id);

        $id_proses = $d_masuk->first()->id_proses;
        $id_keluar = $d_masuk->first()->id_barangkeluar;
        $data['sisa'] = array();

        if(isset($req['kertas'])){
            if($req['kertas'] == 1){
                if($id_proses == 25){
                    $query = "SELECT B.*, CASE WHEN mwk.id_komposisi IS NULL THEN '' ELSE mwk.inisial END AS inisial_komposisi
                                FROM (
                                ".base_stok_masuk(1)."
                                ) B LEFT JOIN ms_warnakomposisi AS mwk ON B.id_komposisi = mwk.id_komposisi WHERE B.id_barangkeluar = $id_keluar AND B.stok > 0 ORDER BY B.nama_corak";
                    $data['sisa'] = DB::select($query);

                }else if($id_proses == 24){
                    $query = "SELECT B.*, CASE WHEN mwk.id_komposisi IS NULL THEN '' ELSE mwk.inisial END AS inisial_komposisi
                                FROM (
                                ".base_stok_masuk(1)."
                                ) B LEFT JOIN ms_warnakomposisi AS mwk ON B.id_komposisi = mwk.id_komposisi WHERE B.id_barangkeluar = $id_keluar AND B.stok > 0 ORDER BY B.nama_motif";
                    $data['sisa'] = DB::select($query);
                }else if($id_proses == 22){
                    $query = "SELECT B.*, CASE WHEN mwk.id_komposisi IS NULL THEN '' ELSE mwk.inisial END AS inisial_komposisi
                                FROM (
                                ".str_replace("A.id_komposisi,", "", base_stok_masuk())."
                                ) B LEFT JOIN ms_warnakomposisi AS mwk ON B.id_komposisi = mwk.id_komposisi WHERE B.id_barangkeluar = $id_keluar AND B.stok > 0 ORDER BY B.nama_corak";
                    $data['sisa'] = DB::select($query);

                }else if($id_proses == 60){
                    $query = "SELECT B.*, CASE WHEN mwk.id_komposisi IS NULL THEN '' ELSE mwk.inisial END AS inisial_komposisi
                                FROM (
                                ".str_replace(["A.id_barang,", ", A.id_motif"], ["", ""], base_stok_masuk())."
                                ) B LEFT JOIN ms_warnakomposisi AS mwk ON B.id_komposisi = mwk.id_komposisi WHERE B.id_barangkeluar = $id_keluar AND B.stok > 0 ORDER BY B.nama_corak";
                    $data['sisa'] = DB::select($query);
                }
                else{
                    $query = "SELECT B.*, CASE WHEN mwk.id_komposisi IS NULL THEN '' ELSE mwk.inisial END AS inisial_komposisi
                                FROM (
                                ".base_stok_masuk()."
                                ) B LEFT JOIN ms_warnakomposisi AS mwk ON B.id_komposisi = mwk.id_komposisi WHERE B.id_barangkeluar = $id_keluar AND B.stok > 0 ORDER BY B.id_barangkeluar_barang";
                    $data['sisa'] = DB::select($query);
                }
            }
        }

        if($d_masuk->first()->id_proses == '37'){
            // $html = view('produksi.cetak.Laporantenun')->with('data',$data);
            //$html = view('produksi.cetak.LaporantenunNosongket')->with('data',$data);
            /*foreach($d_isi->get() as $d){
            print_r($d->nama_satuan.'/'.$d->id_satuan);
            }*/
            if(isset($req['kertas'])){
                if($req['kertas'] == 0){
                    $view = "BarangmasukCetakKecil";
                }else{
                    $view = "Barangmasukcetak";
                }
            }else{
                $view = "Barangmasukcetak";
            }

            $html = view('produksi.cetak.'.$view.'')->with('data',$data);
        }else{
            if(isset($req['kertas'])){
                if($req['kertas'] == 0){
                    $view = "BarangmasukCetakKecil";
                }else{
                    $view = "Barangmasukcetak";
                }
            }else{
                $view = "Barangmasukcetak";
            }

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

    function tambah_tenun($id){
        $d_barangkeluar = DB::table('pro_barangkeluar')->where('id_barangkeluar', $id)->first();
        $id_proses = $d_barangkeluar->id_proses;
        $id_workorder_barang = $d_barangkeluar->id_workorder_barang;
        $id_rekanan = $d_barangkeluar->id_rekanan;
        $id_pegawai = $d_barangkeluar->id_pegawai;

        $data['barang_keluar'] =  get_barangkeluar_byorder()->whereRaw('vk.id_workorder_barang = "'.$id_workorder_barang.'" AND pb.id_rekanan = "'.$id_rekanan.'" AND pb.id_pegawai = "'.$id_pegawai.'" AND vk.tanggal <= "'.date('Y-m-d').'" ')->get();
        
        $data['detail'] = get_detailtenun()->whereRaw('pb.id_proses = "'.$id_proses.'" AND mpr.nama_proses LIKE "%tenun%" AND pb.status != 0 AND pwb.id_workorder_barang = "'.$id_workorder_barang.'" AND pb.id_rekanan = "'.$id_rekanan.'" AND pb.id_pegawai = "'.$id_pegawai.'"')->first();

        $data['barang'] = DB::table('ms_barang AS mb')->join('ms_kategori_barang AS mkb', 'mb.id_kb', 'mkb.id_kb')->join('ms_kategori_barang AS mkb2', 'mkb.induk_kb', 'mkb2.id_kb')->join('ms_satuan AS ms', 'mb.id_satuan', 'ms.id_satuan')->select(DB::raw('mb.*, mkb2.id_kb AS id_kategori, mkb2.nama nama_kategori, mkb.id_kb id_sub, mkb.nama nama_sub, ms.id_satuan, ms.nama_satuan'))->orderBy('nama_kategori', 'asc')->orderBy('nama_sub', 'asc')->orderBy('kode_barang', 'asc')->orderBy('nama_barang', 'asc')->get();
        $data['gudang'] = DB::table('ms_gudang')->orderBy('nama_gudang', 'asc')->get();
        $data['satuan'] = DB::table('ms_satuan')->orderBy('nama_satuan', 'asc')->where("nama_satuan", "LIKE", "%sarung%")->get();
        $data['motif'] = DB::table('ms_motif')->orderBy('nama_motif', 'asc')->get();
        $data['kualitas_proses'] = DB::table('ms_kualitas')->orderBy('nama_kualitas', 'asc')->where('jenis', '2')->get();
        $data['kualitas_tenun'] = DB::table('ms_kualitas')->orderBy('nama_kualitas', 'asc')->where('jenis', '1')->get();
        $data['warna'] = DB::table('ms_warnakomposisi')->orderBy('nama', 'asc')->get();


        $data['d_kemasan'] = $this->data();
        return view('produksi.BarangmasuktenunTambah')->with('data', $data);
    //    print_r('vk.id_workorder_barang = "'.$id_workorder_barang.'" AND pb.id_rekanan = "'.$id_rekanan.'" AND pb.id_pegawai = "'.$id_pegawai.'" AND vk.tanggal <= "'.date('Y-m-d').'"');
    }

    function edit_tenun($id){

        $d_masuk = DB::table('pro_barangmasuk')->where('id_barangmasuk', $id)->select(DB::raw('*, tanggal tanggal_masuk'))->first();

        $d_barangkeluar = DB::table('pro_barangkeluar')->where('id_barangkeluar', $d_masuk->id_barangkeluar)->first();
        $id_proses = $d_barangkeluar->id_proses;
        $id_workorder_barang = $d_barangkeluar->id_workorder_barang;
        $id_rekanan = $d_barangkeluar->id_rekanan;
        $id_pegawai = $d_barangkeluar->id_pegawai;

        $data['barang_keluar'] =  get_barangkeluar_byorder()->whereRaw('vk.id_workorder_barang = "'.$id_workorder_barang.'" AND pb.id_rekanan = "'.$id_rekanan.'" AND pb.id_pegawai = "'.$id_pegawai.'" AND vk.tanggal <= "'.date('Y-m-d').'" ')->get();
        
        $data['detail'] = get_detailtenun()->whereRaw('pb.id_proses = "'.$id_proses.'" AND mpr.nama_proses LIKE "%tenun%" AND pb.status != 0 AND pwb.id_workorder_barang = "'.$id_workorder_barang.'" AND pb.id_rekanan = "'.$id_rekanan.'" AND pb.id_pegawai = "'.$id_pegawai.'"')->first();

        $data['barang'] = DB::table('ms_barang AS mb')->join('ms_kategori_barang AS mkb', 'mb.id_kb', 'mkb.id_kb')->join('ms_kategori_barang AS mkb2', 'mkb.induk_kb', 'mkb2.id_kb')->join('ms_satuan AS ms', 'mb.id_satuan', 'ms.id_satuan')->select(DB::raw('mb.*, mkb2.id_kb AS id_kategori, mkb2.nama nama_kategori, mkb.id_kb id_sub, mkb.nama nama_sub, ms.id_satuan, ms.nama_satuan'))->orderBy('nama_kategori', 'asc')->orderBy('nama_sub', 'asc')->orderBy('kode_barang', 'asc')->orderBy('nama_barang', 'asc')->get();
        $data['gudang'] = DB::table('ms_gudang')->orderBy('nama_gudang', 'asc')->get();
        $data['satuan'] = DB::table('ms_satuan')->orderBy('nama_satuan', 'asc')->where("nama_satuan", "LIKE", "%sarung%")->get();
        $data['motif'] = DB::table('ms_motif')->orderBy('nama_motif', 'asc')->get();
        $data['kualitas_proses'] = DB::table('ms_kualitas')->orderBy('nama_kualitas', 'asc')->where('jenis', '2')->get();
        $data['kualitas_tenun'] = DB::table('ms_kualitas')->orderBy('nama_kualitas', 'asc')->where('jenis', '1')->get();
        $data['warna'] = DB::table('ms_warnakomposisi')->orderBy('nama', 'asc')->get();


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



    function detail_tenun($id){

        $d_masuk = get_detailtenun()->where('pbm.id_barangmasuk', $id)->leftjoin('pro_barangmasuk as pbm', 'pb.id_barangkeluar', 'pbm.id_barangkeluar')->select(DB::raw('pb.id_barangkeluar, MIN(pb.tanggal) tanggal_ambil, MAX(pb.tanggal_perkiraan) tanggal_perkiraan, pb.id_rekanan, mr.nama nama_rekanan, mrk.nama nama_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, pb.id_lokasi, mlp.nama_lokasi, pb.status_beem, pb.tanggal_beem, pb.status_workorder, pbm.id_barangmasuk, pbm.tanggal tanggal_selesai, pbm.status'));

        $id_barangkeluar = $d_masuk->first()->id_barangkeluar;

        $d_barangkeluar = DB::table('pro_barangkeluar')->where('id_barangkeluar', $id_barangkeluar)->first();
        $id_proses = $d_barangkeluar->id_proses;
        $id_workorder_barang = $d_barangkeluar->id_workorder_barang;
        $id_rekanan = $d_barangkeluar->id_rekanan;
        $id_pegawai = $d_barangkeluar->id_pegawai;
        
        $d_barang = get_barangkeluar_byorder()->whereRaw('vk.id_workorder_barang = "'.$id_workorder_barang.'" AND pb.id_rekanan = "'.$id_rekanan.'" AND pb.id_pegawai = "'.$id_pegawai.'" AND vk.tanggal <= "'.$d_masuk->first()->tanggal_selesai.'"');
    
        $d_warna = DB::table('ms_warnakomposisi')->OrderBy('nama', 'asc')->get();
        
        $data['data'] = $this->data(array());
        $data['barang'] = $d_barang->get();
        $data['masuk'] = $d_masuk->first();
    
        return view('produksi.BarangmasuktenunDetail')->with('data',$data);
        
    }


    function get_nomor(Request $request){
        $data = $request->all();
        $arr = array();
        $id_lokasi = $data['id_lokasi'];

        if($data['id'] != ''){
            $d_data = DB::table('pro_barangmasuk')->where('id_barangmasuk', $data['id']);
            
            if($id_lokasi != ''){
                $d_data->join('pro_barangkeluar as pb', 'pro_barangmasuk.id_barangkeluar', 'pb.id_barangkeluar')->select(DB::raw("pro_barangmasuk.noAuto, pro_barangmasuk.noAuto_ext, pb.id_lokasi"));
                $d_data->where('id_lokasi', $id_lokasi);
            }
            
            $d_data = $d_data->first();
            $arr = array('nomor' => $d_data->noAuto,
                         'faktur' => $d_data->noAuto_ext);
        }else{
            $tgl = explode('-', $data['tgl']);
            $no = 1;


            $d_kemasan = DB::table("pro_barangmasuk_kemasan")->select(DB::raw("noAuto, noAuto_ext"));
            
            if($id_lokasi != ''){
                $d_kemasan->join('pro_barangkeluar_kemasan as pkk', 'pro_barangmasuk_kemasan.id_barangkeluar_kemasan', 'pkk.id_barangkeluar_kemasan')->select(DB::raw("pro_barangmasuk_kemasan.noAuto, pro_barangmasuk_kemasan.noAuto_ext, pkk.id_lokasi"));
                $d_kemasan->where('id_lokasi', $id_lokasi);
            }
            
            $d_data = DB::table('pro_barangmasuk')->unionAll($d_kemasan)->select(DB::raw("noAuto, noAuto_ext"));
            
            if($id_lokasi != ''){
                $d_data->join('pro_barangkeluar as pb', 'pro_barangmasuk.id_barangkeluar', 'pb.id_barangkeluar')->select(DB::raw("pro_barangmasuk.noAuto, pro_barangmasuk.noAuto_ext, pb.id_lokasi"));
                $d_data->where('id_lokasi', $id_lokasi);
            }

            $d_data = $d_data->get()->max('noAuto_ext');

            if($d_data == 0 || $d_data == ''){
                $no = 1;
            }else{
                $no = $d_data + 1;
            }

            $arr = array('nomor' => "LH.",
                        'faktur' => $no);
        }

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

    function validasi_nomor($id, $noAuto, $noAuto_ext, $id_lokasi = ''){
        $arr = array();

        if($noAuto != '' && $noAuto_ext != ''){
            
            if($id > 0){
                $d_data = DB::table('pro_barangmasuk')->where('id_barangmasuk', $id);
                
                if($id_lokasi != ''){
                    $d_data->join('pro_barangkeluar as pb', 'pro_barangmasuk.id_barangkeluar', 'pb.id_barangkeluar')->select(DB::raw("pro_barangmasuk.noAuto, pro_barangmasuk.noAuto_ext, pb.id_lokasi"));
                    $d_data->where('id_lokasi', $id_lokasi);
                }
                
                if($d_data->count() > 0){
                    if($d_data->first()->noAuto == $noAuto && $d_data->first()->noAuto_ext == $noAuto_ext){
                        $arr = array('noAuto' => $noAuto,
                            'noAuto_ext' => $noAuto_ext); 
                    }else{
                        DB::table('pro_barangmasuk')->where('id_barangmasuk', $id)->update(['noAuto' => '', 'noAuto_ext' => '']);
                        return $this->validasi_nomor(0, $noAuto, $noAuto_ext, $id_lokasi);
                    }
                }else{
                    $arr = array('noAuto' => $noAuto,
                            'noAuto_ext' => $noAuto_ext); 
                }

            }else{
                $no = 1;
                $where = '';
            
                $d_data[] = "SELECT pm.noAuto, pm.noAuto_ext, pb.id_lokasi FROM pro_barangmasuk as pm JOIN pro_barangkeluar as pb ON pm.id_barangkeluar = pb.id_barangkeluar";
                $d_data[] = "SELECT pbm.noAuto, pbm.noAuto_ext, pkk.id_lokasi FROM pro_barangmasuk_kemasan as pbm JOIN pro_barangkeluar_kemasan as pkk ON pbm.id_barangkeluar_kemasan = pkk.id_barangkeluar_kemasan";

                $union = implode(" UNION ALL ", $d_data);

                if($id_lokasi != ''){
                    $where = " WHERE u.id_lokasi = '".$id_lokasi."' ";
                }
                

                $test = DB::select("SELECT *, max(CAST(u.noAuto_ext as signed)) as nomax FROM ($union) u $where");

                if(count($test) > 0){
                    $nomor = $test[0]->nomax;


                    if($nomor == 0 || $nomor == ''){
                        $no = 1;
                    }else{
                        $no = $nomor + 1;
                    }

                    $arr = array('noAuto' => $noAuto,
                                    'noAuto_ext' => $no);                
                }else{
                    $arr = array('noAuto' => $noAuto,
                                'noAuto_ext' => $noAuto_ext);

                }

            }
        }

        return $arr;
    }

    function get_sisa($id){
        $d_keluar = DB::table('pro_barangkeluar')->where('id_barangkeluar', $id)->first();

        $except = array(24, 25, 22);
        $where = ["pb.id_barangkeluar = '$id'", "pbk.id_barangkeluar = '$id'"];
        if(!in_array($d_keluar->id_proses, $except)){
            $query = "SELECT B.*
                FROM (
                ".base_stok_masuk('', $where)."
                ) B WHERE B.id_barangkeluar = $id AND B.stok > 0 ORDER BY B.id_barangkeluar_barang";

            $result = get_rawquery($query);
        }else if($d_keluar->id_proses == '22'){
            $query = "SELECT B.*
                FROM (
                ".str_replace("A.id_komposisi,", "", base_stok_masuk('', $where))."
                ) B WHERE B.id_barangkeluar = $id AND B.stok > 0 ORDER BY B.id_barangkeluar_barang";

            $result = get_rawquery($query);
        }else if($d_keluar->id_proses == '60'){
            $query = "SELECT B.*
                FROM (
                ".str_replace(", A.id_motif", "", base_stok_masuk('', $where))."
                ) B WHERE B.id_barangkeluar = $id AND B.stok > 0 ORDER BY B.id_barangkeluar_barang";
            
            $result = get_rawquery($query);
        }
        else{
            $result = array();
        }
        return $result;
        

    }



//------------ Fungsi Pembantu

    function data($data = array()){
        if(count($data) > 0){
            $store['id_barangmasuk'] = $data->id_barangmasuk;
            $store['id_barangkeluar'] = $data->id_barangkeluar;
            $store['tanggal']= date_format(date_create($data->tanggal_masuk),'d-m-Y');
        }else{
            $store['id_barangmasuk'] = '';
            $store['id_barangkeluar'] = '';
            $store['tanggal']='';
        }

        return $store;
    }

    function proses($jenis, $status){
        switch ($jenis) {
            case '0':
            case '2':
                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>';
                }else if($status == 3){
                    $d = '<label class="label bg-warning-600">Belum Diterima Sebagian</label>';
                }
                break;
            case '1':
            case '3':
                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, $stat_pekerja,$id_tabel=1, $status_bayar = 0){
        $d = '';
        $d_data = DB::table('pro_barangmasuk_barang')->where('id_barangmasuk', $id)->pluck('status')->toArray();
        switch ($jenis) {
            case '0':
                if($status == 1){
                    $d = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangmasuk/tambah/').'/'.$id.'"><i class="icon-checkbox-checked2"></i></a></div>';
                }else if($status == 2){
                    $d = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangmasuk/tambah/').'/'.$id.'"><i class="icon-checkbox-checked2"></i></a>';
                    
                //    $cek_upah = DB::table("pro_upahproduksi")->where([["id_barangmasuk", $id]]);
                    if($status_bayar == 0){//if($cek_upah->count() == 0){
                        if ($stat_pekerja == "Pegawai") {
                            $d .='<a class="btn btn-success" type="button" href="'.url('upahproduksi/tambah').'/'.$id.'"><i class="icon-cash2"></i></a>';
                        } else {
                            $d .='<a class="btn btn-success" type="button" href="'.url('upahproduksi/rekanan/tambah').'/'.$id.'/'.$id_tabel.'"><i class="icon-cash2"></i></a>';
                        }
                    }

                    $d .= "</div>";

                }else{
                    $d = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangmasuk/tambah/').'/'.$id.'"><i class="icon-checkbox-checked2"></i></a></div>';
                }
                
                break;
            case '1':
                if($status == 0){
                    if(in_array(1, $d_data)){
                        $d = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangmasuk/detail/').'/'.$id.'"><i class="icon-notification2"></i></a>  <a class="btn bg-warning-400" type="button" href="'.url('barangmasuk/edit/').'/'.$id.'"><i class="icon-pencil3"></i></a>'; //hapus_edit
                    }else{
                        $d = '<div class="btn-group"><div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangmasuk/detail/').'/'.$id.'"><i class="icon-notification2"></i></a> <a class="btn bg-warning-400" type="button" href="'.url('barangmasuk/edit/').'/'.$id.'"><i class="icon-pencil3"></i></a></div>';
                    }

                // --    $d .= $this->get_hapus($id);
                    $d .= '<button class="btn bg-danger-800" type="button" onclick="hapus($(this))" id_hasil="'.$id.'" jenis="0"><i class="icon-eraser"></i></button>';
                    
                }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 = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangmasuk/detail/').'/'.$id.'"><i class="icon-notification2"></i></a>  <a class="btn bg-warning-400" type="button" href="'.url('barangmasuk/edit/').'/'.$id.'"><i class="icon-pencil3"></i></a>'; //hapus_edit
                    
                // --    $d .= $this->get_hapus($id);

                //    $cek_upah = DB::table("pro_upahproduksi")->where([["id_barangmasuk", $id]]);
                    if($status_bayar == 0){//if($cek_upah->count() == 0){
                        if ($stat_pekerja == "Pegawai") {
                           $d .='<a class="btn btn-success" type="button" href="'.url('upahproduksi/tambah').'/'.$id.'"><i class="icon-cash2"></i></a>';
                        } else {
                            $d .='<a class="btn btn-success" type="button" href="'.url('upahproduksi/rekanan/tambah').'/'.$id.'/'.$id_tabel.'"><i class="icon-cash2"></i></a>';
                        }

                        $d .= '<button class="btn bg-danger-800" type="button" onclick="hapus($(this))" id_hasil="'.$id.'" jenis="0"><i class="icon-eraser"></i></button>';
                    }
                }

                $d .='<button type="button" class="btn bg-success-400 dropdown-toggle" data-toggle="dropdown"><i class="icon-printer"></i><span class="caret"></span></button>
                            <ul class="dropdown-menu dropdown-menu-right">
                                <li><a href="'.url('barangmasuk/cetak/'.$id).'?kertas=1'.'" target="_blank"><i class="icon-printer4"></i> Cetak Sedang</a></li>
                                <li><a href="'.url('barangmasuk/cetak/'.$id).'?kertas=0'.'" target="_blank"><i class="icon-printer4"></i> Cetak Kecil</a></li>
                            </ul>
                            </div>';
            break;
            // Untuk tenun
            case '2':
                if($status == 1){
                    $d = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangmasuk/tambah_tenun/').'/'.$id.'"><i class="icon-checkbox-checked2"></i></a></div>';
                }else if($status == 2){
                    $d = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangmasuk/tambah_tenun/').'/'.$id.'"><i class="icon-checkbox-checked2"></i></a>';
                    
                //    $cek_upah = DB::table("pro_upahproduksi")->where([["id_barangmasuk", $id]]);
                    if($status_bayar == 0){//if($cek_upah->count() == 0){
                        if ($stat_pekerja == "Pegawai") {
                           $d .='<a class="btn btn-success" type="button" href="'.url('upahproduksi/tambah').'/'.$id.'"><i class="icon-cash2"></i></a>';
                        } else {
                        //    $d .='<a class="btn btn-success" type="button" href="'.url('upahproduksi/rekanan_tenun/tambah').'/'.$id.'"><i class="icon-cash2"></i></a>';
                        }
                    }
                    
                    $d .= '</div>';
                }else{
                    $d = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangmasuk/tambah_tenun/').'/'.$id.'"><i class="icon-checkbox-checked2"></i></a></div>';
                }
                
            break;
            case '3':
                if($status == 0){

                    $d = '<div class="btn-group"><a class="btn bg-warning-400" type="button" href="'.url('barangmasuk/edit_tenun/').'/'.$id.'"><i class="icon-pencil3"></i></a> ';

                // --    $d .= $this->get_hapus($id);
                    $d .= '<button class="btn bg-danger-800" type="button" onclick="hapus($(this))" id_hasil="'.$id.'" jenis="0"><i class="icon-eraser"></i></button>';
                }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 = '<div class="btn-group"><a class="btn bg-info-400" type="button" href="'.url('barangmasuk/detail_tenun/').'/'.$id.'"><i class="icon-notification2"></i></a> <a class="btn bg-warning-400" type="button" href="'.url('barangmasuk/edit_tenun/').'/'.$id.'"><i class="icon-pencil3"></i></a>';
                    
                // --    $d .= $this->get_hapus($id);
                    
                //    $cek_upah = DB::table("pro_upahproduksi")->where([["id_barangmasuk", $id]]);
                    if($status_bayar == 0){//if($cek_upah->count() == 0){
                        if ($stat_pekerja == "Pegawai") {
                           $d .='<a class="btn btn-success" type="button" href="'.url('upahproduksi/tambah').'/'.$id.'"><i class="icon-cash2"></i></a>';
                        } else {
                        //    $d .='<a class="btn btn-success" type="button" href="'.url('upahproduksi/rekanan_tenun/tambah').'/'.$id.'"><i class="icon-cash2"></i></a>';
                        }

                        $d .= '<button class="btn bg-danger-800" type="button" onclick="hapus($(this))" id_hasil="'.$id.'" jenis="0"><i class="icon-eraser"></i></button>';
                    }

                    /*$d .='<button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown"><i class="icon-printer"></i><span class="caret"></span></button>
                            <ul class="dropdown-menu dropdown-menu-right">
                                <li><a href="'.url('barangmasuk/cetak/'.$id).'?kertas=1'.'" target="_blank"><i class="icon-printer4"></i> Cetak Sedang</a></li>
                                <li><a href="'.url('barangmasuk/cetak/'.$id).'?kertas=0'.'" target="_blank"><i class="icon-printer4"></i> Cetak Kecil</a></li>
                                <li><a href="'.url('barangmasuk/cetak/'.$id).'" target="_blank"><i class="icon-printer4"></i> Kartu Stock</a></li>

                            </ul>
                        </div>';*/
                    
                        
                }

                // $d .= '<a href="'.url('barangmasuk/cetak/'.$id).'" class="btn btn-success" target="_blank"><i class="icon-printer"></i></a>
                //         </div>';
                $d .='<button type="button" class="btn bg-success-400 dropdown-toggle" data-toggle="dropdown"><i class="icon-printer"></i><span class="caret"></span></button>
                            <ul class="dropdown-menu dropdown-menu-right">
                                <li><a href="'.url('barangmasuk/cetak/'.$id).'?kertas=0'.'" target="_blank"><i class="icon-printer4"></i> Cetak Kecil</a></li>
                            </ul>
                            </div>';
            break;
            default:
                $d = '';
                break;
        }

        return $d;
    }

    function get_barangkeluar($where){
        $d_barangkeluar = DB::table('pro_barangkeluar')->where($where)->orderBy('tanggal', 'asc');

        if($d_barangkeluar->count() > 0){
            foreach($d_barangkeluar->get() as $d){
                $arr[] = ['id_barangkeluar' =>$d->id_barangkeluar];
            }
        }else{
            $arr = array();
        }

        return $arr;
    }

    function get_hapus($id){
        $cek_upah = DB::table('pro_upahproduksi')->where('id_barangmasuk', $id);
        $del = '';
        if($cek_upah->count() > 0){
            if($cek_upah->first()->status_bayar != 1){
                $del = '<button class="btn bg-danger-800" type="button" onclick="hapus($(this))" id_hasil="'.$id.'" jenis="0"><i class="icon-eraser"></i></button>';
            }else{
                $del = '';    
            }           
        }else{
            $del = '<button class="btn bg-danger-800" type="button" onclick="hapus($(this))" id_hasil="'.$id.'" jenis="0"><i class="icon-eraser"></i></button>';
        }

        return $del;
    }

    function hapus_hasil(Request $request){
        $req = $request->all();
        $id = $req['id'];
        $jenis = $req['jenis'];

        if($jenis == 0){
            // $d = $this->get_barangstok($id);

            // if(in_array(0, $d)){
                // $stat = array('status' => '0');
            // }else{
                $this->hapus($id);
                $stat = array('status' => '1');
            // }
        }else{
            $this->hapus($id);
            $stat = array('status' => '1');
        }

        return $stat;
    }

    function get_barangstok($id){
        $d_masuk = DB::table('view_edit_masuk')->where('id_barangmasuk', $id);
        $jum = array();

        if($d_masuk->count() > 0){
            foreach($d_masuk->get() as $d){

                $jumlah = $d->jumlah;

                $where = "WHERE B.id_gudang = '$d->id_gudang' AND B.id_barang = '$d->id_barang' AND B.id_komposisi = '$d->id_komposisi' AND B.id_corak ='$d->id_corak' AND B.id_motif ='$d->id_motif' AND B.id_kualitas = '$d->id_kualitas' AND B.id_kualitas_dudul = '$d->id_kualitas_dudul' AND B.id_label = '$d->id_label' AND B.id_kemasan = '$d->id_kemasan' AND B.id_kemasansatuan = '$d->id_kemasansatuan' AND B.id_kemasansepuluh = '$d->id_kemasansepuluh' AND B.id_kemasanseratus = '$d->id_kemasanseratus' AND B.id_proses = '$d->id_proses' AND B.id_workorder = '$d->id_workorder' AND B.id_workorder_barang = '$d->id_workorder_barang' AND B.id_cap = '$d->id_cap' AND B.id_kualitas_periksa = '$d->id_kualitas_periksa' AND B.id_kualitas_proses = '$d->id_kualitas_proses' AND B.id_perubahansatuan = '$d->id_perubahansatuan' AND B.jumlah_rencana = '$d->jumlah_rencana'";

                $usersBills = get_rawquery("SELECT B.*
                                             FROM (
                                                 ".base_stok()."
                                             ) B
                                             $where ");

                if(count($usersBills) > 0){
                    if($usersBills[0]['stok'] < $jumlah){
                        $jum[] = 0;
                    }else{
                        $jum[] = 1;
                    }
                }else{
                    $jum[] = 1;
                }

        
            }
        }else{
            $jum[] = 0;
        }

        return $jum;
    }

    function hapus($id){

        $status_0 = array('status' => '0');
        $status_1 = array('status' => '1');
        $status_3 = array('status' => '3');

        $where = array();

        $id_barangkeluar = DB::table('pro_barangmasuk')->where('id_barangmasuk', $id)->first()->id_barangkeluar;

        $detail_keluar = DB::table('pro_barangkeluar')->where('id_barangkeluar', $id_barangkeluar);
        if($detail_keluar->count() > 0){
            $d = $detail_keluar->first();
            if($d->id_proses == '37'){
                $where = array('id_rekanan' => $d->id_rekanan,
                            'id_pegawai' => $d->id_pegawai,
                            'id_mesin' => $d->id_mesin,
                            'id_proses' => $d->id_proses,
                            'id_workorder_barang' => $d->id_workorder_barang);
            }
        }
        

        DB::table('pro_upahproduksi')->where('id_barangmasuk', $id)->delete();
        DB::table('pro_barangmasuk')->where('id_barangmasuk', $id)->delete();
        DB::table('pro_barangmasuk_barang')->where('id_barangmasuk', $id)->delete();
        get_barangproduksi('1', $id);

        $cek_hasil = DB::table('pro_barangmasuk')->where('id_barangkeluar', $id_barangkeluar);

        if($cek_hasil->count() > 0){
            DB::table('pro_barangkeluar')->where('id_barangkeluar', $id_barangkeluar)->update($status_3);
            if(count($where) > 0){
                DB::table('pro_barangkeluar')->where($where)->update($status_3);
            }
        }else{
            DB::table('pro_barangkeluar')->where('id_barangkeluar', $id_barangkeluar)->update($status_1);
            if(count($where) > 0){
                DB::table('pro_barangkeluar')->where($where)->update($status_1);
            }
        }

        return response()->json(array('status' => '1'));
    }
}