Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to build system #1021

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion InvenTree/InvenTree/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def post(self, request, *args, **kwargs):

self.pre_save()
self.object = self.form.save()
self.post_save()
self.post_save(obj=self.object) # TODO: is this necessary? get_object() was failing inside post_save()

# Return the PK of the newly-created object
data['pk'] = self.object.pk
Expand Down
19 changes: 18 additions & 1 deletion InvenTree/build/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@
from stock.models import StockLocation


class CreateBuildForm(HelperForm):
""" Form for creating a Build object.
"""

class Meta:
model = Build
fields = [
'title',
'part',
'parent',
'sales_order',
'quantity',
'take_from',
'batch',
'link',
]


class EditBuildForm(HelperForm):
""" Form for editing a Build object.
"""
Expand All @@ -24,7 +42,6 @@ class Meta:
'part',
'parent',
'sales_order',
'quantity',
'take_from',
'batch',
'link',
Expand Down
44 changes: 17 additions & 27 deletions InvenTree/build/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,35 +293,24 @@ def completeBuild(self, location, serial_numbers, user):
q=self.quantity,
now=str(datetime.now().date())
)

# Generate the build outputs
# Update build output metadata
if self.part.trackable and serial_numbers:
# Add new serial numbers
for serial in serial_numbers:
item = StockModels.StockItem.objects.create(
part=self.part,
build=self,
location=location,
quantity=1,
serial=serial,
batch=str(self.batch) if self.batch else '',
notes=notes
)

item.save()

if len(serial_numbers) != self.build_outputs.count():
raise ValidationError("Number of serial numbers [{}] should equal the number of build outputs [{}]".format(
len(serial_numbers), self.build_outputs.count()
))
for i, build_output in enumerate(self.build_outputs.all()):
build_output.serial = serial_numbers[i]
build_output.location = location
build_output.notes = notes
build_output.save()
build_output.is_building = False
else:
# Add stock of the newly created item
item = StockModels.StockItem.objects.create(
part=self.part,
build=self,
location=location,
quantity=self.quantity,
batch=str(self.batch) if self.batch else '',
notes=notes
)

item.save()
for build_output in self.build_outputs.all():
build_output.location = location
build_output.notes = notes
build_output.save()
build_output.is_building = False

# Finally, mark the build as complete
self.completion_date = datetime.now().date()
Expand Down Expand Up @@ -502,6 +491,7 @@ def complete_allocation(self, user):
# TODO - If the item__part object is not trackable, delete the stock item here

item.build_order = self.build
item.is_building = False
item.save()

build = models.ForeignKey(
Expand Down
11 changes: 11 additions & 0 deletions InvenTree/build/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ def test_complete(self):

# Generate some serial numbers!
serials = ExtractSerialNumbers("1-10", 10)
#
# Create stock items for each build output
for i in range(self.build.quantity):
item = StockItem.objects.create(
part=self.build.part,
build=self.build,
quantity=1,
batch=str(self.build.batch) if self.build.batch else '',
is_building=True
)
item.save()

self.build.completeBuild(None, serials, None)

Expand Down
33 changes: 24 additions & 9 deletions InvenTree/build/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.utils.translation import ugettext as _
from datetime import datetime

from django.core.exceptions import ValidationError
from django.views.generic import DetailView, ListView, UpdateView
from django.forms import HiddenInput
from django.urls import reverse
from django.utils.translation import ugettext as _
from django.views.generic import DetailView, ListView, UpdateView

from part.models import Part
from .models import Build, BuildItem
from . import forms
from stock.models import StockLocation, StockItem

from InvenTree.views import AjaxUpdateView, AjaxCreateView, AjaxDeleteView
from InvenTree.helpers import str2bool, ExtractSerialNumbers
from InvenTree.status_codes import BuildStatus
from InvenTree.views import AjaxUpdateView, AjaxCreateView, AjaxDeleteView
from part.models import Part
from stock.models import StockLocation, StockItem
from . import forms
from .models import Build, BuildItem


class BuildIndex(ListView):
Expand Down Expand Up @@ -389,7 +390,7 @@ class BuildCreate(AjaxCreateView):
""" View to create a new Build object """
model = Build
context_object_name = 'build'
form_class = forms.EditBuildForm
form_class = forms.CreateBuildForm
ajax_form_title = _('Start new Build')
ajax_template_name = 'modal_form.html'

Expand Down Expand Up @@ -418,6 +419,20 @@ def get_data(self):
'success': _('Created new build'),
}

def post_save(self, obj, **kwargs):
build = obj
# Create stock items for each build output
for i in range(build.quantity):
item = StockItem.objects.create(
part=build.part,
build=build,
quantity=build.quantity,
batch=str(build.batch) if build.batch else '',
is_building=True
)
item.save()
super().post_save(**kwargs)


class BuildUpdate(AjaxUpdateView):
""" View for editing a Build object """
Expand Down