I have a module that inherits the "account_voucher.view_vendor_receipt_form" view. When I register payment in this menu, I manually change the amount to be paid because I make a partial payment. I want this 'amount' value to be collected in a variable, say 'amt' after pressing validate.
I have a custom defined class called 'record' which has a variable called 'returns'. I want 'amt' to be subtracted from 'returns', and store it in a variable called 'balance'. But returns is in class record and amt is in class account.voucher.
So how can I perform this calculation and display the result?
Here is 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((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("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'),
'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="register_payment_button_action" model="ir.actions.act_window">
<field name="name">Register Payment</field>
<field name="res_model">account.voucher</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="domain">[('journal_id.type', 'in', ['bank', 'cash']), ('type','=','receipt')]</field>
<field name="context">{'type':'receipt'}</field>
<field name="view_id" ref="account_voucher.view_vendor_receipt_form"/>
<!-- <field name="view_id" ref="account_voucher.view_vendor_receipt_dialog_form"/> -->
<field name="target">new</field>
</record>
<record id="pay_investor_button_action" model="ir.actions.act_window">
<field name="name">Pay Investor</field>
<field name="res_model">account.voucher</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="domain">[('journal_id.type', 'out', ['bank', 'cash']), ('type','=','receipt')]</field>
<field name="context">{'type':'receipt'}</field>
<field name="view_id" ref="account_voucher.view_vendor_receipt_form"/>
<field name="target">new</field>
</record>
<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" context="{'default_amount': amount,'default_partner_id': investor_id'}" string="Register Payment" type="action"/>
<button name="%(pay_investor_button_action)d" context="{'default_amount': (returns * -1),'default_partner_id': investor_id'}" string="Pay Investor" type="action"/>
</group>
</form>
</field>
</record>