From a2ffd06abf3c50b19337e343ca583350ee4131d8 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 13 Aug 2021 17:49:58 +0200 Subject: [PATCH] calculate purchase price for part --- InvenTree/part/models.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 50d3044771..1e45ea5f4c 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -35,6 +35,8 @@ from stdimage.models import StdImageField from decimal import Decimal, InvalidOperation from datetime import datetime import hashlib +from djmoney.contrib.exchange.models import convert_money +from common.settings import currency_code_default from InvenTree import helpers from InvenTree import validators @@ -1514,7 +1516,7 @@ class Part(MPTTModel): return (min_price, max_price) - def get_bom_price_range(self, quantity=1, internal=False): + def get_bom_price_range(self, quantity=1, internal=False, purchase=False): """ Return the price range of the BOM for this part. Adds the minimum price for all components in the BOM. @@ -1531,7 +1533,7 @@ class Part(MPTTModel): print("Warning: Item contains itself in BOM") continue - prices = item.sub_part.get_price_range(quantity * item.quantity, internal=internal) + prices = item.sub_part.get_price_range(quantity * item.quantity, internal=internal, purchase=purchase) if prices is None: continue @@ -1555,16 +1557,17 @@ class Part(MPTTModel): return (min_price, max_price) - def get_price_range(self, quantity=1, buy=True, bom=True, internal=False): + def get_price_range(self, quantity=1, buy=True, bom=True, internal=False, purchase=False): """ Return the price range for this part. This price can be either: - Supplier price (if purchased from suppliers) - BOM price (if built from other parts) - Internal price (if set for the part) + - Purchase price (if set for the part) Returns: - Minimum of the supplier, BOM or internal price. If no pricing available, returns None + Minimum of the supplier, BOM, internal or purchase price. If no pricing available, returns None """ # only get internal price if set and should be used @@ -1572,6 +1575,12 @@ class Part(MPTTModel): internal_price = self.get_internal_price(quantity) return internal_price, internal_price + # only get purchase price if set and should be used + if purchase: + purchase_price = self.get_purchase_price(quantity) + if purchase_price: + return purchase_price + buy_price_range = self.get_supplier_price_range(quantity) if buy else None bom_price_range = self.get_bom_price_range(quantity, internal=internal) if bom else None @@ -1641,6 +1650,11 @@ class Part(MPTTModel): def internal_unit_pricing(self): return self.get_internal_price(1) + def get_purchase_price(self, quantity): + currency = currency_code_default() + prices = [convert_money(item.purchase_price, currency).amount for item in self.stock_items.all()] + return min(prices) * quantity, max(prices) * quantity + @transaction.atomic def copy_bom_from(self, other, clear=True, **kwargs): """