Return price breaks in the correct order

This commit is contained in:
Oliver Walters 2019-05-18 16:42:15 +10:00
parent a3cd54875c
commit 1163f60b23
2 changed files with 26 additions and 3 deletions

View File

@ -0,0 +1,19 @@
# Generated by Django 2.2 on 2019-05-18 06:41
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('part', '0029_auto_20190518_1632'),
]
operations = [
migrations.AlterField(
model_name='supplierpricebreak',
name='part',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='pricebreaks', to='part.SupplierPart'),
),
]

View File

@ -879,6 +879,11 @@ class SupplierPart(models.Model):
def has_price_breaks(self):
return self.price_breaks.count() > 0
@property
def price_breaks(self):
""" Return the associated price breaks in the correct order """
return self.pricebreaks.order_by('quantity').all()
def get_price(self, quantity, moq=True, multiples=True):
""" Calculate the supplier price based on quantity price breaks.
@ -939,7 +944,7 @@ class SupplierPriceBreak(models.Model):
cost: Cost at specified quantity
"""
part = models.ForeignKey(SupplierPart, on_delete=models.CASCADE, related_name='price_breaks')
part = models.ForeignKey(SupplierPart, on_delete=models.CASCADE, related_name='pricebreaks')
quantity = models.PositiveIntegerField(validators=[MinValueValidator(1)])
@ -949,8 +954,7 @@ class SupplierPriceBreak(models.Model):
unique_together = ("part", "quantity")
def __str__(self):
return "{mpn} - {cost}{currency} @ {quan}".format(
return "{mpn} - {cost} @ {quan}".format(
mpn=self.part.MPN,
cost=self.cost,
currency=self.currency if self.currency else '',
quan=self.quantity)