refactor material prices

This commit is contained in:
Martin
2016-11-01 14:39:59 +01:00
parent bf9fc3ae5a
commit 0663bedc80
7 changed files with 79 additions and 51 deletions
+19 -8
View File
@@ -5,22 +5,33 @@ from api.lib.utils import round_half_up
from api.lib.limits import calculate_canvas_limits from api.lib.limits import calculate_canvas_limits
def wallpaper_price(width, height, material_price, market, designer=None, reseller=None, inc_vat=True): def material_price(material, market, designer=None, reseller=None, inc_vat=True):
m2_price = material_price price = material.price
# add designer price premium # add designer price premium
if designer: if designer:
m2_price *= (float(designer.pricepremium) / 100) + 1 price *= (float(designer.pricepremium) / 100) + 1
# add reseller price premium # add reseller price premium
if reseller: if reseller:
m2_price *= (float(reseller.pricepremium) / 100) + 1 price *= (float(reseller.pricepremium) / 100) + 1
# exchange to local currency # exchange to local currency
m2_price *= market.exchange_rate price *= market.exchange_rate
# multiply m2_price with market price adjustments # multiply m2_price with market price adjustments
m2_price *= market.price_adjustments price *= market.price_adjustments
# add vat
if inc_vat:
price *= market.vat
return round_half_up(price)
def wallpaper_price(width, height, material, market, designer=None, reseller=None, inc_vat=True):
m2_price = material_price(
material, market, designer, reseller, inc_vat=False)
# calculate price per square meter # calculate price per square meter
sqm = (width / 100) * (height / 100) sqm = (width / 100) * (height / 100)
@@ -140,14 +151,14 @@ def old_canvas_diy_frame_price(width, height, market, reseller=None, inc_vat=Tru
return round_half_up(price) return round_half_up(price)
def old_canvas_price(width, height, material_price, market, framed=True, designer=None, reseller=None, inc_vat=True): def old_canvas_price(width, height, material, market, framed=True, designer=None, reseller=None, inc_vat=True):
width, height = calculate_canvas_limits(width, height, framed) width, height = calculate_canvas_limits(width, height, framed)
# calculate square meter # calculate square meter
sqm = (width / 100) * (height / 100) sqm = (width / 100) * (height / 100)
sqm = max(sqm, 0.2) # sqm can never go below 0.2 sqm = max(sqm, 0.2) # sqm can never go below 0.2
# calculate price per square meter # calculate price per square meter
price = sqm * material_price price = sqm * material.price
if framed: if framed:
# todo: when product models are in place we should use stockproduct # todo: when product models are in place we should use stockproduct
+1
View File
@@ -2,3 +2,4 @@ from .designer import Designer
from .category import Category from .category import Category
from .contract_customer import ContractCustomer from .contract_customer import ContractCustomer
from .inquiry import Inquiry from .inquiry import Inquiry
from .product import Material
+4 -3
View File
@@ -2,7 +2,7 @@ from flask import Blueprint, request, jsonify, abort
from api.models.product import Material from api.models.product import Material
from api.models.contract_customer import ContractCustomer from api.models.contract_customer import ContractCustomer
from api.models import market as market_model from api.models import market as market_model
from api.lib.prices import wallpaper_price, old_canvas_price, old_canvas_diy_frame_price from api.lib.prices import wallpaper_price, old_canvas_price, old_canvas_diy_frame_price, material_price
from api.lib.limits import calculate_canvas_limits from api.lib.limits import calculate_canvas_limits
from api.validators import validate_territory, validate_number, validate_keys_exists from api.validators import validate_territory, validate_number, validate_keys_exists
@@ -36,7 +36,8 @@ def wallpaper():
data.append({ data.append({
'material': material.name, 'material': material.name,
'material_id': material.id, 'material_id': material.id,
'price': wallpaper_price(width, height, material.price, market, reseller=reseller), 'material_price': material_price(material, market, reseller=reseller, inc_vat=True),
'price': wallpaper_price(width, height, material, market, reseller=reseller),
}) })
return jsonify(data=data) return jsonify(data=data)
@@ -63,7 +64,7 @@ def canvas():
'framed': framed, 'framed': framed,
'width': width, 'width': width,
'height': height, 'height': height,
'price': old_canvas_price(width, height, material.price, market, reseller=reseller, framed=framed) 'price': old_canvas_price(width, height, material, market, reseller=reseller, framed=framed)
}) })
return jsonify(data=data) return jsonify(data=data)
+5 -8
View File
@@ -16,7 +16,7 @@ Calculate price inc VAT of a wallpaper product. The response will contain prices
Sample request Sample request
`curl -X GET 'https://api2.photowall.com/prices/wallpaper?width=100&height=50&territory=SE&dealerurl=ackes'` `curl -X GET 'https://api2.photowall.com/prices/wallpaper?width=200&height=200&territory=SE&dealerurl=ackes'`
Sample response Sample response
``` ```
@@ -25,17 +25,14 @@ Sample response
{ {
"material": "standard-wallpaper", "material": "standard-wallpaper",
"material_id": 1, "material_id": 1,
"price": 295 "material_price": 295,
}, "price": 1180
{
"material": "self-adhesive-wallpaper",
"material_id": 2,
"price": 445
}, },
{ {
"material": "premium-wallpaper", "material": "premium-wallpaper",
"material_id": 4, "material_id": 4,
"price": 325 "material_price": 325,
"price": 1300
} }
] ]
} }
+2 -1
View File
@@ -19,4 +19,5 @@ class TestCalculate(unittest2.TestCase):
self.assertEqual((40, 40), calculate_canvas_frame_limits(1, 1)) self.assertEqual((40, 40), calculate_canvas_frame_limits(1, 1))
self.assertEqual((150, 150), calculate_canvas_frame_limits(200, 200)) self.assertEqual((150, 150), calculate_canvas_frame_limits(200, 200))
self.assertEqual((150, 40), calculate_canvas_frame_limits(200, 10)) self.assertEqual((150, 40), calculate_canvas_frame_limits(200, 10))
self.assertEqual((110, 120), calculate_canvas_frame_limits(112, 117), "round to nearest 10") self.assertEqual((110, 120), calculate_canvas_frame_limits(
112, 117), "round to nearest 10")
+38 -31
View File
@@ -4,6 +4,7 @@ import unittest2
from api.lib.prices import * from api.lib.prices import *
from tests.factories import DesignerFactory, ContractCustomerFactory, InquiryFactory from tests.factories import DesignerFactory, ContractCustomerFactory, InquiryFactory
from api.models import market from api.models import market
from api.models.product import Material, MaterialPrice
sweden = market.from_territory('SE') sweden = market.from_territory('SE')
@@ -22,40 +23,49 @@ designer = DesignerFactory()
designer.pricepremium = 17.647 designer.pricepremium = 17.647
reseller = ContractCustomerFactory(pricepremium=15) reseller = ContractCustomerFactory(pricepremium=15)
standard_wallpaper = Material(name='standard-wallpaper')
standard_wallpaper.material_price = MaterialPrice(price=236)
premium_wallpaper = Material(name='premium-wallpaper')
premium_wallpaper.material_price = MaterialPrice(price=260)
standard_canvas = Material(name='standard-canvas')
standard_canvas.material_price = MaterialPrice(price=799.2)
class TestPrice(unittest2.TestCase): class TestPrice(unittest2.TestCase):
def test_wallpaper_price(self): def test_wallpaper_price(self):
self.assertEqual(295, wallpaper_price( self.assertEqual(295, wallpaper_price(
width=1, height=1, material_price=236, market=sweden)) width=1, height=1, material=standard_wallpaper, market=sweden))
self.assertEqual(295, wallpaper_price( self.assertEqual(295, wallpaper_price(
width=100, height=100, material_price=236, market=sweden)) width=100, height=100, material=standard_wallpaper, market=sweden))
self.assertEqual(1180, wallpaper_price( self.assertEqual(1180, wallpaper_price(
width=200, height=200, material_price=236, market=sweden)) width=200, height=200, material=standard_wallpaper, market=sweden))
self.assertEqual(325, wallpaper_price( self.assertEqual(325, wallpaper_price(
width=100, height=100, material_price=260, market=sweden)) width=100, height=100, material=premium_wallpaper, market=sweden))
self.assertEqual(279, wallpaper_price( self.assertEqual(279, wallpaper_price(
width=100, height=100, material_price=236, market=denmark)) width=100, height=100, material=standard_wallpaper, market=denmark))
self.assertEqual(236, wallpaper_price( self.assertEqual(236, wallpaper_price(
width=100, height=100, material_price=236, market=sweden, inc_vat=False)) width=100, height=100, material=standard_wallpaper, market=sweden, inc_vat=False))
# designer # # designer
self.assertEqual(347, wallpaper_price( self.assertEqual(348, wallpaper_price(width=100, height=100,
width=100, height=100, material_price=236, market=sweden, designer=designer)) material=standard_wallpaper, market=sweden, designer=designer))
self.assertEqual(329, wallpaper_price( self.assertEqual(329, wallpaper_price(width=100, height=100,
width=100, height=100, material_price=236, market=denmark, designer=designer)) material=standard_wallpaper, market=denmark, designer=designer))
# reseller stores # # reseller stores
self.assertEqual(339, wallpaper_price( self.assertEqual(339, wallpaper_price(width=100, height=100,
width=100, height=100, material_price=236, market=sweden, reseller=reseller)) material=standard_wallpaper, market=sweden, reseller=reseller))
self.assertEqual(399, wallpaper_price(width=100, height=100, self.assertEqual(399, wallpaper_price(width=100, height=100,
material_price=236, market=sweden, designer=designer, reseller=reseller)) material=standard_wallpaper, market=sweden, designer=designer, reseller=reseller))
self.assertEqual(154, wallpaper_price(width=100, height=100, material_price=236, self.assertEqual(154, wallpaper_price(width=100, height=100, material=standard_wallpaper,
market=sweden, reseller=ContractCustomerFactory(pricepremium=-47.74))) market=sweden, reseller=ContractCustomerFactory(pricepremium=-47.74)))
def test_wallpaper_price_us(self): def test_wallpaper_price_us(self):
self.assertEqual(59, wallpaper_price(width=100, height=100, self.assertEqual(60, wallpaper_price(width=100, height=100,
material_price=236, market=us), "Minium price for US is 2 sqm") material=standard_wallpaper, market=us), "Minium price for US is 2 sqm")
def test_canvas_cm2_price(self): def test_canvas_cm2_price(self):
self.assertEqual(0.22, canvas_cm2_price(250)) self.assertEqual(0.22, canvas_cm2_price(250))
@@ -101,25 +111,22 @@ class TestPrice(unittest2.TestCase):
def test_old_canvas_price(self): def test_old_canvas_price(self):
self.assertEqual(250, old_canvas_price( self.assertEqual(250, old_canvas_price(
width=50, height=50, material_price=799.2, market=sweden, framed=False)) width=50, height=50, material=standard_canvas, market=sweden, framed=False))
self.assertEqual(200, old_canvas_price( self.assertEqual(200, old_canvas_price(
width=1, height=1, material_price=799.2, market=sweden, framed=False)) width=1, height=1, material=standard_canvas, market=sweden, framed=False))
self.assertEqual(7493, old_canvas_price( self.assertEqual(7493, old_canvas_price(
width=500, height=500, material_price=799.2, market=sweden, framed=False)) width=500, height=500, material=standard_canvas, market=sweden, framed=False))
self.assertEqual(5994, old_canvas_price(width=500, height=500, self.assertEqual(5994, old_canvas_price(width=500, height=500,
material_price=799.2, market=sweden, framed=False, inc_vat=False)) material=standard_canvas, market=sweden, framed=False, inc_vat=False))
self.assertEqual(7493, old_canvas_price( self.assertEqual(7493, old_canvas_price(
width=500, height=500, material_price=799.2, market=sweden, framed=False)) width=500, height=500, material=standard_canvas, market=sweden, framed=False))
self.assertEqual(236, old_canvas_price( self.assertEqual(236, old_canvas_price(
width=50, height=50, material_price=799.2, market=denmark, framed=False)) width=50, height=50, material=standard_canvas, market=denmark, framed=False))
self.assertEqual(294, old_canvas_price( self.assertEqual(294, old_canvas_price(
width=50, height=50, material_price=799.2, market=sweden, designer=designer, framed=False)) width=50, height=50, material=standard_canvas, market=sweden, designer=designer, framed=False))
self.assertEqual(510, old_canvas_price( self.assertEqual(510, old_canvas_price(
width=50, height=50, material_price=799.2, market=sweden, framed=True)) width=50, height=50, material=standard_canvas, market=sweden, framed=True))
self.assertEqual(2972, old_canvas_price( self.assertEqual(2972, old_canvas_price(
width=150, height=140, material_price=799.2, market=sweden, framed=True)) width=150, height=140, material=standard_canvas, market=sweden, framed=True))
self.assertEqual(287, old_canvas_price( self.assertEqual(287, old_canvas_price(
width=50, height=50, material_price=799.2, market=sweden, framed=False, reseller=reseller)) width=50, height=50, material=standard_canvas, market=sweden, framed=False, reseller=reseller))
+10
View File
@@ -23,12 +23,22 @@ class TestEndpoints(flask_testing.TestCase):
db.session.commit() db.session.commit()
return material return material
def test_wallpaper(self):
self._add_material('standard-wallpaper', 1, 236)
self._add_material('premium-wallpaper', 1, 260)
response = self.client.get(
'/prices/wallpaper?width=200&height=10&territory=SE')
self.assert200(response)
products = response.json['data']
self.assertEqual(2, len(products))
def test_canvas(self): def test_canvas(self):
self._add_material('standard-canvas', 2, 799.9) self._add_material('standard-canvas', 2, 799.9)
response = self.client.get( response = self.client.get(
'/prices/canvas?width=200&height=10&territory=SE') '/prices/canvas?width=200&height=10&territory=SE')
self.assert200(response) self.assert200(response)
products = response.json['data'] products = response.json['data']
self.assertEqual(2, len(products))
for product in products: for product in products:
if product['framed']: if product['framed']:
self.assertEqual(150, product['width']) self.assertEqual(150, product['width'])