@extends('layout')
@section('title','Liste des achats fournisseur ')
@section('content')
<div class="section-header">
            <h1>Liste des achats du fournisseur {{$fou}}</h1>
            <div class="section-header-breadcrumb">
              <div class="breadcrumb-item active"><a href="#">Liste des achats fournisseur</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>Proforma</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>Référence</th>
                            <th>Date</th>
                            <th>Montant</th>
							<th>Payé</th>
							<th style="text-align:center;">Reste à payer</th>
                            <th>Etat facture</th>
                            <th>Action</th>
                          </tr>
                        </thead>
                        <tbody>
						 @foreach($appro as $appros)
							@php
							$apayer = DB::table('appro_vendable')->where('id', $appros->id)->first()->montant;
							$sommePaiement =  DB::table('paiement_fou')->where('appro_id', $appros->id)->get()->sum('montant');
							$restantE = $apayer - $sommePaiement;
							@endphp
							
							@if($sommePaiement)
                            @if($apayer - $sommePaiement >  0)
                            @php
                                $reponses = [
                                    'label' => 'warning',
                                    'etatpaie' => 'Règlement partiel',
                                    'restant' => $apayer - $sommePaiement,

                               ];

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

                            @endphp
                        @elseif($apayer - $sommePaiement == 0)
                        @php
                            $reponses = [
                                'label' => 'success',
                                'etatpaie' => 'Payé',
                                'restant' => $apayer - $sommePaiement,

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

                        @elseif($apayer - $sommePaiement < 0)

                            @php
                                $reponses = [
                                    'label' => 'primary',
                                    'etatpaie' => 'En cours',
                                    'restant' => $apayer - $sommePaiement,

                ];
                \Illuminate\Support\Facades\DB::table('appro_vendable')->where('id', $appros->id)->update(
                                   [
                                    'etatpaie' => 'En cours',
                                    'restant' => $apayer - $sommePaiement,
                                   ])
                            @endphp
                        @endif
                    @else

                    @php
                        $reponses = [
                            'label' => 'primary',
                            'etatpaie' => 'Aucun paiement',
                            'restant' => $apayer,

                        ]; \Illuminate\Support\Facades\DB::table('appro_vendable')->where('id', $appros->id)->update(
                                   [
                                    'etatpaie' => 'En cours',
                                    'restant' => $apayer,
                                   ])

                    @endphp

                    @endif
							
                          <tr>
                            <td>{{$loop->iteration}}</td>
                            <td>{{$appros->numero}}</td>
							  @php
                                $dd = implode('/', array_reverse(explode('-', $appros->date_appro)));
                              @endphp
                            <td> {{$dd}} </td>
							@php
							$lh = DB::table('ligne_appro_vendable')->where('appro_id', $appros->id)->get()->sum('montligne');	
							$lh1 =  number_format($lh, 0, ' ', ' ');
							\Illuminate\Support\Facades\DB::table('appro_vendable')->where('id', $appros->id)->update(
							[
							'montant' => $lh,
							]);
							@endphp
                            <td>  {{$lh1}} </td>
							@php
							$lhh = $sommePaiement;	
							$lhh1 =  number_format($lhh, 0, ' ', ' ');
							@endphp
                            <td>  {{$lhh1}} </td>
							@php
							$lhhh = $restantE;	
							$lhhh1 =  number_format($lhhh, 0, ' ', ' ');
							@endphp
                            <td>  {{$lhhh1}} </td>
                    
                            <td class="align-middle">
                                  <div class="badge badge-{{$reponses['label']}} badge-shadow">{{$reponses['etatpaie']}}</div>
                            </td>
							<td style="text-align:center;">
								<a type="button" href="{{route('paiementfou',['id'=>$appros->id])}}" class="btn btn-icon btn-info"><i class="fas fa-money" aria-hidden="true"></i> Paiement</a>
							</td>
                          </tr>
                      @endforeach
						<tr>
							<td colspan="3"> </td>
							<td> Total</td>
							<td> 
							@php
							$total = $total;	
							$total =  number_format($total, 0, ' ', ' ');
							@endphp
							<b>{{$total}} F</b> 
							</td>
							<td> 
							@php
							$totalpaie = $totalpaie;	
							$totalpaie =  number_format($totalpaie, 0, ' ', ' ');
							@endphp
							<b>{{$totalpaie}} F</b>
							</td>
							<td> 
							@php
							$reste = $reste;	
							$reste =  number_format($reste, 0, ' ', ' ');
							@endphp
							<b>{{$reste}}F </b>
							</td>
							<td colspan="2"> </td>
						</tr>
                        </tbody>
                      </table>
                    </div>
                  </div>
                </div>
              </div>
            </div>
           
           
          </div>
		  
             
            </div>
          </div>
		  
  <div class="modal fade" id="Validermodal" >
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Validation appro_vendable général</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="modal-body">
                Voulez-vous valider cet appro_vendable ?
              </div>
              <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="valider">
                
              </div>
            </div>
          </div>
        </div>

@endsection