2

ドロップダウンリストでの選択に基づいて特定の列が表示または非表示になる(ビューの編集?)請求書ラインフォームと同様の効果を達成しようとしています。

この効果を実現するaccount_invoice_layoutのコードを調べようとしていますが、理解するのが少し難しいです。

完全なコードは次のとおりです。

class account_invoice_line(osv.osv):

def move_line_get_item(self, cr, uid, line, context=None):
    if line.state != 'article':
        return None
    return super(account_invoice_line, self).move_line_get_item(cr, uid, line, context)

def fields_get(self, cr, uid, fields=None, context=None):
    article = {
        'article': [('readonly', False), ('invisible', False)],
        'text': [('readonly', True), ('invisible', True), ('required', False)],
        'subtotal': [('readonly', True), ('invisible', True), ('required', False)],
        'title': [('readonly', True), ('invisible', True), ('required', False)],
        'break': [('readonly', True), ('invisible', True), ('required', False)],
        'line': [('readonly', True), ('invisible', True), ('required', False)],
    }
    states = {
        'name': {
            'break': [('readonly', True),('required', False),('invisible', True)],
            'line': [('readonly', True),('required', False),('invisible', True)],
            },
        'product_id': article,
        'account_id': article,
        'quantity': article,
        'uos_id': article,
        'price_unit': article,
        'discount': article,
        'invoice_line_tax_id': article,
        'account_analytic_id': article,
    }
    res = super(account_invoice_line, self).fields_get(cr, uid, fields, context)
    for field in res:
        if states.has_key(field):
            for key,value in states[field].items():
                res[field].setdefault('states',{})
                res[field]['states'][key] = value
    return res

def onchange_invoice_line_view(self, cr, uid, id, type, context=None, *args):

    if (not type):
        return {}
    if type != 'article':
        temp = {'value': {
                'product_id': False,
                'uos_id': False,
                'account_id': False,
                'price_unit': False,
                'price_subtotal': False,
                'quantity': 0,
                'discount': False,
                'invoice_line_tax_id': False,
                'account_analytic_id': False,
                },
            }
        if type == 'line':
            temp['value']['name'] = ' '
        if type == 'break':
            temp['value']['name'] = ' '
        if type == 'subtotal':
            temp['value']['name'] = 'Sub Total'
        return temp
    return {}

def create(self, cr, user, vals, context=None):
    if vals.has_key('state'):
        if vals['state'] == 'line':
            vals['name'] = ' '
        if vals['state'] == 'break':
            vals['name'] = ' '
        if vals['state'] != 'article':
            vals['quantity']= 0
            vals['account_id']= self._default_account(cr, user, None)
    return super(account_invoice_line, self).create(cr, user, vals, context)

def write(self, cr, user, ids, vals, context=None):
    if vals.has_key('state'):
        if vals['state'] != 'article':
            vals['product_id']= False
            vals['uos_id']= False
            vals['account_id']= self._default_account(cr, user, None)
            vals['price_unit']= False
            vals['price_subtotal']= False
            vals['quantity']= 0
            vals['discount']= False
            vals['invoice_line_tax_id']= False
            vals['account_analytic_id']= False
        if vals['state'] == 'line':
            vals['name'] = ' '
        if vals['state'] == 'break':
            vals['name'] = ' '
    return super(account_invoice_line, self).write(cr, user, ids, vals, context)

def copy_data(self, cr, uid, id, default=None, context=None):
    if default is None:
        default = {}
    default['state'] = self.browse(cr, uid, id, context=context).state
    return super(account_invoice_line, self).copy_data(cr, uid, id, default, context)

def _fnct(self, cr, uid, ids, name, args, context=None):
    res = {}
    lines = self.browse(cr, uid, ids, context=context)
    account_ids = [line.account_id.id for line in lines]
    account_names = dict(self.pool.get('account.account').name_get(cr, uid, account_ids, context=context))
    for line in lines:
        if line.state != 'article':
            if line.state == 'line':
                res[line.id] = '-----------------------------------------'
            elif line.state == 'break':
                res[line.id] = 'PAGE BREAK'
            else:
                res[line.id] = ' '
        else:
            res[line.id] = account_names.get(line.account_id.id, '')
    return res

_name = "account.invoice.line"
_order = "invoice_id, sequence asc"
_description = "Invoice Line"
_inherit = "account.invoice.line"
_columns = {
    'state': fields.selection([
            ('article','Product'),
            ('title','Title'),
            ('text','Note'),
            ('subtotal','Sub Total'),
            ('line','Separator Line'),
            ('break','Page Break'),]
        ,'Type', select=True, required=True),
    'sequence': fields.integer('Sequence Number', select=True, help="Gives the sequence order when displaying a list of invoice lines."),
    'functional_field': fields.function(_fnct, arg=None, fnct_inv=None, fnct_inv_arg=None, type='char', fnct_search=None, obj=None, store=False, string="Source Account"),
}

def _default_account(self, cr, uid, context=None):
    cr.execute("select id from account_account where parent_id IS NULL LIMIT 1")
    res = cr.fetchone()
    return res[0]

_defaults = {
    'state': 'article',
    'sequence': 0,
}

account_invoice_line()

fields_getメソッドは正確に何をしますか?いつ実行されますか?上記のコードはどのようにビューを編集しますか?

ありがとうございました。

4

1 に答える 1

4

実際、fields_get()はモジュールのフィールド定義を返します。フィールド定義で、states引数を指定できます。

デフォルトでは、フィールド定義では次のように機能します。

states = {'draft':[('readonly','=',True)]}

状態フォームが「ドラフト」の場合、フィールドは読み取り専用になります。

変数statesarticlesは同じ値を格納します。

ここで示したモジュールでは、状態フィールドの値は通常の値とは異なります。

[('article','Product'),
            ('title','Title'),
            ('text','Note'),
            ('subtotal','Sub Total'),
            ('line','Separator Line'),
            ('break','Page Break'),]

fields_get()関数の最後の部分には、次のものがあります。

res = super(account_invoice_line, self).fields_get(cr, uid, fields, context)
for field in res:
    if states.has_key(field):
        for key,value in states[field].items():
            res[field].setdefault('states',{})
            res[field]['states'][key] = value
return res

スーパークラスのメソッドを使用し、次のように機能します。

fields_get()は、fields引数を使用して、モジュールに存在するフィールドを返します。モジュールは、各フィールドの状態引数を強制するだけです。

最後に、架空の状態フィールドに従ってフィールドを表示できます。あなたの例では:

  • 行が「記事」状態の場合、デフォルトで表示されますが、
  • 行が「タイトル」状態の場合、その行は非表示で読み取り専用で表示されます。

これらの状態の値は、行がタイトル、記事、またはその他であるかどうかを示すためにここにあると思います。

したがって、独自のモジュールで同じことを行う場合は、このfields_get()関数定義をコピーして貼り付け、記事と状態変数をカスタマイズし、必要に応じて、_columnsクラス属性の状態フィールド定義を適応させることを忘れないでください。。

私があなたを助けたことを願っています、

今は

于 2013-01-10T13:13:31.167 に答える