Add ability to quickly duplicate build orders (#3565)

* Adds ability to easily duplicate an existing build order

* Refactor purchase order editing code
This commit is contained in:
Oliver 2022-08-18 12:44:47 +10:00 committed by GitHub
parent 32b11ec5af
commit b70a0164ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 117 additions and 53 deletions

View File

@ -55,6 +55,9 @@ src="{% static 'img/blank_image.png' %}"
{% if build.is_active %} {% if build.is_active %}
<li><a class='dropdown-item' href='#' id='build-cancel'><span class='fas fa-times-circle icon-red'></span> {% trans "Cancel Build" %}</a></li> <li><a class='dropdown-item' href='#' id='build-cancel'><span class='fas fa-times-circle icon-red'></span> {% trans "Cancel Build" %}</a></li>
{% endif %} {% endif %}
{% if roles.build.add %}
<li><a class='dropdown-item' href='#' id='build-duplicate'><span class='fas fa-clone'></span> {% trans "Duplicate Build" %}</a></li>
{% endif %}
{% if build.status == BuildStatus.CANCELLED and roles.build.delete %} {% if build.status == BuildStatus.CANCELLED and roles.build.delete %}
<li><a class='dropdown-item' href='#' id='build-delete'><span class='fas fa-trash-alt icon-red'></span> {% trans "Delete Build" %}</a> <li><a class='dropdown-item' href='#' id='build-delete'><span class='fas fa-trash-alt icon-red'></span> {% trans "Delete Build" %}</a>
{% endif %} {% endif %}
@ -209,6 +212,7 @@ src="{% static 'img/blank_image.png' %}"
{% block js_ready %} {% block js_ready %}
{% if roles.build.change %}
$("#build-edit").click(function () { $("#build-edit").click(function () {
editBuildOrder({{ build.pk }}); editBuildOrder({{ build.pk }});
}); });
@ -230,6 +234,13 @@ src="{% static 'img/blank_image.png' %}"
completed: {% if build.remaining == 0 %}true{% else %}false{% endif %}, completed: {% if build.remaining == 0 %}true{% else %}false{% endif %},
}); });
}); });
{% endif %}
{% if roles.build.add %}
$('#build-duplicate').click(function() {
duplicateBuildOrder({{ build.pk }});
});
{% endif %}
{% if report_enabled %} {% if report_enabled %}
$('#print-build-report').click(function() { $('#print-build-report').click(function() {

View File

@ -219,28 +219,10 @@ $('#print-order-report').click(function() {
$("#edit-order").click(function() { $("#edit-order").click(function() {
constructForm('{% url "api-po-detail" order.pk %}', { editPurchaseOrder({{ order.pk }}, {
fields: { {% if order.lines.count > 0 or order.status != PurchaseOrderStatus.PENDING %}
reference: { hide_supplier: true,
icon: 'fa-hashtag',
},
{% if order.lines.count == 0 and order.status == PurchaseOrderStatus.PENDING %}
supplier: {
},
{% endif %} {% endif %}
supplier_reference: {},
description: {},
target_date: {
icon: 'fa-calendar-alt',
},
link: {
icon: 'fa-link',
},
responsible: {
icon: 'fa-user',
},
},
title: '{% trans "Edit Purchase Order" %}',
reload: true, reload: true,
}); });
}); });

View File

@ -23,6 +23,7 @@
cancelBuildOrder, cancelBuildOrder,
completeBuildOrder, completeBuildOrder,
createBuildOutput, createBuildOutput,
duplicateBuildOrder,
editBuildOrder, editBuildOrder,
loadAllocationTable, loadAllocationTable,
loadBuildOrderAllocationTable, loadBuildOrderAllocationTable,
@ -75,7 +76,9 @@ function buildFormFields() {
}; };
} }
/*
* Edit an existing BuildOrder via the API
*/
function editBuildOrder(pk) { function editBuildOrder(pk) {
var fields = buildFormFields(); var fields = buildFormFields();
@ -87,6 +90,10 @@ function editBuildOrder(pk) {
}); });
} }
/*
* Create a new build order via an API form
*/
function newBuildOrder(options={}) { function newBuildOrder(options={}) {
/* Launch modal form to create a new BuildOrder. /* Launch modal form to create a new BuildOrder.
*/ */
@ -113,8 +120,13 @@ function newBuildOrder(options={}) {
fields.sales_order.value = options.sales_order; fields.sales_order.value = options.sales_order;
} }
if (options.data) {
delete options.data.pk;
}
constructForm(`/api/build/`, { constructForm(`/api/build/`, {
fields: fields, fields: fields,
data: options.data,
follow: true, follow: true,
method: 'POST', method: 'POST',
title: '{% trans "Create Build Order" %}', title: '{% trans "Create Build Order" %}',
@ -123,6 +135,26 @@ function newBuildOrder(options={}) {
} }
/*
* Duplicate an existing build order.
*/
function duplicateBuildOrder(build_id, options={}) {
inventreeGet(`/api/build/${build_id}/`, {}, {
success: function(data) {
// Clear out data we do not want to be duplicated
delete data['pk'];
delete data['issued_by'];
delete data['reference'];
options.data = data;
newBuildOrder(options);
}
});
}
/* Construct a form to cancel a build order */ /* Construct a form to cancel a build order */
function cancelBuildOrder(build_id, options={}) { function cancelBuildOrder(build_id, options={}) {

View File

@ -30,6 +30,7 @@
createPurchaseOrderLineItem, createPurchaseOrderLineItem,
createSalesOrder, createSalesOrder,
createSalesOrderShipment, createSalesOrderShipment,
editPurchaseOrder,
editPurchaseOrderLineItem, editPurchaseOrderLineItem,
exportOrder, exportOrder,
issuePurchaseOrder, issuePurchaseOrder,
@ -493,18 +494,18 @@ function createSalesOrder(options={}) {
}); });
} }
// Create a new PurchaseOrder
function createPurchaseOrder(options={}) {
constructForm('{% url "api-po-list" %}', { /*
method: 'POST', * Construct a set of fields for a purchase order form
fields: { */
function purchaseOrderFields(options={}) {
var fields = {
reference: { reference: {
icon: 'fa-hashtag', icon: 'fa-hashtag',
}, },
supplier: { supplier: {
icon: 'fa-building', icon: 'fa-building',
value: options.supplier,
secondary: { secondary: {
title: '{% trans "Add Supplier" %}', title: '{% trans "Add Supplier" %}',
fields: function() { fields: function() {
@ -526,8 +527,46 @@ function createPurchaseOrder(options={}) {
}, },
responsible: { responsible: {
icon: 'fa-user', icon: 'fa-user',
}
}, },
};
if (options.supplier) {
fields.supplier.value = options.supplier;
}
if (options.hide_supplier) {
fields.supplier.hidden = true;
}
return fields;
}
/*
* Edit an existing PurchaseOrder
*/
function editPurchaseOrder(pk, options={}) {
var fields = purchaseOrderFields(options);
constructForm(`/api/order/po/${pk}/`, {
fields: fields,
title: '{% trans "Edit Purchase Order" %}',
onSuccess: function(response) {
handleFormSuccess(response, options);
}
});
}
// Create a new PurchaseOrder
function createPurchaseOrder(options={}) {
var fields = purchaseOrderFields(options);
constructForm('{% url "api-po-list" %}', {
method: 'POST',
fields: fields,
onSuccess: function(data) { onSuccess: function(data) {
if (options.onSuccess) { if (options.onSuccess) {