I made a code to update the Base Documents before the covenants were processed by a request made to facilitate the unit price calculation on the lines based on the current exchange rate. The code works at 98% today something happened and wanted to simulate the situation again and nothing and thought maybe it is an ERP business rule. And you decide to come here to see if anyone could help me. Thanks in advance. Below is the code.
Dim actualizaAvencas As Boolean
Private askForUpdate As Boolean
Dim document As GcpBE900.GcpBEDocumentoVenda
Private Sub EditorAvencas_AntesDeProcessar(Cancel As Boolean, CancelProcessamento As Boolean)
If Not askForUpdate Then
'Assign the returned value to check if it is the first time or not (Sim = True and N?o = False)
actualizaAvencas = PlataformaPRIMAVERA.Dialogos.MostraPerguntaSimples("Deseja actualizar as Aven?as com o c?mbio actual?")
'Do not ask user again for the next aven?a
askForUpdate = True
End If
If actualizaAvencas Then
'Get the original doc
Set document = BSO.Comercial.Vendas.Edita(Avenca.FilialDocOriginal, Avenca.TipoDocOriginal, Avenca.SerieDocOriginal, Avenca.NumDocOriginal)
ReCalculateAvencas document, GetCurrentExRate()
'Free the memory
Set document = Nothing
End If
End Sub
Public Function GetCurrentExRate() As Double
'get the last ExRate for Venda
Dim listaCambioMAlt As StdBE900.StdBELista
Set listaCambioMAlt = BSO.Consulta("SELECT TOP 1 Venda FROM MoedasHistorico where Moeda = '" & BSO.Contexto.MoedaAlternativa & "' ORDER BY Data DESC")
'Update the original doc with new Cambio.
If Not listaCambioMAlt.Vazia() Then
GetCurrentExRate = CDbl(listaCambioMAlt(0))
End If
Set listaCambioMAlt = Nothing
End Function
'===================================
Public Sub ReCalculateAvencas(ByRef Doc As GcpBEDocumentoVenda, ByRef cambioRef As Double)
On Error GoTo ErrorHandler
'Set in edition mode
Doc.EmModoEdicao = True
'Assign values of CDUs of current line by matching the CDUs of new added Item
'update each line.
Dim counter As Long
For counter = 1 To Doc.Linhas.NumItens
'Verify if the first column is an Item instead of a comment lines, acerts, ...
If Len(Doc.Linhas(counter).artigo) > 0 And HasSpecifiedValueInUSD(Doc.Linhas(counter).artigo) Then
'Store the USD amount of the current line to by used later in multiple places ...
Dim prUnitUSD As Double
prUnitUSD = CDbl(Doc.Linhas(counter).CamposUtil("CDU_PrecoUnitMoedaRef"))
Doc.Linhas(counter).CamposUtil("CDU_TotalLiqMoedaRef") = Doc.Linhas(counter).Quantidade * prUnitUSD
Doc.Linhas(counter).PrecUnit = cambioRef * prUnitUSD
End If
Next counter
'Update cambio of the header
If Doc.Moeda = BSO.Contexto.MoedaAlternativa Then
Doc.Cambio = cambioRef
Else
Doc.CambioMAlt = cambioRef
End If
'Update the document after update its lines.
Dim msg As String
BSO.Comercial.Vendas.Actualiza Doc, msg
'Update aven?a value with new calculated original doc
Avenca.EmModoEdicao = True
Avenca.Valor = Doc.TotalDocumento
Exit Sub
ErrorHandler:
PlataformaPRIMAVERA.Dialogos.MostraAviso "Erro!", PRI_Informativo, msg & vbNewLine & Err.Description
End Sub
Private Sub EditorAvencas_AvencaIdentificada(TipoProcessamento As Integer, Cancel As Boolean)
askForUpdate = False
End Sub
'=======
Function HasSpecifiedValueInUSD(ByVal artigo As String) As Boolean
'Updated If it has specified price in USD
Dim prUnitUSD As Double
prUnitUSD = CDbl(BSO.Comercial.Artigos.DaValorAtributo(artigo, "CDU_PvPMoedaRef"))
If prUnitUSD <> vbNull And prUnitUSD > 0 Then
HasSpecifiedValueInUSD = True
Else
HasSpecifiedValueInUSD = False
End If
End Function
What happened was the exchange rate is 60 and in the first document (FA) to be generated from the base document (FP) it seems that the change was considered to be 62.42 and I do not understand how this could have happened .
Thenormalbehaviorinanotherdocumentwhereeverythingwentwell.