Search code examples
pythonxmlpython-2.7odoo-8odoo

Disable button once clicked - Odoo v8


I have this method:

@api.multi
def account_move_sc5_10(self):
    #if record.state in ('awaitingraw'):
    for record in self:
        #if record.state in ('draft'):
        tempy = record.contract_worksheet.total_alles - record.contract_worksheet.total_totals
        acc_move = self.env['account.move'] 
        move_lines = [
            (0, 0, {
                'name': 'name', # a label so accountant can understand where this line come from
                'debit': tempy or 0.0, # amount of debit
                'credit': 0, # amount of credit
                'account_id': record.transporter.transp_transit.id, #account_id, # account 
                'date': fields.Date.today(), #date,
                'partner_id': record.transporter.id, # partner if there is one
                #'currency_id': currency_id or (account.currency_id.id or False),
            }),
            (0, 0, {
                'name': 'name',
                'debit': 0, 
                'credit': record.contract_worksheet.total_alles or 0.0,
                'account_id': record.transporter.transp_transit.id,
                #'analytic_account_id': context.get('analytic_id', False),
                'date': fields.Date.today(), #date,
                'partner_id': record.transporter.id,
                #'currency_id': currency_id or (account.currency_id.id or False),
            })
        ]

        journal_id = False
        if record.transporter.transp_transit:
            journals = self.env['account.journal'].search([
                ('default_debit_account_id', '=', record.transporter.transp_transit.id)
            ])
            if journals:
                journal_id = journals[0].id
                acc_move.create({
                #'period_id': period_id, #Fiscal period
                    'journal_id': journal_id, #self.aajournal.id, # journal ex: sale journal, cash journal, bank journal....
                    'date': fields.Date.today(),
                    'state': 'draft',
                    'line_id': move_lines, # this is one2many field to account.move.line
                })

Which I call from view like this <button string="Account Moves" name="account_move_sc5_10" states="awaitingraw" type="object" class="oe_highlight"/>

I need that once this button is clicked, it should remain readonly, I know that with a boolean it could be done, but with this account.move method I'm kinda confused on how to achieve it.

Any ideas?


Solution

  • If making the button invisible after clicking on the button is OK for you, you can do this:

    Declare a field in the model where the button method is:

    button_clicked = fields.Boolean(
        string='Button clicked',
        default=False,
    )
    

    Modify the method called by the button, just to add this line:

    @api.multi
    def account_move_sc5_10(self):
        for record in self:
            record.write({
                'button_clicked': True,
            })
            ...
    

    Modify the view of the model where the button belongs to to add the new field and the condition:

    <field name="button_clicked" invisible="1"/>
    
    <button string="Account Moves" name="account_move_sc5_10" type="object" class="oe_highlight" attrs="{'invisible': ['|', ('state', 'not in', 'awaitingraw'), ('button_clicked', '=', True)]}"/>
    

    Note that I've removed the states parameter and that's because attrs and states can't be used together (they will do nothing if you use both). So, add state field to your view if you haven't it.