<?php

namespace App\Http\Controllers;

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

class DepenseController extends Controller
{
    public function voir(){
      
        if(session('user')== null) {
            return redirect('/');
        }
		if(session('user')->roles == "Admin"){
		$depense= DB::table('caisse_depense')->get();	
		} else {
		$depense= DB::table('caisse_depense')->where('user_id', session('user')->id)->where('etat', 'En cours')->orderBydesc('date')->get();	
		}
		$typedepense = DB::table('typedepense')->orderBydesc('id')->get();
		$magasin = DB::table('magasin')->get();
		return view('depense/nouveau',[
		'depense' => $depense,
		'typedepense' => $typedepense,
		'magasin' => $magasin,
	]);
	}
	
	
//ENREGISTREMENT DEPENSE
	function enregistrer3(Request $request)
    {
        $montant=request('montant');
        $datedep=implode('-', array_reverse(explode('/', request('datedep'))));
        $note=request('note');
        $today=date('Y-m-d');
		
		DB::table('caisse_depense')->insert(
			[
				'date' => $datedep,
				'montant' => $montant,
				'note' => $note,
				'user_id' => session('user')->id,
				'magasin_id' => session('user')->magasin_id,
			]
		   );
		$message = [
                "title" => 'success',
                "type" => 'success',
                "response" => 'Enregistrement succès !',
            ];
            return $message;
			 
    }
	
	public function voirdep($id){
		$depense= DB::table('caisse_depense')->where('id', $id)->first();
		return [
		'depense' => $depense,
	];
	}
	public function suppdep(){
		$id = request('id');
		DB::table('caisse_depense')->where('id', $id)->delete();
		return redirect()->route('depense')->with('status', 'Suppression effectuée avec succès');
	}
	public function rechdepensepost(){
      
        if(session('user')== null) {
            return redirect('/');
        }
		$magasin_id= request('magasin_id');
		$date_debut= implode('-', array_reverse(explode('/', request('date_debut'))));
		$date_fin= implode('-', array_reverse(explode('/', request('date_fin'))));
		$magasin = DB::table('magasin')->get();
		if($date_fin > $date_debut || $date_fin == $date_debut){
		$depense= DB::table('caisse_depense')->where('magasin_id', $magasin_id)->whereBetween('date', [$date_debut,$date_fin])->orderBydesc('date')->get();
		$tot = DB::table('caisse_depense')->where('magasin_id', $magasin_id)->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		return view('depense/depenserech',[
		'depense' => $depense,
		'magasin' => $magasin,
		'mag_id' => $magasin_id,
		'date_debut' => $date_debut,
		'date_fin' => $date_fin,
		'tot' => $tot,
	]);
	} else{
		return redirect()->route('depense')->with('error', 'La date de début doit être suppérieure à la date finale sélectionnée');
	}}
	
	//TYPE DEPENSE
	
	function typedep() {

        if(session('user')== null) {
            return redirect('/');
        }

        $typedepense = DB::table('typedepense')->orderBydesc('id')->get();

        return view('depense/typedepense',[

            'typedepense' => $typedepense,

        ]);
    }
	function mtypedep($id) {

        if(session('user')== null) {
            return redirect('/');
        }

        $typedepense = DB::table('typedepense')->orderBydesc('id')->get();

        $typedep1 = DB::table('typedepense')->where('id', $id)->first(); 

        return view('depense/modifier-typedepense',[

            'typedepense' => $typedepense,
            'typedep1' => $typedep1,

        ]);
    }
    function mtypedepost (Request $request, $id){

        $lib= request("lib");
              
        $veri = DB::table('typedepense')->where('libelle', $lib)->first();

            if($veri){
                $message = [
                    "title" => 'Attention',
                    "type" => 'error',
                    "response" => 'Cet enregistrement existe déjà !',
                ];
                return $message;
            }
            else{
                    DB::table('typedepense')->where('id', $id)->update(
    
                        [   
                            'libelle' => $lib,
                        ]
                    );
        
                $message = [
                    "title" => 'success',
                    "type" => 'success',
                    "response" => 'Modification effectuée avec succès !',
                ];
        
                return $message; 
            } 

    }
    
    function enregistrertypedep (Request $request){

        $lib= request("lib");
              
        $veri = DB::table('typedepense')->where('libelle', $lib)->first();

            if($veri){
                $message = [
                    "title" => 'Error',
                    "type" => 'error',
                    "response" => 'Cet enregistrement existe déjà !',
                ];
    
                return $message;
            }

            else{
                  
                    DB::table('typedepense')->insert(
    
                        [   
                            'libelle' => $lib,
                        ]
                    );
        
                $message = [
                    "title" => 'success',
                    "type" => 'success',
                    "response" => 'Enregistrement effectué avec succès !',
                ];
        
                return $message; 
            } 
    }
	
}
