🌹🏠🌹？》《》？《》？<?php
class Stock_dep extends Base
{
public $empresa;
public $deposito;
public $area;
public $articulo;
public $calle;
public $rack;
public $pos;
public $bultos;
public $unidades;
public $volumen;
public $otros;
public $estado;
public $nro_pallet;
public $nro_lote;
public $serie;
public $fecha_stk;
public $con_movim;

// mov_stock
public $cantidad_articulo_pallet;
public $tdoc;
public $letdoc;
public $sucdoc;
public $nrodoc;
public $fecha;
public $signo;
public $usuario;
public $fecha_proc;

// mov_stock_repos
public $calle_dest;
public $rack_dest;
public $pos_dest;
public $nro_pallet_dest;

public $tipo_pos;
public $posicion;
public $grupo;
public $familia;
public $grupo_deposito;

public $error = 0; // falso
public $msg_error;

function __construct($r_empresa,$r_deposito)
{  
$this->empresa   = $r_empresa;
$this->deposito  = $r_deposito;
$this->bultos    = 0;
$this->unidades  = 0;
$this->volumen   = 0;
$this->otros     = 0;
$this->fecha_stk = '19000101';
$this->con_movim = 'S';
$this->posicion  = "";

} 

function r_select_stock_dep()
{
 
$this->error     = 0;
$this->msg_error = "";
$p_sql_st="select * from stock_dep 
             where empresa  = '{$this->empresa}' 
               and deposito = '{$this->deposito}' 
               and area     = '{$this->area}' 
               and articulo = '{$this->articulo}' 
               and calle    = '{$this->calle}' 
               and rack     = '{$this->rack}' 
               and pos      = '{$this->pos}' 
               and nro_lote   = '{$this->nro_lote}' 
               and nro_pallet = '{$this->nro_pallet}' 
               and estado     = '{$this->estado}' 
               and serie      = '{$this->serie}' ";
$resultado=@odbc_exec($this->conexion,$p_sql_st);
if (!$resultado){
  $this->error     = 2;
  $this->msg_error = "01- Error. No se pudo leer [stock_dep] !! ";
  return;
}
$row=odbc_fetch_array($resultado);
if (!$row){
  $this->error     = 1;
  $this->msg_error = "02- Error. No se pudo leer [stock_dep] !! ";
  return;
}
$this->bultos       = $row['bultos'];
$this->unidades     = $row['unidades'];
$this->volumen      = $row['volumen'];
$this->otros        = $row['otros'];
$this->fecha_stk    = $row['fecha_stk'];
odbc_free_result($resultado);

return;
}

function r_select_stock_dep_serie()
{

$this->error     = 0;
$this->msg_error = "";
$p_sql_st="select * from stock_dep
             where empresa  = '{$this->empresa}' 
               and serie      = '{$this->serie}'
               and bultos <> 0
               and estado = 'C'
               and area <> 7";
$resultado=@odbc_exec($this->conexion,$p_sql_st);
if (!$resultado){
   $this->error     = 2;
   $this->msg_error = "03- Error. No se pudo leer [stock_dep] !! ";
   return;
}
$row=odbc_fetch_array($resultado);
if (!$row){
   $this->error     = 1;
   $this->msg_error = "04- Error. Serie inexistente en Deposito !! ";
   return;
}
$this->articulo = rtrim($row['articulo']);
$this->deposito = $row['deposito'];
$this->calle    = trim($row['calle']);
$this->rack     = $row['rack'];
$this->pos      = $row['pos'];
$this->area     = $row['area'];
$this->nro_lote = trim($row['nro_lote']);
$this->nro_pallet = trim($row['nro_pallet']);
$this->bultos   = $row['bultos'];
$this->unidades = $row['unidades'];
$this->volumen  = $row['volumen'];
$this->otros    = $row['otros'];
$this->fecha_stk = $row['fecha_stk'];
odbc_free_result($resultado);

return;
}

function r_select_stock_dep_pallet()
{

$this->error     = 0;
$this->msg_error = "";
$p_sql_st="select * from stock_dep
             where empresa  = '{$this->empresa}' 
               and nro_pallet  = '{$this->nro_pallet}'
               and bultos <> 0
               and area <> 7";
$resultado=@odbc_exec($this->conexion,$p_sql_st);
if (!$resultado){
   $this->error     = 2;
   $this->msg_error = "05- Error. No se pudo leer [stock_dep] !! ";
   return;
}
$row=odbc_fetch_array($resultado);
if (!$row){
   $this->error     = 1;
   $this->msg_error = "06- Error. Pallet inexistente en Deposito !! ";
   return;
}
$this->articulo = rtrim($row['articulo']);
$this->deposito = $row['deposito'];
$this->calle    = trim($row['calle']);
$this->rack     = $row['rack'];
$this->pos      = $row['pos'];
$this->area     = $row['area'];
$this->nro_lote = trim($row['nro_lote']);
$this->serie    = trim($row['serie']);
$this->bultos   = $row['bultos'];
$this->unidades = $row['unidades'];
$this->volumen  = $row['volumen'];
$this->otros    = $row['otros'];
$this->fecha_stk = $row['fecha_stk'];
odbc_free_result($resultado);

return;
}

function r_insert_stock_dep()
{

$this->error     = 0;
$this->msg_error = "";
$this->volumen = sprintf("%0.6f",$this->volumen);
$this->otros   = sprintf("%0.6f",$this->otros);
$this->r_fecha();

$p_sql_st="insert into stock_dep ( '{$this->empresa}','{$this->deposito}' ,'{$this->area}','{$this->articulo}',
                                   '{$this->calle}','{$this->rack}','{$this->pos}',
                                   '{$this->bultos}','{$this->unidades}','{$this->volumen}','{$this->otros}',
                                   '{$this->estado}', '{$this->nro_pallet}','{$this->nro_lote}','{$this->serie}','{$this->fecha_stk}' )";
$resultado=@odbc_exec($this->conexion,$p_sql_st);
if (!$resultado) {
$this->error     = 2;
$this->msg_error = "07- Error. No se pudo insertar [stock_dep] !! ";
return;
}
if (odbc_num_rows($resultado) != 1){
$this->error     = 1;
$this->msg_error = "08- Error. No se pudo insertar [stock_dep] !! ";
return;
}
odbc_free_result($resultado);

if ( $this->con_movim == 'N'){
return;
}

$p_sql_st="insert into mov_stock values ( '{$this->empresa}','{$this->tdoc}','{$this->letdoc}','{$this->sucdoc}',
                                  '{$this->nrodoc}', '{$this->fecha}', '{$this->deposito}', '{$this->area}',
                                  '{$this->signo}', '{$this->articulo}', '{$this->calle}', '{$this->rack}',
                                  '{$this->pos}', '{$this->nro_pallet}','{$this->nro_lote}','{$this->serie}',
                                  '{$this->bultos}','{$this->unidades}','{$this->volumen}','{$this->otros}',
                                  '{$this->estado}','{$this->usuario}', '{$this->fecha_proc}','{$this->fecha_stk}' ) ";
$resultado=@odbc_exec($this->conexion,$p_sql_st);
if (!$resultado) {
   $this->error     = 2;
   $this->msg_error = "09- Error. No se pudo insertar [mov_stock] !! ";
   return;
}
if (odbc_num_rows($resultado) != 1){
   $this->error     = 1;
   $this->msg_error = "10- Error. No se pudo insertar [mov_stock] !! ";
   return;
}
odbc_free_result($resultado);

return;
}

function r_delete_stock_dep() 
{

$this->error     = 0;
$this->msg_error = "";
$p_sql_st="delete from stock_dep
             where empresa  = '{$this->empresa}'
               and deposito = '{$this->deposito}' 
               and area     = '{$this->area}'
               and articulo = '{$this->articulo}'
               and calle    = '{$this->calle}'
               and rack     = '{$this->rack}'
               and pos      = '{$this->pos}'
               and nro_pallet = '{$this->nro_pallet}'
               and nro_lote   = '{$this->nro_lote}'
               and estado     = '{$this->estado}'
               and serie      = '{$this->serie}' ";
$resultado=@odbc_exec($this->conexion,$p_sql_st);
if (!$resultado) {
  $this->error     = 2;
  $this->msg_error = "11- Error. No se pudo borrar [stock_dep] !! ";
  return;
}
if (odbc_num_rows($resultado) != 1){
  $this->error     = 1;
  $this->msg_error = "12- Error. No se pudo borrar [stock_dep] !! ";
  return;  
}
odbc_free_result($resultado);

return;
}

function r_upd_stock_dep_mas()
{

$this->error     = 0;
$this->msg_error = "";
$this->volumen = sprintf("%0.6f",$this->volumen);
$this->otros   = sprintf("%0.6f",$this->otros);
$p_sql_st="select * from stock_dep
             where empresa  = '{$this->empresa}'
               and deposito = '{$this->deposito}'
               and area     = '{$this->area}'
               and articulo = '{$this->articulo}'
               and calle    = '{$this->calle}'
               and rack     = '{$this->rack}'
               and pos      = '{$this->pos}'
               and nro_pallet = '{$this->nro_pallet}'
               and nro_lote   = '{$this->nro_lote}'
               and estado   = '{$this->estado}'
               and serie    = '{$this->serie}' ";
$resultado=@odbc_exec($this->conexion, $p_sql_st);
if (!$resultado){
  $this->error     = 2;
  $this->msg_error = "14- Error. No se pudo leer [stock_dep] !! ";
  return;
}
$row=odbc_fetch_array($resultado);
if (!$row){
   $this->r_fecha();

   $p_sql_st="insert into stock_dep values ( '{$this->empresa}','{$this->deposito}','{$this->area}','{$this->articulo}',
                                       '{$this->calle}', '{$this->rack}','{$this->pos}', 
                                       '{$this->bultos}','{$this->unidades}','{$this->volumen}','{$this->otros}',
                                       '{$this->estado}','{$this->nro_pallet}','{$this->nro_lote}',
                                       '{$this->serie}','{$this->fecha_stk}'
                                      ) ";
}
else{
  $p_sql_st="update stock_dep set bultos    = bultos   + '{$this->bultos}',
                                   unidades  = unidades + '{$this->unidades}',
                                   volumen   = volumen  + '{$this->volumen}',
                                   otros     = otros    + '{$this->otros}'
               where empresa  = '{$this->empresa}'
                 and deposito = '{$this->deposito}'
                 and area     = '{$this->area}'
                 and articulo = '{$this->articulo}'
                 and calle    = '{$this->calle}'
                 and rack     = '{$this->rack}'
                 and pos      = '{$this->pos}'
                 and nro_pallet = '{$this->nro_pallet}' 
                 and nro_lote   = '{$this->nro_lote}'
                 and estado     = '{$this->estado}'
                 and serie      = '{$this->serie}' ";
}
$resultado2=@odbc_exec($this->conexion, $p_sql_st);
if (!$resultado2){
  $this->error     = 2;
  $this->msg_error = "15- Error. No se pudo actualizar [stock_dep] !! ";
  return;
}
if (odbc_num_rows($resultado2) != 1){
  $this->error     = 1;
  $this->msg_error = "16- Error. No se pudo actualizar [stock_dep] !! ";
  return;
}
odbc_free_result($resultado2);
odbc_free_result($resultado);

$p_sql_st="select * from rack_dep
       where calle = '{$this->calle}'
         and rack  = '{$this->rack}'
         and pos   = '{$this->pos}'
         and c_distri = '{$this->deposito}'
         and tipo_pos in ( 'P','N') ";
$resultado=@odbc_exec($this->conexion,$p_sql_st);
if (!$resultado){
  $this->error     = 2;
  $this->msg_error = "17- Error. No se pudo leer [rack_dep] !!! ";
  return;
}
$row=odbc_fetch_array($resultado);
if ($row){
  $p_sql_st=" update rack_dep set estado_stk = 'O' 
          where calle = '{$this->calle}'
            and rack  = '{$this->rack}'
            and pos   = '{$this->pos}'
            and c_distri = '{$this->deposito}' ";
  $resultado2=@odbc_exec($this->conexion, $p_sql_st);
  if (!$resultado2){
    $this->error     = 2;
    $this->msg_error = "18- Error. No se pudo actualizar [rack_dep] !! ";
    return;
  }
  if (odbc_num_rows($resultado2) != 1){
    $this->error     = 1;
    $this->msg_error = "19- Error. No se pudo actualizar [rack_dep] !! ";
    return;
  }
  odbc_free_result($resultado2);
}
odbc_free_result($resultado);

$this->r_upd_mov_stock();

return;

}

function r_upd_stock_dep_menos()
{

$this->error     = 0;
$this->msg_error = "";

$p_estado_aux = $this->estado;
$p_sql_st="select sum(bultos) bultos,sum(unidades) unidades,sum(volumen) volumen,sum(otros) otros
              from stock_dep
             where empresa  = '{$this->empresa}'
               and deposito = '{$this->deposito}'
               and area     = '{$this->area}'
               and articulo = '{$this->articulo}'
               and calle    = '{$this->calle}'
               and rack     = '{$this->rack}'
               and pos      = '{$this->pos}'
               and nro_lote   = '{$this->nro_lote}'
               and nro_pallet = '{$this->nro_pallet}' ";
if ($p_estado_aux != 'L'){
   $p_sql_st.="and estado   = '{$this->estado}' ";
}
$p_sql_st.="and serie    = '{$this->serie}'";
$resultado=@odbc_exec($this->conexion, $p_sql_st);
if(!$resultado){
  $this->error     = 2;
  $this->msg_error = "20- Error. No se pudo leer [stock_dep] !! ";
  return;
}
$row=odbc_fetch_array($resultado);
if (!$row){
  $this->error     = 1;
  $this->msg_error = "21- Error. No se pudo leer [stock_dep] !! ";
  return;
}
$p_bultos   = $row['bultos'];
$p_unidades = $row['unidades'];
$p_volumen  = $row['volumen'];
$p_otros    = $row['otros'];
odbc_free_result($resultado);

$p_bul_aux = $this->bultos   * -1;
$p_uns_aux = $this->unidades * -1;
$p_vol_aux = $this->volumen  * -1;
$p_otr_aux = $this->otros    * -1;
$p_volumen = sprintf("%0.6f",$p_volumen);
$p_volumen = $p_volumen + 0;
$p_vol_aux = sprintf("%0.6f",$p_vol_aux);
$p_vol_aux = $p_vol_aux + 0;
$p_otros   = sprintf("%0.6f",$p_otros);
$p_otros   = $p_otros + 0;
$p_otr_aux = sprintf("%0.6f",$p_otr_aux);
$p_otr_aux = $p_otr_aux + 0;
if ($p_bultos < $p_bul_aux) {
   $this->error     = 1;
   $this->msg_error = "22- Error. Bultos insuficientes !! ";
   return;
}
if ($p_unidades < $p_uns_aux) {
   $this->error     = 1;
   $this->msg_error = "23- Error. Unidades insuficientes !! ";
   return;
}
if ($p_volumen < $p_vol_aux) {
   $this->error     = 1;
   $this->msg_error = "24- Error. Volumen insuficientes !! ";
   return;
}
if ($p_otros < $p_otr_aux) {
   $this->error     = 1;
   $this->msg_error = "25- Error. Otros insuficientes !! ";
   return;
}
if ($this->grupo_deposito != 0){
   $p_bul = $this->r_veo_logico();
   if ($this->error != 0){
      return;
   }
   if ($p_bul < $p_bul_aux){
      $this->error     = 1;
      $this->msg_error = "26- Error. Bultos en reserva insuficientes !! ";
      return;
   }
}

$this->volumen = sprintf("%0.6f",$this->volumen);
$this->otros = sprintf("%0.6f",$this->otros);
$p_sql_st="select *  from stock_dep
           where empresa  = '{$this->empresa}'
             and deposito = '{$this->deposito}'
             and area     = '{$this->area}'
             and articulo = '{$this->articulo}'
             and calle    = '{$this->calle}'
             and rack     = '{$this->rack}'
             and pos      = '{$this->pos}'
             and nro_lote   = '{$this->nro_lote}'
             and nro_pallet = '{$this->nro_pallet}' 
             and estado   = '{$this->estado}' 
             and serie    = '{$this->serie}'";
$resultado=@odbc_exec($this->conexion,$p_sql_st);
if(!$resultado){
   $this->error     = 2;
   $this->msg_error = "27- Error. No se pudo leer [stock_dep] !! ";
   return;
}
$row=odbc_fetch_array($resultado);
if ($row) {
   $p_sql_st = "update stock_dep set bultos   = bultos   + '{$this->bultos}',
                                unidades = unidades + '{$this->unidades}',
                                volumen  = volumen  + '{$this->volumen}',
                                otros    = otros    + '{$this->otros}'
        where empresa  = '{$this->empresa}'
          and deposito = '{$this->deposito}'
          and area     = '{$this->area}'
          and articulo = '{$this->articulo}'
          and calle    = '{$this->calle}'
          and rack     = '{$this->rack}'
          and pos      = '{$this->pos}'
          and nro_pallet = '{$this->nro_pallet}'
          and nro_lote   = '{$this->nro_lote}'
          and estado   = '{$this->estado}'
          and serie    = '{$this->serie}' ";
}
else{
   $this->r_fecha();
   $p_sql_st="insert into stock_dep values ( '{$this->empresa}','{$this->deposito}','{$this->area}','{$this->articulo}',
                                       '{$this->calle}', '{$this->rack}','{$this->pos}', 
                                       '{$this->bultos}','{$this->unidades}','{$this->volumen}','{$this->otros}',
                                       '{$this->estado}','{$this->nro_pallet}','{$this->nro_lote}','{$this->serie}','{$this->fecha_stk}'
                                      ) ";
}
$resultado=@odbc_exec($this->conexion, $p_sql_st);
if (!$resultado){
  $this->error     = 2;
  $this->msg_error = "28- Error. No se pudo actualizar [stock_dep] !! ";
  return;
}      
if (odbc_num_rows($resultado) != 1){
  $this->error     = 1;
  $this->msg_error = "29- Error. No se pudo actualizar [stock_dep] !! ";
  return;
}
odbc_free_result($resultado);

$this->r_upd_mov_stock();

$p_sql_st="select * from rack_dep
       where calle = '{$this->calle}'
         and rack  = '{$this->rack}'
         and pos   = '{$this->pos}'
         and c_distri = '{$this->deposito}'
         and tipo_pos in ( 'P','N') ";
$resultado=@odbc_exec($this->conexion, $p_sql_st);
if (!$resultado){
  $this->error     = 2;
  $this->msg_error = "30- Error. No se pudo leer [rack_dep] !! ";
  return;
}      
$row=odbc_fetch_array($resultado);
if (!$row){
  return;
}
odbc_free_result($resultado);

$p_saldo = 0;
$p_sql_st="select sum(bultos) saldo 
       from stock_dep
      where deposito = '{$this->deposito}'
        and calle    = '{$this->calle}'
        and rack     = '{$this->rack}'
        and pos      = '{$this->pos}' ";
$resultado=@odbc_exec($this->conexion, $p_sql_st);
if (!$resultado){
  $this->error     = 2;
  $this->msg_error = "31- Error. No se pudo leer [stock_dep] !! ";
  return;
}
$row=odbc_fetch_array($resultado);
if (!$row){
  $p_saldo = 0;
}
else{
  $p_saldo = $row['saldo'];
}
odbc_free_result($resultado);

if ($p_saldo <= 0 ){ 
# Solo bloqueo las pos. de picking y normales.
  $p_sql_st="select * from rack_dep
          where calle = '{$this->calle}'
            and rack  = '{$this->rack}'
            and pos   = '{$this->pos}'
            and c_distri = '{$this->deposito}'
            and tipo_pos in ( 'P','N') ";
  $resultado=@odbc_exec($this->conexion, $p_sql_st);
  if (!$resultado){
    $this->error     = 2;
    $this->msg_error = "32- Error. No se pudo leer [rack_dep] !! ";
    return;
  }
  $row=odbc_fetch_array($resultado);
  if ($row){
    $p_sql_st="update rack_dep set estado_stk = 'L' 
          where calle = '{$this->calle}'
            and rack  = '{$this->rack}'
            and pos   = '{$this->pos}'
            and c_distri = '{$this->deposito}' ";
    $resultado2=@odbc_exec($this->conexion, $p_sql_st);
    if (!$resultado2){
      $this->error     = 2;
      $this->msg_error = "33- Error. No se pudo actualizar [rack_dep] !! ";
      return;
    }
    if (odbc_num_rows($resultado2) != 1){
      $this->error     = 1;
      $this->msg_error = "34- Error. No se pudo actualizar [rack_dep] !! ";
      return;
    }
    odbc_free_result($resultado2);
  }
  odbc_free_result($resultado);
}

}

function r_upd_mov_stock()
{

if ($this->con_movim == 'N'){
  return;
}

$p_bultos   = $this->bultos;
$p_unidades = $this->unidades;
$p_volumen  = $this->volumen;
$p_otros    = $this->otros;

if ($this->signo == "-" && $p_bultos > 0){
  $p_bultos   = $p_bultos   * -1;
  $p_unidades = $p_unidades * -1;
  $p_volumen  = $p_volumen  * -1;
  $p_otros    = $p_otros    * -1;
}

$this->fecha      = date("Ymd",strtotime($this->fecha));
$this->fecha_proc = date("Ymd H:i:s",strtotime($this->fecha_proc));
$this->r_fecha();


$p_volumen        = sprintf("%0.6f",$p_volumen);
$p_otros          = sprintf("%0.6f",$p_otros);

$p_sql_st="select * from mov_stock
              where empresa = '{$this->empresa}'
                and tdoc    = '{$this->tdoc}'
                and letdoc  = '{$this->letdoc}'
                and sucdoc  = '{$this->sucdoc}'
                and nrodoc  = '{$this->nrodoc}'
                and fecha   = '{$this->fecha}'
                and deposito = '{$this->deposito}'
                and area     = '{$this->area}'
                and signo    = '{$this->signo}'
                and articulo = '{$this->articulo}'
                and calle    = '{$this->calle}'
                and rack     = '{$this->rack}'
                and pos      = '{$this->pos}'
                and nro_pallet = '{$this->nro_pallet}'
                and serie    = '{$this->serie}' ";
$resultado=@odbc_exec($this->conexion, $p_sql_st);
if (!$resultado){
  $this->error     = 2;
  $this->msg_error = "35- Error. No se pudo leer [mov_stock] !! ";
  return;  
}
$row=odbc_fetch_array($resultado);
if (!$row){
  $p_sql_st = "insert into mov_stock values ('{$this->empresa}' ,'{$this->tdoc}'      ,'{$this->letdoc}',
                                        '{$this->sucdoc}'  ,'{$this->nrodoc}'    ,'{$this->fecha}',
                                        '{$this->deposito}','{$this->area}'      ,'{$this->signo}',
                                        '{$this->articulo}','{$this->calle}'     ,'{$this->rack}',
                                        '{$this->pos}'     ,'{$this->nro_pallet}','{$this->nro_lote}',
                                        '{$this->serie}'   ,'{$p_bultos}'        ,'{$p_unidades}',
                                        '{$p_volumen}'     ,'{$p_otros}'         ,'{$this->estado}',
                                        '{$this->usuario}' ,'{$this->fecha_proc}','{$this->fecha_stk}'
                                      )";
}
else{
  $p_sql_st="update mov_stock set bultos   = bultos   + '{$p_bultos}',
                             unidades = unidades + '{$p_unidades}',
                             volumen  = volumen  + '{$p_volumen}',
                             otros    = otros    + '{$p_otros}'
        where empresa    = '{$this->empresa}'
          and tdoc       = '{$this->tdoc}'
          and letdoc     = '{$this->letdoc}'
          and sucdoc     = '{$this->sucdoc}'
          and nrodoc     = '{$this->nrodoc}'
          and fecha      = '{$this->fecha}'
          and deposito   = '{$this->deposito}'
          and area       = '{$this->area}'
          and signo      = '{$this->signo}'
          and articulo   = '{$this->articulo}'
          and calle      = '{$this->calle}'
          and rack       = '{$this->rack}'
          and pos        = '{$this->pos}'
          and nro_pallet = '{$this->nro_pallet}'
          and serie      = '{$this->serie}' ";
}
$resultado=@odbc_exec($this->conexion, $p_sql_st);
if (!$resultado){
  $this->error     = 1;
  $this->msg_error = "36- Error. No se pudo actualizar [mov_stock] !! ";
  return;  
}
if (odbc_num_rows($resultado) != 1){
  $this->error     = 1;
  $this->msg_error = "37- Error. No se pudo actualizar [mov_stock] !!";
  return;  
}

return;

}

function r_nro_pallet_multicodigo()
{
$this->error     = 0;
$this->msg_error = "";

$this->cantidad_articulo_pallet = 0;
$this->articulo = "";
$p_sql_st="select articulo from stock_dep
       where nro_pallet = '{$this->nro_pallet}'
         and empresa    = '{$this->empresa}'
         and deposito   = '{$this->deposito}'
         and area       = '{$this->area}'
         and ( bultos != 0 or otros != 0 )
       group by articulo ";
$resultado=@odbc_exec($this->conexion, $p_sql_st);
if (!$resultado){
  $this->error     = 2;
  $this->msg_error = "38- Error. No se pudo leer [stock_dep] !! ";
  return;  
}
while ($row=odbc_fetch_array($resultado)){
  $this->articulo = rtrim($row['articulo']);
  $this->cantidad_articulo_pallet++;
}
odbc_free_result($resultado);

return;

}

function r_ctrl_inventario()
{

$this->error     = 0;
$this->msg_error = "";
$p_sql_st="select * 
        from inv_cabe
       where empresa = '{$this->empresa}'
         and (estado  = 'A' or estado = 'S' )
         and tipo    = 'R' ";
$resultado=@odbc_exec($this->conexion, $p_sql_st);
if (!$resultado){
  $this->error     = 1;
  $this->msg_error = "39- Error. No se pudo leer [inv_cabe] !! ";
  return;  
}
$row=odbc_fetch_array($resultado);
if (!$row){
  return;
}
$p_nro_inventario = $row['nro_inventario'];
$p_seleccion = $row['seleccion'];
odbc_free_result($resultado);

switch ($p_seleccion) {
  case 'A':
    $p_sql_st="select * from inv_seleccion
           where empresa  = '{$this->empresa}'
             and nro_inventario = '$p_nro_inventario'
             and articulo = '{$this->articulo}' 
             and estado  != 99 ";
    $resultado=@odbc_exec($this->conexion, $p_sql_st);
    if (!$resultado){
      $this->error     = 2;
      $this->msg_error = "40- Error. No se pudo leer [inv_seleccion] !! ";
      return;  
    }
    $row=odbc_fetch_array($resultado);
    if ($row){
      $this->error     = 1;
      $this->msg_error = "41- Error. Articulo seleccionado en Inventario !! ";
      return;  
    }
    odbc_free_result($resultado);
    break;
  case 'F':  
    $p_sql_st="select * from inv_seleccion
           where empresa  = '{$this->empresa}'
             and nro_inventario = '$p_nro_inventario'
             and familia  = '{$this->familia}'
             and estado  != 99";
    $resultado=@odbc_exec($this->conexion, $p_sql_st);
    if (!$resultado){
      $this->error     = 2;
      $this->msg_error = "42- Error. No se pudo leer [inv_seleccion] !! ";
      return;  
    }
    $row=odbc_fetch_array($resultado);
    if ($row){
      $this->error     = 1;
      $this->msg_error = "43- Error. Articulo/Familia seleccionado en Inventario !! ";
      return;  
    }
    odbc_free_result($resultado);
    break;
  case 'P':
    $p_sql_st="select * from inv_seleccion
           where empresa  = '{$this->empresa}'
             and nro_inventario = '$p_nro_inventario'
             and posicion = '{$this->posicion}' 
             and estado != 99";
    $resultado=@odbc_exec($this->conexion, $p_sql_st);
    if (!$resultado){
      $this->error     = 2;
      $this->msg_error = "44- Error. No se pudo leer [inv_seleccion] !! ";
      return;  
    }
    $row=odbc_fetch_array($resultado);
    if ($row){
      $this->error     = 1;
      $this->msg_error = "45- Error. Posicion seleccionada en Inventario !! ";
      return;  
    }
    odbc_free_result($resultado);
    break;
  case 'G':
    $p_sql_st="select * from inv_seleccion
           where empresa  = '{$this->empresa}'
             and nro_inventario = '$p_nro_inventario'
             and grupo    = '{$this->grupo}' 
             and estado != 99";
    $resultado=@odbc_exec($this->conexion, $p_sql_st);
    if (!$resultado){
      $this->error     = 2;
      $this->msg_error = "46- Error. No se pudo leer [inv_seleccion] !! ";
      return;  
    }
    $row=odbc_fetch_array($resultado);
    if ($row){
      $this->error     = 1;
      $this->msg_error = "47- Error. Grupo seleccionado en Inventario !! ";
      return;  
    }
    odbc_free_result($resultado);
    break;
  default:
      $this->error     = 1;
      $this->msg_error = "48- Error. Tipo de Inventario desconocido !! ";
      return;  
    break;
}

return;
}

function r_mov_stock_repos()
{

$this->fecha      = date("Ymd",strtotime($this->fecha));
$this->fecha_proc = date("Ymd H:i:s",strtotime($this->fecha_proc));
$this->volumen    = sprintf("%0.6f",$this->volumen);
$this->otros      = sprintf("%0.6f",$this->otros);
$p_sql_st = "insert into mov_stock_repos values ('{$this->empresa}'   ,'{$this->tdoc}'     ,'{$this->letdoc}',
                                            '{$this->sucdoc}'    ,'{$this->nrodoc}'   ,'{$this->fecha}',
                                            '{$this->deposito}'  ,'{$this->area}'     ,'{$this->articulo}',
                                            '{$this->calle}'     ,'{$this->rack}'     ,'{$this->pos}',
                                            '{$this->nro_pallet}','{$this->nro_lote}' ,'{$this->serie}',
                                            '{$this->calle_dest}','{$this->rack_dest}','{$this->pos_dest}',
                                            '{$this->nro_pallet_dest}',
                                            '{$this->bultos}'    ,'{$this->unidades}','{$this->volumen}' ,
                                            '{$this->otros}'     ,'{$this->estado}'  ,'{$this->usuario}' ,'{$this->fecha_proc}'
                                           )";
$resultado=@odbc_exec($this->conexion, $p_sql_st);
if (!$resultado){
  $this->error     = 2;
  $this->msg_error = "49- Error. No se pudo actualizar [mov_stock_repos] !! ";
  return;  
}
if (odbc_num_rows($resultado) != 1){
  $this->error     = 1;
  $this->msg_error = "50- Error. No se pudo actualizar [mov_stock_repos] !! ";
  return;  
}
return;
}

// Lo uso solo para borrar registros leidos
function r_resta_mov_stock()
{
$this->msg_error = '';
$this->error     = 0;

$p_bultos   = $this->bultos;
$p_unidades = $this->unidades;
$p_volumen  = $this->volumen;
$p_otros    = $this->otros;
$p_volumen  = sprintf("%0.6f",$p_volumen);
$p_otros    = sprintf("%0.6f",$p_otros);

$p_sql_st="select * from mov_stock
           where empresa = '{$this->empresa}'
             and tdoc    = '{$this->tdoc}'
             and letdoc  = '{$this->letdoc}'
             and sucdoc  = '{$this->sucdoc}'
             and nrodoc  = '{$this->nrodoc}'
             and fecha   = '{$this->fecha}'
             and deposito = '{$this->deposito}'
             and area     = '{$this->area}'
             and signo    = '{$this->signo}'
             and articulo = '{$this->articulo}'
             and calle    = '{$this->calle}'
             and rack     = '{$this->rack}'
             and pos      = '{$this->pos}'
             and nro_pallet = '{$this->nro_pallet}'
             and serie    = '{$this->serie}' ";
$resultado=@odbc_exec($this->conexion, $p_sql_st);
if (!$resultado){
   $this->error     = 2;
   $this->msg_error = "51- Error. No se pudo leer [mov_stock] !! ";
   return;
}
$row=odbc_fetch_array($resultado);
if ($row){
   $p_sql_st="update mov_stock set bultos   = bultos   + '{$p_bultos}',
                              unidades = unidades + '{$p_unidades}',
                              volumen  = volumen  + '{$p_volumen}',
                              otros    = otros    + '{$p_otros}'
     where empresa    = '{$this->empresa}'
       and tdoc       = '{$this->tdoc}'
       and letdoc     = '{$this->letdoc}'
       and sucdoc     = '{$this->sucdoc}'
       and nrodoc     = '{$this->nrodoc}'
       and fecha      = '{$this->fecha}'
       and deposito   = '{$this->deposito}'
       and area       = '{$this->area}'
       and signo      = '{$this->signo}'
       and articulo   = '{$this->articulo}'
       and calle      = '{$this->calle}'
       and rack       = '{$this->rack}'
       and pos        = '{$this->pos}'
       and nro_pallet = '{$this->nro_pallet}'
       and serie      = '{$this->serie}' ";
   $resultado=@odbc_exec($this->conexion, $p_sql_st);
   if (!$resultado){
      $this->error     = 1;
      $this->msg_error = "52- Error. No se pudo actualizar [mov_stock] !! ";
      return;
   }
   if (odbc_num_rows($resultado) != 1){
      $this->error     = 1;
      $this->msg_error = "53- Error. No se pudo actualizar [mov_stock] !!";
      return;
   }
}
return;

}
// solo registros leidos sin confirmar
function r_suma_mov_stock_sin_confirmar()
{
$this->msg_error = '';
$this->error     = 0;

$p_bultos   = $this->bultos;
$p_unidades = $this->unidades;
$p_volumen  = $this->volumen;
$p_otros    = $this->otros;

//and fecha   = '{$this->fecha}'

$p_sql_st="select * from mov_stock
            where empresa = '{$this->empresa}'
            and tdoc    = '{$this->tdoc}'
            and letdoc  = '{$this->letdoc}'
            and sucdoc  = '{$this->sucdoc}'
            and nrodoc  = '{$this->nrodoc}'
            and deposito = '{$this->deposito}'
            and area     = '{$this->area}'
            and signo    = '{$this->signo}'
            and articulo = '{$this->articulo}'
            and calle    = '{$this->calle}'
            and rack     = '{$this->rack}'
            and pos      = '{$this->pos}'
            and nro_pallet = '{$this->nro_pallet}'
            and serie    = '{$this->serie}'
            and estado     = 'L' "; // solo para L porque los cerea para borrar
$resultado=@odbc_exec($this->conexion, $p_sql_st);
if (!$resultado){
   $this->error     = 2;
   $this->msg_error = "54- Error. No se pudo leer [mov_stock] !! ";
   return;
}
$row=odbc_fetch_array($resultado);
if ($row){
   $p_sql_st="update mov_stock set bultos   = bultos   + '{$p_bultos}',
                              unidades = unidades + '{$p_unidades}',
                              volumen  = volumen  + '{$p_volumen}',
                              otros    = otros    + '{$p_otros}'
               where empresa    = '{$this->empresa}'
                and tdoc       = '{$this->tdoc}'
                and letdoc     = '{$this->letdoc}'
                and sucdoc     = '{$this->sucdoc}'
                and nrodoc     = '{$this->nrodoc}'
                and deposito   = '{$this->deposito}'
                and area       = '{$this->area}'
                and signo      = '{$this->signo}'
                and articulo   = '{$this->articulo}'
                and calle      = '{$this->calle}'
                and rack       = '{$this->rack}'
                and pos        = '{$this->pos}'
                and nro_pallet = '{$this->nro_pallet}'
                and serie      = '{$this->serie}'
                and estado     = 'L' "; // solo para L porque los cerea para borrar
   $resultado=@odbc_exec($this->conexion, $p_sql_st);
   if (!$resultado){
      $this->error     = 1;
      $this->msg_error = "55- Error. No se pudo actualizar [mov_stock] !! ";
      return;
   }
   if (odbc_num_rows($resultado) <= 0 ){ //
      $this->error     = 1;
      $this->msg_error = "56- Error. No se pudo actualizar [mov_stock] !!";
      return;
   }
}
return;

}

function r_insert_mov_stock_solo()
{
$this->error = 0;
$this->msg_error = '';

$this->fecha      = date("Ymd",strtotime($this->fecha));
$this->fecha_proc = date("Ymd H:i:s",strtotime($this->fecha_proc));
$this->r_fecha();
$this->volumen    = sprintf("%0.6f",$this->volumen);
$this->otros      = sprintf("%0.6f",$this->otros);

$p_sql_st="select * from mov_stock
         where empresa  = '{$this->empresa}'
           and tdoc     = '{$this->tdoc}'
           and nrodoc   = '{$this->nrodoc}'
           and fecha    = '{$this->fecha}'
           and deposito = '{$this->deposito}'
           and area     = '{$this->area}'
           and signo    = '{$this->signo}'
           and articulo = '{$this->articulo}'
           and calle    = '{$this->calle}'
           and rack     = '{$this->rack}'
           and pos      = '{$this->pos}'
           and nro_pallet = '{$this->nro_pallet}'
           and serie    = '{$this->serie}' ";
$this->r_select_fila($p_sql_st,"mov_stock");
if ($this->error == 0) {
    $this->error = 0;
    $this->msg_error = '';
    $p_sql_st = "update mov_stock set bultos   = bultos + '{$this->bultos}',
                                   unidades = unidades + '{$this->unidades}',
                                   volumen  = volumen + '{$this->volumen}',
                                   otros    = otros + '{$this->otros}' 
         where empresa  = '{$this->empresa}'
           and tdoc     = '{$this->tdoc}'
           and nrodoc   = '{$this->nrodoc}'
           and fecha    = '{$this->fecha}'
           and deposito = '{$this->deposito}'
           and area     = '{$this->area}'
           and signo    = '{$this->signo}'
           and articulo = '{$this->articulo}'
           and calle    = '{$this->calle}'
           and rack     = '{$this->rack}'
           and pos      = '{$this->pos}'
           and nro_pallet = '{$this->nro_pallet}'
           and serie    = '{$this->serie}' ";
}
else {
    $this->error = 0;
    $this->msg_error = '';
    $p_sql_st = "insert into mov_stock values ('{$this->empresa}' ,'{$this->tdoc}'      ,'{$this->letdoc}',
                                            '{$this->sucdoc}'  ,'{$this->nrodoc}'    ,'{$this->fecha}',
                                            '{$this->deposito}','{$this->area}'      ,'{$this->signo}',
                                            '{$this->articulo}','{$this->calle}'     ,'{$this->rack}',
                                            '{$this->pos}'     ,'{$this->nro_pallet}','{$this->nro_lote}',
                                            '{$this->serie}'   ,'{$this->bultos}'    ,'{$this->unidades}',
                                            '{$this->volumen}'     ,'{$this->otros}' ,'{$this->estado}',
                                            '{$this->usuario}' ,'{$this->fecha_proc}','{$this->fecha_stk}'
                                          )";
}
$resultado=@odbc_exec($this->conexion,$p_sql_st);
if (!$resultado){
   $this->error     = 2;
   $this->msg_error = "57- Error. No se pudo actualizar [mov_stock] !! ";
   return;
}
if (odbc_num_rows($resultado) != 1){
   $this->error     = 1;
   $this->msg_error = "58- Error. No se pudo actualizar [mov_stock] !!";
   return;
}
return;
}

//solo registros leidos sin confirmar
function r_upd_stock_dep_dia($r_fecha)
{
$this->msg_error = '';
$this->error     = 0;

if ($this->area == 7){
   return;
}

$r_fecha1 = $r_fecha;
$r_fecha1 = date("Ymd",strtotime($r_fecha));
$p_can = 0;
$p_sql_st="select * from mov_stock
       where empresa  = '{$this->empresa}'
         and tdoc     = '{$this->tdoc}'
         and nrodoc   = '{$this->nrodoc}'
         and articulo = '{$this->articulo}'
         and deposito = '{$this->deposito}'
         and area     = '{$this->area}'
         and calle    = '{$this->calle}'
         and rack     = '{$this->rack}'
         and pos      = '{$this->pos}'
         and nro_pallet = '{$this->nro_pallet}' ";
if ($this->tipo_base != 'I'){
   $p_sql_st.="and fecha_proc < '$r_fecha1' ";
}
else{
   $p_sql_st.="and date(fecha_proc) < '$r_fecha1' ";
}
$p_sql_st.=" order by fecha_proc ";
$resultado=@odbc_exec($this->conexion,$p_sql_st);
if (!$resultado){
   $this->error     = 2;
   $this->msg_error = "59- Error. No se pudo leer [mov_stock] !! ";
   return;
}
while($row=odbc_fetch_array($resultado)){
   $p_fecha_proc = $row['fecha_proc'];
   $p_can++;
   break;
}
odbc_free_result($resultado);
if ($p_can == 0 ) {
# Veo cuando se reubico
   $p_sql_st="select * from nrdeta_ubi
          where empresa  = '{$this->empresa}'
            and tdoc     = '{$this->tdoc}'
            and nrodoc   = '{$this->nrodoc}'
            and articulo = '{$this->articulo}'
            and bultos   = '{$this->bultos}'
            and estado   = 'R' ";
   $resultado=@odbc_exec($this->conexion,$p_sql_st);
   while ($row=odbc_fetch_array($resultado)){
      $p_np = trim($row['nro_pallet']);
      $p_sql_st="select * from mov_stock
             where nro_pallet = '$p_np'
               and empresa    = '{$this->empresa}'
               and tdoc       = '{$row['tdoc_reub']}'
               and nrodoc     = '{$row['nrodoc_reub']}'
               and articulo   = '{$this->articulo}'
               and deposito   = '{$this->deposito}'
               and area       = '{$this->area}'
               and date(fecha_proc) < '$r_fecha1'
             order by fecha_proc ";
      $resultado2=@odbc_exec($this->conexion,$p_sql_st);
      while($row2=odbc_fetch_array($resultado2)){
         $p_fecha_proc = $row2['fecha_proc'];
         $p_can++;
         break;
      }
      odbc_free_result($resultado2);
   }
   odbc_free_result($resultado);
   if ($p_can <= 0 ){
      return;
   }
}

$p_bul = $this->bultos;
$p_uns = $this->unidades;
$p_vol = $this->volumen;
$p_otr = $this->otros;
if ($this->signo == "+"){
   $p_bul = $this->bultos * -1;
   $p_uns = $this->unidades * -1;
   $p_vol = $this->volumen * -1;
   $p_otr = $this->otros * -1;
}

$p_fecha = date('d-m-Y', strtotime($p_fecha_proc));

while ($p_fecha < $r_fecha ){
   $p_fecha_upd = $p_fecha;
   if ($this->tipo_base == 'S'){
      $p_fecha_upd = date("Ymd",strtotime($p_fecha));
   }
   $p_sql_st="update stock_dep_dia set bultos   = bultos   + '$p_bul',
                                        unidades = unidades + '$p_uns',
                                        volumen  = volumen  + '$p_vol',
                                        otros    = otros    + '$p_otr'
                where empresa  = '{$this->empresa}'
                  and fecha    = '$p_fecha_upd'
                  and articulo = '{$this->articulo}'
                  and deposito = '{$this->deposito}'
                  and area     = '{$this->area}'
                  and calle    = '{$this->calle}'
                  and rack     = '{$this->rack}'
                  and pos      = '{$this->pos}'
                  and nro_pallet = '{$this->nro_pallet}'
                  and estado   = '{$this->estado}'"; // # L
   $this->r_exec($p_sql_st,"stock_dep_dia",1);
   if ($this->error != 0){
      if ($this->signo == "+" ) {
         $p_sql_st="insert into stock_dep_dia values ( '{$this->empresa}','{$this->deposito}' ,'{$this->area}','{$this->articulo}',
                                                        '{$this->calle}','{$this->rack}','{$this->pos}',
                                                        '{$this->nro_pallet}','{$this->nro_lote}','$p_fecha_upd',
                                                        '{$this->bultos}','{$this->unidades}','{$this->volumen}','{$this->otros}',
                                                        '{$this->estado}','{$this->fecha_stk}' )";
         $resultado=@odbc_exec($this->conexion,$p_sql_st);
         if (!$resultado) {
            $this->error     = 2;
            $this->msg_error = "60- Error. No se pudo insertar [stock_dep_dia] !! ";
            break;
         }
         if (odbc_num_rows($resultado) != 1){
            $this->error     = 1;
            $this->msg_error = "61- Error. No se pudo insertar [stock_dep_dia] !! ";
            break;
         }
         odbc_free_result($resultado);
      }
      else{
         $this->error = 1;
         $this->msg_error = "62- Error. No se pudo actualizar [stock_dep_dia] !!";
         break;
      }
   }

   $p_fecha = date('d-m-Y', strtotime($p_fecha . "+1 days"));

}

return;

}

function r_veo_logico()
{
$p_bul = 0;
$p_sql_st="select sum(bultos-bultos_res) bultos from stock
       where empresa = '{$this->empresa}'
         and grupo_deposito = '{$this->grupo_deposito}'
         and area    = '{$this->area}'
         and articulo = '{$this->articulo}'
         and nro_lote = '{$this->nro_lote}'
         and estado   = 'C' ";
$this->r_select_fila($p_sql_st,"stock");
if ($this->error == 2){
   $this->error = 2;
   $this->msg_error = '63- Error. No se pudo leer [stock] !! ';
   return $p_bul;
}
$p_bul = $this->row['bultos'];
if (strlen($p_bul) <= 0) $p_bul = 0;

return $p_bul;

}

function r_fecha(){

   if ( date("Y",strtotime($this->fecha_stk)) > 1970 ){
      $this->fecha_stk = date("Ymd",strtotime($this->fecha_stk));
   }
   else{
      $this->fecha_stk = '19000101';
   }
}

static function r_tipo_area($r_area)
{
global $Base;

$pa_return['error'] = 'OK';
$pa_return['msg_error'] = '';
$r_tipo_area     = '';

$p_sql_st = "select tipo_area from areas where area = '$r_area' ";
$resultado=@odbc_exec($Base->conexion, $p_sql_st);
if (!$resultado){
   $pa_return['error'] = 'NOK';
   $pa_return['msg']   = '65- Error. No se pudo leer [areas] !! ';
   return $r_tipo_area;
}
$row=odbc_fetch_array($resultado);
if (!$row){
   $pa_return['error'] = 'NOK';
   $pa_return['msg']   = '66- Error. No se pudo leer [areas] !! ';
   return $r_tipo_area;
}
$r_tipo_area = trim($row['tipo_area']);

return $r_tipo_area;
}

static function r_area_serie($r_area)
{
global $Base;

$pa_return['error'] = 'OK';
$pa_return['msg_error'] = '';
$r_lee_serie       = '';

$p_sql_st = "select lee_serie from areas where area = '$r_area' ";
$resultado=@odbc_exec($Base->conexion, $p_sql_st);
if (!$resultado){
   $pa_return['error'] = 'NOK';
   $pa_return['msg']   = '67- Error. No se pudo leer [areas] !! ';
   return $r_lee_serie;
}
$row=odbc_fetch_array($resultado);
if (!$row){
   $pa_return['error'] = 'NOK';
   $pa_return['msg']   = '68- Error. No se pudo leer [areas] !! ';
   return $r_lee_serie;
}
$r_lee_serie = trim($row['lee_serie']);

return $r_lee_serie;
}

static function r_ctrl_intentario_seleccion($r_empresa,$r_tipo_inventario,$r_nro_inventario,
                                            $r_articulo,$r_grupo,$r_familia,$r_posicion,$r_area)
{
global $Base;

$pa_return['error']     = 'OK';
$pa_return['msg_error'] = '';
$p_item_des      = '';

switch ($r_tipo_inventario){
   case 'A':
      $p_codigo = $r_articulo;
      $p_item_des = $r_articulo;
      $p_sql_st_d = " and articulo = '$r_articulo' ";
      break;
   case 'F':
      $p_codigo   = $r_familia;
      $p_item_des = $r_familia;
      $p_sql_st_d = " and familia = '$r_familia' ";
      break;
   case 'P':
      $p_codigo   = $r_posicion;
      $p_item_des = $r_posicion;
      $p_sql_st_d = " and posicion = '$r_posicion' ";
      break;
   case 'G':
      $p_codigo   = $r_grupo;
      $p_item_des = $r_grupo;
      $p_sql_st_d = " and grupo = '$r_grupo' ";
      break;
   case 'R':
      $p_codigo   = $r_area;
      $p_item_des = $r_area;
      $p_sql_st_d = " and area = '$r_area' ";
      break;
   default:
      $pa_return['error'] = 'NOK';
      $pa_return['msg']   = '69- Error. Tipo de Inventario inexistente !! '.$r_tipo_inventario;
      break;
}

if ($pa_return['error'] != 'OK'){
   return $pa_return;
}
$p_sql_st="select * from inv_seleccion
       where empresa  = '$r_empresa'
         and nro_inventario = '$r_nro_inventario' ".$p_sql_st_d.
       " and estado   != 99 ";
$resultado=@odbc_exec($Base->conexion,$p_sql_st);
if (!$resultado){
   $pa_return['error'] = 'NOK';
   $pa_return['msg']   = '70- Error. No se pudo leer [inv_seleccion] !! ';
}
$row=odbc_fetch_array($resultado);
if ($row){
   $pa_return['error'] = 'NOK';
   $pa_return['msg']   = '71- Item incluido en Inventario !! '.$p_item_des;
}

return $pa_return;

}

/*
# Clasificacion de posiciones: campo rack.tipo_pos.
# P picking
# N comunes
# I solo ingreso ( Stage In )
# R rechazos ( X9696 )
# X posicion no valida.  0-0-0 la busco para area 7.
# S area re despacho ( X9797 )
# Parametro r_tipo.
#    A: abastecimiento: abastece zona de Picking segun rack_grupos y si no
#                       hay mas sigue con pos 2
#    S: almacenaje: almacena de lo sugerido en rack_grupos niveles 2 para arriba
#    a: reporocesos.
#-------------------------------------------------------------------------------
*/
static function r_busco_ubicacion($r_empresa, $r_deposito, $r_articulo,$r_grupo, $r_subgrupo, $r_familia,
                                  $r_tipo,$r_nro_lote = 'Z')
{
global $Base;

$pa_ubi['error'] = 'OK';
$pa_ubi['msg']   = '';
$pa_ubi['calle'] = '';
$pa_ubi['rack']  = 0;
$pa_ubi['pos']   = 0;
$p_hay    = false;

if ($r_tipo == "S"){
   $p_can = 0;
   $p_sql_st="select count(*) can from rack_grupos
               where empresa = '$r_empresa'
                 and operacion  = '$r_tipo' 
                 and estado = 1 ";
   $Base->r_select_fila($p_sql_st,"rack_grupos");
   if ($Base->error == 0){
      if (strlen($Base->row['can']) > 0)  $p_can = $Base->row['can'];
   }
   if ($p_can <= 0 ) {
      return $pa_ubi;
   }
}
if ($r_tipo == 'a'){
   $p_can_fijas = 0;
   $p_sql_st="select count(*) can from rack_grupos
               where empresa = '$r_empresa'
                 and operacion  = 'S' 
                 and estado = 1 ";
   $Base->r_select_fila($p_sql_st,"rack_grupos");
   if ($Base->error == 0){
      if (strlen($Base->row['can']) > 0)  $p_can_fijas = $Base->row['can'];
   }

   // Busco si el articulo esta para sugerir que agrupe
   $p_sql_st="select a.calle,a.rack,a.pos,b.posicion,b.orden 
                from stock_dep a, rack_dep b
               where a.deposito = b.c_distri
                 and a.calle    = b.calle
                 and a.rack     = b.rack
                 and a.pos      = b.pos
                 and a.empresa  = '$r_empresa'
                 and a.deposito = '$r_deposito'
                 and b.tipo_pos not in ( 'I','R','X','S','Z','O','II','PR','ST','CT',
                                         'TR','I2','I3','I4','I5','G' )
                 and a.area not in ( select area from areas where tipo_area  in ( 'Reserva','Pre-Despacho','ResRepro' ))
                 and a.articulo = '$r_articulo'";
   if ($r_nro_lote != 'Z'){
      $p_sql_st.=" and a.nro_lote = '$r_nro_lote' ";
   }
   $p_sql_st.="  and ( a.bultos != 0 or a.otros != 0 )
               order by b.orden";
   $resultado=@odbc_exec($Base->conexion,$p_sql_st);
   if (!$resultado){
      $pa_ubi['error'] = 'NOK';
      $pa_ubi['msg']   = '80- Error. No se pudo leer [rack_grupos] !!';
      return $pa_ubi;
   }
   while ($row=odbc_fetch_array($resultado)) {

      $p_calle = trim($row['calle']);

      /*
      $p_capacidad = 99;
      $p_sql_st = "select capacidad from rack_dep
                    where c_distri = '$r_deposito'
                      and calle    = '$p_calle'
                      and rack     = '{$row['rack']}'
                      and pos      = '{$row['pos']}' ";
      $Base->r_select_fila($p_sql_st, "rack_dep");
      if ($Base->error == 0) {
         $p_capacidad = $Base->row['capacidad'];
      } 20/02/24 */
      $p_capacidad = 99;

      $pa_stk = Stock_dep::r_veo_posicion_stock($r_deposito,$p_calle,$row['rack'],$row['pos']);
      if ($pa_stk['bultos'] > 0 && ($pa_stk['volumen'] < $p_capacidad)) {
      }
      else{
         $p_hay = true;
         $pa_ubi['calle'] = $p_calle;
         $pa_ubi['rack']  = $row['rack'];
         $pa_ubi['pos']   = $row['pos'];
         break;
      }
   }
   odbc_free_result($resultado);

   if ($p_hay ){
      return $pa_ubi;
   }

   $p_sql_st="select a.calle,a.rack,a.pos,b.posicion,b.orden from stock_dep a, rack_dep b
               where a.deposito = b.c_distri
                 and a.calle    = b.calle
                 and a.rack     = b.rack
                 and a.pos      = b.pos
                 and a.empresa  = '$r_empresa'
                 and a.deposito = '$r_deposito'
                 and b.tipo_pos not in ( 'I','R','X','S','Z','O','II','PR','ST','CT',
                                         'TR','I2','I3','I4','I5','G' )
                 and a.area not in ( select area from areas where tipo_area  in ( 'Reserva','Pre-Despacho','ResRepro' ))
                 and a.articulo = '$r_articulo'
                 and ( a.bultos != 0 or a.otros != 0 )
               order by b.orden";
   $resultado=@odbc_exec($Base->conexion,$p_sql_st);
   if (!$resultado){
      $pa_ubi['error'] = 'NOK';
      $pa_ubi['msg']   = '81- Error. No se pudo leer [rack_grupos] !!';
      return $pa_ubi;
   }
   while ($row=odbc_fetch_array($resultado)) {

      $p_calle = trim($row['calle']);

      /*
      $p_capacidad = 99;
      $p_sql_st = "select capacidad from rack_dep
                    where c_distri = '$r_deposito'
                      and calle    = '$p_calle'
                      and rack     = '{$row['rack']}'
                      and pos      = '{$row['pos']}' ";
      $Base->r_select_fila($p_sql_st, "rack_dep");
      if ($Base->error == 0) {
         $p_capacidad = $Base->row['capacidad'];
      } 20/02/24 */
      $p_capacidad = 99;

      $pa_stk = Stock_dep::r_veo_posicion_stock($r_deposito,$p_calle,$row['rack'],$row['pos']);
      if ($pa_stk['bultos'] > 0 && ($pa_stk['volumen'] < $p_capacidad)) {
      }
      else{
         $p_hay = true;
         $pa_ubi['calle'] = $p_calle;
         $pa_ubi['rack']  = $row['rack'];
         $pa_ubi['pos']   = $row['pos'];
         break;
      }
   }
   odbc_free_result($resultado);

   return $pa_ubi;
   // Fin Busco
}

if ($r_grupo == "." ) {
   $r_grupo = "0";
}

if ($r_empresa == 157) {
   $r_familia = '0';
}
if ($r_empresa == 211){
    $r_grupo = '00';
}

$p_cancel = false;
$p_count  = 0;
$p_hay    = false;

#############################################
# Busco ubicacion fija para el articulo
# 0- ###########################################################################
$p_orden = 0;
$p_sql_st="select * from rack_grupos d
       where d.c_distri   = '$r_deposito'
         and d.operacion  = '$r_tipo'
         and d.empresa    = '$r_empresa'
         and d.articulo   = '$r_articulo'
         and d.estado     <> 99";
$resultado=@odbc_exec($Base->conexion,$p_sql_st);
if (!$resultado){
   $pa_ubi['error'] = 'NOK';
   $pa_ubi['msg']   = '80- Error. No se pudo leer [rack_grupos] !!';
   return $pa_ubi;
}
while ($row=odbc_fetch_array($resultado)) {

   $p_calle = trim($row['calle']);

   $p_capacidad = 99;
   $p_sql_st = "select capacidad from rack_dep
          where c_distri = '{$row['c_distri']}'
            and calle    = '$p_calle'
            and rack     = '{$row['rack']}'
            and pos      = '{$row['pos']}' ";
   $Base->r_select_fila($p_sql_st, "rack_dep");
   if ($Base->error == 0) {
      $p_capacidad = $Base->row['capacidad'];
   }

   $pa_stk = Stock_dep::r_veo_posicion_stock($row['c_distri'],$p_calle,$row['rack'],$row['pos']);
   if ($pa_stk['bultos'] > 0 && ($pa_stk['volumen'] >= $p_capacidad)) {
   }
   else{
      $p_hay = true;
      $pa_ubi['calle'] = $p_calle;
      $pa_ubi['rack']  = $row['rack'];
      $pa_ubi['pos']   = $row['pos'];
      break;
   }
}
odbc_free_result($resultado);

if ($p_hay ){
   return $pa_ubi;
}

##############################################
# Busco el mismo articulo dentro del conj. de ubicaciones para la familia.
# 1- ###########################################################################
$p_sql_st="select a.*,d.orden,e.posicion
        from stock_dep a, articulo b, areas c, rack_grupos d, rack_dep e
       where a.empresa  = b.empresa
         and a.articulo = b.articulo
         and a.area     = c.area
         and a.deposito = d.c_distri
         and a.calle    = d.calle
         and a.rack     = d.rack
         and a.pos      = d.pos
         and b.empresa  = d.empresa
         and b.grupo    = d.grupo
         and b.familia  = d.familia
         and a.deposito = e.c_distri
         and a.calle    = e.calle
         and a.rack     = e.rack
         and a.pos      = e.pos
         and d.estado  <> 99
         and d.operacion  = '$r_tipo'
         and a.empresa    = '$r_empresa'
         and a.deposito   = '$r_deposito'
         and a.articulo   = '$r_articulo'
         and a.bultos   <> 0
         and a.estado    = 'C'
         and c.tipo_area in ( 'Disponible','Reserva','1Reproceso' )
       order by d.orden ";
$resultado=@odbc_exec($Base->conexion,$p_sql_st);
if (!$resultado){
   $pa_ubi['error'] = 'NOK';
   $pa_ubi['msg']   = '81- Error. No se pudo leer [rack_grupos] !!';
   return $pa_ubi;
}
while ($row=odbc_fetch_array($resultado)) {

   $p_posicion = trim($row['posicion']);
   $p_calle = trim($row['calle']);
   $p_orden = $row['orden'];
   # para que no barra para los costados.
   if ($row['deposito'] == "T1") {
      $p_orden = 0;
   }
   $p_sql_st = " select a.calle,a.rack,a.pos,b.posicion,a.orden 
              from rack_grupos a, rack_dep b 
             where a.c_distri  = b.c_distri 
               and a.calle     = b.calle 
               and a.rack      = b.rack 
               and a.pos       = b.pos
               and a.c_distri  = '{$row['deposito']}'
               and a.operacion = '$r_tipo' 
               and a.empresa   = '$r_empresa'
               and a.familia   = '$r_familia' 
               and a.grupo     = '$r_grupo' 
               and a.orden    >= '$p_orden'
               and a.estado   <>  99
               and b.posicion <> '$p_posicion' 
               and b.estado   <> 'A' 
               and b.estado_stk <> 'O' 
             order by a.orden,1,2,3 ";
   $resultado2 = @odbc_exec($Base->conexion, $p_sql_st);
   if (!$resultado2) {
      $pa_ubi['error'] = 'NOK';
      $pa_ubi['msg'] = '82- Error. No se pudo leer [stock_dep] !!';
      break;
   }
   while ($row2 = odbc_fetch_array($resultado2)) {

      $p_calle = trim($row2['calle']);
      $pa_stk = Stock_dep::r_veo_posicion_stock($row['deposito'], $p_calle, $row2['rack'], $row2['pos']);
      if ($pa_stk['bultos'] > 0) {
      } else {
         $p_hay = true;
         $pa_ubi['calle'] = $row2['calle'];
         $pa_ubi['rack'] = $row2['rack'];
         $pa_ubi['pos'] = $row2['pos'];
         break;
      }
   }
   odbc_free_result($resultado2);

   if($p_hay) break;

   $p_sql_st=" select a.calle,a.rack,a.pos,b.posicion,a.orden 
            from rack_grupos a, rack_dep b 
           where a.c_distri  = b.c_distri 
             and a.calle     = b.calle 
             and a.rack      = b.rack
             and a.pos       = b.pos 
             and a.c_distri  = '{$row['deposito']}'
             and a.operacion = '$r_tipo' 
             and a.empresa   = '$r_empresa'
             and a.familia   = '$r_familia' 
             and a.grupo     = '$r_grupo' 
             and a.orden     < '$p_orden'
             and a.estado   <>  99
             and b.posicion <> '$p_posicion' 
             and b.estado   <> 'A'
             and b.estado_stk <> 'O'
           order by a.orden,1,2,3 ";
   $resultado2=@odbc_exec($Base->conexion,$p_sql_st);
   if (!$resultado2){
      $pa_ubi['error'] = 'NOK';
      $pa_ubi['msg'] = '83- Error. No se pudo leer [stock_dep] !!';
      break;
   }
   while ($row2=odbc_fetch_array($resultado2)){

      $p_calle = trim($row2['calle']);
      $pa_stk = Stock_dep::r_veo_posicion_stock($row['deposito'], $p_calle, $row2['rack'], $row2['pos']);
      if ($pa_stk['bultos'] > 0) {
      } else {
         $p_hay = true;
         $pa_ubi['calle'] = $row2['calle'];
         $pa_ubi['rack'] = $row2['rack'];
         $pa_ubi['pos'] = $row2['pos'];
         break;
      }
   }
   odbc_free_result($resultado2);
   if($p_hay) break;
}
odbc_free_result($resultado);

if ($p_hay ){
   return $pa_ubi;
}

# 2- ###########################################################################
# Busco dentro del area para la familia si hay algo ocupado de esa familia.
$p_sql_st="select a.*,d.orden,e.posicion
        from stock_dep a, articulo b, areas c, rack_grupos d, rack_dep e
       where a.empresa  = b.empresa
         and a.articulo = b.articulo
         and a.area     = c.area
         and a.deposito = d.c_distri
         and a.calle    = d.calle
         and a.rack     = d.rack
         and a.pos      = d.pos
         and b.empresa = d.empresa
         and b.grupo = d.grupo
         and b.familia  = d.familia
         and a.deposito = e.c_distri
         and a.calle    = e.calle
         and a.rack     = e.rack
         and a.pos      = e.pos
         and d.estado  <> 99
         and d.operacion  = '$r_tipo'
         and a.empresa    = '$r_empresa'
         and b.familia    = '$r_familia'
         and a.deposito   = '$r_deposito'
         and b.grupo      = '$r_grupo'
         and a.estado     = 'C'
         and a.bultos <> 0
         and c.tipo_area in ( 'Disponible','Reserva','Reproceso' )
        order by d.orden ";
$resultado=@odbc_exec($Base->conexion,$p_sql_st);
if (!$resultado){
   $pa_ubi['error'] = 'NOK';
   $pa_ubi['msg']   = '84- Error. No se pudo leer [rack_grupos] !!';
   return $pa_ubi;
}
while ($row=odbc_fetch_array($resultado)){

   $p_posicion = trim($row['posicion']);
   $p_calle = trim($row['calle']);
   $p_orden = $row['orden'];
   # para que no barra para los costados.
   if ($row['deposito'] == "T1") {
      $p_orden = 0;
   }
   $p_sql_st=" select a.calle,a.rack,a.pos,b.posicion,a.orden 
            from rack_grupos a, rack_dep b 
           where a.c_distri  = b.c_distri 
             and a.calle     = b.calle 
             and a.rack      = b.rack 
             and a.pos       = b.pos 
             and a.c_distri  = '{$row['deposito']}'
             and a.operacion = '$r_tipo'
             and a.empresa   = '$r_empresa'
             and a.familia   = '$r_familia' 
             and a.grupo     = '$r_grupo' 
             and a.orden    >= '$p_orden'
             and a.estado   <>  99
             and b.posicion <> '$p_posicion' 
             and b.estado   <> 'A'
             and b.estado_stk <> 'O'
           order by a.orden,1,2,3 ";
   $resultado2 = @odbc_exec($Base->conexion, $p_sql_st);
   if (!$resultado2) {
      $pa_ubi['error'] = 'NOK';
      $pa_ubi['msg'] = '85- Error. No se pudo leer [stock_dep] !!';
      break;
   }
   while ($row2 = odbc_fetch_array($resultado2)) {

      $p_calle = trim($row2['calle']);
      $pa_stk = Stock_dep::r_veo_posicion_stock($row['deposito'], $p_calle, $row2['rack'], $row2['pos']);
      if ($pa_stk['bultos'] > 0) {
      } else {
         $p_hay = true;
         $pa_ubi['calle'] = $row2['calle'];
         $pa_ubi['rack'] = $row2['rack'];
         $pa_ubi['pos'] = $row2['pos'];
         break;
      }
   }
   odbc_free_result($resultado2);

   if($p_hay) break;

   $p_sql_st= "select a.calle,a.rack,a.pos,b.posicion,a.orden 
           from rack_grupos a, rack_dep b 
          where a.c_distri  = b.c_distri 
            and a.calle     = b.calle 
            and a.rack      = b.rack 
            and a.pos       = b.pos 
            and a.c_distri  = '{$row['deposito']}'
            and a.operacion = '$r_tipo' 
            and a.empresa   = '$r_empresa'
            and a.familia   = '$r_familia' 
            and a.grupo     = '$r_grupo' 
            and a.orden     < '$p_orden'
            and a.estado   <>  99
            and b.posicion <> '$p_posicion' 
            and b.estado   <> 'A' 
            and b.estado_stk <> 'O' 
          order by a.orden,1,2,3 ";
   $resultado2 = @odbc_exec($Base->conexion, $p_sql_st);
   if (!$resultado2) {
      $pa_ubi['error'] = 'NOK';
      $pa_ubi['msg'] = '86- Error. No se pudo leer [stock_dep] !!';
      break;
   }
   while ($row2 = odbc_fetch_array($resultado2)) {

      $p_calle = trim($row2['calle']);
      $pa_stk = Stock_dep::r_veo_posicion_stock($row['deposito'], $p_calle, $row2['rack'], $row2['pos']);
      if ($pa_stk['bultos'] > 0) {
      } else {
         $p_hay = true;
         $pa_ubi['calle'] = $row2['calle'];
         $pa_ubi['rack'] = $row2['rack'];
         $pa_ubi['pos'] = $row2['pos'];
         break;
      }
   }
   odbc_free_result($resultado2);

   if($p_hay) break;

}
odbc_free_result($resultado);

if ($p_hay ){
   return $pa_ubi;
}

# 3- ###########################################################################
# Busco ubicaciones libres dentro del sector de la familia
$p_sql_st=" select a.calle,a.rack,a.pos,b.posicion,a.orden 
        from rack_grupos a, rack_dep b 
       where a.c_distri  = b.c_distri 
         and a.calle     = b.calle 
         and a.rack      = b.rack 
         and a.pos       = b.pos 
         and a.c_distri  = '$r_deposito'
         and a.operacion = '$r_tipo'
         and a.empresa   = '$r_empresa'
         and a.familia   = '$r_familia'       
         and a.grupo     = '$r_grupo' 
         and a.estado   <>  99
         and b.estado   <> 'A' 
         and b.estado_stk <> 'O' 
       order by 3,a.orden,1,2 ";
$resultado=@odbc_exec($Base->conexion,$p_sql_st);
if (!$resultado){
   $pa_ubi['error'] = 'NOK';
   $pa_ubi['msg']   = '87- Error. No se pudo leer [rack_grupos] !!';
   return $pa_ubi;
}
while ($row=odbc_fetch_array($resultado)){

   $p_calle = trim($row['calle']);
   $pa_stk = Stock_dep::r_veo_posicion_stock($r_deposito, $p_calle, $row['rack'], $row['pos']);
   if ($pa_stk['bultos'] > 0) {
   } else {
      $p_hay = true;
      $pa_ubi['calle'] = $row['calle'];
      $pa_ubi['rack'] = $row['rack'];
      $pa_ubi['pos'] = $row['pos'];
      break;
   }
}
odbc_free_result($resultado);

if ($p_hay ){
   return $pa_ubi;
}

$p_calle = '0';
$p_sql_st="select min(c.calle) calle
       from rack_grupos c
      where c.c_distri  = '$r_deposito'
        and c.operacion = '$r_tipo'
        and c.empresa   = '$r_empresa'
        and c.familia   = '$r_familia'
        and c.grupo     = '$r_grupo'
        and c.estado  <> 99 ";
$Base->r_select_fila($p_sql_st,"rack_grupos");
if ($Base->error == 0){
   $p_calle = trim($Base->row['calle']);
}

# Busco cualquier otra posicion cerca del grupo de ubicaciones selccionada
# que tenga mercaderia del grupo
$p_sql_st="select a.calle,a.rack,a.pos,a.posicion,a.orden 
        from rack_grupos b, rack_dep a  
       where a.c_distri  = b.c_distri 
         and a.calle     = b.calle 
         and a.rack      = b.rack 
         and a.pos       = b.pos 
         and b.c_distri  = '$r_deposito' 
         and b.operacion = '$r_tipo' 
         and b.empresa   = '$r_empresa'
         and b.grupo     = '$r_grupo' 
         and a.estado    <> 'A'
         and b.estado    <> 99 
         and a.estado_stk <> 'O' 
         and a.tipo_pos not in ('I','R','X','S','Z','O','II','PR','ST','CT', 
                                'TR','I2','I3','I4','I5' ) ";
if ($r_tipo == "S" ) {
   $p_sql_st.=" and a.tipo_pos <> 'P' ";
}
else{
   $p_sql_st.= " and a.pos <= 2 ";
}
$p_sql_st.=" and a.orden <= '$p_calle'
      order by pos,orden desc,1,2 ";
$resultado=@odbc_exec($Base->conexion,$p_sql_st);
if (!$resultado){
   $pa_ubi['error'] = 'NOK';
   $pa_ubi['msg']   = '88- Error. No se pudo leer [rack_grupos] !!';
   return $pa_ubi;
}
while ($row=odbc_fetch_array($resultado)){
   $p_calle = trim($row['calle']);
   $pa_stk = Stock_dep::r_veo_posicion_stock($r_deposito, $p_calle, $row['rack'], $row['pos']);
   if ($pa_stk['bultos'] > 0) {
   } else {
      $p_hay = true;
      $pa_ubi['calle'] = $row['calle'];
      $pa_ubi['rack'] = $row['rack'];
      $pa_ubi['pos'] = $row['pos'];
      break;
   }
}
odbc_free_result($resultado);

if ($p_hay ){
   return $pa_ubi;
}

# Busco cualquier otra posicion cerca del grupo de ubicaciones selccionada
# que tenga mercaderia del grupo para el otro lado
$p_sql_st="select a.calle,a.rack,a.pos,a.posicion,a.orden 
        from rack_grupos b, rack_dep a  
       where a.c_distri = b.c_distri 
         and a.calle    = b.calle 
         and a.rack     = b.rack 
         and a.pos      = b.pos 
         and b.c_distri = '$r_deposito' 
         and b.operacion = '$r_tipo' 
         and b.empresa   = '$r_empresa'
         and b.grupo     = '$r_grupo' 
         and a.estado <> 'A' 
         and b.estado <> 99
         and a.estado_stk <> 'O' 
         and a.tipo_pos not in ( 'I','R','X','S','Z','O','II','PR','ST','CT', 
                                'TR' ,'I2','I3','I4','I5' ) ";
if ($r_tipo == "S" ) {
   $p_sql_st.=" and a.tipo_pos <> 'P' ";
}
else{
   $p_sql_st.=" and a.pos <= 2 ";
}
$p_sql_st.= " and a.orden > '$p_calle'
       order by orden,1,2,3 ";
$resultado=@odbc_exec($Base->conexion,$p_sql_st);
if (!$resultado){
   $pa_ubi['error'] = 'NOK';
   $pa_ubi['msg']   = '89- Error. No se pudo leer [rack_grupos] !!';
   return $pa_ubi;
}
while ($row=odbc_fetch_array($resultado)){
   $p_calle = trim($row['calle']);
   $pa_stk = Stock_dep::r_veo_posicion_stock($r_deposito, $p_calle, $row['rack'], $row['pos']);
   if ($pa_stk['bultos'] > 0) {
   } else {
      $p_hay = true;
      $pa_ubi['calle'] = $row['calle'];
      $pa_ubi['rack'] = $row['rack'];
      $pa_ubi['pos'] = $row['pos'];
      break;
   }
}
odbc_free_result($resultado);

if ($p_hay ){
   return $pa_ubi;
}

if ($r_empresa == 157  || $r_empresa == 15 || $r_empresa == 12  || $r_empresa == 211) {
   $pa_ubi['error'] = 'NOK';
   $pa_ubi['msg']   = '90- No hay ubicaciones libres en el deposito '.$r_deposito." !! ";
   return $pa_ubi;
}

# Busco demas posiciones que no revise o por familia o por grupo
$p_sql_st="select a.calle,a.rack,a.pos,a.posicion,a.orden 
             from rack_dep a 
            where a.estado <> 'A' 
              and a.c_distri = '$r_deposito' 
              and a.tipo_pos not in ( 'I','R','X','S','Z','O','II','PR','ST','CT',
                                     'TR','I2','I3','I4','I5','G' )
              and a.estado_stk <> 'O' ";
if ($r_tipo == "S" ) {
   $p_sql_st.=" and a.tipo_pos <> 'P' ";
}
else {
   $p_sql_st.=" and a.pos <= 2 ";
}
$p_sql_st.=" and a.posicion not in ( select b.posicion from rack_dep b, rack_grupos c 
                                      where b.calle = c.calle 
                                        and b.rack  = c.rack 
                                        and b.pos   = c.pos 
                                        and b.c_distri  = c.c_distri 
                                        and c.c_distri  = '$r_deposito' 
                                        and c.operacion = '$r_tipo' 
                                        and c.empresa   = '$r_empresa'
                                        and (c.familia  = '$r_familia' or  c.grupo = '$r_grupo' ) 
                                        and b.estado <> 'A' and c.estado != 99 ) 
              order by 3,orden,1,2 ";
$resultado=@odbc_exec($Base->conexion,$p_sql_st);
if (!$resultado){
   $pa_ubi['error'] = 'NOK';
   $pa_ubi['msg']   = '91- Error. No se pudo leer [rack_grupos] !!';
   return $pa_ubi;
}
while ($row=odbc_fetch_array($resultado)){
   $p_calle = trim($row['calle']);
   $pa_stk = Stock_dep::r_veo_posicion_stock($r_deposito, $p_calle, $row['rack'], $row['pos']);
   if ($pa_stk['bultos'] > 0) {
   } else {
      $p_hay = true;
      $pa_ubi['calle'] = $row['calle'];
      $pa_ubi['rack'] = $row['rack'];
      $pa_ubi['pos'] = $row['pos'];
      break;
   }
}
odbc_free_result($resultado);

if (!$p_hay){
   $pa_ubi['error'] = 'NOK';
   $pa_ubi['msg']   = '92- No se encontraron Ubicaciones libres en '.$r_deposito." !! ";
}

return $pa_ubi;
}

static function r_busco_ubicacion_12($r_empresa, $r_deposito, $r_articulo)
{
global $Base;

$pa_ubi['error'] = 'OK';
$pa_ubi['msg']   = '';
$pa_ubi['calle'] = '';
$pa_ubi['rack']  = 0;
$pa_ubi['pos']   = 0;

$p_hay    = false;
$p_orden = 0;
$p_sql_st="select * from rack_grupos d
       where d.c_distri   = '$r_deposito'
         and d.operacion  = 'A'
         and d.empresa    = '$r_empresa'
         and d.estado     <> 99
         order by orden ";
$resultado=@odbc_exec($Base->conexion,$p_sql_st);
if (!$resultado){
  $pa_ubi['error'] = 'NOK';
  $pa_ubi['msg']   = '93- Error. No se pudo leer [rack_grupos] !!';
  return $pa_ubi;
}
while ($row=odbc_fetch_array($resultado)) {

  $p_calle = trim($row['calle']);

  $pa_stk = Stock_dep::r_cuento_posicion_stock($row['c_distri'],$p_calle,$row['rack'],$row['pos']);
  if ($pa_stk['bul'] > 0 && ($pa_stk['can'] >= 2)) { // 2 art. por posicion
  }
  else{
    $p_hay = true;
    $pa_ubi['calle'] = $p_calle;
    $pa_ubi['rack']  = $row['rack'];
    $pa_ubi['pos']   = $row['pos'];
    break;
  }
}
odbc_free_result($resultado);

if (!$p_hay ){
  $pa_ubi['error'] = 'NOK';
  $pa_ubi['msg']   = '94- No hay ubicaciones libres en el deposito '.$r_deposito." !! ";
  return $pa_ubi;
}
return $pa_ubi;

}

static function r_veo_posicion_stock($r_deposito,$r_calle,$r_rack,$r_pos)
{
global $Base;

$pa_return['bultos']  = 0;
$pa_return['volumen'] = 0;
$p_sql_st = "select sum(bultos) bultos,sum(volumen) volumen
          from stock_dep a, areas b
         where a.area     = b.area
           and a.deposito = '$r_deposito'
           and a.calle    = '$r_calle'
           and a.rack     = '$r_rack'
           and a.pos      = '$r_pos'
           and a.area not in ( select area from areas 
                                where tipo_area in ('Pre-Despacho','Ajuste','Egreso','Perdido' )) ";
$Base->r_select_fila($p_sql_st, "stock_dep");
if ($Base->error == 2){
   $pa_return['error'] = 'NOK';
   $pa_return['msg']   = '100- Error. No se pudo leer [stock_dep] !! ';
   return $pa_return;
}
if ($Base->error == 0) {
   $pa_return['bultos']  = $Base->row['bultos'];
   $pa_return['volumen'] = $Base->row['volumen'];
}

return $pa_return;
}

static function r_cuento_posicion_stock($r_deposito,$r_calle,$r_rack,$r_pos)
{
global $Base;

$pa_return['can']  = 0;
$pa_return['bul']  = 0;
$p_sql_st = "select articulo,sum(bultos) bultos from stock_dep a, areas b
         where a.area     = b.area
           and a.deposito = '$r_deposito'
           and a.calle    = '$r_calle'
           and a.rack     = '$r_rack'
           and a.pos      = '$r_pos'
           and a.bultos  != 0 
           and a.area not in ( select area from areas 
                                where tipo_area in ('Pre-Despacho','Ajuste','Egreso','Perdido' )) 
        group by articulo ";
$resultado=@odbc_exec($Base->conexion,$p_sql_st);
if (!$resultado){
  $pa_return['error'] = 'NOK';
  $pa_return['msg']   = '101- Error. No se pudo leer [stock_dep] !! ';
  return $pa_return;
}
while ($row=odbc_fetch_array($resultado)){
  $pa_return['can']++;
  $pa_return['bul']+=$row['bultos'];
}
odbc_free_result($resultado);

return $pa_return;
}

function __destruct()
{
  //odbc_close($this->conexion);
}

}

?>