<html>
<head>
    <!--
        ===
        This comment should NOT be removed.

        Charisma v2.0.0

        Copyright 2012-2014 Muhammad Usman
        Licensed under the Apache License v2.0
        http://www.apache.org/licenses/LICENSE-2.0

        http://usman.it
        http://twitter.com/halalit_usman
        ===
    -->
    <meta charset="utf-8">
    <title>RECETTE DE LA CATEGORIE : @if ($categorie_id==0) TOUS @else {{ DB::table('categorie')->where('idcat', $categorie_id)->first()->libelle }} @endif
    DU {{$date_debut}} AU {{$date_fin}}</title>
</head>

<body style="font-family:'Montserrat';font-size:12px;">
    <!-- content ends -->
        <h2 style="text-align:center"><i class="glyphicon glyphicon-user"></i>
       RECETTE DE LA CATEGORIE : @if ($categorie_id==0) TOUS @else {{ DB::table('categorie')->where('idcat', $categorie_id)->first()->libelle }} @endif
                        DU {{implode('/', array_reverse(explode('-', $date_debut)))}} AU {{implode('/', array_reverse(explode('-', $date_fin)))}}</h2>
    <table border="1" class="" style="border-collapse: collapse;font-family:'Montserrat';font-size:12px;width:1020px;">
      <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="4">Vendu</th>
		<th style="text-align:center;">Bénéfice</th>
    </tr>
	<tr>
		
		<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')->whereBetween('date', [$date_debut,$date_fin])->get()->sum('montligne');
		$totalpv11 = DB::table('ligneretourcommande')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->get()->sum('montligne');
		$countv00 = DB::table('ligne_vente')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->sum('qte');
		$countv11 = DB::table('ligneretourcommande')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->sum('qte');
		
		
		//VENTE CLIENT
		$totalpv0 = DB::table('ligne_vente_client')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->get()->sum('montligne');
		$totalpv1 = DB::table('ligneretourcommandeclient')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->sum('montligne');
		$countv0 = DB::table('ligne_vente_client')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->get()->sum('qte');
		$countv1 = DB::table('ligneretourcommandeclient')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->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')->whereBetween('date', [$date_debut,$date_fin])->sum('montligne');
		
		$count = DB::table('ligne_appro_vendable')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->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)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->sum('montligne');
		//PRIX DE REVIENT MOYEN SUR VENTE
		$prm1 = DB::table('ligne_vente')->where('produit_id', $produit->id)->where('etat', 'Validée')->whereBetween('date', [$date_debut,$date_fin])->get()->sum('montlignepr');
		$prm2 = DB::table('ligneretourcommande')->where('produit_id', $produit->id)->where('etat', 'Validée')->whereBetween('date', [$date_debut,$date_fin])->get()->sum('montlignepr');
		$qtm1 = DB::table('ligne_vente')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->sum('qte');
		$qtm2 = DB::table('ligneretourcommande')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->sum('qte');
		
		$prm11 = DB::table('ligne_vente_client')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->get()->sum('montlignepr');
		$prm22 = DB::table('ligneretourcommandeclient')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->sum('montlignepr');
		$qtm11 = DB::table('ligne_vente_client')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->get()->sum('qte');
		$qtm22 = DB::table('ligneretourcommandeclient')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->sum('qte');
		
		$qtm = ($qtm1 + $qtm11) - ($qtm2 + $qtm22);
		$totalventesurpru = ($prm1 + $prm11) - ($prm2 + $prm22) ;
		
		
		//BENEFICES
		
		$benefice1 = DB::table('ligne_vente')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->where('etat','Validée')->sum('benef');
		$benefice2 = DB::table('ligne_vente_client')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->where('etat','Validée')->sum('benef');
		$benefice11 = DB::table('ligneretourcommande')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->where('etat','Validée')->sum('benef');
		$benefice22 = DB::table('ligneretourcommandeclient')->where('produit_id', $produit->id)->whereBetween('date', [$date_debut,$date_fin])->where('etat','Validée')->sum('benef');
		
		
		$benefice = ($benefice1 + $benefice2) - ($benefice11 + $benefice22);
		
		//$tventepvm = $countv * $pvv;
		$tventepvm = $totalpv;
		//$tventepru = $countv * $paa;
		$tventepru = $totalpa;
		
		$veri = DB::table('stock_magasin')->where('produit_id', $produit->id)->get()->count();
		if ($veri>0){
			$rest = DB::table('stock_magasin')->where('produit_id', $produit->id)->orderBydesc('id')->take(1)->first()->apres;
		}else{
			$rest = 0;
		}
		
		$v_avenir = $rest * $pvv;
		$ru_avenir = $rest * $paa;
		$benef_avenir = $v_avenir - $ru_avenir;
	@endphp
	<tr>
        <td style="text-align:left;font-size:13px;">{{$produit->barcode}}</td>
        <td style="text-align:left;font-size:13px;">{{$produit->designation}} ({{$rest}})</td>
        <td style="text-align:center;font-size:13px;">
		{{$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é -->
		{{number_format($benefice, 0, ' ', ' ')}}
        </td>
    </tr>
	
	@endforeach
    </tbody>
   <tfoot>
	@php
	//CA TOTAL
	if ($categorie_id==0){
		$ca1 = DB::table('ligne_vente')->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->get()->sum('montligne');
		$ca2 = DB::table('ligne_vente_client')->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->get()->sum('montligne');
		$ca0 = DB::table('ligneretourcommande')->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->get()->sum('montligne');
		$ca00 = DB::table('ligneretourcommandeclient')->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->sum('montligne');
		$rm1 = DB::table('vente')->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->sum('remise');
		$rm2 = DB::table('vente_client')->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->sum('remise');
		$totventepvm1 = ($ca1 + $ca2) - ($ca0 + $ca00 + $rm1 + $rm2);
		//APPRO TOTAL
		$totalachat = DB::table('ligne_appro_vendable')->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->sum('montligne');
		
		//TOTAL BENEFICE
	    $benefice001 = DB::table('ligne_vente')->whereBetween('date', [$date_debut,$date_fin])->where('etat','Validée')->sum('benef');
	    $benefice002 = DB::table('ligne_vente_client')->whereBetween('date', [$date_debut,$date_fin])->where('etat','Validée')->sum('benef');
	    $benefice011 = DB::table('ligneretourcommande')->whereBetween('date', [$date_debut,$date_fin])->where('etat','Validée')->sum('benef');
	    $benefice022 = DB::table('ligneretourcommandeclient')->whereBetween('date', [$date_debut,$date_fin])->where('etat','Validée')->sum('benef');
		
		$benefice01 = ($benefice001 + $benefice002) - ($benefice011 + $benefice022);
		
	} else {
		$ca1 = DB::table('ligne_vente')->where('categorie_id',$categorie_id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->get()->sum('montligne');
	$ca2 = DB::table('ligne_vente_client')->where('categorie_id',$categorie_id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->get()->sum('montligne');
    $ca0 = DB::table('ligneretourcommande')->where('categorie_id',$categorie_id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->get()->sum('montligne');
	$ca00 = DB::table('ligneretourcommandeclient')->where('categorie_id',$categorie_id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->sum('montligne');
	$rm1 = DB::table('vente')->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->sum('remise');
	$rm2 = DB::table('vente_client')->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->sum('remise');
	
	$totventepvm1 = ($ca1 + $ca2) - ($ca0 + $ca00 + $rm1 + $rm2);
	
	//APPRO TOTAL
	$totalachat = DB::table('ligne_appro_vendable')->where('categorie_id',$categorie_id)->whereBetween('date', [$date_debut,$date_fin])->where('etat', 'Validée')->sum('montligne');
	//TOTAL BENEFICE
	    $benefice001 = DB::table('ligne_vente')->where('categorie_id',$categorie_id)->whereBetween('date', [$date_debut,$date_fin])->where('etat','Validée')->sum('benef');
	    $benefice002 = DB::table('ligne_vente_client')->where('categorie_id',$categorie_id)->whereBetween('date', [$date_debut,$date_fin])->where('etat','Validée')->sum('benef');
	    $benefice011 = DB::table('ligneretourcommande')->where('categorie_id',$categorie_id)->whereBetween('date', [$date_debut,$date_fin])->where('etat','Validée')->sum('benef');
	    $benefice022 = DB::table('ligneretourcommandeclient')->where('categorie_id',$categorie_id)->whereBetween('date', [$date_debut,$date_fin])->where('etat','Validée')->sum('benef');
		
		$benefice01 = ($benefice001 + $benefice002) - ($benefice011 + $benefice022);
	}
	
    //MARGE
    $depense = DB::table('caisse_depense')->where('typeoperation', 'Débit')->whereBetween('date', [$date_debut,$date_fin])->where('etat','Validée')->get()->sum('montant');
	$marge = $totventepvm1 - ($depense);
	
	//TOTAL REMISE
	$trm = $rm1 + $rm2;
	@endphp
    
    <tr>
		<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 colspan="2"><b>BENEFICE</b></td>
		<td colspan="2" style="text-align:right">{{number_format($benefice01, 0, ' ', ' ')}} </td>
    </tr>
    <tr>
		<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 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 colspan="2"><b>MARGE</b></td>
		<td colspan="2" style="text-align:right"><b>{{number_format($marge, 0, ' ', ' ')}} </b></td>
    </tr>
    </tfoot>
	
    </table>
	</body>
</html>