2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-02 03:30:54 +00:00

Part pricing cache (#3710)

* 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
This commit is contained in:
Oliver
2022-11-14 15:58:22 +11:00
committed by GitHub
parent 8ceb1af3c3
commit 06266b48af
69 changed files with 3747 additions and 1629 deletions

View File

@ -445,6 +445,19 @@ class PurchaseOrderLineItemFilter(rest_filters.FilterSet):
return queryset
has_pricing = rest_filters.BooleanFilter(label="Has Pricing", method='filter_has_pricing')
def filter_has_pricing(self, queryset, name, value):
"""Filter by whether or not the line item has pricing information"""
value = str2bool(value)
if value:
queryset = queryset.exclude(purchase_price=None)
else:
queryset = queryset.filter(purchase_price=None)
return queryset
class PurchaseOrderLineItemList(APIDownloadMixin, ListCreateAPI):
"""API endpoint for accessing a list of PurchaseOrderLineItem objects.
@ -776,6 +789,22 @@ class SalesOrderLineItemFilter(rest_filters.FilterSet):
'part',
]
has_pricing = rest_filters.BooleanFilter(label="Has Pricing", method='filter_has_pricing')
def filter_has_pricing(self, queryset, name, value):
"""Filter by whether or not the line item has pricing information"""
value = str2bool(value)
if value:
queryset = queryset.exclude(sale_price=None)
else:
queryset = queryset.filter(sale_price=None)
return queryset
order_status = rest_filters.NumberFilter(label='Order Status', field_name='order__status')
completed = rest_filters.BooleanFilter(label='completed', method='filter_completed')
def filter_completed(self, queryset, name, value):
@ -810,6 +839,8 @@ class SalesOrderLineItemList(ListCreateAPI):
kwargs['part_detail'] = str2bool(params.get('part_detail', False))
kwargs['order_detail'] = str2bool(params.get('order_detail', False))
kwargs['allocations'] = str2bool(params.get('allocations', False))
kwargs['customer_detail'] = str2bool(params.get('customer_detail', False))
except AttributeError:
pass
@ -853,11 +884,6 @@ class SalesOrderLineItemList(ListCreateAPI):
'reference',
]
filterset_fields = [
'order',
'part',
]
class SalesOrderExtraLineList(GeneralExtraLineList, ListCreateAPI):
"""API endpoint for accessing a list of SalesOrderExtraLine objects."""

View File

@ -0,0 +1,35 @@
# Generated by Django 3.2.16 on 2022-11-11 01:53
import InvenTree.fields
from django.db import migrations
import djmoney.models.validators
class Migration(migrations.Migration):
dependencies = [
('order', '0075_auto_20221110_0108'),
]
operations = [
migrations.AlterField(
model_name='purchaseorderextraline',
name='price',
field=InvenTree.fields.InvenTreeModelMoneyField(blank=True, currency_choices=[], decimal_places=6, default_currency='', help_text='Unit price', max_digits=19, null=True, validators=[djmoney.models.validators.MinMoneyValidator(0)], verbose_name='Price'),
),
migrations.AlterField(
model_name='purchaseorderlineitem',
name='purchase_price',
field=InvenTree.fields.InvenTreeModelMoneyField(blank=True, currency_choices=[], decimal_places=6, default_currency='', help_text='Unit purchase price', max_digits=19, null=True, validators=[djmoney.models.validators.MinMoneyValidator(0)], verbose_name='Purchase Price'),
),
migrations.AlterField(
model_name='salesorderextraline',
name='price',
field=InvenTree.fields.InvenTreeModelMoneyField(blank=True, currency_choices=[], decimal_places=6, default_currency='', help_text='Unit price', max_digits=19, null=True, validators=[djmoney.models.validators.MinMoneyValidator(0)], verbose_name='Price'),
),
migrations.AlterField(
model_name='salesorderlineitem',
name='sale_price',
field=InvenTree.fields.InvenTreeModelMoneyField(blank=True, currency_choices=[], decimal_places=6, default_currency='', help_text='Unit sale price', max_digits=19, null=True, validators=[djmoney.models.validators.MinMoneyValidator(0)], verbose_name='Sale Price'),
),
]

View File

@ -24,6 +24,7 @@ from mptt.models import TreeForeignKey
import InvenTree.helpers
import InvenTree.ready
import InvenTree.tasks
import order.validators
from common.notifications import InvenTreeNotificationBodies
from common.settings import currency_code_default
@ -311,6 +312,9 @@ class PurchaseOrder(Order):
reference (str, optional): Reference to item. Defaults to ''.
purchase_price (optional): Price of item. Defaults to None.
Returns:
The newly created PurchaseOrderLineItem instance
Raises:
ValidationError: quantity is smaller than 0
ValidationError: quantity is not type int
@ -338,11 +342,13 @@ class PurchaseOrder(Order):
quantity_new = line.quantity + quantity
line.quantity = quantity_new
supplier_price = supplier_part.get_price(quantity_new)
if line.purchase_price and supplier_price:
line.purchase_price = supplier_price / quantity_new
line.save()
return
return line
line = PurchaseOrderLineItem(
order=self,
@ -354,6 +360,8 @@ class PurchaseOrder(Order):
line.save()
return line
@transaction.atomic
def place_order(self):
"""Marks the PurchaseOrder as PLACED.
@ -376,8 +384,14 @@ class PurchaseOrder(Order):
if self.status == PurchaseOrderStatus.PLACED:
self.status = PurchaseOrderStatus.COMPLETE
self.complete_date = datetime.now().date()
self.save()
# Schedule pricing update for any referenced parts
for line in self.lines.all():
if line.part and line.part.part:
line.part.part.pricing.schedule_for_update()
trigger_event('purchaseorder.completed', id=self.pk)
@property
@ -762,6 +776,10 @@ class SalesOrder(Order):
self.save()
# Schedule pricing update for any referenced parts
for line in self.lines.all():
line.part.pricing.schedule_for_update()
trigger_event('salesorder.completed', id=self.pk)
return True
@ -951,7 +969,7 @@ class OrderExtraLine(OrderLineItem):
price = InvenTreeModelMoneyField(
max_digits=19,
decimal_places=4,
decimal_places=6,
null=True, blank=True,
allow_negative=True,
verbose_name=_('Price'),
@ -1031,7 +1049,7 @@ class PurchaseOrderLineItem(OrderLineItem):
purchase_price = InvenTreeModelMoneyField(
max_digits=19,
decimal_places=4,
decimal_places=6,
null=True, blank=True,
verbose_name=_('Purchase Price'),
help_text=_('Unit purchase price'),
@ -1137,7 +1155,7 @@ class SalesOrderLineItem(OrderLineItem):
sale_price = InvenTreeModelMoneyField(
max_digits=19,
decimal_places=4,
decimal_places=6,
null=True, blank=True,
verbose_name=_('Sale Price'),
help_text=_('Unit sale price'),

View File

@ -39,8 +39,6 @@ class AbstractOrderSerializer(serializers.Serializer):
read_only=True,
)
total_price_string = serializers.CharField(source='get_total_price', read_only=True)
class AbstractExtraLineSerializer(serializers.Serializer):
"""Abstract Serializer for a ExtraLine object."""
@ -60,8 +58,6 @@ class AbstractExtraLineSerializer(serializers.Serializer):
allow_null=True
)
price_string = serializers.CharField(source='price', read_only=True)
price_currency = serializers.ChoiceField(
choices=currency_code_mappings(),
help_text=_('Price currency'),
@ -81,7 +77,6 @@ class AbstractExtraLineMeta:
'order_detail',
'price',
'price_currency',
'price_string',
]
@ -164,7 +159,6 @@ class PurchaseOrderSerializer(AbstractOrderSerializer, InvenTreeModelSerializer)
'target_date',
'notes',
'total_price',
'total_price_string',
]
read_only_fields = [
@ -326,8 +320,6 @@ class PurchaseOrderLineItemSerializer(InvenTreeModelSerializer):
allow_null=True
)
purchase_price_string = serializers.CharField(source='purchase_price', read_only=True)
destination_detail = stock.serializers.LocationBriefSerializer(source='get_destination', read_only=True)
purchase_price_currency = serializers.ChoiceField(
@ -387,7 +379,6 @@ class PurchaseOrderLineItemSerializer(InvenTreeModelSerializer):
'received',
'purchase_price',
'purchase_price_currency',
'purchase_price_string',
'destination',
'destination_detail',
'target_date',
@ -745,7 +736,6 @@ class SalesOrderSerializer(AbstractOrderSerializer, InvenTreeModelSerializer):
'shipment_date',
'target_date',
'total_price',
'total_price_string',
]
read_only_fields = [
@ -870,6 +860,7 @@ class SalesOrderLineItemSerializer(InvenTreeModelSerializer):
part_detail = kwargs.pop('part_detail', False)
order_detail = kwargs.pop('order_detail', False)
allocations = kwargs.pop('allocations', False)
customer_detail = kwargs.pop('customer_detail', False)
super().__init__(*args, **kwargs)
@ -882,6 +873,10 @@ class SalesOrderLineItemSerializer(InvenTreeModelSerializer):
if allocations is not True:
self.fields.pop('allocations')
if customer_detail is not True:
self.fields.pop('customer_detail')
customer_detail = CompanyBriefSerializer(source='order.customer', many=False, read_only=True)
order_detail = SalesOrderSerializer(source='order', many=False, read_only=True)
part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
allocations = SalesOrderAllocationSerializer(many=True, read_only=True, location_detail=True)
@ -900,8 +895,6 @@ class SalesOrderLineItemSerializer(InvenTreeModelSerializer):
allow_null=True
)
sale_price_string = serializers.CharField(source='sale_price', read_only=True)
sale_price_currency = serializers.ChoiceField(
choices=currency_code_mappings(),
help_text=_('Sale price currency'),
@ -917,6 +910,7 @@ class SalesOrderLineItemSerializer(InvenTreeModelSerializer):
'allocated',
'allocations',
'available_stock',
'customer_detail',
'quantity',
'reference',
'notes',
@ -927,7 +921,6 @@ class SalesOrderLineItemSerializer(InvenTreeModelSerializer):
'part_detail',
'sale_price',
'sale_price_currency',
'sale_price_string',
'shipped',
'target_date',
]