From 62cd0f39c7b45405cd77f807528559664f42ca10 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 4 May 2022 15:14:14 +1000 Subject: [PATCH] Replace the existing CancelPurchaseOrderForm with an API driven form --- InvenTree/order/forms.py | 11 -------- .../order/templates/order/order_base.html | 12 ++++++--- .../order/templates/order/order_cancel.html | 11 -------- InvenTree/order/urls.py | 1 - InvenTree/order/views.py | 26 ------------------- InvenTree/templates/js/translated/forms.js | 3 +++ InvenTree/templates/js/translated/order.js | 25 ++++++++++++++++++ 7 files changed, 37 insertions(+), 52 deletions(-) delete mode 100644 InvenTree/order/templates/order/order_cancel.html diff --git a/InvenTree/order/forms.py b/InvenTree/order/forms.py index d6b99b7fd9..0ac55a2f22 100644 --- a/InvenTree/order/forms.py +++ b/InvenTree/order/forms.py @@ -41,17 +41,6 @@ class CompletePurchaseOrderForm(HelperForm): ] -class CancelPurchaseOrderForm(HelperForm): - - confirm = forms.BooleanField(required=True, label=_('Confirm'), help_text=_('Cancel order')) - - class Meta: - model = PurchaseOrder - fields = [ - 'confirm', - ] - - class CancelSalesOrderForm(HelperForm): confirm = forms.BooleanField(required=True, label=_('Confirm'), help_text=_('Cancel order')) diff --git a/InvenTree/order/templates/order/order_base.html b/InvenTree/order/templates/order/order_base.html index c2aa10f722..70d791081b 100644 --- a/InvenTree/order/templates/order/order_base.html +++ b/InvenTree/order/templates/order/order_base.html @@ -258,9 +258,15 @@ $("#complete-order").click(function() { }); $("#cancel-order").click(function() { - launchModalForm("{% url 'po-cancel' order.id %}", { - reload: true, - }); + + cancelPurchaseOrder( + {{ order.pk }}, + { + onSuccess: function() { + window.location.reload(); + } + }, + ); }); $("#export-order").click(function() { diff --git a/InvenTree/order/templates/order/order_cancel.html b/InvenTree/order/templates/order/order_cancel.html deleted file mode 100644 index 7cdb03ae20..0000000000 --- a/InvenTree/order/templates/order/order_cancel.html +++ /dev/null @@ -1,11 +0,0 @@ -{% extends "modal_form.html" %} - -{% load i18n %} - -{% block pre_form_content %} - -
- {% trans "Cancelling this order means that the order and line items will no longer be editable." %} -
- -{% endblock %} \ No newline at end of file diff --git a/InvenTree/order/urls.py b/InvenTree/order/urls.py index f82a581828..79bd897a4d 100644 --- a/InvenTree/order/urls.py +++ b/InvenTree/order/urls.py @@ -11,7 +11,6 @@ from . import views purchase_order_detail_urls = [ - re_path(r'^cancel/', views.PurchaseOrderCancel.as_view(), name='po-cancel'), re_path(r'^issue/', views.PurchaseOrderIssue.as_view(), name='po-issue'), re_path(r'^complete/', views.PurchaseOrderComplete.as_view(), name='po-complete'), diff --git a/InvenTree/order/views.py b/InvenTree/order/views.py index 68b45ebe86..a3f4fc68e6 100644 --- a/InvenTree/order/views.py +++ b/InvenTree/order/views.py @@ -87,32 +87,6 @@ class SalesOrderDetail(InvenTreeRoleMixin, DetailView): template_name = 'order/sales_order_detail.html' -class PurchaseOrderCancel(AjaxUpdateView): - """ View for cancelling a purchase order """ - - model = PurchaseOrder - ajax_form_title = _('Cancel Order') - ajax_template_name = 'order/order_cancel.html' - form_class = order_forms.CancelPurchaseOrderForm - - def validate(self, order, form, **kwargs): - - confirm = str2bool(form.cleaned_data.get('confirm', False)) - - if not confirm: - form.add_error('confirm', _('Confirm order cancellation')) - - if not order.can_cancel(): - form.add_error(None, _('Order cannot be cancelled')) - - def save(self, order, form, **kwargs): - """ - Cancel the PurchaseOrder - """ - - order.cancel_order() - - class SalesOrderCancel(AjaxUpdateView): """ View for cancelling a sales order """ diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index cc138052ef..fe28bb0653 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -123,6 +123,9 @@ function getApiEndpointOptions(url, callback) { return; } + // Include extra context information in the request + url += '?context=true' + // Return the ajax request object $.ajax({ url: url, diff --git a/InvenTree/templates/js/translated/order.js b/InvenTree/templates/js/translated/order.js index d5ca7caf42..c1abe2cafb 100644 --- a/InvenTree/templates/js/translated/order.js +++ b/InvenTree/templates/js/translated/order.js @@ -20,6 +20,7 @@ /* exported allocateStockToSalesOrder, + cancelPurchaseOrder, completeShipment, createSalesOrder, createSalesOrderShipment, @@ -141,6 +142,30 @@ function completeShipment(shipment_id) { } +function cancelPurchaseOrder(order_id, options={}) { + + var html = ` +
+ {% trans "Are you sure you wish to cancel this purchase order?" %} +
`; + + constructForm( + `/api/order/po/${order_id}/cancel/`, + { + method: 'POST', + title: '{% trans "Cancel Purchase Order" %}', + confirm: true, + preFormContent: html, + onSuccess: function(response) { + if (options.onSuccess) { + options.onSuccess(response); + } + } + } + ); +} + + // Open a dialog to create a new sales order shipment function createSalesOrderShipment(options={}) {