mirror of
https://github.com/inventree/InvenTree.git
synced 2026-05-23 01:25:45 +00:00
06266b48af
* 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
403 lines
15 KiB
Python
403 lines
15 KiB
Python
"""Admin class definitions for the 'part' app"""
|
|
|
|
from django.contrib import admin
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
import import_export.widgets as widgets
|
|
from import_export.admin import ImportExportModelAdmin
|
|
from import_export.fields import Field
|
|
|
|
import part.models as models
|
|
from company.models import SupplierPart
|
|
from InvenTree.admin import InvenTreeResource
|
|
from stock.models import StockLocation
|
|
|
|
|
|
class PartResource(InvenTreeResource):
|
|
"""Class for managing Part data import/export."""
|
|
|
|
id = Field(attribute='pk', column_name=_('Part ID'), widget=widgets.IntegerWidget())
|
|
name = Field(attribute='name', column_name=_('Part Name'), widget=widgets.CharWidget())
|
|
description = Field(attribute='description', column_name=_('Part Description'), widget=widgets.CharWidget())
|
|
IPN = Field(attribute='IPN', column_name=_('IPN'), widget=widgets.CharWidget())
|
|
revision = Field(attribute='revision', column_name=_('Revision'), widget=widgets.CharWidget())
|
|
keywords = Field(attribute='keywords', column_name=_('Keywords'), widget=widgets.CharWidget())
|
|
link = Field(attribute='link', column_name=_('Link'), widget=widgets.CharWidget())
|
|
units = Field(attribute='units', column_name=_('Units'), widget=widgets.CharWidget())
|
|
notes = Field(attribute='notes', column_name=_('Notes'))
|
|
category = Field(attribute='category', column_name=_('Category ID'), widget=widgets.ForeignKeyWidget(models.PartCategory))
|
|
category_name = Field(attribute='category__name', column_name=_('Category Name'), readonly=True)
|
|
default_location = Field(attribute='default_location', column_name=_('Default Location ID'), widget=widgets.ForeignKeyWidget(StockLocation))
|
|
default_supplier = Field(attribute='default_supplier', column_name=_('Default Supplier ID'), widget=widgets.ForeignKeyWidget(SupplierPart))
|
|
variant_of = Field(attribute='variant_of', column_name=('Variant Of'), widget=widgets.ForeignKeyWidget(models.Part))
|
|
minimum_stock = Field(attribute='minimum_stock', column_name=_('Minimum Stock'))
|
|
|
|
# Part Attributes
|
|
active = Field(attribute='active', column_name=_('Active'), widget=widgets.BooleanWidget())
|
|
assembly = Field(attribute='assembly', column_name=_('Assembly'), widget=widgets.BooleanWidget())
|
|
component = Field(attribute='component', column_name=_('Component'), widget=widgets.BooleanWidget())
|
|
purchaseable = Field(attribute='purchaseable', column_name=_('Purchaseable'), widget=widgets.BooleanWidget())
|
|
salable = Field(attribute='salable', column_name=_('Salable'), widget=widgets.BooleanWidget())
|
|
is_template = Field(attribute='is_template', column_name=_('Template'), widget=widgets.BooleanWidget())
|
|
trackable = Field(attribute='trackable', column_name=_('Trackable'), widget=widgets.BooleanWidget())
|
|
virtual = Field(attribute='virtual', column_name=_('Virtual'), widget=widgets.BooleanWidget())
|
|
|
|
# Extra calculated meta-data (readonly)
|
|
suppliers = Field(attribute='supplier_count', column_name=_('Suppliers'), readonly=True)
|
|
in_stock = Field(attribute='total_stock', column_name=_('In Stock'), readonly=True, widget=widgets.IntegerWidget())
|
|
on_order = Field(attribute='on_order', column_name=_('On Order'), readonly=True, widget=widgets.IntegerWidget())
|
|
used_in = Field(attribute='used_in_count', column_name=_('Used In'), readonly=True, widget=widgets.IntegerWidget())
|
|
allocated = Field(attribute='allocation_count', column_name=_('Allocated'), readonly=True, widget=widgets.IntegerWidget())
|
|
building = Field(attribute='quantity_being_built', column_name=_('Building'), readonly=True, widget=widgets.IntegerWidget())
|
|
min_cost = Field(attribute='pricing__overall_min', column_name=_('Minimum Cost'), readonly=True)
|
|
max_cost = Field(attribute='pricing__overall_max', column_name=_('Maximum Cost'), readonly=True)
|
|
|
|
def dehydrate_min_cost(self, part):
|
|
"""Render minimum cost value for this Part"""
|
|
|
|
min_cost = part.pricing.overall_min if part.pricing else None
|
|
|
|
if min_cost is not None:
|
|
return float(min_cost.amount)
|
|
|
|
def dehydrate_max_cost(self, part):
|
|
"""Render maximum cost value for this Part"""
|
|
|
|
max_cost = part.pricing.overall_max if part.pricing else None
|
|
|
|
if max_cost is not None:
|
|
return float(max_cost.amount)
|
|
|
|
class Meta:
|
|
"""Metaclass definition"""
|
|
model = models.Part
|
|
skip_unchanged = True
|
|
report_skipped = False
|
|
clean_model_instances = True
|
|
exclude = [
|
|
'bom_checksum', 'bom_checked_by', 'bom_checked_date',
|
|
'lft', 'rght', 'tree_id', 'level',
|
|
'image',
|
|
'metadata',
|
|
'barcode_data', 'barcode_hash',
|
|
]
|
|
|
|
def get_queryset(self):
|
|
"""Prefetch related data for quicker access."""
|
|
query = super().get_queryset()
|
|
query = query.prefetch_related(
|
|
'category',
|
|
'used_in',
|
|
'builds',
|
|
'supplier_parts__purchase_order_line_items',
|
|
'stock_items__allocations'
|
|
)
|
|
|
|
return query
|
|
|
|
def after_import(self, dataset, result, using_transactions, dry_run, **kwargs):
|
|
"""Rebuild MPTT tree structure after importing Part data"""
|
|
|
|
super().after_import(dataset, result, using_transactions, dry_run, **kwargs)
|
|
|
|
# Rebuild the Part tree(s)
|
|
models.Part.objects.rebuild()
|
|
|
|
|
|
class PartAdmin(ImportExportModelAdmin):
|
|
"""Admin class for the Part model"""
|
|
|
|
resource_class = PartResource
|
|
|
|
list_display = ('full_name', 'description', 'total_stock', 'category')
|
|
|
|
list_filter = ('active', 'assembly', 'is_template', 'virtual')
|
|
|
|
search_fields = ('name', 'description', 'category__name', 'category__description', 'IPN')
|
|
|
|
autocomplete_fields = [
|
|
'variant_of',
|
|
'category',
|
|
'default_location',
|
|
'default_supplier',
|
|
]
|
|
|
|
|
|
class PartPricingAdmin(admin.ModelAdmin):
|
|
"""Admin class for PartPricing model"""
|
|
|
|
list_display = ('part', 'overall_min', 'overall_max')
|
|
|
|
autcomplete_fields = [
|
|
'part',
|
|
]
|
|
|
|
|
|
class PartCategoryResource(InvenTreeResource):
|
|
"""Class for managing PartCategory data import/export."""
|
|
|
|
id = Field(attribute='pk', column_name=_('Category ID'))
|
|
name = Field(attribute='name', column_name=_('Category Name'))
|
|
description = Field(attribute='description', column_name=_('Description'))
|
|
parent = Field(attribute='parent', column_name=_('Parent ID'), widget=widgets.ForeignKeyWidget(models.PartCategory))
|
|
parent_name = Field(attribute='parent__name', column_name=_('Parent Name'), readonly=True)
|
|
default_location = Field(attribute='default_location', column_name=_('Default Location ID'), widget=widgets.ForeignKeyWidget(StockLocation))
|
|
default_keywords = Field(attribute='default_keywords', column_name=_('Keywords'))
|
|
pathstring = Field(attribute='pathstring', column_name=_('Category Path'))
|
|
|
|
# Calculated fields
|
|
parts = Field(attribute='item_count', column_name=_('Parts'), widget=widgets.IntegerWidget(), readonly=True)
|
|
|
|
class Meta:
|
|
"""Metaclass definition"""
|
|
model = models.PartCategory
|
|
skip_unchanged = True
|
|
report_skipped = False
|
|
clean_model_instances = True
|
|
|
|
exclude = [
|
|
# Exclude MPTT internal model fields
|
|
'lft', 'rght', 'tree_id', 'level',
|
|
'metadata',
|
|
'icon',
|
|
]
|
|
|
|
def after_import(self, dataset, result, using_transactions, dry_run, **kwargs):
|
|
"""Rebuild MPTT tree structure after importing PartCategory data"""
|
|
|
|
super().after_import(dataset, result, using_transactions, dry_run, **kwargs)
|
|
|
|
# Rebuild the PartCategory tree(s)
|
|
models.PartCategory.objects.rebuild()
|
|
|
|
|
|
class PartCategoryAdmin(ImportExportModelAdmin):
|
|
"""Admin class for the PartCategory model"""
|
|
|
|
resource_class = PartCategoryResource
|
|
|
|
list_display = ('name', 'pathstring', 'description')
|
|
|
|
search_fields = ('name', 'description')
|
|
|
|
autocomplete_fields = ('parent', 'default_location',)
|
|
|
|
|
|
class PartRelatedAdmin(admin.ModelAdmin):
|
|
"""Class to manage PartRelated objects."""
|
|
|
|
autocomplete_fields = ('part_1', 'part_2')
|
|
|
|
|
|
class PartAttachmentAdmin(admin.ModelAdmin):
|
|
"""Admin class for the PartAttachment model"""
|
|
|
|
list_display = ('part', 'attachment', 'comment')
|
|
|
|
autocomplete_fields = ('part',)
|
|
|
|
|
|
class PartTestTemplateAdmin(admin.ModelAdmin):
|
|
"""Admin class for the PartTestTemplate model"""
|
|
|
|
list_display = ('part', 'test_name', 'required')
|
|
|
|
autocomplete_fields = ('part',)
|
|
|
|
|
|
class BomItemResource(InvenTreeResource):
|
|
"""Class for managing BomItem data import/export."""
|
|
|
|
level = Field(attribute='level', column_name=_('BOM Level'), readonly=True)
|
|
|
|
bom_id = Field(attribute='pk', column_name=_('BOM Item ID'))
|
|
|
|
# ID of the parent part
|
|
parent_part_id = Field(attribute='part', column_name=_('Parent ID'), widget=widgets.ForeignKeyWidget(models.Part))
|
|
parent_part_ipn = Field(attribute='part__IPN', column_name=_('Parent IPN'), readonly=True)
|
|
parent_part_name = Field(attribute='part__name', column_name=_('Parent Name'), readonly=True)
|
|
part_id = Field(attribute='sub_part', column_name=_('Part ID'), widget=widgets.ForeignKeyWidget(models.Part))
|
|
part_ipn = Field(attribute='sub_part__IPN', column_name=_('Part IPN'), readonly=True)
|
|
part_name = Field(attribute='sub_part__name', column_name=_('Part Name'), readonly=True)
|
|
part_description = Field(attribute='sub_part__description', column_name=_('Description'), readonly=True)
|
|
quantity = Field(attribute='quantity', column_name=_('Quantity'))
|
|
reference = Field(attribute='reference', column_name=_('Reference'))
|
|
note = Field(attribute='note', column_name=_('Note'))
|
|
min_cost = Field(attribute='sub_part__pricing__overall_min', column_name=_('Minimum Price'), readonly=True)
|
|
max_cost = Field(attribute='sub_part__pricing__overall_max', column_name=_('Maximum Price'), readonly=True)
|
|
|
|
sub_assembly = Field(attribute='sub_part__assembly', column_name=_('Assembly'), readonly=True)
|
|
|
|
def dehydrate_min_cost(self, item):
|
|
"""Render minimum cost value for the BOM line item"""
|
|
|
|
min_price = item.sub_part.pricing.overall_min if item.sub_part.pricing else None
|
|
|
|
if min_price is not None:
|
|
return float(min_price.amount) * float(item.quantity)
|
|
|
|
def dehydrate_max_cost(self, item):
|
|
"""Render maximum cost value for the BOM line item"""
|
|
|
|
max_price = item.sub_part.pricing.overall_max if item.sub_part.pricing else None
|
|
|
|
if max_price is not None:
|
|
return float(max_price.amount) * float(item.quantity)
|
|
|
|
def dehydrate_quantity(self, item):
|
|
"""Special consideration for the 'quantity' field on data export. We do not want a spreadsheet full of "1.0000" (we'd rather "1")
|
|
|
|
Ref: https://django-import-export.readthedocs.io/en/latest/getting_started.html#advanced-data-manipulation-on-export
|
|
"""
|
|
return float(item.quantity)
|
|
|
|
def before_export(self, queryset, *args, **kwargs):
|
|
"""Perform before exporting data"""
|
|
|
|
self.is_importing = kwargs.get('importing', False)
|
|
self.include_pricing = kwargs.pop('include_pricing', False)
|
|
|
|
def get_fields(self, **kwargs):
|
|
"""If we are exporting for the purposes of generating a 'bom-import' template, there are some fields which we are not interested in."""
|
|
fields = super().get_fields(**kwargs)
|
|
|
|
is_importing = getattr(self, 'is_importing', False)
|
|
include_pricing = getattr(self, 'include_pricing', False)
|
|
|
|
to_remove = []
|
|
|
|
if is_importing or not include_pricing:
|
|
# Remove pricing fields in this instance
|
|
to_remove += [
|
|
'sub_part__pricing__overall_min',
|
|
'sub_part__pricing__overall_max',
|
|
]
|
|
|
|
if is_importing:
|
|
to_remove += [
|
|
'level',
|
|
'pk',
|
|
'part',
|
|
'part__IPN',
|
|
'part__name',
|
|
'sub_part__name',
|
|
'sub_part__description',
|
|
'sub_part__assembly'
|
|
]
|
|
|
|
idx = 0
|
|
|
|
while idx < len(fields):
|
|
|
|
if fields[idx].attribute in to_remove:
|
|
del fields[idx]
|
|
else:
|
|
idx += 1
|
|
|
|
return fields
|
|
|
|
class Meta:
|
|
"""Metaclass definition"""
|
|
model = models.BomItem
|
|
skip_unchanged = True
|
|
report_skipped = False
|
|
clean_model_instances = True
|
|
|
|
exclude = [
|
|
'checksum',
|
|
'id',
|
|
'part',
|
|
'sub_part',
|
|
]
|
|
|
|
|
|
class BomItemAdmin(ImportExportModelAdmin):
|
|
"""Admin class for the BomItem model"""
|
|
|
|
resource_class = BomItemResource
|
|
|
|
list_display = ('part', 'sub_part', 'quantity')
|
|
|
|
search_fields = ('part__name', 'part__description', 'sub_part__name', 'sub_part__description')
|
|
|
|
autocomplete_fields = ('part', 'sub_part',)
|
|
|
|
|
|
class ParameterTemplateAdmin(ImportExportModelAdmin):
|
|
"""Admin class for the PartParameterTemplate model"""
|
|
|
|
list_display = ('name', 'units')
|
|
|
|
search_fields = ('name', 'units')
|
|
|
|
|
|
class ParameterResource(InvenTreeResource):
|
|
"""Class for managing PartParameter data import/export."""
|
|
|
|
part = Field(attribute='part', widget=widgets.ForeignKeyWidget(models.Part))
|
|
|
|
part_name = Field(attribute='part__name', readonly=True)
|
|
|
|
template = Field(attribute='template', widget=widgets.ForeignKeyWidget(models.PartParameterTemplate))
|
|
|
|
template_name = Field(attribute='template__name', readonly=True)
|
|
|
|
class Meta:
|
|
"""Metaclass definition"""
|
|
model = models.PartParameter
|
|
skip_unchanged = True
|
|
report_skipped = False
|
|
clean_model_instance = True
|
|
|
|
|
|
class ParameterAdmin(ImportExportModelAdmin):
|
|
"""Admin class for the PartParameter model"""
|
|
|
|
resource_class = ParameterResource
|
|
|
|
list_display = ('part', 'template', 'data')
|
|
|
|
autocomplete_fields = ('part', 'template')
|
|
|
|
|
|
class PartCategoryParameterAdmin(admin.ModelAdmin):
|
|
"""Admin class for the PartCategoryParameterTemplate model"""
|
|
|
|
autocomplete_fields = ('category', 'parameter_template',)
|
|
|
|
|
|
class PartSellPriceBreakAdmin(admin.ModelAdmin):
|
|
"""Admin class for the PartSellPriceBreak model"""
|
|
|
|
class Meta:
|
|
"""Metaclass definition"""
|
|
model = models.PartSellPriceBreak
|
|
|
|
list_display = ('part', 'quantity', 'price',)
|
|
|
|
|
|
class PartInternalPriceBreakAdmin(admin.ModelAdmin):
|
|
"""Admin class for the PartInternalPriceBreak model"""
|
|
|
|
class Meta:
|
|
"""Metaclass definition"""
|
|
model = models.PartInternalPriceBreak
|
|
|
|
list_display = ('part', 'quantity', 'price',)
|
|
|
|
autocomplete_fields = ('part',)
|
|
|
|
|
|
admin.site.register(models.Part, PartAdmin)
|
|
admin.site.register(models.PartCategory, PartCategoryAdmin)
|
|
admin.site.register(models.PartRelated, PartRelatedAdmin)
|
|
admin.site.register(models.PartAttachment, PartAttachmentAdmin)
|
|
admin.site.register(models.BomItem, BomItemAdmin)
|
|
admin.site.register(models.PartParameterTemplate, ParameterTemplateAdmin)
|
|
admin.site.register(models.PartParameter, ParameterAdmin)
|
|
admin.site.register(models.PartCategoryParameterTemplate, PartCategoryParameterAdmin)
|
|
admin.site.register(models.PartTestTemplate, PartTestTemplateAdmin)
|
|
admin.site.register(models.PartSellPriceBreak, PartSellPriceBreakAdmin)
|
|
admin.site.register(models.PartInternalPriceBreak, PartInternalPriceBreakAdmin)
|
|
admin.site.register(models.PartPricing, PartPricingAdmin)
|