<?php


//ini_set("display_errors",1);
require_once ("../../config/Cado.php");
require_once ("../stockvalorizado/cStockvalorizado.php");
$oStockvalorizado = new cStockvalorizado();
require_once ("../preciogestion/cPreciogestion.php");
$oPreciogestion = new cPreciogestion();
require_once ("../formatos/formato.php"); 

if($_POST['cmb_fil_alm_id']>0)
{
	$dts1=$oStockvalorizado->filtrar($_POST['txt_fil_pro_dat1'],$_POST['txt_fil_pro_dat2'],$_POST['txt_fil_pro_dat3'],
$_POST['txt_fil_pro_dat4'],$_POST['cmb_fil_lim'],$_POST['cmb_fil_alm_id'],$_POST['txt_fil_mar']);
	$num_rows= mysqli_num_rows($dts1);
}
else
{
	$num_rows=0;
}
?>
 
<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_producto" class="table table-bordered table-striped">
						<thead>
			                <tr>
			                    <th>NOMBRE</th>
			                    <th>EMPAQUE</th>
			                    <th>LABORATORIO</th>
			                    <th align="right" nowrap>P. COSTO</th>
				                <th align="right">STOCK</th>
				                <th align="right">VALORIZADO COSTO</th>
			                </tr>
			            </thead>
						 <tbody>
			                <?php
			                if($num_rows>0){
								while($dt1 = mysqli_fetch_array($dts1)){
									$stock=$dt1['tb_stock_num'];
									$emp_fac=$dt1['tb_empaque_fac'];
									
									
									$dtspre = $oPreciogestion->mostrar_precioxpuntoventa($dt1['tb_catalogo_id'],$dt1['tb_puntoventa_id']);		
									$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'];
									
									$precosuni = $precos / $emp_fac;
									
									if($stock>0){
									
									$valorizado_cos = $precosuni * $stock;
									
								?>
			                        <tr>
			                           
			                            <td> <span style=""><?php echo $dt1['tb_producto_nom']?></span> </td>
			                            <td><?php echo $dt1['tb_empaque_nom']?></td>
			                            <td><?php echo $dt1['tb_marca_nom']?></td>
			                            <td align="right"><?php echo formato_money($precosuni)?></td>
			                            <td align="right">
											<span><?php echo $stock?></span>
										</td>
			                            <td align="right"><?php echo formato_money($valorizado_cos)?></td>
			                       </tr>
			                <?php
			                	
								$total_costo += $precosuni;
								$total_val_costo += $valorizado_cos;
								$total_stock += $stock;
								}
								}
			                mysqli_free_result($dts1);
			                }
			                ?>
			                </tbody>   
			                <tr class="even">
			                  <td colspan="3">TOTAL</td>
			                  <td align="right"><?php echo formato_money($total_costo)?></td>
			                  <td align="right"><?php echo $total_stock ?></td>
			                  <td align="right"><?php echo formato_money($total_val_costo)?></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();
}


	$(function() { 
	    
	    
$("#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"]; ?>';
			lim = '<?php echo $_POST["cmb_fil_lim"]; ?>';	
			alm_id = '<?php echo $_POST["cmb_fil_alm_id"]; ?>';
			mar = '<?php echo $_POST["txt_fil_mar"]; ?>';	
		
			abrirEnPestana('../stockvalorizado/stockvalorizado_excel.php?dat1='+dat1+'&dat2='+dat2+'&dat3='+dat3+'&dat4='+dat4+'&alm_id='+alm_id+'&mar='+mar+'&lim='+lim);
});


	    
	    
		$("#tabla_producto").DataTable( {
			"columnDefs": [
			{ "orderable": false, "targets": [1] }
			],
			"searching": false,
			"order":[[0,"asc"]]
		});
	}); 
</script>  