<?php

//ini_set('display_errors',1);

session_start();
unset($_SESSION['can_numdoc']);

require_once ("../../config/Cado.php");
require_once ("../consulta_cst/cConsulta.php");
$oConsulta = new cConsulta();
require_once ("../producto/cProducto.php");
$oProducto = new cProducto();

require_once("../tipocambio/cTipocambio.php");
$oTipocambio = new ctipocambio();


require_once ("../formatos/formato.php");
 
$contadorpro = 0;
$total_soles = 0;
$total_dolares = 0;

$dts = $oConsulta->mostrar_productosmasvendidos(fecha_mysql($_POST['txt_fil_con_fec1']),fecha_mysql($_POST['txt_fil_con_fec2']),$_POST['hdd_fil_cli_id'],$_POST['cmb_fil_lim'],$_POST['cmb_fil_punven']); 
$num_rows = mysqli_num_rows($dts);

$dts1 = $oConsulta->mostrar_clientemas(fecha_mysql($_POST['txt_fil_con_fec1']),fecha_mysql($_POST['txt_fil_con_fec2']),$_POST['hdd_fil_cli_id'],$_POST['cmb_fil_lim'],$_POST['cmb_fil_punven']); 
$num_rows1 = mysqli_num_rows($dts1);

$dts2 = $oConsulta->mostrar_numdoc(fecha_mysql($_POST['txt_fil_con_fec1']),fecha_mysql($_POST['txt_fil_con_fec2']),$_POST['hdd_fil_cli_id'],$_POST['cmb_fil_punven']); 
$num_rows2 = mysqli_num_rows($dts2);

$dts3 = $oConsulta->mostrardetalles_numdoc(fecha_mysql($_POST['txt_fil_con_fec1']),fecha_mysql($_POST['txt_fil_con_fec2']),$_POST['hdd_fil_cli_id'],$_POST['cmb_fil_punven']); 
$num_rows3 = mysqli_num_rows($dts3);

?>


<section class="content">
	<div class="container-fluid">
	<div class="row">
	    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
		    <div class="card">
				<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="padding:5px;">
	
		
	
			<?php // echo $_POST['txt_fil_con_fec1'].' - '.$_POST['txt_fil_con_fec2'].' - '.$_POST['hdd_fil_cli_id'];?>

		<div class="table-responsive">
		<table id="tabla_promasven" class="table table-bordered table-striped">

		<thead>
			<tr >
				<th align="left" colspan="4" > <strong class="text-red"> PRODUCTOS MAS VENDIDO </strong></th>	
			</tr>	

		<tr>
		<th>COD. BAR.</th>
		<th>DESCRIPCION</th>
		<th>MARCA</th>
		<th>CAN. VENDIDA</th>
		</tr>
		</thead>
		
		<tbody>
			 <?php 
			 if($num_rows>0){
			while($dt = mysqli_fetch_array($dts))
			 { 
				/* $dtscod = $oProducto->mostrarCod_principal($dt['CATID']);
				$rowcod = mysqli_num_rows($dtscod);
						
				$dtcod = mysqli_fetch_array($dtscod);
				$codprin = $dtcod['tb_productomarcacodigo_cod'];  */
		 
				?>
	   
				<tr>
				<td> <strong class="badge badge-warning" ><?php echo $dt['CODBAR'];?></strong></td>
				<td> <?php echo $dt['PRONOM'];?></td>
				<td> <?php echo $dt['MARNOM'];?></td>
				<td align="right" > <?php echo $dt['CANT'] * 1;?></td>
				</tr>

			 <?php }}else{
				?> 
				<tr >
				<td  colspan="4" > <?php echo 'DATOS NO ENCONTRADOS';?></td>
				</tr>
				
				<?php  
			 }?>
	        </tbody>     
     	</table>
		</div>
		
	
		<div class="table-responsive">
		<table id="tabla_canje" class="table table-bordered table-striped">
	   
			<tr >
				<th align="left" colspan="3" > <strong class="text-green">TOP <?php echo $_POST['cmb_fil_lim'];?> CLIENTE CON MAS VENTAS </strong></th>	
			</tr>			
			
			 <?php 
			 if($num_rows1>0){
			 while($dt1 = mysqli_fetch_array($dts1))
			 { ?>
	   
				<tr>
				<td> <?php echo $dt1['cliruc'];?></td>
				<td> <?php echo $dt1['clinom'];?></td>
				<td align="right" > <?php echo $dt1['sim'].' '.formato_money($dt1['tot']);?></td>
				</tr>

			 <?php }}else{
				?>
				
				<tr colspan="3" >
				<td> <?php echo 'DATOS NO ENCONTRADOS';?></td>
				</tr>
				
				<?php 			  
			 }?>
      
         
     	</table>	
		</div>
		
		<br />
		
		<div class="table-responsive">
		<table id="table_docgen" class="table table-bordered table-striped" >
		<thead>
		<tr >
				<th align="left" colspan="2" > <strong class="text-red"> DOCUMENTOS GENERADOS </strong></th>	
		</tr>
		</thead>
		<tbody>
		<tr>
		<td colspan="2" > 
		 <?php 
		if($num_rows2>0){
			while($dt2 = mysqli_fetch_array($dts2))
			{ 
	   
			echo ' <badge class="badge badge-success" >'.$dt2['tb_venta_numdoc'].'</badge> ';

			}}else{
				 echo 'DATOS NO ENCONTRADOS'; 			  
			}
			?>
		</td>
		</tr>
		</tbody>
		</table>
		</div>
		<br />
		<br />
		
		<div class="table-responsive">
		<table id="table_proven" class="table table-bordered table-striped" >
		 <thead>
		<tr >
				<th align="left" colspan="11" > <strong class="text-red"> PRODUCTOS VENDIDOS </strong></th>	
		</tr>
		
		<tr>
		<th>DOCUMENTO</th>
		<th>CODIGO</th>
		<th>PRODUCTO</th>
		<th>MARCA</th>
		<th>CAN</th>
		<th>VAL. VEN. UNI.</th>
		<th>PRE. VEN. UNI.</th>
		<th>VAL. COS. UNI.</th>
		<th>PRE. COS. UNI.</th>
		<th>UTI. BRU. UNI.</th>
		<th>UTI. BRU. TOT.</th>
		</tr>
		</thead>
		<tbody>
		 <?php 
			 if($num_rows3>0){
			 while($dt3 = mysqli_fetch_array($dts3))
			 { 
		// echo $dt3['tb_catalogo_id'].'-'.$dt3['tb_marca_id'].'-'.$dt3['tb_almacen_id'].'-'.fecha_mysql($dt3['tb_venta_fec']);

		
	$rs = $oTipocambio->consultar(fecha_mysql($dt3['tb_venta_fec']));  
	$rows = mysqli_num_rows($rs);
	$dt = mysqli_fetch_array($rs);
	$dolsun =  formato_money3($dt['tb_tipocambio_dolsun']);
			
			if($dt3['tb_moneda_id']==1){
				$valunimos = ($dt3['tb_ventadetalle_preuni']/1.18) / $dolsun ;
				$preunimos = $dt3['tb_ventadetalle_preuni'] /$dolsun ;
				
				$preunis = $dt3['tb_ventadetalle_preuni'];
				
			}else{
				$valunimos = $dt3['tb_ventadetalle_preuni']/1.18;
				$preunimos = $dt3['tb_ventadetalle_preuni'];
				
				$preunis = $dt3['tb_ventadetalle_preuni']*$dolsun;
			}
		
		
		$dts5 = $oConsulta->mostrar_kardex_por($dt3['tb_catalogo_id'],$dt3['tb_marca_id'],$dt3['tb_almacen_id'],fecha_mysql($dt3['tb_venta_fec'])); 
		$num_rows5 = mysqli_num_rows($dts5);
		$diff = 0;
		if($num_rows5>0){
			$dt5 = mysqli_fetch_array($dts5);
			$valuni_kar = formato_money($dt5['tb_kardexdetalle_cos']);
			$preuni_kar1 = formato_money($dt5['tb_kardexdetalle_cos']*1.18);
			$preuni_kar = $dt5['tb_kardexdetalle_cos']*1.18;
			$diff = formato_money($preunis - $preuni_kar);
		}else{
			$valuni_kar = '<strong class="text text-red">N/K</strong>';
			$preuni_kar1 = '<strong class="text text-red">N/K</strong>';
			$diff = '<strong class="text text-red">N/K</strong>';
		}
			
		 
		 ?>
	   
				<tr>
				
				<td nowrap > 
				<a href="../../files/venta/venta_impcpe_doc.php?venid=<?php echo $dt3['tb_venta_id']; ?>" target="_blank" title="DESCARGAR E IMPRIMIR PDF"><strong class="badge badge-danger"><?php echo $dt3['tb_venta_numdoc'];?> </strong></a>		 
				
				</td>
				<td nowrap > <?php echo $dt3['tb_catalogo_cod'];?></td>
				<td nowrap > <?php echo $dt3['tb_ventadetalle_nom'];?></td>
				<td nowrap > <?php echo $dt3['tb_marca_nom'];?></td>
				<td nowrap align="right" > <?php echo $dt3['tb_ventadetalle_can'] * 1;?></td>
			 <td nowrap align="right" title="<?php echo 'US$ '.formato_money($valunimos);?>" > <?php echo formato_money($preunis/1.18);?></td>
				<td nowrap align="right" title="<?php echo 'US$ '.formato_money($preunimos);?>"> <?php echo formato_money($preunis);?></td>
				<td nowrap align="right" > <?php echo $valuni_kar;?></td>
				<td nowrap align="right" > <?php echo $preuni_kar1;?></td>
				<td nowrap align="right" > <?php echo $diff;?></td>
				<td nowrap align="right" > <?php echo $diff * $dt3['tb_ventadetalle_can'];?></td>
				</tr>

			 <?php 
			 $total_val_ven_uni += $preunis/1.18;
			 $total_pre_ven_uni += $preunis;
			 $total_val_cos_uni += $valuni_kar;
			 $total_pre_cos_uni += $preuni_kar1;
			 $total_uti_bru_uni += $diff;
			 $total_uti_bru_tot += $diff * $dt3['tb_ventadetalle_can'];
			 
			 }
			 
			 
			 }else{
				?>
				
				<tr  >
				
				<td colspan="11" > <?php echo 'DATOS NO ENCONTRADOS';?></td>
				</tr>
				
				<?php 			  
			 }?>
      
		</tbody>
		<tr class="even ">
  <td colspan="5">TOTAL SOLES</td>
  <td NOWRAP class="" align="right"><strong><?php echo '<span class="text-primary">S/</span>'.' '.formato_money($total_val_ven_uni)?></strong></td>
  <td NOWRAP class="" align="right"><strong><?php echo '<span class="text-primary">S/</span>'.' '.formato_money($total_pre_ven_uni)?></strong></td>
  <td NOWRAP class="" align="right"><strong><?php echo '<span class="text-primary">S/</span>'.' '.formato_money($total_val_cos_uni)?></strong></td>
  <td NOWRAP class="" align="right"><strong><?php echo '<span class="text-primary">S/</span>'.' '.formato_money($total_pre_cos_uni)?></strong></td>
  <td NOWRAP class="" align="right"><strong><?php echo '<span class="text-primary">S/</span>'.' '.formato_money($total_uti_bru_uni)?></strong></td>
  <td NOWRAP class="" align="right"><strong><?php echo '<span class="text-primary">S/</span>'.' '.formato_money($total_uti_bru_tot)?></strong></td>
</tr>

		</table>
		</div>
     
    </div>
  </div>
</div>
</div>
</div>
</section>

  <script type="text/javascript">
	 $(function() { 
       
	 $('#table_proven').DataTable({
 
        "language": {
                "lengthMenu": "Mostrar _MENU_ registros",
                "zeroRecords": "No se encontraron resultados",
                "info": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
                "infoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
                "infoFiltered": "(filtrado de un total de _MAX_ registros)",
                "sSearch": "Buscar:",
                "oPaginate": {
                    "sFirst": "Primero",
                    "sLast":"Último",
                    "sNext":"Siguiente",
                    "sPrevious": "Anterior"
			     },
			     "sProcessing":"Procesando...",
            }
    });  
	
	
		 $('#tabla_promasven').DataTable({
 
        "language": {
                "lengthMenu": "Mostrar _MENU_ registros",
                "zeroRecords": "No se encontraron resultados",
                "info": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
                "infoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
                "infoFiltered": "(filtrado de un total de _MAX_ registros)",
                "sSearch": "Buscar:",
                "oPaginate": {
                    "sFirst": "Primero",
                    "sLast":"Último",
                    "sNext":"Siguiente",
                    "sPrevious": "Anterior"
			     },
			     "sProcessing":"Procesando...",
            }
    });  
	

	  
	  
    });

	  </script>