Search code examples
pythonpython-2.7odoo-8odoo

How to get the value of amount_total of sale.order from another model?


I don't know how to get a value of current amount_total to convert it with myfunction, help me please.

from openerp.osv import fields, osv
import conver

class sale_total_extend(osv.Model):
   _inherit='sale.order'
   _name='sale.order'
   _description="le modele ajoute le total d'un devis en lettres"

def _amount_all_text(self, cr, uid, ids, field_name, arg, context=None):
    res = {}
    for order in self.browse(cr, uid, ids, context=context):
        res[order.id] = conver.myfunction(order.amount_total)
    return {}

_columns = {
    'amount_total_text': fields.function(_amount_all_text, string='total en lettres', type='text',store=True, multi='sums', help="Total en lettres"),
}

Solution

  • To override the function _amount_all try something like the following code (this example is in the website_sale_delivery module):

    class SaleOrder(orm.Model):
        _inherit = 'sale.order'
    
        def _amount_all_wrapper(self, cr, uid, ids, field_name, arg, context=None):        
            """ Wrapper because of direct method passing as parameter for function fields """
            return self._amount_all(cr, uid, ids, field_name, arg, context=context)
    
        def _amount_all(self, cr, uid, ids, field_name, arg, context=None):
            res = super(SaleOrder, self)._amount_all(cr, uid, ids, field_name, arg, context=context)
            currency_pool = self.pool.get('res.currency')
            for order in self.browse(cr, uid, ids, context=context):
                line_amount = sum([line.price_subtotal for line in order.order_line if line.is_delivery])
                currency = order.pricelist_id.currency_id
                res[order.id]['amount_delivery'] = currency_pool.round(cr, uid, currency, line_amount)
            return res