diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index ec76475901..31a0031c48 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -205,6 +205,20 @@ class InvenTreeSetting(models.Model): 'validator': bool, }, + 'PART_INTERNAL_PRICE': { + 'name': _('Internal Prices'), + 'description': _('Enable internal prices for parts'), + 'default': False, + 'validator': bool + }, + + 'PART_BOM_USE_INTERNAL_PRICE': { + 'name': _('Internal Price as BOM-Price'), + 'description': _('Use the internal price (if set) in BOM-price calculations'), + 'default': False, + 'validator': bool + }, + 'REPORT_DEBUG_MODE': { 'name': _('Debug Mode'), 'description': _('Generate reports in debug mode (HTML output)'), @@ -726,7 +740,7 @@ class PriceBreak(models.Model): return converted.amount -def get_price(instance, quantity, moq=True, multiples=True, currency=None): +def get_price(instance, quantity, moq=True, multiples=True, currency=None, break_name: str = 'price_breaks'): """ Calculate the price based on quantity price breaks. - Don't forget to add in flat-fee cost (base_cost field) @@ -734,7 +748,10 @@ def get_price(instance, quantity, moq=True, multiples=True, currency=None): - If order multiples are to be observed, then we need to calculate based on that, too """ - price_breaks = instance.price_breaks.all() + if hasattr(instance, break_name): + price_breaks = getattr(instance, break_name).all() + else: + price_breaks = [] # No price break information available? if len(price_breaks) == 0: @@ -756,7 +773,7 @@ def get_price(instance, quantity, moq=True, multiples=True, currency=None): currency = currency_code_default() pb_min = None - for pb in instance.price_breaks.all(): + for pb in price_breaks: # Store smallest price break if not pb_min: pb_min = pb diff --git a/InvenTree/part/admin.py b/InvenTree/part/admin.py index 3945b56d7c..637dbbf2cf 100644 --- a/InvenTree/part/admin.py +++ b/InvenTree/part/admin.py @@ -14,7 +14,7 @@ from .models import BomItem from .models import PartParameterTemplate, PartParameter from .models import PartCategoryParameterTemplate from .models import PartTestTemplate -from .models import PartSellPriceBreak +from .models import PartSellPriceBreak, PartInternalPriceBreak from stock.models import StockLocation from company.models import SupplierPart @@ -286,6 +286,14 @@ class PartSellPriceBreakAdmin(admin.ModelAdmin): list_display = ('part', 'quantity', 'price',) +class PartInternalPriceBreakAdmin(admin.ModelAdmin): + + class Meta: + model = PartInternalPriceBreak + + list_display = ('part', 'quantity', 'price',) + + admin.site.register(Part, PartAdmin) admin.site.register(PartCategory, PartCategoryAdmin) admin.site.register(PartRelated, PartRelatedAdmin) @@ -297,3 +305,4 @@ admin.site.register(PartParameter, ParameterAdmin) admin.site.register(PartCategoryParameterTemplate, PartCategoryParameterAdmin) admin.site.register(PartTestTemplate, PartTestTemplateAdmin) admin.site.register(PartSellPriceBreak, PartSellPriceBreakAdmin) +admin.site.register(PartInternalPriceBreak, PartInternalPriceBreakAdmin) diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 537b0f9e40..c2785b666f 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -25,7 +25,7 @@ from django.urls import reverse from .models import Part, PartCategory, BomItem from .models import PartParameter, PartParameterTemplate from .models import PartAttachment, PartTestTemplate -from .models import PartSellPriceBreak +from .models import PartSellPriceBreak, PartInternalPriceBreak from .models import PartCategoryParameterTemplate from common.models import InvenTreeSetting @@ -194,6 +194,24 @@ class PartSalePriceList(generics.ListCreateAPIView): ] +class PartInternalPriceList(generics.ListCreateAPIView): + """ + API endpoint for list view of PartInternalPriceBreak model + """ + + queryset = PartInternalPriceBreak.objects.all() + serializer_class = part_serializers.PartInternalPriceSerializer + permission_required = 'roles.sales_order.show' + + filter_backends = [ + DjangoFilterBackend + ] + + filter_fields = [ + 'part', + ] + + class PartAttachmentList(generics.ListCreateAPIView, AttachmentMixin): """ API endpoint for listing (and creating) a PartAttachment (file upload). @@ -1017,6 +1035,11 @@ part_api_urls = [ url(r'^.*$', PartSalePriceList.as_view(), name='api-part-sale-price-list'), ])), + # Base URL for part internal pricing + url(r'^internal-price/', include([ + url(r'^.*$', PartInternalPriceList.as_view(), name='api-part-internal-price-list'), + ])), + # Base URL for PartParameter API endpoints url(r'^parameter/', include([ url(r'^template/$', PartParameterTemplateList.as_view(), name='api-part-param-template-list'), diff --git a/InvenTree/part/fixtures/part_pricebreaks.yaml b/InvenTree/part/fixtures/part_pricebreaks.yaml new file mode 100644 index 0000000000..0fd14c84df --- /dev/null +++ b/InvenTree/part/fixtures/part_pricebreaks.yaml @@ -0,0 +1,51 @@ +# Sell price breaks for parts + +# Price breaks for R_2K2_0805 + +- model: part.partsellpricebreak + pk: 1 + fields: + part: 3 + quantity: 1 + price: 0.15 + +- model: part.partsellpricebreak + pk: 2 + fields: + part: 3 + quantity: 10 + price: 0.10 + + +# Internal price breaks for parts + +# Internal Price breaks for R_2K2_0805 + +- model: part.partinternalpricebreak + pk: 1 + fields: + part: 3 + quantity: 1 + price: 0.08 + +- model: part.partinternalpricebreak + pk: 2 + fields: + part: 3 + quantity: 10 + price: 0.05 + +# Internal Price breaks for C_22N_0805 +- model: part.partinternalpricebreak + pk: 3 + fields: + part: 5 + quantity: 1 + price: 1 + +- model: part.partinternalpricebreak + pk: 4 + fields: + part: 5 + quantity: 24 + price: 0.5 diff --git a/InvenTree/part/forms.py b/InvenTree/part/forms.py index 95de4961f9..ec799bcf8d 100644 --- a/InvenTree/part/forms.py +++ b/InvenTree/part/forms.py @@ -20,7 +20,7 @@ from .models import BomItem from .models import PartParameterTemplate, PartParameter from .models import PartCategoryParameterTemplate from .models import PartTestTemplate -from .models import PartSellPriceBreak +from .models import PartSellPriceBreak, PartInternalPriceBreak class PartModelChoiceField(forms.ModelChoiceField): @@ -394,3 +394,19 @@ class EditPartSalePriceBreakForm(HelperForm): 'quantity', 'price', ] + + +class EditPartInternalPriceBreakForm(HelperForm): + """ + Form for creating / editing a internal price for a part + """ + + quantity = RoundingDecimalFormField(max_digits=10, decimal_places=5, label=_('Quantity')) + + class Meta: + model = PartInternalPriceBreak + fields = [ + 'part', + 'quantity', + 'price', + ] diff --git a/InvenTree/part/migrations/0067_partinternalpricebreak.py b/InvenTree/part/migrations/0067_partinternalpricebreak.py new file mode 100644 index 0000000000..f1b16fe87c --- /dev/null +++ b/InvenTree/part/migrations/0067_partinternalpricebreak.py @@ -0,0 +1,30 @@ +# Generated by Django 3.2 on 2021-06-05 14:13 + +import InvenTree.fields +import django.core.validators +from django.db import migrations, models +import django.db.models.deletion +import djmoney.models.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('part', '0066_bomitem_allow_variants'), + ] + + operations = [ + migrations.CreateModel( + name='PartInternalPriceBreak', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('quantity', 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')), + ('price_currency', djmoney.models.fields.CurrencyField(choices=[('AUD', 'Australian Dollar'), ('CAD', 'Canadian Dollar'), ('EUR', 'Euro'), ('NZD', 'New Zealand Dollar'), ('GBP', 'Pound Sterling'), ('USD', 'US Dollar'), ('JPY', 'Yen')], default='USD', editable=False, max_length=3)), + ('price', djmoney.models.fields.MoneyField(decimal_places=4, default_currency='USD', help_text='Unit price at specified quantity', max_digits=19, null=True, verbose_name='Price')), + ('part', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='internalpricebreaks', to='part.part', verbose_name='Part')), + ], + options={ + 'unique_together': {('part', 'quantity')}, + }, + ), + ] diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 7b9038fecb..6c05d62a7e 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -1544,7 +1544,7 @@ class Part(MPTTModel): return (min_price, max_price) - def get_bom_price_range(self, quantity=1): + def get_bom_price_range(self, quantity=1, internal=False): """ Return the price range of the BOM for this part. Adds the minimum price for all components in the BOM. @@ -1561,7 +1561,7 @@ class Part(MPTTModel): print("Warning: Item contains itself in BOM") continue - prices = item.sub_part.get_price_range(quantity * item.quantity) + prices = item.sub_part.get_price_range(quantity * item.quantity, internal=internal) if prices is None: continue @@ -1585,7 +1585,7 @@ class Part(MPTTModel): return (min_price, max_price) - def get_price_range(self, quantity=1, buy=True, bom=True): + def get_price_range(self, quantity=1, buy=True, bom=True, internal=False): """ Return the price range for this part. This price can be either: @@ -1596,8 +1596,13 @@ class Part(MPTTModel): Minimum of the supplier price or BOM price. If no pricing available, returns None """ + # only get internal price if set and should be used + if internal and self.has_internal_price_breaks: + internal_price = self.get_internal_price(quantity) + return internal_price, internal_price + buy_price_range = self.get_supplier_price_range(quantity) if buy else None - bom_price_range = self.get_bom_price_range(quantity) if bom else None + bom_price_range = self.get_bom_price_range(quantity, internal=internal) if bom else None if buy_price_range is None: return bom_price_range @@ -1649,6 +1654,22 @@ class Part(MPTTModel): price=price ) + def get_internal_price(self, quantity, moq=True, multiples=True, currency=None): + return common.models.get_price(self, quantity, moq, multiples, currency, break_name='internal_price_breaks') + + @property + def has_internal_price_breaks(self): + return self.internal_price_breaks.count() > 0 + + @property + def internal_price_breaks(self): + """ Return the associated price breaks in the correct order """ + return self.internalpricebreaks.order_by('quantity').all() + + @property + def internal_unit_pricing(self): + return self.get_internal_price(1) + @transaction.atomic def copy_bom_from(self, other, clear=True, **kwargs): """ @@ -1983,6 +2004,21 @@ class PartSellPriceBreak(common.models.PriceBreak): unique_together = ('part', 'quantity') +class PartInternalPriceBreak(common.models.PriceBreak): + """ + Represents a price break for internally selling this part + """ + + part = models.ForeignKey( + Part, on_delete=models.CASCADE, + related_name='internalpricebreaks', + verbose_name=_('Part') + ) + + class Meta: + unique_together = ('part', 'quantity') + + class PartStar(models.Model): """ A PartStar object creates a relationship between a User and a Part. diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index d03a37c6dc..76275bd5e1 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -17,7 +17,8 @@ from stock.models import StockItem from .models import (BomItem, Part, PartAttachment, PartCategory, PartParameter, PartParameterTemplate, PartSellPriceBreak, - PartStar, PartTestTemplate, PartCategoryParameterTemplate) + PartStar, PartTestTemplate, PartCategoryParameterTemplate, + PartInternalPriceBreak) class CategorySerializer(InvenTreeModelSerializer): @@ -100,6 +101,25 @@ class PartSalePriceSerializer(InvenTreeModelSerializer): ] +class PartInternalPriceSerializer(InvenTreeModelSerializer): + """ + Serializer for internal prices for Part model. + """ + + quantity = serializers.FloatField() + + price = serializers.CharField() + + class Meta: + model = PartInternalPriceBreak + fields = [ + 'pk', + 'part', + 'quantity', + 'price', + ] + + class PartThumbSerializer(serializers.Serializer): """ Serializer for the 'image' field of the Part model. diff --git a/InvenTree/part/templates/part/internal_prices.html b/InvenTree/part/templates/part/internal_prices.html new file mode 100644 index 0000000000..2f54f3bb64 --- /dev/null +++ b/InvenTree/part/templates/part/internal_prices.html @@ -0,0 +1,122 @@ +{% extends "part/part_base.html" %} +{% load static %} +{% load i18n %} +{% load inventree_extras %} + +{% block menubar %} +{% include 'part/navbar.html' with tab='internal-prices' %} +{% endblock %} + +{% block heading %} +{% trans "Internal Price Information" %} +{% endblock %} + +{% block details %} +{% settings_value "PART_INTERNAL_PRICE" as show_internal_price %} +{% if show_internal_price and roles.sales_order.view %} +