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