Function to calculate how many parts can be made with current stock

This commit is contained in:
Oliver 2018-04-16 21:49:38 +10:00
parent 202a8e65b9
commit e43439ef5b
5 changed files with 49 additions and 13 deletions

View File

@ -132,8 +132,34 @@ class Part(models.Model):
verbose_name_plural = "Parts"
@property
def tracked_parts(self):
return self.serials.order_by('serial')
def available_stock(self):
"""
Return the total available stock.
This subtracts stock which is already allocated
"""
# TODO - For now, just return total stock count
return self.stock
@property
def can_build(self):
""" Return the number of units that can be build with available stock
"""
# If this part does NOT have a BOM, result is simply the currently available stock
if not self.has_bom:
return self.available_stock
total = None
for item in self.bom_items.all():
stock = item.sub_part.available_stock
n = int(1.0 * stock / item.quantity)
if total is None or n < total:
total = n
return total
@property
def stock(self):
@ -149,11 +175,15 @@ class Part(models.Model):
return result['total']
@property
def bomItemCount(self):
def has_bom(self):
return self.bom_item_count > 0
@property
def bom_item_count(self):
return self.bom_items.all().count()
@property
def usedInCount(self):
def used_in_count(self):
return self.used_in.all().count()
"""

View File

@ -15,7 +15,7 @@
<tr>
<td><a href="{% url 'part-detail' sub_part.id %}">{{ sub_part.name }}</a></td>
<td>{{ sub_part.description }}</td>
<td>{{ bom_item.quantity }}</td>
<td>{{ bom_item.quantity }}<span class='badge'>{{ bom_item.sub_part.available_stock }}</span></td>
</tr>
{% endwith %}
{% endfor %}

View File

@ -7,8 +7,8 @@
{% block del_body %}
{% if part.usedInCount > 0 %}
<p>This part is used in BOMs for {{ part.usedInCount }} other parts. If you delete this part, the BOMs for the following parts will be updated:
{% if part.used_in_count %}
<p>This part is used in BOMs for {{ part.used_in_count }} other parts. If you delete this part, the BOMs for the following parts will be updated:
<ul class="list-group">
{% for child in part.used_in.all %}
<li class='list-group-item'>{{ child.part.name }} - {{ child.part.description }}</li>

View File

@ -40,9 +40,15 @@
</tr>
{% endif %}
<tr>
<td>Stock</td>
<td>{{ part.stock }}</td>
<td>Available Stock</td>
<td>{{ part.available_stock }}</td>
</tr>
{% if part.has_bom %}
<tr>
<td>Can Build</td>
<td>{{ part.can_build }}</td>
</tr>
{% endif %}
</table>
</div>
</div>

View File

@ -1,11 +1,11 @@
<ul class="nav nav-tabs">
<li{% ifequal tab 'detail' %} class="active"{% endifequal %}><a href="{% url 'part-detail' part.id %}">Details</a></li>
<li{% ifequal tab 'bom' %} class="active"{% endifequal %}><a href="{% url 'part-bom' part.id %}">BOM{% if part.bomItemCount > 0 %}<span class="badge">{{ part.bomItemCount }}</span>{% endif %}</a></li>
{% if part.bomItemCount > 0 %}
<li{% ifequal tab 'bom' %} class="active"{% endifequal %}><a href="{% url 'part-bom' part.id %}">BOM{% if part.has_bom > 0 %}<span class="badge">{{ part.bom_item_cout }}</span>{% endif %}</a></li>
{% if part.has_bom %}
<li{% ifequal tab 'build' %} class "active"{% endifequal %}><a href="#">Build</a></li>
{% endif %}
{% if part.usedInCount > 0 %}
<li{% ifequal tab 'used' %} class="active"{% endifequal %}><a href="{% url 'part-used-in' part.id %}">Used In{% if part.usedInCount > 0 %}<span class="badge">{{ part.usedInCount }}</span>{% endif %}</a></li>
{% if part.used_in_count > 0 %}
<li{% ifequal tab 'used' %} class="active"{% endifequal %}><a href="{% url 'part-used-in' part.id %}">Used In{% if part.used_in_count > 0 %}<span class="badge">{{ part.used_in_count }}</span>{% endif %}</a></li>
{% endif %}
<li{% ifequal tab 'stock' %} class="active"{% endifequal %}><a href="{% url 'part-stock' part.id %}">Stock <span class="badge">{{ part.stock }}</span></a></li>
{% if part.purchaseable %}