Merge pull request #296 from SchrodingersGat/take-from

Take from
This commit is contained in:
Oliver 2019-05-10 19:54:39 +10:00 committed by GitHub
commit be144c0d35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 4 deletions

View File

@ -21,6 +21,7 @@ class EditBuildForm(HelperForm):
'title',
'part',
'quantity',
'take_from',
'batch',
'URL',
'notes',

View File

@ -0,0 +1,20 @@
# Generated by Django 2.2 on 2019-05-10 08:50
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('stock', '0015_stockitem_delete_on_deplete'),
('build', '0012_auto_20190508_2332'),
]
operations = [
migrations.AddField(
model_name='build',
name='take_from',
field=models.ForeignKey(blank=True, help_text='Select location to take stock from for this build (leave blank to take from any stock location', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='sourcing_builds', to='stock.StockLocation'),
),
]

View File

@ -27,6 +27,7 @@ class Build(models.Model):
part: The part to be built (from component BOM items)
title: Brief title describing the build (required)
quantity: Number of units to be built
take_from: Location to take stock from to make this build (if blank, can take from anywhere)
status: Build status code
batch: Batch code transferred to build parts (optional)
creation_date: Date the build was created (auto)
@ -41,6 +42,11 @@ class Build(models.Model):
def get_absolute_url(self):
return reverse('build-detail', kwargs={'pk': self.id})
title = models.CharField(
blank=False,
max_length=100,
help_text='Brief description of the build')
part = models.ForeignKey('part.Part', on_delete=models.CASCADE,
related_name='builds',
limit_choices_to={
@ -50,10 +56,11 @@ class Build(models.Model):
help_text='Select part to build',
)
title = models.CharField(
blank=False,
max_length=100,
help_text='Brief description of the build')
take_from = models.ForeignKey('stock.StockLocation', on_delete=models.SET_NULL,
related_name='sourcing_builds',
null=True, blank=True,
help_text='Select location to take stock from for this build (leave blank to take from any stock location'
)
quantity = models.PositiveIntegerField(
default=1,
@ -139,6 +146,11 @@ class Build(models.Model):
stock = StockItem.objects.filter(part=item.sub_part)
# Ensure that the available stock items are in the correct location
if self.take_from is not None:
# Filter for stock that is located downstream of the designated location
stock = stock.filter(location__in=[loc for loc in self.take_from.getUniqueChildren()])
# Only one StockItem to choose from? Default to that one!
if len(stock) == 1:
stock_item = stock[0]

View File

@ -45,6 +45,16 @@ InvenTree | Build - {{ build }}
<tr>
<td>Quantity</td><td>{{ build.quantity }}</td>
</tr>
<tr>
<td>Stock Source</td>
<td>
{% if build.take_from %}
<a href="{% url 'stock-location-detail' build.take_from.id %}">{{ build.take_from }}</a>
{% else %}
Stock can be taken from any available location.
{% endif %}
</td>
</tr>
<tr>
<td>Status</td><td>{% include "build_status.html" with build=build %}</td>
</tr>

View File

@ -374,6 +374,16 @@ class BuildItemCreate(AjaxCreateView):
query = query.filter(part=part_id)
if build_id is not None:
try:
build = Build.objects.get(id=build_id)
if build.take_from is not None:
# Limit query to stock items that are downstream of the 'take_from' location
query = query.filter(location__in=[loc for loc in build.take_from.getUniqueChildren()])
except Build.DoesNotExist:
pass
# Exclude StockItem objects which are already allocated to this build and part
query = query.exclude(id__in=[item.stock_item.id for item in BuildItem.objects.filter(build=build_id, stock_item__part=part_id)])