@extends('layout')
@section('title','Stock des emballages')
@section('content')
		<br/>
          <div class="section-body">
            <div class="row">
              <div class="col-lg-10 offset-1 col-md-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
		<br/>
                  </div>
                </div>
          </div>
		  
		  
		   <div class="section-body">
            <div class="row">
              <div class="col-12">
                <div class="card">
                  <div class="card-header">
                    <h4>Stock emballage</h4>
                  </div>
                  <div class="card-body">
                    <div class="table-responsive">
                      <table class="table table-striped" id="table-1">
                        <thead>
                          <tr style="text-align:center;">
                            <th class="text-center"> N° </th>
                            <th class="text-center">Désignation</th>
                            <th class="text-center">Initial</th>
                            <th class="text-center">Appro</th>
                            <th class="text-center">Utilisés</th>
                            <th class="text-center">En stock</th>
                          </tr>
                        </thead>
                        <tbody>
						@foreach($emballage as $emballages)
					    @php
					   
			
							$totap00c =  DB::table('stock_emballage')->where('emballage_id', $emballages->id)->where('type_operation', 'Inventaire')->first();
							if ($totap00c){
								$totap00 =  DB::table('stock_emballage')->where('emballage_id', $emballages->id)->where('type_operation', 'Inventaire')->orderBydesc('id')->take(1)->first()->qte;
								$dd =  DB::table('stock_emballage')->where('emballage_id', $emballages->id)->where('type_operation', 'Inventaire')->orderBydesc('id')->take(1)->first()->date;
								$totap012 =  DB::table('stock_emballage')->where('emballage_id', $emballages->id)->where('type_operation', 'Appro')->where('date','>=', $dd)->sum('qte');
								$totap011 =  DB::table('stock_emballage')->where('emballage_id', $emballages->id)->where('type_operation', 'Annulationappro')->where('date','>=', $dd)->sum('qte');
								$totap01 = $totap012 - $totap011;
							} else {
								$totap00 = 0;
								$totap012 =  DB::table('stock_emballage')->where('emballage_id', $emballages->id)->where('type_operation', 'Appro')->sum('qte');
								$totap011 =  DB::table('stock_emballage')->where('emballage_id', $emballages->id)->where('type_operation', 'Annulationappro')->sum('qte');
								$totap01 = $totap012 - $totap011;
							}
							
							
							$totap =$totap01;
							//$idcat = $produit->categorie_id;

							$totvente = DB::table('stock_emballage')->where('emballage_id', $emballages->id)->where('type_operation', 'Transfert')->get()->sum('qte');
							$totvente1 = DB::table('stock_emballage')->where('emballage_id', $emballages->id)->where('type_operation', 'Consignation')->get()->sum('qte');
							$totvente2 = DB::table('stock_emballage')->where('emballage_id', $emballages->id)->where('type_operation', 'Production')->sum('qte');
							$totvente3 = DB::table('stock_emballage')->where('emballage_id', $emballages->id)->where('type_operation', 'AnnulationProduction')->sum('qte');
							$retour = DB::table('stock_emballage')->where('emballage_id', $emballages->id)->where('type_operation', 'Retour')->get()->sum('qte');
							$reste = $totvente - $retour;
							//$totv = $totvente + $totvente1;
							$totv = $totvente2 - $totvente3;
							
							$count = DB::table('stock_emballage')->where('emballage_id', $emballages->id)->get()->count();
							if ($count){
							$apres = DB::table('stock_emballage')->where('emballage_id', $emballages->id)->orderBydesc('id')->take(1)->first()->apres;
							} else {
								$apres=0;
							}
						@endphp
                          <tr>
							<td style="text-align:center;">{{$loop->iteration}}</td>
							<td style="text-align:center;">{{$emballages->designation}}</td>
							<td style="text-align:center;">{{$totap00}}</td>
							<td style="text-align:center;">{{$totap}}</td>
							<td style="text-align:center;">{{$totv}}</td>
							<td style="text-align:center;">{{$apres}}</td>
                          </tr>
						  @endforeach
                        </tbody>
                      </table>
                    </div>
                  </div>
                </div>
              </div>
            </div>
           
           
          </div>
		  
             
            </div>
          </div>
		  
  <div class="modal fade" id="Supprimermodal" >
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Suppression</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 supprimer ce type d'emballage  ?
              </div>
              <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="delete">
                
              </div>
            </div>
          </div>
        </div>
		  
	   @push('scripts') 
	   
	         <script>
        $(".data-supprimer").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/es/Emballage/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('SupprimeremballagePost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.emballage.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#delete').html('').append(html);
          })
          $('#Supprimermodal').modal();
        });
    </script>
@endpush

@endsection