@extends('layout')
@section('title','Rechercher Inventaire magasin ')
@section('description','Rechercher Inventaire magasin ')
@section('content')
<div class="section-header">
            <h1>Inventory search</h1>
            <div class="section-header-breadcrumb">
              <div class="breadcrumb-item active"><a href="#">Inventaire magasin</a></div>
              <div class="breadcrumb-item"><a href="#">Recherche</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 class="row">
              <div class="col-lg-12 col-md-12 ">
                <div class="card">
                  <div class="card-header">
                    <h4>Select date</h4>
                  </div>
				  
                  <div class="card-body">
				  
				  <h7><strong>Les champs en * sont obligatoires</strong></h7>
				  
				<form method="POST" action="{{route('rinventairemagasinPost')}}">
                         {{csrf_field()}}
				  <div class="row" style="padding-top: 30px">
				        <div class="col-lg-3 col-md-3"> 
    					    <div class="form-group">
                            <label>Date inventaire<font color="red">*</font></label>
                            <input type="text" class="form-control" name="dateinv" id="champ_date2" value="{{implode('/', array_reverse(explode('-', $dateinv)))}}" required>
    					    <div id="calendarMain2"></div>
                            </div>
    					</div>
					  <div class="col-lg-5 col-md-12"> 
					    <div class="form-group">
                        <label>Categorie <font color="red">*</font></label>
                            <select  class="form-control select2" name="categorie_id">
                                <option value="">Selectionner la categorie</option>
    						    @foreach($categorie as $categories)
    							<option value="{{$categories->idcat}}">{{$categories->libelle}}</option>
    							@endforeach
                            </select>
                        </div>
					</div>
					<div class="col-lg-4 col-md-12">
                            <button class="btn btn-icon icon-left btn-primary"><i class="fas fa-check"></i> Search</button>
                        </div>
					</div>
					  
				</form>
					  
					    </div>
						 
                    </div>
                    
                    
                    <div class="card">
                  <div class="card-header">
                    <h4>Résultat de la categorie : {{$nom_cat}} | Date : <b>{{ $dateinv }}</b></h4>
                    <a href="{{route('rinventairemagasinpdf')}}" class="btn btn-info" target="_blank"><i class="fa fa-print"></i>Exporter PDF</a>
                  </div>
                  <div class="card-body">
                        @php
                            $total_achat = 0;
                            $total_vente = 0;
                            $total_perte = 0;
                            $total_benef = 0;
                        @endphp
                    <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;">Designation</th>
							<th style="text-align:center;">Transfert <br/> recu</th>
							<th style="text-align:center;">Vente</th>
							<th style="text-align:center;">Reste</th>
							<th style="text-align:center;">Théorique</th>
							<th style="text-align:center;">Réel <br/> (compté)</th>
							<th style="text-align:center;">Difference</th>
							<th style="text-align:center;">Valeur à <br/> la perte</th>
							<th style="text-align:center;">Valeur à <br/> la vente</th>
							<th style="text-align:center;">Valeur à <br/> l'achat</th>
							<th style="text-align:center;">Bénef à <br/> venir</th>
						</tr>
                        </thead>
                       <tbody>
	        @foreach($produits as $produit)
    	    @php
    		    $inv =  DB::table('stock_magasin')->where('produit_id', $produit->id)->where('type_operation', 'Inventaire')->where('date', $dateinv)->first();
    		    if ($inv){
    		        $enstock = $inv->en_stock;
    		        $apres = $inv->apres;
    		    }else{
    		        $enstock = 0;
    		        $apres = 0;
    		    }
    		    $diff = $enstock - $apres;
    		    $transfert_recu = DB::table('ligne_transfert')->where('produit_id', $produit->id)->where('etat', 'Validé')->where('date', '<', $dateinv)->sum('qte');
    		    
    		    $v_cash = DB::table('ligne_vente')->where('produit_id', $produit->id)->where('etat', 'Validée')->where('date', '<', $dateinv)->sum('qte');
    		    $v_credit = DB::table('ligne_vente_client')->where('produit_id', $produit->id)->where('etat', 'Validée')->where('date', '<', $dateinv)->sum('qte');
    		    
    		    $r_cash = DB::table('ligneretourcommande')->where('produit_id', $produit->id)->where('etat', 'Validée')->where('date', '<', $dateinv)->sum('qte');
    		    $r_credit = DB::table('ligneretourcommandeclient')->where('produit_id', $produit->id)->where('etat', 'Validée')->where('date', '<', $dateinv)->sum('qte');
    		    
    		    $valeur_vente = $produit->prix_detail * $apres;
                $valeur_achat  = $produit->pr * $apres;
                $valeur_perte = $produit->prix_detail * $diff;
                
                $benef_a_venir = $valeur_vente - $valeur_achat;
            
                $total_achat += $valeur_achat;
                $total_vente += $valeur_vente;
                $total_perte += $valeur_perte;
                $total_benef += $benef_a_venir;
                
                
    		    
    	    @endphp
	<tr>
        <td style="text-align:center;font-size:14px;">{{$loop->iteration}}</td>
        <td style="text-align:center;font-size:14px;"> {{$produit->desi}}/{{$produit->code}}</td>
        <td style="text-align:center;font-size:14px;">{{$transfert_recu}}</td>
        <td style="text-align:center;font-size:14px;">{{($v_cash + $v_credit) - ($r_cash + $r_credit)}}</td>
        <td style="text-align:center;font-size:14px;">{{ ($inv->qte + $transfert_recu) - ($v_cash + $v_credit - ($r_cash + $r_credit))}}</td>
        <td style="text-align:center;font-size:14px;">{{$enstock}}</td>
        <td style="text-align:center;font-size:14px;"> {{$inv->qte}}</td>
		<td style="text-align:center;font-size:14px;">
			@if($apres > $enstock)
            <span class="badge badge-warning badge-shadow">
			    {{$diff}}
			</span>
			@endif
			@if($enstock > $apres)
			<span class="badge badge-danger badge-shadow">
			    {{$diff}}
			</span>
			@endif
			@if($apres == $enstock)
            <span class="badge badge-success badge-shadow">
			    {{$diff}}
			</span>
			@endif
        </td>
        <td style="text-align:center;font-size:14px;">  {{number_format(( $valeur_perte ), 0, ' ', ' ') }} </td>
        <td style="text-align:center;font-size:14px;">  {{number_format(( $produit->prix_detail * $apres ), 0, ' ', ' ') }} </td>
        <td style="text-align:center;font-size:14px;">  {{number_format(( $produit->pr * $apres ), 0, ' ', ' ') }} </td>
        <td style="text-align:center;font-size:14px;">  {{number_format($benef_a_venir, 0, ' ', ' ') }} </td>
    </tr>
	@endforeach
	
                        </tbody>
                        <tfoot>
                        <tr>
                    	   <td style="text-align:center;font-size:14px;">   </td> 
                    	   <td style="text-align:center;font-size:14px;">   </td> 
                    	   <td style="text-align:center;font-size:14px;">   </td> 
                    	   <td style="text-align:center;font-size:14px;">   </td> 
                    	   <td style="text-align:center;font-size:14px;">   </td> 
                    	   <td style="text-align:center;font-size:14px;">   </td>
                    	   <td style="text-align:center;font-size:14px;">   </td> 
                    	   <td style="text-align:center;font-size:14px;">  <b>Total</b>  </td> 
                    	   <td style="text-align:center;font-size:14px;">   {{number_format($total_perte, 0, ' ', ' ') }} </b></td>
                    	   <td style="text-align:center;font-size:14px;">  <b>{{number_format($total_vente, 0, ' ', ' ') }} </b></td>
                           <td style="text-align:center;font-size:14px;">  <b>{{number_format($total_achat, 0, ' ', ' ') }} </b></td>
                    	   <td style="text-align:center;font-size:14px;">   <b>{{number_format($total_benef, 0, ' ', ' ') }} </b></td>
                    	</tr>
                        </tfoot>
                      </table>
                    </div>
                  </div>
                </div>
                    
                    
                  </div>
                </div>

			
           
          </div>
		  
		 
            </div>
          </div>
		  
	   @push('scripts') 
	  
	   
@endpush

@endsection