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

How to trigger calculations by clicking on a button

By
Arjun Khode
on 3/19/15, 9:02 AM 448 views

Hi, I have a model in which 'rate of interest' is multiplied by 'principal amount' and 'no. of months passed'

This calculation happens through a function field that is linked to a function that calculates the 'amount to be returned'.

I soon realized the drawback that the 'number of months passed' are only taken on the date when that entry was created.

Now I need a button that will calculate the current latest 'amount to be returned' when the button is clicked, for all entries in the table.

Can someone tell me how this can be done?

Here's my code:

PYTHON:


class record(orm.Model):

_name="record"

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

context = context or {}

res = {}

''''''

def monthdelta(d1, d2):

delta = 0

while True:

mdays = monthrange(d1.year, d1.month)[1]

d1 += timedelta(days=mdays)

if d1 <= d2:

delta += 1

else:

break

return delta

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

#(monthdelta(datetime.today(),datetime.strptime(record.start_date,"%Y-%m-%d"))

# month_diff=(datetime.today()-datetime.strptime(record.start_date,"%Y-%m-%d")).months

print("Hello!!")

# print(month_diff)

print((monthdelta(datetime.today(),datetime.strptime(record.start_date,"%Y-%m-%d"))))

r = relativedelta.relativedelta(datetime.today(),datetime.strptime(record.start_date,"%Y-%m-%d"))

print("Monthly interest")

record.monthly_interest=((record.roi or 0.0)*(r.months or 1)*record.amount)

print(record.monthly_interest)

#print((record.roi or 0.0)*(r.months or 1)*record.amount)

print("months")

print(r.months)

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

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 Invested'),

#'status_paid':fields.boolean('Total Amount Paid'),

#'status_returned':fields.boolean('Total Amount Returned'),

'done':fields.boolean('Mark As Done',invisible=True),

'roi':fields.float('Rate of Interest in fraction'),

'monthly_interest':fields.float('Cumulative Monthly Interest'),

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

},)

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" domain="[('is_investor','=',True)]"/>

<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" attrs="{'invisible':[('done','=',True)]}" context="{'default_amount': amount,'default_partner_id': investor_id'}" string="Register Payment" type="action"/>

<button name="%(pay_investor_button_action)d" attrs="{'invisible':[('done','=',True)]}" context="{'default_amount': (returns * -1),'default_partner_id': investor_id'}" string="Pay Investor" type="action"/>

<button name="mark_as_done" string="Mark as Done" type="object"/>

</group>

</form>

</field>

</record>



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/19/15, 9:02 AM
Seen: 448 times
Last updated: 3/19/15, 9:02 AM