<?php

namespace App\Http\Controllers;

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

class DestockageController extends Controller
{
    
    public function nouveau(){
		
        if(session('user')== null) {
            return redirect('/');
        }
		
		$produit=DB::table('produit_vendable')->orderby('designation','asc')->get();
		//$client=DB::table('client')->take(1)->get();
		//$remise=DB::table('remise')->where('magasin_id', session('user')->magasin_id)->where('etat', 'Actif')->orderbydesc('id')->get();
		//$categorie=DB::table('categorie')->get();
		$destockage = DB::table('destockage')->orderBydesc('id')->take(200)->get();
		return view('destockage/nouveau', [
			'produit'=> $produit,
			'destockage'=> $destockage,
			//'client'=> $client,
			//'cat'=> $categorie,
			//'remise'=> $remise,
		]);
	}
	
	function enregistrer()
    {
		$dat=request('datet');
		$observations=request('observations');
        $hour = date('H:i:s');
		$today = implode('-', array_reverse(explode('/', $dat)));
		$todayyy = date('Y-m-d'); 
		//$typedestockage=request('typedestockage');
		$nomcli=request('nomcli');
		$contact=request('contact');
		$remise=request('remise');
		$taxes=request('taxes');
		$peps=request('item');
		if (count($peps) > 0 && $peps != null){
		DB::table('destockage')->insert(
				[
					'date' => $today,
					'user_id' => session('user')->id,
					'magasin_id' => session('user')->magasin_id,
					'observations' => $observations,
				]
			);

			$idLastdestockage = DB::table('destockage')->get()->last()->id;
			$idLast= strlen($idLastdestockage);
	
			$i=0;
			
	foreach ($peps as $key=> $pep){
				$desi =  DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->desi;
				$pr = DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->pr;
				$qtestoc =  DB::table('stock_magasin')->where('produit_id', $pep[0 +$i])->where('magasin_id', session('user')->magasin_id)->orderBydesc('id')->get()->count();
                /*if ($qtestoc == 0){
					DB::table('destockage')->where('id', $idLastdestockage)->delete();
                    DB::table('ligne_destockage')->where('destockage_id', $idLastdestockage)->delete();
					$message = [
                        "title" => 'Erreur de stock',
                        "type" => 'error',
                        "response" => 'Aucun stock de '.$desi.' n\'est disponible dans votre magasin !',
                    ];
                    return $message;
				} else {*/
				//$qtesto =  DB::table('stock_magasin')->where('produit_id', $pep[0 +$i])->where('magasin_id', session('user')->magasin_id)->orderBydesc('id')->take(1)->first()->apres;
				$categorie_id =  DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->categorie_id;
                /*if($qtesto < $pep[1 +$i]){
    
                    DB::table('destockage')->where('id', $idLastdestockage)->delete();
                    DB::table('ligne_destockage')->where('destockage_id', $idLastdestockage)->delete();
    
                    $message = [
                        "title" => 'Erreur d\'enregistrement',
                        "type" => 'error',
                        "response" => 'Stock '.' '.$desi.' '.'insuffisant ! Stock restant : '.$qtesto,
                    ];
        
                    return $message;
    
                }else{*/
				
					//if ($typedestockage=="Détail"){//
						$prix =  DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->prix_detail;
					/*} else {
						$prix =  DB::table('produit_vendable')->where('id', $pep[0+$i])->first()->prix_gros;
					}  */ 
					DB::table('ligne_destockage')->insert(
						[
								'destockage_id' => $idLastdestockage,
								'categorie_id' => $categorie_id,
								'produit_id' => $pep[0 +$i],
								'qte' => $pep[1 +$i],
								'pv' => $pep[2 +$i],
								'pr' => $pr,
								'montligne' => ($pep[1 +$i] * $pep[2 +$i]),
								'montlignepr' => ($pep[1 +$i] * $pr),
								'date' => $today, 
								'user_id' => session('user')->id,
								'magasin_id' => session('user')->magasin_id,
						]
					);
				
					$i; 
						
				//}  
				//}
            	}
			$mht = DB::table('ligne_destockage')->where('destockage_id', $idLastdestockage)->get()->sum('montligne');
			DB::table('destockage')->where('id', $idLastdestockage)->update(
				[
					'montant' => $mht,
				]
			);
					
			
		}

			return redirect()->route('nouveau-destockage')->with('status', 'Enregistrement effectué avec succès !');
	}
	
	
	function valider () {

		$id=request('id');	
		//$montantr=request('montantr');	
		//$smt = DB::table('ligne_destockage')->where('destockage_id', $id)->get()->sum('montligne');
		$smt = DB::table('destockage')->where('id', $id)->first()->montant;
		/*if ($smt > $montantr){
			return redirect()->route('destockage')->with('error', 'Facture non validée car le montant donné par le client est inférieure au solde de la facture !');
		} else {*/
		//$relica = $montantr - $smt;
		DB::table('destockage')->where('id', $id)->update(
			[
				'etat' => "Validée",
				//'montant_donne' => $montantr,
				//'relicat' => $relica,
			]
		);
		DB::table('ligne_destockage')->where('destockage_id', $id)->update(
			[
				'etat' => "Validée",
			]
		);

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

		$today = DB::table('destockage')->where('id', $id)->first()->date; 
		$des = DB::table('destockage')->where('id', $id)->first(); 
        

		foreach($vo as $vos){
					
					$sg = DB::table('produit_vendable')->where('id', $vos->produit_id)->first()->stock_general;
					$qtestoc = DB::table('stock_magasin')->where('produit_id', $vos->produit_id)->where('magasin_id', session('user')->magasin_id)->orderBydesc('id')->get()->count();
					if ($qtestoc > 0){
					    $qtesto = DB::table('stock_magasin')->where('produit_id', $vos->produit_id)->where('magasin_id', session('user')->magasin_id)->orderBydesc('id')->take(1)->first()->apres;
					} else {
					    $qtesto = 0;
					}
					$qteval = $vos->qte;
		
					DB::table('produit_vendable')->where('id', $vos->produit_id)->update(
						[
							'stock_general' => ($sg - $qteval),
						]
					);
					DB::table('stock_magasin')->insert(
							[
								'date' => $today,
								'produit_id' => $vos->produit_id,
								'qte' => $qteval,
								'type_operation' => 'Destockage',
								'en_stock' => $qtesto,
								'apres' => ($qtesto - $qteval),
								'stock_general' => ($sg - $qteval),
								'magasin_id' => session('user')->magasin_id,
								'user_id' => session('user')->id,
								'notes' => $des->observations,
							]
						);
						//BENEF
						/*$tmontligne = DB::table('stock_magasin')->where('produit_id', $vos->produit_id)->whereIn('type_operation',['Inventaire','TransfertRecu','Production'])->where('etat', 'Validée')->get()->sum('montligne');
						$tqte = DB::table('stock_magasin')->where('produit_id', $vos->produit_id)->whereIn('type_operation',['Inventaire','TransfertRecu','Production'])->where('etat', 'Validée')->get()->sum('qte');
						if ($tqte >0){
						$pru = $tmontligne/$tqte;
						} else {
						    	$pru = 0;
						}*/
						$pru = DB::table('produit_vendable')->where('id', $vos->produit_id)->first()->pr;
						$pv = DB::table('ligne_destockage')->where('produit_id', $vos->produit_id)->where('destockage_id', $id)->first()->pv;
						//$remise = DB::table('ligne_destockage')->where('produit_id', $vos->produit_id)->where('destockage_id', $id)->first()->remise;
						$benef = ($pv - $pru) * $qteval;
						DB::table('ligne_destockage')->where('destockage_id', $id)->where('produit_id', $vos->produit_id)->update(
							[
								'benef' => $benef,
							]
						);
						/*$sv = DB::table('produit_vendable')->where('id',  $vos->produit_id)->first()->totvendu;
						DB::table('produit_vendable')->where('id',  $vos->produit_id)->update(
							[
								'totvendu' => $sv + $qteval,
							]
						);*/
			        }
			return redirect()->route('nouveau-destockage')->with('status', 'destockage validé avec succès !');
		//return redirect()->route('facturedestockage',['id'=>$id]);
		//}
	}
	
	function voir ($id) {

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

        $id=request('id');

        $vo = DB::table('ligne_destockage')->where('destockage_id', $id)->where('etat','En cours')->delete();
        $vo2 = DB::table('destockage')->where('id', $id)->delete();

        return redirect()->route('nouveau-destockage')->with('status', 'Suppression effectuée avec succès');
    }

	
	
    
}