Replace the existing CancelPurchaseOrderForm with an API driven form

This commit is contained in:
Oliver Walters 2022-05-04 15:14:14 +10:00
parent e920fc31e7
commit 62cd0f39c7
7 changed files with 37 additions and 52 deletions

View File

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

View File

@ -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() {

View File

@ -1,11 +0,0 @@
{% extends "modal_form.html" %}
{% load i18n %}
{% block pre_form_content %}
<div class='alert alert-danger alert-block'>
{% trans "Cancelling this order means that the order and line items will no longer be editable." %}
</div>
{% endblock %}

View File

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

View File

@ -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 """

View File

@ -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,

View File

@ -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 = `
<div class='alert alert-info alert-block'>
{% trans "Are you sure you wish to cancel this purchase order?" %}
</div>`;
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={}) {