Á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'
"PROTHEUS.CH"
#INCLUDE "FWMVCDEF.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 nValRet   := 0
Local nQuant    := 0
Local dDataRef  := dDataBase
Local dDataDe   := CTOD("  /  /  ")
Local dDataAte  := CTOD("  /  /  ")
Local cAplReajs := "2"
Local cAplHE    := "2"
Local cAplMT    := "2"
Local cTodosLoc := "2"
Local cTodasLin := "2"
Local cCronog   := '1'
Local cProdRet  := '2'
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
aRetroat  := {"1 - Sim", "2 - 
If VALTYPE(oView) == 'O' .AND. VALTYPE(oModel := oViewNão"}
Local oGrp      := Nil
Local oGrp1     := NIL
Local oGrp2     := Nil 
Local oGrp3     := Nil
            
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 475560,300400 PIXEL TITLE "Reajuste Retroativo"
        oGrp1    := TGroup():New(000, 005, 5343, 140200, "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'
       
        @ 4010, 9147 SAY "Apurado em:" SIZE 80, 30 PIXEL
                   
        oCompet := TGet():New( 39020, 077147, { | 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(055045, 005, 123150, 140200, "Reajuste", oDlgSelect,,, .T.)
        
        @ 6555, 9 SAY "PercentualNovo Valor (%R$) " SIZE 50, 30 PIXEL
            
        oDataDe := TGet():New( 7565, 009, { | u | If( PCount() == 0, nPercnValRet, nPercnValRet := u ) },oDlgSelect, ;
            060090, 010, "@E 999999,999,999,999.99",{|| U_VldCpoRe(nValRet,nPerc,"Percentual (%)")}, 0, 16777215,,.F.,,.T.,,.F.,,.F.,.F.,,.F.,.F. ,,"nPercnValRet",,,,.T.)
        
        @ 6585, 779 SAY "MultiplicadorPercentual (%)" SIZE 50, 30 PIXEL
            
        oDataAteoDataDe := TGet():New( 7595, 077009, { | u | If( PCount() == 0, nMultnPerc, nMultnPerc := u ) },oDlgSelect, ;
            060, 010, "@E 999999.99",{||U_VldCpoRe(nPerc,nValRet,"Novo Valor (R$) ")}, 0, 16777215,,.F.,,.T.,,.F.,,.F.,.F.,,.F.,.F. ,,"nMultnPerc",,,,.T.)
            
        @ 9585, 977 SAY "Aplica Reajuste?Multiplicador" SIZE 8050, 30 PIXEL
               
        oCombo1oDataAte := TComboBoxTGet():New(94 95,77 077, { | u |if If( PCount()>0,cAplReajs == 0, nMult, nMult := u,cAplReajs ) },oDlgSelect, ;
            aOpcRea060,40,10,oDlgSelect,,,, 010, "@E 99.99",, 0, 16777215,,.F.,,.T.,,.F.,,.F.,.F.,,.F.,,'cAplReajs')
      .F. ,,"nMult",,,,.T.)

        @ 110115, 9 SAY "Reajusta H.E.Data de ?" SIZE 8050, 30 PIXEL
               
        oCombo2oDataRet := TComboBoxTGet():New(109 125,77 009, { | u |if If( PCount()>0,cAplHE == 0, dDataDe, dDataDe := u,cAplHE ) },oDlgSelect, ;
            aOpcHE060,40,10,oDlgSelect,,,, 010, "@D",, 0, 16777215,,.F.,,.T.,,.F.,,.F.,.F.,,,,'cAplHE')

        oGrp    := TGroup():New(128, 005, 183, 140, "Alteração de Quantidade", oDlgSelect,,, .F.,.F. ,,"dDataDe",,,,.T.)
   
        @ 138115, 977 SAY "EmData todos osAte Locais?" SIZE 8050, 30 PIXEL
               
        oCombooDataRet1 := TComboBoxTGet():New(137 125,77 077, { | u |if If( PCount()>0,cTodosLoc == 0, dDataAte, dDataAte := u,cTodosLoc ) },oDlgSelect, ;
            aOpcs060,40,10,oDlgSelect,,,, 010, "@D",, 0, 16777215,,.F.,,.T.,,.F.,,.F.,.F.,,.F.,.F. ,,"dDataAte",,,,'cTodosLoc').T.)    
               
        @ 15855, 9147 SAY "NovaAplica QuantidadeReajuste?" SIZE 5080, 30 PIXEL
                   
        oQuantoCombo1 := TGetTComboBox():New( 16865, 009147, { | u | Ifif( PCount() == 0, nQuant, nQuant >0,cAplReajs:= u ,cAplReajs) },oDlgSelect, ;
            060aOpcRea, 010, "@E 99,999,999,999",, 0, 16777215,,.F.,,.40,10,oDlgSelect,,,,,,.T.,,.F.,,.F.,.F.,,.F.,.F. ,,"nQuant",,,,.T.)
 ,'cAplReajs')
       
        @ 15885, 77147 SAY "Data de ReferenciaReajusta Materiais?" SIZE 5080, 30 PIXEL
                   
        oDataAteoCombo3 := TGetTComboBox():New( 16895, 077147, { | u | Ifif( PCount() == 0, dDataRef, dDataRef >0,cAplMT:= u ,cAplMT) },oDlgSelect, ;
            060, 010, "@D",, 0, 16777215,,.F.,aOpcMT,40,10,oDlgSelect,,,{|| U_VldOpc(nValRet,.T.,,.F.,,.F.,.F"Reajusta Materiais?",cAplMT)},,,.T.,,.F.,.F. ,,"dDataRef",,,,.T.'cAplMT')
       
        @ 187115, 9147 SAY "Todas as LinhasReajusta H.E.?" SIZE 80, 30 PIXEL
                   
        oCombo1oCombo2 := TComboBox():New(187125, 077147,{|u|if(PCount()>0,cTodasLincAplHE:=u,cTodasLincAplHE)},;
            aQtdaOpcHE,6040,10,oDlgSelect,,,{|| U_VldOpc(nValRet,.T.,"Reajusta H.E.?",cAplHE)},,,.T.,,,,,,,,,'cTodasLincAplHE')
    

        @oGrp 202, 9 SAY "Aplic. Cronograma? := TGroup():New(152, 005, 192, 200, "Alteração de Quantidade", oDlgSelect,,, .T.)
                   
        @ 162, 9 SAY "Nova Quantidade" SIZE 8050, 30 PIXEL
            
        oCombo1oQuant := TComboBoxTGet():New(202 172, 077009, { | u |if If( PCount()>0,cCronog == 0, nQuant, nQuant := u,cCronog ) },oDlgSelect, ;
            aCronog,60,10,oDlgSelect,,,,060, 010, "@E 99,999,999,999",, 0, 16777215,,.F.,,.T.,,.F.,,.F.,.F.,,.F.,.F. ,,"nQuant",,,,'cCronog'.T.)
            
        @ 162, 77 SAY "Data de Referencia" SIZE 50, 30 PIXEL
            oRefresh
        oDataAte := TButtonTGet():New( 225172, 005077, "Aplicar",oDlgSelect,{ | u | 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)) , oDlgSelect:End()}, 60,10, If( PCount() == 0, dDataRef, dDataRef := u ) },oDlgSelect, ;
            060, 010, "@D",, 0, 16777215,,.F.,,.T.,,.F.,,.TF.,.F.,,.F.,.F. ,,"dDataRef",,,,.FT. )

        
  oGrp3      oExit := TButtonTGroup():New(195, 225005,  260, 080200, "SairLocais",oDlgSelect,{|| oDlgSelect:End() }, 60,10,,,.F.,, .T.,.F.,,.F.,,,.F. )
)

        @ 207, 9 SAY "Em todos os Locais?" SIZE 80, 30 PIXEL
        ACTIVATE  MSDIALOG oDlgSelect CENTER
    Else
   
     MsgAlert("Opção disponível apenas paraoCombo contratos Recorrentes"):= TComboBox():New(217,09,{|u|if(PCount()>0,cTodosLoc:=u,cTodosLoc)},;
    EndIf
EndIf
        
Return aOpcs,40,10,oDlgSelect,,,{|| U_VldOpc(nValRet,.T.
,"Em todos os   Locais?",cTodosLoc)},,,.T.,,,,,,,,,'cTodosLoc') 
   
        
User@ Function AplicVl(oModel, cFuncao , nPerc, nMult, cTodosLoc, cEscala, nQuant, dDataRef, cTodasLin, cCompet, cAplReajs, cCronog, cAplHE)
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 oMdlTFJ   := oModel:GetModel("TFJ_REFER")
Local nX207, 77 SAY "Todas os itens de RH?" SIZE 80, 30 PIXEL
      
        oCombo1 := TComboBox():New(217, 077,{|u|if(PCount()>0,cTodasLin:=u,cTodasLin)},;
         := 0
Local nY        := 0
Local cLog  aQtd,60,10,oDlgSelect,,,{|| U_VldOpc(nValRet,.T.,"Todas os itens de RH?",cTodasLin)},,,.T.,,,,,,,,,'cTodasLin')
     := ""
Local aSaveRows := FwSaveRows()
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 cProdRet  := SuperGetMv("MV_PRODRET",,"")
Local cProxParc := ""
Local nValPrx   := 0
Local nDiff     := 0
  
DbSelectArea("TFF")
TFF->(DbSetOrder(1))@ 233, 9 SAY "Aplic. Cronograma?" SIZE 80, 30 PIXEL
      
        oCombo1 := TComboBox():New(243, 09,{|u|if(PCount()>0,cCronog:=u,cCronog)},;
            aCronog,60,10,oDlgSelect,,,,,,.T.,,,,,,,,,'cCronog')

       
If TFF->(ColumnPos("TFF_PRDRET")) > 0 .And. TFF->(ColumnPos("TFF_VLRRET")) > 0
    lServRet := .T.
EndIf
 @ 233, 77 SAY "Cobra Retroativo ?" SIZE 80, 30 PIXEL
       
If nQuant > 0 .And. !lAllLinha
    nPosTFFoCombo2 := oMdlTFF:GetLineTComboBox()
    nPosTFL := oMdlTFL:GetLine()
    If !EMPTY(STRTRAN(cCompet,"/")) .AND. cAplReajs == '1':New(243, 077,{|u|if(PCount()>0,cProdRet:=u,cProdRet)},;
        cProxParc := cCompet
    Else  aRetroat,60,10,oDlgSelect,,,,,,.T.,,,,,,,,,'cProdRet')
     
        cProxParcoExit := TecBDt2CmpTButton(POSICIONE("CNA",1,xFilial("CNA")+oMdlTFL:GetValue("TFL_CONTRT")+oMdlTFL:GetValue("TFL_CONREV")+oMdlTFL:GetValue("TFL_PLAN"), "CNA_PROMED"))):New( 263  , 005, "Sair",oDlgSelect,{|| oDlgSelect:End() }, 090,12,,,.F.,.T.,.F.,,.F.,,,.F. )

    EndIf
    cLogoRefresh +:= "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((nValParc), "@e 999,999,999.99" ))"
    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
TButton():New( 263, 105, "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),LEFT(cProdRet,1),dDataDe,dDataAte,nValRet) , oDlgSelect:End()}, 095,12,,,.F.,.T.,.F.,,.F.,,,.F. )
        
        // Define CSS
        oRefresh:SetCss( U_ColorAplic() )


        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, cProdRet,dDataDe,dDataAte,nValRet)
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 cProxParc := ""
Local cMsg      := ""
Local nValPrx   := 0
Local nDiff     := 0
Local aTFF      := {}
Local aTFFAtu   := {}
Local lDataRet  := U_VldDtRet(nPerc,dDataDe,dDataAte)
Local lReajuste := .F.

If nValRet > 0 .And. nPerc > 0
    MsgAlert("O campo Novo Valor (R$) e o campo Percentual (%) estão preenchidos, verifique qual opção de reajuste será utilizado", "Atenção")
    Return
EndIf

//Para reajuste por valor, a aplicação é somente no posto posicionado e não reajusta H.E e nem Materiais
If nValRet > 0
    lAllLinha   := .F.
    lAllLoc     := .F.
    cAplHE      := '2'
    cAplMT      := '2'
    If oMdlTFF:GetValue("TFF_PRCVEN") > nValRet 
        MsgAlert("O Novo Valor deve ser maior que o valor do Posto selecionado", "Atenção")
        Return
    EndIf
EndIf

DbSelectArea("TFF")
TFF->(DbSetOrder(1))
           
If cProdRet == "1"
    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 .And. lDataRet
    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 nValRet > 0
                        nTotal := nValRet
                    ElseIf 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
                    If nValRet > 0 
                        cLog += " - Valor Novo RH(prox. parcela): R$ " + Alltrim(TRANSFORM((nTotal), "@e 999,999,999.99" )) +;
                        " ( + R$ " + Alltrim(TRANSFORM(nTotal - nValPrx, "@e 999,999,999.99" )) + " )" + Chr(13) + Chr(10)
                        cLog += Chr(13) + Chr(10)
                    Else
                        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)
                    EndIf    
                    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
                    aTFFAtu := U_MontaTFFAtu(oMdlTFF,dDataDe,dDataAte)
                    If lContinua
                        If !EMPTY(STRTRAN(cCompet,"/")) .AND. cAplReajs == '1'
                            cProxParc := cCompet
                            lReajuste := .T.
                        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) .Or. (lServRet .And. lDataRet)

                            If nValRet > 0 
                                nTotal := nValRet
                            Else
                                nTotal := (nValPrc + (nValPrc * (nPerc / 100)))
                            EndIf 

                            If nPerc > 0 .Or. nValRet > 0
                                nDiff := At740PrxPa(/*cTipo*/,;
                                        oMdlTFF:GetValue("TFF_QTDVEN"),;
                                        oMdlTFF:GetValue("TFF_PRCVEN"),;
                                        oMdlTFF:GetValue("TFF_DESCON"),;
                                        oMdlTFF:GetValue("TFF_TXLUCR"),;
                                        oMdlTFF:GetValue("TFF_TXADM"))
                                If oMdlTFF:GetValue("TFF_ENCE") != '1'        
                                    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)
                                Else 
                                    nDiff := nTotal - nDiff
                                EndIf             
                            EndIf
                            
                            If nValRet > 0
                                 nTotal := nValRet
                            ElseIf 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
                                    If !lReajuste .And. nPerc > 0
                                        U_AplicTGT("TFF",oMdlTFF:GetValue("TFF_COD"),cProxParc,oMdlTFJ:GetValue("TFJ_CODIGO"),nTotal)
                                        At740IAuto("TFF", oMdlTFF:GetValue("TFF_COD"), cProxParc, nDiff, oMdlTFJ:GetValue("TFJ_CODIGO"))
                                    EndIf
                                Else
                                    If oMdlTFF:GetValue("TFF_ENCE") != '1'
                                        U_AplicTGT("TFF",oMdlTFF:GetValue("TFF_COD"),cProxParc,oMdlTFJ:GetValue("TFJ_CODIGO"),nTotal)
                                        If  nValRet > 0
                                            At740IAuto("TFF", oMdlTFF:GetValue("TFF_COD"), cProxParc, nTotal, oMdlTFJ:GetValue("TFJ_CODIGO"))
                                        Else
                                            At740IAuto("TFF", oMdlTFF:GetValue("TFF_COD"), cProxParc, nVal + nTotal, oMdlTFJ:GetValue("TFJ_CODIGO"))
                                        EndIf
                                    EndIf    
                                EndIf
                            Else
                                lContinua := lContinua .AND. oMdlTFF:SetValue("TFF_VLPRPA",nVal + nTotal)
                            EndIf

                            If nValRet > 0 
                                nTotal := nValRet
                            Else
                                nTotal := (nValPrc * (nPerc / 100)) * oMdlTFF:GetValue("TFF_QTDVEN")
                            EndIf    

                            //Valida as datas para sempre fazer os calculos baseados em 30 dias
                            If Day(dDataAte) > 30
                                dDataAte := dDataAte - 1
                            EndIf 

                            //Valida as datas para sempre fazer os calculos baseados em 30 dias
                            If Day(dDataDe) > 30
                                dDataDe := dDataDe - 1
                            EndIf 

                            //Busca as TFF´s em revisões anteriores e monta array com valores e dias a serem calculados    
                            //aTFF := U_GetTFFAnt("TFF",oMdlTFF:GetValue("TFF_CONTRT"),oMdlTFF:GetValue("TFF_CONREV"),oMdlTFF:GetValue("TFF_COD"),dDataDe,dDataAte,aTFFAtu,oMdlTFF:GetValue("TFF_ENCE") = '1',oMdlTFF:GetValue("TFF_DTENCE"))

                            aTFF := U_GetValTFF("TFF",oMdlTFF,dDataDe,dDataAte,nValPrc,oMdlTFF:GetValue("TFF_ENCE") = '1',oMdlTFF:GetValue("TFF_DTENCE"))

                            //Realiza o calculo conforme as revisões anteriores
                            If Len(aTFF) > 0
                                nTotal := U_ValRetr(aTFF,nPerc, nDiff / oMdlTFF:GetValue("TFF_QTDVEN") )
                               /* For nX := 1 To 12
                                    U_ValRetMes(aTFF,nX,1000)
                                Next nX   */ 
                            Else 
                                U_CeckMult(dDataDe,dDataAte,@nMult)
                                nTotal := nTotal * nMult
                                nTotal := Round(nTotal,TamSX3("TFF_VLPRPA")[2])
                            EndIf   

                            If cProdRet == "1"
                                U_AplicRet(oMdlTFF,oMdlTFJ,cProxParc,nTotal,,nValPrc,dDataDe,dDataAte,nPerc,nValRet)
                            EndIf
          
                            If cAplMT == "1"
                                If !oMdlTFG:IsEmpty() .And. oMdlTFG:Length() > 0
                                    U_AplicMat("TFG",oMdlTFG,oMdlTFF:GetValue("TFF_COD"),oMdlTFJ:GetValue("TFJ_CODIGO"),cCronog,cProxParc,@cLog,nPerc,lAplicRea,dDataDe,dDataAte,oMdlTFJ,cProdRet,oMdlTFF:GetValue("TFF_ENCE") = '1',oMdlTFF:GetValue("TFF_DTENCE"),nValRet)
                                EndIf
                                If !oMdlTFH:IsEmpty() .And. oMdlTFH:Length() > 0
                                    U_AplicMat("TFH",oMdlTFH,oMdlTFF:GetValue("TFF_COD"),oMdlTFJ:GetValue("TFJ_CODIGO"),cCronog,cProxParc,@cLog,nPerc,lAplicRea,dDataDe,dDataAte,oMdlTFJ,cProdRet,oMdlTFF:GetValue("TFF_ENCE") = '1',oMdlTFF:GetValue("TFF_DTENCE"),nValRet)
                                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      := 30 //qtd de dias mes para o calculo
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)
     
         // Security  = especifico 
         // ---------------------------------------------------         
           /* M->TFJ_XCPROR := FwFldGet("TFJ_XCPROR")
            lPadrao := .T.         
            IF M->TFJ_XCPROR$"1_3"
                lPadrao := .F. 
                IF M->TFJ_XCPROR=="1"
                    // Dias corridos
		            nDataFim := Day(LastDate(dDataRef)) // ultimo dia do mes
        		    nDiv     := nDataFim
                ELSEIF M->TFJ_XCPROR=="3"
                    // 30,42 (365/12) dias
                    nDiv     := 30.42                                       
                    nDataFim := Day(LastDate(dDataRef)) // ultimo dia do mes
                    nDataFim += 0.42 // ultimo dia do mes (28/29/30 OU 31) + 0.42
                ENDIF
                nValor  := oMdlTFF:GetValue("TFF_PRCVEN") * (nQuant - nQtdAnt)   
                nValor := (nValor/nDiv)*((nDataFim-nDataIni)+1)
           ENDIF 

            IF ExistBlock("TELARAT")
                IF M->TFJ_XCPROR$"2_3" .AND. !lPadrao
                    U_TELARAT(M->TFJ_XCPROR , nDiv , nDataIni , nDataFim , oMdlTFF:GetValue("TFF_PRCVEN"), lpadrao )
                ELSE
                    U_TELARAT(M->TFJ_XCPROR,,,,,.t.)
                ENDIF   
            ENDIF   */      
        // ---------------------------------------------------
        //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

            // Security - 20102021_1
            // No processo atual, gera o prorata do decréscimo no mês/ano que será a próxima medição.
            // exemplo: ultima medição 09/2021, próxima medição 10/2021. Redução de posto para 10/11/2021, ou seja prorata competência para 11/2021, 
            // o sistema preencheu cronograma com prorata para 10/2021. Concorda que nesse mês de competência 10/2021 os funcionários vão trabalhar na
            // quantidade integral...não faz sentido cobrar com prorata.
            //IF !Empty(cCompet)
            IF cCompet == substr(dtos(dDataRef),5,2) + "/" +   left(dtos(dDataRef),4)
                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"), cCompet, nValor, oMdlTFJ:GetValue("TFJ_CODIGO"))
                EndIf
            ELSE
                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
    EndIf
Else
    nValor   := oMdlTFF:GetValue("TFF_PRCVEN") * (nQuant - nQtdAnt)  // Valor a ser reajustado
          
    nValor := (nValor/nDataFim)*((nDataFim-nDataIni)+1)
              
    // Security  = especifico  
    // ---------------------------------------------------
    /*M->TFJ_XCPROR := FwFldGet("TFJ_XCPROR")
    lPadrao := .T.
    IF M->TFJ_XCPROR$"1_3"
        lPadrao := .F.  
        IF M->TFJ_XCPROR=="1"
	      // Dias corridos   
          nDataFim := Day(LastDate(dDataRef)) // ultimo dia do mes
 		  nDiv     := nDataFim      
        ELSEIF M->TFJ_XCPROR=="3"
            // 30,42 (365/12) dias
            nDiv     := 30.42     
            nDataFim := Day(LastDate(dDataRef)) // ultimo dia do mes            
            nDataFim += 0.42 // ultimo dia do mes (28/29/30 OU 31) + 0.42
        ENDIF
        nValor  := oMdlTFF:GetValue("TFF_PRCVEN") * (nQuant - nQtdAnt)
        nValor := (nValor/nDiv)*((nDataFim-nDataIni)+1)
    ENDIF     

    IF ExistBlock("TELARAT")
        IF M->TFJ_XCPROR$"1_3" .AND. !lPadrao
            U_TELARAT(M->TFJ_XCPROR , nDiv , nDataIni , nDataFim , oMdlTFF:GetValue("TFF_PRCVEN") )
        ELSE
            U_TELARAT(M->TFJ_XCPROR,,,,,.t.)
        ENDIF   
    ENDIF   */ 

    // ---------------------------------------------------
    //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()
        oMdlMat:GoLine(nX)
        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()
        oMdlMat:GoLine(nX)
        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,dDataDe,dDataAte,oMdlTFJ,cProdRet,lEncerra,dDataEncerra,nValRet)
Local nX    := 0
Local nValPrxMI := 0
Local nValPrxMC := 0
Local nTotal    := 0
Local nValPrc   := 0
Local aMatRet   := {}
Local aTFGAtu   := {}
Local aTFHAtu   := {}
Local nMult     := 0

If cTabela == "TFG"
    For nX := 1 To oMdlMat:Length()
        oMdlMat:GoLine(nX)
        aMatRet := {}
        aTFGAtu := U_MontaMatAtu(cTabela,oMdlMat,dDataDe,dDataAte)
        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

        //Busca as TFF´s em revisões anteriores e monta array com valores e dias a serem calculados    
        //aMatRet := U_GetTFFAnt(cTabela,oMdlMat:GetValue("TFG_CONTRT"),oMdlMat:GetValue("TFG_CONREV"),oMdlMat:GetValue("TFG_COD"),dDataDe,dDataAte,aTFGAtu,lEncerra,dDataEncerra)

        aMatRet := U_GetValTFF(cTabela,oMdlMat,dDataDe,dDataAte,nValPrc,lEncerra,dDataEncerra)

        //Realiza o calculo conforme as revisões anteriores
        If Len(aMatRet) > 0
            nTotal := U_ValRetr(aMatRet,nPerc)
        Else 
            U_CeckMult(dDataDe,dDataAte,@nMult)
            nTotal := nTotal * nMult
            nTotal := Round(nTotal,TamSX3("TFF_VLPRPA")[2])
        EndIf   
        If cProdRet == "1"
            //U_AplicRet(oMdlMat,oMdlTFJ,cProxParc,nTotal,cTabela)
            U_AplicRet(oMdlMat,oMdlTFJ,cProxParc,nTotal,cTabela,nValPrc,dDataDe,dDataAte,nPerc,nValRet)
        EndIf
    Next nX 
ElseIf cTabela == "TFH"
    For nX := 1 To oMdlMat:Length()
        oMdlMat:GoLine(nX)
        aMatRet := {}
        aTFHAtu := U_MontaMatAtu(cTabela,oMdlMat,dDataDe,dDataAte)
        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

        //Busca as TFF´s em revisões anteriores e monta array com valores e dias a serem calculados    
        //aMatRet := U_GetTFFAnt(cTabela,oMdlMat:GetValue("TFH_CONTRT"),oMdlMat:GetValue("TFH_CONREV"),oMdlMat:GetValue("TFH_COD"),dDataDe,dDataAte,aTFHAtu,lEncerra,dDataEncerra)

        aMatRet := U_GetValTFF(cTabela,oMdlMat,dDataDe,dDataAte,nValPrc,lEncerra,dDataEncerra)
        
        //Realiza o calculo conforme as revisões anteriores
        If Len(aMatRet) > 0
            nTotal := U_ValRetr(aMatRet,nPerc)
        Else 
            U_CeckMult(dDataDe,dDataAte,@nMult)
            nTotal := nTotal * nMult
            nTotal := Round(nTotal,TamSX3("TFF_VLPRPA")[2])
        EndIf   
        If cProdRet == "1"
            //U_AplicRet(oMdlMat,oMdlTFJ,cProxParc,nTotal,cTabela)
            U_AplicRet(oMdlMat,oMdlTFJ,cProxParc,nTotal,cTabela,nValPrc,dDataDe,dDataAte,nPerc,nValRet)
        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

User Function AplicRet(oMdlTFF,oMdlTFJ,cCompet,nTotal,cTabela,nValPrc,dDataDe,dDataAte,nPerc,nValRet)
Local lRet      := .T.
Local cProdRet  := ""
Local cCodItem  := ""
Local lCpoTGT   := U_ChkCpoTGT()

Default cTabela     := "TFF"
Default nValPrc     := 0
Default dDataDe     := CTOD('')
Default dDataAte    := CTOD('')
Default nPerc       := 0
Default nValRet     := 0

If cTabela == "TFF"
    If !Empty(oMdlTFF:GetValue("TFF_PRDRET"))
        cProdRet := oMdlTFF:GetValue("TFF_PRDRET")
    Else
        cProdRet := oMdlTFJ:GetValue("TFJ_PRDRET")
    EndIf
    cCodItem  := oMdlTFF:GetValue("TFF_COD")
Else 
    cProdRet := oMdlTFJ:GetValue("TFJ_PRDRET")
    If cTabela == "TFH"
        cCodItem  := oMdlTFF:GetValue("TFH_COD")
    Else 
        cCodItem  := oMdlTFF:GetValue("TFG_COD")
    EndIf 
EndIf

If nTotal < 0
    nTotal := 0
EndIf 
/*
If lCpoTGT 
    If nValRet > 0
        nPerc := U_ChkInd(nValPrc,nValRet)
        nPerc := nPerc * 100
         nValRet := nValRet - nValPrc
    ElseIf nPerc > 0
        nValRet := (nValPrc * (nPerc / 100))
    EndIf     
EndIf 
*/

U_ChkRtTGT(cTabela, cCodItem, cCompet, oMdlTFJ:GetValue("TFJ_CODIGO"))

At740IExce(cTabela, cCodItem, cCompet, nTotal, oMdlTFJ:GetValue("TFJ_CODIGO"),cProdRet,dDataDe,dDataAte,nPerc,nValRet)

Return lRet

User Function ChkRtTGT(cTpItem, cCodItem, cCompet, cTFJ_CODIGO)
Local cSql := ""
Local cAliasAux := GetNextAlias()
   
cSql += " SELECT TGT.R_E_C_N_O_ TGTRECNO FROM " + RetSqlName("TGT") + " TGT "
cSql += " WHERE TGT.D_E_L_E_T_ = ' ' AND "
cSql += " TGT.TGT_TPITEM = '"+cTpItem+"' AND "
cSql += " TGT.TGT_CDITEM = '"+cCodItem+"' AND "
cSql += " TGT.TGT_CODTFJ = '"+cTFJ_CODIGO+"' AND "
cSql += " TGT.TGT_COMPET = '"+cCompet+"' AND "
cSql += " TGT.TGT_EXCEDT = '1' "

cSql := ChangeQuery(cSql)
dbUseArea( .T., "TOPCONN", TCGENQRY(,,cSql),cAliasAux, .F., .T.)
   
If (cAliasAux)->(!EOF())
    TGT->( DbGoTo( (cAliasAux)->TGTRECNO ) )
		Reclock("TGT", .F.)
			TGT->( DbDelete() )
		TGT->( MsUnlock() )
    (cAliasAux)->(DbSkip())
EndIf
(cAliasAux)->(DbCloseArea())

Return

User Function MontaTFFAtu(oMdlTFF,dDataDe,dDataAte)
Local aItTFF  := {}

    If Day(dDataAte) > 30
        dDataAte := dDataAte - 1
    EndIf 

    If Day(dDataDe) > 30
        dDataDe := dDataDe - 1
    EndIf 

    aAdd(aItTFF,{"TFF_COD",oMdlTFF:GetValue("TFF_COD")})
    aAdd(aItTFF,{"TFF_QTDVEN",oMdlTFF:GetValue("TFF_QTDVEN")})
    aAdd(aItTFF,{"TFF_PRCVEN",oMdlTFF:GetValue("TFF_PRCVEN")})
    If oMdlTFF:GetValue("TFF_PERINI") <= dDataDe
        aAdd(aItTFF,{"TFF_PERINI",dDataDe})
    Else 
        aAdd(aItTFF,{"TFF_PERINI",oMdlTFF:GetValue("TFF_PERINI")})
    EndIf
    If oMdlTFF:GetValue("TFF_PERFIM") >= dDataAte
        aAdd(aItTFF,{"TFF_PERFIM",dDataAte})
    Else 
        aAdd(aItTFF,{"TFF_PERFIM",oMdlTFF:GetValue("TFF_PERFIM")})
    EndIf       

Return aItTFF

User Function MontaMatAtu(cTabela,oMdlMat,dDataDe,dDataAte)
Local aMatAtu   := {}

If cTabela == "TFG"
    aAdd(aMatAtu,{"TFG_COD",oMdlMat:GetValue("TFG_COD")})
    aAdd(aMatAtu,{"TFG_QTDVEN",oMdlMat:GetValue("TFG_QTDVEN")})
    aAdd(aMatAtu,{"TFG_PRCVEN",oMdlMat:GetValue("TFG_PRCVEN")})
    aAdd(aMatAtu,{"TFG_PERINI",dDataDe})
    aAdd(aMatAtu,{"TFG_PERFIM",dDataAte}) 
ElseIf cTabela == "TFH"
    aAdd(aMatAtu,{"TFH_COD",oMdlMat:GetValue("TFH_COD")})
    aAdd(aMatAtu,{"TFH_QTDVEN",oMdlMat:GetValue("TFH_QTDVEN")})
    aAdd(aMatAtu,{"TFH_PRCVEN",oMdlMat:GetValue("TFH_PRCVEN")})
    aAdd(aMatAtu,{"TFH_PERINI",dDataDe})
    aAdd(aMatAtu,{"TFH_PERFIM",dDataAte}) 
EndIf 

Return aMatAtu

//Cria um Array com todas as revisões de um Item com as datas de inicio e fim da revisão
User Function GetTFFAnt(cTabela,cContrato,cRevisa,cCodTFF,dDataDe,dDataAte,aTFFAtu,lEncerra,dDataEnce)
Local cSql      := ""
Local aItTFF    := {}
Local aRevAnt   := {}
Local aRevAtu   := {}
Local cAliasAux := GetNextAlias()
Local lRevAnt   := .F.
Local nX        := 0
Local nMeses    := 0
Local nDias     := 0
Local lBuscaRev := !Empty(dDataDe) .And. !Empty(dDataAte)
Local aTFFAnt   := {}
Local dTFFIniBKP := aTFFAtu[4][2]

Default lEncerra  := .F.
Default dDataEnce := CTOD("  /  /  ")

If lBuscaRev
    cSql += " SELECT CN9_DTREV,CN9_NUMERO,CN9_REVISA,CN9_REVATU FROM " + RetSqlName("CN9") + " CN9 "
    cSql += " WHERE CN9.D_E_L_E_T_ = ' ' AND "
    cSql += " CN9.CN9_FILIAL = '" + xFilial("CN9") + "' AND "
    cSql += " CN9.CN9_NUMERO = '" + cContrato + "' AND "
    cSql += " CN9.CN9_DTREV >= '" + DtoS(dDataDe) + "' AND "
    cSql += " CN9.CN9_DTREV <= '" + DtoS(dDataAte) + "' AND "
    cSql += " CN9.CN9_REVISA <= '" + cRevisa + "' "
    cSql += " ORDER BY CN9_REVISA "

    cSql := ChangeQuery(cSql)
    dbUseArea( .T., "TOPCONN", TCGENQRY(,,cSql),cAliasAux, .F., .T.)
    
    While (cAliasAux)->(!EOF())
        If (cAliasAux)->CN9_DTREV >= DtoS(dDataDe) .And. !lRevAnt
            aRevAnt := U_BuscaRevAnt(cTabela,cContrato,cCodTFF,(cAliasAux)->CN9_REVISA,dDataDe,sTod((cAliasAux)->CN9_DTREV))
            If Len(aRevAnt) > 0 
                aAdd(aItTFF,aRevAnt)
                aRevAnt := {}
            EndIf
            aTFFAnt   := U_BuscaItAnt(cTabela,cContrato,(cAliasAux)->CN9_REVISA,cCodTFF,dDataDe,sTod((cAliasAux)->CN9_DTREV))
            If Len(aTFFAnt) > 0
                If sToD((cAliasAux)->CN9_DTREV) > aTFFAnt[4][2]
                    aTFFAnt[4][2] := sToD((cAliasAux)->CN9_DTREV)
                EndIf    
                aAdd(aItTFF,aTFFAnt)
            EndIf
            aTFFAnt   := {}   
            lRevAnt := .T.
            If (cAliasAux)->CN9_REVISA = '001' .And. Empty((cAliasAux)->CN9_REVATU)
                aTFFAnt   :=  U_GetTFFAtu(cTabela,cContrato,cRevisa,cCodTFF,sTod((cAliasAux)->CN9_DTREV),dDataAte)
                If Len(aTFFAnt) > 0
                    aAdd(aItTFF,aTFFAnt)
                EndIf
                aTFFAnt   := {}   
            EndIf     
        Else 
            If (cAliasAux)->CN9_REVISA == cRevisa
                If Len(aItTFF) > 0
                    aItTFF[Len(aItTFF)][5][2] := sToD((cAliasAux)->CN9_DTREV)   
                EndIf
                If !Empty((cAliasAux)->CN9_DTREV)   
                     aTFFAtu[4][2] := sToD((cAliasAux)->CN9_DTREV)  
                EndIf    
                aAdd(aItTFF,aTFFAtu)
            Else
                If Len(aItTFF) > 0
                    aItTFF[Len(aItTFF)][5][2] := sToD((cAliasAux)->CN9_DTREV)
                EndIf
                aTFFAnt   := U_BuscaItAnt(cTabela,cContrato,(cAliasAux)->CN9_REVISA,cCodTFF,dDataDe,sTod((cAliasAux)->CN9_DTREV))    
                If Len(aTFFAnt) > 0
                    aTFFAnt[4][2] := sToD((cAliasAux)->CN9_DTREV)
                    aAdd(aItTFF,aTFFAnt)
                EndIf     
                aTFFAnt := {}
            EndIf 
        EndIf 
        (cAliasAux)->(DbSkip())
    EndDo
    (cAliasAux)->(DbCloseArea())

    //Caso não tenha nenhuma revisão, o array retorna vazio e o array com os dadados atuais serão criados
    If Empty(aItTFF)
        aRevAtu := U_GetTFFAtu(cTabela,cContrato,cRevisa,cCodTFF,dDataDe,dDataAte)
        If Len(aRevAtu) > 0
            aAdd(aItTFF,aRevAtu)
            aRevAtu := {}
        EndIf
    EndIf 

    //Ajusta o array com a data final de reajuste
    U_AjItTFF(aItTFF,dDataAte,lEncerra,dDataEnce,aTFFAtu[5][2],dTFFIniBKP)

    //Verifica se algum periodo ficou com a data maior que a data de encerramento da TFF
    U_ChkDtTFF(aItTFF, aTFFAtu[5][2], dDataAte)

    //Realiza o calculo para verificar a quantidade de dias e meses serão realizados o calculo
    For nX := 1 To Len(aItTFF)
        If aItTFF[nX][4][2] <> aItTFF[nX][5][2] .Or. nX == Len(aItTFF)
            If  aItTFF[nX][5][2] > aItTFF[nX][4][2]
                If Day(aItTFF[nX][4][2]) > 1
                    nMeses := DateDiffMonth(aItTFF[nX][4][2],aItTFF[nX][5][2]) - 1           
                    nDias := (30 - Day(aItTFF[nX][4][2])) + 1  
                    If Day(aItTFF[nX][5][2]) == 30
                        nMeses += 1
                    Else 
                        nDias += Day(aItTFF[nX][5][2]) - 1   
                    EndIf 
                Else 
                    nMeses := DateDiffMonth(aItTFF[nX][4][2],aItTFF[nX][5][2])
                    If Day(aItTFF[nX][5][2]) == 30
                        nMeses += 1
                    Else 
                        nDias += Day(aItTFF[nX][5][2]) - 1
                    EndIf 
                EndIf
                If nDias >= 30
                    nDias := nDias - 30
                    nMeses += 1
                EndIf
            Else 
                If nX == Len(aItTFF) .And. aItTFF[nX][5][2] == aItTFF[nX][4][2]
                    nMeses := 0
                    nDias := 1  
                Else 
                    nMeses := 0
                    nDias := 0  
                EndIf    
            EndIf     
        EndIf    
        aAdd(aItTFF[nX],{"MESES",nMeses})
        aAdd(aItTFF[nX],{"DIAS",nDias})
        nMeses := 0
        nDias := 0     
    Next nX 
EndIf 

Return aItTFF

//Faz uma busca nas revisoes anteriores
User Function BuscaRevAnt(cTabela,cContrato,cCodTFF,cRevAtu,dDataDe,dDataRev)
Local cSql      := ""
Local aItTFF    := {}
Local cAliasAux := GetNextAlias()

cSql += " SELECT CN9_DTREV,CN9_NUMERO,CN9_REVISA,CN9_REVATU FROM " + RetSqlName("CN9") + " CN9 "
cSql += " WHERE CN9.D_E_L_E_T_ = ' ' AND "
cSql += " CN9.CN9_FILIAL = '" + xFilial("CN9") + "' AND "
cSql += " CN9.CN9_NUMERO = '" + cContrato + "' AND "
cSql += " CN9.CN9_REVATU = '" + cRevAtu + "' "

cSql := ChangeQuery(cSql)
dbUseArea( .T., "TOPCONN", TCGENQRY(,,cSql),cAliasAux, .F., .T.)
   
If (cAliasAux)->(!EOF())
    If (cAliasAux)->CN9_DTREV < DtoS(dDataDe)
        aItTFF := U_BuscaItAnt(cTabela,cContrato,(cAliasAux)->CN9_REVISA,cCodTFF,dDataDe,dDataRev,.T.)
    EndIf 
    (cAliasAux)->(DbSkip())
EndIf
(cAliasAux)->(DbCloseArea())

Return aItTFF

//Monta o array com as TFF´s de revisões anteriores com os respectivos dados
User Function BuscaItAnt(cTabela,cContrato,cRevAtu,cCodTFF,dDataDe,dDataRev,lRevAnt)
Local cSql      := ""
Local cAliasAux := GetNextAlias()
Local aItTFF    := {}

Default lRevAnt := .F.

Do Case
    Case cTabela == "TFF"
        cSql += " SELECT TFF.TFF_COD, TFF.TFF_CONREV ,TFF.TFF_QTDVEN,TFF.TFF_PRCVEN,TFF.TFF_PERINI,TFF.TFF_PERFIM FROM " + RetSqlName("TFF") + " TFF "
        cSql += " WHERE TFF.D_E_L_E_T_ = ' ' AND "
        cSql += " TFF.TFF_FILIAL = '" + xFilial("TFF") + "' AND "
        cSql += " TFF.TFF_CONTRT = '" + cContrato + "' AND "
        cSql += " TFF.TFF_CODSUB = '" + cCodTFF + "' "

        cSql := ChangeQuery(cSql)
        dbUseArea( .T., "TOPCONN", TCGENQRY(,,cSql),cAliasAux, .F., .T.)
        
        If (cAliasAux)->(!EOF())
            If (cAliasAux)->TFF_CONREV == cRevAtu
                aAdd(aItTFF,{"TFF_COD",(cAliasAux)->TFF_COD})
                aAdd(aItTFF,{"TFF_QTDVEN",(cAliasAux)->TFF_QTDVEN})
                aAdd(aItTFF,{"TFF_PRCVEN",(cAliasAux)->TFF_PRCVEN})
                If lRevAnt
                    If (cAliasAux)->TFF_PERINI < dToS(dDataDe)
                        aAdd(aItTFF,{"TFF_PERINI",dDataDe})
                    Else 
                        aAdd(aItTFF,{"TFF_PERINI",sToD((cAliasAux)->TFF_PERINI)})
                    EndIf
                    If sToD((cAliasAux)->TFF_PERINI) > dDataRev
                        aAdd(aItTFF,{"TFF_PERFIM",sToD((cAliasAux)->TFF_PERINI)})
                    Else     
                        aAdd(aItTFF,{"TFF_PERFIM",dDataRev})
                    EndIf    
                Else 
                    aAdd(aItTFF,{"TFF_PERINI",dDataRev})  
                    aAdd(aItTFF,{"TFF_PERFIM",dDataRev})
                EndIf    
            Else 
                aItTFF := U_BuscaItAnt(cTabela,cContrato,cRevAtu,(cAliasAux)->TFF_COD,dDataDe,dDataRev,.T.)
            EndIf 
            (cAliasAux)->(DbSkip())
        EndIf
        (cAliasAux)->(DbCloseArea())
    Case cTabela == "TFH"
        cSql += " SELECT TFH.TFH_COD, TFH.TFH_CONREV ,TFH.TFH_QTDVEN,TFH.TFH_PRCVEN,TFH.TFH_PERINI,TFH.TFH_PERFIM FROM " + RetSqlName("TFH") + " TFH "
        cSql += " WHERE TFH.D_E_L_E_T_ = ' ' AND "
        cSql += " TFH.TFH_FILIAL = '" + xFilial("TFH") + "' AND "
        cSql += " TFH.TFH_CONTRT = '" + cContrato + "' AND "
        cSql += " TFH.TFH_CODSUB = '" + cCodTFF + "' "

        cSql := ChangeQuery(cSql)
        dbUseArea( .T., "TOPCONN", TCGENQRY(,,cSql),cAliasAux, .F., .T.)
        
        If (cAliasAux)->(!EOF())
            If (cAliasAux)->TFH_CONREV == cRevAtu
                aAdd(aItTFF,{"TFH_COD",(cAliasAux)->TFH_COD})
                aAdd(aItTFF,{"TFH_QTDVEN",(cAliasAux)->TFH_QTDVEN})
                aAdd(aItTFF,{"TFH_PRCVEN",(cAliasAux)->TFH_PRCVEN})
                If lRevAnt
                    If (cAliasAux)->TFH_PERINI < dToS(dDataDe)
                        aAdd(aItTFF,{"TFH_PERINI",dDataDe})
                    Else 
                        aAdd(aItTFF,{"TFH_PERINI",sToD((cAliasAux)->TFH_PERINI)})
                    EndIf    
                    aAdd(aItTFF,{"TFH_PERFIM",dDataRev})
                Else 
                    aAdd(aItTFF,{"TFH_PERINI",dDataRev})  
                    aAdd(aItTFF,{"TFH_PERFIM",dDataRev})
                EndIf    
            Else 
                aItTFF := U_BuscaItAnt(cTabela,cContrato,cRevAtu,(cAliasAux)->TFH_COD,dDataDe,dDataRev,.T.)
            EndIf 
            (cAliasAux)->(DbSkip())
        EndIf
        (cAliasAux)->(DbCloseArea())
    Case cTabela == "TFG"
        cSql += " SELECT TFG.TFG_COD, TFG.TFG_CONREV ,TFG.TFG_QTDVEN,TFG.TFG_PRCVEN,TFG.TFG_PERINI,TFG.TFG_PERFIM FROM " + RetSqlName("TFG") + " TFG "
        cSql += " WHERE TFG.D_E_L_E_T_ = ' ' AND "
        cSql += " TFG.TFG_FILIAL = '" + xFilial("TFG") + "' AND "
        cSql += " TFG.TFG_CONTRT = '" + cContrato + "' AND "
        cSql += " TFG.TFG_CODSUB = '" + cCodTFF + "' "

        cSql := ChangeQuery(cSql)
        dbUseArea( .T., "TOPCONN", TCGENQRY(,,cSql),cAliasAux, .F., .T.)
        
        If (cAliasAux)->(!EOF())
            If (cAliasAux)->TFG_CONREV == cRevAtu
                aAdd(aItTFF,{"TFG_COD",(cAliasAux)->TFG_COD})
                aAdd(aItTFF,{"TFG_QTDVEN",(cAliasAux)->TFG_QTDVEN})
                aAdd(aItTFF,{"TFG_PRCVEN",(cAliasAux)->TFG_PRCVEN})
                If lRevAnt
                    If (cAliasAux)->TFG_PERINI < dToS(dDataDe)
                        aAdd(aItTFF,{"TFG_PERINI",dDataDe})
                    Else 
                      nPosTFF := oMdlTFF:GetLine()
    aAdd(aItTFF,{"TFG_PERINI",sToD((cAliasAux)->TFG_PERINI)})
             oMdlTFF:GoLine(nPosTFF)
       EndIf    
 Else   
                oMdlTFF:GoLine(nYaAdd(aItTFF,{"TFG_PERFIM",dDataRev})
            EndIf    Else 
            If U_VldFiltro(oMdlTFF,cFuncao,cEscala,cCompet) .AND. oMdlTFF:GetValue("TFF_COBCTR") != '2'
        aAdd(aItTFF,{"TFG_PERINI",dDataRev})  
                 If   !EMPTY(STRTRAN(cCompet,"/")) .AND. cAplReajs == '1'aAdd(aItTFF,{"TFG_PERFIM",dDataRev})
                 EndIf   cProxParc := cCompet
               Else Else
                  aItTFF  cProxParc := TecBDt2Cmp(POSICIONE("CNA",1,xFilial("CNA")+oMdlTFL:GetValue("TFL_CONTRT")+oMdlTFL:GetValue("TFL_CONREV")+oMdlTFL:GetValue("TFL_PLAN"), "CNA_PROMED")U_BuscaItAnt(cTabela,cContrato,cRevAtu,(cAliasAux)->TFG_COD,dDataDe,dDataRev,.T.)
               EndIf EndIf
                If cCronog == '1'(cAliasAux)->(DbSkip())
             EndIf
       If !(TecHasTGT("TFF",oMdlTFF:GetValue("TFF_COD"),cProxParc,oMdlTFJ:GetValue("TFJ_CODIGO"),@nValPrx))
cAliasAux)->(DbCloseArea())    
EndCase

Return aItTFF

//Faz o calculo conforme as datas informadas no array aTFF
User Function    ValRetr(aTFF,nPerc,nValDiff)
Local nRet     nValPrx := At740PrxPa(/*cTipo*/,;
 0
Local nX        := 0
Local nValor    := 0
Local nQtd      := 0

For nX := 1 To Len(aTFF)
   oMdlTFF:GetValue("TFF_QTDVEN"),;
  nValor  := aTFF[nX][3][2]
    nQtd    := aTFF[nX][2][2]
    If aTFF[nX][6][2] > 0
        nRet    oMdlTFF:GetValue("TFF_PRCVEN"),;+= U_CalcMesTFF(nQtd,nValor,nPerc,nValDiff,aTFF[nX][6][2])
    EndIf 
    If aTFF[nX][7][2] > 0 
        nRet += U_CalcDTFF(nQtd,nValor,nPerc,nValDiff,aTFF[nX][7][2])
    EndIf  
Next nX 

 nRet oMdlTFF:GetValue:= Round(nRet,TamSX3("TFF_DESCON"),;
    VLPRPA")[2])

Return nRet

//Realiza o calculo por mês do reajuste retroativo
User Function CalcMesTFF(nQtd,nValor,nPerc,nValDiff,nMes)
Local nVal  := 0
    
    If nPerc > 0
        nVal := (nValor * (nPerc / 100)) * nQtd   oMdlTFF:GetValue("TFF_TXLUCR"),;
                                oMdlTFF:GetValue("TFF_TXADM"))
        nVal := nVal * nMes
    ElseIf nValDiff >  EndIf0
        nVal := (nValDiff * nMes) * nQtd
  Else
  EndIf    

Return nVal

//Realiza o calculo por dia do reajuste retroativo
User Function CalcDTFF(nQtd,nValor,nPerc,nValDiff,nDias)
Local nVal     nValPrx := oMdlTFF:GetValue("TFF_VLPRPA")
    0
Local nValDias  := 0

    If nPerc > 0
     EndIf
   nValDias  := U_CalcValDias(nValor)
    ElseIf nValDiff > 0
   cLog += "Local: " + Alltrim(POSICIONE("ABS",1,xFilial("ABS") + oMdlTFL:GetValue("TFL_LOCAL"), "ABS_DESCRI")) + Chr(13) + Chr(10)
       nValDias  := U_CalcValDias(nValDiff)
    EndIf 

    If nPerc > 0
        nValDias  cLog += " - RH: " + oMdlTFF:GetValue("TFF_COD") + Chr(13) + Chr(10)
:= nValDias * nQtd
        nVal := (nValDias * (nPerc / 100))                   cLog += " - Função: " + Alltrim(POSICIONE("SRJ",1,xFilial("SRJ") + oMdlTFF:GetValue("TFF_FUNCAO"), "RJ_DESC")) + Chr(13) + Chr(10)
        nVal := nVal * nDias
    ElseIf nValDiff cLog += " - Produto: " + Alltrim(Posicione("SB1",1,xFilial("SB1") + oMdlTFF:GetValue("TFF_PRODUT"), "B1_DESC")) + Chr(13) + Chr(10)> 0
         nVal := (nValDias * nDias) * nQtd
    EndIf    

Return nVal

//Retorna o valor diario de um reajuste para calculo
User 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(10Function CalcValDias(nValor)

Return (nValor / 30 )

//Faz o calculo para verificar o periodo que foi informado nos campos Data De e Data Até para realizar os calculos
User Function CeckMult(dDataDe,dDataAte,nMult)
    If !Empty(dDataDe) .And. !Empty(dDataAte)
        nMult := DateDiffMonth(dDataDe,dDataAte) + 1
    EndIf    
Return

//Valida se os campos de data serão preenchidos de forma correta
User Function VldDtRet(nPerc,dDataDe,dDataAte)
Local lRet  := .T.

If nPerc > 0
    If Empty(dDataDe)
        MsgAlert("O campo Data de ? está vazio e deve ser preenchido para cLog += " - Vlr. Venda Anterior: R$ " + Alltrim(TRANSFORM( oMdlTFF:GetValue("TFF_SUBTOT") , "@E 99,999,999,999.99" )) + Chr(13) + Chr(10)reajuste retroativo", "Atenção")
        lRet := .F.
    EndIf 

    If lRet .And. Empty(dDataAte)
        MsgAlert("O campo Data Ate ? cLogestá +=vazio "e -deve Vlr.ser Vendapreenchido Novo:para R$ " + Alltrim(TRANSFORM( ROUND(nQuant * oMdlTFF:GetValue("TFF_SUBTOT"), TamSX3("TFF_SUBTOT")[2]) , "@E 99,999,999,999.99" )) + Chr(13) + Chr(10)reajuste retroativo", "Atenção")
        lRet := .F.
    EndIf 

    If lRet .And. dDataAte < dDataDe
      cLog += MsgAlert("O campo -Data ValorAte Antigo? (prox. parcela): R$ " +  Alltrim(TRANSFORM( nValPrx , "@e 999,999,999.99" )) + Chr(13) + Chr(10)deve ser maior que o campo Data De ?", "Atenção")
        lRet := .F. 
    EndIf 
EndIf 

Return lRet

//Monta o array com as informações da TFF atuais quando não tem revisão nValParc := U_AplicQtd(oMdlTFF,nQuant,dDataRef,.F.,,cCronog == '1')
 criada
User Function GetTFFAtu(cTabela,cContrato,cRevAtu,cCodTFF,dDataDe,dDataAte)
Local cSql            := ""
Local cAliasAux := GetNextAlias()
Local aItTFF    := {}

Default lRevAnt If nPerc :== 0 .ANDF.

Do lAplicReaCase
    Case cTabela == "TFF"
        cSql += " SELECT TFF.TFF_COD, TFF.TFF_CONREV ,TFF.TFF_QTDVEN,TFF.TFF_PRCVEN,TFF.TFF_PERINI,TFF.TFF_PERFIM FROM " nTotal := 0
   + RetSqlName("TFF") + " TFF "
        cSql += " WHERE TFF.D_E_L_E_T_ = ' ' AND Else"
        cSql += " TFF.TFF_FILIAL = '" + xFilial("TFF") + "' AND "
     nTotal := ROUND( (nValPrxcSql *+= (nPerc / 100)) * nMult , TamSX3("TFF_VLPRPA")[2] )
  " TFF.TFF_CONTRT = '" + cContrato + "' AND "
        cSql += " TFF.TFF_COD = '" + cCodTFF + "' EndIf"

        cSql := ChangeQuery(cSql)
        dbUseArea(  nGrandTot += nTotal.T., "TOPCONN", TCGENQRY(,,cSql),cAliasAux, .F., .T.)
        
        If (cAliasAux)->(!EOF())
    If nPerc == 0 .AND. lAplicRea
   If (cAliasAux)->TFF_CONREV == cRevAtu
                aAdd(aItTFF,{"TFF_COD",(cAliasAux)->TFF_COD})
  nValParc := 0
            aAdd(aItTFF,{"TFF_QTDVEN",(cAliasAux)->TFF_QTDVEN})
        Else
        aAdd(aItTFF,{"TFF_PRCVEN",(cAliasAux)->TFF_PRCVEN})
                nValParc := nValPrx + nValParcIf (cAliasAux)->TFF_PERINI < dToS(dDataDe)
                    EndIfaAdd(aItTFF,{"TFF_PERINI",dDataDe})
                Else 
   cLog += " - Valor Novo (prox. parcela): R$ " + Alltrim(TRANSFORM((IIF(nPerc == 0 .AND. lAplicRea, 0, nValParc + nTotal)), "@e 999,999,999.99" )) +;
 aAdd(aItTFF,{"TFF_PERINI",sToD((cAliasAux)->TFF_PERINI)})
                EndIf    
 " ( R$ " + Alltrim(TRANSFORM(nValParc, "@e 999,999,999.99" )) + " )" + " + aAdd( R$ " + Alltrim(TRANSFORM(IIF(nPerc == 0 .AND. lAplicRea, 0, nTotal), "@e 999,999,999.99" )) + " )" + Chr(13) + Chr(10)
aItTFF,{"TFF_PERFIM",dDataAte})
            EndIf 
            (cAliasAux)->(DbSkip())
        EndIf
      cLog += Chr(13) + Chr(10cAliasAux)->(DbCloseArea())
    Case cTabela == "TFH"
        cSql Else
+= " SELECT TFH.TFH_COD, TFH.TFH_CONREV ,TFH.TFH_QTDVEN,TFH.TFH_PRCVEN,TFH.TFH_PERINI,TFH.TFH_PERFIM FROM " + RetSqlName("TFH") + " TFH "
        cLogcSql += " - Valor Antigo (prox. parcela): R$ " +  Alltrim(TRANSFORM( nValPrx , "@e 999,999,999.99" )) + Chr(13) + Chr(10)
             WHERE TFH.D_E_L_E_T_ = ' ' AND "
        cSql += " TFH.TFH_FILIAL = '" + xFilial("TFH") + "' AND "
        cSql += " TFH.TFH_CONTRT = '" + cContrato + "' AND "
       If nPerccSql +== 0" TFH.AND. lAplicRea
      TFH_COD = '" + cCodTFF + "' "

        cSql := ChangeQuery(cSql)
        nTotal := 0
   dbUseArea( .T., "TOPCONN", TCGENQRY(,,cSql),cAliasAux, .F., .T.)
        
         ElseIf (cAliasAux)->(!EOF())
            If (cAliasAux)->TFH_CONREV == cRevAtu
         nTotal := (oMdlTFF:GetValue("TFF_PRCVEN") * (nPerc / 100)) * oMdlTFF:GetValue("TFF_QTDVEN" aAdd(aItTFF,{"TFH_COD",(cAliasAux)->TFH_COD})
                    EndIfaAdd(aItTFF,{"TFH_QTDVEN",(cAliasAux)->TFH_QTDVEN})
                    nTotal := Round(nTotal,TamSX3("TFF_VLPRPA")[2]aAdd(aItTFF,{"TFH_PRCVEN",(cAliasAux)->TFH_PRCVEN})
                If (cAliasAux)->TFH_PERINI   nGrandTot += nTotal< dToS(dDataDe)
                    cLog += " - Valor Novo (prox. parcela): R$ " + Alltrim(TRANSFORM((IIF(nPerc == 0 .AND. lAplicRea, 0, nValPrx) + nTotal), "@e 999,999,999.99" )) +;aAdd(aItTFF,{"TFH_PERINI",dDataDe})
                Else 
                    " ( + R$ " + Alltrim(TRANSFORM(nTotal, "@e 999,999,999.99" )) + " )" + Chr(13) + Chr(10)
        aAdd(aItTFF,{"TFH_PERINI",sToD((cAliasAux)->TFH_PERINI)})
                EndIf    
            cLog += Chr(13) + Chr(10)
aAdd(aItTFF,{"TFH_PERFIM",dDataAte})
            EndIf 
      EndIf
      (cAliasAux)->(DbSkip())
        EndIf
        (cAliasAux)->(DbCloseArea())
    Case cTabela If !lAllLinha== "TFG"
        cSql += " SELECT TFG.TFG_COD, TFG.TFG_CONREV ,TFG.TFG_QTDVEN,TFG.TFG_PRCVEN,TFG.TFG_PERINI,TFG.TFG_PERFIM FROM Exit
" + RetSqlName("TFG") + " TFG "
       EndIf
 cSql += " WHERE TFG.D_E_L_E_T_ = ' ' NextAND nY"
        cLogcSql += " TFG.TFG_FILIAL = '" + REPLICATExFilial("-TFG",10)) + "' AND "
        cLogcSql += Chr(13) + Chr(10)
        If !lAllLoc" TFG.TFG_CONTRT = '" + cContrato + "' AND "
        cSql += "  Exit
        EndIfTFG.TFG_COD = '" + cCodTFF + "' "

    Next nX
EndIf
    
cLogcSql +:= REPLICATE("-",15ChangeQuery(cSql)
cLog  += Chr(13) + Chr(10)
cLog += "Valor adicionaldbUseArea( prox.T. parcela: R$ " + Alltrim(TRANSFORM(nGrandTot, "@e 999,999,999.99" )), "TOPCONN", TCGENQRY(,,cSql),cAliasAux, .F., .T.)
        
AtShowLog(cLog,"Prévia de Valores",/*lVScroll*/,/*lHScroll*/,/*lWrdWrap*/,.F.)
        If (cAliasAux)->(!EOF())
            
If MsgYesNo("Aplicar valores calculados?")(cAliasAux)->TFG_CONREV == cRevAtu
                aAdd(aItTFF,{"TFG_COD",(cAliasAux)->TFG_COD})
      //Verifica se vai aplicar a quantidade somente para a linha posicionada aAdd(aItTFF,{"TFG_QTDVEN",(cAliasAux)->TFG_QTDVEN})
    If !lAllLinha .And. nQuant > 0
        oMdlTFL:GoLine(nPosTFLaAdd(aItTFF,{"TFG_PRCVEN",(cAliasAux)->TFG_PRCVEN})
        oMdlTFF:GoLine(nPosTFF)
        If U_AplicQtd(oMdlTFF,nQuant,dDataRef,.T.,nPerc,cCronog == '1',cCompet(cAliasAux)->TFG_PERINI < dToS(dDataDe)
    EndIf
       
    For nX := 1 To oMdlTFL:Length()
   aAdd(aItTFF,{"TFG_PERINI",dDataDe})
     If lAllLoc
           Else oMdlTFL:GoLine(nX)
        EndIf
        For nY := 1 To oMdlTFF:Length() aAdd(aItTFF,{"TFG_PERINI",sToD((cAliasAux)->TFG_PERINI)})
            If  !lAllLinha
  EndIf    
          oMdlTFF:GoLine(nPosTFF)
      aAdd(aItTFF,{"TFG_PERFIM",dDataAte})
      Else   
   EndIf 
            oMdlTFF:GoLine(nY(cAliasAux)->(DbSkip())
        EndIf
    EndIf
    (cAliasAux)->(DbCloseArea())    
EndCase

Return aItTFF

//Ajusta a Data lAplicDifffinal := .F.
        no Array
User Function AjItTFF(aItTFF,dDataAte,lEncerra,dDataEncerra,dDtFim, dDtIni)
Local nPos := Len(aItTFF)

If nPos > 0
    If U_VldFiltro(oMdlTFF,cFuncao,cEscala,cCompet) .AND. oMdlTFF:GetValue("TFF_COBCTR") != '2'
     lEncerra
        If dDataAte > dDataEncerra
           If cAplHE == '1'
 aItTFF[nPos][5][2] := dDataEncerra
        ElseIf  dDataAte < dDataEncerra
         U_AjustHE(nPerc, oMdlTFF)    aItTFF[nPos][5][2] := dDataAte
        EndIf    
    EndIfElse
        If dDtFim >= dDataAte
     //aplicar a quantidade para todas as linhas encontradas conforme os filtros       aItTFF[nPos][5][2] := dDataAte
        ElseIf dDtFim <= dDataAte
            aItTFF[nPos][5][2] := dDtFim
   If lAllLinha .And. nQuant > 0EndIf
        If dDtIni >= aItTFF[nPos][4][2]
         U_AplicQtd(oMdlTFF,nQuant,dDataRef,.T.,nPerc,cCronog == '1')   aItTFF[nPos][4][2] := dDtIni
        EndIf    
    EndIf
    
EndIf 

Return aItTFF

User Function ChkDtTFF(aItTFF, dDtfimTFF, dDataAte)
Local nPos      If:= !EMPTY(STRTRAN(cCompet,"/")) .AND. cAplReajs == '1'
Len(aItTFF)
Local nX        := 0
Local dDtIniRev 
Local dDtFimRev

If nPos > 0
    For    cProxParcnX := cCompet
1 To nPos
        If dDataAte     Else<= dDtfimTFF
            If aItTFF[nX][5][2] < aItTFF[nX][4][2] 
    cProxParc := TecBDt2Cmp(POSICIONE("CNA",1,xFilial("CNA")+oMdlTFL:GetValue("TFL_CONTRT")+oMdlTFL:GetValue("TFL_CONREV")+oMdlTFL:GetValue("TFL_PLAN"), "CNA_PROMED"))
         dDtIniRev       EndIf:= aItTFF[nX][4][2]
                IfdDtFimRev cCronog :== '1' aItTFF[nX][5][2]
                If nX > 1 If.And. !(TecHasTGT("TFF",oMdlTFF:GetValue("TFF_COD"),cProxParc,oMdlTFJ:GetValue("TFJ_CODIGO"),@nVal))
    (nX + 1) <= nPos
                    nValaItTFF[nX-1][5][2] := At740PrxPa(/*cTipo*/,;aItTFF[nX][5][2]
                    aItTFF[nX+1][4][2] := aItTFF[nX][4][2]
          oMdlTFF:GetValue("TFF_QTDVEN"),;
          aItTFF[nX][4][2] := dDtFimRev
                     oMdlTFF:GetValue("TFF_PRCVEN"),;aItTFF[nX][5][2] := dDtIniRev    
                EndIf
            EndIf    oMdlTFF:GetValue("TFF_DESCON"),;
        Else 
            If aItTFF[nX][5][2] > dDtfimTFF    
     oMdlTFF:GetValue("TFF_TXLUCR"),;
           aItTFF[nX][5][2] := dDtfimTFF    
            EndIf    oMdlTFF:GetValue("TFF_TXADM"))
        EndIf 
    Next nX 
EndIf

Return aItTFF

//Coloca a cor azul Elseno //aplicar apenas a diferença
                        lAplicDiffbotão Aplicar
User Function ColorAplic()
Local cCssCor	:= "QPushButton{margin-top:1px; border-color:#1F739E; font:bold; border-radius:2px; background-color:#1F739E; color:#ffffff; border-style: outset; border-width:1px; }"

Return (cCssCor)

//Valida se os campos de Valor ou Percentual estão preenchidos
User Function VldCpoRe(nValid,nValor,cCampo)
Local lRet  := .T.

If nValor > 0 .And. nValid > 0
    lRet    := .F.
    EndIf
                Else
                    nVal := oMdlTFF:GetValue("TFF_VLPRPA")MsgAlert("O campo " + cCampo + " não pode ser preenchido!")
EndIf 

Return lRet

//Valida os campos quando utiliza o processo de Valor e não Percentual
User Function VldOpc(nValor,lValid,cCampo,cValCpo)
Local lRet  := .T.

If nValor > 0 .And. lValid .And. Alltrim(SubStr(cValCpo,1,1)) == "1"
    lRet    := .F.
    MsgAlert("O Campo " + cCampo EndIf
+ " 
não pode ser configurado com o valor " + cValCpo + " quando utilizar o campo nValPrcNovo :=Valor oMdlTFF:GetValue("TFF_PRCVEN")
       
                If lServRet .And. nMult > 0
      
                    nTotal := (nValPrc + (nValPrc * (nPerc / 100)))
     (R$) " )
EndIf 

Return lRet

User Function ChkCpoTGT()

Return TGT->( ColumnPos('TGT_DTINI') ) > 0 .AND. TGT->( ColumnPos('TGT_DTFIM') ) > 0 .AND. TGT->( ColumnPos('TGT_INDICE') ) > 0 .AND. TGT->( ColumnPos('TGT_VALREA') > 0 )

User Function ChkInd(nValPrc,nValRet)
Local nInd  := 0
Local nDif  := 0

nDif := nValRet - nValPrc
nInd := nDif / nValPrc
nInd := Round(nInd,TamSX3("TGT_INDICE")[2])

Return nInd 

//Faz o calculo para retornar o valor de reajuste de cada mês
User Function ValRetMes(aTFF,nMes,nIndice,nValDiff)
Local aValores  := {}
Local nX        := 0
Local nQtd     If nPerc:= > 0
Local nRet      := 0
Local nRetDias  := 0
Local nValDias  := 0
Local nDias     := 0
Local nValBkp  nDiff := At740PrxPa(/*cTipo*/,;
     nValDiff

For nX := 1 To Len(aTFF)
    nValDiff := nValBkp
    If aTFF[nX][6][2] <> 0 .Or. aTFF[nX][7][2] <> 0
        nQtd    oMdlTFF:GetValue("TFF_QTDVEN"),;:= aTFF[nX][2][2]
        If Month(aTFF[nX][4][2]) < nMes .And. Month(aTFF[nX][5][2]) > nMes
            If nValDiff > 0
   oMdlTFF:GetValue("TFF_PRCVEN"),;
             nValdiff := nValDiff - aTFF[nX][3][2]
                nRet oMdlTFF:GetValue("TFF_DESCON"),;= (nValDiff * 1) * nQtd
            ElseIf nIndice > 0
                nRet oMdlTFF:GetValue("TFF_TXLUCR"),;
  := (aTFF[nX][3][2] * (nIndice / 100)) * nQtd                              oMdlTFF:GetValue("TFF_TXADM"))
     
                nRet :=  oMdlTFF:SetValue("TFF_PRCVEN", nTotal)nRet * 1
            EndIf     
       nDiff := (At740PrxPa(/*cTipo*/,; Else
            If Month(aTFF[nX][4][2]) == nMes
                If oMdlTFF:GetValue("TFF_QTDVEN"),;
     Month(aTFF[nX][4][2]) == nMes .And. Month(aTFF[nX][5][2]) == nMes
                    If nValDiff > 0
    oMdlTFF:GetValue("TFF_PRCVEN"),;
                    nValDiff := nValDiff - aTFF[nX][3][2]
        oMdlTFF:GetValue("TFF_DESCON"),;
                nValDias  := U_CalcValDias(nValDiff)
             oMdlTFF:GetValue("TFF_TXLUCR"),;
           nDias := 30
                   oMdlTFF:GetValue("TFF_TXADM"))- nDiff)
    nRetDias += (nValDias * nDias) * nQtd
          EndIf
          ElseIf nIndice > 0 
      If nPerc == 0 .AND. lAplicRea
             nValDias := U_CalcValDias(aTFF[nX][3][2])
         nTotal := 0
             nValDias := (nValDias * (nIndice / 100)) Else
                        nTotalnDias := (nValPrc * (nPerc / 100)) * oMdlTFF:GetValue("TFF_QTDVEN")
30                         EndIf
       
                       nTotal nRetDias :+= Round(nTotal,TamSX3("TFF_VLPRPA")[2])(nValDias * nDias) * nQtd
  
                  EndIf   If cCronog == '1'
                ElseIf Day(aTFF[nX][4][2]) > 1
        If lAplicDiff
                If nValDiff > 0
                        At740IAuto("TFF", oMdlTFF:GetValue("TFF_COD"), cProxParc, nDiff, oMdlTFJ:GetValue("TFJ_CODIGO"))nValDiff := nValDiff - aTFF[nX][3][2]
                        Else
nValDias  := U_CalcValDias(nValDiff)
                        nDias := At740IAuto("TFF", oMdlTFF:GetValue("TFF_COD"), cProxParc, nVal + nTotal, oMdlTFJ:GetValue("TFJ_CODIGO"))
(30 - Day(aTFF[nX][4][2])) + 1
                        nRetDias += (nValDias * nDias) * EndIfnQtd
                    Else 
                        nValDias oMdlTFF:SetValue("TFF_VLPRPA",nVal + nTotal)
= U_CalcValDias(aTFF[nX][3][2])
                        nValDias :=  EndIf(nValDias * (nIndice / 100)) 
                        nDias := (30 
   - Day(aTFF[nX][4][2])) + 1                 If !Empty(cProdRet)
              
          oMdlTFF:SetValue("TFF_PRDRET",cProdRet)
              nRetDias += (nValDias * nDias) * EndIfnQtd
       
             EndIf
       nTotal  := (nValPrc * (nPerc / 100)) * oMdlTFF:GetValue("TFF_QTDVEN") Else
                    If nValDiff > 0
    
                    nTotalnValDiff := nTotal * nMult nValDiff - aTFF[nX][3][2] 
       
                    nTotalnRet := Round(nTotal,TamSX3("TFF_VLPRPA")[2])
       (nValDiff * 1) * nQtd
                    oMdlTFF:SetValue("TFF_VLRRET",nTotal)
    ElseIf nIndice > 0 
            Else
            nRet := (aTFF[nX][3][2] * (nIndice / 100)) * IfnQtd nPerc > 0
                        nDiff := At740PrxPa(/*cTipo*/,;
      
                        nRet  oMdlTFF:GetValue("TFF_QTDVEN"),;
   := nRet * 1
                    EndIf         oMdlTFF:GetValue("TFF_PRCVEN"),;
                EndIf   
            ElseIf oMdlTFF:GetValue("TFF_DESCON"),;
               Month(aTFF[nX][5][2]) == nMes .And. Day(aTFF[nX][5][2]) > 1
                If oMdlTFF:GetValue("TFF_TXLUCR"),;
  Day(aTFF[nX][5][2]) < 30
                    If nValDiff > 0
       oMdlTFF:GetValue("TFF_TXADM"))
                 nValDiff := nValDiff     oMdlTFF:SetValue("TFF_PRCVEN",;- aTFF[nX][3][2]
                        nValDias    ROUND((nValPrc + (nValPrc * (nPerc / 100))),;:= U_CalcValDias(nValDiff)
                        If    TamSX3("TFF_PRCVEN")Day(aTFF[nX][5][2])) == 1
                        nDiff    nDias := (At740PrxPa(/*cTipo*/,;Day(aTFF[nX][5][2])
                        Else 
       oMdlTFF:GetValue("TFF_QTDVEN"),;
                     nDias := Day(aTFF[nX][5][2]) - 1
       oMdlTFF:GetValue("TFF_PRCVEN"),;
                 EndIf 
              oMdlTFF:GetValue("TFF_DESCON"),;
          nRetDias += (nValDias * nDias) * nQtd
                oMdlTFF:GetValue("TFF_TXLUCR"),;
    ElseIf nIndice > 0
                        nValDias oMdlTFF:GetValue("TFF_TXADM"))- nDiff)= U_CalcValDias(aTFF[nX][3][2])
                    EndIf
    nValDias := (nValDias * (nIndice / 100)) 
         If nPerc == 0 .AND. lAplicRea
          nDias := Day(aTFF[nX][5][2])            IF nQuant == 0
          
                  If cCronog == '1'
   nRetDias += (nValDias * nDias) * nQtd
                    EndIf   At740IAuto("TFF", oMdlTFF:GetValue("TFF_COD"), cProxParc, 0, oMdlTFJ:GetValue("TFJ_CODIGO"))
 
                Else
                Else
    If nValDiff > 0
                        nValdiff oMdlTFF:SetValue("TFF_VLPRPA", 0)
  := nValDiff - aTFF[nX][3][2]
                        nRet := EndIf
(nValDiff * 1) * nQtd
                    EndIf
ElseIf nIndice > 0
                 Else
       nRet := (aTFF[nX][3][2] * (nIndice / 100)) * nQtd         If cCronog == '1'
                       
     If lAplicDiff .And. ROUND(nDiff * nMult,TamSX3("TFF_VLPRPA")[2]) > 0
            nRet := nRet * 1
                At740IAuto("TFF",;
    EndIf     
                EndIf
            EndIf
   oMdlTFF:GetValue("TFF_COD"),;
     EndIf
    EndIf        
Next nX 

If nRetDias > 0
    nRet := Round(nRetDias,TamSX3("TFF_VLPRPA")[2])
Else 
    nRet := Round(nRet,TamSX3("TFF_VLPRPA")[2])
EndIf

aAdd(aValores,{ MesExtenso(nMes),nRet})

Return aValores

User   cProxParc,;
 Function GetValTFF(cTable,oModel,dDataDe,dDataAte,nValor,lEncerra,dDataEnce)
Local aItTFF    := {}
Local aItens    := {}
Local dIniAux   := CTOD("  /  /  ")
Local dFimAux   := CTOD("  /  /  ")

aAdd(aItTFF,{cTable+'_COD',oModel:GetValue(cTable+'_COD')})
aAdd(aItTFF,{cTable+'_QTDVEN',oModel:GetValue(cTable+'_QTDVEN')})
aAdd(aItTFF,{cTable+'_PRCVEN',nValor})

If oModel:GetValue(cTable+'_PERINI') <= dDataDe
     ROUND(nDiff * nMult,TamSX3("TFF_VLPRPA")[2]),;
 aAdd(aItTFF,{cTable+'_PERINI',dDataDe})
Else 
    If Day(oModel:GetValue(cTable+'_PERINI')) > 30
        dIniAux := oModel:GetValue(cTable+'_PERINI') - 1
    Else 
        dIniAux       oMdlTFJ:= oModel:GetValue("TFJ_CODIGO"))cTable+'_PERINI')
    EndIf     
    aAdd(aItTFF,{cTable+'_PERINI',dIniAux})
EndIf

If !lEncerra
    If oModel:GetValue(cTable+'_PERFIM') >= dDataAte
        ElseaAdd(aItTFF,{cTable+'_PERFIM',dDataAte})
    Else 
        If Day(oModel:GetValue(cTable+'_PERFIM')) > 30
            dFimAux :=   At740IAuto("TFF",;oModel:GetValue(cTable+'_PERFIM') - 1
        Else 
            dFimAux := oModel:GetValue(cTable+'_PERFIM')
        EndIf  
        oMdlTFF:GetValue("TFF_COD"),;aAdd(aItTFF,{cTable+'_PERFIM',dFimAux})
    EndIf
Else 
    If dDataEnce > dDataAte
        aAdd(aItTFF,{cTable+'_PERFIM',dDataAte})
    Else 
        If Day(dDataEnce) > 30
    cProxParc,;
        dFimAux := dDataEnce - 1
        Else 
            dFimAux := dDataEnce
        EndIf  ROUND((nVal
 + ((nVal * (nPerc / 100)) * nMult)),TamSX3("TFF_VLPRPA")[2]),; aAdd(aItTFF,{cTable+'_PERFIM',dFimAux})
    EndIf
EndIf     

aAdd(aItens,aItTFF)

//Ajusta os itens para realizar o calculo dos dias
U_GetDtItens(@aItens)

Return aItens

User Function GetDtItens(aItTFF)
Local nX        := 0
Local nMeses    := 0
Local nDias     oMdlTFJ:GetValue("TFJ_CODIGO"))
                   := 0

//Realiza o calculo para verificar a quantidade de dias e meses serão realizados o calculo
For nX := 1 To Len(aItTFF)
    If aItTFF[nX][4][2] <> aItTFF[nX][5][2] .Or. EndIf
nX == Len(aItTFF)
        If  aItTFF[nX][5][2] > aItTFF[nX][4][2]
          Else
  If Day(aItTFF[nX][4][2]) > 1
                nMeses       oMdlTFF:SetValue("TFF_VLPRPA",;
 := DateDiffMonth(aItTFF[nX][4][2],aItTFF[nX][5][2]) - 1           
                nDias := (30 - ROUND((nValDay(aItTFF[nX][4][2])) + ((nVal1 * (nPerc
 / 100)) * nMult)),;
            If Day(aItTFF[nX][5][2]) == 30
                 TamSX3("TFF_VLPRPA")[2]))
   nMeses += 1
                Else 
  EndIf
                  nDias  EndIf+= Day(aItTFF[nX][5][2])  
                EndIf 
            Else EndIf
            If !lAllLinha
   nMeses := DateDiffMonth(aItTFF[nX][4][2],aItTFF[nX][5][2])           Exit
      
				If Day(aItTFF[nX][5][2]) == 30
    EndIf
        Next nY
       nMeses If+= !lAllLoc1
            Exit
       Else EndIf
    Next nX
EndIf
        
FwRestRows( aSaveRows )
        
Return
       
User Function VldFiltro(oMdlTFF,cFuncao,cEscala,cCompet)
Local lRetorno  := .T.
//Verifica se é fevereiro e é o ultimo dia do mês
           
If !Empty(cFuncao) .And. !Empty(cEscala) .AND. !Empty(cCompet)
    If oMdlTFF:GetValue("TFF_FUNCAO"Month(aItTFF[nX][5][2]) == cFuncao2 .And. oMdlTFF:GetValue("TFF_ESCALA"(Day(aItTFF[1][5][2]) == cEscala
28 .Or. Day(aItTFF[1][5][2]) == 29)
    If !(U_VldCompC(oMdlTFF:GetValue("TFF_COD"), cCompet))
            lRetorno := .F.
    nMeses += 1
  EndIf
    Else
        lRetorno := .F.
    EndIf
Else
    If !Empty(cFuncao)
        lRetorno := oMdlTFF:GetValue("TFF_FUNCAO") == cFuncao
    EndIf
   nDias If+= !Empty(cEscala) .AND. lRetorno
Day(aItTFF[nX][5][2])
            lRetorno := oMdlTFF:GetValue("TFF_ESCALA") == cEscala
    EndIf    
      If !Empty(cCompet) .AND. lRetorno
       EndIf lRetorno := U_VldCompC(oMdlTFF:GetValue("TFF_COD"), cCompet)
    EndIf
            
			EndIf
       
Return lRetorno
    If nDias >= 30
User  Function AplicQtd(oMdlTFF,nQuant,dDataRef,lSetValue, nPerc, lCronog, cCompet)
Local lRet         nDias := .T.
Local nDataFim nDias - 30
            := Day(LastDate(dDataRef)) // ultimo dianMeses do+= mes1
Local nValor        := 0
Local nValorCalc  EndIf
  := 0
Local nVlrPrx    Else 
  := oMdlTFF:GetValue("TFF_VLPRPA") //Valor da proxima parcela
Local nDataIni    If nX :== DayLen(dDataRefaItTFF) //dia de inicio do contrato
Local nQtdAnt.And. aItTFF[nX][5][2] == aItTFF[nX][4][2]
        := oMdlTFF:GetValue("TFF_QTDVEN")
Local oModel      nMeses  := oMdlTFF:GetModel()0
Local oMdlTFL       := oModel:GetModel("TFL_LOC")
Local oMdlTFJ      nDias := oModel:GetModel("TFJ_REFER")
Local cProxParc 1  
    := TecBDt2Cmp(POSICIONE("CNA",1,xFilial("CNA")+oMdlTFF:GetValue("TFF_CONTRT")+oMdlTFF:GetValue("TFF_CONREV")+oMdlTFL:GetValue("TFL_PLAN"), "CNA_PROMED"))
 
Default cCompet := ""
 
If lSetValue
       Else 
       //Altera a quantidade do campo TFF_QTDVEN
    lRetnMeses := oMdlTFF:SetValue("TFF_QTDVEN",nQuant)
0
          
     //Realiza onDias calculo:= da0 proxima parcela
       If lRet
    EndIf    nValor
  := oMdlTFF:GetValue("TFF_PRCVEN") * (nQuant - nQtdAnt) //EndIf preço de venda do contrato
    EndIf    
    aAdd(aItTFF[nX],{"MESES",nMeses})
    aAdd(aItTFF[nX],{"DIAS",nDias})
    nValornMeses := (nValor/nDataFim)*((nDataFim-nDataIni)+1)
  
 0
    nDias := 0     
Next nX //Arredonda

Return aItTFF

//Pega os valores e dias para realizar o calculo do valor
  retrotivo por mês, utilizado no relatorio nValor := Round(nValor,TamSX3("TFL_VLPRPA")[2])
        nValorCalcTECR871
User Function GetDtTFF(cTable,nInd,cChave,cContr,dDataDe,dDataAte,lEncerra,dDataEnce)
Local aItTFF    := nValor{}
Local aItens    := {}
Local dIniAux  nValor := nVlrPrxCTOD(" + nValor
/  /  ")
Local dFimAux   oMdlTFF:SetValue= CTOD("TFF_VLPRPA",nValor)
  /      If lCronog/  ")

DbSelectArea(cTable)
(cTable)->(DbSetOrder(nInd))

If (cTable)->(MsSeek(cChave))

    aAdd(aItTFF,{cTable+'_COD',(&("(cTable)->" + cTable+'_COD'))})
    aAdd(aItTFF,{cTable+'_QTDVEN',(&("(cTable)->" + cTable+'_QTDVEN'))})
   If !(TecHasTGT("TFF",oMdlTFF:GetValue("TFF_COD"),cProxParc,oMdlTFJ:GetValue("TFJ_CODIGO"),@nVlrPrx)) aAdd(aItTFF,{cTable+'_PRCVEN',U_ValOriTFF((&("(cTable)->" + cTable+'_FILIAL')),(&("(cTable)->" + cTable+'_COD')),cContr,cTable,cTable+'_PRCVEN')})

    If (&("(cTable)->" + cTable+'_PERINI')) <= dDataDe
       nVlrPrx := At740PrxPa(/*cTipo*/,;aAdd(aItTFF,{cTable+'_PERINI',dDataDe})
    Else 
        If Day((&("(cTable)->" +         nQtdAnt,;
           cTable+'_PERINI'))) > 30
            dIniAux oMdlTFF:GetValue("TFF_PRCVEN"),;
  := (&("(cTable)->" + cTable+'_PERINI')) - 1
        Else 
            dIniAux oMdlTFF:GetValue("TFF_DESCON"),;:= (&("(cTable)->" + cTable+'_PERINI'))
        EndIf     
           oMdlTFF:GetValue("TFF_TXLUCR"),;aAdd(aItTFF,{cTable+'_PERINI',dIniAux})
    EndIf

    If !lEncerra
        If (&("(cTable)->"      oMdlTFF:GetValue("TFF_TXADM"))+ cTable+'_PERFIM')) >= dDataAte
            EndIfaAdd(aItTFF,{cTable+'_PERFIM',dDataAte})
            nValor := nVlrPrx + nValorCalcElse 
            If !Empty(cCompet) .And. cProxParcDay((&("(cTable)->" + cTable+'_PERFIM'))) > cCompet30
                At740IAuto("TFF", oMdlTFF:GetValue("TFF_COD"), cCompet, nValorCalc, oMdlTFJ:GetValue("TFJ_CODIGO"))dFimAux := (&("(cTable)->" + cTable+'_PERFIM')) - 1
            Else 
                At740IAuto("TFF", oMdlTFF:GetValue("TFF_COD"), cProxParc, nValor, oMdlTFJ:GetValue("TFJ_CODIGO"dFimAux := (&("(cTable)->" + cTable+'_PERFIM'))
            EndIf  
            aAdd(aItTFF,{cTable+'_PERFIM',dFimAux})
        EndIf
    EndIf
Else 
    nValor   := oMdlTFF:GetValue("TFF_PRCVEN") * (nQuant - nQtdAnt)  // Valor a ser reajustado
 If dDataEnce > dDataAte
              
aAdd(aItTFF,{cTable+'_PERFIM',dDataAte})
     nValor := (nValor/nDataFim)*((nDataFim-nDataIni)+1)
   Else 
         
    //Arredonda o valorIf Day(dDataEnce) > 30
    nValor := Round(nValor,TamSX3("TFL_VLPRPA")[2])
EndIf
       
Return nValor
  dFimAux := dDataEnce - 1
User    Function CompetCo()
Local aCompets      :=Else CtrCompets()
Local nX
      
For nX := 1 to Len(aCompets)
    aCompets[nX]dFimAux := CVALTOCHAR(nX)+'='+aCompets[nX]
Next nX
dDataEnce
       
Return aCompets
    EndIf  
User Function VldCompC(cCodTFF, cCompet)
Local cQuery    := ""
Local cAliasCND := GetNextAlias(aAdd(aItTFF,{cTable+'_PERFIM',dFimAux})
Local lRet      := .T.EndIf
    EndIf   
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 aAdd(aItens,aItTFF)

    //Ajusta os itens para realizar o calculo dos dias
    U_GetDtItens(@aItens)
EndIf    

Return aItens

//Pega o valor original da TFF antes do reajuste
User Function ValOriTFF(cFilIt,cCodIt,cContr,cTabela,cCampo)
Local cSql      := ""
Local cFields	:= ""
Local cFrom		:= ""
Local nValor    := 0 
Local cAliasIt  := GetNextAlias()

If cTabela == "TFF" 
    
    cFields := 	"TFF.TFF_FILIAL,TFF.TFF_PRCVEN "

    cSql += " TFF.D_E_L_E_T_ = ' ' AND "
    cSql += " TFF.TFF_FILIAL = '" + xFilial("CNE")cFilIt + "' AND "
cQuery    cSql += " AND CNETFF.D_E_L_E_T_TFF_CONTRT = ' ' "
cQuery += "cContr INNER JOIN+ "' +AND RetSQLName("CND")
 + " CND "
cQuerycSql += " ON CNDTFF.CNDTFF_CONTRACODSUB = CNE.CNE_CONTRA "
cQuery'" + cCodIt += "' AND CND.CND_REVISA = CNE.CNE_REVISA "
cQuery += " AND CND.CND_FILIAL = '" + xFilial("CND") + "' "
cQuery"

ElseIf cTabela == "TFG"

    cFields := 	"TFG.TFG_FILIAL,TFG.TFG_PRCVEN "

    cSql += " AND CNDTFG.D_E_L_E_T_ = ' ' AND "
cQuery    cSql += " AND CNDTFG.CNDTFG_COMPETFILIAL = '" + cCompetcFilIt + "' AND "
cQuery += " WHERE "
cQuerycSql += " TFFTFG.TFFTFG_FILIALCONTRT = '" + xFilial("TFF")cContr + "' AND "
cQuery    cSql += " AND TFFTFG.TFFTFG_CODCODSUB = '" + cCodTFFcCodIt + "' "
cQuery +
ElseIf cTabela == " AND TFF.D_E_L_E_T_ = ' ' "
      
cQueryTFH"

    cFields  := ChangeQuery(cQuery)
DbUseArea(.T., "TOPCONN",TcGenQry(,,cQuery), cAliasCND , .T., .T.)	"TFH.TFH_FILIAL,TFH.TFH_PRCVEN "

    cSql +=  
lRet := ( cAliasCND  )->( !EOF() )" TFH.D_E_L_E_T_ = ' ' AND "
    cSql += 
( 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" TFH.TFH_FILIAL = '" + cFilIt + "' AND "
    cSql += " TFH.TFH_CONTRT = '" + cContr + "' AND "
    ForcSql 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


" TFH.TFH_CODSUB = '" + cCodIt + "' "

EndIf 

cFields := "%" + cFields + "%"
cFrom := "%" + RetSqlName(cTabela) + " " + cTabela + "%"
cSql := "%"+cSql+"%"

BeginSql Alias cAliasIt
	
	SELECT %Exp:cFields%
		FROM %Exp:cFrom%
		WHERE %exp:cSql% 
	EndSql

	If (cAliasIt)->(!Eof())
		nValor := (cAliasIt)->&(cCampo)
	Endif

	(cAliasIt)->(DbCloseArea())

Return nValor 
  


04. DEMAIS INFORMAÇÕES

DSERSGS-10241 DT Criação de gatilho para preenchimento de valor pro-rata

DSERSGS-10968 DT Gatilho de pro-rata para o campo Dt.Fim

DT PE AT870Ant Antecipação do Contrato

DSERSGS-12603 DT Gatilho Escala x Hora Extra Planejada

DSERSGS-10243 DT Inclusão de gatilho para recuperar valor de hora extra

5. ASSUNTOS RELACIONADOS

  • Não há.

...