Skip to Content
Odoo Menú
  • Registra entrada
  • Prova-ho gratis
  • Aplicacions
    Finances
    • Comptabilitat
    • Facturació
    • Despeses
    • Full de càlcul (IA)
    • Documents
    • Signatura
    Vendes
    • CRM
    • Vendes
    • Punt de venda per a botigues
    • Punt de venda per a restaurants
    • Subscripcions
    • Lloguer
    Imatges de llocs web
    • Creació de llocs web
    • Comerç electrònic
    • Blog
    • Fòrum
    • Xat en directe
    • Aprenentatge en línia
    Cadena de subministrament
    • Inventari
    • Fabricació
    • PLM
    • Compres
    • Manteniment
    • Qualitat
    Recursos humans
    • Empleats
    • Reclutament
    • Absències
    • Avaluacions
    • Recomanacions
    • Flota
    Màrqueting
    • Màrqueting Social
    • Màrqueting per correu electrònic
    • Màrqueting per SMS
    • Esdeveniments
    • Automatització del màrqueting
    • Enquestes
    Serveis
    • Projectes
    • Fulls d'hores
    • Servei de camp
    • Suport
    • Planificació
    • Cites
    Productivitat
    • Converses
    • Artificial Intelligence
    • IoT
    • VoIP
    • Coneixements
    • WhatsApp
    Aplicacions de tercers Odoo Studio Plataforma d'Odoo al núvol
  • Sectors
    Comerç al detall
    • Llibreria
    • Botiga de roba
    • Botiga de mobles
    • Botiga d'ultramarins
    • Ferreteria
    • Botiga de joguines
    Food & Hospitality
    • Bar i pub
    • Restaurant
    • Menjar ràpid
    • Guest House
    • Distribuïdor de begudes
    • Hotel
    Immobiliari
    • Agència immobiliària
    • Estudi d'arquitectura
    • Construcció
    • Property Management
    • Jardineria
    • Associació de propietaris de béns immobles
    Consultoria
    • Empresa comptable
    • Partner d'Odoo
    • Agència de màrqueting
    • Bufet d'advocats
    • Captació de talent
    • Auditoria i certificació
    Fabricació
    • Textile
    • Metal
    • Mobles
    • Menjar
    • Brewery
    • Regals corporatius
    Salut i fitness
    • Club d'esport
    • Òptica
    • Centre de fitness
    • Especialistes en benestar
    • Farmàcia
    • Perruqueria
    Trades
    • Servei de manteniment
    • Hardware i suport informàtic
    • Sistemes d'energia solar
    • Shoe Maker
    • Serveis de neteja
    • Instal·lacions HVAC
    Altres
    • Nonprofit Organization
    • Agència del medi ambient
    • Lloguer de panells publicitaris
    • Fotografia
    • Lloguer de bicicletes
    • Distribuïdors de programari
    Browse all Industries
  • Comunitat
    Aprèn
    • Tutorials
    • Documentació
    • Certificacions
    • Formació
    • Blog
    • Pòdcast
    Potenciar l'educació
    • Programa educatiu
    • Scale-Up! El joc empresarial
    • Visita Odoo
    Obtindre el programari
    • Descarregar
    • Comparar edicions
    • Novetats de les versions
    Col·laborar
    • GitHub
    • Fòrum
    • Esdeveniments
    • Traduccions
    • Converteix-te en partner
    • Services for Partners
    • Registra la teva empresa comptable
    Obtindre els serveis
    • Troba un partner
    • Troba un comptable
    • Contacta amb un expert
    • Serveis d'implementació
    • Referències del client
    • Suport
    • Actualitzacions
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Programar una demo
  • Preus
  • Ajuda
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
Ajuda

Performing arithmetic on fields

Subscriure's

Get notified when there's activity on this post

This question has been flagged
onchangeInterest
2 Respostes
6189 Vistes
Avatar
Arjun Khode

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.

 

0
Avatar
Descartar
Avatar
Arjun Khode
Autor 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),
                   },)
              }

 

0
Avatar
Descartar
Avatar
Cyril Gaspard (GEM)
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),},
              }

2
Avatar
Descartar
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Registrar-se
Related Posts Respostes Vistes Activitat
trying to auto correct a wrong users value
onchange
Avatar
Avatar
1
d’oct. 23
3775
"Wrong value for %s: %r" % (self, value) Solved
onchange
Avatar
Avatar
2
d’oct. 23
4076
How to add domain in onchange function for a One2many field Solved
onchange
Avatar
Avatar
Avatar
2
d’ag. 23
6365
How to create dynamic domain on many2one fields with onchange function? Solved
onchange
Avatar
Avatar
Avatar
Avatar
Avatar
4
d’ag. 23
23099
Odoo onchange method is not saving values in readonly fields Solved
onchange
Avatar
Avatar
Avatar
Avatar
3
d’oct. 22
13554
Community
  • Tutorials
  • Documentació
  • Fòrum
Codi obert
  • Descarregar
  • GitHub
  • Runbot
  • Traduccions
Serveis
  • Allotjament a Odoo.sh
  • Suport
  • Actualització
  • Desenvolupaments personalitzats
  • Educació
  • Troba un comptable
  • Troba un partner
  • Converteix-te en partner
Sobre nosaltres
  • La nostra empresa
  • Actius de marca
  • Contacta amb nosaltres
  • Llocs de treball
  • Esdeveniments
  • Pòdcast
  • Blog
  • Clients
  • Informació legal • Privacitat
  • Seguretat
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk Slovenščina Español (América Latina) Español Svenska ภาษาไทย Türkçe українська Tiếng Việt

Odoo és un conjunt d'aplicacions empresarials de codi obert que cobreix totes les necessitats de la teva empresa: CRM, comerç electrònic, comptabilitat, inventari, punt de venda, gestió de projectes, etc.

La proposta única de valor d'Odoo és ser molt fàcil d'utilitzar i estar totalment integrat, ambdues alhora.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now