From a3d1591929797376c390a924e16a6f5e2ca3b5f2 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 18 May 2019 20:09:41 +1000 Subject: [PATCH] Display part pricing in Part view - Calculate min_price and max_price based on provided supplier information --- InvenTree/company/models.py | 4 +- InvenTree/part/models.py | 55 ++++++++++++++++++++ InvenTree/part/templates/part/part_base.html | 19 +++++++ 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py index 06aa100f26..b45bd04c3d 100644 --- a/InvenTree/company/models.py +++ b/InvenTree/company/models.py @@ -7,6 +7,8 @@ from __future__ import unicode_literals import os +import math + from django.core.validators import MinValueValidator from django.apps import apps @@ -260,7 +262,7 @@ class SupplierPart(models.Model): # Order multiples if multiples: - quantity = int(math.ceil(quantity / self.multipe) * self.multiple) + quantity = int(math.ceil(quantity / self.multiple) * self.multiple) pb_found = False pb_quantity = -1 diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 62fb6e7553..00fb07290e 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -563,6 +563,61 @@ class Part(models.Model): """ Return the number of supplier parts available for this part """ return self.supplier_parts.count() + @property + def min_single_price(self): + return self.get_min_supplier_price(1) + + @property + def max_single_price(self): + return self.get_max_supplier_price(1) + + def get_min_supplier_price(self, quantity=1): + """ Return the minimum price of this part from all available suppliers. + + Args: + quantity: Number of units we wish to purchase (default = 1) + + Returns: + Numerical price if pricing is available, else None + """ + + min_price = None + + for supplier_part in self.supplier_parts.all(): + supplier_price = supplier_part.get_price(quantity) + + if supplier_price is None: + continue + + if min_price is None or supplier_price < min_price: + min_price = supplier_price + + return min_price + + def get_max_supplier_price(self, quantity=1): + """ Return the maximum price of this part from all available suppliers. + + Args: + quantity: Number of units we wish to purchase (default = 1) + + Returns: + Numerical price if pricing is available, else None + """ + + max_price = None + + for supplier_part in self.supplier_parts.all(): + supplier_price = supplier_part.get_price(quantity) + + if supplier_price is None: + continue + + if max_price is None or supplier_price > max_price: + max_price = supplier_price + + return max_price + + def deepCopy(self, other, **kwargs): """ Duplicates non-field data from another part. Does not alter the normal fields of this part, diff --git a/InvenTree/part/templates/part/part_base.html b/InvenTree/part/templates/part/part_base.html index ea3bbc7617..405556bdfa 100644 --- a/InvenTree/part/templates/part/part_base.html +++ b/InvenTree/part/templates/part/part_base.html @@ -82,6 +82,25 @@ {{ part.allocation_count }} {% endif %} + {% if part.supplier_count > 0 %} + + + Price + + + {% if part.min_single_price %} + {% if part.min_single_price == part.max_single_price %} + {{ part.min_single_price }} + {% else %} + {{ part.min_single_price }} to {{ part.max_single_price }} + {% endif %} + from {{ part.supplier_count }} suppliers. + {% else %} + No pricing data avilable + {% endif %} + + + {% endif %}