2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-16 03:55:41 +00:00

Migrate old units to new units, and remove old field

This commit is contained in:
Oliver
2023-05-19 14:05:24 +10:00
parent 93649b24bf
commit 258bf29331
6 changed files with 70 additions and 19 deletions

View File

@ -390,7 +390,7 @@ class SupplierPartList(ListCreateDestroyAPIView):
'manufacturer', 'manufacturer',
'MPN', 'MPN',
'packaging', 'packaging',
'pack_size', 'pack_units',
'in_stock', 'in_stock',
'updated', 'updated',
] ]

View File

@ -0,0 +1,34 @@
# 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:
sp.pack_units = str(normalize(sp.pack_size))
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'),
]
operations = [
migrations.RunPython(
code=update_supplier_part_units,
reverse_code=migrations.RunPython.noop
)
]

View File

@ -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',
),
]

View File

@ -19,13 +19,14 @@ from taggit.managers import TaggableManager
import common.models import common.models
import common.settings import common.settings
import InvenTree.conversion
import InvenTree.fields import InvenTree.fields
import InvenTree.helpers import InvenTree.helpers
import InvenTree.ready import InvenTree.ready
import InvenTree.tasks import InvenTree.tasks
import InvenTree.validators import InvenTree.validators
from common.settings import currency_code_default from common.settings import currency_code_default
from InvenTree.fields import InvenTreeURLField, RoundingDecimalField from InvenTree.fields import InvenTreeURLField
from InvenTree.models import (InvenTreeAttachment, InvenTreeBarcodeMixin, from InvenTree.models import (InvenTreeAttachment, InvenTreeBarcodeMixin,
InvenTreeNotesMixin, MetadataMixin) InvenTreeNotesMixin, MetadataMixin)
from InvenTree.status_codes import PurchaseOrderStatus from InvenTree.status_codes import PurchaseOrderStatus
@ -436,7 +437,7 @@ class SupplierPart(MetadataMixin, InvenTreeBarcodeMixin, common.models.MetaMixin
multiple: Multiple that the part is provided in multiple: Multiple that the part is provided in
lead_time: Supplier lead time lead_time: Supplier lead time
packaging: packaging that the part is supplied in, e.g. "Reel" packaging: packaging that the part is supplied in, e.g. "Reel"
pack_size: Quantity of item supplied in a single pack (e.g. 30ml in a single tube) pack_units: Quantity of item supplied in a single pack (e.g. 30ml in a single tube)
updated: Date that the SupplierPart was last updated updated: Date that the SupplierPart was last updated
""" """
@ -475,6 +476,15 @@ class SupplierPart(MetadataMixin, InvenTreeBarcodeMixin, common.models.MetaMixin
""" """
super().clean() super().clean()
# Validate that the UOM is compatible with the base part
if self.pack_units and self.part:
try:
InvenTree.conversion.convert_physical_value(self.pack_units, self.part.units)
except ValidationError as e:
raise ValidationError({
'pack_units': e.messages
})
# Ensure that the linked manufacturer_part points to the same part! # Ensure that the linked manufacturer_part points to the same part!
if self.manufacturer_part and self.part: if self.manufacturer_part and self.part:
@ -568,14 +578,6 @@ class SupplierPart(MetadataMixin, InvenTreeBarcodeMixin, common.models.MetaMixin
blank=True, blank=True,
) )
pack_size = RoundingDecimalField(
verbose_name=_('Pack Quantity'),
help_text=_('Unit quantity supplied in a single pack'),
default=1,
max_digits=15, decimal_places=5,
validators=[MinValueValidator(0.001)],
)
multiple = models.PositiveIntegerField(default=1, validators=[MinValueValidator(1)], verbose_name=_('multiple'), help_text=_('Order multiple')) multiple = models.PositiveIntegerField(default=1, validators=[MinValueValidator(1)], verbose_name=_('multiple'), help_text=_('Order multiple'))
# TODO - Reimplement lead-time as a charfield with special validation (pattern matching). # TODO - Reimplement lead-time as a charfield with special validation (pattern matching).

View File

@ -265,7 +265,7 @@ class SupplierPartSerializer(InvenTreeTagModelSerializer):
'pk', 'pk',
'barcode_hash', 'barcode_hash',
'packaging', 'packaging',
'pack_size', 'pack_units',
'part', 'part',
'part_detail', 'part_detail',
'pretty_name', 'pretty_name',
@ -327,8 +327,6 @@ class SupplierPartSerializer(InvenTreeTagModelSerializer):
pretty_name = serializers.CharField(read_only=True) pretty_name = serializers.CharField(read_only=True)
pack_size = serializers.FloatField(label=_('Pack Quantity'))
supplier = serializers.PrimaryKeyRelatedField(queryset=Company.objects.filter(is_supplier=True)) supplier = serializers.PrimaryKeyRelatedField(queryset=Company.objects.filter(is_supplier=True))
manufacturer = serializers.CharField(read_only=True) manufacturer = serializers.CharField(read_only=True)

View File

@ -138,7 +138,7 @@ function supplierPartFields(options={}) {
packaging: { packaging: {
icon: 'fa-box', icon: 'fa-box',
}, },
pack_size: {}, pack_units: {},
}; };
if (options.part) { if (options.part) {
@ -1242,14 +1242,14 @@ function loadSupplierPartTable(table, url, options) {
sortable: true, sortable: true,
}, },
{ {
field: 'pack_size', field: 'pack_units',
title: '{% trans "Pack Quantity" %}', title: '{% trans "Pack Units" %}',
sortable: true, sortable: true,
formatter: function(value, row) { formatter: function(value, row) {
var output = `${value}`; var output = `${formatDecimal(value)}`;
if (row.part_detail && row.part_detail.units) { if (row.part_detail && row.part_detail.units) {
output += ` ${row.part_detail.units}`; output += ` [${row.part_detail.units}]`;
} }
return output; return output;