Files
shoppy/shop/utils.py
T
2024-03-24 14:31:35 +01:00

30 lines
774 B
Python

from decimal import Decimal
from shop.models import OrderLine, Tax, Product, Order
from django.db.models import QuerySet
def create_order_line_for_product(product: Product, quantity: Decimal, tax: Tax):
price = product.prices.last().price
base_total = round(price * quantity, 2)
taxes = round(base_total * (tax.value / Decimal("100")), 2)
return OrderLine.objects.create(
product=product,
quantity=quantity,
price=price,
base_total=base_total,
tax_value=tax.value,
taxes=taxes,
total=base_total + taxes,
)
def create_order(lines: QuerySet):
order = Order.objects.create()
for line in lines.all():
order.lines.add(line)
order.calculate_total_from_lines()
return order