如果在向导中检查了布尔值,则从行加载字段 - Odoo v8

Load fields from line if booleans are checked on wizard - Odoo v8

我有这个向导代码:

class generate_print_order(models.TransientModel):
    _name = 'generate.print.order'

    isbns = fields.One2many('order.lines', 'order_id', 'ISBN')
    production_order = fields.Many2one('bsi.production.order', 'Production Order')

@api.model
def default_get(self, fields):
    res = super(generate_print_order, self).default_get(fields)
    isbns = []
    if self.env.context.has_key('active_id'):
        production_order = self.env.context['active_id']
        order = self.env['bsi.production.order'].browse(production_order)
        for line in order.order_lines:
            if line.remaining_qty > 0:
                val = {
                    'name': 'name',
                    'isbn':line.isbn.id,
                    'qty': line.remaining_qty}
                isbns.append([0,0,val])
        res.update({'isbns':isbns,'production_order':production_order})
    return res

@api.multi    
def generate(self):
    if self.isbns:
        order_lines = []
        print_order = self.env['bsi.print.order'].create({
            'state': 'draft',
            'production_orders':self.production_order.id,
            })
        for line in self.isbns:
            order_lines.append(self.env['bsi.print.order.lines'].create({
                'print_order':print_order.id,
                'isbn':line.isbn.id,
                'qty':line.qty}))
            prod_isbn = self.env['bsi.production.order.lines'].search([('production_order','=',self.production_order.id),
                                                           ('isbn','=',line.isbn.id)])
            prod_isbn.consumed_qty = line.qty
        print_order.write({'order_lines':[(6,0,map(lambda x:x.id,order_lines))]})
        tree_view_id = self.env.ref('bsi.bsi_print_orders_view_tree').id
        form_view_id = self.env.ref('bsi.view_print_order_form').id
        self.production_order.state = 'print_order_inprogress'
        return {
            'name': _('Print Order'),
            'type': 'ir.actions.act_window',
            'res_model': 'bsi.print.order',
            'view_mode': 'tree,form',
            'view_type': 'form',
            'views': [(tree_view_id, 'tree'),(form_view_id, 'form')],
            'view_id':tree_view_id,
            'res_id': [print_order.id],
            'domain': [('id', 'in', [print_order.id])]
             }

class order_lines(models.TransientModel):
    _name = 'order.lines'

    order_id = fields.Many2one('generate.print.order', 'Order')
    isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
    qty = fields.Float(string="Quantity")

这是bsi.print.order.line型号:

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

    print_order = fields.Many2one('bsi.print.order', string="Print 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")

我感到困惑的是,只有当 is_book_block, is_binding and is_edging 三个人都处于 True 状态时,我如何才能启动此向导。

我是否应该像在 TransientModel 上使用 order.lines 那样声明这些字段?

像这样:

class order_lines(models.TransientModel):
    _name = 'order.lines'

    order_id = fields.Many2one('generate.print.order', 'Order')
    isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
    qty = fields.Float(string="Quantity")
    is_binding = fields.Boolean(string="Is Binding Done", default=True)
    is_edging = fields.Boolean(string="Is Edging Done", default=True)
    is_book_block = fields.Boolean(string="Is Book Block Done", default=True)

但我不确定如何实现它

有什么想法吗?

在 odoo 中做到这一点的唯一方法是使用按钮打开向导,但是 我在这里有点困惑,您条件的字段在订单行中 不是顺序所以当你说你想打开这个向导如果这个字段都是真的 你到底是什么意思:

  1 - if there is only one line that all the three field are true, you can open the wizard
  2 - all the line in the order must have this three field then you can open the wizard.

对于这两种情况,您都可以这样做:

 1 - create a boolean field in the order witch is compute field 
 2 - compute the value of the field based on condition 1 or 2
 3 - add a button the the form of the order and show it only if the field is true.
 4- don't forget to add this field too to the form so you can use it in attrs   
 5- in order define a method to open the wizard and pass the same context to wizard (active_id)  

订单形式:

<field name="new_field" invisible="1"/>
<button name="open_wizard" type="object" 
           class="oe_highlight"
           attrs="{'readonly':[('new_field','=',True)]}"
           />

在订单中:

# note change the name of the method so you can understand you code next time ^^
@api.multi
def open_wizard(self):
    """ open wizard for ....  """
    return {
        # pass the same context to access active_id 
        'context': self.env.context,        
        'name': 'Your title Here',
        'view_type': 'form',
        'view_mode': 'form',
        'res_model': 'generate.print.order',
        'type': 'ir.actions.act_window',
    }

对于字段:

new_field = fields.Boolean('check ...' , compute='check_fields', store=True)

depends('line_ids', 
    'line_ids.is_binding',
    'line_ids.is_edging',
    'line_ids.is_book_block',)
def check_fields(self):
    """ check fields"""
    for rec in self:
        if any( line.is_binding and line.is_edging and line.is_book_block               for line in rec.order_lines):
            rec.new_field = True

        else:
            rec.new_field = False