Create an initial stockitem output when a new build is created

This commit is contained in:
Oliver Walters 2020-10-20 23:27:43 +11:00
parent 652c2dbcbe
commit 3bb247a135
6 changed files with 62 additions and 12 deletions

View File

@ -316,13 +316,13 @@ class AjaxCreateView(AjaxMixin, CreateView):
- Handles form validation via AJAX POST requests
"""
def pre_save(self, **kwargs):
def pre_save(self, form, request, **kwargs):
"""
Hook for doing something before the form is validated
"""
pass
def post_save(self, new_object, **kwargs):
def post_save(self, new_object, request, **kwargs):
"""
Hook for doing something with the created object after it is saved
"""
@ -354,9 +354,9 @@ class AjaxCreateView(AjaxMixin, CreateView):
if self.form.is_valid():
self.pre_save()
self.pre_save(self.form, request)
self.object = self.form.save()
self.post_save(self.object)
self.post_save(self.object, request)
# Return the PK of the newly-created object
data['pk'] = self.object.pk

View File

@ -0,0 +1,20 @@
# Generated by Django 3.0.7 on 2020-10-20 11:44
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('part', '0051_bomitem_optional'),
('build', '0023_auto_20201020_1009'),
]
operations = [
migrations.AlterField(
model_name='build',
name='part',
field=models.ForeignKey(help_text='Select part to build', limit_choices_to={'active': True, 'assembly': True, 'virtual': False}, on_delete=django.db.models.deletion.CASCADE, related_name='builds', to='part.Part', verbose_name='Part'),
),
]

View File

@ -111,7 +111,6 @@ class Build(MPTTModel):
on_delete=models.CASCADE,
related_name='builds',
limit_choices_to={
'is_template': False,
'assembly': True,
'active': True,
'virtual': False,
@ -226,6 +225,24 @@ class Build(MPTTModel):
return new_ref
def createInitialStockItem(self, user):
"""
Create an initial output StockItem to be completed by this build.
"""
output = StockModels.StockItem.objects.create(
part=self.part, # Link to the parent part
location=None, # No location (yet) until it is completed
quantity=self.quantity,
batch='', # The 'batch' code is not set until the item is completed
build=self, # Point back to this build
is_building=True, # Mark this StockItem as building
)
output.save()
# TODO - Add a transaction note to the new StockItem
@transaction.atomic
def cancelBuild(self, user):
""" Mark the Build as CANCELLED

View File

@ -33,7 +33,20 @@
{% if build.take_from %}
<a href="{% url 'stock-location-detail' build.take_from.id %}">{{ build.take_from }}</a>
{% else %}
{% trans "Stock can be taken from any available location." %}
<i>{% trans "Stock can be taken from any available location." %}</i>
{% endif %}
</td>
</tr>
<tr>
<td><span class='fas fa-map-marker-alt'></span></td>
<td>{% trans "Destination" %}</td>
<td>
{% if build.destination %}
<a href="{% url 'stock-location-detail' build.destination.id %}">
{{ build.destination }}
</a>
{% else %}
<i>{% trans "Destination location not specified" %}</i>
{% endif %}
</td>
</tr>

View File

@ -439,15 +439,14 @@ class BuildCreate(AjaxCreateView):
return {
'success': _('Created new build'),
}
def post_save(self, new_object):
def post_save(self, new_object, request, **kwargs):
"""
Called immediately after the build has been created.
Called immediately after a new Build object is created.
"""
build = new_object
print("Created:", build)
build.createInitialStockItem(request.user)
class BuildUpdate(AjaxUpdateView):

View File

@ -154,8 +154,9 @@ class StockItemSerializer(InvenTreeModelSerializer):
'allocated',
'batch',
'belongs_to',
'customer',
'build',
'build_order',
'customer',
'in_stock',
'is_building',
'link',