<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Classes\ChiffreEnLettre;
use DB;
use PDF;

class RetourapproemballageController extends Controller
{
	function voir ($id) {

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

		$id=request('id');	
		
		DB::table('retourapproemballage')->where('id', $id)->update(
			[
				'etat' => "Validée",
			]
		);
		DB::table('ligneretourapproemballage')->where('retourapproemballage_id', $id)->update(
			[
				'etat' => "Validée",
			]
		);

		$vo = DB::table('ligneretourapproemballage')->where('retourapproemballage_id', $id)->where('etat','Validée')->get();

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


		foreach($vo as $vos){
					
					//$sg = DB::table('emballage')->where('id', $vos->emballage_id)->first()->stock_general;
					$qtestoc = DB::table('stock_emballage')->where('emballage_id', $vos->emballage_id)->first();
					if ($qtestoc){
					    $qtesto = DB::table('stock_emballage')->where('emballage_id', $vos->emballage_id)->orderBydesc('id')->take(1)->first()->apres;
					} else {
						$qtesto = 0;
					}
					$qteval = $vos->qte;
					DB::table('stock_emballage')->insert(
							[
								'date' => $today,
								'emballage_id' => $vos->emballage_id,
								'qte' => $qteval,
								'type_operation' => 'Retourappro',
								'en_stock' => $qtesto,
								'apres' => ($qtesto - $qteval),
							]
						);
						
			}
		return redirect()->route('retourapproemballage')->with('status', 'Retour validé avec succès !');
	}
	
	function annuler () {

		$id=request('id');	
		
		DB::table('retourapproemballage')->where('id', $id)->update(
			[
				'etat' => "Annulée",
			]
		);
		DB::table('ligneretourapproemballage')->where('retourapproemballage_id', $id)->update(
			[
				'etat' => "Annulée",
			]
		);

		$vo = DB::table('ligneretourapproemballage')->where('retourapproemballage_id', $id)->get();

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


		foreach($vo as $vos){
					
					//$sg = DB::table('emballage')->where('id', $vos->emballage_id)->first()->stock_general;
					$qtestoc = DB::table('stock_emballage')->where('emballage_id', $vos->emballage_id)->first();
					if ($qtestoc){
					    $qtesto = DB::table('stock_emballage')->where('emballage_id', $vos->emballage_id)->orderBydesc('id')->take(1)->first()->apres;
					} else {
						$qtesto = 0;
					}
					$qteval = $vos->qte;
					DB::table('stock_emballage')->insert(
							[
								'date' => $today,
								'emballage_id' => $vos->emballage_id,
								'qte' => $qteval,
								'type_operation' => '  Annulationretourappro',
								'en_stock' => $qtesto,
								'apres' => ($qtesto + $qteval),
							]
						);
						
			}
		return redirect()->route('retourapproemballage')->with('status', 'Retour annulé avec succès !');
	}
	function supprimer () {

        $id=request('id');
		DB::table('ligneretourapproemballage')->where('retourapproemballage_id', $id)->delete();
		DB::table('retourapproemballage')->where('id', $id)->delete();
        return redirect()->route('retourapproemballage')->with('status', 'Suppression effectuée avec succès');
    }
	public function detail($id)
	{
        if(session('user')== null) {
            return redirect('/');
        }

		$detail=DB::table('ligneretourapproemballage')->where('retourapproemballage_id',$id)->get();
		$totqte = DB::table('ligneretourapproemballage')->where('retourapproemballage_id',$id)->get()->sum('qte');
		$totml = DB::table('ligneretourapproemballage')->where('retourapproemballage_id',$id)->get()->sum('montligne');
		return view('retourapproemballage/detail', [
			'detail'=> $detail,
			'totqte'=> $totqte,
			'totml'=> $totml,
		]);
	}
    public function nouveau(){
		
        if(session('user')== null) {
            return redirect('/');
        }
		
		//$emballage=DB::table('emballage_vendable')->where('stock_general', '>', 0)->orderby('designation','asc')->get();
		$emballage=DB::table('emballage')->orderby('designation','asc')->get();
		$retourapproemballage = DB::table('retourapproemballage')->orderBydesc('id')->get();
		return view('retourapproemballage/nouveau', [
			'emballage'=> $emballage,
			'retour'=> $retourapproemballage,
		]);
	}
	function enregistrer()
    {

		$dat = date('d/m/Y');

        $hour = date('H:i:s');

		$today = implode('-', array_reverse(explode('/', $dat)));
		$todayyy = date('Y-m-d'); 
		$numfact=request('numfact');
		$peps=collect(request('peps'))->chunk(2);
		$appro = DB::table('appro_emballage')->where('numero', $numfact)->where('etat','Validée')->first();
	    if ($appro){
		DB::table('retourapproemballage')->insert(
				[
					'date' => $today,
					'numfact' => $numfact,
					'user_id' => session('user')->id,
				]
			);

			$idLastretourapproemballage = DB::table('retourapproemballage')->get()->last()->id;
			$idLast= strlen($idLastretourapproemballage);
	
			if ($idLast==1){
				DB::table('retourapproemballage')->where('id', $idLastretourapproemballage)->update(
					[
						'numero' => '000'.$idLastretourapproemballage,
					]
				);
			} elseif ($idLast==2){
		
				DB::table('retourapproemballage')->where('id', $idLastretourapproemballage)->update(
					[
						'numero' => '00'.$idLastretourapproemballage,
					]
				);
			} elseif ($idLast==3){
				DB::table('retourapproemballage')->where('id', $idLastretourapproemballage)->update(
					[
						'numero' => '0'.$idLastretourapproemballage,
					]
				);
			} else {
				DB::table('retourapproemballage')->where('id', $idLastretourapproemballage)->update(
					[
						'numero' => $idLastretourapproemballage,
					]
				);
			}
	
			$i=0;
			
	foreach ($peps as $key=> $pep){
	            $verif = DB::table('ligne_appro_emballage')->where('appro_emballage_id', $appro->id)->where('emballage_id', $pep[0 +$i])->first();
	            if ($verif){
				$desi =  DB::table('emballage')->where('id', $pep[0 +$i])->first()->designation;
				$nbreencarton =  DB::table('emballage')->where('id', $pep[0 +$i])->first()->nbreencarton;
				$pa = DB::table('ligne_appro_emballage')->where('appro_emballage_id', $appro->id)->where('emballage_id', $pep[0 +$i])->first()->pa;
				DB::table('ligneretourapproemballage')->insert(
						[
							'retourapproemballage_id' => $idLastretourapproemballage,
							'emballage_id' => $pep[0 +$i],
							'qte' => ($pep[1 +$i] * $nbreencarton),
							'montligne' => ($pep[1 +$i] * $pa * $nbreencarton),
							'date' => $todayyy, 
							'user_id' => session('user')->id,
						]
					);
						$i += count($pep->toArray()); 
			$message = [
				"title" => 'success',
				"type" => 'success',
				"response" => 'Opération effectué avec succes !',
			];
	
			return $message;
	        
	} else {
	    DB::table('retourapproemballage')->where('id', $idLastretourapproemballage)->delete();
	    DB::table('ligneretourapproemballage')->where('retourapproemballage_id', $idLastretourapproemballage)->delete();
	    $message = [
				"title" => 'Attention',
				"type" => 'error',
				"response" => 'L\'emballage sélectionné ne fait pas partie du numéro de commande saisi !',
			];
	
			return $message;
	}
	}
	}else {
	    $message = [
				"title" => 'Attention',
				"type" => 'error',
				"response" => 'Le numéro de facture appro matière saisi est inexistant !',
			];
	
			return $message;
	}
        
    }

		
}
