Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Performing arithmetic on fields

By
Arjun Khode
on 3/15/15, 4:51 AM 509 views

Hi, I'm trying to write a module that calculates interest on a certain principal amount given a rate of interest, and displays the new amount to be returned in a field.

This is my python code:

class record(osv.Model):
    _name="record"
    _columns={
              'investment_id':fields.many2one('investment.model','Name of Investment'),
              'investor_id':fields.many2one('res.partner','Investor Name'),   
              'start_date':fields.date('Start Date'),
              #'end_date':fields.date('End Date'),
              'amount':fields.float('Amount Paid'),
              'roi':fields.float('Rate of Interest in fraction'),
              'returns':fields.float('Amount to be returned')
              }

I want 'returns' field to be like: returns=amount+amount*roi

Here is my XML:       

<record id="record_form_view" model="ir.ui.view">        
            <field name="name">record.form.view</field>
            <field name="view_type">form</field>
            <field name="model">record</field>
            <field name="arch" type="xml">
                <form string="Record">
                <group>
                    <field name="investment_id"/>
                    <field name="investor_id"/>
                    <field name="start_date"/>
                    <!--  <field name="end_date"/> -->
                    <field name="roi"/>
                    <field name="amount"/>
                    <field name="returns"/>
                    <!-- <button name="action_compute_returns" string="Compute Returns" type="object"/> -->
                    <button name="%(register_payment_button_action)d" string="Register Payment" type="action"/>
                    <button name="%(pay_investor_button_action)d"  string="Pay Investor" type="action"/>
                                            
                </group>
                </form>            
               </field>
        </record>

How to write or modify my code so that it will calculate the 'returns' field?
I also want to display the updated value of return.

 

2

Cyril Gaspard (GEM)

--Cyril Gaspard (GEM)--
4136
| 5 6 8
Paris, France
--Cyril Gaspard (GEM)--

Forum contributor since 2008

Cyril Gaspard (GEM)
On 3/15/15, 6:13 AM

Hi, no need to add a button for that, just use a field type function and a store parameter that depends of  2 fields you use to update the field returns when one of this 2 fields changes:


import openerp.addons.decimal_precision as dp

from openerp.osv import orm

class record(orm.Model):
    _name="record"

 

    def _amount_returns(self, cr, uid, ids, field, arg, context=None):

        context = context or {}

        res = {}

        for record in self.browse(cr, uid, ids, context=context:

            res[record.id] = (record.amount or 0.0) * (1 + (record.roi or 0.0))

        return res


    _columns={
              'investment_id':fields.many2one('investment.model','Name of Investment'),
              'investor_id':fields.many2one('res.partner','Investor Name'),   
              'start_date':fields.date('Start Date'),
              #'end_date':fields.date('End Date'),
              'amount':fields.float('Amount Paid'),
              'roi':fields.float('Rate of Interest in fraction'),
              'returns':fields.function(_amount_returns, digits_compute=dp.get_precision('Account'), string='Amount to be returned',
            store={
                'record': (lambda self, cr, uid, ids, c={}: ids, ['amount', 'roi'], 10),},
              }

0
Arjun Khode
On 3/16/15, 3:27 AM

Thankyou Cyril. The solution is perfect. This solved a big problem of mine.

Also, there were two parantheses missing but I added them.

Here's the code with correct parantheses:

import openerp.addons.decimal_precision as dp
from openerp.osv import orm

class record(orm.Model):
    _name="record"

    def _amount_returns(self, cr, uid, ids, field, arg, context=None):
        
        context = context or {}

        res = {}

        for record in self.browse(cr, uid, ids, context=context):

            res[record.id] = (record.amount or 0.0) * (1 + (record.roi or 0.0))
        
        return res

    _columns={
              'investment_id':fields.many2one('investment.model','Name of Investment'),
              'investor_id':fields.many2one('res.partner','Investor Name'),   
              'start_date':fields.date('Start Date'),
              #'end_date':fields.date('End Date'),
              'amount':fields.float('Amount Paid'),
              'roi':fields.float('Rate of Interest in fraction'),
              'returns':fields.function(_amount_returns, digits_compute=dp.get_precision('Account'), string='Amount to be returned',
            store={
                'record': (lambda self, cr, uid, ids, c={}: ids, ['amount', 'roi'], 10),
                   },)
              }

 

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 3/15/15, 4:51 AM
Seen: 509 times
Last updated: 3/16/15, 8:10 AM