I am trying to get sum of payslip_net of the selected records from tree view. it will be viewed in a pdf report. but it returns 0. and in log file it shows
Selected Records: hr.payslip()
here is the code in Python.
def get_net_sum(self):
self.ensure_one()
active_ids = self.env.context.get('active_ids', [])
selected_records = self.env['hr.payslip'].browse(active_ids)
logger.info(f"Selected Records: {selected_records}")
total_net = sum(record.payslip_net for record in selected_records if record.payslip_net)
return total_net
here I'm calling the function from XML report
The context here is not passing active_ids, that's why it's returning empty list.
i don't know why the context is not coming with active_ids!
Anyway, this is how I made it worked:
@api.model
def get_net_sum(self, docs):
total_net = sum(float(doc.payslip_net) for doc in docs if doc.payslip_net)
return total_net
in pdf report:
<th><span t-esc="o.get_net_sum(docs)"/></th>