Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Function calling on button click in OpenERP

    • 0
    • 4
    • 4
    • 1
    • 0
    • 0
    • 0
    • 0
    • 3.40k
    Comment on it

    Adding a button on an OpenERP view is very easy and calling a function on it too. For example I created a function to add cartage amount on the sales order, I created a field to add cartage amount on it and then on button click it will be added to the total amount of the invoice. Lets see how:

    Create a function

    def get_cartage(self, cr, uid, ids, invoice_id, context=None):
        res = {}
        acc_id = 39
        name = 'Cartage'
        amount = 0.0
        quantity = 1
        disc = 0.00
        val = 'TRUE'
        order = self.pool.get('sale.order')
        line = self.pool.get('account.invoice.line')        
        for invoice in self.browse(cr,uid,ids):            
            if invoice.cflag is False:
                amount = invoice.cartage
                if amount == 0.0:
                    raise osv.except_osv(_('No Value !')_('You must add cartage'))
                if amount > 0.0:
                    cr.execute('insert into account_invoice_line (invoice_id,name,account_id,origin,price_unit,quantity,discount,partner_id,uos_id,price_subtotal,company_id) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',(invoice.id,name,acc_id,invoice.origin,amount,quantity,disc,invoice.partner_id.id,quantity,amount,invoice.company_id.id))               
                cr.execute('update account_invoice set cflag = %s where id = %s',(val,invoice.id))
            if invoice.cflag is True and invoice.cartage > 0.0:
                raise osv.except_osv(_('Message'),_('Cartage already added'))
            #raise osv.except_osv(_('No Value !'),_(str(invoice.cflag)))
        return res
    

    Add a button in view

    <button name="get_cartage" string="Add Cartage" type="object" />
    

    Note the name attribute, I used name of the function to be called and type as object. This is all you have to do for a function call on a button.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: