XUDF Informix

XUDF Informix

-- **************************************************************************
-- gart_unidefs_get_canbase
-- DEISTER WebStudio XSQL-UDFUNC  -  Engine: ids
-- **************************************************************************
CREATE FUNCTION gart_unidefs_get_canbase(
    p_codart LIKE gvenpedl.codart,
    p_varlog LIKE gvenpedl.varlog,
    p_udmdoc LIKE gvenpedl.udmven,
    p_udmalt LIKE gvenpedl.udmalt,
    p_candoc LIKE gvenpedl.canped,
    p_canalt LIKE gvenpedl.canalt
)
    RETURNING  decimal(12,3);

    -- =================================
    -- Definition of variables
    -- =================================
    DEFINE m_udmbas LIKE garticul.udmbas;
    DEFINE m_udmaux LIKE garticul.udmaux;
    DEFINE m_udmrel LIKE gart_unidefs.udmrel;
    DEFINE m_candes LIKE gvenpedl.canped;

    -- =========================================================
    -- Debug
    -- To activate debug mesages, call sdm_set_debug(1) or level
    -- =========================================================
    DEFINE GLOBAL gl_debug   SMALLINT DEFAULT 0;    -- DEBUG FLAG
    IF gl_debug > 1 THEN
        TRACE ON;
    END IF

    -- =================================
    -- Function body
    -- =================================
    LET m_candes = NULL;
    SELECT udmbas, udmaux
           INTO m_udmbas, m_udmaux
           FROM garticul
          WHERE codigo  = p_codart
        ;
    IF m_udmbas IS NULL THEN
      RAISE EXCEPTION -746, 0, 'Artículo: [' || p_codart || '] no encontrado en garticul';
    END IF;
    IF p_udmdoc = m_udmbas THEN
      RETURN p_candoc;
    END IF;
    IF p_udmalt IS NOT NULL AND p_udmalt = m_udmbas  THEN
      RETURN p_canalt;
    END IF;
    LET m_udmrel = gart_unidefs_get_udmrel(
            p_codart, p_varlog, p_udmdoc
        );
    IF 
                m_udmrel = 'B' OR (m_udmrel = 'A' AND p_udmalt IS NULL)
             THEN
      LET m_candes = gart_uniconv_get_cantid(CAST(0 AS smallint ) , p_codart, p_varlog, p_udmdoc, CAST(NULL AS varchar (255)) , m_udmbas, p_candoc, CAST(NULL AS decimal (16,6)) );
    END IF;
    IF m_candes IS NULL AND p_udmalt IS NOT NULL THEN
      LET m_udmrel = gart_unidefs_get_udmrel(
                    p_codart, p_varlog, p_udmalt
                );
      IF m_udmrel = 'B' THEN
        LET m_candes = gart_uniconv_get_cantid(CAST(0 AS smallint ) , p_codart, p_varlog, p_udmalt, CAST(NULL AS varchar (255)) , m_udmbas, p_canalt, CAST(NULL AS decimal (16,6)) );
      ELSE
              LET m_candes = gart_uniconv_get_cantid(CAST(0 AS smallint ) , p_codart, p_varlog, p_udmdoc, CAST(NULL AS varchar (255)) , m_udmbas, p_candoc, CAST(NULL AS decimal (16,6)) );

      END IF;
    END IF;
    RETURN m_candes;
END FUNCTION;