More fixes

- Allow stock item creation for inactive parts
- Better handling of successful stock item creation
- Disable fields rather than hiding them
This commit is contained in:
Oliver 2021-11-05 00:02:55 +11:00
parent b41dbba2b0
commit f27acde934
7 changed files with 73 additions and 22 deletions

View File

@ -322,7 +322,6 @@ $("#item-create").click(function() {
part: {{ part.part.id }},
supplier_part: {{ part.id }},
},
reload: true,
});
});

View File

@ -886,7 +886,6 @@
onPanelLoad("part-stock", function() {
$('#new-stock-item').click(function () {
createNewStockItem({
reload: true,
data: {
part: {{ part.id }},
{% if part.default_location %}
@ -919,7 +918,6 @@
$('#item-create').click(function () {
createNewStockItem({
reload: true,
data: {
part: {{ part.id }},
}

View File

@ -0,0 +1,20 @@
# Generated by Django 3.2.5 on 2021-11-04 12:40
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('part', '0074_partcategorystar'),
('stock', '0066_stockitem_scheduled_for_deletion'),
]
operations = [
migrations.AlterField(
model_name='stockitem',
name='part',
field=models.ForeignKey(help_text='Base part', limit_choices_to={'virtual': False}, on_delete=django.db.models.deletion.CASCADE, related_name='stock_items', to='part.part', verbose_name='Base Part'),
),
]

View File

@ -456,7 +456,6 @@ class StockItem(MPTTModel):
verbose_name=_('Base Part'),
related_name='stock_items', help_text=_('Base part'),
limit_choices_to={
'active': True,
'virtual': False
})

View File

@ -308,7 +308,6 @@
$('#item-create').click(function () {
createNewStockItem({
table: '#stock-table',
data: {
{% if location %}
location: {{ location.id }}

View File

@ -25,6 +25,9 @@
*/
/* exported
clearFormInput,
disableFormInput,
enableFormInput,
hideFormInput,
setFormGroupVisibility,
showFormInput,
@ -1261,6 +1264,23 @@ function initializeGroups(fields, options) {
}
}
// Clear a form input
function clearFormInput(name, options) {
updateFieldValue(name, null, {}, options);
}
// Disable a form input
function disableFormInput(name, options) {
$(options.modal).find(`#id_${name}`).prop('disabled', true);
}
// Enable a form input
function enableFormInput(name, options) {
$(options.modal).find(`#id_${name}`).prop('disabled', false);
}
// Hide a form input
function hideFormInput(name, options) {
$(options.modal).find(`#div_id_${name}`).hide();

View File

@ -138,20 +138,33 @@ function stockItemFields(options={}) {
onSelect: function(data, field, opts) {
// Callback when a new "part" is selected
// If we are "creating" a new stock item
// If we are "creating" a new stock item,
// change the available fields based on the part properties
if (options.create) {
// If a "trackable" part is selected, enable serial number field
if (data.trackable) {
showFormInput('serial_numbers', opts);
enableFormInput('serial_numbers', opts);
// showFormInput('serial_numbers', opts);
} else {
updateFieldValue('serial_numbers', '', {}, opts);
hideFormInput('serial_numbers', opts);
clearFormInput('serial_numbers', opts);
disableFormInput('serial_numbers', opts);
}
// Enable / disable fields based on purchaseable status
if (data.purchaseable) {
enableFormInput('supplier_part', opts);
enableFormInput('purchase_price', opts);
enableFormInput('purchase_price_currency', opts);
} else {
clearFormInput('supplier_part', opts);
clearFormInput('purchase_price', opts);
disableFormInput('supplier_part', opts);
disableFormInput('purchase_price', opts);
disableFormInput('purchase_price_currency', opts);
}
}
// TODO: Hide "purchase price" fields for non purchaseable parts!
// TODO: Update "location" based on "default_location" returned
}
},
supplier_part: {
@ -204,7 +217,7 @@ function stockItemFields(options={}) {
};
if (options.create) {
// Use "serial numbers" field when creating a new stock item
// Use special "serial numbers" field when creating a new stock item
delete fields['serial'];
} else {
// These fields cannot be edited once the stock item has been created
@ -321,25 +334,28 @@ function createNewStockItem(options={}) {
options.onSuccess = function(response) {
// If a single stock item has been created, follow it!
if (response.pk) {
var url = `/stock/item/${pk}/`;
var url = `/stock/item/${response.pk}/`;
addCachedAlert('{% trans "Created stock item" %}', {
addCachedAlert('{% trans "Created new stock item" %}', {
icon: 'fas fa-boxes',
});
location.href = url;
window.location.href = url;
} else {
// Multiple stock items have been created (i.e. serialized stock)
var q = response.quantity;
showMessage('{% trans "Created stock items" %}', {
showMessage('{% trans "Created multiple stock items" %}', {
icon: 'fas fa-boxes',
details: `{% trans "Serial numbers" %}: ${response.serial_numbers}`
});
if (options.table) {
// Reload the table
$(options.table).bootstrapTable('refresh');
}
var table = options.table || '#stock-table';
// Reload the table
$(table).bootstrapTable('refresh');
}
}
}