Odoo Help

0

How to trigger calculations by clicking on a button

By
Arjun Khode
on 3/19/15, 9:02 AM 732 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>



About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 3/19/15, 9:02 AM
Seen: 732 times
Last updated: 3/19/15, 9:02 AM