From 768080f9a002761980b08ffaa02c3a807f8dc3aa Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 5 Jun 2021 17:06:58 +0200 Subject: [PATCH] Adding internal functions to use internal prices --- InvenTree/common/models.py | 9 ++++++--- InvenTree/part/models.py | 16 ++++++++++++++++ InvenTree/part/templates/part/order_prices.html | 13 +++++++++++++ InvenTree/part/views.py | 6 ++++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 236e48770f..f08aad1c68 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -726,7 +726,7 @@ class PriceBreak(models.Model): return converted.amount -def get_price(instance, quantity, moq=True, multiples=True, currency=None): +def get_price(instance, quantity, moq=True, multiples=True, currency=None, break_name:str='price_breaks'): """ Calculate the price based on quantity price breaks. - Don't forget to add in flat-fee cost (base_cost field) @@ -734,7 +734,10 @@ def get_price(instance, quantity, moq=True, multiples=True, currency=None): - If order multiples are to be observed, then we need to calculate based on that, too """ - price_breaks = instance.price_breaks.all() + if hasattr(instance, break_name): + price_breaks = getattr(instance, break_name).all() + else: + price_breaks = [] # No price break information available? if len(price_breaks) == 0: @@ -756,7 +759,7 @@ def get_price(instance, quantity, moq=True, multiples=True, currency=None): currency = currency_code_default() pb_min = None - for pb in instance.price_breaks.all(): + for pb in price_breaks: # Store smallest price break if not pb_min: pb_min = pb diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index a09e6db217..9ff4938891 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -1649,6 +1649,22 @@ class Part(MPTTModel): price=price ) + def get_internal_price(instance, quantity, moq=True, multiples=True, currency=None): + return common.models.get_price(instance, quantity, moq, multiples, currency, break_name='internal_price_breaks') + + @property + def has_internal_price_breaks(self): + return self.internal_price_breaks.count() > 0 + + @property + def internal_price_breaks(self): + """ Return the associated price breaks in the correct order """ + return self.internalpricebreaks.order_by('quantity').all() + + @property + def internal_unit_pricing(self): + return self.get_internal_price(1) + @transaction.atomic def copy_bom_from(self, other, clear=True, **kwargs): """ diff --git a/InvenTree/part/templates/part/order_prices.html b/InvenTree/part/templates/part/order_prices.html index 6c5b2173bf..23644ef5b8 100644 --- a/InvenTree/part/templates/part/order_prices.html +++ b/InvenTree/part/templates/part/order_prices.html @@ -77,6 +77,19 @@ {% endif %} {% endif %} +{% if total_internal_part_price %} + + {% trans 'Internal Price' %} + {% trans 'Unit Cost' %} + {% include "price.html" with price=unit_internal_part_price %} + + + + {% trans 'Total Cost' %} + {% include "price.html" with price=total_internal_part_price %} + +{% endif %} + {% if total_part_price %} {% trans 'Sale Price' %} diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 5927351124..ee7733a6be 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -2127,6 +2127,12 @@ class PartPricing(AjaxView): ctx['max_total_bom_price'] = max_bom_price ctx['max_unit_bom_price'] = max_unit_bom_price + # internal part pricing information + internal_part_price = part.get_internal_price(quantity) + if internal_part_price is not None: + ctx['total_internal_part_price'] = round(internal_part_price, 3) + ctx['unit_internal_part_price'] = round(internal_part_price / quantity, 3) + # part pricing information part_price = part.get_price(quantity) if part_price is not None: