Skip to Content
Menu
This question has been flagged
2 Replies
6719 Views

Problem is solved, see end of posting.

Hi there,

i try to build a total price in product.product, depending on sub products defined in model bundle.product. Adding or changing a sub product i want to calculate and refresh the total price. I didn't know how to trigger the event to recalculate the total price. There is a one2Many relation between product.product and bundle.product

Thank you in advance,

Kind regards, Franz

This is my code:

from odoo import models, fields, api

class productProduct(models.Model):
_inherit = 'product.product'
bundle_product = fields.Boolean(string="Is Bundle Product ?")
bundle_product_ids = fields.One2many('bundle.product', 'prod_id', string="Bundle Products")

@api.depends('bundle_product_ids.tm_sum')
def _change_lst_price(self):
lst_price = 0.0
for line in self.bundle_product_ids:
lst_price += line.tm_sum

class bundleProduct(models.Model):
_name = "bundle.product"
name = fields.Many2one('product.product', string="Name")
prod_id = fields.Many2one('product.product', string="Product Id")
quantity = fields.Integer(string="Quantity", default="0")
unit_id = fields.Many2one('product.uom', 'Unit of Measure ')
tm_price = fields.Float(string="Preis")
tm_sum = fields.Float(string="Summe")

@api.model
def create(self, vals):
if vals.get('name'):
prod_obj = self.env['product.product'].browse(vals.get('name'))
vals.update({'unit_id': prod_obj.uom_id.id})
return super(bundleProduct, self).create(vals)

@api.onchange('name')
def _onchange_name(self):
product = self.name
if not self.name:
self.tm_price = 0.0
return
else:
self.tm_price = product.list_price
self.unit_id = product.uom_id
print('Test: Preis wurde gesetzt!')
return

@api.onchange('quantity', 'tm_price')
def _onchange_quantity(self):
self.tm_sum = self.tm_price * self.quantity
print('Test: Menge/ Preis wurde geändert!')
return

class bundleProductTask(models.Model):
_inherit = "project.task"
bundle_product_id = fields.Many2one('bundle.product')

@api.onchange('sale_line_id')
def onchange_sale_line_id(self):
bundle_product_ids = self.env['bundle.product'].search([('prod_id', '=', self.sale_line_id.product_id.id)])
return {'domain': {'bundle_product_id': [('id', 'in', bundle_product_ids.ids)]}}

and this my view for sub product

<record model="ir.ui.view" id="product_product_inherit_form">
<field name="name">product_product_inherit</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_normal_form_view" />
<field name="arch" type="xml">

<xpath expr="//field[@name='sale_ok']" position="before">
<field name="bundle_product" />
<label for="bundle_product" />
<br></br>
</xpath>

<xpath expr="//field[@name='lst_price']" position="replace">
<field name="lst_price" readonly="True" widget="monetary" />
</xpath>

<xpath expr="//form/sheet/notebook" position="inside">
<page string="Bundle Product" attrs="{'invisible': [('bundle_product', '=', False)]}">
<field name="bundle_product_ids" nolabel="1" colspan="1">
<tree string="Bundle" editable="bottom">
<field name="name" required="True"/>
<field name="quantity" required="True"/>
<field name="unit_id" readonly="True" invisible="0"/>
<field name="tm_price" widget="monetary"/>
<field name="tm_sum" widget="monetary"/>
</tree>
</field>
<group>
<field name="lst_price" colspan="1"/>
</group>
</page>
</xpath>
</field>
</record>

#This function did the job for me:
@api.onchange('bundle_product_ids')
def _calculate_total(self):
bundle = self.bundle_product_ids
self.lst_price = 0.0

for each in bundle:
self.lst_price += each.tm_sum

instead of:
    @api.depends('bundle_product_ids.tm_sum')
def _change_lst_price(self):
lst_price = 0.0
for line in self.bundle_product_ids:
lst_price += line.tm_sum


Avatar
Discard
Best Answer

Hi, I hope this helps:
To use the second, your field needs to be a compute field. And the compute function to be like this : 

@api.depends('bundle_product_ids.tm_sum')
def _compute_lst_price(self):
price = 0.0
        for record in self:
    for line in record.bundle_product_ids:
    price += line.tm_sum
            record.lst_price = price
Avatar
Discard
Best Answer

Hi Franz.  Is your module available to download anywhere, or is it simply the .py and view packaged as a module.

Avatar
Discard
Author

Hi Bill,

we use app https://www.odoo.com/apps/modules/11.0/gt_bundle_product. We have adapted it to our needs.

Regards, Franz

I see, thanks for replying. Does the modified app generate accounting entries by using the data for each of the individual products (like taxes), or does it just pull data from the bundled product. My issue is that some of the products within bundles we wish to sell have different taxes applied to them so we need the accounting entries to respect those differences.