<?php
session_start();
require_once ("../../config/Cado.php");
require_once ("cProductobonificacion_cst.php");
$oProductobonificacion_cst = new cProductobonificacion_cst();

require_once ("../formatos/formato.php");

$fecha1 = $_POST['txt_fil_com_fec1']; 
$fecha2 = $_POST['txt_fil_com_fec2'];

$dts1=$oProductobonificacion_cst->mostrar_filtrocondetalles($_POST['cmb_fil_usuario_id'],$_POST['cmb_fil_puntoventa_id'],
fecha_mysql($fecha1),fecha_mysql($fecha2),$_POST['cmb_fil_mon_id']);
$num_rows= mysqli_num_rows($dts1);


if($_POST['cmb_fil_mon_id']==1){
	$monsim = 'S/ ';
}else if($_POST['cmb_fil_mon_id']==2){
	$monsim = 'US$ ';
}



?>

<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;">	
						<button class="btn btn-success " id="btn_excel"> <i class="fas fa-file-excel"></i> DESCARGAR EXCEL </button>
					
		<div class="table-responsive">	
          <table id="tabla_ven" class="table">
            <thead>
			
              <tr>
                <th class="" >FECHA</th>
				<th>NUM. DOC.</th>
                <th class="" >USUARIO</th>
                <th class="" >PRODUCTO</th>
                <th class="" >LABORATORIO</th>          
                <th class="" >PRESENTACION | EMPAQUE</th>          
                <th NOWRAP class="" align="right">CAN</th>
                <th NOWRAP class="" align="right">PRE. VEN.</th>
                <th NOWRAP class="bg-success" align="right">PRE. BON.</th>
                <th NOWRAP class="" align="right">PRE. VEN. TOT.</th>
                <th NOWRAP class="bg-success" align="right">PRE. BON. TOT.</th>
              </tr>
            </thead>
            <tbody>
      <?php
       if($num_rows>0){
      while($dt1 = mysqli_fetch_array($dts1))
      {
        
        $canbon = $dt1['tb_ventadetalle_can'] * 1;
        	
		$can = $dt1['tb_ventadetalle_can'] * 1;
		$emp_fac =  $dt1['tb_empaque_fac'];
		
		if($can>=$emp_fac){
				$can = $can/$emp_fac;
			
		}
			
		$prebon = formato_money4($dt1['tb_ventadetalle_bonuni']*1); 
		$preven = formato_money4($dt1['tb_ventadetalle_preuni']*1); 
		$preventot = formato_money4($dt1['tb_ventadetalle_preuni']) * $can; 
		$prebontot = formato_money4($dt1['tb_ventadetalle_bonuni']) * $canbon; 
      	
		$reg = mostrarFechaHora($dt1['tb_venta_reg']);
		
	  
      ?>
      <tr title="FECHA DE REGISTRO : <?php echo "$reg";  ?>" >
        <td class="" nowrap="nowrap"><?php echo mostrarFecha($dt1['tb_venta_fec'])?></td>
        <td NOWRAP ><?php echo $dt1['tb_venta_numdoc']?></td>
        <td NOWRAP class="" ><?php echo $dt1['tb_usuario_nom']; ?></td>
        <td NOWRAP class="" ><strong><?php echo $dt1['tb_producto_nom']?></strong></td>
        <td NOWRAP class="" ><?php echo $dt1['tb_marca_nom']?></td>
        <td NOWRAP class="" ><?php echo $dt1['tb_presentacion_nom'].' | '.$dt1['tb_empaque_nom']; ?></td> 
        <td NOWRAP align="right"><?php echo $can ?></td>
        <td NOWRAP class="" align="right"><?php echo ' '.formato_money($preven);?></td>
        <td NOWRAP class="bg-success" align="right"><?php echo ' '.formato_money($prebon);?></td>
        <td NOWRAP class="" align="right"><?php echo ' '.formato_money($preventot);?></td>
        <td NOWRAP class="bg-success" align="right"><?php echo ' '.formato_money($prebontot);?></td>
     </tr>
      <?php	  
		$totcan += $can;
		$sumpreven += $preven;
		$sumprebon += $prebon;
		$sumpreventot += $preventot;
		$sumprebontot += $prebontot;
	
	}
    mysqli_free_result($dts1);
    }
    ?>
  </tbody>
  <tr class="even ">
  <td colspan="6">TOTAL</td>
  <td NOWRAP class="" align="right"><strong><?php echo ' '.$totcan * 1?></strong></td>
  <td NOWRAP class="" align="right"><strong><?php echo ' '.formato_money($sumpreven)?></strong></td>
  <td NOWRAP class="bg-success" align="right"><strong><?php echo ' '.formato_money($sumprebon)?></strong></td>
  <td NOWRAP class="" align="right"><strong><?php echo ' '.formato_money($sumpreventot)?></strong></td>
  <td NOWRAP class="bg-success" align="right"><strong><?php echo ' '.formato_money($sumprebontot)?></strong></td>
</tr>
            </table>
         </div>

		
	<br/>
	<br/>
	
		<div class="table-responsive">
			<div class="col-lg-5 col-md-5 col-sm-12 col-xs-12" >	
          <table id="tabla_compra" class="table">
            
			<tr>
			<th class="bg bg-yellow" colspan="2" >RESUMENES</th>
			</tr>
            <?php 
			$totcaj = $tot - $totd;
			?>
			<tr>
                <th class="" >TOTAL BONITIFCACION VENTAS</th>
				<td><?php echo ''.formato_money($sumprebontot) ?></td>
            </tr>
			
            </table>
			
			</div>
		</div>
				
		
        </div>
        </div>
      </div>
    </div>
    </div>
  </section>

  
   <script type="text/javascript">
   
      
   function abrirEnPestana(url) {
		var a = document.createElement("a");
		a.target = "_blank";
		a.href = url;
		a.click();
}


  $(function() {  
      
      	$("#btn_excel").click(function(){
		usu_id = "<?php echo $_POST['cmb_fil_usuario_id'] ?>";
		punven_id = "<?php echo $_POST['cmb_fil_puntoventa_id'] ?>";
		mon_id = "<?php echo $_POST['cmb_fil_mon_id'] ?>";
		fec1 = "<?php echo fecha_mysql($fecha1) ?>";
		fec2 = "<?php echo fecha_mysql($fecha2) ?>";
	abrirEnPestana('../../files/productobonificacion_cst/productobonificacion_cst_excel.php?fec1='+fec1+'&fec2='+fec2+'&usu_id='+usu_id+'&punven_id='+punven_id+'&mon_id='+mon_id);
	});


    
	 $('#tabla_dev,#tabla_ven').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>  