<?php

namespace App\Http\Controllers\inventori;

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

    public function index_non(){
        
        return view('inventori.PenerimaanNonProduksiList');
    }

    public function edit($id, $induk){
        $data['pembelian'] = DB::table('pem_pembelian as b')->leftjoin('ms_lokasi_produksi as mlp', 'mlp.id_lokasi', 'b.lokasi')->select(DB::raw("b.*, mlp.nama_lokasi, mp.nama nama_penyedia"))->where('id_pembelian',$id)->leftjoin('ms_penyedia as mp', 'b.id_penyedia', 'mp.id_penyedia')->first();
        $d_barang = DB::table('pem_pembelian_barang AS a')->join('pem_pembelian AS b','a.id_pembelian','b.id_pembelian')->join('ms_barang AS c','a.id_barang','c.id')->join('ms_satuan as ms', 'c.id_satuan', 'ms.id_satuan')->select('a.*','c.kode_barang','c.nama_barang', 'ms.nama_satuan')->where('a.id_pembelian',$id);

        $penerimaan_induk = DB::table('pem_penerimaan_induk as ppi')->leftjoin('pem_penerimaan as pp', 'ppi.id_induk', 'pp.id_induk')->where('pp.id_pembelian', $id)->where('ppi.id_induk', '=', $induk);

        if($penerimaan_induk->count() > 0){
            $data['penerimaan_induk'] = $this->data(DB::table('pem_penerimaan_induk as ppi')->leftjoin('pem_penerimaan as pp', 'ppi.id_induk', 'pp.id_induk')->where('pp.id_pembelian', $id)->where('ppi.id_induk', $induk)->first());

            $induk_lain = DB::table('pem_penerimaan_induk as ppi')->leftjoin('pem_penerimaan as pp', 'ppi.id_induk', 'pp.id_induk')->where('pp.id_pembelian', $id)->where('ppi.id_induk', '!=', $induk);
            foreach($d_barang->get() as $d){
                $jum = 0;
                if($induk_lain->count() > 0){
                    foreach ($induk_lain->get() as $p) {
                    
                        if($d->id_beli_barang == $p->id_beli_barang){
                            $jum = $d->jml_barang - $p->jml_barang_terima;
                        }

                    }
                }else{
                    $jum = $d->jml_barang;
                }

                $last = DB::table('pem_penerimaan')->where('id_beli_barang', '=', $d->id_beli_barang)->where('id_induk', $induk);
                $d_last = $last->first();
                $id_terima = '';
                $jml_terima = '';
                $gudang = '';

                if($last->count() > 0){
                    $id_terima = $d_last->id_penerimaan;
                    $jml_terima = $d_last->jml_barang_terima;
                    $gudang = $d_last->id_gudang;
                }

                $arr[] = array('id_pembelian' => $d->id_pembelian,
                                'id_beli_barang' => $d->id_beli_barang,
                                'id_barang' => $d->id_barang,
                                'jml_barang' => $jum,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang,
                                'id_penerimaan' => $id_terima,
                                'jml_terima' => $jml_terima,
                                'gudang' => $gudang,
                                'nama_satuan' => $d->nama_satuan);
            }
            

        }else{
            $data['penerimaan_induk'] = $this->data(array());
            foreach($d_barang->get() as $d){
                

                $last = DB::table('pem_penerimaan')->where('id_beli_barang', '=', $d->id_beli_barang);
                $d_last = $last->first();
                $id_terima = '';
                $jml_terima = '';
                $gudang = '';

                if($last->count() > 0){
                    $id_terima = $d_last->id_penerimaan;
                    $jml_terima = $d_last->jml_barang_terima;
                    $gudang = $d_last->id_gudang;
                }

                $arr[] = array('id_pembelian' => $d->id_pembelian,
                                'id_beli_barang' => $d->id_beli_barang,
                                'id_barang' => $d->id_barang,
                                'jml_barang' => $d->jml_barang,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang,
                                'id_penerimaan' => $id_terima,
                                'jml_terima' => $jml_terima,
                                'gudang' => $gudang,
                                'nama_satuan' => $d->nama_satuan);
            }

        }
        $id_gudang = $this->session['id_gudang'];
        $data['gudang'] = DB::table('ms_gudang');
        if($id_gudang != 0){
            $data['gudang']->wherein('id_gudang', explode(',', $id_gudang));
        }
        $data['gudang'] = $data['gudang']->get();

        $data['detail'] = $arr;
        return view('inventori.PenerimaanEdit')->with('data',$data);
    //    print_r($arr);
    }

    public function get_data(){
        $id_gudang = $this->session['id_gudang'];
        $wherein  = array();
        // if($id_gudang != 0){
        //     $ex_gudang = explode(',', $id_gudang);

        //     if(in_array(29, $ex_gudang)){
        //         if(count($ex_gudang) > 1){
        //             $wherein += [5, 17, 27];
        //         }else{
        //             $wherein += [27];
        //         }
        //     }else{
        //         $wherein += [5, 17, 27];
        //     }
        // }else{
        //     $wherein += [5, 17, 27];
        // }

        

        DB::statement(DB::raw('set @rownum=0'));
        $id_produksi = DB::table('pem_pembelian_barang as ppb')->leftjoin('ms_barang as mb', 'ppb.id_barang', 'mb.id')->join('ms_kategori_barang as mkb', 'mb.id_kb', 'mkb.id_kb')->join('ms_kategori_barang as mkb2', 'mkb.induk_kb', 'mkb2.id_kb')
        ->when($wherein, function($qry) use ($wherein){
            return $qry->wherein('mkb2.id_kb', $wherein);
        })
        // ->wherein('mkb2.id_kb', $wherein)
        ->select(DB::raw('ppb.id_pembelian'))->groupby('ppb.id_pembelian')->get();
        $id_beli = array();
        foreach($id_produksi as $i){
            $id_beli[] = $i->id_pembelian;
        }
        
        $pembelian = DB::table('pem_pembelian as b')->leftjoin('ms_lokasi_produksi as mlp', 'mlp.id_lokasi', 'b.lokasi')->select(DB::raw('b.*, mlp.id_lokasi, mlp.nama_lokasi, mp.nama nama_penyedia, mtp.id id_serah, mtp.nama tempat_serah, mlp2.id_lokasi id_lokasi_serah, mlp2.nama_lokasi nama_lokasi_serah, @rownum  := @rownum  + 1 AS rownum'))->leftjoin('ms_penyedia as mp', 'b.id_penyedia', 'mp.id_penyedia')->orderby('b.tanggal', 'desc')->leftjoin('ms_tempat_penyerahan AS mtp', 'b.tempat_serah', 'mtp.id')->leftjoin('ms_lokasi_produksi AS mlp2', 'mlp2.id_lokasi', 'mtp.id_lokasi')->wherein('b.id_pembelian', $id_beli);
        
        if($this->session['id_lokasi'] != 0){
            $pembelian->where('mlp2.id_lokasi','=',$this->session['id_lokasi']);
        }


        if($pembelian->count() > 0){
            foreach($pembelian->get() as $d){
                // $penerimaan = DB::table('pem_penerimaan')->where('id_pembelian',$d->id_pembelian)->select(DB::raw('*,@rownum := @rownum + 1 AS rownum'))->orderby('status','asc');
                $jenis = 0;

                if($d->status == 0){
                    $jenis = 0;
                    $induk = 0;
                    $arr[] = array('id_pembelian' => $d->id_pembelian,
                            'no_terima' => '',
                            'no_faktur' => $d->no_faktur,
                            'suffix' => $d->sufix_no_faktur,
                            'faktur' => $d->no_faktur.$d->sufix_no_faktur,
                            'nama_penyedia' => $d->nama_penyedia,
                            'tanggal' => tgl_full($d->tanggal, ''),
                            'status_id' => $d->status,
                            'status' => $this->get_status($d->status),
                            'rownum' => $d->rownum,
                            'id_lokasi' => $d->id_lokasi,
                            'nama_lokasi' => $d->nama_lokasi,
                            'aksi' => $this->aksi($jenis, $d->status, $d->id_pembelian, $induk));
                }else{
                    $cek_terima = DB::table('pem_penerimaan_induk as ppi')->leftjoin('pem_penerimaan as pp', 'ppi.id_induk', 'pp.id_induk')->where('ppi.id_pembelian', $d->id_pembelian)->select('ppi.*');
                    $d_induk = DB::table('pem_penerimaan_induk')->where('id_pembelian', $d->id_pembelian);
                    $jenis = 1;
                    $induk = 0;
                    // if($d->status == '1'){
                    //     if($cek_terima->count() > 0){
                    //         $status = $cek_terima->first()->status;
                    //         $induk = $cek_terima->first()->id_induk;
                    //     }else{
                    //         $status = '1';
                    //     }

                    // }else if($d->status == '2'){
                    //     if($cek_terima->count() > 0){
                    //         $status = $cek_terima->first()->status;
                    //         $induk = $cek_terima->first()->id_induk;
                    //     }else{
                    //         $status = '1';
                    //     }
                    // }

                   

                    // $arr[] = array('id_pembelian' => $d->id_pembelian,
                    //         'no_faktur' => $d->no_faktur,
                    //         'suffix' => $d->sufix_no_faktur,
                    //         'faktur' => $d->no_faktur.$d->sufix_no_faktur,
                    //         'nama_penyedia' => $d->nama_penyedia,
                    //         'tanggal' => tgl_full($d->tanggal, ''),
                    //         'status_id' => $d->status,
                    //         'status' => $this->get_status($status),
                    //         'rownum' => $d->rownum,
                    //         'id_lokasi' => $d->id_lokasi,
                    //         'nama_lokasi' => $d->nama_lokasi,
                    //         'aksi' => $this->aksi($jenis, $status, $d->id_pembelian, $induk));

                    if($d->status == '2'){
                        $jenis = 0;
                        $status = 2;
                        $induk = 0;
                        $arr[] = array('id_pembelian' => $d->id_pembelian,
                            'no_terima' => '',
                            'no_faktur' => $d->no_faktur,
                            'suffix' => $d->sufix_no_faktur,
                            'faktur' => $d->no_faktur.$d->sufix_no_faktur,
                            'nama_penyedia' => $d->nama_penyedia,
                            'tanggal' => tgl_full($d->tanggal, ''),
                            'status_id' => $d->status,
                            'status' => $this->get_status($status),
                            'rownum' => $d->rownum,
                            'id_lokasi' => $d->id_lokasi,
                            'nama_lokasi' => $d->nama_lokasi,
                            'aksi' => $this->aksi($jenis, $status, $d->id_pembelian, $induk));
                    }


                    if($d_induk->count() > 0){
                        $jenis = 1;
                        foreach($d_induk->get() as $i){
                            $status = $i->status;
                            $induk = $i->id_induk;

                            $pop = '<a href="#" data-popup="tooltip" title="Detail" data-placement="right" onclick="popup($(this))" id_beli="'.$d->id_pembelian.'" induk="'.$induk.'">'.$i->no_faktur.$i->faktur.'</a>';

                            $arr[] = array('id_pembelian' => $d->id_pembelian,
                                'no_terima' => $pop,
                                'no_faktur' => $d->no_faktur,
                                'suffix' => $d->sufix_no_faktur,
                                'faktur' => $d->no_faktur.$d->sufix_no_faktur,
                                'nama_penyedia' => $d->nama_penyedia,
                                'tanggal' => tgl_full($d->tanggal, ''),
                                'status_id' => $d->status,
                                'status' => $this->get_status($status),
                                'rownum' => $d->rownum,
                                'id_lokasi' => $d->id_lokasi,
                                'nama_lokasi' => $d->nama_lokasi,
                                'aksi' => $this->aksi($jenis, $status, $d->id_pembelian, $induk));
                        }
                    }else{
                        $status = $d->status;
                            $induk = '0';
                            $jenis = '1';

                            $pop = '<a href="#" data-popup="tooltip" title="Detail" data-placement="right" onclick="popup($(this))" id_beli="'.$d->id_pembelian.'" induk="'.$induk.'"> Tanpa Nomor </a>';

                            $arr[] = array('id_pembelian' => $d->id_pembelian,
                                'no_terima' => $pop,
                                'no_faktur' => $d->no_faktur,
                                'suffix' => $d->sufix_no_faktur,
                                'faktur' => $d->no_faktur.$d->sufix_no_faktur,
                                'nama_penyedia' => $d->nama_penyedia,
                                'tanggal' => tgl_full($d->tanggal, ''),
                                'status_id' => $d->status,
                                'status' => $this->get_status($status),
                                'rownum' => $d->rownum,
                                'id_lokasi' => $d->id_lokasi,
                                'nama_lokasi' => $d->nama_lokasi,
                                'aksi' => $this->aksi($jenis, $status, $d->id_pembelian, $induk));   
                    }                   

                }

                
            }
        }else{
            $arr = array();
        }
        
        $data = new Collection($arr);
        return Datatables::of($data)->make(true);
        // print_r($id_gudang);
        // print_r("\n");
        // print_r($wherein);
    }

    public function get_datanon(){
        DB::statement(DB::raw('set @rownum=0'));
        $id_produksi = DB::table('pem_pembelian_barang as ppb')->leftjoin('ms_barang as mb', 'ppb.id_barang', 'mb.id')->join('ms_kategori_barang as mkb', 'mb.id_kb', 'mkb.id_kb')->join('ms_kategori_barang as mkb2', 'mkb.induk_kb', 'mkb2.id_kb')->wherenotin('mkb2.id_kb', [5, 17, 27])->select(DB::raw('ppb.id_pembelian'))->groupby('ppb.id_pembelian')->get();
        $id_beli = array();
        foreach($id_produksi as $i){
            $id_beli[] = $i->id_pembelian;
        }
        
        $pembelian = DB::table('pem_pembelian as b')->leftjoin('ms_lokasi_produksi as mlp', 'mlp.id_lokasi', 'b.lokasi')->select(DB::raw('b.*, mlp.id_lokasi, mlp.nama_lokasi, mp.nama nama_penyedia, mtp.id id_serah, mtp.nama tempat_serah, mlp2.id_lokasi id_lokasi_serah, mlp2.nama_lokasi nama_lokasi_serah, @rownum  := @rownum  + 1 AS rownum'))->leftjoin('ms_penyedia as mp', 'b.id_penyedia', 'mp.id_penyedia')->leftjoin('ms_tempat_penyerahan AS mtp', 'b.tempat_serah', 'mtp.id')->leftjoin('ms_lokasi_produksi AS mlp2', 'mlp2.id_lokasi', 'mtp.id_lokasi')->orderby('b.tanggal', 'desc')->wherein('b.id_pembelian', $id_beli);
        if($this->session['id_lokasi'] != 0){
            $pembelian->where('mlp2.id_lokasi','=',$this->session['id_lokasi']);
        }


        if($pembelian->count() > 0){
            foreach($pembelian->get() as $d){
                // $penerimaan = DB::table('pem_penerimaan')->where('id_pembelian',$d->id_pembelian)->select(DB::raw('*,@rownum := @rownum + 1 AS rownum'))->orderby('status','asc');
                $jenis = 0;

                if($d->status == 0){
                    $jenis = 0;
                    $induk = 0;
                    $arr[] = array('id_pembelian' => $d->id_pembelian,
                            'no_terima' => '',
                            'no_faktur' => $d->no_faktur,
                            'suffix' => $d->sufix_no_faktur,
                            'faktur' => $d->no_faktur.$d->sufix_no_faktur,
                            'nama_penyedia' => $d->nama_penyedia,
                            'tanggal' => tgl_full($d->tanggal, ''),
                            'status_id' => $d->status,
                            'status' => $this->get_status($d->status),
                            'rownum' => $d->rownum,
                            'id_lokasi' => $d->id_lokasi,
                            'nama_lokasi' => $d->nama_lokasi,
                            'aksi' => $this->aksi($jenis, $d->status, $d->id_pembelian, $induk));
                }else{
                    $cek_terima = DB::table('pem_penerimaan_induk as ppi')->leftjoin('pem_penerimaan as pp', 'ppi.id_induk', 'pp.id_induk')->where('ppi.id_pembelian', $d->id_pembelian)->select('ppi.*');
                    $d_induk = DB::table('pem_penerimaan_induk')->where('id_pembelian', $d->id_pembelian);
                    $jenis = 1;
                    $induk = 0;

                    if($d->status == '2'){
                        $jenis = 0;
                        $status = 2;
                        $induk = 0;
                        $arr[] = array('id_pembelian' => $d->id_pembelian,
                            'no_terima' => '',
                            'no_faktur' => $d->no_faktur,
                            'suffix' => $d->sufix_no_faktur,
                            'faktur' => $d->no_faktur.$d->sufix_no_faktur,
                            'nama_penyedia' => $d->nama_penyedia,
                            'tanggal' => tgl_full($d->tanggal, ''),
                            'status_id' => $d->status,
                            'status' => $this->get_status($status),
                            'rownum' => $d->rownum,
                            'id_lokasi' => $d->id_lokasi,
                            'nama_lokasi' => $d->nama_lokasi,
                            'aksi' => $this->aksi($jenis, $status, $d->id_pembelian, $induk));
                    }


                    if($d_induk->count() > 0){
                        $jenis = 1;
                        foreach($d_induk->get() as $i){
                            $status = $i->status;
                            $induk = $i->id_induk;

                            $pop = '<a href="#" data-popup="tooltip" title="Detail" data-placement="right" onclick="popup($(this))" id_beli="'.$d->id_pembelian.'" induk="'.$induk.'">'.$i->no_faktur.$i->faktur.'</a>';

                            $arr[] = array('id_pembelian' => $d->id_pembelian,
                                'no_terima' => $pop,
                                'no_faktur' => $d->no_faktur,
                                'suffix' => $d->sufix_no_faktur,
                                'faktur' => $d->no_faktur.$d->sufix_no_faktur,
                                'nama_penyedia' => $d->nama_penyedia,
                                'tanggal' => tgl_full($d->tanggal, ''),
                                'status_id' => $d->status,
                                'status' => $this->get_status($status),
                                'rownum' => $d->rownum,
                                'id_lokasi' => $d->id_lokasi,
                                'nama_lokasi' => $d->nama_lokasi,
                                'aksi' => $this->aksi($jenis, $status, $d->id_pembelian, $induk));
                        }
                    }else{
                        $status = $d->status;
                            $induk = '0';
                            $jenis = '1';

                            $pop = '<a href="#" data-popup="tooltip" title="Detail" data-placement="right" onclick="popup($(this))" id_beli="'.$d->id_pembelian.'" induk="'.$induk.'"> Tanpa Nomor </a>';

                            $arr[] = array('id_pembelian' => $d->id_pembelian,
                                'no_faktur' => $d->no_faktur,
                                'no_terima' => $pop,
                                'suffix' => $d->sufix_no_faktur,
                                'faktur' => $d->no_faktur.$d->sufix_no_faktur,
                                'nama_penyedia' => $d->nama_penyedia,
                                'tanggal' => tgl_full($d->tanggal, ''),
                                'status_id' => $d->status,
                                'status' => $this->get_status($status),
                                'rownum' => $d->rownum,
                                'id_lokasi' => $d->id_lokasi,
                                'nama_lokasi' => $d->nama_lokasi,
                                'aksi' => $this->aksi($jenis, $status, $d->id_pembelian, $induk));   
                    }
                    

                }

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

    // public function get_datanon(){
    //     DB::statement(DB::raw('set @rownum=0'));
    //     $id_produksi = DB::table('pem_pembelian_barang as ppb')->leftjoin('ms_barang as mb', 'ppb.id_barang', 'mb.id')->join('ms_kategori_barang as mkb', 'mb.id_kb', 'mkb.id_kb')->join('ms_kategori_barang as mkb2', 'mkb.induk_kb', 'mkb2.id_kb')->wherenotin('mkb2.id_kb', [5, 17, 27])->select(DB::raw('ppb.id_pembelian'))->groupby('ppb.id_pembelian')->get();
    //     $id_beli = array();
    //     foreach($id_produksi as $i){
    //         $id_beli[] = $i->id_pembelian;
    //     }
        
    //     $pembelian = DB::table('pem_pembelian as b')->leftjoin('ms_lokasi_produksi as mlp', 'mlp.id_lokasi', 'b.lokasi')->select(DB::raw('b.*, mlp.id_lokasi, mlp.nama_lokasi, mp.nama nama_penyedia, @rownum  := @rownum  + 1 AS rownum'))->leftjoin('ms_penyedia as mp', 'b.id_penyedia', 'mp.id_penyedia')->orderby('status', 'asc')->wherein('b.id_pembelian', $id_beli);


    //     if($pembelian->count() > 0){
    //         foreach($pembelian->get() as $d){
    //             $penerimaan = DB::table('pem_penerimaan')->where('id_pembelian',$d->id_pembelian)->select(DB::raw('*,@rownum := @rownum + 1 AS rownum'))->orderby('status','asc');
    //             if($d->status == '0'){
    //                 $aksi = '<div class="btn-group"><a class="btn btn-success  btn-sm" href="'.url('penerimaan/cek/').'/'.$d->id_pembelian.'" ><i class="icon-checkbox-checked2"></i></a> </div>';
    //             }else{
    //                 $aksi = '<div class="btn-group"><a class="btn btn-success  btn-sm" href="'.url('penerimaan/cek/').'/'.$d->id_pembelian.'" ><i class="icon-checkbox-checked2"></i></a> <a class="btn btn-warning btn-sm" href="'.url('penerimaan/edit/').'/'.$d->id_pembelian.'" ><i class="icon-pencil"></i></a> <a class="btn bg-success-400 btn-sm" type="button" target="_blank" href="'.url('penerimaan/cetak').'/'.$d->id_pembelian.'"><i class="icon-printer"></i> </a></div>';
    //             }
    //             $arr[] = array('id_pembelian' => $d->id_pembelian,
    //                         'no_faktur' => $d->no_faktur,
    //                         'suffix' => $d->sufix_no_faktur,
    //                         'faktur' => $d->no_faktur.$d->sufix_no_faktur,
    //                         'nama_penyedia' => $d->nama_penyedia,
    //                         'tanggal' => tgl_full($d->tanggal, ''),
    //                         'status_id' => $d->status,
    //                         'status' => $this->get_status($d->status),
    //                         'rownum' => $d->rownum,
    //                         'id_lokasi' => $d->id_lokasi,
    //                         'nama_lokasi' => $d->nama_lokasi,
    //                         'aksi' => $aksi);
    //         }
    //     }else{
    //         $arr = array();
    //     }
        
    //     $data = new Collection($arr);
    //     return Datatables::of($data)->make(true);
    // }

    public function cek($id){
    	$data['pembelian'] = DB::table('pem_pembelian as b')->leftjoin('ms_lokasi_produksi as mlp', 'mlp.id_lokasi', 'b.lokasi')->where('id_pembelian',$id)->select(DB::raw("b.*, mlp.nama_lokasi, mp.nama nama_penyedia"))->leftjoin('ms_penyedia as mp', 'b.id_penyedia', 'mp.id_penyedia')->first();
        
        $penerimaan_induk = DB::table('pem_penerimaan_induk as ppi')->leftjoin('pem_penerimaan as pp', 'ppi.id_induk', 'pp.id_induk')->where('pp.id_pembelian', $id);
    	
        $d_barang = DB::table('pem_pembelian_barang AS a')->join('pem_pembelian AS b','a.id_pembelian','b.id_pembelian')->join('ms_barang AS c','a.id_barang','c.id')->leftjoin('ms_satuan as ms', 'a.id_satuan', 'ms.id_satuan')->select('a.*','c.kode_barang','c.nama_barang', 'ms.nama_satuan')->where('a.id_pembelian',$id);


        $arr = array();
        if($data['pembelian']->status != '2'){
            $data['penerimaan_induk'] = $this->data($penerimaan_induk->first());

            foreach($d_barang->get() as $d){
                $last = DB::table('pem_penerimaan')->where('id_beli_barang', '=', $d->id_beli_barang);
                $d_last = $last->first();
                $id_terima = '';
                $jml_terima = '';
                $gudang = '';

                if($last->count() > 0){
                    $id_terima = $d_last->id_penerimaan;
                    $jml_terima = $d_last->jml_barang_terima;
                    $gudang = $d_last->id_gudang;
                }
                $arr[] = array('id_pembelian' => $d->id_pembelian,
                                'id_beli_barang' => $d->id_beli_barang,
                                'id_barang' => $d->id_barang,
                                'jml_barang' => $d->jml_barang,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang,
                                'id_penerimaan' => $id_terima,
                                'jml_terima' => $jml_terima,
                                'gudang' => $gudang,
                                'nama_satuan' => $d->nama_satuan);
            }
        }else{
            $data['penerimaan_induk'] = $this->data(array());

            foreach($d_barang->get() as $d){
                $jum = 0;
                foreach ($penerimaan_induk->get() as $p) {
                    
                    if($d->id_beli_barang == $p->id_beli_barang){
                        $jum = $d->jml_barang - $p->jml_barang_terima;
                    }else{
                        $jum = $d->jml_barang;
                    }

                }

                $arr[] = array('id_pembelian' => $d->id_pembelian,
                                'id_beli_barang' => $d->id_beli_barang,
                                'id_barang' => $d->id_barang,
                                'jml_barang' => $jum,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang,
                                'id_penerimaan' => '',
                                'jml_terima' => '',
                                'gudang' => '',
                                'nama_satuan' => $d->nama_satuan);
            }

            $arr = array_filter($arr, function ($var){
                return ($var['jml_barang'] > 0);
            });
        }

        //$data['penerimaan_induk'] = $this->data($penerimaan_induk);
        $data['gudang'] = DB::table('ms_gudang');
        $id_gudang = $this->session['id_gudang'];
        if($id_gudang != 0){
            $data['gudang']->wherein('id_gudang', explode(',', $id_gudang));
        }
        $data['gudang'] = $data['gudang']->get();
        $data['detail'] = $arr;
    	return view('inventori.PenerimaanAdd')->with('data',$data);
    }

    public function detail($id, $induk){
        $data['pembelian'] = DB::table('pem_pembelian as b')->leftjoin('ms_lokasi_produksi as mlp', 'mlp.id_lokasi', 'b.lokasi')->where('id_pembelian',$id)->select(DB::raw("b.*, mlp.nama_lokasi, mp.nama nama_penyedia"))->leftjoin('ms_penyedia as mp', 'b.id_penyedia', 'mp.id_penyedia')->first();
        
        $penerimaan_induk = DB::table('pem_penerimaan_induk as ppi')->leftjoin('pem_penerimaan as pp', 'ppi.id_induk', 'pp.id_induk')->where('pp.id_pembelian', $id)->where('ppi.id_induk', $induk);
        
        $d_barang = DB::table('pem_pembelian_barang AS a')->join('pem_pembelian AS b','a.id_pembelian','b.id_pembelian')->join('ms_barang AS c','a.id_barang','c.id')->leftjoin('ms_satuan as ms', 'a.id_satuan', 'ms.id_satuan')->select('a.*','c.kode_barang','c.nama_barang', 'ms.nama_satuan')->where('a.id_pembelian',$id);


        $arr = array();
        foreach($d_barang->get() as $d){
            $where = "pp.id_beli_barang != ''";
            if($penerimaan_induk->count() > 0){
                $where = "pp.id_beli_barang = $d->id_beli_barang AND pp.id_induk = $induk ";
            }else{
                $where = "pp.id_beli_barang = $d->id_beli_barang ";
            }

            $last = DB::table('pem_penerimaan as pp')->leftjoin('ms_gudang as mg', 'mg.id_gudang', 'pp.id_gudang')->whereRaw($where);

            $d_last = $last->first();
            $id_terima = '';
            $jml_terima = '';
            $gudang = '';
            $nama_gudang = '';

            if($last->count() > 0){
                $id_terima = $d_last->id_penerimaan;
                $jml_terima = $d_last->jml_barang_terima;
                $gudang = $d_last->id_gudang;
                $nama_gudang = $d_last->nama_gudang;
            }
            $arr[] = array('id_pembelian' => $d->id_pembelian,
                            'id_beli_barang' => $d->id_beli_barang,
                            'id_barang' => $d->id_barang,
                            'jml_barang' => $d->jml_barang,
                            'kode_barang' => $d->kode_barang,
                            'nama_barang' => $d->nama_barang,
                            'id_penerimaan' => $id_terima,
                            'jml_terima' => $jml_terima,
                            'gudang' => $gudang,
                            'nama_gudang' => $nama_gudang,
                            'nama_satuan' => $d->nama_satuan);
        }

        $arr = array_filter($arr, function ($var){
                return ($var['gudang'] != 0);
            });

        $data['penerimaan_induk'] = $this->data($penerimaan_induk->first());
        $data['detail'] = $arr;
        return view('inventori.PenerimaanDetail')->with('data',$data);
    }

    function get_gudang(){
    	$d_data = DB::table('ms_gudang');
		if($d_data->count() > 0){
			$arr['data'] = $d_data->get();
			$arr['status'] = array('1');
		}else{
			$arr['status'] = array('0');
		}

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

    public function simpan(Request $request){
    	$id_pembelian = $request->get('id_pembelian');
    	$id_induk = $request->get('id_induk');
    	
    	$tgl_diterima = $request->get('tgl_terima');
        $no_faktur = $request->get('nomor_faktur');
        $faktur = $request->get('faktur');
        $nomor_sj = $request->get('nomor_sj');
        $nopol = $request->get('nopol');
        $supir = $request->get('supir');
    	
        $status_pembelian = $request->get('status_pembelian');

    	$id_beli_barang = $request->get('id_beli_barang');
        $id_penerimaan = $request->get('id_penerimaan');
    	$jml_diterima = $request->get('jml_terima');
    	$gudang = $request->get('gudang'); 	

        if($status_pembelian != '0'){
            
            $data_pembelian = array('status'=>$status_pembelian);
            DB::table('pem_pembelian')->where('id_pembelian', $id_pembelian)->update($data_pembelian);

            if($id_induk == ''){
                $input['id_pembelian'] = $id_pembelian;
                $input['tanggal_terima'] = tgl_full($tgl_diterima, '2');
                $input['no_faktur'] = $no_faktur;
                $input['faktur'] = $faktur;
                $input['status'] = '1';
                $input['no_suratjalan'] = $nomor_sj;
                $input['nopol'] = $nopol;
                $input['nama_supir'] = $supir;
                $input['id_user'] = $this->session['kode_user'];
                $input['terima'] = tgl_full($tgl_diterima, '5');
                
                $id = DB::table("pem_penerimaan_induk")->insertGetId($input);

                for($i = 0; $i < count($id_beli_barang); $i++){
                    $data['id_induk'] = $id;
                    $data['id_pembelian'] = $id_pembelian;
                    $data['id_beli_barang'] = $id_beli_barang[$i];
                    $data['jml_barang_terima'] = $jml_diterima[$i];
                    $data['id_gudang'] = $gudang[$i];
                    $data['status'] = '1';

                    // if($data['jml_barang_terima'] == ''){
                    //     $data['jml_barang_terima'] = 0;
                    // }

                    if($id_penerimaan[$i] == ''){
                        if($data['jml_barang_terima'] > 0){
                            DB::table('pem_penerimaan')->insert($data);
                        }
                       
                    }else if($id_penerimaan[$i] != ''){
                        $where = array('id_penerimaan' => $id_penerimaan[$i]);
                        DB::table('pem_penerimaan')->where($where)->update($data);
                    }

                }
            }else{
                $input['id_pembelian'] = $id_pembelian;
                $input['tanggal_terima'] = tgl_full($tgl_diterima, '2');
                $input['no_faktur'] = $no_faktur;
                $input['faktur'] = $faktur;
                $input['status'] = '1';
                $input['no_suratjalan'] = $nomor_sj;
                $input['nopol'] = $nopol;
                $input['nama_supir'] = $supir;
                $input['id_user'] = $this->session['kode_user'];
                $input['terima'] = tgl_full($tgl_diterima, '5');

                DB::table('pem_penerimaan_induk')->where('id_induk', $id_induk)->update($input);

                for($i = 0; $i < count($id_beli_barang); $i++){
                    $data['id_induk'] = $id_induk;
                    $data['id_pembelian'] = $id_pembelian;
                    $data['id_beli_barang'] = $id_beli_barang[$i];
                    $data['jml_barang_terima'] = $jml_diterima[$i];
                    $data['id_gudang'] = $gudang[$i];
                    $data['status'] = '1';

                    if($data['jml_barang_terima'] == ''){
                        $data['jml_barang_terima'] = 0;
                    }

                    if($id_penerimaan[$i] == ''){
                        DB::table('pem_penerimaan')->insert($data);
                       
                    }else if($id_penerimaan[$i] != ''){
                        $where = array('id_penerimaan' => $id_penerimaan[$i]);
                        DB::table('pem_penerimaan')->where($where)->update($data);
                    }
                }
                
            }

            
        }

       $id_produksi = DB::table('pem_pembelian_barang as ppb')->leftjoin('ms_barang as mb', 'ppb.id_barang', 'mb.id')->join('ms_kategori_barang as mkb', 'mb.id_kb', 'mkb.id_kb')->join('ms_kategori_barang as mkb2', 'mkb.induk_kb', 'mkb2.id_kb')->where('ppb.id_pembelian', $id_pembelian)->wherein('mkb2.id_kb', [5, 17, 27])->select(DB::raw('ppb.id_pembelian'))->groupby('ppb.id_pembelian');

        if($id_produksi->count() > 0){
            return redirect('penerimaan');
        }else{
            return redirect('penerimaan_nonproduksi');
        }

        
    }

    function get_status($id){
        switch ($id) {
            case '0':
                return "<label type=button' class='label label-sm bg-orange '> Belum Diterima </label>";
                break;
            case '1':
                return "<label type=button' class='label label-sm bg-green '> Sudah Diterima </label>";
                break;
            case '2':
                return "<label type=button' class='label label-sm bg-orange '> Belum Diterima Sebagian </label>";
                break;
            default:
                return "<label type=button' class='label label-sm bg-green '> Sudah Diterima </label>";
                break;
        }
    }

    function aksi($jenis, $status, $id_pembelian, $induk){
        $d_status = DB::table('pem_penerimaan')->where(['id_pembelian' => $id_pembelian, 'id_induk' => $induk])->pluck('status')->toArray();
        switch ($jenis) {
            case '0':

                if($status == '0'){
                    $aksi = '<div class="btn-group"><a class="btn btn-success  btn-sm" href="'.url('penerimaan/cek/').'/'.$id_pembelian.'" ><i class="icon-checkbox-checked2"></i></a> </div>';    
                }else if($status = '2'){
                    $aksi = '<div class="btn-group"><a class="btn btn-success  btn-sm" href="'.url('penerimaan/cek/').'/'.$id_pembelian. '" ><i class="icon-checkbox-checked2"></i></a> </div>';    
                }else{
                    $aksi = '';
                }
                
                break;
            case '1':
                $aksi = '<div class="btn-group"><a class="btn btn-info  btn-sm" href="'.url('penerimaan/detail/').'/'.$id_pembelian.'/'.$induk.'" ><i class="icon-checkbox-checked2"></i></a> <a class="btn btn-warning btn-sm" href="'.url('penerimaan/edit/').'/'.$id_pembelian.'/'.$induk.'" ><i class="icon-pencil"></i></a> <a class="btn btn-sm btn-success" type="button" onclick="cetak($(this))" link_data="'.url('penerimaan/cetak/').'" idbeli="'.$id_pembelian.'" induk="'.$induk.'"><i class="icon-printer"></i></a>';

                $hapus = '<button class="btn bg-danger-800" type="button" onclick="hapus($(this))" idbeli="'.$id_pembelian.'" induk="'.$induk.'"><i class="icon-eraser"></i></button>';
                if($status == 0){
                    if(in_array(1, $d_status)){
                        $aksi = $aksi;
                    }else{
                        $aksi = $aksi.$hapus;
                    }
                }
                else if($status == 1){
                    $aksi = $aksi;
                    //<a class="btn bg-success-400 btn-sm" type="button" target="_blank" href="'.url('penerimaan/cetak').'/'.$id_pembelian.'/'.$induk.'"><i class="icon-printer"></i>
                }else{
                    $aksi = $aksi;
                }
                
                $aksi = $aksi."</div>";

                break;
            default:
                $aksi = '';
                break;
        }

        return $aksi;
    }

    function data($data = array()){
        if(count($data) > 0){
            $store['id_induk'] = $data->id_induk;
            $store['id_pembelian'] = $data->id_pembelian;
            $store['tanggal_terima'] = $data->tanggal_terima;
            $store['no_faktur'] = $data->no_faktur;
            $store['faktur'] = $data->faktur;
            $store['status'] = $data->status;
            $store['nopol'] = $data->nopol;
            $store['nama_supir'] = $data->nama_supir;
            $store['no_suratjalan'] = $data->no_suratjalan;
        }else{
            $store['id_induk'] = '';
            $store['id_pembelian'] = '';
            $store['tanggal_terima'] = '';
            $store['no_faktur'] = '';
            $store['faktur'] = '';
            $store['status'] = '';
            $store['nopol'] = '';
            $store['nama_supir'] = '';
            $store['no_suratjalan'] = '';
        }

        return $store;
    }

    public function cetak(Request $request){
        
        if($_POST){

            $data = $request->all();

            $id_pembelian = $data['popup_id'];
            $id_induk = $data['popup_induk'];
            $ukuran = $data['popup_ukuran'];
            
            $where = "pp.id_pembelian = $id_pembelian ";
            if($id_induk != ''){
                $where .= "AND ppi.id_induk = $id_induk";
            }


            $data['data'] = DB::table('pem_pembelian as pp')->leftjoin('pem_penerimaan_induk as ppi', 'pp.id_pembelian', 'ppi.id_pembelian')->join('ms_lokasi_produksi as mlp', 'pp.lokasi', 'mlp.id_lokasi')->leftjoin('ms_penyedia as mp', 'pp.id_penyedia', 'mp.id_penyedia')->select(DB::raw("pp.*, mlp.nama_lokasi, mp.nama nama_penyedia, ppi.tanggal_terima, ppi.no_faktur no_faktur_terima, ppi.faktur faktur_terima, ppi.nopol, ppi.nama_supir, ppi.no_suratjalan"))->whereRaw($where)->first();

            
            $data['detail'] =DB::table('view_edit_penerimaan as v')->leftjoin('pem_penerimaan as pp', 'v.id_penerimaan', 'pp.id_penerimaan')->where('id', '=', $id_pembelian)->where('pp.status', '1')->select(DB::raw("v.*, pp.id_induk, pp.status"))->orderby('id_beli_barang', 'asc');
            if($id_induk != ''){
                $data['detail'] = $data['detail']->where('pp.id_induk', $id_induk);
            }

            $data['detail'] = $data['detail']->get();

            if($ukuran == '0'){ //kecil
                $view = "PenerimaanbarangkecilCetak";
            }else{
                $view = "PenerimaanbarangCetak";
            }
            
            require('fpdf181/Mc_table.php');
            $html = view('inventori.cetak.'.$view.'')->with('data', $data);
            return response($html)->header('Content-Type', 'application/pdf');

        //    print_r($ukuran);
        }else{
            return redirect('penerimaan');
        }


        
    }

    function get_faktur(Request $request){
        $data = $request->all();
        $arr = array();

    //    $d_pembelian = DB::table('pem_pembelian')->where('id_pembelian', $data['id'])->first();

        if($data['id'] != ''){
            $d_data = DB::table('pem_penerimaan_induk')->where('id_induk', $data['id'])->first();
            $arr = array('no_faktur' => sprintf('%03d', $d_data->no_faktur),
                         'faktur' => $d_data->faktur);
        }else{
            $tgl = explode('-', $data['tgl']);
            $no = 1;
            $d_data = DB::table('pem_penerimaan_induk')->whereRaw("YEAR(tanggal_terima) = $tgl[2] AND MONTH(tanggal_terima) = $tgl[1]")->select(DB::raw("MAX(no_faktur) as no_faktur"))->first();
            if($d_data->no_faktur == 0){
                $no = 1;
            }else{
                $no = $d_data->no_faktur + 1;
            }

            $arr = array('no_faktur' => sprintf('%03d', $no),
                        'faktur' => '/'.$tgl[1].'/'.$tgl[2]);
        }

        return response()->json($arr);
    }
    
    function get_detail(Request $request){
        $data = $request->all();
        $idbeli = $data['id_beli'];
        $induk = $data['induk'];

        //$data['data'] = DB::table('pem_penerimaan_induk as ppi')->leftjoin('pem_pembelian as pp', 'ppi.id_pembelian', 'pp.id_pembelian')->leftjoin('ms_penyedia as mp', 'pp.id_penyedia', 'mp.id_penyedia')->leftjoin('ms_lokasi_produksi as mlp', 'pp.lokasi', 'mlp.id_lokasi')->where(['ppi.id_induk' => $induk, 'ppi.id_pembelian' => $idbeli])->select(DB::raw('ppi.*, pp.no_faktur no_faktur_pembelian, pp.sufix_no_faktur faktur_pembelian, DATE_FORMAT(pp.tanggal_faktur, "%d-%m-%Y") as tanggal_faktur, mp.nama nama_penyedia, mlp.nama_lokasi'));

        $where = "pp.id_pembelian = $idbeli ";
        if($induk != '0'){
            $where .= "AND ppi.id_induk = $induk";
        }


        $d_data = DB::table('pem_pembelian as pp')->leftjoin('pem_penerimaan_induk as ppi', 'pp.id_pembelian', 'ppi.id_pembelian')->join('ms_lokasi_produksi as mlp', 'pp.lokasi', 'mlp.id_lokasi')->leftjoin('ms_penyedia as mp', 'pp.id_penyedia', 'mp.id_penyedia')->select(DB::raw("pp.*, DATE_FORMAT(pp.tanggal_faktur, '%d-%m-%Y') as tanggal_faktur, mlp.nama_lokasi, mp.nama nama_penyedia, ppi.tanggal_terima, ppi.no_faktur no_faktur_terima, ppi.faktur faktur_terima, ppi.id_induk"))->whereRaw($where);

        $arr = array();

        if($d_data->count() > 0){
            $arr['data'] = $d_data->first();
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

        return $arr;
    }


    function redirect_terima($id_pembelian){
        $id_produksi = DB::table('pem_pembelian_barang as ppb')->leftjoin('ms_barang as mb', 'ppb.id_barang', 'mb.id')->join('ms_kategori_barang as mkb', 'mb.id_kb', 'mkb.id_kb')->join('ms_kategori_barang as mkb2', 'mkb.induk_kb', 'mkb2.id_kb')->where('ppb.id_pembelian', $id_pembelian)->wherein('mkb2.id_kb', [5, 17, 27])->select(DB::raw('ppb.id_pembelian'))->groupby('ppb.id_pembelian');

        if($id_produksi->count() > 0){
            return redirect('penerimaan');
        }else{
            return redirect('penerimaan_nonproduksi');
        }
    }

    function get_detail_terima(Request $request){
        $data = $request->all();
        $idbeli = $data['id_beli'];
        $induk = $data['induk'];
        
        $where = "pp.id_pembelian = $idbeli ";
        if($induk != '0'){
            $where .= "AND pp.id_induk = $induk";
        }

        $d_data = DB::table("pem_penerimaan as pp")->join("pem_pembelian_barang as ppb", "pp.id_beli_barang", "ppb.id_beli_barang")->join('ms_gudang as mg', 'mg.id_gudang', 'pp.id_gudang')->join('ms_barang as mb', 'ppb.id_barang', 'mb.id')->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', 'ms.id_satuan', 'ppb.id_satuan')->select(DB::raw("pp.*, mb.nama_barang, mg.nama_gudang, mkb2.nama nama_kategori, mkb.nama nama_sub, ms.nama_satuan"))->whereRaw($where);

        $arr = array();

        if($d_data->count() > 0){
            foreach($d_data->get() as $d){
                $arr['data'][] = array('id_penerimaan' => $d->id_penerimaan,
                                    'id_pembelian' => $d->id_pembelian,
                                    'jml_barang_terima' => $d->jml_barang_terima,
                                    'status_id' => $d->status,
                                    'nama_gudang' => $d->nama_gudang,
                                    'nama_barang' => $d->nama_kategori." - ".$d->nama_sub." - ".$d->nama_barang,
                                    'nama_kategori' => $d->nama_kategori,
                                    'nama_sub' => $d->nama_sub,
                                    'nama_satuan' => $d->nama_satuan,
                                    'status' => status_terima_barang($d->status),
                                    'aksi' => $this->aksi_terima($d->status, $d->id_penerimaan));
            }
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

        return $arr;
    }

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

        $id = $req['id'];
        $status = $req['status'];
        $jenis = $req['jenis'];
    
        if($status == 0){
            $d = $this->get_barangstok($id);


            if(in_array(0, $d)){
                if($jenis == 0){
                    $stat = array('status' => '0');
                }else{
                    $d_data = DB::table('pem_penerimaan')->where('id_penerimaan', $id)->update(['status' => $status]);
                    $this->rubah_terima_induk($id);
                    $stat = array('status' => '1');
                }
            }else{
                $d_data = DB::table('pem_penerimaan')->where('id_penerimaan', $id)->update(['status' => $status]);
                $this->rubah_terima_induk($id);
                $stat = array('status' => '1');
            }  
        }else{
            $d_data = DB::table('pem_penerimaan')->where('id_penerimaan', $id)->update(['status' => $status]);
            $this->rubah_terima_induk($id);
            $stat = array('status' => '1');
        }

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

    function get_barangstok($id){
        $d_rubah = DB::table('view_edit_penerimaan')->where('id_penerimaan', $id);
        $jum = array();

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

                $jumlah = $d->jumlah;

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

                $PDO = DB::connection('mysql')->getPdo();
                $billingStmt = $PDO->prepare("SELECT B.*
                                             FROM (
                                                 ".base_stok()."
                                             ) B
                                             $where ");
            
                $billingStmt->execute();
                $usersBills = $billingStmt->fetchAll((\PDO::FETCH_ASSOC));

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


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

        return $jum;
    }

    function rubah_terima_induk($id){
        $id_pembelian = DB::table('pem_penerimaan')->where('id_penerimaan', $id)->first();
        
        if($id_pembelian->id_induk != '0'){
            $cek_status = DB::table('pem_penerimaan')->where('id_pembelian', $id_pembelian->id_pembelian)->where('id_penerimaan', '!=', $id)->where('id_induk', $id_pembelian->id_induk);
        }else{
            //$cek_status = DB::table('pem_penerimaan')->where('id_pembelian', $id_pembelian->id_pembelian)->where('id_penerimaan', '!=', $id);
        }


        $cek_pembelian = DB::table('pem_pembelian')->where('id_pembelian', $id_pembelian->id_pembelian)->first();

        if($cek_status->count() > 0){
            $id_induk = $cek_status->first()->id_induk;
        }else{
            $id_induk = DB::table('pem_penerimaan')->where('id_pembelian', $id_pembelian->id_pembelian)->where('id_penerimaan', $id)->first()->id_induk;  
        }

        $arr_status = DB::table('pem_penerimaan')->where('id_pembelian', $id_pembelian->id_pembelian)->where('id_induk', $id_induk);

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

        if($cek_status->count() > 0){

            
            
            // echo "Ada";
            if($cek_status->first()->id_induk != 0){
                
                $induk = DB::table('pem_penerimaan')->where('id_pembelian', $id_pembelian->id_pembelian)->where('id_penerimaan', $id)->first()->id_induk;

                if(in_array(0, $arr_status->pluck('status')->toArray())){
                    DB::table('pem_penerimaan_induk')->where('id_induk', $induk)->update($status_0);
                }else{
                    DB::table('pem_penerimaan_induk')->where('id_induk', $induk)->update($status_1);
                }
            }else{

            }
        }else{
            if($id_induk != 0){
                
                $induk = DB::table('pem_penerimaan')->where('id_pembelian', $id_pembelian->id_pembelian)->where('id_penerimaan', $id)->first()->id_induk;

                // print_r($arr_status->pluck('status')->toArray());
                // print_r("\n");
                // print_r($induk);

                if(in_array(0, $arr_status->pluck('status')->toArray())){
                    DB::table('pem_penerimaan_induk')->where('id_induk', $induk)->update($status_0);
                }else{
                    DB::table('pem_penerimaan_induk')->where('id_induk', $induk)->update($status_1);
                }
            }
        }
    }

    function aksi_terima($status, $terima){
        // $d_terima = DB::table('pem_penerimaan')->where('id_penerimaan', $terima)->first();
        // $c_terima = DB::table('pem_penerimaan')->where(['id_pembelian' => $d_terima->id_pembelian, 'id_induk' => $d_terima->id_induk ]);


        switch ($status) {
            case '0':
                $aksi = '<div class="btn-group"><a class="btn btn-success btn-sm" onclick="verif($(this))" id_terima="'.$terima.'" status="1"><i class="icon-cart-add2"></i></a>';
                // if($c_terima->where('status', '1')->count() > 0){
                //     $aksi = $aksi.'<button class="btn bg-danger-800" type="button" onclick="hapus($(this))" idbeli="'.$terima.'" induk="'.$induk.'"><i class="icon-eraser"></i></button>';
                // }

                $aksi = $aksi.'</div>';

                break;
            case '1':
                $aksi = '<div class="btn-group"><a class="btn btn-warning btn-sm" onclick="verif($(this))" id_terima="'.$terima.'" status="0"><i class="icon-cart-remove"></i></a> </div>';
                break;
            default:
                $aksi = '';
                break;
        }

        return $aksi;
    }

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

        $id_beli = $req['idbeli'];
        $induk = $req['induk'];

        $status_0 = array('status' => '0');
        $status_1 = array('status' => '1');
        $status_2 = array('status' => '2');

        $d_beli = DB::table('pem_pembelian')->where('id_pembelian', $id_beli);
        $d_terima = DB::table('pem_penerimaan')->where('id_pembelian', $id_beli)->where('id_induk', '!=', $induk);

        if($d_terima->count() > 0){
            $d_beli->update($status_2);
        }else{
            $d_beli->update($status_0);
        }

        DB::table('pem_penerimaan')->where(array('id_pembelian' => $id_beli, 'id_induk' => $induk))->delete();
        if($induk != 0){
            DB::table('pem_penerimaan_induk')->where(array('id_pembelian' => $id_beli, 'id_induk' => $induk))->delete();
            DB::table('pem_penerimaan')->where(array('id_pembelian' => $id_beli, 'id_induk' => $induk))->delete();
        }else{
            DB::table('pem_penerimaan')->where(array('id_pembelian' => $id_beli, 'id_induk' => $induk))->delete();
        }

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

}
