Sales order can now be completed via the API

This commit is contained in:
Oliver 2021-12-04 13:50:11 +11:00
parent 008c52ef39
commit 31398b4c10
4 changed files with 50 additions and 16 deletions

View File

@ -683,7 +683,7 @@ class SalesOrderComplete(generics.CreateAPIView):
"""
queryset = models.SalesOrder.objects.all()
serializer_class = serializers.SalesOrderShipmentCompleteSerializer
serializer_class = serializers.SalesOrderCompleteSerializer
def get_serializer_context(self):

View File

@ -621,23 +621,43 @@ class SalesOrder(Order):
return self.lines.count() > 0 and all([line.is_completed() for line in self.lines.all()])
def can_complete(self, raise_error=False):
"""
Test if this SalesOrder can be completed.
Throws a ValidationError if cannot be completed.
"""
# Order without line items cannot be completed
if self.lines.count() == 0:
if raise_error:
raise ValidationError(_('Order cannot be completed as no parts have been assigned'))
# Only a PENDING order can be marked as SHIPPED
elif self.status != SalesOrderStatus.PENDING:
if raise_error:
raise ValidationError(_('Only a pending order can be marked as complete'))
elif self.pending_shipment_count > 0:
if raise_error:
raise ValidationError(_("Order cannot be completed as there are incomplete shipments"))
elif self.pending_line_count > 0:
if raise_error:
raise ValidationError(_("Order cannot be completed as there are incomplete line items"))
else:
return True
return False
def complete_order(self, user):
"""
Mark this order as "complete"
"""
if self.lines.count() == 0:
# Order without line items cannot be completed
raise ValidationError(_('Order cannot be completed as no parts have been assigned'))
if self.status != SalesOrderStatus.PENDING:
# Only a PENDING order can be marked as SHIPPED
raise ValidationError(_('Only a pending order can be marked as complete'))
# Check if there are any incomplete shipments
for shipment in self.shipments.all():
if not shipment.shipment_date:
raise ValidationError(_('Order cannot be completed as there are pending shipments'))
if not self.can_complete():
return
self.status = SalesOrderStatus.SHIPPED
self.shipped_by = user

View File

@ -749,11 +749,20 @@ class SalesOrderCompleteSerializer(serializers.Serializer):
DRF serializer for manually marking a sales order as complete
"""
def validate(self, data):
data = super().validate(data)
order = self.context['order']
order.can_complete(raise_error=True)
return data
def save(self):
request = self.context['request']
order = self.context['order']
# data = self.validated_data
user = getattr(request, 'user', None)

View File

@ -63,7 +63,7 @@ src="{% static 'img/blank_image.png' %}"
</div>
{% if order.status == SalesOrderStatus.PENDING %}
<button type='button' class='btn btn-success' id='complete-order' title='{% trans "Complete Sales Order" %}'>
<button type='button' class='btn btn-success' id='complete-order' title='{% trans "Complete Sales Order" %}'{% if not order.is_completed %} disabled{% endif %}>
<span class='fas fa-check-circle'></span> {% trans "Complete Order" %}
</button>
{% endif %}
@ -224,7 +224,12 @@ $("#cancel-order").click(function() {
});
$("#complete-order").click(function() {
completeSalesOrder({{ order.pk }});
constructForm('{% url "api-so-complete" order.id %}', {
method: 'POST',
title: '{% trans "Complete Sales Order" %}',
confirm: true,
reload: true,
});
});
{% if report_enabled %}