<?php

// ini_set('display_errors',1);

session_start();
require_once ("../../config/Cado.php");
require_once ("cOrdencompra.php");
$oOrdencompra = new cOrdencompra();

require_once ("../formatos/formato.php");

$fecha1 = $_POST['txt_fil_ordcom_fec1']; 
$fecha2 = $_POST['txt_fil_ordcom_fec2'];

$dts1=$oOrdencompra->mostrar_filtro(fecha_mysql($fecha1),fecha_mysql($fecha2),
$_POST['cmb_fil_empresa_id'],$_POST['txt_fil_docnum'],$_POST['hdd_fil_pro_id']);
$num_rows= mysqli_num_rows($dts1);
?>

<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_compra" class="table">
            <thead>
              <tr>
					<th>&nbsp;</th>
					<th class="" >REFERENCIAS</th>
					<th class="" >FECHA</th>
					<th>DOCUMENTO</th>                    
					<th>PROVEEDOR</th>
              </tr>
            </thead>
            <tbody>
      <?php
       if($num_rows>0){
      while($dt1 = mysqli_fetch_array($dts1))
      {

			if($dt1['tb_moneda_id']==1){
				$monsim = '<span class="text-primary">'.$dt1['tb_moneda_sim'].'</span>';
				$total_pagar_sol += $dt1['tb_ordencompra_tot'];
	   
			}else if($dt1['tb_moneda_id']==2){
				$monsim = '<span class="text-green">'.$dt1['tb_moneda_sim'].'</span>';
				$total_pagar_dol += $dt1['tb_ordencompra_tot'];
			}
			
		$reg = mostrarFechaHora($dt1['tb_ordencompra_reg']);

		$dts3=$oOrdencompra->mostrar_ordencompra_detallesuma($dt1['tb_ordencompra_id']);
		$dt3= mysqli_fetch_array($dts3);
		$canord = $dt3['sumacan'];
		
		$dts2=$oOrdencompra->mostrar_referenciasuma($dt1['tb_ordencompra_id']);
		$dt2= mysqli_fetch_array($dts2);
		$candoc = $dt2['sumaref'];
		
		
		if($canord>$candoc){
			$est = '1';	
			$estlbl = '<span class="badge badge-danger" style="font-size:0.8rem;" >PEDIDO RECIBIDO INCOMPLETO</span>';	
		}else{
			$est = '2';	
			$estlbl = '<span class="badge badge-primary" style="font-size:0.8rem;" >PEDIDO RECIBIDO COMPLETAMENTE</span>';	
		}
		
		//echo 'nro ord compra- '.$canord.' cantidad compras '.$candoc;
		
	  
      ?>
      <tr title="FECHA DE REGISTRO : <?php echo "$reg";  ?>" >
        <td align="left" nowrap="nowrap">
			<a  class="btn btn-sm" style="background-color:#00897b;color:#fff;text-align:left" 
			href="../../files/ordencompra/ordencompra_impcpe_doc.php?ordcomid=<?php echo $dt1['tb_ordencompra_id'] ?>" 
			target="_blank"><i class="fa fa-file-pdf"></i></a>
		
			<?php if($est==1){
			?>
		  
			<span class="btn btn-sm btn-primary" onClick="ordencompra_form('editar','<?php echo $dt1['tb_ordencompra_id'];?>')"><i class="fa fa-edit"></i></span>
			<span class="btn btn-sm btn-danger" onClick="ordencompra_anular('eliminar','<?php echo $dt1['tb_ordencompra_id']?>','<?php echo $dt1['tb_ordencompra_numdoc']?>')"><i class="fa fa-trash"></i></span>
			<span class="btn btn-sm btn-warning" data-toggle="tooltip" data-placement="top" title="Registrar compra" 
			onClick="compra_form('insertar','<?php echo $dt1['tb_ordencompra_id']?> ')">
			<i class="fa fa-shopping-cart"></i>
			</span>
			
			<?php 	  	  
			}else{
			?>
			<span class="btn btn-sm btn-primary" onClick="ordencompra_form('editar','<?php echo $dt1['tb_ordencompra_id']?>')"><i class="fa fa-eye"></i></span>
			<?php 
			}
			?>
		</td>
     
		<td class="" nowrap="nowrap">
		<?php 
		 
		
		$dts4=$oOrdencompra->mostrar_referencia($dt1['tb_ordencompra_id']);
		$num_rows4= mysqli_num_rows($dts4);
		
		
		if($num_rows4>0){
			while($dt4 = mysqli_fetch_array($dts4)){
			
			$com_id = $dt4['tb_compra_id'];
		?>
		
		<span class="badge btn-primary" style="font-size:0.8rem;cursor:pointer;" onClick="compra_form_vista('editar','<?php echo $dt4['tb_compra_id']?>','<?php echo $dt4['saldo'] ?>')" ><?php echo $dt4['tb_documento_abr'].' '.$dt4['tb_compra_numdoc'];?></span>
			
		<?php 
			}
		
		
		echo $estlbl;
	  }
		?>
		</td>
		
		<td class="" nowrap="nowrap"><?php echo mostrarFecha($dt1['tb_ordencompra_fec'])?></td>
        <td NOWRAP ><?php echo $dt1['tb_documento_abr'].' - '.$dt1['tb_ordencompra_numdoc']?></td>
        <td NOWRAP ><?php echo $dt1['tb_proveedor_nom']?></td>
      </tr>
      <?php
    }
    mysqli_free_result($dts1);
    }
    ?>
  </tbody>
 
            </table>
          </div>
        </div>
        </div>
        </div>
      </div>
    </div>
  </section>

  
   <script type="text/javascript">
  $(function() {  
    $("#tabla_compra").DataTable( {
      paging: true,
	  searching: false,
	  ordering:false,
	  "columnDefs": [
        { "orderable": true, "targets": [0] }
      ],
    });
  }); 
</script>  