From 51d2d85c267544239be91360dd6f5dca5a523ecd Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 12 Nov 2020 11:14:50 +1100 Subject: [PATCH] When creating a new price break for a supplier part, default to using the currency code specified for the supplier company --- InvenTree/common/models.py | 9 ++++- InvenTree/company/forms.py | 7 +++- .../migrations/0030_auto_20201112_1112.py | 20 +++++++++++ InvenTree/company/models.py | 19 +++++++++++ InvenTree/company/views.py | 33 +++++++++++++++---- .../migrations/0059_auto_20201112_1112.py | 20 +++++++++++ 6 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 InvenTree/company/migrations/0030_auto_20201112_1112.py create mode 100644 InvenTree/part/migrations/0059_auto_20201112_1112.py diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 4ebd60e8b0..e4e1bc1718 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -464,7 +464,14 @@ class PriceBreak(models.Model): class Meta: 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( max_digits=19, diff --git a/InvenTree/company/forms.py b/InvenTree/company/forms.py index 27f2dce0dc..da90286b35 100644 --- a/InvenTree/company/forms.py +++ b/InvenTree/company/forms.py @@ -107,7 +107,12 @@ class EditSupplierPartForm(HelperForm): class EditPriceBreakForm(HelperForm): """ 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: model = SupplierPriceBreak diff --git a/InvenTree/company/migrations/0030_auto_20201112_1112.py b/InvenTree/company/migrations/0030_auto_20201112_1112.py new file mode 100644 index 0000000000..367ed303e5 --- /dev/null +++ b/InvenTree/company/migrations/0030_auto_20201112_1112.py @@ -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'), + ), + ] diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py index 711a1966ff..241c5fe1f7 100644 --- a/InvenTree/company/models.py +++ b/InvenTree/company/models.py @@ -17,6 +17,8 @@ from django.db.models import Sum, Q, UniqueConstraint from django.apps import apps from django.urls import reverse +from moneyed import CURRENCIES + from markdownx.models import MarkdownxField from stdimage.models import StdImageField @@ -29,6 +31,7 @@ from InvenTree.status_codes import PurchaseOrderStatus import InvenTree.validators import common.models +import common.settings def rename_company_image(instance, filename): @@ -137,6 +140,22 @@ class Company(models.Model): 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): """ Get string representation of a Company """ return "{n} - {d}".format(n=self.name, d=self.description) diff --git a/InvenTree/company/views.py b/InvenTree/company/views.py index abdad55322..758e8a2990 100644 --- a/InvenTree/company/views.py +++ b/InvenTree/company/views.py @@ -30,6 +30,7 @@ from .forms import EditSupplierPartForm from .forms import EditPriceBreakForm import common.models +import common.settings class CompanyIndex(InvenTreeRoleMixin, ListView): @@ -419,10 +420,23 @@ class PriceBreakCreate(AjaxCreateView): } def get_part(self): + """ + Attempt to extract SupplierPart object from the supplied data. + """ + try: - return SupplierPart.objects.get(id=self.request.GET.get('part')) - except SupplierPart.DoesNotExist: - return SupplierPart.objects.get(id=self.request.POST.get('part')) + supplier_part = SupplierPart.objects.get(pk=self.request.GET.get('part')) + return supplier_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): @@ -435,12 +449,19 @@ class PriceBreakCreate(AjaxCreateView): initials = super(AjaxCreateView, self).get_initial() + supplier_part = self.get_part() + initials['part'] = self.get_part() - default_currency = common.models.InvenTreeSetting.get_setting('INVENTREE_DEFAULT_CURRENCY') - currency = CURRENCIES.get(default_currency, None) + if supplier_part is not 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] return initials diff --git a/InvenTree/part/migrations/0059_auto_20201112_1112.py b/InvenTree/part/migrations/0059_auto_20201112_1112.py new file mode 100644 index 0000000000..5075b8431c --- /dev/null +++ b/InvenTree/part/migrations/0059_auto_20201112_1112.py @@ -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'), + ), + ]