<?php

namespace App\Http\Controllers\produksi;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Fpdf;
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); 
        });
    }
    
    function index(){
        $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['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');
        $data['motif'] = DB::table('ms_motif')->orderBy('nama_motif','asc');
        $data['corak'] = DB::table('ms_motif_barang')->orderBy('motif_keterangan','asc');


        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 mutasiproduksi(Request $request){
        $req = $request->all();

        $add_where = "";

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

        $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']." ";
            
            $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_motif'])){
            if($req['id_motif'] != ''){
                $where[] = "A.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']." ";

                $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']." ";

                $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, "nama_motif" => $motif, "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_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";
        $data['where'] = $add_where.$data['where'].$data['group'];


        //$d_query = get_rawquery(get_mutasi($data['where']));
        //print_r(get_mutasi($data['where']));           
        $d_query = get_rawquery(get_mutasiproduksi($data['where'])); 
        //print_r($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']])){
                    
                    $where = "WHERE 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_mutasiproduksi('')." ) 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";

                    $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' => "Pindahan",
                            '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' => "Pindahan",
                            '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 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_mutasiproduksi($where));
            $arr[] = $w+array('total' => $w['stok'], 'jenis' => '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');
            //    $arr_2[] = $d+array('stok' => '0');
            }                
        }

        
        $data['detail'] = $arr;        
        

        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 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, A.ket_3";
            $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($arr);
        

                
    }


}
