Performance programa com BOs

Discussão em 'Progress 4GL' iniciado por diegocf, Outubro 29, 2010.

  1. diegocf

    diegocf Membro Participativo

    Boa tarde meus caros amigos,

    Ca estou novamente, desta vez estou com um relatorio de notas fiscas x fretes do TMS, este relatorio foi desenvolvido por um analisata a alguns anos atras e a performance dele é muito lenta, mas muito mesmo, sem brincadeira fiz um teste aqui e demorou 1 dia para gerar 2 meses de dados, e nao tem tanto dado assim.

    Eu vi que o programa usa chamada de BOs da Datasul. Fiz varios testes e pelo que vi é ela que faz ficar muito lento, fiz um programa a parte com consulta direto nas tabelas, e ele fez 1000 mais rapido, mas quando chegou na parte que ele traz o valor do frete e faz um rateio para (1 frete x varias notas) nmao consegui achar uma forma de fazer, somente quando colocar a BO de novo ele faz o calculo. Sinceramente tenho muito conhecimento de BOS, entao queria saber como posso melhorar o programa e talvez nao usar estas BOs caso alguem saiba quais sao os campos.

    Eis o codigo:

    Código:
    def temp-table tt-valor-frete no-undo
        field rw-nf       as rowid
        field rw-precon   as rowid
        field d-tot-frete as dec.
    
    define temp-table tt-pre-con like pre-con
        field r-rowid as rowid.
        
    define temp-table tt-nota-fiscal-tr like nota-fiscal-tr
        field r-rowid     as rowid
        field nr-calculo  like pre-con.nr-calculo
        field dt-calculo  like pre-con.dt-criacao
        field nr-romaneio like romaneio-docto.nr-romaneio.
    
    define temp-table tt-documento-frete
        field cnpj-emit         like mgtms.docto-frete.cnpj-emit
        field dt-criacao        like mgtms.docto-frete.dt-criacao
        field dt-integr-of      like mgtms.docto-frete.dt-integr-of
        field dt-emissao-docto  like mgtms.docto-frete.dt-emissao-docto
        field nr-documento      like mgtms.docto-frete.nr-documento
        field serie             like mgtms.docto-frete.serie
        field nr-nf             like nota-fiscal-tr.nr-nf
        field cod-estabel       like nota-fiscal-tr.cod-estabel
        field cd-serie          like nota-fiscal-tr.cd-serie
        field id-tipo           like mgtms.docto-frete.id-tipo
        field vl-conhecimento   like mgtms.docto-frete.vl-conhecimento
        field nr-fatura         like mgtms.fatura-frete.nr-fatura
        field dt-emis-fatura    like mgtms.fatura-frete.dt-emissao
        field dt-venct-fatura   like mgtms.fatura-frete.dt-vencto
        field dt-integr-ap      like mgtms.fatura-frete.dt-integr-ap
        field dt-pagto-fat      like tit_ap.dat_liquidac_tit_ap.
    
    define buffer b-nota-fiscal-tr for nota-fiscal-tr.
    define buffer b-docto-frete    for mgtms.docto-frete.
    define buffer b-docto-frete-nf for mgtms.docto-frete-nf.
    
    define variable h-botr098q01      as handle                       no-undo.
    define variable h-botr098rat      as handle                       no-undo.
    define variable h-acomp           as handle                       no-undo.
    define variable c-cod-estabel-ini like nota-fiscal-tr.cod-estabel no-undo label 'Estab.'    initial ''.
    define variable c-cod-estabel-fim like nota-fiscal-tr.cod-estabel no-undo label ''          initial 'ZZZ'.
    define variable da-data-ini       like nota-fiscal-tr.dt-emissao  no-undo label 'Data'      initial 01/01/2000.
    define variable da-data-fim       like nota-fiscal-tr.dt-emissao  no-undo label ''          initial 12/31/2999.
    define variable d-tot-frete       as decimal                      no-undo.
    define variable d-tot-frete-notas as decimal                      no-undo.
    define variable d-tot-frete-calc  as decimal                      no-undo.
    define variable d-perc-frete-nfs  as decimal                      no-undo.
    define variable c-arquivo-destino as character                    no-undo.
    define variable ch-excel          as com-handle                   no-undo.
    define variable h-botr118         as handle                       no-undo.
    define variable i-nr-romaneio     as integer                      no-undo.
    define variable c-tipo-docto      as character                    no-undo.
    define variable i-nr-linha        as integer                      no-undo.
    
    update c-cod-estabel-ini at 8.5   '         ====> ' 
           c-cod-estabel-fim at 40                              skip
           da-data-ini       at 10    '====> '
           da-data-fim       at 40                              skip(1)
           'Tecle ENTER na data final para iniciar a execucao'  skip(1)
           'O arquivo ser  gravado em  c:\fretes_geral.csv'     
           with frame a overlay three-d view-as dialog-box side-labels title 'Informe os Dados:  Lista Vlr Frete x Docto Frete x Fatura'.
    
    run trbo/botr098q01.p persistent set h-botr098q01.
    run trbo/botr098rat.p persistent set h-botr098rat.
    run trbo/botr118.p    persistent set h-botr118.
    
    run utp/ut-acomp.p persistent set h-acomp.
    run pi-inicializar in h-acomp (input 'Verificando...').
    
    assign c-arquivo-destino = 'c:\fretes_geral.csv'   /* 'y:\douglas.lima\fretes_geral.csv' */
           i-nr-linha        = 1.
    
    output to value(c-arquivo-destino).
    
    put 'Estabel;Nro Nota;Serie;Emissao;Embarque (TMS);Vl Total Nota (ERP);C¢d. Rep;Nome Abrev Rep;Cidade;UF;NR Calculo;Dt Calculo;Calculo Frete Total;Docto Frete;Serie;Tipo CTRC;Data Emissao;Dt Intreg OF;Data Entrada;Valor CTRC;Fatura;Dt Emis Fatura;Dt Integr APB;Dt Vencto Fatura;Dt Pagto Fat;;'  skip.
    
    for  each nota-fiscal-tr no-lock
        where nota-fiscal-tr.dt-emissao  >= da-data-ini
          and nota-fiscal-tr.dt-emissao  <= da-data-fim
          and nota-fiscal-tr.cod-estabel >= c-cod-estabel-ini
          and nota-fiscal-tr.cod-estabel <= c-cod-estabel-fim
          and nota-fiscal-tr.dt-cancela   = ?:
    
        run pi-acompanhar in h-acomp (input 'Data:  '  + string(nota-fiscal-tr.dt-emissao)
                                          + ' - NF: '  + string(nota-fiscal-tr.nr-nf)
                                          + ' - Est: ' + nota-fiscal-tr.cod-estabel).
    
        empty temp-table tt-nota-fiscal-tr.
        empty temp-table tt-documento-frete.    
        empty temp-table tt-valor-frete.
        
        assign i-nr-romaneio = 0.
    
        create tt-nota-fiscal-tr.
        buffer-copy nota-fiscal-tr to tt-nota-fiscal-tr.
        assign tt-nota-fiscal-tr.r-rowid = rowid(nota-fiscal-tr).
    
        run pi-calc-frete (input nota-fiscal-tr.cgc-rem,
                           input nota-fiscal-tr.nr-nf,
                           input nota-fiscal-tr.cd-serie,
                           input rowid(nota-fiscal-tr)).
    
        assign d-tot-frete = d-tot-frete-calc.
        if d-tot-frete = 0 
        then next.
    
        run setConstraintNotaFiscal in h-botr118 (input tt-Nota-fiscal-tr.cgc-rem,
                                                  input tt-Nota-fiscal-tr.cd-serie,
                                                  input tt-Nota-fiscal-tr.nr-nf) no-error.
    
        run openQueryStatic in h-botr118 (input 'NotaFiscal':U) no-error.
    
        run GetIntField in h-botr118 (input  'nr-romaneio',
                                      output i-nr-romaneio).
    
        assign tt-nota-fiscal-tr.nr-romaneio = i-nr-romaneio.
    
        /*--- verificar documentos de frete ---*/
        for  each mgtms.docto-frete-nf no-lock
            where mgtms.docto-frete-nf.nr-nf    = nota-fiscal-tr.nr-nf
              and mgtms.docto-frete-nf.cd-serie = nota-fiscal-tr.cd-serie
              and mgtms.docto-frete-nf.cgc-rem  = nota-fiscal-tr.cgc-rem:
    
            /*--- C lculo do Rateio do Vlr CTRC pelas Notas Fiscais do CTRC ---*/
            assign d-tot-frete-notas = 0.
            for  each b-docto-frete-nf no-lock
                where b-docto-frete-nf.nr-documento = mgtms.docto-frete-nf.nr-documento
                  and b-docto-frete-nf.serie        = mgtms.docto-frete-nf.serie
                  and b-docto-frete-nf.cod-estabel  = mgtms.docto-frete-nf.cod-estabel
                  and rowid(b-docto-frete-nf)      <> rowid(mgtms.docto-frete-nf):
    
                find first b-nota-fiscal-tr no-lock
                     where b-nota-fiscal-tr.nr-nf    = b-docto-frete-nf.nr-nf   
                       and b-nota-fiscal-tr.cd-serie = b-docto-frete-nf.cd-serie
                       and b-nota-fiscal-tr.cgc-rem  = b-docto-frete-nf.cgc-rem no-error.
                       
                run pi-calc-frete (input b-nota-fiscal-tr.cgc-rem,
                                   input b-nota-fiscal-tr.nr-nf,
                                   input b-nota-fiscal-tr.cd-serie,
                                   input rowid(b-nota-fiscal-tr)).
    
                assign d-tot-frete-notas = d-tot-frete-calc.
                
            end.
            assign d-perc-frete-nfs  = d-tot-frete / (d-tot-frete-notas + d-tot-frete).
            
            find first mgtms.docto-frete of mgtms.docto-frete-nf no-error.
            if avail mgtms.docto-frete
            then do:
                find first tt-documento-frete 
                     where tt-documento-frete.nr-nf        = nota-fiscal-tr.nr-nf   
                       and tt-documento-frete.cd-serie     = nota-fiscal-tr.cd-serie
                       and tt-documento-frete.cod-estabel  = nota-fiscal-tr.cod-estabel
                       and tt-documento-frete.nr-documento = mgtms.docto-frete.nr-documento
                       and tt-documento-frete.serie        = mgtms.docto-frete.serie 
                       and tt-documento-frete.id-tipo      = mgtms.docto-frete.id-tipo no-error.
                if not avail tt-documento-frete
                then do:
                    create tt-documento-frete.
                    assign tt-documento-frete.nr-nf            = nota-fiscal-tr.nr-nf
                           tt-documento-frete.cd-serie         = nota-fiscal-tr.cd-serie
                           tt-documento-frete.cod-estabel      = nota-fiscal-tr.cod-estabel
                           tt-documento-frete.nr-documento     = mgtms.docto-frete.nr-documento
                           tt-documento-frete.cnpj-emit        = mgtms.docto-frete.cnpj-emit
                           tt-documento-frete.serie            = mgtms.docto-frete.serie
                           tt-documento-frete.id-tipo          = mgtms.docto-frete.id-tipo
                           tt-documento-frete.dt-criacao       = mgtms.docto-frete.dt-criacao
                           tt-documento-frete.dt-integr-of     = mgtms.docto-frete.dt-integr-of
                           tt-documento-frete.dt-emissao-docto = mgtms.docto-frete.dt-emissao-docto
                           tt-documento-frete.vl-conhecimento  = mgtms.docto-frete.vl-conhecimento * d-perc-frete-nfs.
                end.
            end.
        end.
        
        for  each tt-documento-frete:
    
            find first fatura-docto-frete no-lock use-index docto-frete 
                 where fatura-docto-frete.cnpj-emit        = tt-documento-frete.cnpj-emit
                   and fatura-docto-frete.serie            = tt-documento-frete.serie
                   and fatura-docto-frete.nr-documento     = tt-documento-frete.nr-documento 
                   /* and fatura-docto-frete.id-tp-docto      = tt-documento-frete.id-tipo */
                   and fatura-docto-frete.dt-emissao-docto = tt-documento-frete.dt-emissao-docto no-error.
            if avail fatura-docto-frete 
            then do:
                assign tt-documento-frete.nr-fatura = fatura-docto-frete.nr-fatura.
    
                find first mgtms.fatura-frete
                     where mgtms.fatura-frete.serie      = mgtms.fatura-docto-frete.serie-fat
                       and mgtms.fatura-frete.nr-fatura  = mgtms.fatura-docto-frete.nr-fatura
                       and mgtms.fatura-frete.cgc-transp = mgtms.fatura-docto-frete.cgc-transp no-error.
    
                if avail mgtms.fatura-frete
                then do:
                    assign tt-documento-frete.dt-emis-fatura  = mgtms.fatura-frete.dt-emissao
                           tt-documento-frete.dt-venct-fatura = mgtms.fatura-frete.dt-vencto
                           tt-documento-frete.dt-integr-ap    = mgtms.fatura-frete.dt-integr-ap.
                           
                    find first emitente no-lock
                         where emitente.cgc = mgtms.fatura-frete.cgc-transp no-error.
    
                    find first tit_ap no-lock
                         where tit_ap.cod_estab      = mgtms.fatura-frete.cod-estabel
                           and tit_ap.cdn_fornecedor = emitente.cod-emitente
                           and tit_ap.cod_ser_docto  = mgtms.fatura-frete.serie
                           and tit_ap.cod_tit_ap     = mgtms.fatura-frete.nr-fatura no-error.
                    if avail tit_ap
                    then assign tt-documento-frete.dt-pagto-fat = tit_ap.dat_liquidac_tit_ap.
                end.
                else assign tt-documento-frete.dt-emis-fatura  = ?
                            tt-documento-frete.dt-venct-fatura = ?
                            tt-documento-frete.dt-integr-ap    = ?
                            tt-documento-frete.dt-pagto-fat    = ?.
            end.
        end.
        
    
        find first nota-fiscal no-lock
             where nota-fiscal.nr-nota-fis = string(nota-fiscal-tr.nr-nf,'9999999')
               and nota-fiscal.serie       = nota-fiscal-tr.cd-serie
               and nota-fiscal.cod-estabel = nota-fiscal-tr.cod-estabel no-error.
    
        if can-find(first tt-documento-frete
                    where tt-documento-frete.nr-nf       = nota-fiscal-tr.nr-nf
                      and tt-documento-frete.cd-serie    = nota-fiscal-tr.cd-serie
                      and tt-documento-frete.cod-estabel = nota-fiscal-tr.cod-estabel)
        then do:
            for  each tt-documento-frete
                where tt-documento-frete.nr-nf       = nota-fiscal-tr.nr-nf   
                  and tt-documento-frete.cd-serie    = nota-fiscal-tr.cd-serie
                  and tt-documento-frete.cod-estabel = nota-fiscal-tr.cod-estabel:
                  
                run pi-imprime.
    
                put tt-documento-frete.nr-documento                                                                                                 ';'
                    tt-documento-frete.serie                                                                                                        ';'
                    entry(tt-documento-frete.id-tipo,'Normal,Comp Valor,Comp ICMS,Reentrega,Devolu‡Æo,Redespacho,Anula‡Æo,Di ria') format 'x(20)'   ';'
                    tt-documento-frete.dt-emissao-docto                                                                                             ';'
                    tt-documento-frete.dt-integr-of                                                                                                 ';'
                    tt-documento-frete.dt-criacao                                                                                                   ';'
                    tt-documento-frete.vl-conhecimento                                                                                              ';'
                    tt-documento-frete.nr-fatura                                                                                                    ';'
                    tt-documento-frete.dt-emis-fatura                                                                                               ';'
                    tt-documento-frete.dt-integr-ap                                                                                                 ';'
                    tt-documento-frete.dt-venct-fatura                                                                                              ';'
                    tt-documento-frete.dt-pagto-fat                                                                                                 ';'.
                    
                put ';;;' skip.
                    
            end.
        end.
        else do:
            run pi-imprime.
    
            put ';;;' skip.
    
        end.
    
    end.
    output close.
    
    if valid-handle(h-botr098q01) then delete procedure h-botr098q01.
    if valid-handle(h-botr098rat) then delete procedure h-botr098rat.
    if valid-handle(h-acomp)      then run pi-finalizar in h-acomp.
    
    if valid-handle(h-BOTR118)    then run destroy in h-BOTR118.
    
    assign h-botr098q01 = ?
           h-botr098rat = ?.
           
    /*abrir arquivo csv*/
    create 'excel.application' ch-excel.
    assign file-info:file-name = c-arquivo-destino.
    
    ch-excel:workbooks:open(file-info:file-name,true).
    ch-excel:sheets:item(1).
    
    ch-excel:range("A1:ZZ1"):font:bold      = true.      /*negrito*/
    ch-Excel:range("F2:F" + string(i-nr-linha)):numberformat = '###.###.##0,00'.                   /*formato numero*/
    ch-Excel:range("I2:I" + string(i-nr-linha)):numberformat = '###.###.##0,00'.                   /*formato numero*/
    ch-Excel:range("P2:P" + string(i-nr-linha)):numberformat = '###.###.##0,00'.                   /*formato numero*/
    
    ch-excel:visible = yes.
    
    release object ch-excel.
    
    /*--- fim ---*/
    
    procedure pi-imprime:
    
        assign i-nr-linha = + 1.
    
        /*--- ImpressÆo dados padräes ---*/
        put tt-nota-fiscal-tr.cod-estabel                                   ';'
            tt-nota-fiscal-tr.nr-nf                                         ';'
            tt-nota-fiscal-tr.cd-serie                                      ';'
            tt-nota-fiscal-tr.dt-emissao                                    ';'
            tt-nota-fiscal-tr.nr-romaneio                                   ';'
    
           (if avail nota-fiscal then nota-fiscal.vl-tot-nota else 0)       ';'
           (if avail nota-fiscal then nota-fiscal.cod-rep else 0)           ';'
           (if avail nota-fiscal then nota-fiscal.no-ab-reppri else '')     ';'
           (if avail nota-fiscal then nota-fiscal.cidade else '')           ';'
           (if avail nota-fiscal then nota-fiscal.estado else '')           ';'
    
            tt-nota-fiscal-tr.nr-calculo                                    ';'
            tt-nota-fiscal-tr.dt-calculo                                    ';'
            d-tot-frete                                                     ';'.
    
    end procedure.
    
    procedure pi-calc-frete:
    
        define input param c-cgc-rem  like nota-fiscal-tr.cgc-rem  no-undo.
        define input param c-nr-nf    like nota-fiscal-tr.nr-nf    no-undo.
        define input param c-cd-serie like nota-fiscal-tr.cd-serie no-undo.
        define input param r-nota     as rowid                     no-undo.
    
        /*--- Calcular os Valores de Frete para a Nota Fiscal ---*/
        if valid-handle(h-botr098q01) 
        then do:
           empty temp-table tt-pre-con.
    
           run SetConstraintPreCon in h-botr098q01 (input c-cgc-rem,
                                                    input c-nr-nf,
                                                    input c-cd-serie) no-error.
    
           run piRetornaCalcs in h-botr098q01 (output table tt-pre-con) no-error.
    
           run getValFreteRat in h-botr098rat (input r-nota,
                                               output table tt-valor-frete).
        end.
        
        for  each tt-pre-con:
            assign tt-nota-fiscal-tr.nr-calculo = tt-pre-con.nr-calculo
                   tt-nota-fiscal-tr.dt-calculo = tt-pre-con.dt-criacao.
        end.
    
        assign d-tot-frete-calc = 0.
        for  each tt-valor-frete:
           assign d-tot-frete-calc = d-tot-frete-calc + tt-valor-frete.d-tot-frete.
        end.
    
    end procedure.
    
    Abraços

Compartilhe esta Página