diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py
index 880d37869b..d054aff040 100644
--- a/InvenTree/company/models.py
+++ b/InvenTree/company/models.py
@@ -307,6 +307,11 @@ class SupplierPart(models.Model):
else:
return None
+ def purchase_orders(self):
+ """ Returns a list of purchase orders relating to this supplier part """
+
+ return [line.order for line in self.purchase_order_line_items.all().prefetch_related('order')]
+
def __str__(self):
s = "{supplier} ({sku})".format(
sku=self.SKU,
diff --git a/InvenTree/order/migrations/0007_auto_20190605_2138.py b/InvenTree/order/migrations/0007_auto_20190605_2138.py
new file mode 100644
index 0000000000..ce2119f258
--- /dev/null
+++ b/InvenTree/order/migrations/0007_auto_20190605_2138.py
@@ -0,0 +1,19 @@
+# Generated by Django 2.2 on 2019-06-05 11:38
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('order', '0006_auto_20190605_2056'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='purchaseorderlineitem',
+ name='part',
+ field=models.ForeignKey(blank=True, help_text='Supplier part', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='order_line_items', to='company.SupplierPart'),
+ ),
+ ]
diff --git a/InvenTree/order/migrations/0008_auto_20190605_2140.py b/InvenTree/order/migrations/0008_auto_20190605_2140.py
new file mode 100644
index 0000000000..688c8cf15c
--- /dev/null
+++ b/InvenTree/order/migrations/0008_auto_20190605_2140.py
@@ -0,0 +1,19 @@
+# Generated by Django 2.2 on 2019-06-05 11:40
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('order', '0007_auto_20190605_2138'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='purchaseorderlineitem',
+ name='part',
+ field=models.ForeignKey(blank=True, help_text='Supplier part', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='purchase_order_line_items', to='company.SupplierPart'),
+ ),
+ ]
diff --git a/InvenTree/order/models.py b/InvenTree/order/models.py
index 56ef5952fb..dc72522e67 100644
--- a/InvenTree/order/models.py
+++ b/InvenTree/order/models.py
@@ -124,7 +124,7 @@ class PurchaseOrderLineItem(OrderLineItem):
part = models.ForeignKey(
SupplierPart, on_delete=models.SET_NULL,
blank=True, null=True,
- related_name='orders',
+ related_name='purchase_order_line_items',
help_text=_("Supplier part"),
)
diff --git a/InvenTree/order/templates/order/po_table.html b/InvenTree/order/templates/order/po_table.html
new file mode 100644
index 0000000000..5d5295b25e
--- /dev/null
+++ b/InvenTree/order/templates/order/po_table.html
@@ -0,0 +1,16 @@
+
+
+ Company |
+ Order Reference |
+ Description |
+ Status |
+
+ {% for order in orders %}
+
+ {% include "hover_image.html" with image=order.supplier.image hover=True %}{{ order.supplier.name }} |
+ {{ order }} |
+ {{ order.description }} |
+ {% include "order/order_status.html" %} |
+
+ {% endfor %}
+
\ No newline at end of file
diff --git a/InvenTree/order/templates/order/purchase_orders.html b/InvenTree/order/templates/order/purchase_orders.html
index 4e48effa59..c599bfcd20 100644
--- a/InvenTree/order/templates/order/purchase_orders.html
+++ b/InvenTree/order/templates/order/purchase_orders.html
@@ -10,22 +10,6 @@ InvenTree | Purchase Orders
Purchase Orders
-
-
-
- Company |
- Order Reference |
- Description |
- Status |
-
- {% for order in orders %}
-
- {% include "hover_image.html" with image=order.supplier.image hover=True %}{{ order.supplier.name }} |
- {{ order }} |
- {{ order.description }} |
- {% include "order/order_status.html" %} |
-
- {% endfor %}
-
+{% include "order/po_table.html" %}
{% endblock %}
\ No newline at end of file
diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py
index 29a11125ab..9a2486798d 100644
--- a/InvenTree/part/models.py
+++ b/InvenTree/part/models.py
@@ -794,6 +794,18 @@ class Part(models.Model):
return n
+ def purchase_orders(self):
+ """ Return a list of purchase orders which reference this part """
+
+ orders = []
+
+ for part in self.supplier_parts.all().prefetch_related('purchase_order_line_items'):
+ for order in part.purchase_orders():
+ if order not in orders:
+ orders.append(order)
+
+ return orders
+
def attach_file(instance, filename):
""" Function for storing a file for a PartAttachment
diff --git a/InvenTree/part/templates/part/orders.html b/InvenTree/part/templates/part/orders.html
new file mode 100644
index 0000000000..ed8beb9b42
--- /dev/null
+++ b/InvenTree/part/templates/part/orders.html
@@ -0,0 +1,18 @@
+{% extends "part/part_base.html" %}
+{% load static %}
+
+{% block details %}
+
+{% include 'part/tabs.html' with tab='orders' %}
+
+
+
+{% include "order/po_table.html" with orders=part.purchase_orders %}
+
+{% endblock %}
\ No newline at end of file
diff --git a/InvenTree/part/templates/part/tabs.html b/InvenTree/part/templates/part/tabs.html
index dd5034040d..c7dbb2418f 100644
--- a/InvenTree/part/templates/part/tabs.html
+++ b/InvenTree/part/templates/part/tabs.html
@@ -25,11 +25,17 @@
Used In{% if part.used_in_count > 0 %}{{ part.used_in_count }}{% endif %}
{% endif %}
- {% if part.purchaseable and part.is_template == False %}
+ {% if part.purchaseable %}
+ {% if part.is_template == False %}
Suppliers
{{ part.supplier_count }}
-
+
+
+ {% endif %}
+
+ Purchase Orders {{ part.purchase_orders|length }}
+
{% endif %}
{% if part.trackable and 0 %}
diff --git a/InvenTree/part/urls.py b/InvenTree/part/urls.py
index 0147cd4d07..e8a0175503 100644
--- a/InvenTree/part/urls.py
+++ b/InvenTree/part/urls.py
@@ -34,6 +34,7 @@ part_detail_urls = [
url(r'^build/?', views.PartDetail.as_view(template_name='part/build.html'), name='part-build'),
url(r'^used/?', views.PartDetail.as_view(template_name='part/used_in.html'), name='part-used-in'),
url(r'^suppliers/?', views.PartDetail.as_view(template_name='part/supplier.html'), name='part-suppliers'),
+ url(r'^orders/?', views.PartDetail.as_view(template_name='part/orders.html'), name='part-orders'),
url(r'^track/?', views.PartDetail.as_view(template_name='part/track.html'), name='part-track'),
url(r'^attachments/?', views.PartDetail.as_view(template_name='part/attachments.html'), name='part-attachments'),
diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py
index 4f61036f95..9f8b8652bd 100644
--- a/InvenTree/part/views.py
+++ b/InvenTree/part/views.py
@@ -24,6 +24,7 @@ from InvenTree.views import AjaxView, AjaxCreateView, AjaxUpdateView, AjaxDelete
from InvenTree.views import QRCodeView
from InvenTree.helpers import DownloadFile, str2bool
+from InvenTree.status_codes import OrderStatus
class PartIndex(ListView):
@@ -446,6 +447,8 @@ class PartDetail(DetailView):
context['starred'] = part.isStarredBy(self.request.user)
+ context['OrderStatus'] = OrderStatus
+
return context