<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

use PDF;

class TransfertdepotController extends Controller
{
    //
    function valider () {

        $id=request('id');	

        $today=date('Y-m-d');
		
		DB::table('transfertdepot')->where('id', $id)->update(
			[
				'etat' => "Validé",
			]
        );
		DB::table('ligne_transfertdepot')->where('transfert_id', $id)->update(
			[
				'etat' => "Validé",
			]
        );

        $ligne = DB::table('ligne_transfertdepot')->where('transfert_id', $id)->get();

        $magx = DB::table('transfertdepot')->where('id', $id)->first()->magasin_id;

        $nommagx = DB::table('depot')->where('id', $magx)->first()->nom;
        
        //$magy = DB::table('transfert')->where('id', $id)->first()->magasiny_id;

        //$nommagy = DB::table('magasin')->where('id', $magy)->first()->nom;

        foreach($ligne as $lignes){

            $qteval = $lignes->qte;

            //$qtestox = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->orderbydesc('id')->take(1)->first()->stock_general;
			$stg = DB::table('produit_vendable')->where('id', $lignes->produit_id)->first()->stock_general;
            $stm = DB::table('produit_vendable')->where('id', $lignes->produit_id)->first()->stock_magasin;
            
			/*
			$smxc = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->where('magasin_id', $magx)->get()->count();
			if ($smxc > 0){
			$smx = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->where('magasin_id', $magx)->orderbydesc('id')->take(1)->first()->apres;
			} else {
				$smx = 0;
			} */
			
			$smx = DB::table('magasin_commande')->where('produit_id', $lignes->produit_id)->orderbydesc('id')->take(1)->first()->apres;
            DB::table('magasin_commande')->insert(
                [
                    'date' => $today,
                    'produit_id' => $lignes->produit_id,
                    'qte' => $qteval,
                    'type_operation' => 'Transfert',
                    'notes' => 'Transfert vers '.$nommagx,
                    'en_stock' => $smx,
                    'apres' => ($smx - $qteval),
                    //'magasin_id' => $magx,
                    'user_id' => session('user')->id,
                ]
            );

            $verif = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->where('magasin_id', $magx)->get()->count();

            if ($verif > 0){
				/*  recuperation du stock général */
                $qtestoyc = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->get()->count();
                if ($qtestoyc>0){
				$qtestoy = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->orderbydesc('id')->take(1)->first()->stock_general;
				} else {
					$qtestoy = 0;
				}
                $smy = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->where('magasin_id', $magx)->orderbydesc('id')->take(1)->first()->apres;

                DB::table('magasin_general')->insert(
                    [
                        'date' => $today,
                        'magasin_id' => $magx,
                        'produit_id' => $lignes->produit_id,
                        'qte' => $qteval,
                        'type_operation' => 'TransfertRecu',
                        'notes' => 'Transfert reçu',
						'en_stock' => $smy,
						'apres' => ($smy + $qteval),
						'stock_general' => ($qtestoy + $qteval),
						'user_id' => session('user')->id,
                    ]
                );

            }else{

                $smy = 0;

                $qtestoyc = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->get()->count();
                if ($qtestoyc>0){
				$qtestoy = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->orderbydesc('id')->take(1)->first()->stock_general;
				} else {
					$qtestoy = 0;
				}

				DB::table('magasin_general')->insert(
                    [
                        'date' => $today,
                        'magasin_id' => $magx,
                        'produit_id' => $lignes->produit_id,
                        'qte' => $qteval,
                        'type_operation' => 'TransfertRecu',
                        'notes' => 'Transfert reçu ',
						'en_stock' => $smy,
						'apres' => ($smy + $qteval),
                        'stock_general' => ($qtestoy + $qteval),
                        'user_id' => session('user')->id,
                    ]
                ); 

            }
			/* A NE PLUS UTILISER */
			DB::table('produit_vendable')->where('id', $lignes->produit_id)->update(
                [
                    'stock_magasin' => ($stm - $qteval),
                    'stock_general' => ($stg + $qteval),
                ]
            );
        }

        return redirect()->route('nouveau_transfertdepot')->with('status', 'Transfert validé avec succès !');
       
	}


    function voirvalider ($id) {

        $tran= DB::table('transfertdepot')->where('id',$id)->first();

        return [
            'tran' => $tran,
        ];
	}

    public function detail($id){

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

        $ligne=DB::table('ligne_transfertdepot')->where('transfert_id',$id)->get();
        
		return view('transfertdepot/detail', [

            'ligne'=> $ligne,
            
		]);
	}
	public function detail2(){

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

        $ligne=DB::table('ligne_transfertdepot')->get();
        
		return view('transfertdepot/ligne', [

            'ligne'=> $ligne,
            
		]);
	}
	public function lignetpost(){

		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){
        $ligne=DB::table('ligne_transfertdepot')->whereBetween('date', [$date_debut,$date_fin])->get();
        
		return view('transfertdepot/lignep', [

            'ligne'=> $ligne,
            
		]);
	} else {
		return redirect()->route('ligne_transfert_recudepot')->with('error', 'La date de fin ne doit pas dépasser la date de début!');
	}
	}
	public function lignetpdf(){

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

        $ligne=DB::table('ligne_transfertdepot')->whereBetween('date', [$date_debut,$date_fin])->get();
        
		$pdf = PDF::loadView('transfertdepot/lignepdf', [

            'ligne'=> $ligne,
            
		]);
		return $pdf->stream();
	}


    function liste1(){

        if(session('user')== null) {
            return redirect('/');
        }
            $transfert = DB::table('transfertdepot')->get();
           
			return view('transfertdepot/transfert_effectue',[

            'transfert' => $transfert,
            
        ]);	
    }
    

    function liste2(){

        if(session('user')== null) {
            return redirect('/');
        }
            $transfert = DB::table('transfertdepot')->get();
           
			return view('transfertdepot/transfert_recu',[

            'transfert' => $transfert,
            
        ]);	
    }
	
    public function transfert(){
		
        if(session('user')== null) {
            return redirect('/');
        }

        $magx = session('user')->magasin_id ;
		
		$produit=DB::table('produit_vendable')->orderby('designation','asc')->get();
        
        //$magasin = DB::table('magasin')->where('id', '<>', $magx)->get();
        //$magasin = DB::table('magasin')->get();
        $depot = DB::table('depot')->get();
		$categorie=DB::table('categorie')->get();
        $transfert = DB::table('transfertdepot')->where('etat', 'En cours')->orderbydesc('id')->get();
		
		return view('transfertdepot/nouveau', [

			'produit'=> $produit,
            'depot'=> $depot,
            //'magasin'=> $magasin,
            'transfert'=> $transfert,
            'cat'=> $categorie,
			
		]);
    }
    

    function enregistrer(Request $request)
    {

        $datetransfert= implode('-', array_reverse(explode('/', request('datetransfert'))));
        $magasinx=request('magasinx');
        //$magasiny=request('magasiny');
        $transmag = collect(request('transmag'))->chunk(3);
       
        DB::table('transfertdepot')->insert(
            [
                'date' => $datetransfert,
                'magasin_id' => $magasinx,
                //'magasiny_id' => $magasiny,
                'etat' => "En cours",
                'user_id' => session('user')->id,
            ]
        );

        $idLasttranfert = DB::table('transfertdepot')->get()->last()->id;

        $idLastmagx = DB::table('transfertdepot')->get()->last()->magasin_id;

        //$idLastmagy = DB::table('transfertdepot')->get()->last()->magasiny_id;

        $idLastetat = DB::table('transfertdepot')->get()->last()->etat;

        $idLastuser = DB::table('transfertdepot')->get()->last()->user_id;

        $idLast= strlen($idLasttranfert);

        if ($idLast==1){
            DB::table('transfertdepot')->where('id', $idLasttranfert)->update(
                [
                    'numero' => 'TRANSFERT-'.'000'.$idLasttranfert,
                ]
            );
        } elseif ($idLast==2){
    
            DB::table('transfertdepot')->where('id', $idLasttranfert)->update(
                [
                    'numero' => 'TRANSFERT-'.'00'.$idLasttranfert,
                ]
            );
        } elseif ($idLast==3){
            DB::table('transfertdepot')->where('id', $idLasttranfert)->update(
                [
                    'numero' => 'TRANSFERT-'.'0'.$idLasttranfert,
                ]
            );
        } else {
            DB::table('transfertdepot')->where('id', $idLasttranfert)->update(
                [
                    'numero' => 'TRANSFERT-'.$idLasttranfert,
                ]
            );
        }


        $i=0;
			
	    foreach ($transmag as $key=> $transmags){

            $desi =  DB::table('produit_vendable')->where('id', $transmags[1 +$i])->first()->designation;
           
            $veriprodx =  DB::table('magasin_commande')->where('produit_id', $transmags[1 +$i])->first();

            if($veriprodx){

                //$qtestocx =  DB::table('magasin_general')->where('produit_id', $transmags[1 +$i])->where('magasin_id', $magasinx)->get()->last()->apres;
                $qtestocx =  DB::table('magasin_commande')->where('produit_id', $transmags[1 +$i])->orderBydesc('id')->take(1)->first()->apres;

            if ($qtestocx <= 0){

                DB::table('transfertdepot')->where('id', $idLasttranfert)->delete();

                DB::table('ligne_transfertdepot')->where('transfert_id', $idLasttranfert)->delete();

                $message = [
                    "title" => 'Erreur de stock',
                    "type" => 'error',
                    "response" => 'Aucun stock de '.$desi.' n\'est disponible pour réaliser cette opération !',
                ];
    
                return $message;
            } else {

                if ($qtestocx < $transmags[2 +$i]){

                    DB::table('transfertdepot')->where('id', $idLasttranfert)->delete();

                    DB::table('ligne_transfertdepot')->where('transfert_id', $idLasttranfert)->delete();
    
                    $message = [
                        "title" => 'Erreur de stock',
                        "type" => 'error',
                        "response" => 'Stock de '.$desi.' insuffisant !',
                    ];
        
                    return $message;
                    
                }else{

                    DB::table('ligne_transfertdepot')->insert(
						[
								'transfert_id' => $idLasttranfert,
								'produit_id' => $transmags[1 +$i],
								'qte' => $transmags[2 +$i],
								'etat' => $idLastetat,    
								'magasin_id' => $idLastmagx,
								//'magy' => $idLastmagy, 
								'user_id' => $idLastuser,
								'date' => $datetransfert,
								
						]
					);
						$i += count($transmags->toArray()); 

                }

            }

            } else {

                DB::table('transfertdepot')->where('id', $idLasttranfert)->delete();

                DB::table('ligne_transfertdepot')->where('transfert_id', $idLasttranfert)->delete();

                $message = [
                    "title" => 'Erreur de stock',
                    "type" => 'error',
                    "response" => 'Aucun stock de '.$desi.' n\'est disponible pour réaliser cette opération !',
                ];
    
                return $message;

            }

        }

        $message = [
            "title" => 'success',
            "type" => 'success',
            "response" => 'Opération effectué avec succes !',
        ];

        return $message; 

    }


    function supprimer () {

        $id=request('id');

        DB::table('ligne_transfertdepot')->where('transfert_id', $id)->delete();
        
        DB::table('transfertdepot')->where('id', $id)->delete();
        
        return redirect()->route('nouveau_transfertdepot')->with('status', 'Suppression effectuée avec succès');

    }



 
	//--******************TRANSFERT DEPOT DEPOT*****************----//
	
	public function depotdepot(){
		
        if(session('user')== null) {
            return redirect('/');
        }

		$produit=DB::table('produit_vendable')->orderby('designation','asc')->get();
        //$magasin = DB::table('magasin')->where('id', '<>', $magx)->get();
        $depot = DB::table('depot')->get();
		$categorie=DB::table('categorie')->get();
        $transfert = DB::table('transfertdepotdepot')->orderBydesc('id')->take(100)->get();
		
		return view('transfertdepot/depot-depot', [

			'produit'=> $produit,
            'depot'=> $depot,
            //'magasin'=> $magasin,
            'transfert'=> $transfert,
            'cat'=> $categorie,
			
		]);
    }
	function voirtransfert ($id) {
        $transfert= DB::table('transfertdepotdepot')->where('id',$id)->first();
        return [
            'transfert' => $transfert,
        ];
	}
	function supprimerdepdep () {

        $id=request('id');
        DB::table('ligne_transfertdepotdepot')->where('transfert_id', $id)->delete();
        
        DB::table('transfertdepotdepot')->where('id', $id)->delete();
        
        return redirect()->route('depot-depot')->with('status', 'Suppression effectuée avec succès');

    }
	
	function depotdepotpost(Request $request)
    {

        $datetransfert= implode('-', array_reverse(explode('/', request('datetransfert'))));
        $depotx_id=request('depotx_id');
        $depoty_id=request('depoty_id');
        $transmag = collect(request('transmag'))->chunk(2);
        if ($depotx_id == $depoty_id){
            $message = [
                        "title" => 'Attention',
                        "type" => 'error',
                        "response" => 'Le dépôt source doit être différent de la destination',
                    ];
                    return $message;
        } else {
        DB::table('transfertdepotdepot')->insert(
            [
                'date' => $datetransfert,
                'depotx_id' => $depotx_id,
                'depoty_id' => $depoty_id,
                'etat' => "En cours",
                'user_id' => session('user')->id,
            ]
        );

        $idLasttranfert = DB::table('transfertdepotdepot')->get()->last()->id;

        //$idLastdepotx = DB::table('transfertdepotdepot')->get()->last()->depotx_id;

        //$idLastdepoty = DB::table('transfertdepotdepot')->get()->last()->depoty_id;

        $idLastetat = DB::table('transfertdepotdepot')->get()->last()->etat;

        $idLastuser = DB::table('transfertdepotdepot')->get()->last()->user_id;

        $idLast= strlen($idLasttranfert);

        if ($idLast==1){
            DB::table('transfertdepotdepot')->where('id', $idLasttranfert)->update(
                [
                    'numero' => 'TRANSFERT-'.'000'.$idLasttranfert,
                ]
            );
        } elseif ($idLast==2){
    
            DB::table('transfertdepotdepot')->where('id', $idLasttranfert)->update(
                [
                    'numero' => 'TRANSFERT-'.'00'.$idLasttranfert,
                ]
            );
        } elseif ($idLast==3){
            DB::table('transfertdepotdepot')->where('id', $idLasttranfert)->update(
                [
                    'numero' => 'TRANSFERT-'.'0'.$idLasttranfert,
                ]
            );
        } else {
            DB::table('transfertdepotdepot')->where('id', $idLasttranfert)->update(
                [
                    'numero' => 'TRANSFERT-'.$idLasttranfert,
                ]
            );
        }


        $i=0;
			
	    foreach ($transmag as $key=> $transmags){

            $desi =  DB::table('produit_vendable')->where('id', $transmags[0 +$i])->first()->designation;
            $cat_id =  DB::table('produit_vendable')->where('id', $transmags[0 +$i])->first()->categorie_id;
           
            $veriprodx =  DB::table('magasin_general')->where('produit_id', $transmags[0 +$i])->where('depot_id', $depotx_id)->first();

            if($veriprodx){

                $qtestocx =  DB::table('magasin_general')->where('produit_id', $transmags[0 +$i])->where('depot_id', $depotx_id)->get()->last()->apres;
                
            if ($qtestocx <= 0){

                DB::table('transfertdepotdepot')->where('id', $idLasttranfert)->delete();

                DB::table('ligne_transfertdepotdepot')->where('transfert_id', $idLasttranfert)->delete();

                $message = [
                    "title" => 'Erreur de stock',
                    "type" => 'error',
                    "response" => 'Aucun stock de '.$desi.' n\'est disponible dans le dépot source pour réaliser cette opération !',
                ];
                return $message;
            } else {

                if ($qtestocx < $transmags[1 +$i]){

                    DB::table('transfertdepotdepot')->where('id', $idLasttranfert)->delete();

                    DB::table('ligne_transfertdepotdepot')->where('transfert_id', $idLasttranfert)->delete();
    
                    $message = [
                        "title" => 'Erreur de stock',
                        "type" => 'error',
                        "response" => 'Stock de '.$desi.' insuffisant ! Restant : '.$qtestocx,
                    ];
        
                    return $message;
                    
                }else{

                    DB::table('ligne_transfertdepotdepot')->insert(
						[
								'transfert_id' => $idLasttranfert,
								'produit_id' => $transmags[0 +$i],
								'qte' => $transmags[1 +$i],
								'etat' => $idLastetat,    
								'depotx_id' => $depotx_id,
								'depoty_id' => $depoty_id, 
								'user_id' => session('user')->id,
								'categorie_id' => $cat_id, 
								'date' => $datetransfert,
								
						]
					);
						$i += count($transmags->toArray()); 

                }

           }

           } else {

                DB::table('transfertdepotdepot')->where('id', $idLasttranfert)->delete();

                DB::table('ligne_transfertdepotdepot')->where('transfert_id', $idLasttranfert)->delete();

                $message = [
                    "title" => 'Erreur de stock',
                    "type" => 'error',
                    "response" => 'Aucun stock de '.$desi.' n\'est disponible dans le dépot source pour réaliser cette opération !',
                ];
    
                return $message;

            }
        
        }
        $message = [
            "title" => 'success',
            "type" => 'success',
            "response" => 'Opération effectué avec succes !',
        ];

        return $message; 

    }}
	function validerdepdep () {

        $id=request('id');	

        $today=date('Y-m-d');
		
		DB::table('transfertdepotdepot')->where('id', $id)->update(
			[
				'etat' => "Validé",
			]
        );
		DB::table('ligne_transfertdepotdepot')->where('transfert_id', $id)->update(
			[
				'etat' => "Validé",
			]
        );

        $ligne = DB::table('ligne_transfertdepotdepot')->where('transfert_id', $id)->get();

        $depotx_id = DB::table('transfertdepotdepot')->where('id', $id)->first()->depotx_id;
        $depoty_id = DB::table('transfertdepotdepot')->where('id', $id)->first()->depoty_id;

        foreach($ligne as $lignes){

            $qteval = $lignes->qte;

            $qtestox = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->orderbydesc('id')->take(1)->first()->stock_general;
			$stg = DB::table('produit_vendable')->where('id', $lignes->produit_id)->first()->stock_general;
            $stm = DB::table('produit_vendable')->where('id', $lignes->produit_id)->first()->stock_magasin;
            
			
			$smxc = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->where('depot_id', $depotx_id)->get()->count();
			if ($smxc > 0){
			$smx = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->where('depot_id', $depotx_id)->orderbydesc('id')->take(1)->first()->apres;
			} else {
				$smx = 0;
			} 
            
                DB::table('magasin_general')->insert(
                    [
                        'date' => $lignes->date,
                        'depot_id' => $depotx_id,
                        'produit_id' => $lignes->produit_id,
                        'qte' => $qteval,
                        'type_operation' => 'Transfert',
                        'notes' => 'Transfert',
						'en_stock' => $smx,
						'apres' => ($smx - $qteval),
						'transfert_id' => $id,
                    ]
                );
				$smyc = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->where('depot_id', $depoty_id)->get()->count();
				if ($smyc == 0){
					$smy = 0;
				} else {
					$smy = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->where('depot_id', $depoty_id)->orderbydesc('id')->take(1)->first()->apres;
				}
                
                DB::table('magasin_general')->insert(
                    [
                        'date' => $lignes->date,
                        'depot_id' => $depoty_id,
                        'produit_id' => $lignes->produit_id,
                        'qte' => $qteval,
                        'type_operation' => 'TransfertRecu',
                        'notes' => 'Transfert reçu',
						'en_stock' => $smy,
						'apres' => ($smy + $qteval),
						'transfert_id' => $id,
                    ]
                );
        }

        return redirect()->route('depot-depot')->with('status', 'Transfert validé avec succès !');
       
	}
	function annulerdepdep () {

        $id=request('id');	

        $today=date('Y-m-d');
		
		DB::table('transfertdepotdepot')->where('id', $id)->update(
			[
				'etat' => "Annulé",
			]
        );
		DB::table('ligne_transfertdepotdepot')->where('transfert_id', $id)->update(
			[
				'etat' => "Annulé",
			]
        );

        $ligne = DB::table('ligne_transfertdepotdepot')->where('transfert_id', $id)->get();
        $depotx_id = DB::table('transfertdepotdepot')->where('id', $id)->first()->depotx_id;
        $depoty_id = DB::table('transfertdepotdepot')->where('id', $id)->first()->depoty_id;

        foreach($ligne as $lignes){

            $qteval = $lignes->qte;

            //$qtestox = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->orderbydesc('id')->take(1)->first()->stock_general;
			$stg = DB::table('produit_vendable')->where('id', $lignes->produit_id)->first()->stock_general;
            $stm = DB::table('produit_vendable')->where('id', $lignes->produit_id)->first()->stock_magasin;
        
            $smxc = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->where('depot_id', $depotx_id)->get()->count();
			if ($smxc == 0){
				$smx = 0;
			} else {
				$smx = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->where('depot_id', $depotx_id)->orderbydesc('id')->take(1)->first()->apres;
			}
            
                DB::table('magasin_general')->insert(
                    [
                        'date' => $lignes->date,
                        'depot_id' => $depotx_id,
                        'produit_id' => $lignes->produit_id,
                        'qte' => $qteval,
                        'type_operation' => 'Annulation transfert',
                        'notes' => 'Transfert annulé',
						'en_stock' => $smx,
						'apres' => ($smx + $qteval),
						'transfert_id' => $id,
						'user_id' => session('user')->id,
                    ]
                );
				$smyc = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->where('depot_id', $depoty_id)->get()->count();
				if ($smyc == 0){
					$smy = 0;
				} else {
					$smy = DB::table('magasin_general')->where('produit_id', $lignes->produit_id)->where('depot_id', $depoty_id)->orderbydesc('id')->take(1)->first()->apres;
				}
                
                DB::table('magasin_general')->insert(
                    [
                        'date' => $lignes->date,
                        'depot_id' => $depoty_id,
                        'produit_id' => $lignes->produit_id,
                        'qte' => $qteval,
                        'type_operation' => 'Annulation transfertrecu',
                        'notes' => 'Transfert reçu annulé',
						'en_stock' => $smy,
						'apres' => ($smy - $qteval),
						'transfert_id' => $id,
						'user_id' => session('user')->id,
                    ]
                );
        }

        return redirect()->route('depot-depot')->with('status', 'Transfert validé avec succès !');
       
	}
	public function listedepotdepot(){
		
        if(session('user')== null) {
            return redirect('/');
        }

		$produit=DB::table('produit_vendable')->orderby('designation','asc')->get();
        //$magasin = DB::table('magasin')->where('id', '<>', $magx)->get();
        $depot = DB::table('depot')->get();
		$categorie=DB::table('categorie')->get();
        $transfert = DB::table('transfertdepotdepot')->orderbydesc('id')->get();
		
		return view('transfertdepot/liste-depot-depot', [

			'produit'=> $produit,
            'depot'=> $depot,
            //'magasin'=> $magasin,
            'transfert'=> $transfert,
            'cat'=> $categorie,
			
		]);
    }

    	//TRANSFERT AYANT QUITTÉ UN DEPOT
	function versdep() {

        if(session('user')== null) {
            return redirect('/');
        }
        //$depot = DB::table('depot')->where('id',2)->get();
        $depot = DB::table('depot')->get();
        return view('transfertdepot/versdep',[
            'depot' => $depot,
        ]);
    }
	function versdeppost() {

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

        $depot_id=request('depot_id');

        $datedeb=implode('-', array_reverse(explode('/', request('date_debut'))));
        $datefin=implode('-', array_reverse(explode('/', request('date_fin'))));
		
		session(['datedeb'=> $datedeb]);
		session(['datefin'=> $datefin]);
		session(['depot_id'=> $depot_id]);
		
        //$depot = DB::table('depot')->where('id',2)->get();
        $depot = DB::table('depot')->get();
            $transfert = DB::table('transfertdepotdepot')->where('depotx_id', $depot_id)
								->where('etat', "Validé")
								->whereBetween('date',[$datedeb,$datefin])
								->get();
        if( $datedeb > $datefin ){
            return redirect()->route('transfert-vers-depot')->with('error', 'Erreur ! La date de début ne doit pas dépasser la date fin');
        }else{
            return view('transfertdepot/versdep2',[
                'datedeb' => $datedeb,
                'datefin' => $datefin,
                'transfert' => $transfert,
                'depot' => $depot,
                'depot_id' => $depot_id,
            ]);
        }

    }
}
