Skip to content

Commit

Permalink
Реализовал механизм расчета финальной стоимости продукта
Browse files Browse the repository at this point in the history
  • Loading branch information
Abramov0Alexandr committed Sep 27, 2023
1 parent 6982c7a commit 768453f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
15 changes: 15 additions & 0 deletions products/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.auth import get_user_model
from django.db import models
from decimal import Decimal


class Product(models.Model):
Expand All @@ -23,6 +24,20 @@ class Product(models.Model):
def shop_name(self):
return self.seller.shop_name

def calculate_final_price(self):
"""
Расчет финальной стоимости товара
"""

base_price = self.price
tax = base_price * Decimal('0.06') #: Налог
bank_fee = base_price * Decimal('0.02') #: Комиссия банку
author_commission = base_price * Decimal('0.02') #: Комиссия за транзакцию продавца
marketplace_fee = base_price * Decimal('0.20') #: Выручка маркетплейса

final_price = base_price + tax + bank_fee + author_commission + marketplace_fee
return final_price

def __str__(self):
return f'{self.product_title} {self.shop_name} {self.seller}'

Expand Down
3 changes: 3 additions & 0 deletions products/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def perform_create(self, serializer):

new_product = serializer.save(seller=self.request.user)
new_product.seller = self.request.user

total_price = new_product.calculate_final_price()
new_product.price = total_price
new_product.save()


Expand Down

0 comments on commit 768453f

Please sign in to comment.