Merge pull request #2539 from SchrodingersGat/exchange-rate-bug-fix

Add try/except blocks for calls to conert_money
This commit is contained in:
Oliver 2022-01-12 12:23:40 +11:00 committed by GitHub
commit 506f5696ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View File

@ -29,6 +29,8 @@ from markdownx.models import MarkdownxField
from django_cleanup import cleanup from django_cleanup import cleanup
from djmoney.contrib.exchange.exceptions import MissingRate
from mptt.models import TreeForeignKey, MPTTModel from mptt.models import TreeForeignKey, MPTTModel
from mptt.exceptions import InvalidMove from mptt.exceptions import InvalidMove
from mptt.managers import TreeManager from mptt.managers import TreeManager
@ -1832,9 +1834,14 @@ class Part(MPTTModel):
def get_purchase_price(self, quantity): def get_purchase_price(self, quantity):
currency = currency_code_default() currency = currency_code_default()
try:
prices = [convert_money(item.purchase_price, currency).amount for item in self.stock_items.all() if item.purchase_price] prices = [convert_money(item.purchase_price, currency).amount for item in self.stock_items.all() if item.purchase_price]
except MissingRate:
prices = None
if prices: if prices:
return min(prices) * quantity, max(prices) * quantity return min(prices) * quantity, max(prices) * quantity
return None return None
@transaction.atomic @transaction.atomic

View File

@ -20,6 +20,7 @@ from django.contrib import messages
from moneyed import CURRENCIES from moneyed import CURRENCIES
from djmoney.contrib.exchange.models import convert_money from djmoney.contrib.exchange.models import convert_money
from djmoney.contrib.exchange.exceptions import MissingRate
from PIL import Image from PIL import Image
@ -425,7 +426,11 @@ class PartDetail(InvenTreeRoleMixin, DetailView):
continue continue
# convert purchase price to current currency - only one currency in the graph # convert purchase price to current currency - only one currency in the graph
try:
price = convert_money(stock_item.purchase_price, default_currency) price = convert_money(stock_item.purchase_price, default_currency)
except MissingRate:
continue
line = { line = {
'price': price.amount, 'price': price.amount,
'qty': stock_item.quantity 'qty': stock_item.quantity
@ -487,7 +492,11 @@ class PartDetail(InvenTreeRoleMixin, DetailView):
if None in [sale_item.purchase_price, sale_item.quantity]: if None in [sale_item.purchase_price, sale_item.quantity]:
continue continue
try:
price = convert_money(sale_item.purchase_price, default_currency) price = convert_money(sale_item.purchase_price, default_currency)
except MissingRate:
continue
line = { line = {
'price': price.amount if price else 0, 'price': price.amount if price else 0,
'qty': sale_item.quantity, 'qty': sale_item.quantity,