简体   繁体   中英

Odoo, how get supplier Price value from supplierinfo, in custom module?

I'm a odoo 13 user (not developer). In a custom module which add products lines in Purchase Order, I need to add: product_id, name, price_unit , taxes_id. product_id, name, taxes_id values are available in product.product and I can get its value with this code:

'product_id': product.id,
'name': product.name,
'taxes_id': product.supplier_taxes_id,

but 'price_unit' value is in product.supplierinfo (it's supplier price) and if I use code:

'price_unit':  supplierinfo.price

I have error: NameError: name 'supplierinfo' is not defined

What's my mistake? How I can get Supplier Price value from product.supplierinfo and assign it to 'price_unit'???

I'm not developer and I don't know how create code, please any suggestion it's appreciated with code. Thanks Below complete code:

    def _add_product(self, product, qty, price):
        corresponding_line = self.order_line.filtered(lambda r: r.product_id.id == product.id)
        if corresponding_line:
            corresponding_line[0].product_qty += float(qty)
            corresponding_line[0].price_unit = float(price) or supplierinfo.price
        else:
            self.order_line += self.order_line.new({
                'product_id': product.id,
                'product_qty': qty,
                'date_planned': fields.Datetime.to_string(datetime.datetime.now() - dateutil.relativedelta.relativedelta(months=1)),
                'name': product.name,
                'product_uom': product.uom_id.id,
                'price_unit': float(price) or supplierinfo.price,       
                'taxes_id': product.supplier_taxes_id,
            })
        return True

Try with this code,

    def _add_product(self, product, qty, price):
        corresponding_line = self.order_line.filtered(lambda r: r.product_id.id == product.id)
        # My Cust Start
        supplierinfo = product.mapped('seller_ids')[:1]
        # My Cust End
        if corresponding_line:
            corresponding_line[0].product_qty += float(qty)
            corresponding_line[0].price_unit = float(price) or supplierinfo.price
        else:
            self.order_line += self.order_line.new({
                'product_id': product.id,
                'product_qty': qty,
                'date_planned': fields.Datetime.to_string(datetime.datetime.now() - dateutil.relativedelta.relativedelta(months=1)),
                'name': product.name,
                'product_uom': product.uom_id.id,
                'price_unit': float(price) or supplierinfo.price,       
                'taxes_id': product.supplier_taxes_id,
            })
        return True

Thanks

If I interpreted your post correctly you yould just write

'price_unit':  product.supplierinfo.price

as the price is an attribute of supplierinfo which is an attribute of product .

Most of these things are already in Odoo, but some custom modules don't use them. Usually Odoo logic for prices, taxes and product description calculation is only triggered in the UI (browser) by onchange events. For example the product onchange event for purchase or sales order lines.

You or developers can use them very easily. Just set up a new purchase/sales order line with all required fields and trigger the method behind that event by yourself.

The following code is not a final implementation of your requirement, because the onchange method could change more values, you maybe think it would. But just change those values right afterwards to your desired ones, like i've done with date_planned and name .

line = self.order_line.new({
    'product_id': product.id,
    'product_qty': qty,
    'product_uom': product.uom_id.id,
    'price_unit': 0.0,
    'order_id': self.id
})
line.onchange_product_id()
line.write({
    'date_planned': datetime.date.today() - dateutil.relativedelta.relativedelta(months=1),
    'name': product.name,
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM