Adicão da rota de despesas, agora atualizando

This commit is contained in:
Ricardo 2026-09-24 13:54:39 -03:00
parent 9aad6ff8f4
commit e9a98424bd
2 changed files with 34 additions and 9 deletions

View file

@ -20,18 +20,13 @@ def inserir_nota_despesas(
repo = NotasDespesas(conexao)
try:
inseriu = repo.inserir_nota_despesas(dados_nota_holmes)
acao = repo.inserir_nota_despesas(dados_nota_holmes)
except ValueError as erro:
raise HTTPException(
status_code= status.HTTP_422_UNPROCESSABLE_ENTITY,
detail= str(erro)
)
if not inseriu:
raise HTTPException(
status_code= status.HTTP_500_INTERNAL_SERVER_ERROR,
detail= "Não foi possível inserir a nota"
)
return {
'mensagem': 'Nota inserida'
'mensagem': f'Nota {acao}'
}

View file

@ -27,7 +27,7 @@ class NotasDespesas:
})
return parcelas
def inserir_nota_despesas(self, nota: NotasHolmesInput) -> bool:
def inserir_nota_despesas(self, nota: NotasHolmesInput) -> str:
props = nota.properties
datas = sorted(
@ -45,6 +45,36 @@ class NotasDespesas:
vencimentos_json = json.dumps(self.montar_vencimentos(valor, datas))
cursor = self.conexao.cursor()
# A mesma nota pode chegar mais de uma vez: se já existe, atualiza
cursor.execute("""
UPDATE PD_HOLMES_APOLLO
SET
EMPRESA_NOME = ?,
TIPO_DOCUMENTO = ?,
SITUACAO = ?,
NUM_NOTA = ?,
DTA_EMISSAO = ?,
VAL_NOTA = ?,
VENCIMENTOS_JSON = ?,
ORIGEM = ?
WHERE ID_HOLMES = ?
""", (
props.empresa,
props.tipo_de_documento,
props.aprovacao,
props.num_nota,
props.data_emissao,
valor,
vencimentos_json,
props.origem,
nota.id,
))
if cursor.rowcount > 0:
self.conexao.commit()
return 'atualizada'
cursor.execute("""
INSERT INTO PD_HOLMES_APOLLO (
ID_HOLMES,
@ -72,4 +102,4 @@ class NotasDespesas:
))
self.conexao.commit()
return cursor.rowcount > 0
return 'inserida'