コード例 #1
0
ファイル: api.py プロジェクト: sfabris/InvenTree
        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 price:
                if currency and default_currency:
                    try:
                        # Get adjusted price
                        price_adjusted = convert_money(Money(price, currency),
                                                       default_currency)
                    except MissingRate:
                        # 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 price_adjusted and decimal_places:
                price_adjusted.decimal_places = decimal_places

            return price_adjusted
コード例 #2
0
    def get_total_price(self, target_currency=currency_code_default()):
        """
        Calculates the total price of all order lines, and converts to the specified target currency.

        If not specified, the default system currency is used.

        If currency conversion fails (e.g. there are no valid conversion rates),
        then we simply return zero, rather than attempting some other calculation.
        """

        total = Money(0, target_currency)

        # gather name reference
        price_ref_tag = 'sale_price' if isinstance(
            self, SalesOrder) else 'purchase_price'

        # order items
        for line in self.lines.all():

            price_ref = getattr(line, price_ref_tag)

            if not price_ref:
                continue

            try:
                total += line.quantity * convert_money(price_ref,
                                                       target_currency)
            except MissingRate:
                # Record the error, try to press on
                kind, info, data = sys.exc_info()

                Error.objects.create(
                    kind=kind.__name__,
                    info=info,
                    data='\n'.join(traceback.format_exception(
                        kind, info, data)),
                    path='order.get_total_price',
                )

                logger.error(f"Missing exchange rate for '{target_currency}'")

                # Return None to indicate the calculated price is invalid
                return None

        # extra items
        for line in self.extra_lines.all():

            if not line.price:
                continue

            try:
                total += line.quantity * convert_money(line.price,
                                                       target_currency)
            except MissingRate:
                # Record the error, try to press on
                kind, info, data = sys.exc_info()

                Error.objects.create(
                    kind=kind.__name__,
                    info=info,
                    data='\n'.join(traceback.format_exception(
                        kind, info, data)),
                    path='order.get_total_price',
                )

                logger.error(f"Missing exchange rate for '{target_currency}'")

                # Return None to indicate the calculated price is invalid
                return None

        # set decimal-places
        total.decimal_places = 4

        return total