mirror of
https://github.com/inventree/InvenTree.git
synced 2025-08-06 12:01:41 +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:
@@ -10,6 +10,7 @@ import common.settings
|
||||
import company.models
|
||||
import order.models
|
||||
import part.models
|
||||
import stock.models
|
||||
from InvenTree.helpers import InvenTreeTestCase
|
||||
from InvenTree.status_codes import PurchaseOrderStatus
|
||||
|
||||
@@ -234,6 +235,54 @@ class PartPricingTests(InvenTreeTestCase):
|
||||
self.assertEqual(pricing.internal_cost_max, Money(10, currency))
|
||||
self.assertEqual(pricing.overall_max, Money(10, currency))
|
||||
|
||||
def test_stock_item_pricing(self):
|
||||
"""Test for stock item pricing data"""
|
||||
|
||||
# Create a part
|
||||
p = part.models.Part.objects.create(
|
||||
name='Test part for pricing',
|
||||
description='hello world',
|
||||
)
|
||||
|
||||
# Create some stock items
|
||||
prices = [
|
||||
(10, 'AUD'),
|
||||
(5, 'USD'),
|
||||
(2, 'CAD'),
|
||||
]
|
||||
|
||||
for price, currency in prices:
|
||||
|
||||
stock.models.StockItem.objects.create(
|
||||
part=p,
|
||||
quantity=10,
|
||||
purchase_price=price,
|
||||
purchase_price_currency=currency
|
||||
)
|
||||
|
||||
# Ensure that initially, stock item pricing is disabled
|
||||
common.models.InvenTreeSetting.set_setting('PRICING_USE_STOCK_PRICING', False, None)
|
||||
|
||||
pricing = p.pricing
|
||||
pricing.update_pricing()
|
||||
|
||||
# Check that stock item pricing data is not used
|
||||
self.assertIsNone(pricing.purchase_cost_min)
|
||||
self.assertIsNone(pricing.purchase_cost_max)
|
||||
self.assertIsNone(pricing.overall_min)
|
||||
self.assertIsNone(pricing.overall_max)
|
||||
|
||||
# Turn on stock pricing
|
||||
common.models.InvenTreeSetting.set_setting('PRICING_USE_STOCK_PRICING', True, None)
|
||||
|
||||
pricing.update_pricing()
|
||||
|
||||
self.assertIsNotNone(pricing.purchase_cost_min)
|
||||
self.assertIsNotNone(pricing.purchase_cost_max)
|
||||
|
||||
self.assertEqual(pricing.overall_min, Money(1.176471, 'USD'))
|
||||
self.assertEqual(pricing.overall_max, Money(6.666667, 'USD'))
|
||||
|
||||
def test_bom_pricing(self):
|
||||
"""Unit test for BOM pricing calculations"""
|
||||
|
||||
|
Reference in New Issue
Block a user