<?php
$queryclose = mysqli_query($mysqli, "SELECT apertura FROM apertura_cierre where id = 1") 
or die('error: '.mysqli_error($mysqli));
$rows  = mysqli_num_rows($queryclose);
$data  = mysqli_fetch_assoc($queryclose);
$bandera = $data['apertura'];
/*print_r($bandera);
die();*/

if($bandera == 'CERRADO') {
	echo '<script>
                alert("EL ALMACEN SE ENCUENTRA CERRADO POR INVENTARIO DEL SUPERVISOR");
                window.location = "https://inventarios.casatequilaarandas.com/main.php?module=closed";
          </script>';     
    return;
}
?>

<style type="text/css">
  .btn-primary-spacing {
    margin-right: 5px;    
  }
</style>


<section class="content-header">

 <FONT SIZE=6 >Entradas de producto para saldo inicial</FONT>
  <h1>
    
 
	 
	 <!--<a <button type="button" class="btn btn-primary btn-sm"  href="modules/trade/saldo_inicial/print.php" title="Imprimir"
    data-toggle="tooltip" onclick="return fnSelect(1)" id="lnkPrint"></button><i class="fa fa-print"></i>Guardar
	 </a> -->
	 <a   class="btn btn-primary btn-sm" class="left"  href="modules/trade/saldo_inicial/print.php" title="Imprimir"
    data-toggle="tooltip" onclick="return fnSelect(1)" id="lnkPrint" target="_blank">
      <i class="fa fa-print"></i>Guardar
    </a>  	
	 
	  <a <button type="button" class="btn btn-primary btn-sm"  href="?module=saldo_inicial_form&form=add" title="Agregar"
    data-toggle="tooltip"></button><i class="fa fa-plus"></i>Agregar
	 </a> 
	 
	     
	<a <button type="button" class="btn btn-primary btn-sm"  href="modules/trade/saldo_inicial/cancel.php" title="Cancelar"
    data-toggle="tooltip" onclick="return fnSelect(2)" id="lnkCancel"></button><i class="fa fa-remove"></i>Eliminar partida
	 </a>  
	 
    <!--<a class="btn btn-primary btn-social pull-right" href="modules/trade/saldo_inicial/cancel.php" title="Cancelar"
    data-toggle="tooltip" onclick="return fnSelect(2)" id="lnkCancel">
      <i class="fa fa-remove"></i>Devolver
    </a> 
    <a class="btn btn-primary btn-social pull-right btn-primary-spacing" href="modules/trade/saldo_inicial/print.php" title="Imprimir"
    data-toggle="tooltip" onclick="fnSelect(1)" id="lnkPrint" target="_blank">
      <i class="fa fa-print"></i>Guardar
    </a>   
    <a class="btn btn-primary btn-social pull-right btn-primary-spacing" href="?module=form_medicines_transaction&form=add" title="Agregar" data-toggle="tooltip">
 <i class="fa fa-plus"></i>Generar entradas  --> 
    </a>    
  </h1>
</section>
<section class="content-header">
</section>
</section>
<section class="content-header">
</section>

<section class="content">
  <div class="row">
    <div class="col-md-12">

    <?php  

    if (empty($_GET['alert'])) {
      echo "";
    } 
    elseif ($_GET['alert'] == 1) {
      echo "<div class='alert alert-success alert-dismissable'>
              <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
              <h4>  <i class='icon fa fa-check-circle'></i> Exito!</h4>
              Datos de productos han sido registrado correctamente.
            </div>";
    }
    elseif ($_GET['alert'] == 2) {
      echo "<div class='alert alert-danger alert-dismissable'>
              <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
              <h4>  <i class='icon fa fa-warning'></i> Error!</h4>
              Favor de seleccionar registros.
            </div>";
    }     
    elseif ($_GET['alert'] == 3) {
      echo "<div class='alert alert-success alert-dismissable'>
              <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
              <h4>  <i class='icon fa fa-check-circle'></i> Exito!</h4>
              Registros cancelados correctamente.
            </div>";
    }    
    ?>

      <div class="box box-primary">
        <div class="box-body">
          <form>
             <div class="form-group col-md-3">             
              <label for="fecha">Fecha</label>                
              <input type="text" class="form-control date-picker" data-date-format="dd-mm-yyyy" name="fecha" id="fecha" autocomplete="off" value="<?php echo date("Y-m-d"); ?>" readonly required>              
            </div>
            <div class="form-group col-md-3">             
              <label for="fecha">Usuario que genera entrada </label>                
              <input type="text"  class="form-control" autocomplete="off" value="<?php echo $_SESSION['name_user']; ?>" readonly required>              
            </div>
			
			 
			 
			  <div class="form-group col-md-3">
				<?php 
					//Query Encabezado
				$queryfol = mysqli_query($mysqli, "
				select concat(serie,(select folio from transaccion_medicamentos_enc order by  folio desc  LIMIT 1 ) + 1) as folio 
				from  transaccion_medicamentos_enc 
				order by serie desc limit 1 ")
						or die('Error '.mysqli_error($mysqli));
				$rw_folio=mysqli_fetch_array($queryfol); 
			
				?>		  
              <label for="fecha">Folio de entrada</label>                
              <input type="text"  class="form-control"  value="<?php echo $rw_folio['folio']; ?>" readonly required>              
            </div>
             <br>
<br>			 
            <div class="form-group row">			
							
			</div>

			
              <div class="form-group col-md-6">    
              <label for="observacion_enc">Observacion General:</label>                
              <textarea id="observacion_enc" name="observacion_enc" class="form-control" required> 	</textarea>				  
            </div>	
            <br><br>
			<br><br>
			<br>

               <div class="form-group">  
                <label class="col-sm-2 control-label">Enviar documento de entrada por correo a:</label>
                <div class="col-sm-5">
                  <select class="chosen-select" id="txtCorreo" name="txtCorreo" data-placeholder="-- Seleccionar correo --" onchange="tampil_obat(this)" autocomplete="off" required>
                    <option value=""></option>
                    <?php
                      $query_correos = mysqli_query($mysqli, "SELECT nombre,tipo FROM correos ORDER BY tipo ASC")
                                                            or die('error '.mysqli_error($mysqli));
                      while ($data_correos = mysqli_fetch_assoc($query_correos)) {
                        echo"<option value=\"$data_correos[tipo]\"> $data_correos[nombre] | $data_correos[tipo] </option>";
                      }
                    ?>
                  </select>
                </div>
              </div>			
          </form>
          <div class="clearfix"></div>
          <hr>
        </div>
        <div class="box-body"> 
        <div class="table-responsive">         
          <table id="dataTables1" class="table table-bordered table-striped table-hover">
           
            <thead>
              <tr>
                <th class="center"></th>
                <th class="center">No.</th>
                <th class="center">Codigo de Transación</th>
                <th class="center">Fecha</th>
                <th class="center">Codigo de producto</th>
                <th class="center">Productos</th>
				<th class="center">Tipo</th>
                <th class="center">Cant.</th>
                <th class="center">Unidad</th>			
                <th class="center">Observaciones</th>
              </tr>
            </thead>
         
            <tbody>
            <?php  
            $no = 1;
           
            $query = mysqli_query($mysqli, "
              SELECT a.tipo_transaccion, a.codigo_transaccion,a.fecha,a.codigo,a.numero,b.codigo,b.nombre,b.unidad,a.observaciones
              FROM transaccion_medicamentos as a 
			  INNER JOIN medicamentos as b ON a.codigo = b.codigo 
              WHERE a.tipo_transaccion='Entrada' and a.idenc is NULL and a.status = 'A'
              ORDER BY codigo_transaccion DESC")
                     or die('error '.mysqli_error($mysqli));

           
            while ($data = mysqli_fetch_assoc($query)) { 
              $transacc = $data['codigo_transaccion'];  //  Adrian
              $fecha    = $data['fecha'];
              $exp      = explode('-',$fecha);
              $fecha2   = $exp[2]."-".$exp[1]."-".$exp[0];

             
              echo "<tr>
                      <td width='30' class='center'>
                      <input type='checkbox' name='chk_" . $transacc . "' />
                      </td>
                      <td width='30' class='center'>$no</td>
                      <td width='100' class='center'>$data[codigo_transaccion]</td>
                      <td width='80' class='center'>$fecha</td>
                      <td width='80' class='center'>$data[codigo]</td>
                      <td width='200'>$data[nombre]</td>
					  <td width='80' class='center'>$data[tipo_transaccion]</td>
                      <td width='100' align='right'>$data[numero]</td>
                      <td width='80' class='center'>$data[unidad]</td>					  
                      <td width='80' class='center'>$data[observaciones]</td>
                    </tr>";
              $no++;
            }
            ?>
            </tbody>
          </table>
		      </div><!-- /.box-body -->
        </div><!-- /.box-body -->
      </div><!-- /.box -->
    </div><!--/.col -->
  </div>   <!-- /.row -->
</section><!-- /.content -->

<script type="text/javascript">
  function fnSelect(tipo) {
    //tipo = 1 -> Imprimir
    //tipo = 2 -> Cancelar

    var fecha = $('#fecha').val();
    var nombre = $('#nombre').val();
	var observacion_enc = $('#observacion_enc').val();
    var txtCorreo = $('#txtCorreo').val();
    var sel = "";
    //get selected items
    $.each($("input[type=checkbox]:checked"), function(){
      sel += $(this).attr('name') + "|";
    });

    //remove last character
    if(sel != "")
      sel = sel.substring(0, sel.length - 1);
    
    //set URL default    
    $('#lnkPrint').attr("href", "modules/trade/saldo_inicial/print.php");        
    $('#lnkCancel').attr("href", "modules/trade/saldo_inicial/cancel.php");
    
    var url = '?sel=' + sel;

    //send to print PDF or Cancel
    if(tipo == 1) {
      url = $('#lnkPrint').attr("href") + url;
      url = url + '&fecha=' + fecha + '&nombre=' + nombre + '&observacion_enc=' + observacion_enc + '&txtCorreo=' + txtCorreo;
      $('#lnkPrint').attr("href", url);
      setTimeout(retardo, 2000);     
    }
    else if(tipo == 2) {
      if(confirm('Estas seguro de cancelar?') == true) {
        url = $('#lnkCancel').attr("href") + url;
        $('#lnkCancel').attr("href", url);
        return true;
      }
      else{        
        return false;
      }
    }
    
  }

  function retardo() {    
    window.location = "main.php?module=saldo_inicial";
    document.location.reload();
  }
</script>
