This question has been flagged
2 Replies
3327 Views

I overrided an onchange method : basic_onchange_partner of account_voucher ( Customer Payments view), wich displays the default accout_id of the selected partner, as follows :

from openerp.osv import fields, orm, osv

class AccountVoucher(orm.Model):

    _inherit = 'account.voucher'
    _name = 'account.voucher'
    _columns = {
        'bls_to_pay': fields.one2many('stock.picking.out','bl_id','BLS to pay'),
    }

   def basic_onchange_partner(self, cr, uid, ids, partner_id, journal_id, ttype, context=None):
        if context==None:
            context={ }
       # here is the call of super 

       super(AccountVoucher, self).basic_onchange_partner(cr,uid,ids, partner_id, journal_id, ttype, context)

      # end of super call
        DO = self.pool.get('stock.picking.out')
        default = {
            'value': {'bls_to_pay' : [] ,},
        }
        do2pay_ids = DO.search(cr, uid, [('partner_id','=',partner_id),('state','=','done'),('invoice_state','=','2binvoiced')])
        for do2pay in do2pay_ids :
            do_rec= DO.browse(cr, uid, do2pay)
            rs = {
                'num_bl': do_rec.num_bl,
                'date' : do_rec.date,
                'origin': do_rec.origin,
            }
            default['value']['bls_to_pay'].append(rs) 
        return default

All stuffs that I added are working, apart of displaying account_id of selected partner.

Knowing that the original method does this :

    def basic_onchange_partner(self, cr, uid, ids, partner_id, journal_id, ttype, context=None):
        partner_pool = self.pool.get('res.partner')
        journal_pool = self.pool.get('account.journal')
        res = {'value': {'account_id': False}}
        if not partner_id or not journal_id:
            return res

        journal = journal_pool.browse(cr, uid, journal_id, context=context)
        partner = partner_pool.browse(cr, uid, partner_id, context=context)
        account_id = False
        if journal.type in ('sale','sale_refund'):
            account_id = partner.property_account_receivable.id
        elif journal.type in ('purchase', 'purchase_refund','expense'):
            account_id = partner.property_account_payable.id
        else:
            account_id = journal.default_credit_account_id.id or journal.default_debit_account_id.id

        res['value']['account_id'] = account_id
        return res

 Is it about the return value ? what makes the original one not diplaying account_id field value ?.

 

 

Avatar
Discard
Best Answer

Hi Yassine,

you can check below code.

 

from openerp.osv import fields, orm, osv

class AccountVoucher(orm.Model):

    _inherit = 'account.voucher'
    _name = 'account.voucher'
    _columns = {
        'bls_to_pay': fields.one2many('stock.picking.out','bl_id','BLS to pay'),
    }

    def basic_onchange_partner(self, cr, uid, ids, partner_id, journal_id, ttype, context=None):
        if context==None:
            context={ }
            

        res = super(AccountVoucher, self).basic_onchange_partner(cr,uid,ids, partner_id, journal_id, ttype, context)

        
        DO = self.pool.get('stock.picking.out')
        res['value']['bls_to_pay'] = []
         
        do2pay_ids = DO.search(cr, uid, [('partner_id','=',partner_id),('state','=','done'),('invoice_state','=','2binvoiced')])
        for do2pay in do2pay_ids :
            do_rec= DO.browse(cr, uid, do2pay)
            rs = {
                'num_bl': do_rec.num_bl,
                'date' : do_rec.date,
                'origin': do_rec.origin,
            }
            res['value']['bls_to_pay'].append(rs)
        return res

 

Best,

Harsh Dhaduk

Avatar
Discard
Best Answer

Yes, you need to take the super's return and incorporate whatever changes to that return value (or incorporate whatever in the return value to a new return value).

So you should do: super_res = super(AccountVoucher, self).basic_onchange_partner(cr,uid,ids, partner_id, journal_id, ttype, context)

And then before return, default['values'].update(super_res.get('values', {})). And do it for the other 2 keys for the return values: domain and warning.  You may need to set default[key] = {} if it is not initialized yet.

Avatar
Discard
Author

and if I want to add only one field, and not 2 and more to the original return, should I just do Like Harsh Dhaduk told ?

@Harsh Dhaduk's answer would work as it is using the return of super as the primary variable to return instead of creating a new variable (variable default in your code).