diff --git a/InvenTree/company/api.py b/InvenTree/company/api.py index 239d6064d3..ddd5624238 100644 --- a/InvenTree/company/api.py +++ b/InvenTree/company/api.py @@ -94,6 +94,7 @@ class ManufacturerPartList(generics.ListCreateAPIView): queryset = ManufacturerPart.objects.all().prefetch_related( 'part', 'manufacturer', + 'supplier_parts', ) serializer_class = ManufacturerPartSerializer @@ -141,6 +142,12 @@ class ManufacturerPartList(generics.ListCreateAPIView): if part is not None: queryset = queryset.filter(part=part) + # Filter by supplier part? + supplier_part = params.get('supplier_part', None) + + if supplier_part is not None: + queryset = queryset.filter(supplier_parts=supplier_part) + # Filter by 'active' status of the part? active = params.get('active', None) diff --git a/InvenTree/company/fixtures/manufacturer_part.yaml b/InvenTree/company/fixtures/manufacturer_part.yaml index 7532955eac..880a0e5862 100644 --- a/InvenTree/company/fixtures/manufacturer_part.yaml +++ b/InvenTree/company/fixtures/manufacturer_part.yaml @@ -20,3 +20,20 @@ part: 5 manufacturer: 7 MPN: 'MPN789' + +# Supplier parts linked to Manufacturer parts +- model: company.supplierpart + pk: 10 + fields: + part: 3 + manufacturer_part: 2 + supplier: 2 + SKU: 'MPN456-APPEL' + +- model: company.supplierpart + pk: 11 + fields: + part: 3 + manufacturer_part: 2 + supplier: 3 + SKU: 'MPN456-ZERG' diff --git a/InvenTree/company/fixtures/supplier_part.yaml b/InvenTree/company/fixtures/supplier_part.yaml index b4c4d7e58a..446339d58b 100644 --- a/InvenTree/company/fixtures/supplier_part.yaml +++ b/InvenTree/company/fixtures/supplier_part.yaml @@ -52,20 +52,3 @@ part: 2 supplier: 2 SKU: 'ZERGM312' - -# Supplier parts linked to Manufacturer parts -- model: company.supplierpart - pk: 10 - fields: - part: 3 - manufacturer_part: 2 - supplier: 2 - SKU: 'MPN456-APPEL' - -- model: company.supplierpart - pk: 11 - fields: - part: 3 - manufacturer_part: 2 - supplier: 3 - SKU: 'MPN456-ZERG' diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py index 594d8a97ea..6a46745635 100644 --- a/InvenTree/company/models.py +++ b/InvenTree/company/models.py @@ -338,7 +338,7 @@ class ManufacturerPart(models.Model): ) @classmethod - def create(cls, part, manufacturer, mpn, link, description): + def create(cls, part, manufacturer, mpn, description, link=None): """ Check if ManufacturerPart instance does not already exist then create it """ @@ -415,8 +415,7 @@ class SupplierPart(models.Model): manufacturer_part = ManufacturerPart.create(part=self.part, manufacturer=manufacturer, mpn=MPN, - description=self.description, - link=self.link) + description=self.description) self.manufacturer_part = manufacturer_part else: # Update ManufacturerPart (if ID exists) diff --git a/InvenTree/company/serializers.py b/InvenTree/company/serializers.py index e3ecd625ee..35e84aac1e 100644 --- a/InvenTree/company/serializers.py +++ b/InvenTree/company/serializers.py @@ -162,6 +162,8 @@ class SupplierPartSerializer(InvenTreeModelSerializer): MPN = serializers.StringRelatedField(source='manufacturer_part.MPN') + manufacturer_part = ManufacturerPartSerializer(read_only=True) + class Meta: model = SupplierPart fields = [ @@ -175,6 +177,7 @@ class SupplierPartSerializer(InvenTreeModelSerializer): 'manufacturer', 'MPN', 'manufacturer_detail', + 'manufacturer_part', 'description', 'link', ] @@ -190,25 +193,10 @@ class SupplierPartSerializer(InvenTreeModelSerializer): MPN = self.initial_data.get('MPN', None) if manufacturer_id or MPN: - # Get SupplierPart data - part = validated_data.get('part', None) - description = validated_data.get('description', None) - link = validated_data.get('link', None) - - # Get manufacturer - try: - manufacturer = Company.objects.get(pk=int(manufacturer_id)) - except Company.DoesNotExist: - manufacturer = None - - # Create ManufacturerPart - manufacturer_part = ManufacturerPart.create(part=part, - manufacturer=manufacturer, - mpn=MPN, - description=description, - link=link) - supplier_part.manufacturer_part = manufacturer_part - supplier_part.save() + kwargs = {'manufacturer': manufacturer_id, + 'MPN': MPN, + } + supplier_part.save(**kwargs) return supplier_part diff --git a/InvenTree/company/templates/company/supplier_part_manufacturers.html b/InvenTree/company/templates/company/supplier_part_manufacturers.html index f2ce84b00b..deef6cdd1d 100644 --- a/InvenTree/company/templates/company/supplier_part_manufacturers.html +++ b/InvenTree/company/templates/company/supplier_part_manufacturers.html @@ -33,7 +33,7 @@ "{% url 'api-manufacturer-part-list' %}", { params: { - part: {{ part.id }}, + supplier_part: {{ part.id }}, part_detail: true, manufacturer_detail: true, },