<?php
//ini_set('display_errors',1);
require_once ("../../config/Cado.php");
require_once ("cGestionstock.php");
$oGestionstock = new cGestionstock();
require_once ("../formatos/formato.php");
$limite = $_POST['cmb_fil_lim'];
require_once ("../puntoventa/cPuntoventa.php");
$oPuntoventa = new cPuntoventa();
require_once ("../preciogestion/cPreciogestion.php");
$oPreciogestion = new cPreciogestion();
require_once("../stock/cStock.php");
$oStock = new cStock();
$dts =$oPuntoventa->filtrar('','');


$dts1 = $oGestionstock->filtrar($_POST['txt_fil_pro_dat1'],$_POST['txt_fil_pro_dat2'],$_POST['txt_fil_pro_dat3'],
$_POST['txt_fil_pro_dat4'],$limite,$_POST['txt_fil_mar'],$_POST['txt_fil_pro_acc'],$_POST['cmb_fil_cat_id']);
$num_rows= mysqli_num_rows($dts1);

?>
<style>
	#tabla_gestion_precios td{
		vertical-align: middle;
		padding: 4px 4px !important;
	}
	#tabla_gestion_precios td.editando{
			background-color: rgba(18, 180, 243, 0.05);
			outline: 2px solid #35A4F3;
	}
	#tabla_gestion_precios td input{
		height: 100%;
		width: 100%;
		box-sizing: border-box;
		display: inline-block;
		border: none;
		background: transparent;
	}
	#tabla_gestion_precios td input:focus{
		background: transparent;
			height: 100%;
			width: 100%;
			outline: none;
			border: none;
	}
	#tabla_gestion_precios td input[readonly]{
		background: transparent;
	}
 
	</style>
<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;">
	
					<div class="table-responsive">	
					
					<span style="cursor:pointer" id="btn_excel" class="btn btn-success d-none"> <i class="fas fa-file-excel"></i> DESCARGAR EXCEL</span>
		
					
					
					<table id="tabla_gestion_precios" class="table table-bordered table-striped">
						<thead>
							<tr>
								<th>COD. INTERNO</th>
								<th>NOMBRE</th>
								<th>LABORATOTIO / CATEGORIA</th>
								<th>PRESENTACION / EMPAQUE</th>
								<th>COSTO</th>
								
								<?php 
								while($dt = mysqli_fetch_array($dts)){
								?>	
									<th><?php echo $dt['tb_puntoventa_nom']; ?></th>
								<?php 	
								}
								?>
								
							
							<th>CAN. VENTAS TOTALES</th>
							
							</tr>
						</thead>
						<?php if($num_rows>=1){ ?>
						<tbody>	
						
						<?php 
						
						while($dt1 = mysqli_fetch_array($dts1)){
						
						$canven = 0;
						
						$dtspre = $oPreciogestion->mostrar_precioxpuntoventa($dt1['tb_catalogo_id'],1);		
									$numrowspre = mysqli_num_rows($dtspre);
									
									$dtp = mysqli_fetch_array($dtspre);
									$prevenf = $dtp['tb_precioproducto_prevenf'];
									$preven = $dtp['tb_precioproducto_preven'];
									$precos = $dtp['tb_precioproducto_precom'];
									$preproesp_id = $dtp['tb_precioproducto_id'];
									
									
							/* 
							$emp_fac = $dt1['tb_empaque_fac'];
							$can = $stock / $emp_fac;
							$canf = 0;
							$cane = 0;
							
							if($stock<$emp_fac){
								$cane = $cane;
								$canf = $stock;
							}else{
								list($cane,$canf) = explode('.',$can);
							}
							
							
							if($cane>0 && $canf>0){
								
								$canemp = $cane * $emp_fac;
								$canf = $stock - $canemp;
								$stock = $cane.'F'.$canf;
								
							}else if($cane>0 && $canf==""){
								$stock = $cane;
							}else if($cane=="" && $canf>0){
								
								$stock = 'F'.$canf;
							}else if($cane==0 && $canf>0){
								
								$stock = 'F'.$canf;
							}
							 */
                            
                        $dtscv = $oGestionstock->filtrar_cantidadvendida($dt1['tb_catalogo_id'],$dt1['tb_marca_id']);
						$dtcv = mysqli_fetch_array($dtscv);
						$canven = $dtcv['cantidadvendida']; 
					   
                            
						?>
					
					<tr>
					<td><?php echo $dt1['tb_producto_codint']?></td>
					<td><?php echo $dt1['tb_producto_nom']?></td>
					<td><?php echo $dt1['tb_marca_nom'].' / '.$dt1['tb_categoria_nom']?></td>								
					<td><?php echo $dt1['tb_presentacion_nom'].' / '.$dt1['tb_empaque_nom']?></td>								
					<td><?php echo formato_money($precos); ?></td>								
					<?php
					$dts2=$oPuntoventa->filtrar('','');					
					while($dt2 = mysqli_fetch_array($dts2)){
						
						$dtssto = $oStock->stock_por_presentacion($dt1['tb_catalogo_id'],$dt1['tb_marca_id'],$dt2['tb_almacen_id']);
						$dtsto = mysqli_fetch_array($dtssto);
						$stock = $dtsto['tb_stock_num']*1; 
						$stock_id = $dtsto['tb_stock_id']; 
						
					?>	
					<td><?php echo $stock; ?></td>
					<?php 	
					}
					?>
					
						<td><?php echo $canven*1; ?></td>
						
					
					</tr>
								
								<?php } 
								mysqli_free_result($dts1); ?>
							</tbody>
							<?php } ?>
							
							<tr class="even">
								<td colspan="13"><?php echo $num_rows.' registros'?></td>
							</tr>
						</table>
					
						</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();
}

$("#btn_excel").click(function(){
			dat1 = '<?php echo $_POST["txt_fil_pro_dat1"]; ?>';
			dat2 = '<?php echo $_POST["txt_fil_pro_dat2"]; ?>';
			dat3 = '<?php echo $_POST["txt_fil_pro_dat3"]; ?>';
			dat4 = '<?php echo $_POST["txt_fil_pro_dat4"]; ?>';
			lit = '<?php echo $limite; ?>';	
			mar = '<?php echo $_POST["txt_fil_mar"]; ?>';	
			acc = '<?php echo $_POST["txt_fil_pro_acc"]; ?>';	
			cat_id = '<?php echo $_POST["cmb_fil_cat_id"]; ?>';	
			abrirEnPestana('../../files/gestionstock/gestionstock_excel.php?dat1='+dat1+'&dat2='+dat2+'&dat3='+dat3+'&dat4='+dat4+'&mar='+mar+'&lit='+lit+'&cat_id='+cat_id);
});



</script>
	
	
	