@extends('layout')
@section('title','Mouvements stock dépôt ')
@section('description','Mouvements stock dépôt')
@section('content')
			<br/>
          <div class="section-body">
		  <br/>
			<div class="row">
			<div class="col-md-12">
			<a type="button" target="_blank" href="{{route('magasingeneralpdfPost')}}" class="btn btn-icon btn-info pull-right"><i class="fas fa-print"></i> Imprimer</a>	
			<a class="btn btn-icon btn-warning pull-right" href="{{route('nouveauProduit')}}"> <i class="fa fa-sign-plus"></i> Ajouter nouveau</a>
			<br/>
			<br/>
			</div>
			
              <div class="col-md-12">
                  @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>Mouvements stock dépôt : {{DB::table('depot')->where('id', $depot_id)->first()->nom}} |
					@if ($date_debut == $date_fin)
					{{$date_debut}} 
					@else
					{{$date_debut}} au {{$date_fin}}
					@endif
					</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;">Initial</th>
							<th style="text-align:center;">Entrées(Qté approv.)</th>
							<th style="text-align:center;">Sorties(Qté transférée)</th>
							<th style="text-align:center;">En stock</th>
							<th style="text-align:center;">Actions</th>
						</tr>
                        </thead>
                       <tbody>
	@foreach($produits as $produit)
	@php
	if ($date_debut == ""){
	   $vinv = DB::table('magasin_general')
	        ->where('produit_id', $produit->id)
	        ->where('depot_id', $depot_id)
	        ->where('type_operation', 'Inventaire')
	        ->get()->count();
	   if ($vinv>0){
	    $init = DB::table('magasin_general')
	        ->where('produit_id', $produit->id)
	        ->where('depot_id', $depot_id)
	        ->where('type_operation', 'Inventaire')
	        ->orderBydesc('id')
	        ->take(1)->first();
	    $entree1 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Appro')
	            ->where('id', '>', $init->id)
	            ->sum('qte');
	   $entree2 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'TransfertRecu')
	            ->where('id', '>', $init->id)
	            ->sum('qte');
	   $entree0 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Annulation appro')
	            ->where('id', '>', $init->id)
	            ->sum('qte');
	   $entree00 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Annulation transfertrecu')
	            ->where('id', '>', $init->id)
	            ->sum('qte');
	   $entree = ($entree1 + $entree2) - ($entree0 + $entree00);
	    $sortie1 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Transfert')
	            ->where('id', '>', $init->id)
	            ->sum('qte');
	   $sortie0 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Annulation transfert')
	            ->where('id', '>', $init->id)
	            ->sum('qte');
	  $sortie20 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Vente')
	            ->where('id', '>', $init->id)
	            ->sum('qte');
	   $sortie211 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Annulationvente')
	            ->where('id', '>', $init->id)
	            ->sum('qte');
	   $sortie = ($sortie1 + $sortie20) - ($sortie0 + $sortie211) ;
	   $initial = $init->qte;    
	    $totap = $initial + $entree;
	    $totstk = $totap - $sortie;
	    
	   }else
	   {
	         $initial = 0;
	         $entree1 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Appro')
	            ->sum('qte');
	        $entree2 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'TransfertRecu')
	            ->sum('qte');
	       $entree0 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Annulation appro')
	            ->sum('qte');
	       $entree00 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Annulation transfertrecu')
	            ->sum('qte');
	           $entree = ($entree1 + $entree2) - ($entree0 + $entree00);
	         $sortie1 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Transfert')
	            ->sum('qte');
	       $sortie0 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Annulation transfert')
	            ->sum('qte');
	    $sortie20 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Vente')
	            ->sum('qte');
	       $sortie201 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Annulationvente')
	            ->sum('qte');
	       $sortie = ($sortie1 + $sortie20) - ($sortie0 + $sortie201);
	    $totap = $initial + $entree;
	    $totstk = $totap - $sortie;
	   }
	} else {
	    $vinv = DB::table('magasin_general')
	        ->where('produit_id', $produit->id)
	        ->where('depot_id', $depot_id)
	        ->where('type_operation', 'Inventaire')
	        ->whereBetween('date', [$date_debut,$date_fin])
	        ->get()->count();
	   if ($vinv>0){
	    $init = DB::table('magasin_general')
	        ->where('produit_id', $produit->id)
	        ->where('depot_id', $depot_id)
	        ->where('type_operation', 'Inventaire')
	        ->orderBydesc('id')
	        ->take(1)->first();
	    $entree1 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Appro')
	            ->where('id', '>', $init->id)
	            ->sum('qte');
	   $entree2 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'TransfertRecu')
	            ->where('id', '>', $init->id)
	            ->sum('qte');
	   $entree0 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Annulation appro')
	            ->where('id', '>', $init->id)
	            ->sum('qte');
	    $entree00 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Annulation transfertrecu')
	            ->where('id', '>', $init->id)
	            ->sum('qte');
	   $entree = ($entree1 + $entree2) - ($entree0 + $entree00);
	    $sortie1 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Transfert')
	            ->where('id', '>', $init->id)
	            ->sum('qte');
	   $sortie0 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Annulation transfert')
	            ->where('id', '>', $init->id)
	            ->sum('qte');
	           $sortie3 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Vente')
	            ->where('id', '>', $init->id)
	            ->sum('qte');
	           $sortie33 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Annulationvente')
	            ->where('id', '>', $init->id)
	            ->sum('qte');
	   $sortie = ($sortie1 + $sortie3) - ($sortie0 + $sortie33);
	   $initial = $init->qte;    
	    $totap = $initial + $entree;
	    $totstk = $totap - $sortie;
	    
	   }else
	   {
	         $initial = 0;
	         $entree1 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Appro')
	            ->whereBetween('date', [$date_debut,$date_fin])
	            ->sum('qte');
	        $entree2 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'TransfertRecu')
	            ->whereBetween('date', [$date_debut,$date_fin])
	            ->sum('qte');
	       $entree0 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Annulation appro')
	            ->whereBetween('date', [$date_debut,$date_fin])
	            ->sum('qte');
	        $entree00 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Annulation transfertrecu')
	            ->whereBetween('date', [$date_debut,$date_fin])
	            ->sum('qte');
	       $entree =($entree1 + $entree2) - ($entree0 + $entree00);
	         
	         $sortie1 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Transfert')
	            ->whereBetween('date', [$date_debut,$date_fin])
	            ->sum('qte');
	            
	       $sortie0 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Annulation transfert')
	            ->whereBetween('date', [$date_debut,$date_fin])
	            ->sum('qte');
	       $sortie3 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Vente')
	            ->whereBetween('date', [$date_debut,$date_fin])
	            ->sum('qte');
	           $sortie33 = DB::table('magasin_general')
	            ->where('produit_id', $produit->id)
	            ->where('depot_id', $depot_id)
	            ->where('type_operation', 'Annulationvente')
	            ->whereBetween('date', [$date_debut,$date_fin])
	            ->sum('qte');
	           $sortie = ($sortie1 + $sortie3) - ($sortie0 + $sortie33);
	            
	    $totap = $initial + $entree;
	    $totstk = $totap - $sortie;
	   }
	}
		
	@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;"> {{$initial}}</td>
        <td style="text-align:center;font-size:14px;"> {{$totap}}</td>
        <td style="text-align:center;font-size:14px;">{{$sortie}}</td>
		<td style="text-align:center;font-size:14px;">
		    @if($produit->stock_mini >= $totstk)
            <span class="badge badge-danger badge-shadow">
		        {{$totstk}}
			</span>
			@else
			<span class="badge badge-success badge-shadow">
    			{{$totstk}}
			</span>
			@endif
		
        </td>
        <td class="center" style="text-align:center;">
			<a class="btn btn-icon btn-danger" href="{{route('detail-mouvement-depot',['id'=>$produit->id])}}">
			<i class="fas fa-file"></i>
            </a>
			<a target="_blank" 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