<?php
  session_start();
  ob_start();
  
  if (isset($_POST['id']) && isset($_POST['comentario'])) {
      $id = $_POST['id'];
	  $comentario = $_POST['comentario'];
	  


  if($_POST['id'] == ""){
      echo "<meta http-equiv='refresh' content='0; url=https://inventarios.casatequilaarandas.com/main.php?module=entrada_consulta&alert=2'>";
      return;
  }
  
  $sel = $_POST['id'];
  $sel = str_replace("chk_", "", $sel);
  $id = explode("|",$sel);
  $cond = "";
  for($x = 0; $x < count($id); $x++){
      $cond .= "'" . $id[$x] . "',";
  }
  if($cond != "")
      $cond =substr($cond, 0, - 1);

  //echo $cond;
  //return;

  require_once "../../../config/database.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;
    }  

  //Get actual status
  for($x = 0; $x < count($id); $x++) {
    $query = mysqli_query($mysqli, "SELECT status, serie FROM transaccion_medicamentos_enc WHERE id = $id[$x]")
    or die('error'.mysqli_error($mysqli));

    $rows  = mysqli_num_rows($query);
    $data  = mysqli_fetch_assoc($query);
    $sta = $data['status'];
    $folio = $data['serie'].$id[$x];
    if($sta == 'C') {
      echo "<meta http-equiv='refresh' content='0; url=https://inventarios.casatequilaarandas.com/main.php?module=entrada_consulta&alert=4&folio=$folio'>";
        return;
    }
  }

  //Update Stock and Status of transaccion_medicamentos and transaccion_medicamentos_enc
  for($x = 0; $x < count($id); $x++) {
    //Get inventory outputs    
    $query1 = mysqli_query($mysqli, "SELECT codigo_transaccion FROM transaccion_medicamentos WHERE idenc = $id[$x]")
    or die('error'.mysqli_error($mysqli));
    
    while ($data = mysqli_fetch_assoc($query1)) { 
      $tra = $data['codigo_transaccion'];

      //Get number to return and code of product
      $query = mysqli_query($mysqli, "SELECT numero, codigo FROM transaccion_medicamentos WHERE codigo_transaccion = '$tra'")
      or die('error'.mysqli_error($mysqli));

      $rows  = mysqli_num_rows($query);
      $data  = mysqli_fetch_assoc($query);
      $qty = $data['numero'];
      $cod = $data['codigo'];

      //Set new value of stock
      $query = mysqli_query($mysqli, "
          UPDATE medicamentos SET stock = stock - $qty WHERE codigo = '$cod'")
          or die('error: '.mysqli_error($mysqli));

      //Set cancel status detail
      $query = mysqli_query($mysqli, "
          UPDATE transaccion_medicamentos SET status = 'C' WHERE codigo_transaccion = '$tra'")
          or die('error: '.mysqli_error($mysqli));
    }

    //Set cancel status header
    $query = mysqli_query($mysqli, "
    UPDATE transaccion_medicamentos_enc SET status = 'C', txtCancel = '".$comentario."' WHERE id = $id[$x]")
    or die('error: '.mysqli_error($mysqli));
	$rows  = mysqli_num_rows($query);
    $data  = mysqli_fetch_assoc($query);
    $txtCancelEnc = $data['txtCancel'];
   
	/*
	// Codigo Agregado por JLRO
	
	//Set cancel status header remision
    $query = mysqli_query($mysqli, "
    UPDATE remision_encabezado SET status = 'C',txtCancel = CONCAT('CANCELACION ENTRADA CDC-', $id[$x]) WHERE idSalida = $id[$x]")
    or die('error: '.mysqli_error($mysqli));
	
	//Set cancel status detail remision
    $query = mysqli_query($mysqli, "
    UPDATE remision_detalle SET status = 'C' WHERE idenc = (SELECT id FROM remision_encabezado WHERE status = 'C' AND idSalida = $id[$x])")
    or die('error: '.mysqli_error($mysqli));
	
	
	*/
	
	
  }
  }

  echo "<meta http-equiv='refresh' content='0; url=https://inventarios.casatequilaarandas.com/main.php?module=entrada_consulta&alert=3'>";
?>