Inherit Python Class

How to inherit python normal class in different module

Mustufa Rangwala

Base Module:

class AmountToWord(object):

    def _get_amount_total(self, obj):
        amount_total = 0.0
        try: # Order, Invoice
            amount_total = obj.amount_total
        except AttributeError:
            try: # Voucher
                for cr_line in obj.line_cr_ids:
                    amount_total += cr_line.amount
                for dr_line in obj.line_dr_ids:
                    amount_total -= dr_line.amount
                amount_total = abs(amount_total)
            except AttributeError:
                pass
        return amount_total

    def _amount_to_word_en(self, cursor, user, ids, name, arg, context=None):
        res = {}
        minus = False
        amount_text = ''
        for obj in self.browse(cursor, user, ids, context=context):
            a = 'Baht'
            b = 'Satang'
            if obj.currency_id.name == 'JYP':
                a = 'Yen'
                b = 'Sen'
            if obj.currency_id.name == 'GBP':
                a = 'Pound'
                b = 'Penny'
            if obj.currency_id.name == 'USD':
                a = 'Dollar'
                b = 'Cent'
            if obj.currency_id.name == 'EUR':
                a = 'Euro'
                b = 'Cent'   
            amount_total = self._get_amount_total(obj)
            if amount_total < 0:
                minus = True
                amount_total = -amount_total
            amount_text = amount_to_text(amount_total, 'en', a).replace('and Zero Cent', 'Only').replace('Cent', b).replace('Cents', b)
            res[obj.id] = minus and 'Minus ' + amount_text or amount_text
        return res
    
    def _amount_to_word_th(self, cursor, user, ids, name, arg, context=None):
        res = {}
        minus = False
        amount_text = ''
        for obj in self.browse(cursor, user, ids, context=context):
            amount_total = self._get_amount_total(obj)
            if amount_total < 0:
                minus = True
                amount_total = -amount_total
            amount_text = amount_to_text_th(amount_total, obj.currency_id.name)
            res[obj.id] = minus and 'ลบ' + amount_text or amount_text
            
        return res

class sale_order(AmountToWord, osv.osv):

    
    _inherit = 'sale.order'

    def _amount_total_text_en(self, cursor, user, ids, name, arg, context=None):   
        return self._amount_to_word_en(cursor, user, ids, name, arg, context=context)

    def _amount_total_text_th(self, cursor, user, ids, name, arg, context=None):   
        return self._amount_to_word_th(cursor, user, ids, name, arg, context=context)

    def _get_order(self, cr, uid, ids, context=None):
        result = {}
        for line in self.pool.get('sale.order.line').browse(cr, uid, ids, context=context):
            result[line.order_id.id] = True
        return result.keys()
    
    _columns = {
        'amount_total_text_en': fields.function(_amount_total_text_en, string='Amount Total (EN)', type='char',
            store={
                'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),
                'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
            }),
        'amount_total_text_th': fields.function(_amount_total_text_th, string='Amount Total (TH)', type='char',
            store={
                'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),
                'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
            }),    
    }

Now lets see how we can override it in another module.

from openerp.addons.amount_to_word.all_docs import AmountToWord

class AmountToWord_inherit(AmountToWord):

    def _get_amount_total(self, obj):
        amount_total = 0.0
        try: # Order, Invoice
            amount_total = obj.amount_total
        except AttributeError:
            try: # Voucher
                for cr_line in obj.line_cr_ids:
                    amount_total += cr_line.amount
                for dr_line in obj.line_dr_ids:
                    amount_total -= dr_line.amount
                amount_total = abs(amount_total)
            except AttributeError:
                pass
        return amount_total

class sale_order(AmountToWord_inherit, osv.osv):
        _inherit = 'sale.order'

.............

.............

..............