Display part pricing in Part view

- Calculate min_price and max_price based on provided supplier information
This commit is contained in:
Oliver Walters 2019-05-18 20:09:41 +10:00
parent 0cfb243eb3
commit a3d1591929
3 changed files with 77 additions and 1 deletions

View File

@ -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

View File

@ -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,

View File

@ -82,6 +82,25 @@
<td>{{ part.allocation_count }}</td>
</tr>
{% endif %}
{% if part.supplier_count > 0 %}
<tr>
<td>
Price
</td>
<td>
{% 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 %}
<span class='warning-msg'><i>No pricing data avilable</i></span>
{% endif %}
</td>
</tr>
{% endif %}
</table>
</div>
</div>