Skip to content

Instantly share code, notes, and snippets.

@vndmtrx
Created June 22, 2012 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vndmtrx/2969691 to your computer and use it in GitHub Desktop.
Save vndmtrx/2969691 to your computer and use it in GitHub Desktop.
Macro DuploLancamento
REM ***** BASIC *****
'
' Macro DuploLancamento
'
' Macro para LibreOffice Calc criada com objetivo de fazer duplo lançamento de transferências entre contas,
' de acordo com a estrutura das tabelas de conta.
'
' Eduardo Rolim - www.tocadoelfo.com.br
' Palmas - 2012
option explicit
sub efetua_lancamento
dim atual as object
dim ultima_linha as object
dim nova_linha as object
dim outra_plan as object
atual = ThisComponent.CurrentController.getActiveSheet()
ultima_linha = busca_ultimo_registro(atual.getName())
if (valida_linha(ultima_linha) = 1) then
nova_linha = busca_linha_livre(ultima_linha.getCellByPosition(5,0).String)
nova_linha.getCellByPosition(0,0).setString(ultima_linha.getCellByPosition(0,0).getString())
nova_linha.getCellByPosition(1,0).setString(ultima_linha.getCellByPosition(1,0).getString())
nova_linha.getCellByPosition(2,0).setData(ultima_linha.getCellByPosition(2,0).getData())
if (ultima_linha.getCellByPosition(3,0).String = ThisComponent.getSheets().getByName("Config").getCellrangeByName("C2").String) then
nova_linha.getCellByPosition(3,0).setString(ThisComponent.getSheets().getByName("Config").getCellrangeByName("C3").getString())
else
nova_linha.getCellByPosition(3,0).setString(ThisComponent.getSheets().getByName("Config").getCellrangeByName("C2").getString())
end if
nova_linha.getCellByPosition(4,0).setString(ultima_linha.getCellByPosition(4,0).getString())
nova_linha.getCellByPosition(5,0).setString(ultima_linha.getCellByPosition(5,0).getString())
ultima_linha.getCellByPosition(6,0).setString("OK")
nova_linha.getCellByPosition(6,0).setString("OK")
end if
end sub
' Função busca_ultimo_registro
'
' Método responsável por encontrar a última linha preenchida na tabela
' Retorna um objeto linha com a linha correspondente.
'
function busca_ultimo_registro(byval plan as string) as object
dim rows as object
dim i as integer
rows = ThisComponent.getSheets().getByName(plan).getRows()
i = 0
do
'if (rows.getByIndex(i).getCellByPosition(0,0).getString() <> "") then
if (is_linha_vazia(rows.getByIndex(i)) = 0) then
i = i + 1
else
i = i - 1 'Se a linha atual é vazia, a linha anterior é a última.
exit do
end if
loop
busca_ultimo_registro() = rows.getByIndex(i)
end function
' Função busca_linha_livre
'
' Método responsável por encontrar a próxima linha disponível na tabela
' Retorna um objeto linha com a linha correspondente.
'
function busca_linha_livre(byval plan as string) as object
dim rows as object
dim i as integer
rows = ThisComponent.getSheets().getByName(plan).getRows()
i = 0
do
'if (rows.getByIndex(i).getCellByPosition(0,0).getString() <> "") then
if (is_linha_vazia(rows.getByIndex(i)) = 0) then
i = i + 1
else
exit do
end if
loop
busca_linha_livre() = rows.getByIndex(i)
end function
' Função is_linha_vazia
'
' Método responsável por avaliar se uma linha possui campos preenchidos
' Retorna 1 caso a linha esteja vazia; 0 caso haja campos preenchidos na linha.
'
function is_linha_vazia(linha as object) as integer
dim i as integer
dim retorno as integer
retorno = 1
for i = 0 to 6
if (linha.getCellByPosition(i,0).String <> "") then
retorno = 0
exit for
end if
next i
is_linha_vazia() = retorno
end function
' Função valida_linha
'
' Método responsável por fazer a verificação de todos os parâmetros da linha de forma a garantir que
' a operação de lançamento reverso funcione de maneira correta.
'
' Caso hajam campos faltantes ou errados, uma mensagem será exibida dizendo o erro relatado.
'
function valida_linha(linha as object) as integer
dim retorno as integer
dim linhas as object
dim enum_linhas as object
dim erro as Boolean
dim i as integer
retorno = 1
erro = 0
' Operação já feita na linha
if (linha.getCellByPosition(6,0).String = "OK") then
retorno = 0
Msgbox("Linha já validada", 16, "Transferência de Valores")
exit function
end if
' Data, Item e Montante
if (linha.getCellByPosition(0,0).String = "") or (linha.getCellByPosition(1,0).String = "") or (linha.getCellByPosition(2,0).String = "") then
retorno = 0
Msgbox("Data, Item e Montante não podem ser vazios!", 16, "Transferência de Valores")
end if
'operação = dèbito ou crédito
if (linha.getCellByPosition(3,0).String <> ThisComponent.getSheets().getByName("Config").getCellrangeByName("C2").String) and (linha.getCellByPosition(3,0).String <> ThisComponent.getSheets().getByName("Config").getCellrangeByName("C3").String) then
retorno = 0
Msgbox("Operação deve ser " + ThisComponent.getSheets().getByName("Config").getCellrangeByName("C2").String + " ou " + ThisComponent.getSheets().getByName("Config").getCellrangeByName("C3").String + "!", 16, "Transferência de Valores")
end if
'categoria = transferência
if (linha.getCellByPosition(4,0).String <> ThisComponent.getSheets().getByName("Config").getCellrangeByName("A2").String) then
retorno = 0
Msgbox("Categoria da operação deve ser " + ThisComponent.getSheets().getByName("Config").getCellrangeByName("A2").String + "!", 16, "Transferência de Valores")
end if
'notas = uma das contas
linhas = ThisComponent.getSheets().getByName("Contas").getCellRangeByName("AdmContas")
for i = lbound(linhas.getData()) to ubound(linhas.getData())
if (linha.getCellByPosition(5,0).String = linhas.getCellByPosition(0,i).String) then
erro = 1
exit for
end if
next i
if (erro = 0) then
retorno = 0
Msgbox("Em notas deve constar o nome da conta de destino!", 16, "Transferência de Valores")
end if
valida_linha() = retorno
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment