<?php
// ini_set('display_errors',1);

require_once ("../../config/Cado.php");
require_once ("../producto/cProducto.php");
$oProducto = new cProducto();
require_once ("../stock/cStock.php");
$oStock = new cStock();
require_once("../producto/cProductomarca.php");
$oProductomarca = new cProductomarca();
require_once ("../formatos/formato.php"); 
require_once("../producto/cProductotag.php");
$oProductotag = new cProductotag();

require_once("../stockvalorizadoventa/cStockvalorizadoventa.php");
$oStockvalorizadoventa = new cStockvalorizadoventa();

$punven_id = $_POST['cmb_fil_puntoventa_id'];

if($punven_id>0)
{

	$dts1=$oStockvalorizadoventa->mostrar_filtro(fecha_mysql($_POST['txt_fil_valven_fec1']),fecha_mysql($_POST['txt_fil_valven_fec2']),
	$_POST['txt_fil_pro_dat1'],$_POST['txt_fil_pro_dat2'],
	$_POST['cmb_fil_cat_id'],$_POST['txt_fil_pro_mar'],$punven_id);
	$num_rows= mysqli_num_rows($dts1);
 // echo '---'.$num_rows;
}
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;">
	
					<div class="table-responsive">
	
					<table id="tabla_producto" class="table table-bordered table-striped">
						<thead>
			                <tr>
			                    <th nowrap >FECHA - DOCUMENTO</th>
			                    <th nowrap >CANTIDAD</th>
			                    <th nowrap >NOMBRE</th>
			                    <th nowrap >MARCA</th>
			                    <th nowrap >CATEGORIA</th>
			                    <th align="right" nowrap>P. COSTO UNITARIO</th>
			                    <th align="right" nowrap>P. VENTA UNITARIO</th>
			                    <th align="right" nowrap>P. COSTO TOTAL</th>
			                    <th align="right" nowrap>P. VENTA TOTAL</th>
			                    <th align="right" nowrap>DIFERENCIA C-V</th>
			                </tr>
			            </thead>
						 <tbody>
			                <?php
			                if($num_rows>0){
							
							while($dt1 = mysqli_fetch_array($dts1)){
									
								$emp_fac = $dt1['tb_empaque_fac'];
								$igv = $dt1['tb_venta_igvsub'];
								$preuni = $dt1['tb_ventadetalle_preuni'];
								$canven = $dt1['tb_ventadetalle_can'];
								
								
								if($canven>=$emp_fac){
									$preuni = $dt1['tb_ventadetalle_preuni']/$emp_fac;
								}
								
								$totpreven = $dt1['tb_ventadetalle_can'] * $preuni;
							
									
									$dts2=$oStockvalorizadoventa->mostrarkardex_producto(fecha_mysql($dt1['tb_venta_fec']),$dt1['tb_catalogo_id'],$dt1['tb_marca_id']);
									$numrows = mysqli_num_rows($dts2);
									
									$com_preuni = 0;
									if($numrows>0){
										
										$dt2 = mysqli_fetch_array($dts2);
										$kar_idm=$dt2['tb_kardex_idm'];
										mysqli_free_result($dts2);
										
										$dts3=$oStockvalorizadoventa->mostrarcompra_producto($kar_idm,$dt1['tb_catalogo_id'],$dt1['tb_marca_id']);
										$dt3 = mysqli_fetch_array($dts3);
										$com_igv=$dt3['tb_compra_igvsub'];
										$cancom = $dt3['tb_compradetalle_can'];
										$com_preuni=$dt3['tb_compradetalle_preuni'] * $com_igv;
										mysqli_free_result($dts3);
										
										
										if($cancom>=$emp_fac){
									
											$com_preuni = $com_preuni / $emp_fac;
									
										}else{
											$com_preuni = $com_preuni;
										}
			
			
			
									
									}else{
										
										$com_preuni = 0;
									}
									
									
									
									$totprecos = $com_preuni * $dt1['tb_ventadetalle_can'];
									
								if($totprecos>0){
									$diff = $totpreven - $totprecos;
								}else{
									$diff = 0;
								}
									
							?>
			                        <tr>
			                           
			                            <td nowrap ><span  style=""><?php echo mostrarFecha($dt1['tb_venta_fec']).'  '.$dt1['tb_venta_numdoc']; ?></span></td>
			                            <td nowrap ><span  style=""><?php echo $dt1['tb_ventadetalle_can']; ?></span></td>
			                            <td nowrap ><span style=""><?php echo $dt1['tb_producto_nom']?></span></td>
			                            <td nowrap ><?php echo $dt1['tb_marca_nom']?></td>
			                            <td nowrap ><?php echo $dt1['tb_categoria_nom']?></td>
			                            <td nowrap align="right"><?php echo formato_money($com_preuni) ?></td>
			                            <td nowrap align="right"><?php echo formato_money($preuni) ?></td>
			                            <td nowrap align="right"><?php echo formato_money($totprecos)?></td>
			                            <td nowrap align="right"><?php echo formato_money($totpreven)?></td>
			                            <td nowrap align="right"><?php echo formato_money($diff)?></td>
			                       </tr>
			                <?php
								$total_com_preuni += $com_preuni;
								$total_preuni += $preuni;
								$total_com_precos += $totprecos;
								$total_preven += $totpreven;
								$total_diff += $diff;
								
								
								
								}			               
								mysqli_free_result($dts1);
			                }
			                ?>
			                </tbody>   
			                <tr class="even">
			                  <td colspan="5">TOTAL</td>
			                  <td nowrap align="right"><?php echo formato_money($total_com_preuni)?></td>
			                  <td nowrap align="right"><?php echo formato_money($total_preuni)?></td>
			                  <td nowrap align="right"><?php echo formato_money($total_com_precos)?></td>	
			                  <td nowrap align="right"><?php echo formato_money($total_preven)?></td>	
			                  <td nowrap align="right"><?php echo formato_money($total_diff)?></td>	
			                </tr>
					</table>
					</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>



<script type="text/javascript">

	$(function() {  
		$("#tabla_producto").DataTable( {
			"columnDefs": [
			{ "orderable": false, "targets": [] }
			],
			"searching": false,
			"order":[[0,"asc"]]

		});

	}); 

</script>  