<html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Stat Inventory</title>
		<style>
	.bloc1{
		display:inline-block;
		position:absolute;	
		margin-top:20px;
		border-right: 1px solid #004365;
		height:60px;
	}
	.bloc2{
		display:inline-block;
		position:relative;
		margin-left:330px;
		margin-top:2px;
		width:350px;
		font-family:Tahoma;
		
		padding-top: -10px;
	}
	.bloc{
		display:inline-block;
		position:absolute;
		width:700px;
		border:1px solid #004365;
		border-radius:10px;
		padding-left: 25px;
		height:100px;
		
	}
	table {
			border-collapse: collapse;
			border-radius: 1em;
			overflow: hidden;
			width:745px;
			border : 1px solid #004365;
			font-size:16px;
			margin-left:-29px;
		}
		
		th, td {
			padding : 0.1em;
			background : #fff;
			border-bottom: 1px solid #004365;
		}
	
    </style>
    </head>
      <body style="font-family:'Montserrat';font-size:13px;" onload="window.print();">
	  <h3 style="text-align:left;margin-top:-15px;"><u>SITUATION DU STOCK AU {{ $dateinv }}</u></h3>
			<div class="container">
		<div class="row"> 
				<div class="col-md-4" style="margin-top:20px; border: 1px solid #004365;border-radius:10px;width:500px;margin-left:50px">
				<p style="border-collapse: collapse;margin-left:20px;width:300px;line-height:2;margin-top:-2px;" cellspacing="3" >	
					
				<h4>Résultat des inventaires de la catégorie : {{$nom_cat}} | Date : <b>{{ $dateinv }}</b></h4>
				<br/>
			    </p>
			    </div>
				
		</div>
		</div>
		@php
                            $total_achat = 0;
                            $total_vente = 0;
                            $total_perte = 0;
                            $total_benef = 0;
                        @endphp
		<br/>
			 <table style="width:1100px">
                        <thead>
                          <tr>
							<th style="text-align:center;">N째</th>
							<th style="text-align:center;">Designation</th>
							<th style="text-align:center;">Transfert 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 (compté)</th>
							<th style="text-align:center;">Difference</th>
							<th style="text-align:center;">Valeur à la perte</th>
							<th style="text-align:center;">Valeur à la vente</th>
							<th style="text-align:center;">Valeur à l'achat</th>
							<th style="text-align:center;">Bénef à 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;">{{ $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;"> {{$apres}}</td>
                            
                    		<td style="text-align:center;font-size:18px;">
                    		    
                    			@if($apres > $enstock)
                                <span class="badge badge-warning badge-shadow" style="background-color:#ffc107; color:#000">
                    			    {{$diff}}
                    			</span>
                    			@endif
                    			@if($enstock > $apres)
                    			<span class="badge badge-danger badge-shadow" style="background-color:#fc544b; color:#fff">
                    			    {{$diff}}
                    			</span>
                    			@endif
                    			@if($apres == $enstock)
                                <span class="badge badge-success badge-shadow" style="background-color:#54ca68; color:#fff">
                    			    {{$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
        				<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>
						</tbody>
					</table>
						
				  </body>
				 </html> 