mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-13 02:25:38 +00:00
update api to support custom status
This commit is contained in:
@ -156,7 +156,11 @@ class InvenTreeCustomStatusSerializerMixin:
|
||||
):
|
||||
setattr(self.instance, follower_field_name, self.initial_data[field])
|
||||
|
||||
# Mirror values from follower to leader
|
||||
self.mirror_follower(validated_data)
|
||||
return super().update(instance, validated_data)
|
||||
|
||||
def mirror_follower(self, validated_data):
|
||||
"""Mirror the follower fields to the leader fields."""
|
||||
for field in self._custom_fields_follower:
|
||||
leader_field_name = field.replace('_custom_key', '')
|
||||
if field in validated_data and leader_field_name not in self.initial_data:
|
||||
@ -171,7 +175,6 @@ class InvenTreeCustomStatusSerializerMixin:
|
||||
validated_data[leader_field_name] = validated_data[field]
|
||||
else:
|
||||
raise serializers.ValidationError('Invalid choice')
|
||||
return super().update(instance, validated_data)
|
||||
|
||||
def to_representation(self, instance):
|
||||
"""Ensure custom state fields are not served empty."""
|
||||
|
@ -18,6 +18,8 @@ from taggit.serializers import TagListSerializerField
|
||||
import build.models
|
||||
import company.models
|
||||
import company.serializers as company_serializers
|
||||
import generic.states.fields
|
||||
import InvenTree.fields
|
||||
import InvenTree.helpers
|
||||
import InvenTree.serializers
|
||||
import order.models
|
||||
@ -999,13 +1001,15 @@ class ReturnStockItemSerializer(serializers.Serializer):
|
||||
item.return_from_customer(location, user=request.user, notes=notes)
|
||||
|
||||
|
||||
class StockChangeStatusSerializer(serializers.Serializer):
|
||||
class StockChangeStatusSerializer(
|
||||
InvenTreeCustomStatusSerializerMixin, serializers.Serializer
|
||||
):
|
||||
"""Serializer for changing status of multiple StockItem objects."""
|
||||
|
||||
class Meta:
|
||||
"""Metaclass options."""
|
||||
|
||||
fields = ['items', 'status', 'note']
|
||||
fields = ['items', 'status', 'status_custom_key', 'note']
|
||||
|
||||
items = serializers.PrimaryKeyRelatedField(
|
||||
queryset=StockItem.objects.all(),
|
||||
@ -1029,6 +1033,14 @@ class StockChangeStatusSerializer(serializers.Serializer):
|
||||
label=_('Status'),
|
||||
)
|
||||
|
||||
status_custom_key = generic.states.fields.ExtraCustomChoiceField(
|
||||
choices=stock.status_codes.StockStatus.items(),
|
||||
default=status.default,
|
||||
choice_mdl=StockItem,
|
||||
choice_field='status',
|
||||
is_custom=True,
|
||||
)
|
||||
|
||||
note = serializers.CharField(
|
||||
label=_('Notes'),
|
||||
help_text=_('Add transaction note (optional)'),
|
||||
@ -1041,8 +1053,12 @@ class StockChangeStatusSerializer(serializers.Serializer):
|
||||
"""Save the serializer to change the status of the selected stock items."""
|
||||
data = self.validated_data
|
||||
|
||||
self.gather_custom_fields()
|
||||
self.mirror_follower(data)
|
||||
|
||||
items = data['items']
|
||||
status = data['status']
|
||||
status_custom_key = data['status_custom_key']
|
||||
|
||||
request = self.context['request']
|
||||
user = getattr(request, 'user', None)
|
||||
@ -1052,7 +1068,7 @@ class StockChangeStatusSerializer(serializers.Serializer):
|
||||
items_to_update = []
|
||||
transaction_notes = []
|
||||
|
||||
deltas = {'status': status}
|
||||
deltas = {'status': status, 'status_custom_key': status_custom_key}
|
||||
|
||||
now = InvenTree.helpers.current_time()
|
||||
|
||||
@ -1061,11 +1077,12 @@ class StockChangeStatusSerializer(serializers.Serializer):
|
||||
|
||||
for item in items:
|
||||
# Ignore items which are already in the desired status
|
||||
if item.status == status:
|
||||
if item.status == status and item.status_custom_key == status_custom_key:
|
||||
continue
|
||||
|
||||
item.updated = now
|
||||
item.status = status
|
||||
item.status_custom_key = status_custom_key
|
||||
items_to_update.append(item)
|
||||
|
||||
# Create a new transaction note for each item
|
||||
@ -1081,7 +1098,9 @@ class StockChangeStatusSerializer(serializers.Serializer):
|
||||
)
|
||||
|
||||
# Update status
|
||||
StockItem.objects.bulk_update(items_to_update, ['status', 'updated'])
|
||||
StockItem.objects.bulk_update(
|
||||
items_to_update, ['status', 'status_custom_key', 'updated']
|
||||
)
|
||||
|
||||
# Create entries
|
||||
StockItemTracking.objects.bulk_create(transaction_notes)
|
||||
|
@ -737,7 +737,7 @@ function stockChangeStatusFields(items: any[]): ApiFormFieldSet {
|
||||
},
|
||||
headers: [t`Part`, t`Location`, t`In Stock`, t`Actions`]
|
||||
},
|
||||
status: {},
|
||||
status_custom_key: {},
|
||||
note: {}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user