Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
4 Risposte
6522 Visualizzazioni

Hello! How to add these fields  in pivot view?

version: 11

model: 'crossovered.budget.lines'

fields: practical_amount, theoretical_amount

They are computed:

practical_amount = fields.Float(compute='_compute_practical_amount', string='Practical Amount', digits=0)
@api.multi
def _compute_practical_amount(self):
for line in self:
result = 0.0
acc_ids = line.general_budget_id.account_ids.ids
date_to = self.env.context.get('wizard_date_to') or line.date_to
date_from = self.env.context.get('wizard_date_from') or line.date_from
if line.analytic_account_id.id:
self.env.cr.execute("""
SELECT SUM(amount)
FROM account_analytic_line
WHERE account_id=%s
AND (date between to_date(%s,'yyyy-mm-dd') AND to_date(%s,'yyyy-mm-dd'))
AND general_account_id=ANY(%s)""",
(line.analytic_account_id.id, date_from, date_to, acc_ids,))
result = self.env.cr.fetchone()[0] or 0.0
line.practical_amount = result


theoritical_amount = fields.Float(compute='_compute_theoritical_amount', string='Theoretical Amount', digits=0)
@api.multi
def _compute_theoritical_amount(self):
today = fields.Datetime.now()
for line in self:
# Used for the report

if self.env.context.get('wizard_date_from') and self.env.context.get('wizard_date_to'):
date_from = fields.Datetime.from_string(self.env.context.get('wizard_date_from'))
date_to = fields.Datetime.from_string(self.env.context.get('wizard_date_to'))
if date_from < fields.Datetime.from_string(line.date_from):
date_from = fields.Datetime.from_string(line.date_from)
elif date_from > fields.Datetime.from_string(line.date_to):
date_from = False

if date_to > fields.Datetime.from_string(line.date_to):
date_to = fields.Datetime.from_string(line.date_to)
elif date_to < fields.Datetime.from_string(line.date_from):
date_to = False

theo_amt = 0.00
if date_from and date_to:
line_timedelta = fields.Datetime.from_string(line.date_to) - fields.Datetime.from_string(line.date_from)
elapsed_timedelta = date_to - date_from
if elapsed_timedelta.days > 0:
theo_amt = (elapsed_timedelta.total_seconds() / line_timedelta.total_seconds()) * line.planned_amount
else:
if line.paid_date:
if fields.Datetime.from_string(line.date_to) <= fields.Datetime.from_string(line.paid_date):
theo_amt = 0.00
else:
theo_amt = line.planned_amount
else:
line_timedelta = fields.Datetime.from_string(line.date_to) - fields.Datetime.from_string(line.date_from)
elapsed_timedelta = fields.Datetime.from_string(today) - (fields.Datetime.from_string(line.date_from))

if elapsed_timedelta.days < 0:
# If the budget line has not started yet, theoretical amount should be zero
theo_amt = 0.00
elif line_timedelta.days > 0 and fields.Datetime.from_string(today) < fields.Datetime.from_string(line.date_to):
# If today is between the budget line date_from and date_to
theo_amt = (elapsed_timedelta.total_seconds() / line_timedelta.total_seconds()) * line.planned_amount
else:
theo_amt = line.planned_amount

line.theoritical_amount = theo_amt

Does it possible to do this?

Avatar
Abbandona

These field value store in your DB?

Autore

No, they are not stored

Risposta migliore

Hello, 
I think your field does not store in DB so the first store this value in your DB after you can easy to use this field in your pivot view.

In the case of computing method maybe you have to use @api.depend and use store = True. 
Maybe it's work for you. 


Avatar
Abbandona
Autore Risposta migliore

Yes, I thought about this way, but I was thinking to find the way not to redeclare standard functions.

Thanks for your answer, Manish! I'll do as you propose.

Avatar
Abbandona
Post correlati Risposte Visualizzazioni Attività
1
feb 19
2323
1
ago 18
14856
1
apr 25
14401
2
mar 25
376
1
mar 25
1492