mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Now processing currencies
This commit is contained in:
parent
68f5ec8b6a
commit
1940fd5199
@ -887,44 +887,54 @@ class BomList(generics.ListCreateAPIView):
|
||||
purchase_price_min=Min('sub_part__stock_items__purchase_price'),
|
||||
purchase_price_max=Max('sub_part__stock_items__purchase_price'),
|
||||
purchase_price_avg=Avg('sub_part__stock_items__purchase_price'),
|
||||
purchase_price_currency=F('sub_part__stock_items__purchase_price_currency'),
|
||||
)
|
||||
|
||||
# Convert prices to default currency (using backend conversion rates)
|
||||
for item in queryset:
|
||||
# Get values for currencies
|
||||
currencies = queryset.annotate(
|
||||
purchase_price_currency=F('sub_part__stock_items__purchase_price_currency'),
|
||||
).values('pk', 'sub_part', 'purchase_price_currency')
|
||||
|
||||
def convert_price(price, currency, decimal_places=4):
|
||||
""" Convert price field, returns Money field """
|
||||
|
||||
price_adjusted = None
|
||||
|
||||
# Get default currency from settings
|
||||
default_currency = InvenTreeSetting.get_setting('INVENTREE_DEFAULT_CURRENCY')
|
||||
|
||||
if default_currency:
|
||||
if item.purchase_price_min:
|
||||
# Convert minimum
|
||||
if price:
|
||||
if currency and default_currency:
|
||||
try:
|
||||
# Get adjusted price
|
||||
purchase_price_adjusted = convert_money(Money(item.purchase_price_min, item.purchase_price_currency), default_currency)
|
||||
# Update queryset
|
||||
item.purchase_price_min = purchase_price_adjusted
|
||||
price_adjusted = convert_money(Money(price, currency), default_currency)
|
||||
except MissingRate:
|
||||
pass
|
||||
# No conversion rate set
|
||||
price_adjusted = Money(price, currency)
|
||||
else:
|
||||
# Currency exists
|
||||
if currency:
|
||||
price_adjusted = Money(price, currency)
|
||||
# Default currency exists
|
||||
if default_currency:
|
||||
price_adjusted = Money(price, default_currency)
|
||||
|
||||
if item.purchase_price_max:
|
||||
# Convert maximum
|
||||
try:
|
||||
# Get adjusted price
|
||||
purchase_price_adjusted = convert_money(Money(item.purchase_price_max, item.purchase_price_currency), default_currency)
|
||||
# Update queryset
|
||||
item.purchase_price_max = purchase_price_adjusted
|
||||
except MissingRate:
|
||||
pass
|
||||
if price_adjusted and decimal_places:
|
||||
price_adjusted.decimal_places = decimal_places
|
||||
|
||||
if item.purchase_price_avg:
|
||||
# Convert average
|
||||
try:
|
||||
# Get adjusted price
|
||||
purchase_price_adjusted = convert_money(Money(item.purchase_price_avg, item.purchase_price_currency), default_currency)
|
||||
# Update queryset
|
||||
item.purchase_price_avg = purchase_price_adjusted
|
||||
except MissingRate:
|
||||
pass
|
||||
return price_adjusted
|
||||
|
||||
# Convert prices to default currency (using backend conversion rates)
|
||||
for bom_item in queryset:
|
||||
# Find associated currency (select first found)
|
||||
purchase_price_currency = None
|
||||
for currency_item in currencies:
|
||||
if currency_item['pk'] == bom_item.pk and currency_item['sub_part'] == bom_item.sub_part:
|
||||
purchase_price_currency = currency_item['purchase_price_currency']
|
||||
break
|
||||
# Convert prices
|
||||
bom_item.purchase_price_min = convert_price(bom_item.purchase_price_min, purchase_price_currency)
|
||||
bom_item.purchase_price_max = convert_price(bom_item.purchase_price_max, purchase_price_currency)
|
||||
bom_item.purchase_price_avg = convert_price(bom_item.purchase_price_avg, purchase_price_currency)
|
||||
|
||||
return queryset
|
||||
|
||||
|
@ -368,11 +368,13 @@ class BomItemSerializer(InvenTreeModelSerializer):
|
||||
|
||||
validated = serializers.BooleanField(read_only=True, source='is_line_valid')
|
||||
|
||||
purchase_price_min = MoneyField(max_digits=10, decimal_places=4, read_only=True)
|
||||
purchase_price_min = MoneyField(max_digits=10, decimal_places=6, read_only=True)
|
||||
|
||||
purchase_price_max = MoneyField(max_digits=10, decimal_places=4, read_only=True)
|
||||
purchase_price_max = MoneyField(max_digits=10, decimal_places=6, read_only=True)
|
||||
|
||||
purchase_price_avg = MoneyField(max_digits=10, decimal_places=4, read_only=True)
|
||||
purchase_price_avg = serializers.SerializerMethodField()
|
||||
|
||||
purchase_price_range = serializers.SerializerMethodField()
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
# part_detail and sub_part_detail serializers are only included if requested.
|
||||
@ -401,6 +403,38 @@ class BomItemSerializer(InvenTreeModelSerializer):
|
||||
queryset = queryset.prefetch_related('sub_part__supplier_parts__pricebreaks')
|
||||
return queryset
|
||||
|
||||
def get_purchase_price_range(self, obj):
|
||||
""" Return purchase price range """
|
||||
|
||||
if obj.purchase_price_min and not obj.purchase_price_max:
|
||||
# Get price range
|
||||
purchase_price_range = str(obj.purchase_price_max)
|
||||
elif not obj.purchase_price_min and obj.purchase_price_max:
|
||||
# Get price range
|
||||
purchase_price_range = str(obj.purchase_price_max)
|
||||
elif obj.purchase_price_min and obj.purchase_price_max:
|
||||
# Get price range
|
||||
if obj.purchase_price_min >= obj.purchase_price_max:
|
||||
# If min > max: use min only
|
||||
purchase_price_range = str(obj.purchase_price_min)
|
||||
else:
|
||||
purchase_price_range = str(obj.purchase_price_min) + " - " + str(obj.purchase_price_max)
|
||||
else:
|
||||
purchase_price_range = '-'
|
||||
|
||||
return purchase_price_range
|
||||
|
||||
def get_purchase_price_avg(self, obj):
|
||||
""" Return purchase price average """
|
||||
|
||||
if obj.purchase_price_avg:
|
||||
# Get string representation of price average
|
||||
purchase_price_avg = str(obj.purchase_price_avg)
|
||||
else:
|
||||
purchase_price_avg = '-'
|
||||
|
||||
return purchase_price_avg
|
||||
|
||||
class Meta:
|
||||
model = BomItem
|
||||
fields = [
|
||||
@ -420,6 +454,7 @@ class BomItemSerializer(InvenTreeModelSerializer):
|
||||
'purchase_price_min',
|
||||
'purchase_price_max',
|
||||
'purchase_price_avg',
|
||||
'purchase_price_range',
|
||||
]
|
||||
|
||||
|
||||
|
@ -249,19 +249,6 @@ function loadBomTable(table, options) {
|
||||
title: '{% trans "Purchase Price Range" %}',
|
||||
searchable: false,
|
||||
sortable: true,
|
||||
formatter: function(value, row, index, field) {
|
||||
var purchase_price_range = 0;
|
||||
|
||||
if (row.purchase_price_min > 0) {
|
||||
if (row.purchase_price_min >= row.purchase_price_max) {
|
||||
purchase_price_range = row.purchase_price_min;
|
||||
} else {
|
||||
purchase_price_range = row.purchase_price_min + " - " + row.purchase_price_max;
|
||||
}
|
||||
}
|
||||
|
||||
return purchase_price_range;
|
||||
},
|
||||
});
|
||||
|
||||
cols.push(
|
||||
|
Loading…
Reference in New Issue
Block a user