Odoo8-通过给定的用户和数据范围从Python生成报告

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

我需要在给定的数据范围内为给定的用户生成时间表报告。而且我还需要按日期汇总时间表小时数,以便报表中的每一行都有唯一的日期。

我已经准备好简单的报告以及用于选择特定用户和日期的向导。

但是我在用Python实际生成报表并将数据传递到报表时遇到了麻烦。当我在向导上单击打印按钮时,出现此错误:

ProgrammingError: relation "hr_timesheet_karty_wizard" does not exist
LINE 1: INSERT INTO "hr_timesheet_karty_wizard" ("id", "date_from", ...

我做错了什么?我不确定如何使用records。我应该将它们放在“ doc_ids”还是“ docs”中?如何按日期对记录进行求和?

我正在使用Odoo 8。

这是我的python代码:

# __init__.py

# -*- coding: utf-8 -*-
from . import wizard
# wizard/__init__.py

# -*- coding: utf-8 -*-
from . import wizard_karty
# wizard/wizard_karty.py

from openerp import api, models, fields
from datetime import datetime

class hr_timesheet_karty_wizard(models.AbstractModel):
    _name = 'hr.timesheet.karty.wizard'

    employee_id = fields.Many2one(comodel_name="hr.employee", required=True)
    date_from = fields.Date(default=fields.Datetime.now, required=True)
    date_to = fields.Date(default=fields.Datetime.now, required=True)

    @api.multi
    def print_report(self, data=None):

        records = self.env['hr.analytic.timesheet'].search([('employee_id', '=', self.employee_id),('date_from', '>=', self.date_from),('date_to', '<=', self.date_to)])   
        # sum records hours 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)

这是我的向导:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
  <data>

    <record id="view_hr_timesheet_karty_wizard" model="ir.ui.view">
        <field name="name">hr.timesheet.karty.wizard.form</field>
        <field name="model">hr.timesheet.karty.wizard</field>
        <field name="arch" type="xml">
        <form string="Leaves by Department">
            <group>
                <field name="employee_id"/>
                <field name="date_from"/>
                <field name="date_to"/>
            </group>
            <footer>
                <button name="print_report" string="Print" type="object" class="oe_highlight"/> or
                <button string="Cancel" special="cancel" class="oe_link"/>
            </footer>
        </form>
        </field>
    </record>

    <record id="action_hr_timesheet_karty_wizard" model="ir.actions.act_window">
        <field name="name">Time sheet report</field>
        <field name="res_model">hr.timesheet.karty.wizard</field>
        <field name="view_type">form</field>
        <field name="view_mode">form</field>
        <field name="view_id" ref="view_hr_timesheet_karty_wizard"/>
        <field name="target">new</field>
    </record>

    <menuitem id="menu_hr_timesheet_karty_wizard"
        name="Time sheet report"
        parent="hr.menu_hr_reporting_timesheet"
        action="action_hr_timesheet_karty_wizard"
        sequence="1"
        icon="STOCK_PRINT"/>

  </data>
</openerp>

这是我的报告:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
  <data>
    <report
        id="action_hr_timesheet_karty"
        string="Time sheet report"
        model="hr.analytic.timesheet"
        report_type="qweb-pdf"
        name="hr_timesheet_karty.template_report_hr_timesheet_karty"
    />

    <template id="template_hr_timesheet_karty">
        <t t-call="report.html_container">
            <t t-call="report.external_layout">
                <div class="page">

                    <table class="table table-condensed">
                        <thead>
                            <tr>
                                <th>User</th>
                                <th>Date</th>
                                <th>Time</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr t-foreach="docs" t-as="o">
                                <td><span t-field="o.user_id.name"/></td>
                                <td><span t-field="o.date"/></td>
                                <td class="text-right"><span t-field="o.unit_amount"/> h</td>
                            </tr>
                        </tbody>
                    </table>

                </div>

            </t>
        </t>
    </template>

  </data>
</openerp>

请帮助

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

该错误消息表明您的数据库中没有向导的表。即使它是一个抽象模型,也应该有一个。

您是否将Python代码导入__init__.py -file?


1
投票

用于创建抽象类的抽象模型超类,该抽象类应由常规模型(模型或TransientModels)继承[[但并不意味着可以单独使用或持久使用。

    _auto = False
  • #不为AbstractModels创建任何数据库后端
  • _register = False
  • 在ORM注册表中不可见,仅用于python继承
TransientModel用于存储在数据库中的临时数据,但是每隔一段时间就会自动清理。

您需要使用TransientModel而不是AbstractModel

class hr_timesheet_karty_wizard(models.TransientModel): _name = 'hr.timesheet.karty.wizard'

print_report方法看起来像qweb报告解析器的render_html方法。

  • print_report更改为:

    @api.multi def print_report(self): self.ensure_one() datas = {'wizard_id': self.id} return self.env['report'].get_action(self, 'hr_timesheet_karty.template_hr_timesheet_karty', data=datas)

  • 添加报告解析器:

    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([('employee_id', '=', wizard.employee_id.id), ('date_from', '>=', wizard.date_from), ('date_to', '<=', wizard.date_to)]) else: records = hr_analytic_timesheet.browse(self._ids) # sum records hours 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)

  • 将报告操作名称和模板ID更改为hr_timesheet_karty.template_hr_timesheet_karty

    <report id="action_hr_timesheet_karty" string="Time sheet report" model="hr.analytic.timesheet" report_type="qweb-pdf" name="hr_timesheet_karty.template_hr_timesheet_karty" /> <template id="template_hr_timesheet_karty">

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