@extends('layout')
@section('title','Liste des clients ')
@section('content')
		<br/>
       
		   <div class="section-body">
            <div class="row">
              <div class="col-12">
			   @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 class="card">
                  <div class="card-header">
                    <h4>Rechercher facture client pour une période</h4>
                  </div>
                  <div class="card-body">
				<form  action="{{route('liste-rech')}}" method="POST">
                         {{csrf_field()}}
				  <div class="row" style="padding-top: 30px">
					<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Client <font color="red">*</font></label>
                      <select class="form-control select2" name="client">
                        <option value="0">Tous les clients</option>
						@foreach($cli as $clients)
						<option value="{{$clients->id}}">{{DB::table('client_externe')->where('id', $clients->id)->first()->noms}}</option>
						@endforeach
                      </select>
                    </div>
					</div>
                     <div class="col-lg-3 col-md-3"> 
					  <div class="form-group">
                      <label>Date début <font color="red">*</font></label>
                      <input type="text" class="form-control" name="date_debut" id="champ_date" required>
					  <div id="calendarMain"></div>
                    </div>
					</div>
					<div class="col-lg-3 col-md-3"> 
					  <div class="form-group">
                      <label>Date fin <font color="red">*</font></label>
                      <input type="text" class="form-control" name="date_fin" id="champ_date2" required>
					  <div id="calendarMain2"></div>
                    </div>
					</div>
					<div class="col-lg-2 col-md-2" style="margin-top:25px"> 
                      <button class="btn btn-icon icon-left btn-success"><i class="fas fa-search"></i> Rech</button>
					</div>
                      </div>
					<br><br>	  
				</form>
					  
					    </div>
						 
                    </div>
                <div class="card">
                     @php
						         $today = date('Y-m-d');
						         $df = date_parse($today);
						         $y = $df['year'];
				    @endphp
                  <div class="card-header">
                    <h4>Comptes clients</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">Noms</th>
                            <th class="text-center">Solde initial <font color="red">{{$y}}</font></th>
							<th style="text-align:center;">Total à payer</th>
							<th style="text-align:center;">Payé</th>
							<th style="text-align:center;">Reste à payer</th>
                            <th  style="text-align:center;">Action</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($client as $clients)
						
						   @php
						         $today = date('Y-m-d');
						         $df = date_parse($today);
						         $y = $df['year'];
						         $si = DB::table('soldepaiment')
						                ->whereYear('date', '=', $y)
						                ->where('etat', 'Validée')
						                ->where('client_id', $clients->id)
						                ->get()->count();
						         if ($si>0){
						         $sd = DB::table('soldepaiment')
						                ->whereYear('date', '=', $y)
						                ->where('etat', 'Validée')
						                ->where('client_id', $clients->id)
						                ->orderBydesc('id')
						                ->take(1)->first()->montant;
						         }else{
						            $sd = 0;
						         }
								 $apayer0 = DB::table('vente_client')->where('client_id',$clients->id)->where('etat','Validée')->get()->sum('montant');
								 $retour =  DB::table('retourcommandeclient')->where('client_id', $clients->id)->where('etat','Validée')->get()->sum('montant');
								 $apayer = $apayer0 - $retour;
								 $sommePaiement =  DB::table('paiement_client')->where('client_id', $clients->id)->get()->sum('montant');
								 $restantF = ($apayer + $sd) - $sommePaiement;

							@endphp
							
                          <tr>
							<td style="text-align:center;">{{$loop->iteration}}</td>
							<td style="text-align:center;">{{$clients->noms}} </td>
							<td style="text-align:center;">{{ number_format($sd, 0, ' ', ' ') }} </td>
							<td style="text-align:center;">
							@php
							$lh = $apayer;
							$lh1 =  number_format($lh, 0, ' ', ' ');
							echo $lh1."&nbsp;F";
							@endphp
							</td>
							<td style="text-align:center;">
							@php
							$lhhh = $sommePaiement;
							$lhhh1 =  number_format($lhhh, 0, ' ', ' ');
							echo $lhhh1."&nbsp;F";
							@endphp
							</td>
							<td style="text-align:center;">
							@php
							$lhh = $restantF;
							$lhh1 =  number_format($lhh, 0, ' ', ' ');
							echo $lhh1."&nbsp;F";
							@endphp
							</td>
							<td style="text-align:center;">
								<a type="button" href="{{route('vente_client_externe',['id'=>$clients->id])}}" target="_blank"
									class="btn btn-icon btn-primary" data-toggle="tooltip" data-placement="bottom" title="Toutes les factures">
									<i class="fas fa-print" aria-hidden="true"></i>
								</a>
								<a type="button" href="{{route('facturepaiefinale',['id'=>$clients->id])}}" target="_blank"
									class="btn btn-icon btn-success" data-toggle="tooltip" data-placement="bottom" title="Facture globale">
									<i class="fas fa-print" aria-hidden="true"></i>
								</a>
								<a type="button" href="{{route('facturepaiefinale2',['id'=>$clients->id])}}" target="_blank"
									class="btn btn-icon btn-secondary" data-toggle="tooltip" data-placement="bottom" title="Facture globale EN">
									<i class="fas fa-print" aria-hidden="true"></i>
								</a>
								<a type="button" href="{{route('soldeini',['id'=>$clients->id])}}" class="btn btn-icon btn-danger"><i class="fas fa-money" aria-hidden="true"></i> <i class="fas fa-plus"></i>Solde</a>
							    <a type="button" href="{{route('paiementclient',['id'=>$clients->id])}}" 
									class="btn btn-icon btn-info" data-placement="bottom" title="Ajouter paiement">
								<i class="fas fa-money" aria-hidden="true"></i> <i class="fas fa-plus"></i>Paie.</a>
							</td>
                          </tr>
						  @endforeach
                        </tbody>
                      </table>
                    </div>
                  </div>
                </div>
              </div>
            </div>
           
           
          </div>
		  
             
            </div>
          </div>
		  
 
		  
 @push('scripts') 
	   
@endpush

@endsection