Refactor to use more generic forms approach

This commit is contained in:
Oliver 2021-07-12 16:55:35 +10:00
parent 7531984c78
commit 747cccfa42

View File

@ -28,10 +28,15 @@ function adjustStock(items, options={}) {
var formTitle = 'Form Title Here'; var formTitle = 'Form Title Here';
var actionTitle = null; var actionTitle = null;
var specifyLocation = false;
var allowSerializedStock = false;
switch (options.action) { switch (options.action) {
case 'move': case 'move':
formTitle = '{% trans "Transfer Stock" %}'; formTitle = '{% trans "Transfer Stock" %}';
actionTitle = '{% trans "Move" %}'; actionTitle = '{% trans "Move" %}';
specifyLocation = true;
allowSerializedStock = true;
break; break;
case 'count': case 'count':
formTitle = '{% trans "Count Stock" %}'; formTitle = '{% trans "Count Stock" %}';
@ -47,6 +52,7 @@ function adjustStock(items, options={}) {
break; break;
case 'delete': case 'delete':
formTitle = '{% trans "Delete Stock" %}'; formTitle = '{% trans "Delete Stock" %}';
allowSerializedStock = true;
break; break;
default: default:
break; break;
@ -67,7 +73,15 @@ function adjustStock(items, options={}) {
<tbody> <tbody>
`; `;
items.forEach(function(item) { var itemCount = 0;
for (var idx = 0; idx < items.length; idx++) {
var item = items[idx];
if ((item.serial != null) && !allowSerializedStock) {
continue;
}
var pk = item.pk; var pk = item.pk;
@ -150,7 +164,17 @@ function adjustStock(items, options={}) {
<td id='buttons_${pk}'>${buttons}</td> <td id='buttons_${pk}'>${buttons}</td>
</tr>`; </tr>`;
}); itemCount += 1;
}
if (itemCount == 0) {
showAlertDialog(
'{% trans "Select Stock Items" %}',
'{% trans "You must select at least one available stock item" %}',
);
return;
}
html += `</tbody></table>`; html += `</tbody></table>`;
@ -158,24 +182,32 @@ function adjustStock(items, options={}) {
title: formTitle, title: formTitle,
}); });
constructFormBody({}, { // Extra fields
fields: { var extraFields = {
location: { location: {
label: '{% trans "Location" %}', label: '{% trans "Location" %}',
help_text: '{% trans "Select stock location" %}', help_text: '{% trans "Select destination stock location" %}',
type: 'related field', type: 'related field',
required: true, required: true,
api_url: `/api/stock/location/`, api_url: `/api/stock/location/`,
model: 'stocklocation', model: 'stocklocation',
},
note: {
label: '{% trans "Notes" %}',
help_text: '{% trans "Stock transaction notes" %}',
type: 'string',
}
}, },
note: {
label: '{% trans "Notes" %}',
help_text: '{% trans "Stock transaction notes" %}',
type: 'string',
}
};
if (!specifyLocation) {
delete extraFields.location;
}
constructFormBody({}, {
preFormContent: html, preFormContent: html,
fields: extraFields,
confirm: true, confirm: true,
confirmMessage: '{% trans "Confirm stock adjustment" %}',
modal: modal, modal: modal,
onSubmit: function(fields, options) { onSubmit: function(fields, options) {
console.log("submit!"); console.log("submit!");