Ejemplo n.º 1
0
    def get_unit_price(self, order_id, message):
        """Get unit price of this resource

        As the resource has subscribed to the product, so we should
        calculate price from the subscriptions instead of the product.
        """
        c = self.get_collection(message)
        product = self.gclient.get_product(
            c.product_name, c.service, c.region_id)

        if not product:
            return 0

        subs = self.gclient.get_subscriptions(order_id,
                                              product_id=product['product_id'])
        if subs:
            sub = subs[0]
        else:
            LOG.warn("The order %s has no subscriptions" % order_id)
            return 0

        if 'extra' in sub:
            price_data = pricing.get_price_data(sub['unit_price'])
        else:
            price_data = None

        return pricing.calculate_price(
            c.resource_volume, price_data)
Ejemplo n.º 2
0
    def get_unit_price(self, order_id, message):
        """Get unit price of this resource

        As the resource has subscribed to the product, so we should
        calculate price from the subscriptions instead of the product.
        """
        c = self.get_collection(message)
        product = self.gclient.get_product(c.product_name, c.service,
                                           c.region_id)

        if not product:
            return 0

        subs = self.gclient.get_subscriptions(order_id,
                                              product_id=product['product_id'])
        if subs:
            sub = subs[0]
        else:
            LOG.warn("The order %s has no subscriptions" % order_id)
            return 0

        if 'extra' in sub:
            price_data = pricing.get_price_data(sub['unit_price'])
        else:
            price_data = None

        return pricing.calculate_price(c.resource_volume, price_data)
Ejemplo n.º 3
0
    def get_all(self, purchase):
        """Get price of a group of products."""

        if purchase.bill_method not in ['hour', 'month', 'year']:
            err = 'Should specify bill_method among hour, month and year'
            raise exception.InvalidParameterValue(err=err)

        if not isinstance(purchase.bill_period, int):
            purchase.bill_period = 1

        conn = pecan.request.db_conn

        unit_price = quantize_decimal(0)
        unit = purchase.bill_method

        for p in purchase.purchases:
            if all([p.product_id, p.quantity]):
                try:
                    product = conn.get_product(request.context, p.product_id)
                except exception.ProductIdNotFound:
                    LOG.warn("Product %s not found" % p.product_id)
                    raise
            elif all([p.product_name, p.service, p.region_id, p.quantity]):
                filters = dict(name=p.product_name,
                               service=p.service,
                               region_id=p.region_id)
                products = list(conn.get_products(
                    request.context, filters=filters))
                if len(products) == 0:
                    LOG.error('Product %s of region %s not found',
                              p.product_name, p.region_id)
                    raise exception.ProductNameNotFound(
                        product_name=p.product_name)
                product = products[0]
            else:
                err = "Every purchase item should specify product_name, "\
                      "service, region_id and quantity or "\
                      "product_id and quantity."
                raise exception.MissingRequiredParams(reason=err)
            try:
                if product.unit_price:
                    unit_price_data = jsonutils.loads(product.unit_price)
                    price_data = pricing.get_price_data(unit_price_data,
                                                        unit)
                else:
                    price_data = None

                unit_price += pricing.calculate_price(
                    p.quantity, price_data)
            except (Exception) as e:
                LOG.error('Calculate price of product %s failed, %s',
                          p.product_name, e)
                raise e
        total_price = unit_price * purchase.bill_period
        return models.Price.transform(unit_price=unit_price,
                                      unit=unit,
                                      total_price=total_price)
Ejemplo n.º 4
0
    def get_all(self, purchase):
        """Get price of a group of products."""

        if purchase.bill_method not in ['hour', 'month', 'year']:
            err = 'Should specify bill_method among hour, month and year'
            raise exception.InvalidParameterValue(err=err)

        if not isinstance(purchase.bill_period, int):
            purchase.bill_period = 1

        conn = pecan.request.db_conn

        unit_price = quantize_decimal(0)
        unit = purchase.bill_method

        for p in purchase.purchases:
            if all([p.product_id, p.quantity]):
                try:
                    product = conn.get_product(request.context, p.product_id)
                except exception.ProductIdNotFound:
                    LOG.warn("Product %s not found" % p.product_id)
                    raise
            elif all([p.product_name, p.service, p.region_id, p.quantity]):
                filters = dict(name=p.product_name,
                               service=p.service,
                               region_id=p.region_id)
                products = list(
                    conn.get_products(request.context, filters=filters))
                if len(products) == 0:
                    LOG.error('Product %s of region %s not found',
                              p.product_name, p.region_id)
                    raise exception.ProductNameNotFound(
                        product_name=p.product_name)
                product = products[0]
            else:
                err = "Every purchase item should specify product_name, "\
                      "service, region_id and quantity or "\
                      "product_id and quantity."
                raise exception.MissingRequiredParams(reason=err)
            try:
                if product.unit_price:
                    unit_price_data = jsonutils.loads(product.unit_price)
                    price_data = pricing.get_price_data(unit_price_data, unit)
                else:
                    price_data = None

                unit_price += pricing.calculate_price(p.quantity, price_data)
            except (Exception) as e:
                LOG.error('Calculate price of product %s failed, %s',
                          p.product_name, e)
                raise e
        total_price = unit_price * purchase.bill_period
        return models.Price.transform(unit_price=unit_price,
                                      unit=unit,
                                      total_price=total_price)
Ejemplo n.º 5
0
    def get_unit_price(self, env, body, method):
        """Get product unit price"""
        collection = self.get_collection(env, body)
        product = self.gclient.get_product(collection.product_name,
                                           collection.service,
                                           collection.region_id)
        if product:
            if 'extra' in product:
                price_data = pricing.get_price_data(product['extra'], method)
            else:
                price_data = None

            return pricing.calculate_price(
                collection.resource_volume, product['unit_price'], price_data)
        else:
            return 0
Ejemplo n.º 6
0
    def get_unit_price(self, env, body, method):
        """Get product unit price"""
        collection = self.get_collection(env, body)
        product = self.gclient.get_product(collection.product_name,
                                           collection.service,
                                           collection.region_id)
        if product:
            if 'unit_price' in product:
                price_data = pricing.get_price_data(product['unit_price'],
                                                    method)
            else:
                price_data = None

            return pricing.calculate_price(collection.resource_volume,
                                           price_data)
        else:
            return 0