ALTER TABLE TBMANIFESTO_NF
ADD NUMERO_NOTAFISCAL INTEGER;

ALTER TABLE TBMANIFESTO_LOTE
ADD USUARIO_ID INTEGER;

ALTER TABLE TBMANIFESTO_NF
ADD SERIE VARCHAR(3);


CREATE OR ALTER trigger tbmanifesto_nf_b03i for tbmanifesto_nf
active before insert or update position 3
AS
DECLARE VARIABLE EMPRESA_UF VARCHAR(2);

BEGIN
    IF(CHAR_LENGTH(NEW.CHAVE_ACESSO) >= 2) THEN
    BEGIN

        SELECT FIRST 1 U.UF FROM TBUF U WHERE U.CODIGO_IBGE = SUBSTRING(NEW.CHAVE_ACESSO FROM 1 FOR 2) INTO :EMPRESA_UF;

        NEW.EMPRESA_UF = :EMPRESA_UF;

        NEW.NUMERO_NOTAFISCAL = CAST(SUBSTRING(NEW.CHAVE_ACESSO FROM 25 FOR 9) AS INTEGER);

        NEW.SERIE = SUBSTRING(NEW.CHAVE_ACESSO FROM 22 FOR 3);

    END
END;