@extends('layout')
@section('title','Stock ')
@section('description','Stock')
@section('content')
    <!-- content ends -->
	<div class="row">
	
    <div class="box col-md-12">
	@if (session('status'))
				<div class="alert alert-success" role="alert">
					<i class="ti-check"></i> {{ session('status') }}
				</div>
			@endif

			@if (session('error'))
				<div class="alert alert-danger" role="alert">
					<i class="ti-na"></i> {{ session('error') }}
				</div>
			@endif
	@if(session('user')->roles == "Admin")
		<div class="card">
                  <div class="card-header">
                    <h4>Par période</h4>
                  </div>
				  
                  <div class="card-body">
				  
				  <h7><strong>Veuillez d'abord sélectionner la date début et la date fin</strong></h7>
				  
				<form  action="{{route('recherche-situation')}}" method="POST">
                         {{csrf_field()}}
				
				  <div class="row" style="padding-top: 30px">
					 <div class="col-lg-3 col-md-12"> 
					 <div class="form-group">
                      <label>Date début</label>
                      <input type="date" class="form-control" name="date_debut">
                    </div>
					
					</div>
					
					<div class="col-lg-3 col-md-12"> 
					 <div class="form-group">
                      <label>Date fin</label>
                      <input type="date" class="form-control" name="date_fin">
                    </div>
					
					</div>
					
					 <div class="col-lg-4 col-md-12" style="margin-top: 30px"> 
					
					 <button class="btn btn-icon  btn-primary " ><i  class="fa fa-search"></i> Rechercher</button>
					</div>
					
					
					</div>
		
                  
            
			<br><br>
					  
				</form>
					  
					    </div>
						 
                    </div>
					@endif
		<br/>
	<div class="card">
                  <div class="card-header">
                    <h4>Situation stock</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;margin-top:-20px;" rowspan="2">Code</th>
        <th style="text-align:center;" rowspan="2">Article</th>
        <th style="text-align:center;" colspan="3">Achat</th>
		<th style="text-align:center;" colspan="4">Vendu</th>
		<th style="text-align:center;">Bénéfice</th>
    </tr>
	<tr>
        <th style="text-align:center;">Quantité</th>
        <th style="text-align:center;">P.R.U</th>
        <th style="text-align:center;">Valeur</th>
		
		<th style="text-align:center;">Qte</th>
		<th style="text-align:center;">P.V.M</th>
        <th style="text-align:center;">Valeur sur PVM</th>
        <th style="text-align:center;">Valeur sur PRU</th>
		
		<th style="text-align:center;">Valeur </th>
		
    </tr>
    </thead>
    <tbody>
	@foreach($produits as $produit)
	@php
		//ACHAT
		$totalpv00 = DB::table('ligne_vente')->where('produit_id', $produit->id)->where('etat', 'Validée')->get()->sum('montligne');
		$totalpv11 = DB::table('ligneretourcommande')->where('produit_id', $produit->id)->where('etat', 'Validée')->get()->sum('montligne');
		$countv00 = DB::table('ligne_vente')->where('produit_id', $produit->id)->where('etat', 'Validée')->sum('qte');
		$countv11 = DB::table('ligneretourcommande')->where('produit_id', $produit->id)->where('etat', 'Validée')->sum('qte');
		
		
		//VENTE CLIENT
		$totalpv0 = DB::table('ligne_vente_client')->where('produit_id', $produit->id)->where('etat', 'Validée')->get()->sum('montligne');
		$totalpv1 = DB::table('ligneretourcommandeclient')->where('produit_id', $produit->id)->where('etat', 'Validée')->sum('montligne');
		$countv0 = DB::table('ligne_vente_client')->where('produit_id', $produit->id)->where('etat', 'Validée')->get()->sum('qte');
		$countv1 = DB::table('ligneretourcommandeclient')->where('produit_id', $produit->id)->where('etat', 'Validée')->sum('qte');
		
		$countv = ($countv0 + $countv00) - ($countv1 + $countv11);
		$totalpv = ($totalpv0 + $totalpv00) - ($totalpv1 + $totalpv11) ;
		
		//
		
		//$totalpa = DB::table('ligne_appro_vendable')->where('produit_id', $produit->id)->where('etat', 'Validée')->get()->sum('montligne');
		$spr1 = DB::table('ligne_vente')->where('produit_id', $produit->id)->where('etat', 'Validée')->get()->sum('pr');
		$spr2 = DB::table('ligneretourcommande')->where('produit_id', $produit->id)->where('etat', 'Validée')->get()->sum('pr');
		$totalpa = (($spr1 * $countv00) + ($spr2 * $countv0))/$countv;
		
		$count = DB::table('ligne_appro_vendable')->where('produit_id', $produit->id)->where('etat', 'Validée')->get()->sum('qte');
		
		
		if ($countv>0){
		$pvv = $totalpv/$countv;
		} else {
			$pvv = 0;
		}
		if ($countv>0){
		$paa = $totalpa/$countv;
		} else {
			$paa = 0;
		}
		//$tachat = $count * $paa;
		$tachat = DB::table('ligne_appro_vendable')->where('produit_id', $produit->id)->where('etat', 'Validée')->sum('montligne');
		
	
		
		$benefice1 = DB::table('ligne_vente')->where('produit_id', $produit->id)->where('etat','Validée')->sum('benef');
		$benefice2 = DB::table('ligne_vente_client')->where('produit_id', $produit->id)->where('etat','Validée')->sum('benef');
		$benefice11 = DB::table('ligneretourcommande')->where('produit_id', $produit->id)->where('etat','Validée')->sum('benef');
		$benefice22 = DB::table('ligneretourcommandeclient')->where('produit_id', $produit->id)->where('etat','Validée')->sum('benef');
		
		//PRIX DE REVIENT MOYEN SUR VENTE
		$prm1 = DB::table('ligne_vente')->where('produit_id', $produit->id)->where('etat', 'Validée')->get()->sum('montlignepr');
		$prm2 = DB::table('ligneretourcommande')->where('produit_id', $produit->id)->where('etat', 'Validée')->get()->sum('montlignepr');
		$qtm1 = DB::table('ligne_vente')->where('produit_id', $produit->id)->where('etat', 'Validée')->sum('qte');
		$qtm2 = DB::table('ligneretourcommande')->where('produit_id', $produit->id)->where('etat', 'Validée')->sum('qte');
		
		$prm11 = DB::table('ligne_vente_client')->where('produit_id', $produit->id)->where('etat', 'Validée')->get()->sum('montlignepr');
		$prm22 = DB::table('ligneretourcommandeclient')->where('produit_id', $produit->id)->where('etat', 'Validée')->sum('montlignepr');
		$qtm11 = DB::table('ligne_vente_client')->where('produit_id', $produit->id)->where('etat', 'Validée')->get()->sum('qte');
		$qtm22 = DB::table('ligneretourcommandeclient')->where('produit_id', $produit->id)->where('etat', 'Validée')->sum('qte');
		
		$qtm = ($qtm1 + $qtm11) - ($qtm2 + $qtm22);
		$totalventesurpru = ($prm1 + $prm11) - ($prm2 + $prm22) ;
		
		
		//$tventepvm = $countv * $pvv;
		$tventepvm = $totalpv;
		//$tventepru = $countv * $paa;
		$tventepru = $totalpa;
		
		$benef = ($benefice1 + $benefice2) - ($benefice11 + $benefice22);
		$rest = DB::table('stock_magasin')->where('produit_id', $produit->id)->orderBydesc('id')->take(1)->first()->apres;
		
		$v_avenir = $rest * $pvv;
		$ru_avenir = $rest * $paa;
		$benef_avenir = $v_avenir - $ru_avenir;
	@endphp
	<tr>
        <td style="text-align:center;font-size:13px;">{{$produit->barcode}}</td>
        <td style="text-align:center;font-size:13px;">{{$produit->designation}} ({{DB::table('stock_magasin')->where('produit_id', $produit->id)->orderBydesc('id')->take(1)->first()->apres}})</td>
        <td style="text-align:center;font-size:13px;">
		{{$count}}
		</td>
		<td style="text-align:center;font-size:13px;">
		    <!--Theorique PRU -->
    	  {{number_format($paa, 0, ' ', ' ')}}

        </td>
		<td style="text-align:center;font-size:13px;">
		<!--Theorique Valeur -->
	 
		 {{number_format($tachat, 0, ' ', ' ')}}
		
        </td>
        <td class="center" style="text-align:center;font-size:13px;">
			<!--Theorique Valeur -->
			{{$countv}}
        </td>
		<td class="center" style="text-align:center;font-size:13px;">
			<!--Theorique Valeur -->
			{{$pvv}}
        </td>
		<td style="text-align:center;font-size:13px;">
		    <!--Valeur Quantité -->
		{{number_format($tventepvm, 0, ' ', ' ')}}
        </td>
		<td style="text-align:center;font-size:13px;">
		    <!--Valeur Quantité -->
		{{number_format($totalventesurpru, 0, ' ', ' ')}}
        </td>
        <td class="center" style="text-align:center;">
			<!--Difference Quantité -->
		@php
		$bn =  number_format($benef, 0, ' ', ' ');
		echo $bn."&nbsp;F";
		@endphp
        </td>
		
    </tr>
	
	@endforeach
    </tbody>
    <tfoot>
	@php
	//CA TOTAL
	$ca1 = DB::table('ligne_vente')->where('etat', 'Validée')->get()->sum('montligne');
	$ca2 = DB::table('ligne_vente_client')->where('etat', 'Validée')->get()->sum('montligne');
    $ca0 = DB::table('ligneretourcommande')->where('etat', 'Validée')->get()->sum('montligne');
	$ca00 = DB::table('ligneretourcommandeclient')->where('etat', 'Validée')->sum('montligne');
	$rm1 = DB::table('vente')->where('etat', 'Validée')->sum('remise');
	$rm2 = DB::table('vente_client')->where('etat', 'Validée')->sum('remise');
	
	$totventepvm1 = ($ca1 + $ca2) - ($ca0 + $ca00 + $rm1 + $rm2);
	
	//APPRO TOTAL
	$totalachat = DB::table('appro_vendable')->where('etat', 'Validée')->sum('montant');
	
	//TOTAL BENEFICE
	$benefice001 = DB::table('ligne_vente')->where('etat','Validée')->sum('benef');
	$benefice002 = DB::table('ligne_vente_client')->where('etat','Validée')->sum('benef');
	$benefice011 = DB::table('ligneretourcommande')->where('etat','Validée')->sum('benef');
	$benefice022 = DB::table('ligneretourcommandeclient')->where('etat','Validée')->sum('benef');
		
	$tbenef = ($benefice001 + $benefice002) - ($benefice011 + $benefice022);
    //MARGE
    $depense = DB::table('caisse_depense')->where('typeoperation', 'Débit')->where('etat','Validée')->get()->sum('montant');
	$marge = $tbenef - ($depense+ $rm1 + $rm2);
	
	//TOTAL REMISE
	$trm = $rm1 + $rm2;
	
	$v_avenir1[] =  $v_avenir;
	$v_avenir2 = array_sum($v_avenir1);
	
	$benef_avenir1[] =  $benef_avenir;
	$benef_avenir2 = array_sum($benef_avenir1);
	
	@endphp
    <tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td colspan="2"><b>TOTAL ACHAT</b></td>
		<td colspan="2" style="text-align:right">{{number_format($totalachat, 0, ' ', ' ')}} </td>
    </tr>
    <tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td colspan="2"><b>TOTAL VENTES</b></td>
		<td colspan="2" style="text-align:right">{{number_format($totventepvm1, 0, ' ', ' ')}} </td>
    </tr>
    <tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td colspan="2"><b>TOTAL BENEFICE SUR VENTES</b></td>
		<td colspan="2" style="text-align:right">{{number_format($tbenef, 0, ' ', ' ')}} </td>
    </tr>
    <tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td colspan="2"><b>TOTAL REMISE</b></td>
		<td colspan="2" style="text-align:right">{{number_format($trm, 0, ' ', ' ')}} </td>
    </tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td colspan="2"><b>TOTAL DÉPENSE</b></td>
		<td colspan="2" style="text-align:right">{{number_format($depense, 0, ' ', ' ')}} </td>
    </tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td colspan="2"><b>MARGE</b></td>
		<td colspan="2" style="text-align:right"><b>{{number_format($marge, 0, ' ', ' ')}} </b></td>
    </tr>
    <tr>
		<td>--</td>
			<td>--</td>
				<td>--</td>
					<td>--</td>
						
		<td>--</td>
		<td>--</td>
		<td><b>A VENIR </b></td>
		<td>--</td>
		<td>--</td>
		<td>--</td>
    </tr>
    <tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td colspan="2"><b>VENTES A VENIR </b></td>
		<td colspan="2" style="text-align:right"><b>{{number_format($v_avenir2, 0, ' ', ' ')}} </b></td>
    </tr>
    <tr hidden>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td colspan="2"><b>BENEF A VENIR </b></td>
		<td colspan="2" style="text-align:right"><b>{{number_format($benef_avenir2, 0, ' ', ' ')}} </b></td>
    </tr>
    </tfoot>
	
    </table>
	<!--<p align="center">
		<a class="btn btn-danger" target="_blank" href="{{route('stockPDF')}}">
			<i class="glyphicon glyphicon-print icon-white"></i> Exporter en PDF 
		</a>
	</p>-->
    </div>
    </div>
    </div>
</div>
</div><!--/#content.col-md-0-->
	@push('scripts')
	@endpush
@endsection