如何使 pricelist_id.id return 成为特定的id

How to make pricelist_id.id return the specific id

您好,我正在尝试在销售订单的 odoo 中给 return 一个特定的价目表 ID:

@api.onchange('product_uom', 'product_uom_qty')
def product_uom_change(self):
    if not self.product_uom:
        self.price_unit = 0.0
        return
    if self.order_id.pricelist_id and self.order_id.partner_id:
        product = self.product_id.with_context(
            lang=self.order_id.partner_id.lang,
            partner=self.order_id.partner_id.id,
            quantity=self.product_uom_qty,
            date_order=self.order_id.date_order,
            pricelist=self.order_id.pricelist_id.id, # if eqp this pricelist_id."id" should point to the last list
            uom=self.product_uom.id,
            fiscal_position=self.env.context.get('fiscal_position')
        )
        self.price_unit = self.env['account.tax']._fix_tax_included_price(product.price, 
            product.taxes_id, self.tax_id)

这是在sale.order.line

中获取id的函数

这就是 product.pricelist

中的调用(我不确定)
def _get_item_ids(self, cr, uid, ctx):
    ProductPricelistItem = self.pool.get('product.pricelist.item')
    fields_list = ProductPricelistItem._defaults.keys()
    vals = ProductPricelistItem.default_get(cr, uid, fields_list, context=ctx)
    vals['compute_price'] = 'formula'
    return [[0, False, vals]]


def _price_rule_get_multi(self, cr, uid, pricelist, products_by_qty_by_partner, context=None):
    context = context or {}
    date = context.get('date') and context['date'][0:10] or time.strftime(DEFAULT_SERVER_DATE_FORMAT)
    products = map(lambda x: x[0], products_by_qty_by_partner)
    product_uom_obj = self.pool.get('product.uom')

    if not products:
        return {}

    categ_ids = {}
    for p in products:
        categ = p.categ_id
        while categ:
            categ_ids[categ.id] = True
            categ = categ.parent_id
    categ_ids = categ_ids.keys()

    is_product_template = products[0]._name == "product.template"
    if is_product_template:
        prod_tmpl_ids = [tmpl.id for tmpl in products]
        # all variants of all products
        prod_ids = [p.id for p in
                    list(chain.from_iterable([t.product_variant_ids for t in products]))]
    else:
        prod_ids = [product.id for product in products]
        prod_tmpl_ids = [product.product_tmpl_id.id for product in products]

    # Load all rules
    cr.execute(
        'SELECT i.id '
        'FROM product_pricelist_item AS i '
        'LEFT JOIN product_category AS c '
        'ON i.categ_id = c.id '
        'WHERE (product_tmpl_id IS NULL OR product_tmpl_id = any(%s))'
        'AND (product_id IS NULL OR product_id = any(%s))'
        'AND (categ_id IS NULL OR categ_id = any(%s)) '
        'AND (pricelist_id = %s) '
        'AND ((i.date_start IS NULL OR i.date_start<=%s) AND (i.date_end IS NULL OR i.date_end>=%s))'
        'ORDER BY applied_on, min_quantity desc, c.parent_left desc',
        (prod_tmpl_ids, prod_ids, categ_ids, pricelist.id, date, date))

    item_ids = [x[0] for x in cr.fetchall()]
    items = self.pool.get('product.pricelist.item').browse(cr, uid, item_ids, context=context)
    results = {}
    for product, qty, partner in products_by_qty_by_partner:
        results[product.id] = 0.0
        suitable_rule = False

        # Final unit price is computed according to `qty` in the `qty_uom_id` UoM.
        # An intermediary unit price may be computed according to a different UoM, in
        # which case the price_uom_id contains that UoM.
        # The final price will be converted to match `qty_uom_id`.
        qty_uom_id = context.get('uom') or product.uom_id.id
        price_uom_id = product.uom_id.id
        qty_in_product_uom = qty
        if qty_uom_id != product.uom_id.id:
            try:
                qty_in_product_uom = product_uom_obj._compute_qty(
                    cr, uid, context['uom'], qty, product.uom_id.id)
            except UserError:
                # Ignored - incompatible UoM in context, use default product UoM
                pass

        # if Public user try to access standard price from website sale, need to call _price_get.
        price = self.pool['product.template']._price_get(cr, uid, [product], 'list_price', context=context)[product.id]

        price_uom_id = qty_uom_id
        for rule in items:
            if rule.min_quantity and qty_in_product_uom < rule.min_quantity:
                continue
            if is_product_template:
                if rule.product_tmpl_id and product.id != rule.product_tmpl_id.id:
                    continue
                if rule.product_id and not (product.product_variant_count == 1 and product.product_variant_ids[0].id == rule.product_id.id):
                    # product rule acceptable on template if has only one variant
                    continue
            else:
                if rule.product_tmpl_id and product.product_tmpl_id.id != rule.product_tmpl_id.id:
                    continue
                if rule.product_id and product.id != rule.product_id.id:
                    continue

            if rule.categ_id:
                cat = product.categ_id
                while cat:
                    if cat.id == rule.categ_id.id:
                        break
                    cat = cat.parent_id
                if not cat:
                    continue

            if rule.base == 'pricelist' and rule.base_pricelist_id:
                price_tmp = self._price_get_multi(cr, uid, rule.base_pricelist_id, [(product, qty, partner)], context=context)[product.id]
                ptype_src = rule.base_pricelist_id.currency_id.id
                price = self.pool['res.currency'].compute(cr, uid, ptype_src, pricelist.currency_id.id, price_tmp, round=False, context=context)
            else:
                # if base option is public price take sale price else cost price of product
                # price_get returns the price in the context UoM, i.e. qty_uom_id
                price = self.pool['product.template']._price_get(cr, uid, [product], rule.base, context=context)[product.id]

            convert_to_price_uom = (lambda price: product_uom_obj._compute_price(
                                        cr, uid, product.uom_id.id,
                                        price, price_uom_id))

            if price is not False:
                if rule.compute_price == 'fixed':
                    price = convert_to_price_uom(rule.fixed_price)
                elif rule.compute_price == 'percentage':
                    price = (price - (price * (rule.percent_price / 100))) or 0.0
                else:
                    #complete formula
                    price_limit = price
                    price = (price - (price * (rule.price_discount / 100))) or 0.0
                    if rule.price_round:
                        price = tools.float_round(price, precision_rounding=rule.price_round)

                    if rule.price_surcharge:
                        price_surcharge = convert_to_price_uom(rule.price_surcharge)
                        price += price_surcharge

                    if rule.price_min_margin:
                        price_min_margin = convert_to_price_uom(rule.price_min_margin)
                        price = max(price, price_limit + price_min_margin)

                    if rule.price_max_margin:
                        price_max_margin = convert_to_price_uom(rule.price_max_margin)
                        price = min(price, price_limit + price_max_margin)
                suitable_rule = rule
            break
        # Final price conversion into pricelist currency
        if suitable_rule and suitable_rule.compute_price != 'fixed' and suitable_rule.base != 'pricelist':
            price = self.pool['res.currency'].compute(cr, uid, product.currency_id.id, pricelist.currency_id.id, price, round=False, context=context)

        results[product.id] = (price, suitable_rule and suitable_rule.id or False)
    return results

现在我该怎么做呢 return 是一个特定的 ID,用于表示列表中的最后一个 ID。

这里逻辑有问题。 self.order_id.pricelist_id 指向 单一 价目表,没有第一个或最后一个选项。

如果您想获取价目表列表并获取最后一个,您可以执行以下操作:

domain = []  # returns all records in the table
domain = [('name', '=', 'A certain pricelist')]  # returns only the records matching this domain
ProductPricelist = self.env['product.pricelist']
pricelist_id = ProductPricelist.search(domain, limit=1, order='id desc').id

然后在product = {..中使用它。

limit=1 限制为单个结果,而 order='id desc' 确保最后一条可用记录 id