PW-567 add the old canvas formula

This commit is contained in:
Martin
2016-10-31 17:57:11 +01:00
parent cbee61b564
commit bf9fc3ae5a
10 changed files with 352 additions and 35 deletions
+52
View File
@@ -0,0 +1,52 @@
# coding=UTF-8
CANVAS = {
'max_long_side': 500,
'min_long_side': 20,
'max_short_side': 150,
'min_short_side': 20
}
CANVAS_FRAME = {
'max_long_side': 150,
'min_long_side': 40,
'max_short_side': 150,
'min_short_side': 40,
}
def calculate_canvas_frame_limits(width, height):
if width > height:
width = min(width, CANVAS_FRAME.get('max_long_side'))
width = max(width, CANVAS_FRAME.get('min_long_side'))
height = min(height, CANVAS_FRAME.get('max_short_side'))
height = max(height, CANVAS_FRAME.get('min_short_side'))
else:
height = min(height, CANVAS_FRAME.get('max_long_side'))
height = max(height, CANVAS_FRAME.get('min_long_side'))
width = min(width, CANVAS_FRAME.get('max_short_side'))
width = max(width, CANVAS_FRAME.get('min_short_side'))
# round to nearest 10
width = round(width, -1)
height = round(height, -1)
return width, height
def calculate_canvas_limits(width, height, framed=False):
if framed:
return calculate_canvas_frame_limits(width, height)
if width > height:
width = min(width, CANVAS.get('max_long_side'))
width = max(width, CANVAS.get('min_long_side'))
height = min(height, CANVAS.get('max_short_side'))
height = max(height, CANVAS.get('min_short_side'))
else:
height = min(height, CANVAS.get('max_long_side'))
height = max(height, CANVAS.get('min_long_side'))
width = min(width, CANVAS.get('max_short_side'))
width = max(width, CANVAS.get('min_short_side'))
return width, height
+81 -1
View File
@@ -1,7 +1,9 @@
# coding=UTF-8
""" Price formulas """
from api.lib.utils import round_half_up
from api.lib.limits import calculate_canvas_limits
def wallpaper_price(width, height, material_price, market, designer=None, reseller=None, inc_vat=True):
m2_price = material_price
@@ -96,3 +98,81 @@ def canvas_price(width, height, market, framed=True, designer=None, reseller=Non
price *= market.vat
return round(price)
def canvas_frame_price(width, height, stock_price, additional_price=None):
width, height = calculate_canvas_limits(width, height, framed=True)
circumference = (2 * width) + (2 * height)
circumference = max(circumference, 170)
price = stock_price * circumference
# if width or height goes above this limit additional costs are added
if additional_price:
additional_price_limit = 120
if width > additional_price_limit or height > additional_price_limit:
price += additional_price
return price
def old_canvas_diy_frame_price(width, height, market, reseller=None, inc_vat=True):
# todo: when product models are in place we should use stockproduct ""Do it yourself frame""
# instead of hardcoded values
stock_price = 1.61084
additional_price = 94.750000
price = canvas_frame_price(width, height, stock_price, additional_price)
# exchange to local currency
price *= market.exchange_rate
# multiply price with market price adjustments
price *= market.price_adjustments
# add reseller price premium
if reseller:
price *= (float(reseller.pricepremium) / 100) + 1
# add vat
if inc_vat:
price *= market.vat
return round_half_up(price)
def old_canvas_price(width, height, material_price, market, framed=True, designer=None, reseller=None, inc_vat=True):
width, height = calculate_canvas_limits(width, height, framed)
# calculate square meter
sqm = (width / 100) * (height / 100)
sqm = max(sqm, 0.2) # sqm can never go below 0.2
# calculate price per square meter
price = sqm * material_price
if framed:
# todo: when product models are in place we should use stockproduct
# "Canvasframe" instead of hardcoded values
stock_price = 1.04230824
additional_price = 94.750000
price += canvas_frame_price(width, height,
stock_price, additional_price)
# add designer price premium
if designer:
price *= (float(designer.pricepremium) / 100) + 1
# add reseller price premium
if reseller:
price *= (float(reseller.pricepremium) / 100) + 1
# exchange to local currency
price *= market.exchange_rate
# multiply price with market price adjustments
price *= market.price_adjustments
# add vat
if inc_vat:
price *= market.vat
return round_half_up(price)
+5
View File
@@ -0,0 +1,5 @@
from decimal import Decimal, ROUND_HALF_UP
def round_half_up(val):
return int(Decimal(val).quantize(0, ROUND_HALF_UP))
+15
View File
@@ -1,9 +1,24 @@
# coding=UTF-8
from flask_sqlalchemy import BaseQuery
from api import db
class ContractCustomerQuery(BaseQuery):
def reseller_store(self, url, territory):
return self.filter(
ContractCustomer.dealerstore == True,
ContractCustomer.dealerurl == url,
ContractCustomer.country == territory).first()
class ContractCustomer(db.Model):
__tablename__ = 'contract_customers'
query_class = ContractCustomerQuery
id = db.Column('contractcustomerid', db.Integer, primary_key=True)
country = db.Column(db.String(2))
pricepremium = db.Column(db.Numeric, nullable=False, default=0)
+1 -1
View File
@@ -78,7 +78,7 @@ _markets = [
Market('United kingdom', 'en_GB', 'GBP', 1.20, 'GBR'),
Market('Denmark', 'da_DK', 'DKK', 1.25, 'DNK'),
Market('Finland', 'fi_FI', 'EUR', 1.24, 'FIN'),
Market('Russia', 'ru_RU', 'RUB', 1, 'RUS'),
Market('Russia', 'ru_RU', 'EUR', 1, 'RUS'),
Market('Germany', 'de_DE', 'EUR', 1.19, 'DEU'),
Market('Netherlands', 'nl_NL', 'EUR', 1.21, 'NLD'),
Market('Austria', 'de_AT', 'EUR', 1.20, 'AUT'),
+46 -29
View File
@@ -2,33 +2,33 @@ from flask import Blueprint, request, jsonify, abort
from api.models.product import Material
from api.models.contract_customer import ContractCustomer
from api.models import market as market_model
from api.lib.prices import wallpaper_price, canvas_price
from api.lib.prices import wallpaper_price, old_canvas_price, old_canvas_diy_frame_price
from api.lib.limits import calculate_canvas_limits
from api.validators import validate_territory, validate_number, validate_keys_exists
mod = Blueprint('prices', __name__, url_prefix='/prices')
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
@mod.route("/wallpaper", methods=['GET'])
def wallpaper():
validate_keys_exists(request.args, 'width', 'height', 'territory')
validate_number(request.args.get('width'), request.args.get('height'))
validate_territory(request.args.get('territory'))
width = request.args.get('width', type=int)
height = request.args.get('height', type=int)
market = market_model.from_territory(request.args.get('territory'))
params = {
'width': request.args.get('width', type=int),
'height': request.args.get('height', type=int),
'market': market,
}
dealerurl = request.args.get('dealerurl')
if dealerurl:
reseller = ContractCustomer.query.filter_by(
dealerstore=True, dealerurl=dealerurl, country=market.territory).first()
if reseller:
params['reseller'] = reseller
reseller = get_reseller()
materials = Material.query.wallpapers().all()
data = []
@@ -36,7 +36,7 @@ def wallpaper():
data.append({
'material': material.name,
'material_id': material.id,
'price': wallpaper_price(material_price=material.price, **params),
'price': wallpaper_price(width, height, material.price, market, reseller=reseller),
})
return jsonify(data=data)
@@ -47,28 +47,45 @@ def canvas():
validate_keys_exists(request.args, 'width', 'height', 'territory')
validate_number(request.args.get('width'), request.args.get('height'))
validate_territory(request.args.get('territory'))
market = market_model.from_territory(request.args.get('territory'))
params = {
'width': request.args.get('width', type=int),
'height': request.args.get('height', type=int),
'market': market
}
dealerurl = request.args.get('dealerurl')
if dealerurl:
reseller = ContractCustomer.query.filter_by(
dealerstore=True, dealerurl=dealerurl, country=market.territory).first()
if reseller:
params['reseller'] = reseller
reseller = get_reseller()
material = Material.query.canvas().first()
data = []
for framed in [True, False]:
width, height = calculate_canvas_limits(
request.args.get('width', type=int),
request.args.get('height', type=int),
framed
)
data.append({
'framed': framed,
'price': canvas_price(framed=framed, **params)
'width': width,
'height': height,
'price': old_canvas_price(width, height, material.price, market, reseller=reseller, framed=framed)
})
return jsonify(data=data)
@mod.route('/diy-frame', methods=['GET'])
def diy_frame():
validate_keys_exists(request.args, 'width', 'height', 'territory')
validate_number(request.args.get('width'), request.args.get('height'))
validate_territory(request.args.get('territory'))
market = market_model.from_territory(request.args.get('territory'))
reseller = get_reseller()
width, height = calculate_canvas_limits(
request.args.get('width', type=int),
request.args.get('height', type=int),
framed=True
)
data = [{
'width': width,
'height': height,
'price': old_canvas_diy_frame_price(width, height, market, reseller=reseller)
}]
return jsonify(data=data)