diff --git a/app/report/repository.py b/app/report/repository.py index 23458056..9b5fa29e 100644 --- a/app/report/repository.py +++ b/app/report/repository.py @@ -7,7 +7,7 @@ from sqlalchemy.orm import Session from pydantic import TypeAdapter -from app.infra.models import InformUserProductDB, SalesCommissionDB, UserDB +from app.infra.models import CoProducerFeeDB, FeeDB, InformUserProductDB, SalesCommissionDB, UserDB from app.entities.report import CommissionInDB, InformUserProduct, UserSalesCommissions @@ -75,3 +75,13 @@ async def save_user_inform( transaction.add(inform_db) return inform_db + +def get_fees(transaction): + """Get active fees.""" + query = select(FeeDB).where(FeeDB.active.is_(True)) + return transaction.scalars(query).all() + +def get_coproducer(transaction): + """Get co producers.""" + query = select(CoProducerFeeDB).where(CoProducerFeeDB.active.is_(True)) + return transaction.scalars(query).all() diff --git a/app/report/services.py b/app/report/services.py index 9b652f7d..725d47de 100644 --- a/app/report/services.py +++ b/app/report/services.py @@ -3,6 +3,7 @@ from decimal import Decimal from typing import Any from app.entities.product import ProductInDB +from app.infra.constants import FeeType from sqlalchemy.orm import sessionmaker from app.infra.database import get_session @@ -39,13 +40,34 @@ def create_sales_commission( # noqa: PLR0913 db: sessionmaker = get_session(), ) -> SalesCommissionDB: """Get sales commit at all.""" - today = datetime.now(tz=UTC) - release_data = today + timedelta(days=30) - if not commission_percentage: - raise ValueError - commission_value = Decimal(subtotal) * Decimal(commission_percentage) with db.begin() as transaction: + fees = repository.get_fees(transaction) + total_with_fees = subtotal + for fee in fees: + if fee.fee_type == FeeType.PERCENTAGE: + total_with_fees -= total_with_fees * fee.value + if fee.fee_type == FeeType.FIXED: + total_with_fees -= fee.value + + co_producers_fee = repository.get_coproducer(transaction) + if co_producers_fee: + for co_producer_fee in co_producers_fee: + total_with_fees -= total_with_fees * (co_producer_fee.percentage / 100) + + if not commission_percentage: + raise ValueError( + "Nenhum percentual de comissão de vendedor ativo encontrado", + ) + + free_freight = 700 + freight_tax = 65 + if subtotal > free_freight: + total_with_fees = total_with_fees - freight_tax + commission_value = total_with_fees * commission_percentage + today = datetime.now(tz=UTC) + release_data = today + timedelta(days=30) + commission_db = SalesCommissionDB( order_id=order_id, user_id=user_id,