mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-18 04:55:44 +00:00
Build can be "unallocated" against a single build output
This commit is contained in:
@ -46,6 +46,25 @@ class EditBuildForm(HelperForm):
|
||||
]
|
||||
|
||||
|
||||
class UnallocateBuildForm(HelperForm):
|
||||
"""
|
||||
Form for auto-de-allocation of stock from a build
|
||||
"""
|
||||
|
||||
confirm = forms.BooleanField(required=False, help_text=_('Confirm unallocation of stock'))
|
||||
|
||||
output_id = forms.IntegerField(
|
||||
required=False,
|
||||
widget=forms.HiddenInput()
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = Build
|
||||
fields = [
|
||||
'confirm',
|
||||
]
|
||||
|
||||
|
||||
class ConfirmBuildForm(HelperForm):
|
||||
""" Form for auto-allocation of stock to a build """
|
||||
|
||||
|
@ -363,10 +363,22 @@ class Build(MPTTModel):
|
||||
return allocations
|
||||
|
||||
@transaction.atomic
|
||||
def unallocateStock(self):
|
||||
""" Deletes all stock allocations for this build. """
|
||||
def unallocateStock(self, output=None):
|
||||
"""
|
||||
Deletes all stock allocations for this build.
|
||||
|
||||
Args:
|
||||
output: Specify which build output to delete allocations (optional)
|
||||
|
||||
BuildItem.objects.filter(build=self.id).delete()
|
||||
"""
|
||||
|
||||
allocations = BuildItem.objects.filter(build=self.pk)
|
||||
|
||||
if output:
|
||||
allocations = allocations.filter(install_into=output.pk)
|
||||
|
||||
# Remove all the allocations
|
||||
allocations.delete()
|
||||
|
||||
@transaction.atomic
|
||||
def autoAllocate(self):
|
||||
@ -682,6 +694,8 @@ class BuildItem(models.Model):
|
||||
|
||||
def complete_allocation(self, user):
|
||||
|
||||
# TODO : This required much reworking!!
|
||||
|
||||
item = self.stock_item
|
||||
|
||||
# Split the allocated stock if there are more available than allocated
|
||||
|
@ -148,11 +148,29 @@ class BuildUnallocate(AjaxUpdateView):
|
||||
"""
|
||||
|
||||
model = Build
|
||||
form_class = forms.ConfirmBuildForm
|
||||
form_class = forms.UnallocateBuildForm
|
||||
ajax_form_title = _("Unallocate Stock")
|
||||
ajax_template_name = "build/unallocate.html"
|
||||
form_required = 'build.change'
|
||||
|
||||
def get_initial(self):
|
||||
|
||||
initials = super().get_initial()
|
||||
|
||||
# Pointing to a particular build output?
|
||||
output = self.get_param('output')
|
||||
|
||||
if output:
|
||||
try:
|
||||
output = StockItem.objects.get(pk=output)
|
||||
except (ValueError, StockItem.DoesNotExist):
|
||||
output = None
|
||||
|
||||
if output:
|
||||
initials['output_id'] = output.pk
|
||||
|
||||
return initials
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
|
||||
build = self.get_object()
|
||||
@ -160,13 +178,20 @@ class BuildUnallocate(AjaxUpdateView):
|
||||
|
||||
confirm = request.POST.get('confirm', False)
|
||||
|
||||
output_id = request.POST.get('output_id', None)
|
||||
|
||||
try:
|
||||
output = StockItem.objects.get(pk=output_id)
|
||||
except (ValueError, StockItem.DoesNotExist):
|
||||
output = None
|
||||
|
||||
valid = False
|
||||
|
||||
if confirm is False:
|
||||
form.errors['confirm'] = [_('Confirm unallocation of build stock')]
|
||||
form.non_field_errors = [_('Check the confirmation box')]
|
||||
else:
|
||||
build.unallocateStock()
|
||||
build.unallocateStock(output=output)
|
||||
valid = True
|
||||
|
||||
data = {
|
||||
|
Reference in New Issue
Block a user