<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

class CuisineController extends Controller
{
    //
	
    public function consulter($id)
	{
		
		if(session('user')== null) {
            return redirect('/');
        }
		$detail=DB::table('ligne_appro_cuisine')->where('appro_id',$id)->get();
		return view('cuisine/detail', [
			'detail'=> $detail,
		]);
	}

    function voirvalider ($id) {

        $appro= DB::table('appro_cuisine')->where('id',$id)->first();
        return [
            'appro' => $appro,
        ];
    }
    
    function valider () {

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

            DB::table('ligne_appro_cuisine')->where('appro_id', $id)->update(
                [
                    'etat' => "Validée",
                ]
            );

        $ligne = DB::table('ligne_appro_cuisine')->where('appro_id', $id)->get();
		$magasin = DB::table('appro_cuisine')->where('id', $id)->first()->magasin_id;
        foreach($ligne as $lignes){

            $qtesto = DB::table('stock_magasin')->where('produit_id', $lignes->produit_id)->orderBydesc('id')->take(1)->first()->apres;
            $stockcuisinecount = DB::table('stock_cuisine')->where('produit_id', $lignes->produit_id)->get()->count();
            if ($stockcuisinecount >0){
				$stockcuisine = DB::table('stock_cuisine')->where('produit_id', $lignes->produit_id)->orderBydesc('id')->take(1)->first()->apres;
			} else {
				$stockcuisine = 0;
			}
            $qteval = $lignes->qte;
			
			//$verif = DB::table('stock_magasin')->where('produit_id', $lignes->produit_id)->get()->count();
			$sm = DB::table('stock_magasin')->where('produit_id', $lignes->produit_id)->where('magasin_id', $magasin)->orderBydesc('id')->take(1)->first()->apres;
				DB::table('stock_magasin')->insert(
                    [
                        'date' => $today,
                        'magasin_id' => $magasin,
                        'produit_id' => $lignes->produit_id,
                        'qte' => $qteval,
                        'type_operation' => 'Transfert',
						'en_stock' => $sm,
						'apres' => ($sm - $qteval),
                    ]
                );

            DB::table('produit_vendable')->where('id', $lignes->produit_id)->update(
                [
                    'stock_general' => ($qtesto - $qteval),
                    'stock_cuisine' => ($stockcuisine + $qteval),
                ]
            );
			DB::table('stock_cuisine')->insert(
                    [
                        'date' => $today,
                        'produit_id' => $lignes->produit_id,
                        'nbre' => $qteval,
						'type_operation' => 'Appro',
						'en_stock' => $stockcuisine,
						'apres' => ($stockcuisine + $qteval),
                    ]
                );
				/*DB::table('stock_magasin')->insert(
                    [
                        'date' => $today,
                        'produit_id' => $lignes->produit_id,
                        'qte' => $qteval,
						'type_operation' => 'Sortie',
						'en_stock' => $qtesto,
						'apres' => ($qtesto - $qteval),
                    ]
                );*/
        }

       
        return redirect()->route('nouveau_appro_cuisine')->with('status', 'Validation effectuée avec succes');
	}

    function nouveau() {
		if(session('user')== null) {
            return redirect('/');
        }
        $produits = DB::table('produit_vendable')->get();
        $magasin = DB::table('magasin')->get();
        $appros = DB::table('appro_cuisine')->orderBydesc('id') ->limit(5)->get();

        return view('cuisine/nouveau',[

            'produit' => $produits,
            'appro' => $appros,
            'magasin' => $magasin,
        ]);
    }


    function enregistrer(Request $request)
    {

        //$dateappro=implode('-', array_reverse(explode('/', request('dateappro'))));
        $magasin_id= request('magasin');
        $dateappro= date('Y-m-d');
        $approvision=collect(request('approvision'))->chunk(2);

            DB::table('appro_cuisine')->insert(
                [
                    'date_appro' => $dateappro,
                    'etat' => "Proforma",
                    'magasin_id' => $magasin_id,
                   
                ]
            );

            $idLastappro = DB::table('appro_cuisine')->get()->last()->id;

            $id2= strlen($idLastappro);

            if ($id2==1){

                DB::table('appro_cuisine')->where('id', $idLastappro)->update(
                    [
                        'numero' => 'APPRO_CUISINE-000'.$idLastappro,
                    ]
                );

            } elseif ($id2==2){

                DB::table('appro_cuisine')->where('id', $idLastappro)->update(
                    [
                        'numero' => 'APPRO_CUISINE-00'.$idLastappro,
                    ]
                );

            } elseif ($id2==3){

                DB::table('appro_cuisine')->where('id', $idLastappro)->update(
                    [
                        'numero' => 'APPRO_CUISINE-0'.$idLastappro,
                    ]
                );

            } else {

                DB::table('appro_cuisine')->where('id', $idLastappro)->update(
                    [
                        'numero' => 'APPRO_CUISINE-'.$idLastappro,
                    ]
                );
            }

        $i=0;

        foreach ($approvision as $key=> $approvisions){
			
			//$qtesto =  DB::table('stock_magasin')->where('produit_id', $approvisions[0 +$i])->orderBydesc('id')->take(1)->first()->apres;
            
			$qt1 = DB::table('stock_magasin')->where('produit_id', $approvisions[0 +$i])->where('type_operation', 'Inventaire')->where('magasin_id', $magasin_id)->sum('qte');
			$qt2 = DB::table('stock_magasin')->where('produit_id', $approvisions[0 +$i])->where('type_operation', 'Appro')->where('magasin_id', $magasin_id)->sum('qte');
			$qtesto = $qt1 + $qt2;
			
			$qcount =  DB::table('stock_cuisine')->where('produit_id', $approvisions[0 +$i])->get()->count();
			if ($qcount > 0){
			$qtesto2 =  DB::table('stock_cuisine')->where('produit_id', $approvisions[0 +$i])->orderBydesc('id')->take(1)->first()->apres;
			} else {
				$qtesto2 = 0;
			}
            $desi =  DB::table('produit_vendable')->where('id', $approvisions[0 +$i])->first()->designation;

            if($qtesto < $approvisions[1 +$i]){

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

                DB::table('ligne_appro_cuisine')->where('appro_id', $idLastappro)->delete();

                $message = [
                    "title" => 'Erreur d\'enregistrement',
                    "type" => 'error',
                    "response" => 'Stock '.' '.$desi.' '.'insuffisant dans le magasin sélectionné',
                ];
    
                return $message;

            }else{

                DB::table('ligne_appro_cuisine')->insert(
                    [
                        'appro_id' => $idLastappro,
						'magasin_id' => $magasin_id,
                        'etat' => "Proforma",
						'date' => $dateappro,
                        'produit_id' => $approvisions[0 +$i],
                        'qte' => $approvisions[1 +$i],
						'en_stock' => $qtesto2,
						'apres' => ($qtesto2 + $approvisions[1 +$i]),
                    ]
                );
    
                $i += count($approvisions->toArray());

            }

        }

            $message = [
                "title" => 'Succès',
                "type" => 'success',
                "response" => 'Enregistrement effectué avec succès !',
            ];

            return $message;
    }

	// Fonction supprimer
	function supprimer () {

        $id=request('id');

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

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

        return redirect()->route('nouveau_appro_cuisine')->with('status', 'Suppression effectuée avec succes');
      

    }
	
}
