mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 05:05:42 +00:00 
			
		
		
		
	* Create new model for storing Part pricing data
Currently this model does not "do" anything but will be used for caching pre-calculated pricing information
* Define function for accessing pricing information for a specific part
* Adds admin site support for new PartPricing model
* Specify role for PartPricing model
* Allow blank values for PartPricing model fields
* Add some TODO entries
* Update migration files to sync with latest master
* Expose API endpoint for viewing part pricing information
* Update migration file
* Improvements:
- Updated model with new fields
- Code for calculating BOM price
- Code for calculating internal price
- Code for calculating supplier price
- Updated unit testing
* Fix (and test) for API serializer
* Including min/max pricing data in part serializer
* Bump API version
* Add pricing overview information in part table
- Adds helper function for formatting currency data
- No longer pre-render "price strings" on the server
* Overhaul of BOM API
- Pricing data no longer calculated "on the fly"
- Remove expensive annotation operations
- Display cached price range information in BOM table
* Filter BOM items by "has pricing"
* Part API endpoint can be filtered by price range
* Updpated API version notes
* Improvements for price caching calculations
- Handle null price values
- Handle case where conversion rates are missing
- Allow manual update via API
* Button to manually refresh pricing
* Improve rendering of price-break table
* Update supplier part pricing table
* Updated js functions
* Adds background task to update assembly pricing whenever a part price cache is changed
* Updates for task offloading
* HTML tweaks
* Implement calculation of historical purchase cost
- take supplier part pack size into account
- improve unit tests
* Improvements for pricing tab rendering
* Refactor of pricing page
- Move javascript functions out into separate files
- Change price-break tables to use bar graphs
- Display part pricing history table and chart
- Remove server-side rendering for price history data
- Fix rendering of supplier pricing table
- Adds extra filtering options to the SupplierPriceBreak API endpoint
* Refactor BOM pricing chart / table
- Display as bar chart with min/max pricing
- Display simplified BOM table
* Update page anchors
* Improvements for BOM pricing table display
* Refactoring sales data tables
- Add extra data and filter options to sales order API endpoints
- Display sales order history table and chart
* Add extra fields to PartPricing model:
- sale_price_min
- sale_price_max
- sale_history_min
- sale_history_max
* Calculate and cache sale price data
* Update part pricing when PurchaseOrder is completed
* Update part pricing when sales order is completed
* Signals for updating part pricing cache
- Whenever an internal price break is created / edited / deleted
- Whenever a sale price break is created / edited / deleted
* Also trigger part pricing update when BomItem is created  / edited / deleted
* Update part pricing whenever a supplier price break is updated
* Remove has_complete_bom_pricing method
* Export min/max pricing data in BOM file
* Fix pricing data in BOM export
- Calculate total line cost
- Use more than two digits
* Add pricing information to part export
Also some improvements to part exporting
* Allow download of part category table
* Allow export of stock location data to file
* Improved exporting of StockItem data
* Add cached variant pricing data
- New fields in part pricing model
- Display variant pricing overview in "pricing" tab
* Remove outdated "PART_SHOW_PRICE_HISTORY" setting
* Adds scheduled background task to periodically update part pricing
* Internal prices can optionally override other pricing
* Update js file checks
* Update price breaks to use 6 decimal places
* Fix for InvenTreeMoneySerializer class
- Allow 6 decimal places through the API
* Update for supplier price break table
* javascript linting fix
* Further js fixes
* Unit test updates
* Improve rendering of currency in templates
- Do not artificially limit to 2 decimal places
* Unit test fixes
* Add pricing information to part "details" tab
* Tweak for money formatting
* Enable sort-by-price in BOM table
* More unit test tweaks
* Update BOM exporting
* Fixes for background worker process
- To determine if worker is running, look for *any* successful task, not just heartbeat
- Heartbeat rate increased to 5 minute intervals
- Small adjustments to django_q settings
Ref: https://github.com/inventree/InvenTree/issues/3921
(cherry picked from commit cb26003b92)
* Force background processing of heartbeat task when server is started
- Removes the ~5 minute window in which the server "thinks" that the worker is not actually running
* Adjust strategy for preventing recursion
- Rather than looking for duplicate parts, simply increment a counter
- Add a "scheduled_for_update" flag to prevent multiple updates being scheduled
- Consolidate migration files
* Adds helper function for rendering a range of prices
* Include variant cost in calculations
* Fixes for "has_pricing" API filters
* Ensure part pricing status flags are reset when the server restarts
* Bug fix for BOM API filter
* Include BOM quantity in BOM pricing chart
* Small tweaks to pricing tab
* Prevent caching when looking up settings in background worker
- Caching across mnultiple processes causes issues
- Need to move to something like redis to solve this
- Ref: https://github.com/inventree/InvenTree/issues/3921
* Fixes for /part/pricing/ detail API endpoint
* Update pricing tab
- Consistent naming
* Unit test fixes
* Prevent pricing updates when loading test fixtures
* Fix for Part.pricing
* Updates for "check_missing_pricing"
* Change to pie chart for BOM pricing
* Unit test fix
* Updates
- Sort BOM pie chart correctly
- Simplify PartPricing.is_valid
- Pass "limit" through to check_missing_pricing
- Improved logic for update scheduling
* Add option for changing how many decimals to use when displaying pricing data
* remove old unused setting
* Consolidate settings tabs for pricing and currencies
* Fix CI after changing settings page
* Fix rendering for "Supplier Pricing"
- Take unit pricing / pack size into account
* Extra filtering / ordering options for the SupplierPriceBreak API endpoint
* Fix for purchase price history graph
- Use unit pricing (take pack size into account)
* JS fixes
		
	
		
			
				
	
	
		
			424 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			424 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| """JSON serializers for Company app."""
 | |
| 
 | |
| import io
 | |
| 
 | |
| from django.core.files.base import ContentFile
 | |
| from django.utils.translation import gettext_lazy as _
 | |
| 
 | |
| from rest_framework import serializers
 | |
| from sql_util.utils import SubqueryCount
 | |
| 
 | |
| import part.filters
 | |
| from common.settings import currency_code_default, currency_code_mappings
 | |
| from InvenTree.serializers import (InvenTreeAttachmentSerializer,
 | |
|                                    InvenTreeDecimalField,
 | |
|                                    InvenTreeImageSerializerField,
 | |
|                                    InvenTreeModelSerializer,
 | |
|                                    InvenTreeMoneySerializer, RemoteImageMixin)
 | |
| from part.serializers import PartBriefSerializer
 | |
| 
 | |
| from .models import (Company, ManufacturerPart, ManufacturerPartAttachment,
 | |
|                      ManufacturerPartParameter, SupplierPart,
 | |
|                      SupplierPriceBreak)
 | |
| 
 | |
| 
 | |
| class CompanyBriefSerializer(InvenTreeModelSerializer):
 | |
|     """Serializer for Company object (limited detail)"""
 | |
| 
 | |
|     url = serializers.CharField(source='get_absolute_url', read_only=True)
 | |
| 
 | |
|     image = serializers.CharField(source='get_thumbnail_url', read_only=True)
 | |
| 
 | |
|     class Meta:
 | |
|         """Metaclass options."""
 | |
| 
 | |
|         model = Company
 | |
|         fields = [
 | |
|             'pk',
 | |
|             'url',
 | |
|             'name',
 | |
|             'description',
 | |
|             'image',
 | |
|         ]
 | |
| 
 | |
| 
 | |
| class CompanySerializer(RemoteImageMixin, InvenTreeModelSerializer):
 | |
|     """Serializer for Company object (full detail)"""
 | |
| 
 | |
|     @staticmethod
 | |
|     def annotate_queryset(queryset):
 | |
|         """Annoate the supplied queryset with aggregated information"""
 | |
|         # Add count of parts manufactured
 | |
|         queryset = queryset.annotate(
 | |
|             parts_manufactured=SubqueryCount('manufactured_parts')
 | |
|         )
 | |
| 
 | |
|         queryset = queryset.annotate(
 | |
|             parts_supplied=SubqueryCount('supplied_parts')
 | |
|         )
 | |
| 
 | |
|         return queryset
 | |
| 
 | |
|     url = serializers.CharField(source='get_absolute_url', read_only=True)
 | |
| 
 | |
|     image = InvenTreeImageSerializerField(required=False, allow_null=True)
 | |
| 
 | |
|     parts_supplied = serializers.IntegerField(read_only=True)
 | |
|     parts_manufactured = serializers.IntegerField(read_only=True)
 | |
| 
 | |
|     currency = serializers.ChoiceField(
 | |
|         choices=currency_code_mappings(),
 | |
|         initial=currency_code_default,
 | |
|         help_text=_('Default currency used for this supplier'),
 | |
|         label=_('Currency Code'),
 | |
|         required=True,
 | |
|     )
 | |
| 
 | |
|     class Meta:
 | |
|         """Metaclass options."""
 | |
| 
 | |
|         model = Company
 | |
|         fields = [
 | |
|             'pk',
 | |
|             'url',
 | |
|             'name',
 | |
|             'description',
 | |
|             'website',
 | |
|             'name',
 | |
|             'phone',
 | |
|             'address',
 | |
|             'email',
 | |
|             'currency',
 | |
|             'contact',
 | |
|             'link',
 | |
|             'image',
 | |
|             'is_customer',
 | |
|             'is_manufacturer',
 | |
|             'is_supplier',
 | |
|             'notes',
 | |
|             'parts_supplied',
 | |
|             'parts_manufactured',
 | |
|             'remote_image',
 | |
|         ]
 | |
| 
 | |
|     def save(self):
 | |
|         """Save the Company instance"""
 | |
|         super().save()
 | |
| 
 | |
|         company = self.instance
 | |
| 
 | |
|         # Check if an image was downloaded from a remote URL
 | |
|         remote_img = getattr(self, 'remote_image_file', None)
 | |
| 
 | |
|         if remote_img and company:
 | |
|             fmt = remote_img.format or 'PNG'
 | |
|             buffer = io.BytesIO()
 | |
|             remote_img.save(buffer, format=fmt)
 | |
| 
 | |
|             # Construct a simplified name for the image
 | |
|             filename = f"company_{company.pk}_image.{fmt.lower()}"
 | |
| 
 | |
|             company.image.save(
 | |
|                 filename,
 | |
|                 ContentFile(buffer.getvalue()),
 | |
|             )
 | |
| 
 | |
|         return self.instance
 | |
| 
 | |
| 
 | |
| class ManufacturerPartSerializer(InvenTreeModelSerializer):
 | |
|     """Serializer for ManufacturerPart object."""
 | |
| 
 | |
|     part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
 | |
| 
 | |
|     manufacturer_detail = CompanyBriefSerializer(source='manufacturer', many=False, read_only=True)
 | |
| 
 | |
|     pretty_name = serializers.CharField(read_only=True)
 | |
| 
 | |
|     def __init__(self, *args, **kwargs):
 | |
|         """Initialize this serializer with extra detail fields as required"""
 | |
|         part_detail = kwargs.pop('part_detail', True)
 | |
|         manufacturer_detail = kwargs.pop('manufacturer_detail', True)
 | |
|         prettify = kwargs.pop('pretty', False)
 | |
| 
 | |
|         super().__init__(*args, **kwargs)
 | |
| 
 | |
|         if part_detail is not True:
 | |
|             self.fields.pop('part_detail')
 | |
| 
 | |
|         if manufacturer_detail is not True:
 | |
|             self.fields.pop('manufacturer_detail')
 | |
| 
 | |
|         if prettify is not True:
 | |
|             self.fields.pop('pretty_name')
 | |
| 
 | |
|     manufacturer = serializers.PrimaryKeyRelatedField(queryset=Company.objects.filter(is_manufacturer=True))
 | |
| 
 | |
|     class Meta:
 | |
|         """Metaclass options."""
 | |
| 
 | |
|         model = ManufacturerPart
 | |
|         fields = [
 | |
|             'pk',
 | |
|             'part',
 | |
|             'part_detail',
 | |
|             'pretty_name',
 | |
|             'manufacturer',
 | |
|             'manufacturer_detail',
 | |
|             'description',
 | |
|             'MPN',
 | |
|             'link',
 | |
|         ]
 | |
| 
 | |
| 
 | |
| class ManufacturerPartAttachmentSerializer(InvenTreeAttachmentSerializer):
 | |
|     """Serializer for the ManufacturerPartAttachment class."""
 | |
| 
 | |
|     class Meta:
 | |
|         """Metaclass options."""
 | |
| 
 | |
|         model = ManufacturerPartAttachment
 | |
| 
 | |
|         fields = [
 | |
|             'pk',
 | |
|             'manufacturer_part',
 | |
|             'attachment',
 | |
|             'filename',
 | |
|             'link',
 | |
|             'comment',
 | |
|             'upload_date',
 | |
|             'user',
 | |
|             'user_detail',
 | |
|         ]
 | |
| 
 | |
|         read_only_fields = [
 | |
|             'upload_date',
 | |
|         ]
 | |
| 
 | |
| 
 | |
| class ManufacturerPartParameterSerializer(InvenTreeModelSerializer):
 | |
|     """Serializer for the ManufacturerPartParameter model."""
 | |
| 
 | |
|     manufacturer_part_detail = ManufacturerPartSerializer(source='manufacturer_part', many=False, read_only=True)
 | |
| 
 | |
|     def __init__(self, *args, **kwargs):
 | |
|         """Initialize this serializer with extra detail fields as required"""
 | |
|         man_detail = kwargs.pop('manufacturer_part_detail', False)
 | |
| 
 | |
|         super().__init__(*args, **kwargs)
 | |
| 
 | |
|         if not man_detail:
 | |
|             self.fields.pop('manufacturer_part_detail')
 | |
| 
 | |
|     class Meta:
 | |
|         """Metaclass options."""
 | |
| 
 | |
|         model = ManufacturerPartParameter
 | |
| 
 | |
|         fields = [
 | |
|             'pk',
 | |
|             'manufacturer_part',
 | |
|             'manufacturer_part_detail',
 | |
|             'name',
 | |
|             'value',
 | |
|             'units',
 | |
|         ]
 | |
| 
 | |
| 
 | |
| class SupplierPartSerializer(InvenTreeModelSerializer):
 | |
|     """Serializer for SupplierPart object."""
 | |
| 
 | |
|     # Annotated field showing total in-stock quantity
 | |
|     in_stock = serializers.FloatField(read_only=True)
 | |
| 
 | |
|     part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
 | |
| 
 | |
|     supplier_detail = CompanyBriefSerializer(source='supplier', many=False, read_only=True)
 | |
| 
 | |
|     manufacturer_detail = CompanyBriefSerializer(source='manufacturer_part.manufacturer', many=False, read_only=True)
 | |
| 
 | |
|     pretty_name = serializers.CharField(read_only=True)
 | |
| 
 | |
|     pack_size = serializers.FloatField(label=_('Pack Quantity'))
 | |
| 
 | |
|     def __init__(self, *args, **kwargs):
 | |
|         """Initialize this serializer with extra detail fields as required"""
 | |
| 
 | |
|         # Check if 'available' quantity was supplied
 | |
|         self.has_available_quantity = 'available' in kwargs.get('data', {})
 | |
| 
 | |
|         brief = kwargs.pop('brief', False)
 | |
| 
 | |
|         detail_default = not brief
 | |
| 
 | |
|         part_detail = kwargs.pop('part_detail', detail_default)
 | |
|         supplier_detail = kwargs.pop('supplier_detail', detail_default)
 | |
|         manufacturer_detail = kwargs.pop('manufacturer_detail', detail_default)
 | |
| 
 | |
|         prettify = kwargs.pop('pretty', False)
 | |
| 
 | |
|         super().__init__(*args, **kwargs)
 | |
| 
 | |
|         if part_detail is not True:
 | |
|             self.fields.pop('part_detail')
 | |
| 
 | |
|         if supplier_detail is not True:
 | |
|             self.fields.pop('supplier_detail')
 | |
| 
 | |
|         if manufacturer_detail is not True:
 | |
|             self.fields.pop('manufacturer_detail')
 | |
|             self.fields.pop('manufacturer_part_detail')
 | |
| 
 | |
|         if prettify is not True:
 | |
|             self.fields.pop('pretty_name')
 | |
| 
 | |
|     supplier = serializers.PrimaryKeyRelatedField(queryset=Company.objects.filter(is_supplier=True))
 | |
| 
 | |
|     manufacturer = serializers.CharField(read_only=True)
 | |
| 
 | |
|     MPN = serializers.CharField(read_only=True)
 | |
| 
 | |
|     manufacturer_part_detail = ManufacturerPartSerializer(source='manufacturer_part', read_only=True)
 | |
| 
 | |
|     url = serializers.CharField(source='get_absolute_url', read_only=True)
 | |
| 
 | |
|     class Meta:
 | |
|         """Metaclass options."""
 | |
| 
 | |
|         model = SupplierPart
 | |
|         fields = [
 | |
|             'available',
 | |
|             'availability_updated',
 | |
|             'description',
 | |
|             'in_stock',
 | |
|             'link',
 | |
|             'manufacturer',
 | |
|             'manufacturer_detail',
 | |
|             'manufacturer_part',
 | |
|             'manufacturer_part_detail',
 | |
|             'MPN',
 | |
|             'note',
 | |
|             'pk',
 | |
|             'barcode_hash',
 | |
|             'packaging',
 | |
|             'pack_size',
 | |
|             'part',
 | |
|             'part_detail',
 | |
|             'pretty_name',
 | |
|             'SKU',
 | |
|             'supplier',
 | |
|             'supplier_detail',
 | |
|             'url',
 | |
|         ]
 | |
| 
 | |
|         read_only_fields = [
 | |
|             'availability_updated',
 | |
|             'barcode_hash',
 | |
|         ]
 | |
| 
 | |
|     @staticmethod
 | |
|     def annotate_queryset(queryset):
 | |
|         """Annotate the SupplierPart queryset with extra fields:
 | |
| 
 | |
|         Fields:
 | |
|             in_stock: Current stock quantity for each SupplierPart
 | |
|         """
 | |
| 
 | |
|         queryset = queryset.annotate(
 | |
|             in_stock=part.filters.annotate_total_stock()
 | |
|         )
 | |
| 
 | |
|         return queryset
 | |
| 
 | |
|     def update(self, supplier_part, data):
 | |
|         """Custom update functionality for the serializer"""
 | |
| 
 | |
|         available = data.pop('available', None)
 | |
| 
 | |
|         response = super().update(supplier_part, data)
 | |
| 
 | |
|         if available is not None and self.has_available_quantity:
 | |
|             supplier_part.update_available_quantity(available)
 | |
| 
 | |
|         return response
 | |
| 
 | |
|     def create(self, validated_data):
 | |
|         """Extract manufacturer data and process ManufacturerPart."""
 | |
| 
 | |
|         # Extract 'available' quantity from the serializer
 | |
|         available = validated_data.pop('available', None)
 | |
| 
 | |
|         # Create SupplierPart
 | |
|         supplier_part = super().create(validated_data)
 | |
| 
 | |
|         if available is not None and self.has_available_quantity:
 | |
|             supplier_part.update_available_quantity(available)
 | |
| 
 | |
|         # Get ManufacturerPart raw data (unvalidated)
 | |
|         manufacturer = self.initial_data.get('manufacturer', None)
 | |
|         MPN = self.initial_data.get('MPN', None)
 | |
| 
 | |
|         if manufacturer and MPN:
 | |
|             kwargs = {
 | |
|                 'manufacturer': manufacturer,
 | |
|                 'MPN': MPN,
 | |
|             }
 | |
|             supplier_part.save(**kwargs)
 | |
| 
 | |
|         return supplier_part
 | |
| 
 | |
| 
 | |
| class SupplierPriceBreakSerializer(InvenTreeModelSerializer):
 | |
|     """Serializer for SupplierPriceBreak object."""
 | |
| 
 | |
|     def __init__(self, *args, **kwargs):
 | |
|         """Initialize this serializer with extra fields as required"""
 | |
| 
 | |
|         supplier_detail = kwargs.pop('supplier_detail', False)
 | |
|         part_detail = kwargs.pop('part_detail', False)
 | |
| 
 | |
|         super().__init__(*args, **kwargs)
 | |
| 
 | |
|         if not supplier_detail:
 | |
|             self.fields.pop('supplier_detail')
 | |
| 
 | |
|         if not part_detail:
 | |
|             self.fields.pop('part_detail')
 | |
| 
 | |
|     quantity = InvenTreeDecimalField()
 | |
| 
 | |
|     price = InvenTreeMoneySerializer(
 | |
|         allow_null=True,
 | |
|         required=True,
 | |
|         label=_('Price'),
 | |
|     )
 | |
| 
 | |
|     price_currency = serializers.ChoiceField(
 | |
|         choices=currency_code_mappings(),
 | |
|         default=currency_code_default,
 | |
|         label=_('Currency'),
 | |
|     )
 | |
| 
 | |
|     supplier = serializers.PrimaryKeyRelatedField(source='part.supplier', many=False, read_only=True)
 | |
| 
 | |
|     supplier_detail = CompanyBriefSerializer(source='part.supplier', many=False, read_only=True)
 | |
| 
 | |
|     # Detail serializer for SupplierPart
 | |
|     part_detail = SupplierPartSerializer(source='part', brief=True, many=False, read_only=True)
 | |
| 
 | |
|     class Meta:
 | |
|         """Metaclass options."""
 | |
| 
 | |
|         model = SupplierPriceBreak
 | |
|         fields = [
 | |
|             'pk',
 | |
|             'part',
 | |
|             'part_detail',
 | |
|             'quantity',
 | |
|             'price',
 | |
|             'price_currency',
 | |
|             'supplier',
 | |
|             'supplier_detail',
 | |
|             'updated',
 | |
|         ]
 |