mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-01 03:00:54 +00:00
Part units (#4854)
* Add validation to part units field * Add "pack_units" field to the SupplierPart model * Migrate old units to new units, and remove old field * Table fix * Fixture fix * Update migration * Improve "hook" for loading custom unit database * Display part units column in part table - Also allow ordering by part units - Allow filtering to show parts which have defined units * Adds data migration for converting units to valid values * Add "pack_units_native" field to company.SupplierPart model * Clean pack units when saving a SupplierPart - Convert to native part units - Handle empty units value - Add unit tests * Add background function to rebuild supplier parts when a part is saved - Required to ensure that the "pack_size_native" is up to date * Template updates * Sort by native units first * Bump API version * Rename "pack_units" to "pack_quantity" * Update migration file - Allow reverse migration * Fix for currency migration - Handle case where no currencies are provided - Handle case where base currency is not in provided options * Adds unit test for data migration * Add unit test for part.units data migration - Check that units fields are updated correctly * Add some extra "default units" - each / piece - dozen / hundred / thousand - Add unit testing also * Update references to "pack_size" - Replace with "pack_quantity" or "pack_quantity_native" as appropriate * Improvements based on unit testing * catch error * Docs updates * Fixes for pricing tests * Update unit tests for part migrations · 1b6b6d9d * Bug fix for conversion code * javascript updates * JS formatting fix
This commit is contained in:
@ -613,7 +613,7 @@ class StockList(APIDownloadMixin, ListCreateDestroyAPIView):
|
||||
'supplier_part': _('The given supplier part does not exist'),
|
||||
})
|
||||
|
||||
if supplier_part.pack_size != 1:
|
||||
if supplier_part.base_quantity() != 1:
|
||||
# Skip this check if pack size is 1 - makes no difference
|
||||
# use_pack_size = True -> Multiply quantity by pack size
|
||||
# use_pack_size = False -> Use quantity as is
|
||||
@ -623,10 +623,9 @@ class StockList(APIDownloadMixin, ListCreateDestroyAPIView):
|
||||
})
|
||||
else:
|
||||
if bool(data.get('use_pack_size')):
|
||||
data['quantity'] = int(quantity) * float(supplier_part.pack_size)
|
||||
quantity = data.get('quantity', None)
|
||||
quantity = data['quantity'] = supplier_part.base_quantity(quantity)
|
||||
# Divide purchase price by pack size, to save correct price per stock item
|
||||
data['purchase_price'] = float(data['purchase_price']) / float(supplier_part.pack_size)
|
||||
data['purchase_price'] = float(data['purchase_price']) / float(supplier_part.pack_quantity_native)
|
||||
|
||||
# Now remove the flag from data, so that it doesn't interfere with saving
|
||||
# Do this regardless of results above
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
import logging
|
||||
|
||||
from django.core.exceptions import FieldError
|
||||
from django.db import migrations
|
||||
|
||||
logger = logging.getLogger('inventree')
|
||||
@ -38,10 +39,13 @@ def fix_purchase_price(apps, schema_editor):
|
||||
supplier_part=None
|
||||
).exclude(
|
||||
purchase_price=None
|
||||
).exclude(
|
||||
supplier_part__pack_size=1
|
||||
)
|
||||
|
||||
try:
|
||||
items = items.exclude(supplier_part__pack_size=1)
|
||||
except FieldError:
|
||||
pass
|
||||
|
||||
n_updated = 0
|
||||
|
||||
for item in items:
|
||||
|
@ -698,6 +698,7 @@ class StockItemTest(StockAPITestCase):
|
||||
},
|
||||
expected_code=201
|
||||
)
|
||||
|
||||
# Reload part, count stock again
|
||||
part_4 = part.models.Part.objects.get(pk=4)
|
||||
self.assertEqual(part_4.available_stock, current_count + 3)
|
||||
|
Reference in New Issue
Block a user