0
votes

I am building a module (Odoo 8) , my target is create offers in sale order, this offer can set a fix price for a determinate product or set a gift to zero cost.

I am adding my custom model offer_line, in new tab inside sale order.

Is defined like this:

class OfferSaleOrderLine(models.Model):

_name = 'offer.sale.order.line'

sale_order_ref = fields.Many2one('sale.order',ondelete='set null', string="Sale Order", index=True)

offer_ref = fields.Many2one('offer',ondelete='set null', string="Oferta", index=True)

is_active = fields.Boolean(default=True,string='Activo')

accumulations = fields.Float(digits=(6, 2), help="Acumulaciones")

 class SaleOrder(models.Model):

_inherit = 'sale.order'

offers_lines = fields.One2many('offer.sale.order.line','sale_order_ref', string="Lineas de Ofertas")

I have a new api onchange method inside sale order:

 @api.onchange('offers_lines')

def _onchange_offers_lines(self):

I check is offer need apply, and i add to offers_line new lines from this onchange function, like this:

self.offers_lines += self.env['offer.sale.order.line'].new({'is_active': True, 'offer_ref': offer, 'accumulations' : is_offer})

This is working perfect, lines is created, added to tab in form and onchange methods is trigger.

But the problem is next, if i try the same with sale order line, no working:

     val = {

'name': gift_line.free_product.name,

'order_id': self.id,

'product_id': gift_line.free_product.id,

'product_uom_qty': gift_line.qty,

'product_uom': self.order_line[0].product_uom.id,

'price_unit': 0.0,

'state': 'draft',

}

self.order_line += self.env['sale.order.line'].new(val)

In log, this lines is created, i can see the newid id is created when i foreach self.order_line

****ORDER LINES : ID : ; Product: product.product(5152,) ; Qty: 6.0 ; Price: 0.0 ;****

but the item is no created in sale order line tab, i dont know why, my custom lines(One2many) is created, but, the sale_order_lines, with same code and one2many field too, is not created. I have the same problem if i try set the price_unit to this sale_order_lines. Log says changes is added, but is not updated in form. In next onchange trigger, the changes is dissapear.

Thanks to all!

3

3 Answers

1
votes
@api.onchange('Put Your Onchange Field Here')
def _onchange_offers_lines(self):

    vals = {
         'name': gift_line.free_product.name,
         'order_id': self.id,
         'product_id': gift_line.free_product.id,
         'product_uom_qty': gift_line.qty,
         'product_uom': self.order_line[0].product_uom.id,
         'price_unit': 0.0,
         'state': 'draft'
         }
    self.order_line = [(0, 0, vals)]

Hope it will help you.

0
votes

Odoo doesn't natively support onchange on *2many fields, anymore.

You can see that in openerp.models here https://github.com/odoo/odoo/blob/9.0/openerp/models.py#L6050

And furthermore a discussion on that topic here: https://github.com/odoo/odoo/issues/2693

0
votes

I'm not sure to have properly understand your problem, but I see two things about it.

First you need to check that the field you want to set onchange is not already set in the base module that you are extending. If so, you had to disable the old-style onchange in the view by setting the attribute to 1 in the field (keep in mind that by disabling the api-v7 onchange on the field will not call the old onchange function you will probably want to call it in your new onchange function).

The second problem is that you can't add an item to a one2many field, you probably can to a many2one instead. You also can't use var += value to add an item to a relation field, you must use the special tupple (as described here).