2017-11-18 124 views
1

我有此嚮導代碼:負載領域,如果布爾值是在嚮導檢查 - 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這三個狀態。

我是否應該像order.linesTransientModel那樣聲明這些字段?

像這樣:

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) 

但我不知道如何去實現它

任何想法?

回答

1

在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. 

爲bouth情況下,你可以做一些事情這樣的:

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)]}" 
      /> 

在oreder:

# 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 
+0

你是我的英雄,哈哈,我當然學到了很多與你:)非常感謝你 – NeoVe

+0

很高興知道你問一個好問題^^。 – Cherif