Adicão da rota de despesas, agora melhorado
This commit is contained in:
parent
2e18489163
commit
9aad6ff8f4
3 changed files with 101 additions and 18 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
from datetime import date
|
from datetime import date
|
||||||
|
|
||||||
from pydantic import AliasChoices, BaseModel, ConfigDict, Field
|
from pydantic import AliasChoices, BaseModel, ConfigDict, Field, validate_call
|
||||||
|
|
||||||
|
|
||||||
class NotasHolmesProperties(BaseModel):
|
class NotasHolmesProperties(BaseModel):
|
||||||
|
|
@ -16,6 +16,8 @@ class NotasHolmesProperties(BaseModel):
|
||||||
origem: int | None = Field(None, validation_alias=AliasChoices("orige", "Origem"))
|
origem: int | None = Field(None, validation_alias=AliasChoices("orige", "Origem"))
|
||||||
data_vencimento: date | None = Field(None, validation_alias=AliasChoices("data_vencimento", "venc", "Data de Vencimento"))
|
data_vencimento: date | None = Field(None, validation_alias=AliasChoices("data_vencimento", "venc", "Data de Vencimento"))
|
||||||
|
|
||||||
|
aprovacao: str | None = Field(None, validation_alias=AliasChoices("Aprovação"))
|
||||||
|
|
||||||
vencimento: date | None = Field(None, validation_alias=AliasChoices("vencimento", "Vencimento"))
|
vencimento: date | None = Field(None, validation_alias=AliasChoices("vencimento", "Vencimento"))
|
||||||
vencimento_2: date | None = Field(None, validation_alias=AliasChoices("vencimento_2", "Vencimento 2"))
|
vencimento_2: date | None = Field(None, validation_alias=AliasChoices("vencimento_2", "Vencimento 2"))
|
||||||
vencimento_3: date | None = Field(None, validation_alias=AliasChoices("vencimento_3", "Vencimento 3"))
|
vencimento_3: date | None = Field(None, validation_alias=AliasChoices("vencimento_3", "Vencimento 3"))
|
||||||
|
|
|
||||||
|
|
@ -2,30 +2,36 @@ from fastapi import APIRouter, HTTPException, status
|
||||||
|
|
||||||
from app.core.database import Conexao
|
from app.core.database import Conexao
|
||||||
from app.v1.modules.escrituracao.models.modelo_notas import NotasHolmesInput
|
from app.v1.modules.escrituracao.models.modelo_notas import NotasHolmesInput
|
||||||
|
from app.v1.modules.escrituracao.service import NotasDespesas
|
||||||
|
|
||||||
escrituracao_router = APIRouter(
|
escrituracao_router = APIRouter(
|
||||||
prefix='/escrituracao'
|
prefix='/escrituracao'
|
||||||
)
|
)
|
||||||
|
|
||||||
# Rota de cancelamento (logica pra informar que o processo foi cancelado, esses vendedores que ficam crinando proposta pra dps cancelar é foda)
|
# Rota que recebe a nota do Holmes e grava na PD_HOLMES_APOLLO
|
||||||
@escrituracao_router.post(
|
@escrituracao_router.post(
|
||||||
path='/notas_despesas'
|
path='/notas_despesas'
|
||||||
)
|
)
|
||||||
def cancelar_proposta(
|
def inserir_nota_despesas(
|
||||||
dados_proposta_holmes: NotasHolmesInput,
|
dados_nota_holmes: NotasHolmesInput,
|
||||||
conexao: Conexao,
|
conexao: Conexao,
|
||||||
):
|
):
|
||||||
|
|
||||||
print(dados_proposta_holmes)
|
repo = NotasDespesas(conexao)
|
||||||
# repo = CancelamentoProposta(conexao)
|
|
||||||
|
try:
|
||||||
# cancelou = repo.cancelar_no_banco(dados_proposta_holmes.id)
|
inseriu = repo.inserir_nota_despesas(dados_nota_holmes)
|
||||||
|
except ValueError as erro:
|
||||||
# if not cancelou:
|
raise HTTPException(
|
||||||
# raise HTTPException(
|
status_code= status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||||
# status_code= status.HTTP_404_NOT_FOUND,
|
detail= str(erro)
|
||||||
# detail= "Proposta não encontrada no banco de dados"
|
)
|
||||||
# )
|
|
||||||
# return {
|
if not inseriu:
|
||||||
# 'mensagem': 'Proposta cancelada'
|
raise HTTPException(
|
||||||
# }
|
status_code= status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail= "Não foi possível inserir a nota"
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
'mensagem': 'Nota inserida'
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue