Convert SupplierPriceBreak model to use the abstract PriceBreak class

This commit is contained in:
Oliver Walters 2020-09-17 22:47:31 +10:00
parent 95c5c4b575
commit 805e8daa57
2 changed files with 20 additions and 21 deletions

View File

@ -7,6 +7,7 @@ These models are 'generic' and do not fit a particular business logic object.
from __future__ import unicode_literals from __future__ import unicode_literals
import os import os
import decimal
from django.db import models from django.db import models
from django.conf import settings from django.conf import settings
@ -162,6 +163,9 @@ class Currency(models.Model):
class PriceBreak(models.Model): class PriceBreak(models.Model):
"""
Represents a PriceBreak model
"""
class Meta: class Meta:
abstract = True abstract = True
@ -172,6 +176,18 @@ class PriceBreak(models.Model):
currency = models.ForeignKey(Currency, blank=True, null=True, on_delete=models.SET_NULL) currency = models.ForeignKey(Currency, blank=True, null=True, on_delete=models.SET_NULL)
@property
def converted_cost(self):
"""
Return the cost of this price break, converted to the base currency
"""
scaler = decimal.Decimal(1.0)
if self.currency:
scaler = self.currency.value
return self.cost * scaler
class ColorTheme(models.Model): class ColorTheme(models.Model):

View File

@ -8,7 +8,6 @@ from __future__ import unicode_literals
import os import os
import math import math
from decimal import Decimal
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.core.validators import MinValueValidator from django.core.validators import MinValueValidator
@ -24,9 +23,10 @@ from stdimage.models import StdImageField
from InvenTree.helpers import getMediaUrl, getBlankImage, getBlankThumbnail from InvenTree.helpers import getMediaUrl, getBlankImage, getBlankThumbnail
from InvenTree.helpers import normalize from InvenTree.helpers import normalize
from InvenTree.fields import InvenTreeURLField, RoundingDecimalField from InvenTree.fields import InvenTreeURLField
from InvenTree.status_codes import PurchaseOrderStatus from InvenTree.status_codes import PurchaseOrderStatus
from common.models import Currency
import common.models
def rename_company_image(instance, filename): def rename_company_image(instance, filename):
@ -433,7 +433,7 @@ class SupplierPart(models.Model):
return s return s
class SupplierPriceBreak(models.Model): class SupplierPriceBreak(common.models.PriceBreak):
""" Represents a quantity price break for a SupplierPart. """ Represents a quantity price break for a SupplierPart.
- Suppliers can offer discounts at larger quantities - Suppliers can offer discounts at larger quantities
- SupplierPart(s) may have zero-or-more associated SupplierPriceBreak(s) - SupplierPart(s) may have zero-or-more associated SupplierPriceBreak(s)
@ -447,23 +447,6 @@ class SupplierPriceBreak(models.Model):
part = models.ForeignKey(SupplierPart, on_delete=models.CASCADE, related_name='pricebreaks') part = models.ForeignKey(SupplierPart, on_delete=models.CASCADE, related_name='pricebreaks')
quantity = RoundingDecimalField(max_digits=15, decimal_places=5, default=1, validators=[MinValueValidator(1)])
cost = RoundingDecimalField(max_digits=10, decimal_places=5, validators=[MinValueValidator(0)])
currency = models.ForeignKey(Currency, blank=True, null=True, on_delete=models.SET_NULL)
@property
def converted_cost(self):
""" Return the cost of this price break, converted to the base currency """
scaler = Decimal(1.0)
if self.currency:
scaler = self.currency.value
return self.cost * scaler
class Meta: class Meta:
unique_together = ("part", "quantity") unique_together = ("part", "quantity")