This question has been flagged
2 Replies
4184 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.

 

Avatar
Discard
Author Best Answer

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),
                   },)
              }

 

Avatar
Discard
Best Answer

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),},
              }

Avatar
Discard