<?php

namespace App\Http\Controllers\produksi;

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

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

        $this->session = array('id_gudang'=>Session::get('id_gudang'), 'id_lokasi'=>Session::get('id_lokasi'), 'kode_user' => Session::get('kode_user'), 'kode_group' => Session::get('kode_group'), 'id_proses' => Session::get('id_proses'));
    }
    
    function index(){
        $id_lokasi = $this->session['id_lokasi'];
        $id_proses = $this->session['id_proses'];
        $id_gudang = $this->session['id_gudang'];

        $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');
        $data['lokasi'] = DB::table('ms_lokasi_produksi')->orderBy('nama_lokasi', 'asc');
        $data['mesin'] = DB::table('ms_mesin')->orderby("nama");

        $data['kategori'] = DB::table('ms_kategori_barang')->where('induk_kb','=','0');
        $data['gudang'] = DB::table('ms_gudang')->orderBy('nama_gudang', 'asc');
        $data['warna'] = DB::table('ms_warnakomposisi')->orderBy('nama', 'asc');
        $data['proses'] = DB::table('ms_proses')->where('parent', '!=', '0');
        if($id_proses != 0){
            $data['proses']->whereIn('id_proses', explode(",", $id_proses));
        }
        $data['motif'] = DB::table('ms_motif')->orderBy('nama_motif','asc');
        $data['corak'] = DB::table('ms_motif_barang')->orderBy('motif_keterangan','asc');
        $data['spk'] = DB::table('pem_workorder')->orderBy('nomor','asc');
        $data['kualitas_tenun'] = DB::table('ms_kualitas')->where('jenis',1)->orderBy('nama_kualitas','asc');
        $data['workorder'] = DB::table('pem_workorder_barang')->orderBy('no_workorder','asc');
        $data['benang'] = 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')->where('mkb2.id_kb','5')->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');

        $data['pegawai'] = DB::table('ms_pegawai as mp')->join("ms_departement as md", "mp.id_departement", "md.id_departement")->select("mp.*", "md.id_lokasi")->orderBy('nama_pegawai', 'asc');
        $data['rekanan'] = DB::table('ms_rekanan as mr')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->select(DB::raw('mr.*, mrk.nama nama_kelompok'))->orderby('mr.nama', 'asc')->get();
        $data['makelar'] = DB::table('ms_rekanan_kelompok')->orderby('nama', 'asc')->get();

        $data['proses_stok'] = DB::table('ms_proses')->where('parent', '!=', '0')->where('jenis', '0');
        $data['proses_multi'] = DB::table('ms_proses')->where('parent', '!=', '0');// ->where('jenis', '1');
        $data['proses_tenun'] = DB::table('ms_proses')->where('parent', '!=', '0')->where('jenis', '0')->where('nama_proses', 'like', '%tenun%')->orwhere('nama_proses', 'like', '%sekir%');

        if($id_lokasi != 0 && $id_lokasi != ''){
            $data['lokasi'] = $data['lokasi']->where('id_lokasi', explode(", ", $id_lokasi));
            $data['mesin'] = $data['mesin']->where('id_lokasi', explode(", ", $id_lokasi));
            $data['gudang'] = $data['gudang']->whereIn("id_gudang", explode(",", $id_gudang)); //->where("id_lokasi", explode(", ", $id_lokasi))
            $data['spk'] = $data['spk']->where("id_lokasi", explode(", ", $id_lokasi));
            $data['pegawai'] = $data['pegawai']->where("id_lokasi", explode(", ", $id_lokasi));
            $data['proses_stok'] = $data['proses_stok']->whereIn('id_proses', explode(",", $id_proses));
            $data['proses_multi'] = $data['proses_multi']->whereIn('id_proses', explode(",", $id_proses));
            $data['proses_tenun'] = $data['proses_tenun']->whereIn('id_proses', explode(",", $id_proses));

        }

        $data['pegawai'] = $data['pegawai']->get();
        $data['mesin'] = $data['mesin']->get();
        return view('produksi.LaporanproduksiList')->with('data', $data);

    }

    function get_barang(Request $request){
        $id_kategori = $request->get('id');
        $nama        = $request->get('nama');
        $jenis       = $request->get('jenis');

        $d_query = 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')->where('mkb2.id_kb','=',$id_kategori)->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('mkb2.nama', 'asc')->orderBy('mkb.nama', 'asc')->orderBy('kode_barang', 'asc')->orderBy('nama_barang', 'asc');
        $d_query_all = 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('mkb2.nama', 'asc')->orderBy('mkb.nama', 'asc')->orderBy('kode_barang', 'asc')->orderBy('nama_barang', 'asc');
        
        if($d_query->count() > 0 AND $id_kategori <> '' AND $jenis == 1){
            $arr['data'] = $d_query->get();
            // $arr['data_all'] = array();
            $arr['status'] = array('1');
        }else if($d_query_all->count() > 0 AND $id_kategori == '' AND $jenis == 0){
            $arr['data'] = $d_query_all->get();
            // $arr['data_all'] = array();
            $arr['status'] = array('2');
        }else{
            $arr['data'] = array();
            // $arr['data_all'] = array();
            $arr['status'] = array('0');
        }

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

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

        $id_makelar = $req['id_makelar'];

        $query = DB::table("ms_rekanan_kelompok AS mrk")->leftjoin("ms_rekanan AS mr", "mrk.id_kelompok", "mr.id_kelompok")->select(DB::raw("mr.*, mrk.id_kelompok, mrk.nama nama_rekanan"))->orderby('mr.nama')->orderby('mrk.nama');

        if($id_makelar != '' && $id_makelar != 0){
            $query->where("mrk.id_kelompok", $id_makelar);
        }

        return response()->json($query->get());

    }

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

        $add_where = "";

        $wheres = array();
        $where = array();
        $where2 = array();
        $barang = array();
        $lokasi = "";
        $tanggal = ""; 
        $gudang = ["nama_gudang" => '', "lokasi_gudang" => ''];
        $warna = ["nama" => '', "inisial" => ''];
        $proses = '';
        $motif = "";
        $corak = "";
        $arr = array();
        $group_celupA = '';
        $group_celupB = '';
        $id_proses = '';

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

        if(count($req) > 0){
            $add_where = "WHERE ";
        }

        if(isset($req['id_barang'])){
            if($req['id_barang'] != ''){
            $where[] = "A.id_barang = ".$req['id_barang']." ";

            $where2[] = "B.id_barang = ".$req['id_barang']." ";
            
            $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'))->where('mb.id', $req['id_barang'])->first();
            }
        }
        
        if(isset($req['id_lokasistok'])){
            $where[] = "A.id_lokasi = ".$req['id_lokasistok']." ";
            $where2[] = "B.id_lokasi = ".$req['id_lokasistok']." ";
            
            $lokasi = DB::table('ms_lokasi_produksi')->where('id_lokasi', $req['id_lokasistok'])->first()->nama_lokasi;
        }

        if(isset($req['tgl_awal']) && isset($req['tgl_akhir'])){
            $tgl_awal = tgl_full($req['tgl_awal'], '2');
            $tgl_akhir = tgl_full($req['tgl_akhir'], '2');
            
            $where[] = "A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";
            $where2[] = "B.tanggal < '$tgl_awal' ";
        }

        if(isset($req['id_motif'])){
            if($req['id_motif'] != ''){
                $where[] = "A.id_motif = ".$req['id_motif']." ";
                $where2[] = "B.id_motif = ".$req['id_motif']." ";

                $motif = DB::table('ms_motif')->where('id_motif',$req['id_motif'])->first()->nama_motif;
            }
        }

        if(isset($req['id_corak'])){
            if($req['id_corak'] != ''){
                $where[] = "A.id_corak = ".$req['id_corak']." ";
                $where2[] = "B.id_corak = ".$req['id_corak']." ";

                $corak = DB::table('ms_motif_barang')->where('id_motif_barang',$req['id_corak'])->first()->motif_keterangan;
            }
        }

        if(isset($req['id_warna'])){
            if($req['id_warna'] != ''){
                $where[] = "A.id_komposisi = ".$req['id_warna']." ";
                $where2[] = "B.id_komposisi = ".$req['id_warna']." ";

                $d_warna = DB::table('ms_warnakomposisi')->where('id_komposisi', $req['id_warna'])->first();
                $warna = ["nama" => $d_warna->nama, "inisial" => $d_warna->inisial];
            }
        }

        if(isset($req['id_proses'])){
            if($req['id_proses'] != ''){
                $where[] = "A.id_proses_sekarang = ".$req['id_proses']." ";
                $where2[] = "B.id_proses_sekarang = ".$req['id_proses']." ";
                
                $proses = DB::table('ms_proses')->where('id_proses', $req['id_proses'])->first()->nama_proses;

                if($req['id_proses'] == '18'){
                    $group_celupA = $this->groupby_pro_celup('A');
                    $group_celupB = $this->groupby_pro_celup('B');
                }else{
                    $group_celupA = $this->groupby_pro('A');
                    $group_celupB = $this->groupby_pro('B');
                }

                $id_proses = $req['id_proses'];
            }
        }       


        $data['induk'] = array("barang" => $barang, 'nama_lokasi' => $lokasi, 'tanggal_awal' => $tgl_awal, 'tanggal_akhir' => $tgl_akhir, 'nama_gudang' => $gudang['nama_gudang'], 'lokasi_gudang' => $gudang['lokasi_gudang'], 'nama_komposisi' => $warna['nama'], 'inisial_komposisi' => $warna['inisial'], 'nama_proses' => $proses, "nama_motif" => $motif, "nama_corak" => $corak);
        
        $w1 = implode(" AND ", $where);
        $w2 = implode(" AND ", $where2);
        

        
        $data['where'] = $add_where.$w1.$group_celupA;      
        $data['where2'] = $add_where.$w2.$group_celupB;

        $data['where_mutasi'] = $add_where.$w1;
        
        $d_range = get_rawquery(get_produksiview($data['where_mutasi'])); 
        
        $d_query = get_rawquery(get_produksiawal($data['where'], $data['where2'])); 

        // print_r("\n");
        // print_r(count(get_rawquery(get_produksiview($data['where_mutasi']))));
    
        foreach($d_query as $d){
            if($d['jenis'] == 0){
                $wheres[] = array('id' => $d['id'], 
                            'id_barang' => $d['id_barang'], 
                            'kode_barang' => $d['kode_barang'],
                            'nama_barang' => $d['nama_barang'],
                            'id_kategori' => $d['id_kategori'],
                            'nama_kategori' => $d['nama_kategori'],
                            'id_subkategori' => $d['id_subkategori'],
                            'nama_subkategori' => $d['nama_subkategori'],
                            'jumlah' => '0',
                            'masuk' => '0',
                            'keluar' => '0',
                            'id_gudang' => $d['id_gudang'],
                            'nama_gudang' => $d['nama_gudang'],
                            'tanggal' => $req['tgl_awal'],
                            'id_proses' => $d['id_proses'],
                            'nama_proses' => $d['nama_proses'],
                            'id_komposisi' => $d['id_komposisi'],
                            'nama_komposisi' => $d['nama_komposisi'],
                            'inisial_komposisi' => $d['inisial_warna'],
                            'id_satuan' => $d['id_satuan'],
                            'nama_satuan' => $d['nama_satuan'],
                            'id_corak' => $d['id_corak'],
                            'nama_corak' => $d['nama_corak'],
                            'id_motif' => $d['id_motif'],
                            'nama_motif' => $d['nama_motif'],
                            'id_workorder' => $d['id_workorder'],
                            'no_workorder' => $d['no_workorder'],
                            'id_workorder_barang' => $d['id_workorder_barang'],
                            'no_workorder_barang' => $d['no_workorder_barang'],
                            '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_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_cap' => $d['id_cap'],
                            'nama_cap' => $d['nama_cap'],
                            'id_kualitas_proses' => $d['id_kualitas_proses'],
                            'nama_kualitas_proses' => $d['nama_kualitas_proses'],
                            'id_kualitas_periksa' => $d['id_kualitas_periksa'],
                            'nama_kualitas_periksa' => $d['nama_kualitas_periksa'],
                            'id_perubahansatuan' => $d['id_perubahansatuan'],
                            'nama_perubahansatuan' => $d['nama_perubahansatuan'],
                            'jumlah_rencana' => $d['jumlah_rencana'],
                            'ket_1' => "",
                            'ket_2' => "",
                            'ket_3' => "Stok Awal",
                            'ket_4' => "",
                            'ket_5' => $d['ket_5'],
                            'stok' => '0',
                            'nama_warna' => $d['nama_warna'],
                            'inisial_warna' => $d['inisial_warna'],
                            'ket_proses' => $d['ket_proses'],
                            'id_proses_sekarang' => $d['id_proses_sekarang']
                            );
            }else{
                $wheres[] = array('id' => $d['id'], 
                            'id_barang' => $d['id_barang'], 
                            'kode_barang' => $d['kode_barang'],
                            'nama_barang' => $d['nama_barang'],
                            'id_kategori' => $d['id_kategori'],
                            'nama_kategori' => $d['nama_kategori'],
                            'id_subkategori' => $d['id_subkategori'],
                            'nama_subkategori' => $d['nama_subkategori'],
                            'jumlah' => $d['stok'],
                            'masuk' => $d['stok']+$d['keluar'],
                            'keluar' => $d['keluar'],
                            'id_gudang' => $d['id_gudang'],
                            'nama_gudang' => $d['nama_gudang'],
                            'tanggal' => $tgl_awal,
                            'id_proses' => $d['id_proses'],
                            'nama_proses' => $d['nama_proses'],
                            'id_komposisi' => $d['id_komposisi'],
                            'nama_komposisi' => $d['nama_komposisi'],
                            'inisial_komposisi' => $d['inisial_warna'],
                            'id_satuan' => $d['id_satuan'],
                            'nama_satuan' => $d['nama_satuan'],
                            'id_corak' => $d['id_corak'],
                            'nama_corak' => $d['nama_corak'],
                            'id_motif' => $d['id_motif'],
                            'nama_motif' => $d['nama_motif'],
                            'id_workorder' => $d['id_workorder'],
                            'no_workorder' => $d['no_workorder'],
                            'id_workorder_barang' => $d['id_workorder_barang'],
                            'no_workorder_barang' => $d['no_workorder_barang'],
                            '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_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_cap' => $d['id_cap'],
                            'nama_cap' => $d['nama_cap'],
                            'id_kualitas_proses' => $d['id_kualitas_proses'],
                            'nama_kualitas_proses' => $d['nama_kualitas_proses'],
                            'id_kualitas_periksa' => $d['id_kualitas_periksa'],
                            'nama_kualitas_periksa' => $d['nama_kualitas_periksa'],
                            'id_perubahansatuan' => $d['id_perubahansatuan'],
                            'nama_perubahansatuan' => $d['nama_perubahansatuan'],
                            'jumlah_rencana' => $d['jumlah_rencana'],
                            'ket_1' => $d['ket_1'],
                            'ket_2' => $d['ket_2'],
                            'ket_3' => "Stok Awal",
                            'ket_4' => "",
                            'ket_5' => $d['ket_5'],
                            'stok' => $d['stok'],
                            'nama_warna' => $d['nama_warna'],
                            'inisial_warna' => $d['inisial_warna'],
                            'ket_proses' => $d['ket_proses'],
                            'id_proses_sekarang' => $d['id_proses_sekarang']
                            );
            }
        }

        $new = array();

        foreach($wheres as $w){
            
            $arr[] = $w+array('total' => $w['stok'], 'jenis' => '0');
            $sisa = $w['stok'];
            
            $new = array_filter($d_range, function ($var) use ($w, $id_proses) {

                if($id_proses == '18'){
                    return $var['id_barang'] == $w['id_barang']  && $var['id_proses_sekarang'] == $w['id_proses_sekarang']  &&   $var['id_satuan'] == $w['id_satuan'] && $var['id_corak'] == $w['id_corak'] &&   $var['id_motif'] == $w['id_motif'] &&   $var['id_workorder'] == $w['id_workorder'] &&   $var['id_workorder_barang'] == $w['id_workorder_barang'] && $var['id_kualitas_dudul'] == $w['id_kualitas_dudul'] && $var['id_label'] == $w['id_label'] &&   $var['id_kemasan'] == $w['id_kemasan'] &&   $var['id_kualitas'] == $w['id_kualitas'] && $var['id_kemasansatuan'] == $w['id_kemasansatuan'] &&   $var['id_kemasanseratus'] == $w['id_kemasanseratus'] && $var['id_cap'] == $w['id_cap'] &&   $var['id_kualitas_proses'] == $w['id_kualitas_proses'] &&   $var['id_kualitas_periksa'] == $w['id_kualitas_periksa'] && $var['id_perubahansatuan'] == $w['id_perubahansatuan'] &&   $var['jumlah_rencana'] == $w['jumlah_rencana'];
                }else{



                return $var['id_barang'] == $w['id_barang'] &&  $var['id_proses_sekarang'] == $w['id_proses_sekarang'] && $var['id_komposisi'] == $w['id_komposisi'] &&   $var['id_satuan'] == $w['id_satuan'] && $var['id_corak'] == $w['id_corak'] &&   $var['id_motif'] == $w['id_motif'] &&   $var['id_workorder'] == $w['id_workorder'] &&   $var['id_workorder_barang'] == $w['id_workorder_barang'] && $var['id_kualitas_dudul'] == $w['id_kualitas_dudul'] && $var['id_label'] == $w['id_label'] &&   $var['id_kemasan'] == $w['id_kemasan'] &&   $var['id_kualitas'] == $w['id_kualitas'] && $var['id_kemasansatuan'] == $w['id_kemasansatuan'] &&   $var['id_kemasanseratus'] == $w['id_kemasanseratus'] && $var['id_cap'] == $w['id_cap'] &&   $var['id_kualitas_proses'] == $w['id_kualitas_proses'] &&   $var['id_kualitas_periksa'] == $w['id_kualitas_periksa'] ;//&& $var['id_perubahansatuan'] == $w['id_perubahansatuan'] &&   $var['jumlah_rencana'] == $w['jumlah_rencana'];
                    // $var['id_gudang'] == $w['id_gudang'] &&
                }
            });

            foreach($new as $d){
                
                $sisa = ($sisa+$d['keluar'])-$d['masuk'];                
                $arr[] = $d+array('stok' => '0', 'total' => $sisa, 'inisial_komposisi' => $d['inisial_warna'], 'jenis' => '1', 'j_awal' => $w['stok']);
            }  

            // print_r($new);
        }


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

        switch ($jenis) {
            case '0':
                $html = "MutasibarangfilterCetak"; //"MutasibarangCetak";
                break;
            case '1':
                $html = "MutasibarangfilterCetak";
                break;
            default:
                $html = "MutasibarangCetak";
                break;
        }
        require('fpdf181/Mc_table.php');
        return response(view('produksi.cetak.'.$html)->with('data', $data))->header('Content-Type', 'application/pdf');
        
    }


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

        $add_where = "";

        $wheres = array();
        $where = array();
        $where2 = array();
        $wherev = array();
        $barang = array();
        $lokasi = "";
        $tanggal = ""; 
        $gudang = ["nama_gudang" => '', "lokasi_gudang" => ''];
        $warna = ["nama" => '', "inisial" => ''];
        $proses = '';
        $motif = "";
        $corak = "";
        $arr = array();
        $arr_detail = array();
        $group_celupA = '';
        $group_celupB = '';
        $id_proses = '';
        $tgl_awal = '';
        $where_rekap = array();
        $on = ' AND A.id_komposisi = C.id_komposisi';
        $kategori = '';
        $orderby_proses = '';
        $pegawai = '';
        $rekanan = '';
        $kelompok = '';
        $mesin = '';
        $helper_tgl_awal = '';
        $helper_tgl_between = '';
        $where_tenun = array();
        $where_nontenun = array();
        $where_nonbasic = array();
        $where_view = "";

        $group_inside = '';
        $group_outside = '';

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

        if(count($req) > 0){
            $add_where = "WHERE ";
        }

        if(isset($req['id_kategoribarang'])){
            if($req['id_kategoribarang'] != ''){
                $where[] = "A.id_kategori = ".$req['id_kategoribarang']." ";

                $where2[] = "B.id_kategori = ".$req['id_kategoribarang']." ";
                $wherev[] = "v.id_kategori = ".$req['id_kategoribarang']." ";

                $where_rekap[] = "A.id_kategori = ".$req['id_kategoribarang']." ";

                $where_tenun[] = "B.id_kategori = ".$req['id_kategoribarang']." ";
                $where_nontenun[] = "B.id_kategori = ".$req['id_kategoribarang']." ";
                $kategori =  DB::table('ms_kategori_barang')->where('id_kb','=',$req['id_kategoribarang'])->first()->nama;
            }
        }

        if(isset($req['id_barang'])){
            if($req['id_barang'] != ''){
            $where[] = "A.id_barang = ".$req['id_barang']." ";

            $where2[] = "B.id_barang = ".$req['id_barang']." ";
            $wherev[] = "v.id_barang = ".$req['id_barang']." ";

            $where_rekap[] = "A.id_barang = ".$req['id_barang']." ";
            
            $where_tenun[] = "B.id_barang = ".$req['id_barang']." ";
            $where_nontenun[] = "B.id_barang = ".$req['id_barang']." ";
            $where_nonbasic[] = "id_barang = ".$req['id_barang']." ";
            $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'))->where('mb.id', $req['id_barang'])->first();
            }
        }
        
        if(isset($req['id_lokasistok'])){
            $where[] = "A.id_lokasi = ".$req['id_lokasistok']." ";
            $where2[] = "B.id_lokasi = ".$req['id_lokasistok']." ";
            $wherev[] = "v.id_lokasi = ".$req['id_lokasistok']." ";
            
            $where_rekap[] = "A.id_lokasi = ".$req['id_lokasistok']." ";

            $where_tenun[] = "B.id_lokasi = ".$req['id_lokasistok']." ";
            $where_nontenun[] = "B.id_lokasi = ".$req['id_lokasistok']." ";
            $where_nonbasic[] = "id_lokasi = ".$req['id_lokasistok']." ";
            $lokasi = DB::table('ms_lokasi_produksi')->where('id_lokasi', $req['id_lokasistok'])->first()->nama_lokasi;
        }

        if(isset($req['tgl_awal']) && isset($req['tgl_akhir'])){
            $tgl_awal = tgl_full($req['tgl_awal'], '2');
            $tgl_akhir = tgl_full($req['tgl_akhir'], '2');
            
            $where[] = "A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";
            $where2[] = "B.tanggal < '$tgl_awal' ";
            $where_rekap[] = "A.tanggal < '$tgl_awal'";
            $helper_tgl_awal = "tanggal < '$tgl_awal'";
            $helper_tgl_between = "tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";
        //    $wherev[] = "v.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";
        }

        if(isset($req['id_motif'])){
            if($req['id_motif'] != ''){
                $where[] = "A.id_motif = ".$req['id_motif']." ";
                $where2[] = "B.id_motif = ".$req['id_motif']." ";
                $wherev[] = "v.id_motif = ".$req['id_motif']." ";
                $where_rekap[] = "A.id_motif = ".$req['id_motif']." ";

                $where_tenun[] = "B.id_motif = ".$req['id_motif']." ";
                $where_nontenun[] = "B.id_motif = ".$req['id_motif']." ";
                $where_nonbasic[] = "id_motif = ".$req['id_motif']." ";
                $motif = DB::table('ms_motif')->where('id_motif',$req['id_motif'])->first()->nama_motif;
            }
        }

        if(isset($req['id_corak'])){
            if($req['id_corak'] != ''){
                $where[] = "A.id_corak = ".$req['id_corak']." ";
                $where2[] = "B.id_corak = ".$req['id_corak']." ";
                $wherev[] = "v.id_corak = ".$req['id_corak']." ";
                $where_rekap[] = "A.id_corak = ".$req['id_corak']." ";

                $where_tenun[] = "B.id_corak = ".$req['id_corak']." ";
                $where_nontenun[] = "B.id_corak = ".$req['id_corak']." ";
                $where_nonbasic[] = "id_corak = ".$req['id_corak']." ";
                $corak = DB::table('ms_motif_barang')->where('id_motif_barang',$req['id_corak'])->first()->motif_keterangan;
            }
        }

        if(isset($req['id_warna'])){
            if($req['id_warna'] != ''){
                $where[] = "A.id_warna = ".$req['id_warna']." ";
                $where2[] = "B.id_warna = ".$req['id_warna']." ";
                $wherev[] = "v.id_warna = ".$req['id_warna']." ";

                $where_rekap[] = "A.id_warna = ".$req['id_warna']." ";

                $where_tenun[] = "B.id_warna = ".$req['id_warna']." ";
                $where_nontenun[] = "B.id_warna = ".$req['id_warna']." ";
                $where_nonbasic[] = "id_warna = ".$req['id_warna']." ";
                $d_warna = DB::table('ms_warnakomposisi')->where('id_komposisi', $req['id_warna'])->first();
                $warna = ["nama" => $d_warna->nama, "inisial" => $d_warna->inisial];

                // if(isset($req['id_proses'])){
                //     if($req['id_proses'] == '18'){
                //         $where[] = "A.id_warna = ".$req['id_warna']." ";
                //     }
                // }
            }
        }

        if(isset($req['id_proses'])){
            if($req['id_proses'] != ''){
                $where[] = "A.id_proses_sekarang = ".$req['id_proses']." ";
                $where2[] = "B.id_proses_sekarang = ".$req['id_proses']." ";
                $where_rekap[] = "A.id_proses_sekarang = ".$req['id_proses']." ";
                
                $where_tenun[] = "B.id_proses_sekarang = ".$req['id_proses']." ";
                $where_nontenun[] = "B.id_proses_sekarang = ".$req['id_proses']." ";
                $where_nonbasic[] = "id_proses_sekarang = ".$req['id_proses']." ";

                $where_view = "WHERE id_proses_sekarang = ".$req['id_proses']." ";
                $proses = DB::table('ms_proses')->where('id_proses', $req['id_proses'])->first()->nama_proses;

                if($req['id_proses'] == '18'){
                    $group_celupA = $this->groupby_pro_celup('A');
                    $group_celupB = $this->groupby_pro_celup('B');
                    $on = '';

                    $group_inside = $this->groupby_pro_celup('C');
                    $group_outside = $this->groupby_pro_celup('A');
                }else{
                    $group_celupA = $this->groupby_pro('A');
                    $group_celupB = $this->groupby_pro('B');

                    $group_inside = $this->groupby_pro('C');
                    $group_outside = $this->groupby_pro('A');
                }

                $id_proses = $req['id_proses'];
            }else{
                $proses = "Semua Proses";
                $group_celupA = $this->groupby_pro('A');
                $group_celupB = $this->groupby_pro('B');

                $group_inside = $this->groupby_pro('C');
                $group_outside = $this->groupby_pro('A');
            }

            if(in_array($req['id_proses'], setup_benang())){
                $orderby_proses = " ORDER BY A.nama_subkategori, A.nama_barang";
            }else if(in_array($req['id_proses'], setup_corak())){
                $orderby_proses = " ORDER BY A.nama_corak, A.nama_motif";
            }else{

            }

            $orderby_proses .= ", A.nama_komposisi";

        }else{
            $proses = "Semua Proses";
            $group_celupA = $this->groupby_pro('A');
            $group_celupB = $this->groupby_pro('B');

            $group_inside = $this->groupby_pro('C');
            $group_outside = $this->groupby_pro('A');
        }

        if(isset($req['id_pegawai_proses'])){
            if($req['id_pegawai_proses'] != 0){
                $where[] = "A.id_pegawai = ".$req['id_pegawai_proses']." ";
                $where2[] = "B.id_pegawai = ".$req['id_pegawai_proses']." ";
                $wherev[] = "v.id_pegawai = ".$req['id_pegawai_proses']." ";
                $where_rekap[] = "A.id_pegawai = ".$req['id_pegawai_proses']." ";

                $where_tenun[] = "B.id_pegawai = ".$req['id_pegawai_proses']." ";
                $where_nontenun[] = "B.id_pegawai = ".$req['id_pegawai_proses']." ";
                $where_nonbasic[] = "id_pegawai = ".$req['id_pegawai_proses']." ";
                $pegawai = DB::table('ms_pegawai')->where('id_pegawai', $req['id_pegawai_proses'])->first()->nama_pegawai;
            }else if($req['id_pegawai_proses'] == '0'){
                $pegawai = "Semua Pegawai";
                $where[] = "A.id_pegawai != '' ";
                $where2[] = "B.id_pegawai != '' ";
                $wherev[] = "v.id_pegawai != '' ";
                $where_rekap[] = "A.id_pegawai != '' ";
                
                $where_tenun[] = "B.id_pegawai != '' ";
                $where_nontenun[] = "B.id_pegawai != '' ";
                $where_nonbasic[] = "id_pegawai != '' ";
            }else{
                $pegawai = "";
            }
        }

        if(isset($req['id_makelar_proses'])){
            if($req['id_makelar_proses'] != 0){
                $d_petenun = DB::table('ms_rekanan as mr')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->where("mrk.id_kelompok", $req['id_makelar_proses'])->select(DB::raw("mr.*, mrk.nama nama_kelompok"))->get();
                $d_count = $d_petenun->count();
                $d_kelompok = $d_petenun->first()->nama_kelompok;
                $d_petenun = $d_petenun->pluck('id_rekanan')->toArray();
                
                $where[] = "A.id_rekanan IN ('".implode("', '", $d_petenun)."') ";
                $where2[] = "B.id_rekanan IN ('".implode("', '", $d_petenun)."') ";
                $wherev[] = "v.id_rekanan IN ('".implode("', '", $d_petenun)."') ";
                $where_rekap[] = "A.id_rekanan IN ('".implode("', '", $d_petenun)."') ";
                
                $where_tenun[] = "B.id_rekanan IN ('".implode("', '", $d_petenun)."') ";
                $where_nontenun[] = "B.id_rekanan IN ('".implode("', '", $d_petenun)."') ";
                $where_nonbasic[] = "id_rekanan IN ('".implode("', '", $d_petenun)."') ";
                // print_r($d_petenun); exit();
                $kelompok = $d_kelompok;
            }else if($req['id_rekanan_proses'] == 0){
                $kelompok = "Semua Makelar";
            }else{

            }
        }else{

        }

        if(isset($req['id_rekanan_proses'])){
            if($req['id_rekanan_proses'] != 0){
                $where[] = "A.id_rekanan = ".$req['id_rekanan_proses']." ";
                $where2[] = "B.id_rekanan = ".$req['id_rekanan_proses']." ";
                $wherev[] = "v.id_rekanan = ".$req['id_rekanan_proses']." ";
                $where_rekap[] = "A.id_rekanan = ".$req['id_rekanan_proses']." ";
                
                $where_tenun[] = "B.id_rekanan = ".$req['id_rekanan_proses']." ";
                $where_nontenun[] = "B.id_rekanan = ".$req['id_rekanan_proses']." ";
                $where_nonbasic[] = "id_rekanan = ".$req['id_rekanan_proses']." ";
                $d_data = DB::table('ms_rekanan as mr')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->where('id_rekanan', $req['id_rekanan_proses'])->select(DB::raw("mr.*, mrk.nama nama_kelompok"))->first();
                $rekanan = $d_data->nama; //.' - '.$d_data->nama_kelompok;
                if($kelompok == ''){
                    $kelompok = $d_data->nama_kelompok;
                }
            }else if($req['id_rekanan_proses'] == '0'){
                $where[] = "A.id_rekanan != '' ";
                $where2[] = "B.id_rekanan != '' ";
                $wherev[] = "v.id_rekanan != '' ";
                $where_rekap[] = "A.id_rekanan != '' ";
                $rekanan = "Semua Rekanan";
            }else{
                $rekanan = "";
            }  
        }

        if(isset($req['id_mesin_proses'])){
            if($req['id_mesin_proses'] != 0){
                $where[] = "A.id_mesin = ".$req['id_mesin_proses']." ";
                $where2[] = "B.id_mesin = ".$req['id_mesin_proses']." ";
                $wherev[] = "v.id_mesin = ".$req['id_mesin_proses']." ";
                $where_rekap[] = "A.id_mesin = ".$req['id_mesin_proses']." ";

                $where_tenun[] = "B.id_mesin = ".$req['id_mesin_proses']." ";
                $where_nontenun[] = "B.id_mesin = ".$req['id_mesin_proses']." ";
                $where_nonbasic[] = "id_mesin = ".$req['id_mesin_proses']." ";
                $mesin = DB::table('ms_mesin')->where('id_mesin', $req['id_mesin_proses'])->first()->nama;
            }else if($req['id_mesin_proses'] == '0'){
                $mesin = "Semua Mesin";
                $where[] = "A.id_mesin != '' ";
                $where2[] = "B.id_mesin != '' ";
                $wherev[] = "v.id_mesin != '' ";
                $where_rekap[] = "A.id_mesin != '' ";
                $where_tenun[] = "B.id_mesin != '' ";
            }else{
                $mesin = "";
            }
        }




        $data['induk'] = array("barang" => $barang, 'nama_lokasi' => $lokasi, 'tanggal_awal' => $tgl_awal, 'tanggal_akhir' => $tgl_akhir, 'nama_gudang' => $gudang['nama_gudang'], 'lokasi_gudang' => $gudang['lokasi_gudang'], 'nama_komposisi' => $warna['nama'], 'inisial_komposisi' => $warna['inisial'], 'nama_proses' => $proses, "nama_motif" => $motif, "nama_corak" => $corak, "pegawai" => $pegawai, "rekanan" => $rekanan, "kelompok" => $kelompok, "mesin" => $mesin);
        
        $wherev[] = "v.id_proses_sekarang = 37";
        $wherev[] = "v.id_proses = 24";
        $w1 = implode(" AND ", $where);
        $w2 = implode(" AND ", $where2);
        $v1 = implode(" AND ", $wherev);
        $vr = implode(" AND ", $where_rekap);

        $w_tenun = implode(" AND ", $where_tenun);
        $w_nontenun = implode(" AND ", $where_nontenun);
        $w_nonbasic = implode(" AND ", $where_nonbasic);

        
        /*Celup tidak menyertakan concat atau group warna*/

        if(isset($req['id_proses'])){
            if($req['id_proses'] == '25'){
                $group_celupA = str_replace(", A.id_corak", "", $group_celupA);
                $group_celupB = str_replace(", B.id_corak", "", $group_celupB);

                $group_outside = str_replace(", A.id_corak", "", $group_outside);
                $group_inside = str_replace(", C.id_corak", "", $group_inside);
            }else if($req['id_proses'] == '24' || $req['id_proses'] == '94'){
                $group_celupA = "GROUP BY A.id_proses_sekarang, A.id_workorder_barang";
                $group_celupB = "GROUP BY B.id_proses_sekarang, B.id_workorder_barang";

                $group_inside = "GROUP BY C.id_proses_sekarang, C.id_workorder_barang";
                $group_outside = "GROUP BY A.id_proses_sekarang, A.id_workorder_barang";
            }else if($req['id_proses'] == '41'){
                $group_celupA = str_replace(", A.id_workorder_barang", "", $group_celupA);
                $group_celupB = str_replace(", B.id_workorder_barang", "", $group_celupB);

                $group_inside = str_replace(", C.id_workorder_barang", "", $group_inside);
                $group_outside = str_replace(", A.id_workorder_barang", "", $group_outside);
            }else if($req['id_proses'] == '22'){
                $group_celupA = str_replace(", A.id_komposisi", "", $group_celupA);
                $group_celupB = str_replace(", B.id_komposisi", "", $group_celupB);

                $group_inside = str_replace(", C.id_komposisi", "", $group_inside);
                $group_outside = str_replace(", A.id_komposisi", "", $group_outside);
            }else if($req['id_proses'] == '37'){
                $group_celupA = str_replace(", A.id_motif", "", $group_celupA);
                $group_celupB = str_replace(", B.id_motif", "", $group_celupB);

                $group_inside = str_replace(", C.id_motif", "", $group_inside);
                $group_outside = str_replace(", A.id_motif", "", $group_outside);
            }else if($req['id_proses'] == '60'){ //lokal 60, server 80
                $group_celupA = str_replace(["A.id_barang,", "A.id_motif,"], ["", ''], $group_celupA);
                $group_celupB = str_replace(["B.id_barang,", "B.id_motif,"], ["", ''], $group_celupB);

                $group_inside = str_replace(["C.id_barang,", "C.id_motif,"], ["", ''], $group_inside);
                $group_outside = str_replace(["A.id_barang,", "A.id_motif,"], ["", ''], $group_outside);
            }
        }

        if(isset($req['id_pegawai_proses'])){
            if($req['id_pegawai_proses'] != '0'){
                $group_celupA .= ", A.id_pegawai";
                $group_celupB .= ", B.id_pegawai";

                $group_inside .= ", C.id_pegawai";
                $group_outside .= ", A.id_pegawai";
            }
        }

        if(isset($req['id_rekanan_proses'])){
            if($req['id_rekanan_proses'] != '0'){
                $group_celupA .= ", A.id_rekanan";
                $group_celupB .= ", B.id_rekanan";

                $group_inside .= ", C.id_rekanan";
                $group_outside .= ", A.id_rekanan";
            }
        }

        if(isset($req['id_mesin_proses'])){
            if($req['id_mesin_proses'] != '0'){
                $group_celupA .= ", A.id_mesin";
                $group_celupB .= ", B.id_mesin";

                $group_inside .= ", C.id_mesin";
                $group_outside .= ", A.id_mesin";
            }
        }
        
        $data['where'] = $add_where.$w1.$group_celupA;      
        $data['where2'] = $add_where.$w2.$group_celupB;
        $data['where3'] = $add_where.$w2.$group_celupA;
        $data['wherev'] = $add_where.$v1."  GROUP BY  v.id_workorder_barang"; // v.id_motif,



        $data['where_mutasi'] = $add_where.$w1;

        $data['where_tenun_awal'] = $add_where.$w_tenun." AND $helper_tgl_awal AND B.id_proses IN (24, 37) GROUP BY B.id_proses_sekarang, B.id_workorder_barang, B.id_motif, B.id_proses ORDER BY B.id_proses, B.id_motif, B.id_komposisi";
        $data['where_tenun_bet'] = $add_where.$w_tenun." AND $helper_tgl_between AND B.id_proses IN (24, 37) GROUP BY B.id_proses_sekarang, B.id_workorder_barang, B.id_motif, B.id_proses ORDER BY B.id_proses, B.id_motif, B.id_komposisi";
        $data['where_tenun_bet_detail'] = $add_where.$w_tenun." AND $helper_tgl_between AND B.id_proses IN (24, 37)";

        $data['where_nontenun_awal'] = $add_where.$w_nontenun." AND $helper_tgl_awal ";
        $data['where_nontenun_bet'] = $add_where.$w_nontenun." AND $helper_tgl_between ";
        $data['where_nonbasic'] = $add_where.$w_nonbasic;
        
        if($jenis == 1){
            $where_pro = '';
            if($id_proses != 37){
                if($id_proses == 41){
                    $group_celupA = str_replace(", A.id_workorder_barang", "", $group_celupA);

                    $group_inside = str_replace(", C.id_workorder_barang", "", $group_inside);
                    $group_outside = str_replace(", A.id_workorder_barang", "", $group_outside);
                }else if($id_proses == '18'){
                    if($req['id_warna'] == '' ){
                        $group_celupA = $group_celupA.", A.id_warna";

                        $group_inside = $group_inside.", C.id_warna";
                        $group_outside = $group_outside.", A.id_warna";
                    }
                }else if($id_proses == 24){
                    $order_ = "A.nama_proses DESC, ";
                    $where_pro = ", C.id_proses ORDER BY nama_proses DESC";
                }

                $concat = $this->concat_produksi($id_proses, 'C');

                // print_r($concat); exit();

                $detail[] = "SELECT X.*, CASE WHEN Z.id_sort IS NULL THEN X.id_sort ELSE Z.id_sort END AS id_sort_awal, CASE WHEN Z.id_sort_detail IS NULL THEN 0 ELSE Z.id_sort_detail END AS id_sort_detail_awal, CASE WHEN Z.jumlah_awal IS NULL THEN 0 ELSE Z.jumlah_awal END AS jumlah_awal, CASE WHEN ms.id_satuan IS NULL THEN '' ELSE ms.satuan END AS inisial_satuan FROM ( SELECT C.*, C.masuk jumlah_masuk, C.keluar jumlah_keluar, 0 jumlah_awal, CONCAT(".$concat.") id_sort, 1 id_sort_detail FROM (".get_prosesnontenun($data['where_nonbasic'], $data['where_nontenun_bet']).") AS C ) AS X";
                $detail[] = "(SELECT C.*, 0 jumlah_masuk, 0 jumlah_keluar, SUM(C.keluar - C.masuk) jumlah_awal, CONCAT(".$concat.") id_sort, 0 id_sort_detail FROM (".get_prosesnontenun($data['where_nonbasic'], $data['where_nontenun_awal'].") AS C ".$group_inside.$where_pro).") AS Z";
                $join = "ON X.id_kategori = Z.id_kategori AND X.id_barang = Z.id_barang AND X.id_proses_sekarang = Z.id_proses_sekarang AND X.id_satuan = Z.id_satuan AND X.id_corak = Z.id_corak AND X.id_motif = Z.id_motif AND X.id_workorder = Z.id_workorder AND X.id_workorder_barang = Z.id_workorder_barang AND X.id_warna = Z.id_warna";

                $query = implode(" LEFT JOIN ", $detail)." ".$join." LEFT JOIN ms_satuan AS ms ON X.id_satuan = ms.id_satuan ORDER BY X.id_sort, X.id_sort_detail, X.tanggal_full";
                $query_detail = DB::select($query);

                $arr = array();
                $arr_detail = array();

                if($query_detail > 0){
                    $total = 0;
                    $total_detail = 0;
                    $jum = array();
                    $isi_arr = array();

                    $store_idwork = array();

                    foreach($query_detail as $d){
                        
                        if(!in_array($d->id_sort, $store_idwork)){
                            $ket_1 = '';
                            $ket_2 = '';
                            $ket_3 = 'Saldo Awal';
                            $ket_4 = '';
                            $ket_5 = '';
                            $total = $d->jumlah_awal;
                            $masuk = '0';
                            $keluar = '0';
                            $jumlah = $d->jumlah_awal;
                            $tanggal = $tgl_awal;
                            $id_sort = $d->id_sort_awal;
                            $id_sort_detail = $d->id_sort_detail_awal;

                            $store_idwork[] = $d->id_sort;

                            $arr[] = array('id' => $d->id, 
                                        'id_barang' => $d->id_barang, 
                                        'kode_barang' => $d->kode_barang,
                                        'nama_barang' => $d->nama_barang,
                                        'id_kategori' => $d->id_kategori,
                                        'nama_kategori' => $d->nama_kategori,
                                        'id_subkategori' => $d->id_subkategori,
                                        'kategori' => $d->nama_subkategori,
                                        'jumlah' => $jumlah,
                                        'masuk' => $masuk,
                                        'keluar' => $keluar,
                                        'id_gudang' => $d->id_gudang,
                                        'nama_gudang' => $d->nama_gudang,
                                        'tanggal' => $tanggal,
                                        'id_proses' => $d->id_proses,
                                        'nama_proses' => $d->nama_proses,
                                        'id_komposisi' => $d->id_komposisi,
                                        'nama_komposisi' => $d->nama_komposisi,
                                        'inisial_komposisi' => $d->inisial_warna,
                                        'id_satuan' => $d->id_satuan,
                                        'nama_satuan' => $d->inisial_satuan,
                                        'id_corak' => $d->id_corak,
                                        'nama_corak' => $d->nama_corak,
                                        'id_motif' => $d->id_motif,
                                        'nama_motif' => $d->nama_motif,
                                        'id_workorder' => $d->id_workorder,
                                        'no_workorder' => $d->no_workorder,
                                        'id_workorder_barang' => $d->id_workorder_barang,
                                        'no_workorder_barang' => $d->no_workorder_barang,
                                        '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_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_cap' => $d->id_cap,
                                        'nama_cap' => $d->nama_cap,
                                        'id_kualitas_proses' => $d->id_kualitas_proses,
                                        'nama_kualitas_proses' => $d->nama_kualitas_proses,
                                        'id_kualitas_periksa' => $d->id_kualitas_periksa,
                                        'nama_kualitas_periksa' => $d->nama_kualitas_periksa,
                                        'id_perubahansatuan' => $d->id_perubahansatuan,
                                        'nama_perubahansatuan' => $d->nama_perubahansatuan,
                                        'jumlah_rencana' => $d->jumlah_rencana,
                                        'ket_1' => $ket_1,
                                        'ket_2' => $ket_2,
                                        'ket_3' => $ket_3,
                                        'ket_4' => $ket_4,
                                        'ket_5' => $ket_5,
                                        'stok' => '0',
                                        'nama_warna' => $d->nama_warna,
                                        'inisial_warna' => $d->inisial_warna,
                                        'ket_proses' => $d->ket_proses,
                                        'id_sort' => $id_sort,
                                        'jenis' => $id_sort_detail,
                                        'total' => $total
                                        );
                            }
                    }
                    
                    // print_r($arr); exit();

                    foreach($arr as $a){
                        $arr_detail[] = $a;
                        $nilai_awal = $a['total'];

                        foreach($query_detail as $d){
                            if($d->id_sort == $a['id_sort']){
                                
                                $ket_1 = $d->ket_1;
                                $ket_2 = $d->ket_2;
                                $ket_3 = $d->ket_3;
                                $ket_4 = $d->ket_4;
                                $ket_5 = $d->ket_5;
                                $total_detail = ($nilai_awal+$d->jumlah_keluar)-$d->jumlah_masuk;
                                $masuk = $d->jumlah_keluar;
                                $keluar = $d->jumlah_masuk;
                                $jumlah = 0;
                                $tanggal = $d->tanggal;
                                $id_sort = $d->id_sort;
                                $id_sort_detail = $d->id_sort_detail;
                                $nilai_awal = $total_detail;

                                $arr_detail[] = array('id' => $d->id, 
                                        'id_barang' => $d->id_barang, 
                                        'kode_barang' => $d->kode_barang,
                                        'nama_barang' => $d->nama_barang,
                                        'id_kategori' => $d->id_kategori,
                                        'nama_kategori' => $d->nama_kategori,
                                        'id_subkategori' => $d->id_subkategori,
                                        'kategori' => $d->nama_subkategori,
                                        'jumlah' => $jumlah,
                                        'masuk' => $masuk,
                                        'keluar' => $keluar,
                                        'id_gudang' => $d->id_gudang,
                                        'nama_gudang' => $d->nama_gudang,
                                        'tanggal' => $tanggal,
                                        'id_proses' => $d->id_proses,
                                        'nama_proses' => $d->nama_proses,
                                        'id_komposisi' => $d->id_komposisi,
                                        'nama_komposisi' => $d->nama_komposisi,
                                        'inisial_komposisi' => $d->inisial_warna,
                                        'id_satuan' => $d->id_satuan,
                                        'nama_satuan' => $d->inisial_satuan,
                                        'id_corak' => $d->id_corak,
                                        'nama_corak' => $d->nama_corak,
                                        'id_motif' => $d->id_motif,
                                        'nama_motif' => $d->nama_motif,
                                        'id_workorder' => $d->id_workorder,
                                        'no_workorder' => $d->no_workorder,
                                        'id_workorder_barang' => $d->id_workorder_barang,
                                        'no_workorder_barang' => $d->no_workorder_barang,
                                        '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_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_cap' => $d->id_cap,
                                        'nama_cap' => $d->nama_cap,
                                        'id_kualitas_proses' => $d->id_kualitas_proses,
                                        'nama_kualitas_proses' => $d->nama_kualitas_proses,
                                        'id_kualitas_periksa' => $d->id_kualitas_periksa,
                                        'nama_kualitas_periksa' => $d->nama_kualitas_periksa,
                                        'id_perubahansatuan' => $d->id_perubahansatuan,
                                        'nama_perubahansatuan' => $d->nama_perubahansatuan,
                                        'jumlah_rencana' => $d->jumlah_rencana,
                                        'ket_1' => $ket_1,
                                        'ket_2' => $ket_2,
                                        'ket_3' => $ket_3,
                                        'ket_4' => $ket_4,
                                        'ket_5' => $ket_5,
                                        'stok' => '0',
                                        'nama_warna' => $d->nama_warna,
                                        'inisial_warna' => $d->inisial_warna,
                                        'ket_proses' => $d->ket_proses,
                                        'id_sort' => $id_sort,
                                        'jenis' => $id_sort_detail,
                                        'total' => $total_detail
                                        );
                            }
                        }

                    }

                    // print_r($arr_detail); exit();

                    foreach($arr_detail as $a){
                        if(isset($jum[$a['id_sort']])){
                            $jum[$a['id_sort']] = ['jumlah' => $jum[$a['id_sort']]['jumlah']+$a['jenis']];
                        }else{
                            $jum[$a['id_sort']] = ['jumlah' => $a['jenis']];
                        }
                    }



                    foreach($jum as $j => $jd){
                        if($jd['jumlah'] == 0){
                            $isi_arr[] = $j;
                        }
                    }
                    
                    // print_r($isi_arr);
                    // print_r(removeElementWithValue($arr_detail, 'id_sort', $isi_arr));
                    // exit();
                    $arr_detail = removeElementWithValue($arr_detail, 'id_sort', $isi_arr);

                }else{
                    $arr_detail = array();
                }

                $data['detail'] = barang_byproses($arr_detail, 5, $proses);

                // print_r($data['detail']); exit();
                $html = "MutasiproduksiDetailCetak";
            }else{
                
                $where_sekir = $add_where.$w2." AND B.id_proses = 24 ".$group_celupB;
                $where_hasil = $add_where.$w2." AND B.id_proses = 37 ".$group_celupB;
                $where_sisa = $data['wherev']; // ." HAVING sisa_setor > 0";
                $where_detail = $add_where.$w1; //." AND A.id_proses = '37'";
                // $query = DB::select("SELECT A.*, ms.satuan as inisial_satuan FROM (".get_mutasitenun($where_sekir, $where_hasil, $where_sisa, $where_detail).") A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan_hasil ORDER BY A.id_sort ASC, A.id_sort_detail, A.tanggal_full");
                $select_detail = ", CASE WHEN B.id_proses = 24 AND B.jumlah_rencana != '' AND B.keluar > 0 THEN B.jumlah_rencana WHEN B.id_proses = 24 AND B.jumlah_rencana = '' THEN B.keluar*20 WHEN B.id_proses = 37 AND B.keluar > 0 THEN B.keluar ELSE 0 END AS data_keluar, CASE WHEN B.id_proses = 24 AND B.jumlah_rencana != '' AND B.masuk > 0 THEN B.jumlah_rencana WHEN B.id_proses = 24 AND B.jumlah_rencana = '' THEN B.masuk*20 WHEN B.id_proses = 37 AND B.masuk > 0 THEN B.masuk + 0 ELSE 0 END AS data_masuk, CASE WHEN B.id_perubahansatuan IS NULL OR B.id_perubahansatuan = '' THEN 11 ELSE B.id_perubahansatuan END AS id_satuanhasil, CONCAT(B.id_proses_sekarang, B.id_workorder_barang) AS id_sort, 1 id_sort_detail";

                $d_detail[] = "SELECT X.*, CASE WHEN Z.id_sort IS NULL THEN X.id_sort ELSE Z.id_sort END AS id_sort_awal, CASE WHEN Z.id_sort_detail IS NULL THEN 0 ELSE Z.id_sort_detail END AS id_sort_detail_awal, CASE WHEN Z.jumlah_awal IS NULL THEN 0 ELSE Z.jumlah_awal END AS jumlah_awal, CASE WHEN ms.id_satuan IS NULL THEN '' ELSE ms.satuan END AS inisial_satuan FROM ( ".get_prosestenun_detail($select_detail, $data['where_tenun_bet_detail']).") AS X";
                $d_detail[] = "( ".get_prosestenun($data['where_tenun_awal'], '', ", '0' jumlah_masuk, '0' jumlah_keluar, SUM(C.sekir_keluar)-SUM(C.jumlah_setor+C.sekir_masuk) jumlah_awal, CONCAT(C.id_proses_sekarang, C.id_workorder_barang) AS id_sort, 0 id_sort_detail").") AS Z";
                $join = "ON X.id_proses_sekarang = Z.id_proses_sekarang AND X.id_motif = Z.id_motif AND X.id_workorder_barang = Z.id_workorder_barang AND X.id_rekanan = Z.id_rekanan AND X.id_pegawai = Z.id_pegawai AND X.id_mesin = Z.id_mesin LEFT JOIN ms_satuan AS ms ON X.id_satuanhasil = ms.id_satuan ORDER BY nama_motif, tanggal_full ASC";// ORDER BY X.tanggal_full, X.nama_motif, X.inisial_warna, X.no_workorder_barang";

                // print_r(implode(" LEFT JOIN ", $d_detail)." ".$join); exit();

                $query = DB::select(implode(" LEFT JOIN ", $d_detail)." ".$join);
                
                if($query > 0){
                    $total = 0;
                    $total_detail = 0;
                    $jum = array();
                    $isi_arr = array();

                    $store_idwork = array();

                    foreach($query as $d){
                        
                     if(!in_array($d->id_workorder_barang, $store_idwork)){
                        $ket_1 = '';
                        $ket_2 = '';
                        $ket_3 = 'Saldo Awal';
                        $ket_4 = '';
                        $ket_5 = '';
                        $total = $d->jumlah_awal;
                        $masuk = '0';
                        $keluar = '0';
                        $jumlah = $d->jumlah_awal;
                        $tanggal = $tgl_awal;
                        $id_sort = $d->id_sort_awal;
                        $id_sort_detail = $d->id_sort_detail_awal;

                        $store_idwork[] = $d->id_workorder_barang;

                        $arr[] = array('id' => $d->id, 
                                    'id_barang' => $d->id_barang, 
                                    'kode_barang' => $d->kode_barang,
                                    'nama_barang' => $d->nama_barang,
                                    'id_kategori' => $d->id_kategori,
                                    'nama_kategori' => $d->nama_kategori,
                                    'id_subkategori' => $d->id_subkategori,
                                    'kategori' => $d->nama_subkategori,
                                    'jumlah' => $jumlah,
                                    'masuk' => $masuk,
                                    'keluar' => $keluar,
                                    'id_gudang' => $d->id_gudang,
                                    'nama_gudang' => $d->nama_gudang,
                                    'tanggal' => $tanggal,
                                    'id_proses' => $d->id_proses,
                                    'nama_proses' => $d->nama_proses,
                                    'id_komposisi' => $d->id_komposisi,
                                    'nama_komposisi' => $d->nama_komposisi,
                                    'inisial_komposisi' => $d->inisial_warna,
                                    'id_satuan' => $d->id_satuanhasil,
                                    'nama_satuan' => $d->inisial_satuan,
                                    'id_corak' => $d->id_corak,
                                    'nama_corak' => $d->nama_corak,
                                    'id_motif' => $d->id_motif,
                                    'nama_motif' => $d->nama_motif,
                                    'id_workorder' => $d->id_workorder,
                                    'no_workorder' => $d->no_workorder,
                                    'id_workorder_barang' => $d->id_workorder_barang,
                                    'no_workorder_barang' => $d->no_workorder_barang,
                                    '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_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_cap' => $d->id_cap,
                                    'nama_cap' => $d->nama_cap,
                                    'id_kualitas_proses' => $d->id_kualitas_proses,
                                    'nama_kualitas_proses' => $d->nama_kualitas_proses,
                                    'id_kualitas_periksa' => $d->id_kualitas_periksa,
                                    'nama_kualitas_periksa' => $d->nama_kualitas_periksa,
                                    'id_perubahansatuan' => $d->id_perubahansatuan,
                                    'nama_perubahansatuan' => $d->nama_perubahansatuan,
                                    'jumlah_rencana' => $d->jumlah_rencana,
                                    'ket_1' => $ket_1,
                                    'ket_2' => $ket_2,
                                    'ket_3' => $ket_3,
                                    'ket_4' => $ket_4,
                                    'ket_5' => $ket_5,
                                    'stok' => '0',
                                    'nama_warna' => $d->nama_warna,
                                    'inisial_warna' => $d->inisial_warna,
                                    'ket_proses' => $d->ket_proses,
                                    'id_sort' => $id_sort,
                                    'jenis' => $id_sort_detail,
                                    'total' => $total
                                    );
                        }
                    }
                    
                    foreach($arr as $a){
                        $arr_detail[] = $a;
                        $nilai_awal = $a['total'];

                        foreach($query as $d){
                            if($d->id_workorder_barang == $a['id_workorder_barang']){
                                
                                $ket_1 = $d->ket_1;
                                $ket_2 = $d->ket_2;
                                $ket_3 = $d->ket_3;
                                $ket_4 = $d->ket_4;
                                $ket_5 = $d->ket_5;
                                $total_detail = ($nilai_awal+$d->data_keluar)-$d->data_masuk;
                                $masuk = $d->data_keluar;
                                $keluar = $d->data_masuk;
                                $jumlah = 0;
                                $tanggal = $d->tanggal;
                                $id_sort = $d->id_sort;
                                $id_sort_detail = $d->id_sort_detail;
                                $nilai_awal = $total_detail;

                                $arr_detail[] = array('id' => $d->id, 
                                        'id_barang' => $d->id_barang, 
                                        'kode_barang' => $d->kode_barang,
                                        'nama_barang' => $d->nama_barang,
                                        'id_kategori' => $d->id_kategori,
                                        'nama_kategori' => $d->nama_kategori,
                                        'id_subkategori' => $d->id_subkategori,
                                        'kategori' => $d->nama_subkategori,
                                        'jumlah' => $jumlah,
                                        'masuk' => $masuk,
                                        'keluar' => $keluar,
                                        'id_gudang' => $d->id_gudang,
                                        'nama_gudang' => $d->nama_gudang,
                                        'tanggal' => $tanggal,
                                        'id_proses' => $d->id_proses,
                                        'nama_proses' => $d->nama_proses,
                                        'id_komposisi' => $d->id_komposisi,
                                        'nama_komposisi' => $d->nama_komposisi,
                                        'inisial_komposisi' => $d->inisial_warna,
                                        'id_satuan' => $d->id_satuanhasil,
                                        'nama_satuan' => $d->inisial_satuan,
                                        'id_corak' => $d->id_corak,
                                        'nama_corak' => $d->nama_corak,
                                        'id_motif' => $d->id_motif,
                                        'nama_motif' => $d->nama_motif,
                                        'id_workorder' => $d->id_workorder,
                                        'no_workorder' => $d->no_workorder,
                                        'id_workorder_barang' => $d->id_workorder_barang,
                                        'no_workorder_barang' => $d->no_workorder_barang,
                                        '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_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_cap' => $d->id_cap,
                                        'nama_cap' => $d->nama_cap,
                                        'id_kualitas_proses' => $d->id_kualitas_proses,
                                        'nama_kualitas_proses' => $d->nama_kualitas_proses,
                                        'id_kualitas_periksa' => $d->id_kualitas_periksa,
                                        'nama_kualitas_periksa' => $d->nama_kualitas_periksa,
                                        'id_perubahansatuan' => $d->id_perubahansatuan,
                                        'nama_perubahansatuan' => $d->nama_perubahansatuan,
                                        'jumlah_rencana' => $d->jumlah_rencana,
                                        'ket_1' => $ket_1,
                                        'ket_2' => $ket_2,
                                        'ket_3' => $ket_3,
                                        'ket_4' => $ket_4,
                                        'ket_5' => $ket_5,
                                        'stok' => '0',
                                        'nama_warna' => $d->nama_warna,
                                        'inisial_warna' => $d->inisial_warna,
                                        'ket_proses' => $d->ket_proses,
                                        'id_sort' => $id_sort,
                                        'jenis' => $id_sort_detail,
                                        'total' => $total_detail
                                        );
                            }
                        }

                    }

                    // print_r($arr_detail); exit();

                    foreach($arr_detail as $a){
                        if(isset($jum[$a['id_sort']])){
                            $jum[$a['id_sort']] = ['jumlah' => $jum[$a['id_sort']]['jumlah']+$a['jenis']];
                        }else{
                            $jum[$a['id_sort']] = ['jumlah' => $a['jenis']];
                        }
                    }



                    foreach($jum as $j => $jd){
                        if($jd['jumlah'] == 0){
                            $isi_arr[] = $j;
                        }
                    }

                    
                    // print_r($isi_arr);
                    // print_r(removeElementWithValue($arr, 'id_sort', $isi_arr));

                    $arr_detail = removeElementWithValue($arr_detail, 'id_sort', $isi_arr);

                }else{
                    $arr_detail = array();
                }
                $html = "MutasiproduksiTenunDetailCetak";

                $data['detail'] = barang_byproses($arr_detail, 5, $proses);   
            }
            // $data['detail'] = barang_byproses($arr_detail, 5, $proses);
        //    print_r("SELECT A.*, ms.satuan as inisial_satuan FROM (".get_mutasitenun($where_sekir, $where_hasil, $where_sisa, $where_detail).") A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan_hasil");
        }else if($jenis == 2){
            $order_ = '';
            $where_pro = "";
            if($id_proses != 37){
                if($id_proses == 41){
                    $group_celupA = str_replace(", A.id_workorder_barang", "", $group_celupA);

                    $group_inside = str_replace(", C.id_workorder_barang", "", $group_inside);
                    $group_outside = str_replace(", A.id_workorder_barang", "", $group_outside);
                }else if($id_proses == '18'){
                    if($req['id_warna'] == '' ){
                        $group_celupA = $group_celupA.", A.id_warna";

                        $group_inside = $group_inside.", C.id_warna";
                        $group_outside = $group_outside.", A.id_warna";
                    }
                }else if($id_proses == 24){
                    $order_ = "B.nama_proses DESC ";
                    $where_pro = ", C.id_proses ORDER BY nama_proses DESC";
                }
                
                // $query_[] = "SELECT A.*, '0' AS jumlah_masuk, '0' AS jumlah_keluar, SUM(A.keluar-A.masuk) AS jumlah_awal FROM (".get_produksiview($add_where.$vr).") A ".$group_celupA." HAVING jumlah_awal IS NOT NULL";
                // $query_[] = "SELECT A.*, SUM(A.masuk) AS jumlah_masuk, SUM(A.keluar) AS jumlah_keluar, '0' AS jumlah_awal FROM (".get_produksiview($add_where.$w1, $order_).") A ".$group_celupA;
                
                // $query_all = "SELECT A.*, SUM(A.jumlah_masuk) AS mutasi_masuk, SUM(A.jumlah_keluar) AS mutasi_keluar, (SUM(A.jumlah_awal)+SUM(A.jumlah_keluar))-SUM(A.jumlah_masuk) sisa, ms.satuan as inisial_satuan, A.inisial_warna inisial_komposisi, A.nama_subkategori kategori FROM (".implode(" UNION ALL ", $query_).") A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ".$group_celupA.$orderby_proses;
                
                // $data['detail'] = barang_byproses(DB::select($query_all), 100, $id_proses);
                
                $query[] = "SELECT C.*, 0 jumlah_masuk, 0 jumlah_keluar, SUM(C.keluar - C.masuk) jumlah_awal FROM (".get_prosesnontenun($data['where_nonbasic'], $data['where_nontenun_awal']).") C ".$group_inside." HAVING jumlah_awal IS NOT NULL";

                $query[] = "SELECT C.*, SUM(C.masuk) jumlah_masuk, SUM(C.keluar) jumlah_keluar, 0 jumlah_awal FROM (".get_prosesnontenun($data['where_nonbasic'], $data['where_nontenun_bet'])." ) C ".$group_inside.$where_pro;
                
                $query_all = "SELECT A.*, SUM(A.jumlah_masuk) AS mutasi_masuk, SUM(A.jumlah_keluar) AS mutasi_keluar, (SUM(A.jumlah_awal)+SUM(A.jumlah_keluar))-SUM(A.jumlah_masuk) sisa, ms.satuan as inisial_satuan, A.inisial_warna inisial_komposisi, A.nama_subkategori kategori FROM (".implode(" UNION ALL ", $query).") AS A LEFT JOIN ms_satuan AS ms ON A.id_satuan = ms.id_satuan ".$group_outside.$orderby_proses;
                
                // print_r($query_all); exit();
                $data['detail'] = barang_byproses(DB::select($query_all), 100, $id_proses);
                $html = "MutasiproduksiRekapCetak";

               // print_r($query_all);
            }else{
                $query[] = get_prosestenun($data['where_tenun_awal'], '', ", '0' jumlah_masuk, '0' jumlah_keluar, SUM(C.sekir_keluar)-SUM(C.jumlah_setor+C.sekir_masuk) jumlah_awal");
                $query[] = get_prosestenun($data['where_tenun_bet'], '', ", SUM(C.jumlah_setor+C.sekir_masuk) jumlah_masuk, SUM(C.sekir_keluar) jumlah_keluar, 0 jumlah_awal");
                $query_g = implode(" UNION ALL ", $query);
                $q = "SELECT A.*, ABS(SUM(A.jumlah_masuk)) mutasi_masuk, ABS(SUM(A.jumlah_keluar)) mutasi_keluar, SUM(A.jumlah_awal) mutasi_awal, (SUM(A.jumlah_awal)+SUM(A.jumlah_keluar))-SUM(A.jumlah_masuk) sisa
                    FROM (".$query_g.") AS A GROUP BY A.id_workorder_barang, A.id_proses_sekarang ORDER BY A.nama_motif, A.inisial_warna, A.no_workorder_barang";
                // print_r($q); exit();
                $query_hasil = DB::select($q);
                $data['detail'] = $query_hasil;
                $html = "MutasiproduksiTenunRekapCetak";
            }            
        }
        else{
            $data['detail'] = array();
            $html = "MutasiproduksiDetailCetak";
        }
        

       // print_r($data['induk']);

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


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

        $add_where = "";

        $where = array();
        $barang = array();
        $kategori = array();
        $lokasi = "";
        $tanggal = ""; 
        $gudang = ["nama_gudang" => '', "lokasi_gudang" => ''];
        $warna = ["nama" => '', "inisial" => ''];
        $proses = '';


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

        if(count($req) > 0){
            $add_where = "WHERE ";
        }


        if(isset($req['id_kategoribarang'])){
            if($req['id_kategoribarang'] != ''){
                $where[] = "mkb2.id_kb = ".$req['id_kategoribarang']." ";

                $kategori =  DB::table('ms_kategori_barang')->where('id_kb','=',$req['id_kategoribarang'])->first()->nama;
            }
        }

        if(isset($req['id_barang'])){
            if($req['id_barang'] != ''){
            $where[] = "A.id_barang = ".$req['id_barang']." ";
            
            $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'))->where('mb.id', $req['id_barang'])->first();
            }

       
        }
        
        if(isset($req['id_lokasi'])){
            $where[] = "mg.id_lokasi = ".$req['id_lokasi']." ";
            
            $lokasi = DB::table('ms_lokasi_produksi')->where('id_lokasi', $req['id_lokasi'])->first()->nama_lokasi;
        }

        if(isset($req['tgl_awal']) && isset($req['tgl_akhir'])){
            $tgl_awal = tgl_full($req['tgl_awal'], '2');
            $tgl_akhir = tgl_full($req['tgl_akhir'], '2');
            
            $where[] = "A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";

            //$where[] = "A.tanggal <= '$tgl_akhir' ";            
            //$tanggal = date_range($tgl_awal, $tgl_akhir);

        }

        if(isset($req['id_gudang'])){
            if($req['id_gudang'] != ''){
                $where[] = "A.id_gudang = ".$req['id_gudang']." ";

                $d_gudang = DB::table('ms_gudang as mg')->join('ms_lokasi_produksi as mlp', 'mg.id_lokasi', 'mlp.id_lokasi')->where('mg.id_gudang', $req['id_gudang'])->select(DB::raw("mg.*, mlp.nama_lokasi"))->first();
                $gudang = ['nama_gudang' => $d_gudang->nama_gudang, 'lokasi_gudang' => $d_gudang->nama_lokasi];
            }
        }

        if(isset($req['id_warna'])){
            if($req['id_warna'] != ''){
                $where[] = "A.id_komposisi = ".$req['id_warna']." ";

                $d_warna = DB::table('ms_warnakomposisi')->where('id_komposisi', $req['id_warna'])->first();
                $warna = ["nama" => $d_warna->nama, "inisial" => $d_warna->inisial];
            }
        }

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

                $where[] = "A.id_proses = ".$req['id_proses']." ";
                
                $proses = DB::table('ms_proses')->where('id_proses', $req['id_proses'])->first()->nama_proses;
            }
        }       


        $data['induk'] = array("barang" => $barang, 'nama_lokasi' => $lokasi, 'tanggal_awal' => $tgl_awal, 'tanggal_akhir' => $tgl_akhir, 'nama_gudang' => $gudang['nama_gudang'], 'lokasi_gudang' => $gudang['lokasi_gudang'], 'nama_komposisi' => $warna['nama'], 'inisial_komposisi' => $warna['inisial'], 'nama_proses' => $proses, 'kategori' => $kategori);
        $data['where'] = implode(" AND ", $where);
        $data['group'] = "GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_motif, A.id_workorder_barang, A.id_kualitas, A.id_kualitas_dudul, A.id_label, A.id_kemasan, A.id_kemasansatuan, A.id_kemasansepuluh, A.id_kemasanseratus, A.id_cap, A.id_kualitas_proses, A.id_kualitas_periksa, A.id_perubahansatuan, A.jumlah_rencana, mkb2.id_kb";
        $data['where'] = $add_where.$data['where'].$data['group'];      

                
        $d_query = get_rawquery(get_mutasigudang($data['where'])); 
        
           

    
        $wheres = array();
        $arr = array();
        if(count($d_query) > 0){
            foreach($d_query as $d){
                $where = '';
                if(!isset($wheres[$d['id_barang']][$d['id_gudang']][$d['id_komposisi']][$d['id_satuan']][$d['id_corak']][$d['id_motif']][$d['id_kualitas']][$d['id_kualitas_dudul']][$d['id_label']][$d['id_kemasan']][$d['id_kemasansatuan']][$d['id_kemasansepuluh']][$d['id_kemasanseratus']][$d['id_proses']][$d['id_workorder']][$d['id_workorder_barang']][$d['id_cap']][$d['id_kualitas_periksa']][$d['id_kualitas_proses']][$d['id_perubahansatuan']][$d['jumlah_rencana']][$d['id_kategori']])){
                    
                    $where = "WHERE B.id_kategori = '".$d['id_kategori']."' AND B.id_barang = '".$d['id_barang']."' AND B.id_gudang = '".$d['id_gudang']."' AND B.id_proses = '".$d['id_proses']."' AND B.id_komposisi = '".$d['id_komposisi']."' AND B.id_satuan = '".$d['id_satuan']."' AND B.id_corak = '".$d['id_corak']."' AND B.id_motif = '".$d['id_motif']."' AND B.id_workorder = '".$d['id_workorder']."' AND B.id_workorder_barang = '".$d['id_workorder_barang']."' 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_kualitas = '".$d['id_kualitas']."' AND B.id_kemasansatuan = '".$d['id_kemasansatuan']."' AND B.id_kemasanseratus = '".$d['id_kemasanseratus']."' AND B.id_cap = '".$d['id_cap']."' AND B.id_kualitas_proses = '".$d['id_kualitas_proses']."' AND B.id_kualitas_periksa = '".$d['id_kualitas_periksa']."' AND B.id_perubahansatuan = '".$d['id_perubahansatuan']."' AND B.jumlah_rencana = '".$d['jumlah_rencana']."' AND B.tanggal < '".$tgl_awal."' ";
                    
                    // FROM ( ".get_mutasi('')." ) B
                    $query_sebelum = "SELECT B.*, CASE WHEN sum(masuk - keluar) % 1 = 0 THEN sum(masuk - keluar) ELSE ROUND(sum(masuk - keluar), 2) END AS stok
                    FROM ( ".get_mutasigudang('')." ) B
                    $where
                    GROUP BY B.id_barang, B.id_gudang, B.id_proses, B.id_komposisi, B.id_satuan, B.id_corak, B.id_motif, B.id_workorder_barang, B.id_kualitas, B.id_kualitas_dudul, B.id_label, B.id_kemasan, B.id_kemasansatuan, B.id_kemasansepuluh, B.id_kemasanseratus, B.id_cap, B.id_kualitas_proses, B.id_kualitas_periksa, B.id_perubahansatuan, B.jumlah_rencana, B.id_kategori";

                    $hasil_sebelum = get_rawquery($query_sebelum);

                    //print_r($query_sebelum);
                    //print_r("\n\n");
                    $warna = DB::table('ms_warnakomposisi')->where('id_komposisi', $d['id_komposisi']);
                    $inisial_warna = "";
                    if($warna->count() > 0){
                        $inisial_warna = $warna->first()->inisial;
                    }

                    if(count($hasil_sebelum) == 0){

                        $wheres[] = array('id' => $d['id'], 
                            'id_barang' => $d['id_barang'], 
                            'kode_barang' => $d['kode_barang'],
                            'nama_barang' => $d['nama_barang'],
                            'id_kategori' => $d['id_kategori'],
                            'nama_kategori' => $d['nama_kategori'],
                            'id_subkategori' => $d['id_subkategori'],
                            'nama_subkategori' => $d['nama_subkategori'],
                            'jumlah' => '0',
                            'masuk' => '0',
                            'keluar' => '0',
                            'id_gudang' => $d['id_gudang'],
                            'nama_gudang' => $d['nama_gudang'],
                            'tanggal' => $req['tgl_awal'],
                            'id_proses' => $d['id_proses'],
                            'nama_proses' => $d['nama_proses'],
                            'id_komposisi' => $d['id_komposisi'],
                            'nama_komposisi' => $d['nama_komposisi'],
                            'inisial_komposisi' => $inisial_warna,
                            'id_satuan' => $d['id_satuan'],
                            'nama_satuan' => $d['nama_satuan'],
                            'id_corak' => $d['id_corak'],
                            'nama_corak' => $d['nama_corak'],
                            'id_motif' => $d['id_motif'],
                            'nama_motif' => $d['nama_motif'],
                            'id_workorder' => $d['id_workorder'],
                            'no_workorder' => $d['no_workorder'],
                            'id_workorder_barang' => $d['id_workorder_barang'],
                            'no_workorder_barang' => $d['no_workorder_barang'],
                            '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_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_cap' => $d['id_cap'],
                            'nama_cap' => $d['nama_cap'],
                            'id_kualitas_proses' => $d['id_kualitas_proses'],
                            'nama_kualitas_proses' => $d['nama_kualitas_proses'],
                            'id_kualitas_periksa' => $d['id_kualitas_periksa'],
                            'nama_kualitas_periksa' => $d['nama_kualitas_periksa'],
                            'id_perubahansatuan' => $d['id_perubahansatuan'],
                            'nama_perubahansatuan' => $d['nama_perubahansatuan'],
                            'jumlah_rencana' => $d['jumlah_rencana'],
                            'ket_1' => "",
                            'ket_2' => "",
                            'ket_3' => "Stok Awal",
                            'ket_4' => "",
                            'ket_5' => $d['ket_5'],
                            'stok' => '0',
                            'nama_warna' => $d['nama_warna'],
                            'inisial_warna' => $d['inisial_warna'],
                            'ket_proses' => $d['ket_proses']
                            );
                    }else{
                        $wheres[] = array('id' => $hasil_sebelum[0]['id'], 
                            'id_barang' => $hasil_sebelum[0]['id_barang'], 
                            'kode_barang' => $hasil_sebelum[0]['kode_barang'],
                            'nama_barang' => $hasil_sebelum[0]['nama_barang'],
                            'id_kategori' => $hasil_sebelum[0]['id_kategori'],
                            'nama_kategori' => $hasil_sebelum[0]['nama_kategori'],
                            'id_subkategori' => $hasil_sebelum[0]['id_subkategori'],
                            'nama_subkategori' => $hasil_sebelum[0]['nama_subkategori'],
                            'jumlah' => $hasil_sebelum[0]['stok'],
                            'masuk' => $hasil_sebelum[0]['stok']+$hasil_sebelum[0]['keluar'],
                            'keluar' => $hasil_sebelum[0]['keluar'],
                            'id_gudang' => $hasil_sebelum[0]['id_gudang'],
                            'nama_gudang' => $hasil_sebelum[0]['nama_gudang'],
                            'tanggal' => $tgl_awal,
                            'id_proses' => $hasil_sebelum[0]['id_proses'],
                            'nama_proses' => $hasil_sebelum[0]['nama_proses'],
                            'id_komposisi' => $hasil_sebelum[0]['id_komposisi'],
                            'nama_komposisi' => $hasil_sebelum[0]['nama_komposisi'],
                            'inisial_komposisi' => $inisial_warna,
                            'id_satuan' => $hasil_sebelum[0]['id_satuan'],
                            'nama_satuan' => $hasil_sebelum[0]['nama_satuan'],
                            'id_corak' => $hasil_sebelum[0]['id_corak'],
                            'nama_corak' => $hasil_sebelum[0]['nama_corak'],
                            'id_motif' => $hasil_sebelum[0]['id_motif'],
                            'nama_motif' => $hasil_sebelum[0]['nama_motif'],
                            'id_workorder' => $hasil_sebelum[0]['id_workorder'],
                            'no_workorder' => $hasil_sebelum[0]['no_workorder'],
                            'id_workorder_barang' => $hasil_sebelum[0]['id_workorder_barang'],
                            'no_workorder_barang' => $hasil_sebelum[0]['no_workorder_barang'],
                            'id_kualitas' => $hasil_sebelum[0]['id_kualitas'],
                            'nama_kualitas' => $hasil_sebelum[0]['nama_kualitas'],
                            'id_kualitas_dudul' => $hasil_sebelum[0]['id_kualitas_dudul'],
                            'kualitas_dudul' => $hasil_sebelum[0]['kualitas_dudul'],
                            'id_label' => $hasil_sebelum[0]['id_label'],
                            'nama_label' => $hasil_sebelum[0]['nama_label'],
                            'id_kemasan' => $hasil_sebelum[0]['id_kemasan'],
                            'nama_kemasan' => $hasil_sebelum[0]['nama_kemasan'],
                            'id_kemasansatuan' => $hasil_sebelum[0]['id_kemasansatuan'],
                            'nama_kemasansatuan' => $hasil_sebelum[0]['nama_kemasansatuan'],
                            'id_kemasansepuluh' => $hasil_sebelum[0]['id_kemasansepuluh'],
                            'nama_kemasansepuluh' => $hasil_sebelum[0]['nama_kemasansepuluh'],
                            'id_kemasanseratus' => $hasil_sebelum[0]['id_kemasanseratus'],
                            'nama_kemasanseratus' => $hasil_sebelum[0]['nama_kemasanseratus'],
                            'id_cap' => $hasil_sebelum[0]['id_cap'],
                            'nama_cap' => $hasil_sebelum[0]['nama_cap'],
                            'id_kualitas_proses' => $hasil_sebelum[0]['id_kualitas_proses'],
                            'nama_kualitas_proses' => $hasil_sebelum[0]['nama_kualitas_proses'],
                            'id_kualitas_periksa' => $hasil_sebelum[0]['id_kualitas_periksa'],
                            'nama_kualitas_periksa' => $hasil_sebelum[0]['nama_kualitas_periksa'],
                            'id_perubahansatuan' => $hasil_sebelum[0]['id_perubahansatuan'],
                            'nama_perubahansatuan' => $hasil_sebelum[0]['nama_perubahansatuan'],
                            'jumlah_rencana' => $hasil_sebelum[0]['jumlah_rencana'],
                            'ket_1' => $hasil_sebelum[0]['ket_1'],
                            'ket_2' => $hasil_sebelum[0]['ket_2'],
                            'ket_3' => "Stok Awal",
                            'ket_4' => "",
                            'ket_5' => $hasil_sebelum[0]['ket_5'],
                            'stok' => $hasil_sebelum[0]['stok'],
                            'nama_warna' => $hasil_sebelum[0]['nama_warna'],
                            'inisial_warna' => $hasil_sebelum[0]['inisial_warna'],
                            'ket_proses' => $hasil_sebelum[0]['ket_proses']
                            );
                    }
                    
                }

            }         

            
        }else{
            
        }



        $arr_2 = array();

        foreach($wheres as $w){
            $where = "WHERE mkb2.id_kb = '".$w['id_kategori']."' AND A.id_barang = '".$w['id_barang']."' AND A.id_gudang = '".$w['id_gudang']."' AND A.id_proses = '".$w['id_proses']."' AND A.id_komposisi = '".$w['id_komposisi']."' AND A.id_satuan = '".$w['id_satuan']."' AND A.id_corak = '".$w['id_corak']."' AND A.id_motif = '".$w['id_motif']."' AND A.id_workorder = '".$w['id_workorder']."' AND A.id_workorder_barang = '".$w['id_workorder_barang']."' AND A.id_kualitas_dudul = '".$w['id_kualitas_dudul']."' AND A.id_label = '".$w['id_label']."' AND A.id_kemasan = '".$w['id_kemasan']."' AND A.id_kualitas = '".$w['id_kualitas']."' AND A.id_kemasansatuan = '".$w['id_kemasansatuan']."' AND A.id_kemasanseratus = '".$w['id_kemasanseratus']."' AND A.id_cap = '".$w['id_cap']."' AND A.id_kualitas_proses = '".$w['id_kualitas_proses']."' AND A.id_kualitas_periksa = '".$w['id_kualitas_periksa']."' AND A.id_perubahansatuan = '".$w['id_perubahansatuan']."' AND A.jumlah_rencana = '".$w['jumlah_rencana']."' AND A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";
            
            

            $d_query = get_rawquery(get_mutasigudang($where));
            $arr[] = $w+array('total' => $w['stok'], 'jenis' => '0', 'j_awal' => '0');
            $sisa = $w['stok'];
            foreach($d_query as $d){
                $warna = DB::table('ms_warnakomposisi')->where('id_komposisi', $d['id_komposisi']);
                $inisial_warna = "";
                if($warna->count() > 0){
                    $inisial_warna = $warna->first()->inisial;
                }
                $sisa = ($sisa+$d['masuk'])-$d['keluar'];                
                $arr[] = $d+array('stok' => '0', 'total' => $sisa, 'inisial_komposisi' => $inisial_warna, 'jenis' => '1', 'j_awal' => $w['stok']);
            }                
        }
        
        $data['detail'] = $arr;
        

        switch ($jenis) {
            case '0':
                $html = "MutasigudangfilterBaruCetak"; //"MutasibarangCetak";
                break;
            case '1':
                $html = "MutasigudangfilterBaruCetak";
                break;
            default:
                $html = "MutasigudangCetak";
                break;
        }
        require('fpdf181/Mc_table.php');
        return response(view('produksi.cetak.'.$html)->with('data', $data))->header('Content-Type', 'application/pdf');
        //return response()->json($arr);

                
    }


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

        $add_where = "";

        $where = array();
        $barang = array();
        $kategori = array();
        $lokasi = "";
        $tanggal = ""; 
        $gudang = ["nama_gudang" => '', "lokasi_gudang" => ''];
        $warna = ["nama" => '', "inisial" => ''];
        $proses = '';


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

        if(count($req) > 0){
            $add_where = "WHERE ";
        }


        if(isset($req['id_kategoribarang'])){
            if($req['id_kategoribarang'] != ''){
                $where[] = "mkb2.id_kb = ".$req['id_kategoribarang']." ";

                $kategori =  DB::table('ms_kategori_barang')->where('id_kb','=',$req['id_kategoribarang'])->first()->nama;
            }
        }

        if(isset($req['id_barang'])){
            if($req['id_barang'] != ''){
            $where[] = "A.id_barang = ".$req['id_barang']." ";
            
            $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'))->where('mb.id', $req['id_barang'])->first();
            }

       
        }
        
        if(isset($req['id_lokasi'])){
            $where[] = "mg.id_lokasi = ".$req['id_lokasi']." ";
            
            $lokasi = DB::table('ms_lokasi_produksi')->where('id_lokasi', $req['id_lokasi'])->first()->nama_lokasi;
        }

        if(isset($req['tgl_awal']) && isset($req['tgl_akhir'])){
            $tgl_awal = tgl_full($req['tgl_awal'], '2');
            $tgl_akhir = tgl_full($req['tgl_akhir'], '2');
            
            $where[] = "A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";

            //$where[] = "A.tanggal <= '$tgl_akhir' ";            
            //$tanggal = date_range($tgl_awal, $tgl_akhir);

        }

        if(isset($req['id_gudang'])){
            if($req['id_gudang'] != ''){
                $where[] = "A.id_gudang = ".$req['id_gudang']." ";

                $d_gudang = DB::table('ms_gudang as mg')->join('ms_lokasi_produksi as mlp', 'mg.id_lokasi', 'mlp.id_lokasi')->where('mg.id_gudang', $req['id_gudang'])->select(DB::raw("mg.*, mlp.nama_lokasi"))->first();
                $gudang = ['nama_gudang' => $d_gudang->nama_gudang, 'lokasi_gudang' => $d_gudang->nama_lokasi];
            }
        }

        if(isset($req['id_warna'])){
            if($req['id_warna'] != ''){
                $where[] = "A.id_komposisi = ".$req['id_warna']." ";

                $d_warna = DB::table('ms_warnakomposisi')->where('id_komposisi', $req['id_warna'])->first();
                $warna = ["nama" => $d_warna->nama, "inisial" => $d_warna->inisial];
            }
        }

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

                $where[] = "A.id_proses = ".$req['id_proses']." ";
                
                $proses = DB::table('ms_proses')->where('id_proses', $req['id_proses'])->first()->nama_proses;
            }
        }       


        $data['induk'] = array("barang" => $barang, 'nama_lokasi' => $lokasi, 'tanggal_awal' => $tgl_awal, 'tanggal_akhir' => $tgl_akhir, 'nama_gudang' => $gudang['nama_gudang'], 'lokasi_gudang' => $gudang['lokasi_gudang'], 'nama_komposisi' => $warna['nama'], 'inisial_komposisi' => $warna['inisial'], 'nama_proses' => $proses, 'kategori' => $kategori);
        $data['where'] = implode(" AND ", $where);
        $data['group'] = "GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_motif, A.id_workorder_barang, A.id_kualitas, A.id_kualitas_dudul, A.id_label, A.id_kemasan, A.id_kemasansatuan, A.id_kemasansepuluh, A.id_kemasanseratus, A.id_cap, A.id_kualitas_proses, A.id_kualitas_periksa, A.id_perubahansatuan, A.jumlah_rencana, mkb2.id_kb";
        $data['where'] = $add_where.$data['where'].$data['group'];      

                
        $d_query = get_rawquery(get_mutasirekap($data['where'], '','')); 
        
    

    
        $wheres = array();
        $arr = array();
        if(count($d_query) > 0){
            foreach($d_query as $d){
                $where = '';
                if(!isset($wheres[$d['id_barang']][$d['id_gudang']][$d['id_komposisi']][$d['id_satuan']][$d['id_corak']][$d['id_motif']][$d['id_kualitas']][$d['id_kualitas_dudul']][$d['id_label']][$d['id_kemasan']][$d['id_kemasansatuan']][$d['id_kemasansepuluh']][$d['id_kemasanseratus']][$d['id_proses']][$d['id_workorder']][$d['id_workorder_barang']][$d['id_cap']][$d['id_kualitas_periksa']][$d['id_kualitas_proses']][$d['id_perubahansatuan']][$d['jumlah_rencana']][$d['id_kategori']])){
                    
                    $where = "WHERE B.id_kategori = '".$d['id_kategori']."' AND B.id_barang = '".$d['id_barang']."' AND B.id_gudang = '".$d['id_gudang']."' AND B.id_proses = '".$d['id_proses']."' AND B.id_komposisi = '".$d['id_komposisi']."' AND B.id_satuan = '".$d['id_satuan']."' AND B.id_corak = '".$d['id_corak']."' AND B.id_motif = '".$d['id_motif']."' AND B.id_workorder = '".$d['id_workorder']."' AND B.id_workorder_barang = '".$d['id_workorder_barang']."' 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_kualitas = '".$d['id_kualitas']."' AND B.id_kemasansatuan = '".$d['id_kemasansatuan']."' AND B.id_kemasanseratus = '".$d['id_kemasanseratus']."' AND B.id_cap = '".$d['id_cap']."' AND B.id_kualitas_proses = '".$d['id_kualitas_proses']."' AND B.id_kualitas_periksa = '".$d['id_kualitas_periksa']."' AND B.id_perubahansatuan = '".$d['id_perubahansatuan']."' AND B.jumlah_rencana = '".$d['jumlah_rencana']."' AND B.tanggal < '".$tgl_awal."' ";
                    
                    // FROM ( ".get_mutasi('')." ) B
                    $query_sebelum = "SELECT B.*, CASE WHEN sum(masuk - keluar) % 1 = 0 THEN sum(masuk - keluar) ELSE ROUND(sum(masuk - keluar), 2) END AS stok
                    FROM ( ".get_mutasirekap('','','')." ) B
                    $where
                    GROUP BY B.id_barang, B.id_gudang, B.id_proses, B.id_komposisi, B.id_satuan, B.id_corak, B.id_motif, B.id_workorder_barang, B.id_kualitas, B.id_kualitas_dudul, B.id_label, B.id_kemasan, B.id_kemasansatuan, B.id_kemasansepuluh, B.id_kemasanseratus, B.id_cap, B.id_kualitas_proses, B.id_kualitas_periksa, B.id_perubahansatuan, B.jumlah_rencana, B.id_kategori";

                    $hasil_sebelum = get_rawquery($query_sebelum);

                    //print_r($query_sebelum);
                    //print_r("\n\n");
                    $warna = DB::table('ms_warnakomposisi')->where('id_komposisi', $d['id_komposisi']);
                    $inisial_warna = "";
                    if($warna->count() > 0){
                        $inisial_warna = $warna->first()->inisial;
                    }

                    if(count($hasil_sebelum) == 0){

                        $wheres[] = array('id' => $d['id'], 
                            'id_barang' => $d['id_barang'], 
                            'kode_barang' => $d['kode_barang'],
                            'nama_barang' => $d['nama_barang'],
                            'id_kategori' => $d['id_kategori'],
                            'nama_kategori' => $d['nama_kategori'],
                            'id_subkategori' => $d['id_subkategori'],
                            'nama_subkategori' => $d['nama_subkategori'],
                            'jumlah' => '0',
                            'masuk' => '0',
                            'keluar' => '0',
                            'id_gudang' => $d['id_gudang'],
                            'nama_gudang' => $d['nama_gudang'],
                            'tanggal' => $req['tgl_awal'],
                            'id_proses' => $d['id_proses'],
                            'nama_proses' => $d['nama_proses'],
                            'id_komposisi' => $d['id_komposisi'],
                            'nama_komposisi' => $d['nama_komposisi'],
                            'inisial_komposisi' => $inisial_warna,
                            'id_satuan' => $d['id_satuan'],
                            'nama_satuan' => $d['nama_satuan'],
                            'id_corak' => $d['id_corak'],
                            'nama_corak' => $d['nama_corak'],
                            'id_motif' => $d['id_motif'],
                            'nama_motif' => $d['nama_motif'],
                            'id_workorder' => $d['id_workorder'],
                            'no_workorder' => $d['no_workorder'],
                            'id_workorder_barang' => $d['id_workorder_barang'],
                            'no_workorder_barang' => $d['no_workorder_barang'],
                            '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_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_cap' => $d['id_cap'],
                            'nama_cap' => $d['nama_cap'],
                            'id_kualitas_proses' => $d['id_kualitas_proses'],
                            'nama_kualitas_proses' => $d['nama_kualitas_proses'],
                            'id_kualitas_periksa' => $d['id_kualitas_periksa'],
                            'nama_kualitas_periksa' => $d['nama_kualitas_periksa'],
                            'id_perubahansatuan' => $d['id_perubahansatuan'],
                            'nama_perubahansatuan' => $d['nama_perubahansatuan'],
                            'jumlah_rencana' => $d['jumlah_rencana'],
                            'ket_1' => "",
                            'ket_2' => "",
                            'ket_3' => "Stok Awal",
                            'ket_4' => "",
                            'ket_5' => $d['ket_5'],
                            'stok' => '0',
                            'nama_warna' => $d['nama_warna'],
                            'inisial_warna' => $d['inisial_warna']
                            );
                    }else{
                        $wheres[] = array('id' => $hasil_sebelum[0]['id'], 
                            'id_barang' => $hasil_sebelum[0]['id_barang'], 
                            'kode_barang' => $hasil_sebelum[0]['kode_barang'],
                            'nama_barang' => $hasil_sebelum[0]['nama_barang'],
                            'id_kategori' => $hasil_sebelum[0]['id_kategori'],
                            'nama_kategori' => $hasil_sebelum[0]['nama_kategori'],
                            'id_subkategori' => $hasil_sebelum[0]['id_subkategori'],
                            'nama_subkategori' => $hasil_sebelum[0]['nama_subkategori'],
                            'jumlah' => $hasil_sebelum[0]['stok'],
                            'masuk' => $hasil_sebelum[0]['stok']+$hasil_sebelum[0]['keluar'],
                            'keluar' => $hasil_sebelum[0]['keluar'],
                            'id_gudang' => $hasil_sebelum[0]['id_gudang'],
                            'nama_gudang' => $hasil_sebelum[0]['nama_gudang'],
                            'tanggal' => $tgl_awal,
                            'id_proses' => $hasil_sebelum[0]['id_proses'],
                            'nama_proses' => $hasil_sebelum[0]['nama_proses'],
                            'id_komposisi' => $hasil_sebelum[0]['id_komposisi'],
                            'nama_komposisi' => $hasil_sebelum[0]['nama_komposisi'],
                            'inisial_komposisi' => $inisial_warna,
                            'id_satuan' => $hasil_sebelum[0]['id_satuan'],
                            'nama_satuan' => $hasil_sebelum[0]['nama_satuan'],
                            'id_corak' => $hasil_sebelum[0]['id_corak'],
                            'nama_corak' => $hasil_sebelum[0]['nama_corak'],
                            'id_motif' => $hasil_sebelum[0]['id_motif'],
                            'nama_motif' => $hasil_sebelum[0]['nama_motif'],
                            'id_workorder' => $hasil_sebelum[0]['id_workorder'],
                            'no_workorder' => $hasil_sebelum[0]['no_workorder'],
                            'id_workorder_barang' => $hasil_sebelum[0]['id_workorder_barang'],
                            'no_workorder_barang' => $hasil_sebelum[0]['no_workorder_barang'],
                            'id_kualitas' => $hasil_sebelum[0]['id_kualitas'],
                            'nama_kualitas' => $hasil_sebelum[0]['nama_kualitas'],
                            'id_kualitas_dudul' => $hasil_sebelum[0]['id_kualitas_dudul'],
                            'kualitas_dudul' => $hasil_sebelum[0]['kualitas_dudul'],
                            'id_label' => $hasil_sebelum[0]['id_label'],
                            'nama_label' => $hasil_sebelum[0]['nama_label'],
                            'id_kemasan' => $hasil_sebelum[0]['id_kemasan'],
                            'nama_kemasan' => $hasil_sebelum[0]['nama_kemasan'],
                            'id_kemasansatuan' => $hasil_sebelum[0]['id_kemasansatuan'],
                            'nama_kemasansatuan' => $hasil_sebelum[0]['nama_kemasansatuan'],
                            'id_kemasansepuluh' => $hasil_sebelum[0]['id_kemasansepuluh'],
                            'nama_kemasansepuluh' => $hasil_sebelum[0]['nama_kemasansepuluh'],
                            'id_kemasanseratus' => $hasil_sebelum[0]['id_kemasanseratus'],
                            'nama_kemasanseratus' => $hasil_sebelum[0]['nama_kemasanseratus'],
                            'id_cap' => $hasil_sebelum[0]['id_cap'],
                            'nama_cap' => $hasil_sebelum[0]['nama_cap'],
                            'id_kualitas_proses' => $hasil_sebelum[0]['id_kualitas_proses'],
                            'nama_kualitas_proses' => $hasil_sebelum[0]['nama_kualitas_proses'],
                            'id_kualitas_periksa' => $hasil_sebelum[0]['id_kualitas_periksa'],
                            'nama_kualitas_periksa' => $hasil_sebelum[0]['nama_kualitas_periksa'],
                            'id_perubahansatuan' => $hasil_sebelum[0]['id_perubahansatuan'],
                            'nama_perubahansatuan' => $hasil_sebelum[0]['nama_perubahansatuan'],
                            'jumlah_rencana' => $hasil_sebelum[0]['jumlah_rencana'],
                            'ket_1' => $hasil_sebelum[0]['ket_1'],
                            'ket_2' => $hasil_sebelum[0]['ket_2'],
                            'ket_3' => "Stok Awal",
                            'ket_4' => "",
                            'ket_5' => $hasil_sebelum[0]['ket_5'],
                            'stok' => $hasil_sebelum[0]['stok'],
                            'nama_warna' => $hasil_sebelum[0]['nama_warna'],
                            'inisial_warna' => $hasil_sebelum[0]['inisial_warna']
                            );
                    }
                    
                }

            }         

            
        }else{
            
        }



        $arr_2 = array();

        foreach($wheres as $w){
            $where = "WHERE mkb2.id_kb = '".$w['id_kategori']."' AND A.id_barang = '".$w['id_barang']."' AND A.id_gudang = '".$w['id_gudang']."' AND A.id_proses = '".$w['id_proses']."' AND A.id_komposisi = '".$w['id_komposisi']."' AND A.id_satuan = '".$w['id_satuan']."' AND A.id_corak = '".$w['id_corak']."' AND A.id_motif = '".$w['id_motif']."' AND A.id_workorder = '".$w['id_workorder']."' AND A.id_workorder_barang = '".$w['id_workorder_barang']."' AND A.id_kualitas_dudul = '".$w['id_kualitas_dudul']."' AND A.id_label = '".$w['id_label']."' AND A.id_kemasan = '".$w['id_kemasan']."' AND A.id_kualitas = '".$w['id_kualitas']."' AND A.id_kemasansatuan = '".$w['id_kemasansatuan']."' AND A.id_kemasanseratus = '".$w['id_kemasanseratus']."' AND A.id_cap = '".$w['id_cap']."' AND A.id_kualitas_proses = '".$w['id_kualitas_proses']."' AND A.id_kualitas_periksa = '".$w['id_kualitas_periksa']."' AND A.id_perubahansatuan = '".$w['id_perubahansatuan']."' AND A.jumlah_rencana = '".$w['jumlah_rencana']."' AND A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";
            
            
            $group = "GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_motif, A.id_workorder_barang, A.id_kualitas, A.id_kualitas_dudul, A.id_label, A.id_kemasan, A.id_kemasansatuan, A.id_kemasansepuluh, A.id_kemasanseratus, A.id_cap, A.id_kualitas_proses, A.id_kualitas_periksa, A.id_perubahansatuan, A.jumlah_rencana, mkb2.id_kb";
            $sum = ",SUM(A.masuk) AS jumlah_masuk, SUM(A.keluar) AS jumlah_keluar";
            $d_query = get_rawquery(get_mutasirekap($where, $group, $sum));
            $arr[] = $w+array('total' => $w['stok'], 'jenis' => '0', 'j_awal' => '0');
            $sisa = $w['stok'];
            foreach($d_query as $d){
                $warna = DB::table('ms_warnakomposisi')->where('id_komposisi', $d['id_komposisi']);
                $inisial_warna = "";
                if($warna->count() > 0){
                    $inisial_warna = $warna->first()->inisial;
                }
                $sisa = ($sisa+$d['jumlah_masuk'])-$d['jumlah_keluar'];                
                $arr[] = $d+array('stok' => '0', 'total' => $sisa, 'inisial_komposisi' => $inisial_warna, 'jenis' => '1', 'j_awal' => $w['stok'], 'jumlah_masuk' => $d['jumlah_masuk'], 'jumlah_keluar' => $d['jumlah_keluar']);
            }                
        }
        
        $data['detail'] = $arr;
        

        switch ($jenis) {
            case '0':
                $html = "MutasirekapfilterCetak"; //"MutasibarangCetak";
                break;
            case '1':
                $html = "MutasirekapfilterCetak";
                break;
            default:
                $html = "MutasirekapCetak";
                break;
        }
        require('fpdf181/Mc_table.php');
        return response(view('produksi.cetak.'.$html)->with('data', $data))->header('Content-Type', 'application/pdf');
       // return response()->json(count($arr));
        

                
    }

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

        $add_where = "";

        $where = array();
        $barang = array();
        $kategori = '';
        $lokasi = "";
        $tanggal = ""; 
        $gudang = ["nama_gudang" => '', "lokasi_gudang" => ''];
        $warna = ["nama" => '', "inisial" => ''];
        $proses = '';
        $text_orderby = " ORDER BY ";
        $orderby_proses = '';
        $concat_rekap = '';

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

        if(count($req) > 0){
            $add_where = "WHERE ";
        }


        if(isset($req['id_kategoribarang'])){
            if($req['id_kategoribarang'] != ''){
                $where[] = "A.id_kategori = ".$req['id_kategoribarang']." ";

                $kategori =  DB::table('ms_kategori_barang')->where('id_kb','=',$req['id_kategoribarang'])->first()->nama;
            }
        }

        if(isset($req['id_barang'])){
            if($req['id_barang'] != ''){
            $where[] = "A.id_barang = ".$req['id_barang']." ";
            
            $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'))->where('mb.id', $req['id_barang'])->first();
            }

       
        }
        
        if(isset($req['id_lokasi'])){
            $where[] = "A.id_lokasi = ".$req['id_lokasi']." ";
            
            $lokasi = DB::table('ms_lokasi_produksi')->where('id_lokasi', $req['id_lokasi'])->first()->nama_lokasi;
        }

        if(isset($req['tgl_awal']) && isset($req['tgl_akhir'])){
            $tgl_awal = tgl_full($req['tgl_awal'], '2');
            $tgl_akhir = tgl_full($req['tgl_akhir'], '2');
            
            $tgl_where_detail = "AND A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";

            $tgl_where_awal = "AND A.tanggal < '$tgl_awal'";

            //$where[] = "A.tanggal <= '$tgl_akhir' ";            
            //$tanggal = date_range($tgl_awal, $tgl_akhir);

        }

        if(isset($req['id_gudang'])){
            if($req['id_gudang'] != ''){
                $where[] = "A.id_gudang = ".$req['id_gudang']." ";

                $d_gudang = DB::table('ms_gudang as mg')->join('ms_lokasi_produksi as mlp', 'mg.id_lokasi', 'mlp.id_lokasi')->where('mg.id_gudang', $req['id_gudang'])->select(DB::raw("mg.*, mlp.nama_lokasi"))->first();
                $gudang = ['nama_gudang' => $d_gudang->nama_gudang, 'lokasi_gudang' => $d_gudang->nama_lokasi];
            }
        }

        if(isset($req['id_warna'])){
            if($req['id_warna'] != ''){
                $where[] = "A.id_komposisi = ".$req['id_warna']." ";
                $d_warna = DB::table('ms_warnakomposisi')->where('id_komposisi', $req['id_warna'])->first();
                $warna = ["nama" => $d_warna->nama, "inisial" => $d_warna->inisial];
            }else{
                if(isset($req['id_gudang'])){
                    if($req['id_gudang'] == 4){
                        $where[] = "A.id_komposisi = '' ";
                        $where[] = "A.id_motif = '' ";
                        $where[] = "A.id_corak = '' "; 
                    }
                }
            }
        }

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

                if($req['id_proses'] == 0){
                    $where[] = "A.id_proses = '' ";
                
                    $proses = '-';
                }else{
                    $where[] = "A.id_proses = ".$req['id_proses']." ";
                
                    $proses = DB::table('ms_proses')->where('id_proses', $req['id_proses'])->first()->nama_proses;
                }

                if(in_array($req['id_proses'], setup_benang()) || $req['id_proses'] == 0){
                    $orderby_proses = " A.nama_subkategori, A.nama_barang";
                    $groupBy = "GROUP BY A.id_barang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori";
                    $concat_rekap = ", CONCAT(A.nama_subkategori) id_sort";
                }else if(in_array($req['id_proses'], setup_corak())){
                    if($req['id_proses'] == 24){
                        $orderby_proses = " A.nama_motif";
                        $groupBy = "GROUP BY A.id_motif, A.id_proses, A.id_satuan, A.id_kategori"; //A.id_komposisi,
                        $concat_rekap = ", CONCAT(A.nama_subkategori, CAST(A.nama_motif AS UNSIGNED)) id_sort";
                    }else{
                        $orderby_proses = " A.nama_corak";
                        $groupBy = "GROUP BY A.id_corak, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori";
                        $concat_rekap = ", CONCAT(A.nama_subkategori, CAST(A.nama_corak AS UNSIGNED)) id_sort";
                    }
                }else{
                    $orderby_proses = " A.nama_motif";
                    $groupBy = "GROUP BY A.id_motif, A.id_proses, A.id_satuan, A.id_kategori"; //A.id_komposisi,
                    $concat_rekap = ", CONCAT(A.nama_subkategori, CAST(A.nama_motif AS UNSIGNED)) id_sort";
                }

                $orderby_proses .= ", A.nama_komposisi";
            }else{
                $orderby_proses = " A.nama_subkategori, A.nama_barang, A.nama_corak, A.nama_motif, A.nama_komposisi";
                $groupBy = "GROUP BY A.id_barang, A.id_corak, A.id_motif, A.id_proses, A.id_komposisi, A.id_satuan";
                $concat_rekap = ", CONCAT(A.nama_subkategori) id_sort";
            }
        }            


        $data['induk'] = array("barang" => $barang, 'nama_lokasi' => $lokasi, 'tanggal_awal' => $tgl_awal, 'tanggal_akhir' => $tgl_akhir, 'nama_gudang' => $gudang['nama_gudang'], 'lokasi_gudang' => $gudang['lokasi_gudang'], 'nama_komposisi' => $warna['nama'], 'inisial_komposisi' => $warna['inisial'], 'nama_proses' => $proses, 'kategori' => $kategori);
        $data['where'] = implode(" AND ", $where);

        if($jenis == 0){
            $where_detail = $add_where.$data['where'].$tgl_where_detail.$groupBy; 
            $field_sum_detail = ", SUM(A.masuk) AS jumlah_masuk, SUM(A.keluar) AS jumlah_keluar, '0' AS jumlah_awal";    
            $where_awal = $add_where.$data['where'].$tgl_where_awal.$groupBy;
            $field_sum_awal = ", '0' AS jumlah_masuk, '0' AS jumlah_keluar, SUM(A.masuk-A.keluar) AS jumlah_awal";

            $query['saldo_awal'] = get_mutasirekap2($where_awal, '', $field_sum_awal);
            $query['detail'] = get_mutasirekap2($where_detail, '', $field_sum_detail);
            $query_mutasi = implode(" UNION ALL ", $query);
            $query_rekap_mutasi = "SELECT A.*, mk.inisial as inisial_komposisi, SUM(A.jumlah_masuk) AS mutasi_masuk, SUM(A.jumlah_keluar) AS mutasi_keluar, SUM(A.jumlah_awal) AS mutasi_awal, ms.satuan as inisial_satuan FROM ($query_mutasi) AS A LEFT JOIN ms_warnakomposisi AS mk ON A.id_komposisi=mk.id_komposisi LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan $groupBy $text_orderby $orderby_proses";

            $query_hasil_rekap = DB::select($query_rekap_mutasi);

            $arr = array();
            if($query_hasil_rekap > 0){
                foreach($query_hasil_rekap as $d){
                $arr[] = array('id' => $d->id, 
                                'id_barang' => $d->id_barang, 
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang,
                                'id_kategori' => $d->id_kategori,
                                'nama_kategori' => $d->nama_kategori,
                                'id_subkategori' => $d->id_subkategori,
                                'kategori' => $d->nama_subkategori,
                                'sisa' => ($d->mutasi_awal+$d->mutasi_masuk)-$d->mutasi_keluar,
                                'masuk' => $d->mutasi_masuk,
                                'keluar' => $d->mutasi_keluar,
                                'awal' => $d->mutasi_awal,
                                'id_gudang' => $d->id_gudang,
                                'nama_gudang' => $d->nama_gudang,
                                'tanggal' => $req['tgl_awal'],
                                'id_proses' => $d->id_proses,
                                'nama_proses' => $d->nama_proses,
                                'id_komposisi' => $d->id_komposisi,
                                'nama_komposisi' => $d->nama_komposisi,
                                'inisial_komposisi' => $d->inisial_komposisi,
                                'id_satuan' => $d->id_satuan,
                                'nama_satuan' => $d->inisial_satuan,
                                'id_corak' => $d->id_corak,
                                'nama_corak' => $d->nama_corak,
                                'id_motif' => $d->id_motif,
                                'nama_motif' => $d->nama_motif,
                                'id_workorder' => $d->id_workorder,
                                'no_workorder' => $d->no_workorder,
                                'id_workorder_barang' => $d->id_workorder_barang,
                                'no_workorder_barang' => $d->no_workorder_barang,
                                '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_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_cap' => $d->id_cap,
                                'nama_cap' => $d->nama_cap,
                                'id_kualitas_proses' => $d->id_kualitas_proses,
                                'nama_kualitas_proses' => $d->nama_kualitas_proses,
                                'id_kualitas_periksa' => $d->id_kualitas_periksa,
                                'nama_kualitas_periksa' => $d->nama_kualitas_periksa,
                                'id_perubahansatuan' => $d->id_perubahansatuan,
                                'nama_perubahansatuan' => $d->nama_perubahansatuan,
                                'jumlah_rencana' => $d->jumlah_rencana
                                );
                }
            }else{
                $arr = array();
            }

            $data['detail'] = barang_byproses($arr, 4, $proses);
        }else if($jenis == 1){
            $where_detail = $add_where.$data['where'].$tgl_where_detail.$groupBy; 
            $field_sum_detail = ", SUM(A.masuk) AS jumlah_masuk, SUM(A.keluar) AS jumlah_keluar, '0' AS jumlah_awal";    
            $where_awal = $add_where.$data['where'].$tgl_where_awal.$groupBy;
            $field_sum_awal = ", '0' AS jumlah_masuk, '0' AS jumlah_keluar, SUM(A.masuk-A.keluar) AS jumlah_awal";

            $query['saldo_awal'] = get_mutasirekap2($where_awal, '', $field_sum_awal);
            $query['detail'] = get_mutasirekap2($where_detail, '', $field_sum_detail);
            $query_mutasi = implode(" UNION ALL ", $query);
            $query_rekap_mutasi[] = "SELECT A.*, mk.inisial as inisial_komposisi, SUM(A.jumlah_masuk) AS mutasi_masuk, SUM(A.jumlah_keluar) AS mutasi_keluar, SUM(A.jumlah_awal) AS mutasi_awal, ms.satuan as inisial_satuan ".$concat_rekap." , 0 id_sort_detail, SUM((A.jumlah_awal+A.jumlah_masuk)-A.jumlah_keluar) jumlah_mutasi FROM ($query_mutasi) AS A LEFT JOIN ms_warnakomposisi AS mk ON A.id_komposisi=mk.id_komposisi LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan $groupBy";
            $query_rekap_mutasi[] = "SELECT A.*, mk.inisial as inisial_komposisi, SUM(A.jumlah_masuk) AS mutasi_masuk, SUM(A.jumlah_keluar) AS mutasi_keluar, SUM(A.jumlah_awal) AS mutasi_awal, ms.satuan as inisial_satuan ".$concat_rekap." , 1 id_sort_detail, SUM((A.jumlah_awal+A.jumlah_masuk)-A.jumlah_keluar) jumlah_mutasi FROM ($query_mutasi) AS A LEFT JOIN ms_warnakomposisi AS mk ON A.id_komposisi=mk.id_komposisi LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan GROUP BY A.id_proses, A.id_satuan, A.id_subkategori";

            $query_last = "SELECT A.*, A.nama_subkategori kategori FROM (".implode(" UNION ALL ", $query_rekap_mutasi).") A ORDER BY A.id_sort, A.id_sort_detail, $orderby_proses";

            $data['detail'] = barang_byproses(DB::select($query_last), 101, $proses);

        }else{

        }

        switch ($jenis) {
            case '0':
                $html = "Mutasirekapfilter2Cetak"; //"MutasibarangCetak";
                break;
            case '1':
                $html = "MutasiRekapPerKategoriCetak";
                break;
            default:
                $html = "MutasirekapCetak";
                break;
        }

        if(isset($req['is_excelrekapgudang'])){
            if($req['is_excelrekapgudang'] == 1){
                $barang = 'Semua Barang';
                if($data['induk']['barang'] != false){
                    $barang = $data['induk']['barang']->nama_barang;
                }
                Excel::create("Mutasi Rekap _".$data['induk']['nama_gudang']."_".$barang."_".date_range($data['induk']['tanggal_awal'], $data['induk']['tanggal_akhir']), function($excel) use ($html, $data){
                    $excel->sheet("Sheet 1", function($sheet) use ($html, $data){
                        $sheet->loadView('produksi.cetak.'.$html."_Excel", $data);
                    });
                })->export("xlsx");
                // return response(view('produksi.cetak.'.$html."_Excel")->with('data', $data));
            }else{
                require('fpdf181/Mc_table.php');
                return response(view('produksi.cetak.'.$html)->with('data', $data))->header('Content-Type', 'application/pdf');
            }
        }
    }


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

        $add_where = "";

        $where = array();
        $where2 = array();
        $barang = array();
        $kategori = array();
        $lokasi = "";
        $tanggal = ""; 
        $gudang = ["nama_gudang" => '', "lokasi_gudang" => ''];
        $warna = ["nama" => '', "inisial" => ''];
        $kualitas = '';
        $proses = '';


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

        if(count($req) > 0){
            $add_where = "WHERE ";
        }


        if(isset($req['id_kategoribarang'])){
            if($req['id_kategoribarang'] != ''){
                $where[] = "A.id_kategori = 5 ";
                $where2[] = "B.id_kategori = 5 ";

                $kategori =  DB::table('ms_kategori_barang')->where('id_kb','=',$req['id_kategoribarang'])->first()->nama;
            }
        }

        if(isset($req['id_barang'])){
            if($req['id_barang'] != ''){
            $where[] = "A.id_barang = ".$req['id_barang']." ";
            $where2[] = "B.id_barang = ".$req['id_barang']." ";
            
            $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'))->where('mb.id', $req['id_barang'])->first();
            }

       
        }
        
        if(isset($req['id_lokasi'])){
            $where[] = "A.id_lokasi = ".$req['id_lokasi']." ";
            $where2[] = "B.id_lokasi = ".$req['id_lokasi']." ";
            
            $lokasi = DB::table('ms_lokasi_produksi')->where('id_lokasi', $req['id_lokasi'])->first()->nama_lokasi;
        }

        if(isset($req['tgl_awal']) && isset($req['tgl_akhir'])){
            $tgl_awal = tgl_full($req['tgl_awal'], '2');
            $tgl_akhir = tgl_full($req['tgl_akhir'], '2');
            $tgl_where_detail = "AND A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";

            $tgl_where_awal = "AND A.tanggal < '$tgl_awal'";
            

            //$where2[] = "A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";
            $where2[] = "B.tanggal < '$tgl_awal' ";


            //$where[] = "A.tanggal <= '$tgl_akhir' ";            
            //$tanggal = date_range($tgl_awal, $tgl_akhir);

        }

        if(isset($req['id_gudang'])){
            if($req['id_gudang'] != ''){
                $where[] = "A.id_gudang = ".$req['id_gudang']." ";
                $where2[] = "B.id_gudang = ".$req['id_gudang']." ";

                $d_gudang = DB::table('ms_gudang as mg')->join('ms_lokasi_produksi as mlp', 'mg.id_lokasi', 'mlp.id_lokasi')->where('mg.id_gudang', $req['id_gudang'])->select(DB::raw("mg.*, mlp.nama_lokasi"))->first();
                $gudang = ['nama_gudang' => $d_gudang->nama_gudang, 'lokasi_gudang' => $d_gudang->nama_lokasi];
            }
        }

        if(isset($req['id_warna'])){
            if($req['id_warna'] != ''){
                $where[] = "A.id_komposisi = ".$req['id_warna']." ";
                $where2[] = "B.id_komposisi = ".$req['id_warna']." ";

                $d_warna = DB::table('ms_warnakomposisi')->where('id_komposisi', $req['id_warna'])->first();
                $warna = ["nama" => $d_warna->nama, "inisial" => $d_warna->inisial];
            }
        }

        if(isset($req['id_proses'])){
            if($req['id_proses'] != ''){
                if($req['id_proses'] == '0'){
                    $where[] = "A.id_proses = '' ";
                    $where2[] = "B.id_proses = '' ";
                    
                    $proses = '';
                }else{
                    $where[] = "A.id_proses = ".$req['id_proses']." ";
                    $where2[] = "B.id_proses = ".$req['id_proses']." ";
                    
                    $proses = DB::table('ms_proses')->where('id_proses', $req['id_proses'])->first()->nama_proses;
                }
            }
        }

        if(isset($req['id_kualitas'])){
            if($req['id_kualitas'] != ''){

                $where[] = "A.id_kualitas = ".$req['id_kualitas']." ";
                $where2[] = " B.id_kualitas = ".$req['id_kualitas']." ";

                $kualitas = DB::table('ms_kualitas')->where('id_kualitas', $req['id_kualitas'])->first()->nama_kualitas;
            }
        }

        if(isset($req['id_kualitas_dudul'])){
            if($req['id_kualitas_dudul'] != ''){

                $where[] = "A.id_kualitas_dudul = ".$req['id_kualitas']." ";
                $where2[] = "B.id_kualitas_dudul = ".$req['id_kualitas']." ";

                $kualitas_dudul = DB::table('ms_kualitas')->where('id_kualitas', $req['id_kualitas'])->first()->nama_kualitas;

            }
        }

        if(isset($req['id_motif'])){
            if($req['id_motif'] == 0){

                $where[] = "A.id_motif = '' ";
                $where2[] = "B.id_motif = '' ";
            }
        }

        if(isset($req['id_corak'])){
            if($req['id_corak'] == 0){

                $where[] = "A.id_corak = '' ";
                $where2[] = "B.id_corak = '' ";
            }
        }

        $where[] = "A.id_motif = '' ";
        $where[] = "A.id_corak = '' ";

        switch ($jenis) {
            case '1':
            $data['induk'] = array("barang" => $barang, 'nama_lokasi' => $lokasi, 'tanggal_awal' => $tgl_awal, 'tanggal_akhir' => $tgl_akhir, 'nama_gudang' => $gudang['nama_gudang'], 'lokasi_gudang' => $gudang['lokasi_gudang'], 'nama_komposisi' => $warna['nama'], 'inisial_komposisi' => $warna['inisial'], 'nama_proses' => $proses, 'kategori' => $kategori);
        $data['where'] = implode(" AND ", $where);
        $data['where2'] = implode(" AND ", $where2);
        $groupBy_detail = "GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_motif, A.id_corak, A.id_workorder_barang, A.id_kualitas, A.id_kualitas_dudul, A.id_label, A.id_kemasan, A.id_kemasansatuan, A.id_kemasansepuluh, A.id_kemasanseratus, A.id_cap, A.id_kualitas_proses, A.id_kualitas_periksa, A.id_perubahansatuan, A.jumlah_rencana, A.id_kategori, A.tanggal";
        $groupBy_awal = "GROUP BY A.id_barang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori";
        $groupBy_akhir = "GROUP BY B.id_barang, B.id_proses, B.id_komposisi, B.id_satuan, B.id_kategori";
        $on_where = "ON A.id_kategori = C.id_kategori AND A.id_barang = C.id_barang AND A.id_gudang = C.id_gudang AND A.id_proses = C.id_proses AND A.id_komposisi = C.id_komposisi AND A.id_satuan = C.id_satuan";
        $concat_awal = ", CONCAT(A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort, '0' as id_sort_detail, '0' AS jenis";
        $concat_detail = ", CONCAT(A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort, CONCAT(A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort_detail, '1' AS jenis";

        $where_detail = $add_where.$data['where'].$tgl_where_detail;    
        $where_awal = $add_where.$data['where'].$tgl_where_detail.$groupBy_awal;
        $where2_awal = $add_where.$data['where2'].$groupBy_akhir;

        $query['saldo_awal'] = get_mutasitenun_saldoawal($where_awal, $where2_awal, $on_where, $concat_awal);
        $query['detail'] = get_mutasitenun_detail($where_detail, $concat_detail);
        $query_mutasi = implode(" UNION ALL ", $query);
        $query_rekap_mutasi = "SELECT A.*, mk.inisial as inisial_komposisi, ms.satuan as inisial_satuan FROM ($query_mutasi) AS A LEFT JOIN ms_warnakomposisi AS mk ON A.id_komposisi=mk.id_komposisi LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY A.id_sort, A.id_sort_detail, A.tanggal_full, A.nama_barang, A.nama_komposisi";
        $query_hasil_rekap = DB::select($query_rekap_mutasi);

    

        $arr = array();
        if($query_hasil_rekap > 0){
            $total = 0;
            foreach($query_hasil_rekap as $d){
            
             if($d->jenis == 0){
                $ket_1 = '';
                $ket_2 = '';
                $ket_3 = 'Saldo Awal';
                $ket_4 = '';
                $ket_5 = '';
                $total = $d->stok;
                $masuk = '0';
                $keluar = '0';
                $jumlah = $d->stok;
             }else{
                $ket_1 = $d->ket_1;
                $ket_2 = $d->ket_2;
                $ket_3 = $d->ket_3;
                $ket_4 = $d->ket_4;
                $ket_5 = $d->ket_5;
                $total = ($total+$d->masuk)-$d->keluar;
                $masuk = $d->masuk;
                $keluar = $d->keluar;
                $jumlah = 0;
             }   
            $arr[] = array('id' => $d->id, 
                            'id_barang' => $d->id_barang, 
                            'kode_barang' => $d->kode_barang,
                            'nama_barang' => $d->nama_barang,
                            'id_kategori' => $d->id_kategori,
                            'nama_kategori' => $d->nama_kategori,
                            'id_subkategori' => $d->id_subkategori,
                            'kategori' => $d->nama_subkategori,
                            'jumlah' => $jumlah,
                            'masuk' => $masuk,
                            'keluar' => $keluar,
                            'id_gudang' => $d->id_gudang,
                            'nama_gudang' => $d->nama_gudang,
                            'tanggal' => $d->tanggal,
                            'id_proses' => $d->id_proses,
                            'nama_proses' => $d->nama_proses,
                            'id_komposisi' => $d->id_komposisi,
                            'nama_komposisi' => $d->nama_komposisi,
                            'inisial_komposisi' => $d->inisial_warna,
                            'id_satuan' => $d->id_satuan,
                            'nama_satuan' => $d->inisial_satuan,
                            'id_corak' => $d->id_corak,
                            'nama_corak' => $d->nama_corak,
                            'id_motif' => $d->id_motif,
                            'nama_motif' => $d->nama_motif,
                            'id_workorder' => $d->id_workorder,
                            'no_workorder' => $d->no_workorder,
                            'id_workorder_barang' => $d->id_workorder_barang,
                            'no_workorder_barang' => $d->no_workorder_barang,
                            '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_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_cap' => $d->id_cap,
                            'nama_cap' => $d->nama_cap,
                            'id_kualitas_proses' => $d->id_kualitas_proses,
                            'nama_kualitas_proses' => $d->nama_kualitas_proses,
                            'id_kualitas_periksa' => $d->id_kualitas_periksa,
                            'nama_kualitas_periksa' => $d->nama_kualitas_periksa,
                            'id_perubahansatuan' => $d->id_perubahansatuan,
                            'nama_perubahansatuan' => $d->nama_perubahansatuan,
                            'jumlah_rencana' => $d->jumlah_rencana,
                            'ket_1' => $ket_1,
                            'ket_2' => $ket_2,
                            'ket_3' => $ket_3,
                            'ket_4' => $ket_4,
                            'ket_5' => $ket_5,
                            'stok' => '0',
                            'nama_warna' => $d->nama_warna,
                            'inisial_warna' => $d->inisial_warna,
                            'ket_proses' => $d->ket_proses,
                            'jenis' => $d->jenis,
                            'total' => $total,
                            'lebar' => '', //$d->lebar,
                            'panjang' => '', //$d->panjang,
                            'pick' => '', //$d->pick
                            );
            }
        }else{
            $arr = array();
        }
                
      $data['detail'] = barang_byproses($arr, 4, $proses);
                $html = "MutasiBenang_barangfilterCetak"; //"MutasibarangCetak";
                break;



            case '2':
                $data['induk'] = array("barang" => $barang, 'nama_lokasi' => $lokasi, 'tanggal_awal' => $tgl_awal, 'tanggal_akhir' => $tgl_akhir, 'nama_gudang' => $gudang['nama_gudang'], 'lokasi_gudang' => $gudang['lokasi_gudang'], 'nama_komposisi' => $warna['nama'], 'inisial_komposisi' => $warna['inisial'], 'nama_proses' => $proses, 'kategori' => $kategori);
        $data['where'] = implode(" AND ", $where);
        $data['group'] = "GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_workorder_barang, A.id_kualitas, A.id_kualitas_dudul, A.id_label, A.id_kemasan, A.id_kemasansatuan, A.id_kemasansepuluh, A.id_kemasanseratus, A.id_cap, A.id_kualitas_proses, A.id_kualitas_periksa, A.id_perubahansatuan, A.jumlah_rencana, A.id_kategori";
        $groupBy = "GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori";

        $where_detail = $add_where.$data['where'].$tgl_where_detail.$data['group']; 
        $field_sum_detail = ", SUM(A.masuk) AS jumlah_masuk, SUM(A.keluar) AS jumlah_keluar, '0' AS jumlah_awal";    
        $where_awal = $add_where.$data['where'].$tgl_where_awal.$data['group'];
        $field_sum_awal = ", '0' AS jumlah_masuk, '0' AS jumlah_keluar, SUM(A.masuk-A.keluar) AS jumlah_awal";

        $query['saldo_awal'] = get_mutasitenun_rekap($where_awal, '', $field_sum_awal);
        $query['detail'] = get_mutasitenun_rekap($where_detail, '', $field_sum_detail);
        $query_mutasi = implode(" UNION ALL ", $query);
        $query_rekap_mutasi = "SELECT A.*, mk.inisial as inisial_komposisi, SUM(A.jumlah_masuk) AS mutasi_masuk, SUM(A.jumlah_keluar) AS mutasi_keluar, SUM(A.jumlah_awal) AS mutasi_awal, ms.satuan as inisial_satuan FROM ($query_mutasi) AS A LEFT JOIN ms_warnakomposisi AS mk ON A.id_komposisi=mk.id_komposisi LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan $groupBy ORDER BY A.nama_subkategori, A.nama_barang, A.nama_corak, A.nama_komposisi, A.nama_proses";
        $query_hasil_rekap = DB::select($query_rekap_mutasi);

        $arr = array();
        if($query_hasil_rekap > 0){
            foreach($query_hasil_rekap as $d){
            $arr[] = array('id' => $d->id, 
                            'id_barang' => $d->id_barang, 
                            'kode_barang' => $d->kode_barang,
                            'nama_barang' => $d->nama_barang,
                            'id_kategori' => $d->id_kategori,
                            'nama_kategori' => $d->nama_kategori,
                            'id_subkategori' => $d->id_subkategori,
                            'kategori' => $d->nama_subkategori,
                            'sisa' => ($d->mutasi_awal+$d->mutasi_masuk)-$d->mutasi_keluar,
                            'masuk' => $d->mutasi_masuk,
                            'keluar' => $d->mutasi_keluar,
                            'awal' => $d->mutasi_awal,
                            'id_gudang' => $d->id_gudang,
                            'nama_gudang' => $d->nama_gudang,
                            'tanggal' => $req['tgl_awal'],
                            'id_proses' => $d->id_proses,
                            'nama_proses' => $d->nama_proses,
                            'id_komposisi' => $d->id_komposisi,
                            'nama_komposisi' => $d->nama_komposisi,
                            'inisial_komposisi' => $d->inisial_komposisi,
                            'id_satuan' => $d->id_satuan,
                            'nama_satuan' => $d->inisial_satuan,
                            'id_corak' => $d->id_corak,
                            'nama_corak' => $d->nama_corak,
                            'id_motif' => $d->id_motif,
                            'nama_motif' => $d->nama_motif,
                            'id_workorder' => $d->id_workorder,
                            'no_workorder' => $d->no_workorder,
                            'id_workorder_barang' => $d->id_workorder_barang,
                            'no_workorder_barang' => $d->no_workorder_barang,
                            '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_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_cap' => $d->id_cap,
                            'nama_cap' => $d->nama_cap,
                            'id_kualitas_proses' => $d->id_kualitas_proses,
                            'nama_kualitas_proses' => $d->nama_kualitas_proses,
                            'id_kualitas_periksa' => $d->id_kualitas_periksa,
                            'nama_kualitas_periksa' => $d->nama_kualitas_periksa,
                            'id_perubahansatuan' => $d->id_perubahansatuan,
                            'nama_perubahansatuan' => $d->nama_perubahansatuan,
                            'jumlah_rencana' => $d->jumlah_rencana,
                            'lebar' => $d->lebar,
                            'panjang' => $d->panjang,
                            'pick' => $d->pick
                            );
            }
        }else{
            $arr = array();
        }

        $data['detail'] = barang_byproses($arr, 4, $proses);
                $html = "MutasiBenang_rekapfilterCetak";
                break;
            default:
                $html = "MutasigudangCetak";
                break;
        }
        

        if(isset($req['is_excelgudangwarna'])){
            if($req['is_excelgudangwarna'] == 1){
                $barang = 'Semua Barang';
                if($data['induk']['barang'] != false){
                    $barang = $data['induk']['barang']->nama_barang;
                }
                Excel::create("Mutasi Rekap _".$data['induk']['nama_gudang']."_".$barang."_".date_range($data['induk']['tanggal_awal'], $data['induk']['tanggal_akhir']), function($excel) use ($html, $data){
                    $excel->sheet("Sheet 1", function($sheet) use ($html, $data){
                        $sheet->loadView('produksi.cetak.'.$html."_Excel", $data);
                    });
                })->export("xlsx");
            }else{
                require('fpdf181/Mc_table.php');
                return response(view('produksi.cetak.'.$html)->with('data', $data))->header('Content-Type', 'application/pdf');
            }
        }else{
            require('fpdf181/Mc_table.php');
            return response(view('produksi.cetak.'.$html)->with('data', $data))->header('Content-Type', 'application/pdf');
        }
                
    }


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

        $add_where = "";

        $where = array();
        $where2 = array();
        $barang = array();
        $kategori = array();
        $lokasi = "";
        $tanggal = ""; 
        $gudang = ["nama_gudang" => '', "lokasi_gudang" => ''];
        $warna = ["nama" => '', "inisial" => ''];
        $proses = '';
        $corak = '';
        $pick = '';


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

        if(count($req) > 0){
            $add_where = "WHERE ";
        }


        if(isset($req['id_kategoribarang'])){
            if($req['id_kategoribarang'] != ''){
                $where[] = "A.id_kategori = 5 ";
                $where2[] = "B.id_kategori = 5 ";

                $kategori =  DB::table('ms_kategori_barang')->where('id_kb','=',$req['id_kategoribarang'])->first()->nama;
            }
        }

        if(isset($req['id_barang'])){
            if($req['id_barang'] != ''){
            $where[] = "A.id_barang = ".$req['id_barang']." ";
            $where2[] = "B.id_barang = ".$req['id_barang']." ";
            
            $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'))->where('mb.id', $req['id_barang'])->first();
            }

       
        }
        
        if(isset($req['id_lokasi'])){
            $where[] = "A.id_lokasi = ".$req['id_lokasi']." ";
            $where2[] = "B.id_lokasi = ".$req['id_lokasi']." ";
            
            $lokasi = DB::table('ms_lokasi_produksi')->where('id_lokasi', $req['id_lokasi'])->first()->nama_lokasi;
        }

        if(isset($req['tgl_awal']) && isset($req['tgl_akhir'])){
            $tgl_awal = tgl_full($req['tgl_awal'], '2');
            $tgl_akhir = tgl_full($req['tgl_akhir'], '2');
            $tgl_where_detail = "AND A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";

            $tgl_where_awal = "AND A.tanggal < '$tgl_awal'";
            

            //$where2[] = "A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";
            $where2[] = "B.tanggal < '$tgl_awal' ";


            //$where[] = "A.tanggal <= '$tgl_akhir' ";            
            //$tanggal = date_range($tgl_awal, $tgl_akhir);

        }

        if(isset($req['id_gudang'])){
            if($req['id_gudang'] != ''){
                $where[] = "A.id_gudang = ".$req['id_gudang']." ";
                $where2[] = "B.id_gudang = ".$req['id_gudang']." ";

                $d_gudang = DB::table('ms_gudang as mg')->join('ms_lokasi_produksi as mlp', 'mg.id_lokasi', 'mlp.id_lokasi')->where('mg.id_gudang', $req['id_gudang'])->select(DB::raw("mg.*, mlp.nama_lokasi"))->first();
                $gudang = ['nama_gudang' => $d_gudang->nama_gudang, 'lokasi_gudang' => $d_gudang->nama_lokasi];
            }
        }

        if(isset($req['id_warna'])){
            if($req['id_warna'] != ''){
                $where[] = "A.id_komposisi = ".$req['id_warna']." ";
                $where2[] = "B.id_komposisi = ".$req['id_warna']." ";

                $d_warna = DB::table('ms_warnakomposisi')->where('id_komposisi', $req['id_warna'])->first();
                $warna = ["nama" => $d_warna->nama, "inisial" => $d_warna->inisial];
            }
        }

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

                $where[] = "A.id_proses = ".$req['id_proses']." ";
                $where2[] = "B.id_proses = ".$req['id_proses']." ";
                
                $proses = DB::table('ms_proses')->where('id_proses', $req['id_proses'])->first()->nama_proses;
            }
        } 

        if(isset($req['id_motif'])){
            if($req['id_motif'] == 0){

                $where[] = "A.id_motif = '' ";
                $where2[] = "B.id_motif = '' ";
            }
        }

        if(isset($req['id_corak'])){
            if($req['id_corak'] != ''){

                $where[] = "A.id_corak = ".$req['id_corak']." ";
                $where2[] = "B.id_corak = ".$req['id_corak']." ";

                $corak  = DB::table('ms_motif_barang')->where('id_motif_barang', $req['id_corak'])->first()->motif_keterangan;
            }else{
                $where[] = "A.id_corak != '' ";
                $where2[] = "B.id_corak != '' ";
            }
        }

        switch ($jenis) {
            case '1':
        $data['induk'] = array("barang" => $barang, 'nama_lokasi' => $lokasi, 'tanggal_awal' => $tgl_awal, 'tanggal_akhir' => $tgl_akhir, 'nama_gudang' => $gudang['nama_gudang'], 'lokasi_gudang' => $gudang['lokasi_gudang'], 'nama_komposisi' => $warna['nama'], 'inisial_komposisi' => $warna['inisial'], 'nama_proses' => $proses, 'kategori' => $kategori, 'nama_corak' => $corak);
        $data['where'] = implode(" AND ", $where);
        $data['where2'] = implode(" AND ", $where2);
        //    $groupBy_detail = "GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_motif, A.id_corak, A.id_workorder_barang, A.id_kualitas, A.id_kualitas_dudul, A.id_label, A.id_kemasan, A.id_kemasansatuan, A.id_kemasansepuluh, A.id_kemasanseratus, A.id_cap, A.id_kualitas_proses, A.id_kualitas_periksa, A.id_perubahansatuan, A.jumlah_rencana, A.id_kategori, A.tanggal";
        $groupBy_awal = "GROUP BY A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_kategori";
        $groupBy_detail = "GROUP BY B.id_proses, B.id_komposisi, B.id_satuan, B.id_corak, B.id_kategori";
        $on_where = "ON A.id_kategori = C.id_kategori AND A.id_proses = C.id_proses AND A.id_satuan = C.id_satuan AND A.id_corak = C.id_corak AND A.id_komposisi = C.id_komposisi";
        $concat_awal = ", CONCAT(A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_kategori) as id_sort, '0' as id_sort_detail, '0' AS jenis";
        $concat_detail = ", CONCAT(A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_kategori) as id_sort, CONCAT(A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_kategori) as id_sort_detail, '1' AS jenis";

        $where_detail = $add_where.$data['where'].$tgl_where_detail;    
        $where_awal = $add_where.$data['where'].$tgl_where_detail.$groupBy_awal;
        $where2_awal = $add_where.$data['where2'].$groupBy_detail;

        $query['saldo_awal'] = get_mutasi_saldoawal($where_awal, $where2_awal, $on_where, $concat_awal);
        $query['detail'] = get_mutasi_detail($where_detail, $concat_detail);
        $query_mutasi = implode(" UNION ALL ", $query);
        $query_rekap_mutasi = "SELECT A.*, mk.inisial as inisial_komposisi, ms.satuan as inisial_satuan FROM ($query_mutasi) AS A LEFT JOIN ms_warnakomposisi AS mk ON A.id_komposisi=mk.id_komposisi LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY A.id_sort, A.id_sort_detail, A.tanggal_full, A.nama_subkategori, A.nama_barang, A.nama_komposisi";
        $query_hasil_rekap = DB::select($query_rekap_mutasi);

        $arr = array();
        if($query_hasil_rekap > 0){
            $total = 0;
            foreach($query_hasil_rekap as $d){
            
             if($d->jenis == 0){
                $ket_1 = '';
                $ket_2 = '';
                $ket_3 = 'Saldo Awal';
                $ket_4 = '';
                $ket_5 = '';
                $total = $d->stok;
                $masuk = '0';
                $keluar = '0';
                $jumlah = $d->stok;
             }else{
                $ket_1 = $d->ket_1;
                $ket_2 = $d->ket_2;
                $ket_3 = $d->ket_3;
                $ket_4 = $d->ket_4;
                $ket_5 = $d->ket_5;
                $total = ($total+$d->masuk)-$d->keluar;
                $masuk = $d->masuk;
                $keluar = $d->keluar;
                $jumlah = 0;
             }   
            $arr[] = array('id' => $d->id, 
                            'id_barang' => $d->id_barang, 
                            'kode_barang' => $d->kode_barang,
                            'nama_barang' => $d->nama_barang,
                            'id_kategori' => $d->id_kategori,
                            'nama_kategori' => $d->nama_kategori,
                            'id_subkategori' => $d->id_subkategori,
                            'kategori' => $d->nama_subkategori,
                            'jumlah' => $jumlah,
                            'masuk' => $masuk,
                            'keluar' => $keluar,
                            'id_gudang' => $d->id_gudang,
                            'nama_gudang' => $d->nama_gudang,
                            'tanggal' => $d->tanggal,
                            'id_proses' => $d->id_proses,
                            'nama_proses' => $d->nama_proses,
                            'id_komposisi' => $d->id_komposisi,
                            'nama_komposisi' => $d->nama_komposisi,
                            'inisial_komposisi' => $d->inisial_warna,
                            'id_satuan' => $d->id_satuan,
                            'nama_satuan' => $d->inisial_satuan,
                            'id_corak' => $d->id_corak,
                            'nama_corak' => $d->nama_corak,
                            'id_motif' => $d->id_motif,
                            'nama_motif' => $d->nama_motif,
                            'id_workorder' => $d->id_workorder,
                            'no_workorder' => $d->no_workorder,
                            'id_workorder_barang' => $d->id_workorder_barang,
                            'no_workorder_barang' => $d->no_workorder_barang,
                            '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_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_cap' => $d->id_cap,
                            'nama_cap' => $d->nama_cap,
                            'id_kualitas_proses' => $d->id_kualitas_proses,
                            'nama_kualitas_proses' => $d->nama_kualitas_proses,
                            'id_kualitas_periksa' => $d->id_kualitas_periksa,
                            'nama_kualitas_periksa' => $d->nama_kualitas_periksa,
                            'id_perubahansatuan' => $d->id_perubahansatuan,
                            'nama_perubahansatuan' => $d->nama_perubahansatuan,
                            'jumlah_rencana' => $d->jumlah_rencana,
                            'ket_1' => $ket_1,
                            'ket_2' => $ket_2,
                            'ket_3' => $ket_3,
                            'ket_4' => $ket_4,
                            'ket_5' => $ket_5,
                            'stok' => '0',
                            'nama_warna' => $d->nama_warna,
                            'inisial_warna' => $d->inisial_warna,
                            'ket_proses' => $d->ket_proses,
                            'jenis' => $d->jenis,
                            'total' => $total
                            );
            }
        }else{
            $arr = array();
        }
                
        $data['detail'] = barang_byproses($arr, 4, $proses);
        $html = "MutasiCorak_barangfilterCetak"; //"MutasibarangCetak";
        break;



            case '2':
                $data['induk'] = array("barang" => $barang, 'nama_lokasi' => $lokasi, 'tanggal_awal' => $tgl_awal, 'tanggal_akhir' => $tgl_akhir, 'nama_gudang' => $gudang['nama_gudang'], 'lokasi_gudang' => $gudang['lokasi_gudang'], 'nama_komposisi' => $warna['nama'], 'inisial_komposisi' => $warna['inisial'], 'nama_proses' => $proses, 'kategori' => $kategori, 'nama_corak' => $corak);
        $data['where'] = implode(" AND ", $where);
        $data['group'] = " GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_workorder_barang, A.id_kualitas, A.id_kualitas_dudul, A.id_label, A.id_kemasan, A.id_kemasansatuan, A.id_kemasansepuluh, A.id_kemasanseratus, A.id_cap, A.id_kualitas_proses, A.id_kualitas_periksa, A.jumlah_rencana, A.id_kategori";
        $groupBy = " GROUP BY A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_kategori";

        $where_detail = $add_where.$data['where'].$tgl_where_detail.$data['group']; 
        $field_sum_detail = ", SUM(A.masuk) AS jumlah_masuk, SUM(A.keluar) AS jumlah_keluar, '0' AS jumlah_awal";    
        $where_awal = $add_where.$data['where'].$tgl_where_awal.$data['group'];
        $field_sum_awal = ", '0' AS jumlah_masuk, '0' AS jumlah_keluar, SUM(A.masuk-A.keluar) AS jumlah_awal";

        $query['saldo_awal'] = get_mutasirekap2($where_awal, '', $field_sum_awal);
        $query['detail'] = get_mutasirekap2($where_detail, '', $field_sum_detail);
        $query_mutasi = implode(" UNION ALL ", $query);
        $query_rekap_mutasi = "SELECT A.*, mk.inisial as inisial_komposisi, SUM(A.jumlah_masuk) AS mutasi_masuk, SUM(A.jumlah_keluar) AS mutasi_keluar, SUM(A.jumlah_awal) AS mutasi_awal, ms.satuan as inisial_satuan FROM ($query_mutasi) AS A LEFT JOIN ms_warnakomposisi AS mk ON A.id_komposisi=mk.id_komposisi LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan $groupBy ORDER BY A.nama_corak, A.nama_komposisi, A.nama_proses, A.nama_subkategori, A.nama_barang";
        $query_hasil_rekap = DB::select($query_rekap_mutasi);

        $arr = array();
        if($query_hasil_rekap > 0){
            foreach($query_hasil_rekap as $d){
            $arr[] = array('id' => $d->id, 
                            'id_barang' => $d->id_barang, 
                            'kode_barang' => $d->kode_barang,
                            'nama_barang' => $d->nama_barang,
                            'id_kategori' => $d->id_kategori,
                            'nama_kategori' => $d->nama_kategori,
                            'id_subkategori' => $d->id_subkategori,
                            'kategori' => $d->nama_subkategori,
                            'sisa' => ($d->mutasi_awal+$d->mutasi_masuk)-$d->mutasi_keluar,
                            'masuk' => $d->mutasi_masuk,
                            'keluar' => $d->mutasi_keluar,
                            'awal' => $d->mutasi_awal,
                            'id_gudang' => $d->id_gudang,
                            'nama_gudang' => $d->nama_gudang,
                            'tanggal' => $req['tgl_awal'],
                            'id_proses' => $d->id_proses,
                            'nama_proses' => $d->nama_proses,
                            'id_komposisi' => $d->id_komposisi,
                            'nama_komposisi' => $d->nama_komposisi,
                            'inisial_komposisi' => $d->inisial_komposisi,
                            'id_satuan' => $d->id_satuan,
                            'nama_satuan' => $d->inisial_satuan,
                            'id_corak' => $d->id_corak,
                            'nama_corak' => $d->nama_corak,
                            'id_motif' => $d->id_motif,
                            'nama_motif' => $d->nama_motif,
                            'id_workorder' => $d->id_workorder,
                            'no_workorder' => $d->no_workorder,
                            'id_workorder_barang' => $d->id_workorder_barang,
                            'no_workorder_barang' => $d->no_workorder_barang,
                            '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_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_cap' => $d->id_cap,
                            'nama_cap' => $d->nama_cap,
                            'id_kualitas_proses' => $d->id_kualitas_proses,
                            'nama_kualitas_proses' => $d->nama_kualitas_proses,
                            'id_kualitas_periksa' => $d->id_kualitas_periksa,
                            'nama_kualitas_periksa' => $d->nama_kualitas_periksa,
                            'id_perubahansatuan' => $d->id_perubahansatuan,
                            'nama_perubahansatuan' => $d->nama_perubahansatuan,
                            'jumlah_rencana' => $d->jumlah_rencana
                            );
            }
        }else{
            $arr = array();
        }

        $data['detail'] = barang_byproses($arr, 103, $proses);
                $html = "MutasiCorak_rekapfilterCetak";
                break;
            default:
                $html = "MutasigudangCetak";
                break;
        }
        

        if(isset($req['is_excelgudangcorak'])){
            if($req['is_excelgudangcorak'] == 1){
                $barang = 'Semua Barang';
                if($data['induk']['barang'] != false){
                    $barang = $data['induk']['barang']->nama_barang;
                }
                Excel::create("Mutasi Rekap _".$data['induk']['nama_gudang']."_".$barang."_".date_range($data['induk']['tanggal_awal'], $data['induk']['tanggal_akhir']), function($excel) use ($html, $data){
                    $excel->sheet("Sheet 1", function($sheet) use ($html, $data){
                        $sheet->loadView('produksi.cetak.'.$html."_Excel", $data);
                    });
                })->export("xlsx");
            }else{
                require('fpdf181/Mc_table.php');
                return response(view('produksi.cetak.'.$html)->with('data', $data))->header('Content-Type', 'application/pdf');
            }
        }else{
            require('fpdf181/Mc_table.php');
            return response(view('produksi.cetak.'.$html)->with('data', $data))->header('Content-Type', 'application/pdf');
        }
                
    }

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

        $add_where = "";

        $where = array();
        $where2 = array();
        $barang = array();
        $kategori = '';
        $id_lokasi = "";
        $lokasi = "";
        $tanggal = ""; 
        $gudang = ["nama_gudang" => '', "lokasi_gudang" => ''];
        $warna = ["nama" => '', "inisial" => ''];
        $proses = '';
        $motif = '';
        $order = '';
        $id_proses = '';
        $_proses = "";

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

        if(count($req) > 0){
            $add_where = "WHERE ";
        }


        if(isset($req['id_kategoribarang'])){
            if($req['id_kategoribarang'] != ''){
                $where[] = "A.id_kategori = ".$req['id_kategoribarang']." ";
                $where2[] = "B.id_kategori = ".$req['id_kategoribarang']." ";

                $kategori =  DB::table('ms_kategori_barang')->where('id_kb','=',$req['id_kategoribarang'])->first()->nama;
            }
        }

        if(isset($req['id_barang'])){
            if($req['id_barang'] != ''){
            $where[] = "A.id_barang = ".$req['id_barang']." ";
            $where2[] = "B.id_barang = ".$req['id_barang']." ";
            
            $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'))->where('mb.id', $req['id_barang'])->first();
            }

       
        }
        
        if(isset($req['id_lokasi'])){
            $where[] = "A.id_lokasi = ".$req['id_lokasi']." ";
            $where2[] = "B.id_lokasi = ".$req['id_lokasi']." ";
            $id_lokasi = $req['id_lokasi'];
            $lokasi = DB::table('ms_lokasi_produksi')->where('id_lokasi', $req['id_lokasi'])->first()->nama_lokasi;
        }

        if(isset($req['tgl_awal']) && isset($req['tgl_akhir'])){
            $tgl_awal = tgl_full($req['tgl_awal'], '2');
            $tgl_akhir = tgl_full($req['tgl_akhir'], '2');
            $tgl_where_detail = "AND A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";

            $tgl_where_awal = "AND A.tanggal < '$tgl_awal'";
            

            //$where2[] = "A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";
            $where2[] = "B.tanggal < '$tgl_awal' ";


            //$where[] = "A.tanggal <= '$tgl_akhir' ";            
            //$tanggal = date_range($tgl_awal, $tgl_akhir);

        }

        if(isset($req['id_gudang'])){
            if($req['id_gudang'] != ''){
                $where[] = "A.id_gudang = ".$req['id_gudang']." ";
                $where2[] = "B.id_gudang = ".$req['id_gudang']." ";

                $d_gudang = DB::table('ms_gudang as mg')->join('ms_lokasi_produksi as mlp', 'mg.id_lokasi', 'mlp.id_lokasi')->where('mg.id_gudang', $req['id_gudang'])->select(DB::raw("mg.*, mlp.nama_lokasi"))->first();
                $gudang = ['nama_gudang' => $d_gudang->nama_gudang, 'lokasi_gudang' => $d_gudang->nama_lokasi];
            }
        }

        if(isset($req['id_warnatenun'])){
            if($req['id_warnatenun'] != ''){
                $where[] = "A.id_komposisi = ".$req['id_warnatenun']." ";
                $where2[] = "B.id_komposisi = ".$req['id_warnatenun']." ";

                $d_warna = DB::table('ms_warnakomposisi')->where('id_komposisi', $req['id_warnatenun'])->first();
                $warna = ["nama" => $d_warna->nama, "inisial" => $d_warna->inisial];
            }
        }

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

                $where[] = "A.id_proses = ".$req['id_proses']." ";
                $where2[] = "B.id_proses = ".$req['id_proses']." ";
                
                $proses = DB::table('ms_proses')->where('id_proses', $req['id_proses'])->first()->nama_proses;
                $id_proses = '1';
                $_proses = $req['id_proses'];
            }else{
                $q_proses = DB::table('ms_proses')->where('nama_proses', 'LIKE', '%tenun%')->orwhere('nama_proses', 'LIKE', '%sekir%')->pluck('id_proses')->toArray();
                $q_proses = implode(', ', $q_proses);
                $where[] = "A.id_proses IN (".$q_proses.") ";
                $where2[] = "B.id_proses IN (".$q_proses.") ";
            }
        } 

        if(isset($req['id_workorder_barang'])){
            if($req['id_workorder_barang'] != ''){

                $where[] = "A.id_workorder_barang = ".$req['id_workorder_barang']." ";
                $where2[] = "B.id_workorder_barang = ".$req['id_workorder_barang']." ";
                
                $order = DB::table('pem_workorder_barang')->where('id_workorder_barang', $req['id_workorder_barang'])->first()->no_workorder;
            }
        } 

        if(isset($req['id_motif'])){
            if($req['id_motif'] != ''){

                $where[] = "A.id_motif = ".$req['id_motif']." ";
                $where2[] = "B.id_motif = ".$req['id_motif']." ";

                $motif  = DB::table('ms_motif')->where('id_motif', $req['id_motif'])->first()->nama_motif;
            }else{
                $where[] = "A.id_motif != '' ";
                $where2[] = "B.id_motif != '' ";    
            }
        }else{
            $where[] = "A.id_motif != '' ";
            $where2[] = "B.id_motif != '' ";            
        }

        if(isset($req['id_kualitas'])){
            if($req['id_kualitas'] != ''){

                $where[] = "A.id_kualitas = ".$req['id_kualitas']." ";
                $where2[] = "B.id_kualitas = ".$req['id_kualitas']." ";
            }
        }


        switch ($jenis) {
            case '1':
        $data['induk'] = array("barang" => $barang, 'nama_lokasi' => $lokasi, 'tanggal_awal' => $tgl_awal, 'tanggal_akhir' => $tgl_akhir, 'nama_gudang' => $gudang['nama_gudang'], 'lokasi_gudang' => $gudang['lokasi_gudang'], 'nama_komposisi' => $warna['nama'], 'inisial_komposisi' => $warna['inisial'], 'id_proses' => $id_proses, 'nama_proses' => $proses, "_proses" => $_proses, 'kategori' => $kategori, 'nama_motif' => $motif, "id_lokasi" => $id_lokasi);
        $data['where'] = implode(" AND ", $where);
        $data['where2'] = implode(" AND ", $where2);
        
        $groupBy_awal = "GROUP BY A.id_barang, A.id_proses, A.id_satuan, A.id_corak, A.id_kategori, A.id_motif";
        $groupBy_detail = "GROUP BY B.id_barang, B.id_proses, B.id_satuan, B.id_corak, B.id_kategori, B.id_motif";
        $on_where = "ON A.id_kategori = C.id_kategori AND A.id_barang = C.id_barang AND A.id_gudang = C.id_gudang AND A.id_proses = C.id_proses AND A.id_satuan = C.id_satuan AND A.id_motif = C.id_motif";
        $concat_awal = ", CONCAT(A.id_barang, A.id_proses, A.id_satuan, A.id_motif, A.id_kategori) as id_sort, '0' as id_sort_detail, '0' AS jenis";
        $concat_detail = ", CONCAT(A.id_barang, A.id_proses, A.id_satuan, A.id_motif, A.id_kategori) as id_sort, CONCAT(A.id_barang, A.id_proses, A.id_satuan, A.id_motif, A.id_kategori) as id_sort_detail, '1' AS jenis";

        $where_detail = $add_where.$data['where'].$tgl_where_detail;    
        $where_awal = $add_where.$data['where'].$tgl_where_detail.$groupBy_awal;
        $where2_awal = $add_where.$data['where2'].$groupBy_detail;

        $query['saldo_awal'] = get_mutasi_saldoawal($where_awal, $where2_awal, $on_where, $concat_awal);
        $query['detail'] = get_mutasi_detail($where_detail, $concat_detail);
        $query_mutasi = implode(" UNION ALL ", $query);
        $query_rekap_mutasi = "SELECT A.*, mk.inisial as inisial_komposisi, ms.satuan as inisial_satuan FROM ($query_mutasi) AS A LEFT JOIN ms_warnakomposisi AS mk ON A.id_komposisi=mk.id_komposisi LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY A.id_sort, A.id_sort_detail, A.tanggal_full";
        $query_hasil_rekap = DB::select($query_rekap_mutasi);

        //print_r($query_rekap_mutasi);

        $arr = array();
        if($query_hasil_rekap > 0){
            $total = 0;
            foreach($query_hasil_rekap as $d){
            
             if($d->jenis == 0){
                $ket_1 = '';
                $ket_2 = '';
                $ket_3 = 'Saldo Awal';
                $ket_4 = '';
                $ket_5 = '';
                $total = $d->stok;
                $masuk = '0';
                $keluar = '0';
                $jumlah = $d->stok;
             }else{
                $ket_1 = $d->ket_1;
                $ket_2 = $d->ket_2;
                $ket_3 = $d->ket_3;
                $ket_4 = $d->ket_4;
                $ket_5 = $d->ket_5;
                $total = ($total+$d->masuk)-$d->keluar;
                $masuk = $d->masuk;
                $keluar = $d->keluar;
                $jumlah = 0;
             }   
            $arr[] = array('id' => $d->id, 
                            'id_barang' => $d->id_barang, 
                            'kode_barang' => $d->kode_barang,
                            'nama_barang' => $d->nama_barang,
                            'id_kategori' => $d->id_kategori,
                            'nama_kategori' => $d->nama_kategori,
                            'id_subkategori' => $d->id_subkategori,
                            'nama_subkategori' => $d->nama_subkategori,
                            'jumlah' => $jumlah,
                            'masuk' => $masuk,
                            'keluar' => $keluar,
                            'id_gudang' => $d->id_gudang,
                            'nama_gudang' => $d->nama_gudang,
                            'tanggal' => $d->tanggal,
                            'id_proses' => $d->id_proses,
                            'nama_proses' => $d->nama_proses,
                            'id_komposisi' => $d->id_komposisi,
                            'nama_komposisi' => $d->nama_komposisi,
                            'inisial_komposisi' => $d->inisial_warna,
                            'id_satuan' => $d->id_satuan,
                            'nama_satuan' => $d->inisial_satuan,
                            'id_corak' => $d->id_corak,
                            'nama_corak' => $d->nama_corak,
                            'id_motif' => $d->id_motif,
                            'nama_motif' => $d->nama_motif,
                            'id_workorder' => $d->id_workorder,
                            'no_workorder' => $d->no_workorder,
                            'id_workorder_barang' => $d->id_workorder_barang,
                            'no_workorder_barang' => $d->no_workorder_barang,
                            '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_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_cap' => $d->id_cap,
                            'nama_cap' => $d->nama_cap,
                            'id_kualitas_proses' => $d->id_kualitas_proses,
                            'nama_kualitas_proses' => $d->nama_kualitas_proses,
                            'id_kualitas_periksa' => $d->id_kualitas_periksa,
                            'nama_kualitas_periksa' => $d->nama_kualitas_periksa,
                            'id_perubahansatuan' => $d->id_perubahansatuan,
                            'nama_perubahansatuan' => $d->nama_perubahansatuan,
                            'jumlah_rencana' => $d->jumlah_rencana,
                            'ket_1' => $ket_1,
                            'ket_2' => $ket_2,
                            'ket_3' => $ket_3,
                            'ket_4' => $ket_4,
                            'ket_5' => $ket_5,
                            'stok' => '0',
                            'nama_warna' => $d->nama_warna,
                            'inisial_warna' => $d->inisial_warna,
                            'ket_proses' => $d->ket_proses,
                            'jenis' => $d->jenis,
                            'total' => $total
                            );
            }
        }else{
            $arr = array();
        }
               
        $data['detail'] = barang_byproses($arr, 5, $proses);
                $html = "MutasiTenun_barangfilterCetak"; //"MutasibarangCetak";
                break;



            case '2':
                $data['induk'] = array("barang" => $barang, 'nama_lokasi' => $lokasi, 'tanggal_awal' => $tgl_awal, 'tanggal_akhir' => $tgl_akhir, 'nama_gudang' => $gudang['nama_gudang'], 'lokasi_gudang' => $gudang['lokasi_gudang'], 'nama_komposisi' => $warna['nama'], 'inisial_komposisi' => $warna['inisial'], 'id_proses' => $id_proses, 'nama_proses' => $proses, 'kategori' => $kategori, 'nama_motif' => $motif);

        $data['where'] = implode(" AND ", $where);
        $data['group'] = " GROUP BY A.id_proses, A.id_satuan, A.id_motif, A.id_workorder_barang,  A.id_kategori".$this->non_sekir($_proses, "A", "0"); //A.id_barang, 
        $groupBy = " GROUP BY A.id_proses, A.id_satuan, A.id_motif, A.id_kategori".$this->non_sekir($_proses, "A", "0");

        $where_detail = $add_where.$data['where'].$tgl_where_detail.$data['group']; 
        $field_sum_detail = ", SUM(A.masuk) AS jumlah_masuk, SUM(A.keluar) AS jumlah_keluar, '0' AS jumlah_awal";    
        $where_awal = $add_where.$data['where'].$tgl_where_awal.$data['group'];
        $field_sum_awal = ", '0' AS jumlah_masuk, '0' AS jumlah_keluar, SUM(A.masuk-A.keluar) AS jumlah_awal";
        $orderby = _getorderproduksi($_proses, "A");
        $query['saldo_awal'] = get_mutasirekap2($where_awal, '', $field_sum_awal);
        $query['detail'] = get_mutasirekap2($where_detail, '', $field_sum_detail);
        $query_mutasi = implode(" UNION ALL ", $query);
        $query_rekap_mutasi = "SELECT A.*, mk.inisial as inisial_komposisi, SUM(A.jumlah_masuk) AS mutasi_masuk, SUM(A.jumlah_keluar) AS mutasi_keluar, SUM(A.jumlah_awal) AS mutasi_awal, ms.satuan as inisial_satuan FROM ($query_mutasi) AS A LEFT JOIN ms_warnakomposisi AS mk ON A.id_komposisi=mk.id_komposisi LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan $groupBy $orderby";
        $query_hasil_rekap = DB::select($query_rekap_mutasi);

        $arr = array();
        if($query_hasil_rekap > 0){
            foreach($query_hasil_rekap as $d){
            $arr[] = array('id' => $d->id, 
                            'id_barang' => $d->id_barang, 
                            'kode_barang' => $d->kode_barang,
                            'nama_barang' => $d->nama_barang,
                            'id_kategori' => $d->id_kategori,
                            'nama_kategori' => $d->nama_kategori,
                            'id_subkategori' => $d->id_subkategori,
                            'nama_subkategori' => $d->nama_subkategori,
                            'sisa' => ($d->mutasi_awal+$d->mutasi_masuk)-$d->mutasi_keluar,
                            'masuk' => $d->mutasi_masuk,
                            'keluar' => $d->mutasi_keluar,
                            'awal' => $d->mutasi_awal,
                            'id_gudang' => $d->id_gudang,
                            'nama_gudang' => $d->nama_gudang,
                            'tanggal' => $req['tgl_awal'],
                            'id_proses' => $d->id_proses,
                            'nama_proses' => $d->nama_proses,
                            'id_komposisi' => $d->id_komposisi,
                            'nama_komposisi' => $d->nama_komposisi,
                            'inisial_komposisi' => $d->inisial_komposisi,
                            'id_satuan' => $d->id_satuan,
                            'nama_satuan' => $d->inisial_satuan,
                            'id_corak' => $d->id_corak,
                            'nama_corak' => $d->nama_corak,
                            'id_motif' => $d->id_motif,
                            'nama_motif' => $d->nama_motif,
                            'id_workorder' => $d->id_workorder,
                            'no_workorder' => $d->no_workorder,
                            'id_workorder_barang' => $d->id_workorder_barang,
                            'no_workorder_barang' => $d->no_workorder_barang,
                            '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_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_cap' => $d->id_cap,
                            'nama_cap' => $d->nama_cap,
                            'id_kualitas_proses' => $d->id_kualitas_proses,
                            'nama_kualitas_proses' => $d->nama_kualitas_proses,
                            'id_kualitas_periksa' => $d->id_kualitas_periksa,
                            'nama_kualitas_periksa' => $d->nama_kualitas_periksa,
                            'id_perubahansatuan' => $d->id_perubahansatuan,
                            'nama_perubahansatuan' => $d->nama_perubahansatuan,
                            'jumlah_rencana' => $d->jumlah_rencana
                            );
            }
        }else{
            $arr = array();
        }
            //    print_r("SELECT A.*, mk.inisial as inisial_komposisi, SUM(A.jumlah_masuk) AS mutasi_masuk, SUM(A.jumlah_keluar) AS mutasi_keluar, SUM(A.jumlah_awal) AS mutasi_awal, ms.satuan as inisial_satuan FROM ($query_mutasi) AS A LEFT JOIN ms_warnakomposisi AS mk ON A.id_komposisi=mk.id_komposisi LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan $groupBy ORDER BY A.nama_barang, A.nama_motif, A.nama_komposisi, A.nama_proses");
        $data['detail'] = barang_byproses($arr, 4, $proses);
                $html = "MutasiTenun_rekapfilterCetak";
                break;
            default:
                $html = "MutasigudangCetak";
                break;
        }
        // require('fpdf181/Mc_table.php');
        // return response(view('produksi.cetak.'.$html)->with('data', $data))->header('Content-Type', 'application/pdf');
        //return response()->json($jenis);

        if(isset($req['is_exceltenun'])){
            if($req['is_exceltenun'] == 1){
                $barang = 'Semua Barang';
                if($data['induk']['barang'] != false){
                    $barang = $data['induk']['barang']->nama_barang;
                }
                Excel::create("Mutasi Rekap _".$data['induk']['nama_gudang']."_".$barang."_".date_range($data['induk']['tanggal_awal'], $data['induk']['tanggal_akhir']), function($excel) use ($html, $data){
                    $excel->sheet("Sheet 1", function($sheet) use ($html, $data){
                        $sheet->loadView('produksi.cetak.'.$html."_Excel", $data);
                    });
                })->export("xlsx");
            }else{
                require('fpdf181/Mc_table.php');
                return response(view('produksi.cetak.'.$html)->with('data', $data))->header('Content-Type', 'application/pdf');
            }
        }else{
            require('fpdf181/Mc_table.php');
            return response(view('produksi.cetak.'.$html)->with('data', $data))->header('Content-Type', 'application/pdf');
        }
                
    }

    
    //--------------------- Laporan Produksi -------------------

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

        

        $data['header'] = array("nama_pegawai" => '', 
                            "nama_rekanan" => '', 
                            "nama_proses" => '', 
                            "nama_lokasi" => '', 
                            "id_proses" => '',
                            "tanggal_awal" => tgl_full($req['tgl_awal'], ''), 
                            "tanggal_akhir" => tgl_full($req['tgl_akhir'], ''),
                            "tanggal_range" => date_range(tgl_full($req['tgl_awal'], ''), tgl_full($req['tgl_akhir'], '')));
    

        $d_query = DB::table(DB::raw("(".semua_hasil()->toSql()." UNION ALL ".semua_hasil_kemasan()->toSql().") AS A"));

        if(isset($req['id_pegawai_hasil'])){
            if($req['id_pegawai_hasil'] != 0){
                $d_query = $d_query->where("A.id_pegawai", $req['id_pegawai_hasil']);

                $data['header']['nama_pegawai'] = DB::table('ms_pegawai')->where('id_pegawai', $req['id_pegawai_hasil'])->first()->nama_pegawai;
            }else if($req['id_pegawai_hasil'] == '0'){
                $d_query = $d_query->where("A.id_pegawai", "!=", "");
                $data['header']['nama_pegawai'] = "Semua Pegawai";
            }else{
                $data['header']['nama_pegawai'] = "";
            }
        }

        if(isset($req['id_rekanan_hasil'])){
            if($req['id_rekanan_hasil'] != 0){
                $d_query = $d_query->where("A.id_rekanan", $req['id_rekanan_hasil']);
                $d_data = DB::table('ms_rekanan as mr')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->where('id_rekanan', $req['id_rekanan_hasil'])->select(DB::raw("mr.*, mrk.nama nama_kelompok"))->first();
                $data['header']['nama_rekanan'] = $d_data->nama.' - '.$d_data->nama_kelompok;
            }else if($req['id_rekanan_hasil'] == '0'){
                $data['header']['nama_rekanan'] = "Semua Rekanan";
                $d_query = $d_query->where("A.id_rekanan", "!=", "");
            }else{
                $data['header']['nama_rekanan'] = "";
            }  
        }

        if(isset($req['id_proses_hasil'])){
            if($req['id_proses_hasil'] != 0){
                $d_query = $d_query->where("A.id_proses_sekarang", $req['id_proses_hasil']);

                $data['header']['nama_proses'] = DB::table('ms_proses')->where('id_proses', $req['id_proses_hasil'])->first()->nama_proses;
                $data['header']['id_proses'] = $req['id_proses_hasil'];
            }else if($req['id_proses_hasil'] == '0'){
                $data['header']['nama_proses'] = "Semua Proses";
                $data['header']['id_proses'] = '0';
            }else{
                $data['header']['nama_proses'] = "";
                $data['header']['id_proses'] = '';
            }   
        }

        if(isset($req['id_lokasi_hasil'])){
            $d_query = $d_query->where("A.id_lokasi", $req['id_lokasi_hasil']);

            $data['header']['nama_lokasi'] = DB::table('ms_lokasi_produksi')->where('id_lokasi', $req['id_lokasi_hasil'])->first()->nama_lokasi;
        }   


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

            $d_query = $d_query->whereBetween("A.tanggal", [$tgl_awal, $tgl_akhir]);
        }

        $d_query = $d_query;

        

        if(isset($req['id_jenis_hasil'])){
            if($req['id_jenis_hasil'] == 0){
                
                $d = $d_query->groupby(DB::raw("A.id_barang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_motif, A.id_workorder_barang, A.id_kualitas, A.id_kualitas_dudul, A.id_label, A.id_kemasan, A.id_kemasansatuan, A.id_kemasansepuluh, A.id_kemasanseratus, A.id_cap, A.id_kualitas_proses, A.id_kualitas_periksa"))->select(DB::raw("A.*, CASE WHEN sum(masuk) % 1 = 0 THEN sum(masuk) ELSE ROUND(sum(masuk), 2) END AS stok"))->orderby('A.nama_proses', 'asc')->orderby('A.nama_barang', 'asc')->orderby("A.nama_corak", 'asc')->orderby('A.nama_motif', 'asc');


                $data['data'] = $d;
                if($d->count() > 0){
                    $data['status'] = 1;
                }else{
                    $data['status'] = 0;
                }
                $html = "LaporanhasilproduksirekapCetak";
            }else{
                
                if($d_query->count() > 0){
                    $jumlah = ''; $grand_tot = ''; $jml = '';
                    $tanggal = '';
                    $no = 0;
                    $tanggal_sum = '';
                    foreach($d_query->orderby('A.tanggal', 'asc')->orderby("A.noAuto", 'asc')->orderby('A.noAuto_ext', 'asc')->get() as $j => $h){

                        $tanggal_sum = tgl_full($h->tanggal, '4');

                        if($tanggal != ''){
                            if($tanggal == tgl_full($h->tanggal, '4')){
                                $tanggal = tgl_full($h->tanggal, '4');    
                            }
                        }else{
                            $tanggal = tgl_full($h->tanggal, '4');
                        }
                    

                        

                        if(isset($jumlah[$tanggal_sum][$h->id_satuan])){
                            $jumlah[$tanggal_sum][$h->id_satuan] = array('nama_satuan' => $h->nama_satuan, 'jumlah' => $jumlah[$tanggal_sum][$h->id_satuan]['jumlah']+$h->jum_barang);
                        }else{
                            $jumlah[$tanggal_sum][$h->id_satuan] = array('nama_satuan' => $h->nama_satuan, 'jumlah' => $h->jum_barang);
                        }

                        if(isset($grand_tot[$h->id_satuan])){
                            $grand_tot[$h->id_satuan] = array('jumlah' => $grand_tot[$h->id_satuan]['jumlah']+$h->jum_barang, 'nama_satuan' => $h->nama_satuan);
                        }else{
                            $grand_tot[$h->id_satuan] = array('jumlah' => $h->jum_barang, 'nama_satuan' => $h->nama_satuan);
                        }

                        $jml[$tanggal_sum][$h->id_satuan][] = array('nama_satuan' => $h->nama_satuan, 'jumlah' => $h->jum_barang);

                        
                        if($tanggal != tgl_full($h->tanggal, 4)){
                            
                            foreach($jumlah[$tanggal] as $j){
                                $data['data'][] = array('jenis' => '0', 'jumlah' => $j['jumlah'], 'nama_satuan' => $j['nama_satuan']);
                            }

                            $d = json_decode(json_encode($h), true);
                            $data['data'][] = $d+array("jenis"=>'1');

                            $tanggal = tgl_full($h->tanggal, '4');

                        }else{
                            $d = json_decode(json_encode($h), true);
                            $data['data'][] = $d+array("jenis"=>'1');

                        }

                        

                        $no++;
                    }

                    if($d_query->count() == $no){
                        foreach($jumlah[$tanggal] as $j){
                                $data['data'][] = array('jenis' => '0', 'jumlah' => $j['jumlah'], 'nama_satuan' => $j['nama_satuan']);
                            }
                        foreach($grand_tot as $j){
                            $data['data'][] = array('jenis' => '2', 'jumlah' => $j['jumlah'], 'nama_satuan' => $j['nama_satuan']);
                            
                        }
                        
                    }

                    $data['status'] = 1;
                    
                }else{
                    $data['data'] = array();
                    $data['status'] = 0;
                }


                $html = "LaporanhasilproduksiCetak";
            }
        }

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

    }


    // --------------- Mutasi Corak Multi Proses
    function mutasicorak_proses(Request $request){
        $req = $request->all();

        $add_where = "";

        $where = array();
        $where2 = array();
        $barang = array();
        $kategori = array();
        $lokasi = "";
        $tanggal = ""; 
        $gudang = ["nama_gudang" => '', "lokasi_gudang" => ''];
        $warna = ["nama" => '', "inisial" => ''];
        $proses = ''; $id_proses = '';
        $corak = '';


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

        if(count($req) > 0){
            $add_where = "WHERE ";
        }


        if(isset($req['id_kategoribarang'])){
            if($req['id_kategoribarang'] != ''){
                $where[] = "A.id_kategori = 5 ";
                $where2[] = "B.id_kategori = 5 ";

                $kategori =  DB::table('ms_kategori_barang')->where('id_kb','=',$req['id_kategoribarang'])->first()->nama;
            }
        }

        if(isset($req['id_barang'])){
            if($req['id_barang'] != ''){
            $where[] = "A.id_barang = ".$req['id_barang']." ";
            $where2[] = "B.id_barang = ".$req['id_barang']." ";
            
            $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'))->where('mb.id', $req['id_barang'])->first();
            }

       
        }
        
        if(isset($req['id_lokasi_corak'])){
            $where[] = "A.id_lokasi = ".$req['id_lokasi_corak']." ";
            $where2[] = "B.id_lokasi = ".$req['id_lokasi_corak']." ";
            
            $lokasi = DB::table('ms_lokasi_produksi')->where('id_lokasi', $req['id_lokasi_corak'])->first()->nama_lokasi;
        }

        if(isset($req['tgl_awal']) && isset($req['tgl_akhir'])){
            $tgl_awal = tgl_full($req['tgl_awal'], '2');
            $tgl_akhir = tgl_full($req['tgl_akhir'], '2');
            $tgl_where_detail = "AND A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";

            $tgl_where_awal = "AND A.tanggal < '$tgl_awal'";
            

            //$where2[] = "A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";
            $where2[] = "B.tanggal < '$tgl_awal' ";


            //$where[] = "A.tanggal <= '$tgl_akhir' ";            
            //$tanggal = date_range($tgl_awal, $tgl_akhir);

        }

        if(isset($req['id_gudang_corak'])){
            if($req['id_gudang_corak'] != ''){
                $where[] = "A.id_gudang = ".$req['id_gudang_corak']." ";
                $where2[] = "B.id_gudang = ".$req['id_gudang_corak']." ";

                $d_gudang = DB::table('ms_gudang as mg')->join('ms_lokasi_produksi as mlp', 'mg.id_lokasi', 'mlp.id_lokasi')->where('mg.id_gudang', $req['id_gudang_corak'])->select(DB::raw("mg.*, mlp.nama_lokasi"))->first();
                $gudang = ['nama_gudang' => $d_gudang->nama_gudang, 'lokasi_gudang' => $d_gudang->nama_lokasi];
            }
        }

        if(isset($req['id_warna'])){
            if($req['id_warna'] != ''){
                $where[] = "A.id_komposisi = ".$req['id_warna']." ";
                $where2[] = "B.id_komposisi = ".$req['id_warna']." ";

                $d_warna = DB::table('ms_warnakomposisi')->where('id_komposisi', $req['id_warna'])->first();
                $warna = ["nama" => $d_warna->nama, "inisial" => $d_warna->inisial];
            }
        }

        // if(isset($req['id_proses'])){
        //     if($req['id_proses'] != ''){

        //         $where[] = "A.id_proses = ".$req['id_proses']." ";
        //         $where2[] = "B.id_proses = ".$req['id_proses']." ";
                
        //     //    $proses = DB::table('ms_proses')->where('id_proses', $req['id_proses'])->first()->nama_proses;
        //         $proses = '';
        //     }
        // }

        $proses = '';
        if(isset($req['id_proses'])){
            if(!in_array('', $req['id_proses'])){

                $arr_proses = implode(',', $req['id_proses']);

                $proses = DB::table('ms_proses')->whereIn('id_proses', $req['id_proses'])->pluck('nama_proses')->toArray();
                $proses = implode(', ', $proses);
                $id_proses = '1';
                $where[] = "A.id_proses IN (".$arr_proses.") ";
                $where2[] = "B.id_proses IN (".$arr_proses.") ";
            }else{
                $proses = "Semua Proses";
            }
        }else{
            $proses = "Semua Proses";
        }

        if(isset($req['id_motif'])){
            if($req['id_motif'] == 0){

                $where[] = "A.id_motif = '' ";
                $where2[] = "B.id_motif = '' ";
            }
        }

        if(isset($req['id_corak'])){
            if($req['id_corak'] != ''){

                $where[] = "A.id_corak = ".$req['id_corak']." ";
                $where2[] = "B.id_corak = ".$req['id_corak']." ";

                $corak  = DB::table('ms_motif_barang')->where('id_motif_barang', $req['id_corak'])->first()->motif_keterangan;
            }else{
                $where[] = "A.id_corak != '' ";
                $where2[] = "B.id_corak != '' ";
            }
        }

        $data['induk'] = array("barang" => $barang, 'nama_lokasi' => $lokasi, 'tanggal_awal' => $tgl_awal, 'tanggal_akhir' => $tgl_akhir, 'nama_gudang' => $gudang['nama_gudang'], 'lokasi_gudang' => $gudang['lokasi_gudang'], 'nama_komposisi' => $warna['nama'], 'inisial_komposisi' => $warna['inisial'], 'id_proses' => $id_proses, 'nama_proses' => $proses, 'kategori' => $kategori, 'nama_corak' => $corak);

        if ($jenis == '1') {
            
            $data['where'] = implode(" AND ", $where);
            $data['where2'] = implode(" AND ", $where2);
        //    $groupBy_detail = "GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_motif, A.id_corak, A.id_workorder_barang, A.id_kualitas, A.id_kualitas_dudul, A.id_label, A.id_kemasan, A.id_kemasansatuan, A.id_kemasansepuluh, A.id_kemasanseratus, A.id_cap, A.id_kualitas_proses, A.id_kualitas_periksa, A.id_perubahansatuan, A.jumlah_rencana, A.id_kategori, A.tanggal";
            $groupBy_awal = "GROUP BY A.id_proses, A.id_satuan, A.id_corak, A.id_kategori, A.id_komposisi";
            $groupBy_detail = "GROUP BY B.id_proses, B.id_satuan, B.id_corak, B.id_kategori, B.id_komposisi";
            $on_where = "ON A.id_kategori = C.id_kategori  AND A.id_proses = C.id_proses AND A.id_komposisi = C.id_komposisi AND A.id_satuan = C.id_satuan AND A.id_corak = C.id_corak ";
            $concat_awal = ", CONCAT(A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_kategori) as id_sort, '0' as id_sort_detail, '0' AS jenis";
            $concat_detail = ", CONCAT(A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_kategori) as id_sort, CONCAT(A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_kategori) as id_sort_detail, '1' AS jenis";

            $where_detail = $add_where.$data['where'].$tgl_where_detail;    
            $where_awal = $add_where.$data['where'].$tgl_where_detail.$groupBy_awal;
            $where2_awal = $add_where.$data['where2'].$groupBy_detail;

            $query['saldo_awal'] = get_mutasi_saldoawal($where_awal, $where2_awal, $on_where, $concat_awal);
            $query['detail'] = get_mutasi_detail($where_detail, $concat_detail);
            $query_mutasi = implode(" UNION ALL ", $query);
            $query_rekap_mutasi = "SELECT A.*, mk.inisial as inisial_komposisi, ms.satuan as inisial_satuan FROM ($query_mutasi) AS A LEFT JOIN ms_warnakomposisi AS mk ON A.id_komposisi=mk.id_komposisi LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY A.id_sort, A.id_sort_detail, A.tanggal_full";
            $query_hasil_rekap = DB::select($query_rekap_mutasi);

            $arr = array();
            if($query_hasil_rekap > 0){
                $total = 0;
                foreach($query_hasil_rekap as $d){
                
                 if($d->jenis == 0){
                    $ket_1 = '';
                    $ket_2 = '';
                    $ket_3 = 'Saldo Awal';
                    $ket_4 = '';
                    $ket_5 = '';
                    $total = $d->stok;
                    $masuk = '0';
                    $keluar = '0';
                    $jumlah = $d->stok;
                 }else{
                    $ket_1 = $d->ket_1;
                    $ket_2 = $d->ket_2;
                    $ket_3 = $d->ket_3;
                    $ket_4 = $d->ket_4;
                    $ket_5 = $d->ket_5;
                    $total = ($total+$d->masuk)-$d->keluar;
                    $masuk = $d->masuk;
                    $keluar = $d->keluar;
                    $jumlah = 0;
                 }   
                $arr[] = array('id' => $d->id, 
                                'id_barang' => $d->id_barang, 
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang,
                                'id_kategori' => $d->id_kategori,
                                'nama_kategori' => $d->nama_kategori,
                                'id_subkategori' => $d->id_subkategori,
                                'kategori' => $d->nama_subkategori,
                                'jumlah' => $jumlah,
                                'masuk' => $masuk,
                                'keluar' => $keluar,
                                'id_gudang' => $d->id_gudang,
                                'nama_gudang' => $d->nama_gudang,
                                'tanggal' => $d->tanggal,
                                'id_proses' => $d->id_proses,
                                'nama_proses' => $d->nama_proses,
                                'id_komposisi' => $d->id_komposisi,
                                'nama_komposisi' => $d->nama_komposisi,
                                'inisial_komposisi' => $d->inisial_warna,
                                'id_satuan' => $d->id_satuan,
                                'nama_satuan' => $d->inisial_satuan,
                                'id_corak' => $d->id_corak,
                                'nama_corak' => $d->nama_corak,
                                'id_motif' => $d->id_motif,
                                'nama_motif' => $d->nama_motif,
                                'id_workorder' => $d->id_workorder,
                                'no_workorder' => $d->no_workorder,
                                'id_workorder_barang' => $d->id_workorder_barang,
                                'no_workorder_barang' => $d->no_workorder_barang,
                                '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_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_cap' => $d->id_cap,
                                'nama_cap' => $d->nama_cap,
                                'id_kualitas_proses' => $d->id_kualitas_proses,
                                'nama_kualitas_proses' => $d->nama_kualitas_proses,
                                'id_kualitas_periksa' => $d->id_kualitas_periksa,
                                'nama_kualitas_periksa' => $d->nama_kualitas_periksa,
                                'id_perubahansatuan' => $d->id_perubahansatuan,
                                'nama_perubahansatuan' => $d->nama_perubahansatuan,
                                'jumlah_rencana' => $d->jumlah_rencana,
                                'ket_1' => $ket_1,
                                'ket_2' => $ket_2,
                                'ket_3' => $ket_3,
                                'ket_4' => $ket_4,
                                'ket_5' => $ket_5,
                                'stok' => '0',
                                'nama_warna' => $d->nama_warna,
                                'inisial_warna' => $d->inisial_warna,
                                'ket_proses' => $d->ket_proses,
                                'jenis' => $d->jenis,
                                'total' => $total
                                );
                }
            }else{
                $arr = array();
            }
                    
            $data['detail'] = barang_byproses($arr, 4, $proses);
            $html = "Mutasicorak_multiprosesdetailCetak";


        }else if($jenis == '2'){
            $data['where'] = implode(" AND ", $where);
            $data['group'] = " GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_workorder_barang, A.id_kualitas, A.id_kualitas_dudul, A.id_label, A.id_kemasan, A.id_kemasansatuan, A.id_kemasansepuluh, A.id_kemasanseratus, A.id_cap, A.id_kualitas_proses, A.id_kualitas_periksa, A.id_kategori, A.id_komposisi";
            $groupBy = " GROUP BY A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_kategori";

            $where_detail = $add_where.$data['where'].$tgl_where_detail.$data['group']; 
            $field_sum_detail = ", SUM(A.masuk) AS jumlah_masuk, SUM(A.keluar) AS jumlah_keluar, '0' AS jumlah_awal";    
            $where_awal = $add_where.$data['where'].$tgl_where_awal.$data['group'];
            $field_sum_awal = ", '0' AS jumlah_masuk, '0' AS jumlah_keluar, SUM(A.masuk-A.keluar) AS jumlah_awal";

            $query['saldo_awal'] = get_mutasirekap2($where_awal, '', $field_sum_awal);
            $query['detail'] = get_mutasirekap2($where_detail, '', $field_sum_detail);
            $query_mutasi = implode(" UNION ALL ", $query);
            $query_rekap_mutasi = "SELECT A.*, mk.inisial as inisial_komposisi, SUM(A.jumlah_masuk) AS mutasi_masuk, SUM(A.jumlah_keluar) AS mutasi_keluar, SUM(A.jumlah_awal) AS mutasi_awal, ms.satuan as inisial_satuan FROM ($query_mutasi) AS A LEFT JOIN ms_warnakomposisi AS mk ON A.id_komposisi=mk.id_komposisi LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan $groupBy ORDER BY A.nama_corak, A.nama_komposisi, A.nama_proses, A.nama_subkategori, A.nama_barang";
            $query_hasil_rekap = DB::select($query_rekap_mutasi);

            $arr = array();
            if($query_hasil_rekap > 0){
                foreach($query_hasil_rekap as $d){
                $arr[] = array('id' => $d->id, 
                                'id_barang' => $d->id_barang, 
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang,
                                'id_kategori' => $d->id_kategori,
                                'nama_kategori' => $d->nama_kategori,
                                'id_subkategori' => $d->id_subkategori,
                                'kategori' => $d->nama_subkategori,
                                'sisa' => ($d->mutasi_awal+$d->mutasi_masuk)-$d->mutasi_keluar,
                                'masuk' => $d->mutasi_masuk,
                                'keluar' => $d->mutasi_keluar,
                                'awal' => $d->mutasi_awal,
                                'id_gudang' => $d->id_gudang,
                                'nama_gudang' => $d->nama_gudang,
                                'tanggal' => $req['tgl_awal'],
                                'id_proses' => $d->id_proses,
                                'nama_proses' => $d->nama_proses,
                                'id_komposisi' => $d->id_komposisi,
                                'nama_komposisi' => $d->nama_komposisi,
                                'inisial_komposisi' => $d->inisial_komposisi,
                                'id_satuan' => $d->id_satuan,
                                'nama_satuan' => $d->inisial_satuan,
                                'id_corak' => $d->id_corak,
                                'nama_corak' => $d->nama_corak,
                                'id_motif' => $d->id_motif,
                                'nama_motif' => $d->nama_motif,
                                'id_workorder' => $d->id_workorder,
                                'no_workorder' => $d->no_workorder,
                                'id_workorder_barang' => $d->id_workorder_barang,
                                'no_workorder_barang' => $d->no_workorder_barang,
                                '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_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_cap' => $d->id_cap,
                                'nama_cap' => $d->nama_cap,
                                'id_kualitas_proses' => $d->id_kualitas_proses,
                                'nama_kualitas_proses' => $d->nama_kualitas_proses,
                                'id_kualitas_periksa' => $d->id_kualitas_periksa,
                                'nama_kualitas_periksa' => $d->nama_kualitas_periksa,
                                'id_perubahansatuan' => $d->id_perubahansatuan,
                                'nama_perubahansatuan' => $d->nama_perubahansatuan,
                                'jumlah_rencana' => $d->jumlah_rencana
                                );
                }
            }else{
                $arr = array();
            }

            $data['detail'] = barang_byproses($arr, 103, $proses);
            $html = "Mutasicorak_multiprosesCetak";
        }else{

        }
        

        if(isset($req['is_excelgudangcorakmulti'])){
            if($req['is_excelgudangcorakmulti'] == 1){
                $barang = 'Semua Barang';
                if($data['induk']['barang'] != false){
                    $barang = $data['induk']['barang']->nama_barang;
                }
                Excel::create("Mutasi Rekap _".$data['induk']['nama_gudang']."_".$barang."_".date_range($data['induk']['tanggal_awal'], $data['induk']['tanggal_akhir']), function($excel) use ($html, $data){
                    $excel->sheet("Sheet 1", function($sheet) use ($html, $data){
                        $sheet->loadView('produksi.cetak.'.$html."_Excel", $data);
                    });
                })->export("xlsx");
            }else{
                require('fpdf181/Mc_table.php');
                return response(view('produksi.cetak.'.$html)->with('data', $data))->header('Content-Type', 'application/pdf');
            }
        }else{
            require('fpdf181/Mc_table.php');
            return response(view('produksi.cetak.'.$html)->with('data', $data))->header('Content-Type', 'application/pdf');
        }
    }
    // --------------- Mutasi Corak Multi Proses


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

        $add_where = "";

        $where = array();
        $where2 = array();
        $barang = array();
        $kategori = array();
        $lokasi = "";
        $tanggal = ""; 
        $gudang = ["nama_gudang" => '', "lokasi_gudang" => ''];
        $warna = ["nama" => '', "inisial" => ''];
        $proses = '';
        $motif = '';
        $kualitas = "Semua";
        $where_like = "";

        $jenis = 0;
        if(isset($req['jenis_multi'])){
            $jenis = $req['jenis_multi'];
        }

        if(count($req) > 0){
            $add_where = "WHERE ";
        }

        if(isset($req['id_kategoribarang'])){
            if($req['id_kategoribarang'] != ''){
                $where[] = "A.id_kategori = ".$req['id_kategoribarang']." ";

                $where2[] = "B.id_kategori = ".$req['id_kategoribarang']." ";

                $kategori =  DB::table('ms_kategori_barang')->where('id_kb','=',$req['id_kategoribarang'])->first()->nama;
            }
        }

        if(isset($req['id_barangmulti'])){
            if($req['id_barangmulti'] != ''){
            $where[] = "A.id_barang = ".$req['id_barangmulti']." ";
            $where2[] = "B.id_barang = ".$req['id_barangmulti']." ";
            
            $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'))->where('mb.id', $req['id_barangmulti'])->first();
            }       
        }

        if(isset($req['id_kualitasmulti'])){
            if($req['id_kualitasmulti'] != ''){
                if($req['id_kualitasmulti'] == 1){
                    $where[] = "A.nama_barang REGEXP 'BS|(K)|GREEN' ";
                    $where2[] = "B.nama_barang REGEXP 'BS|(K)|GREEN' ";
                    $kualitas = "BS";
                    $where_like = " WHERE A.nama_barang LIKE '%BS%' OR A.nama_barang LIKE '%GREEN%' OR A.nama_barang LIKE '%(K)%'";
                }else{
                    $where[] = "A.nama_barang NOT REGEXP 'BS|GREEN' ";
                    $where2[] = "B.nama_barang NOT REGEXP 'BS|GREEN' ";
                    $kualitas = "BAIK";
                    $where_like = " WHERE A.nama_barang NOT LIKE '%(K)%'";
                }
            }
        }
        
        if(isset($req['id_lokasimulti'])){
            $where[] = "A.id_lokasi = ".$req['id_lokasimulti']." ";
            $where2[] = "B.id_lokasi = ".$req['id_lokasimulti']." ";
            
            $lokasi = DB::table('ms_lokasi_produksi')->where('id_lokasi', $req['id_lokasimulti'])->first()->nama_lokasi;
        }

        if(isset($req['tgl_awalmulti']) && isset($req['tgl_akhirmulti'])){
            $tgl_awal = tgl_full($req['tgl_awalmulti'], '2');
            $tgl_akhir = tgl_full($req['tgl_akhirmulti'], '2');
            $tgl_where_detail = "AND A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";

            $tgl_where_awal = "AND A.tanggal < '$tgl_awal'";
            $where2[] = "B.tanggal < '$tgl_awal' ";

        }

        if(isset($req['id_gudangmulti'])){
            if($req['id_gudangmulti'] != ''){
                $where[] = "A.id_gudang = ".$req['id_gudangmulti']." ";
                $where2[] = "B.id_gudang = ".$req['id_gudangmulti']." ";

                $d_gudang = DB::table('ms_gudang as mg')->join('ms_lokasi_produksi as mlp', 'mg.id_lokasi', 'mlp.id_lokasi')->where('mg.id_gudang', $req['id_gudangmulti'])->select(DB::raw("mg.*, mlp.nama_lokasi"))->first();
                $gudang = ['nama_gudang' => $d_gudang->nama_gudang, 'lokasi_gudang' => $d_gudang->nama_lokasi];
            }
        }

        if(isset($req['id_prosesmulti'])){
            if($req['id_prosesmulti'] != ''){

                $where[] = "A.id_proses = ".$req['id_prosesmulti']." ";
                $where2[] = "B.id_proses = ".$req['id_prosesmulti']." ";
                
                $proses = DB::table('ms_proses')->where('id_proses', $req['id_prosesmulti'])->first()->nama_proses;
            }
        }

        if(isset($req['id_motifmulti'])){
            if($req['id_motifmulti'] != ''){

                $where[] = "A.id_motif = ".$req['id_motifmulti']." ";
                $where2[] = "B.id_motif = ".$req['id_motifmulti']." ";

                $motif  = DB::table('ms_motif')->where('id_motif', $req['id_motifmulti'])->first()->nama_motif;
            }else{
                $where[] = "A.id_motif != '' ";
                $where2[] = "B.id_motif != '' ";
            }
        }

        if($jenis == '1'){
            $data['induk'] = array("barang" => $barang, 'nama_lokasi' => $lokasi, 'tanggal_awal' => $tgl_awal, 'tanggal_akhir' => $tgl_akhir, 'nama_gudang' => $gudang['nama_gudang'], 'lokasi_gudang' => $gudang['lokasi_gudang'], 'nama_komposisi' => $warna['nama'], 'inisial_komposisi' => $warna['inisial'], 'nama_proses' => $proses, 'kategori' => $kategori, 'nama_motif' => $motif, "kualitas" => $kualitas);
            $data['where'] = implode(" AND ", $where);
            $data['where2'] = implode(" AND ", $where2);
        //    $groupBy_detail = "GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_motif, A.id_corak, A.id_workorder_barang, A.id_kualitas, A.id_kualitas_dudul, A.id_label, A.id_kemasan, A.id_kemasansatuan, A.id_kemasansepuluh, A.id_kemasanseratus, A.id_cap, A.id_kualitas_proses, A.id_kualitas_periksa, A.id_perubahansatuan, A.jumlah_rencana, A.id_kategori, A.tanggal";
            $groupBy_awal = "GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_satuan, A.id_corak, A.id_kategori, A.id_komposisi";
            $on_where = "ON A.id_kategori = C.id_kategori AND A.id_barang = C.id_barang AND A.id_gudang = C.id_gudang AND A.id_proses = C.id_proses AND A.id_komposisi = C.id_komposisi AND A.id_satuan = C.id_satuan AND A.id_corak = C.id_corak AND A.id_motif = C.id_motif AND A.id_workorder = C.id_workorder AND A.id_workorder_barang = C.id_workorder_barang AND A.id_kualitas_dudul = C.id_kualitas_dudul AND A.id_label = C.id_label AND A.id_kemasan = C.id_kemasan AND A.id_kualitas = C.id_kualitas AND A.id_kemasansatuan = C.id_kemasansatuan AND A.id_kemasanseratus = C.id_kemasanseratus AND A.id_cap = C.id_cap AND A.id_kualitas_proses = C.id_kualitas_proses AND A.id_kualitas_periksa = C.id_kualitas_periksa";
            $concat_awal = ", CONCAT(".$this->concat_multi("A").") as id_sort, '0' as id_sort_detail, '0' AS jenis";
            $concat_detail = ", CONCAT(".$this->concat_multi("A").") as id_sort, CONCAT(".$this->concat_multi("A").") as id_sort_detail, '1' AS jenis";

            $where_detail = $add_where.$data['where'].$tgl_where_detail;    
            $where_awal = $add_where.$data['where'].$tgl_where_detail.$this->groupby_multi('A');
            $where2_awal = $add_where.$data['where2'].$this->groupby_multi('B');

            $query['saldo_awal'] = get_mutasi_saldoawal($where_awal, $where2_awal, $on_where, $concat_awal);
            $query['detail'] = get_mutasi_detail($where_detail, $concat_detail);
            $query_mutasi = implode(" UNION ALL ", $query);
            $query_rekap_mutasi = "SELECT A.*, mk.inisial as inisial_komposisi, ms.satuan as inisial_satuan FROM ($query_mutasi) AS A LEFT JOIN ms_warnakomposisi AS mk ON A.id_komposisi=mk.id_komposisi LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan $where_like ORDER BY A.id_sort, A.id_sort_detail, A.tanggal_full";
            $query_hasil_rekap = DB::select($query_rekap_mutasi);

            $arr = array();
            if($query_hasil_rekap > 0){
                $total = 0;
                foreach($query_hasil_rekap as $d){
                
                 if($d->jenis == 0){
                    $ket_1 = '';
                    $ket_2 = '';
                    $ket_3 = 'Saldo Awal';
                    $ket_4 = '';
                    $ket_5 = '';
                    $total = $d->stok;
                    $masuk = '0';
                    $keluar = '0';
                    $jumlah = $d->stok;
                 }else{
                    $ket_1 = $d->ket_1;
                    $ket_2 = $d->ket_2;
                    $ket_3 = $d->ket_3;
                    $ket_4 = $d->ket_4;
                    $ket_5 = $d->ket_5;
                    $total = ($total+$d->masuk)-$d->keluar;
                    $masuk = $d->masuk;
                    $keluar = $d->keluar;
                    $jumlah = 0;
                 }   
                $arr[] = array('id' => $d->id, 
                                'id_barang' => $d->id_barang, 
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang,
                                'id_kategori' => $d->id_kategori,
                                'nama_kategori' => $d->nama_kategori,
                                'id_subkategori' => $d->id_subkategori,
                                'nama_subkategori' => $d->nama_subkategori,
                                'jumlah' => $jumlah,
                                'masuk' => $masuk,
                                'keluar' => $keluar,
                                'id_gudang' => $d->id_gudang,
                                'nama_gudang' => $d->nama_gudang,
                                'tanggal' => $d->tanggal,
                                'id_proses' => $d->id_proses,
                                'nama_proses' => $d->nama_proses,
                                'id_komposisi' => $d->id_komposisi,
                                'nama_komposisi' => $d->nama_komposisi,
                                'inisial_komposisi' => $d->inisial_warna,
                                'id_satuan' => $d->id_satuan,
                                'nama_satuan' => $d->inisial_satuan,
                                'id_corak' => $d->id_corak,
                                'nama_corak' => $d->nama_corak,
                                'id_motif' => $d->id_motif,
                                'nama_motif' => $d->nama_motif,
                                'id_workorder' => $d->id_workorder,
                                'no_workorder' => $d->no_workorder,
                                'id_workorder_barang' => $d->id_workorder_barang,
                                'no_workorder_barang' => $d->no_workorder_barang,
                                '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_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_cap' => $d->id_cap,
                                'nama_cap' => $d->nama_cap,
                                'id_kualitas_proses' => $d->id_kualitas_proses,
                                'nama_kualitas_proses' => $d->nama_kualitas_proses,
                                'id_kualitas_periksa' => $d->id_kualitas_periksa,
                                'nama_kualitas_periksa' => $d->nama_kualitas_periksa,
                                'id_perubahansatuan' => $d->id_perubahansatuan,
                                'nama_perubahansatuan' => $d->nama_perubahansatuan,
                                'jumlah_rencana' => $d->jumlah_rencana,
                                'ket_1' => $ket_1,
                                'ket_2' => $ket_2,
                                'ket_3' => $ket_3,
                                'ket_4' => $ket_4,
                                'ket_5' => $ket_5,
                                'stok' => '0',
                                'nama_warna' => $d->nama_warna,
                                'inisial_warna' => $d->inisial_warna,
                                'ket_proses' => $d->ket_proses,
                                'jenis' => $d->jenis,
                                'total' => $total
                                );
                }
            }else{
                $arr = array();
            }

            $data['detail'] = barang_byproses($arr, 4, $proses);
            $html = "Mutasigudang_multidetail";

        //    print_r($query_rekap_mutasi);

        }else if($jenis == '2'){
            $data['induk'] = array("barang" => $barang, 'nama_lokasi' => $lokasi, 'tanggal_awal' => $tgl_awal, 'tanggal_akhir' => $tgl_akhir, 'nama_gudang' => $gudang['nama_gudang'], 'lokasi_gudang' => $gudang['lokasi_gudang'], 'nama_komposisi' => $warna['nama'], 'inisial_komposisi' => $warna['inisial'], 'nama_proses' => $proses, 'kategori' => $kategori, 'nama_motif' => $motif, "kualitas" => $kualitas);
            $data['where'] = implode(" AND ", $where);
            $data['group'] = " GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_motif, A.id_workorder_barang, A.id_kualitas, A.id_kualitas_dudul, A.id_label, A.id_kemasan, A.id_kemasansatuan, A.id_kemasansepuluh, A.id_kemasanseratus, A.id_cap, A.id_kualitas_proses, A.id_kualitas_periksa, A.id_kategori, A.id_komposisi";
            $groupBy = " GROUP BY A.id_barang, A.id_gudang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_corak, A.id_motif, A.id_kualitas, A.id_kualitas_dudul, A.id_label, A.id_kemasan, A.id_kemasansatuan, A.id_kemasansepuluh, A.id_kemasanseratus, A.id_cap, A.id_kualitas_proses, A.id_kualitas_periksa, A.id_kategori, A.id_komposisi";

            $where_detail = $add_where.$data['where'].$tgl_where_detail.$this->groupby_multi('A'); 
            $field_sum_detail = ", SUM(A.masuk) AS jumlah_masuk, SUM(A.keluar) AS jumlah_keluar, '0' AS jumlah_awal";    
            $where_awal = $add_where.$data['where'].$tgl_where_awal.$this->groupby_multi('A');
            $field_sum_awal = ", '0' AS jumlah_masuk, '0' AS jumlah_keluar, SUM(A.masuk-A.keluar) AS jumlah_awal";

            $query['saldo_awal'] = get_mutasirekap2($where_awal, '', $field_sum_awal);
            $query['detail'] = get_mutasirekap2($where_detail, '', $field_sum_detail);
            $query_mutasi = implode(" UNION ALL ", $query);
            $query_rekap_mutasi = "SELECT A.*, mk.inisial as inisial_komposisi, SUM(A.jumlah_masuk) AS mutasi_masuk, SUM(A.jumlah_keluar) AS mutasi_keluar, SUM(A.jumlah_awal) AS mutasi_awal, ms.satuan as inisial_satuan FROM ($query_mutasi) AS A LEFT JOIN ms_warnakomposisi AS mk ON A.id_komposisi=mk.id_komposisi LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan $where_like $groupBy ORDER BY A.nama_subkategori, A.nama_barang, A.nama_corak, A.nama_komposisi, A.nama_proses";
            // print_r($query_rekap_mutasi); exit();
            $query_hasil_rekap = DB::select($query_rekap_mutasi);

            $arr = array();
            if($query_hasil_rekap > 0){
                foreach($query_hasil_rekap as $d){
                $arr[] = array('id' => $d->id, 
                                'id_barang' => $d->id_barang, 
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang,
                                'id_kategori' => $d->id_kategori,
                                'nama_kategori' => $d->nama_kategori,
                                'id_subkategori' => $d->id_subkategori,
                                'nama_subkategori' => $d->nama_subkategori,
                                'sisa' => ($d->mutasi_awal+$d->mutasi_masuk)-$d->mutasi_keluar,
                                'masuk' => $d->mutasi_masuk,
                                'keluar' => $d->mutasi_keluar,
                                'awal' => $d->mutasi_awal,
                                'id_gudang' => $d->id_gudang,
                                'nama_gudang' => $d->nama_gudang,
                                'tanggal' => $req['tgl_awalmulti'],
                                'id_proses' => $d->id_proses,
                                'nama_proses' => $d->nama_proses,
                                'id_komposisi' => $d->id_komposisi,
                                'nama_komposisi' => $d->nama_komposisi,
                                'inisial_komposisi' => $d->inisial_komposisi,
                                'id_satuan' => $d->id_satuan,
                                'nama_satuan' => $d->inisial_satuan,
                                'id_corak' => $d->id_corak,
                                'nama_corak' => $d->nama_corak,
                                'id_motif' => $d->id_motif,
                                'nama_motif' => $d->nama_motif,
                                'id_workorder' => $d->id_workorder,
                                'no_workorder' => $d->no_workorder,
                                'id_workorder_barang' => $d->id_workorder_barang,
                                'no_workorder_barang' => $d->no_workorder_barang,
                                '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_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_cap' => $d->id_cap,
                                'nama_cap' => $d->nama_cap,
                                'id_kualitas_proses' => $d->id_kualitas_proses,
                                'nama_kualitas_proses' => $d->nama_kualitas_proses,
                                'id_kualitas_periksa' => $d->id_kualitas_periksa,
                                'nama_kualitas_periksa' => $d->nama_kualitas_periksa,
                                'id_perubahansatuan' => $d->id_perubahansatuan,
                                'nama_perubahansatuan' => $d->nama_perubahansatuan,
                                'jumlah_rencana' => $d->jumlah_rencana
                                );
                }
            }else{
                $arr = array();
            }

            $data['detail'] = barang_byproses($arr, 4, $proses);
            $html = "Mutasigudang_multi";

        }else{

        }

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

        if(isset($req['is_excelmulti'])){
            if($req['is_excelmulti'] == 1){
                $barang = 'Semua Barang';
                if($data['induk']['barang'] != false){
                    $barang = $data['induk']['barang']->nama_barang;
                }
                Excel::create("Mutasi Rekap _".$data['induk']['nama_gudang']."_".$barang."_".date_range($data['induk']['tanggal_awal'], $data['induk']['tanggal_akhir']), function($excel) use ($html, $data){
                    $excel->sheet("Sheet 1", function($sheet) use ($html, $data){
                        $sheet->loadView('produksi.cetak.'.$html."_Excel", $data);
                    });
                })->export("xlsx");
            }else{
                require('fpdf181/Mc_table.php');
                return response(view('produksi.cetak.'.$html)->with('data', $data))->header('Content-Type', 'application/pdf');
            }
        }else{
            require('fpdf181/Mc_table.php');
            return response(view('produksi.cetak.'.$html)->with('data', $data))->header('Content-Type', 'application/pdf');
        }
    }


    // --------------- Laporan Bon Bahan

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

        $add_where = "";

        $where = array();
        $where2 = array();
        $data = array();
        $lokasi = "";
        $tanggal = ""; 
        $id_proses = '';
        $proses = '';
        $pegawai = '';
        $kelompok = '';
        $rekanan = '';
        $mesin = '';
        $arr = '';
        $orderby_proses = '';

        $concat_bbtb = "mkel.nama";
        $groupby_bbtb = "mkel.id_kelompok";
        $orderby_bbtb = ", A.nama_kelompok";

        $jenis = 0;
        if(isset($req['id_jenis_bon'])){
            $jenis = $req['id_jenis_bon'];
        }

        if(count($req) > 0){
            $add_where = "WHERE ";
        }

        if(isset($req['id_pegawai_bon'])){
            if($req['id_pegawai_bon'] != 0){
                $where[] = "pp.id_pegawai = " .$req['id_pegawai_bon']. "";

                $pegawai = DB::table('ms_pegawai')->where('id_pegawai', $req['id_pegawai_bon'])->first()->nama_pegawai;
            }else if($req['id_pegawai_bon'] == '0'){
                $pegawai = "Semua Pegawai";
                $where[] = "pp.id_pegawai != '' ";
            }else{
                $pegawai = "";
            }
        }

        if(isset($req['id_mesin_bon'])){
            $concat_bbtb = "ms.nama";
            $groupby_bbtb = "ms.id_mesin";
            $orderby_bbtb = ", A.nama_mesin";
            if($req['id_mesin_bon'] != 0){
                $where[] = "ms.id_mesin = " .$req['id_mesin_bon']. "";

                $mesin = DB::table('ms_mesin')->where('id_mesin', $req['id_mesin_bon'])->first()->nama;
            }else if($req['id_mesin_bon'] == '0'){
                $mesin = "Semua Mesin";
                $where[] = "ms.id_mesin != '' ";
            }else{
                $mesin = "";
            }
        }

        if(isset($req['id_makelar_bon'])){
            if($req['id_makelar_bon'] != 0){
                $d_petenun = DB::table('ms_rekanan as mr')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->where("mrk.id_kelompok", $req['id_makelar_bon'])->select(DB::raw("mr.*, mrk.nama nama_kelompok"));
                $d_count = $d_petenun->get()->count();
                $d_pekerja = $d_petenun->pluck('id_rekanan')->toArray();

                $where[] = "mr.id_rekanan IN ('".implode("', '", $d_pekerja)."') ";
                $d_kelompok = $d_petenun->first()->nama_kelompok;
                $kelompok = $d_kelompok;
            }else if($req['id_rekanan_bon'] == 0){
                $where[] = "mr.id_rekanan != '' ";
                $kelompok = "Semua Makelar";
            }else{

            }
        }else{

        }

        if(isset($req['id_rekanan_bon'])){
            if($req['id_rekanan_bon'] != 0 && $req['id_rekanan_bon'] != ''){
                $where[] = "mr.id_rekanan = ".$req['id_rekanan_bon']."";
                
                $d_data = DB::table('ms_rekanan as mr')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->where('id_rekanan', $req['id_rekanan_bon'])->select(DB::raw("mr.*, mrk.nama nama_kelompok"))->first();
                $rekanan = $d_data->nama;//.' - '.$d_data->nama_kelompok;
            }else if($req['id_rekanan_bon'] == '0'){
                $where[] = "mr.id_rekanan != '' ";
                $rekanan = "Semua Rekanan";
            }else{
                $rekanan = "";
            }  
        }

        
        if(isset($req['id_lokasi_bon'])){
            $where[] = "mlp.id_lokasi = ".$req['id_lokasi_bon']." ";
            
            
            $lokasi = DB::table('ms_lokasi_produksi')->where('id_lokasi', $req['id_lokasi_bon'])->first()->nama_lokasi;
        }

        if(isset($req['tgl_awal']) && isset($req['tgl_akhir'])){
            $tgl_awal = tgl_full($req['tgl_awal'], '2');
            $tgl_akhir = tgl_full($req['tgl_akhir'], '2');
            $where[] = "v.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";

            $tgl_where_awal = "AND A.tanggal < '$tgl_awal'";

            $tanggal = date_range(tgl_full($req['tgl_awal'], ''), tgl_full($req['tgl_akhir'], ''));
        }

        if(isset($req['id_proses_bon'])){
            if($req['id_proses_bon'] != ''){

                $where[] = "v.id_proses_sekarang = ".$req['id_proses_bon']." ";
                
                $proses = DB::table('ms_proses')->where('id_proses', $req['id_proses_bon'])->first();
                $id_proses = $proses->id_proses;
                $proses = $proses->nama_proses;
            }else{
                $id_proses = '';
                $proses = "Semua Proses";
            }
        }else{
            $id_proses = '';
            $proses = "Semua Proses";
        }

        $orderby_text = " ORDER BY ";

        if($id_proses != ''){
            if(in_array($id_proses, setup_benang())){
                $orderby_proses = " A.nama_barang, A.nama_komposisi ";
            }else if(in_array($id_proses, setup_corak())){
                $orderby_proses = " A.nama_corak, A.nama_motif, A.nama_komposisi ";
            }else{
                $orderby_proses = " A.nama_proses, A.nama_barang, A.nama_corak, A.nama_motif, A.nama_komposisi  ";    
            }
        }else{
            $orderby_proses = " A.nama_proses, A.nama_barang, A.nama_corak, A.nama_motif, A.nama_komposisi  ";
        }

        $data['induk'] = array('nama_lokasi' => $lokasi, 'tanggal_range' => $tanggal, 'id_proses' => $id_proses, 'nama_proses' => $proses, 'nama_pegawai' => $pegawai, 'nama_rekanan' => $rekanan, "nama_kelompok" => $kelompok, "mesin" => $mesin);
        $where_1 = implode(" AND ", $where);

        if ($jenis == '1') {

            $concat = ", CONCAT(".$this->concat_bon('v').") AS id_sort, '1' id_sort_detail, SUM(v.jumlah) jum_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";

            $concat_ = ", CONCAT(".$this->concat_bon('v').") AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) jum_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";

            $query[] = rekap_bon($concat, $add_where.$where_1." ".$this->groupby_bon('v', 'mk')."");

            $query[] = rekap_bonmulti($concat_, $add_where.$where_1." ".$this->groupby_bon('v', 'v')."");
            // print_r("SELECT A.*, ms.satuan as inisial_satuan FROM (".implode(' UNION ALL ', $query).") A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan".$orderby_text.$orderby_proses); exit();
            $rekap = DB::select("SELECT A.*, ms.satuan as inisial_satuan FROM (".implode(' UNION ALL ', $query).") A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan".$orderby_text.$orderby_proses);
            $data['detail'] = $rekap;
            $html = "LaporanBonBahanRekap";

        }else if($jenis == '2'){
            
            $concat_ = ", CONCAT(".$this->concat_bon('v').") AS id_sort, '0' id_sort_detail, v.jumlah jum_barang, v.masuk masuk_barang, v.keluar keluar_barang";
            $query['first'] = rekap_bon($concat_, $add_where.$where_1);

            $concat_multi = ", CONCAT(".$this->concat_bon('v').") AS id_sort, '0' id_sort_detail, v.jum_barang jum_barang, v.masuk masuk_barang, v.keluar keluar_barang";
            $query['first_multi'] = rekap_bonmulti($concat_multi, $add_where.$where_1);


            $concat = ", CONCAT(".$this->concat_bon('v').") AS id_sort, '1' id_sort_detail, SUM(v.jumlah) jum_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";
            $query['second'] = rekap_bon($concat, $add_where.$where_1." ".$this->groupby_bon('v', 'v')." ");

            $concat_multir = ", CONCAT(".$this->concat_bon('v').") AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) jum_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";
            $query['second_multir'] = rekap_bonmulti($concat_multir, $add_where.$where_1." ".$this->groupby_bon('v', 'v')." ");

            $group_query = implode(" UNION ALL ", $query);

            $last_query = "SELECT A.*, ms.satuan as inisial_satuan FROM ($group_query) A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY A.id_sort, A.id_sort_detail, A.tanggal_full";

            $rekap = DB::select($last_query);

            // print_r($rekap);

            $data['detail'] = $rekap;
            $html = "LaporanBonBahanDetailBarang";
        }else if($jenis == 3){
            $concat_ = ", '0' AS id_sort, '0' id_sort_detail, v.jumlah jum_barang, v.masuk masuk_barang, v.keluar keluar_barang";
            $query['first'] = rekap_bon($concat_, $add_where.$where_1);

            $concat_multi = ", '0' AS id_sort, '0' id_sort_detail, v.jum_barang jum_barang, v.masuk masuk_barang, v.keluar keluar_barang";
            $query['first_multi'] = rekap_bonmulti($concat_multi, $add_where.$where_1);


            $concat = ", '0' AS id_sort, '1' id_sort_detail, SUM(v.jumlah) jum_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";
            $query['second'] = rekap_bon($concat, $add_where.$where_1." GROUP BY v.id_satuan ");

            $concat_multir = ", '0' AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) jum_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";
            $query['second_multir'] = rekap_bonmulti($concat_multir, $add_where.$where_1." GROUP BY v.id_satuan ");

            $group_query = implode(" UNION ALL ", $query);

            $last_query = "SELECT A.*, ms.satuan as inisial_satuan FROM ($group_query) A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY A.id_sort, A.id_sort_detail, A.tanggal_full";

            $rekap = DB::select($last_query);

            $data['detail'] = $rekap;
            $html = "LaporanBonBahanDetailCetak";
        }else if($jenis == 4){
            $where_1 .= " AND v.id_proses = '24'";
            $concat_ = ", CONCAT(".$concat_bbtb.") AS id_sort, '0' id_sort_detail, v.jumlah jum_barang, v.masuk masuk_barang, v.keluar keluar_barang";
            $query['first'] = rekap_bon($concat_, $add_where.$where_1);

            $concat_multi = ", CONCAT(".$concat_bbtb.") AS id_sort, '0' id_sort_detail, v.jum_barang jum_barang, v.masuk masuk_barang, v.keluar keluar_barang";
            $query['first_multi'] = rekap_bonmulti($concat_multi, $add_where.$where_1);

            $concat = ", CONCAT(".$concat_bbtb.") AS id_sort, '1' id_sort_detail, SUM(v.jumlah) jum_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";
            $query['second'] = rekap_bon($concat, $add_where.$where_1." GROUP BY ".$groupby_bbtb.", v.id_satuan ");

            $concat_multir = ", CONCAT(".$concat_bbtb.") AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) jum_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";
            $query['second_multir'] = rekap_bonmulti($concat_multir, $add_where.$where_1." GROUP BY ".$groupby_bbtb.", v.id_satuan ");
            $group_query = implode(" UNION ALL ", $query);

            $last_query = "SELECT A.*, ms.satuan as inisial_satuan FROM ($group_query) A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY A.id_sort, A.id_sort_detail ".$orderby_bbtb." ASC, A.tanggal ASC, A.no_workorder_barang ASC, A.nama_motif ASC, A.inisial_warna ASC";
            // print_r($last_query); exit();
            $rekap = DB::select($last_query);
            
            $data['detail'] = $rekap;
            $html = "LaporanBonBahanTenunSekirCetak";
        }else if($jenis == 5){
            // $where_1 .= " AND v.id_proses = '24'";
            $concat_ = ", CONCAT(".$concat_bbtb.", v.no_workorder_barang) AS id_sort, '0' id_sort_detail, v.jumlah jum_barang, v.masuk masuk_barang, v.keluar keluar_barang";
            $query['first'] = rekap_bon($concat_, $add_where.$where_1);

            $concat_multi = ", CONCAT(".$concat_bbtb.", v.no_workorder_barang) AS id_sort, '0' id_sort_detail, v.jum_barang jum_barang, v.masuk masuk_barang, v.keluar keluar_barang";
            $query['first_multi'] = rekap_bonmulti($concat_multi, $add_where.$where_1);

            $concat = ", CONCAT(".$concat_bbtb.", v.no_workorder_barang) AS id_sort, '1' id_sort_detail, SUM(v.jumlah) jum_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";
            $query['second'] = rekap_bon($concat, $add_where.$where_1." GROUP BY ".$groupby_bbtb.", v.id_satuan, v.no_workorder_barang ");

            $concat_multir = ", CONCAT(".$concat_bbtb.", v.no_workorder_barang) AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) jum_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";
            $query['second_multir'] = rekap_bonmulti($concat_multir, $add_where.$where_1." GROUP BY ".$groupby_bbtb.", v.id_satuan, v.no_workorder_barang ");
            $group_query = implode(" UNION ALL ", $query);

            $last_query = "SELECT A.*, ms.satuan as inisial_satuan FROM ($group_query) A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY A.id_sort, A.id_sort_detail ".$orderby_bbtb." ASC, A.tanggal ASC, A.no_workorder_barang ASC, A.nama_motif ASC, A.inisial_warna ASC";
            
            $rekap = DB::select($last_query);

            $data['detail'] = $rekap;
            $html = "LaporanBonBahanTenunSemuaCetak";
        }
        else{
            $data['detail'] = array();
            $html = "LaporanBonBahanDetailBarang";
        }

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

    }

    // --------------- Laporan Bon Bahan

    // --------------- Laporan Hasil

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

        $where = array();
        $where_non = array();
        $jenis = '';
        $data = array();
        $orderby_proses = "";

        $data['header'] = array("nama_pegawai" => '', 
                            "nama_kelompok" => '',
                            "nama_rekanan" => '', 
                            "nama_mesin" => "",
                            "nama_proses" => '', 
                            "nama_lokasi" => '', 
                            "id_proses" => '',
                            "tanggal_awal" => tgl_full($req['tgl_awal'], ''), 
                            "tanggal_akhir" => tgl_full($req['tgl_akhir'], ''),
                            "tanggal_range" => date_range(tgl_full($req['tgl_awal'], ''), tgl_full($req['tgl_akhir'], '')));
        
        $jenis = 0;
        if(isset($req['id_jenis_hasil'])){
            $jenis = $req['id_jenis_hasil'];
        }

        if(count($req) > 0){
            $add_where = "WHERE ";
        }       

        if(isset($req['id_pegawai_hasil'])){
            if($req['id_pegawai_hasil'] != 0){
                $where[] = "mp.id_pegawai = " .$req['id_pegawai_hasil']. "";
                $where_non[] = "mpeg.id_pegawai = " .$req['id_pegawai_hasil']. "";

                $data['header']['nama_pegawai'] = DB::table('ms_pegawai')->where('id_pegawai', $req['id_pegawai_hasil'])->first()->nama_pegawai;
            }else if($req['id_pegawai_hasil'] == '0'){
                $where[] = "mp.id_pegawai != '' ";
                $where_non[] = "mpeg.id_pegawai != '' ";

                $data['header']['nama_pegawai'] = "Semua Pegawai";
            }else{
                $data['header']['nama_pegawai'] = "";
            }
        }

        if(isset($req['id_makelar_hasil'])){
            if($req['id_makelar_hasil'] != 0){
                $d_petenun = DB::table('ms_rekanan as mr')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->where("mrk.id_kelompok", $req['id_makelar_hasil'])->select(DB::raw("mr.*, mrk.nama nama_kelompok"));
                $d_count = $d_petenun->get()->count();
                $d_pekerja = $d_petenun->pluck('id_rekanan')->toArray();

                $where[] = "mr.id_rekanan IN ('".implode("', '", $d_pekerja)."') ";
                $where_non[] = "mr.id_rekanan IN ('".implode("', '", $d_pekerja)."') ";
                $d_kelompok = $d_petenun->first()->nama_kelompok;
                $data['header']['nama_kelompok'] = $d_kelompok;
            }else if($req['id_rekanan_hasil'] == 0){
                $where[] = "mr.id_rekanan != '' ";
                $where_non[] = "mr.id_rekanan != '' ";
                $data['header']['nama_kelompok'] = "Semua Makelar";
            }else{

            }
        }else{

        }

        if(isset($req['id_rekanan_hasil'])){
            if($req['id_rekanan_hasil'] != 0){
                $where[] = "mr.id_rekanan = " .$req['id_rekanan_hasil']. "";
                $where_non[] = "mr.id_rekanan = " .$req['id_rekanan_hasil']. "";

                $d_data = DB::table('ms_rekanan as mr')->leftjoin('ms_rekanan_kelompok as mrk', 'mr.id_kelompok', 'mrk.id_kelompok')->where('id_rekanan', $req['id_rekanan_hasil'])->select(DB::raw("mr.*, mrk.nama nama_kelompok"))->first();
                $data['header']['nama_rekanan'] = $d_data->nama;//.' - '.$d_data->nama_kelompok;
            }else if($req['id_rekanan_hasil'] == '0'){
                $data['header']['nama_rekanan'] = "Semua Rekanan";
                $where[] = "mr.id_rekanan != '' ";
                $where_non[] = "mr.id_rekanan != '' ";

            }else{
                $data['header']['nama_rekanan'] = "";
            }  
        }

        if(isset($req['id_mesin_hasil'])){
            if($req['id_mesin_hasil'] != 0){
                $where[] = "mm.id_mesin = " .$req['id_mesin_hasil']. "";
                

                $d_data = DB::table('ms_mesin')->where('id_mesin', $req['id_mesin_hasil'])->first();
                $data['header']['nama_mesin'] = $d_data->nama;//.' - '.$d_data->nama_kelompok;
            }else if($req['id_mesin_hasil'] == '0'){
                $data['header']['nama_mesin'] = "Semua Mesin";
                $where[] = "mm.id_mesin != '' ";
                

            }else{
                $data['header']['nama_mesin'] = "";
            }  
        }

        if(isset($req['id_proses_hasil'])){
            if($req['id_proses_hasil'] != 0){
                $where[] = "v.id_proses = " .$req['id_proses_hasil']. "";
                $where_non[] = "mp.id_proses = " .$req['id_proses_hasil']. "";

                $data['header']['nama_proses'] = DB::table('ms_proses')->where('id_proses', $req['id_proses_hasil'])->first()->nama_proses;
                $data['header']['id_proses'] = $req['id_proses_hasil'];

                if(in_array($req['id_proses_hasil'], setup_benang())){
                    $orderby_proses = " ORDER BY A.nama_proses, A.nama_barang, A.nama_komposisi";
                }else if(in_array($req['id_proses_hasil'], setup_corak())){
                    $orderby_proses = " ORDER BY A.nama_corak, A.nama_motif, A.nama_komposisi";
                }else{
                    $orderby_proses = " ORDER BY A.nama_proses, A.nama_barang, A.nama_corak, A.nama_motif, A.nama_komposisi";
                }

            }else if($req['id_proses_hasil'] == '0'){
                $data['header']['nama_proses'] = "Semua Proses";
                $data['header']['id_proses'] = '0';
                $orderby_proses = " ORDER BY A.nama_proses, A.nama_barang, A.nama_corak, A.nama_motif, A.nama_komposisi";
            }else{
                $data['header']['nama_proses'] = "";
                $data['header']['id_proses'] = '';
                $orderby_proses = " ORDER BY A.nama_proses, A.nama_barang, A.nama_corak, A.nama_motif, A.nama_komposisi";
            }   
        }

        if(isset($req['id_lokasi_hasil'])){
            $where[] = "mlp.id_lokasi = " .$req['id_lokasi_hasil']. "";
            $where_non[] = "mlp.id_lokasi = " .$req['id_lokasi_hasil']. "";

            $data['header']['nama_lokasi'] = DB::table('ms_lokasi_produksi')->where('id_lokasi', $req['id_lokasi_hasil'])->first()->nama_lokasi;
        }   


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

            $where[] = "v.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";
            $where_non[] = "p.tanggal_hasil BETWEEN '$tgl_awal' AND '$tgl_akhir' ";
        }

        $where_1 = implode(" AND ", $where);
        $where_2 = implode(" AND ", $where_non);
        //rekap
        if($jenis == 0){
            $concat = ", CONCAT(".$this->concat_bon('v').") AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) total_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";

            $concat_ = ", CONCAT(".$this->concat_bon('v').") AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) total_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";

            $concat_non = ", '0' AS id_sort, '1' id_sort_detail, SUM(pu.jumlah) total_barang, SUM(pu.jumlah) masuk_barang, '0' keluar_barang";

            $query[] = rekap_hasil($concat, $add_where.$where_1." ".$this->groupby_bon('v', 'mwk')."");

            $query[] = rekap_hasilmulti($concat_, $add_where.$where_1." ".$this->groupby_bon('v', 'v')."");

            $query[] = rekap_tanpahasil($concat_non, $add_where.$where_2." AND p.jenis = '1' GROUP BY pu.id_upah, pu.id_upah, mp.id_proses, msat.id_satuan"); //, ms.id_satuan

            // print_r("SELECT A.*, ms.satuan as inisial_satuan FROM (".implode(' UNION ALL ', $query)." ) A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ".$orderby_proses);exit();

            $rekap = DB::select("SELECT A.*, ms.satuan as inisial_satuan FROM (".implode(' UNION ALL ', $query)." ) A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ".$orderby_proses);
            $data['detail'] = $rekap;

            //print_r(implode(' UNION ALL ', $query));

            $html = "LaporanhasilproduksirekapCetak";
            // print_r($data); exit();
        }else if($jenis == 1){

            $concat_ = ", CONCAT(CASE WHEN v.tanggal != '' THEN DATE_FORMAT(v.tanggal, '%d%m%Y') ELSE '0' END) AS id_sort, '0' id_sort_detail, v.jum_barang total_barang, v.masuk masuk_barang, v.keluar keluar_barang";
            $query['first'] = rekap_hasil($concat_, $add_where.$where_1);

            $concat_multi = ", CONCAT(CASE WHEN v.tanggal != '' THEN DATE_FORMAT(v.tanggal, '%d%m%Y') ELSE '0' END) AS id_sort, '0' id_sort_detail, v.jum_barang total_barang, v.masuk masuk_barang, v.keluar keluar_barang";
            $query['first_multi'] = rekap_hasilmulti($concat_multi, $add_where.$where_1);


            $concat = ", CONCAT(CASE WHEN v.tanggal != '' THEN DATE_FORMAT(v.tanggal, '%d%m%Y') ELSE '0' END) AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) total_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";
            $query['second'] = rekap_hasil($concat, $add_where.$where_1." GROUP BY v.tanggal, v.id_satuan ");

            $concat_multir = ", CONCAT(CASE WHEN v.tanggal != '' THEN DATE_FORMAT(v.tanggal, '%d%m%Y') ELSE '0' END) AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) total_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";
            $query['second_multir'] = rekap_hasilmulti($concat_multir, $add_where.$where_1." GROUP BY v.tanggal, v.id_satuan ");

            $concat_non = ", CONCAT(CASE WHEN p.tanggal_hasil != '' THEN DATE_FORMAT(p.tanggal_hasil, '%d%m%Y') ELSE '0' END) AS id_sort, '0' id_sort_detail, pu.jumlah total_barang, pu.jumlah masuk_barang, '0' keluar_barang";
            $query['third'] = rekap_tanpahasil($concat_non, $add_where.$where_2);

            $concat_nonmutli = ", CONCAT(CASE WHEN p.tanggal_hasil != '' THEN DATE_FORMAT(p.tanggal_hasil, '%d%m%Y') ELSE '0' END) AS id_sort, '1' id_sort_detail, SUM(pu.jumlah) total_barang, SUM(pu.jumlah) masuk_barang, '0' keluar_barang";
            $query['third_multi'] = rekap_tanpahasil($concat_nonmutli, $add_where.$where_2." GROUP BY p.tanggal_hasil, msat.id_satuan");

            $group_query = implode(" UNION ALL ", $query);

            $last_query = "SELECT A.*, ms.satuan as inisial_satuan FROM ($group_query) A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY A.id_sort, A.id_sort_detail, A.tanggal_full, A.nama_proses, A.nama_barang, A.nama_corak, A.nama_motif";
            
            $rekap = DB::select($last_query);

            $data['detail'] = $rekap;

            $html = "LaporanhasilproduksiCetak";
        }else if($jenis == 2){
            $concat_ = ", CONCAT(CASE WHEN mp.id_pegawai != '' THEN mp.id_pegawai ELSE '0' END, CASE WHEN mr.id_rekanan != '' THEN mr.id_rekanan ELSE '0' END, CASE WHEN mm.id_mesin != '' THEN mm.id_mesin ELSE '0' END) AS id_sort, '0' id_sort_detail, v.jum_barang total_barang, v.masuk masuk_barang, v.keluar keluar_barang";
            $query['first'] = rekap_hasil($concat_, $add_where.$where_1);

            $concat_multi = ", CONCAT(CASE WHEN mp.id_pegawai != '' THEN mp.id_pegawai ELSE '0' END, CASE WHEN mr.id_rekanan != '' THEN mr.id_rekanan ELSE '0' END, 0) AS id_sort, '0' id_sort_detail, v.jum_barang total_barang, v.masuk masuk_barang, v.keluar keluar_barang";
            $query['first_multi'] = rekap_hasilmulti($concat_multi, $add_where.$where_1);


            $concat = ", CONCAT(CASE WHEN mp.id_pegawai != '' THEN mp.id_pegawai ELSE '0' END, CASE WHEN mr.id_rekanan != '' THEN mr.id_rekanan ELSE '0' END, CASE WHEN mm.id_mesin != '' THEN mm.id_mesin ELSE '0' END) AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) total_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";
            $query['second'] = rekap_hasil($concat, $add_where.$where_1." GROUP BY v.id_satuan, mp.id_pegawai, mr.id_rekanan ");

            $concat_multir = ", CONCAT(CASE WHEN mp.id_pegawai != '' THEN mp.id_pegawai ELSE '0' END, CASE WHEN mr.id_rekanan != '' THEN mr.id_rekanan ELSE '0' END, 0) AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) total_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";
            $query['second_multir'] = rekap_hasilmulti($concat_multir, $add_where.$where_1." GROUP BY v.id_satuan, mp.id_pegawai, mr.id_rekanan ");


            $concat_non = ", CONCAT(CASE WHEN mpeg.id_pegawai != '' THEN mpeg.id_pegawai ELSE '0' END) AS id_sort, '0' id_sort_detail, pu.jumlah total_barang, pu.jumlah masuk_barang, '0' keluar_barang";
            $query['third'] = rekap_tanpahasil($concat_non, $add_where.$where_2);

            $concat_nonmutli = ", CONCAT(CASE WHEN mpeg.id_pegawai != '' THEN mpeg.id_pegawai ELSE '0' END) AS id_sort, '1' id_sort_detail, SUM(pu.jumlah) total_barang, SUM(pu.jumlah) masuk_barang, '0' keluar_barang";
            $query['third_multi'] = rekap_tanpahasil($concat_nonmutli, $add_where.$where_2." GROUP BY msat.id_satuan, mpeg.id_pegawai");


            $group_query = implode(" UNION ALL ", $query);

            $last_query = "SELECT A.*, ms.satuan as inisial_satuan FROM ($group_query) A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY A.id_sort, A.id_sort_detail, A.tanggal_full, A.nama_proses, A.nama_barang, A.nama_corak, A.nama_motif";

            $rekap = DB::select($last_query);

            $data['detail'] = $rekap;

            $html = "LaporanhasilproduksipekerjaCetak";
        }else if($jenis == 3){
             $concat_ = ", CONCAT(CASE WHEN mp.id_pegawai != '' THEN mp.id_pegawai ELSE '0' END, CASE WHEN mr.id_rekanan != '' THEN mr.id_rekanan ELSE '0' END, CASE WHEN mm.id_mesin != '' THEN mm.id_mesin ELSE '0' END) AS id_sort, '0' id_sort_detail, v.jum_barang total_barang, v.masuk masuk_barang, v.keluar keluar_barang";
            $query['first'] = rekap_hasil($concat_, $add_where.$where_1);

            $concat_multi = ", CONCAT(CASE WHEN mp.id_pegawai != '' THEN mp.id_pegawai ELSE '0' END, CASE WHEN mr.id_rekanan != '' THEN mr.id_rekanan ELSE '0' END, 0) AS id_sort, '0' id_sort_detail, v.jum_barang total_barang, v.masuk masuk_barang, v.keluar keluar_barang";
            $query['first_multi'] = rekap_hasilmulti($concat_multi, $add_where.$where_1);

            $concat_non = ", '0' AS id_sort, '0' id_sort_detail, pu.jumlah total_barang, pu.jumlah masuk_barang, '0' keluar_barang";
            $query[] = rekap_tanpahasil($concat_non, $add_where.$where_2." AND p.jenis = '1'");

            $group_query = implode(" UNION ALL ", $query);

            $last_query = "SELECT A.*, ms.satuan as inisial_satuan FROM ($group_query) A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY A.tanggal_full, A.id_proses, CONCAT(A.noAuto, A.noAuto_ext), A.nama_barang, A.inisial_warna";

            $rekap = DB::select($last_query);

            $data['detail'] = $rekap;
            // print_r($data); exit();
            $html = "LaporanhasilproduksiDetailCetak";
        }else if($jenis == 4){

            /*
            "CASE WHEN ".$huruf.".id_barang != '' THEN ".$huruf.".id_barang ELSE '0' END, CASE WHEN ".$huruf.".id_proses != '' THEN ".$huruf.".id_proses ELSE '0' END, CASE WHEN ".$huruf.".id_komposisi != '' THEN ".$huruf.".id_komposisi ELSE '0' END, CASE WHEN ".$huruf.".id_satuan != '' THEN ".$huruf.".id_satuan ELSE '0' END, CASE WHEN ".$huruf.".id_corak != '' THEN ".$huruf.".id_corak ELSE '0' END, CASE WHEN ".$huruf.".id_motif != '' THEN ".$huruf.".id_motif ELSE '0' END"


            " GROUP BY ".$huruf.".id_barang, ".$huruf.".id_proses, ".$huruf2.".id_komposisi, ".$huruf.".id_satuan, ".$huruf.".id_corak, ".$huruf.".id_motif"
            */
            $concat_4 = '';
            $groupby_bon = "";
            $huruf = 'v';
            $order_last = '';
            if(isset($req['id_proses_hasil'])){
                $huruf = 'v';
                if(in_array($req['id_proses_hasil'], setup_benang())){
                    $concat_4 = $this->concat_bon('v');
                    $groupby_bon = $this->groupby_bon('v', 'v');
                    $order_last = " A.nama_barang";
                }else if(in_array($req['id_proses_hasil'], setup_corak())){
                    $concat_4 = str_replace("CASE WHEN ".$huruf.".id_komposisi != '' THEN ".$huruf.".id_komposisi ELSE '0' END,", "", $this->concat_bon('v'));
                    $groupby_bon = str_replace(" ".$huruf.".id_barang,", "", $this->groupby_bon($huruf, $huruf));
                    $groupby_bon = str_replace(" ".$huruf.".id_komposisi,", "", $this->groupby_bon($huruf, $huruf));
                    $order_last = " A.nama_corak";
                }else{
                    $concat_4 = $this->concat_bon('v');
                    $groupby_bon = $this->groupby_bon('v', 'v');
                    $order_last = " A.nama_motif";
                }
            }else{
                $concat_4 = $this->concat_bon('v');
                $groupby_bon = $this->groupby_bon('v', 'v');
                $order_last = " A.nama_barang, A.nama_corak, A.nama_motif";
            }

             $concat_ = ", CONCAT(".$concat_4.") AS id_sort, '0' id_sort_detail, v.jum_barang total_barang, v.masuk masuk_barang, v.keluar keluar_barang";
            $query['first'] = rekap_hasil($concat_, $add_where.$where_1);

            $concat_multi = ", CONCAT(".$concat_4.") AS id_sort, '0' id_sort_detail, v.jum_barang total_barang, v.masuk masuk_barang, v.keluar keluar_barang";
            $query['first_multi'] = rekap_hasilmulti($concat_multi, $add_where.$where_1);


            $concat = ", CONCAT(".$concat_4.") AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) total_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";
            $query['second'] = rekap_hasil($concat, $add_where.$where_1." ".$groupby_bon);

            $concat_multir = ", CONCAT(".$concat_4.") AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) total_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang";
            $query['second_multir'] = rekap_hasilmulti($concat_multir, $add_where.$where_1." ".$groupby_bon);

            $concat_ = "pu.id_upah, mp.id_proses, msat.id_satuan";
            $group_non = " GROUP BY pu.id_upah, mp.id_proses, msat.id_satuan";

            $concat_non = ", CONCAT(".$concat_.") AS id_sort, '0' id_sort_detail, pu.jumlah total_barang, pu.jumlah masuk_barang, '0' keluar_barang";
            $query['third'] = rekap_tanpahasil($concat_non, $add_where.$where_2);

            $concat_nonmutli = ", CONCAT(".$concat_.") AS id_sort, '1' id_sort_detail, SUM(pu.jumlah) total_barang, SUM(pu.jumlah) masuk_barang, '0' keluar_barang";
            $query['third_multi'] = rekap_tanpahasil($concat_nonmutli, $add_where.$where_2." ".$group_non);

            $group_query = implode(" UNION ALL ", $query);

            $last_query = "SELECT A.*, ms.satuan as inisial_satuan FROM ($group_query) A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY ".$order_last.", A.id_sort, A.id_sort_detail, A.tanggal_full, A.nama_proses ";

            $rekap = DB::select($last_query);

            $data['detail'] = $rekap;

        //    print_r($last_query);

            $html = "LaporanhasilproduksiPerJenisCetak";
        }
        else{

        }


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

    // --------------- Laporan Hasil


    // -------------- Laporan Hasil Tenun

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

        $where = array();
        $where2 = array();
        $id_rekanan = '';
        $rekanan = '';
        $id_makelar = '';
        $makelar = '';
        $lokasi = '';
        $proses = '';
        $count_kualitas = 0;

        $jenis = 0;
        if(isset($req['id_jenis'])){
            $jenis = $req['id_jenis'];
        }

        if(count($req) > 0){
            $add_where = "WHERE ";
        }

        if(isset($req['id_makelar'])){
            if($req['id_makelar'] != 0){
                $where[] = "mrk.id_kelompok = " .$req['id_makelar']. "";
                $id_makelar = $req['id_makelar'];
                $makelar = DB::table('ms_rekanan_kelompok')->where('id_kelompok', $req['id_makelar'])->first()->nama;
            }else if($req['id_makelar'] == '0'){
                $makelar = "Semua Makelar";
                $where[] = "mrk.id_kelompok != '' ";
            }else{
                $makelar = "";
            }
        }

        if(isset($req['id_rekanan'])){
            if($req['id_rekanan'] != 0){
                $where[] = "mr.id_rekanan = " .$req['id_rekanan']. "";
                $id_rekanan = $req['id_rekanan'];
                $rekanan = DB::table('ms_rekanan')->where('id_rekanan', $req['id_rekanan'])->first()->nama;
            }else if($req['id_rekanan'] == '0'){
                $rekanan = "Semua Rekanan";
                $where[] = "mr.id_rekanan != '' ";
            }else{
                $rekanan = "Semua Petenun";
            }
        }

        if(isset($req['id_motif'])){
            if($req['id_motif'] != ''){

                $where[] = "v.id_motif = ".$req['id_motif']." ";
                $where2[] = "v.id_motif = ".$req['id_motif']." ";

                $motif  = DB::table('ms_motif')->where('id_motif', $req['id_motif'])->first()->nama_motif;
            }else{
                $where[] = "v.id_motif != '' ";
                $where2[] = "v.id_motif != '' ";    
            }
        }else{
            $where[] = "v.id_motif != '' ";
            $where2[] = "v.id_motif != '' ";            
        }

        if(isset($req['id_kualitas'])){
            if($req['id_kualitas'] != ''){

                $where[] = "v.id_kualitas = ".$req['id_kualitas']." ";
                $where2[] = "v.id_kualitas = ".$req['id_kualitas']." ";
            }
        }

        if(isset($req['id_lokasi'])){
            $where[] = "mlp.id_lokasi = ".$req['id_lokasi']." ";
            
            
            $lokasi = DB::table('ms_lokasi_produksi')->where('id_lokasi', $req['id_lokasi'])->first()->nama_lokasi;
        }

        if(isset($req['tgl_awal']) && isset($req['tgl_akhir'])){
            $tgl_awal = tgl_full($req['tgl_awal'], '2');
            $tgl_akhir = tgl_full($req['tgl_akhir'], '2');
            $where[] = "v.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";

            $tanggal = date_range(tgl_full($req['tgl_awal'], ''), tgl_full($req['tgl_akhir'], ''));
        }

        $where[] = "v.id_proses = '37'";
        $where2[] = "v.id_proses = '37'";
        $id_proses = '37';
        $proses = DB::table('ms_proses')->where('id_proses', $id_proses)->first()->nama_proses;

        $where_1 = implode(" AND ", $where);

        $data['header'] = array('nama_lokasi' => $lokasi, 'tanggal_range' => $tanggal, 'id_proses' => $id_proses, 'nama_proses' => $proses, 'id_makelar' => $id_makelar, 'nama_makelar' => $makelar, 'id_rekanan' => $id_rekanan, 'nama_rekanan' => $rekanan, 'count_kualitas' => $count_kualitas, "nama_kualitas" => array(), "id_kualitas" => array());

        //Per makelar
        if($jenis == 0){
            $concat = ", CONCAT(CASE WHEN v.tanggal != '' THEN DATE_FORMAT(v.tanggal, '%d%m%Y') ELSE '0' END) AS id_sort, '0' id_sort_detail, SUM(v.jum_barang) total_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang, pbb.panjang, pbb.lebar, pbb.pick, CASE WHEN mrk.id_kelompok IS NULL THEN '' ELSE mrk.id_kelompok END AS id_kelompok, CASE WHEN mrk.nama IS NULL THEN '' ELSE mrk.nama END AS nama_kelompok";

            $concatmulti = ", CONCAT(CASE WHEN v.tanggal != '' THEN DATE_FORMAT(v.tanggal, '%d%m%Y') ELSE '0' END) AS id_sort, '0' id_sort_detail, SUM(v.jum_barang) total_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang, '0' panjang, '0' lebar, '0' pick, CASE WHEN mrk.id_kelompok IS NULL THEN '' ELSE mrk.id_kelompok END AS id_kelompok, CASE WHEN mrk.nama IS NULL THEN '' ELSE mrk.nama END AS nama_kelompok";

            $concat_ = ", CONCAT(CASE WHEN v.tanggal != '' THEN DATE_FORMAT(v.tanggal, '%d%m%Y') ELSE '0' END) AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) total_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang, pbb.panjang, pbb.lebar, pbb.pick, CASE WHEN mrk.id_kelompok IS NULL THEN '' ELSE mrk.id_kelompok END AS id_kelompok, CASE WHEN mrk.nama IS NULL THEN '' ELSE mrk.nama END AS nama_kelompok";

            $concatmulti_ = ", CONCAT(CASE WHEN v.tanggal != '' THEN DATE_FORMAT(v.tanggal, '%d%m%Y') ELSE '0' END) AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) total_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang, '0' panjang, '0' lebar, '0' pick, CASE WHEN mrk.id_kelompok IS NULL THEN '' ELSE mrk.id_kelompok END AS id_kelompok, CASE WHEN mrk.nama IS NULL THEN '' ELSE mrk.nama END AS nama_kelompok";

            $query[] = rekap_hasil($concat, $add_where.$where_1." GROUP BY v.id_barang, v.id_satuan, v.id_motif, mrk.id_kelompok, mr.id_rekanan, v.tanggal");
            $query[] = rekap_hasilmulti($concatmulti, $add_where.$where_1." GROUP BY v.id_barang, v.id_satuan, v.id_motif, mrk.id_kelompok, v.tanggal");

            $query[] = rekap_hasil($concat_, $add_where.$where_1." GROUP BY v.id_satuan, v.tanggal");
            $query[] = rekap_hasilmulti($concatmulti_, $add_where.$where_1." GROUP BY v.id_satuan, v.tanggal");
            
            // print_r("SELECT A.*, ms.satuan as inisial_satuan FROM (".implode(' UNION ALL ', $query)." ) A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY A.tanggal_full, A.nama_kelompok, A.nama_rekanan"); exit();

            $rekap = DB::select("SELECT A.*, ms.satuan as inisial_satuan FROM (".implode(' UNION ALL ', $query)." ) A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY A.id_sort, A.id_sort_detail, A.nama_kelompok, A.nama_rekanan");
            $data['detail'] = $rekap;

            $html = "LaporanhasiltenunMakelarTanggalCetak";

            //Per nomer produksi
        }else if($jenis == 1){
            $concat = ", CONCAT(CASE WHEN pbk.noAuto != '' THEN pbk.noAuto ELSE '0' END, CASE WHEN pbk.noAuto_ext != '' THEN pbk.noAuto_ext ELSE '0' END) AS id_sort, '0' id_sort_detail, v.jum_barang total_barang, v.masuk masuk_barang, v.keluar keluar_barang, pbb.panjang, pbb.lebar, pbb.pick, CASE WHEN mrk.id_kelompok IS NULL THEN '' ELSE mrk.id_kelompok END AS id_kelompok, CASE WHEN mrk.nama IS NULL THEN '' ELSE mrk.nama END AS nama_kelompok, CASE WHEN pbk.noAuto != '' THEN pbk.noAuto ELSE '0' END AS noAuto_bb, CASE WHEN pbk.noAuto_ext != '' THEN pbk.noAuto_ext ELSE '0' END AS noAuto_ext_bb";

            $concatmulti = ", CONCAT(CASE WHEN pbk.noAuto != '' THEN pbk.noAuto ELSE '0' END, CASE WHEN pbk.noAuto_ext != '' THEN pbk.noAuto_ext ELSE '0' END) AS id_sort, '0' id_sort_detail, v.jum_barang total_barang, v.masuk masuk_barang, v.keluar keluar_barang, '0' panjang, '0' lebar, '0' pick, CASE WHEN mrk.id_kelompok IS NULL THEN '' ELSE mrk.id_kelompok END AS id_kelompok, CASE WHEN mrk.nama IS NULL THEN '' ELSE mrk.nama END AS nama_kelompok, CASE WHEN pbk.noAuto != '' THEN pbk.noAuto ELSE '0' END AS noAuto_bb, CASE WHEN pbk.noAuto_ext != '' THEN pbk.noAuto_ext ELSE '0' END AS noAuto_ext_bb";

            $concat_ = ", CONCAT(CASE WHEN pbk.noAuto != '' THEN pbk.noAuto ELSE '0' END, CASE WHEN pbk.noAuto_ext != '' THEN pbk.noAuto_ext ELSE '0' END) AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) total_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang, pbb.panjang, pbb.lebar, pbb.pick, CASE WHEN mrk.id_kelompok IS NULL THEN '' ELSE mrk.id_kelompok END AS id_kelompok, CASE WHEN mrk.nama IS NULL THEN '' ELSE mrk.nama END AS nama_kelompok, CASE WHEN pbk.noAuto != '' THEN pbk.noAuto ELSE '0' END AS noAuto_bb, CASE WHEN pbk.noAuto_ext != '' THEN pbk.noAuto_ext ELSE '0' END AS noAuto_ext_bb";

            $concatmulti_ = ", CONCAT(CASE WHEN pbk.noAuto != '' THEN pbk.noAuto ELSE '0' END, CASE WHEN pbk.noAuto_ext != '' THEN pbk.noAuto_ext ELSE '0' END) AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) total_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang, '0' panjang, '0' lebar, '0' pick, CASE WHEN mrk.id_kelompok IS NULL THEN '' ELSE mrk.id_kelompok END AS id_kelompok, CASE WHEN mrk.nama IS NULL THEN '' ELSE mrk.nama END AS nama_kelompok, CASE WHEN pbk.noAuto != '' THEN pbk.noAuto ELSE '0' END AS noAuto_bb, CASE WHEN pbk.noAuto_ext != '' THEN pbk.noAuto_ext ELSE '0' END AS noAuto_ext_bb";

            $query[] = rekap_hasil($concat, $add_where.$where_1);
            $query[] = rekap_hasilmulti($concatmulti, $add_where.$where_1);

            $query[] = rekap_hasil($concat_, $add_where.$where_1." GROUP BY v.id_satuan, pbk.noAuto, pbk.noAuto_ext");
            $query[] = rekap_hasilmulti($concatmulti_, $add_where.$where_1." GROUP BY v.id_satuan, pbk.noAuto, pbk.noAuto_ext");
            

            $rekap = DB::select("SELECT A.*, ms.satuan as inisial_satuan FROM (".implode(' UNION ALL ', $query)." ) A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY A.id_sort, A.id_sort_detail, A.tanggal_full");
            $data['detail'] = $rekap;



            $html = "LaporanhasiltenunPerNomorCetak";
        }else if($jenis == 2){
            $d_kualitas = DB::table('ms_kualitas')->where('jenis', '1')->orderby('orderby');
            $arr_select = array();
            $select = '';
            $data['header']['count_kualitas'] = $d_kualitas->count();
            $data['header']['nama_kualitas'] = $d_kualitas->pluck('nama_kualitas');
            $data['header']['id_kualitas'] = $d_kualitas->pluck('id_kualitas');
            if($d_kualitas->count() > 0){
                foreach($d_kualitas->get() as $d){
                //    $arr_select[] = "SUM(CASE WHEN A.id_kualitas = '".$d->id_kualitas."' THEN A.total_barang END) tenun_".$d->id_kualitas."";
                    $arr_select[] = "CASE WHEN SUM(CASE WHEN A.id_kualitas = '".$d->id_kualitas."' THEN A.total_barang END) IS NULL THEN 0 ELSE SUM(CASE WHEN A.id_kualitas = '".$d->id_kualitas."' THEN A.total_barang END) END AS tenun_".$d->id_kualitas." ";
                }
            }

            $select = implode(', ', $arr_select);

            $concat = ", v.jum_barang total_barang, v.masuk masuk_barang, v.keluar keluar_barang, pbb.panjang, pbb.lebar, pbb.pick, CASE WHEN mrk.id_kelompok IS NULL THEN '' ELSE mrk.id_kelompok END AS id_kelompok, CASE WHEN mrk.nama IS NULL THEN '' ELSE mrk.nama END AS nama_kelompok, CASE WHEN pbk.noAuto != '' THEN pbk.noAuto ELSE '0' END AS noAuto_bb, CASE WHEN pbk.noAuto_ext != '' THEN pbk.noAuto_ext ELSE '0' END AS noAuto_ext_bb, '0' id_sort";

            $concatmulti = ", v.jum_barang total_barang, v.masuk masuk_barang, v.keluar keluar_barang, '0' panjang, '0' lebar, '0' pick, CASE WHEN mrk.id_kelompok IS NULL THEN '' ELSE mrk.id_kelompok END AS id_kelompok, CASE WHEN mrk.nama IS NULL THEN '' ELSE mrk.nama END AS nama_kelompok, CASE WHEN pbk.noAuto != '' THEN pbk.noAuto ELSE '0' END AS noAuto_bb, CASE WHEN pbk.noAuto_ext != '' THEN pbk.noAuto_ext ELSE '0' END AS noAuto_ext_bb, '0' id_sort";

            $concat_hasil = ", v.jum_barang total_barang, v.masuk masuk_barang, v.keluar keluar_barang, pbb.panjang, pbb.lebar, pbb.pick, CASE WHEN mrk.id_kelompok IS NULL THEN '' ELSE mrk.id_kelompok END AS id_kelompok, CASE WHEN mrk.nama IS NULL THEN '' ELSE mrk.nama END AS nama_kelompok, CASE WHEN pbk.noAuto != '' THEN pbk.noAuto ELSE '0' END AS noAuto_bb, CASE WHEN pbk.noAuto_ext != '' THEN pbk.noAuto_ext ELSE '0' END AS noAuto_ext_bb, '1' id_sort";

            $concatmulti_hasil = ", v.jum_barang total_barang, v.masuk masuk_barang, v.keluar keluar_barang, '0' panjang, '0' lebar, '0' pick, CASE WHEN mrk.id_kelompok IS NULL THEN '' ELSE mrk.id_kelompok END AS id_kelompok, CASE WHEN mrk.nama IS NULL THEN '' ELSE mrk.nama END AS nama_kelompok, CASE WHEN pbk.noAuto != '' THEN pbk.noAuto ELSE '0' END AS noAuto_bb, CASE WHEN pbk.noAuto_ext != '' THEN pbk.noAuto_ext ELSE '0' END AS noAuto_ext_bb, '1' id_sort";

            $query[] = rekap_hasil($concat, $add_where.$where_1);
            $query[] = rekap_hasilmulti($concatmulti, $add_where.$where_1);

            $query_hasil[] = rekap_hasil($concat_hasil, $add_where.$where_1);
            $query_hasil[] = rekap_hasilmulti($concatmulti_hasil, $add_where.$where_1);

            $select_union[] = "SELECT A.*, ".$select.", SUM(A.total_barang) row_total FROM (".implode(' UNION ALL ', $query)." ) A GROUP BY A.id_barang, A.id_satuan ";

           $select_union[] = "SELECT A.*, ".$select.", SUM(A.total_barang) row_total FROM (".implode(' UNION ALL ', $query_hasil)." ) A GROUP BY A.id_satuan";            

            $rekap = DB::select("SELECT B.*, ms.satuan as inisial_satuan FROM (".implode(' UNION ALL ', $select_union)." ) B LEFT JOIN ms_satuan as ms ON ms.id_satuan = B.id_satuan ORDER BY B.id_sort, B.nama_barang");                        

            //print_r($rekap);

            $data['detail'] = $rekap;
            $html = "LaporanhasiltenunPerKualitasCetak";
        }else if($jenis == '3'){
            $concat = ", CONCAT(CASE WHEN v.id_barang != '' THEN v.id_barang ELSE '0' END) AS id_sort, '0' id_sort_detail, v.jum_barang total_barang, v.masuk masuk_barang, v.keluar keluar_barang, pbb.panjang, pbb.lebar, pbb.pick, CASE WHEN mrk.id_kelompok IS NULL THEN '' ELSE mrk.id_kelompok END AS id_kelompok, CASE WHEN mrk.nama IS NULL THEN '' ELSE mrk.nama END AS nama_kelompok";

            $concatmulti = ", CONCAT(CASE WHEN v.id_barang != '' THEN v.id_barang ELSE '0' END) AS id_sort, '0' id_sort_detail, v.jum_barang total_barang, v.masuk masuk_barang, v.keluar keluar_barang, '0' panjang, '0' lebar, '0' pick, CASE WHEN mrk.id_kelompok IS NULL THEN '' ELSE mrk.id_kelompok END AS id_kelompok, CASE WHEN mrk.nama IS NULL THEN '' ELSE mrk.nama END AS nama_kelompok";

            $concat_ = ", CONCAT(CASE WHEN v.id_barang != '' THEN v.id_barang ELSE '0' END) AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) total_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang, pbb.panjang, pbb.lebar, pbb.pick, CASE WHEN mrk.id_kelompok IS NULL THEN '' ELSE mrk.id_kelompok END AS id_kelompok, CASE WHEN mrk.nama IS NULL THEN '' ELSE mrk.nama END AS nama_kelompok";

            $concatmulti_ = ", CONCAT(CASE WHEN v.id_barang != '' THEN v.id_barang ELSE '0' END) AS id_sort, '1' id_sort_detail, SUM(v.jum_barang) total_barang, SUM(v.masuk) masuk_barang, SUM(v.keluar) keluar_barang, '0' panjang, '0' lebar, '0' pick, CASE WHEN mrk.id_kelompok IS NULL THEN '' ELSE mrk.id_kelompok END AS id_kelompok, CASE WHEN mrk.nama IS NULL THEN '' ELSE mrk.nama END AS nama_kelompok";

            $query[] = rekap_hasil($concat, $add_where.$where_1."");
            $query[] = rekap_hasilmulti($concatmulti, $add_where.$where_1."");

            $query[] = rekap_hasil($concat_, $add_where.$where_1." GROUP BY v.id_satuan, v.id_barang");
            $query[] = rekap_hasilmulti($concatmulti_, $add_where.$where_1." GROUP BY v.id_satuan, v.id_barang");
            

            $rekap = DB::select("SELECT A.*, ms.satuan as inisial_satuan FROM (".implode(' UNION ALL ', $query)." ) A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY A.id_sort, A.id_sort_detail, A.tanggal_full, A.nama_barang");
            $data['detail'] = $rekap;

        //    print_r("SELECT A.* FROM (".implode(' UNION ALL ', $query)." ) A ORDER BY A.tanggal_full, A.nama_barang");

            $html = "LaporanhasiltenunPerJenisCetak";
        }
        else{
            $data['detail'] = array();
            $html = "LaporanhasiltenunMakelarTanggalCetak";
        }

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

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

        $where = array();
        $where_detail = array();
        $where_habisbom = array();
        $id_rekanan = '';
        $rekanan = '';
        $id_makelar = '';
        $makelar = '';
        $lokasi = '';
        $proses = '';
        $nama_gudang = '';
        $lokasi_gudang = '';

        $jenis = 0;
        if(isset($req['id_jenis'])){
            $jenis = $req['id_jenis'];
        }

        if(count($req) > 0){
            $add_where = "WHERE ";
        }

        if(isset($req['id_makelar'])){
            if($req['id_makelar'] != 0){
                $where[] = "mrk.id_kelompok = " .$req['id_makelar']. "";
                $where_habisbom[] = "mrk.id_kelompok = " .$req['id_makelar']. "";
                $id_makelar = $req['id_makelar'];
                $makelar = DB::table('ms_rekanan_kelompok')->where('id_kelompok', $req['id_makelar'])->first()->nama;
            }else if($req['id_makelar'] == '0'){
                $makelar = "Semua Makelar";
                $where[] = "mrk.id_kelompok != '' ";
                $where_habisbom[] = "mrk.id_kelompok != '' ";
            }else{
                $makelar = "";
            }
        }

        if(isset($req['id_rekanan'])){
            if($req['id_rekanan'] != 0){
                $where[] = "mr.id_rekanan = " .$req['id_rekanan']. "";
                $where_habisbom[] = "mr.id_rekanan = " .$req['id_rekanan']. "";
                $id_rekanan = $req['id_rekanan'];
                $rekanan = DB::table('ms_rekanan')->where('id_rekanan', $req['id_rekanan'])->first()->nama;
            }else if($req['id_rekanan'] == '0'){
                $rekanan = "Semua Rekanan";
                $where[] = "mr.id_rekanan != '' ";
                $where_habisbom[] = "mr.id_rekanan != '' ";
            }else{
                $rekanan = "Semua Petenun";
            }
        }

        if(isset($req['id_motif'])){
            if($req['id_motif'] != ''){

                $where[] = "v.id_motif = ".$req['id_motif']." ";
                
                $where_detail[] = "A.id_motif = ".$req['id_motif']." ";
                $where_habisbom[] = "v.id_motif = ".$req['id_motif']." ";

                $motif  = DB::table('ms_motif')->where('id_motif', $req['id_motif'])->first()->nama_motif;
            }else{
                $where[] = "v.id_motif != '' ";
                $where_detail[] = "A.id_motif != '' ";
                $where_habisbom[] = "v.id_motif != '' ";
            }
        }else{
            $where[] = "v.id_motif != '' ";
            $where_detail[] = "A.id_motif != '' ";
            $where_habisbom[] = "v.id_motif != '' ";
        }

        

        if(isset($req['id_lokasi'])){
            $where[] = "mlp.id_lokasi = ".$req['id_lokasi']." ";
            
            $where_detail[] = "A.id_lokasi = ".$req['id_lokasi']." ";
            $where_habisbom[] = "mlp.id_lokasi = ".$req['id_lokasi']." ";
            
            $lokasi = DB::table('ms_lokasi_produksi')->where('id_lokasi', $req['id_lokasi'])->first()->nama_lokasi;
        }

        if(isset($req['id_gudang'])){
            if($req['id_gudang'] != ''){
                $where[] = "A.id_gudang = ".$req['id_gudang']." ";

                $where_detail[] = "A.id_gudang = ".$req['id_gudang']." ";

                $d_gudang = DB::table('ms_gudang as mg')->join('ms_lokasi_produksi as mlp', 'mg.id_lokasi', 'mlp.id_lokasi')->where('mg.id_gudang', $req['id_gudang'])->select(DB::raw("mg.*, mlp.nama_lokasi"))->first();
                $nama_gudang = $d_gudang->nama_gudang;
                $lokasi_gudang = $d_gudang->nama_lokasi;
            }
        }

        if(isset($req['tgl_awal']) && isset($req['tgl_akhir'])){
            $tgl_awal = tgl_full($req['tgl_awal'], '2');
            $tgl_akhir = tgl_full($req['tgl_akhir'], '2');
            $where[] = "v.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";
            $where_detail[] = "A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";
            $where_habisbom[] = "v.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";

            $tanggal = date_range(tgl_full($req['tgl_awal'], ''), tgl_full($req['tgl_akhir'], ''));
        }

        $where[] = "v.id_proses = '24'";
        $where_detail[] = "A.id_proses = '24'";
        $where_habisbom[] = "v.id_proses = '24'";
        $id_proses = '24';
        $proses = DB::table('ms_proses')->where('id_proses', $id_proses)->first()->nama_proses;

        $where_1 = implode(" AND ", $where);

        $data['header'] = array('nama_lokasi' => $lokasi, 'tanggal_range' => $tanggal, 'id_proses' => $id_proses, 'nama_proses' => $proses, 'id_makelar' => $id_makelar, 'nama_makelar' => $makelar, 'id_rekanan' => $id_rekanan, 'nama_rekanan' => $rekanan, 'nama_gudang' => $nama_gudang, 'lokasi_gudang' => $lokasi_gudang);

        if($jenis == 0){

            $data['where'] = implode(" AND ", $where_detail);
            $group_detail = " GROUP BY A.id_barang, A.id_komposisi, A.id_satuan, A.id_motif, A.id_perubahansatuan, A.jumlah_rencana, A.id_workorder_barang";
            $group_sum = " GROUP BY A.id_barang, A.id_satuan, A.id_perubahansatuan";

            $field_sum_detail = ", CASE WHEN sum(masuk - keluar) % 1 = 0 THEN sum(masuk - keluar) ELSE ROUND(sum(masuk - keluar), 2) END AS stok, '0' id_sort";
            $field_sum = ", CASE WHEN sum(masuk - keluar) % 1 = 0 THEN sum(masuk - keluar) ELSE ROUND(sum(masuk - keluar), 2) END AS stok, '1' id_sort";
            
            $where_query = $add_where.$data['where'].$group_detail;
            $where_sum = $add_where.$data['where'].$group_sum;
            //$where, group by, select
            $query_detail = get_mutasirekap2($where_query, '', $field_sum_detail);
            $query_sum = get_mutasirekap2($where_sum, '', $field_sum);

            $query = DB::select("SELECT A.*, ms.satuan as inisial_satuan, ms2.satuan as inisial_perubahan FROM (".$query_detail.") A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan LEFT JOIN ms_satuan as ms2 ON ms2.id_satuan = A.id_perubahansatuan WHERE A.stok > 0 ORDER BY A.tanggal, A.no_workorder_barang"); //A.id_sort, A.id_barang
            $data['detail'] = $query;            

        //    print_r("SELECT A.*, ms.satuan as inisial_satuan, ms2.satuan as inisial_perubahan FROM (".$query_detail.") A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan LEFT JOIN ms_satuan as ms2 ON ms2.id_satuan = A.id_perubahansatuan WHERE A.stok > 0 ORDER BY A.id_sort, A.id_barang");

            $html = "LaporanbomCetak";
        }else if ($jenis == 1){
            $data['where'] = implode(" AND ", $where_detail);
            $group_detail = " GROUP BY A.id_barang, A.id_komposisi, A.id_satuan, A.id_motif";
            $group_sum = " GROUP BY A.id_barang, A.id_satuan, A.id_perubahansatuan";

            $field_sum_detail = ", CASE WHEN sum(masuk - keluar) % 1 = 0 THEN sum(masuk - keluar) ELSE ROUND(sum(masuk - keluar), 2) END AS stok, '0' id_sort";
            $field_sum = ", CASE WHEN sum(masuk - keluar) % 1 = 0 THEN sum(masuk - keluar) ELSE ROUND(sum(masuk - keluar), 2) END AS stok, '1' id_sort";
            
            $where_query = $add_where.$data['where'].$group_detail;
            $where_sum = $add_where.$data['where'].$group_sum;
            //$where, group by, select
            $query_detail = get_mutasirekap2($where_query, '', $field_sum_detail);
            $query_sum = get_mutasirekap2($where_sum, '', $field_sum);

            $query = DB::select("SELECT A.*, ms.satuan as inisial_satuan FROM (".$query_detail.") A LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan WHERE A.stok > 0 ORDER BY A.nama_motif, A.inisial_warna"); //A.id_sort, A.id_barang
            $data['detail'] = $query;            
        //    print_r("SELECT A.* FROM (".$query_detail.") A WHERE A.stok > 0 ORDER BY A.id_sort, A.id_barang");
            $html = "LaporanbomrekapCetak";
        }
        else if($jenis == 2){
            $where = implode(" AND ", $where_habisbom);
            $data['detail'] = DB::select(get_habisbom(', pb.*, v.*', $add_where.$where." AND pb.status_beem = '1' GROUP BY pb.id_workorder_barang ORDER BY v.tanggal_full, v.no_workorder_barang"));

            $html = "LaporanbomhabisCetak";
        }else if($jenis == 3){
            $where = implode(" AND ", $where_habisbom);
            
            $habis_bom = get_habisbom(", pb.*, v.* , '0' id_sort, ms.satuan as inisial_satuan, ms2.satuan as inisial_perubahan ", " LEFT JOIN ms_satuan as ms ON ms.id_satuan = v.id_satuan LEFT JOIN ms_satuan as ms2 ON ms2.id_satuan = v.id_perubahansatuan  ".$add_where.$where." GROUP BY pb.id_workorder_barang ORDER BY v.tanggal_full, pb.noAuto, pb.noAuto_ext, v.nama_motif, mw.inisial");
            $data['detail'] = DB::select($habis_bom);
            $html = "LaporanbomkeluarCetak";

        } else{
            $data['detail'] = array();
            $html = "LaporanhasiltenunMakelarTanggalCetak";
        }

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

    // -------------- Laporan Hasil Tenun 

    // -------------- Mutasi Gudang

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

        $add_where = "";

        $where = array();
        $wheres = array();
        $where2 = array();
        $barang = array();
        $kategori = "";
        $lokasi = "";
        $tanggal = ""; 
        $gudang = ["nama_gudang" => '', "lokasi_gudang" => ''];
        $warna = ["nama" => '', "inisial" => ''];
        $proses = '';
        $arr = array();
        $concat_awal = '';
        $concat_detail = '';
        $groupBy_awal = '';

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

        if(count($req) > 0){
            $add_where = "WHERE ";
        }


        if(isset($req['id_kategoribarang'])){
            if($req['id_kategoribarang'] != ''){
                $where[] = "A.id_kategori = ".$req['id_kategoribarang']." ";

                $where2[] = "B.id_kategori = ".$req['id_kategoribarang']." ";

                $kategori =  DB::table('ms_kategori_barang')->where('id_kb','=',$req['id_kategoribarang'])->first()->nama;
            }
        }

        if(isset($req['id_barang'])){
            if($req['id_barang'] != ''){
            $where[] = "A.id_barang = ".$req['id_barang']." ";

            $where2[] = "B.id_barang = ".$req['id_barang']." ";
            
            $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'))->where('mb.id', $req['id_barang'])->first();
            }

       
        }
        
        if(isset($req['id_lokasi'])){
            $where[] = "A.id_lokasi = ".$req['id_lokasi']." ";

            $where2[] = "B.id_lokasi = ".$req['id_lokasi']." ";
            
            $lokasi = DB::table('ms_lokasi_produksi')->where('id_lokasi', $req['id_lokasi'])->first()->nama_lokasi;
        }

        if(isset($req['tgl_awal']) && isset($req['tgl_akhir'])){
            $tgl_awal = tgl_full($req['tgl_awal'], '2');
            $tgl_akhir = tgl_full($req['tgl_akhir'], '2');
            
            $where[] = "A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";

            $where2[] = "B.tanggal < '$tgl_awal' ";

            $tgl_where_detail = "AND A.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' ";

            $tgl_where_awal = "AND A.tanggal < '$tgl_awal'";

            //$where[] = "A.tanggal <= '$tgl_akhir' ";            
            //$tanggal = date_range($tgl_awal, $tgl_akhir);

        }

        if(isset($req['id_gudang'])){
            if($req['id_gudang'] != ''){
                $where[] = "A.id_gudang = ".$req['id_gudang']." ";

                $where2[] = "B.id_gudang = ".$req['id_gudang']." ";

                $d_gudang = DB::table('ms_gudang as mg')->join('ms_lokasi_produksi as mlp', 'mg.id_lokasi', 'mlp.id_lokasi')->where('mg.id_gudang', $req['id_gudang'])->select(DB::raw("mg.*, mlp.nama_lokasi"))->first();
                $gudang = ['nama_gudang' => $d_gudang->nama_gudang, 'lokasi_gudang' => $d_gudang->nama_lokasi];
            }
        }

        if(isset($req['id_warna'])){
            if($req['id_warna'] != ''){
                $where[] = "A.id_komposisi = ".$req['id_warna']." ";

                $where2[] = "B.id_komposisi = ".$req['id_warna']." ";

                $d_warna = DB::table('ms_warnakomposisi')->where('id_komposisi', $req['id_warna'])->first();
                $warna = ["nama" => $d_warna->nama, "inisial" => $d_warna->inisial];
            }
        }

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

                if($req['id_proses'] == 0){
                    $where[] = "A.id_proses = '' ";
                    $where2[] = "B.id_proses = '' ";
                    
                    $proses = "-";
                }else{
                    $where[] = "A.id_proses = ".$req['id_proses']." ";

                    $where2[] = "B.id_proses = ".$req['id_proses']." ";
                    
                    $proses = DB::table('ms_proses')->where('id_proses', $req['id_proses'])->first()->nama_proses;
                }

                if(in_array($req['id_proses'], setup_benang()) || $req['id_proses'] == 0){
                    $concat_awal = ", CONCAT(A.id_barang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort, '0' as id_sort_detail, '0' AS jenis";
                    $concat_detail = ", CONCAT(A.id_barang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort, CONCAT(A.id_barang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort_detail, '1' AS jenis";
                    $groupBy_awal = "GROUP BY A.id_barang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori";
                }else if(in_array($req['id_proses'], setup_corak())){
                    if($req['id_proses'] == 24){
                        $concat_awal = ", CONCAT(A.id_motif, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort, '0' as id_sort_detail, '0' AS jenis";
                        $concat_detail = ", CONCAT(A.id_motif, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort, CONCAT(A.id_motif, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort_detail, '1' AS jenis";
                        $groupBy_awal = "GROUP BY A.id_motif, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori";
                    }else{
                        $concat_awal = ", CONCAT(A.id_corak, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort, '0' as id_sort_detail, '0' AS jenis";
                        $concat_detail = ", CONCAT(A.id_corak, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort, CONCAT(A.id_corak, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort_detail, '1' AS jenis";
                        $groupBy_awal = "GROUP BY A.id_corak, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori";
                    }
                }else{
                    $concat_awal = ", CONCAT(A.id_motif, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort, '0' as id_sort_detail, '0' AS jenis";
                    $concat_detail = ", CONCAT(A.id_motif, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort, CONCAT(A.id_barang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort_detail, '1' AS jenis";
                    $groupBy_awal = "GROUP BY A.id_motif, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori";
                }
                
            }else{
                $concat_awal = ", CONCAT(A.id_barang, A.id_corak, A.id_motif, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort, '0' as id_sort_detail, '0' AS jenis";
                $concat_detail = ", CONCAT(A.id_barang, A.id_corak, A.id_motif, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort, CONCAT(A.id_barang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort_detail, '1' AS jenis";
                $groupBy_awal = "GROUP BY A.id_barang, A.id_corak, A.id_motif, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori";
            }
        }   

        $data['induk'] = array("barang" => $barang, 'nama_lokasi' => $lokasi, 'tanggal_awal' => $tgl_awal, 'tanggal_akhir' => $tgl_akhir, 'nama_gudang' => $gudang['nama_gudang'], 'lokasi_gudang' => $gudang['lokasi_gudang'], 'nama_komposisi' => $warna['nama'], 'inisial_komposisi' => $warna['inisial'], 'nama_proses' => $proses, 'kategori' => $kategori);
        $data['where'] = implode(" AND ", $where);
        $data['where2'] = implode(" AND ", $where2);

        $on_where = "ON A.id_kategori = C.id_kategori AND A.id_barang = C.id_barang AND A.id_proses = C.id_proses AND A.id_komposisi = C.id_komposisi AND A.id_satuan = C.id_satuan";
        // $concat_awal = ", CONCAT(A.id_barang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort, '0' as id_sort_detail, '0' AS jenis";
        // $concat_detail = ", CONCAT(A.id_barang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort, CONCAT(A.id_barang, A.id_proses, A.id_komposisi, A.id_satuan, A.id_kategori) as id_sort_detail, '1' AS jenis";

        $where_detail = $add_where.$data['where'].$tgl_where_detail;    
        $where_awal = $add_where.$data['where'].$tgl_where_detail.$groupBy_awal;
        $where2_awal = $add_where.$data['where2'].str_replace("A.", "B.", $groupBy_awal);

        $query['saldo_awal'] = get_mutasitenun_saldoawal($where_awal, $where2_awal, $on_where, $concat_awal);
        $query['detail'] = get_mutasitenun_detail($where_detail, $concat_detail);
        $query_mutasi = implode(" UNION ALL ", $query);
        $query_rekap_mutasi = "SELECT A.*, mk.inisial as inisial_komposisi, ms.satuan as inisial_satuan FROM ($query_mutasi) AS A LEFT JOIN ms_warnakomposisi AS mk ON A.id_komposisi=mk.id_komposisi LEFT JOIN ms_satuan as ms ON ms.id_satuan = A.id_satuan ORDER BY A.id_sort, A.id_sort_detail, A.tanggal_full, A.nama_komposisi";
        $query_hasil_rekap = DB::select($query_rekap_mutasi);

        

        $arr = array();
        if($query_hasil_rekap > 0){
            $total = 0;
            foreach($query_hasil_rekap as $d){
            
             if($d->jenis == 0){
                $ket_1 = '';
                $ket_2 = '';
                $ket_3 = 'Saldo Awal';
                $ket_4 = '';
                $ket_5 = '';
                $total = $d->stok;
                $masuk = '0';
                $keluar = '0';
                $jumlah = $d->stok;
             }else{
                $ket_1 = $d->ket_1;
                $ket_2 = $d->ket_2;
                $ket_3 = $d->ket_3;
                $ket_4 = $d->ket_4;
                $ket_5 = $d->ket_5;
                $total = ($total+$d->masuk)-$d->keluar;
                $masuk = $d->masuk;
                $keluar = $d->keluar;
                $jumlah = 0;
             }   
            $arr[] = array('id' => $d->id, 
                            'id_barang' => $d->id_barang, 
                            'kode_barang' => $d->kode_barang,
                            'nama_barang' => $d->nama_barang,
                            'id_kategori' => $d->id_kategori,
                            'nama_kategori' => $d->nama_kategori,
                            'id_subkategori' => $d->id_subkategori,
                            'kategori' => $d->nama_subkategori,
                            'jumlah' => $jumlah,
                            'masuk' => $masuk,
                            'keluar' => $keluar,
                            'id_gudang' => $d->id_gudang,
                            'nama_gudang' => $d->nama_gudang,
                            'tanggal' => $d->tanggal,
                            'id_proses' => $d->id_proses,
                            'nama_proses' => $d->nama_proses,
                            'id_komposisi' => $d->id_komposisi,
                            'nama_komposisi' => $d->nama_komposisi,
                            'inisial_komposisi' => $d->inisial_warna,
                            'id_satuan' => $d->id_satuan,
                            'nama_satuan' => $d->inisial_satuan,
                            'id_corak' => $d->id_corak,
                            'nama_corak' => $d->nama_corak,
                            'id_motif' => $d->id_motif,
                            'nama_motif' => $d->nama_motif,
                            'id_workorder' => $d->id_workorder,
                            'no_workorder' => $d->no_workorder,
                            'id_workorder_barang' => $d->id_workorder_barang,
                            'no_workorder_barang' => $d->no_workorder_barang,
                            '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_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_cap' => $d->id_cap,
                            'nama_cap' => $d->nama_cap,
                            'id_kualitas_proses' => $d->id_kualitas_proses,
                            'nama_kualitas_proses' => $d->nama_kualitas_proses,
                            'id_kualitas_periksa' => $d->id_kualitas_periksa,
                            'nama_kualitas_periksa' => $d->nama_kualitas_periksa,
                            'id_perubahansatuan' => $d->id_perubahansatuan,
                            'nama_perubahansatuan' => $d->nama_perubahansatuan,
                            'jumlah_rencana' => $d->jumlah_rencana,
                            'ket_1' => $ket_1,
                            'ket_2' => $ket_2,
                            'ket_3' => $ket_3,
                            'ket_4' => $ket_4,
                            'ket_5' => $ket_5,
                            'stok' => '0',
                            'nama_warna' => $d->nama_warna,
                            'inisial_warna' => $d->inisial_warna,
                            'ket_proses' => $d->ket_proses,
                            'jenis' => $d->jenis,
                            'total' => $total,
                            'lebar' => '', //$d->lebar,
                            'panjang' => '', //$d->panjang,
                            'pick' => '', //$d->pick
                            );
            }
        }else{
            $arr = array();
        }
                
        $data['detail'] = barang_byproses($arr, 4, $proses);


        switch ($jenis) {
            case '0':
                $html = "MutasigudangfilterBaruCetak"; //"MutasibarangCetak";
                break;
            case '1':
                $html = "MutasigudangfilterBaruCetak";
                break;
            default:
                $html = "MutasigudangCetak";
                break;
        }
        require('fpdf181/Mc_table.php');
        return response(view('produksi.cetak.'.$html)->with('data', $data))->header('Content-Type', 'application/pdf');

                
    }

    /* ----------------------- EXCEL ------------------------------*/

    /* ----------------------- EXCEL ------------------------------*/

    function groupby($huruf){
        return "GROUP BY ".$huruf.".id_barang, ".$huruf.".id_gudang, ".$huruf.".id_proses, ".$huruf.".id_komposisi, ".$huruf.".id_satuan, ".$huruf.".id_corak, ".$huruf.".id_motif, ".$huruf.".id_workorder_barang, ".$huruf.".id_kualitas, ".$huruf.".id_kualitas_dudul, ".$huruf.".id_label, ".$huruf.".id_kemasan, ".$huruf.".id_kemasansatuan, ".$huruf.".id_kemasansepuluh, ".$huruf.".id_kemasanseratus, ".$huruf.".id_cap, ".$huruf.".id_kualitas_proses, ".$huruf.".id_kualitas_periksa, ".$huruf.".id_perubahansatuan, ".$huruf.".jumlah_rencana, ".$huruf.".id_kategori";
    }

    function groupby_awal($huruf){
         return "GROUP BY ".$huruf.".id_barang, ".$huruf.".id_gudang, ".$huruf.".id_proses, ".$huruf.".id_komposisi, ".$huruf.".id_satuan, ".$huruf.".id_kategori";
    }

    function groupby_pro($huruf){
        return "GROUP BY ".$huruf.".id_barang,".$huruf.".id_proses_sekarang, ".$huruf.".id_komposisi, ".$huruf.".id_satuan, ".$huruf.".id_corak, ".$huruf.".id_motif, ".$huruf.".id_kategori, ".$huruf.".id_workorder_barang"; // ".$huruf.".id_workorder_barang, ".$huruf.".id_kualitas, ".$huruf.".id_kualitas_dudul, ".$huruf.".id_label, ".$huruf.".id_kemasan, ".$huruf.".id_kemasansatuan, ".$huruf.".id_kemasansepuluh, ".$huruf.".id_kemasanseratus, ".$huruf.".id_cap, ".$huruf.".id_kualitas_proses, ".$huruf.".id_kualitas_periksa,   ".$huruf.".id_gudang, ".$huruf.".id_perubahansatuan, ";.$huruf.".jumlah_rencana,
    }

    function groupby_pro_celup($huruf){
        return "GROUP BY ".$huruf.".id_barang, ".$huruf.".id_proses_sekarang, ".$huruf.".id_satuan, ".$huruf.".id_corak, ".$huruf.".id_motif, ".$huruf.".id_kategori"; //".$huruf.".id_workorder_barang, ".$huruf.".id_kualitas, ".$huruf.".id_kualitas_dudul, ".$huruf.".id_label, ".$huruf.".id_kemasan, ".$huruf.".id_kemasansatuan, ".$huruf.".id_kemasansepuluh, ".$huruf.".id_kemasanseratus, ".$huruf.".id_cap, ".$huruf.".id_kualitas_proses, ".$huruf.".id_kualitas_periksa, ".$huruf.".id_perubahansatuan, ".$huruf.".jumlah_rencana,
    }

    function groupby_proses($huruf){
        return "GROUP BY ".$huruf.".id_barang, ".$huruf.".id_gudang, ".$huruf.".id_proses, ".$huruf.".id_komposisi, ".$huruf.".id_satuan, ".$huruf.".id_corak, ".$huruf.".id_kualitas, ".$huruf.".id_kualitas_dudul, ".$huruf.".id_label, ".$huruf.".id_kemasan, ".$huruf.".id_kemasansatuan, ".$huruf.".id_kemasansepuluh, ".$huruf.".id_kemasanseratus, ".$huruf.".id_cap, ".$huruf.".id_kualitas_proses, ".$huruf.".id_kualitas_periksa, ".$huruf.".id_kategori";
    }

    function groupby_multi($huruf){
        return "GROUP BY ".$huruf.".id_barang, ".$huruf.".id_proses, ".$huruf.".id_satuan, ".$huruf.".id_motif, ".$huruf.".id_kategori";
    }

    function concat_multi($huruf){
        return "CASE WHEN ".$huruf.".id_barang != '' THEN ".$huruf.".id_barang ELSE '0' END, CASE WHEN ".$huruf.".id_proses != '' THEN ".$huruf.".id_proses ELSE '0' END, CASE WHEN ".$huruf.".id_komposisi != '' THEN ".$huruf.".id_komposisi ELSE '0' END, CASE WHEN ".$huruf.".id_satuan != '' THEN ".$huruf.".id_satuan ELSE '0' END, CASE WHEN ".$huruf.".id_corak != '' THEN ".$huruf.".id_corak ELSE '0' END, CASE WHEN ".$huruf.".id_motif != '' THEN ".$huruf.".id_motif ELSE '0' END"; //, CASE WHEN ".$huruf.".id_kualitas != '' THEN ".$huruf.".id_kualitas ELSE '0' END, CASE WHEN ".$huruf.".id_kualitas_dudul != '' THEN ".$huruf.".id_kualitas_dudul ELSE '0' END, CASE WHEN ".$huruf.".id_label != '' THEN ".$huruf.".id_label ELSE '0' END, CASE WHEN ".$huruf.".id_kemasan != '' THEN ".$huruf.".id_kemasan ELSE '0' END, CASE WHEN ".$huruf.".id_kemasansatuan != '' THEN ".$huruf.".id_kemasansatuan ELSE '0' END, CASE WHEN ".$huruf.".id_kemasansepuluh != '' THEN ".$huruf.".id_kemasansepuluh ELSE '0' END, CASE WHEN ".$huruf.".id_kemasanseratus != '' THEN ".$huruf.".id_kemasanseratus ELSE '0' END, CASE WHEN ".$huruf.".id_cap != '' THEN ".$huruf.".id_cap ELSE '0' END, CASE WHEN ".$huruf.".id_kualitas_proses != '' THEN ".$huruf.".id_kualitas_proses ELSE '0' END, CASE WHEN ".$huruf.".id_kualitas_periksa != '' THEN ".$huruf.".id_kualitas_periksa ELSE '0' END";
    }

    function concat_bon($huruf){
        return "CASE WHEN ".$huruf.".id_barang != '' THEN ".$huruf.".id_barang ELSE '0' END, CASE WHEN ".$huruf.".id_proses != '' THEN ".$huruf.".id_proses ELSE '0' END, CASE WHEN ".$huruf.".id_komposisi != '' THEN ".$huruf.".id_komposisi ELSE '0' END, CASE WHEN ".$huruf.".id_satuan != '' THEN ".$huruf.".id_satuan ELSE '0' END, CASE WHEN ".$huruf.".id_corak != '' THEN ".$huruf.".id_corak ELSE '0' END, CASE WHEN ".$huruf.".id_motif != '' THEN ".$huruf.".id_motif ELSE '0' END"; //, CASE WHEN ".$huruf.".id_workorder_barang != '' THEN ".$huruf.".id_workorder_barang ELSE '0' END, CASE WHEN ".$huruf.".id_kualitas != '' THEN ".$huruf.".id_kualitas ELSE '0' END, CASE WHEN ".$huruf.".id_kualitas_dudul != '' THEN ".$huruf.".id_kualitas_dudul ELSE '0' END, CASE WHEN ".$huruf.".id_label != '' THEN ".$huruf.".id_label ELSE '0' END, CASE WHEN ".$huruf.".id_kemasan != '' THEN ".$huruf.".id_kemasan ELSE '0' END, CASE WHEN ".$huruf.".id_kemasansatuan != '' THEN ".$huruf.".id_kemasansatuan ELSE '0' END, CASE WHEN ".$huruf.".id_kemasansepuluh != '' THEN ".$huruf.".id_kemasansepuluh ELSE '0' END, CASE WHEN ".$huruf.".id_kemasanseratus != '' THEN ".$huruf.".id_kemasanseratus ELSE '0' END, CASE WHEN ".$huruf.".id_cap != '' THEN ".$huruf.".id_cap ELSE '0' END, CASE WHEN ".$huruf.".id_kualitas_proses != '' THEN ".$huruf.".id_kualitas_proses ELSE '0' END, CASE WHEN ".$huruf.".id_kualitas_periksa != '' THEN ".$huruf.".id_kualitas_periksa ELSE '0' END";
    }

    function groupby_bon($huruf, $huruf2){
        return " GROUP BY ".$huruf.".id_barang, ".$huruf.".id_proses, ".$huruf2.".id_komposisi, ".$huruf.".id_satuan, ".$huruf.".id_corak, ".$huruf.".id_motif";//, ".$huruf.".id_workorder_barang, ".$huruf.".id_kualitas, ".$huruf.".id_kualitas_dudul, ".$huruf.".id_label, ".$huruf.".id_kemasan, ".$huruf.".id_kemasansatuan, ".$huruf.".id_kemasansepuluh, ".$huruf.".id_kemasanseratus, ".$huruf.".id_cap, ".$huruf.".id_kualitas_proses, ".$huruf.".id_kualitas_periksa";
    }

    function concat_produksi($jenis, $alias = 'A'){
        $base_concat = "CASE WHEN ".$alias.".id_barang != '' THEN ".$alias.".id_barang ELSE '0' END, CASE WHEN ".$alias.".id_proses_sekarang != '' THEN ".$alias.".id_proses_sekarang ELSE '0' END, CASE WHEN ".$alias.".id_satuan != '' THEN ".$alias.".id_satuan ELSE '0' END,  CASE WHEN ".$alias.".id_corak != '' THEN ".$alias.".id_corak ELSE '0' END, CASE WHEN ".$alias.".id_workorder_barang != '' THEN ".$alias.".id_workorder_barang ELSE '0' END, CASE WHEN ".$alias.".id_motif != '' THEN ".$alias.".id_motif ELSE '0' END"; //,  CASE WHEN ".$alias.".id_kualitas != '' THEN ".$alias.".id_kualitas ELSE '0' END,  CASE WHEN ".$alias.".id_kualitas_dudul != '' THEN ".$alias.".id_kualitas_dudul ELSE '0' END,  CASE WHEN ".$alias.".id_label != '' THEN ".$alias.".id_label ELSE '0' END,  CASE WHEN ".$alias.".id_kemasan != '' THEN ".$alias.".id_kemasan ELSE '0' END,  CASE WHEN ".$alias.".id_kemasansatuan != '' THEN ".$alias.".id_kemasansatuan ELSE '0' END,  CASE WHEN ".$alias.".id_kemasansepuluh != '' THEN ".$alias.".id_kemasansepuluh ELSE '0' END,  CASE WHEN ".$alias.".id_kemasanseratus != '' THEN ".$alias.".id_kemasanseratus ELSE '0' END,  CASE WHEN ".$alias.".id_cap != '' THEN ".$alias.".id_cap ELSE '0' END,  CASE WHEN ".$alias.".id_kualitas_proses != '' THEN ".$alias.".id_kualitas_proses ELSE '0' END,  CASE WHEN ".$alias.".id_kualitas_periksa != '' THEN ".$alias.".id_kualitas_periksa ELSE '0' END

        switch ($jenis) {
            // Celup
            case '18':
            case '22':
                return $base_concat.", CASE WHEN ".$alias.".id_warna != '' THEN ".$alias.".id_warna ELSE '0' END";
                break;
            case '24':
                return str_replace(["CASE WHEN ".$alias.".id_barang != '' THEN ".$alias.".id_barang ELSE '0' END,", ", CASE WHEN ".$alias.".id_motif != '' THEN ".$alias.".id_motif ELSE '0' END"], ["", ""], $base_concat);;
                break;
            case '94':
                return $base_concat;
                break;
            case '25':
                $base_concat = str_replace(",  CASE WHEN ".$alias.".id_corak != '' THEN ".$alias.".id_corak ELSE '0' END", "", $base_concat);
                return $base_concat.", CASE WHEN ".$alias.".id_komposisi != '' THEN ".$alias.".id_komposisi ELSE '0' END";
                break;
            case '41':
                $base_concat = str_replace(", CASE WHEN ".$alias.".id_workorder_barang != '' THEN ".$alias.".id_workorder_barang ELSE '0' END", "", $base_concat);
                return $base_concat;
                break;
            default:
                return $base_concat.", CASE WHEN ".$alias.".id_warna != '' THEN ".$alias.".id_warna ELSE '0' END";
                break;
        }
    }

    function non_sekir($id_proses, $alias = "A", $pos = '0'){
        $return = "";
        if($id_proses != 24){
            $return = $alias.".id_barang";

            if($pos == 0){
                $return = ", ".$return;
            }else{
                $return .= ", ";
            }
        }

        return $return;
    }
}

function removeElementWithValue($array, $key, $value){
     foreach($array as $subKey => $subArray){
        
            if(in_array($subArray[$key], $value)){
                unset($array[$subKey]);
            }

          // if($subArray[$key] == $value){
          //      unset($array[$subKey]);
          // }
     }
     return $array;
}