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?
These field value store in your DB?
No, they are not stored