<?php
 //ini_set('display_errors',1);
 
 date_default_timezone_set('America/Lima');
  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/cProductolote.php");
$oProductolote = new cProductolote();
require_once ("../preciogestion/cPreciogestion.php");
$oPreciogestion = new cPreciogestion();
require_once("../producto/cProductomarca.php");
$oProductomarca = new cProductomarca();
require_once ("../preciogestion/cPreciogestion.php");
$oPreciogestion = new cPreciogestion();
require_once("../producto/cProductomarcacodigo.php");
$oProductomarcacodigo = new cProductomarcacodigo();
require_once ("../formatos/formato.php");

$fec = date("m-d-Y");

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 >FEC. VEN.</th>
			            <th nowrap >LOTE</th>
			            <th nowrap >PRE. VEN.</th>
			            <th nowrap >PRE. VEN. F.</th>
						<th nowrap >NOMBRE</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;
		color:#000;
	}
	#tabla_producto td input:focus{
		background: transparent;
		height: 100%;
		width: 100%;
		outline: none;
		border: none;
		color:#000;
	}
	#tabla_producto td input[readonly]{
		background: transparent;
		color:#000;
		
	}
</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 >LOTE</th>
						<th nowrap >FEC. VENCIMIENTO</th>

						<th nowrap >PRE. VEN.</th>
						<th nowrap >PRE. VEN. F.</th>
						<th nowrap >NOMBRE</th>
			            <th >PRESENTACION</th>
			            <th >EMPAQUE</th>
			            <th >LABORATORIO</th>
			            <th >PRECIO VENTA</th>
			            <th >PRECIO VENTA F</th>
			            <th >FEC. DE VCTO</th>
			            <th >STOCK</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;
							}
								
							$dias = $oProducto->calcula_fecha($dt1['tb_productomarca_fecven'],date('d-m-Y'));
							
							$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'];
							$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'];
					
					
							
							
						?>
				            <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:5pt; font-weight:bold" > 
							
									<input type="text" value="<?php echo '' ?>" class="can_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id'] ?>"
									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 align="right" width="50" class="editable" style="font-size:5pt; font-weight:bold" > 
							
									<input type="text" value="<?php echo '' ?>" placeholder="<?php echo '' ?>"
									readonly data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" class="lote_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id'] ?>"
									data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" style="text-align: right;">
				
								</td>
								
									<td align="right" width="50" class="editable" style="font-size:5pt; font-weight:bold" > 
							
									<input type="text" value="<?php echo '' ?>" placeholder="<?php echo $fec ?>"
									readonly data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" class="fecven_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id'] ?>"
									data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" style="text-align: right;">
				
								</td>
								
								<td align="right" width="50" class="editable" style="font-size:5pt; font-weight:bold" > 
							
									<input type="text" value="<?php echo '' ?>" placeholder="<?php echo '0.00' ?> " 
									readonly data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" class="preven_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id'] ?>"
									data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" style="text-align: right;">
				
								</td>
								
								
								<td align="right" width="50" class="editable" style="font-size:5pt; font-weight:bold" > 
							
									<input type="text" value="<?php echo '' ?>" placeholder="<?php echo '0.00' ?>"
									readonly data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" class="prevenf_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id'] ?>"
									data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" style="text-align: right;">
				
								</td>
								
								
								<td nowrap="" style="font-weight:bold" > <strong> <?php echo $dt1['tb_producto_nom']?> </strong> </td>
								<td style="font-weight:bold" nowrap=""  > <strong class="" style="padding:5px;background-color:#3F51B5;color:#fff;"  > <?php echo $lote?> </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" > <strong class=""> <?php echo $dt1['tb_marca_nom'];?> </strong></td>
				                <td style="font-weight:bold" align="right" > <strong class=""> <?php echo formato_money($preven);?></strong></td>
				                <td style="font-weight:bold" align="right" > <strong class=""> <?php echo formato_money($prevenf);?></strong></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:#00C853;" title="VENCE HOY" > <?php echo mostrarFecha($dt1['tb_productomarca_fecven']);?></strong>
								<?php 
								}else if($dias>0 && $dias < 10){
								?>
								<strong style="font-size:1rem;color:#C6FF00;" 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>
				                <td align="right" ><?php if($stock==''){}else{  if($stock<=10){ echo '<strong style="color:#C6FF00;font-size:2rem;">'.$stock .'</strong>';}else{ echo '<strong>'.$stock .'</strong>';}} ?></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:#00C853;height:12px;width:12px;"></div>
		  </td>
		  <td style="height:10px;"></td>
		  <td> 
		  <strong style="color:#00C853;" > PRODUCTO VENCE HOY.</strong>
		  </td>
		  </tr>
		  <tr>
		  <td>
			<div style="background-color:#C6FF00;height:12px;width:12px;"></div>
		  </td>
		  <td style="height:10px;"></td>
		  <td> 
		  <strong style="color:#C6FF00;" > 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 can = $(".can_"+cat_id+""+mar_id).val();
			var fecven = $(".fecven_"+cat_id+""+mar_id).val();
			var lote = $(".lote_"+cat_id+""+mar_id).val();
			var preven = $(".preven_"+cat_id+""+mar_id).val();
			var prevenf = $(".prevenf_"+cat_id+""+mar_id).val();
		
			agregar_producto('agregar',cat_id,can,'',mar_id,fecven,lote,preven, prevenf)
			
				$(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');
			}
		});
		 
		  
		  </script>
	
<?php }?>