add support for product price calculations
This commit is contained in:
+5
-2
@@ -153,7 +153,7 @@ 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)
|
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, inquiry=None, inc_vat=True):
|
def old_canvas_price(width, height, material, market, framed=True, designer=None, reseller=None, inquiry=None, rounded=True, inc_vat=True):
|
||||||
width, height = calculate_canvas_limits(width, height, framed)
|
width, height = calculate_canvas_limits(width, height, framed)
|
||||||
price = m2_price(material, market, designer, reseller,
|
price = m2_price(material, market, designer, reseller,
|
||||||
inquiry, rounded=False, inc_vat=False)
|
inquiry, rounded=False, inc_vat=False)
|
||||||
@@ -175,7 +175,10 @@ def old_canvas_price(width, height, material, market, framed=True, designer=None
|
|||||||
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 inquiry_price(inquiry, inc_price_retouch=True, rounded=True, inc_vat=True, **kwargs):
|
def inquiry_price(inquiry, inc_price_retouch=True, rounded=True, inc_vat=True, **kwargs):
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ class Designer(db.Model):
|
|||||||
visible_search = db.Column(db.Integer(), nullable=False, default=1)
|
visible_search = db.Column(db.Integer(), nullable=False, default=1)
|
||||||
pricepremium = db.Column(db.Numeric(), nullable=False, default=0)
|
pricepremium = db.Column(db.Numeric(), nullable=False, default=0)
|
||||||
|
|
||||||
def __init__(self, name, username, territory, currency):
|
def __init__(self, name, username, territory, currency, *args, **kwargs):
|
||||||
|
super(Designer, self).__init__(*args, **kwargs)
|
||||||
self.name = name
|
self.name = name
|
||||||
self.username = username
|
self.username = username
|
||||||
self.territory = territory
|
self.territory = territory
|
||||||
|
|||||||
+18
-6
@@ -2,7 +2,7 @@ from flask import Blueprint, request, jsonify, abort
|
|||||||
from api.helpers import check_api_key
|
from api.helpers import check_api_key
|
||||||
from api.validators import validate_territory, validate_number
|
from api.validators import validate_territory, validate_number
|
||||||
from api.models.inquiry import Inquiry
|
from api.models.inquiry import Inquiry
|
||||||
from api.models.product import Material
|
from api.models.product import Material, Product
|
||||||
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, m2_price, inquiry_price
|
from api.lib.prices import wallpaper_price, old_canvas_price, old_canvas_diy_frame_price, m2_price, inquiry_price
|
||||||
@@ -20,6 +20,15 @@ def get_reseller():
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def get_designer():
|
||||||
|
product_id = request.args.get('product')
|
||||||
|
if product_id:
|
||||||
|
product = Product.query.get(product_id)
|
||||||
|
if product:
|
||||||
|
return product.designer
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
@mod.route("/wallpaper", methods=['GET'])
|
@mod.route("/wallpaper", methods=['GET'])
|
||||||
@validate_number('width', 'height')
|
@validate_number('width', 'height')
|
||||||
@validate_territory('territory')
|
@validate_territory('territory')
|
||||||
@@ -27,7 +36,7 @@ def wallpaper():
|
|||||||
width = request.args.get('width', type=int)
|
width = request.args.get('width', type=int)
|
||||||
height = request.args.get('height', type=int)
|
height = request.args.get('height', type=int)
|
||||||
market = market_model.from_territory(request.args.get('territory'))
|
market = market_model.from_territory(request.args.get('territory'))
|
||||||
|
designer = get_designer()
|
||||||
reseller = get_reseller()
|
reseller = get_reseller()
|
||||||
|
|
||||||
materials = Material.query.wallpapers().all()
|
materials = Material.query.wallpapers().all()
|
||||||
@@ -36,8 +45,9 @@ def wallpaper():
|
|||||||
data.append({
|
data.append({
|
||||||
'material': material.name,
|
'material': material.name,
|
||||||
'material_id': material.id,
|
'material_id': material.id,
|
||||||
'm2_price': m2_price(material, market, reseller=reseller, inc_vat=True),
|
'm2_price': m2_price(material, market, reseller=reseller, designer=designer, rounded=False, inc_vat=True),
|
||||||
'price': wallpaper_price(width, height, material, market, reseller=reseller),
|
'price': wallpaper_price(width, height, material, market, reseller=reseller, designer=designer, rounded=False, inc_vat=True),
|
||||||
|
'price_excl_vat': wallpaper_price(width, height, material, market, reseller=reseller, designer=designer, rounded=False, inc_vat=False),
|
||||||
})
|
})
|
||||||
|
|
||||||
return jsonify(data=data)
|
return jsonify(data=data)
|
||||||
@@ -49,6 +59,7 @@ def wallpaper():
|
|||||||
def canvas():
|
def canvas():
|
||||||
market = market_model.from_territory(request.args.get('territory'))
|
market = market_model.from_territory(request.args.get('territory'))
|
||||||
reseller = get_reseller()
|
reseller = get_reseller()
|
||||||
|
designer = get_designer()
|
||||||
material = Material.query.canvas().first()
|
material = Material.query.canvas().first()
|
||||||
|
|
||||||
data = []
|
data = []
|
||||||
@@ -63,8 +74,9 @@ def canvas():
|
|||||||
'framed': framed,
|
'framed': framed,
|
||||||
'width': width,
|
'width': width,
|
||||||
'height': height,
|
'height': height,
|
||||||
'm2_price': m2_price(material, market, reseller=reseller, inc_vat=True),
|
'm2_price': m2_price(material, market, reseller=reseller, designer=designer, rounded=False, inc_vat=True),
|
||||||
'price': old_canvas_price(width, height, material, market, reseller=reseller, framed=framed)
|
'price': old_canvas_price(width, height, material, market, reseller=reseller, designer=designer, framed=framed, rounded=False, inc_vat=True),
|
||||||
|
'price_excl_vat': old_canvas_price(width, height, material, market, reseller=reseller, designer=designer, framed=framed, rounded=False, inc_vat=False)
|
||||||
})
|
})
|
||||||
|
|
||||||
return jsonify(data=data)
|
return jsonify(data=data)
|
||||||
|
|||||||
+17
-9
@@ -12,27 +12,30 @@ Calculate price inc VAT of a wallpaper product. The response will contain prices
|
|||||||
| `height` | Wallpaper height in cm e.g `50` |
|
| `height` | Wallpaper height in cm e.g `50` |
|
||||||
| `territory` | Two letter iso code e.g `SE`. Territory affect the price in different ways like VAT, currency exchanges etc. |
|
| `territory` | Two letter iso code e.g `SE`. Territory affect the price in different ways like VAT, currency exchanges etc. |
|
||||||
| `dealerurl` | Optional parameter e.g `ackes`. Should always be used when calling the api from a dealerstore. If a dealerstore exists with this url additional fees will be applied to the price. |
|
| `dealerurl` | Optional parameter e.g `ackes`. Should always be used when calling the api from a dealerstore. If a dealerstore exists with this url additional fees will be applied to the price. |
|
||||||
|
| `product` | Optional parameter e.g `43894`. If a product with this id exists the api will calculate the price for the given product. |
|
||||||
|
|
||||||
|
|
||||||
Sample request
|
Sample request
|
||||||
|
|
||||||
`curl -X GET 'https://api2.photowall.com/prices/wallpaper?width=200&height=200&territory=SE&dealerurl=ackes'`
|
`curl -X GET 'https://api2.photowall.com/prices/wallpaper?width=200&height=200&territory=SE&dealerurl=ackes&product=43894'`
|
||||||
|
|
||||||
Sample response
|
Sample response
|
||||||
```
|
```
|
||||||
{
|
{
|
||||||
"data": [
|
"data": [
|
||||||
{
|
{
|
||||||
|
"m2_price": 295.0,
|
||||||
"material": "standard-wallpaper",
|
"material": "standard-wallpaper",
|
||||||
"material_id": 1,
|
"material_id": 1,
|
||||||
"m2_price": 295,
|
"price": 1180.0,
|
||||||
"price": 1180
|
"price_excl_vat": 944.0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"m2_price": 325.0,
|
||||||
"material": "premium-wallpaper",
|
"material": "premium-wallpaper",
|
||||||
"material_id": 4,
|
"material_id": 4,
|
||||||
"m2_price": 325,
|
"price": 1300.0,
|
||||||
"price": 1300
|
"price_excl_vat": 1040.0
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -48,6 +51,7 @@ Calculate price inc VAT of a canvas product. The response will contain informati
|
|||||||
| `height` | Canvas height in cm e.g `50` |
|
| `height` | Canvas height in cm e.g `50` |
|
||||||
| `territory` | Two letter iso code e.g `SE`. Territory affect the price in different ways like VAT, currency exchanges etc. |
|
| `territory` | Two letter iso code e.g `SE`. Territory affect the price in different ways like VAT, currency exchanges etc. |
|
||||||
| `dealerurl` | Optional parameter e.g `ackes`. Should always be used when calling the api from a dealerstore. If a dealerstore exists with this url additional fees will be applied to the price. |
|
| `dealerurl` | Optional parameter e.g `ackes`. Should always be used when calling the api from a dealerstore. If a dealerstore exists with this url additional fees will be applied to the price. |
|
||||||
|
| `product` | Optional parameter e.g `43894`. If a product with this id exists the api will calculate the price for the given product. |
|
||||||
|
|
||||||
|
|
||||||
Sample request
|
Sample request
|
||||||
@@ -60,15 +64,19 @@ Sample response
|
|||||||
"data": [
|
"data": [
|
||||||
{
|
{
|
||||||
"framed": true,
|
"framed": true,
|
||||||
"price": 284,
|
|
||||||
"width": 100,
|
|
||||||
"height": 50,
|
"height": 50,
|
||||||
|
"m2_price": 252.37137599999997,
|
||||||
|
"price": 224.92771680815997,
|
||||||
|
"price_excl_vat": 179.942173446528,
|
||||||
|
"width": 100
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"framed": false,
|
"framed": false,
|
||||||
"price": 249,
|
|
||||||
"width": 100,
|
|
||||||
"height": 50,
|
"height": 50,
|
||||||
|
"m2_price": 252.37137599999997,
|
||||||
|
"price": 126.18568799999998,
|
||||||
|
"price_excl_vat": 100.94855039999999,
|
||||||
|
"width": 100
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
+103
-14
@@ -1,8 +1,9 @@
|
|||||||
import json
|
import json
|
||||||
import flask_testing
|
import flask_testing
|
||||||
from api import create_app, db
|
from api import create_app, db
|
||||||
from api.models.product import Material, MaterialPrice
|
from api.models.product import Material, MaterialPrice, Product
|
||||||
from api.models.inquiry import Inquiry
|
from api.models.inquiry import Inquiry
|
||||||
|
from api.models.designer import Designer
|
||||||
|
|
||||||
|
|
||||||
class TestEndpoints(flask_testing.TestCase):
|
class TestEndpoints(flask_testing.TestCase):
|
||||||
@@ -31,31 +32,119 @@ class TestEndpoints(flask_testing.TestCase):
|
|||||||
db.session.commit()
|
db.session.commit()
|
||||||
return inquiry
|
return inquiry
|
||||||
|
|
||||||
|
def _add_designer(self, pricepremium):
|
||||||
|
designer = Designer('Disney', 'disney', 'US',
|
||||||
|
'USD', pricepremium=pricepremium)
|
||||||
|
db.session.add(designer)
|
||||||
|
db.session.commit()
|
||||||
|
return designer
|
||||||
|
|
||||||
|
def _add_product(self, designer=None):
|
||||||
|
product = Product(designer=designer)
|
||||||
|
db.session.add(product)
|
||||||
|
db.session.commit()
|
||||||
|
return product
|
||||||
|
|
||||||
def test_wallpaper(self):
|
def test_wallpaper(self):
|
||||||
self._add_material('standard-wallpaper', 1, 236)
|
self._add_material('standard-wallpaper', 1, 236)
|
||||||
self._add_material('premium-wallpaper', 1, 260)
|
self._add_material('premium-wallpaper', 1, 260)
|
||||||
response = self.client.get(
|
response = self.client.get(
|
||||||
'/prices/wallpaper?width=200&height=10&territory=SE')
|
'/prices/wallpaper?width=200&height=10&territory=SE')
|
||||||
self.assert200(response)
|
self.assert200(response)
|
||||||
products = response.json['data']
|
expected = [
|
||||||
self.assertEqual(2, len(products))
|
{
|
||||||
|
'm2_price': 295.0,
|
||||||
|
'price_excl_vat': 236.0,
|
||||||
|
'price': 295.0,
|
||||||
|
'material': 'standard-wallpaper',
|
||||||
|
'material_id': 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'm2_price': 325.0,
|
||||||
|
'price_excl_vat': 260.0,
|
||||||
|
'price': 325.0,
|
||||||
|
'material': 'premium-wallpaper',
|
||||||
|
'material_id': 2
|
||||||
|
}
|
||||||
|
]
|
||||||
|
self.assertEqual(expected, response.json['data'])
|
||||||
|
|
||||||
|
def test_wallpaper_with_product(self):
|
||||||
|
self._add_material('standard-wallpaper', 1, 236)
|
||||||
|
self._add_material('premium-wallpaper', 1, 260)
|
||||||
|
designer = self._add_designer(4)
|
||||||
|
product = self._add_product(designer)
|
||||||
|
response = self.client.get(
|
||||||
|
'/prices/wallpaper?width=200&height=200&territory=SE&product=1')
|
||||||
|
expected = [
|
||||||
|
{
|
||||||
|
'price': 1227.2,
|
||||||
|
'price_excl_vat': 981.76,
|
||||||
|
'material_id': 1,
|
||||||
|
'm2_price': 306.8,
|
||||||
|
'material': 'standard-wallpaper'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'price': 1352.0000000000002,
|
||||||
|
'price_excl_vat': 1081.6000000000001,
|
||||||
|
'material_id': 2,
|
||||||
|
'm2_price': 338.00000000000006,
|
||||||
|
'material': 'premium-wallpaper'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
self.assertEqual(expected, response.json['data'])
|
||||||
|
|
||||||
def test_canvas(self):
|
def test_canvas(self):
|
||||||
self._add_material('standard-canvas', 2, 799.2)
|
self._add_material('standard-canvas', 2, 799.2)
|
||||||
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']
|
expected = [
|
||||||
self.assertEqual(2, len(products))
|
{
|
||||||
for product in products:
|
'price_excl_vat': 970.3471312000001,
|
||||||
if product['framed']:
|
'price': 1212.9339140000002,
|
||||||
self.assertEqual(150, product['width'])
|
'width': 150,
|
||||||
self.assertEqual(40, product['height'])
|
'height': 40,
|
||||||
self.assertEqual(1213, product['price'])
|
'framed': True,
|
||||||
else:
|
'm2_price': 999.0
|
||||||
self.assertEqual(200, product['width'])
|
},
|
||||||
self.assertEqual(20, product['height'])
|
{
|
||||||
self.assertEqual(400, product['price'])
|
'price_excl_vat': 319.68000000000006,
|
||||||
|
'price': 399.6000000000001,
|
||||||
|
'width': 200,
|
||||||
|
'height': 20,
|
||||||
|
'framed': False,
|
||||||
|
'm2_price': 999.0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
self.assertEqual(expected, response.json['data'])
|
||||||
|
|
||||||
|
def test_canvas_with_product(self):
|
||||||
|
self._add_material('standard-canvas', 2, 799.2)
|
||||||
|
designer = self._add_designer(4)
|
||||||
|
product = self._add_product(designer)
|
||||||
|
response = self.client.get(
|
||||||
|
'/prices/canvas?width=200&height=10&territory=SE&product=1')
|
||||||
|
self.assert200(response)
|
||||||
|
expected = [
|
||||||
|
{
|
||||||
|
'height': 40,
|
||||||
|
'price_excl_vat': 989.5279312000002,
|
||||||
|
'price': 1236.9099140000003,
|
||||||
|
'framed': True,
|
||||||
|
'm2_price': 1038.96,
|
||||||
|
'width': 150
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'height': 20,
|
||||||
|
'price_excl_vat': 332.46720000000005,
|
||||||
|
'price': 415.58400000000006,
|
||||||
|
'framed': False,
|
||||||
|
'm2_price': 1038.96,
|
||||||
|
'width': 200
|
||||||
|
}
|
||||||
|
]
|
||||||
|
self.assertEqual(expected, response.json['data'])
|
||||||
|
|
||||||
def test_diy_frame(self):
|
def test_diy_frame(self):
|
||||||
response = self.client.get(
|
response = self.client.get(
|
||||||
|
|||||||
Reference in New Issue
Block a user