mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 05:05:42 +00:00 
			
		
		
		
	Now processing currencies
This commit is contained in:
		| @@ -887,44 +887,54 @@ class BomList(generics.ListCreateAPIView): | |||||||
|             purchase_price_min=Min('sub_part__stock_items__purchase_price'), |             purchase_price_min=Min('sub_part__stock_items__purchase_price'), | ||||||
|             purchase_price_max=Max('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_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) |         # Get values for currencies | ||||||
|         for item in queryset: |         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 |             # Get default currency from settings | ||||||
|             default_currency = InvenTreeSetting.get_setting('INVENTREE_DEFAULT_CURRENCY') |             default_currency = InvenTreeSetting.get_setting('INVENTREE_DEFAULT_CURRENCY') | ||||||
|              |              | ||||||
|             if default_currency: |             if price: | ||||||
|                 if item.purchase_price_min: |                 if currency and default_currency: | ||||||
|                     # Convert minimum |  | ||||||
|                     try: |                     try: | ||||||
|                         # Get adjusted price |                         # Get adjusted price | ||||||
|                         purchase_price_adjusted = convert_money(Money(item.purchase_price_min, item.purchase_price_currency), default_currency) |                         price_adjusted = convert_money(Money(price, currency), default_currency) | ||||||
|                         # Update queryset |  | ||||||
|                         item.purchase_price_min = purchase_price_adjusted |  | ||||||
|                     except MissingRate: |                     except MissingRate: | ||||||
|                         pass |                         # No conversion rate set | ||||||
|                  |                         price_adjusted = Money(price, currency) | ||||||
|                 if item.purchase_price_max: |                 else: | ||||||
|                     # Convert maximum |                     # Currency exists | ||||||
|                     try: |                     if currency: | ||||||
|                         # Get adjusted price |                         price_adjusted = Money(price, currency) | ||||||
|                         purchase_price_adjusted = convert_money(Money(item.purchase_price_max, item.purchase_price_currency), default_currency) |                     # Default currency exists | ||||||
|                         # Update queryset |                     if default_currency: | ||||||
|                         item.purchase_price_max = purchase_price_adjusted |                         price_adjusted = Money(price, default_currency) | ||||||
|                     except MissingRate: |  | ||||||
|                         pass |             if price_adjusted and decimal_places: | ||||||
|                  |                 price_adjusted.decimal_places = decimal_places | ||||||
|                 if item.purchase_price_avg: |  | ||||||
|                     # Convert average |             return price_adjusted | ||||||
|                     try: |  | ||||||
|                         # Get adjusted price |         # Convert prices to default currency (using backend conversion rates) | ||||||
|                         purchase_price_adjusted = convert_money(Money(item.purchase_price_avg, item.purchase_price_currency), default_currency) |         for bom_item in queryset: | ||||||
|                         # Update queryset |             # Find associated currency (select first found) | ||||||
|                         item.purchase_price_avg = purchase_price_adjusted |             purchase_price_currency = None | ||||||
|                     except MissingRate: |             for currency_item in currencies: | ||||||
|                         pass |                 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 |         return queryset | ||||||
|  |  | ||||||
|   | |||||||
| @@ -368,11 +368,13 @@ class BomItemSerializer(InvenTreeModelSerializer): | |||||||
|  |  | ||||||
|     validated = serializers.BooleanField(read_only=True, source='is_line_valid') |     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): |     def __init__(self, *args, **kwargs): | ||||||
|         # part_detail and sub_part_detail serializers are only included if requested. |         # 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') |         queryset = queryset.prefetch_related('sub_part__supplier_parts__pricebreaks') | ||||||
|         return queryset |         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: |     class Meta: | ||||||
|         model = BomItem |         model = BomItem | ||||||
|         fields = [ |         fields = [ | ||||||
| @@ -420,6 +454,7 @@ class BomItemSerializer(InvenTreeModelSerializer): | |||||||
|             'purchase_price_min', |             'purchase_price_min', | ||||||
|             'purchase_price_max', |             'purchase_price_max', | ||||||
|             'purchase_price_avg', |             'purchase_price_avg', | ||||||
|  |             'purchase_price_range', | ||||||
|         ] |         ] | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -249,19 +249,6 @@ function loadBomTable(table, options) { | |||||||
|         title: '{% trans "Purchase Price Range" %}', |         title: '{% trans "Purchase Price Range" %}', | ||||||
|         searchable: false, |         searchable: false, | ||||||
|         sortable: true, |         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( |     cols.push( | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user