mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 07:05:41 +00:00 
			
		
		
		
	Serialize BOMItem pricing information
- Account for quantity - Prefetch required items
This commit is contained in:
		@@ -933,3 +933,19 @@ class BomItem(models.Model):
 | 
				
			|||||||
        base_quantity = self.quantity * build_quantity
 | 
					        base_quantity = self.quantity * build_quantity
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return base_quantity + self.get_overage_quantity(base_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)
 | 
				
			||||||
@@ -122,6 +122,7 @@ class BomItemSerializer(InvenTreeModelSerializer):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
 | 
					    part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
 | 
				
			||||||
    sub_part_detail = PartBriefSerializer(source='sub_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
 | 
					    @staticmethod
 | 
				
			||||||
    def setup_eager_loading(queryset):
 | 
					    def setup_eager_loading(queryset):
 | 
				
			||||||
@@ -131,6 +132,7 @@ class BomItemSerializer(InvenTreeModelSerializer):
 | 
				
			|||||||
        queryset = queryset.prefetch_related('sub_part')
 | 
					        queryset = queryset.prefetch_related('sub_part')
 | 
				
			||||||
        queryset = queryset.prefetch_related('sub_part__category')
 | 
					        queryset = queryset.prefetch_related('sub_part__category')
 | 
				
			||||||
        queryset = queryset.prefetch_related('sub_part__stock_items')
 | 
					        queryset = queryset.prefetch_related('sub_part__stock_items')
 | 
				
			||||||
 | 
					        queryset = queryset.prefetch_related('sub_part__suppliers__price_breaks')
 | 
				
			||||||
        return queryset
 | 
					        return queryset
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    class Meta:
 | 
					    class Meta:
 | 
				
			||||||
@@ -142,6 +144,7 @@ class BomItemSerializer(InvenTreeModelSerializer):
 | 
				
			|||||||
            'sub_part',
 | 
					            'sub_part',
 | 
				
			||||||
            'sub_part_detail',
 | 
					            'sub_part_detail',
 | 
				
			||||||
            'quantity',
 | 
					            'quantity',
 | 
				
			||||||
 | 
					            'price_range',
 | 
				
			||||||
            'overage',
 | 
					            'overage',
 | 
				
			||||||
            'note',
 | 
					            'note',
 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -149,8 +149,22 @@ function loadBomTable(table, options) {
 | 
				
			|||||||
                
 | 
					                
 | 
				
			||||||
                return renderLink(text, row.sub_part_detail.url + "stock/");
 | 
					                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 "<span class='warning-msg'>No pricing available</span>";
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
        );
 | 
					            }
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    // Part notes
 | 
					    // Part notes
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user