Refactor all "adjustment" forms to use the new API approach

This commit is contained in:
Oliver 2021-07-12 20:38:54 +10:00
parent 74e5b2cd3f
commit 9fc7976569
6 changed files with 56 additions and 83 deletions

View File

@ -273,13 +273,25 @@
}); });
$("#part-count").click(function() { $("#part-count").click(function() {
launchModalForm("/stock/adjust/", { inventreeGet(
data: { '{% url "api-stock-list" %}',
action: "count", {
part: {{ part.id }}, part: {{ part.id }},
in_stock: true,
allow_variants: true,
part_detail: true,
location_detail: true,
}, },
reload: true, {
}); success: function(items) {
adjustStock('count', items, {
onSuccess: function() {
location.reload();
}
});
},
}
);
}); });
$("#price-button").click(function() { $("#price-button").click(function() {

View File

@ -481,13 +481,6 @@ class StockList(generics.ListCreateAPIView):
- GET: Return a list of all StockItem objects (with optional query filters) - GET: Return a list of all StockItem objects (with optional query filters)
- POST: Create a new StockItem - POST: Create a new StockItem
Additional query parameters are available:
- location: Filter stock by location
- category: Filter by parts belonging to a certain category
- supplier: Filter by supplier
- ancestor: Filter by an 'ancestor' StockItem
- status: Filter by the StockItem status
""" """
serializer_class = StockItemSerializer serializer_class = StockItemSerializer

View File

@ -534,14 +534,21 @@ $("#barcode-scan-into-location").click(function() {
}); });
function itemAdjust(action) { function itemAdjust(action) {
launchModalForm("/stock/adjust/",
inventreeGet(
'{% url "api-stock-detail" item.pk %}',
{ {
data: { part_detail: true,
action: action, location_detail: true,
item: {{ item.id }}, },
}, {
reload: true, success: function(item) {
follow: true, adjustStock(action, [item], {
onSuccess: function() {
location.reload();
}
});
}
} }
); );
} }

View File

@ -216,13 +216,25 @@
{% if location %} {% if location %}
$("#location-count").click(function() { $("#location-count").click(function() {
launchModalForm("/stock/adjust/", {
data: { inventreeGet(
action: "count", '{% url "api-stock-list" %}',
{
location: {{ location.id }}, location: {{ location.id }},
reload: true, in_stock: true,
part_detail: true,
location_detail: true,
},
{
success: function(items) {
adjustStock('count', items, {
onSuccess: function() {
location.reload();
}
});
}
} }
}); );
}); });
$('#print-label').click(function() { $('#print-label').click(function() {

View File

@ -105,31 +105,6 @@ class StockItemTest(StockViewTestCase):
response = self.client.get(reverse('stock-item-qr', args=(9999,)), HTTP_X_REQUESTED_WITH='XMLHttpRequest') response = self.client.get(reverse('stock-item-qr', args=(9999,)), HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_adjust_items(self):
url = reverse('stock-adjust')
# Move items
response = self.client.get(url, {'stock[]': [1, 2, 3, 4, 5], 'action': 'move'}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.status_code, 200)
# Count part
response = self.client.get(url, {'part': 1}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.status_code, 200)
# Remove items
response = self.client.get(url, {'location': 1, 'action': 'take'}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.status_code, 200)
# Add items
response = self.client.get(url, {'item': 1, 'action': 'add'}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.status_code, 200)
# Blank response
response = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.status_code, 200)
# TODO - Tests for POST data
def test_edit_item(self): def test_edit_item(self):
# Test edit view for StockItem # Test edit view for StockItem
response = self.client.get(reverse('stock-item-edit', args=(1,)), HTTP_X_REQUESTED_WITH='XMLHttpRequest') response = self.client.get(reverse('stock-item-edit', args=(1,)), HTTP_X_REQUESTED_WITH='XMLHttpRequest')

View File

@ -23,7 +23,7 @@ function stockStatusCodes() {
/** /**
* Perform stock adjustments * Perform stock adjustments
*/ */
function adjustStock(items, options={}) { function adjustStock(action, items, options={}) {
var formTitle = 'Form Title Here'; var formTitle = 'Form Title Here';
var actionTitle = null; var actionTitle = null;
@ -34,7 +34,7 @@ function adjustStock(items, options={}) {
var specifyLocation = false; var specifyLocation = false;
var allowSerializedStock = false; var allowSerializedStock = false;
switch (options.action) { switch (action) {
case 'move': case 'move':
formTitle = '{% trans "Transfer Stock" %}'; formTitle = '{% trans "Transfer Stock" %}';
actionTitle = '{% trans "Move" %}'; actionTitle = '{% trans "Move" %}';
@ -97,7 +97,7 @@ function adjustStock(items, options={}) {
var maxValue = null; var maxValue = null;
var value = null; var value = null;
switch (options.action) { switch (action) {
case 'move': case 'move':
minValue = 0; minValue = 0;
maxValue = item.quantity; maxValue = item.quantity;
@ -224,7 +224,7 @@ function adjustStock(items, options={}) {
onSubmit: function(fields, opts) { onSubmit: function(fields, opts) {
// "Delete" action gets handled differently // "Delete" action gets handled differently
if (options.action == 'delete') { if (action == 'delete') {
var requests = []; var requests = [];
@ -259,7 +259,9 @@ function adjustStock(items, options={}) {
var q = getFormFieldValue(item.pk, {}, {modal: modal}); var q = getFormFieldValue(item.pk, {}, {modal: modal});
data.items.push({pk: item.pk, quantity: q}) if (q != null) {
data.items.push({pk: item.pk, quantity: q});
}
}); });
// Add in extra field data // Add in extra field data
@ -1053,39 +1055,11 @@ function loadStockTable(table, options) {
function stockAdjustment(action) { function stockAdjustment(action) {
var items = $("#stock-table").bootstrapTable("getSelections"); var items = $("#stock-table").bootstrapTable("getSelections");
adjustStock(items, { adjustStock(action, items, {
action: action,
onSuccess: function() { onSuccess: function() {
$('#stock-table').bootstrapTable('refresh'); $('#stock-table').bootstrapTable('refresh');
} }
}); });
return;
// Buttons for launching secondary modals
var secondary = [];
if (action == 'move') {
secondary.push({
field: 'destination',
label: '{% trans "New Location" %}',
title: '{% trans "Create new location" %}',
url: "/stock/location/new/",
});
}
launchModalForm("/stock/adjust/",
{
data: {
action: action,
stock: stock,
},
success: function() {
$("#stock-table").bootstrapTable('refresh');
},
secondary: secondary,
}
);
} }
// Automatically link button callbacks // Automatically link button callbacks