<?php
session_start();
require_once ("../../config/Cado.php");
require_once ("cVentageneral_cst.php");
$oVentageneral_cst = new cVentageneral_cst();
require_once ("../preciogestion/cPreciogestion.php");
$oPreciogestion = new cPreciogestion();
require_once ("../formatos/formato.php");
require_once ("../venta/cVentapago.php");
$oVentapago = new cVentapago();
$fecha1 = $_POST['txt_fil_com_fec1']; 
$fecha2 = $_POST['txt_fil_com_fec2'];

$dts1=$oVentageneral_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;">	
					
					<span style="cursor:pointer" id="btn_excel" class="btn btn-success"> <i class="fas fa-file-excel"></i> DESCARGAR EXCEL </span>
		
		
		<div class="table-responsive">	
          <table id="tabla_ven" class="table">
            <thead>
			<tr>
			<th class="bg bg-primary" colspan="10" >REGISTRO DE VENTAS</th>
			</tr>
			
              <tr>
				<th class="d-none" >EMPRESA</th>
                <th class="d-none" >USUARIO REG.</th>
                <th class="" >USUARIO VEN.</th>
                <th class="" >FECHA</th>
                <th class="" >DETALLE</th>
				<th style="white-space: nowrap;">ARTÍCULO</th>
				<th class="" style="white-space: nowrap;" >CATEGORÍA | MARCA</th>		
				<th align="right">CAN</th>			
				<th align="right" class="d-none bg-red" >PRECIO SUGERIDO</th>	
				<th align="right" >P. UNI.</th>	
				<th align="right" >PRECIO TOTAL</th>
              </tr>
            </thead>
            <tbody>
      <?php
       if($num_rows>0){
      while($dt1 = mysqli_fetch_array($dts1))
      {

		$can = $dt1['tb_ventadetalle_can'];
		$pre_uni=$dt1['tb_ventadetalle_preuni'];
		$des_uni = $dt1['tb_ventadetalle_des'];
		$esp_id = $dt1['tb_especificacion_id'];
		
		$punven_id =$dt1['tb_puntoventa_id'];
		
			        $dtspre = $oPreciogestion->mostrar_precioxpuntoventa($dt1['tb_catalogo_id'],$punven_id);		
					$numrowspre = mysqli_num_rows($dtspre);
					
					$dtp = mysqli_fetch_array($dtspre);
					$preven = $dtp['tb_precioproducto_preven'];
					
					
		
		$valor_uni = $pre_uni/ 1.18;
		$igv_uni = $pre_uni - $valor_uni;
			
		$val_tot = $valor_uni * $can;
		$des_tot = $des_uni * $can;
		$igv_tot = $igv_uni * $can;
		$pre_tot = $pre_uni * $can;
		
		
		$est = $dt1['tb_ventadetalle_est'];
		

	
	//echo $preven.'-'.$pre_uni.'|';
	  
      ?>
      <tr  >
			<td NOWRAP class="d-none" ><?php echo $dt1['tb_empresa_nomcom']?></td>
			<td NOWRAP class="d-none" ><?php echo $dt1['usu_reg']?></td>
			<td NOWRAP class="" ><?php echo $dt1['usu_ven']?></td>
			<td class="" nowrap="nowrap"><?php echo mostrarFecha($dt1['tb_venta_fec'])?></td>
		<td NOWRAP >
		<?php 
		echo $dt1['tb_venta_numdoc'].'</br>';;
		/*
		$dts4=$oVentapago->filtrar($dt1['tb_venta_id']);
		$num_rows= mysqli_num_rows($dts4);
		while($dt4 = mysqli_fetch_array($dts4))
		{
			if($dt4['tb_formapago_id']==1) echo '<span style="color:#4A148C" >CONTADO - </span>';
			if($dt4['tb_formapago_id']==2) echo '<span style="color:#FF1744" >CREDITO</span>';
			if($dt4['tb_formapago_id']==1){
						if($dt4['tb_modopago_id']==1)echo '<span style="color:#33691E" >EFECTIVO</span>';
						if($dt4['tb_modopago_id']==2)echo '<span style="color:#FF9100" >DEPOSITO</span>';
						if($dt4['tb_modopago_id']==4)echo '<span style="color:#FF9100" >TRANSFERENCIA</span>';
						if($dt4['tb_modopago_id']==5)echo '<span style="color:#33691E" >CHEQUE</span>';
						if($dt4['tb_modopago_id']==6)echo '<span style="color:#33691E" >TARJETA</span>';
						if($dt4['tb_modopago_id']==7)echo '<span style="color:#880E4F" >YAPE</span>';
						if($dt4['tb_modopago_id']==8)echo '<span style="color:#880E4F" >PLIN</span>';
			}
			echo '</br>';
		}
		*/
		?>
	  </td>
       
		
							<td style="padding:2px;" > <strong> <?php echo $dt1['tb_ventadetalle_nom']?> </strong>  </td>
		
			
			<td NOWRAP class="" ><?php echo $dt1['tb_marca_nom'].' | '.$dt1['tb_unidad_nom']; ?></td>
       
			<td NOWRAP align="right"><?php echo $dt1['tb_ventadetalle_can'] * 1;
		
			?></td>
			<td NOWRAP class="text-red d-none " align="right"><?php echo $monsim.' 1'.formato_money($preven);?></td>
			<td NOWRAP class="" align="right" <?php if(formato_money($preven)!=formato_money($pre_uni)) echo '';?>   ><?php echo ' '.formato_money($pre_uni);?></td>
			<td NOWRAP class="" align="right"><?php echo '  '.formato_money($pre_tot);?></td>
     </tr>
      <?php
	  
	  $totcan += $dt1['tb_ventadetalle_can'];
	  $totpreuni += $pre_uni;
	  $tot += $pre_tot;
	  
    }
    mysqli_free_result($dts1);
    }
    ?>
  </tbody>
  <tr class="even ">
  <td colspan="5">TOTAL SOLES</td>
  <td NOWRAP class="" align="right"><strong><?php echo '<span class="text-primary"></span>'.' '.$totcan * 1?></strong></td>
  <td NOWRAP class="" align="right"><strong><?php echo '<span class="text-primary">'.$monsim.'</span>'.' '.formato_money($totpreuni)?></strong></td>
  <td NOWRAP class="" align="right"><strong><?php echo '<span class="text-primary">'.$monsim.'</span>'.' '.formato_money($tot)?></strong></td>
</tr>
            </table>
         </div>

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

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


$("#btn_excel").click(function(){
			usu_id = '<?php echo $_POST['cmb_fil_usuario_id']; ?>';
			punven_id = '<?php echo $_POST['cmb_fil_puntoventa_id']; ?>';
			fec1 = '<?php echo $fecha1; ?>';
			fec2 = '<?php echo $fecha2; ?>';
			mon_id = '<?php echo $_POST['cmb_fil_mon_id']; ?>';
			abrirEnPestana('../ventageneral_cst/ventageneral_cst_excel.php?usu_id='+usu_id+'&punven_id='+punven_id+'&fec1='+fec1+'&fec2='+fec2+'&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>  