@extends('layout')
@section('title','Liste des ventes par client')
@section('content')
		<br/>
		<div class="section-header">
            <h4>Liste des  ventes du client /  <strong style="font-size: 20px ; color : blue"> {{$client}}  </strong> </h4>
            <div class="section-header-breadcrumb">
              <div class="breadcrumb-item active"><a href="#">Liste des  ventes par client</a></div>
              <div class="breadcrumb-item"><a href="#">Liste</a></div>
            </div>
          </div>
          <div class="section-body">
		    @if (session('status'))
            <div class="alert alert-warning" style="font-size: 15px; background-color: #328039; color: white">
                <i class="fa fa-check"></i> &nbsp; {{ session('status') }}
            </div>
        @endif

        @if (session('error'))
            <div class="alert alert-danger" style="font-size: 15px; background-color: #fb5757; color: white">
                <i class="ti-na"></i> {{ session('error') }}
            </div>
        @endif
        @if ($errors->any())
            <div class="alert alert-danger" style="font-size: 15px; background-color: #fb5757; color: white" >
                <ul>
                    @foreach ($errors->all() as $error)
                        <li> <i class="ti-na"></i> {{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif
          </div>
    
		   <div class="section-body">
            <div class="row">
              <div class="col-12">
                <div class="card">
                  <div class="card-header">
                    <h4>Factures ventes</h4>
                  </div>
                  <div class="card-body">
                    <div class="table-responsive">
                      <table class="table table-striped" id="table-1">
                        <thead>
                          <tr>
                            <th class="text-center"> N° </th>
                            <th class="text-center">ID de vente</th>
							<th class="text-center">Date</th>
                            <th class="text-center">Montant</th>
                            <!--<th class="text-center">Payé</th>-->
                            <!--<th class="text-center">Reste à payer</th>-->
                            <!--<th class="text-center">Etat</th>-->
                            <th class="text-center">Actions</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($vente as $ventes)
						
							@php
							$apayer0 = DB::table('vente_client')->where('id', $ventes->id)->first()->montant;
							$retour =  DB::table('retourcommandeclient')->where('vente_id', $ventes->id)->where('etat','Validée')->get()->sum('montant');
							$apayer = $apayer0 - $retour;
							$sommePaiement =  DB::table('paiement_client')->where('vente_id', $ventes->id)->get()->sum('montant');
							$restantE = $apayer - $sommePaiement;
							@endphp
							
							@if($sommePaiement)
                            
                        @if($apayer - $sommePaiement == 0)
                        @php
                            $reponses = [
                                'label' => 'success',
                                'etatpaie' => 'Payé',
                                'restant' => $apayer - $sommePaiement,

                            ];
                           \Illuminate\Support\Facades\DB::table('vente_client')->where('id', $ventes->id)->update(
                                            [
                                                'etatpaie' => 'Payé',
                                                'restant' => $apayer - $sommePaiement,
                                            ])
                        @endphp

                        @elseif($apayer - $sommePaiement <= 0)

                            @php
                                $reponses = [
                                    'label' => 'danger',
                                    'etatpaie' => 'Aucun paiement',
                                    'restant' => $apayer - $sommePaiement,

                ];
                \Illuminate\Support\Facades\DB::table('vente_client')->where('id', $ventes->id)->update(
                                   [
                                    'etatpaie' => 'Aucun paiement',
                                    'restant' => $apayer - $sommePaiement,
                                   ])
                            @endphp
                        @endif
                    @else

                    @php
                        $reponses = [
                            'label' => 'warning',
                            'etatpaie' => 'Règlement partiel',
                            'restant' => $apayer,

                        ]; 
						\Illuminate\Support\Facades\DB::table('vente_client')->where('id', $ventes->id)->update(
                                   [
                                    'etatpaie' => 'Règlement partiel',
                                    'restant' => $apayer,
                                   ])

                    @endphp

                    @endif
						
						
                          <tr>
						    <td>{{$loop->iteration}}</td>
							<td style="text-align:center;">{{$ventes->id}}</td>
							
							@php
							
							 $dd = implode('/', array_reverse(explode('-', $ventes->date)));
							 
							@endphp
							
							<td style="text-align:center;">{{$dd}} {{$ventes->heure}}</td>
							<td style="text-align:center;">{{number_format($apayer, 0, ' ', ' ')}}</td>
							<!--<td style="text-align:center;">{{number_format($sommePaiement, 0, ' ', ' ')}}</td>-->
							<!--<td style="text-align:center;">{{number_format($restantE, 0, ' ', ' ')}}</td>-->
							<!--<td style="text-align:center;">
                                        @if( $ventes->etatpaie == "Règlement partiel")
                                        <div class="badge badge-warning badge-shadow">{{$ventes->etatpaie}}</div>
                                        @elseif( $ventes->etatpaie == "Payé")
                                        <div class="badge badge-success badge-shadow">{{$ventes->etatpaie}}</div>
                                        @else
                                        <div class="badge badge-info badge-shadow">{{$ventes->etatpaie}}</div>
                                        @endif
                                    </td>-->
							<td style="text-align:center;">
							    <a type="button" target="_blank" href="{{route('factureclient',['id'=>$ventes->id])}}" class="btn btn-icon btn-warning"><i class="fas fa-print"></i></a>
								<a type="button" target="_blank" href="{{route('factureclient21',['id'=>$ventes->id])}}" class="btn btn-icon btn-secondary" data-toggle="tooltip" data-placement="bottom" title="Invoice"><i class="fas fa-print"></i></a>
								<!--<a type="button" href="{{route('paiementclient',['id'=>$ventes->id])}}" class="btn btn-icon btn-info"><i class="fas fa-money" aria-hidden="true"></i> <i class="fas fa-plus"></i>Paie.</a>-->
							</td>
							
						  </tr>
						  @endforeach
                        </tbody>
                        <tfoot>
						<tr>
						@php
							$total = $total;	
							$total =  number_format($total, 0, ' ', ' ');
							@endphp
							<td> </td>
							<td> </td>
							<td style="text-align:right;"><b> TOTAL ACHATS</b></td>
							<td style="text-align:center;"><b>{{$total}} F</b> </td>
							<!--<td style="text-align:center;"> 

							</td>
							<td> </td>-->
						</tr>
                        </tfoot>
                      </table>
                    </div>
                  </div>
                </div>
              </div>
            </div>
           
           
          </div>
		  
             
            </div>
          </div>
		  
@push('scripts') 
	     
 
@endpush

@endsection