Odoo-按日期分组记录并使用Python打印它们

问题描述 投票:0回答:1

我需要制作一个报告,其中每个记录都有唯一的日期。但是我的数据在同一日期有多个记录。

因此,如果日期相同,我必须想办法汇总记录并求和一列(在这种情况下为unit_amount)。像这样:

enter image description here

例如,记录中的其余字段可以从具有特定日期的第一条记录中获取。甚至将其保留为空,这样会更容易。

目前我有这样的python代码:

class HrTimesheetKartyReport(models.AbstractModel):
    _name = 'report.hr_timesheet_karty.template_hr_timesheet_karty'

    @api.multi
    def render_html(self, data=None):
        hr_analytic_timesheet = self.env['hr.analytic.timesheet']
        if data and 'wizard_id' in data:
            wizard = self.env['hr.timesheet.karty.wizard'].browse(
                data['wizard_id'])
            records = hr_analytic_timesheet.search([('user_id', '=', wizard.user_id.id),
                                                    ('date', '>=', wizard.date_from),
                                                    ('date', '<=', wizard.date_to)])                                                    
        else:
            records = hr_analytic_timesheet.browse(self._ids)

        # here sum the record.unit_amount if date is the same (group by date)

        report_obj = self.env['report']
        report = report_obj._get_report_from_name(
            'hr_timesheet_karty.template_hr_timesheet_karty')
        docargs = {
            'doc_ids': self._ids,
            'doc_model': report.model,
            'docs': records,
        }
        return report_obj.render('hr_timesheet_karty.template_hr_timesheet_karty', docargs)

我正在选择要在变量records中打印的记录,并且在打印之前需要对其进行分组。

我正在使用Odoo8。

请帮助!

python odoo odoo-8
1个回答
0
投票

古玛

您可以使用def read_group方法用domain和要用分组依据过滤的field过滤数据。例如:

reached_goals = self.pool.get('gamification.goal').read_group(cr, uid, [
                ('challenge_id', '=', challenge.id),
                ('end_date', '=', end_date),
                ('state', '=', 'reached')
            ], fields=['user_id'], groupby=['user_id'], context=context)

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.