Adicão da rota de despesas, agora melhorado

This commit is contained in:
Ricardo 2026-09-24 13:46:28 -03:00
parent 2e18489163
commit 9aad6ff8f4
3 changed files with 101 additions and 18 deletions

View file

@ -0,0 +1,75 @@
import json
from datetime import date
from decimal import ROUND_HALF_UP, Decimal
from sqlalchemy.pool import PoolProxiedConnection
from app.v1.modules.escrituracao.models.modelo_notas import NotasHolmesInput
class NotasDespesas:
def __init__(self, conexao: PoolProxiedConnection) -> None:
self.conexao = conexao
@staticmethod
def montar_vencimentos(valor: Decimal, datas: list[date]) -> list[dict]:
# Divide em centavos pra soma bater exato; a sobra vai pra primeira parcela
total_centavos = int(valor * 100)
qtd = len(datas)
base, sobra = divmod(total_centavos, qtd)
parcelas = []
for i, data in enumerate(datas):
centavos = base + (sobra if i == 0 else 0)
parcelas.append({
'valor': float(Decimal(centavos) / 100),
'data': data.isoformat(),
})
return parcelas
def inserir_nota_despesas(self, nota: NotasHolmesInput) -> bool:
props = nota.properties
datas = sorted(
d for d in (getattr(props, f'vencimento_{n}' if n > 1 else 'vencimento') for n in range(1, 13))
if d is not None
)
if not datas and props.data_vencimento:
datas = [props.data_vencimento]
if not datas:
raise ValueError('Nota sem data de vencimento')
if props.valor is None:
raise ValueError('Nota sem valor')
valor = Decimal(str(props.valor)).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
vencimentos_json = json.dumps(self.montar_vencimentos(valor, datas))
cursor = self.conexao.cursor()
cursor.execute("""
INSERT INTO PD_HOLMES_APOLLO (
ID_HOLMES,
PROTOCOLO,
EMPRESA_NOME,
TIPO_DOCUMENTO,
SITUACAO,
NUM_NOTA,
DTA_EMISSAO,
VAL_NOTA,
VENCIMENTOS_JSON,
ORIGEM
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
nota.id,
props.protocolo,
props.empresa,
props.tipo_de_documento,
props.aprovacao,
props.num_nota,
props.data_emissao,
valor,
vencimentos_json,
props.origem,
))
self.conexao.commit()
return cursor.rowcount > 0