Merge pull request #1892 from eeintech/stock_installed_items

Re-enabled installing stock items into others
This commit is contained in:
Oliver 2021-08-04 09:01:28 +10:00 committed by GitHub
commit 69f242d11d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 134 additions and 39 deletions

View File

@ -8,7 +8,6 @@ from __future__ import unicode_literals
from django import forms from django import forms
from django.forms.utils import ErrorDict from django.forms.utils import ErrorDict
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.core.validators import MinValueValidator
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from mptt.fields import TreeNodeChoiceField from mptt.fields import TreeNodeChoiceField
@ -241,14 +240,9 @@ class InstallStockForm(HelperForm):
help_text=_('Stock item to install') help_text=_('Stock item to install')
) )
quantity_to_install = RoundingDecimalFormField( to_install = forms.BooleanField(
max_digits=10, decimal_places=5, widget=forms.HiddenInput(),
initial=1, required=False,
label=_('Quantity'),
help_text=_('Stock quantity to assign'),
validators=[
MinValueValidator(0.001)
]
) )
notes = forms.CharField( notes = forms.CharField(
@ -261,7 +255,7 @@ class InstallStockForm(HelperForm):
fields = [ fields = [
'part', 'part',
'stock_item', 'stock_item',
'quantity_to_install', # 'quantity_to_install',
'notes', 'notes',
] ]

View File

@ -119,6 +119,11 @@
<h4>{% trans "Installed Stock Items" %}</h4> <h4>{% trans "Installed Stock Items" %}</h4>
</div> </div>
<div class='panel-content'> <div class='panel-content'>
<div class='btn-group'>
<button type='button' class='btn btn-success' id='stock-item-install'>
<span class='fas fa-plus-circle'></span> {% trans "Install Stock Item" %}
</button>
</div>
<table class='table table-striped table-condensed' id='installed-table'></table> <table class='table table-striped table-condensed' id='installed-table'></table>
</div> </div>
</div> </div>
@ -128,6 +133,20 @@
{% block js_ready %} {% block js_ready %}
{{ block.super }} {{ block.super }}
$('#stock-item-install').click(function() {
launchModalForm(
"{% url 'stock-item-install' item.pk %}",
{
data: {
'part': {{ item.part.pk }},
'install_item': true,
},
reload: true,
}
);
});
loadInstalledInTable( loadInstalledInTable(
$('#installed-table'), $('#installed-table'),
{ {

View File

@ -127,9 +127,11 @@
<li><a href='#' id='stock-return-from-customer' title='{% trans "Return to stock" %}'><span class='fas fa-undo'></span> {% trans "Return to stock" %}</a></li> <li><a href='#' id='stock-return-from-customer' title='{% trans "Return to stock" %}'><span class='fas fa-undo'></span> {% trans "Return to stock" %}</a></li>
{% endif %} {% endif %}
{% if item.belongs_to %} {% if item.belongs_to %}
<li> <li><a href='#' id='stock-uninstall' title='{% trans "Uninstall stock item" %}'><span class='fas fa-unlink'></span> {% trans "Uninstall" %}</a></li>
<a href='#' id='stock-uninstall' title='{% trans "Uninstall stock item" %}'><span class='fas fa-unlink'></span> {% trans "Uninstall" %}</a> {% else %}
</li> {% if item.part.get_used_in %}
<li><a href='#' id='stock-install-in' title='{% trans "Install stock item" %}'><span class='fas fa-link'></span> {% trans "Install" %}</a></li>
{% endif %}
{% endif %} {% endif %}
</ul> </ul>
</div> </div>
@ -461,6 +463,20 @@ $("#stock-serialize").click(function() {
); );
}); });
$('#stock-install-in').click(function() {
launchModalForm(
"{% url 'stock-item-install' item.pk %}",
{
data: {
'part': {{ item.part.pk }},
'install_in': true,
},
reload: true,
}
);
});
$('#stock-uninstall').click(function() { $('#stock-uninstall').click(function() {
launchModalForm( launchModalForm(

View File

@ -3,6 +3,7 @@
{% block pre_form_content %} {% block pre_form_content %}
{% if install_item %}
<p> <p>
{% trans "Install another Stock Item into this item." %} {% trans "Install another Stock Item into this item." %}
</p> </p>
@ -12,6 +13,21 @@
<ul> <ul>
<li>{% trans "The Stock Item links to a Part which is in the BOM for this Stock Item" %}</li> <li>{% trans "The Stock Item links to a Part which is in the BOM for this Stock Item" %}</li>
<li>{% trans "The Stock Item is currently in stock" %}</li> <li>{% trans "The Stock Item is currently in stock" %}</li>
<li>{% trans "The Stock Item is serialized and does not belong to another item" %}</li>
</ul> </ul>
</p> </p>
{% elif install_in %}
<p>
{% trans "Install this Stock Item in another stock item." %}
</p>
<p>
{% trans "Stock items can only be installed if they meet the following criteria" %}:
<ul>
<li>{% trans "The part associated to this Stock Item belongs to another part's BOM" %}</li>
<li>{% trans "This Stock Item is serialized and does not belong to another item" %}</li>
</ul>
</p>
{% endif %}
{% endblock %} {% endblock %}

View File

@ -536,36 +536,73 @@ class StockItemInstall(AjaxUpdateView):
part = None part = None
def get_params(self):
""" Retrieve GET parameters """
# Look at GET params
self.part_id = self.request.GET.get('part', None)
self.install_in = self.request.GET.get('install_in', False)
self.install_item = self.request.GET.get('install_item', False)
if self.part_id is None:
# Look at POST params
self.part_id = self.request.POST.get('part', None)
try:
self.part = Part.objects.get(pk=self.part_id)
except (ValueError, Part.DoesNotExist):
self.part = None
def get_stock_items(self): def get_stock_items(self):
""" """
Return a list of stock items suitable for displaying to the user. Return a list of stock items suitable for displaying to the user.
Requirements: Requirements:
- Items must be in stock - Items must be in stock
- Items must be in BOM of stock item
Filters: - Items must be serialized
- Items can be filtered by Part reference
""" """
# Filter items in stock
items = StockItem.objects.filter(StockItem.IN_STOCK_FILTER) items = StockItem.objects.filter(StockItem.IN_STOCK_FILTER)
# Filter by Part association # Filter serialized stock items
items = items.exclude(serial__isnull=True).exclude(serial__exact='')
# Look at GET params if self.part:
part_id = self.request.GET.get('part', None) # Filter for parts to install this item in
if self.install_in:
# Get parts using this part
allowed_parts = self.part.get_used_in()
# Filter
items = items.filter(part__in=allowed_parts)
if part_id is None: # Filter for parts to install in this item
# Look at POST params if self.install_item:
part_id = self.request.POST.get('part', None) # Get parts used in this part's BOM
bom_items = self.part.get_bom_items()
try: allowed_parts = [item.sub_part for item in bom_items]
self.part = Part.objects.get(pk=part_id) # Filter
items = items.filter(part=self.part) items = items.filter(part__in=allowed_parts)
except (ValueError, Part.DoesNotExist):
self.part = None
return items return items
def get_context_data(self, **kwargs):
""" Retrieve parameters and update context """
ctx = super().get_context_data(**kwargs)
# Get request parameters
self.get_params()
ctx.update({
'part': self.part,
'install_in': self.install_in,
'install_item': self.install_item,
})
return ctx
def get_initial(self): def get_initial(self):
initials = super().get_initial() initials = super().get_initial()
@ -576,11 +613,16 @@ class StockItemInstall(AjaxUpdateView):
if items.count() == 1: if items.count() == 1:
item = items.first() item = items.first()
initials['stock_item'] = item.pk initials['stock_item'] = item.pk
initials['quantity_to_install'] = item.quantity
if self.part: if self.part:
initials['part'] = self.part initials['part'] = self.part
try:
# Is this stock item being installed in the other stock item?
initials['to_install'] = self.install_in or not self.install_item
except AttributeError:
pass
return initials return initials
def get_form(self): def get_form(self):
@ -593,6 +635,8 @@ class StockItemInstall(AjaxUpdateView):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
self.get_params()
form = self.get_form() form = self.get_form()
valid = form.is_valid() valid = form.is_valid()
@ -602,12 +646,18 @@ class StockItemInstall(AjaxUpdateView):
data = form.cleaned_data data = form.cleaned_data
other_stock_item = data['stock_item'] other_stock_item = data['stock_item']
quantity = data['quantity_to_install'] # Quantity will always be 1 for serialized item
quantity = 1
notes = data['notes'] notes = data['notes']
# Install the other stock item into this one # Get stock item
this_stock_item = self.get_object() this_stock_item = self.get_object()
if data['to_install']:
# Install this stock item into the other stock item
other_stock_item.installStockItem(this_stock_item, quantity, request.user, notes)
else:
# Install the other stock item into this one
this_stock_item.installStockItem(other_stock_item, quantity, request.user, notes) this_stock_item.installStockItem(other_stock_item, quantity, request.user, notes)
data = { data = {