SET TERM ^ ;

CREATE OR ALTER procedure SPC_PEDIDO_PRECO_MODELO_LIST (
    CLIENTE_ID integer,
    ESTABELECIMENTO_ID integer,
    PRODUTO_ID integer,
    MODELO_ID integer,
    GRADE_ID integer,
    PRECO_NF_CF_ID integer = 0,
    DATA_PEDIDO date = 'NOW',
    TABELA_PRECO integer = 1)
returns (
    TIPO integer,
    LISTA_PRECOS varchar(8000),
    VALIDO integer)
as
declare variable EMPRESA_VINCULO integer;
declare variable PERCENTUAL_BASE numeric(18,8);
declare variable DECIMAIS_BASE integer;
declare variable TABELA_VALIDA integer;
BEGIN

    EMPRESA_VINCULO = COALESCE((SELECT FIRST 1 C.EMPRESA_VINCULO FROM TBCLIENTE C WHERE C.CODIGO = :CLIENTE_ID),0);
    PERCENTUAL_BASE = COALESCE((SELECT FIRST 1 COALESCE(C.PERCENTUAL_BASE,0.00)/100.00 FROM TBTABELA_PRECO C WHERE C.ID = :PRECO_NF_CF_ID),0);
    DECIMAIS_BASE   = COALESCE((SELECT FIRST 1 COALESCE(C.DECIMAIS_BASE,2) FROM TBTABELA_PRECO C WHERE C.ID = :PRECO_NF_CF_ID),2);
    TABELA_VALIDA   = COALESCE((SELECT FIRST 1 1 FROM TBTABELA_PRECO D WHERE D.ID = :PRECO_NF_CF_ID AND :DATA_PEDIDO BETWEEN D.DATA_INICIAL AND D.DATA_FINAL),0);

    FOR

      SELECT
        TIPO,
        CAST(LISTA_PRECOS AS VARCHAR(8000)) AS LISTA_PRECOS,
        VALIDO
            
            FROM
               (
               
                SELECT
                    1 TIPO,
                    '['||LIST(
                        fn_to_json_v3(
                            fn_to_par_json_v2('T01',IIF(B.pr_nf_01 > 0, B.pr_nf_01 , 0)),
                            fn_to_par_json_v2('T02',IIF(B.pr_nf_02 > 0, B.pr_nf_02 , 0)),
                            fn_to_par_json_v2('T03',IIF(B.pr_nf_03 > 0, B.pr_nf_03 , 0)),
                            fn_to_par_json_v2('T04',IIF(B.pr_nf_04 > 0, B.pr_nf_04 , 0)),
                            fn_to_par_json_v2('T05',IIF(B.pr_nf_05 > 0, B.pr_nf_05 , 0)),
                            fn_to_par_json_v2('T06',IIF(B.pr_nf_06 > 0, B.pr_nf_06 , 0)),
                            fn_to_par_json_v2('T07',IIF(B.pr_nf_07 > 0, B.pr_nf_07 , 0)),
                            fn_to_par_json_v2('T08',IIF(B.pr_nf_08 > 0, B.pr_nf_08 , 0)),
                            fn_to_par_json_v2('T09',IIF(B.pr_nf_09 > 0, B.pr_nf_09 , 0)),
                            fn_to_par_json_v2('T10',IIF(B.pr_nf_10 > 0, B.pr_nf_10 , 0)),
                            fn_to_par_json_v2('T11',IIF(B.pr_nf_11 > 0, B.pr_nf_11 , 0)),
                            fn_to_par_json_v2('T12',IIF(B.pr_nf_12 > 0, B.pr_nf_12 , 0)),
                            fn_to_par_json_v2('T13',IIF(B.pr_nf_13 > 0, B.pr_nf_13 , 0)),
                            fn_to_par_json_v2('T14',IIF(B.pr_nf_14 > 0, B.pr_nf_14 , 0)),
                            fn_to_par_json_v2('T15',IIF(B.pr_nf_15 > 0, B.pr_nf_15 , 0)),
                            fn_to_par_json_v2('T16',IIF(B.pr_nf_16 > 0, B.pr_nf_16 , 0)),
                            fn_to_par_json_v2('T17',IIF(B.pr_nf_17 > 0, B.pr_nf_17 , 0)),
                            fn_to_par_json_v2('T18',IIF(B.pr_nf_18 > 0, B.pr_nf_18 , 0)),
                            fn_to_par_json_v2('T19',IIF(B.pr_nf_19 > 0, B.pr_nf_19 , 0)),
                            fn_to_par_json_v2('T20',IIF(B.pr_nf_20 > 0, B.pr_nf_20 , 0))
                        ),
                    ',')||']' LISTA_PRECOS,
                    :TABELA_VALIDA AS VALIDO

                FROM TBCLIENTE_PRODUTO_PRECO B
                WHERE B.CLIENTE_CODIGO = :CLIENTE_ID AND B.PRODUTO_CODIGO = :PRODUTO_ID AND B.TABELA_ID = :PRECO_NF_CF_ID

                UNION
                
                SELECT
                    1 TIPO,
                    '['||LIST(
                        fn_to_json_v3(
                            fn_to_par_json_v2('T01',IIF(B.pr_nf_01 > 0, FN_NUMBER_CAST((B.pr_nf_01*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T02',IIF(B.pr_nf_02 > 0, FN_NUMBER_CAST((B.pr_nf_02*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T03',IIF(B.pr_nf_03 > 0, FN_NUMBER_CAST((B.pr_nf_03*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T04',IIF(B.pr_nf_04 > 0, FN_NUMBER_CAST((B.pr_nf_04*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T05',IIF(B.pr_nf_05 > 0, FN_NUMBER_CAST((B.pr_nf_05*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T06',IIF(B.pr_nf_06 > 0, FN_NUMBER_CAST((B.pr_nf_06*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T07',IIF(B.pr_nf_07 > 0, FN_NUMBER_CAST((B.pr_nf_07*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T08',IIF(B.pr_nf_08 > 0, FN_NUMBER_CAST((B.pr_nf_08*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T09',IIF(B.pr_nf_09 > 0, FN_NUMBER_CAST((B.pr_nf_09*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T10',IIF(B.pr_nf_10 > 0, FN_NUMBER_CAST((B.pr_nf_10*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T11',IIF(B.pr_nf_11 > 0, FN_NUMBER_CAST((B.pr_nf_11*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T12',IIF(B.pr_nf_12 > 0, FN_NUMBER_CAST((B.pr_nf_12*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T13',IIF(B.pr_nf_13 > 0, FN_NUMBER_CAST((B.pr_nf_13*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T14',IIF(B.pr_nf_14 > 0, FN_NUMBER_CAST((B.pr_nf_14*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T15',IIF(B.pr_nf_15 > 0, FN_NUMBER_CAST((B.pr_nf_15*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T16',IIF(B.pr_nf_16 > 0, FN_NUMBER_CAST((B.pr_nf_16*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T17',IIF(B.pr_nf_17 > 0, FN_NUMBER_CAST((B.pr_nf_17*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T18',IIF(B.pr_nf_18 > 0, FN_NUMBER_CAST((B.pr_nf_18*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T19',IIF(B.pr_nf_19 > 0, FN_NUMBER_CAST((B.pr_nf_19*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T20',IIF(B.pr_nf_20 > 0, FN_NUMBER_CAST((B.pr_nf_20*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0))
                        ),
                    ',')||']' LISTA_PRECOS,
                    :TABELA_VALIDA AS VALIDO

                FROM TBCLIENTE_PRODUTO_PRECO B
                where (B.CLIENTE_CODIGO = :CLIENTE_ID AND B.PRODUTO_CODIGO = :PRODUTO_ID AND B.TABELA_ID = (SELECT FIRST 1 C.TABELA_PRECO_ID_BASE FROM TBTABELA_PRECO C WHERE C.ID = :PRECO_NF_CF_ID))

                UNION

                SELECT
                    1 TIPO,
                     '['||LIST(
                            fn_to_json_v3(
                                fn_to_par_json_v2('T01',IIF(B.pr_nf_01 > 0, B.pr_nf_01 , 0)),
                                fn_to_par_json_v2('T02',IIF(B.pr_nf_02 > 0, B.pr_nf_02 , 0)),
                                fn_to_par_json_v2('T03',IIF(B.pr_nf_03 > 0, B.pr_nf_03 , 0)),
                                fn_to_par_json_v2('T04',IIF(B.pr_nf_04 > 0, B.pr_nf_04 , 0)),
                                fn_to_par_json_v2('T05',IIF(B.pr_nf_05 > 0, B.pr_nf_05 , 0)),
                                fn_to_par_json_v2('T06',IIF(B.pr_nf_06 > 0, B.pr_nf_06 , 0)),
                                fn_to_par_json_v2('T07',IIF(B.pr_nf_07 > 0, B.pr_nf_07 , 0)),
                                fn_to_par_json_v2('T08',IIF(B.pr_nf_08 > 0, B.pr_nf_08 , 0)),
                                fn_to_par_json_v2('T09',IIF(B.pr_nf_09 > 0, B.pr_nf_09 , 0)),
                                fn_to_par_json_v2('T10',IIF(B.pr_nf_10 > 0, B.pr_nf_10 , 0)),
                                fn_to_par_json_v2('T11',IIF(B.pr_nf_11 > 0, B.pr_nf_11 , 0)),
                                fn_to_par_json_v2('T12',IIF(B.pr_nf_12 > 0, B.pr_nf_12 , 0)),
                                fn_to_par_json_v2('T13',IIF(B.pr_nf_13 > 0, B.pr_nf_13 , 0)),
                                fn_to_par_json_v2('T14',IIF(B.pr_nf_14 > 0, B.pr_nf_14 , 0)),
                                fn_to_par_json_v2('T15',IIF(B.pr_nf_15 > 0, B.pr_nf_15 , 0)),
                                fn_to_par_json_v2('T16',IIF(B.pr_nf_16 > 0, B.pr_nf_16 , 0)),
                                fn_to_par_json_v2('T17',IIF(B.pr_nf_17 > 0, B.pr_nf_17 , 0)),
                                fn_to_par_json_v2('T18',IIF(B.pr_nf_18 > 0, B.pr_nf_18 , 0)),
                                fn_to_par_json_v2('T19',IIF(B.pr_nf_19 > 0, B.pr_nf_19 , 0)),
                                fn_to_par_json_v2('T20',IIF(B.pr_nf_20 > 0, B.pr_nf_20 , 0))
                            ),
                    ',')||']' LISTA_PRECOS,
                    :TABELA_VALIDA AS VALIDO

                FROM
                    (SELECT FIRST 1
                     COALESCE(C.PERCENTUAL_BASE,0.00) AS PERCENTUAL_BASE,
                     COALESCE(C.DECIMAIS_BASE,2) AS DECIMAIS_BASE,
                     B.pr_nf_01,
                     B.pr_nf_02,
                     B.pr_nf_03,
                     B.pr_nf_04,
                     B.pr_nf_05,
                     B.pr_nf_06,
                     B.pr_nf_07,
                     B.pr_nf_08,
                     B.pr_nf_09,
                     B.pr_nf_10,
                     B.pr_nf_11,
                     B.pr_nf_12,
                     B.pr_nf_13,
                     B.pr_nf_14,
                     B.pr_nf_15,
                     B.pr_nf_16,
                     B.pr_nf_17,
                     B.pr_nf_18,
                     B.pr_nf_19,
                     B.pr_nf_20

                     FROM TBCLIENTE_PRODUTO_PRECO B, TBTABELA_PRECO C
                     WHERE :TABELA_PRECO = 1 AND B.CLIENTE_CODIGO IN --(SELECT C.CODIGO FROM TBCLIENTE C WHERE C.EMPRESA_VINCULO = :EMPRESA_VINCULO)
                      (SELECT C.CODIGO FROM TBCLIENTE C, TBEMPRESA_GRUPO_CONTAS W WHERE W.EMPRESA_ID = C.CODIGO AND W.EMPRESA_GRUPO_ID = :EMPRESA_VINCULO)
                     AND B.PRODUTO_CODIGO    = :PRODUTO_ID
                     AND B.TABELA_ID        = C.ID
                     AND B.TABELA_ID        = :PRECO_NF_CF_ID) B

                
                UNION

                SELECT
                    1 TIPO,
                    '['||LIST(
                            fn_to_json_v3(
                                fn_to_par_json_v2('T01',IIF(B.pr_nf_01 > 0, FN_NUMBER_CAST((B.pr_nf_01*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T02',IIF(B.pr_nf_02 > 0, FN_NUMBER_CAST((B.pr_nf_02*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T03',IIF(B.pr_nf_03 > 0, FN_NUMBER_CAST((B.pr_nf_03*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T04',IIF(B.pr_nf_04 > 0, FN_NUMBER_CAST((B.pr_nf_04*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T05',IIF(B.pr_nf_05 > 0, FN_NUMBER_CAST((B.pr_nf_05*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T06',IIF(B.pr_nf_06 > 0, FN_NUMBER_CAST((B.pr_nf_06*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T07',IIF(B.pr_nf_07 > 0, FN_NUMBER_CAST((B.pr_nf_07*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T08',IIF(B.pr_nf_08 > 0, FN_NUMBER_CAST((B.pr_nf_08*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T09',IIF(B.pr_nf_09 > 0, FN_NUMBER_CAST((B.pr_nf_09*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T10',IIF(B.pr_nf_10 > 0, FN_NUMBER_CAST((B.pr_nf_10*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T11',IIF(B.pr_nf_11 > 0, FN_NUMBER_CAST((B.pr_nf_11*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T12',IIF(B.pr_nf_12 > 0, FN_NUMBER_CAST((B.pr_nf_12*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T13',IIF(B.pr_nf_13 > 0, FN_NUMBER_CAST((B.pr_nf_13*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T14',IIF(B.pr_nf_14 > 0, FN_NUMBER_CAST((B.pr_nf_14*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T15',IIF(B.pr_nf_15 > 0, FN_NUMBER_CAST((B.pr_nf_15*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T16',IIF(B.pr_nf_16 > 0, FN_NUMBER_CAST((B.pr_nf_16*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T17',IIF(B.pr_nf_17 > 0, FN_NUMBER_CAST((B.pr_nf_17*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T18',IIF(B.pr_nf_18 > 0, FN_NUMBER_CAST((B.pr_nf_18*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T19',IIF(B.pr_nf_19 > 0, FN_NUMBER_CAST((B.pr_nf_19*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T20',IIF(B.pr_nf_20 > 0, FN_NUMBER_CAST((B.pr_nf_20*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0))
                            ),
                        ',')||']' LISTA_PRECOS,
                    :TABELA_VALIDA AS VALIDO

                FROM
                    (
                        SELECT FIRST 1
                         COALESCE(C.PERCENTUAL_BASE,0.00) AS PERCENTUAL_BASE,
                         COALESCE(C.DECIMAIS_BASE,2) AS DECIMAIS_BASE,
                         B.pr_nf_01,
                         B.pr_nf_02,
                         B.pr_nf_03,
                         B.pr_nf_04,
                         B.pr_nf_05,
                         B.pr_nf_06,
                         B.pr_nf_07,
                         B.pr_nf_08,
                         B.pr_nf_09,
                         B.pr_nf_10,
                         B.pr_nf_11,
                         B.pr_nf_12,
                         B.pr_nf_13,
                         B.pr_nf_14,
                         B.pr_nf_15,
                         B.pr_nf_16,
                         B.pr_nf_17,
                         B.pr_nf_18,
                         B.pr_nf_19,
                         B.pr_nf_20
                         FROM TBCLIENTE_PRODUTO_PRECO B, TBTABELA_PRECO C
                         WHERE :TABELA_PRECO = 1 AND B.CLIENTE_CODIGO IN --(SELECT C.CODIGO FROM TBCLIENTE C WHERE C.EMPRESA_VINCULO = :EMPRESA_VINCULO)
                          (SELECT C.CODIGO FROM TBCLIENTE C, TBEMPRESA_GRUPO_CONTAS W WHERE W.EMPRESA_ID = C.CODIGO AND W.EMPRESA_GRUPO_ID = :EMPRESA_VINCULO)
                         AND B.PRODUTO_CODIGO    = :PRODUTO_ID
                         AND B.TABELA_ID        = C.ID
                         AND B.TABELA_ID        = (SELECT FIRST 1 C.TABELA_PRECO_ID_BASE FROM TBTABELA_PRECO C WHERE C.ID = :PRECO_NF_CF_ID)) B

                
                UNION

                
                SELECT
                             
                    2 TIPO,
                    '['||LIST(
                            fn_to_json_v3(
                                fn_to_par_json_v2('T01',IIF(B.pr_nf_01 > 0, B.pr_nf_01 , 0)),
                                fn_to_par_json_v2('T02',IIF(B.pr_nf_02 > 0, B.pr_nf_02 , 0)),
                                fn_to_par_json_v2('T03',IIF(B.pr_nf_03 > 0, B.pr_nf_03 , 0)),
                                fn_to_par_json_v2('T04',IIF(B.pr_nf_04 > 0, B.pr_nf_04 , 0)),
                                fn_to_par_json_v2('T05',IIF(B.pr_nf_05 > 0, B.pr_nf_05 , 0)),
                                fn_to_par_json_v2('T06',IIF(B.pr_nf_06 > 0, B.pr_nf_06 , 0)),
                                fn_to_par_json_v2('T07',IIF(B.pr_nf_07 > 0, B.pr_nf_07 , 0)),
                                fn_to_par_json_v2('T08',IIF(B.pr_nf_08 > 0, B.pr_nf_08 , 0)),
                                fn_to_par_json_v2('T09',IIF(B.pr_nf_09 > 0, B.pr_nf_09 , 0)),
                                fn_to_par_json_v2('T10',IIF(B.pr_nf_10 > 0, B.pr_nf_10 , 0)),
                                fn_to_par_json_v2('T11',IIF(B.pr_nf_11 > 0, B.pr_nf_11 , 0)),
                                fn_to_par_json_v2('T12',IIF(B.pr_nf_12 > 0, B.pr_nf_12 , 0)),
                                fn_to_par_json_v2('T13',IIF(B.pr_nf_13 > 0, B.pr_nf_13 , 0)),
                                fn_to_par_json_v2('T14',IIF(B.pr_nf_14 > 0, B.pr_nf_14 , 0)),
                                fn_to_par_json_v2('T15',IIF(B.pr_nf_15 > 0, B.pr_nf_15 , 0)),
                                fn_to_par_json_v2('T16',IIF(B.pr_nf_16 > 0, B.pr_nf_16 , 0)),
                                fn_to_par_json_v2('T17',IIF(B.pr_nf_17 > 0, B.pr_nf_17 , 0)),
                                fn_to_par_json_v2('T18',IIF(B.pr_nf_18 > 0, B.pr_nf_18 , 0)),
                                fn_to_par_json_v2('T19',IIF(B.pr_nf_19 > 0, B.pr_nf_19 , 0)),
                                fn_to_par_json_v2('T20',IIF(B.pr_nf_20 > 0, B.pr_nf_20 , 0))
                            ),
                    ',')||']' LISTA_PRECOS,
                    :TABELA_VALIDA AS VALIDO
                
                FROM  TBCLIENTE_MODELO_PRECO  B
                WHERE (B.CLIENTE_CODIGO = :CLIENTE_ID AND B.MODELO_CODIGO  = :MODELO_ID AND B.TABELA_ID = :PRECO_NF_CF_ID)


               UNION
                                        
                   SELECT
                        2 TIPO,
                         '['||LIST(
                            fn_to_json_v3(
                                fn_to_par_json_v2('T01',IIF(B.pr_nf_01 > 0, FN_NUMBER_CAST((B.pr_nf_01*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T02',IIF(B.pr_nf_02 > 0, FN_NUMBER_CAST((B.pr_nf_02*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T03',IIF(B.pr_nf_03 > 0, FN_NUMBER_CAST((B.pr_nf_03*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T04',IIF(B.pr_nf_04 > 0, FN_NUMBER_CAST((B.pr_nf_04*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T05',IIF(B.pr_nf_05 > 0, FN_NUMBER_CAST((B.pr_nf_05*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T06',IIF(B.pr_nf_06 > 0, FN_NUMBER_CAST((B.pr_nf_06*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T07',IIF(B.pr_nf_07 > 0, FN_NUMBER_CAST((B.pr_nf_07*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T08',IIF(B.pr_nf_08 > 0, FN_NUMBER_CAST((B.pr_nf_08*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T09',IIF(B.pr_nf_09 > 0, FN_NUMBER_CAST((B.pr_nf_09*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T10',IIF(B.pr_nf_10 > 0, FN_NUMBER_CAST((B.pr_nf_10*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T11',IIF(B.pr_nf_11 > 0, FN_NUMBER_CAST((B.pr_nf_11*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T12',IIF(B.pr_nf_12 > 0, FN_NUMBER_CAST((B.pr_nf_12*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T13',IIF(B.pr_nf_13 > 0, FN_NUMBER_CAST((B.pr_nf_13*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T14',IIF(B.pr_nf_14 > 0, FN_NUMBER_CAST((B.pr_nf_14*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T15',IIF(B.pr_nf_15 > 0, FN_NUMBER_CAST((B.pr_nf_15*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T16',IIF(B.pr_nf_16 > 0, FN_NUMBER_CAST((B.pr_nf_16*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T17',IIF(B.pr_nf_17 > 0, FN_NUMBER_CAST((B.pr_nf_17*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T18',IIF(B.pr_nf_18 > 0, FN_NUMBER_CAST((B.pr_nf_18*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T19',IIF(B.pr_nf_19 > 0, FN_NUMBER_CAST((B.pr_nf_19*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                                fn_to_par_json_v2('T20',IIF(B.pr_nf_20 > 0, FN_NUMBER_CAST((B.pr_nf_20*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0))
                            ),
                        ',')||']' LISTA_PRECOS,
                        :TABELA_VALIDA AS VALIDO
                    
                    FROM TBCLIENTE_MODELO_PRECO  B
                    WHERE (B.CLIENTE_CODIGO = :CLIENTE_ID AND B.MODELO_CODIGO  = :MODELO_ID AND B.TABELA_ID = (SELECT FIRST 1 C.TABELA_PRECO_ID_BASE FROM TBTABELA_PRECO C WHERE C.ID = :PRECO_NF_CF_ID))

                UNION

                    SELECT
                                 
                        2 TIPO,
                        '['||LIST(
                            fn_to_json_v3(
                                fn_to_par_json_v2('T01',IIF(B.pr_nf_01 > 0, B.pr_nf_01 , 0)),
                                fn_to_par_json_v2('T02',IIF(B.pr_nf_02 > 0, B.pr_nf_02 , 0)),
                                fn_to_par_json_v2('T03',IIF(B.pr_nf_03 > 0, B.pr_nf_03 , 0)),
                                fn_to_par_json_v2('T04',IIF(B.pr_nf_04 > 0, B.pr_nf_04 , 0)),
                                fn_to_par_json_v2('T05',IIF(B.pr_nf_05 > 0, B.pr_nf_05 , 0)),
                                fn_to_par_json_v2('T06',IIF(B.pr_nf_06 > 0, B.pr_nf_06 , 0)),
                                fn_to_par_json_v2('T07',IIF(B.pr_nf_07 > 0, B.pr_nf_07 , 0)),
                                fn_to_par_json_v2('T08',IIF(B.pr_nf_08 > 0, B.pr_nf_08 , 0)),
                                fn_to_par_json_v2('T09',IIF(B.pr_nf_09 > 0, B.pr_nf_09 , 0)),
                                fn_to_par_json_v2('T10',IIF(B.pr_nf_10 > 0, B.pr_nf_10 , 0)),
                                fn_to_par_json_v2('T11',IIF(B.pr_nf_11 > 0, B.pr_nf_11 , 0)),
                                fn_to_par_json_v2('T12',IIF(B.pr_nf_12 > 0, B.pr_nf_12 , 0)),
                                fn_to_par_json_v2('T13',IIF(B.pr_nf_13 > 0, B.pr_nf_13 , 0)),
                                fn_to_par_json_v2('T14',IIF(B.pr_nf_14 > 0, B.pr_nf_14 , 0)),
                                fn_to_par_json_v2('T15',IIF(B.pr_nf_15 > 0, B.pr_nf_15 , 0)),
                                fn_to_par_json_v2('T16',IIF(B.pr_nf_16 > 0, B.pr_nf_16 , 0)),
                                fn_to_par_json_v2('T17',IIF(B.pr_nf_17 > 0, B.pr_nf_17 , 0)),
                                fn_to_par_json_v2('T18',IIF(B.pr_nf_18 > 0, B.pr_nf_18 , 0)),
                                fn_to_par_json_v2('T19',IIF(B.pr_nf_19 > 0, B.pr_nf_19 , 0)),
                                fn_to_par_json_v2('T20',IIF(B.pr_nf_20 > 0, B.pr_nf_20 , 0))
                            ),
                        ',')||']' LISTA_PRECOS,
                        :TABELA_VALIDA AS VALIDO
                    
                    FROM (SELECT FIRST 1
                             COALESCE(C.PERCENTUAL_BASE,0.00) AS PERCENTUAL_BASE,
                             COALESCE(C.DECIMAIS_BASE,2) AS DECIMAIS_BASE,
                             B.pr_nf_01,
                             B.pr_nf_02,
                             B.pr_nf_03,
                             B.pr_nf_04,
                             B.pr_nf_05,
                             B.pr_nf_06,
                             B.pr_nf_07,
                             B.pr_nf_08,
                             B.pr_nf_09,
                             B.pr_nf_10,
                             B.pr_nf_11,
                             B.pr_nf_12,
                             B.pr_nf_13,
                             B.pr_nf_14,
                             B.pr_nf_15,
                             B.pr_nf_16,
                             B.pr_nf_17,
                             B.pr_nf_18,
                             B.pr_nf_19,
                             B.pr_nf_20
                             FROM TBCLIENTE_MODELO_PRECO B, TBTABELA_PRECO C
                             WHERE :TABELA_PRECO = 1 AND B.CLIENTE_CODIGO IN --(SELECT C.CODIGO FROM TBCLIENTE C WHERE C.EMPRESA_VINCULO = :EMPRESA_VINCULO)
                              (SELECT C.CODIGO FROM TBCLIENTE C, TBEMPRESA_GRUPO_CONTAS W WHERE W.EMPRESA_ID = C.CODIGO AND W.EMPRESA_GRUPO_ID = :EMPRESA_VINCULO)
                             AND B.MODELO_CODIGO     = :MODELO_ID
                             AND B.TABELA_ID         = C.ID
                             AND B.TABELA_ID        = :PRECO_NF_CF_ID) B
    
                UNION

                  SELECT
                    2 TIPO,
                    '['||LIST(
                        fn_to_json_v3(
                            fn_to_par_json_v2('T01',IIF(B.pr_nf_01 > 0, FN_NUMBER_CAST((B.pr_nf_01*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T02',IIF(B.pr_nf_02 > 0, FN_NUMBER_CAST((B.pr_nf_02*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T03',IIF(B.pr_nf_03 > 0, FN_NUMBER_CAST((B.pr_nf_03*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T04',IIF(B.pr_nf_04 > 0, FN_NUMBER_CAST((B.pr_nf_04*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T05',IIF(B.pr_nf_05 > 0, FN_NUMBER_CAST((B.pr_nf_05*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T06',IIF(B.pr_nf_06 > 0, FN_NUMBER_CAST((B.pr_nf_06*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T07',IIF(B.pr_nf_07 > 0, FN_NUMBER_CAST((B.pr_nf_07*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T08',IIF(B.pr_nf_08 > 0, FN_NUMBER_CAST((B.pr_nf_08*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T09',IIF(B.pr_nf_09 > 0, FN_NUMBER_CAST((B.pr_nf_09*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T10',IIF(B.pr_nf_10 > 0, FN_NUMBER_CAST((B.pr_nf_10*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T11',IIF(B.pr_nf_11 > 0, FN_NUMBER_CAST((B.pr_nf_11*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T12',IIF(B.pr_nf_12 > 0, FN_NUMBER_CAST((B.pr_nf_12*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T13',IIF(B.pr_nf_13 > 0, FN_NUMBER_CAST((B.pr_nf_13*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T14',IIF(B.pr_nf_14 > 0, FN_NUMBER_CAST((B.pr_nf_14*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T15',IIF(B.pr_nf_15 > 0, FN_NUMBER_CAST((B.pr_nf_15*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T16',IIF(B.pr_nf_16 > 0, FN_NUMBER_CAST((B.pr_nf_16*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T17',IIF(B.pr_nf_17 > 0, FN_NUMBER_CAST((B.pr_nf_17*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T18',IIF(B.pr_nf_18 > 0, FN_NUMBER_CAST((B.pr_nf_18*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T19',IIF(B.pr_nf_19 > 0, FN_NUMBER_CAST((B.pr_nf_19*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0)),
                            fn_to_par_json_v2('T20',IIF(B.pr_nf_20 > 0, FN_NUMBER_CAST((B.pr_nf_20*:PERCENTUAL_BASE),:DECIMAIS_BASE), 0))
                        ),
                    ',')||']' LISTA_PRECOS,
                    :TABELA_VALIDA AS VALIDO
                    
                    FROM (SELECT FIRST 1
                         COALESCE(C.PERCENTUAL_BASE,0.00) AS PERCENTUAL_BASE,
                         COALESCE(C.DECIMAIS_BASE,2) AS DECIMAIS_BASE,
                         B.pr_nf_01,
                         B.pr_nf_02,
                         B.pr_nf_03,
                         B.pr_nf_04,
                         B.pr_nf_05,
                         B.pr_nf_06,
                         B.pr_nf_07,
                         B.pr_nf_08,
                         B.pr_nf_09,
                         B.pr_nf_10,
                         B.pr_nf_11,
                         B.pr_nf_12,
                         B.pr_nf_13,
                         B.pr_nf_14,
                         B.pr_nf_15,
                         B.pr_nf_16,
                         B.pr_nf_17,
                         B.pr_nf_18,
                         B.pr_nf_19,
                         B.pr_nf_20
                         FROM TBCLIENTE_MODELO_PRECO B, TBTABELA_PRECO C
                         WHERE :TABELA_PRECO = 1 AND (B.CLIENTE_CODIGO IN --(SELECT C.CODIGO FROM TBCLIENTE C WHERE C.EMPRESA_VINCULO = :EMPRESA_VINCULO)
                          (SELECT C.CODIGO FROM TBCLIENTE C, TBEMPRESA_GRUPO_CONTAS W WHERE W.EMPRESA_ID = C.CODIGO AND W.EMPRESA_GRUPO_ID = :EMPRESA_VINCULO)
                         AND B.MODELO_CODIGO     = :MODELO_ID
                         AND B.TABELA_ID         = C.ID
                         AND (B.TABELA_ID        = (SELECT FIRST 1 C.TABELA_PRECO_ID_BASE FROM TBTABELA_PRECO C WHERE C.ID = :PRECO_NF_CF_ID)))) B

               UNION

                SELECT
                             
                    3 TIPO,
                    '['||LIST(
                            fn_to_json_v3(
                                fn_to_par_json_v2('T01',IIF(B.pr_nf_01 > 0, B.pr_nf_01 , 0)),
                                fn_to_par_json_v2('T02',IIF(B.pr_nf_02 > 0, B.pr_nf_02 , 0)),
                                fn_to_par_json_v2('T03',IIF(B.pr_nf_03 > 0, B.pr_nf_03 , 0)),
                                fn_to_par_json_v2('T04',IIF(B.pr_nf_04 > 0, B.pr_nf_04 , 0)),
                                fn_to_par_json_v2('T05',IIF(B.pr_nf_05 > 0, B.pr_nf_05 , 0)),
                                fn_to_par_json_v2('T06',IIF(B.pr_nf_06 > 0, B.pr_nf_06 , 0)),
                                fn_to_par_json_v2('T07',IIF(B.pr_nf_07 > 0, B.pr_nf_07 , 0)),
                                fn_to_par_json_v2('T08',IIF(B.pr_nf_08 > 0, B.pr_nf_08 , 0)),
                                fn_to_par_json_v2('T09',IIF(B.pr_nf_09 > 0, B.pr_nf_09 , 0)),
                                fn_to_par_json_v2('T10',IIF(B.pr_nf_10 > 0, B.pr_nf_10 , 0)),
                                fn_to_par_json_v2('T11',IIF(B.pr_nf_11 > 0, B.pr_nf_11 , 0)),
                                fn_to_par_json_v2('T12',IIF(B.pr_nf_12 > 0, B.pr_nf_12 , 0)),
                                fn_to_par_json_v2('T13',IIF(B.pr_nf_13 > 0, B.pr_nf_13 , 0)),
                                fn_to_par_json_v2('T14',IIF(B.pr_nf_14 > 0, B.pr_nf_14 , 0)),
                                fn_to_par_json_v2('T15',IIF(B.pr_nf_15 > 0, B.pr_nf_15 , 0)),
                                fn_to_par_json_v2('T16',IIF(B.pr_nf_16 > 0, B.pr_nf_16 , 0)),
                                fn_to_par_json_v2('T17',IIF(B.pr_nf_17 > 0, B.pr_nf_17 , 0)),
                                fn_to_par_json_v2('T18',IIF(B.pr_nf_18 > 0, B.pr_nf_18 , 0)),
                                fn_to_par_json_v2('T19',IIF(B.pr_nf_19 > 0, B.pr_nf_19 , 0)),
                                fn_to_par_json_v2('T20',IIF(B.pr_nf_20 > 0, B.pr_nf_20 , 0))
                            ),
                        ',')||']' LISTA_PRECOS,
                        :VALIDO AS VALIDO

                FROM TBPRODUTO_PRECO   B
                WHERE B.ESTABELECIMENTO_CODIGO = :ESTABELECIMENTO_ID AND B.PRODUTO_CODIGO = :PRODUTO_ID AND B.TABELA_ID = :PRECO_NF_CF_ID


                UNION
                
                SELECT
                             
                    3 TIPO,
                      '['||LIST(
                        fn_to_json_v3(
                            fn_to_par_json_v2('T01',IIF(B.pr_nf_01 > 0, FN_NUMBER_CAST((B.pr_nf_01*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T02',IIF(B.pr_nf_02 > 0, FN_NUMBER_CAST((B.pr_nf_02*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T03',IIF(B.pr_nf_03 > 0, FN_NUMBER_CAST((B.pr_nf_03*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T04',IIF(B.pr_nf_04 > 0, FN_NUMBER_CAST((B.pr_nf_04*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T05',IIF(B.pr_nf_05 > 0, FN_NUMBER_CAST((B.pr_nf_05*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T06',IIF(B.pr_nf_06 > 0, FN_NUMBER_CAST((B.pr_nf_06*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T07',IIF(B.pr_nf_07 > 0, FN_NUMBER_CAST((B.pr_nf_07*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T08',IIF(B.pr_nf_08 > 0, FN_NUMBER_CAST((B.pr_nf_08*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T09',IIF(B.pr_nf_09 > 0, FN_NUMBER_CAST((B.pr_nf_09*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T10',IIF(B.pr_nf_10 > 0, FN_NUMBER_CAST((B.pr_nf_10*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T11',IIF(B.pr_nf_11 > 0, FN_NUMBER_CAST((B.pr_nf_11*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T12',IIF(B.pr_nf_12 > 0, FN_NUMBER_CAST((B.pr_nf_12*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T13',IIF(B.pr_nf_13 > 0, FN_NUMBER_CAST((B.pr_nf_13*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T14',IIF(B.pr_nf_14 > 0, FN_NUMBER_CAST((B.pr_nf_14*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T15',IIF(B.pr_nf_15 > 0, FN_NUMBER_CAST((B.pr_nf_15*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T16',IIF(B.pr_nf_16 > 0, FN_NUMBER_CAST((B.pr_nf_16*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T17',IIF(B.pr_nf_17 > 0, FN_NUMBER_CAST((B.pr_nf_17*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T18',IIF(B.pr_nf_18 > 0, FN_NUMBER_CAST((B.pr_nf_18*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T19',IIF(B.pr_nf_19 > 0, FN_NUMBER_CAST((B.pr_nf_19*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                            fn_to_par_json_v2('T20',IIF(B.pr_nf_20 > 0, FN_NUMBER_CAST((B.pr_nf_20*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0))
                        ),
                    ',')||']' LISTA_PRECOS,
                        :VALIDO AS VALIDO

                FROM TBPRODUTO_PRECO   B
                WHERE B.ESTABELECIMENTO_CODIGO = :ESTABELECIMENTO_ID AND B.PRODUTO_CODIGO = :PRODUTO_ID AND B.TABELA_ID = (SELECT FIRST 1 C.TABELA_PRECO_ID_BASE FROM TBTABELA_PRECO C WHERE C.ID = :PRECO_NF_CF_ID)

                                            
                UNION

                    SELECT
                                 
                        4 TIPO,
                        '['||LIST(
                            fn_to_json_v3(
                                fn_to_par_json_v2('T01',IIF(B.pr_nf_01 > 0, B.pr_nf_01 , 0)),
                                fn_to_par_json_v2('T02',IIF(B.pr_nf_02 > 0, B.pr_nf_02 , 0)),
                                fn_to_par_json_v2('T03',IIF(B.pr_nf_03 > 0, B.pr_nf_03 , 0)),
                                fn_to_par_json_v2('T04',IIF(B.pr_nf_04 > 0, B.pr_nf_04 , 0)),
                                fn_to_par_json_v2('T05',IIF(B.pr_nf_05 > 0, B.pr_nf_05 , 0)),
                                fn_to_par_json_v2('T06',IIF(B.pr_nf_06 > 0, B.pr_nf_06 , 0)),
                                fn_to_par_json_v2('T07',IIF(B.pr_nf_07 > 0, B.pr_nf_07 , 0)),
                                fn_to_par_json_v2('T08',IIF(B.pr_nf_08 > 0, B.pr_nf_08 , 0)),
                                fn_to_par_json_v2('T09',IIF(B.pr_nf_09 > 0, B.pr_nf_09 , 0)),
                                fn_to_par_json_v2('T10',IIF(B.pr_nf_10 > 0, B.pr_nf_10 , 0)),
                                fn_to_par_json_v2('T11',IIF(B.pr_nf_11 > 0, B.pr_nf_11 , 0)),
                                fn_to_par_json_v2('T12',IIF(B.pr_nf_12 > 0, B.pr_nf_12 , 0)),
                                fn_to_par_json_v2('T13',IIF(B.pr_nf_13 > 0, B.pr_nf_13 , 0)),
                                fn_to_par_json_v2('T14',IIF(B.pr_nf_14 > 0, B.pr_nf_14 , 0)),
                                fn_to_par_json_v2('T15',IIF(B.pr_nf_15 > 0, B.pr_nf_15 , 0)),
                                fn_to_par_json_v2('T16',IIF(B.pr_nf_16 > 0, B.pr_nf_16 , 0)),
                                fn_to_par_json_v2('T17',IIF(B.pr_nf_17 > 0, B.pr_nf_17 , 0)),
                                fn_to_par_json_v2('T18',IIF(B.pr_nf_18 > 0, B.pr_nf_18 , 0)),
                                fn_to_par_json_v2('T19',IIF(B.pr_nf_19 > 0, B.pr_nf_19 , 0)),
                                fn_to_par_json_v2('T20',IIF(B.pr_nf_20 > 0, B.pr_nf_20 , 0))
                            ),
                        ',')||']' LISTA_PRECOS,
                        :VALIDO AS VALIDO
                    
                        FROM TBMODELO_PRECO B
                        WHERE B.ESTABELECIMENTO_CODIGO = :ESTABELECIMENTO_ID AND B.MODELO_CODIGO = :MODELO_ID AND B.TABELA_ID = :PRECO_NF_CF_ID

                        GROUP BY B.ESTABELECIMENTO_CODIGO, B.MODELO_CODIGO, B.TABELA_ID

                    
                UNION
                
                SELECT
                             
                    4 TIPO,
                        '['||LIST(
                                fn_to_json_v3(
                                    fn_to_par_json_v2('T01',IIF(B.pr_nf_01 > 0, FN_NUMBER_CAST((B.pr_nf_01*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T02',IIF(B.pr_nf_02 > 0, FN_NUMBER_CAST((B.pr_nf_02*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T03',IIF(B.pr_nf_03 > 0, FN_NUMBER_CAST((B.pr_nf_03*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T04',IIF(B.pr_nf_04 > 0, FN_NUMBER_CAST((B.pr_nf_04*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T05',IIF(B.pr_nf_05 > 0, FN_NUMBER_CAST((B.pr_nf_05*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T06',IIF(B.pr_nf_06 > 0, FN_NUMBER_CAST((B.pr_nf_06*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T07',IIF(B.pr_nf_07 > 0, FN_NUMBER_CAST((B.pr_nf_07*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T08',IIF(B.pr_nf_08 > 0, FN_NUMBER_CAST((B.pr_nf_08*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T09',IIF(B.pr_nf_09 > 0, FN_NUMBER_CAST((B.pr_nf_09*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T10',IIF(B.pr_nf_10 > 0, FN_NUMBER_CAST((B.pr_nf_10*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T11',IIF(B.pr_nf_11 > 0, FN_NUMBER_CAST((B.pr_nf_11*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T12',IIF(B.pr_nf_12 > 0, FN_NUMBER_CAST((B.pr_nf_12*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T13',IIF(B.pr_nf_13 > 0, FN_NUMBER_CAST((B.pr_nf_13*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T14',IIF(B.pr_nf_14 > 0, FN_NUMBER_CAST((B.pr_nf_14*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T15',IIF(B.pr_nf_15 > 0, FN_NUMBER_CAST((B.pr_nf_15*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T16',IIF(B.pr_nf_16 > 0, FN_NUMBER_CAST((B.pr_nf_16*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T17',IIF(B.pr_nf_17 > 0, FN_NUMBER_CAST((B.pr_nf_17*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T18',IIF(B.pr_nf_18 > 0, FN_NUMBER_CAST((B.pr_nf_18*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T19',IIF(B.pr_nf_19 > 0, FN_NUMBER_CAST((B.pr_nf_19*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0)),
                                    fn_to_par_json_v2('T20',IIF(B.pr_nf_20 > 0, FN_NUMBER_CAST((B.pr_nf_20*:PERCENTUAL_BASE),:DECIMAIS_BASE) , 0))
                                ),
                            ',')||']' LISTA_PRECOS,
                        :VALIDO AS VALIDO
                    
                        FROM TBMODELO_PRECO B
                        WHERE B.ESTABELECIMENTO_CODIGO = :ESTABELECIMENTO_ID AND B.MODELO_CODIGO = :MODELO_ID AND B.TABELA_ID = (SELECT FIRST 1 C.TABELA_PRECO_ID_BASE FROM TBTABELA_PRECO C WHERE C.ID = :PRECO_NF_CF_ID)
                       GROUP BY B.ESTABELECIMENTO_CODIGO, B.MODELO_CODIGO, B.TABELA_ID

                ) X
        WHERE LISTA_PRECOS IS NOT NULL

    INTO
        :TIPO,
        :LISTA_PRECOS,
        :VALIDO
    DO
    SUSPEND;
END
^

SET TERM ; ^

