PW-570 inquiry price calculations

This commit is contained in:
Martin
2016-11-19 16:38:37 +01:00
parent 980aa8f88b
commit 4679dd2bbe
9 changed files with 371 additions and 15 deletions
+43 -5
View File
@@ -5,7 +5,7 @@ from api.lib.utils import round_half_up
from api.lib.limits import calculate_canvas_limits
def m2_price(material, market, designer=None, reseller=None, rounded=True, inc_vat=True):
def m2_price(material, market, designer=None, reseller=None, inquiry=None, rounded=True, inc_vat=True):
""" calculates price per square meter """
price = material.price
@@ -15,6 +15,9 @@ def m2_price(material, market, designer=None, reseller=None, rounded=True, inc_v
if reseller:
price *= (float(reseller.pricepremium) / 100) + 1
if inquiry:
price *= (float(inquiry.pricepremium) / 100) + 1
price *= market.exchange_rate
price *= market.price_adjustments
@@ -27,9 +30,9 @@ def m2_price(material, market, designer=None, reseller=None, rounded=True, inc_v
return price
def wallpaper_price(width, height, material, market, designer=None, reseller=None, rounded=True, inc_vat=True):
def wallpaper_price(width, height, material, market, designer=None, reseller=None, inquiry=None, rounded=True, inc_vat=True):
price = m2_price(material, market, designer, reseller,
rounded=rounded, inc_vat=False)
inquiry, rounded=rounded, inc_vat=False)
sqm = (width / 100) * (height / 100)
@@ -150,10 +153,10 @@ def old_canvas_diy_frame_price(width, height, market, reseller=None, rounded=Tru
return canvas_frame_price(width, height, stock_price, market, additional_price, reseller=reseller, rounded=rounded, inc_vat=inc_vat)
def old_canvas_price(width, height, material, market, framed=True, designer=None, reseller=None, inc_vat=True):
def old_canvas_price(width, height, material, market, framed=True, designer=None, reseller=None, inquiry=None, inc_vat=True):
width, height = calculate_canvas_limits(width, height, framed)
price = m2_price(material, market, designer, reseller,
rounded=False, inc_vat=False)
inquiry, rounded=False, inc_vat=False)
# calculate square meter
sqm = (width / 100) * (height / 100)
@@ -173,3 +176,38 @@ def old_canvas_price(width, height, material, market, framed=True, designer=None
price *= market.vat
return round_half_up(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)
framed = kwargs.get('framed', inquiry.framed)
if inquiry.product_group == 'photo-wallpaper':
price = wallpaper_price(width, height, material, inquiry.market,
designer=inquiry.designer, reseller=inquiry.dealer_store, inquiry=inquiry, rounded=False, inc_vat=False)
elif inquiry.product_group == 'canvas':
price = old_canvas_price(width, height, material, inquiry.market,
designer=inquiry.designer, reseller=inquiry.dealer_store, inquiry=inquiry, framed=framed, 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
+1 -1
View File
@@ -4,4 +4,4 @@ from .designer import Designer
from .category import Category
from .contract_customer import ContractCustomer
from .inquiry import Inquiry
from .product import Material
from .product import Material, Product
+44 -1
View File
@@ -1,14 +1,57 @@
# coding=UTF-8
from api.extensions import db
from api.models import market as market_model
from api.lib.utils import round_half_up
class Inquiry(db.Model):
__tablename__ = 'inquiries'
inquiryid = db.Column(db.Integer, primary_key=True)
id = db.Column('inquiryid', db.Integer, primary_key=True)
territory = db.Column(db.String(2))
product_group = db.Column(db.String(100))
price_m2 = db.Column(db.Numeric)
price_effect = db.Column(db.Numeric)
price_retouch = db.Column(db.Numeric)
width = db.Column(db.Integer)
height = db.Column(db.Integer)
framed = db.Column(db.Integer, nullable=False, default=0)
dealer_store_id = db.Column('dealer_store', db.Integer, db.ForeignKey(
'contract_customers.contractcustomerid'))
dealer_store = db.relationship('ContractCustomer', uselist=False)
framed = db.Column(db.Integer, default=0, nullable=False)
materialid = db.Column(db.Integer, db.ForeignKey(
'product-materials.materialid'), default=1, nullable=False)
material = db.relationship('Material', uselist=False)
pricepremium = db.Column(db.Numeric, nullable=False, default=0)
productId = db.Column(db.Integer, db.ForeignKey(
'product-products.productid'))
product = db.relationship('Product', uselist=False)
automated = db.Column(db.Integer, nullable=False, default=0)
@property
def market(self):
market = market_model.from_territory(self.territory)
if not market:
raise ValueError('Invalid territory: {}'.format(self.territory))
return market
def local_price_retouch(self, rounded=True, inc_vat=False):
""" returns the retouch price in the inquiry local currency """
if not self.price_retouch:
return 0
price = float(self.price_retouch)
price *= self.market.exchange_rate
price *= self.market.price_adjustments
if inc_vat:
price *= self.market.vat
if rounded:
price = round_half_up(price)
return price
@property
def designer(self):
if self.product and self.product.designer:
return self.product.designer
return None
+9
View File
@@ -4,6 +4,15 @@ from api.extensions import db
from flask_sqlalchemy import BaseQuery
class Product(db.Model):
__tablename__ = 'product-products'
id = db.Column('productid', db.Integer, primary_key=True)
designerid = db.Column(db.Integer, db.ForeignKey('designers.designerid'))
designer = db.relationship('Designer', backref='products')
class Currencies(db.Model):
__tablename__ = 'product-currencies'
+88 -3
View File
@@ -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)