Skip to content

Commit

Permalink
feat: add commission flees
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatasoli committed Oct 5, 2024
1 parent b7f8743 commit e18042e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
12 changes: 11 additions & 1 deletion app/report/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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()
32 changes: 27 additions & 5 deletions app/report/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e18042e

Please sign in to comment.