refactor canvas material price calculations

This commit is contained in:
Martin
2016-11-08 13:04:17 +01:00
parent fa1528f138
commit cf5fe9fc69
2 changed files with 32 additions and 17 deletions
+8 -17
View File
@@ -5,7 +5,7 @@ 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 material_price(material, market, designer=None, reseller=None, inc_vat=True): def material_price(material, market, designer=None, reseller=None, rounded=True, inc_vat=True):
price = material.price price = material.price
# add designer price premium # add designer price premium
@@ -26,7 +26,10 @@ def material_price(material, market, designer=None, reseller=None, inc_vat=True)
if inc_vat: if inc_vat:
price *= market.vat price *= market.vat
return round_half_up(price) if rounded:
price = round_half_up(price)
return price
def wallpaper_price(width, height, material, market, designer=None, reseller=None, inc_vat=True): def wallpaper_price(width, height, material, market, designer=None, reseller=None, inc_vat=True):
@@ -153,12 +156,14 @@ def old_canvas_diy_frame_price(width, height, market, reseller=None, inc_vat=Tru
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, inc_vat=True):
width, height = calculate_canvas_limits(width, height, framed) width, height = calculate_canvas_limits(width, height, framed)
m2_price = material_price(
material, market, designer, reseller, rounded=False, inc_vat=False)
# 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 * m2_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
@@ -168,20 +173,6 @@ def old_canvas_price(width, height, material, market, framed=True, designer=None
price += canvas_frame_price(width, height, price += canvas_frame_price(width, height,
stock_price, additional_price) 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 # add vat
if inc_vat: if inc_vat:
price *= market.vat price *= market.vat
+24
View File
@@ -19,9 +19,17 @@ us = market.from_territory('US')
us.price_adjustments = 1.05 us.price_adjustments = 1.05
us.exchange_rate = 0.119800 us.exchange_rate = 0.119800
russia = market.from_territory('RU')
russia.price_adjustments = 1
russia.exchange_rate = 0.100000 # russia uses EUR
designer = DesignerFactory() designer = DesignerFactory()
designer.pricepremium = 17.647 designer.pricepremium = 17.647
reseller = ContractCustomerFactory(pricepremium=15) reseller = ContractCustomerFactory(pricepremium=15)
piterra = ContractCustomerFactory(pricepremium=36.029411)
designer_moomin = DesignerFactory()
designer_moomin.pricepremium = 2
standard_wallpaper = Material(name='standard-wallpaper') standard_wallpaper = Material(name='standard-wallpaper')
standard_wallpaper.material_price = MaterialPrice(price=236) standard_wallpaper.material_price = MaterialPrice(price=236)
@@ -35,6 +43,22 @@ standard_canvas.material_price = MaterialPrice(price=799.2)
class TestPrice(unittest2.TestCase): class TestPrice(unittest2.TestCase):
def test_material_price(self):
self.assertEqual(236, material_price(
material=standard_wallpaper, market=sweden, inc_vat=False))
self.assertEqual(260, material_price(
material=premium_wallpaper, market=sweden, inc_vat=False))
self.assertEqual(295, material_price(
material=standard_wallpaper, market=sweden, inc_vat=True))
self.assertEqual(223, material_price(
material=standard_wallpaper, market=denmark, inc_vat=False))
self.assertAlmostEqual(79.92, material_price(
material=standard_canvas, market=russia, rounded=False, inc_vat=False), places=2)
self.assertAlmostEqual(81.5184, material_price(
material=standard_canvas, market=russia, designer=designer_moomin, rounded=False, inc_vat=False), places=4)
self.assertAlmostEqual(110.88899937662403, material_price(material=standard_canvas, market=russia,
designer=designer_moomin, reseller=piterra, rounded=False, inc_vat=False), places=4)
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=standard_wallpaper, market=sweden)) width=1, height=1, material=standard_wallpaper, market=sweden))