From e9a98424bd7c5a0db8a978c847e5a22e4d25dbb7 Mon Sep 17 00:00:00 2001 From: Ricardo Date: Thu, 24 Sep 2026 13:54:39 -0300 Subject: [PATCH] =?UTF-8?q?Adic=C3=A3o=20da=20rota=20de=20despesas,=20agor?= =?UTF-8?q?a=20atualizando?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/v1/modules/escrituracao/router.py | 9 ++----- app/v1/modules/escrituracao/service.py | 34 ++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/app/v1/modules/escrituracao/router.py b/app/v1/modules/escrituracao/router.py index 44659db..1fcce21 100644 --- a/app/v1/modules/escrituracao/router.py +++ b/app/v1/modules/escrituracao/router.py @@ -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}' } \ No newline at end of file diff --git a/app/v1/modules/escrituracao/service.py b/app/v1/modules/escrituracao/service.py index 08e3f35..587baa5 100644 --- a/app/v1/modules/escrituracao/service.py +++ b/app/v1/modules/escrituracao/service.py @@ -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'