Search code examples
pythonpython-2.7odoo-8odoo

Avoid changing state of class from computed fields - Odoo v8


I have these classes:

class bsi_production_order(models.Model):
    _name = 'bsi.production.order'

    name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
    order_lines = fields.One2many('bsi.production.order.lines', 'production_order', states={'finished': [('readonly', True)], 'cancel': [('readonly', True)]}, string="Order lines", copy=True)
print_orders = fields.One2many('bsi.print.order', 'production_orders', string="Print Orders")
state = fields.Selection([
        ('draft','Draft'),
        ('start','Started'),
        ('inprogress','In progress'),
        ('print_order_inprogress','Print Order In Progress'),
        ('finished','Finished'),
        ('cancel','Cancel'),
    ], string='State', index=True,  
    track_visibility='onchange', copy=False,
    help=" ")

class bsi_print_order(models.Model):
    _name = 'bsi.print.order'

    name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
    order_lines = fields.One2many('bsi.print.order.lines', 'print_order', string="Order lines")
    state = fields.Selection([
        ('inprogress','Draft'),
        ('awaitingraw','Awaiting raw materials'),
        ('work_in_progress','Work in Progress'),
        ('delivered','Delivered'),
        ('cancel','Cancel'),
    ], string="State")
    notes = fields.Text(string="Notes")

class bsi_production_order_lines(models.Model):
    _name = 'bsi.production.order.lines'

    production_order = fields.Many2one('bsi.production.order', string="Production Orders")
    isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
    qty = fields.Float(string="Quantity")
    consumed_qty = fields.Float(string="Consumed quantity")
    remaining_qty = fields.Float(string="Remaining quantity", compute="_remaining_func") #

    @api.onchange('qty', 'consumed_qty')
    def _remaining_func(self):
        if self.qty or self.consumed_qty:
            self.remaining_qty = self.qty +(-self.consumed_qty)

class bsi_print_order_lines(models.Model):
    _name = 'bsi.print.order.lines'

    print_order = fields.Many2one('bsi.print.order', string="Print Order")
    production_orders = fields.Many2one('bsi.production.order', ondelete='cascade', string="Production Order")
    isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
    qty = fields.Integer(string="Quantity")
    consumed_qty = fields.Integer(string="Quantity consumed")
    remaining_qty = fields.Float(string="Remaining quantity", compute="_remaining_func")
    is_book_block = fields.Boolean(string="Is Book Block Done")
    is_binding = fields.Boolean(string="Is Binding Done")
    is_edging = fields.Boolean(string="Is Edging Done")
    isbns = fields.Many2one('worksheets.isbns', string="Worksheet ISBNS")

@api.onchange('qty', 'consumed_qty')
def _remaining_func(self):
    if self.consumed_qty or self.qty:
        self.remaining_qty = self.qty +(-self.consumed_qty)

I create a bsi.print.order from a bsi.production.order class with this method:

@api.multi
def create_printy(self):
    copy_record = self.env['bsi.print.order'] 
    for record in self:
        order_lines = []
        for rec in record.order_lines:
            order_lines.append(
            (0,0,
            {
                'isbn': rec.isbn.id,
                'qty': rec.qty,
                }
            ))
        copy_record.create({
            'state' : 'inprogress', 
            'order_lines': order_lines, # here we pass the list of commands that we created earlier
        })
        record.update({'state': 'print_order_inprogress',},)

But no matter what state I choose, on record.update the state it's always changed on bsi.print.order.

I know it's because of the calculated fields, so, is there a way to pass them indirectly? Or a way to not affect the states of the child class? (bsi.print.order).

EDIT

These are the computed fields:

class bsi_production_order_lines(models.Model):
    _name = 'bsi.production.order.lines'

    production_order = fields.Many2one('bsi.production.order', string="Production Orders")
    isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
    qty = fields.Float(string="Quantity")
    consumed_qty = fields.Float(string="Consumed quantity")
    remaining_qty = fields.Float(string="Remaining quantity", compute="_remaining_func") #

@api.onchange('qty', 'consumed_qty')
def _remaining_func(self):
    for s in self:
        for qty in s.isbn:
            if s.qty or s.consumed_qty:
                s.remaining_qty = s.qty +(-s.consumed_qty)

On the other class these are the same fields.


Solution

  • Try to replace update with write in your ORM create method. I think it is the only problem, because I see computed fields nowhere, so why would the value field change after you modify it from the create method? I think that using that update in an ORM method is not working, so the state never changes its value.

    @api.multi
    def create_printy(self):
        copy_record = self.env['bsi.print.order'] 
        for record in self:
            order_lines = []
            for rec in record.order_lines:
                order_lines.append(
                (0,0,
                {
                    'isbn': rec.isbn.id,
                    'qty': rec.qty,
                    }
                ))
            copy_record.create({
                'state' : 'inprogress', 
                'order_lines': order_lines, # here we pass the list of commands that we created earlier
            })
            record.write({'state': 'print_order_inprogress',},)