<?php

namespace App\Http\Controllers;

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

class ProformaController extends Controller
{

	function voir ($id) {

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

            'proforma' => $proforma,
        ];
    }

    function supprimer () {
        $id=request('id');
		DB::table('ligne_proforma')->where('proforma_id', $id)->delete();
		DB::table('ligne_taxe_proforma')->where('proforma_id', $id)->delete();
		DB::table('proforma')->where('id', $id)->delete();
        return redirect()->route('proforma')->with('status', 'Suppression effectuée avec succès');
    }

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

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

	
    public function nouveau(){
		
        if(session('user')== null) {
            return redirect('/');
        }
		
		$produit=DB::table('produit_vendable')->orderby('designation','asc')->get();
		$client=DB::table('client_externe')->orderby('noms','asc')->get();
		$remise=DB::table('remise')->where('magasin_id', session('user')->magasin_id)->where('etat', 'Actif')->orderbydesc('id')->get();
		$categorie=DB::table('categorie')->get();
		if (session('user')->roles=="Caissier"){
		$proformas = DB::table('proforma')->where('etat2', 'En cours')->where('user_id', session('user')->id)->orderBydesc('id')->take(50)->get();
		$magasin = DB::table('magasin')->where('id', session('user')->magasin_id)->get();
		}elseif (session('user')->roles=="Validateur" || session('user')->roles=="Gérant"){
		$proformas = DB::table('proforma')->where('etat2', 'En cours')->where('magasin_id', session('user')->magasin_id)->orderBydesc('id')->take(50)->get(); 
		$magasin = DB::table('magasin')->where('id', session('user')->magasin_id)->get();
		}else{
		 $proformas = DB::table('proforma')->where('etat2', 'En cours')->orderBydesc('id')->take(50)->get();  
		 $magasin = DB::table('magasin')->orderBy('nom', 'ASC')->get();
		}
		return view('proforma/nouveau', [
			'produit'=> $produit,
			'proforma'=> $proformas,
			'client'=> $client,
			'cat'=> $categorie,
			'remise'=> $remise,
			'magasin'=> $magasin,
		]);
	}
	//Get compte Where compte
	function get_product($search)
	{

		$compte = DB::table('produit_vendable')->where('desi','like','%'.$search)->get();
		return $compte;
	}
	public function proforma2(){
		
        if(session('user')== null) {
            return redirect('/');
        }
		
		$produit=DB::table('produit_vendable')->orderby('designation','asc')->get();
		$client=DB::table('client')->orderBy('nom', 'ASC')->get();
		$proforma = DB::table('proforma')->orderBydesc('id')->get();
		return view('proforma/nouveau', [
			'produit'=> $produit,
			'proforma'=> $proforma,
			'client'=> $client,
		]);
	}
	
	public function liste(){

		if(session('user')== null) {
            return redirect('/');
        }
		if (session('user')->roles=="Admin"){
			$proformas=DB::table('proforma')->orderBydesc('id')->take(100)->get();
			$client=DB::table('client')->orderBydesc('id')->get();
			$magasin=DB::table('magasin')->get();
			$total=DB::table('proforma')->where('etat', 'Validée')->sum('montant');
		} else {
			$proformas=DB::table('proforma')->where('magasin_id', session('user')->magasin_id)->orderBydesc('id')->take(100)->get();
			$client=DB::table('client')->orderBydesc('id')->get();
			$magasin=DB::table('magasin')->where('id', session('user')->magasin_id)->get();
			$total=DB::table('proforma')->where('magasin_id', session('user')->magasin_id)->where('etat', 'Validée')->sum('montant');
		}
		
		return view('proforma/liste', [
			'proformas'=> $proformas,
			'client'=> $client,
			'magasin'=> $magasin,
			'total'=> $total,
		]);
	}
	public function listeproforma233(){

		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){
		$proformas=DB::table('proforma')->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->orderBydesc('id')->get();
		$client=DB::table('client')->orderBydesc('id')->get();
		$magasin1=DB::table('magasin')->get();
		$total=DB::table('proforma')->where('etat', 'Validée')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		$totalcount= "N/A";
		} else {
				$proformas=DB::table('proforma')->where('magasin_id', $magasin)->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->orderBydesc('id')->get();
				$client=DB::table('client')->orderBydesc('id')->get();
				$magasin1=DB::table('magasin')->where('id', $magasin)->get();
				$total=DB::table('proforma')->where('magasin_id', $magasin)->where('etat', 'Validée')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
				$totalcount=DB::table('proforma')->where('magasin_id', $magasin)->where('etat', 'Validée')->whereBetween('date', [$date_debut,$date_fin])->get()->count();
		}
		return view('proforma/toutes-les-proformas-rech', [
			'proformas'=> $proformas,
			'client'=> $client,
			'magasin'=> $magasin,
			'magasin1'=> $magasin1,
			'total'=> $total,
			'totalcount'=> $totalcount,
			'date_debut'=> $date_debut,
			'date_fin'=> $date_fin,
		]);
	} else 
		 {
			return redirect()->route('toutes-les-proformas')->with('error', 'La date de fin ne doit pas dépasser la date de début!');
	}}
	public function listeproforma233PDF(){

		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){
		$proformas=DB::table('proforma')->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->orderBydesc('id')->get();
		$client=DB::table('client')->orderBydesc('id')->get();
		$magasin1=DB::table('magasin')->get();
		$total=DB::table('proforma')->where('etat', 'Validée')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		$totalcount= "N/A";
		} else {
				$proformas=DB::table('proforma')->where('magasin_id', $magasin)->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->orderBydesc('id')->get();
				$client=DB::table('client')->orderBydesc('id')->get();
				$magasin1=DB::table('magasin')->get();
				$total=DB::table('proforma')->where('magasin_id', $magasin)->where('etat', 'Validée')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
				$totalcount=DB::table('proforma')->where('magasin_id', $magasin)->where('etat', 'Validée')->whereBetween('date', [$date_debut,$date_fin])->get()->count();
		}
		$pdf = PDF::loadView('proforma/toutes-les-proformas-pdf', [
			'proformas'=> $proformas,
			'client'=> $client,
			'magasin'=> $magasin,
			'magasin1'=> $magasin1,
			'total'=> $total,
			'totalcount'=> $totalcount,
			'date_debut'=> $date_debut,
			'date_fin'=> $date_fin,
		]);
		return $pdf->stream();
	}}
	//RECHERCHE proforma JOURNALIÈRE
	public function rechproformaPost(){
		
		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){
			 
		$proformas=DB::table('proforma')->whereBetween('date', [$date_debut,$date_fin])->orderBydesc('id')->get();
		$total=DB::table('proforma')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		//$proformacount=DB::table('proforma')->where('etat','Validée')->get()->count();
		//$client=DB::table('client')->orderBydesc('id')->get();
		$magasin=DB::table('magasin')->get();
		//$nbtick=DB::table('proforma')->where('client_id', 2)->whereBetween('date', [$date_debut,$date_fin])->get()->count();
		$pdf = PDF::loadView('proforma/liste3', [
			'proformas'=> $proformas,
			'magasin'=> $magasin,
			'total'=> $total,
			//'client'=> $client,
			//'nbtick'=> $nbtick,
			'date_debut'=> $date_debut,
			'date_fin'=> $date_fin,
		]);
		return $pdf->stream();
	} else 
			return redirect()->route('liste-des-proformas-de-la-journee')->with('error', 'La date de fin ne doit pas dépasser la date de début!');
	}
	public function rechproformaPDF(){

		if(session('user')== null) {
            return redirect('/');
        }
		
		$date_debut = session('date_debut');
		$date_fin = session('date_fin');
		
		 if($date_fin > $date_debut || $date_fin == $date_debut){
			 
		$proformas=DB::table('proforma')->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->orderBydesc('id')->get();
		$total=DB::table('proforma')->whereBetween('date', [$date_debut,$date_fin])->sum('montant');
		//$proformacount=DB::table('proforma')->where('etat','Validée')->get()->count();
		$client=DB::table('client')->orderBydesc('id')->get();
		$pdf = PDF::loadView('proforma/liste33', [
			'proformas'=> $proformas,
			//'proformacount'=> $proformacount,
			'client'=> $client,
			'total'=> $total,
			'date_debut'=> $date_debut,
			'date_fin'=> $date_fin,
		]);
		return $pdf->stream();
	}}
	public function listeproforma2(){
		
		if(session('user')== null) {
            return redirect('/');
        }
		
		$today= date('Y-m-d');
		$proformas=DB::table('proforma')->orderBydesc('id')->get();
		$total=DB::table('proforma')->sum('montant');
		//$proformacount=DB::table('proforma')->where('etat','Validée')->get()->count();
		//$client=DB::table('client')->orderBydesc('id')->get();
		$magasin=DB::table('magasin')->get();
		return view('proforma/liste2', [
			'proformas'=> $proformas,
			'magasin'=> $magasin,
			'total'=> $total,
			//'client'=> $client,
		]);
	}
	//PAR REPAS


	function facture($id)
    {
		if(session('user')== null) {
            return redirect('/');
		}
		
		$proforma = DB::table('proforma')->where('id',$id)->first();
		$ligne = DB::table('ligne_proforma')->where('proforma_id',$id)->get();
		$lignetax = DB::table('ligne_taxe_proforma')->where('proforma_id',$id)->get();
		$tva = DB::table('ligne_taxe_proforma')->where('proforma_id',$id)->sum('montligne');
		$ligneM = DB::table('ligne_proforma')->where('proforma_id',$id)->get()->sum('montligne');
		$total_article = DB::table('ligne_proforma')->where('proforma_id',$id)->sum('qte');
		$montant = $proforma->montant;
		$montant=round($montant,0);
		$montant1 = (new \App\Classes\ChiffreEnLettre)->Conversion($montant);
        
        $pdf = \App::make('dompdf.wrapper');
        $pdf = PDF::loadView('/proforma/facture_a5', [

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

	
	function enregistrer()
    {
		$dat=request('datet');
        $hour = date('H:i:s');
		$today = implode('-', array_reverse(explode('/', $dat)));
		$todayyy = date('Y-m-d'); 
		//$typeproforma=request('typeproforma');
		$client_id=request('client_id');
		$magasin_id=request('magasin_id');
		$contact=request('contact');
		$remise=request('remise');
		$taxes=request('taxes');
		$peps=request('item');
		if (count($peps) > 0 && $peps != null){
		DB::table('proforma')->insert(
				[
					'date' => $today,
					'heure' => $hour,
					'client_id' => $client_id,
					'remise' => $remise,
					'user_id' => session('user')->id,
					'nom_user' => session('user')->nom,
					'magasin_id' => $magasin_id,
				]
			);

			$idLastproforma = DB::table('proforma')->get()->last()->id;
			$idLast= strlen($idLastproforma);
	
			if ($idLast==1){
				DB::table('proforma')->where('id', $idLastproforma)->update(
					[
						'numero' => '000'.$idLastproforma,
					]
				);
			} elseif ($idLast==2){
		
				DB::table('proforma')->where('id', $idLastproforma)->update(
					[
						'numero' => '00'.$idLastproforma,
					]
				);
			} elseif ($idLast==3){
				DB::table('proforma')->where('id', $idLastproforma)->update(
					[
						'numero' => '0'.$idLastproforma,
					]
				);
			} else {
				DB::table('proforma')->where('id', $idLastproforma)->update(
					[
						'numero' => $idLastproforma,
					]
				);
			}
	
			$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();
                
				$categorie_id =  DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->categorie_id;
                
				$prix =  DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->prix_detail;
					
					DB::table('ligne_proforma')->insert(
						[
								'proforma_id' => $idLastproforma,
								'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' => $magasin_id,
						]
					);
	}
			$mht = DB::table('ligne_proforma')->where('proforma_id', $idLastproforma)->get()->sum('montligne');
			$mht1 = $mht - $remise;
			DB::table('proforma')->where('id', $idLastproforma)->update(
				[
					'montant_ht' => $mht,
					'remise' => $remise,
					'montant_remise' => $remise,
					'net_com' => $mht1,
				]
			);
					
			$nc = DB::table('proforma')->where('id', $idLastproforma)->first()->net_com;
			if ($taxes == "Oui"){
				$tau = DB::table('taxes')->get();
					foreach($tau as $taux){
					DB::table('ligne_taxe_proforma')->insert(
							[
								'proforma_id' => $idLastproforma,
								'taux' => $taux->taux,
								'montligne' => $nc * $taux->taux,
							]
						);
					}
					$sumtaux = DB::table('ligne_taxe_proforma')->where('proforma_id', $idLastproforma)->get()->sum('montligne');
					$ttc = $sumtaux + $nc;
				} else {
					$sumtaux = 0;
					$ttc = $nc;
				}
			DB::table('proforma')->where('id', $idLastproforma)->update(
						[
							'total_taux' => $sumtaux,
							'montant' => $ttc,
						]
					);
			
		}
			return redirect()->route('proforma')->with('status', 'Proforma enregistré avec succès !');
	}
    


	public function recherche() {

		if(session('user')== null) {
            return redirect('/');
		}
		
		$date_debut = request('date_debut');
		$cli = request('client');
        $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(['cli'=> $cli]);
		
		 if($date_fin > $date_debut || $date_fin == $date_debut){
			 if ($cli==0){
				$proformas=DB::table('proforma')->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->orderBydesc('id')->get();
				$proformacount=DB::table('proforma')->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->get()->count();
			 } else {
			 $proformas=DB::table('proforma')->where('client_id',$cli)->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->orderBydesc('id')->get();
			 $proformacount=DB::table('proforma')->where('client_id',$cli)->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->get()->count();
			 }
			 
			 $client=DB::table('client')->orderBydesc('id')->get();
			return view('liste_proformarech',[
            'proformas' => $proformas,
            'date_debut' => $date_debut,
            'date_fin' => $date_fin,
            'client' => $client,
            'cli' => $cli,
            'proformacount' => $proformacount,
        ]);	
		 } else 
		 {
			return redirect()->route('listeproforma')->with('error', 'La date de fin ne doit pas dépasser la date de début!');
		 }
	}
	function liste4(){

        if(session('user')== null) {
            return redirect('/');
        }
		$date_debut = session('date_debut');
		$date_fin = session('date_fin');
		$cli = session('cli');
		if($date_fin > $date_debut || $date_fin == $date_debut){
			 if ($cli==0){
				$proformas=DB::table('proforma')->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->orderBydesc('id')->get();
				$proformacount=DB::table('proforma')->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->get()->count();
			 } else {
			 $proformas=DB::table('proforma')->where('client_id',$cli)->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->orderBydesc('id')->get();
			 $proformacount=DB::table('proforma')->where('client_id',$cli)->where('etat','Validée')->whereBetween('date', [$date_debut,$date_fin])->get()->count();
			 }
			 //$approcount = DB::table('paiement_fou')->whereBetween('date', [$date_debut,$date_fin])->get()->count();
			 $client=DB::table('client')->orderBydesc('id')->get();
		$pdf = PDF::loadView('liste_proformarechPDF', [
			'proformas' => $proformas,
            'date_debut' => $date_debut,
            'date_fin' => $date_fin,
            'client' => $client,
            'cli' => $cli,
            'proformacount' => $proformacount,
		]);
        return $pdf->stream();
	}else 
		 {
			return redirect()->route('listeproforma')->with('error', 'La date de fin ne doit pas dépasser la date de début!');
		 }
	
	}
	//PAIEMENTS CLIENTS

	//PDF RECHERCHE PAIEMENTS CLIENTS

	
	//MODIFICATION proformaS
	/*function modifproforma($id) {
        if(session('user')== null) {
            return redirect('/');
        }
        $client = DB::table('client')->get();
        $produit = DB::table('produit_vendable')->orderBy('designation','ASC')->get();
        $proforma = DB::table('proforma')->where('id',$id)->first();
        $ligne_proforma = DB::table('ligne_proforma')->where('proforma_id',$id)->get();
        return view('proforma/modifier',[
            'client' => $client,
            'produit' => $produit,
            'ligne_proforma' => $ligne_proforma,
            'proforma' => $proforma,
        ]);
    }*/
	function modifier($id) {
        if(session('user')== null) {
            return redirect('/');
        }
        $client=DB::table('client_externe')->orderby('noms','asc')->get();
        $produit = DB::table('produit_vendable')->orderBy('designation','ASC')->get();
        $proforma1 = DB::table('proforma')->where('id',$id)->first();
		
		if (session('user')->roles=="Caissier"){
		$proformas = DB::table('proforma')->where('user_id', session('user')->id)->orderBydesc('id')->take(50)->get();
		//$magasin = DB::table('magasin')->where('id', session('user')->magasin_id)->get();
		}elseif (session('user')->roles=="Validateur" || session('user')->roles=="Gérant"){
		$proformas = DB::table('proforma')->where('etat2', 'En cours')->where('magasin_id', session('user')->magasin_id)->orderBydesc('id')->take(50)->get(); 
		$magasin = DB::table('magasin')->where('id', session('user')->magasin_id)->get();
		}else{
		 $proformas = DB::table('proforma')->orderBydesc('id')->take(50)->get();  
		 $magasin = DB::table('magasin')->orderBy('nom', 'ASC')->get();
		}
		
        $ligne_proforma = DB::table('ligne_proforma')->where('proforma_id',$id)->get();
        return view('proforma/modifier',[
            'client' => $client,
            'magasin' => $magasin,
            'produit' => $produit,
            'ligne_proforma' => $ligne_proforma,
            'proforma1' => $proforma1,
            'proformas' => $proformas,
            'proforma_id' => $id,
        ]);
    }
		function modifierproformapost($id)
		{
		    $datet = request('datet');
			$todayyy =implode('-', array_reverse(explode('/', $datet)));
			$supl = DB::table('ligne_proforma')->where('proforma_id', $id)->delete();
			$sup2 = DB::table('ligne_taxe_proforma')->where('proforma_id', $id)->delete();
			$typeproforma=request('typeproforma');
			$client_id=request('client_id');
			$contact=request('contact');
			$taxes=request('taxes');
			$remise=request('remise');
			//$peps=collect(request('peps'))->chunk(3);
			$peps=request('item');

			DB::table('proforma')->where('id', $id)->update(
				[
				        'date' => $todayyy,
						'client_id' => $client_id,
						'remise' => $remise,
						'nom_user' => session('user')->nom,
				]
			);
			$idLastproforma = DB::table('proforma')->where('id', $id)->first()->id;
			$date = DB::table('proforma')->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){
						$qtesto = 0;
					}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 ($typeproforma == "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;
					}
						
						$categorie_id = DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->categorie_id;
						DB::table('ligne_proforma')->insert(
							[
									'proforma_id' => $id,
									'categorie_id' => $categorie_id,
									'produit_id' => $pep[0 +$i],
									'qte' => $pep[1 +$i],
									'remise' => 0,
									'pv' => $pep[2 +$i],
									'pr' => $pr,
									'montligne' => ($pep[1 +$i] * $pep[2 +$i]),
									'montlignepr' => ($pep[1 +$i] * $pr),
									'date' => $todayyy, 
									'user_id' => session('user')->id,
									'magasin_id' => session('user')->magasin_id,
							]
						);
							$i; 
					}  
				$mht = DB::table('ligne_proforma')->where('proforma_id', $id)->get()->sum('montligne');
				$mtremise = $remise;
				$mht1 = $mht - $mtremise;
				DB::table('proforma')->where('id', $id)->update(
							[
								'montant_ht' => $mht,
								'remise' => $mtremise,
								'montant_remise' => $mtremise,
								'net_com' => $mht1,
							]
						);
						
				$nc = DB::table('proforma')->where('id', $id)->first()->net_com;
				
				if ($taxes == "Oui"){
					$tau = DB::table('taxes')->get();
						foreach($tau as $taux){
						DB::table('ligne_taxe_proforma')->insert(
								[
									'proforma_id' => $id,
									'taux' => $taux->taux,
									'montligne' => $nc * $taux->taux,
								]
							);
						}
						$sumtaux = DB::table('ligne_taxe_proforma')->where('proforma_id', $id)->get()->sum('montligne');
						$ttc = $sumtaux + $nc;
					} else {
						$sumtaux = 0;
						$ttc = $nc;
					}
				DB::table('proforma')->where('id', $id)->update(
							[
								'total_taux' => $sumtaux,
								'montant' => $ttc,
							]
						);
				return redirect()->route('proforma')->with('status','Proforma modifiée avec succès' );
		}
	

	function modifproformav2($id)
		{
			//$todayyy =implode('-', array_reverse(explode('/', $dat)));
			$todayyy = date('Y-m-d');
			$typeproforma=request('typeproforma');
			$nomcli=request('nomcli');
			$contact=request('contact');
			$taxes=request('taxes');
			$remise=request('remise');
			//$peps=collect(request('peps'))->chunk(3);
			$peps=request('item');
            
            $vo = DB::table('ligne_proforma')->where('proforma_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 proforma',
								'en_stock' => $qtesto,
								'apres' => ($qtesto + $qteval),
								'stock_general' => ($sg + $qteval),
								'magasin_id' => session('user')->magasin_id,
								'user_id' => session('user')->id,
								'proforma_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_proforma')->where('proforma_id', $id)->delete();
			$sup2 = DB::table('ligne_taxe_proforma')->where('proforma_id', $id)->delete();
            
			DB::table('proforma')->where('id', $id)->update(
				[
						'nomcli' => $nomcli,
						'contact' => $contact,
						'remise' => $remise,
						'etat' => "Proforma",
				]
			);
			
			$date = DB::table('proforma')->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){
						$qtesto = 0;
					}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 ($typeproforma == "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;
					}*/
						
						$categorie_id = DB::table('produit_vendable')->where('id', $pep[0 +$i])->first()->categorie_id;
						DB::table('ligne_proforma')->insert(
							[
									'proforma_id' => $id,
									'categorie_id' => $categorie_id,
									'produit_id' => $pep[0 +$i],
									'qte' => $pep[1 +$i],
									'remise' => 0,
									'pv' => $pep[2 +$i],
									'pr' => $pr,
									'montligne' => ($pep[1 +$i] * $pep[2 +$i]),
									'montlignepr' => ($pep[1 +$i] * $pr),
									'date' => $todayyy,
									'user_id' => session('user')->id,
									'magasin_id' => session('user')->magasin_id,
									'etat' => "Proforma",
							]
						);
							$i; 
					}  
				$mht = DB::table('ligne_proforma')->where('proforma_id', $id)->get()->sum('montligne');
				$mtremise = $remise;
				$mht1 = $mht - $mtremise;
				DB::table('proforma')->where('id', $id)->update(
							[
								'montant_ht' => $mht,
								'remise' => $mtremise,
								'montant_remise' => $mtremise,
								'net_com' => $mht1,
							]
						);
						
				$nc = DB::table('proforma')->where('id', $id)->first()->net_com;
				/*$today = date('Y-m-d'); 
				foreach($tau as $taux){
					DB::table('ligne_taxe_proforma')->insert(
							[
								'proforma_id' => $id,
								'taux' => $taux->taux,
								'montligne' => $nc * $taux->taux,
							]
						);
				}
				$sumtaux = DB::table('ligne_taxe_proforma')->where('proforma_id', $id)->get()->sum('montligne');
				$ttc = $sumtaux + $nc;*/
				if ($taxes == "Oui"){
					$tau = DB::table('taxes')->get();
						foreach($tau as $taux){
						DB::table('ligne_taxe_proforma')->insert(
								[
									'proforma_id' => $id,
									'taux' => $taux->taux,
									'montligne' => $nc * $taux->taux,
								]
							);
						}
						$sumtaux = DB::table('ligne_taxe_proforma')->where('proforma_id', $id)->get()->sum('montligne');
						$ttc = $sumtaux + $nc;
					} else {
						$sumtaux = 0;
						$ttc = $nc;
					}
				DB::table('proforma')->where('id', $id)->update(
							[
								'total_taux' => $sumtaux,
								'montant' => $ttc,
							]
						);
				return redirect()->route('proforma-code')->with('status','proforma modifiée avec succès' );
		}
	
}