<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

use PDF;

class VenteclientController extends Controller
{
    //
	 public function vente2($id){
		
        if(session('user')== null) {
            return redirect('/');
        }
		
		$produit=DB::table('produit_vendable')->orderBy('designation', 'asc')->get();
		$client=DB::table('client_externe')->get();
		$vente1=DB::table('vente_client')->where('id',$id)->first();
		$ligne_vente=DB::table('ligne_vente_client')->where('vente_id',$id)->get();
		$categorie=DB::table('categorie')->get();
		$remise=DB::table('remise')->where('magasin_id', session('user')->magasin_id)->where('etat', 'Actif')->orderBydesc('id')->get();
		if (session('user')->roles=="Admin"){
		$ventes = DB::table('vente_client')->where('etat2', 'En cours')->orderBydesc('id')->get();
		} elseif (session('user')->roles=="Gérant") {
			$ventes = DB::table('vente_client')->where('etat2', 'En cours')->where('magasin_id', session('user')->magasin_id)->orderBydesc('id')->get();
		} else {
			$ventes = DB::table('vente_client')->where('etat2', 'En cours')->where('user_id', session('user')->id)->orderBydesc('id')->get();
		}
		return view('client_externe/modifier2', [
			'produit'=> $produit,
			'client'=> $client,
			'ventes'=> $ventes,
			'cat'=> $categorie,
			'remise'=> $remise,
			'vente1'=> $vente1,
			'ligne_vente'=> $ligne_vente,
			'vente_id'=> $id,
		]);
    }
	function fg()
    {
        if(session('user')== null) {
            return redirect('/');
		}
	
		$produit=DB::table('produit_vendable_fini')->get();
		$cli=DB::table('client_externe')->get();
		$serveuse=DB::table('employe')->where('fonction', 'Serveuse')->get();
		$ventes = DB::table('factureglobale')->orderBydesc('id')->take(5)->get();
		return view('client_externe/facture-globale',[
            'produit' => $produit,
            'cli' => $cli,
            'ventes' => $ventes,
            'serveuse' => $serveuse,
        ]);	
    }
    function listepaiement()
    {
        if(session('user')== null) {
            return redirect('/');
		}
            $paie = DB::table('paiement_client')->orderBydesc('id')->get();
		
		return view('client_externe/liste_paiement',[
            
            'paie' => $paie,
        ]);	
    }
    function VoirSuppressionpaie ($id) {

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

            'pai' => $pai,
        ];
    }

    function supprimerpaie () {

		$id=request('id');

        $idclient = DB::table('paiement_client')->where('id', $id)->first()->client_id;

        DB::table('paiement_client')->where('id', $id)->delete();
        
        DB::table('solde')->where('paie_cli_ext_id',$id)->delete();
        
        return redirect()->route('paiementclient', ['id' => $idclient])->with('status', 'Suppression effectuée avec succes');
	}


    function facturepaie($id)
    {
        if(session('user')== null) {
            return redirect('/');
        }
        $paie = DB::table('paiement_client')->where('id',$id)->first();
        //$vente_id = DB::table('paiement_client')->where('id',$id)->first()->vente_id;
        $client = DB::table('paiement_client')->where('id',$id)->first()->client_id;
        $ligne = DB::table('ligne_vente_client')->where('client_id',$client)->where('etat','Validée')->get();
        //$paye = DB::table('paiement_fou')->where('appro_id',$appro_id)->get()->sum('montant');
        //$vente = DB::table('vente_client')->where('client_id',$client)->where('etat','Validée')->first();
        $vente = DB::table('vente_client')->where('id',$paie->vente_id)->get()->sum('montant');
		$pdf = \App::make('dompdf.wrapper');
        $pdf = PDF::loadView('/client_externe/facture_paie', [

            'paie' => $paie,
            'ligne' => $ligne,
            'vente' => $vente,
            'client' => $client,
            //'paye' => $paye,
        ])->setPaper('A5', 'portrait');
        
        return $pdf->stream();
    }
	
	function facturepaiefinale($id)
    {
        if(session('user')== null) {
            return redirect('/');
        }
        $paie = DB::table('paiement_client')->where('client_id',$id)->get();
        $tpaie = DB::table('paiement_client')->where('client_id',$id)->get()->sum('montant');
        $client = DB::table('client_externe')->where('id',$id)->first();
        $vente = DB::table('vente_client')->where('client_id',$id)->where('etat','Validée')->get();
		$tvente = DB::table('vente_client')->where('client_id',$id)->where('etat','Validée')->get()->sum('montant');
		$pdf = \App::make('dompdf.wrapper');
        $pdf = PDF::loadView('/client_externe/facture-paie-globale', [

            'paie' => $paie,
            'tpaie' => $tpaie,
            'vente' => $vente,
            'tvente' => $tvente,
            'client' => $client,
        ])->setPaper('A4', 'portrait');
        
        return $pdf->stream();
    }
    
    function facturepaiefinale2($id)
    {
        if(session('user')== null) {
            return redirect('/');
        }
        $paie = DB::table('paiement_client')->where('client_id',$id)->get();
        $tpaie = DB::table('paiement_client')->where('client_id',$id)->get()->sum('montant');
        $client = DB::table('client_externe')->where('id',$id)->first();
        $vente = DB::table('vente_client')->where('client_id',$id)->where('etat','Validée')->get();
		$tvente = DB::table('vente_client')->where('client_id',$id)->where('etat','Validée')->get()->sum('montant');
		$pdf = \App::make('dompdf.wrapper');
        $pdf = PDF::loadView('/client_externe/facture-paie-globale-en', [

            'paie' => $paie,
            'tpaie' => $tpaie,
            'vente' => $vente,
            'tvente' => $tvente,
            'client' => $client,
        ])->setPaper('A4', 'portrait');
        
        return $pdf->stream();
    }

    function pclient($id)
    {
        if(session('user')== null) {
            return redirect('/');
		}
		//$vente = DB::table('vente_client')->where('id',$id)->first();
		$cli = DB::table('client_externe')->where('id',$id)->first();
		$paie = DB::table('paiement_client')->where('client_id',$id)->get();
		return view('client_externe/paiement',[
            'id' => $id,
            'paie' => $paie,
            'cli' => $cli,
        ]);	
    }

	public function venteclient($id){
      
        if(session('user')== null) {
            return redirect('/');
        }
		$vente= DB::table('vente_client')->where('client_id',$id)->where('etat','Validée')->get();
		$total= DB::table('vente_client')->where('client_id',$id)->where('etat','Validée')->get()->sum('montant');
		$totalpaie= DB::table('paiement_client')->where('client_id',$id)->get()->sum('montant');
		$reste = $total - $totalpaie;
        $client= DB::table('client_externe')->where('id',$id)->first()->noms;
        
         return view('client_externe/liste_vente',[

		'vente' => $vente,
		'client' => $client,
		'total' => $total,
		'totalpaie' => $totalpaie,
        'reste' => $reste,
        
	  ]);
	}


	function facturevente3($id)
    {
		if(session('user')== null) {
            return redirect('/');
		}
		$pdf = \App::make('dompdf.wrapper');
		$vente = DB::table('vente_client')->where('id',$id)->first();

		$ligne = DB::table('ligne_vente_client')->where('vente_id',$id)->get();
		$lignetax = DB::table('ligne_taxe_client')->where('vente_id',$id)->get();
		$ligneM = DB::table('ligne_vente_client')->where('vente_id',$id)->get()->sum('montligne');
		$total_article = DB::table('ligne_vente_client')->where('vente_id',$id)->sum('qte');
		$pdf = \App::make('dompdf.wrapper');
        $pdf = PDF::loadView('/client_externe/facture3', [

            'ligne' => $ligne,
			'vente' => $vente,
			'ligneM' => $ligneM,
			'lignetax' => $lignetax,
			'total_article' => $total_article,
		])->setPaper('A5', 'portrait');
		
        return $pdf->stream();
    }
    function facturevente($id)
    {
		if(session('user')== null) {
            return redirect('/');
		}
		
		$vente = DB::table('vente_client')->where('id',$id)->first();

		$ligne = DB::table('ligne_vente_client')->where('vente_id',$id)->get();
		$lignetax = DB::table('ligne_taxe_client')->where('vente_id',$id)->get();
		$ligneM = DB::table('ligne_vente_client')->where('vente_id',$id)->get()->sum('montligne');
		$total_article = DB::table('ligne_vente_client')->where('vente_id',$id)->sum('qte');
		$pdf = \App::make('dompdf.wrapper');
        $pdf = PDF::loadView('/client_externe/facture', [

            'ligne' => $ligne,
			'vente' => $vente,
			'ligneM' => $ligneM,
			'lignetax' => $lignetax,
			'total_article' => $total_article,
		])->setPaper('A5', 'portrait');
		
        return $pdf->stream();
    }
	function facturevente2($id)
    {
		if(session('user')== null) {
            return redirect('/');
		}
		$pdf = \App::make('dompdf.wrapper');
		$vente = DB::table('vente_client')->where('id',$id)->first();

		$ligne = DB::table('ligne_vente_client')->where('vente_id',$id)->get();
		$lignetax = DB::table('ligne_taxe_client')->where('vente_id',$id)->get();
		$ligneM = DB::table('ligne_vente_client')->where('vente_id',$id)->get()->sum('montligne');
		$total_article = DB::table('ligne_vente_client')->where('vente_id',$id)->sum('qte');
		$pdf = \App::make('dompdf.wrapper');
        $pdf = PDF::loadView('/client_externe/facture_a5', [

            'ligne' => $ligne,
			'vente' => $vente,
			'ligneM' => $ligneM,
			'lignetax' => $lignetax,
			'total_article' => $total_article,
		])->setPaper('A5', 'portrait');
		
        return $pdf->stream();
    }
    function facturevente21($id)
    {
		if(session('user')== null) {
            return redirect('/');
		}
		$pdf = \App::make('dompdf.wrapper');
		$vente = DB::table('vente_client')->where('id',$id)->first();

		$ligne = DB::table('ligne_vente_client')->where('vente_id',$id)->get();
		$lignetax = DB::table('ligne_taxe_client')->where('vente_id',$id)->get();
		$ligneM = DB::table('ligne_vente_client')->where('vente_id',$id)->get()->sum('montligne');
		$total_article = DB::table('ligne_vente_client')->where('vente_id',$id)->sum('qte');
		$pdf = \App::make('dompdf.wrapper');
        $pdf = PDF::loadView('/client_externe/facture_a5_en', [

            'ligne' => $ligne,
			'vente' => $vente,
			'ligneM' => $ligneM,
			'lignetax' => $lignetax,
			'total_article' => $total_article,
		])->setPaper('A5', 'portrait');
		
        return $pdf->stream();
    }


    function VoirSuppression ($id) {

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

            'ven' => $ven,
        ];
    }
	function voirsuppresiondetail ($id) {

        $detailvente= DB::table('ligne_vente_client')->where('id',$id)->first();
        return [
            'detailventec' => $detailvente,
        ];
	}
	function supprimerdetail () {

		$id=request('id');
		
		$idvente = DB::table('ligne_vente_client')->where('id', $id)->first()->vente_id;
		$total_taux = DB::table('vente_client')->where('id', $idvente)->first()->total_taux;
		
        DB::table('ligne_vente_client')->where('id', $id)->delete();
        DB::table('ligne_taxe_client')->where('vente_id', $idvente)->delete();
		
		
		$verif  = DB::table('ligne_vente_client')->where('vente_id', $idvente)->get()->count();
		if ($verif > 0){
			$mht = DB::table('ligne_vente_client')->where('vente_id', $idvente)->get()->sum('montligne');
			$rem2 = DB::table('vente_client')->where('id', $idvente)->first()->remise;
			$mtremise = $rem2;
			$mht1 = $mht - $mtremise;
			DB::table('vente_client')->where('id', $idvente)->update(
						[
							'montant_ht' => $mht,
							'remise' => $rem2,
							'montant_remise' => $mtremise,
							'net_com' => $mht1,
						]
					);
					
			$tau = DB::table('taxes')->get();
			$nc = DB::table('vente_client')->where('id', $idvente)->first()->net_com;
			$today = date('Y-m-d'); 
			foreach($tau as $taux){
				DB::table('ligne_taxe_client')->insert(
						[
							'vente_id' => $idvente,
							'taux' => $taux->taux,
							'montligne' => $nc * $taux->taux,
						]
					);
			}
			$sumtaux = DB::table('ligne_taxe_client')->where('vente_id', $idvente)->get()->sum('montligne');
			$ttc = $sumtaux + $nc;
			DB::table('vente_client')->where('id', $idvente)->update(
						[
							'total_taux' => $sumtaux,
							'montant' => $ttc,
						]
					);
		} else {
			DB::table('vente_client')->where('id', $idvente)->delete();
		}
        return redirect()->route('ligne-vente-client', ['id' => $idvente])->with('status', 'Suppression effectuée avec succes');
    }

    function supprimer () {

        $id=request('id');

        DB::table('ligne_vente_client')->where('vente_id', $id)->delete();
        DB::table('ligne_taxe_client')->where('vente_id', $id)->delete();

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

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

    public function consulter($id)
	{
        if(session('user')== null) {
            return redirect('/');
        }

		$detail=DB::table('ligne_vente_client')->where('vente_id',$id)->get();
		$totqte = DB::table('ligne_vente_client')->where('vente_id',$id)->get()->sum('qte');
		$totml = DB::table('ligne_vente_client')->where('vente_id',$id)->get()->sum('montligne');
		return view('client_externe/detail', [
			'detail'=> $detail,
			'totqte'=> $totqte,
			'totml'=> $totml,
		]);
	}

    function voirvalider ($id) {

        $vente= DB::table('vente_client')->where('id',$id)->first();
        return [
            'vente' => $vente,
        ];
	}



    function nouveau() {

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

        $client = DB::table('client_externe')->orderBydesc('id')->get();

        return view('client_externe/nouveau_client',[

            'client' => $client,

        ]);
    }

    function modifier($id) {

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

        $client = DB::table('client_externe')->orderBydesc('id')->get();

        $clien = DB::table('client_externe')->where('id', $id)->first(); 

        return view('client_externe/modification',[

            'client' => $client,
            'clien' => $clien,

        ]);
    }
	//MODIFICATION VENTES
	function modifvente($id) {
        if(session('user')== null) {
            return redirect('/');
        }
        $client = DB::table('client_externe')->get();
        $produit = DB::table('produit_vendable')->get();
        $vente = DB::table('vente_client')->where('id',$id)->first();
        $ligne_vente = DB::table('ligne_vente_client')->where('vente_id',$id)->get();
        return view('client_externe/modifier',[
            'client' => $client,
            'produit' => $produit,
            'ligne_vente' => $ligne_vente,
            'vente' => $vente,
        ]);
    }
	
	function modifventev($id)
    {
		$dat=request('datet');

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

		$today = implode('-', array_reverse(explode('/', $dat)));
		$todayyy = date('Y-m-d');
		$supl = DB::table('ligne_vente_client')->where('vente_id', $id)->delete();
		$sup2 = DB::table('ligne_taxe_client')->where('vente_id', $id)->delete();
		$typevente=request('typevente');
		$taxes=request('taxes');
		
		$remise=request('remise');
		$client=request('client');
        $peps=request('item');

        DB::table('vente_client')->where('id', $id)->update(
            [
					'date' => $todayyy,
					'remise' => $remise,
					'client_id' => $client,
            ]
        );
        $idLastvente = DB::table('vente_client')->where('id', $id)->first()->id;
        $date = DB::table('vente_client')->where('id', $id)->first();

		$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('vente_client')->where('id', $idLastvente)->delete();
                    //DB::table('ligne_vente_client')->where('vente_id', $idLastvente)->delete();
					$message = [
                        "title" => 'Erreur de stock',
                        "type" => 'error',
                        "response" => 'Aucun stock de '.$desi.' n\'est disponible dans ce dépôt !',
                    ];
        
                    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[1 +$i])->first()->categorie_id;
                /*if($qtesto < $pep[1 +$i]){
    
                    DB::table('vente_client')->where('id', $idLastvente)->delete();
                    DB::table('ligne_vente_client')->where('vente_id', $idLastvente)->delete();
    
                    $message = [
                        "title" => 'Erreur d\'enregistrement',
                        "type" => 'error',
                        "response" => 'Stock '.' '.$desi.' '.'insuffisant ! Stock restant : '.$qtesto,
                    ];
        
                    return $message;
    
                } else {*/
				
					/*if ($typevente == "Détail"){
                          $pv = DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->prix_detail;
                        }elseif ($typevente == "Semigros") {
                            $pv = DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->prix_semigros;
                        }else{
                            $pv = DB::table('produit_vendable')->where('id',$pep[0 +$i])->first()->prix_gros;
                        }*/
					$categorie_id =  DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->categorie_id;
					  
					DB::table('ligne_vente_client')->insert(
						[
								'vente_id' => $idLastvente,
								'produit_id' => $pep[0 +$i],
								'qte' => $pep[1 +$i],
								'reste' => $pep[1 +$i],
								'pv' => $pep[2 +$i],
								'pr' => $pr,
								'montligne' => ($pep[1 +$i] * $pep[2 +$i]),
								'montlignepr' => ($pep[1 +$i] * $pr),
								'date' => $todayyy, 
								'categorie_id' => $categorie_id,
								'user_id' => session('user')->id,
								'magasin_id' => session('user')->magasin_id,
						]
					);
						$i; 
				//}  
				//}
	}
			$mht = DB::table('ligne_vente_client')->where('vente_id', $idLastvente)->get()->sum('montligne');
			$rem2 = DB::table('vente_client')->where('id', $idLastvente)->first()->remise;
			$mtremise = $rem2;
			//$mht11 = ($mht * $mtremise)/100;
			$mht1 = $mht - $mtremise;
			DB::table('vente_client')->where('id', $idLastvente)->update(
						[
							'montant_ht' => $mht,
							'remise' => $remise,
							'montant_remise' => $mtremise,
							'net_com' => $mht1,
						]
					);
			$nc = DB::table('vente_client')->where('id', $idLastvente)->first()->net_com;
			/*$today = date('Y-m-d'); 
			foreach($tau as $taux){
				DB::table('ligne_taxe_client')->insert(
						[
							'vente_id' => $idLastvente,
							'taux' => $taux->taux,
							'montligne' => $nc * $taux->taux,
						]
					);
			}
			$sumtaux = DB::table('ligne_taxe_client')->where('vente_id', $idLastvente)->get()->sum('montligne');
			$ttc = $sumtaux + $nc;*/
			if ($taxes == "Oui"){
				$tau = DB::table('taxes')->get();
					foreach($tau as $taux){
					DB::table('ligne_taxe_client')->insert(
							[
								'vente_id' => $idLastvente,
								'taux' => $taux->taux,
								'montligne' => $nc * $taux->taux,
							]
						);
					}
					$sumtaux = DB::table('ligne_taxe_client')->where('vente_id', $idLastvente)->get()->sum('montligne');
					$ttc = $sumtaux + $nc;
				} else {
					$sumtaux = 0;
					$ttc = $nc;
				}
			DB::table('vente_client')->where('id', $idLastvente)->update(
						[
							'total_taux' => $sumtaux,
							'montant' => $ttc,
						]
					);
			
		return redirect()->route('vente_client')->with('status','Vente modifiée avec succès' );
	}
	
	

    public function liste(){

		if(session('user')== null) {
            return redirect('/');
        }
		
			$vente = DB::table('vente_client')->get();
			$plucked = $vente->pluck('client_id');
			//$client = DB::table('client_externe')->whereIn('id', $plucked)->get();
			$client = DB::table('client_externe')->orderBydesc('noms')->get();
			$cli= DB::table('client_externe')->get();
		
		return view('client_externe/liste', [
			'client'=> $client,
			'cli'=> $cli,
		]);
	}
	//CREANCES 
	public function creance(){

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

        $vente = DB::table('vente_client')->get();
        $totvente = DB::table('vente_client')->sum('montant');
        $totpaie = DB::table('paiement_client')->sum('montant');
        $client = DB::table('client_externe')->get();
		$cli= DB::table('client_externe')->get();
		return view('client_externe/creance', [
			'client'=> $client,
			'cli'=> $cli,
			'totvente'=> $totvente,
			'totpaie'=> $totpaie,
		]);
	}
	//CREANCES  RECH
	public function creancerech(){

		if(session('user')== null) {
            return redirect('/');
        }
		$date_debut = request('date_debut');
        $date_debut= explode('/', $date_debut);
        $date_debut= array_reverse($date_debut);
        $date_debut= implode('-', $date_debut);

        $date_fin = request('date_fin');
        $date_fin= explode('/', $date_fin);
        $date_fin= array_reverse($date_fin);
        $date_fin= implode('-', $date_fin);
		
		session(['date_debut'=> $date_debut]);
		session(['date_fin'=> $date_fin]);
		
		 if($date_fin > $date_debut || $date_fin == $date_debut){
        $vente = DB::table('vente_client')->whereBetween('date', [$date_debut,$date_fin])->get();
        $totvente = DB::table('vente_client')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		$totpaie = DB::table('paiement_client')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
        $client = DB::table('client_externe')->get();
		$cli= DB::table('client_externe')->get();
		$pdf = PDF::loadView('client_externe/creancepdf', [
			'client'=> $client,
			'cli'=> $cli,
			'date_debut'=> $date_debut,
			'date_fin'=> $date_fin,
			'totvente'=> $totvente,
			'totpaie'=> $totpaie,
		]);
		return $pdf->stream();
	} else 
			return redirect()->route('creance')->with('error', 'La date de fin ne doit pas dépasser la date de début!');
	}
	public function listerech(){

		if(session('user')== null) {
            return redirect('/');
		}
		$client = request('client');
		$date_debut = request('date_debut');
        $date_debut= explode('/', $date_debut);
        $date_debut= array_reverse($date_debut);
        $date_debut= implode('-', $date_debut);

        $date_fin = request('date_fin');
        $date_fin= explode('/', $date_fin);
        $date_fin= array_reverse($date_fin);
        $date_fin= implode('-', $date_fin);
		
		session(['date_debut'=> $date_debut]);
		session(['date_fin'=> $date_fin]);
		session(['client'=> $client]);
		if($date_fin > $date_debut || $date_fin == $date_debut){
		if ($client==0){
		$vente = DB::table('vente_client')->whereBetween('date', [$date_debut,$date_fin])->get();
		$total = DB::table('vente_client')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		$sommePaiement = DB::table('paiement_client')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		/*$ligne = DB::table('ligne_vente')->where('vente_id',$id)->where('etat','Validée')->get();
		$ligneM = DB::table('ligne_vente')->where('vente_id',$id)->where('etat','Validée')->get()->sum('montligne');*/
		} else {
		$vente = DB::table('vente_client')->where('client_id',$client)->whereBetween('date', [$date_debut,$date_fin])->get();
		$total = DB::table('vente_client')->where('client_id',$client)->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		$sommePaiement = DB::table('paiement_client')->where('client_id',$client)->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		/*$ligne = DB::table('ligne_vente')->where('vente_id',$id)->where('etat','Validée')->get();
		$ligneM = DB::table('ligne_vente')->where('vente_id',$id)->where('etat','Validée')->get()->sum('montligne');
		*/
		}
		$client2 = DB::table('client_externe')->get();
        return view('/client_externe/liste-recherche', [

            //'ligne' => $ligne,
			'vente' => $vente,
			'total' => $total,
			'client' => $client,
			'client2' => $client2,
			'date_debut' => $date_debut,
			'date_fin' => $date_fin,
			'sommePaiement' => $sommePaiement,
			//'ligneM' => $ligneM,
		]);
		
        return $pdf->stream();
	} else 
		 {
			return redirect()->route('liste_des_ventes_client')->with('error', 'La date de fin ne doit pas dépasser la date de début!');
	}}
	//Facture du mois client externe
	
	public function listerechpdf(){

		if(session('user')== null) {
            return redirect('/');
		}
		$date_debut = session('date_debut');
		$date_fin = session('date_fin');
		$client = session('client');
		if($date_fin > $date_debut || $date_fin == $date_debut){
		if ($client==0){
		$vente = DB::table('vente_client')->whereBetween('date', [$date_debut,$date_fin])->get();
		$total = DB::table('vente_client')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		$sommePaiement = DB::table('paiement_client')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		/*$ligne = DB::table('ligne_vente')->where('vente_id',$id)->where('etat','Validée')->get();
		$ligneM = DB::table('ligne_vente')->where('vente_id',$id)->where('etat','Validée')->get()->sum('montligne');*/
		} else {
		$vente = DB::table('vente_client')->where('client_id',$client)->whereBetween('date', [$date_debut,$date_fin])->get();
		$total = DB::table('vente_client')->where('client_id',$client)->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		$sommePaiement = DB::table('paiement_client')->where('client_id',$client)->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		/*$ligne = DB::table('ligne_vente')->where('vente_id',$id)->where('etat','Validée')->get();
		$ligneM = DB::table('ligne_vente')->where('vente_id',$id)->where('etat','Validée')->get()->sum('montligne');
		*/
		}
		$client2 = DB::table('client_externe')->get();
        $pdf = PDF::loadView('/client_externe/liste_rech', [

            //'ligne' => $ligne,
			'vente' => $vente,
			'total' => $total,
			'client' => $client,
			'client2' => $client2,
			'date_debut' => $date_debut,
			'date_fin' => $date_fin,
			'sommePaiement' => $sommePaiement,
			//'ligneM' => $ligneM,
		]);
		
        return $pdf->stream();
	} else 
		 {
			return redirect()->route('liste_des_ventes_client')->with('error', 'La date de fin ne doit pas dépasser la date de début!');
	}}
	//ETAT RECAPITULATIF
		public function listerechpdf2(){

		if(session('user')== null) {
            return redirect('/');
		}
		$date_debut = session('date_debut');
		$date_fin = session('date_fin');
		$client = session('client');
		if($date_fin > $date_debut || $date_fin == $date_debut){
		if ($client==0){
		$vente = DB::table('vente_client')->whereBetween('date', [$date_debut,$date_fin])->get();
		$paiement = DB::table('paiement_client')->whereBetween('date', [$date_debut,$date_fin])->get();
		$total = DB::table('vente_client')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		$sommePaiement = DB::table('paiement_client')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		/*$ligne = DB::table('ligne_vente')->where('vente_id',$id)->where('etat','Validée')->get();
		$ligneM = DB::table('ligne_vente')->where('vente_id',$id)->where('etat','Validée')->get()->sum('montligne');*/
		} else {
		$vente = DB::table('vente_client')->where('client_id',$client)->whereBetween('date', [$date_debut,$date_fin])->get();
		$paiement = DB::table('paiement_client')->where('client_id',$client)->whereBetween('date', [$date_debut,$date_fin])->get();
		$total = DB::table('vente_client')->where('client_id',$client)->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		$sommePaiement = DB::table('paiement_client')->where('client_id',$client)->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		/*$ligne = DB::table('ligne_vente')->where('vente_id',$id)->where('etat','Validée')->get();
		$ligneM = DB::table('ligne_vente')->where('vente_id',$id)->where('etat','Validée')->get()->sum('montligne');
		*/
		}
		$client2 = DB::table('client_externe')->get();
        $pdf = PDF::loadView('/client_externe/liste_rech2', [

            //'ligne' => $ligne,
			'vente' => $vente,
			'paiement' => $paiement,
			'total' => $total,
			'client' => $client,
			'client2' => $client2,
			'date_debut' => $date_debut,
			'date_fin' => $date_fin,
			'sommePaiement' => $sommePaiement,
			//'ligneM' => $ligneM,
		]);
		
        return $pdf->stream();
	} else 
		 {
			return redirect()->route('liste_des_ventes_client')->with('error', 'La date de fin ne doit pas dépasser la date de début!');
	}}

    public function vente(){
		
        if(session('user')== null) {
            return redirect('/');
        }
		
		$produit=DB::table('produit_vendable')->orderBy('designation', 'asc')->get();
		$client=DB::table('client_externe')->get();
		$categorie=DB::table('categorie')->get();
		$remise=DB::table('remise')->where('magasin_id', session('user')->magasin_id)->where('etat', 'Actif')->orderBydesc('id')->get();
		if (session('user')->roles=="Admin"){
		$ventes = DB::table('vente_client')->where('etat2', 'En cours')->where('etat', '!=', 'Annulée')->orderBy('etat', 'ASC')->get();
		} elseif (session('user')->roles=="Gérant") {
			$ventes = DB::table('vente_client')->where('etat2', 'En cours')->where('magasin_id', session('user')->magasin_id)->orderBydesc('id')->get();
		} else {
			$ventes = DB::table('vente_client')->where('etat2', 'En cours')->where('user_id', session('user')->id)->orderBydesc('id')->get();
		}
		
		
		
		
		
		if (session('user')->roles== "Admin"){
		    $magasin = DB::table('magasin')->orderBydesc('id')->get();
            $depot = DB::table('depot')->orderBydesc('id')->get();
		}else{
		    $magasin = DB::table('magasin')->where('id', session('user')->magasin_id)->get();
            $depot = DB::table('depot')->orderBydesc('id')->get();
		}
		
		//dd(sizeof($magasin));
		
		return view('client_externe/vente', [
		    'magasin' => $magasin,
            'depot' => $depot,
			'produit'=> $produit,
			'client'=> $client,
			'ventes'=> $ventes,
			'cat'=> $categorie,
			'remise'=> $remise,
		]);
    }
    

    function enregistrerclient (Request $request){

        $nom= request("nom");
        $nom = strtoupper($nom);
        $tel= request("tel");
        $adresse= request("adresse");
             
        $veri = DB::table('client_externe')->where('noms', $nom)->where('tel', $tel)->first();

            if($veri){
                $message = [
                    "title" => 'Error',
                    "type" => 'error',
                    "response" => 'Ce client a été déjà enregistrée !',
                ];
    
                return $message;
            }

            else{
                  
                    DB::table('client_externe')->insert(
    
                        [   
                            'noms' => $nom,
                            'tel' => $tel,
                            'adresse' => $adresse,
                            'magasin_id' => session('user')->magasin_id,
                            'user_id' => session('user')->id,
                        ]
                    );
        
                $message = [
                    "title" => 'success',
                    "type" => 'success',
                    "response" => 'Enregistrement effectué avec succès !',
                ];
        
                return $message; 
            } 

    }

    function valider (){

		$id=request('id');	
		$v = DB::table('vente_client')->where('id', $id)->first();
		$cli = DB::table('client_externe')->where('id', $v->client_id)->first()->noms;
		DB::table('vente_client')->where('id', $id)->update(
			[
				'etat' => "Validée",
			]
		);
		DB::table('ligne_vente_client')->where('vente_id', $id)->update(
			[
				'etat' => "Validée",
			]
		);

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

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


		foreach($vo as $vos){
					
					$sg = DB::table('produit_vendable')->where('id', $vos->produit_id)->first()->stock_general;
					$qteval = $vos->qte;
                    if ($v->depot_id > 0){
                        $verif = DB::table('magasin_general')->where('produit_id', $vos->produit_id)->where('depot_id', $v->depot_id)->get()->count();
            			   if ($verif>0){
            			       $sm = DB::table('magasin_general')->where('produit_id', $vos->produit_id)->where('depot_id', $v->depot_id)->orderBydesc('id')->take(1)->first()->apres;
            			   }else{
            			       $sm = 0;
            			   }
                        DB::table('magasin_general')->insert(
                            [
                                'date' => $vos->date,
                                'produit_id' => $vos->produit_id,
                                'qte' => $qteval,
                                'type_operation' => 'Vente',
        						'en_stock' => $sm,
        						'apres' => ($sm - $qteval),
        						'depot_id' => $v->depot_id,
        						'notes' => 'au client '.$cli,
                            ]
                        );
                    }else{
                        $verif =  DB::table('stock_magasin')->where('produit_id', $vos->produit_id)->where('magasin_id', $v->magasin_id)->get()->count();
                        if ($verif > 0){
                            $qtesto = DB::table('stock_magasin')->where('produit_id', $vos->produit_id)->where('magasin_id', $v->magasin_id)->orderBydesc('id')->take(1)->first()->apres;
                        }else{
                            $qtesto = 0;
                        }
                        
                        DB::table('stock_magasin')->insert(
							[
								'date' => $vos->date,
								'produit_id' => $vos->produit_id,
								'qte' => $qteval,
								'type_operation' => 'Vente',
								'en_stock' => $qtesto,
								'apres' => ($qtesto - $qteval),
								'stock_general' => ($sg - $qteval),
								'magasin_id' => session('user')->magasin_id,
								'user_id' => session('user')->id,
								'vente_client_id' => $id,
								'notes' => 'au client '.$cli,
							]
						);
                    }
                    $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('vente_client')->with('status', 'Vente validée avec succès !');
	}

    function VoirSuppressionclient ($id) {

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

            'cli' => $cli,
        ];
    }

    function supprimerclient () {

        $id=request('id');

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

        if($veri) {

            return redirect()->route('nouveau_client')->with('error', 'Suppression impossible ! Opération déjà effectuée sur ce client');
        
        }else{

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


   function enregistrer()
    {

		//$dat = date('d/m/Y');
		$dat=request('datet');
        $hour = date('H:i:s');
		$today = implode('-', array_reverse(explode('/', $dat)));
		$todayyy = date('Y-m-d'); 
		$typevente=request('typevente');
		$remm=request('remise');
		$taxes=request('taxes');
		$client=request('client');
		$magasin_id=request('magasin_id');
		$depot_id=request('depot_id');
		$peps=request('item');
		if ($peps != ""){
		DB::table('vente_client')->insert(
				[
					'date' => $today,
					'heure' => $hour,
					'client_id' => $client,
					'type_vente' => $typevente,
					'remise' => $remm,
					'user_id' => session('user')->id,
					'magasin_id' => $magasin_id,
					'depot_id' => $depot_id,
				]
			);
            $dg = date_parse($todayyy);
		    $annee2 = $dg['year'];
	    	$mois = $dg['month'];
		    
			if($mois<10){
			   $mois1='0'.$mois;
			}else{
			    $mois1=$mois;
			}
            //$b1 = DB::table('vente')->where('date', 'like',  '%'.$mois.'%')->get()->count();
			$b2 = DB::table('vente_client')->where('date', 'like',  '%'.$annee2.'-'.$mois1.'%')->get()->count();
			$b = $b2;
			$idLastvente = DB::table('vente_client')->get()->last()->id;
			$idLast= strlen($b);
	
			if ($idLast==1){
				DB::table('vente_client')->where('id', $idLastvente)->update(
					[
						'numero' => $annee2.'/'.$mois1.'/'.'000'.$b,
					]
				);
			} elseif ($idLast==2){
		
				DB::table('vente_client')->where('id', $idLastvente)->update(
					[
						'numero' => $annee2.'/'.$mois1.'/'.'00'.$b,
					]
				);
			} elseif ($idLast==3){
				DB::table('vente_client')->where('id', $idLastvente)->update(
					[
						'numero' => $annee2.'/'.$mois1.'/'.'0'.$b,
					]
				);
			} else {
				DB::table('vente_client')->where('id', $idLastvente)->update(
					[
						'numero' => $annee2.'/'.$mois1.'/'.$b,
					]
				);
			}
	
			$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;
				if ($pr > 0){
				    $pr = DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->pr;
				}else{
				    $pr = 0;
				}
				//$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('vente_client')->where('id', $idLastvente)->delete();
                    DB::table('ligne_vente_client')->where('vente_id', $idLastvente)->delete();
					$message = [
                        "title" => 'Erreur de stock',
                        "type" => 'error',
                        "response" => 'Aucun stock de '.$desi.' n\'est disponible dans ce dépôt !',
                    ];
        
                    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('vente_client')->where('id', $idLastvente)->delete();
                    DB::table('ligne_vente_client')->where('vente_id', $idLastvente)->delete();
    
                    $message = [
                        "title" => 'Erreur d\'enregistrement',
                        "type" => 'error',
                        "response" => 'Stock '.' '.$desi.' '.'insuffisant ! Stock restant : '.$qtesto,
                    ];
        
                    return $message;
    
                } else {*/	
				    	/*if ($typevente == "Détail"){
                          $pv = DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->prix_detail;
                        }elseif ($typevente == "Semigros") {
                            $pv = DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->prix_semigros;
                        }else{
                            $pv = DB::table('produit_vendable')->where('id',$pep[0 +$i])->first()->prix_gros;
                        }*/
                        $benef = $pep[1 +$i] * ($pep[2 +$i] - $pr);
					DB::table('ligne_vente_client')->insert(
						[
								'vente_id' => $idLastvente,
								'produit_id' => $pep[0 +$i],
								'qte' => $pep[1 +$i],
								'reste' => $pep[1 +$i],
								'pv' => $pep[2 +$i],
								'pr' => $pr,
								'benef' => $benef,
								'montligne' => ($pep[1 +$i] * $pep[2 +$i]),
								'montlignepr' => ($pep[1 +$i] * $pr),
								'date' => $today, 
								'categorie_id' => $categorie_id,
								'user_id' => session('user')->id,
								'magasin_id' => $magasin_id,
					            'depot_id' => $depot_id,
						]
					);
						//$i += count($pep->toArray()); 
						$i;
				//}  
				//}
                }
			$mht = DB::table('ligne_vente_client')->where('vente_id', $idLastvente)->get()->sum('montligne');
			$rem2 = DB::table('vente_client')->where('id', $idLastvente)->first()->remise;
			$mtremise = $rem2;
			$mht1 = $mht - $remm;
			DB::table('vente_client')->where('id', $idLastvente)->update(
						[
							'montant_ht' => $mht,
							'remise' => $remm,
							'montant_remise' => $remm,
							'net_com' => $mht1,
						]
					);
			//$tau = DB::table('taxes')->get();
			$nc = DB::table('vente_client')->where('id', $idLastvente)->first()->net_com;
		
			if ($taxes == "Oui"){
				$tau = DB::table('taxes')->get();
					foreach($tau as $taux){
					DB::table('ligne_taxe_client')->insert(
							[
								'vente_id' => $idLastvente,
								'taux' => $taux->taux,
								'montligne' => $nc * $taux->taux,
							]
						);
					}
					$sumtaux = DB::table('ligne_taxe_client')->where('vente_id', $idLastvente)->get()->sum('montligne');
					$ttc = $sumtaux + $nc;
				} else {
					$sumtaux = 0;
					$ttc = $nc;
				}
			DB::table('vente_client')->where('id', $idLastvente)->update(
						[
							'total_taux' => $sumtaux,
							'montant' => $ttc,
						]
					);
			
	}
	/*$message = [
				"title" => 'success',
				"type" => 'success',
				"response" => 'Opération effectuée avec succes !',
			];
	
			return $message;*/
			return redirect()->route('vente_client')->with('status', 'Vente enregistrée avec succès !');
	}
    function validerclient () {

		$id=request('id');	
		DB::table('vente_client')->where('id', $id)->update(
			[
				'etat' => "Validée",
			]
		);
		DB::table('ligne_vente_client')->where('vente_id', $id)->update(
			[
				'etat' => "Validée",
			]
		);

		$vo = DB::table('ligne_vente_client')->where('vente_id', $id)->where('etat','Validée')->get();
        $v = DB::table('vente_client')->where('id', $id)->first();
		$today = date('Y-m-d'); 


		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)->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' => $v->date,
								'produit_id' => $vos->produit_id,
								'qte' => $qteval,
								'type_operation' => 'Vente',
								'en_stock' => $qtesto,
								'apres' => ($qtesto - $qteval),
								'stock_general' => ($sg - $qteval),
								'magasin_id' => session('user')->magasin_id,
								'user_id' => session('user')->id,
								'vente_client_id' => $id,
							]
						);
					//BENEF
					$pru = DB::table('produit_vendable')->where('id', $vos->produit_id)->first()->pr;
					if ($pru > 0){
				    $pru = DB::table('produit_vendable')->where('id', $vos->produit_id)->first()->pr;
    				}else{
    				    $pru = 0;
    				}
						$pv = DB::table('ligne_vente_client')->where('produit_id', $vos->produit_id)->where('vente_id', $id)->first()->pv;
						$benef = ($pv - $pru) * $qteval;
					DB::table('ligne_vente_client')->where('vente_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('vente_client')->with('status', 'Vente validée avec succès !');
    }
    

    function enregistrer3(Request $request)
    {
        $client_id=request('client_id');
        //$vente_id=request('vente_id');
        $montant=request('montant');
        $date=implode('-', array_reverse(explode('/', request('date'))));
        $mode=request('mode');
		$note=request('note');
		
        $today2 = date('Y-m-d');
        $df = date_parse($today2);
		$y = $df['year'];
		
		//dd($y);
		
		
		$apayer00 = DB::table('vente_client')->where('client_id', $client_id)->where('etat','Validée')->get()->sum('montant');
		$retour =  DB::table('retourcommandeclient')->where('client_id', $client_id)->where('etat','Validée')->get()->sum('montant');
		$apayer0 = $apayer00 - $retour;
		/*$si = DB::table('soldepaiment')
		        ->where('client_id', $client_id)
                ->whereYear('date', $y)
                ->get()->count();
         if ($si>0){*/
         $sd = DB::table('soldepaiment')
                ->where('client_id', $client_id)
                ->sum('montant');
                //->whereYear('date', $y)
                //->orderBydesc('id')
                //->take(1)->first()->montant;
         /*}else{
            $sd = 0;
         }*/
         
		$apayer = $sd + $apayer0;
		
		$sommePaiement =  DB::table('paiement_client')->where('client_id', $client_id)->get()->sum('montant');
		$total = $sommePaiement + $montant ;
		$mtpd = DB::table('paiement_client')->where('client_id', $client_id)->get()->sum('montant');
		$mtpd1 = $mtpd + $montant;
		$mtd = $apayer - $mtpd1;
		
		if( $total >  $apayer ) {
                $message = [
                "title" => 'Attention !',
                "type" => 'error',
                "response" => 'Le montant saisi ne doit pas dépasser le restant à payer :'. ' '. ($apayer - $sommePaiement).' F/'.$y,
                ];

                return $message;
       }else{
        DB::table('paiement_client')->insert(
            [
                'client_id' => $client_id,
                //'vente_id' => $vente_id,
                'montant' => $montant,
                'mode' => $mode,
                'date' => $date,
                'note' => $note,
                'paye' => $mtpd1, 
                'reste' => $mtd,
                'etat' => "En cours",
                'user_id' => session('user')->id,
                'magasin_id' => session('user')->magasin_id,
            ]
        );
        $idLastpaie = DB::table('paiement_client')->get()->last()->id;
		DB::table('solde')->insert(
			[
				'date' => $date,
				'nature' => 'Paiement',
				'montant' => $montant,
				'paie_cli_ext_id' => $idLastpaie,
				'user_id' => session('user')->id,
			]
		);
        $message = [
            "title" => 'success',
            "type" => 'success',
            "response" => 'Paiement enregistré avec succès !',
        ];

        return $message;
	
   }   
 }
    
 function validerpaie () {

    $id=request('id');
    
    $montpaie = DB::table('paiement_client')->where('id', $id)->first()->montant;

    $client_id = DB::table('paiement_client')->where('id', $id)->first()->client_id;
    $noms = DB::table('client_externe')->where('id', $client_id)->first()->noms;
    $today2 = date('Y-m-d');

    DB::table('paiement_client')->where('id', $id)->update(
        [
            'etat' => "Validé",
        ]
    );
	return redirect()->route('liste_des_paiements')->with('status', 'Paiement validé avec succès !');
	}
  
	function fgpost()
    {

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

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

		$today = implode('-', array_reverse(explode('/', $dat)));
		$todayyy = date('Y-m-d'); 
		$client= request('client');
		$serveuse= request('serveuse');
		$typevente=request('typevente');

		$peps=collect(request('peps'))->chunk(2);

			DB::table('factureglobale')->insert(
				[
					'date' => $today,
					'heure' => $hour,
					'client_id' => $client,
					'serveuse_id' => $serveuse,
					'type_vente' => $typevente,
				]
			);

			$idLastvente = DB::table('factureglobale')->get()->last()->id;
			$idLast= strlen($idLastvente);
	
			if ($idLast==1){
				DB::table('factureglobale')->where('id', $idLastvente)->update(
					[
						'numero' => '000'.$idLastvente,
					]
				);
			} elseif ($idLast==2){
		
				DB::table('factureglobale')->where('id', $idLastvente)->update(
					[
						'numero' => '00'.$idLastvente,
					]
				);
			} elseif ($idLast==3){
				DB::table('factureglobale')->where('id', $idLastvente)->update(
					[
						'numero' => '0'.$idLastvente,
					]
				);
			} else {
				DB::table('factureglobale')->where('id', $idLastvente)->update(
					[
						'numero' => $idLastvente,
					]
				);
			}
	
			$i=0;
			
			foreach ($peps as $key=> $pep){
            
                $qtesto =  DB::table('produit_vendable_fini')->where('id', $pep[0 +$i])->first()->stock_gene;
    
                $desi =  DB::table('produit_vendable_fini')->where('id', $pep[0 +$i])->first()->designation;
   
                    $prov_id =  DB::table('produit_vendable_fini')->where('id', $pep[0 +$i])->first()->pv_id;
				
                    if ($typevente=="VIP"){
                        $prix =  DB::table('produit_vendable_fini')->where('id', $pep[0 +$i])->first()->prix_vip;
                    } else {
					$prix =  DB::table('produit_vendable_fini')->where('id', $pep[0 +$i])->first()->prix_ordinaire;
			     	}
	
				   
					DB::table('ligne_factureglobale')->insert(
						[
							'vente_id' => $idLastvente,
							'produit_id' => $pep[0 +$i],
							'qte' => $pep[1 +$i],
							'pv' => $prix,
							'montligne' => ($pep[1 +$i] * $prix),
							'date' => $todayyy,
							'client_id' => $client,
							'produitvendable_id' => $prov_id, 
						]
					);
		
					$i += count($pep->toArray());  
			}
			$message = [
				"title" => 'success',
				"type" => 'success',
				"response" => 'Opération effectué avec succes !',
			];
	
			return $message;
    }
	function factureglobalepdf($id)
    {
		if(session('user')== null) {
            return redirect('/');
		}
		
		$vente = DB::table('factureglobale')->where('id',$id)->first();

		$ligne = DB::table('ligne_factureglobale')->where('vente_id',$id)->get();
		$ligneM = DB::table('ligne_factureglobale')->where('vente_id',$id)->get()->sum('montligne');
		

        $pdf = PDF::loadView('/client_externe/factureglobalepdf', [

            'ligne' => $ligne,
			'vente' => $vente,
			'ligneM' => $ligneM,
		]);
		
        return $pdf->stream();
    }
	function VoirSupp ($id) {

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

            'cli' => $cli,
        ];
    }
	function supp () {

        $id=request('id');

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

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

        return redirect()->route('facture-globale')->with('status', 'Suppression effectuée avec succes');
    }
	public function listevente234(){

		if(session('user')== null) {
            return redirect('/');
        }
		//$numero = request('numero');
		if (session('user')->roles== "Admin"){
			$ventes=DB::table('vente_client')->where('etat','Validée')->orderBydesc('id')->get();
		} elseif (session('user')->roles== "Gérant"){
			$ventes=DB::table('vente_client')->where('etat','Validée')->where('magasin_id', session('user')->magasin_id)->orderBydesc('id')->get();
		} else {
			$ventes=DB::table('vente_client')->where('etat','Validée')->where('user_id', session('user')->id)->orderBydesc('id')->get();
		}
		
		return view('client_externe/rech-fact-cli', [
			'ventes'=> $ventes,
		]);
	}
	public function listevente235(){

		if(session('user')== null) {
            return redirect('/');
        }
		$numero = request('numero');
		if (session('user')->roles== "Admin"){
		$verif=DB::table('vente_client')->where('numero', $numero)->first();
		} elseif (session('user')->roles== "Admin"){
		$verif=DB::table('vente_client')->where('magasin_id', session('user')->magasin_id)->where('numero', $numero)->first();
		} else {
		$verif=DB::table('vente_client')->where('user_id', session('user')->id)->where('numero', $numero)->first();	
		}
		if ($verif){
		$ventes=DB::table('vente_client')->where('numero', $numero)->get();
		return view('client_externe/rech-fact-cli2', [
			'ventes'=> $ventes,
		]);
	} else 
		 {
			return redirect()->route('recherche-facture-client')->with('error', 'Vous avez saisi un numéro incorrect ou soit la facture n\'est pas enregistrée sous votre nom !');
	}}
	function annuler () {

        $id=request('id');
		$v = DB::table('vente_client')->where('id', $id)->first();
        DB::table('vente_client')->where('id', $id)->update(
            [
                'etat' => "Annulée",
            ]
        );
		DB::table('ligne_vente_client')->where('vente_id', $id)->update(
            [
                'etat' => "Annulée",
            ]
        );
		$numero = DB::table('vente_client')->where('id', $id)->first()->numero;
		$magasin_id = DB::table('vente_client')->where('id', $id)->first()->magasin_id;
		$vo = DB::table('ligne_vente_client')->where('vente_id', $id)->get();
		$today = date('Y-m-d'); 

			foreach($vo as $vos){
				
				$sg = DB::table('produit_vendable')->where('id', $vos->produit_id)->first()->stock_general;
				$tv = DB::table('produit_vendable')->where('id', $vos->produit_id)->first()->totvendu;
				//$qtesto = DB::table('stock_magasin')->where('produit_id', $vos->produit_id)->where('magasin_id', $magasin_id)->orderBydesc('id')->take(1)->first()->apres;
				//$idproduit = DB::table('produit_vendable')->where('id', $vos->produit_id)->first()->pv_id;
				$qteval = $vos->qte;
		
					DB::table('produit_vendable')->where('id', $vos->produit_id)->update(
						[
							'totvendu' => ($tv - $qteval),
						]
					);
					
					
					if ($v->depot_id > 0){
                        $verif = DB::table('magasin_general')->where('produit_id', $vos->produit_id)->where('depot_id', $v->depot_id)->get()->count();
            			   if ($verif>0){
            			       $sm = DB::table('magasin_general')->where('produit_id', $vos->produit_id)->where('depot_id', $v->depot_id)->orderBydesc('id')->take(1)->first()->apres;
            			   }else{
            			       $sm = 0;
            			   }
                        DB::table('magasin_general')->insert(
                            [
                                'date' => $vos->date,
                                'produit_id' => $vos->produit_id,
                                'qte' => $qteval,
                                'type_operation' => 'Annulationvente',
        						'en_stock' => $sm,
        						'apres' => ($sm + $qteval),
        						'depot_id' => $v->depot_id,
        						//'stock_general' => ($qtesto + $qteval),
                            ]
                        );
                    }else{
                        $verif =  DB::table('stock_magasin')->where('produit_id', $vos->produit_id)->where('magasin_id', $v->magasin_id)->get()->count();
                        if ($verif > 0){
                            $qtesto = DB::table('stock_magasin')->where('produit_id', $vos->produit_id)->where('magasin_id', $v->magasin_id)->orderBydesc('id')->take(1)->first()->apres;
                        }else{
                            $qtesto = 0;
                        }
                        
                        DB::table('stock_magasin')->insert(
							[
								'date' => $vos->date,
								'produit_id' => $vos->produit_id,
								'qte' => $qteval,
								'type_operation' => 'Annulationvente',
								'en_stock' => $qtesto,
								'apres' => ($qtesto + $qteval),
								'notes' => "Facture vente ".$numero." annulée.",
								'stock_general' => ($sg + $qteval),
								'magasin_id' => $v->magasin_id,
								'user_id' => session('user')->id,
								'vente_client_id' => $id,
							]
						);
                    }
				
	}
	return redirect()->route('toutes-les-ventes-client')->with('status', 'Facture annulée avec succes');
	}
	//toutes
	public function listevente23(){

		if(session('user')== null) {
            return redirect('/');
        }
		if (session('user')->roles=="Admin"){
			$ventes=DB::table('vente_client')->orderBydesc('id')->get();
			$client=DB::table('client_externe')->orderBydesc('id')->get();
			$magasin=DB::table('magasin')->get();
			$total=DB::table('vente_client')->where('etat', 'Validée')->sum('montant');
		} elseif (session('user')->roles=="Gérant") {
			$ventes=DB::table('vente_client')->where('magasin_id', session('user')->magasin_id)->orderBydesc('id')->get();
			$client=DB::table('client_externe')->orderBydesc('id')->get();
			$magasin=DB::table('magasin')->where('id', session('user')->magasin_id)->get();
			$total=DB::table('vente_client')->where('magasin_id', session('user')->magasin_id)->where('etat', 'Validée')->sum('montant');
		} else {
			$ventes=DB::table('vente_client')->where('magasin_id', session('user')->magasin_id)->orderBydesc('id')->get();
			$client=DB::table('client_externe')->orderBydesc('id')->get();
			$magasin=DB::table('magasin')->where('id', session('user')->magasin_id)->get();
			$total=DB::table('vente_client')->where('magasin_id', session('user')->magasin_id)->where('etat', 'Validée')->sum('montant');
		}
		
		return view('client_externe/toutes-les-ventes', [
			'ventes'=> $ventes,
			'client'=> $client,
			'magasin'=> $magasin,
			'total'=> $total,
		]);
	}
	public function listevente233(){

		if(session('user')== null) {
            return redirect('/');
        }
		
		$date_debut = request('date_debut');
		$magasin = request('magasin');
        $date_debut= explode('/', $date_debut);
        $date_debut= array_reverse($date_debut);
        $date_debut= implode('-', $date_debut);

        $date_fin = request('date_fin');
        $date_fin= explode('/', $date_fin);
        $date_fin= array_reverse($date_fin);
        $date_fin= implode('-', $date_fin);
		
		session(['date_debut'=> $date_debut]);
		session(['date_fin'=> $date_fin]);
		session(['magasin'=> $magasin]);
		
		if($date_fin > $date_debut || $date_fin == $date_debut){
		if ($magasin==0){
    		$ventes=DB::table('vente_client')->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->orderBydesc('id')->get();
    		$client=DB::table('client_externe')->orderBydesc('id')->get();
    		$magasin1=DB::table('magasin')->get();
    		$total=DB::table('vente_client')->where('etat', 'Validée')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
    		$totalcount= "N/A";
		} else {
				$ventes=DB::table('vente_client')->where('magasin_id', $magasin)->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->orderBydesc('id')->get();
				$client=DB::table('client_externe')->orderBydesc('id')->get();
				$magasin1=DB::table('magasin')->get();
				$total=DB::table('vente_client')->where('magasin_id', $magasin)->where('etat', 'Validée')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
				$totalcount=DB::table('vente_client')->where('magasin_id', $magasin)->where('etat', 'Validée')->whereBetween('date', [$date_debut,$date_fin])->get()->count();
		}
		return view('client_externe/toutes-les-ventes-rech', [
			'ventes'=> $ventes,
			'client'=> $client,
			'magasin'=> $magasin,
			'magasin1'=> $magasin1,
			'total'=> $total,
			'totalcount'=> $totalcount,
			'date_debut'=> $date_debut,
			'date_fin'=> $date_fin,
		]);
	} else 
		 {
			return redirect()->route('toutes-les-ventes-client')->with('error', 'La date de fin ne doit pas dépasser la date de début!');
	}}
	public function listevente233PDF(){

		if(session('user')== null) {
            return redirect('/');
        }
		
		$date_debut = session('date_debut');
		$date_fin = session('date_fin');
		$magasin = session('magasin');
		
		if($date_fin > $date_debut || $date_fin == $date_debut){
		if ($magasin==0){
		$ventes=DB::table('vente_client')->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->orderBydesc('id')->get();
		$client=DB::table('client_externe')->orderBydesc('id')->get();
		$magasin1=DB::table('magasin')->get();
		$total=DB::table('vente_client')->where('etat', 'Validée')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		$totalcount= "N/A";
		} else {
				$ventes=DB::table('vente_client')->where('magasin_id', $magasin)->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->orderBydesc('id')->get();
				$client=DB::table('client_externe')->orderBydesc('id')->get();
				$magasin1=DB::table('magasin')->get();
				$total=DB::table('vente_client')->where('magasin_id', $magasin)->where('etat', 'Validée')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
				$totalcount=DB::table('vente_client')->where('magasin_id', $magasin)->where('etat', 'Validée')->whereBetween('date', [$date_debut,$date_fin])->get()->count();
		}
		$pdf = PDF::loadView('client_externe/toutes-les-ventes-pdf', [
			'ventes'=> $ventes,
			'client'=> $client,
			'magasin'=> $magasin,
			'magasin1'=> $magasin1,
			'total'=> $total,
			'totalcount'=> $totalcount,
			'date_debut'=> $date_debut,
			'date_fin'=> $date_fin,
		]);
		return $pdf->stream();
	}}
	
	 public function historiquebenef(){

		if(session('user')== null) {
            return redirect('/');
        }
			$vente = DB::table('ligne_vente_client')->get();
			$plucked = $vente->pluck('produit_id');
			$produit = DB::table('produit_vendable')->whereIn('id', $plucked)->orderBydesc('designation')->get();
		
		return view('client_externe/historiquebenef', [
			'produit'=> $produit,
		]);
	}
	public function historiquebenef2(){

		if(session('user')== null) {
            return redirect('/');
        }
        $date_debut = implode('-', array_reverse(explode('/', request('date_debut'))));
		$date_fin = implode('-', array_reverse(explode('/', request('date_fin'))));
			$vente = DB::table('ligne_vente_client')->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->get();
			$plucked = $vente->pluck('produit_id');
			$produit = DB::table('produit_vendable')->whereIn('id', $plucked)->orderBydesc('designation')->get();
		
		return view('client_externe/historiquebenef2', [
			'produit'=> $produit,
			'date_debut'=> $date_debut,
			'date_fin'=> $date_fin,
		]);
	}
	
	//INITIALISER SOLDE CLIENT
	function pclient2($id)
    {
        if(session('user')== null) {
            return redirect('/');
		}
		$vente = DB::table('vente_client')->where('id',$id)->first();
		$paie = DB::table('soldepaiment')->where('client_id',$id)->get();
		return view('client_externe/soldepaiment',[
            'id' => $id,
            'paie' => $paie,
        ]);	
    }
	function enregistrer4(Request $request)
    {
        $client_id=request('client_id');
        $montant=request('montant');
        $date=implode('-', array_reverse(explode('/', request('date'))));
		$notes=request('notes');
		
        $today2 = date('Y-m-d');
        $df = date_parse($today2);
		$y = $df['year'];

		$si = DB::table('soldepaiment')
		        ->where('client_id', $client_id)
                ->whereYear('date', '=', $y)
                ->where('etat', 'Validée')
                ->get()->count();
         
		if( $si >  0 ) {
                $message = [
                "title" => 'Attention',
                "type" => 'error',
                "response" => 'Solde déjà initialisé pour ce client',
                ];

                return $message;
       }else{
        DB::table('soldepaiment')->insert(
            [
                'client_id' => $client_id,
                'montant' => $montant,
                'date' => $date,
                'notes' => $notes,
                'annee' => $y, 
            ]
        );
        $message = [
            "title" => 'success',
            "type" => 'success',
            "response" => 'Paiement enregistré avec succès !',
        ];

        return $message;
	
   }   
 }
function Voirsoldepaiment ($id) {

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

            'pai' => $pai,
        ];
    }

    function supprimersoldepaiment () {

		$id=request('id');

        $idclient = DB::table('soldepaiment')->where('id', $id)->first()->client_id;

        DB::table('soldepaiment')->where('id', $id)->delete();
        
        return redirect()->route('soldeini', ['id' => $idclient])->with('status', 'Suppression effectuée avec succes');
	}
	function validersoldepaiment () {

    $id=request('id');
    $idclient = DB::table('soldepaiment')->where('id', $id)->first()->client_id;
    DB::table('soldepaiment')->where('id', $id)->update(
        [
            'etat' => "Validée",
        ]
    );
	return redirect()->route('soldeini', ['id' => $idclient])->with('status', 'Validation effectuée avec succès');
	}
	
	
	function modifierclientpost(Request $request, $id)
    {

        $nom=request('nom');
        $telephone=request('tel');
        $adresse=request('adresse');

        DB::table('client_externe')->where('id', $id)->update(
                [
                    'noms' => $nom,
                    'tel' => $telephone,
                    'adresse' => $adresse,
                ]
            );
            $message = [
				"title" => 'success',
				"type" => 'success',
				"response" => 'Opération effectué avec succes !',
			];
    }
    public function modifier3($id){
		
        if(session('user')== null) {
            return redirect('/');
        }
		
		$produit=DB::table('produit_vendable')->orderBy('designation', 'asc')->get();
		$client=DB::table('client_externe')->get();
		$vente1=DB::table('vente_client')->where('id',$id)->first();
		$ligne_vente=DB::table('ligne_vente_client')->where('vente_id',$id)->get();
		$categorie=DB::table('categorie')->get();
		$remise=DB::table('remise')->where('magasin_id', session('user')->magasin_id)->where('etat', 'Actif')->orderBydesc('id')->get();
		if (session('user')->roles=="Admin"){
		$ventes = DB::table('vente_client')->where('etat2', 'En cours')->orderBydesc('id')->get();
		} elseif (session('user')->roles=="Gérant") {
			$ventes = DB::table('vente_client')->where('etat2', 'En cours')->where('magasin_id', session('user')->magasin_id)->orderBydesc('id')->get();
		} else {
			$ventes = DB::table('vente_client')->where('etat2', 'En cours')->where('user_id', session('user')->id)->orderBydesc('id')->get();
		}
		return view('client_externe/modifier3', [
			'produit'=> $produit,
			'client'=> $client,
			'ventes'=> $ventes,
			'cat'=> $categorie,
			'remise'=> $remise,
			'vente1'=> $vente1,
			'ligne_vente'=> $ligne_vente,
			'vente_id'=> $id,
		]);
    }
    function modifventev2($id)
    {
		$dat=request('datet');

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

		//$today = implode('-', array_reverse(explode('/', $dat)));
		$todayyy = date('Y-m-d');
		$typevente=request('typevente');
		$taxes=request('taxes');
		$remise=request('remise');
		$client=request('client');
        $peps=request('item');
		
		$vo = DB::table('ligne_vente_client')->where('vente_id', $id)->get();
		foreach($vo as $vos){					
					$sg = DB::table('produit_vendable')->where('id', $vos->produit_id)->first()->stock_general;
					$qtesto = DB::table('stock_magasin')->where('produit_id', $vos->produit_id)->where('magasin_id', session('user')->magasin_id)->orderBydesc('id')->take(1)->first()->apres;
					$qteval = $vos->qte;
					DB::table('produit_vendable')->where('id', $vos->produit_id)->update(
						[
							'stock_general' => ($sg - $qteval),
						]
					);
					DB::table('stock_magasin')->insert(
							[
								'date' => $todayyy,
								'produit_id' => $vos->produit_id,
								'qte' => $qteval,
								'type_operation' => 'Modification vente',
								'en_stock' => $qtesto,
								'apres' => ($qtesto + $qteval),
								'stock_general' => ($sg + $qteval),
								'magasin_id' => session('user')->magasin_id,
								'user_id' => session('user')->id,
								'vente_client_id' => $id,
							]
						);
						
					$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,
						]
					);
			}
			
		$supl = DB::table('ligne_vente_client')->where('vente_id', $id)->delete();
		$sup2 = DB::table('ligne_taxe_client')->where('vente_id', $id)->delete();
		

        DB::table('vente_client')->where('id', $id)->update(
            [
					'date' => $todayyy,
					'remise' => $remise,
					'client_id' => $client,	
					'etat' => 'Proforma',
            ]
        );
        
        $date = DB::table('vente_client')->where('id', $id)->first();
		$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;
				if ($pr > 0){
				    $pr = DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->pr;
    				}else{
    				    $pr = 0;
    				}
				//$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('vente_client')->where('id', $idLastvente)->delete();
                    //DB::table('ligne_vente_client')->where('vente_id', $idLastvente)->delete();
					$message = [
                        "title" => 'Erreur de stock',
                        "type" => 'error',
                        "response" => 'Aucun stock de '.$desi.' n\'est disponible dans ce dépôt !',
                    ];
        
                    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[1 +$i])->first()->categorie_id;
                /*if($qtesto < $pep[1 +$i]){
    
                    DB::table('vente_client')->where('id', $idLastvente)->delete();
                    DB::table('ligne_vente_client')->where('vente_id', $idLastvente)->delete();
    
                    $message = [
                        "title" => 'Erreur d\'enregistrement',
                        "type" => 'error',
                        "response" => 'Stock '.' '.$desi.' '.'insuffisant ! Stock restant : '.$qtesto,
                    ];
        
                    return $message;
    
                } else {*/
				
					/*if ($typevente == "Détail"){
                          $pv = DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->prix_detail;
                        }elseif ($typevente == "Semigros") {
                            $pv = DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->prix_semigros;
                        }else{
                            $pv = DB::table('produit_vendable')->where('id',$pep[0 +$i])->first()->prix_gros;
                        }*/
					$categorie_id =  DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->categorie_id;
					  
					DB::table('ligne_vente_client')->insert(
						[
								'vente_id' => $id,
								'produit_id' => $pep[0 +$i],
								'qte' => $pep[1 +$i],
								'reste' => $pep[1 +$i],
								'pv' => $pep[2 +$i],
								'pr' => $pr,
								'montligne' => ($pep[1 +$i] * $pep[2 +$i]),
								'montlignepr' => ($pep[1 +$i] * $pr),
								'date' => $todayyy, 
								'categorie_id' => $categorie_id,
								'user_id' => session('user')->id,
								'magasin_id' => session('user')->magasin_id,
						]
					);
						$i; 
				//}  
				//}
	            }
			$mht = DB::table('ligne_vente_client')->where('vente_id', $id)->get()->sum('montligne');
			$rem2 = DB::table('vente_client')->where('id', $id)->first()->remise;
			$mtremise = $rem2;
			//$mht11 = ($mht * $mtremise)/100;
			$mht1 = $mht - $mtremise;
			DB::table('vente_client')->where('id', $id)->update(
						[
							'montant_ht' => $mht,
							'remise' => $remise,
							'montant_remise' => $mtremise,
							'net_com' => $mht1,
						]
					);
			$nc = DB::table('vente_client')->where('id', $id)->first()->net_com;
			/*$today = date('Y-m-d'); 
			foreach($tau as $taux){
				DB::table('ligne_taxe_client')->insert(
						[
							'vente_id' => $id,
							'taux' => $taux->taux,
							'montligne' => $nc * $taux->taux,
						]
					);
			}
			$sumtaux = DB::table('ligne_taxe_client')->where('vente_id', $id)->get()->sum('montligne');
			$ttc = $sumtaux + $nc;*/
			if ($taxes == "Oui"){
				$tau = DB::table('taxes')->get();
					foreach($tau as $taux){
					DB::table('ligne_taxe_client')->insert(
							[
								'vente_id' => $id,
								'taux' => $taux->taux,
								'montligne' => $nc * $taux->taux,
							]
						);
					}
					$sumtaux = DB::table('ligne_taxe_client')->where('vente_id', $id)->get()->sum('montligne');
					$ttc = $sumtaux + $nc;
				} else {
					$sumtaux = 0;
					$ttc = $nc;
				}
			DB::table('vente_client')->where('id', $id)->update(
						[
							'total_taux' => $sumtaux,
							'montant' => $ttc,
						]
					);
			
		return redirect()->route('vente_client')->with('status','Vente modifiée avec succès' );
	}
    
}
