<?php

namespace App\Http\Controllers\laporan;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use DB;
use Illuminate\Http\Request;
use App\LokasiProduksi;

use Yajra\Datatables\Datatables;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Session;

class PerKategoriController 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');
         }
         
        return $next($request);	
        });
    }

public function perkategori(Request $request){
		$input = $request->all();		
        $data['lokasi'] = LokasiProduksi::get();
        $data['lokasiTerpilih']='';
        $data['kategori']=DB::table("ms_kategori_barang")->where('induk_kb','=','0')->get();
        $data['kategoriTerpilih']='';
        $data['label']=array('0' => 'Non CV', '1'=>'CV' );
		$data['jurnal_umum'] = [];
		if(sizeof($input) > 0){
			$tanggal_awal = implode("/", array_reverse(explode("-", $input['tanggal_awal'])));
			$tanggal_akhir = implode("/", array_reverse(explode("-", $input['tanggal_akhir'])));
			$data['jurnal_umum'] = $this->_set_data_jurnal($tanggal_awal, $tanggal_akhir);
		}
		return view('laporan.PerKategori',$data);
	}
	public function getdata($id){
		$data=DB::table("ms_kategori_barang")->where("induk_kb","<>","0")->where("induk_kb","=",$id)->get();
		$arr['data']=$data;
        return response()->json($arr);
	}
	public function cetakPerKategori(Request $request){
		$lokasi=$request->get('lokasi');
		$kategori=$request->get('kategori');
		$cv=$request->get('cv');
		$sub_kategori=$request->get('sub_kategori');

		$originalDate1 = $request->get('tanggal_awal');
		$originalDate2 = $request->get('tanggal_akhir');
		$tgl_awal=date("Y-m-d", strtotime($originalDate1));
		$tgl_akhir=date("Y-m-d", strtotime($originalDate2));;

        // DB::enableQueryLog();
		$d_data=DB::table('pem_pembelian_barang as ppb')->leftjoin("ms_barang as mb","mb.id","ppb.id_barang")->leftjoin("ms_kategori_barang as mkb","mkb.id_kb","mb.id_kb")->select(DB::raw("mb.nama_barang,ms.nama_satuan,mb.kode_barang,ppb.*"))->leftjoin("ms_satuan as ms","mb.id_satuan","ms.id_satuan")->where("mkb.id_kb","=",$sub_kategori)->where(DB::raw('date(ppb.created_at)'),'>=',$tgl_awal)->where(DB::raw('date(ppb.created_at)'),'<=',$tgl_akhir)->get();
		$arr['query']=$d_data->toArray();
		// dd(DB::getQueryLog());
		$arr['data_tanggal']=array(
			'tanggal_awal' =>tgl_full($request->get('tanggal_awal'),0) ,
			'tanggal_akhir'=>tgl_full($request->get('tanggal_akhir'),0) 
		);
		// echo "<pre>";print_r($arr);exit();


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


	}
}