<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use PDF;

class DepotController extends Controller
{
    //

    function depot()
    {
        /*if(session('user')== null) {
            return redirect('/');
        }*/
		$depot = DB::table('depot')->orderBydesc('id')->get();
	return view('depot/nouveau', [
		'depot' => $depot,
	]);
    }
	
     function modifier($id) {
        if(session('user')== null) {
            return redirect('/');
        }
        $depot=DB::table('depot')->get();
        $depot1 = DB::table('depot')->where('id', $id)->first();
        return view('depot/modifier',[
            'depot' => $depot,
            'depot1' => $depot1,
        ]);
    }

    function enregistrer(Request $request)
    {

        $nom=request('nom');
        $date=date('Y-m-d');

        $verification= DB::table('depot')->where('nom',$nom)->first();
        if($verification){
            $message = [
                "title" => 'Error',
                "type" => 'error',
                "response" => 'Cet enregistrement existe déjà',
            ];

            return $message;
        }
        else {
            DB::table('depot')->insert(
                [
                    'nom' => $nom,
                    'date' => $date,
                    'user_id' => session('user')->id,
                ]
            );
			
			$message = [
                "title" => 'success',
                "type" => 'success',
                "response" => 'Enregistrement effectué avec succès !',
            ];
            return $message;

		}

    }
    
function valider(Request $request, $id)
    {

			$nom = $request->nom;
			$today = date('Y-m-d');
			
			DB::table('depot')->where('id',$id)->update(
            [
						'nom' => $nom,
						'date' => $today,	
                    ]
				);
				
			$message = [
					"title" => 'Succès',
					"type" => 'success',
					"response" => 'Modification succès !',
				];
			return $message;
			   
	}

    function Voir ($id) {
        $emp= DB::table('magasin')->where('id',$id)->first();
        return [
            'emp' => $emp,
        ];
    }
	
	
	//STOCK GENERAL MAGASIN
	function stockmagasin(){

        if(session('user')== null) {
            return redirect('/');
        }
	$produits = DB::table('produit_vendable')->get();
			return view('magasin/stock_magasin',[
            'produits' => $produits,
        ]);	
	}
	function listee(){

        if(session('user')== null) {
            return redirect('/');
        }
	$produits = DB::table('produit_vendable')->where('stock_magasin','>',0)->get();
			return view('magasin/stock',[
            'produits' => $produits,
        ]);	
	}
	function listeee(){

        if(session('user')== null) {
            return redirect('/');
        }
	$produits = DB::table('produit_vendable')->get();
	$pdf = PDF::loadView('magasin/stockPDF', [
			'produits' => $produits,
		]);
        return $pdf->stream();
	}
	//RECHERCHER MOUVEMENTS MAGASIN
	public function mouvementPost()
	{
        if(session('user')== null) {
            return redirect('/');
        }
		$id = session('id');
		$date_debut = request('date_debut');
        $date_debut= explode('/', $date_debut);
        $date_debut= array_reverse($date_debut);
        $date_debut= implode('-', $date_debut);

        $date_fin = request('date_fin');
        $date_fin= explode('/', $date_fin);
        $date_fin= array_reverse($date_fin);
        $date_fin= implode('-', $date_fin);
		
		session(['date_debut'=> $date_debut]);
		session(['date_fin'=> $date_fin]);
		
		if($date_fin > $date_debut || $date_fin == $date_debut){
			$detail=DB::table('magasin_general')->where('produit_id',$id)->whereBetween('date', [$date_debut,$date_fin])->get();
			return view('magasin/detail-mouvement-magasin-rech', [
			'detail'=> $detail,
			'date_debut'=> $date_debut,
			'date_fin'=> $date_fin,
		]);
	}else {
			return redirect()->route('stock-general-magasin')->with('error', 'La date de fin ne doit pas dépasser la date de début!');
	}}
	public function mouvementPDF()
	{
        if(session('user')== null) {
            return redirect('/');
        }
		$id = session('id');
		$date_debut = session('date_debut');
		$date_fin = session('date_fin');
		$magasin = session('magasin');
		
		 if($date_fin > $date_debut || $date_fin == $date_debut){

		$detail=DB::table('magasin_general')->where('produit_id',$id)->whereBetween('date', [$date_debut,$date_fin])->get();
		$pdf = PDF::loadView('magasin/detail-mouvement-magasin-pdf', [
			'detail'=> $detail,
			'date_debut'=> $date_debut,
			'date_fin'=> $date_fin,
			'magasin'=> $magasin,
		]);
		
		return $pdf->stream();
	}}
	function magasinPost(){

        if(session('user')== null) {
            return redirect('/');
        }
		$date_debut = request('date_debut');
		$date_debut= explode('/', $date_debut);
        $date_debut= array_reverse($date_debut);
        $date_debut= implode('-', $date_debut);

        $date_fin = request('date_fin');
        $date_fin= explode('/', $date_fin);
        $date_fin= array_reverse($date_fin);
        $date_fin= implode('-', $date_fin);
		
		session(['date_debut'=> $date_debut]);
		session(['date_fin'=> $date_fin]);
		
		 if($date_fin > $date_debut || $date_fin == $date_debut){
			$produits = DB::table('produit_vendable')->get();
			$totappro = DB::table('ligne_appro_vendable')->where('etat', 'Validée')->whereBetween('date', [$date_debut,$date_fin])->get()->sum('qte');
			$qtevendu = DB::table('ligne_transfert')->where('etat', 'Validé')->whereBetween('date', [$date_debut,$date_fin])->get()->sum('qte');
			return view('magasin/stock-general-rech',[
            'produits' => $produits,
            'totappro' => $totappro,
            'qtevendu' => $qtevendu,
            'date_debut' => $date_debut,
            'date_fin' => $date_fin,
        ]);	
	} else {
			return redirect()->route('stock-general-magasin')->with('error', 'La date de fin ne doit pas dépasser la date de début!');
	}
	}
	//MAGASIN POST PDF
	function magasinpostpdf(){

        if(session('user')== null) {
            return redirect('/');
        }
        $date_debut = session('date_debut');
        $date_fin = session('date_fin');
		$produits = DB::table('produit_vendable')->get();
		$totappro = DB::table('ligne_appro_vendable')->where('etat', 'Validée')->whereBetween('date', [$date_debut,$date_fin])->get()->sum('qte');
		$qtevendu = DB::table('ligne_transfert')->where('etat', 'Validé')->whereBetween('date', [$date_debut,$date_fin])->get()->sum('qte');
			$pdf = PDF::loadView('magasin/stock-general-rech-pdf', [
            'produits' => $produits,
            'totappro' => $totappro,
            'qtevendu' => $qtevendu,
            'date_debut' => $date_debut,
            'date_fin' => $date_fin,

        ]);	
		return $pdf->stream();
	}
}
