<?php

namespace App\Http\Controllers;

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

class EmballageController extends Controller
{
    //

	function VoirSuppression ($id) {

        $emballage= DB::table('emballage')->where('id',$id)->first();
        return [

            'emballage' => $emballage,
        ];
    }

    function supprimer () {

        $id=request('id');

        $veri =  DB::table('stock_emballage')->where('emballage_id', $id)->first();

        if($veri) {

            return redirect()->route('nouvel-emballage')->with('error', 'Suppression impossible ! Ce enregistrement est lié à d\'autres données');
        
        }else{

            DB::table('emballage')->where('id', $id)->delete();
    
            return redirect()->route('nouvel-emballage')->with('status', 'Suppression effectuée avec succes');
        }
   
    }


    function nouveau()
    {
        if(session('user')== null) {
            return redirect('/');
        }
		$emballage = DB::table('emballage')->orderBy('designation','asc')->get();
	return view('emballage/nouveau', [
		'emballage' => $emballage,
	]);
    }
	
	 function liste()
    {
        if(session('user')== null) {
            return redirect('/');
        }
		$emballage = DB::table('emballage')->orderBydesc('id')->get();
	return view('emballage/liste', [
		'emballage' => $emballage,
	]);
    }
   
    function modifier($id) {
        if(session('user')== null) {
            return redirect('/');
        }
        $emballage=DB::table('emballage')->orderBy('designation','asc')->get();
        $emb = DB::table('emballage')->where('id', $id)->first();
        return view('emballage/modifier',[
            'emballage' => $emballage,
            'emballage1'=>$emb,
        ]);
    }

    function enregistrer(Request $request)
    {

        $designation=request('designation');
        $nbreencarton=request('nbreencarton');
        //$prix=request('prix');
        $today=date('Y-m-d');

        $verification= DB::table('emballage')->where('designation',$designation)->first();
        if($verification){
            return redirect()->route('nouvel-emballage')->with('error', 'Cet enregistrement existe déjà');
        }
        else {
            DB::table('emballage')->insert(
                [
                    'designation' => $designation,
                    'nbreencarton' => $nbreencarton,
                    'date_entree' => $today,
                    'user_id' => session('user')->id,
                ]
            );
		}
		
            return redirect()->route('nouvel-emballage')->with('status', 'Succès de l\' enregistrement !');
		}

    function mpost(Request $request, $id)
    {

        $designation=request('designation');
        $nbreencarton=request('nbreencarton');
        $today=date('Y-m-d');

        //$verification= DB::table('emballage')->where('designation',$designation)->first();
       
            DB::table('emballage')->where('id',$id)->update(
                [
                    'designation' => $designation,
                    'nbreencarton' => $nbreencarton,
                    'date_entree' => $today,
                    'user_id' => session('user')->id,
                ]
            );
            return redirect()->route('nouvel-emballage')->with('status', 'Modification effectuée avec succès!');
    }

    function Voir ($id) {
        $emballage= DB::table('emballage')->where('id',$id)->first();
        return [
            'emballage' => $emballage,
        ];
    }
	//INVENTAIRE
	public function inventaire(){
	
        if(session('user')== null) {
            return redirect('/');
        }
        $emballage=DB::table('emballage')->get();

		$inventaire = DB::table('inventaire_emballage')->orderBydesc('id')->take(10)->get();

		return view('emballage/inventaire_emballage', [
			'emballage'=> $emballage,
			'inventaire'=> $inventaire,
		]);
    }

	function inventairepost(Request $request)
    {
        $today=date('Y-m-d');
        $invmag=collect(request('invmag'))->chunk(3);

        DB::table('inventaire_emballage')->insert(
            [
                'date' => $today,
				'user_id' => session('user')->id,
				'etat' => 'En cours',
            ]
        );

        $idLastmag = DB::table('inventaire_emballage')->get()->last()->id;

        $id2= strlen($idLastmag);

        if ($id2==1){

            DB::table('inventaire_emballage')->where('id', $idLastmag)->update(
                [
                    'reference' => 'INVMAG-000'.$idLastmag,
                ]
            );

        } elseif ($id2==2){

            DB::table('inventaire_emballage')->where('id', $idLastmag)->update(
                [
                    'reference' => 'INVMAG-00'.$idLastmag,
                ]
            );

        } elseif ($id2==3){

            DB::table('inventaire_emballage')->where('id', $idLastmag)->update(
                [
                    'reference' => 'INVMAG-0'.$idLastmag,
                ]
            );

        } else {

            DB::table('inventaire_emballage')->where('id', $idLastmag)->update(
                [
                    'reference' => 'INVMAG-'.$idLastmag,
                ]
            );
        }

        $i=0;

        foreach ($invmag as $key=> $invmags){
			
			$qtestoc = DB::table('stock_emballage')->where('emballage_id', $invmags[0 +$i])->get()->count();
			if ($qtestoc == 0){
				$qtesto = 0;
			} else {
				$qtesto = DB::table('stock_emballage')->where('emballage_id', $invmags[0 +$i])->orderBydesc('id')->take(1)->first()->apres;
			}
			
            DB::table('ligne_inv_emballage')->insert(
                [
                    'ligne_id' => $idLastmag,
                    'emballage_id' => $invmags[0 +$i],
                    'qte' => $invmags[1 +$i],
                    'qte_theo' => $qtesto,
                    'date' => $today,
                    'pa' => $invmags[2 +$i],
                    'montligne' => ($invmags[2 +$i] * $invmags[1 +$i]),
					'user_id' => session('user')->id,
					'etat' => 'En cours',
                ]
            );
			
            $i += count($invmags->toArray());
			
			
        }

        $message = [
            "title" => 'Succès',
            "type" => 'success',
            "response" => 'Enregistrement effectué avec succès !',
        ];
        return $message;
    }
	function voirinv ($id) {
        $invent= DB::table('inventaire_emballage')->where('id',$id)->first();
        return [
            'invent' => $invent,
        ];
    }
	function vdemballage()
    {
		$id = request('id');
		$vo = DB::table('ligne_inv_emballage')->where('ligne_id', $id)->get();
		$today = date('Y-m-d');
		foreach($vo as $vos){
			$qtestoc = DB::table('stock_emballage')->where('emballage_id', $vos->emballage_id)->get()->count();
			if ($qtestoc == 0){
				$qtesto = 0;
			} else {
				$qtesto = DB::table('stock_emballage')->where('emballage_id', $vos->emballage_id)->orderBydesc('id')->take(1)->first()->apres;
			}
			$qteval = $vos->qte;
		
					DB::table('emballage')->where('id', $vos->emballage_id)->update(
						[
							'stock_general' => $qteval,
						]
					);
					DB::table('stock_emballage')->insert(
							[
								'date' => $vos->date,
								'emballage_id' => $vos->emballage_id,
								'qte' => $vos->qte,
								'type_operation' => 'Inventaire',
								'en_stock' => $qtesto,
								'apres' => $vos->qte,
								'pa' => $vos->pa,
								'montligne' => $vos->montligne,
							]
						);	
			}
			DB::table('ligne_inv_emballage')->where('ligne_id', $id)->update(
			[
				'etat' => "Validée",
			]
		);
		DB::table('inventaire_emballage')->where('id', $id)->update(
			[
				'etat' => "Validée",
			]
		);

			return redirect()->route('inventaire-emballage')->with('status', 'Inventaire validé avec succès !');
	}
	function supprimeremballage () {

        $id=request('id');

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

        DB::table('ligne_inv_emballage')->where('ligne_id', $id)->delete();

        return redirect()->route('inventaire-emballage')->with('status', 'Suppression effectuée avec succès');
    }
	function modifierinv($id) {

        if(session('user')== null) {
            return redirect('/');
        }
        $invent = DB::table('inventaire_emballage')->where('id', $id)->first();
        $ligne = DB::table('ligne_inv_emballage')->where('ligne_id', $id)->get();
        $emballage=DB::table('emballage')->orderBy('designation','asc')->get();

        return view('emballage/modif_inventaire_emballage',[

            'invent' => $invent,
            'ligne' => $ligne,
            'emballage'=> $emballage,
        ]);
    }
	 function valider_emballage($id)
    {
        $invmag=collect(request('invmag'))->chunk(3);
		$today = date('Y-m-d');
        /*DB::table('inventaire_general')->where('id', $id)->update(
            [
                'magasin_id' => $magasin,
               
            ]
        );*/

        $i=0;
        //$nbreLigne = DB::table('ligne_inv_general')->where('ligne_id', $id)->get()->count() - 1;
        DB::table('ligne_inv_emballage')->where('ligne_id', $id)->delete();

        foreach ($invmag as $key=> $invmags){
            DB::table('ligne_inv_emballage')->insert(
                [

                    'ligne_id' => $id,
                    'emballage_id' => $invmags[0 +$i],
                    'qte' => $invmags[1 +$i],
					'montligne' => ($invmags[1 +$i] * $invmags[2 +$i]),
					'date' => $today,
                    'pa' => $invmags[2 +$i],
                    'etat' => 'En cours',
                    'user_id' => session('user')->id,
                ]
            );

            $i += count($invmags->toArray());
		}
		$message = [
            "title" => 'Succès',
            "type" => 'success',
            "response" => 'Modification effectué avec succès !',
        ];

        return $message;

	}
	function detailemballage($id) {

        if(session('user')== null) {
            return redirect('/');
        }
       
        $ligne = DB::table('ligne_inv_emballage')->where('ligne_id', $id)->get();

        $iddetailmag= $id;

        session(['iddetailmag'=> $iddetailmag]);

        return view('emballage/detail_emballage',[

            'ligne' => $ligne,
        ]);
    }
	function voirdetailemballage ($id) {

        $ligne= DB::table('ligne_inv_emballage')->where('id',$id)->first();
        return [
            'ligne' => $ligne,
        ];
    }
	function supprimerdetailemballage () {

        $id=request('id');
		$invid = DB::table('ligne_inv_emballage')->where('id', $id)->first()->ligne_id;
        DB::table('ligne_inv_emballage')->where('id', $id)->delete();
		$verif = DB::table('ligne_inv_emballage')->where('ligne_id', $invid)->get()->count();
		if ($verif > 0){
			return redirect()->route('inventaire-emballage')->with('status', 'Ligne supprimée avec succès');
		} else {
			DB::table('inventaire_emballage')->where('id', $invid)->delete();
			//return redirect()->route('detail_magasin', ['id'=> session('iddetailmag')])->with('status', 'Suppression effectuée avec succes');
			return redirect()->route('inventaire-emballage')->with('status', 'Ligne supprimée avec succès');
		}
    }
	public function embp($id){
	
        if(session('user')== null) {
            return redirect('/');
        }
        $emballage=DB::table('emballage')->get();
		$embp = DB::table('emballage_produit')->where('produit_id',$id)->get();

		return view('produit/emballage_produit', [
			'emballage'=> $emballage,
			'embp'=> $embp,
			'idp'=> $id,
		]);
    }
	function embpost($id)
    {
        $today=date('Y-m-d');
        $invmag=collect(request('invmag'))->chunk(1);
		
        $i=0;

        foreach ($invmag as $key=> $invmags){
			$desi = DB::table('emballage')->where('id', $invmags[0 +$i])->first()->designation;
			$verif = DB::table('emballage_produit')->where('produit_id', $id)->where('emballage_id',$invmags[0 +$i])->first();
			if ($verif){
				$message = [
					"title" => 'Attention',
					"type" => 'error',
					"response" => 'L\'emballage '.$desi.' a déjà été défini sur ce produit',
				];
				return $message;
			} else {
            DB::table('emballage_produit')->insert(
                [
                    'date' => $today,
                    'emballage_id' => $invmags[0 +$i],
                    'produit_id' => $id,
					'user_id' => session('user')->id,
                ]
            );
			
            $i += count($invmags->toArray());
			}
		}
			$message = [
				"title" => 'Succès',
				"type" => 'success',
				"response" => 'Enregistrement effectué avec succès !',
			];
			return $message;
	}
	function voirligne ($id) {

        $ligne= DB::table('emballage_produit')->where('id',$id)->first();
        return [
            'ligne' => $ligne,
        ];
    }
	function suppembp () {
        $id=request('id');
        $idp = DB::table('emballage_produit')->where('id', $id)->first()->produit_id;
        DB::table('emballage_produit')->where('id', $id)->delete();
		return redirect()->route('emballage-produit',['id'=>$idp])->with('status', 'Ligne supprimée avec succès');
	}
}
