Files
api2/api/lib/prices.py
T

186 lines
4.4 KiB
Python

# coding=UTF-8
""" Price formulas """
from api.lib.utils import round_half_up
from api.models.external_print_product import get_external_print_product
from api.models.price_adjustments import get_price_adjustment
def wallpaper_m2_price(
material,
market,
designer=None,
reseller=None,
inquiry=None,
rounded=True,
inc_vat=True,
):
""" calculates price per square meter """
price = material.price / 100.0
if designer:
price *= (float(designer.pricepremium_wallpaper) / 100) + 1
if reseller:
price *= (float(reseller.pricepremium) / 100) + 1
if inquiry:
price *= (float(inquiry.pricepremium) / 100) + 1
price *= market.exchange_rate
price *= market.price_adjustments
inquiry_or_product_key = "product" if inquiry is None else "inquiry"
price *= get_price_adjustment(market.name, material.name, inquiry_or_product_key)
if inc_vat:
price *= market.vat
if rounded:
price = round_half_up(price)
return price
def wallpaper_price(
width,
height,
material,
market,
designer=None,
reseller=None,
inquiry=None,
rounded=True,
inc_vat=True,
):
price = wallpaper_m2_price(
material, market, designer, reseller, inquiry, rounded=False, inc_vat=False
)
sqm = (width / 100) * (height / 100)
min_sqm = 1
sqm = max(sqm, min_sqm)
price *= sqm
if inc_vat:
price *= market.vat
if rounded:
price = round(price)
return price
def _designer_pricepremium(designer, group_id):
if group_id in [1,3,6]:
return designer.pricepremium_wallpaper
if group_id == 2:
return designer.pricepremium_canvas
if group_id == 7:
return designer.pricepremium_poster
if group_id == 8:
return designer.pricepremium_framed_print
raise ValueError("Unexpected group id: {}".format(group_id))
def external_product_price(
id, market, designer=None, inquiry=None, rounded=True, inc_vat=True
):
external_product = get_external_print_product(id)
price = external_product["price"]
price *= market.price_adjustments
if designer:
designer_pricepremium = _designer_pricepremium(designer, external_product["group_id"])
price *= (float(designer_pricepremium) / 100) + 1
if inquiry:
price *= (float(inquiry.pricepremium) / 100) + 1
price *= market.exchange_rate
if inc_vat:
price *= market.vat
if rounded:
price = round_half_up(price)
return price
def inquiry_price(
inquiry, inc_price_retouch=True, rounded=True, inc_vat=True, **kwargs
):
width = kwargs.get("width", inquiry.width)
height = kwargs.get("height", inquiry.height)
material = kwargs.get("material", inquiry.material)
reseller = kwargs.get("reseller", inquiry.dealer_store)
external_id = kwargs.get("external_id")
if external_id:
price = external_product_price(
id=external_id,
market=inquiry.market,
designer=inquiry.designer,
inquiry=inquiry,
rounded=False,
inc_vat=False,
)
elif inquiry.product_group == "photo-wallpaper":
price = wallpaper_price(
width,
height,
material,
inquiry.market,
designer=inquiry.designer,
reseller=reseller,
inquiry=inquiry,
rounded=False,
inc_vat=False,
)
else:
raise ValueError("Invalid product group: {}".format(inquiry.product_group))
effect_price = inquiry.price_effect
if effect_price:
price += float(effect_price)
if inc_price_retouch:
retouch_price = inquiry.local_price_retouch(rounded=False, inc_vat=False)
if retouch_price:
price += retouch_price
if inc_vat:
price *= inquiry.market.vat
if rounded:
price = round_half_up(price)
return price
def wallpaper_kit_price(
market, reseller=None, rounded=True, inc_vat=True
):
# Hardcode price as it hasn't been updated for a long time
# price from "product-stockprices"
price = 143.2
price *= market.price_adjustments
if reseller:
price *= (float(reseller.pricepremium) / 100) + 1
price *= market.exchange_rate
if inc_vat:
price *= market.vat
if rounded:
price = round_half_up(price)
return price