Merge pull request #343 from SchrodingersGat/filter-by-supplier

Filter by supplier
This commit is contained in:
Oliver 2019-05-16 21:33:14 +10:00 committed by GitHub
commit 283bd0e3ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 0 deletions

View File

@ -7,6 +7,7 @@ from __future__ import unicode_literals
import os
from django.apps import apps
from django.db import models
from django.urls import reverse
from django.conf import settings
@ -111,6 +112,18 @@ class Company(models.Model):
""" 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 """
stock = apps.get_model('stock', 'StockItem')
return stock.objects.filter(supplier_part__supplier=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()
class Contact(models.Model):
""" A Contact represents a person who works at a particular company.

View File

@ -72,6 +72,11 @@ InvenTree | Company - {{ company.name }}
{% endblock %}
{% block js_load %}
{{ block.super }}
<script type='text/javascript' src="{% static 'script/inventree/stock.js' %}"></script>
{% endblock %}
{% block js_ready %}
enableDragAndDrop(

View File

@ -0,0 +1,25 @@
{% extends "company/company_base.html" %}
{% load static %}
{% block details %}
{% include "company/tabs.html" with tab='stock' %}
<h3>Supplier Stock</h3>
<table class='table table-striped table-condensed' id='stock-table'>
</table>
{% endblock %}
{% block js_ready %}
{{ block.super }}
loadStockTable($('#stock-table'),
{
url: "{% url 'api-stock-list' %}",
params: {
supplier: {{ company.id }},
}
});
{% endblock %}

View File

@ -6,6 +6,9 @@
<li{% if tab == 'parts' %} class='active'{% endif %}>
<a href="{% url 'company-detail-parts' company.id %}">Supplier Parts <span class='badge'>{{ company.part_count }}</span></a>
</li>
<li{% if tab == 'stock' %} class='active'{% endif %}>
<a href="{% url 'company-detail-stock' company.id %}">Stock <span class='badge'>{{ company.stock_count }}</a>
</li>
{% if 0 %}
<li{% if tab == 'po' %} class='active'{% endif %}>
<a href="#">Purchase Orders</a>

View File

@ -16,6 +16,7 @@ company_detail_urls = [
# url(r'orders/?', views.CompanyDetail.as_view(template_name='company/orders.html'), name='company-detail-orders'),
url(r'parts/?', views.CompanyDetail.as_view(template_name='company/detail_part.html'), name='company-detail-parts'),
url(r'stock/?', views.CompanyDetail.as_view(template_name='company/detail_stock.html'), name='company-detail-stock'),
url(r'thumbnail/?', views.CompanyImage.as_view(), name='company-image'),

View File

@ -242,6 +242,7 @@ class StockList(generics.ListCreateAPIView):
Additional query parameters are available:
- location: Filter stock by location
- category: Filter by parts belonging to a certain category
- supplier: Filter by supplier
"""
def get_queryset(self):
@ -275,6 +276,12 @@ class StockList(generics.ListCreateAPIView):
except PartCategory.DoesNotExist:
pass
# Filter by supplier
supplier_id = self.request.query_params.get('supplier', None)
if supplier_id:
stock_list = stock_list.filter(supplier_part__supplier=supplier_id)
return stock_list
serializer_class = StockItemSerializer