<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

use PDF;

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

        $id=request('id');	

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

        $ligne = DB::table('ligne_transfert_emballage')->where('transfert_emballage_id', $id)->get();
        
        $magasin = DB::table('transfert_emballage')->where('id', $id)->first()->magasin_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,
                    'emballage_id' => $lignes->emballage_id,
                    'qte' => $qteval,
                    'type_operation' => 'Transfert',
                    '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-transfert-emballage')->with('status', 'Transfert 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) {

        $transfert= DB::table('transfert_emballage')->where('id',$id)->first();

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

    public function detail($id){

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

        $ligne=DB::table('ligne_transfert_emballage')->where('transfert_emballage_id',$id)->get();
        
		return view('transfertemballage/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();
        $transfert_emballage = DB::table('transfert_emballage')->orderbydesc('id')->get();	
		return view('transfertemballage/nouveau', [
			'emballage'=> $emballage,
            'client'=> $client,
            'transfert'=> $transfert_emballage,
		]);
    }
    

    function enregistrer(Request $request)
    {

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

        $idLasttranfert = DB::table('transfert_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('transfert_emballage')->get()->last()->etat;

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

        $idLast= strlen($idLasttranfert);

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


        $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('transfert_emballage')->where('id', $idLasttranfert)->delete();

                DB::table('ligne_transfert_emballage')->where('transfert_emballage_id', $idLasttranfert)->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('transfert_emballage')->where('id', $idLasttranfert)->delete();

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


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

                }

            }

            }else{

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

                DB::table('ligne_transfert_emballage')->where('transfert_emballage_id', $idLasttranfert)->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_transfert_emballage')->where('transfert_emballage_id', $id)->delete();
        DB::table('transfert_emballage')->where('id', $id)->delete();
        return redirect()->route('nouveau-transfert-emballage')->with('status', 'Suppression effectuée avec succès');
    }
	
	//consignation retour et transfert emballage
	public function listecli(){

		if(session('user')== null) {
            return redirect('/');
        }
		
			//$consignation = DB::table('consignation')->get();
			//$plucked = $consignation->pluck('client_id');
			//$client = DB::table('client_externe')->whereIn('id', $plucked)->get();
			$client = DB::table('client_externe')->get();
			$cli= DB::table('client_externe')->get();
		
		return view('transfertemballage/listecli', [
			'client'=> $client,
			'cli'=> $cli,
		]);
	}
}
