From 988a35d23742378efc3b98891b67eed87b6b67d5 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 21 May 2019 15:15:54 +1000 Subject: [PATCH 1/3] Serialize BOMItem pricing information - Account for quantity - Prefetch required items --- InvenTree/part/models.py | 16 ++++++++++++++++ InvenTree/part/serializers.py | 3 +++ InvenTree/static/script/inventree/bom.js | 18 ++++++++++++++++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 1d1d1b4fec..829f654c06 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -933,3 +933,19 @@ class BomItem(models.Model): base_quantity = self.quantity * build_quantity return base_quantity + self.get_overage_quantity(base_quantity) + + @property + def price_range(self): + """ Return the price-range for this BOM item. """ + + prange = self.sub_part.get_price_range(self.quantity) + + if prange is None: + return prange + + pmin, pmax = prange + + if pmin == pmax: + return str(pmin) + + return "{pmin} to {pmax}".format(pmin=pmin, pmax=pmax) \ No newline at end of file diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index 213cb9c75e..dd3c5f6313 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -122,6 +122,7 @@ class BomItemSerializer(InvenTreeModelSerializer): part_detail = PartBriefSerializer(source='part', many=False, read_only=True) sub_part_detail = PartBriefSerializer(source='sub_part', many=False, read_only=True) + price_range = serializers.CharField(read_only=True) @staticmethod def setup_eager_loading(queryset): @@ -131,6 +132,7 @@ class BomItemSerializer(InvenTreeModelSerializer): queryset = queryset.prefetch_related('sub_part') queryset = queryset.prefetch_related('sub_part__category') queryset = queryset.prefetch_related('sub_part__stock_items') + queryset = queryset.prefetch_related('sub_part__suppliers__price_breaks') return queryset class Meta: @@ -142,6 +144,7 @@ class BomItemSerializer(InvenTreeModelSerializer): 'sub_part', 'sub_part_detail', 'quantity', + 'price_range', 'overage', 'note', ] diff --git a/InvenTree/static/script/inventree/bom.js b/InvenTree/static/script/inventree/bom.js index 6ff81de4fc..a995fe0ed8 100644 --- a/InvenTree/static/script/inventree/bom.js +++ b/InvenTree/static/script/inventree/bom.js @@ -149,8 +149,22 @@ function loadBomTable(table, options) { return renderLink(text, row.sub_part_detail.url + "stock/"); } - } - ); + }); + + cols.push( + { + field: 'price_range', + title: 'Price', + sortable: true, + formatter: function(value, row, index, field) { + if (value) { + return value; + } else { + return "No pricing available"; + } + } + }); + } // Part notes From d1dfad591dffa75729b1cb0360bf3d41606dac24 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 21 May 2019 15:38:22 +1000 Subject: [PATCH 2/3] Pepperoni --- InvenTree/part/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 829f654c06..2cfb37d3e3 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -948,4 +948,4 @@ class BomItem(models.Model): if pmin == pmax: return str(pmin) - return "{pmin} to {pmax}".format(pmin=pmin, pmax=pmax) \ No newline at end of file + return "{pmin} to {pmax}".format(pmin=pmin, pmax=pmax) From d3a5661dbdb40546d7802b04c43c21658bb2e5b5 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 21 May 2019 15:42:52 +1000 Subject: [PATCH 3/3] Serializer fix --- InvenTree/part/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index dd3c5f6313..cb7cc8bda3 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -132,7 +132,7 @@ class BomItemSerializer(InvenTreeModelSerializer): queryset = queryset.prefetch_related('sub_part') queryset = queryset.prefetch_related('sub_part__category') queryset = queryset.prefetch_related('sub_part__stock_items') - queryset = queryset.prefetch_related('sub_part__suppliers__price_breaks') + queryset = queryset.prefetch_related('sub_part__supplier_parts__pricebreaks') return queryset class Meta: