<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use PDF;
use Validator;
class MagasinController extends Controller
{
    //
	public function consulter3($id)
	{
        if(session('user')== null) {
            return redirect('/');
        }
		session(['id'=> $id]);
		$detail=DB::table('magasin_general')->where('produit_id',$id)->orderBydesc('id')->get();
		$magasin=DB::table('magasin')->orderBydesc('id')->get();

		return view('magasin/detail-mouvement-magasin', [
			'detail'=> $detail,
			'magasin'=> $magasin,
		]);
	}
	public function consulter33($id)
	{
        if(session('user')== null) {
            return redirect('/');
        }
		session(['id'=> $id]);
		$depot_id = session('depot_id');
		$detail=DB::table('magasin_general')->where('produit_id',$id)->where('depot_id',$depot_id)->orderBydesc('id')->get();

		return view('magasin/detail-mouvement-depot', [
			'detail'=> $detail,
			'depot_id'=> $depot_id,
		]);
	}
    function magasin()
    {
        /*if(session('user')== null) {
            return redirect('/');
        }*/
		$magasin = DB::table('magasin')->orderBydesc('id')->get();
	return view('magasin/nouveau', [
		'magasin' => $magasin,
	]);
    }
    function liste() {
        /*if(session('user')== null) {
            return redirect('/');
        }*/
        $magasin = DB::table('magasin')->orderBydesc('id')->get();
        return view('magasin/liste',[
            'magasin' => $magasin,
        ]);
    }
	
    function modifier($id) {
        if(session('user')== null) {
            return redirect('/');
        }
        $magasin=DB::table('magasin')->get();
        $magasin1 = DB::table('magasin')->where('id', $id)->first();
        return view('magasin/modifier',[
            'magasin' => $magasin,
            'magasin1' => $magasin1,
        ]);
    }
	function modifierlogo($id) {
        if(session('user')== null) {
            return redirect('/');
        }
        $magasin=DB::table('magasin')->get();
        $magasin1 = DB::table('magasin')->where('id', $id)->first();
        return view('magasin/modifier-logo',[
            'magasin' => $magasin,
            'magasin1' => $magasin1,
        ]);
    }
	public function enregistrer(Request $request)
	{
		
			$nom = $request->nom;
			$email = $request->email;
			$adresse = $request->adresse;
			$telephone = $request->telephone;
			
			$today = date('Y-m-d');
			$today2 = date('Y-m-d H:i:s');
			$verif = DB::table('magasin')->get()->count();
			if ($verif ==3){
				$message = [
					"title" => 'Attention',
					"type" => 'error',
					"response" => 'Votre boutique a déjà été créée',
				];
			return $message;
			} else {
			DB::table('magasin')->insert(
            [
						'nom' => $nom,
						'adresse' => $adresse,
						'email' => $email,
						'telephone' => $telephone,
						'date' => $today,	
                    ]
				);
				$message = [
					"title" => 'Succès !',
					"type" => 'success',
					"response" => "Succès de l'enregistrement",
				];
			return $message;
			//return redirect()->route('succes-demande')->with('status', 'Succès de l\'opération ! Votre demande a bien été enregistrée!; Vous serez averti par mail lorsque votre demande traitée et prise en compte.');
			   
			}
		}

    function valider(Request $request, $id)
    {

			$nom = $request->nom;
			$email = $request->email;
			$adresse = $request->adresse;
			$telephone = $request->telephone;
			
			$today = date('Y-m-d');
			$today2 = date('Y-m-d H:i:s');
			
			DB::table('magasin')->where('id',$id)->update(
            [
						'nom' => $nom,
						'adresse' => $adresse,
						'email' => $email,
						'telephone' => $telephone,
						'date' => $today,	
                    ]
				);
				
			$message = [
					"title" => 'Succès',
					"type" => 'success',
					"response" => 'Modification succès !',
				];
			return $message;
			   
	}
	public function validerlogo(Request $request, $id)
	{
			$validation = Validator::make($request->all(),[
			'file' => 'required|mimes:png,jpg,jpeg',
		],
		[
			'file.required'=> '- Vous devez choisir un fichier document(png,jpg,jpeg)<br/>',
		]
		);
			
		    if ($validation->passes()){
			$image = $request->file('file');
			$filename = $image->getClientOriginalName();
			
			$today = date('Y-m-d');
			$today2 = date('Y-m-d H:i:s');
			
			DB::table('magasin')->where('id',$id)->update(
            [
						'logo_name' => $filename,
						'logo_fichier' => $image->storeAs('Logo', time(). '_'.$filename, 'public'),
						//'date' => $today,	
                    ]
				);
			$message = [
					"title" => 'Succès',
					"type" => 'success',
					"response" => 'Modification succès !',
				];
			return $message;
		}
		
		else{
			$message = [
					"title" => 'Attention',
					"type" => 'error',
					"response" => $validation->errors()->all(),
				];
			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();
	$depot = DB::table('depot')->orderBy('nom','asc')->get();
			return view('magasin/stock_magasin',[
            'produits' => $produits,
            'depot' => $depot,
        ]);	
	}
	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('/');
        }
        $depot_id = request('depot_id');
        $date_debut = request('date_debut');
        $date_fin = request('date_fin');
        if ($date_debut !="" && $date_fin !=""){
            if ($date_fin < $date_debut){
                return redirect()->route('stock-general-magasin')->with('error', 'La date de fin ne doit pas dépasser la date de début!');
            }else{
                    
            	
            		$date_debut= explode('/', $date_debut);
                    $date_debut= array_reverse($date_debut);
                    $date_debut= implode('-', $date_debut);
            
                    $date_fin= explode('/', $date_fin);
                    $date_fin= array_reverse($date_fin);
                    $date_fin= implode('-', $date_fin);
                    $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');
            }
        } elseif ($date_debut =="" && $date_fin =="") {
            $date_debut = NULL;
            $date_fin= NULL;
            $totappro = DB::table('ligne_appro_vendable')->where('etat', 'Validée')->get()->sum('qte');
			$qtevendu = DB::table('ligne_transfert')->where('etat', 'Validé')->get()->sum('qte');
        } else {
            return redirect()->route('stock-general-magasin')->with('error', 'Tous les champs date doivent être renseignés ou laissés vides');
        }
       
		
		session(['date_debut'=> $date_debut]);
		session(['date_fin'=> $date_fin]);
		session(['depot_id'=> $depot_id]);
		
			$produits = DB::table('produit_vendable')->orderBy('designation','asc')->get();
			$depot = DB::table('depot')->orderBy('nom','asc')->get();
			return view('magasin/stock-general-rech',[
            'produits' => $produits,
            'totappro' => $totappro,
            'qtevendu' => $qtevendu,
            'date_debut' => $date_debut,
            'date_fin' => $date_fin,
            'depot_id' => $depot_id,
            'depot' => $depot,
        ]);
	}
	//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();
	}
}
