CREATE OR ALTER trigger ttbnfe_item_b01iu for tbnfe_item
active before insert or update position 0
AS
BEGIN
    IF(INSERTING) THEN
    BEGIN
        IF(TRIM(COALESCE(NEW.NCM_CODIGO, '')) <> '' AND CHAR_LENGTH(TRIM(NEW.NCM_CODIGO)) <> 2 AND CHAR_LENGTH(TRIM(NEW.NCM_CODIGO)) <> 8) THEN
        BEGIN
            EXCEPTION"..."'NCM '||NEW.NCM_CODIGO||' INVALIDO PARA O ITEM '||FN_LPAD(NEW.SEQUENCIA, 3, '0')||' - PRODUTO '||NEW.PRODUTO_DESCRICAO||'';
        END

    END

    IF(UPDATING) THEN
    BEGIN
        IF(OLD.NCM_CODIGO <> NEW.NCM_CODIGO) THEN
        BEGIN
            IF(TRIM(COALESCE(NEW.NCM_CODIGO, '')) <> '' AND CHAR_LENGTH(TRIM(NEW.NCM_CODIGO)) <> 2 AND CHAR_LENGTH(TRIM(NEW.NCM_CODIGO)) <> 8) THEN
            BEGIN
                EXCEPTION"..."'NCM '||NEW.NCM_CODIGO||' INVALIDO PARA O ITEM '||FN_LPAD(NEW.SEQUENCIA, 3, '0')||' - PRODUTO '||NEW.PRODUTO_DESCRICAO||'';
            END
        END

    END
END;


