From 0663bedc8036e07a948442d96ad970ad53496bc2 Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 1 Nov 2016 14:39:59 +0100 Subject: [PATCH] refactor material prices --- api/lib/prices.py | 27 +++++++++---- api/models/__init__.py | 1 + api/resources/prices.py | 7 ++-- docs/prices.md | 13 +++---- tests/lib/test_limits.py | 3 +- tests/lib/test_prices.py | 69 +++++++++++++++++++--------------- tests/resources/test_prices.py | 10 +++++ 7 files changed, 79 insertions(+), 51 deletions(-) diff --git a/api/lib/prices.py b/api/lib/prices.py index 9b1f3d4..fe6dfbf 100644 --- a/api/lib/prices.py +++ b/api/lib/prices.py @@ -5,22 +5,33 @@ 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 +def material_price(material, market, designer=None, reseller=None, inc_vat=True): + price = material.price # add designer price premium if designer: - m2_price *= (float(designer.pricepremium) / 100) + 1 + price *= (float(designer.pricepremium) / 100) + 1 # add reseller price premium if reseller: - m2_price *= (float(reseller.pricepremium) / 100) + 1 + price *= (float(reseller.pricepremium) / 100) + 1 # exchange to local currency - m2_price *= market.exchange_rate + price *= market.exchange_rate # 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 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) -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) # 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 + price = sqm * material.price if framed: # todo: when product models are in place we should use stockproduct diff --git a/api/models/__init__.py b/api/models/__init__.py index 0552a64..775abaa 100644 --- a/api/models/__init__.py +++ b/api/models/__init__.py @@ -2,3 +2,4 @@ from .designer import Designer from .category import Category from .contract_customer import ContractCustomer from .inquiry import Inquiry +from .product import Material diff --git a/api/resources/prices.py b/api/resources/prices.py index f6d42b9..33a77f8 100644 --- a/api/resources/prices.py +++ b/api/resources/prices.py @@ -2,7 +2,7 @@ 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, 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.validators import validate_territory, validate_number, validate_keys_exists @@ -36,7 +36,8 @@ def wallpaper(): data.append({ 'material': material.name, '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) @@ -63,7 +64,7 @@ def canvas(): 'framed': framed, 'width': width, '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) diff --git a/docs/prices.md b/docs/prices.md index 6a56d6b..eb9c564 100644 --- a/docs/prices.md +++ b/docs/prices.md @@ -16,7 +16,7 @@ Calculate price inc VAT of a wallpaper product. The response will contain prices 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 ``` @@ -25,17 +25,14 @@ Sample response { "material": "standard-wallpaper", "material_id": 1, - "price": 295 - }, - { - "material": "self-adhesive-wallpaper", - "material_id": 2, - "price": 445 + "material_price": 295, + "price": 1180 }, { "material": "premium-wallpaper", "material_id": 4, - "price": 325 + "material_price": 325, + "price": 1300 } ] } diff --git a/tests/lib/test_limits.py b/tests/lib/test_limits.py index f3aac75..485822e 100644 --- a/tests/lib/test_limits.py +++ b/tests/lib/test_limits.py @@ -19,4 +19,5 @@ class TestCalculate(unittest2.TestCase): self.assertEqual((40, 40), calculate_canvas_frame_limits(1, 1)) self.assertEqual((150, 150), calculate_canvas_frame_limits(200, 200)) 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") diff --git a/tests/lib/test_prices.py b/tests/lib/test_prices.py index b86d8fd..c0b6cce 100644 --- a/tests/lib/test_prices.py +++ b/tests/lib/test_prices.py @@ -4,6 +4,7 @@ import unittest2 from api.lib.prices import * from tests.factories import DesignerFactory, ContractCustomerFactory, InquiryFactory from api.models import market +from api.models.product import Material, MaterialPrice sweden = market.from_territory('SE') @@ -22,40 +23,49 @@ designer = DesignerFactory() designer.pricepremium = 17.647 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): def test_wallpaper_price(self): 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( - width=100, height=100, material_price=236, market=sweden)) + width=100, height=100, material=standard_wallpaper, market=sweden)) 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( - width=100, height=100, material_price=260, market=sweden)) + width=100, height=100, material=premium_wallpaper, market=sweden)) 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( - 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 - self.assertEqual(347, wallpaper_price( - width=100, height=100, material_price=236, market=sweden, designer=designer)) - self.assertEqual(329, wallpaper_price( - width=100, height=100, material_price=236, market=denmark, designer=designer)) + # # designer + self.assertEqual(348, wallpaper_price(width=100, height=100, + material=standard_wallpaper, market=sweden, designer=designer)) + self.assertEqual(329, wallpaper_price(width=100, height=100, + material=standard_wallpaper, market=denmark, designer=designer)) - # reseller stores - self.assertEqual(339, wallpaper_price( - width=100, height=100, material_price=236, market=sweden, reseller=reseller)) + # # reseller stores + self.assertEqual(339, wallpaper_price(width=100, height=100, + material=standard_wallpaper, market=sweden, reseller=reseller)) self.assertEqual(399, wallpaper_price(width=100, height=100, - material_price=236, market=sweden, designer=designer, reseller=reseller)) - self.assertEqual(154, wallpaper_price(width=100, height=100, material_price=236, + material=standard_wallpaper, market=sweden, designer=designer, reseller=reseller)) + self.assertEqual(154, wallpaper_price(width=100, height=100, material=standard_wallpaper, market=sweden, reseller=ContractCustomerFactory(pricepremium=-47.74))) def test_wallpaper_price_us(self): - self.assertEqual(59, wallpaper_price(width=100, height=100, - material_price=236, market=us), "Minium price for US is 2 sqm") + self.assertEqual(60, wallpaper_price(width=100, height=100, + material=standard_wallpaper, market=us), "Minium price for US is 2 sqm") def test_canvas_cm2_price(self): self.assertEqual(0.22, canvas_cm2_price(250)) @@ -101,25 +111,22 @@ class TestPrice(unittest2.TestCase): def test_old_canvas_price(self): 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( - 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( - 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, - 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( - 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( - 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( - 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( - 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( - 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( - 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)) diff --git a/tests/resources/test_prices.py b/tests/resources/test_prices.py index 75dfaa4..659e392 100644 --- a/tests/resources/test_prices.py +++ b/tests/resources/test_prices.py @@ -23,12 +23,22 @@ class TestEndpoints(flask_testing.TestCase): db.session.commit() 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): self._add_material('standard-canvas', 2, 799.9) response = self.client.get( '/prices/canvas?width=200&height=10&territory=SE') self.assert200(response) products = response.json['data'] + self.assertEqual(2, len(products)) for product in products: if product['framed']: self.assertEqual(150, product['width'])