Files

384 lines
12 KiB
Python

from api.models.external_print_product import get_cheapest_canvas_product, get_cheapest_poster_product
from flask import Blueprint, request, jsonify
from api.helpers import check_api_key
from api.validators import ValidationError, validate_number, validate_exists
from api.models import (
Inquiry,
Market,
Material,
Product,
ContractCustomer,
)
from api.lib.prices import (
wallpaper_price,
wallpaper_m2_price,
inquiry_price,
wallpaper_kit_price,
external_product_price,
wallpaper_price_new,
external_product_price_new,
inquiry_price_new,
)
mod = Blueprint("prices", __name__, url_prefix="/prices")
mod.before_request(check_api_key)
def get_reseller():
dealerurl = request.args.get("dealerurl")
territory = request.args.get("territory")
if dealerurl and territory:
return ContractCustomer.query.reseller_store(dealerurl, territory)
return None
def get_designer():
product_id = request.args.get("product")
if product_id:
product = Product.query.get(product_id)
if product:
return product.designer
return None
@mod.route("/wallpaper", methods=["GET"])
@validate_number("width", "height")
@validate_exists("market")
def wallpaper():
width = request.args.get("width", type=float)
height = request.args.get("height", type=float)
market_id = request.args.get("market", type=int)
product_id = request.args.get("product", type=int)
unit = request.args.get("unit", type=str)
new_version = request.args.get("new_version") is not None
market = Market.query.get_or_404(market_id)
designer = get_designer()
reseller = get_reseller()
if width is None or height is None:
raise ValidationError("width and height must be float")
materials = Material.query.wallpapers().all()
data = []
for material in materials:
if new_version:
prices = wallpaper_price_new(product_id, width, height, material, market, unit)
data.append(
{
"material": material.name,
"material_id": material.id,
"m2_price": prices["m2_price_incl_vat"],
"m2_price_excl_vat": prices["m2_price_excl_vat"],
"price": prices["incl_vat"],
"price_excl_vat": prices["excl_vat"],
"segment": prices["segment"],
"segment_price": prices["segment_price"],
}
)
continue
data.append(
{
"material": material.name,
"material_id": material.id,
"m2_price": wallpaper_m2_price(
material,
market,
reseller=reseller,
designer=designer,
rounded=False,
inc_vat=True,
),
"m2_price_excl_vat": wallpaper_m2_price(
material,
market,
reseller=reseller,
designer=designer,
rounded=False,
inc_vat=False,
),
"price": wallpaper_price(
width,
height,
material,
market,
reseller=reseller,
designer=designer,
rounded=False,
inc_vat=True,
),
"price_excl_vat": wallpaper_price(
width,
height,
material,
market,
reseller=reseller,
designer=designer,
rounded=False,
inc_vat=False,
),
}
)
return jsonify(data=data)
@mod.route("/wallpaper-kit", methods=["GET"])
@validate_exists("market")
def wallpaper_kit():
market_id = request.args.get("market", type=int)
market = Market.query.get_or_404(market_id)
reseller = get_reseller()
data = [
{
"price": wallpaper_kit_price(market, reseller=reseller, rounded=False),
"price_excl_vat": wallpaper_kit_price(
market, reseller=reseller, rounded=False, inc_vat=False
),
}
]
return jsonify(data=data)
def get_inquiry_measurements(inquiry):
""" this function returns width and height to be used in the price calculations """
# first try to get it from the query string. If that fails try to get it
# from the inquiry row in the database
unit = request.args.get("unit", type=str, default=None)
width = request.args.get("width", type=float, default=None)
height = request.args.get("height", type=float, default=None)
if unit and not (width and height):
raise Exception("With unit in query string width and height must also be there.")
if not width:
width = inquiry.width
if not height:
height = inquiry.height
# if we still are missing a width/height just set them to 1 to avoid
# errors in the price calculations
if not width:
width = 1
if not height:
height = 1
return width, height, unit
def inquiry_wallpaper(inquiry, new_version=False):
width, height, unit = get_inquiry_measurements(inquiry)
data = {
"id": inquiry.id,
"group": "wallpaper",
"width": width,
"height": height,
"unit": unit,
"retouch_price": inquiry.local_price_retouch(rounded=False, inc_vat=True),
}
materials = Material.query.wallpapers().all()
prices = []
for material in materials:
if new_version:
price = inquiry_price_new(inquiry, material=material, width=width, height=height, unit=unit)
prices.append(
{
"material": material.name,
"material_id": material.id,
"m2_price": price["m2_price_incl_vat"],
"m2_price_excl_vat": price["m2_price_excl_vat"],
"inquiry_price": price["incl_vat"],
"inquiry_price_excl_vat": price["excl_vat"],
"inquiry_price_excl_retouch": price["excl_retouch_incl_vat"],
"inquiry_price_excl_retouch_excl_vat": price["excl_retouch_excl_vat"],
"segment": price["segment"],
"segment_price": price["segment_price"],
}
)
continue
prices.append(
{
"material": material.name,
"material_id": material.id,
"m2_price": wallpaper_m2_price(
material,
inquiry.market,
designer=inquiry.designer,
reseller=inquiry.dealer_store,
inquiry=inquiry,
rounded=False,
inc_vat=True,
),
"m2_price_excl_vat": wallpaper_m2_price(
material,
inquiry.market,
designer=inquiry.designer,
reseller=inquiry.dealer_store,
inquiry=inquiry,
rounded=False,
inc_vat=False,
),
"inquiry_price_excl_retouch": inquiry_price(
inquiry,
material=material,
width=width,
height=height,
rounded=False,
inc_price_retouch=False,
inc_vat=True,
),
"inquiry_price": inquiry_price(
inquiry,
material=material,
width=width,
height=height,
rounded=False,
inc_vat=True,
),
"inquiry_price_excl_vat": inquiry_price(
inquiry,
material=material,
width=width,
height=height,
rounded=False,
inc_vat=False,
),
"inquiry_price_excl_price_premium": inquiry_price(
inquiry,
material=material,
width=width,
height=height,
reseller=None,
inc_price_retouch=False,
rounded=False,
inc_vat=False,
),
}
)
data["prices"] = prices
return data
def inquiry_external_product(inquiry, external_id, new_version):
if new_version:
result = inquiry_price_new(inquiry, external_id=external_id)
data = {
"id": inquiry.id,
# No manual inquiries for external products, hence no retouch price
"retouch_price": 0,
"prices": [
{
"inquiry_price": result["incl_vat"],
"inquiry_price_excl_vat": result["excl_vat"],
"inquiry_price_excl_retouch": result["incl_vat"],
"inquiry_price_excl_retouch_excl_vat": result["excl_vat"],
"segment": result["segment"],
"segment_price": result["segment_price"],
}
],
}
return data
data = {
"id": inquiry.id,
"retouch_price": inquiry.local_price_retouch(rounded=False, inc_vat=True),
"prices": [
{
"inquiry_price": inquiry_price(
inquiry, external_id=external_id, rounded=False, inc_vat=True
),
"inquiry_price_excl_vat": inquiry_price(
inquiry, external_id=external_id, rounded=False, inc_vat=False
),
"inquiry_price_excl_retouch": inquiry_price(
inquiry, external_id=external_id, rounded=False, inc_vat=True, inc_price_retouch=False,
),
"inquiry_price_excl_retouch_excl_vat": inquiry_price(
inquiry, external_id=external_id, rounded=False, inc_vat=False, inc_price_retouch=False,
),
}
],
}
return data
@mod.route("/inquiry/<int:inquiry_id>", methods=["GET"])
def inquiry(inquiry_id):
inquiry = Inquiry.query.get_or_404(inquiry_id)
external_product_id = request.args.get("external_product_id")
listprice = request.args.get("listprice")
new_version = request.args.get("new_version") is not None
if listprice:
if listprice == 'canvas':
external_product_id = get_cheapest_canvas_product(new_version)
elif listprice == 'poster':
external_product_id = get_cheapest_poster_product(new_version)
else:
raise ValueError("Invalid listprice group {}".format(listprice))
if external_product_id:
data = inquiry_external_product(inquiry, external_product_id, new_version)
elif inquiry.product_group == "photo-wallpaper":
data = inquiry_wallpaper(inquiry, new_version)
else:
raise ValueError("Invalid group {}".format(inquiry.product_group))
return jsonify(data)
@mod.route("/external-product", methods=["GET"])
@validate_exists("market")
@validate_exists("id")
def external_product():
market_id = request.args.get("market", type=int)
market = Market.query.get_or_404(market_id)
id = request.args.get("id", type=str)
new_version = request.args.get("new_version") is not None
if new_version:
product_id = request.args.get("product", type=int)
prices = external_product_price_new(product_id, id, market)
data = [
{
"id": id,
"price": prices["incl_vat"],
"price_excl_vat": prices["excl_vat"],
"segment": prices["segment"],
"segment_price": prices["segment_price"],
}
]
return jsonify(data=data)
designer = get_designer()
data = [
{
"id": id,
"price": external_product_price(
id,
market,
designer=designer,
rounded=False,
inc_vat=True,
),
"price_excl_vat": external_product_price(
id,
market,
designer=designer,
rounded=False,
inc_vat=False,
),
}
]
return jsonify(data=data)