Merge pull request #1726 from matmair/bom-pricing

Bom pricing
This commit is contained in:
Oliver 2021-06-28 09:15:52 +10:00 committed by GitHub
commit b5086ab731
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 18 deletions

View File

@ -21,6 +21,9 @@ import InvenTree.version
from common.models import InvenTreeSetting from common.models import InvenTreeSetting
from .settings import MEDIA_URL, STATIC_URL from .settings import MEDIA_URL, STATIC_URL
from common.settings import currency_code_default
from djmoney.money import Money
def getSetting(key, backup_value=None): def getSetting(key, backup_value=None):
@ -247,6 +250,22 @@ def decimal2string(d):
return s.rstrip("0").rstrip(".") return s.rstrip("0").rstrip(".")
def decimal2money(d, currency=None):
"""
Format a Decimal number as Money
Args:
d: A python Decimal object
currency: Currency of the input amount, defaults to default currency in settings
Returns:
A Money object from the input(s)
"""
if not currency:
currency = currency_code_default()
return Money(d, currency)
def WrapWithQuotes(text, quote='"'): def WrapWithQuotes(text, quote='"'):
""" Wrap the supplied text with quotes """ Wrap the supplied text with quotes

View File

@ -39,7 +39,7 @@ from InvenTree import helpers
from InvenTree import validators from InvenTree import validators
from InvenTree.models import InvenTreeTree, InvenTreeAttachment from InvenTree.models import InvenTreeTree, InvenTreeAttachment
from InvenTree.fields import InvenTreeURLField from InvenTree.fields import InvenTreeURLField
from InvenTree.helpers import decimal2string, normalize from InvenTree.helpers import decimal2string, normalize, decimal2money
from InvenTree.status_codes import BuildStatus, PurchaseOrderStatus, SalesOrderStatus from InvenTree.status_codes import BuildStatus, PurchaseOrderStatus, SalesOrderStatus
@ -2414,7 +2414,7 @@ class BomItem(models.Model):
return "{n} x {child} to make {parent}".format( return "{n} x {child} to make {parent}".format(
parent=self.part.full_name, parent=self.part.full_name,
child=self.sub_part.full_name, child=self.sub_part.full_name,
n=helpers.decimal2string(self.quantity)) n=decimal2string(self.quantity))
def available_stock(self): def available_stock(self):
""" """
@ -2498,12 +2498,12 @@ class BomItem(models.Model):
return required return required
@property @property
def price_range(self): def price_range(self, internal=False):
""" Return the price-range for this BOM item. """ """ Return the price-range for this BOM item. """
# get internal price setting # get internal price setting
use_internal = common.models.InvenTreeSetting.get_setting('PART_BOM_USE_INTERNAL_PRICE', False) use_internal = common.models.InvenTreeSetting.get_setting('PART_BOM_USE_INTERNAL_PRICE', False)
prange = self.sub_part.get_price_range(self.quantity, intenal=use_internal) prange = self.sub_part.get_price_range(self.quantity, internal=use_internal and internal)
if prange is None: if prange is None:
return prange return prange
@ -2511,11 +2511,11 @@ class BomItem(models.Model):
pmin, pmax = prange pmin, pmax = prange
if pmin == pmax: if pmin == pmax:
return decimal2string(pmin) return decimal2money(pmin)
# Convert to better string representation # Convert to better string representation
pmin = decimal2string(pmin) pmin = decimal2money(pmin)
pmax = decimal2string(pmax) pmax = decimal2money(pmax)
return "{pmin} to {pmax}".format(pmin=pmin, pmax=pmax) return "{pmin} to {pmax}".format(pmin=pmin, pmax=pmax)

View File

@ -377,7 +377,7 @@ class PartStarSerializer(InvenTreeModelSerializer):
class BomItemSerializer(InvenTreeModelSerializer): class BomItemSerializer(InvenTreeModelSerializer):
""" Serializer for BomItem object """ """ Serializer for BomItem object """
# price_range = serializers.CharField(read_only=True) price_range = serializers.CharField(read_only=True)
quantity = serializers.FloatField() quantity = serializers.FloatField()
@ -492,7 +492,7 @@ class BomItemSerializer(InvenTreeModelSerializer):
'reference', 'reference',
'sub_part', 'sub_part',
'sub_part_detail', 'sub_part_detail',
# 'price_range', 'price_range',
'validated', 'validated',
] ]

View File

@ -259,26 +259,19 @@ function loadBomTable(table, options) {
sortable: true, sortable: true,
}); });
/*
// TODO - Re-introduce the pricing column at a later stage,
// once the pricing has been "fixed"
// O.W. 2020-11-24
cols.push( cols.push(
{ {
field: 'price_range', field: 'price_range',
title: '{% trans "Price" %}', title: '{% trans "Buy Price" %}',
sortable: true, sortable: true,
formatter: function(value, row, index, field) { formatter: function(value, row, index, field) {
if (value) { if (value) {
return value; return value;
} else { } else {
return "<span class='warning-msg'>{% trans "No pricing available" %}</span>"; return "<span class='warning-msg'>{% trans 'No pricing available' %}</span>";
} }
} }
}); });
*/
cols.push({ cols.push({
field: 'optional', field: 'optional',