PW-570 inquiry price calculations
This commit is contained in:
+88
-3
@@ -1,15 +1,14 @@
|
||||
from flask import Blueprint, request, jsonify, abort
|
||||
from api.helpers import check_api_key
|
||||
from api.validators import validate_territory, validate_number
|
||||
from api.models.inquiry import Inquiry
|
||||
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, old_canvas_price, old_canvas_diy_frame_price, m2_price
|
||||
from api.lib.prices import wallpaper_price, old_canvas_price, old_canvas_diy_frame_price, m2_price, inquiry_price
|
||||
from api.lib.limits import calculate_canvas_limits
|
||||
|
||||
|
||||
mod = Blueprint('prices', __name__, url_prefix='/prices')
|
||||
|
||||
mod.before_request(check_api_key)
|
||||
|
||||
|
||||
@@ -90,3 +89,89 @@ def diy_frame():
|
||||
}]
|
||||
|
||||
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
|
||||
width = request.args.get('width', type=int, default=inquiry.width)
|
||||
height = request.args.get('height', type=int, default=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
|
||||
|
||||
|
||||
def inquiry_wallpaper(inquiry):
|
||||
width, height = get_inquiry_measurements(inquiry)
|
||||
|
||||
data = {
|
||||
'id': inquiry.id,
|
||||
'group': 'wallpaper',
|
||||
'width': width,
|
||||
'height': height,
|
||||
'retouch_price': inquiry.local_price_retouch(rounded=False, inc_vat=True),
|
||||
}
|
||||
materials = Material.query.wallpapers().all()
|
||||
prices = []
|
||||
for material in materials:
|
||||
prices.append({
|
||||
'material': material.name,
|
||||
'material_id': material.id,
|
||||
'm2_price': m2_price(material, inquiry.market, designer=inquiry.designer, reseller=inquiry.dealer_store, inquiry=inquiry, rounded=False, inc_vat=True),
|
||||
'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)
|
||||
})
|
||||
data['prices'] = prices
|
||||
return data
|
||||
|
||||
|
||||
def inquiry_canvas(inquiry):
|
||||
in_width, in_height = get_inquiry_measurements(inquiry)
|
||||
|
||||
data = {
|
||||
'id': inquiry.id,
|
||||
'group': 'canvas',
|
||||
'retouch_price': inquiry.local_price_retouch(rounded=False, inc_vat=True)
|
||||
}
|
||||
material = Material.query.canvas().first()
|
||||
|
||||
prices = []
|
||||
|
||||
for framed in [True, False]:
|
||||
width, height = calculate_canvas_limits(in_width, in_height, framed)
|
||||
prices.append({
|
||||
'framed': framed,
|
||||
'width': width,
|
||||
'height': height,
|
||||
'm2_price': m2_price(material, inquiry.market, designer=inquiry.designer, reseller=inquiry.dealer_store, inquiry=inquiry, rounded=False, inc_vat=True),
|
||||
'inquiry_price_excl_retouch': inquiry_price(inquiry, material=material, width=width, height=height, framed=framed, rounded=False, inc_price_retouch=False, inc_vat=True),
|
||||
'inquiry_price': inquiry_price(inquiry, material=material, width=width, height=height, framed=framed, rounded=False, inc_vat=True),
|
||||
'inquiry_price_excl_vat': inquiry_price(inquiry, material=material, width=width, height=height, framed=framed, rounded=False, inc_vat=False)
|
||||
})
|
||||
data['prices'] = prices
|
||||
|
||||
return data
|
||||
|
||||
|
||||
@mod.route('/inquiry/<int:inquiry_id>', methods=["GET"])
|
||||
def inquiry(inquiry_id):
|
||||
inquiry = Inquiry.query.get_or_404(inquiry_id)
|
||||
|
||||
if inquiry.product_group == 'photo-wallpaper':
|
||||
data = inquiry_wallpaper(inquiry)
|
||||
elif inquiry.product_group == 'canvas':
|
||||
data = inquiry_canvas(inquiry)
|
||||
else:
|
||||
raise ValueError('Invalid group {}'.format(inquiry.product_group))
|
||||
|
||||
return jsonify(data)
|
||||
|
||||
Reference in New Issue
Block a user