From a80c11f3ce83b64d9915a749c00684f8713772c3 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 15 May 2019 00:22:10 +1000 Subject: [PATCH] Add function to infer default_supplier for a Part --- InvenTree/part/models.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index b316da2813..7e83486f18 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -300,6 +300,23 @@ class Part(models.Model): # Default case - no default category found return None + def get_default_supplier(self): + """ Get the default supplier part for this part (may be None). + + - If the part specifies a default_supplier, return that + - If there is only one supplier part available, return that + - Else, return None + """ + + if self.default_supplier: + return self.default_suppliers + + if self.supplier_count == 1: + return self.supplier_parts.first() + + # Default to None if there are multiple suppliers to choose from + return None + default_supplier = models.ForeignKey('part.SupplierPart', on_delete=models.SET_NULL, blank=True, null=True,