2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-11-05 15:45:42 +00:00

Include stock item purchase price in pricing cache (#4292)

* Add setting to control pricing calculation from stock items

* Bug fix for displaying a "null" setting

* Add new fields to PartPricing model

* Add code for calculation of min/max stock item costs

* Update pricing display to use stock item pricing

* Add unit testing for new pricing features

* Automatically update pricing when stock item is created or edited

* Increment API version

* Improvements for price rendering

* Update based on feedback:

- Roll stock item pricing into purchase pricing
- Simplify models
- Update unit tests
This commit is contained in:
Oliver
2023-02-02 21:23:16 +11:00
committed by GitHub
parent df4209801a
commit 9a289948e5
10 changed files with 126 additions and 42 deletions

View File

@@ -6,7 +6,7 @@ import decimal
import hashlib
import logging
import os
from datetime import datetime
from datetime import datetime, timedelta
from decimal import Decimal, InvalidOperation
from django.contrib.auth.models import User
@@ -2510,6 +2510,31 @@ class PartPricing(common.models.MetaMixin):
if purchase_max is None or purchase_cost > purchase_max:
purchase_max = purchase_cost
# Also check if manual stock item pricing is included
if InvenTreeSetting.get_setting('PRICING_USE_STOCK_PRICING', True, cache=False):
items = self.part.stock_items.all()
# Limit to stock items updated within a certain window
days = int(InvenTreeSetting.get_setting('PRICING_STOCK_ITEM_AGE_DAYS', 0, cache=False))
if days > 0:
date_threshold = datetime.now().date() - timedelta(days=days)
items = items.filter(updated__gte=date_threshold)
for item in items:
cost = self.convert(item.purchase_price)
# Skip if the cost could not be converted (for some reason)
if cost is None:
continue
if purchase_min is None or cost < purchase_min:
purchase_min = cost
if purchase_max is None or cost > purchase_max:
purchase_max = cost
self.purchase_cost_min = purchase_min
self.purchase_cost_max = purchase_max
@@ -2651,6 +2676,7 @@ class PartPricing(common.models.MetaMixin):
max_costs.append(self.supplier_price_max)
if InvenTreeSetting.get_setting('PRICING_USE_VARIANT_PRICING', True, cache=False):
# Include variant pricing in overall calculations
min_costs.append(self.variant_cost_min)
max_costs.append(self.variant_cost_max)