When creating a new price break for a supplier part, default to using the currency code specified for the supplier company

This commit is contained in:
Oliver Walters 2020-11-12 11:14:50 +11:00
parent 1532be9c1e
commit 51d2d85c26
6 changed files with 100 additions and 8 deletions

View File

@ -464,7 +464,14 @@ class PriceBreak(models.Model):
class Meta: class Meta:
abstract = True abstract = True
quantity = InvenTree.fields.RoundingDecimalField(max_digits=15, decimal_places=5, default=1, validators=[MinValueValidator(1)]) quantity = InvenTree.fields.RoundingDecimalField(
max_digits=15,
decimal_places=5,
default=1,
validators=[MinValueValidator(1)],
verbose_name=_('Quantity'),
help_text=_('Price break quantity'),
)
price = MoneyField( price = MoneyField(
max_digits=19, max_digits=19,

View File

@ -107,7 +107,12 @@ class EditSupplierPartForm(HelperForm):
class EditPriceBreakForm(HelperForm): class EditPriceBreakForm(HelperForm):
""" Form for creating / editing a supplier price break """ """ Form for creating / editing a supplier price break """
quantity = RoundingDecimalFormField(max_digits=10, decimal_places=5) quantity = RoundingDecimalFormField(
max_digits=10,
decimal_places=5,
label=_('Quantity'),
help_text=_('Price break quantity'),
)
class Meta: class Meta:
model = SupplierPriceBreak model = SupplierPriceBreak

View File

@ -0,0 +1,20 @@
# Generated by Django 3.0.7 on 2020-11-12 00:12
import InvenTree.fields
import django.core.validators
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('company', '0029_company_currency'),
]
operations = [
migrations.AlterField(
model_name='supplierpricebreak',
name='quantity',
field=InvenTree.fields.RoundingDecimalField(decimal_places=5, default=1, help_text='Price break quantity', max_digits=15, validators=[django.core.validators.MinValueValidator(1)], verbose_name='Quantity'),
),
]

View File

@ -17,6 +17,8 @@ from django.db.models import Sum, Q, UniqueConstraint
from django.apps import apps from django.apps import apps
from django.urls import reverse from django.urls import reverse
from moneyed import CURRENCIES
from markdownx.models import MarkdownxField from markdownx.models import MarkdownxField
from stdimage.models import StdImageField from stdimage.models import StdImageField
@ -29,6 +31,7 @@ from InvenTree.status_codes import PurchaseOrderStatus
import InvenTree.validators import InvenTree.validators
import common.models import common.models
import common.settings
def rename_company_image(instance, filename): def rename_company_image(instance, filename):
@ -137,6 +140,22 @@ class Company(models.Model):
validators=[InvenTree.validators.validate_currency_code], validators=[InvenTree.validators.validate_currency_code],
) )
@property
def currency_code(self):
"""
Return the currency code associated with this company.
- If the currency code is invalid, use the default currency
- If the currency code is not specified, use the default currency
"""
code = self.currency
if code not in CURRENCIES:
code = common.settings.currency_code_default()
return code
def __str__(self): def __str__(self):
""" Get string representation of a Company """ """ Get string representation of a Company """
return "{n} - {d}".format(n=self.name, d=self.description) return "{n} - {d}".format(n=self.name, d=self.description)

View File

@ -30,6 +30,7 @@ from .forms import EditSupplierPartForm
from .forms import EditPriceBreakForm from .forms import EditPriceBreakForm
import common.models import common.models
import common.settings
class CompanyIndex(InvenTreeRoleMixin, ListView): class CompanyIndex(InvenTreeRoleMixin, ListView):
@ -419,10 +420,23 @@ class PriceBreakCreate(AjaxCreateView):
} }
def get_part(self): def get_part(self):
"""
Attempt to extract SupplierPart object from the supplied data.
"""
try: try:
return SupplierPart.objects.get(id=self.request.GET.get('part')) supplier_part = SupplierPart.objects.get(pk=self.request.GET.get('part'))
except SupplierPart.DoesNotExist: return supplier_part
return SupplierPart.objects.get(id=self.request.POST.get('part')) except (ValueError, SupplierPart.DoesNotExist):
pass
try:
supplier_part = SupplierPart.objects.get(pk=self.request.POST.get('part'))
return supplier_part
except (ValueError, SupplierPart.DoesNotExist):
pass
return None
def get_form(self): def get_form(self):
@ -435,12 +449,19 @@ class PriceBreakCreate(AjaxCreateView):
initials = super(AjaxCreateView, self).get_initial() initials = super(AjaxCreateView, self).get_initial()
supplier_part = self.get_part()
initials['part'] = self.get_part() initials['part'] = self.get_part()
default_currency = common.models.InvenTreeSetting.get_setting('INVENTREE_DEFAULT_CURRENCY') if supplier_part is not None:
currency = CURRENCIES.get(default_currency, None) currency_code = supplier_part.supplier.currency_code
else:
currency_code = common.settings.currency_code_default()
if currency is not None: # Extract the currency object associated with the code
currency = CURRENCIES.get(currency_code, None)
if currency:
initials['price'] = [1.0, currency] initials['price'] = [1.0, currency]
return initials return initials

View File

@ -0,0 +1,20 @@
# Generated by Django 3.0.7 on 2020-11-12 00:12
import InvenTree.fields
import django.core.validators
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('part', '0058_remove_partsellpricebreak_cost'),
]
operations = [
migrations.AlterField(
model_name='partsellpricebreak',
name='quantity',
field=InvenTree.fields.RoundingDecimalField(decimal_places=5, default=1, help_text='Price break quantity', max_digits=15, validators=[django.core.validators.MinValueValidator(1)], verbose_name='Quantity'),
),
]