From 1b1cd944be274e48bdd6daa4f57b196ff051508e Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 13 Apr 2020 18:50:59 +1000 Subject: [PATCH] API changes - Allow SupplierPart to be filtered by 'company' in addition to 'supplier' and 'manufacturer' - Stock can now also be filtered by 'company' --- .../static/script/inventree/company.js | 8 ---- InvenTree/company/api.py | 17 ++++--- InvenTree/company/models.py | 44 +++++++++++++------ .../company/templates/company/delete.html | 4 +- .../company/templates/company/detail.html | 13 ++++-- .../templates/company/detail_stock.html | 3 +- InvenTree/company/templates/company/tabs.html | 6 ++- InvenTree/company/tests.py | 6 +-- InvenTree/company/urls.py | 2 +- InvenTree/stock/api.py | 21 +++++++-- 10 files changed, 81 insertions(+), 43 deletions(-) diff --git a/InvenTree/InvenTree/static/script/inventree/company.js b/InvenTree/InvenTree/static/script/inventree/company.js index 3bb9da60b8..3dc5041a87 100644 --- a/InvenTree/InvenTree/static/script/inventree/company.js +++ b/InvenTree/InvenTree/static/script/inventree/company.js @@ -69,14 +69,6 @@ function loadCompanyTable(table, url, options={}) { return ''; } }, - { - field: 'part_count', - title: 'Parts', - sortable: true, - formatter: function(value, row, index, field) { - return renderLink(value, row.url + 'parts/'); - } - }, ], }); } diff --git a/InvenTree/company/api.py b/InvenTree/company/api.py index 7c43d6ea2c..824558a810 100644 --- a/InvenTree/company/api.py +++ b/InvenTree/company/api.py @@ -81,12 +81,19 @@ class SupplierPartList(generics.ListCreateAPIView): queryset = SupplierPart.objects.all().prefetch_related( 'part', - 'part__category', - 'part__stock_items', - 'part__bom_items', - 'part__builds', 'supplier', - 'pricebreaks') + 'manufacturer' + ) + + def get_queryset(self): + + queryset = super().get_queryset() + + # Filter by EITHER manufacturer or supplier + company = self.request.query_params.get('company', None) + + if company is not None: + queryset = queryset.filter(Q(manufacturer=company) | Q(supplier=company)) def get_serializer(self, *args, **kwargs): diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py index b807d6825f..e0b995255a 100644 --- a/InvenTree/company/models.py +++ b/InvenTree/company/models.py @@ -13,7 +13,7 @@ from decimal import Decimal from django.utils.translation import gettext_lazy as _ from django.core.validators import MinValueValidator from django.db import models -from django.db.models import Sum +from django.db.models import Sum, Q from django.apps import apps from django.urls import reverse @@ -140,26 +140,44 @@ class Company(models.Model): return getBlankThumbnail() @property - def part_count(self): + def manufactured_part_count(self): + """ The number of parts manufactured by this company """ + return self.manufactured_parts.count() + + @property + def has_manufactured_parts(self): + return self.manufactured_part_count > 0 + + @property + def supplied_part_count(self): """ The number of parts supplied by this company """ + return self.supplied_parts.count() + + @property + def has_supplied_parts(self): + """ Return True if this company supplies any parts """ + return self.supplied_part_count > 0 + + @property + def parts(self): + """ Return SupplierPart objects which are supplied or manufactured by this company """ + return SupplierPart.objects.filter(Q(supplier=self.id) | Q(manufacturer=self.id)) + + @property + def part_count(self): + """ The number of parts manufactured (or supplied) by this Company """ return self.parts.count() - @property - def has_parts(self): - """ Return True if this company supplies any parts """ - return self.part_count > 0 - @property def stock_items(self): - """ Return a list of all stock items supplied by this company """ + """ Return a list of all stock items supplied or manufactured by this company """ stock = apps.get_model('stock', 'StockItem') - return stock.objects.filter(supplier_part__supplier=self.id).all() + return stock.objects.filter(Q(supplier_part__supplier=self.id) | Q(supplier_part__manufacturer=self.id)).all() @property def stock_count(self): - """ Return the number of stock items supplied by this company """ - stock = apps.get_model('stock', 'StockItem') - return stock.objects.filter(supplier_part__supplier=self.id).count() + """ Return the number of stock items supplied or manufactured by this company """ + return self.stock_items.count() def outstanding_purchase_orders(self): """ Return purchase orders which are 'outstanding' """ @@ -255,7 +273,7 @@ class SupplierPart(models.Model): ) supplier = models.ForeignKey(Company, on_delete=models.CASCADE, - related_name='parts', + related_name='supplied_parts', limit_choices_to={'is_supplier': True}, help_text=_('Select supplier'), ) diff --git a/InvenTree/company/templates/company/delete.html b/InvenTree/company/templates/company/delete.html index 9a94404739..191e07c2f3 100644 --- a/InvenTree/company/templates/company/delete.html +++ b/InvenTree/company/templates/company/delete.html @@ -6,8 +6,8 @@ Are you sure you want to delete company '{{ company.name }}'?
-{% if company.part_count > 0 %} -

There are {{ company.part_count }} parts sourced from this company.
+{% if company.supplied_part_count > 0 %} +

There are {{ company.supplied_part_count }} parts sourced from this company.
If this supplier is deleted, these supplier part entries will also be deleted.