@extends('layout')
@section('title','Stock du magasin général ')
@section('description','Stock du magasin général')
@section('content')
			<br/>
          <div class="section-body">
		  <br/>
			<div class="row">
			<div class="col-md-12">
			@if (session('user')->roles=="Admin")
			<a class="btn btn-icon btn-warning pull-right" href="{{route('nouveauProduit')}}"> <i class="fa fa-sign-plus"></i> Ajouter nouveau produit</a>
			@endif
			<a style="margin-right:13px" class="btn btn-icon btn-danger pull-right" href="{{route('inventaire-general')}}"> <i class="fas fa-file-contract"></i> Faire inventaire</a>
			<br/>
			<br/>
			</div>
			
              <div class="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
			  @if (session('user')->roles=="Admin")
			  <div class="card">
                  <div class="card-header">
                    <h4>Recherche</h4>
                  </div>
                  <div class="card-body">
				<form  action="{{route('magasingeneralPost')}}" method="POST">
                         {{csrf_field()}}
				  <div class="row" style="padding-top: 30px">
				      <div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Nom du dépot<font color="red">*</font></label>
                      <select class="form-control select2" name="depot_id" required>
                         <option value="">Sélectionner le depot</option>
						 @foreach($depot as $depots)
						<option value="{{$depots->id}}">{{$depots->nom}} </option>
						@endforeach
                      </select>
                    </div>
					</div>
                     <div class="col-lg-3 col-md-12"> 
					  <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">
					  <div id="calendarMain"></div>
                    </div>
					</div>
					<div class="col-lg-3 col-md-12"> 
					  <div class="form-group">
                      <label>Date fin <font color="red">*</font></label>
                      <input type="text" class="form-control" name="date_fin" id="champ_date2">
					  <div id="calendarMain2"></div>
                    </div>
					</div>
					<div class="col-lg-2 col-md-12"  style="margin-top:25px"> 
                      <button class="btn btn-icon icon-left btn-primary"><i class="fas fa-search"></i> Rech</button>
					</div>
                      </div>
					<br><br>	  
				</form>
					  
					    </div>
						 
                    </div>
					@endif
                <div class="card">
                  <div class="card-header">
                    <h4>Stock disponible en dépôt</h4>
                  </div>
                  <div class="card-body">
                    <div class="table-responsive">
                      <table class="table table-striped" id="table-1">
                        <thead>
                          <tr>
							<th style="text-align:center;">N°</th>
							<th style="text-align:center;">Désignation</th>
							<th style="text-align:center;">Entrées(Approv.)</th>
							<th style="text-align:center;">Sorties(Transf.)</th>
							<th style="text-align:center;">Stock(Qté)</th>
							<th style="text-align:center;">Actions</th>
						</tr>
                        </thead>
                       <tbody>
                    	@foreach($produits as $produit)
                    	   @php
                    	   
                    	        
                    	   
                    		    $totap0 = DB::table('ligne_appro_vendable')->where('produit_id', $produit->id)->where('etat', 'Validée')->get()->sum('qte');
                    		    $totap00 =  DB::table('magasin_general')->where('produit_id', $produit->id)->where('type_operation', 'Inventaire')->sum('qte');
                    		    $totap01 =  DB::table('magasin_general')->where('produit_id', $produit->id)->where('type_operation', 'Appro')->sum('qte');
                    		    $totap02 =  DB::table('magasin_general')->where('produit_id', $produit->id)->where('type_operation', 'Annulation appro')->sum('qte');
                    			$totap = ($totap00 + $totap01) - $totap02;
                    		    $idcat = $produit->categorie_id;
                    			//$totvente0 = DB::table('magasin_general')->where('produit_id', $produit->id)->where('type_operation', 'Transfert')->get()->sum('qte');
                    			$totvente1 = DB::table('magasin_general')->where('produit_id', $produit->id)->where('type_operation', 'Transfert')->get()->sum('qte');
                    			$totvente = $totvente1;
                    			$count = DB::table('magasin_general')->where('produit_id', $produit->id)->get()->count();
                    			//$apres = DB::table('magasin_general')->where('id', $produit->id)->first()->stock_magasin;
                    			if ($count>0){
                    			$apres = DB::table('magasin_general')->where('produit_id', $produit->id)->orderBydesc('id')->take(1)->first()->apres;
                    			} else{
                    				$apres = 0;
                    			}
                    			
                    			
                    	@endphp
                    	<tr>
                            <td style="text-align:center;font-size:14px;">{{$loop->iteration}}</td>
                            <td style="text-align:center;font-size:14px;">{{$produit->desi}}</td>
                            <td style="text-align:center;font-size:14px;"> {{$totap}}</td>
                            <td style="text-align:center;font-size:14px;">{{$totvente}}</td>
                            
                    		<td style="text-align:center;font-size:18px;">
                    		    
                    			@if($produit->stock_mini >= $apres)
                                <span class="badge badge-danger badge-shadow">
                    			{{$apres}}
                    			</span>
                    			@else
                    			<span class="badge badge-success badge-shadow">
                    			{{$apres}}
                    			</span>
                    			@endif
                            </td>
                            <td class="center" style="text-align:center;">
                    			<!--<a class="btn btn-icon btn-warning" href="{{route('detail-appro-produit',['id'=>$produit->id])}}">Mouvements d'entrée
                                </a> -->
                    			<a class="btn btn-icon btn-danger" href="{{route('detail-mouvement-magasin-general',['id'=>$produit->id])}}">
                    			<i class="fas fa-file"></i>
                                </a>
                    			<a class="btn btn-info" href="{{route('modifierproduit',['id'=>$produit->id])}}">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <a target="_blank" class="btn btn-warning" href="{{route('listeproduitmagpdf22',['id'=>$produit->id])}}">
                                    <i class="fas fa-eye"></i>
                                </a>
                            </td>
                        </tr>
                    	@endforeach
                        </tbody>
                      </table>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
		<div class="modal fade bd-example-modal-sm" id="Validermodal">
			<div class="modal-dialog modal-sm">
				<div class="modal-content">
					<div class="modal-body">
						<p>Voulez-vous valider la facture ?</p>
					</div>
					<div class="modal-footer" style="background-color:#ffffff" id="valider">

					</div>
				</div>
			</div>
		</div>

        <div class="modal fade bd-example-modal-sm" id="Annulermodal">
			<div class="modal-dialog modal-sm">
				<div class="modal-content">
					<div class="modal-body">
						<p>Etes-vous sûr d'annuler cette facture ?</p>
					</div>
					<div class="modal-footer" style="background-color:#ffffff" id="annuler">

					</div>
				</div>
			</div>
		</div>
		<div class="modal fade bd-example-modal-sm" id="Supprimermodal">
			<div class="modal-dialog modal-sm">
				<div class="modal-content">
					<div class="modal-body">
						<p>Etes-vous sûr de supprimer cette facture ?</p>
					</div>
					<div class="modal-footer" style="background-color:#ffffff" id="supprimer">

					</div>
				</div>
			</div>
		</div>

@endsection