mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-04 20:51:00 +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:
25
InvenTree/company/migrations/0059_supplierpart_pack_units.py
Normal file
25
InvenTree/company/migrations/0059_supplierpart_pack_units.py
Normal file
@ -0,0 +1,25 @@
|
||||
# Generated by Django 3.2.19 on 2023-05-19 03:41
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
import InvenTree.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('company', '0058_auto_20230515_0004'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='supplierpart',
|
||||
name='pack_quantity',
|
||||
field=models.CharField(blank=True, help_text='Total quantity supplied in a single pack. Leave empty for single items.', max_length=25, verbose_name='Pack Quantity'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='supplierpart',
|
||||
name='pack_quantity_native',
|
||||
field=InvenTree.fields.RoundingDecimalField(decimal_places=10, default=1, max_digits=20, null=True),
|
||||
),
|
||||
]
|
51
InvenTree/company/migrations/0060_auto_20230519_0344.py
Normal file
51
InvenTree/company/migrations/0060_auto_20230519_0344.py
Normal file
@ -0,0 +1,51 @@
|
||||
# Generated by Django 3.2.19 on 2023-05-19 03:44
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
from InvenTree.helpers import normalize
|
||||
|
||||
|
||||
def update_supplier_part_units(apps, schema_editor):
|
||||
"""Migrate existing supplier part units to new field"""
|
||||
|
||||
SupplierPart = apps.get_model('company', 'SupplierPart')
|
||||
|
||||
supplier_parts = SupplierPart.objects.all()
|
||||
|
||||
for sp in supplier_parts:
|
||||
pack_size = normalize(sp.pack_size)
|
||||
sp.pack_quantity = str(pack_size)
|
||||
sp.pack_quantity_native = pack_size
|
||||
sp.save()
|
||||
|
||||
if supplier_parts.count() > 0:
|
||||
print(f"Updated {supplier_parts.count()} supplier part units")
|
||||
|
||||
|
||||
def reverse_pack_quantity(apps, schema_editor):
|
||||
"""Reverse the migrations"""
|
||||
|
||||
SupplierPart = apps.get_model('company', 'SupplierPart')
|
||||
|
||||
supplier_parts = SupplierPart.objects.all()
|
||||
|
||||
for sp in supplier_parts:
|
||||
sp.pack_size = sp.pack_quantity_native
|
||||
sp.save()
|
||||
|
||||
if supplier_parts.count() > 0:
|
||||
print(f"Updated {supplier_parts.count()} supplier part units")
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('company', '0059_supplierpart_pack_units'),
|
||||
('part', '0111_auto_20230521_1350'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(
|
||||
code=update_supplier_part_units,
|
||||
reverse_code=reverse_pack_quantity,
|
||||
)
|
||||
]
|
@ -0,0 +1,17 @@
|
||||
# Generated by Django 3.2.19 on 2023-05-19 04:03
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('company', '0060_auto_20230519_0344'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='supplierpart',
|
||||
name='pack_size',
|
||||
),
|
||||
]
|
Reference in New Issue
Block a user