mirror of
https://github.com/inventree/InvenTree.git
synced 2026-06-12 11:38:47 +00:00
Transfer Order (#11281)
* initial skel commit for transfer orders * initial transfer order backend model * add some serializers, rename PLACED to ISSUED for TransferOrders * adding from admin console works * simple table list almost working, but we need to add order line items.... * add other cols to table * add Transfer Order from table view * moving towards a detail view * wip: adding detail view * add take from and destination serializer details * add other detail grid items * edit/duplicate transfer order * more action buttons * first crack at adding line items * add to line item * add filters * starting work on row actions * more action buttons for line items * fix copy lines in duplicate * basic allocation works * allocations table actions * allocate serials * allocated serial row expansion * add transferred qty to serializers * move items on complete, show in tracking * change panel to transferred stock upon complete * allow incomplete line items * disable edit allocations when completed * add ref pattern and to settings * add admin to line item inline * add calendar and parametric view * basic transfer order report * add transfer order ruleset * starting allocation buisness logic throughout for TOs * disable accept incomplete logic, which was incorrect, until I fix * fix incomplete allocation option * add transferred col to default report * add transfer order to calendar ics view * chain condition for readability * add transfer order allocations table to stockitem view * don't account TO allocations in availability * add transfer orders table for a part * 'consume' option by doing take_stock * squash migrations * starting to test transfer order * more transfer order tests * add transfer order consume test * wip, more tests * more transfer order tests * had to refresh_from_db * switch "to" to "transfer-order" in url paths * only select non-virtual parts from transfer order * add transfer order docs * deconflict migrations * fix frontend build error * fix validation on transfer order reference pattern * add oath2 scope for transfer order * fix state test to include transfer order state * add barcode_model_type_code for transfer order * bump api version * check view role for transfer order, remove debug/commented out lines * add serialized allocation test * Fix migrations * Frontend fixes * Implement required 'company' attribute * transfer order report context * attempt to fix tests * delete transfer order allocations on cancel * add a few playwright tests, more incoming * more playwright * add source and destination locations to table * deconflict migrations * Fix build issue * attempt to fix flaky transfer order test * duplicate transfer order before running tests * Adjust playwright tests * Fix migration dependency order --------- Co-authored-by: Oliver <oliver.henry.walters@gmail.com> Co-authored-by: Matthias Mair <code@mjmair.com>
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
"""InvenTree API version information."""
|
||||
|
||||
# InvenTree API version
|
||||
INVENTREE_API_VERSION = 491
|
||||
INVENTREE_API_VERSION = 492
|
||||
"""Increment this API version number whenever there is a significant change to the API that any clients need to know about."""
|
||||
|
||||
INVENTREE_API_TEXT = """
|
||||
|
||||
v492 -> 2026-05-22 : https://github.com/inventree/InvenTree/pull/11281
|
||||
- Add Transfer Order model and associated API endpoint
|
||||
|
||||
v491 -> 2026-05-21 : https://github.com/inventree/InvenTree/pull/11979
|
||||
- Add API serializer for deleting a part category
|
||||
- Add API serializer for deleting a stock location
|
||||
|
||||
@@ -904,6 +904,26 @@ SYSTEM_SETTINGS: dict[str, InvenTreeSettingsKeyType] = {
|
||||
'default': False,
|
||||
'validator': bool,
|
||||
},
|
||||
'TRANSFERORDER_ENABLED': {
|
||||
'name': _('Enable Transfer Orders'),
|
||||
'description': _('Enable transfer order functionality in the user interface'),
|
||||
'validator': bool,
|
||||
'default': False,
|
||||
},
|
||||
'TRANSFERORDER_REFERENCE_PATTERN': {
|
||||
'name': _('Transfer Order Reference Pattern'),
|
||||
'description': _(
|
||||
'Required pattern for generating Transfer Order reference field'
|
||||
),
|
||||
'default': 'TO-{ref:04d}',
|
||||
'validator': order.validators.validate_transfer_order_reference_pattern,
|
||||
},
|
||||
'TRANSFERORDER_REQUIRE_RESPONSIBLE': {
|
||||
'name': _('Require Responsible Owner'),
|
||||
'description': _('A responsible owner must be assigned to each order'),
|
||||
'default': False,
|
||||
'validator': bool,
|
||||
},
|
||||
'SALESORDER_BLOCK_INCOMPLETE_ITEM_TESTS': {
|
||||
'name': _('Block Incomplete Item Tests'),
|
||||
'description': _(
|
||||
|
||||
@@ -232,8 +232,8 @@ class ApiTests(InvenTreeAPITestCase):
|
||||
"""Test the API endpoint for listing all status models."""
|
||||
response = self.get(reverse('api-status-all'))
|
||||
|
||||
# 10 built-in state classes, plus the added GeneralState class
|
||||
self.assertEqual(len(response.data), 11)
|
||||
# 11 built-in state classes, plus the added GeneralState class
|
||||
self.assertEqual(len(response.data), 12)
|
||||
|
||||
# Test the BuildStatus model
|
||||
build_status = response.data['BuildStatus']
|
||||
@@ -273,7 +273,7 @@ class ApiTests(InvenTreeAPITestCase):
|
||||
)
|
||||
response = self.get(reverse('api-status-all'))
|
||||
|
||||
self.assertEqual(len(response.data), 11)
|
||||
self.assertEqual(len(response.data), 12)
|
||||
|
||||
stock_status_cstm = response.data['StockStatus']
|
||||
self.assertEqual(stock_status_cstm['status_class'], 'StockStatus')
|
||||
|
||||
@@ -170,3 +170,41 @@ class ReturnOrderLineItemAdmin(admin.ModelAdmin):
|
||||
@admin.register(models.ReturnOrderExtraLine)
|
||||
class ReturnOrdeerExtraLineAdmin(GeneralExtraLineAdmin, admin.ModelAdmin):
|
||||
"""Admin class for the ReturnOrderExtraLine model."""
|
||||
|
||||
|
||||
class TransferOrderLineItemInlineAdmin(admin.StackedInline):
|
||||
"""Inline admin class for the TransferOrderLineItem model."""
|
||||
|
||||
autocomplete_fields = ['part']
|
||||
|
||||
model = models.TransferOrderLineItem
|
||||
extra = 0
|
||||
|
||||
|
||||
@admin.register(models.TransferOrder)
|
||||
class TransferOrderAdmin(admin.ModelAdmin):
|
||||
"""Admin class for the TransferOrder model."""
|
||||
|
||||
exclude = ['reference_int', 'address', 'contact']
|
||||
|
||||
list_display = (
|
||||
'reference',
|
||||
'status',
|
||||
'description',
|
||||
'take_from',
|
||||
'destination',
|
||||
'consume',
|
||||
'creation_date',
|
||||
)
|
||||
|
||||
search_fields = ['reference', 'description']
|
||||
|
||||
inlines = [TransferOrderLineItemInlineAdmin]
|
||||
|
||||
autocomplete_fields = [
|
||||
'created_by',
|
||||
'take_from',
|
||||
'destination',
|
||||
'project_code',
|
||||
'responsible',
|
||||
]
|
||||
|
||||
@@ -56,6 +56,8 @@ from order.status_codes import (
|
||||
ReturnOrderStatus,
|
||||
SalesOrderStatus,
|
||||
SalesOrderStatusGroups,
|
||||
TransferOrderStatus,
|
||||
TransferOrderStatusGroups,
|
||||
)
|
||||
from part.models import Part
|
||||
from users.models import Owner
|
||||
@@ -1766,6 +1768,521 @@ class ReturnOrderExtraLineDetail(RetrieveUpdateDestroyAPI):
|
||||
serializer_class = serializers.ReturnOrderExtraLineSerializer
|
||||
|
||||
|
||||
class TransferOrderFilter(OrderFilter):
|
||||
"""Custom API filters for the TransferOrderList endpoint."""
|
||||
|
||||
class Meta:
|
||||
"""Metaclass options."""
|
||||
|
||||
model = models.TransferOrder
|
||||
fields = []
|
||||
|
||||
include_variants = rest_filters.BooleanFilter(
|
||||
label=_('Include Variants'), method='filter_include_variants'
|
||||
)
|
||||
|
||||
def filter_include_variants(self, queryset, name, value):
|
||||
"""Filter by whether or not to include variants of the selected part.
|
||||
|
||||
Note:
|
||||
- This filter does nothing by itself, and requires the 'part' filter to be set.
|
||||
- Refer to the 'filter_part' method for more information.
|
||||
"""
|
||||
return queryset
|
||||
|
||||
part = rest_filters.ModelChoiceFilter(
|
||||
queryset=Part.objects.all(), field_name='part', method='filter_part'
|
||||
)
|
||||
|
||||
@extend_schema_field(OpenApiTypes.INT)
|
||||
def filter_part(self, queryset, name, part):
|
||||
"""Filter by selected 'part'.
|
||||
|
||||
Note:
|
||||
- If 'include_variants' is set to True, then all variants of the selected part will be included.
|
||||
- Otherwise, just filter by the selected part.
|
||||
"""
|
||||
include_variants = str2bool(self.data.get('include_variants', False))
|
||||
|
||||
if include_variants:
|
||||
parts = part.get_descendants(include_self=True)
|
||||
else:
|
||||
parts = Part.objects.filter(pk=part.pk)
|
||||
|
||||
# Now that we have a queryset of parts, find all the matching return orders
|
||||
line_items = models.TransferOrderLineItem.objects.filter(part__in=parts)
|
||||
|
||||
# Generate a list of ID values for the matching transfer orders
|
||||
transfer_orders = line_items.values_list('order', flat=True).distinct()
|
||||
|
||||
# Now we have a list of matching IDs, filter the queryset
|
||||
return queryset.filter(pk__in=transfer_orders)
|
||||
|
||||
completed_before = InvenTreeDateFilter(
|
||||
label=_('Completed Before'), field_name='complete_date', lookup_expr='lt'
|
||||
)
|
||||
|
||||
completed_after = InvenTreeDateFilter(
|
||||
label=_('Completed After'), field_name='complete_date', lookup_expr='gt'
|
||||
)
|
||||
|
||||
|
||||
class TransferOrderMixin(SerializerContextMixin):
|
||||
"""Mixin class for TransferOrder endpoints."""
|
||||
|
||||
queryset = models.TransferOrder.objects.all()
|
||||
serializer_class = serializers.TransferOrderSerializer
|
||||
|
||||
def get_queryset(self, *args, **kwargs):
|
||||
"""Return annotated queryset for this endpoint."""
|
||||
queryset = super().get_queryset(*args, **kwargs)
|
||||
queryset = serializers.TransferOrderSerializer.annotate_queryset(queryset)
|
||||
queryset = queryset.prefetch_related('created_by', 'responsible')
|
||||
|
||||
return queryset
|
||||
|
||||
|
||||
class TransferOrderList(
|
||||
TransferOrderMixin,
|
||||
OrderCreateMixin,
|
||||
DataExportViewMixin,
|
||||
OutputOptionsMixin,
|
||||
ParameterListMixin,
|
||||
ListCreateAPI,
|
||||
):
|
||||
"""API endpoint for accessing a list of TransferOrder objects."""
|
||||
|
||||
filterset_class = TransferOrderFilter
|
||||
filter_backends = SEARCH_ORDER_FILTER
|
||||
|
||||
# TODO:
|
||||
# output_options = TransferOrderOutputOptions
|
||||
|
||||
ordering_field_aliases = {
|
||||
'reference': ['reference_int', 'reference'],
|
||||
'project_code': ['project_code__code'],
|
||||
}
|
||||
|
||||
ordering_fields = [
|
||||
'creation_date',
|
||||
'created_by',
|
||||
'reference',
|
||||
'line_items',
|
||||
'status',
|
||||
'start_date',
|
||||
'target_date',
|
||||
'complete_date',
|
||||
'project_code',
|
||||
]
|
||||
|
||||
search_fields = ['reference', 'description', 'project_code__code']
|
||||
|
||||
ordering = '-reference'
|
||||
|
||||
|
||||
class TransferOrderDetail(
|
||||
TransferOrderMixin, OutputOptionsMixin, RetrieveUpdateDestroyAPI
|
||||
):
|
||||
"""API endpoint for detail view of a single TransferOrder object."""
|
||||
|
||||
# output_options = TransferOrderOutputOptions
|
||||
|
||||
|
||||
class TransferOrderContextMixin:
|
||||
"""Simple mixin class to add a TransferOrder to the serializer context."""
|
||||
|
||||
queryset = models.TransferOrder.objects.all()
|
||||
|
||||
def get_serializer_context(self):
|
||||
"""Add the TransferOrder object to the serializer context."""
|
||||
context = super().get_serializer_context()
|
||||
|
||||
# Pass the Transfer instance through to the serializer for validation
|
||||
try:
|
||||
context['order'] = models.TransferOrder.objects.get(
|
||||
pk=self.kwargs.get('pk', None)
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
context['request'] = self.request
|
||||
|
||||
return context
|
||||
|
||||
|
||||
class TransferOrderCancel(TransferOrderContextMixin, CreateAPI):
|
||||
"""API endpoint to cancel a TransferOrder."""
|
||||
|
||||
serializer_class = serializers.TransferOrderCancelSerializer
|
||||
|
||||
|
||||
class TransferOrderHold(TransferOrderContextMixin, CreateAPI):
|
||||
"""API endpoint to hold a TransferOrder."""
|
||||
|
||||
serializer_class = serializers.TransferOrderHoldSerializer
|
||||
|
||||
|
||||
class TransferOrderComplete(TransferOrderContextMixin, CreateAPI):
|
||||
"""API endpoint to complete a TransferOrder."""
|
||||
|
||||
serializer_class = serializers.TransferOrderCompleteSerializer
|
||||
|
||||
|
||||
class TransferOrderIssue(TransferOrderContextMixin, CreateAPI):
|
||||
"""API endpoint to issue a Transfer Order."""
|
||||
|
||||
serializer_class = serializers.TransferOrderIssueSerializer
|
||||
|
||||
|
||||
class TransferOrderAllocateSerials(TransferOrderContextMixin, CreateAPI):
|
||||
"""API endpoint to allocation stock items against a TransferOrder, by specifying serial numbers."""
|
||||
|
||||
queryset = models.TransferOrder.objects.none()
|
||||
serializer_class = serializers.TransferOrderSerialAllocationSerializer
|
||||
|
||||
|
||||
class TransferOrderAllocate(TransferOrderContextMixin, CreateAPI):
|
||||
"""API endpoint to allocate stock items against a TransferOrder.
|
||||
|
||||
- The TransferOrder is specified in the URL
|
||||
- See the TransferOrderAllocationSerializer class
|
||||
"""
|
||||
|
||||
queryset = models.TransferOrder.objects.none()
|
||||
serializer_class = serializers.TransferOrderLineItemAllocationSerializer
|
||||
|
||||
|
||||
class TransferOrderAllocationFilter(FilterSet):
|
||||
"""Custom filterset for the TransferOrderAllocationList endpoint."""
|
||||
|
||||
class Meta:
|
||||
"""Metaclass options."""
|
||||
|
||||
model = models.TransferOrderAllocation
|
||||
fields = ['line', 'item']
|
||||
|
||||
order = rest_filters.ModelChoiceFilter(
|
||||
queryset=models.TransferOrder.objects.all(),
|
||||
field_name='line__order',
|
||||
label=_('Order'),
|
||||
)
|
||||
|
||||
include_variants = rest_filters.BooleanFilter(
|
||||
label=_('Include Variants'), method='filter_include_variants'
|
||||
)
|
||||
|
||||
def filter_include_variants(self, queryset, name, value):
|
||||
"""Filter by whether or not to include variants of the selected part.
|
||||
|
||||
Note:
|
||||
- This filter does nothing by itself, and requires the 'part' filter to be set.
|
||||
- Refer to the 'filter_part' method for more information.
|
||||
"""
|
||||
return queryset
|
||||
|
||||
part = rest_filters.ModelChoiceFilter(
|
||||
queryset=Part.objects.all(), method='filter_part', label=_('Part')
|
||||
)
|
||||
|
||||
@extend_schema_field(rest_framework.serializers.IntegerField(help_text=_('Part')))
|
||||
def filter_part(self, queryset, name, part):
|
||||
"""Filter by the 'part' attribute.
|
||||
|
||||
Note:
|
||||
- If "include_variants" is True, include all variants of the selected part
|
||||
- Otherwise, just filter by the selected part
|
||||
"""
|
||||
include_variants = str2bool(self.data.get('include_variants', False))
|
||||
|
||||
if include_variants:
|
||||
parts = part.get_descendants(include_self=True)
|
||||
return queryset.filter(item__part__in=parts)
|
||||
else:
|
||||
return queryset.filter(item__part=part)
|
||||
|
||||
outstanding = rest_filters.BooleanFilter(
|
||||
label=_('Outstanding'), method='filter_outstanding'
|
||||
)
|
||||
|
||||
def filter_outstanding(self, queryset, name, value):
|
||||
"""Filter by "outstanding" status (boolean)."""
|
||||
if str2bool(value):
|
||||
return queryset.filter(
|
||||
line__order__status__in=TransferOrderStatusGroups.OPEN
|
||||
# TODO: is there an additional filter here if we aren't using a "shipment"
|
||||
# shipment__shipment_date=None,
|
||||
)
|
||||
return queryset.exclude(
|
||||
# TODO: is there an additional filter here if we aren't using a "shipment"
|
||||
# shipment__shipment_date=None,
|
||||
line__order__status__in=TransferOrderStatusGroups.OPEN
|
||||
)
|
||||
|
||||
location = rest_filters.ModelChoiceFilter(
|
||||
queryset=stock_models.StockLocation.objects.all(),
|
||||
label=_('Location'),
|
||||
method='filter_location',
|
||||
)
|
||||
|
||||
@extend_schema_field(
|
||||
rest_framework.serializers.IntegerField(help_text=_('Location'))
|
||||
)
|
||||
def filter_location(self, queryset, name, location):
|
||||
"""Filter by the location of the allocated StockItem."""
|
||||
locations = location.get_descendants(include_self=True)
|
||||
return queryset.filter(item__location__in=locations)
|
||||
|
||||
|
||||
class TransferOrderAllocationMixin:
|
||||
"""Mixin class for TransferOrderAllocation endpoints."""
|
||||
|
||||
queryset = models.TransferOrderAllocation.objects.all()
|
||||
serializer_class = serializers.TransferOrderAllocationSerializer
|
||||
|
||||
def get_queryset(self, *args, **kwargs):
|
||||
"""Annotate the queryset for this endpoint."""
|
||||
queryset = super().get_queryset(*args, **kwargs)
|
||||
|
||||
queryset = queryset.prefetch_related(
|
||||
'item',
|
||||
'item__sales_order',
|
||||
'item__part',
|
||||
'line__part',
|
||||
'item__location',
|
||||
'line__order',
|
||||
'line__order__responsible',
|
||||
'line__order__project_code',
|
||||
'line__order__project_code__responsible',
|
||||
).select_related('line__part__pricing_data', 'item__part__pricing_data')
|
||||
|
||||
return queryset
|
||||
|
||||
|
||||
class TransferOrderAllocationOutputOptions(OutputConfiguration):
|
||||
"""Output options for the TransferOrderAllocation endpoint."""
|
||||
|
||||
OPTIONS = [
|
||||
InvenTreeOutputOption('part_detail'),
|
||||
InvenTreeOutputOption('item_detail'),
|
||||
InvenTreeOutputOption('order_detail'),
|
||||
InvenTreeOutputOption('location_detail'),
|
||||
]
|
||||
|
||||
|
||||
class TransferOrderAllocationList(
|
||||
TransferOrderAllocationMixin, BulkUpdateMixin, OutputOptionsMixin, ListAPI
|
||||
):
|
||||
"""API endpoint for listing TransferOrderAllocation objects."""
|
||||
|
||||
filterset_class = TransferOrderAllocationFilter
|
||||
filter_backends = SEARCH_ORDER_FILTER
|
||||
output_options = TransferOrderAllocationOutputOptions
|
||||
|
||||
ordering_fields = [
|
||||
'quantity',
|
||||
'part',
|
||||
'serial',
|
||||
'IPN',
|
||||
'batch',
|
||||
'location',
|
||||
'order',
|
||||
]
|
||||
|
||||
ordering_field_aliases = {
|
||||
'IPN': 'item__part__IPN',
|
||||
'part': 'item__part__name',
|
||||
'serial': ['item__serial_int', 'item__serial'],
|
||||
'batch': 'item__batch',
|
||||
'location': 'item__location__name',
|
||||
'order': 'line__order__reference',
|
||||
}
|
||||
|
||||
search_fields = {
|
||||
'item__part__name',
|
||||
'item__part__IPN',
|
||||
'item__serial',
|
||||
'item__batch',
|
||||
}
|
||||
|
||||
|
||||
class TransferOrderAllocationDetail(
|
||||
TransferOrderAllocationMixin, RetrieveUpdateDestroyAPI
|
||||
):
|
||||
"""API endpoint for detail view of a TransferOrderAllocation object."""
|
||||
|
||||
|
||||
class TransferOrderLineItemFilter(LineItemFilter):
|
||||
"""Custom filters for TransferOrderLineItemList endpoint."""
|
||||
|
||||
class Meta:
|
||||
"""Metaclass options."""
|
||||
|
||||
model = models.TransferOrderLineItem
|
||||
fields = []
|
||||
|
||||
order = rest_filters.ModelChoiceFilter(
|
||||
queryset=models.TransferOrder.objects.all(),
|
||||
field_name='order',
|
||||
label=_('Order'),
|
||||
)
|
||||
|
||||
def filter_include_variants(self, queryset, name, value):
|
||||
"""Filter by whether or not to include variants of the selected part.
|
||||
|
||||
Note:
|
||||
- This filter does nothing by itself, and requires the 'part' filter to be set.
|
||||
- Refer to the 'filter_part' method for more information.
|
||||
"""
|
||||
return queryset
|
||||
|
||||
part = rest_filters.ModelChoiceFilter(
|
||||
queryset=Part.objects.all(),
|
||||
field_name='part',
|
||||
label=_('Part'),
|
||||
method='filter_part',
|
||||
)
|
||||
|
||||
@extend_schema_field(OpenApiTypes.INT)
|
||||
def filter_part(self, queryset, name, part):
|
||||
"""Filter TransferOrderLineItem by selected 'part'.
|
||||
|
||||
Note:
|
||||
- If 'include_variants' is set to True, then all variants of the selected part will be included.
|
||||
- Otherwise, just filter by the selected part.
|
||||
"""
|
||||
include_variants = str2bool(self.data.get('include_variants', False))
|
||||
|
||||
# Construct a queryset of parts to filter by
|
||||
if include_variants:
|
||||
parts = part.get_descendants(include_self=True)
|
||||
else:
|
||||
parts = Part.objects.filter(pk=part.pk)
|
||||
|
||||
return queryset.filter(part__in=parts)
|
||||
|
||||
allocated = rest_filters.BooleanFilter(
|
||||
label=_('Allocated'), method='filter_allocated'
|
||||
)
|
||||
|
||||
def filter_allocated(self, queryset, name, value):
|
||||
"""Filter by lines which are 'allocated'.
|
||||
|
||||
A line is 'allocated' when allocated >= quantity
|
||||
"""
|
||||
q = Q(allocated__gte=F('quantity'))
|
||||
|
||||
if str2bool(value):
|
||||
return queryset.filter(q)
|
||||
return queryset.exclude(q)
|
||||
|
||||
completed = rest_filters.BooleanFilter(
|
||||
label=_('Completed'), method='filter_completed'
|
||||
)
|
||||
|
||||
def filter_completed(self, queryset, name, value):
|
||||
"""Filter by lines which are "completed".
|
||||
|
||||
A line is 'completed' when transferred >= quantity
|
||||
"""
|
||||
q = Q(transferred__gte=F('quantity'))
|
||||
|
||||
if str2bool(value):
|
||||
return queryset.filter(q)
|
||||
return queryset.exclude(q)
|
||||
|
||||
order_complete = rest_filters.BooleanFilter(
|
||||
label=_('Order Complete'), method='filter_order_complete'
|
||||
)
|
||||
|
||||
def filter_order_complete(self, queryset, name, value):
|
||||
"""Filter by whether the order is 'complete' or not."""
|
||||
if str2bool(value):
|
||||
return queryset.filter(order__status__in=TransferOrderStatusGroups.COMPLETE)
|
||||
|
||||
return queryset.exclude(order__status__in=TransferOrderStatusGroups.COMPLETE)
|
||||
|
||||
order_outstanding = rest_filters.BooleanFilter(
|
||||
label=_('Order Outstanding'), method='filter_order_outstanding'
|
||||
)
|
||||
|
||||
def filter_order_outstanding(self, queryset, name, value):
|
||||
"""Filter by whether the order is 'outstanding' or not."""
|
||||
if str2bool(value):
|
||||
return queryset.filter(order__status__in=TransferOrderStatusGroups.OPEN)
|
||||
|
||||
return queryset.exclude(order__status__in=TransferOrderStatusGroups.OPEN)
|
||||
|
||||
|
||||
class TransferOrderLineItemMixin(SerializerContextMixin):
|
||||
"""Mixin class for TransferOrderLineItem endpoints."""
|
||||
|
||||
queryset = models.TransferOrderLineItem.objects.all()
|
||||
serializer_class = serializers.TransferOrderLineItemSerializer
|
||||
|
||||
def get_queryset(self, *args, **kwargs):
|
||||
"""Return annotated queryset for this endpoint."""
|
||||
queryset = super().get_queryset(*args, **kwargs)
|
||||
|
||||
queryset = queryset.prefetch_related(
|
||||
'part',
|
||||
'allocations',
|
||||
# 'allocations__transfer',
|
||||
'allocations__item__part',
|
||||
'allocations__item__location',
|
||||
'order',
|
||||
)
|
||||
|
||||
queryset = serializers.TransferOrderLineItemSerializer.annotate_queryset(
|
||||
queryset
|
||||
)
|
||||
|
||||
return queryset
|
||||
|
||||
|
||||
class TransferOrderLineItemOutputOptions(OutputConfiguration):
|
||||
"""Output options for the TransferOrderAllocation endpoint."""
|
||||
|
||||
OPTIONS = [
|
||||
InvenTreeOutputOption('part_detail'),
|
||||
InvenTreeOutputOption('order_detail'),
|
||||
]
|
||||
|
||||
|
||||
class TransferOrderLineItemList(
|
||||
TransferOrderLineItemMixin, DataExportViewMixin, OutputOptionsMixin, ListCreateAPI
|
||||
):
|
||||
"""API endpoint for accessing a list of TransferOrderLineItem objects."""
|
||||
|
||||
filterset_class = TransferOrderLineItemFilter
|
||||
|
||||
filter_backends = SEARCH_ORDER_FILTER
|
||||
|
||||
output_options = TransferOrderLineItemOutputOptions
|
||||
|
||||
ordering_fields = [
|
||||
'order',
|
||||
'part',
|
||||
'part__name',
|
||||
'quantity',
|
||||
'allocated',
|
||||
'transferred',
|
||||
'reference',
|
||||
'target_date',
|
||||
]
|
||||
|
||||
ordering_field_aliases = {'part': 'part__name', 'order': 'order__reference'}
|
||||
|
||||
search_fields = ['part__name', 'quantity', 'reference']
|
||||
|
||||
|
||||
class TransferOrderLineItemDetail(
|
||||
TransferOrderLineItemMixin, OutputOptionsMixin, RetrieveUpdateDestroyAPI
|
||||
):
|
||||
"""API endpoint for detail view of a TransferOrderLineItem object."""
|
||||
|
||||
output_options = TransferOrderLineItemOutputOptions
|
||||
|
||||
|
||||
class OrderCalendarExport(ICalFeed):
|
||||
"""Calendar export for Purchase/Sales Orders.
|
||||
|
||||
@@ -1844,6 +2361,8 @@ class OrderCalendarExport(ICalFeed):
|
||||
ordertype_title = _('Sales Order')
|
||||
elif obj['ordertype'] == 'return-order':
|
||||
ordertype_title = _('Return Order')
|
||||
elif obj['ordertype'] == 'transfer-order':
|
||||
ordertype_title = _('Transfer Order')
|
||||
else:
|
||||
ordertype_title = _('Unknown')
|
||||
|
||||
@@ -1889,6 +2408,15 @@ class OrderCalendarExport(ICalFeed):
|
||||
).filter(status__lt=ReturnOrderStatus.COMPLETE.value)
|
||||
else:
|
||||
outlist = models.ReturnOrder.objects.filter(target_date__isnull=False)
|
||||
elif obj['ordertype'] == 'transfer-order':
|
||||
if obj['include_completed'] is False:
|
||||
# Do not include completed orders from list in this case
|
||||
# Complete status = 30
|
||||
outlist = models.TransferOrder.objects.filter(
|
||||
target_date__isnull=False
|
||||
).filter(status__lt=TransferOrderStatus.COMPLETE.value)
|
||||
else:
|
||||
outlist = models.TransferOrder.objects.filter(target_date__isnull=False)
|
||||
else:
|
||||
outlist = []
|
||||
|
||||
@@ -1900,7 +2428,12 @@ class OrderCalendarExport(ICalFeed):
|
||||
|
||||
def item_description(self, item):
|
||||
"""Set the event description."""
|
||||
return f'Company: {item.company.name}\nStatus: {item.get_status_display()}\nDescription: {item.description}'
|
||||
if hasattr(item, 'company') and item.company:
|
||||
return f'Company: {item.company.name}\nStatus: {item.get_status_display()}\nDescription: {item.description}'
|
||||
else:
|
||||
return (
|
||||
f'Status: {item.get_status_display()}\nDescription: {item.description}'
|
||||
)
|
||||
|
||||
def item_start_datetime(self, item):
|
||||
"""Set event start to target date. Goal is all-day event."""
|
||||
@@ -2216,9 +2749,97 @@ order_api_urls = [
|
||||
),
|
||||
]),
|
||||
),
|
||||
# API endpoints for transfer orders
|
||||
path(
|
||||
'transfer-order/',
|
||||
include([
|
||||
# Transfer Order detail endpoints
|
||||
path(
|
||||
'<int:pk>/',
|
||||
include([
|
||||
path(
|
||||
'allocate/',
|
||||
TransferOrderAllocate.as_view(),
|
||||
name='api-transfer-order-allocate',
|
||||
),
|
||||
path(
|
||||
'allocate-serials/',
|
||||
TransferOrderAllocateSerials.as_view(),
|
||||
name='api-transfer-order-allocate-serials',
|
||||
),
|
||||
path(
|
||||
'cancel/',
|
||||
TransferOrderCancel.as_view(),
|
||||
name='api-transfer-order-cancel',
|
||||
),
|
||||
path(
|
||||
'hold/',
|
||||
TransferOrderHold.as_view(),
|
||||
name='api-transfer-order-hold',
|
||||
),
|
||||
path(
|
||||
'complete/',
|
||||
TransferOrderComplete.as_view(),
|
||||
name='api-transfer-order-complete',
|
||||
),
|
||||
path(
|
||||
'issue/',
|
||||
TransferOrderIssue.as_view(),
|
||||
name='api-transfer-order-issue',
|
||||
),
|
||||
meta_path(models.TransferOrder),
|
||||
path(
|
||||
'',
|
||||
TransferOrderDetail.as_view(),
|
||||
name='api-transfer-order-detail',
|
||||
),
|
||||
]),
|
||||
),
|
||||
# Transfer Order list
|
||||
path('', TransferOrderList.as_view(), name='api-transfer-order-list'),
|
||||
]),
|
||||
),
|
||||
# API endpoints for transfer order line items
|
||||
path(
|
||||
'transfer-order-line/',
|
||||
include([
|
||||
path(
|
||||
'<int:pk>/',
|
||||
include([
|
||||
meta_path(models.TransferOrderLineItem),
|
||||
path(
|
||||
'',
|
||||
TransferOrderLineItemDetail.as_view(),
|
||||
name='api-transfer-order-line-detail',
|
||||
),
|
||||
]),
|
||||
),
|
||||
path(
|
||||
'',
|
||||
TransferOrderLineItemList.as_view(),
|
||||
name='api-transfer-order-line-list',
|
||||
),
|
||||
]),
|
||||
),
|
||||
# API endpoints for sales order allocations
|
||||
path(
|
||||
'transfer-order-allocation/',
|
||||
include([
|
||||
path(
|
||||
'<int:pk>/',
|
||||
TransferOrderAllocationDetail.as_view(),
|
||||
name='api-transfer-order-allocation-detail',
|
||||
),
|
||||
path(
|
||||
'',
|
||||
TransferOrderAllocationList.as_view(),
|
||||
name='api-transfer-order-allocation-list',
|
||||
),
|
||||
]),
|
||||
),
|
||||
# API endpoint for subscribing to ICS calendar of purchase/sales/return orders
|
||||
re_path(
|
||||
r'^calendar/(?P<ordertype>purchase-order|sales-order|return-order)/calendar.ics',
|
||||
r'^calendar/(?P<ordertype>purchase-order|sales-order|return-order|transfer-order)/calendar.ics',
|
||||
OrderCalendarExport(),
|
||||
name='api-po-so-calendar',
|
||||
),
|
||||
|
||||
@@ -37,3 +37,12 @@ class ReturnOrderEvents(BaseEventEnum):
|
||||
COMPLETED = 'returnorder.completed'
|
||||
CANCELLED = 'returnorder.cancelled'
|
||||
HOLD = 'returnorder.hold'
|
||||
|
||||
|
||||
class TransferOrderEvents(BaseEventEnum):
|
||||
"""Event enumeration for the PurchaseOrder models."""
|
||||
|
||||
ISSUED = 'transferorder.placed'
|
||||
COMPLETED = 'transferorder.completed'
|
||||
CANCELLED = 'transferorder.cancelled'
|
||||
HOLD = 'transferorder.hold'
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
- model: order.transferorder
|
||||
pk: 1
|
||||
fields:
|
||||
reference: 'TO-123'
|
||||
description: "One transfer order, please"
|
||||
status: 10 # Pending
|
||||
|
||||
- model: order.transferorder
|
||||
pk: 2
|
||||
fields:
|
||||
reference: 'TO-124'
|
||||
description: "One transfer order, please"
|
||||
status: 40 # Cancelled
|
||||
|
||||
- model: order.transferorder
|
||||
pk: 3
|
||||
fields:
|
||||
reference: 'TO-125'
|
||||
description: "One transfer order, please"
|
||||
status: 25 # On Hold
|
||||
|
||||
- model: order.transferorder
|
||||
pk: 4
|
||||
fields:
|
||||
reference: 'TO-126'
|
||||
description: "One transfer order, please"
|
||||
status: 20 # Issued
|
||||
|
||||
- model: order.transferorder
|
||||
pk: 5
|
||||
fields:
|
||||
reference: 'TO-127'
|
||||
description: "One transfer order, please"
|
||||
status: 30 # Complete
|
||||
|
||||
|
||||
# Line items for transfer orders
|
||||
- model: order.transferorderlineitem
|
||||
pk: 1
|
||||
fields:
|
||||
order: 5 # the completed order
|
||||
part: 10001 # blue chair
|
||||
quantity: 1
|
||||
|
||||
- model: order.transferorderlineitem
|
||||
pk: 2
|
||||
fields:
|
||||
order: 4 # the issued order
|
||||
part: 10001 # blue chair
|
||||
quantity: 1
|
||||
transferred: 1
|
||||
|
||||
# Allocations for transfer orders
|
||||
# an 'allocated' allocation
|
||||
- model: order.transferorderallocation
|
||||
pk: 1
|
||||
fields:
|
||||
line: 1 # the line item on the completed order
|
||||
item: 1 # stock item
|
||||
quantity: 1
|
||||
|
||||
# a 'complete' allocation
|
||||
- model: order.transferorderallocation
|
||||
pk: 2
|
||||
fields:
|
||||
line: 2 # the line item on the issued order
|
||||
item: 500 # stock item for the blue chair
|
||||
quantity: 1
|
||||
@@ -0,0 +1,482 @@
|
||||
# Generated by Django 5.2.11 on 2026-02-27 22:00
|
||||
|
||||
import InvenTree.fields
|
||||
import InvenTree.models
|
||||
import django.core.validators
|
||||
import django.db.models.deletion
|
||||
import generic.states.fields
|
||||
import generic.states.states
|
||||
import generic.states.transition
|
||||
import generic.states.validators
|
||||
import order.status_codes
|
||||
import order.validators
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("common", "0041_auto_20251203_1244"),
|
||||
("company", "0077_delete_manufacturerpartparameter"),
|
||||
("order", "0117_purchaseorderextraline_line_int_and_more"),
|
||||
("part", "0146_auto_20251203_1241"),
|
||||
("stock", "0116_alter_stockitem_link"),
|
||||
("users", "0005_owner_model"),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="TransferOrder",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.AutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
(
|
||||
"metadata",
|
||||
models.JSONField(
|
||||
blank=True,
|
||||
help_text="JSON metadata field, for use by external plugins",
|
||||
null=True,
|
||||
verbose_name="Plugin Metadata",
|
||||
),
|
||||
),
|
||||
("reference_int", models.BigIntegerField(default=0)),
|
||||
(
|
||||
"notes",
|
||||
InvenTree.fields.InvenTreeNotesField(
|
||||
blank=True,
|
||||
help_text="Markdown notes (optional)",
|
||||
max_length=50000,
|
||||
null=True,
|
||||
verbose_name="Notes",
|
||||
),
|
||||
),
|
||||
(
|
||||
"barcode_data",
|
||||
models.CharField(
|
||||
blank=True,
|
||||
help_text="Third party barcode data",
|
||||
max_length=500,
|
||||
verbose_name="Barcode Data",
|
||||
),
|
||||
),
|
||||
(
|
||||
"barcode_hash",
|
||||
models.CharField(
|
||||
blank=True,
|
||||
help_text="Unique hash of barcode data",
|
||||
max_length=128,
|
||||
verbose_name="Barcode Hash",
|
||||
),
|
||||
),
|
||||
(
|
||||
"description",
|
||||
models.CharField(
|
||||
blank=True,
|
||||
help_text="Order description (optional)",
|
||||
max_length=250,
|
||||
verbose_name="Description",
|
||||
),
|
||||
),
|
||||
(
|
||||
"link",
|
||||
InvenTree.fields.InvenTreeURLField(
|
||||
blank=True,
|
||||
help_text="Link to external page",
|
||||
max_length=2000,
|
||||
verbose_name="Link",
|
||||
),
|
||||
),
|
||||
(
|
||||
"start_date",
|
||||
models.DateField(
|
||||
blank=True,
|
||||
help_text="Scheduled start date for this order",
|
||||
null=True,
|
||||
verbose_name="Start date",
|
||||
),
|
||||
),
|
||||
(
|
||||
"target_date",
|
||||
models.DateField(
|
||||
blank=True,
|
||||
help_text="Expected date for order delivery. Order will be overdue after this date.",
|
||||
null=True,
|
||||
verbose_name="Target Date",
|
||||
),
|
||||
),
|
||||
(
|
||||
"creation_date",
|
||||
models.DateField(
|
||||
blank=True, null=True, verbose_name="Creation Date"
|
||||
),
|
||||
),
|
||||
(
|
||||
"issue_date",
|
||||
models.DateField(
|
||||
blank=True,
|
||||
help_text="Date order was issued",
|
||||
null=True,
|
||||
verbose_name="Issue Date",
|
||||
),
|
||||
),
|
||||
(
|
||||
"reference",
|
||||
models.CharField(
|
||||
default=order.validators.generate_next_transfer_order_reference,
|
||||
help_text="Transfer Order Reference",
|
||||
max_length=64,
|
||||
unique=True,
|
||||
validators=[order.validators.validate_transfer_order_reference],
|
||||
verbose_name="Reference",
|
||||
),
|
||||
),
|
||||
(
|
||||
"consume",
|
||||
models.BooleanField(
|
||||
default=False,
|
||||
help_text='Rather than transfer the stock to the destination, "consume" it, by removing transferred quantity from the allocated stock item',
|
||||
verbose_name="Consume Stock",
|
||||
),
|
||||
),
|
||||
(
|
||||
"complete_date",
|
||||
models.DateField(
|
||||
blank=True,
|
||||
help_text="Date order was completed",
|
||||
null=True,
|
||||
verbose_name="Completion Date",
|
||||
),
|
||||
),
|
||||
(
|
||||
"status_custom_key",
|
||||
generic.states.fields.ExtraInvenTreeCustomStatusModelField(
|
||||
blank=True,
|
||||
default=None,
|
||||
help_text="Additional status information for this item",
|
||||
null=True,
|
||||
validators=[
|
||||
generic.states.validators.CustomStatusCodeValidator(
|
||||
status_class=order.status_codes.TransferOrderStatus
|
||||
)
|
||||
],
|
||||
verbose_name="Custom status key",
|
||||
),
|
||||
),
|
||||
(
|
||||
"status",
|
||||
generic.states.fields.InvenTreeCustomStatusModelField(
|
||||
choices=[
|
||||
(10, "Pending"),
|
||||
(20, "Issued"),
|
||||
(25, "On Hold"),
|
||||
(30, "Complete"),
|
||||
(40, "Cancelled"),
|
||||
],
|
||||
default=10,
|
||||
help_text="Transfer order status",
|
||||
validators=[
|
||||
generic.states.validators.CustomStatusCodeValidator(
|
||||
status_class=order.status_codes.TransferOrderStatus
|
||||
)
|
||||
],
|
||||
verbose_name="Status",
|
||||
),
|
||||
),
|
||||
(
|
||||
"address",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
help_text="Company address for this order",
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.SET_NULL,
|
||||
related_name="+",
|
||||
to="company.address",
|
||||
verbose_name="Address",
|
||||
),
|
||||
),
|
||||
(
|
||||
"contact",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
help_text="Point of contact for this order",
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.SET_NULL,
|
||||
related_name="+",
|
||||
to="company.contact",
|
||||
verbose_name="Contact",
|
||||
),
|
||||
),
|
||||
(
|
||||
"created_by",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.SET_NULL,
|
||||
related_name="+",
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
verbose_name="Created By",
|
||||
),
|
||||
),
|
||||
(
|
||||
"destination",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
help_text="Destination for transferred items",
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.SET_NULL,
|
||||
related_name="incoming_transfers",
|
||||
to="stock.stocklocation",
|
||||
verbose_name="Destination Location",
|
||||
),
|
||||
),
|
||||
(
|
||||
"project_code",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
help_text="Select project code for this order",
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.SET_NULL,
|
||||
to="common.projectcode",
|
||||
verbose_name="Project Code",
|
||||
),
|
||||
),
|
||||
(
|
||||
"responsible",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
help_text="User or group responsible for this order",
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.SET_NULL,
|
||||
related_name="+",
|
||||
to="users.owner",
|
||||
verbose_name="Responsible",
|
||||
),
|
||||
),
|
||||
(
|
||||
"take_from",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
help_text="Source for transferred items",
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.SET_NULL,
|
||||
related_name="sourcing_transfers",
|
||||
to="stock.stocklocation",
|
||||
verbose_name="Source Location",
|
||||
),
|
||||
),
|
||||
(
|
||||
"updated_at",
|
||||
models.DateTimeField(
|
||||
blank=True,
|
||||
help_text="Timestamp of last update",
|
||||
null=True,
|
||||
verbose_name="Updated At",
|
||||
),
|
||||
)
|
||||
],
|
||||
options={
|
||||
"verbose_name": "Transfer Order",
|
||||
},
|
||||
bases=(
|
||||
generic.states.states.StatusCodeMixin,
|
||||
generic.states.transition.StateTransitionMixin,
|
||||
InvenTree.models.InvenTreeAttachmentMixin,
|
||||
InvenTree.models.InvenTreePermissionCheckMixin,
|
||||
InvenTree.models.ContentTypeMixin,
|
||||
InvenTree.models.PluginValidationMixin,
|
||||
models.Model,
|
||||
),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="TransferOrderLineItem",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.AutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
(
|
||||
"metadata",
|
||||
models.JSONField(
|
||||
blank=True,
|
||||
help_text="JSON metadata field, for use by external plugins",
|
||||
null=True,
|
||||
verbose_name="Plugin Metadata",
|
||||
),
|
||||
),
|
||||
(
|
||||
"quantity",
|
||||
InvenTree.fields.RoundingDecimalField(
|
||||
decimal_places=5,
|
||||
default=1,
|
||||
help_text="Item quantity",
|
||||
max_digits=15,
|
||||
validators=[django.core.validators.MinValueValidator(0)],
|
||||
verbose_name="Quantity",
|
||||
),
|
||||
),
|
||||
(
|
||||
"reference",
|
||||
models.CharField(
|
||||
blank=True,
|
||||
help_text="Line item reference",
|
||||
max_length=100,
|
||||
verbose_name="Reference",
|
||||
),
|
||||
),
|
||||
(
|
||||
"notes",
|
||||
models.CharField(
|
||||
blank=True,
|
||||
help_text="Line item notes",
|
||||
max_length=500,
|
||||
verbose_name="Notes",
|
||||
),
|
||||
),
|
||||
(
|
||||
"link",
|
||||
InvenTree.fields.InvenTreeURLField(
|
||||
blank=True,
|
||||
help_text="Link to external page",
|
||||
max_length=2000,
|
||||
verbose_name="Link",
|
||||
),
|
||||
),
|
||||
(
|
||||
"target_date",
|
||||
models.DateField(
|
||||
blank=True,
|
||||
help_text="Target date for this line item (leave blank to use the target date from the order)",
|
||||
null=True,
|
||||
verbose_name="Target Date",
|
||||
),
|
||||
),
|
||||
(
|
||||
"order",
|
||||
models.ForeignKey(
|
||||
help_text="Transfer Order",
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="lines",
|
||||
to="order.transferorder",
|
||||
verbose_name="Order",
|
||||
),
|
||||
),
|
||||
(
|
||||
"part",
|
||||
models.ForeignKey(
|
||||
help_text="Part",
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.SET_NULL,
|
||||
related_name="transfer_order_line_items",
|
||||
to="part.part",
|
||||
verbose_name="Part",
|
||||
),
|
||||
),
|
||||
(
|
||||
"project_code",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
help_text="Select project code for this order",
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.SET_NULL,
|
||||
to="common.projectcode",
|
||||
verbose_name="Project Code",
|
||||
),
|
||||
),
|
||||
(
|
||||
"transferred",
|
||||
InvenTree.fields.RoundingDecimalField(
|
||||
decimal_places=5,
|
||||
default=0,
|
||||
help_text="transferred quantity",
|
||||
max_digits=15,
|
||||
validators=[django.core.validators.MinValueValidator(0)],
|
||||
verbose_name="transferred",
|
||||
),
|
||||
),
|
||||
(
|
||||
"line",
|
||||
models.CharField(
|
||||
blank=True,
|
||||
default="",
|
||||
help_text="Line number for this item (optional)",
|
||||
max_length=20,
|
||||
verbose_name="Line Number",
|
||||
),
|
||||
)
|
||||
],
|
||||
options={
|
||||
"verbose_name": "Transfer Order Line Item",
|
||||
},
|
||||
bases=(
|
||||
InvenTree.models.ContentTypeMixin,
|
||||
InvenTree.models.PluginValidationMixin,
|
||||
models.Model,
|
||||
),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="TransferOrderAllocation",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.AutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
(
|
||||
"quantity",
|
||||
InvenTree.fields.RoundingDecimalField(
|
||||
decimal_places=5,
|
||||
default=1,
|
||||
help_text="Enter stock allocation quantity",
|
||||
max_digits=15,
|
||||
validators=[django.core.validators.MinValueValidator(0)],
|
||||
verbose_name="Quantity",
|
||||
),
|
||||
),
|
||||
(
|
||||
"item",
|
||||
models.ForeignKey(
|
||||
help_text="Select stock item to allocate",
|
||||
limit_choices_to={
|
||||
"belongs_to": None,
|
||||
"part__virtual": False,
|
||||
"sales_order": None,
|
||||
},
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="transfer_order_allocations",
|
||||
to="stock.stockitem",
|
||||
verbose_name="Item",
|
||||
),
|
||||
),
|
||||
(
|
||||
"line",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="allocations",
|
||||
to="order.transferorderlineitem",
|
||||
verbose_name="Line",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"verbose_name": "Transfer Order Allocation",
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.2.13 on 2026-05-11 21:37
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("order", "0118_transferorder"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="transferorderlineitem",
|
||||
name="line_int",
|
||||
field=models.IntegerField(default=0),
|
||||
),
|
||||
]
|
||||
@@ -45,7 +45,12 @@ from InvenTree.fields import (
|
||||
)
|
||||
from InvenTree.helpers import decimal2string, pui_url
|
||||
from InvenTree.helpers_model import notify_responsible
|
||||
from order.events import PurchaseOrderEvents, ReturnOrderEvents, SalesOrderEvents
|
||||
from order.events import (
|
||||
PurchaseOrderEvents,
|
||||
ReturnOrderEvents,
|
||||
SalesOrderEvents,
|
||||
TransferOrderEvents,
|
||||
)
|
||||
from order.status_codes import (
|
||||
PurchaseOrderStatus,
|
||||
PurchaseOrderStatusGroups,
|
||||
@@ -54,6 +59,8 @@ from order.status_codes import (
|
||||
ReturnOrderStatusGroups,
|
||||
SalesOrderStatus,
|
||||
SalesOrderStatusGroups,
|
||||
TransferOrderStatus,
|
||||
TransferOrderStatusGroups,
|
||||
)
|
||||
from part import models as PartModels
|
||||
from plugin.events import trigger_event
|
||||
@@ -265,6 +272,27 @@ class ReturnOrderReportContext(report.mixins.BaseReportContext, TypedDict):
|
||||
customer: Optional[Company]
|
||||
|
||||
|
||||
class TransferOrderReportContext(report.mixins.BaseReportContext, TypedDict):
|
||||
"""Context for the transfer order model.
|
||||
|
||||
Attributes:
|
||||
description: The description field of the TransferOrder
|
||||
reference: The reference field of the TransferOrder
|
||||
title: The title (string representation) of the TransferOrder
|
||||
lines: Query set of all line items associated with the TransferOrder
|
||||
order: The TransferOrder instance itself
|
||||
"""
|
||||
|
||||
description: str
|
||||
reference: str
|
||||
title: str
|
||||
lines: report.mixins.QuerySet['TransferOrderLineItem']
|
||||
order: 'TransferOrder'
|
||||
take_from: 'stock.models.StockLocation'
|
||||
destination: 'stock.models.StockLocation'
|
||||
consume: bool
|
||||
|
||||
|
||||
class Order(
|
||||
StatusCodeMixin,
|
||||
StateTransitionMixin,
|
||||
@@ -374,11 +402,16 @@ class Order(
|
||||
})
|
||||
|
||||
# Check that the referenced 'contact' matches the correct 'company'
|
||||
if self.company and self.contact:
|
||||
if self.contact.company != self.company:
|
||||
raise ValidationError({
|
||||
'contact': _('Contact does not match selected company')
|
||||
})
|
||||
if (
|
||||
hasattr(self, 'company')
|
||||
and hasattr(self, 'contact')
|
||||
and self.company
|
||||
and self.contact
|
||||
and (self.contact.company != self.company)
|
||||
):
|
||||
raise ValidationError({
|
||||
'contact': _('Contact does not match selected company')
|
||||
})
|
||||
|
||||
# Target date should be *after* the start date
|
||||
if self.start_date and self.target_date and self.start_date > self.target_date:
|
||||
@@ -388,11 +421,15 @@ class Order(
|
||||
})
|
||||
|
||||
# Check that the referenced 'address' matches the correct 'company'
|
||||
if self.company and self.address:
|
||||
if self.address.company != self.company:
|
||||
raise ValidationError({
|
||||
'address': _('Address does not match selected company')
|
||||
})
|
||||
if (
|
||||
hasattr(self, 'company')
|
||||
and self.company
|
||||
and self.address
|
||||
and (self.address.company != self.company)
|
||||
):
|
||||
raise ValidationError({
|
||||
'address': _('Address does not match selected company')
|
||||
})
|
||||
|
||||
def clean_line_item(self, line):
|
||||
"""Clean a line item for this order.
|
||||
@@ -408,7 +445,9 @@ class Order(
|
||||
"""Generate context data for the reporting interface."""
|
||||
return {
|
||||
'description': self.description,
|
||||
'extra_lines': self.extra_lines,
|
||||
'extra_lines': getattr(
|
||||
self, 'extra_lines', None
|
||||
), # Transfer Order doesn't have extra lines
|
||||
'lines': self.lines,
|
||||
'order': self,
|
||||
'reference': self.reference,
|
||||
@@ -3155,6 +3194,628 @@ class ReturnOrderExtraLine(OrderExtraLine):
|
||||
)
|
||||
|
||||
|
||||
class TransferOrder(Order):
|
||||
"""A Transfer Order represents a request to transfer stock from one location to another. It provides a place to queue and review changes before execution.
|
||||
|
||||
Attributes:
|
||||
take_from: The stock location to source items from (or null to )
|
||||
destination: The stock location to move items to
|
||||
consume: Rather than move the stock, "consume" it. Helpful if you want to queue up removing stock from inventory
|
||||
"""
|
||||
|
||||
# Global setting for specifying reference pattern
|
||||
REFERENCE_PATTERN_SETTING = 'TRANSFERORDER_REFERENCE_PATTERN'
|
||||
REQUIRE_RESPONSIBLE_SETTING = 'TRANSFERORDER_REQUIRE_RESPONSIBLE'
|
||||
STATUS_CLASS = TransferOrderStatus
|
||||
# UNLOCK_SETTING = 'TRANSFERORDER_EDIT_COMPLETED_ORDERS'
|
||||
|
||||
class Meta:
|
||||
"""Model meta options."""
|
||||
|
||||
verbose_name = _('Transfer Order')
|
||||
|
||||
def report_context(self) -> TransferOrderReportContext:
|
||||
"""Return report context data for this TransferOrder."""
|
||||
return {
|
||||
**super().report_context(),
|
||||
'take_from': self.take_from,
|
||||
'destination': self.destination,
|
||||
'consume': self.consume,
|
||||
}
|
||||
|
||||
def get_absolute_url(self) -> str:
|
||||
"""Get the 'web' URL for this order."""
|
||||
return pui_url(f'/stock/transfer-order/{self.pk}')
|
||||
|
||||
@staticmethod
|
||||
def get_api_url() -> str:
|
||||
"""Return the API URL associated with the TransferOrder model."""
|
||||
return reverse('api-transfer-order-list')
|
||||
|
||||
@classmethod
|
||||
def get_status_class(cls):
|
||||
"""Return the TransferOrderStatus class."""
|
||||
return TransferOrderStatusGroups
|
||||
|
||||
@classmethod
|
||||
def api_defaults(cls, request=None):
|
||||
"""Return default values for this model when issuing an API OPTIONS request."""
|
||||
defaults = {
|
||||
'reference': order.validators.generate_next_transfer_order_reference()
|
||||
}
|
||||
|
||||
return defaults
|
||||
|
||||
@classmethod
|
||||
def barcode_model_type_code(cls):
|
||||
"""Return the associated barcode model type code for this model."""
|
||||
return 'TO'
|
||||
|
||||
def subscribed_users(self) -> list[User]:
|
||||
"""Return a list of users subscribed to this TransferOrder.
|
||||
|
||||
By this, we mean users to are interested in any of the parts associated with this order.
|
||||
"""
|
||||
subscribed_users = set()
|
||||
|
||||
for line in self.lines.all():
|
||||
if line.part:
|
||||
# Add the part to the list of subscribed users
|
||||
for user in line.part.get_subscribers():
|
||||
subscribed_users.add(user)
|
||||
|
||||
return list(subscribed_users)
|
||||
|
||||
def clean_line_item(self, line):
|
||||
"""Clean a line item for this PurchaseOrder."""
|
||||
super().clean_line_item(line)
|
||||
line.transferred = 0
|
||||
|
||||
def __str__(self):
|
||||
"""Render a string representation of this TransferOrder."""
|
||||
return f'{self.reference} - {self.take_from.name if self.take_from else _("deleted")} --> {self.destination.name if self.destination else _("deleted")}'
|
||||
|
||||
reference = models.CharField(
|
||||
unique=True,
|
||||
max_length=64,
|
||||
blank=False,
|
||||
help_text=_('Transfer Order Reference'),
|
||||
verbose_name=_('Reference'),
|
||||
default=order.validators.generate_next_transfer_order_reference,
|
||||
validators=[order.validators.validate_transfer_order_reference],
|
||||
)
|
||||
|
||||
status = InvenTreeCustomStatusModelField(
|
||||
default=TransferOrderStatus.PENDING.value,
|
||||
choices=TransferOrderStatus.items(),
|
||||
status_class=TransferOrderStatus,
|
||||
verbose_name=_('Status'),
|
||||
help_text=_('Transfer order status'),
|
||||
)
|
||||
|
||||
@property
|
||||
def status_text(self):
|
||||
"""Return the text representation of the status field."""
|
||||
return TransferOrderStatus.text(self.status)
|
||||
|
||||
take_from = models.ForeignKey(
|
||||
'stock.StockLocation',
|
||||
verbose_name=_('Source Location'),
|
||||
on_delete=models.SET_NULL,
|
||||
related_name='sourcing_transfers',
|
||||
blank=True,
|
||||
null=True,
|
||||
help_text=_('Source for transferred items'),
|
||||
)
|
||||
|
||||
destination = models.ForeignKey(
|
||||
'stock.StockLocation',
|
||||
verbose_name=_('Destination Location'),
|
||||
on_delete=models.SET_NULL,
|
||||
related_name='incoming_transfers',
|
||||
blank=True,
|
||||
null=True,
|
||||
help_text=_('Destination for transferred items'),
|
||||
)
|
||||
|
||||
consume = models.BooleanField(
|
||||
default=False,
|
||||
verbose_name=_('Consume Stock'),
|
||||
help_text=_(
|
||||
'Rather than transfer the stock to the destination, "consume" it, by removing transferred quantity from the allocated stock item'
|
||||
),
|
||||
)
|
||||
|
||||
complete_date = models.DateField(
|
||||
blank=True,
|
||||
null=True,
|
||||
verbose_name=_('Completion Date'),
|
||||
help_text=_('Date order was completed'),
|
||||
)
|
||||
|
||||
@property
|
||||
def company(self) -> None:
|
||||
"""Required accessor helper for Order base class."""
|
||||
return None
|
||||
|
||||
@property
|
||||
def is_pending(self) -> bool:
|
||||
"""Return True if the TransferOrder is 'pending'."""
|
||||
return self.status == TransferOrderStatus.PENDING.value
|
||||
|
||||
@property
|
||||
def is_open(self) -> bool:
|
||||
"""Return True if the TransferOrder is 'open'."""
|
||||
return self.status in TransferOrderStatusGroups.OPEN
|
||||
|
||||
@property
|
||||
def stock_allocations(self) -> QuerySet:
|
||||
"""Return a queryset containing all allocations for this order."""
|
||||
return TransferOrderAllocation.objects.filter(
|
||||
line__in=[line.pk for line in self.lines.all()]
|
||||
)
|
||||
|
||||
def is_fully_allocated(self) -> bool:
|
||||
"""Return True if all line items are fully allocated."""
|
||||
return all(line.is_fully_allocated() for line in self.lines.all())
|
||||
|
||||
def is_overallocated(self) -> bool:
|
||||
"""Return true if any lines in the order are over-allocated."""
|
||||
return any(line.is_overallocated() for line in self.lines.all())
|
||||
|
||||
def is_completed(self) -> bool:
|
||||
"""Check if this order is "transferred" (all line items transferred)."""
|
||||
return all(line.is_completed() for line in self.lines.all())
|
||||
|
||||
def can_complete(
|
||||
self, raise_error: bool = False, allow_incomplete_lines: bool = False
|
||||
) -> bool:
|
||||
"""Test if this TransferOrder can be completed."""
|
||||
try:
|
||||
if self.status == TransferOrderStatus.COMPLETE.value:
|
||||
raise ValidationError(_('Order is already complete'))
|
||||
|
||||
if self.status == TransferOrderStatus.CANCELLED.value:
|
||||
raise ValidationError(_('Order is already cancelled'))
|
||||
|
||||
if not self.consume and not self.destination:
|
||||
raise ValidationError(
|
||||
_('Order cannot be completed until a destination location is set')
|
||||
)
|
||||
|
||||
if not (self.is_fully_allocated() or allow_incomplete_lines):
|
||||
raise ValidationError(
|
||||
_('Order cannot be completed until it is fully allocated')
|
||||
)
|
||||
except ValidationError as e:
|
||||
if raise_error:
|
||||
raise e
|
||||
else:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
@property
|
||||
def can_issue(self) -> bool:
|
||||
"""Return True if this order can be issued."""
|
||||
return self.status in [
|
||||
TransferOrderStatus.PENDING.value,
|
||||
TransferOrderStatus.ON_HOLD.value,
|
||||
]
|
||||
|
||||
@transaction.atomic
|
||||
def issue_order(self):
|
||||
"""Attempt to transition to PLACED status."""
|
||||
return self.handle_transition(
|
||||
self.status, TransferOrderStatus.ISSUED.value, self, self._action_issue
|
||||
)
|
||||
|
||||
# region state changes
|
||||
def _action_issue(self, *args, **kwargs):
|
||||
"""Marks the TransferOrder as ISSUED.
|
||||
|
||||
Order must be currently PENDING.
|
||||
"""
|
||||
if self.can_issue:
|
||||
self.status = TransferOrderStatus.ISSUED.value
|
||||
self.issue_date = InvenTree.helpers.current_date()
|
||||
self.save()
|
||||
|
||||
trigger_event(TransferOrderEvents.ISSUED, id=self.pk)
|
||||
|
||||
# Notify users that the order has been issued
|
||||
notify_responsible(
|
||||
self,
|
||||
TransferOrder,
|
||||
exclude=self.created_by,
|
||||
content=InvenTreeNotificationBodies.NewOrder,
|
||||
extra_users=self.subscribed_users(),
|
||||
)
|
||||
|
||||
@property
|
||||
def can_hold(self) -> bool:
|
||||
"""Return True if this order can be placed on hold."""
|
||||
return self.status in [
|
||||
TransferOrderStatus.PENDING.value,
|
||||
TransferOrderStatus.ISSUED.value,
|
||||
]
|
||||
|
||||
def _action_hold(self, *args, **kwargs):
|
||||
"""Mark this transfer order as 'on hold'."""
|
||||
if self.can_hold:
|
||||
self.status = TransferOrderStatus.ON_HOLD.value
|
||||
self.save()
|
||||
|
||||
trigger_event(TransferOrderEvents.HOLD, id=self.pk)
|
||||
|
||||
@transaction.atomic
|
||||
def _action_complete(self, *args, **kwargs):
|
||||
"""Marks the TransferOrder as COMPLETE.
|
||||
|
||||
Order must be currently ISSUED.
|
||||
"""
|
||||
user = kwargs.pop('user', None)
|
||||
|
||||
if not self.can_complete(raise_error=True, **kwargs):
|
||||
return False
|
||||
|
||||
if self.status == TransferOrderStatus.ISSUED:
|
||||
for allocation in self.allocations():
|
||||
# execute each transfer
|
||||
allocation.complete_allocation(user)
|
||||
|
||||
self.status = TransferOrderStatus.COMPLETE.value
|
||||
self.complete_date = InvenTree.helpers.current_date()
|
||||
|
||||
self.save()
|
||||
|
||||
trigger_event(TransferOrderEvents.COMPLETED, id=self.pk)
|
||||
|
||||
return True
|
||||
|
||||
@transaction.atomic
|
||||
def complete_order(self, user, **kwargs):
|
||||
"""Attempt to transition to COMPLETE status."""
|
||||
return self.handle_transition(
|
||||
self.status,
|
||||
TransferOrderStatus.COMPLETE.value,
|
||||
self,
|
||||
self._action_complete,
|
||||
user=user,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
@transaction.atomic
|
||||
def hold_order(self):
|
||||
"""Attempt to transition to ON_HOLD status."""
|
||||
return self.handle_transition(
|
||||
self.status, TransferOrderStatus.ON_HOLD.value, self, self._action_hold
|
||||
)
|
||||
|
||||
@transaction.atomic
|
||||
def cancel_order(self):
|
||||
"""Attempt to transition to CANCELLED status."""
|
||||
return self.handle_transition(
|
||||
self.status, TransferOrderStatus.CANCELLED.value, self, self._action_cancel
|
||||
)
|
||||
|
||||
@property
|
||||
def can_cancel(self) -> bool:
|
||||
"""A TransferOrder can only be cancelled under the following circumstances.
|
||||
|
||||
- Status is ISSUED
|
||||
- Status is PENDING (or ON_HOLD)
|
||||
"""
|
||||
return self.status in TransferOrderStatusGroups.OPEN
|
||||
|
||||
def _action_cancel(self, *args, **kwargs):
|
||||
"""Cancel this TransferOrder (only if we're allowed to).
|
||||
|
||||
Executes:
|
||||
- Mark the order as 'cancelled'
|
||||
- Delete any StockItems which have been allocated
|
||||
"""
|
||||
if not self.can_cancel:
|
||||
return False
|
||||
|
||||
self.status = TransferOrderStatus.CANCELLED.value
|
||||
self.save()
|
||||
|
||||
# delete allocations
|
||||
for line in self.lines.all():
|
||||
for allocation in line.allocations.all():
|
||||
allocation.delete()
|
||||
|
||||
trigger_event(TransferOrderEvents.CANCELLED, id=self.pk)
|
||||
|
||||
# Notify users that the order has been canceled
|
||||
notify_responsible(
|
||||
self,
|
||||
TransferOrder,
|
||||
exclude=self.created_by,
|
||||
content=InvenTreeNotificationBodies.OrderCanceled,
|
||||
extra_users=self.subscribed_users(),
|
||||
)
|
||||
|
||||
# endregion
|
||||
|
||||
@property
|
||||
def line_count(self) -> int:
|
||||
"""Return the total number of lines associated with this order."""
|
||||
return self.lines.count()
|
||||
|
||||
def completed_line_items(self) -> QuerySet:
|
||||
"""Return a queryset of the completed line items for this order."""
|
||||
return self.lines.filter(transferred__gte=F('quantity'))
|
||||
|
||||
def pending_line_items(self) -> QuerySet:
|
||||
"""Return a queryset of the pending line items for this order."""
|
||||
return self.lines.filter(transferred__lt=F('quantity'))
|
||||
|
||||
@property
|
||||
def completed_line_count(self) -> int:
|
||||
"""Return the number of completed lines for this order."""
|
||||
return self.completed_line_items().count()
|
||||
|
||||
@property
|
||||
def pending_line_count(self) -> int:
|
||||
"""Return the number of pending (incomplete) lines associated with this order."""
|
||||
return self.pending_line_items().count()
|
||||
|
||||
def allocations(self) -> QuerySet:
|
||||
"""Return a queryset of all allocations for this order."""
|
||||
return TransferOrderAllocation.objects.filter(line__order=self)
|
||||
|
||||
|
||||
class TransferOrderLineItem(OrderLineItem):
|
||||
"""Model for a single LineItem in a TransferOrder.
|
||||
|
||||
Attributes:
|
||||
order: Link to the TransferOrder that this line item belongs to
|
||||
part: Link to a Part object (may be null)
|
||||
transferred: The number of items which have actually transferred against this line item
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
"""Model meta options."""
|
||||
|
||||
verbose_name = _('Transfer Order Line Item')
|
||||
|
||||
# Filter for determining if a particular TransferOrderLineItem is overdue
|
||||
OVERDUE_FILTER = (
|
||||
Q(transferred__lt=F('quantity'))
|
||||
& ~Q(target_date=None)
|
||||
& Q(target_date__lt=InvenTree.helpers.current_date())
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def get_api_url():
|
||||
"""Return the API URL associated with the TransferOrderLineItem model."""
|
||||
return reverse('api-transfer-order-line-list')
|
||||
|
||||
order = models.ForeignKey(
|
||||
TransferOrder,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='lines',
|
||||
verbose_name=_('Order'),
|
||||
help_text=_('Transfer Order'),
|
||||
)
|
||||
|
||||
part = models.ForeignKey(
|
||||
'part.Part',
|
||||
on_delete=models.SET_NULL,
|
||||
related_name='transfer_order_line_items',
|
||||
null=True,
|
||||
verbose_name=_('Part'),
|
||||
help_text=_('Part'),
|
||||
# limit_choices_to={'salable': True},
|
||||
)
|
||||
|
||||
transferred = RoundingDecimalField(
|
||||
verbose_name=_('transferred'),
|
||||
help_text=_('transferred quantity'),
|
||||
default=0,
|
||||
max_digits=15,
|
||||
decimal_places=5,
|
||||
validators=[MinValueValidator(0)],
|
||||
)
|
||||
|
||||
def allocated_quantity(self):
|
||||
"""Return the total stock quantity allocated to this LineItem.
|
||||
|
||||
This is a summation of the quantity of each attached StockItem
|
||||
"""
|
||||
if not self.pk:
|
||||
return 0
|
||||
|
||||
query = self.allocations.aggregate(
|
||||
allocated=Coalesce(Sum('quantity'), Decimal(0))
|
||||
)
|
||||
|
||||
return query['allocated']
|
||||
|
||||
def is_fully_allocated(self) -> bool:
|
||||
"""Return True if this line item is fully allocated."""
|
||||
# If the linked part is "virtual", then we cannot allocate stock against it
|
||||
if self.part and self.part.virtual:
|
||||
return True
|
||||
|
||||
return self.allocated_quantity() >= self.quantity
|
||||
|
||||
def is_overallocated(self) -> bool:
|
||||
"""Return True if this line item is over allocated."""
|
||||
return self.allocated_quantity() > self.quantity
|
||||
|
||||
def is_completed(self) -> bool:
|
||||
"""Return True if this line item is completed (has been fully shipped)."""
|
||||
# A "virtual" part is always considered to be "completed"
|
||||
if self.part and self.part.virtual:
|
||||
return True
|
||||
|
||||
return self.transferred >= self.quantity
|
||||
|
||||
|
||||
class TransferOrderAllocation(models.Model):
|
||||
"""This model is used to 'allocate' stock items to a TransferOrder. Items that are "allocated" to a TransferOrder are not yet "attached" to the order, but they will be once the order is fulfilled.
|
||||
|
||||
Attributes:
|
||||
line: TransferOrderLineItem reference
|
||||
item: StockItem reference
|
||||
quantity: Quantity to take from the StockItem
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
"""Model meta options."""
|
||||
|
||||
verbose_name = _('Transfer Order Allocation')
|
||||
|
||||
@staticmethod
|
||||
def get_api_url():
|
||||
"""Return the API URL associated with the TransferOrderAllocation model."""
|
||||
return reverse('api-transfer-order-allocation-list')
|
||||
|
||||
def clean(self):
|
||||
"""Validate the TransferOrderAllocation object.
|
||||
|
||||
Executes:
|
||||
- Cannot allocate stock to a line item without a part reference
|
||||
- The referenced part must match the part associated with the line item
|
||||
- Allocated quantity cannot exceed the quantity of the stock item
|
||||
- Allocation quantity must be "1" if the StockItem is serialized
|
||||
- Allocation quantity cannot be zero
|
||||
"""
|
||||
super().clean()
|
||||
|
||||
errors = {}
|
||||
|
||||
try:
|
||||
if not self.item:
|
||||
raise ValidationError({'item': _('Stock item has not been assigned')})
|
||||
except stock.models.StockItem.DoesNotExist:
|
||||
raise ValidationError({'item': _('Stock item has not been assigned')})
|
||||
|
||||
try:
|
||||
if self.line.part != self.item.part:
|
||||
variants = self.line.part.get_descendants(include_self=True)
|
||||
if self.line.part not in variants:
|
||||
errors['item'] = _(
|
||||
'Cannot allocate stock item to a line with a different part'
|
||||
)
|
||||
except PartModels.Part.DoesNotExist:
|
||||
errors['line'] = _('Cannot allocate stock to a line without a part')
|
||||
|
||||
if self.quantity > self.item.quantity:
|
||||
errors['quantity'] = _('Allocation quantity cannot exceed stock quantity')
|
||||
|
||||
# Ensure that we do not 'over allocate' a stock item
|
||||
build_allocation_count = self.item.build_allocation_count()
|
||||
sales_allocation_count = self.item.sales_order_allocation_count(
|
||||
exclude_allocations={'pk': self.pk}
|
||||
)
|
||||
|
||||
total_allocation = (
|
||||
build_allocation_count + sales_allocation_count + self.quantity
|
||||
)
|
||||
|
||||
if total_allocation > self.item.quantity:
|
||||
errors['quantity'] = _('Stock item is over-allocated')
|
||||
|
||||
if self.quantity <= 0:
|
||||
errors['quantity'] = _('Allocation quantity must be greater than zero')
|
||||
|
||||
if self.item.serial and self.quantity != 1:
|
||||
errors['quantity'] = _('Quantity must be 1 for serialized stock item')
|
||||
|
||||
if len(errors) > 0:
|
||||
raise ValidationError(errors)
|
||||
|
||||
line = models.ForeignKey(
|
||||
TransferOrderLineItem,
|
||||
on_delete=models.CASCADE,
|
||||
verbose_name=_('Line'),
|
||||
related_name='allocations',
|
||||
)
|
||||
|
||||
item = models.ForeignKey(
|
||||
'stock.StockItem',
|
||||
on_delete=models.CASCADE,
|
||||
related_name='transfer_order_allocations',
|
||||
limit_choices_to={
|
||||
'part__virtual': False,
|
||||
'belongs_to': None,
|
||||
'sales_order': None,
|
||||
},
|
||||
verbose_name=_('Item'),
|
||||
help_text=_('Select stock item to allocate'),
|
||||
)
|
||||
|
||||
quantity = RoundingDecimalField(
|
||||
max_digits=15,
|
||||
decimal_places=5,
|
||||
validators=[MinValueValidator(0)],
|
||||
default=1,
|
||||
verbose_name=_('Quantity'),
|
||||
help_text=_('Enter stock allocation quantity'),
|
||||
)
|
||||
|
||||
def get_location(self):
|
||||
"""Return the <pk> value of the location associated with this allocation."""
|
||||
return self.item.location.id if self.item.location else None
|
||||
|
||||
def get_po(self):
|
||||
"""Return the PurchaseOrder associated with this allocation."""
|
||||
return self.item.purchase_order
|
||||
|
||||
def complete_allocation(self, user):
|
||||
"""Complete this allocation (called when the parent TransferOrder is marked as "completed").
|
||||
|
||||
Executes:
|
||||
- Determine if the referenced StockItem needs to be "split" (if allocated quantity != stock quantity)
|
||||
- Move the StockItem to the new location
|
||||
- Updates the transferred qty
|
||||
- If order is marked as "consume", reduce quantity rather than move
|
||||
"""
|
||||
order: TransferOrder = self.line.order
|
||||
self.item: stock.models.StockItem # for type hints
|
||||
self.line: TransferOrderLineItem # for type hints
|
||||
|
||||
# The allocation is the only thing linking this stock item to the transfer
|
||||
# As a result, we must keep the allocation present even after completion
|
||||
# This means allocations to transfer orders don't affect "available" stock
|
||||
# (otherwise it would permanently reduce available stock)
|
||||
|
||||
if order.consume:
|
||||
# rather than transferring the stock, we simply reduce its quantity to release it from tracked inventory
|
||||
# NOTE: if delete_on_deplete is enabled, this will result in the "transferred stock" panel being empty
|
||||
# after completion. A more sophesticated immutable tracking that doesn't rely on allocations
|
||||
# would be helpful here
|
||||
self.item.take_stock(
|
||||
quantity=self.quantity,
|
||||
user=user,
|
||||
code=StockHistoryCode.STOCK_REMOVE,
|
||||
transferorder=order,
|
||||
)
|
||||
else:
|
||||
if self.quantity < self.item.quantity:
|
||||
# update our own reference to the StockItem which was split
|
||||
self.item = self.item.splitStock(
|
||||
quantity=self.quantity,
|
||||
location=order.destination,
|
||||
user=user,
|
||||
transferorder=order,
|
||||
)
|
||||
self.save()
|
||||
else:
|
||||
# move item directly, we don't have to split
|
||||
self.item.move(
|
||||
location=order.destination, user=user, transferorder=order, notes=''
|
||||
)
|
||||
|
||||
# Update the transferred qty
|
||||
self.line.transferred += self.quantity
|
||||
self.line.save()
|
||||
|
||||
|
||||
def _touch_order_updated_at(instance):
|
||||
"""Bump updated_at on the parent order without triggering a full save."""
|
||||
if not InvenTree.ready.canAppAccessDatabase(allow_test=True):
|
||||
@@ -3190,6 +3851,16 @@ def _touch_order_updated_at(instance):
|
||||
@receiver(
|
||||
post_delete, sender=ReturnOrderExtraLine, dispatch_uid='ro_extraline_post_delete'
|
||||
)
|
||||
@receiver(
|
||||
post_save,
|
||||
sender=TransferOrderLineItem,
|
||||
dispatch_uid='transfer_order_lineitem_post_save',
|
||||
)
|
||||
@receiver(
|
||||
post_delete,
|
||||
sender=TransferOrderLineItem,
|
||||
dispatch_uid='transfer_order_lineitem_post_delete',
|
||||
)
|
||||
def update_order_on_lineitem_change(sender, instance, **kwargs):
|
||||
"""Update parent order updated_at when any line item is saved or deleted."""
|
||||
_touch_order_updated_at(instance)
|
||||
|
||||
@@ -44,6 +44,7 @@ from order.status_codes import (
|
||||
ReturnOrderLineStatus,
|
||||
ReturnOrderStatus,
|
||||
SalesOrderStatusGroups,
|
||||
TransferOrderStatusGroups,
|
||||
)
|
||||
from part.serializers import PartBriefSerializer
|
||||
from stock.status_codes import StockStatus
|
||||
@@ -2298,3 +2299,622 @@ class ReturnOrderExtraLineSerializer(
|
||||
'allow_null': True,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@register_importer()
|
||||
class TransferOrderSerializer(
|
||||
NotesFieldMixin,
|
||||
InvenTreeCustomStatusSerializerMixin,
|
||||
AbstractOrderSerializer,
|
||||
InvenTreeModelSerializer,
|
||||
):
|
||||
"""Serializer for a TransferOrder object."""
|
||||
|
||||
class Meta:
|
||||
"""Metaclass options."""
|
||||
|
||||
model = order.models.TransferOrder
|
||||
fields = AbstractOrderSerializer.order_fields([
|
||||
'take_from',
|
||||
'take_from_detail',
|
||||
'destination',
|
||||
'destination_detail',
|
||||
'consume',
|
||||
'complete_date',
|
||||
])
|
||||
read_only_fields = ['creation_date']
|
||||
extra_kwargs = {}
|
||||
|
||||
def skip_create_fields(self):
|
||||
"""Skip these fields when instantiating a new object."""
|
||||
fields = super().skip_create_fields()
|
||||
|
||||
return [*fields, 'duplicate']
|
||||
|
||||
@staticmethod
|
||||
def annotate_queryset(queryset):
|
||||
"""Add extra information to the queryset.
|
||||
|
||||
- Number of line items in the TransferOrder
|
||||
- Number of completed line items in the TransferOrder
|
||||
- Overdue status of the TransferOrder
|
||||
"""
|
||||
queryset = AbstractOrderSerializer.annotate_queryset(queryset)
|
||||
|
||||
queryset = queryset.annotate(
|
||||
completed_lines=SubqueryCount(
|
||||
'lines', filter=Q(quantity__lte=F('transferred'))
|
||||
)
|
||||
)
|
||||
|
||||
queryset = queryset.annotate(
|
||||
overdue=Case(
|
||||
When(
|
||||
order.models.TransferOrder.overdue_filter(),
|
||||
then=Value(True, output_field=BooleanField()),
|
||||
),
|
||||
default=Value(False, output_field=BooleanField()),
|
||||
)
|
||||
)
|
||||
|
||||
return queryset
|
||||
|
||||
take_from_detail = OptionalField(
|
||||
serializer_class=stock.serializers.LocationSerializer,
|
||||
serializer_kwargs={
|
||||
'source': 'take_from',
|
||||
'many': False,
|
||||
'read_only': True,
|
||||
'allow_null': True,
|
||||
},
|
||||
default_include=True,
|
||||
)
|
||||
|
||||
destination_detail = OptionalField(
|
||||
serializer_class=stock.serializers.LocationSerializer,
|
||||
serializer_kwargs={
|
||||
'source': 'destination',
|
||||
'many': False,
|
||||
'read_only': True,
|
||||
'allow_null': True,
|
||||
},
|
||||
default_include=True,
|
||||
)
|
||||
|
||||
|
||||
class TransferOrderHoldSerializer(OrderAdjustSerializer):
|
||||
"""Serializer for placing a TransferOrder on hold."""
|
||||
|
||||
def save(self):
|
||||
"""Save the serializer to 'hold' the order."""
|
||||
self.order.hold_order()
|
||||
|
||||
|
||||
class TransferOrderIssueSerializer(OrderAdjustSerializer):
|
||||
"""Serializer for issuing a transfer order."""
|
||||
|
||||
def save(self):
|
||||
"""Save the serializer to 'issue' the order."""
|
||||
self.order.issue_order()
|
||||
|
||||
|
||||
class TransferOrderCancelSerializer(OrderAdjustSerializer):
|
||||
"""Serializer for cancelling a TransferOrder."""
|
||||
|
||||
def save(self):
|
||||
"""Save the serializer to 'cancel' the order."""
|
||||
if not self.order.can_cancel:
|
||||
raise ValidationError(_('Order cannot be cancelled'))
|
||||
|
||||
self.order.cancel_order()
|
||||
|
||||
|
||||
class TransferOrderCompleteSerializer(OrderAdjustSerializer):
|
||||
"""Serializer for completing a transfer order."""
|
||||
|
||||
class Meta:
|
||||
"""Metaclass options."""
|
||||
|
||||
fields = ['accept_incomplete_allocation']
|
||||
|
||||
accept_incomplete_allocation = serializers.BooleanField(
|
||||
label=_('Accept Incomplete Allocation'),
|
||||
help_text=_('Allow order to complete with incomplete allocations'),
|
||||
required=False,
|
||||
default=False,
|
||||
)
|
||||
|
||||
def validate_accept_incomplete_allocation(self, value):
|
||||
"""Check if the 'accept_incomplete_allocation' field is required."""
|
||||
order = self.context['order']
|
||||
|
||||
if not value and not order.is_fully_allocated():
|
||||
raise ValidationError(_('Order has incomplete allocations'))
|
||||
|
||||
return value
|
||||
|
||||
def get_context_data(self):
|
||||
"""Custom context information for this serializer."""
|
||||
order = self.context['order']
|
||||
|
||||
return {'is_complete': order.is_completed()}
|
||||
|
||||
def validate(self, data):
|
||||
"""Custom validation for the serializer."""
|
||||
data = super().validate(data)
|
||||
self.order.can_complete(
|
||||
raise_error=True,
|
||||
allow_incomplete_lines=str2bool(
|
||||
data.get('accept_incomplete_allocation', False)
|
||||
),
|
||||
)
|
||||
return data
|
||||
|
||||
def save(self):
|
||||
"""Save the serializer to 'complete' the order."""
|
||||
request = self.context.get('request')
|
||||
data = self.validated_data
|
||||
user = request.user if request else None
|
||||
|
||||
self.order.complete_order(
|
||||
user=user,
|
||||
allow_incomplete_lines=data.get('accept_incomplete_allocation', False),
|
||||
)
|
||||
|
||||
|
||||
@register_importer()
|
||||
class TransferOrderLineItemSerializer(
|
||||
DataImportExportSerializerMixin,
|
||||
AbstractLineItemSerializer,
|
||||
InvenTreeModelSerializer,
|
||||
):
|
||||
"""Serializer for a TransferOrderLineItem object."""
|
||||
|
||||
class Meta:
|
||||
"""Metaclass options."""
|
||||
|
||||
model = order.models.TransferOrderLineItem
|
||||
fields = AbstractLineItemSerializer.line_fields([
|
||||
'allocated',
|
||||
'overdue',
|
||||
'part',
|
||||
'part_detail',
|
||||
'transferred',
|
||||
# Annotated fields for part stocking information
|
||||
'available_stock',
|
||||
'available_variant_stock',
|
||||
'building',
|
||||
'on_order',
|
||||
# Filterable detail fields
|
||||
])
|
||||
|
||||
@staticmethod
|
||||
def annotate_queryset(queryset):
|
||||
"""Add some extra annotations to this queryset.
|
||||
|
||||
- "overdue" status (boolean field)
|
||||
- "available_quantity"
|
||||
- "building"
|
||||
- "on_order"
|
||||
"""
|
||||
queryset = queryset.annotate(
|
||||
overdue=Case(
|
||||
When(
|
||||
Q(order__status__in=TransferOrderStatusGroups.OPEN)
|
||||
& order.models.TransferOrderLineItem.OVERDUE_FILTER,
|
||||
then=Value(True, output_field=BooleanField()),
|
||||
),
|
||||
default=Value(False, output_field=BooleanField()),
|
||||
)
|
||||
)
|
||||
|
||||
# Annotate each line with the available stock quantity
|
||||
# To do this, we need to look at the total stock and any allocations
|
||||
queryset = queryset.alias(
|
||||
total_stock=part_filters.annotate_total_stock(reference='part__'),
|
||||
allocated_to_sales_orders=part_filters.annotate_sales_order_allocations(
|
||||
reference='part__'
|
||||
),
|
||||
allocated_to_build_orders=part_filters.annotate_build_order_allocations(
|
||||
reference='part__'
|
||||
),
|
||||
)
|
||||
|
||||
queryset = queryset.annotate(
|
||||
available_stock=Greatest(
|
||||
ExpressionWrapper(
|
||||
F('total_stock')
|
||||
- F('allocated_to_sales_orders')
|
||||
- F('allocated_to_build_orders'),
|
||||
output_field=models.DecimalField(),
|
||||
),
|
||||
0,
|
||||
output_field=models.DecimalField(),
|
||||
)
|
||||
)
|
||||
|
||||
# Add information about the quantity of parts currently on order
|
||||
queryset = queryset.annotate(
|
||||
on_order=part_filters.annotate_on_order_quantity(reference='part__')
|
||||
)
|
||||
|
||||
# Add information about the quantity of parts currently in production
|
||||
queryset = queryset.annotate(
|
||||
building=part_filters.annotate_in_production_quantity(reference='part__')
|
||||
)
|
||||
|
||||
# Annotate total 'allocated' stock quantity
|
||||
queryset = queryset.annotate(
|
||||
allocated=Coalesce(
|
||||
SubquerySum('allocations__quantity'),
|
||||
Decimal(0),
|
||||
output_field=models.DecimalField(),
|
||||
)
|
||||
)
|
||||
|
||||
return queryset
|
||||
|
||||
order_detail = OptionalField(
|
||||
serializer_class=TransferOrderSerializer,
|
||||
serializer_kwargs={
|
||||
'source': 'order',
|
||||
'many': False,
|
||||
'read_only': True,
|
||||
'allow_null': True,
|
||||
},
|
||||
prefetch_fields=[
|
||||
'order__created_by',
|
||||
'order__responsible',
|
||||
'order__project_code',
|
||||
],
|
||||
)
|
||||
|
||||
part_detail = OptionalField(
|
||||
serializer_class=PartBriefSerializer,
|
||||
serializer_kwargs={
|
||||
'source': 'part',
|
||||
'many': False,
|
||||
'read_only': True,
|
||||
'allow_null': True,
|
||||
},
|
||||
prefetch_fields=['part__pricing_data'],
|
||||
)
|
||||
|
||||
# Annotated fields
|
||||
overdue = serializers.BooleanField(read_only=True, allow_null=True)
|
||||
available_stock = serializers.FloatField(read_only=True)
|
||||
available_variant_stock = serializers.FloatField(read_only=True)
|
||||
on_order = serializers.FloatField(label=_('On Order'), read_only=True)
|
||||
building = serializers.FloatField(label=_('In Production'), read_only=True)
|
||||
|
||||
quantity = InvenTreeDecimalField()
|
||||
|
||||
allocated = serializers.FloatField(read_only=True)
|
||||
|
||||
transferred = InvenTreeDecimalField(read_only=True)
|
||||
|
||||
|
||||
class TransferOrderAllocationItemSerializer(serializers.Serializer):
|
||||
"""A serializer for allocating a single stock-item against a TransferOrder line item."""
|
||||
|
||||
class Meta:
|
||||
"""Metaclass options."""
|
||||
|
||||
fields = ['line_item', 'stock_item', 'quantity']
|
||||
|
||||
line_item = serializers.PrimaryKeyRelatedField(
|
||||
queryset=order.models.TransferOrderLineItem.objects.all(),
|
||||
many=False,
|
||||
allow_null=False,
|
||||
required=True,
|
||||
label=_('Stock Item'),
|
||||
)
|
||||
|
||||
def validate_line_item(self, line_item):
|
||||
"""Custom validation for the 'line_item' field.
|
||||
|
||||
- Ensure the line_item is associated with the particular TransferOrder
|
||||
"""
|
||||
order = self.context['order']
|
||||
|
||||
# Ensure that the line item points to the correct order
|
||||
if line_item.order != order:
|
||||
raise ValidationError(_('Line item is not associated with this order'))
|
||||
|
||||
return line_item
|
||||
|
||||
stock_item = serializers.PrimaryKeyRelatedField(
|
||||
queryset=stock.models.StockItem.objects.all(),
|
||||
many=False,
|
||||
allow_null=False,
|
||||
required=True,
|
||||
label=_('Stock Item'),
|
||||
)
|
||||
|
||||
quantity = serializers.DecimalField(
|
||||
max_digits=15, decimal_places=5, min_value=Decimal(0), required=True
|
||||
)
|
||||
|
||||
def validate_quantity(self, quantity):
|
||||
"""Custom validation for the 'quantity' field."""
|
||||
if quantity <= 0:
|
||||
raise ValidationError(_('Quantity must be positive'))
|
||||
|
||||
return quantity
|
||||
|
||||
def validate(self, data):
|
||||
"""Custom validation for the serializer.
|
||||
|
||||
- Ensure that the quantity is 1 for serialized stock
|
||||
- Quantity cannot exceed the available amount
|
||||
"""
|
||||
data = super().validate(data)
|
||||
|
||||
stock_item = data['stock_item']
|
||||
quantity = data['quantity']
|
||||
|
||||
if stock_item.serialized and quantity != 1:
|
||||
raise ValidationError({
|
||||
'quantity': _('Quantity must be 1 for serialized stock item')
|
||||
})
|
||||
|
||||
q = normalize(stock_item.unallocated_quantity())
|
||||
|
||||
if quantity > q:
|
||||
raise ValidationError({'quantity': _(f'Available quantity ({q}) exceeded')})
|
||||
|
||||
return data
|
||||
|
||||
|
||||
class TransferOrderLineItemAllocationSerializer(serializers.Serializer):
|
||||
"""DRF serializer for allocation of stock items against a transfer order line item."""
|
||||
|
||||
class Meta:
|
||||
"""Metaclass options."""
|
||||
|
||||
fields = ['items']
|
||||
|
||||
items = TransferOrderAllocationItemSerializer(many=True)
|
||||
|
||||
def validate(self, data):
|
||||
"""Serializer validation."""
|
||||
data = super().validate(data)
|
||||
|
||||
# Extract TransferOrder from serializer context
|
||||
# order = self.context['order']
|
||||
|
||||
items = data.get('items', [])
|
||||
|
||||
if len(items) == 0:
|
||||
raise ValidationError(_('Allocation items must be provided'))
|
||||
|
||||
return data
|
||||
|
||||
def save(self):
|
||||
"""Perform the allocation of items against this order."""
|
||||
data = self.validated_data
|
||||
|
||||
items = data['items']
|
||||
|
||||
with transaction.atomic():
|
||||
for entry in items:
|
||||
# Create a new TransferOrderAllocation
|
||||
allocation = order.models.TransferOrderAllocation(
|
||||
line=entry.get('line_item'),
|
||||
item=entry.get('stock_item'),
|
||||
quantity=entry.get('quantity'),
|
||||
)
|
||||
|
||||
allocation.full_clean()
|
||||
allocation.save()
|
||||
|
||||
|
||||
class TransferOrderAllocationSerializer(
|
||||
FilterableSerializerMixin, InvenTreeModelSerializer
|
||||
):
|
||||
"""Serializer for the TransferOrderAllocation model.
|
||||
|
||||
This includes some fields from the related model objects.
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
"""Metaclass options."""
|
||||
|
||||
model = order.models.TransferOrderAllocation
|
||||
fields = [
|
||||
'pk',
|
||||
'item',
|
||||
'quantity',
|
||||
# Annotated read-only fields
|
||||
'line',
|
||||
'part',
|
||||
'order',
|
||||
'serial',
|
||||
'location',
|
||||
# Extra detail fields
|
||||
'item_detail',
|
||||
'part_detail',
|
||||
'order_detail',
|
||||
'location_detail',
|
||||
]
|
||||
read_only_fields = ['line', '']
|
||||
|
||||
part = serializers.PrimaryKeyRelatedField(source='item.part', read_only=True)
|
||||
order = serializers.PrimaryKeyRelatedField(
|
||||
source='line.order', many=False, read_only=True
|
||||
)
|
||||
serial = serializers.CharField(source='get_serial', read_only=True, allow_null=True)
|
||||
quantity = serializers.FloatField(read_only=False)
|
||||
location = serializers.PrimaryKeyRelatedField(
|
||||
source='item.location', many=False, read_only=True
|
||||
)
|
||||
|
||||
# Extra detail fields
|
||||
order_detail = OptionalField(
|
||||
serializer_class=TransferOrderSerializer,
|
||||
serializer_kwargs={
|
||||
'source': 'line.order',
|
||||
'many': False,
|
||||
'read_only': True,
|
||||
'allow_null': True,
|
||||
},
|
||||
)
|
||||
|
||||
part_detail = OptionalField(
|
||||
serializer_class=PartBriefSerializer,
|
||||
serializer_kwargs={
|
||||
'source': 'item.part',
|
||||
'many': False,
|
||||
'read_only': True,
|
||||
'allow_null': True,
|
||||
},
|
||||
)
|
||||
|
||||
item_detail = OptionalField(
|
||||
serializer_class=stock.serializers.StockItemSerializer,
|
||||
serializer_kwargs={
|
||||
'source': 'item',
|
||||
'many': False,
|
||||
'read_only': True,
|
||||
'allow_null': True,
|
||||
'part_detail': False,
|
||||
'location_detail': False,
|
||||
'supplier_part_detail': False,
|
||||
},
|
||||
)
|
||||
|
||||
location_detail = OptionalField(
|
||||
serializer_class=stock.serializers.LocationBriefSerializer,
|
||||
serializer_kwargs={
|
||||
'source': 'item.location',
|
||||
'many': False,
|
||||
'read_only': True,
|
||||
'allow_null': True,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class TransferOrderSerialAllocationSerializer(serializers.Serializer):
|
||||
"""DRF serializer for allocation of serial numbers against a transfer order."""
|
||||
|
||||
class Meta:
|
||||
"""Metaclass options."""
|
||||
|
||||
fields = ['line_item', 'quantity', 'serial_numbers']
|
||||
|
||||
line_item = serializers.PrimaryKeyRelatedField(
|
||||
queryset=order.models.TransferOrderLineItem.objects.all(),
|
||||
many=False,
|
||||
required=True,
|
||||
allow_null=False,
|
||||
label=_('Line Item'),
|
||||
)
|
||||
|
||||
def validate_line_item(self, line_item):
|
||||
"""Ensure that the line_item is valid."""
|
||||
order = self.context['order']
|
||||
|
||||
# Ensure that the line item points to the correct order
|
||||
if line_item.order != order:
|
||||
raise ValidationError(_('Line item is not associated with this order'))
|
||||
|
||||
return line_item
|
||||
|
||||
quantity = serializers.IntegerField(
|
||||
min_value=1, required=True, allow_null=False, label=_('Quantity')
|
||||
)
|
||||
|
||||
serial_numbers = serializers.CharField(
|
||||
label=_('Serial Numbers'),
|
||||
help_text=_('Enter serial numbers to allocate'),
|
||||
required=True,
|
||||
allow_blank=False,
|
||||
)
|
||||
|
||||
def validate(self, data):
|
||||
"""Validation for the serializer.
|
||||
|
||||
- Ensure the serial_numbers and quantity fields match
|
||||
- Check that all serial numbers exist
|
||||
- Check that the serial numbers are not yet allocated
|
||||
"""
|
||||
data = super().validate(data)
|
||||
|
||||
line_item = data['line_item']
|
||||
quantity = data['quantity']
|
||||
serial_numbers = data['serial_numbers']
|
||||
|
||||
part = line_item.part
|
||||
|
||||
try:
|
||||
data['serials'] = extract_serial_numbers(
|
||||
serial_numbers, quantity, part.get_latest_serial_number(), part=part
|
||||
)
|
||||
except DjangoValidationError as e:
|
||||
raise ValidationError({'serial_numbers': e.messages})
|
||||
|
||||
serials_not_exist = set()
|
||||
serials_unavailable = set()
|
||||
stock_items_to_allocate = []
|
||||
|
||||
for serial in data['serials']:
|
||||
serial = str(serial).strip()
|
||||
|
||||
items = stock.models.StockItem.objects.filter(
|
||||
part=part, serial=serial, quantity=1
|
||||
)
|
||||
|
||||
if not items.exists():
|
||||
serials_not_exist.add(str(serial))
|
||||
continue
|
||||
|
||||
stock_item = items[0]
|
||||
|
||||
if not stock_item.in_stock:
|
||||
serials_unavailable.add(str(serial))
|
||||
continue
|
||||
|
||||
if stock_item.unallocated_quantity() < 1:
|
||||
serials_unavailable.add(str(serial))
|
||||
continue
|
||||
|
||||
# At this point, the serial number is valid, and can be added to the list
|
||||
stock_items_to_allocate.append(stock_item)
|
||||
|
||||
if len(serials_not_exist) > 0:
|
||||
error_msg = _('No match found for the following serial numbers')
|
||||
error_msg += ': '
|
||||
error_msg += ','.join(sorted(serials_not_exist))
|
||||
|
||||
raise ValidationError({'serial_numbers': error_msg})
|
||||
|
||||
if len(serials_unavailable) > 0:
|
||||
error_msg = _('The following serial numbers are unavailable')
|
||||
error_msg += ': '
|
||||
error_msg += ','.join(sorted(serials_unavailable))
|
||||
|
||||
raise ValidationError({'serial_numbers': error_msg})
|
||||
|
||||
data['stock_items'] = stock_items_to_allocate
|
||||
|
||||
return data
|
||||
|
||||
def save(self):
|
||||
"""Allocate stock items against the transfer order."""
|
||||
data = self.validated_data
|
||||
|
||||
line_item = data['line_item']
|
||||
stock_items = data['stock_items']
|
||||
|
||||
allocations = []
|
||||
|
||||
for stock_item in stock_items:
|
||||
# Create a new TransferOrderAllocation
|
||||
allocations.append(
|
||||
order.models.TransferOrderAllocation(
|
||||
line=line_item, item=stock_item, quantity=1
|
||||
)
|
||||
)
|
||||
|
||||
with transaction.atomic():
|
||||
order.models.TransferOrderAllocation.objects.bulk_create(allocations)
|
||||
|
||||
@@ -115,3 +115,30 @@ class ReturnOrderLineStatus(StatusCode):
|
||||
|
||||
# Item is rejected
|
||||
REJECT = 60, _('Reject'), ColorEnum.danger
|
||||
|
||||
|
||||
class TransferOrderStatus(StatusCode):
|
||||
"""Defines a set of status codes for a TransferOrder."""
|
||||
|
||||
# Order status codes
|
||||
PENDING = 10, _('Pending'), ColorEnum.secondary # Order is pending (not yet issued)
|
||||
ISSUED = 20, _('Issued'), ColorEnum.primary # Order has been issued
|
||||
ON_HOLD = 25, _('On Hold'), ColorEnum.warning # Order is on hold
|
||||
COMPLETE = 30, _('Complete'), ColorEnum.success # Order has been completed
|
||||
CANCELLED = 40, _('Cancelled'), ColorEnum.danger # Order was cancelled
|
||||
|
||||
|
||||
class TransferOrderStatusGroups:
|
||||
"""Groups for TransferOrderStatus codes."""
|
||||
|
||||
# Open orders
|
||||
OPEN = [
|
||||
TransferOrderStatus.PENDING.value,
|
||||
TransferOrderStatus.ON_HOLD.value,
|
||||
TransferOrderStatus.ISSUED.value,
|
||||
]
|
||||
|
||||
# Failed orders
|
||||
FAILED = [TransferOrderStatus.CANCELLED.value]
|
||||
|
||||
COMPLETE = [TransferOrderStatus.COMPLETE.value]
|
||||
|
||||
@@ -27,6 +27,8 @@ from order.status_codes import (
|
||||
ReturnOrderStatus,
|
||||
SalesOrderStatus,
|
||||
SalesOrderStatusGroups,
|
||||
TransferOrderStatus,
|
||||
TransferOrderStatusGroups,
|
||||
)
|
||||
from part.models import Part
|
||||
from stock.models import StockItem, StockLocation
|
||||
@@ -46,9 +48,10 @@ class OrderTest(InvenTreeAPITestCase):
|
||||
'stock',
|
||||
'order',
|
||||
'sales_order',
|
||||
'transfer_order',
|
||||
]
|
||||
|
||||
roles = ['purchase_order.change', 'sales_order.change']
|
||||
roles = ['purchase_order.change', 'sales_order.change', 'transfer_order.change']
|
||||
|
||||
def filter(self, filters, count):
|
||||
"""Test API filters."""
|
||||
@@ -2892,3 +2895,848 @@ class ReturnOrderLineItemTests(InvenTreeAPITestCase):
|
||||
|
||||
line = models.ReturnOrderLineItem.objects.get(pk=1)
|
||||
self.assertEqual(float(line.price.amount), 15.75)
|
||||
|
||||
|
||||
class TransferOrderTest(OrderTest):
|
||||
"""Tests for the TransferOrder API."""
|
||||
|
||||
LIST_URL = reverse('api-transfer-order-list')
|
||||
|
||||
def test_transfer_order_list(self):
|
||||
"""Test the TransferOrder list API endpoint."""
|
||||
# all orders
|
||||
self.filter({}, 5)
|
||||
|
||||
# filter by outstanding
|
||||
self.filter({'outstanding': True}, 3)
|
||||
self.filter({'outstanding': False}, 2)
|
||||
|
||||
# Filter by status
|
||||
self.filter({'status': TransferOrderStatus.PENDING.value}, 1)
|
||||
self.filter({'status': SalesOrderStatus.COMPLETE.value}, 1)
|
||||
self.filter({'status': 99}, 0) # Invalid
|
||||
|
||||
# Filter by "reference"
|
||||
self.filter({'reference': 'TO-123'}, 1)
|
||||
self.filter({'reference': 'TO-999'}, 0)
|
||||
|
||||
# Filter by "assigned_to_me"
|
||||
self.filter({'assigned_to_me': 1}, 0)
|
||||
self.filter({'assigned_to_me': 0}, 5)
|
||||
|
||||
def test_overdue(self):
|
||||
"""Test "overdue" status."""
|
||||
self.filter({'overdue': True}, 0)
|
||||
self.filter({'overdue': False}, 5)
|
||||
|
||||
# pick two orders that are still open (not cancelled or complete)
|
||||
for pk in [1, 4]:
|
||||
order = models.TransferOrder.objects.get(pk=pk)
|
||||
order.target_date = datetime.now().date() - timedelta(days=10)
|
||||
order.save()
|
||||
|
||||
self.filter({'overdue': True}, 2)
|
||||
self.filter({'overdue': False}, 3)
|
||||
|
||||
def test_transfer_order_detail(self):
|
||||
"""Test the TransferOrder detail endpoint."""
|
||||
url = '/api/order/transfer-order/1/'
|
||||
|
||||
response = self.get(url)
|
||||
|
||||
data = response.data
|
||||
|
||||
self.assertEqual(data['pk'], 1)
|
||||
|
||||
def test_transfer_order_attachments(self):
|
||||
"""Test the list endpoint for the Transfer Order Attachments."""
|
||||
url = reverse('api-attachment-list')
|
||||
|
||||
# Filter by 'transferorder'
|
||||
self.get(
|
||||
url, data={'model_type': 'transferorder', 'model_id': 1}, expected_code=200
|
||||
)
|
||||
|
||||
def test_transfer_order_operations(self):
|
||||
"""Test that we can create / edit and delete a TransferOrder via the API."""
|
||||
n = models.TransferOrder.objects.count()
|
||||
|
||||
url = reverse('api-transfer-order-list')
|
||||
|
||||
# Initially we do not have "add" permission for the TransferOrder model,
|
||||
# so this POST request should return 403 (denied)
|
||||
response = self.post(
|
||||
url,
|
||||
{'reference': 'TO-43245', 'description': 'Transfer order'},
|
||||
expected_code=403,
|
||||
)
|
||||
|
||||
self.assignRole('transfer_order.add')
|
||||
|
||||
# Now we should be able to create a TransferOrder via the API
|
||||
response = self.post(
|
||||
url,
|
||||
{'reference': 'TO-12345', 'description': 'Transfer order'},
|
||||
expected_code=201,
|
||||
)
|
||||
|
||||
# Check that the new order has been created
|
||||
self.assertEqual(models.TransferOrder.objects.count(), n + 1)
|
||||
|
||||
# Grab the PK for the newly created TransferOrder
|
||||
pk = response.data['pk']
|
||||
|
||||
# Basic checks against the newly created TransferOrder
|
||||
so = models.TransferOrder.objects.get(pk=pk)
|
||||
self.assertEqual(so.reference, 'TO-12345')
|
||||
self.assertEqual(so.created_by.username, 'testuser')
|
||||
|
||||
# Try to create a TO with identical reference (should fail)
|
||||
response = self.post(
|
||||
url,
|
||||
{
|
||||
'customer': 4,
|
||||
'reference': 'TO-12345',
|
||||
'description': 'Another transfer order',
|
||||
},
|
||||
expected_code=400,
|
||||
)
|
||||
|
||||
url = reverse('api-transfer-order-detail', kwargs={'pk': pk})
|
||||
|
||||
# Extract detail info for the TransferOrder
|
||||
response = self.get(url)
|
||||
self.assertEqual(response.data['reference'], 'TO-12345')
|
||||
|
||||
# Try to alter (edit) the TransferOrder
|
||||
# Initially try with an invalid reference field value
|
||||
response = self.patch(url, {'reference': 'TO-12345-a'}, expected_code=400)
|
||||
|
||||
response = self.patch(url, {'reference': 'TO-12346'}, expected_code=200)
|
||||
|
||||
# Reference should have changed
|
||||
self.assertEqual(response.data['reference'], 'TO-12346')
|
||||
|
||||
# Now, let's try to delete this TransferOrder
|
||||
# Initially, we do not have the required permission
|
||||
response = self.delete(url, expected_code=403)
|
||||
|
||||
self.assignRole('transfer_order.delete')
|
||||
|
||||
response = self.delete(url, expected_code=204)
|
||||
|
||||
# Check that the number of transfer orders has decreased
|
||||
self.assertEqual(models.TransferOrder.objects.count(), n)
|
||||
|
||||
# And the resource should no longer be available
|
||||
response = self.get(url, expected_code=404)
|
||||
|
||||
def test_transfer_order_create(self):
|
||||
"""Test that we can create a new TransferOrder via the API."""
|
||||
self.assignRole('transfer_order.add')
|
||||
|
||||
url = reverse('api-transfer-order-list')
|
||||
|
||||
# Will fail due to invalid reference field
|
||||
response = self.post(
|
||||
url,
|
||||
{'reference': '1234566778', 'description': 'A test transfer order'},
|
||||
expected_code=400,
|
||||
)
|
||||
|
||||
self.assertIn(
|
||||
'Reference must match required pattern', str(response.data['reference'])
|
||||
)
|
||||
|
||||
self.post(
|
||||
url,
|
||||
{'reference': 'TO-12345', 'description': 'A better test transfer order'},
|
||||
expected_code=201,
|
||||
)
|
||||
|
||||
def test_transfer_order_cancel(self):
|
||||
"""Test API endpoint for cancelling a TransferOrder."""
|
||||
to = models.TransferOrder.objects.get(pk=1)
|
||||
|
||||
self.assertEqual(to.status, TransferOrderStatus.PENDING)
|
||||
|
||||
url = reverse('api-transfer-order-cancel', kwargs={'pk': to.pk})
|
||||
|
||||
# Try to cancel, without permission
|
||||
self.post(url, {}, expected_code=403)
|
||||
|
||||
self.assignRole('transfer_order.add')
|
||||
|
||||
self.post(url, {}, expected_code=201)
|
||||
|
||||
to.refresh_from_db()
|
||||
|
||||
self.assertEqual(to.status, TransferOrderStatus.CANCELLED)
|
||||
|
||||
def test_transfer_order_hold(self):
|
||||
"""Test API endpoint for holdling a TransferOrder."""
|
||||
to = models.TransferOrder.objects.get(pk=1)
|
||||
|
||||
self.assertEqual(to.status, TransferOrderStatus.PENDING)
|
||||
|
||||
url = reverse('api-transfer-order-hold', kwargs={'pk': to.pk})
|
||||
|
||||
# Try to hold, without permission
|
||||
self.post(url, {}, expected_code=403)
|
||||
|
||||
self.assignRole('transfer_order.add')
|
||||
|
||||
self.post(url, {}, expected_code=201)
|
||||
|
||||
to.refresh_from_db()
|
||||
|
||||
self.assertEqual(to.status, TransferOrderStatus.ON_HOLD)
|
||||
|
||||
def test_transfer_order_calendar(self):
|
||||
"""Test the calendar export endpoint."""
|
||||
# Create required transfer orders
|
||||
self.assignRole('transfer_order.add')
|
||||
|
||||
for i in range(1, 9):
|
||||
self.post(
|
||||
reverse('api-transfer-order-list'),
|
||||
{
|
||||
'reference': f'TO-1100000{i}',
|
||||
'description': f'Calendar SO {i}',
|
||||
'target_date': f'2024-12-{i:02d}',
|
||||
},
|
||||
expected_code=201,
|
||||
)
|
||||
|
||||
# Cancel a few orders - these will not show in incomplete view below
|
||||
for to in models.TransferOrder.objects.filter(target_date__isnull=False):
|
||||
if to.reference in [
|
||||
'TO-11000006',
|
||||
'TO-11000007',
|
||||
'TO-11000008',
|
||||
'TO-11000009',
|
||||
]:
|
||||
self.post(
|
||||
reverse('api-transfer-order-cancel', kwargs={'pk': to.pk}),
|
||||
expected_code=201,
|
||||
)
|
||||
|
||||
url = reverse('api-po-so-calendar', kwargs={'ordertype': 'transfer-order'})
|
||||
|
||||
# Test without completed orders
|
||||
response = self.get(url, expected_code=200, format=None)
|
||||
|
||||
number_orders = len(
|
||||
models.TransferOrder.objects.filter(target_date__isnull=False).filter(
|
||||
status__lt=TransferOrderStatus.COMPLETE.value
|
||||
)
|
||||
)
|
||||
|
||||
# Transform content to a Calendar object
|
||||
calendar = Calendar.from_ical(response.content)
|
||||
n_events = 0
|
||||
# Count number of events in calendar
|
||||
for component in calendar.walk():
|
||||
if component.name == 'VEVENT':
|
||||
n_events += 1
|
||||
|
||||
self.assertGreaterEqual(n_events, 1)
|
||||
self.assertEqual(number_orders, n_events)
|
||||
|
||||
# Test with completed orders
|
||||
response = self.get(
|
||||
url, data={'include_completed': 'True'}, expected_code=200, format=None
|
||||
)
|
||||
|
||||
number_orders_incl_complete = len(
|
||||
models.TransferOrder.objects.filter(target_date__isnull=False)
|
||||
)
|
||||
self.assertGreater(number_orders_incl_complete, number_orders)
|
||||
|
||||
# Transform content to a Calendar object
|
||||
calendar = Calendar.from_ical(response.content)
|
||||
n_events = 0
|
||||
# Count number of events in calendar
|
||||
for component in calendar.walk():
|
||||
if component.name == 'VEVENT':
|
||||
n_events += 1
|
||||
|
||||
self.assertGreaterEqual(n_events, 1)
|
||||
self.assertEqual(number_orders_incl_complete, n_events)
|
||||
|
||||
def test_export(self):
|
||||
"""Test we can export the TransferOrder list."""
|
||||
n = models.TransferOrder.objects.count()
|
||||
|
||||
# Check there are some sales orders
|
||||
self.assertGreater(n, 0)
|
||||
|
||||
# Download file, check we get a 200 response
|
||||
for fmt in ['csv', 'xlsx', 'tsv']:
|
||||
self.export_data(
|
||||
reverse('api-transfer-order-list'),
|
||||
export_format=fmt,
|
||||
decode=fmt == 'csv',
|
||||
expected_code=200,
|
||||
expected_fn=r'InvenTree_TransferOrder_.+',
|
||||
)
|
||||
|
||||
def test_transfer_order_complete(self):
|
||||
"""Tests for marking a TransferOrder as complete."""
|
||||
self.assignRole('transfer_order.add')
|
||||
destination = StockLocation.objects.first()
|
||||
# Let's create a TransferOrder
|
||||
to = models.TransferOrder.objects.create(
|
||||
reference='TO-12345', description='Test TO'
|
||||
)
|
||||
|
||||
self.assertEqual(to.status, TransferOrderStatus.PENDING.value)
|
||||
|
||||
# Create a line item
|
||||
part = Part.objects.exclude(virtual=True).first()
|
||||
|
||||
line = models.TransferOrderLineItem.objects.create(
|
||||
order=to, part=part, quantity=10
|
||||
)
|
||||
|
||||
# issue the order
|
||||
url = reverse('api-transfer-order-issue', kwargs={'pk': to.pk})
|
||||
self.post(url, {}, expected_code=201)
|
||||
to.refresh_from_db()
|
||||
self.assertEqual(to.status, TransferOrderStatus.ISSUED.value)
|
||||
|
||||
# Allocate some stock
|
||||
item = StockItem.objects.create(
|
||||
part=part, quantity=100, location=None, batch='transfer-order-test'
|
||||
)
|
||||
short_allocation = models.TransferOrderAllocation.objects.create(
|
||||
quantity=5, line=line, item=item
|
||||
)
|
||||
|
||||
# attempt to complete the order, but fail because there are incomplete allocations
|
||||
url = reverse('api-transfer-order-complete', kwargs={'pk': to.pk})
|
||||
response = self.post(url, {}, expected_code=400)
|
||||
self.assertIn('has incomplete allocations', str(response.data))
|
||||
# allocate more stock
|
||||
short_allocation.delete()
|
||||
models.TransferOrderAllocation.objects.create(quantity=10, line=line, item=item)
|
||||
|
||||
# attempt to complete the order, but fail because there is no destination yet
|
||||
url = reverse('api-transfer-order-complete', kwargs={'pk': to.pk})
|
||||
response = self.post(url, {}, expected_code=400)
|
||||
self.assertIn('until a destination location is set', str(response.data))
|
||||
# add destination
|
||||
to.destination = destination
|
||||
to.save()
|
||||
|
||||
# Ok, now we should be able to "complete" the transfer via the API
|
||||
url = reverse('api-transfer-order-complete', kwargs={'pk': to.pk})
|
||||
self.post(url, {}, expected_code=201)
|
||||
|
||||
to.refresh_from_db()
|
||||
self.assertEqual(to.status, TransferOrderStatus.COMPLETE.value)
|
||||
self.assertIsNotNone(to.complete_date)
|
||||
|
||||
# Now, let's try *again* (it should fail as the order is already complete)
|
||||
response = self.post(url, {}, expected_code=400)
|
||||
self.assertIn('Order is already complete', str(response.data))
|
||||
|
||||
# Now, we make sure the affected stock was transferred to the correct location
|
||||
StockItem.objects.get(
|
||||
part=part, quantity=10, batch='transfer-order-test', location=destination
|
||||
)
|
||||
|
||||
def test_transfer_order_consume(self):
|
||||
"""Tests for marking a TransferOrder consume the stock it 'transfers'."""
|
||||
self.assignRole('transfer_order.add')
|
||||
destination = StockLocation.objects.first()
|
||||
# Let's create a TransferOrder
|
||||
to = models.TransferOrder.objects.create(
|
||||
reference='TO-12345',
|
||||
description='Test TO',
|
||||
consume=True,
|
||||
destination=destination,
|
||||
)
|
||||
|
||||
self.assertEqual(to.status, TransferOrderStatus.PENDING.value)
|
||||
|
||||
# Create a line item
|
||||
part = Part.objects.exclude(virtual=True).first()
|
||||
|
||||
line = models.TransferOrderLineItem.objects.create(
|
||||
order=to, part=part, quantity=10
|
||||
)
|
||||
|
||||
# issue the order
|
||||
url = reverse('api-transfer-order-issue', kwargs={'pk': to.pk})
|
||||
self.post(url, {}, expected_code=201)
|
||||
to.refresh_from_db()
|
||||
self.assertEqual(to.status, TransferOrderStatus.ISSUED.value)
|
||||
|
||||
# Allocate some stock
|
||||
item = StockItem.objects.create(
|
||||
part=part, quantity=100, location=None, batch='transfer-order-test'
|
||||
)
|
||||
models.TransferOrderAllocation.objects.create(quantity=10, line=line, item=item)
|
||||
|
||||
# Ok, now we should be able to "complete" the transfer via the API
|
||||
url = reverse('api-transfer-order-complete', kwargs={'pk': to.pk})
|
||||
self.post(url, {}, expected_code=201)
|
||||
|
||||
to.refresh_from_db()
|
||||
self.assertEqual(to.status, TransferOrderStatus.COMPLETE.value)
|
||||
self.assertIsNotNone(to.complete_date)
|
||||
|
||||
# Now, we make sure the affected stock was 'consumed', reducing available quantity
|
||||
item.refresh_from_db()
|
||||
self.assertEqual(item.quantity, 90)
|
||||
|
||||
# and that it wasn't transferred to the destination
|
||||
with self.assertRaises(StockItem.DoesNotExist):
|
||||
StockItem.objects.get(
|
||||
part=part,
|
||||
quantity=10,
|
||||
batch='transfer-order-test',
|
||||
location=destination,
|
||||
)
|
||||
|
||||
def test_output_options(self):
|
||||
"""Test the output options for the TransferOrder detail endpoint."""
|
||||
self.run_output_test(
|
||||
reverse('api-transfer-order-detail', kwargs={'pk': 1}),
|
||||
['take_from_detail', 'destination_detail'],
|
||||
)
|
||||
|
||||
|
||||
class TransferOrderLineItemTest(OrderTest):
|
||||
"""Tests for the TransferOrderLineItem API."""
|
||||
|
||||
LIST_URL = reverse('api-transfer-order-line-list')
|
||||
|
||||
# adjust counts in asserts based on those created in setUpTestData
|
||||
# plus those in fixtures
|
||||
NUM_LINE_ITEMS_IN_FIXTURES = 2
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
"""Init routine for this unit test class."""
|
||||
super().setUpTestData()
|
||||
|
||||
# List of 'transferrable' parts
|
||||
parts = Part.objects.exclude(virtual=True)
|
||||
|
||||
lines = []
|
||||
|
||||
# Create a bunch of TransferOrderLineItems for each order
|
||||
for idx, to in enumerate(models.TransferOrder.objects.all()):
|
||||
for part in parts:
|
||||
lines.append(
|
||||
models.TransferOrderLineItem(
|
||||
order=to,
|
||||
part=part,
|
||||
quantity=(idx + 1) * 5,
|
||||
reference=f'Order {to.reference} - line {idx}',
|
||||
)
|
||||
)
|
||||
|
||||
# Bulk create
|
||||
models.TransferOrderLineItem.objects.bulk_create(lines)
|
||||
|
||||
cls.url = reverse('api-transfer-order-line-list')
|
||||
|
||||
def test_transfer_order_line_list(self):
|
||||
"""Test list endpoint."""
|
||||
response = self.get(self.url, {}, expected_code=200)
|
||||
|
||||
n = models.TransferOrderLineItem.objects.count()
|
||||
|
||||
# We should have received *all* lines
|
||||
self.assertEqual(len(response.data), n)
|
||||
|
||||
# List *all* lines, but paginate
|
||||
response = self.get(self.url, {'limit': 5}, expected_code=200)
|
||||
|
||||
self.assertEqual(response.data['count'], n)
|
||||
self.assertEqual(len(response.data['results']), 5)
|
||||
|
||||
n_orders = models.TransferOrder.objects.count()
|
||||
n_parts = Part.objects.exclude(virtual=True).count()
|
||||
|
||||
# List by part
|
||||
# fixures add line items, avoid those here with [:3] for predictable counts
|
||||
for part in Part.objects.exclude(virtual=True)[:3]:
|
||||
response = self.get(self.url, {'part': part.pk, 'limit': 10})
|
||||
self.assertEqual(response.data['count'], n_orders)
|
||||
|
||||
# List by order
|
||||
# fixures add line items, avoid those here with [:3] for predictable counts
|
||||
for order in models.TransferOrder.objects.all()[:3]:
|
||||
response = self.get(self.url, {'order': order.pk, 'limit': 10})
|
||||
# count of line items equal to number of parts because
|
||||
# we created a line item per part on each order in setUpTestData
|
||||
self.assertEqual(response.data['count'], n_parts)
|
||||
|
||||
# Filter by 'completed' status
|
||||
self.filter({'completed': 1}, 1)
|
||||
self.filter({'completed': 0}, n - 1)
|
||||
|
||||
# Filter by 'allocated' status
|
||||
self.filter({'allocated': 'true'}, 2)
|
||||
self.filter({'allocated': 'false'}, n - 2)
|
||||
|
||||
def test_transfer_order_line_allocated_filters(self):
|
||||
"""Test filtering by allocation status for a TransferOrderLineItem."""
|
||||
self.assignRole('transfer_order.add')
|
||||
|
||||
destination = StockLocation.objects.first()
|
||||
assert destination
|
||||
|
||||
response = self.post(
|
||||
reverse('api-transfer-order-list'),
|
||||
{
|
||||
'reference': 'TO-12345',
|
||||
'description': 'Test Transfer Order',
|
||||
'destination': destination.pk,
|
||||
},
|
||||
)
|
||||
|
||||
order_id = response.data['pk']
|
||||
order = models.TransferOrder.objects.get(pk=order_id)
|
||||
|
||||
transfer_order_line_url = reverse('api-transfer-order-line-list')
|
||||
|
||||
# Initially, there should be no line items against this order
|
||||
response = self.get(transfer_order_line_url, {'order': order_id})
|
||||
|
||||
self.assertEqual(len(response.data), 0)
|
||||
|
||||
parts = [25, 50, 100]
|
||||
|
||||
# Let's create some new line items
|
||||
for part_id in parts:
|
||||
self.post(
|
||||
transfer_order_line_url,
|
||||
{'order': order_id, 'part': part_id, 'quantity': 10},
|
||||
)
|
||||
|
||||
# Should be three items now
|
||||
response = self.get(transfer_order_line_url, {'order': order_id})
|
||||
|
||||
self.assertEqual(len(response.data), 3)
|
||||
|
||||
for item in response.data:
|
||||
# Check that the line item has been created
|
||||
self.assertEqual(item['order'], order_id)
|
||||
|
||||
# Check that the line quantities are correct
|
||||
self.assertEqual(item['quantity'], 10)
|
||||
self.assertEqual(item['allocated'], 0)
|
||||
self.assertEqual(item['transferred'], 0)
|
||||
|
||||
# Initial API filters should return no results
|
||||
self.filter({'order': order_id, 'allocated': 1}, 0)
|
||||
self.filter({'order': order_id, 'completed': 1}, 0)
|
||||
|
||||
# issue the order
|
||||
order_issue_url = reverse('api-transfer-order-issue', kwargs={'pk': order.pk})
|
||||
self.post(order_issue_url, {}, expected_code=201)
|
||||
|
||||
# Next, allocate stock against 2 line items
|
||||
for item in parts[:2]:
|
||||
p = Part.objects.get(pk=item)
|
||||
s = StockItem.objects.create(part=p, quantity=100)
|
||||
l = models.TransferOrderLineItem.objects.filter(order=order, part=p).first()
|
||||
assert l
|
||||
|
||||
# Allocate against the API
|
||||
self.post(
|
||||
reverse('api-transfer-order-allocate', kwargs={'pk': order.pk}),
|
||||
{'items': [{'line_item': l.pk, 'stock_item': s.pk, 'quantity': 10}]},
|
||||
)
|
||||
|
||||
# Filter by 'fully allocated' status
|
||||
self.filter({'order': order_id, 'allocated': 1}, 2)
|
||||
self.filter({'order': order_id, 'allocated': 0}, 1)
|
||||
|
||||
self.filter({'order': order_id, 'completed': 1}, 0)
|
||||
self.filter({'order': order_id, 'completed': 0}, 3)
|
||||
|
||||
# Finally, attempt to transfer this line item
|
||||
# we have incomplete allocations, so must specify arg
|
||||
self.post(
|
||||
reverse('api-transfer-order-complete', kwargs={'pk': order.pk}),
|
||||
{'accept_incomplete_allocation': 'true'},
|
||||
)
|
||||
|
||||
# Filter by 'completed' status
|
||||
self.filter({'order': order_id, 'completed': 1}, 2)
|
||||
self.filter({'order': order_id, 'completed': 0}, 1)
|
||||
|
||||
def test_output_options(self):
|
||||
"""Test the various output options for the TransferOrderLineItem detail endpoint."""
|
||||
self.run_output_test(
|
||||
reverse('api-transfer-order-line-detail', kwargs={'pk': 1}),
|
||||
['part_detail', 'order_detail'],
|
||||
)
|
||||
|
||||
|
||||
class TransferOrderDownloadTest(OrderTest):
|
||||
"""Unit tests for downloading TransferOrder data via the API endpoint."""
|
||||
|
||||
def test_download_fail(self):
|
||||
"""Test that downloading without the 'export' option fails."""
|
||||
url = reverse('api-transfer-order-list')
|
||||
|
||||
response = self.export_data(url, export_plugin='no-plugin', expected_code=400)
|
||||
self.assertIn('is not a valid choice', str(response['export_plugin']))
|
||||
|
||||
def test_download_xlsx(self):
|
||||
"""Test xlsx file download."""
|
||||
url = reverse('api-transfer-order-list')
|
||||
|
||||
# Download .xls file
|
||||
with self.export_data(
|
||||
url, export_format='xlsx', expected_code=200, decode=False
|
||||
) as file:
|
||||
self.assertIsInstance(file, io.BytesIO)
|
||||
|
||||
def test_download_csv(self):
|
||||
"""Test that the list of transfer orders can be downloaded as a .csv file."""
|
||||
url = reverse('api-transfer-order-list')
|
||||
|
||||
required_cols = [
|
||||
'Line Items',
|
||||
'Completed Lines',
|
||||
'ID',
|
||||
'Reference',
|
||||
'Order Status',
|
||||
'Description',
|
||||
'Project Code',
|
||||
'Responsible',
|
||||
'Consume Stock',
|
||||
]
|
||||
|
||||
excluded_cols = ['metadata']
|
||||
|
||||
# Download .xls file
|
||||
with self.export_data(url, export_format='csv') as file:
|
||||
data = self.process_csv(
|
||||
file,
|
||||
required_cols=required_cols,
|
||||
excluded_cols=excluded_cols,
|
||||
required_rows=models.TransferOrder.objects.count(),
|
||||
)
|
||||
|
||||
for line in data:
|
||||
order = models.TransferOrder.objects.get(pk=line['ID'])
|
||||
|
||||
self.assertEqual(line['Description'], order.description)
|
||||
self.assertEqual(line['Order Status'], str(order.status))
|
||||
|
||||
# Download only outstanding transfer orders
|
||||
with self.export_data(url, {'outstanding': True}, export_format='tsv') as file:
|
||||
self.process_csv(
|
||||
file,
|
||||
required_cols=required_cols,
|
||||
excluded_cols=excluded_cols,
|
||||
required_rows=models.TransferOrder.objects.filter(
|
||||
status__in=TransferOrderStatusGroups.OPEN
|
||||
).count(),
|
||||
delimiter='\t',
|
||||
)
|
||||
|
||||
|
||||
class TransferOrderAllocateTest(OrderTest):
|
||||
"""Unit tests for allocating stock items against a TransferOrder."""
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
"""Init routine for this unit test class."""
|
||||
super().setUpTestData()
|
||||
|
||||
def setUp(self):
|
||||
"""Init routines for this unit testing class."""
|
||||
super().setUp()
|
||||
|
||||
self.assignRole('transfer_order.add')
|
||||
|
||||
self.url = reverse('api-transfer-order-allocate', kwargs={'pk': 1})
|
||||
self.url_serialized = reverse(
|
||||
'api-transfer-order-allocate-serials', kwargs={'pk': 1}
|
||||
)
|
||||
|
||||
self.order = models.TransferOrder.objects.get(pk=1)
|
||||
|
||||
# Create some line items for this transfer order
|
||||
parts = Part.objects.exclude(virtual=True)
|
||||
|
||||
for part in parts:
|
||||
# Create a new line item
|
||||
models.TransferOrderLineItem.objects.create(
|
||||
order=self.order, part=part, quantity=5
|
||||
)
|
||||
|
||||
# Ensure we have stock!
|
||||
StockItem.objects.create(part=part, quantity=100)
|
||||
|
||||
# Create a new shipment against this TransferOrder
|
||||
# self.shipment = models.TransferOrderShipment.objects.create(order=self.order)
|
||||
|
||||
def test_invalid(self):
|
||||
"""Test POST with invalid data."""
|
||||
# No data
|
||||
response = self.post(self.url, {}, expected_code=400)
|
||||
|
||||
self.assertIn('This field is required', str(response.data['items']))
|
||||
|
||||
# Test with a single line items
|
||||
line = self.order.lines.first()
|
||||
part = line.part
|
||||
|
||||
# Valid stock_item, but quantity is invalid
|
||||
data = {
|
||||
'items': [
|
||||
{
|
||||
'line_item': line.pk,
|
||||
'stock_item': part.stock_items.last().pk,
|
||||
'quantity': 0,
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
response = self.post(self.url, data, expected_code=400)
|
||||
|
||||
self.assertIn('Quantity must be positive', str(response.data['items']))
|
||||
|
||||
# Valid stock item, too much quantity
|
||||
data['items'][0]['quantity'] = 250
|
||||
|
||||
response = self.post(self.url, data, expected_code=400)
|
||||
|
||||
self.assertIn('Available quantity (100) exceeded', str(response.data['items']))
|
||||
|
||||
def test_allocate(self):
|
||||
"""Test that the allocation endpoint acts as expected, when provided with valid data!"""
|
||||
# First, check that there are no line items allocated against this TransferOrder
|
||||
self.assertEqual(self.order.stock_allocations.count(), 0)
|
||||
|
||||
data = {'items': []}
|
||||
|
||||
for line in self.order.lines.all():
|
||||
for stock_item in line.part.stock_items.filter(quantity__gt=5):
|
||||
# Find a non-serialized stock item to allocate
|
||||
if not stock_item.serialized:
|
||||
break
|
||||
|
||||
# Fully-allocate each line
|
||||
data['items'].append({
|
||||
'line_item': line.pk,
|
||||
'stock_item': stock_item.pk,
|
||||
'quantity': 5,
|
||||
})
|
||||
|
||||
self.post(self.url, data, expected_code=201)
|
||||
|
||||
# There should have been 1 stock item allocated against each line item
|
||||
n_lines = self.order.lines.count()
|
||||
|
||||
self.assertEqual(self.order.stock_allocations.count(), n_lines)
|
||||
|
||||
for line in self.order.lines.all():
|
||||
self.assertEqual(line.allocations.count(), 1)
|
||||
|
||||
def test_allocate_serials(self):
|
||||
"""Test that the allocation endpoint acts as expected, when provided with serials."""
|
||||
self.assertEqual(self.order.stock_allocations.count(), 0)
|
||||
|
||||
trackable_lines = self.order.lines.filter(part__trackable=True)
|
||||
for line in trackable_lines:
|
||||
stock_item = (
|
||||
line.part.stock_items
|
||||
.exclude(serial=None)
|
||||
.filter(StockItem.IN_STOCK_FILTER)
|
||||
.first()
|
||||
)
|
||||
|
||||
# Allocate this serialized item to the transfer order
|
||||
data = {
|
||||
'line_item': line.pk,
|
||||
'quantity': 1,
|
||||
'serial_numbers': stock_item.serial,
|
||||
}
|
||||
|
||||
self.post(self.url_serialized, data, expected_code=201)
|
||||
|
||||
# There should have been 1 stock item allocated against each line item
|
||||
n_lines = trackable_lines.count()
|
||||
self.assertEqual(self.order.stock_allocations.count(), n_lines)
|
||||
|
||||
for line in trackable_lines.all():
|
||||
self.assertEqual(line.allocations.count(), 1)
|
||||
|
||||
def test_allocate_variant(self):
|
||||
"""Test that the allocation endpoint acts as expected, when provided with variant."""
|
||||
# First, check that there are no line items allocated against this TransferOrder
|
||||
self.assertEqual(self.order.stock_allocations.count(), 0)
|
||||
|
||||
data = {'items': []}
|
||||
|
||||
def check_template(line_item):
|
||||
return line_item.part.is_template
|
||||
|
||||
for line in filter(check_template, self.order.lines.all()):
|
||||
stock_item: Optional[StockItem] = None
|
||||
|
||||
stock_item = None
|
||||
|
||||
# Allocate a matching variant
|
||||
parts: list[Part] = (
|
||||
Part.objects
|
||||
.exclude(virtual=True)
|
||||
.exclude(is_template=True)
|
||||
.filter(variant_of=line.part.pk)
|
||||
)
|
||||
# if we don't have a matching variant, continue
|
||||
if not parts.exists():
|
||||
continue
|
||||
for part in parts:
|
||||
# ensure we have the quantity necessary to allocate
|
||||
if not part.stock_items.filter(quantity__gt=5).exists():
|
||||
continue
|
||||
|
||||
stock_item = part.stock_items.last()
|
||||
|
||||
for item in part.stock_items.filter(quantity__gt=5):
|
||||
if item.serialized:
|
||||
continue
|
||||
|
||||
stock_item = item
|
||||
break
|
||||
|
||||
if stock_item is not None:
|
||||
break
|
||||
|
||||
if stock_item is None:
|
||||
raise self.fail('No stock item found for part') # pragma: no cover
|
||||
|
||||
# Fully-allocate each line
|
||||
data['items'].append({
|
||||
'line_item': line.pk,
|
||||
'stock_item': stock_item.pk,
|
||||
'quantity': 5,
|
||||
})
|
||||
|
||||
self.post(self.url, data, expected_code=201)
|
||||
|
||||
# At least one item should be allocated, and all should be variants
|
||||
self.assertGreater(self.order.stock_allocations.count(), 0)
|
||||
for allocation in self.order.stock_allocations.all():
|
||||
self.assertNotEqual(allocation.item.part.pk, allocation.line.part.pk)
|
||||
|
||||
def test_output_options(self):
|
||||
"""Test the various output options for the SalesOrderAllocation detail endpoint."""
|
||||
self.run_output_test(
|
||||
reverse('api-transfer-order-allocation-list'),
|
||||
['part_detail', 'item_detail', 'order_detail', 'location_detail'],
|
||||
assert_subset=True,
|
||||
)
|
||||
|
||||
@@ -22,6 +22,13 @@ def generate_next_return_order_reference():
|
||||
return ReturnOrder.generate_reference()
|
||||
|
||||
|
||||
def generate_next_transfer_order_reference():
|
||||
"""Generate the next available TransferOrder reference."""
|
||||
from order.models import TransferOrder
|
||||
|
||||
return TransferOrder.generate_reference()
|
||||
|
||||
|
||||
def validate_sales_order_reference_pattern(pattern):
|
||||
"""Validate the SalesOrder reference 'pattern' setting."""
|
||||
from order.models import SalesOrder
|
||||
@@ -62,3 +69,17 @@ def validate_return_order_reference(value):
|
||||
from order.models import ReturnOrder
|
||||
|
||||
ReturnOrder.validate_reference_field(value)
|
||||
|
||||
|
||||
def validate_transfer_order_reference_pattern(pattern):
|
||||
"""Validate the TransferOrder reference 'pattern' setting."""
|
||||
from order.models import TransferOrder
|
||||
|
||||
TransferOrder.validate_reference_pattern(pattern)
|
||||
|
||||
|
||||
def validate_transfer_order_reference(value):
|
||||
"""Validate that the ReturnOrder reference field matches the required pattern."""
|
||||
from order.models import TransferOrder
|
||||
|
||||
TransferOrder.validate_reference_field(value)
|
||||
|
||||
@@ -37,7 +37,11 @@ from sql_util.utils import SubquerySum
|
||||
import part.models
|
||||
import stock.models
|
||||
from build.status_codes import BuildStatusGroups
|
||||
from order.status_codes import PurchaseOrderStatusGroups, SalesOrderStatusGroups
|
||||
from order.status_codes import (
|
||||
PurchaseOrderStatusGroups,
|
||||
SalesOrderStatusGroups,
|
||||
TransferOrderStatusGroups,
|
||||
)
|
||||
|
||||
|
||||
def annotate_in_production_quantity(reference: str = '') -> QuerySet:
|
||||
@@ -274,6 +278,40 @@ def annotate_sales_order_allocations(reference: str = '', location=None) -> Quer
|
||||
)
|
||||
|
||||
|
||||
def annotate_transfer_order_allocations(reference: str = '', location=None) -> QuerySet:
|
||||
"""Annotate the total quantity of each part allocated to transfer orders.
|
||||
|
||||
- This function calculates the total part quantity allocated to open transfer orders"
|
||||
- Finds all transfer order allocations for each part (using the provided filter)
|
||||
- Aggregates the 'allocated quantity' for each relevant transfer order allocation item
|
||||
|
||||
Arguments:
|
||||
reference: The relationship reference of the part from the current model
|
||||
location: If provided, only allocated stock items from this location are considered
|
||||
"""
|
||||
# Order filter only returns open orders
|
||||
order_filter = Q(line__order__status__in=TransferOrderStatusGroups.OPEN)
|
||||
|
||||
if location is not None:
|
||||
# Filter by location (including any child locations)
|
||||
|
||||
order_filter &= Q(
|
||||
item__location__tree_id=location.tree_id,
|
||||
item__location__lft__gte=location.lft,
|
||||
item__location__rght__lte=location.rght,
|
||||
item__location__level__gte=location.level,
|
||||
)
|
||||
|
||||
return Coalesce(
|
||||
SubquerySum(
|
||||
f'{reference}stock_items__transfer_order_allocations__quantity',
|
||||
filter=order_filter,
|
||||
),
|
||||
Decimal(0),
|
||||
output_field=models.DecimalField(),
|
||||
)
|
||||
|
||||
|
||||
def variant_stock_query(reference: str = '', filter: Optional[Q] = None) -> QuerySet:
|
||||
"""Create a queryset to retrieve all stock items for variant parts under the specified part.
|
||||
|
||||
|
||||
@@ -60,6 +60,7 @@ from order.status_codes import (
|
||||
PurchaseOrderStatus,
|
||||
PurchaseOrderStatusGroups,
|
||||
SalesOrderStatusGroups,
|
||||
TransferOrderStatusGroups,
|
||||
)
|
||||
from stock import models as StockModels
|
||||
|
||||
@@ -1768,8 +1769,50 @@ class Part(
|
||||
|
||||
return query['total']
|
||||
|
||||
def transfer_order_allocations(self, **kwargs):
|
||||
"""Return all transfer-order-allocation objects which allocate this part to a TransferOrder."""
|
||||
include_variants = kwargs.get('include_variants', True)
|
||||
|
||||
queryset = OrderModels.TransferOrderAllocation.objects.all()
|
||||
|
||||
if include_variants:
|
||||
# Include allocations for all variants
|
||||
variants = self.get_descendants(include_self=True)
|
||||
queryset = queryset.filter(item__part__in=variants)
|
||||
else:
|
||||
# Only look at this part
|
||||
queryset = queryset.filter(item__part=self)
|
||||
|
||||
# Default behaviour is to only return *pending* allocations
|
||||
pending = kwargs.get('pending', True)
|
||||
|
||||
if pending is True:
|
||||
# Look only for 'open' orders
|
||||
queryset = queryset.filter(
|
||||
line__order__status__in=TransferOrderStatusGroups.OPEN
|
||||
)
|
||||
elif pending is False:
|
||||
# Look only for 'closed' orders
|
||||
queryset = queryset.exclude(
|
||||
line__order__status__in=TransferOrderStatusGroups.OPEN
|
||||
)
|
||||
|
||||
return queryset
|
||||
|
||||
def transfer_order_allocation_count(self, **kwargs):
|
||||
"""Return the total quantity of this part allocated to transfer orders."""
|
||||
query = self.transfer_order_allocations(**kwargs).aggregate(
|
||||
total=Coalesce(
|
||||
Sum('quantity', output_field=models.DecimalField()),
|
||||
0,
|
||||
output_field=models.DecimalField(),
|
||||
)
|
||||
)
|
||||
|
||||
return query['total']
|
||||
|
||||
def allocation_count(self, **kwargs):
|
||||
"""Return the total quantity of stock allocated for this part, against both build orders and sales orders."""
|
||||
"""Return the total quantity of stock allocated for this part, against build orders, sales orders, and transfer orders."""
|
||||
if self.id is None:
|
||||
# If this instance has not been saved, foreign-key lookups will fail
|
||||
return 0
|
||||
@@ -1777,6 +1820,8 @@ class Part(
|
||||
return sum([
|
||||
self.build_order_allocation_count(**kwargs),
|
||||
self.sales_order_allocation_count(**kwargs),
|
||||
# For now, stock allocated to a transfer order will not impact its availability
|
||||
# self.transfer_order_allocation_count(**kwargs),
|
||||
])
|
||||
|
||||
def stock_entries(
|
||||
|
||||
@@ -735,6 +735,8 @@ class PartSerializer(
|
||||
ordering=part_filters.annotate_on_order_quantity(),
|
||||
in_stock=part_filters.annotate_total_stock(),
|
||||
allocated_to_sales_orders=part_filters.annotate_sales_order_allocations(),
|
||||
# NOTE: for now, decided that allocations to Transfer Orders don't reduce available stock
|
||||
# allocated_to_transfer_orders=part_filters.annotate_transfer_order_allocations(),
|
||||
allocated_to_build_orders=part_filters.annotate_build_order_allocations(),
|
||||
)
|
||||
|
||||
@@ -759,6 +761,8 @@ class PartSerializer(
|
||||
ExpressionWrapper(
|
||||
F('total_in_stock')
|
||||
- F('allocated_to_sales_orders')
|
||||
# NOTE: for now, decided that allocations to Transfer Orders don't reduce available stock
|
||||
# - F('allocated_to_transfer_orders'),
|
||||
- F('allocated_to_build_orders'),
|
||||
output_field=models.DecimalField(),
|
||||
),
|
||||
@@ -768,6 +772,8 @@ class PartSerializer(
|
||||
)
|
||||
|
||||
# Annotate with the total 'required for builds' quantity
|
||||
# NOTE: for now, we don't consider transfer orders for required quantities
|
||||
# and they are assumed to operate on stock that already exists.
|
||||
queryset = queryset.annotate(
|
||||
required_for_build_orders=part_filters.annotate_build_order_requirements(),
|
||||
required_for_sales_orders=part_filters.annotate_sales_order_requirements(),
|
||||
@@ -1907,6 +1913,7 @@ class BomItemSerializer(
|
||||
'sub_part__stock_items',
|
||||
'sub_part__stock_items__allocations',
|
||||
'sub_part__stock_items__sales_order_allocations',
|
||||
'sub_part__stock_items__transfer_order_allocations',
|
||||
)
|
||||
|
||||
# Annotate with the 'total pricing' information based on unit pricing and quantity
|
||||
|
||||
@@ -229,6 +229,13 @@ class ReportConfig(AppConfig):
|
||||
'model_type': 'returnorder',
|
||||
'filename_pattern': 'ReturnOrder-{{ reference }}.pdf',
|
||||
},
|
||||
{
|
||||
'file': 'inventree_transfer_order_report.html',
|
||||
'name': 'InvenTree Transfer Order',
|
||||
'description': 'Sample transfer order report',
|
||||
'model_type': 'transferorder',
|
||||
'filename_pattern': 'TransferOrder-{{ reference }}.pdf',
|
||||
},
|
||||
{
|
||||
'file': 'inventree_test_report.html',
|
||||
'name': 'InvenTree Test Report',
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
{% extends "report/inventree_order_report_base.html" %}
|
||||
|
||||
{% load i18n %}
|
||||
{% load report %}
|
||||
{% load barcode %}
|
||||
{% load inventree_extras %}
|
||||
{% load markdownify %}
|
||||
|
||||
{% block header_content %}
|
||||
|
||||
<div class='header-right'>
|
||||
<h3>{% trans "Transfer Order" %} {{ prefix }}{{ reference }}</h3>
|
||||
{{ order.take_from.pathstring }} → {{ order.destination.pathstring }}
|
||||
</div>
|
||||
|
||||
{% endblock header_content %}
|
||||
|
||||
{% block page_content %}
|
||||
|
||||
<h3>{% trans "Line Items" %}</h3>
|
||||
|
||||
<table class='table table-striped table-condensed'>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{% trans "Part" %}</th>
|
||||
<th>{% trans "Reference" %}</th>
|
||||
<th>{% trans "Quantity" %}</th>
|
||||
<th>{% trans "Transferred" %}</th>
|
||||
<th>{% trans "Note" %}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for line in lines.all %}
|
||||
<tr>
|
||||
<td>
|
||||
<div class='thumb-container'>
|
||||
<img src='{% part_image line.part height=240 %}' alt='{% trans "Part image" %}' class='part-thumb'>
|
||||
</div>
|
||||
<div class='part-text'>
|
||||
{{ line.part.full_name }}
|
||||
</div>
|
||||
</td>
|
||||
<td>{{ line.reference }}</td>
|
||||
<td>{% decimal line.quantity %}</td>
|
||||
<td>{% decimal line.transferred %}</td>
|
||||
<td>{{ line.notes }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
{% endblock page_content %}
|
||||
@@ -53,11 +53,12 @@ from InvenTree.mixins import (
|
||||
RetrieveUpdateDestroyAPI,
|
||||
SerializerContextMixin,
|
||||
)
|
||||
from order.models import PurchaseOrder, ReturnOrder, SalesOrder
|
||||
from order.models import PurchaseOrder, ReturnOrder, SalesOrder, TransferOrder
|
||||
from order.serializers import (
|
||||
PurchaseOrderSerializer,
|
||||
ReturnOrderSerializer,
|
||||
SalesOrderSerializer,
|
||||
TransferOrderSerializer,
|
||||
)
|
||||
from part.models import BomItem, Part, PartCategory
|
||||
from part.serializers import PartBriefSerializer
|
||||
@@ -672,13 +673,17 @@ class StockFilter(FilterSet):
|
||||
def filter_allocated(self, queryset, name, value):
|
||||
"""Filter by whether or not the stock item is 'allocated'."""
|
||||
if str2bool(value):
|
||||
# Filter StockItem with either build allocations or sales order allocations
|
||||
# Filter StockItem with either build allocations or transfer order allocations or sales order allocations
|
||||
return queryset.filter(
|
||||
Q(sales_order_allocations__isnull=False) | Q(allocations__isnull=False)
|
||||
Q(sales_order_allocations__isnull=False)
|
||||
| Q(transfer_order_allocations__isnull=False)
|
||||
| Q(allocations__isnull=False)
|
||||
).distinct()
|
||||
# Filter StockItem without build allocations or sales order allocations
|
||||
# Filter StockItem without build allocations or transfer order allocations or sales order allocations
|
||||
return queryset.filter(
|
||||
Q(sales_order_allocations__isnull=True) & Q(allocations__isnull=True)
|
||||
Q(sales_order_allocations__isnull=True)
|
||||
& Q(transfer_order_allocations__isnull=True)
|
||||
& Q(allocations__isnull=True)
|
||||
)
|
||||
|
||||
expired = rest_filters.BooleanFilter(label='Expired', method='filter_expired')
|
||||
@@ -1584,6 +1589,7 @@ class StockTrackingList(
|
||||
'purchaseorder': (PurchaseOrder, PurchaseOrderSerializer),
|
||||
'salesorder': (SalesOrder, SalesOrderSerializer),
|
||||
'returnorder': (ReturnOrder, ReturnOrderSerializer),
|
||||
'transferorder': (TransferOrder, TransferOrderSerializer),
|
||||
'buildorder': (Build, BuildSerializer),
|
||||
'item': (StockItem, StockSerializers.StockItemSerializer),
|
||||
'stockitem': (StockItem, StockSerializers.StockItemSerializer),
|
||||
|
||||
@@ -47,6 +47,7 @@ from InvenTree.status_codes import (
|
||||
StockStatus,
|
||||
StockStatusGroups,
|
||||
)
|
||||
from order.status_codes import TransferOrderStatusGroups
|
||||
from part import models as PartModels
|
||||
from plugin.events import trigger_event
|
||||
from stock.events import StockEvents
|
||||
@@ -1535,7 +1536,7 @@ class StockItem(
|
||||
item.save(add_note=False)
|
||||
|
||||
def is_allocated(self):
|
||||
"""Return True if this StockItem is allocated to a SalesOrder or a Build."""
|
||||
"""Return True if this StockItem is allocated to a SalesOrder, TransferOrder, or a Build."""
|
||||
return self.allocation_count() > 0
|
||||
|
||||
def build_allocation_count(self, **kwargs):
|
||||
@@ -1595,12 +1596,48 @@ class StockItem(
|
||||
|
||||
return total
|
||||
|
||||
def get_transfer_order_allocations(self, active=True, **kwargs):
|
||||
"""Return a queryset for TransferOrderAllocations against this StockItem, with optional filters.
|
||||
|
||||
Arguments:
|
||||
active: Filter by 'active' status of the allocation
|
||||
"""
|
||||
query = self.transfer_order_allocations.all()
|
||||
|
||||
if filter_allocations := kwargs.get('filter_allocations'):
|
||||
query = query.filter(**filter_allocations)
|
||||
|
||||
if exclude_allocations := kwargs.get('exclude_allocations'):
|
||||
query = query.exclude(**exclude_allocations)
|
||||
|
||||
if active is True:
|
||||
query = query.filter(line__order__status__in=TransferOrderStatusGroups.OPEN)
|
||||
elif active is False:
|
||||
query = query.exclude(
|
||||
line__order__status__in=TransferOrderStatusGroups.OPEN
|
||||
)
|
||||
|
||||
return query
|
||||
|
||||
def transfer_order_allocation_count(self, active=True, **kwargs):
|
||||
"""Return the total quantity allocated to TransferOrders."""
|
||||
query = self.get_transfer_order_allocations(active=active, **kwargs)
|
||||
query = query.aggregate(q=Coalesce(Sum('quantity'), Decimal(0)))
|
||||
|
||||
total = query['q']
|
||||
|
||||
if total is None:
|
||||
total = Decimal(0)
|
||||
|
||||
return total
|
||||
|
||||
def allocation_count(self):
|
||||
"""Return the total quantity allocated to builds or orders."""
|
||||
bo = self.build_allocation_count()
|
||||
so = self.sales_order_allocation_count()
|
||||
to = self.transfer_order_allocation_count()
|
||||
|
||||
return bo + so
|
||||
return bo + so + to
|
||||
|
||||
def unallocated_quantity(self):
|
||||
"""Return the quantity of this StockItem which is *not* allocated."""
|
||||
@@ -2330,6 +2367,10 @@ class StockItem(
|
||||
|
||||
deltas = {'stockitem': self.pk}
|
||||
|
||||
transferorder = kwargs.pop('transferorder', None)
|
||||
if transferorder:
|
||||
deltas['transferorder'] = transferorder.pk
|
||||
|
||||
# Optional fields which can be supplied in a 'move' call
|
||||
for field in StockItem.optional_transfer_fields():
|
||||
if field in kwargs:
|
||||
@@ -2478,6 +2519,10 @@ class StockItem(
|
||||
)
|
||||
tracking_info['old_status_logical'] = old_status_logical
|
||||
|
||||
transferorder = kwargs.pop('transferorder', None)
|
||||
if transferorder:
|
||||
tracking_info['transferorder'] = transferorder.pk
|
||||
|
||||
# Optional fields which can be supplied in a 'move' call
|
||||
for field in StockItem.optional_transfer_fields():
|
||||
if field in kwargs:
|
||||
@@ -2717,6 +2762,10 @@ class StockItem(
|
||||
setattr(self, field, kwargs[field])
|
||||
deltas[field] = kwargs[field]
|
||||
|
||||
transferorder = kwargs.pop('transferorder', None)
|
||||
if transferorder:
|
||||
deltas['transferorder'] = transferorder.pk
|
||||
|
||||
self.save(add_note=False)
|
||||
|
||||
self.add_tracking_entry(
|
||||
|
||||
@@ -520,6 +520,8 @@ class StockItemSerializer(
|
||||
allocated=Coalesce(
|
||||
SubquerySum('sales_order_allocations__quantity'), Decimal(0)
|
||||
)
|
||||
# For now, stock allocated to a transfer order will not impact its availability
|
||||
# + Coalesce(SubquerySum('transfer_order_allocations__quantity'), Decimal(0))
|
||||
+ Coalesce(SubquerySum('allocations__quantity'), Decimal(0))
|
||||
)
|
||||
|
||||
@@ -1390,6 +1392,10 @@ class StockAssignmentItemSerializer(serializers.Serializer):
|
||||
if item.sales_order_allocations.count() > 0:
|
||||
raise ValidationError(_('Item is allocated to a sales order'))
|
||||
|
||||
# The item must not be allocated to a transfer order
|
||||
if item.transfer_order_allocations.count() > 0:
|
||||
raise ValidationError(_('Item is allocated to a transfer order'))
|
||||
|
||||
# The item must not be allocated to a build order
|
||||
if item.allocations.count() > 0:
|
||||
raise ValidationError(_('Item is allocated to a build order'))
|
||||
|
||||
@@ -20,6 +20,7 @@ _roles = {
|
||||
'purchase_order': 'Role Purchase Orders',
|
||||
'sales_order': 'Role Sales Orders',
|
||||
'return_order': 'Role Return Orders',
|
||||
'transfer_order': 'Role Transfer Orders',
|
||||
}
|
||||
_methods = {'view': 'GET', 'add': 'POST', 'change': 'PUT / PATCH', 'delete': 'DELETE'}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ class RuleSetEnum(StringEnum):
|
||||
PURCHASE_ORDER = 'purchase_order'
|
||||
SALES_ORDER = 'sales_order'
|
||||
RETURN_ORDER = 'return_order'
|
||||
TRANSFER_ORDER = 'transfer_order'
|
||||
|
||||
|
||||
# This is a list of all the ruleset choices available in the system.
|
||||
@@ -34,6 +35,7 @@ RULESET_CHOICES = [
|
||||
(RuleSetEnum.PURCHASE_ORDER, _('Purchase Orders')),
|
||||
(RuleSetEnum.SALES_ORDER, _('Sales Orders')),
|
||||
(RuleSetEnum.RETURN_ORDER, _('Return Orders')),
|
||||
(RuleSetEnum.TRANSFER_ORDER, _('Transfer Orders')),
|
||||
]
|
||||
|
||||
# Ruleset names available in the system.
|
||||
@@ -161,6 +163,11 @@ def get_ruleset_models() -> dict:
|
||||
'order_returnorderlineitem',
|
||||
'order_returnorderextraline',
|
||||
],
|
||||
RuleSetEnum.TRANSFER_ORDER: [
|
||||
'order_transferorder',
|
||||
'order_transferorderallocation',
|
||||
'order_transferorderlineitem',
|
||||
],
|
||||
}
|
||||
|
||||
if settings.SITE_MULTI:
|
||||
|
||||
Reference in New Issue
Block a user