<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

use PDF;

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

        $id=request('id');	

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

        $ligne = DB::table('ligne_retour_emballage')->where('retour_emballage_id', $id)->get();
        
        $magasin = DB::table('retour_emballage')->where('id', $id)->first()->magasin_id;
        $user_id = DB::table('retour_emballage')->where('id', $id)->first()->user_id;

        foreach($ligne as $lignes){

            $qteval = $lignes->qte;

			$stg = DB::table('emballage')->where('id', $lignes->emballage_id)->first()->stock_general;
            //$stm = DB::table('emballage')->where('id', $lignes->produit_id)->first()->stock_magasin;
            $smx = DB::table('stock_emballage')->where('emballage_id', $lignes->emballage_id)->where('magasin_id', $magasin)->orderbydesc('id')->take(1)->first()->apres;

            DB::table('stock_emballage')->insert(
                [
                    'date' => $today,
                    'magasin_id' => $magasin,
                    'user_id' => $user_id,
                    'emballage_id' => $lignes->emballage_id,
                    'qte' => $qteval,
                    'type_operation' => 'Retour',
                    'en_stock' => $smx,
                    'apres' => ($smx + $qteval),
                    'stock_general' => ($stg + $qteval),
                ]
            );

			DB::table('emballage')->where('id', $lignes->emballage_id)->update(
                [
                    'stock_general' => ($stg + $qteval),
                ]
            );
        }

        return redirect()->route('nouveau-retour-emballage')->with('status', 'Retour d\'emballage validé avec succès !');
       
	}

	/*
    function valider1 () {

        $id=request('id');	

		
        
        return redirect()->route('nouveau_transfert_emballage')->with('status', 'transfert_emballage validé avec succès !');
       
	}*/


    function voirvalider ($id) {

        $retour= DB::table('retour_emballage')->where('id',$id)->first();

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

    public function detail($id){

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

        $ligne=DB::table('ligne_retour_emballage')->where('retour_emballage_id',$id)->get();
        
		return view('retouremballage/detail', [

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


    /*function liste1(){

        if(session('user')== null) {
            return redirect('/');
        }
            $transfert_emballage = DB::table('transfert_emballage')->where('magasinx_id', session('user')->magasin_id)->get();
           
			return view('transfert_emballage/transfert_emballage_effectue',[

            'transfert_emballage' => $transfert_emballage,
            
        ]);	
    }*/
    
/*
    function liste2(){

        if(session('user')== null) {
            return redirect('/');
        }
            $transfert_emballage = DB::table('transfert_emballage')->where('etat', "Validé")->where('magasiny_id', session('user')->magasin_id)->get();
           
			return view('transfert_emballage/transfert_emballage_recu',[

            'transfert_emballage' => $transfert_emballage,
            
        ]);	
    }*/
    

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

        $magasin = session('user')->magasin_id ;
		$emballage=DB::table('emballage')->where('stock_general', '>', 0)->orderby('libelle','asc')->get();
		$client = DB::table('client_externe')->get();
        $retour_emballage = DB::table('retour_emballage')->orderbydesc('id')->get();	
		return view('retouremballage/nouveau', [
			'emballage'=> $emballage,
            'client'=> $client,
            'retour'=> $retour_emballage,
		]);
    }
    

    function enregistrer(Request $request)
    {

        $dateretour= implode('-', array_reverse(explode('/', request('dateretour'))));
        $magasin=session('user')->magasin_id;
        $transmag = collect(request('transmag'))->chunk(2);
        $client = request('client');
       
        DB::table('retour_emballage')->insert(
            [
                'date' => $dateretour,
                'magasin_id' => $magasin,
                'client_id' => $client,
                'etat' => "En cours",
                'user_id' => session('user')->id,
            ]
        );

        $idLastretour = DB::table('retour_emballage')->get()->last()->id;

        //$idLastmagx = DB::table('transfert_emballage')->get()->last()->magasinx_id;

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

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

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

        $idLast= strlen($idLastretour);

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


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

            $desi =  DB::table('emballage')->where('id', $transmags[0 +$i])->first()->libelle;
           
            $veriprodx =  DB::table('stock_emballage')->where('emballage_id', $transmags[0 +$i])->where('magasin_id', session('user')->magasin_id)->first();

            if($veriprodx){

                $qtestocx =  DB::table('stock_emballage')->where('emballage_id', $transmags[0 +$i])->where('magasin_id', session('user')->magasin_id)->get()->last()->apres;

            if ($qtestocx <= 0){

                DB::table('retour_emballage')->where('id', $idLastretour)->delete();

                DB::table('ligne_retour_emballage')->where('retour_emballage_id', $idLastretour)->delete();

                $message = [
                    "title" => 'Erreur de stock',
                    "type" => 'error',
                    "response" => 'Aucun stock de '.$desi.' n\'est disponible dans ce dépôt !',
                ];
    
                return $message;
            } else {

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

                    DB::table('retour_emballage')->where('id', $idLastretour)->delete();

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


                    DB::table('ligne_retour_emballage')->insert(
						[
								'retour_emballage_id' => $idLastretour,
								'emballage_id' => $transmags[0 +$i],
								'qte' => $transmags[1 +$i],
								'etat' => $idLastetat,    
								'magasin_id' => $magasin, 
								'user_id' => $idLastuser,
								'client_id' => $client,
								'date' => $dateretour,
								
						]
					);
						$i += count($transmags->toArray()); 

                }

            }

            }else{

                DB::table('retour_emballage')->where('id', $idLastretour)->delete();

                DB::table('ligne_retour_emballage')->where('retour_emballage_id', $idLastretour)->delete();

                $message = [
                    "title" => 'Erreur de stock',
                    "type" => 'error',
                    "response" => 'Aucun stock de '.$desi.' n\'est disponible dans ce dépôt !',
                ];
    
                return $message;

            }

        }

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

        return $message; 
    }
    function supprimer () {

        $id=request('id');
        DB::table('ligne_retour_emballage')->where('retour_emballage_id', $id)->delete();
        DB::table('retour_emballage')->where('id', $id)->delete();
        return redirect()->route('nouveau-retour-emballage')->with('status', 'Suppression effectuée avec succès');
    }
}
