Árvore de páginas

Versões comparadas

Chave

  • Esta linha foi adicionada.
  • Esta linha foi removida.
  • A formatação mudou.

...

Bloco de código
titleImplementação
linenumberstrue
#INCLUDE 'PROTHEUS.CH'
       
User function a740GrdV()
Local oView  :=   PARAMIXB[1] //Viewdef
          
If !isBlind() .AND. VALTYPE(oView) == 'O' .AND. (isInCallStack("At870PRev") .OR. isInCallStack("AT870PlaRe"))
    oView:AddUserButton("Reajuste Retroativo","",{|oView| U_RRetroat(oView)},,,)
EndIf
Return
          
User Function RRetroat(oView)
Local oModel
Local oMdlTFJ
Local oDlgSelect
Local cFuncao       := SPACE(TamSx3("TFF_FUNCAO")[1])
Local cEscala       := SPACE(TamSx3("TFF_ESCALA")[1])
Local nPerc := 0
Local nMult := 0
Local nQuant    := 0
Local dDataRef  := dDataBase
Local cAplReajs := "2"
Local cAplHE    := "2"
Local cAplMT    := "2"
Local cTodosLoc := "2"
Local cTodasLin := "2"
Local cCronog   := '1'
Local cCompet   := Space(7)
Local aOpcRea   := {"2 - Não","1 - Sim"}
Local aOpcHE    := {"2 - Não","1 - Sim"}
Local aOpcMT    := {"2 - Não","1 - Sim"}
Local aOpcs     := {"2 - Não","1 - Sim"}
Local aOpcComp  := {}
Local aQtd      := {"2 - Posicionada","1 - Todos"}
Local aCronog   := {"1 - Sim", "2 - Não"}
Local oGrp
          
If VALTYPE(oView) == 'O' .AND. VALTYPE(oModel := oView:GetModel()) == 'O'
    oMdlTFJ := oModel:GetModel("TFJ_REFER")
    If oMdlTFJ:GetValue("TFJ_CNTREC") == '1'
        aOpcComp := U_CompetCo()
        DEFINE MSDIALOG oDlgSelect FROM 0,0 TO 525,300 PIXEL TITLE "Reajuste Retroativo"
        oGrp1    := TGroup():New(000, 005, 53, 140, "Filtros", oDlgSelect,,, .T.)
     
         @ 10, 9 SAY "Função" SIZE 30, 30 PIXEL
          
        oGetSRA := TGet():New( 020, 009, { | u | If(PCount() > 0, cFuncao := u, cFuncao) },oDlgSelect, ;
            060, 010, "!@",{ || .T.}, 0, 16777215,,.F.,,.T.,,.F.,;
            ,.F.,.F.,{|| .T.},.F.,.F. ,,"cFuncao",,,,.T.  )
        oGetSRA:cF3 := 'SRJ'
         
        @ 10, 77 SAY "Escala" SIZE 30, 30 PIXEL
         
        oGetTDW := TGet():New( 020, 077, { | u | If(PCount() > 0, cEscala := u, cEscala) },oDlgSelect, ;
            060, 010, "!@",{ || .T.}, 0, 16777215,,.F.,,.T.,,.F.,;
            ,.F.,.F.,{|| .T.},.F.,.F. ,,"cEscala",,,,.T.  )
        oGetTDW:cF3 := 'TDW'
     
        @ 40, 9 SAY "Apurado em:" SIZE 80, 30 PIXEL
                 
        oCompet := TGet():New( 39, 077, { | u | If( PCount() == 0, cCompet, cCompet := u ) },oDlgSelect, ;
            020, 010, "@E 99/9999",, 0, 16777215,,.F.,,.T.,,.F.,,.F.,.F.,,.F.,.F. ,,"cCompet",,,,.T.)
     
        oGrp2    := TGroup():New(055, 005, 143, 140, "Reajuste", oDlgSelect,,, .T.)
     
        @ 65, 9 SAY "Percentual (%)" SIZE 50, 30 PIXEL
          
        oDataDe := TGet():New( 75, 009, { | u | If( PCount() == 0, nPerc, nPerc := u ) },oDlgSelect, ;
            060, 010, "@E 9999.99",, 0, 16777215,,.F.,,.T.,,.F.,,.F.,.F.,,.F.,.F. ,,"nPerc",,,,.T.)
          
        @ 65, 77 SAY "Multiplicador" SIZE 50, 30 PIXEL
          
        oDataAte := TGet():New( 75, 077, { | u | If( PCount() == 0, nMult, nMult := u ) },oDlgSelect, ;
            060, 010, "@E 99.99",, 0, 16777215,,.F.,,.T.,,.F.,,.F.,.F.,,.F.,.F. ,,"nMult",,,,.T.)
             
        @ 95, 9 SAY "Aplica Reajuste?" SIZE 80, 30 PIXEL
                 
        oCombo1 := TComboBox():New(94,77,{|u|if(PCount()>0,cAplReajs:=u,cAplReajs)},;
            aOpcRea,40,10,oDlgSelect,,,,,,.T.,,,,,,,,,'cAplReajs')
        
        @ 110, 9 SAY "Reajusta H.E.?" SIZE 80, 30 PIXEL
                 
        oCombo2 := TComboBox():New(109,77,{|u|if(PCount()>0,cAplHE:=u,cAplHE)},;
            aOpcHE,40,10,oDlgSelect,,,,,,.T.,,,,,,,,,'cAplHE')
 
        @ 125, 9 SAY "Reajusta Materiais?" SIZE 80, 30 PIXEL
                 
        oCombo3 := TComboBox():New(124,77,{|u|if(PCount()>0,cAplMT:=u,cAplMT)},;
            aOpcMT,40,10,oDlgSelect,,,,,,.T.,,,,,,,,,'cAplMT')
 
        oGrp    := TGroup():New(148, 005, 190, 140, "Alteração de Quantidade", oDlgSelect,,, .T.)
                 
        @ 158, 9 SAY "Nova Quantidade" SIZE 50, 30 PIXEL
          
        oQuant := TGet():New( 168, 009, { | u | If( PCount() == 0, nQuant, nQuant := u ) },oDlgSelect, ;
            060, 010, "@E 99,999,999,999",, 0, 16777215,,.F.,,.T.,,.F.,,.F.,.F.,,.F.,.F. ,,"nQuant",,,,.T.)
          
        @ 158, 77 SAY "Data de Referencia" SIZE 50, 30 PIXEL
          
        oDataAte := TGet():New( 168, 077, { | u | If( PCount() == 0, dDataRef, dDataRef := u ) },oDlgSelect, ;
            060, 010, "@D",, 0, 16777215,,.F.,,.T.,,.F.,,.F.,.F.,,.F.,.F. ,,"dDataRef",,,,.T.)
         
        @ 200, 9 SAY "Em todos os Locais?" SIZE 80, 30 PIXEL
                 
        oCombo := TComboBox():New(200,77,{|u|if(PCount()>0,cTodosLoc:=u,cTodosLoc)},;
            aOpcs,40,10,oDlgSelect,,,,,,.T.,,,,,,,,,'cTodosLoc')   
 
        @ 215, 9 SAY "Todas os itens de RH?" SIZE 80, 30 PIXEL
    
        oCombo1 := TComboBox():New(215, 077,{|u|if(PCount()>0,cTodasLin:=u,cTodasLin)},;
            aQtd,60,10,oDlgSelect,,,,,,.T.,,,,,,,,,'cTodasLin')
    
        @ 230, 9 SAY "Aplic. Cronograma?" SIZE 80, 30 PIXEL
    
        oCombo1 := TComboBox():New(230, 077,{|u|if(PCount()>0,cCronog:=u,cCronog)},;
            aCronog,60,10,oDlgSelect,,,,,,.T.,,,,,,,,,'cCronog')
                 
        oRefresh := TButton():New( 250, 005, "Aplicar",oDlgSelect,{|| U_AplicVl(oModel, cFuncao , nPerc, nMult, LEFT(cTodosLoc,1), cEscala, nQuant, dDataRef, LEFT(cTodasLin,1), cCompet, LEFT(cAplReajs,1), LEFT(cCronog,1), LEFT(cAplHE,1), LEFT(cAplMT,1)) , oDlgSelect:End()}, 60,10,,,.F.,.T.,.F.,,.F.,,,.F. )
          
        oExit := TButton():New( 250  , 080, "Sair",oDlgSelect,{|| oDlgSelect:End() }, 60,10,,,.F.,.T.,.F.,,.F.,,,.F. )
                 
        ACTIVATE MSDIALOG oDlgSelect CENTER
    Else
        MsgAlert("Opção disponível apenas para contratos Recorrentes")
    EndIf
EndIf
          
Return .T.
          
          
User Function AplicVl(oModel, cFuncao , nPerc, nMult, cTodosLoc, cEscala, nQuant, dDataRef, cTodasLin, cCompet, cAplReajs, cCronog, cAplHE,cAplMT)
Local lAllLoc   := cTodosLoc == '1'
Local lAllLinha := cTodasLin == '1'
Local lAplicRea := cAplReajs == '1'
Local oMdlTFL   := oModel:GetModel("TFL_LOC")
Local oMdlTFF   := oModel:GetModel("TFF_RH")
Local oMdlTFG   := oModel:GetModel("TFG_MI")
Local oMdlTFH   := oModel:GetModel("TFH_MC")
Local oMdlTFJ   := oModel:GetModel("TFJ_REFER")
Local nX        := 0
Local nY        := 0
Local nZ        := 0
Local cLog      := ""
Local aSaveRows := FwSaveRows()
Local aErroMVC  := {}
Local aErrors   := {}
Local nTotal    := 0
Local nGrandTot := 0
Local nVal      := 0
Local nValPrc   := 0
Local nPosTFF   := 0
Local nPosTFL   := 0
Local nValParc  := 0
Local lServRet  := .F.
Local lContinua := .T.
Local cProdRet  := SuperGetMv("MV_PRODRET",,"")
Local cProxParc := ""
Local cMsg      := ""
Local nValPrx   := 0
Local nDiff     := 0
    
DbSelectArea("TFF")
TFF->(DbSetOrder(1))
         
If TFF->(ColumnPos("TFF_PRDRET")) > 0 .And. TFF->(ColumnPos("TFF_VLRRET")) > 0
    lServRet := .T.
EndIf
         
If nQuant > 0 .And. !lAllLinha
    nPosTFF := oMdlTFF:GetLine()
    nPosTFL := oMdlTFL:GetLine()
    If !EMPTY(STRTRAN(cCompet,"/")) .AND. cAplReajs == '1'
        cProxParc := cCompet
    Else
        cProxParc := TecBDt2Cmp(POSICIONE("CNA",1,xFilial("CNA")+oMdlTFL:GetValue("TFL_CONTRT")+oMdlTFL:GetValue("TFL_CONREV")+oMdlTFL:GetValue("TFL_PLAN"), "CNA_PROMED"))
    EndIf
    cLog += "Local: " + Alltrim(POSICIONE("ABS",1,xFilial("ABS") + oMdlTFL:GetValue("TFL_LOCAL"), "ABS_DESCRI")) + Chr(13) + Chr(10)
    cLog += " - RH: " + oMdlTFF:GetValue("TFF_COD") + Chr(13) + Chr(10)
    cLog += " - Função: " + Alltrim(POSICIONE("SRJ",1,xFilial("SRJ") + oMdlTFF:GetValue("TFF_FUNCAO"), "RJ_DESC")) + Chr(13) + Chr(10)
    cLog += " - Produto: " + Alltrim(Posicione("SB1",1,xFilial("SB1") + oMdlTFF:GetValue("TFF_PRODUT"), "B1_DESC")) + Chr(13) + Chr(10)
    cLog += " - Qtd. Venda Anterior: " + Alltrim(AllToChar(oMdlTFF:GetValue("TFF_QTDVEN"))) + Chr(13) + Chr(10)
    cLog += " - Qtd. Venda: " + Alltrim(AllToChar(nQuant)) + Chr(13) + Chr(10)
    cLog += " - Vlr. Venda Anterior: R$ " + Alltrim(TRANSFORM( oMdlTFF:GetValue("TFF_SUBTOT") , "@E 99,999,999,999.99" )) + Chr(13) + Chr(10)
    cLog += " - Vlr. Venda Novo: R$ " + Alltrim(TRANSFORM( ROUND(nQuant * oMdlTFF:GetValue("TFF_SUBTOT"), TamSX3("TFF_SUBTOT")[2]) , "@E 99,999,999,999.99" )) + Chr(13) + Chr(10)
    IF nPerc == 0 .AND. lAplicRea
        nValParc := 0
    Else
        nValParc := U_AplicQtd(oMdlTFF,nQuant,dDataRef,.F.,,cCronog == '1')
    EndIf
    nGrandTot += nValParc
    If cCronog == '1'
        If !(TecHasTGT("TFF",oMdlTFF:GetValue("TFF_COD"),cProxParc,oMdlTFJ:GetValue("TFJ_CODIGO"),@nTotal))
            nTotal := At740PrxPa(/*cTipo*/,;
                    oMdlTFF:GetValue("TFF_QTDVEN"),;
                    oMdlTFF:GetValue("TFF_PRCVEN"),;
                    oMdlTFF:GetValue("TFF_DESCON"),;
                    oMdlTFF:GetValue("TFF_TXLUCR"),;
                    oMdlTFF:GetValue("TFF_TXADM"))
        EndIf
    Else
        nTotal := oMdlTFF:GetValue("TFF_VLPRPA")
    EndIf
    cLog += " - Valor Novo (prox. parcela): R$ " + Alltrim(TRANSFORM((IIF(nPerc == 0 .AND. lAplicRea, 0, nValParc + nTotal)), "@e 999,999,999.99" )) +;
    " ( R$ " + Alltrim(TRANSFORM(nValParc, "@e 999,999,999.99" )) + " )" + " + ( R$ " + Alltrim(TRANSFORM(IIF(nPerc == 0 .AND. lAplicRea, 0, nTotal), "@e 999,999,999.99" )) + " )" + Chr(13) + Chr(10)
    cLog += Chr(13) + Chr(10)
    cLog += Chr(13) + Chr(10)
EndIf


If lAplicRea
    For nX := 1 To oMdlTFL:Length()
        If lAllLoc
            oMdlTFL:GoLine(nX)
        EndIf
        For nY := 1 To oMdlTFF:Length()
            If !lAllLinha
                nPosTFF := oMdlTFF:GetLine()
                oMdlTFF:GoLine(nPosTFF)
            Else 
                oMdlTFF:GoLine(nY)
            EndIf 
            If U_VldFiltro(oMdlTFF,cFuncao,cEscala,cCompet) .AND. oMdlTFF:GetValue("TFF_COBCTR") != '2' .And. oMdlTFF:GetValue("TFF_ENCE") != '1'
                If !EMPTY(STRTRAN(cCompet,"/")) .AND. cAplReajs == '1'
                    cProxParc := cCompet
                Else
                    cProxParc := TecBDt2Cmp(POSICIONE("CNA",1,xFilial("CNA")+oMdlTFL:GetValue("TFL_CONTRT")+oMdlTFL:GetValue("TFL_CONREV")+oMdlTFL:GetValue("TFL_PLAN"), "CNA_PROMED"))
                EndIf
                If cCronog == '1'
                    If !(TecHasTGT("TFF",oMdlTFF:GetValue("TFF_COD"),cProxParc,oMdlTFJ:GetValue("TFJ_CODIGO"),@nValPrx))
                        nValPrx := At740PrxPa(/*cTipo*/,;
                                oMdlTFF:GetValue("TFF_QTDVEN"),;
                                oMdlTFF:GetValue("TFF_PRCVEN"),;
                                oMdlTFF:GetValue("TFF_DESCON"),;
                                oMdlTFF:GetValue("TFF_TXLUCR"),;
                                oMdlTFF:GetValue("TFF_TXADM"))
                    EndIf
                Else
                    nValPrx := oMdlTFF:GetValue("TFF_VLPRPA")
                EndIf
                cLog += "Local: " + Alltrim(POSICIONE("ABS",1,xFilial("ABS") + oMdlTFL:GetValue("TFL_LOCAL"), "ABS_DESCRI")) + Chr(13) + Chr(10)
                cLog += " - RH: " + oMdlTFF:GetValue("TFF_COD") + Chr(13) + Chr(10)
                cLog += " - Função: " + Alltrim(POSICIONE("SRJ",1,xFilial("SRJ") + oMdlTFF:GetValue("TFF_FUNCAO"), "RJ_DESC")) + Chr(13) + Chr(10)
                cLog += " - Produto: " + Alltrim(Posicione("SB1",1,xFilial("SB1") + oMdlTFF:GetValue("TFF_PRODUT"), "B1_DESC")) + Chr(13) + Chr(10)
                If lAllLinha .And. nQuant > 0
                    cLog += " - Qtd. Venda Anterior: " + Alltrim(AllToChar(oMdlTFF:GetValue("TFF_QTDVEN"))) + Chr(13) + Chr(10)
                    cLog += " - Qtd. Venda: " + Alltrim(AllToChar(nQuant)) + Chr(13) + Chr(10)
                    cLog += " - Vlr. Venda Anterior: R$ " + Alltrim(TRANSFORM( oMdlTFF:GetValue("TFF_SUBTOT") , "@E 99,999,999,999.99" )) + Chr(13) + Chr(10)
                    cLog += " - Vlr. Venda Novo: R$ " + Alltrim(TRANSFORM( ROUND(nQuant * oMdlTFF:GetValue("TFF_SUBTOT"), TamSX3("TFF_SUBTOT")[2]) , "@E 99,999,999,999.99" )) + Chr(13) + Chr(10)
                    cLog += " - Valor Antigo (prox. parcela): R$ " +  Alltrim(TRANSFORM( nValPrx , "@e 999,999,999.99" )) + Chr(13) + Chr(10)

                    nValParc := U_AplicQtd(oMdlTFF,nQuant,dDataRef,.F.,,cCronog == '1')

                    If nPerc == 0 .AND. lAplicRea
                        nTotal := 0
                    Else
                        nTotal := ROUND( (nValPrx * (nPerc / 100)) * nMult , TamSX3("TFF_VLPRPA")[2] )
                    EndIf
                    nGrandTot += nTotal
                    If nPerc == 0 .AND. lAplicRea
                        nValParc := 0
                    Else
                        nValParc := nValPrx + nValParc
                    EndIf
                    cLog += " - Valor Novo (prox. parcela): R$ " + Alltrim(TRANSFORM((IIF(nPerc == 0 .AND. lAplicRea, 0, nValParc + nTotal)), "@e 999,999,999.99" )) +;
                    " ( R$ " + Alltrim(TRANSFORM(nValParc, "@e 999,999,999.99" )) + " )" + " + ( R$ " + Alltrim(TRANSFORM(IIF(nPerc == 0 .AND. lAplicRea, 0, nTotal), "@e 999,999,999.99" )) + " )" + Chr(13) + Chr(10)
                    cLog += Chr(13) + Chr(10)
                Else 
                    cLog += " - Valor Antigo RH (prox. parcela): R$ " +  Alltrim(TRANSFORM( nValPrx , "@e 999,999,999.99" )) + Chr(13) + Chr(10)
                    If nPerc == 0 .AND. lAplicRea
                        nTotal := 0
                    Else
                        nTotal := (oMdlTFF:GetValue("TFF_PRCVEN") * (nPerc / 100)) * oMdlTFF:GetValue("TFF_QTDVEN")
                    EndIf
                    nTotal := Round(nTotal,TamSX3("TFF_VLPRPA")[2])
                    nGrandTot += nTotal
                    cLog += " - Valor Novo RH(prox. parcela): R$ " + Alltrim(TRANSFORM((IIF(nPerc == 0 .AND. lAplicRea, 0, nValPrx) + nTotal), "@e 999,999,999.99" )) +;
                    " ( + R$ " + Alltrim(TRANSFORM(nTotal, "@e 999,999,999.99" )) + " )" + Chr(13) + Chr(10)
                    cLog += Chr(13) + Chr(10)
                    If cAplMT == "1"
                        If oMdlTFG:Length() > 0
                            U_CheckMat("TFG",oMdlTFG,oMdlTFF:GetValue("TFF_COD"),oMdlTFJ:GetValue("TFJ_CODIGO"),cCronog,cProxParc,@cLog,nPerc,lAplicRea,@nGrandTot)
                        EndIf
                        If oMdlTFH:Length() > 0
                            U_CheckMat("TFH",oMdlTFH,oMdlTFF:GetValue("TFF_COD"),oMdlTFJ:GetValue("TFJ_CODIGO"),cCronog,cProxParc,@cLog,nPerc,lAplicRea,@nGrandTot)
                        EndIf
                    EndIf 
                EndIf
            EndIf
            If !lAllLinha
                Exit
            EndIf
        Next nY
        cLog += REPLICATE("-",10)
        cLog += Chr(13) + Chr(10)
        If !lAllLoc
            Exit
        EndIf
    Next nX
EndIf

cLog += REPLICATE("-",15)
cLog += Chr(13) + Chr(10)
cLog += "Valor adicional prox. parcela: R$ " + Alltrim(TRANSFORM(nGrandTot, "@e 999,999,999.99" ))
        
AtShowLog(cLog,"Prévia de Valores",/*lVScroll*/,/*lHScroll*/,/*lWrdWrap*/,.F.)
        
If MsgYesNo("Aplicar valores calculados?")
        
    //Verifica se vai aplicar a quantidade somente para a linha posicionada
    If !lAllLinha .And. nQuant > 0
        oMdlTFL:GoLine(nPosTFL)
        oMdlTFF:GoLine(nPosTFF)
        U_AplicQtd(oMdlTFF,nQuant,dDataRef,.T.,nPerc,cCronog == '1',cCompet, @lContinua)
    EndIf
    If lContinua
        For nX := 1 To oMdlTFL:Length()
            If lAllLoc
                oMdlTFL:GoLine(nX)
            EndIf
            For nY := 1 To oMdlTFF:Length()
                If !lAllLinha
                    oMdlTFF:GoLine(nPosTFF)
                Else 
                    oMdlTFF:GoLine(nY)
                EndIf
                lAplicDiff := .F.
                If U_VldFiltro(oMdlTFF,cFuncao,cEscala,cCompet) .AND. oMdlTFF:GetValue("TFF_COBCTR") != '2' .And. oMdlTFF:GetValue("TFF_ENCE") != '1'
                    If cAplHE == '1'
                        U_AjustHE(nPerc, oMdlTFF)
                    EndIf
                    //aplicar a quantidade para todas as linhas encontradas conforme os filtros
                    If lAllLinha .And. nQuant > 0
                        U_AplicQtd(oMdlTFF,nQuant,dDataRef,.T.,nPerc,cCronog == '1', , @lContinua)
                    EndIf
                    If lContinua
                        If !EMPTY(STRTRAN(cCompet,"/")) .AND. cAplReajs == '1'
                            cProxParc := cCompet
                        Else
                            cProxParc := TecBDt2Cmp(POSICIONE("CNA",1,xFilial("CNA")+oMdlTFL:GetValue("TFL_CONTRT")+oMdlTFL:GetValue("TFL_CONREV")+oMdlTFL:GetValue("TFL_PLAN"), "CNA_PROMED"))
                        EndIf
                        If cCronog == '1'
                            If !(TecHasTGT("TFF",oMdlTFF:GetValue("TFF_COD"),cProxParc,oMdlTFJ:GetValue("TFJ_CODIGO"),@nVal))
                                nVal := At740PrxPa(/*cTipo*/,;
                                        oMdlTFF:GetValue("TFF_QTDVEN"),;
                                        oMdlTFF:GetValue("TFF_PRCVEN"),;
                                        oMdlTFF:GetValue("TFF_DESCON"),;
                                        oMdlTFF:GetValue("TFF_TXLUCR"),;
                                        oMdlTFF:GetValue("TFF_TXADM"))
                            Else //aplicar apenas a diferença
                                lAplicDiff := .T.
                            EndIf
                        Else
                            nVal := oMdlTFF:GetValue("TFF_VLPRPA")
                        EndIf
            
                        nValPrc := oMdlTFF:GetValue("TFF_PRCVEN")
                
                        If lServRet .And. nMult > 0
                
                            nTotal := (nValPrc + (nValPrc * (nPerc / 100)))
                            If nPerc > 0
                                nDiff := At740PrxPa(/*cTipo*/,;
                                        oMdlTFF:GetValue("TFF_QTDVEN"),;
                                        oMdlTFF:GetValue("TFF_PRCVEN"),;
                                        oMdlTFF:GetValue("TFF_DESCON"),;
                                        oMdlTFF:GetValue("TFF_TXLUCR"),;
                                        oMdlTFF:GetValue("TFF_TXADM"))
                                lContinua := lContinua .AND. oMdlTFF:SetValue("TFF_PRCVEN", nTotal)
                                nDiff := (At740PrxPa(/*cTipo*/,;
                                        oMdlTFF:GetValue("TFF_QTDVEN"),;
                                        oMdlTFF:GetValue("TFF_PRCVEN"),;
                                        oMdlTFF:GetValue("TFF_DESCON"),;
                                        oMdlTFF:GetValue("TFF_TXLUCR"),;
                                        oMdlTFF:GetValue("TFF_TXADM"))- nDiff)
                            EndIf
                            If nPerc == 0 .AND. lAplicRea
                                nTotal := 0
                            Else
                                nTotal := (nValPrc * (nPerc / 100)) * oMdlTFF:GetValue("TFF_QTDVEN")
                            EndIf
                
                            nTotal := Round(nTotal,TamSX3("TFF_VLPRPA")[2])
            
                            If cCronog == '1'
                                If lAplicDiff
                                    At740IAuto("TFF", oMdlTFF:GetValue("TFF_COD"), cProxParc, nDiff, oMdlTFJ:GetValue("TFJ_CODIGO"))
                                Else
                                    U_AplicTGT("TFF",oMdlTFF:GetValue("TFF_COD"),cProxParc,oMdlTFJ:GetValue("TFJ_CODIGO"),nTotal)
                                    At740IAuto("TFF", oMdlTFF:GetValue("TFF_COD"), cProxParc, nVal + nTotal, oMdlTFJ:GetValue("TFJ_CODIGO"))
                                EndIf
                            Else
                                lContinua := lContinua .AND. oMdlTFF:SetValue("TFF_VLPRPA",nVal + nTotal)
                            EndIf
                                    
                            If !Empty(cProdRet)
                                lContinua := lContinua .AND. oMdlTFF:SetValue("TFF_PRDRET",cProdRet)
                            EndIf
                
                            nTotal := (nValPrc * (nPerc / 100)) * oMdlTFF:GetValue("TFF_QTDVEN")
                                    
                            nTotal := nTotal * nMult
                
                            nTotal := Round(nTotal,TamSX3("TFF_VLPRPA")[2])
                
                                lContinua := lContinua .AND. oMdlTFF:SetValue("TFF_VLRRET",nTotal)
        
                            If cAplMT == "1"
                                If oMdlTFG:Length() > 0
                                    U_AplicMat("TFG",oMdlTFG,oMdlTFF:GetValue("TFF_COD"),oMdlTFJ:GetValue("TFJ_CODIGO"),cCronog,cProxParc,@cLog,nPerc,lAplicRea)
                                EndIf
                                If oMdlTFH:Length() > 0
                                    U_AplicMat("TFH",oMdlTFH,oMdlTFF:GetValue("TFF_COD"),oMdlTFJ:GetValue("TFJ_CODIGO"),cCronog,cProxParc,@cLog,nPerc,lAplicRea)
                                EndIf
                            EndIf 
        
                        Else
                            If nPerc > 0
                                nDiff := At740PrxPa(/*cTipo*/,;
                                        oMdlTFF:GetValue("TFF_QTDVEN"),;
                                        oMdlTFF:GetValue("TFF_PRCVEN"),;
                                        oMdlTFF:GetValue("TFF_DESCON"),;
                                        oMdlTFF:GetValue("TFF_TXLUCR"),;
                                        oMdlTFF:GetValue("TFF_TXADM"))
                                    lContinua := lContinua .AND. oMdlTFF:SetValue("TFF_PRCVEN",;
                                    ROUND((nValPrc + (nValPrc * (nPerc / 100))),;
                                    TamSX3("TFF_PRCVEN")[2]))
                                nDiff := (At740PrxPa(/*cTipo*/,;
                                        oMdlTFF:GetValue("TFF_QTDVEN"),;
                                        oMdlTFF:GetValue("TFF_PRCVEN"),;
                                        oMdlTFF:GetValue("TFF_DESCON"),;
                                        oMdlTFF:GetValue("TFF_TXLUCR"),;
                                        oMdlTFF:GetValue("TFF_TXADM"))- nDiff)
                            EndIf
                            If nPerc == 0 .AND. lAplicRea
                                IF nQuant == 0
                                    If cCronog == '1'
                                        At740IAuto("TFF", oMdlTFF:GetValue("TFF_COD"), cProxParc, 0, oMdlTFJ:GetValue("TFJ_CODIGO"))
                                    Else
                                        lContinua := lContinua .AND. oMdlTFF:SetValue("TFF_VLPRPA", 0)
                                    EndIf
                                EndIf
                            Else
                                If cCronog == '1'
                                    If lAplicDiff .And. ROUND(nDiff * nMult,TamSX3("TFF_VLPRPA")[2]) > 0
                                        At740IAuto("TFF",;
                                                oMdlTFF:GetValue("TFF_COD"),;
                                                cProxParc,;
                                                ROUND(nDiff * nMult,TamSX3("TFF_VLPRPA")[2]),;
                                                oMdlTFJ:GetValue("TFJ_CODIGO"))
                                    Else
                                        At740IAuto("TFF",;
                                                oMdlTFF:GetValue("TFF_COD"),;
                                                cProxParc,;
                                                ROUND((nVal + ((nVal * (nPerc / 100)) * nMult)),TamSX3("TFF_VLPRPA")[2]),;
                                                oMdlTFJ:GetValue("TFJ_CODIGO"))
                                    EndIf
                                Else
                                    lContinua := lContinua .AND. oMdlTFF:SetValue("TFF_VLPRPA",;
                                        ROUND((nVal + ((nVal * (nPerc / 100)) * nMult)),;
                                        TamSX3("TFF_VLPRPA")[2]))
                                EndIf
                            EndIf
                        EndIf
                    EndIf
                EndIf
                If !lAllLinha .OR. !lContinua
                    Exit
                EndIf
            Next nY
            If !lAllLoc .OR. !lContinua
                Exit
            EndIf
        Next nX
    EndIf
EndIf     

If !lContinua
    aErroMVC := oModel:GetErrorMessage()
    If !Empty(aErroMVC[6])
        AADD(aErrors, {	 "Id do formulário de origem:"  + ' [' + AllToChar( aErroMVC[1] ) + ']',;	//"Id do formulário de origem:"
									 "Id do campo de origem:" + ' [' + AllToChar( aErroMVC[2] ) + ']',;	//"Id do campo de origem:"
									 "Id do formulário de erro:" + ' [' + AllToChar( aErroMVC[3] ) + ']',;	//"Id do formulário de erro:"
									 "Id do campo de erro:" + ' [' + AllToChar( aErroMVC[4] ) + ']',;	//"Id do campo de erro:"
									 "Id do erro:" + ' [' + AllToChar( aErroMVC[5] ) + ']',;	//"Id do erro:"
									 "Mensagem do erro:" + ' [' + AllToChar( aErroMVC[6] ) + ']',;	//"Mensagem do erro:"
									 "Mensagem da solução:" + ' [' + AllToChar( aErroMVC[7] ) + ']',;	//"Mensagem da solução:"
									 "Valor atribuído:" + ' [' + AllToChar( aErroMVC[8] ) + ']',;	//"Valor atribuído:"
									 "Valor anterior:" + ' [' + AllToChar( aErroMVC[9] ) + ']';	//"Valor anterior:"
									 })			
        For nX := 1 To LEN(aErrors)
            For nZ := 1 To LEN(aErrors[nX])
                cMsg += If(Empty(aErrors[nX][nZ]), aErrors[nX][nZ], aErrors[nX][nZ] + CRLF )
            Next
            cMsg += CRLF + REPLICATE("-",30) + CRLF
        Next
        If !ISBlind()
            AtShowLog(cMsg,"Inconsistência",/*lVScroll*/,/*lHScroll*/,/*lWrdWrap*/,.F.)	//"Error"
        EndIf
    EndIf
EndIf

FwRestRows( aSaveRows )

Return
         
User Function VldFiltro(oMdlTFF,cFuncao,cEscala,cCompet)
Local lRetorno  := .T.
        
If !Empty(cFuncao) .And. !Empty(cEscala) .AND. !Empty(cCompet)
    If oMdlTFF:GetValue("TFF_FUNCAO") == cFuncao .And. oMdlTFF:GetValue("TFF_ESCALA") == cEscala
        If !(U_VldCompC(oMdlTFF:GetValue("TFF_COD"), cCompet))
            lRetorno := .F.
        EndIf
    Else
        lRetorno := .F.
    EndIf
Else
    If !Empty(cFuncao)
        lRetorno := oMdlTFF:GetValue("TFF_FUNCAO") == cFuncao
    EndIf
    If !Empty(cEscala) .AND. lRetorno
        lRetorno := oMdlTFF:GetValue("TFF_ESCALA") == cEscala
    EndIf
    If !Empty(cCompet) .AND. lRetorno
        lRetorno := U_VldCompC(oMdlTFF:GetValue("TFF_COD"), cCompet)
    EndIf
EndIf
         
Return lRetorno
         
User Function AplicQtd(oMdlTFF,nQuant,dDataRef,lSetValue, nPerc, lCronog, cCompet, lContinua)
Local nDataFim      := Day(LastDate(dDataRef)) // ultimo dia do mes
Local nValor        := 0
Local nValorCalc    := 0
Local nVlrPrx       := oMdlTFF:GetValue("TFF_VLPRPA") //Valor da proxima parcela
Local nDataIni      := Day(dDataRef) //dia de inicio do contrato
Local nQtdAnt       := oMdlTFF:GetValue("TFF_QTDVEN")
Local oModel        := oMdlTFF:GetModel()
Local oMdlTFL       := oModel:GetModel("TFL_LOC")
Local oMdlTFJ       := oModel:GetModel("TFJ_REFER")
Local cProxParc     := TecBDt2Cmp(POSICIONE("CNA",1,xFilial("CNA")+oMdlTFF:GetValue("TFF_CONTRT")+oMdlTFF:GetValue("TFF_CONREV")+oMdlTFL:GetValue("TFL_PLAN"), "CNA_PROMED"))
   
Default cCompet := ""
   
If lSetValue
    //Altera a quantidade do campo TFF_QTDVEN
    lContinua := oMdlTFF:SetValue("TFF_QTDVEN",nQuant)
         
    //Realiza o calculo da proxima parcela
    If lContinua
        nValor  := oMdlTFF:GetValue("TFF_PRCVEN") * (nQuant - nQtdAnt) // preço de venda do contrato
              
        nValor := (nValor/nDataFim)*((nDataFim-nDataIni)+1)
    
        //Arredonda o valor
        nValor := Round(nValor,TamSX3("TFL_VLPRPA")[2])
        nValorCalc := nValor
        nValor := nVlrPrx + nValor
        lContinua := oMdlTFF:SetValue("TFF_VLPRPA",nValor)
        If lCronog
            If !(TecHasTGT("TFF",oMdlTFF:GetValue("TFF_COD"),cProxParc,oMdlTFJ:GetValue("TFJ_CODIGO"),@nVlrPrx))
                nVlrPrx := At740PrxPa(/*cTipo*/,;
                        nQtdAnt,;
                        oMdlTFF:GetValue("TFF_PRCVEN"),;
                        oMdlTFF:GetValue("TFF_DESCON"),;
                        oMdlTFF:GetValue("TFF_TXLUCR"),;
                        oMdlTFF:GetValue("TFF_TXADM"))
            EndIf
            nValor := nVlrPrx + nValorCalc
            If !Empty(cCompet) .And. cProxParc > cCompet
                At740IAuto("TFF", oMdlTFF:GetValue("TFF_COD"), cCompet, nValorCalc, oMdlTFJ:GetValue("TFJ_CODIGO"))
            Else
                At740IAuto("TFF", oMdlTFF:GetValue("TFF_COD"), cProxParc, nValor, oMdlTFJ:GetValue("TFJ_CODIGO"))
            EndIf 
        EndIf
    EndIf
Else
    nValor   := oMdlTFF:GetValue("TFF_PRCVEN") * (nQuant - nQtdAnt)  // Valor a ser reajustado
         
    nValor := (nValor/nDataFim)*((nDataFim-nDataIni)+1)
             
    //Arredonda o valor
    nValor := Round(nValor,TamSX3("TFL_VLPRPA")[2])
EndIf
         
Return nValor
        
User Function CompetCo()
Local aCompets      := CtrCompets()
Local nX
        
For nX := 1 to Len(aCompets)
    aCompets[nX] := CVALTOCHAR(nX)+'='+aCompets[nX]
Next nX
        
Return aCompets
        
User Function VldCompC(cCodTFF, cCompet)
Local cQuery    := ""
Local cAliasCND := GetNextAlias()
Local lRet      := .T.
        
cQuery := ""
cQuery += " SELECT 1 FROM " + RetSQLName("TFF") + " TFF "
cQuery += " INNER JOIN " + RetSQLName("TFL") + " TFL "
cQuery += " ON TFL.TFL_CODIGO = TFF.TFF_CODPAI "
cQuery += " AND TFL.TFL_FILIAL = '" + xFilial("TFL") + "' "
cQuery += " AND TFL.D_E_L_E_T_ = ' ' "
cQuery += " INNER JOIN " + RetSQLName("CNE") + " CNE "
cQuery += " ON CNE.CNE_CONTRA = TFF.TFF_CONTRT "
cQuery += " AND CNE.CNE_NUMERO = TFL.TFL_PLAN "
cQuery += " AND CNE.CNE_REVISA = TFF.TFF_CONREV "
cQuery += " AND CNE.CNE_ITEM = TFF.TFF_ITCNB "
cQuery += " AND CNE.CNE_FILIAL = '" + xFilial("CNE") + "' "
cQuery += " AND CNE.D_E_L_E_T_ = ' ' "
cQuery += " INNER JOIN " + RetSQLName("CND") + " CND "
cQuery += " ON CND.CND_CONTRA = CNE.CNE_CONTRA "
cQuery += " AND CND.CND_REVISA = CNE.CNE_REVISA "
cQuery += " AND CND.CND_FILIAL = '" + xFilial("CND") + "' "
cQuery += " AND CND.D_E_L_E_T_ = ' ' "
cQuery += " AND CND.CND_COMPET = '" + cCompet + "' "
cQuery += " WHERE "
cQuery += " TFF.TFF_FILIAL = '" + xFilial("TFF") + "' "
cQuery += " AND TFF.TFF_COD = '" + cCodTFF + "' "
cQuery += " AND TFF.D_E_L_E_T_ = ' ' "
        
cQuery      := ChangeQuery(cQuery)
DbUseArea(.T., "TOPCONN",TcGenQry(,,cQuery), cAliasCND , .T., .T.)
        
lRet := ( cAliasCND  )->( !EOF() )
        
( cAliasCND  )->(DbCloseArea())
        
Return lRet
  
User Function AjustHE(nPerc, oMdlTFF)
Local oModel := oMdlTFF:GetModel()
Local oMdlHE := oModel:GetModel("TFU_HE")
Local nX
Local nValor := 0
  
If nPerc != 0
    For nX := 1 To oMdlHE:Length()
        oMdlHE:GoLine(nX)
        nValor := ROUND( oMdlHE:GetValue("TFU_VALOR") + (oMdlHE:GetValue("TFU_VALOR") * nPerc / 100), TamSX3("TFU_VALOR")[2] )
        oMdlHE:SetValue("TFU_VALOR", nValor  )
    Next nX
EndIf
Return
 
User Function CheckMat(cTabela,oMdlMat,cTFFCOD,cTFJCOD,cCronog,cProxParc,cLog,nPerc,lAplicRea,nGrandTot)
Local nX        := 0
Local nValPrxMI := 0
Local nValPrxMC := 0
Local nTotal    := 0
Local cDescProd := ""
 
If cTabela == "TFG"
    For nX := 1 To oMdlMat:Length()
        cDescProd := Alltrim(Posicione("SB1",1,xFilial("SB1") + oMdlMat:GetValue("TFG_PRODUT"), "B1_DESC"))
        cLog += "   - Material de Implantação: " + oMdlMat:GetValue("TFG_COD") + " - " + cDescProd + Chr(13) + Chr(10)
        If cCronog == '1'
            If !(TecHasTGT("TFG",oMdlMat:GetValue("TFG_COD"),cProxParc,cTFJCOD,@nValPrxMI))
                nValPrxMI := At740PrxPa(/*cTipo*/,;
                    oMdlMat:GetValue("TFG_QTDVEN"),;
                    oMdlMat:GetValue("TFG_PRCVEN"),;
                    oMdlMat:GetValue("TFG_DESCON"),;
                    oMdlMat:GetValue("TFG_TXLUCR"),;
                    oMdlMat:GetValue("TFG_TXADM"))
            EndIf
        Else
            nValPrxMI := oMdlMat:GetValue("TFG_VLPRPA")
        EndIf
        cLog += "       - Valor Antigo MI (prox. parcela): R$ " +  Alltrim(TRANSFORM( nValPrxMI , "@e 999,999,999.99" )) + Chr(13) + Chr(10)
        If nPerc == 0 .AND. lAplicRea
            nTotal := 0
        Else
            nTotal := (oMdlMat:GetValue("TFG_PRCVEN") * (nPerc / 100)) * oMdlMat:GetValue("TFG_QTDVEN")
        EndIf
        nTotal := Round(nTotal,TamSX3("TFG_VLPRPA")[2])
        nGrandTot += nTotal
        cLog += "       - Valor Novo MI (prox. parcela): R$ " + Alltrim(TRANSFORM((IIF(nPerc == 0 .AND. lAplicRea, 0, nValPrxMI) + nTotal), "@e 999,999,999.99" )) +;
            " ( + R$ " + Alltrim(TRANSFORM(nTotal, "@e 999,999,999.99" )) + " )" + Chr(13) + Chr(10)
        cLog += Chr(13) + Chr(10)
    Next nX
ElseIf cTabela == "TFH"
    For nX := 1 To oMdlMat:Length()
        cDescProd := Alltrim(Posicione("SB1",1,xFilial("SB1") + oMdlMat:GetValue("TFH_PRODUT"), "B1_DESC"))
        cLog += "   - Material de Consumo: " + oMdlMat:GetValue("TFH_COD") + " - " + cDescProd + Chr(13) + Chr(10)
        If cCronog == '1'
            If !(TecHasTGT("TFH",oMdlMat:GetValue("TFH_COD"),cProxParc,cTFJCOD,@nValPrxMC))
                nValPrxMC := At740PrxPa(/*cTipo*/,;
                    oMdlMat:GetValue("TFH_QTDVEN"),;
                    oMdlMat:GetValue("TFH_PRCVEN"),;
                    oMdlMat:GetValue("TFH_DESCON"),;
                    oMdlMat:GetValue("TFH_TXLUCR"),;
                    oMdlMat:GetValue("TFH_TXADM"))
            EndIf
        Else
            nValPrxMC := oMdlMat:GetValue("TFH_VLPRPA")
        EndIf
        cLog += "       - Valor Antigo MC (prox. parcela): R$ " +  Alltrim(TRANSFORM( nValPrxMC , "@e 999,999,999.99" )) + Chr(13) + Chr(10)
        If nPerc == 0 .AND. lAplicRea
            nTotal := 0
        Else
            nTotal := (oMdlMat:GetValue("TFH_PRCVEN") * (nPerc / 100)) * oMdlMat:GetValue("TFH_QTDVEN")
        EndIf
        nTotal := Round(nTotal,TamSX3("TFH_VLPRPA")[2])
        nGrandTot += nTotal
        cLog += "       - Valor Novo MC (prox. parcela): R$ " + Alltrim(TRANSFORM((IIF(nPerc == 0 .AND. lAplicRea, 0, nValPrxMC) + nTotal), "@e 999,999,999.99" )) +;
            " ( + R$ " + Alltrim(TRANSFORM(nTotal, "@e 999,999,999.99" )) + " )" + Chr(13) + Chr(10)
        cLog += Chr(13) + Chr(10)
    Next nX
EndIf
 
Return
 
User Function AplicMat(cTabela,oMdlMat,cTFFCOD,cTFJCOD,cCronog,cProxParc,cLog,nPerc,lAplicRea)
Local nX    := 0
Local nValPrxMI := 0
Local nValPrxMC := 0
Local nTotal    := 0
Local nValPrc   := 0
 
If cTabela == "TFG"
    For nX := 1 To oMdlMat:Length()
        If cCronog == '1'
            If !(TecHasTGT("TFG",oMdlMat:GetValue("TFG_COD"),cProxParc,cTFJCOD,@nValPrxMI))
                nValPrxMI := At740PrxPa(/*cTipo*/,;
                    oMdlMat:GetValue("TFG_QTDVEN"),;
                    oMdlMat:GetValue("TFG_PRCVEN"),;
                    oMdlMat:GetValue("TFG_DESCON"),;
                    oMdlMat:GetValue("TFG_TXLUCR"),;
                    oMdlMat:GetValue("TFG_TXADM"))
            EndIf
        Else
            nValPrxMI := oMdlMat:GetValue("TFG_VLPRPA")
        EndIf
        nValPrc := oMdlMat:GetValue("TFG_PRCVEN")
        nTotal := (nValPrc + (nValPrc * (nPerc / 100)))
        If nPerc > 0
            oMdlMat:SetValue("TFG_PRCVEN", nTotal)
        EndIf
        If nPerc == 0 .AND. lAplicRea
            nTotal := 0
        Else
            nTotal := (nValPrc * (nPerc / 100)) * oMdlMat:GetValue("TFG_QTDVEN")
        EndIf
 
        nTotal := Round(nTotal,TamSX3("TFG_VLPRPA")[2])
 
        If cCronog == '1'
            At740IAuto("TFG", oMdlMat:GetValue("TFG_COD"), cProxParc, nValPrxMI + nTotal, cTFJCOD)
        Else
            oMdlMat:SetValue("TFG_VLPRPA",nValPrxMI + nTotal)
        EndIf
    Next nX   
ElseIf cTabela == "TFH"
    For nX := 1 To oMdlMat:Length()
        If cCronog == '1'
            If !(TecHasTGT("TFH",oMdlMat:GetValue("TFH_COD"),cProxParc,cTFJCOD,@nValPrxMC))
                nValPrxMC := At740PrxPa(/*cTipo*/,;
                    oMdlMat:GetValue("TFH_QTDVEN"),;
                    oMdlMat:GetValue("TFH_PRCVEN"),;
                    oMdlMat:GetValue("TFH_DESCON"),;
                    oMdlMat:GetValue("TFH_TXLUCR"),;
                    oMdlMat:GetValue("TFH_TXADM"))
            EndIf
        Else
            nValPrxMC := oMdlMat:GetValue("TFH_VLPRPA")
        EndIf
        nValPrc := oMdlMat:GetValue("TFH_PRCVEN")
        nTotal := (nValPrc + (nValPrc * (nPerc / 100)))
        If nPerc > 0
            oMdlMat:SetValue("TFH_PRCVEN", nTotal)
        EndIf
        If nPerc == 0 .AND. lAplicRea
            nTotal := 0
        Else
            nTotal := (nValPrc * (nPerc / 100)) * oMdlMat:GetValue("TFH_QTDVEN")
        EndIf
 
        nTotal := Round(nTotal,TamSX3("TFH_VLPRPA")[2])
 
        If cCronog == '1'
            At740IAuto("TFH", oMdlMat:GetValue("TFH_COD"), cProxParc, nValPrxMC + nTotal, cTFJCOD)
        Else
            oMdlMat:SetValue("TFH_VLPRPA",nValPrxMC + nTotal)
        EndIf
    Next nX   
EndIf
 
Return
 
User Function AplicTGT(cTpItem,cCodItem,cCompet,cCodTFJ,nVal)
Local aArea := GetArea()
Local cSql := ""
Local cAliasAux := GetNextAlias()
 
cSql += " SELECT TGT.TGT_VALOR, TGT.TGT_COMPET FROM " + RetSqlName("TGT") + " TGT "
cSql += " WHERE TGT.D_E_L_E_T_ = ' ' AND "
cSql += " TGT.TGT_TPITEM = '"+cTpItem+"' AND "
cSql += " TGT.TGT_CDITEM = '"+cCodItem+"' "
cSql := ChangeQuery(cSql)
dbUseArea( .T., "TOPCONN", TCGENQRY(,,cSql),cAliasAux, .F., .T.)
 
While (cAliasAux)->(!EOF())
    If Val(Substr((cAliasAux)->TGT_COMPET,1,2)) + Val(Substr((cAliasAux)->TGT_COMPET,4,7)) >= Val(Substr(cCompet,1,2)) + Val(Substr(cCompet,4,7))
        lRet := .T.
        At740IAuto("TFF", cCodItem, (cAliasAux)->TGT_COMPET, (cAliasAux)->TGT_VALOR + nVal, cCodTFJ)
    EndIf   
    (cAliasAux)->(DbSkip())
EndDo
(cAliasAux)->(DbCloseArea())
 
RestArea(aArea)
Return

...