<?php
 //ini_set('display_errors',1);
 session_start();
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("../producto/cProductolote.php");
$oProductolote = new cProductolote();

require_once ("../preciogestion/cPreciogestion.php");
$oPreciogestion = new cPreciogestion();
//echo $_SESSION['puntoventa_id'];
require_once("../producto/cProductomarcacodigo.php");
$oProductomarcacodigo = new cProductomarcacodigo();
require_once ("../formatos/formato.php");
if($_POST['dat1']=="" && $_POST['dat2']=="" && $_POST['dat3']=="" && $_POST['dat4']=="" && $_POST['mar']==""&& $_POST['codmar']=="")
{
?>
	
	<div class="table-responsive">		
				<div class="col-lg-12 col-md-12 col-xs-12 " >
				
            <table id="tabla_producto" class="table table-bordered table-striped">
			    <thead>
			        <tr>
			            <th nowrap >CANTIDAD</th>
						<th nowrap >NOMBRE</th>
						<th nowrap >PRINCIPIO</th>
			            <th >PRESENTACION</th>
			            <th >EMPAQUE</th>
			            <th >LABORATORIO</th>
			            <th >PRECIO VENTA</th>
			            <th >PRECIO COSTO</th>
			            <th >STOCK</th>
			        </tr>
			    </thead> 
				    <tbody>
					
					<tr>
					<td <?php if($_POST['vista']=='cotizacion_form'){echo 'colspan="7"'; }else{ echo 'colspan="7"'; }?> >NO HAY FILTROS DE BUSQUEDA</td>
					</tr>
				       
					</tbody> 
            </table>
          
		  </div>
		  </div>
		  
	
<?php 
	
}else{
	
$dts1 = $oProducto->mostrar_filtro_proceso($_POST['dat1'],$_POST['dat2'],$_POST['dat3'],$_POST['dat4'],'','',$_POST['mar'],$_POST['codmar'],'','');
$num_rows= mysqli_num_rows($dts1);
//}
?>
<!--
<section class="content" id="table" style="min-height:0px;padding-bottom:0px;margin-bottom:-30px;">
  <div class="row">
    <div class="col-xs-12">
      <div class="box">
	  <div class="box-body">
-->	
<style>
	#tabla_producto td{
		vertical-align: middle;
		padding: 4px 4px !important;
	}
	#tabla_producto td.editable,#tabla_producto td.editable *{
		cursor: text !important;
		height: 100%; 
		border-bottom-color: #ccc;
		border-bottom-style: dashed; 
		border-bottom-width: 0.5px;
	}
	#tabla_producto td.editando{
		background-color: rgba(18, 180, 243, 0.05);
		outline: 2px solid #35A4F3;
	}
	#tabla_producto td input{
		height: 100%;
		width: 100%;
		box-sizing: border-box;
		display: inline-block;
		border: none;
		background: transparent;
		font-size:20px;
	}
	#tabla_producto td input:focus{
		background: transparent;
		height: 100%;
		width: 100%;
		outline: none;
		border: none;
		color:#000;
	}
	#tabla_producto td input[readonly]{
		background: transparent;
	}
</style> 
	<div class="table-responsive">		
				<div class="col-lg-12 col-md-12 col-xs-12 " >
				
            <table id="tabla_producto" class="table table-bordered table-striped">
			    <thead>
			        <tr>
			           <!-- <th nowrap >CODIGO</th>
			            -->
						<th nowrap >CANTIDAD</th>
						<th nowrap >COD. INT.</th>
						<th nowrap >NOMBRE</th>
						<th nowrap >LOTE</th>
						<th nowrap >PRINCIPIO</th>
						<th >STOCK</th>
						 <th >PRECIO VENTA</th>
			            <th >PRECIO VENTA F</th>
						<th >LABORATORIO</th>
			            <th >PRESENTACION</th>
			            <th >EMPAQUE</th>
			            <th >FEC. DE VCTO</th>
			            
			        </tr>
			    </thead> 
				    <tbody>
					
					
					 <?php 
					 	
						if($num_rows>=1){ 
						
						while($dt1 = mysqli_fetch_array($dts1)) {
							
						/* 	$dtspri = $oProductomarcacodigo->mostrarUno($dt1['tb_catalogo_id'],'1');
							$dtpri = mysqli_fetch_array($dtspri);
							$codpri = $dtpri['tb_productomarcacodigo_cod'];
						*/
							
							$stock = mysqli_fetch_array($oStock->stock_por_presentacion($dt1['tb_catalogo_id'],$dt1['tb_marca_id'],$_POST['alm_id']))['tb_stock_num']*1;
							//echo $stock;
							
							$emp_fac = $dt1['tb_empaque_fac'];
							$can = $stock / $emp_fac;
							$canf = 0;
							$cane = 0;
							//echo $emp_fac.'--';
							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;
							}
							
							
					$dtspre = $oPreciogestion->mostrar_precioxpuntoventa($dt1['tb_catalogo_id'],$_SESSION['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'];
					$prepro_bon = $dtp['tb_precioproducto_bon'];
					$preproesp_id = $dtp['tb_precioproducto_id'];
					
					
					$dtspl = $oProductolote->mostrarUltimoLote($dt1['tb_catalogo_id']);		
					$numrowspl = mysqli_num_rows($dtspl);
					
					$dtp = mysqli_fetch_array($dtspl);
					$lote = $dtp['tb_productolote_nom'];
					
					
							
							
							$dias = $oProducto->calcula_fecha($dt1['tb_productomarca_fecven'],date('d-m-Y'));
							
							$des = substr($dt1['tb_producto_des'],0,30);							
							
						?>
				            <tr	class="tr_pro" data-catid = "<?php echo $dt1['tb_catalogo_id']?>" 
							data-proid = "<?php echo $dt1['tb_producto_id']?>" style="cursor:pointer;">
				            	
				            <!--	<td nowrap="" ><?php echo '<span style="font-size:16px;" class="label label-warning label-sm">'.$codpri.'</span>'; ?></td>
				            	-->
								<td align="right" width="50" class="editable" style="font-size:9pt; font-weight:bold" > 
							
							<input type="text" value="<?php echo '' ?>" 
							readonly data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" 
							data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" style="text-align: right;">
				
								</td>
									<td style="font-weight:bold" nowrap=""  > 
								<strong> <?php echo $dt1['tb_producto_codint']; ?> </strong> 
								</td>
								<td style="font-weight:bold" nowrap=""  > 
								<strong> <?php echo $dt1['tb_producto_nom']?> </strong> 
								
								<?php if(formato_money($prepro_bon)>0){?>
								<strong  class="btn btn-sm" data-toggle="tooltip" data-placement="top" 
								title="<?php echo formato_money($prepro_bon); ?>" ><i style="color:#FFAB00;font-size:1.2rem;" class="fab fa-creative-commons-nc"></i></strong> 
								<?php }?>
								
								<span class="btn btn-primary btn-xs" title="Ver todos almacenes" data-toggle="tooltip" data-placement="top"   onclick="ver_almacenes('<?php echo $dt1['tb_catalogo_id']; ?>','<?php echo $dt1['tb_marca_id']; ?>','<?php echo $_POST['alm_id']; ?>');"  >
									<i class="fas fa-store"></i>
								</span>
								
								</td>
								<td style="font-weight:bold" nowrap=""  > <strong style="padding:5px;background-color:#3F51B5;color:#fff;"  > <?php echo $lote?> </strong> </td>
								<td style="font-weight:bold" nowrap=""  > <strong data-toggle="tooltip" data-placement="top" title="<?php echo $dt1['tb_producto_des']; ?>" > <?php echo $des?> </strong> </td>
				                <td style="font-weight:bold" align="right" ><?php if($stock==''){}else{  if($stock<=10){ echo '<strong style="color:#64DD17;font-size:2rem;">'.$stock .'</strong>';}else{ echo '<strong style="font-size:2rem;" >'.$stock .'</strong>';}} ?></td>
				              	<td style="font-weight:bold" align="right" > <strong class="text-danger"> <?php echo formato_money($preven);?></strong></td>
				                <td style="font-weight:bold" align="right" > <strong class="text-danger"> <?php echo formato_money($prevenf);?></strong></td>
				              	<td style="font-weight:bold" > <strong class="text-danger"> <?php echo $dt1['tb_marca_nom'];?> </strong></td>
				                <td style="font-weight:bold" ><?php echo $dt1['tb_presentacion_nom'];?></td>
				                <td style="font-weight:bold" ><?php echo $dt1['tb_empaque_nom'];?></td>
				                
				                
				                <td style="font-weight:bold" nowrap align="right" > 
								
								<?php if($dias<0){
								?>
								<strong style="font-size:1.2rem;color:#FF7043;" title="VENCIDO"> <?php echo mostrarFecha($dt1['tb_productomarca_fecven']);?></strong>
								<?php 
								}else if($dias==0){
								?>
								<strong style="font-size:1.1rem;color:#01579B;" title="VENCE HOY" > <?php echo mostrarFecha($dt1['tb_productomarca_fecven']);?></strong>
								<?php 
								}else if($dias>0 && $dias < 10){
								?>
								<strong style="font-size:1rem;color:#64DD17;" title=" A <?php echo $dias ?> POR VENCER"> <?php echo mostrarFecha($dt1['tb_productomarca_fecven']);?></strong>
								<?php 
								}else{
								?>
								<strong class=""> <?php echo mostrarFecha($dt1['tb_productomarca_fecven']);?></strong>
								<?php 
								}?>
								
								
								</td>
				               				   
				            </tr>
													
				        <?php
						
						} 
						mysqli_free_result($dts1); 
						}
							
					//	if($_POST['codmar']!=""){}else{
							?>
						<!--	<tr>
							<td colspan="4" >INGRESE UN CODIGO Y BUSQUE</td>
							</tr>
							-->
							<?php 
					//	}
						?>
					
				       
					</tbody> 
            </table>
		
		<table>
		  
		  <tr>
		  <td>
			<div style="background-color:#FF7043;height:12px;width:12px;"></div>
		  </td>
		  <td style="height:10px;"></td>
		  <td> 
		  <strong style="color:#FF7043;" > PRODUCTO VENCIDO.</strong>
		  </td>
		  </tr>
		  <tr>
		  <td>
			<div style="background-color:#01579B;height:12px;width:12px;"></div>
		  </td>
		  <td style="height:10px;"></td>
		  <td> 
		  <strong style="color:#01579B;" > PRODUCTO VENCE HOY.</strong>
		  </td>
		  </tr>
		  <tr>
		  <td>
			<div style="background-color:#64DD17;height:12px;width:12px;"></div>
		  </td>
		  <td style="height:10px;"></td>
		  <td> 
		  <strong style="color:#64DD17;" > PRODUCTO POR VENCER.</strong>
		  </td>
		  </tr>
		  
				
		
		</table>
		  
		  </div>
		  </div>
		 
		
		  
		  
		  <script type="text/javascript">
		  
		  	$('#tabla_producto td input').keydown(function(ev) {
			if(ev.keyCode == 13 && !$(this).attr('readonly')) {
			
			var cat_id = $(this).attr('data-cat_id');
			var mar_id = $(this).attr('data-mar_id');
			var cantidad = $(this).val();
	
			agregar_producto('agregar',cat_id,cantidad,'',mar_id,'','','<?php echo $_POST['cot_id'] ?>')
			
				$(this).attr('readonly', 'true');
				$(this).parent().removeClass('editando');
				return false;
			}
		});
		  
		  
		  	$('#tabla_producto td input').parent().click(function(ev) {
			var $input = $(this).find('input');
			var $td = $input.parent().addClass('editando');
			$input.removeAttr('readonly').focus().select();
		});
		$('#tabla_producto td input').blur(function(event) {
			$(this).val($(this).attr('data-original'));
			$(this).attr('readonly', 'true');
			$(this).parent().removeClass('editando');
		});
		$('#tabla_producto td input').keydown(function(ev) {
			if(ev.keyCode==27){
				$(this).val($(this).attr('data-original'));
				$(this).attr('readonly', 'true');
				$(this).parent().removeClass('editando');
			}
		});
		 
		 
		 
function ver_almacenes(cat_id,mar_id,alm_id)
	{
	
	$('#div_inventarioveralmacenes_form_dialog').modal();
	$.ajax({
		type: "POST",
		url: "../stock/stock_form_almacenes.php",
		async:true,
		dataType: "html",                      
		data: ({
			vista : 'inventarioinicial_tabla',
			cat_id:cat_id,
			mar_id:mar_id,
			alm_id:alm_id
		}),
		beforeSend: function() {

			var div = "<center> <br> <span class='spinner-border spinner-border-lg mr-2'></span> <span style='font-size:1.5rem;'>PROCESANDO PETICIÓN</span></center>";	
			$('.div_inventarioveralmacenes_form').html(div);
        },
		success: function(html){
			$('.div_inventarioveralmacenes_form').html(html);
		},
		complete: function(){			
		}
	});    
	}	


		  
		  </script>
	
<?php }?>