如何在不循环的情况下在odoo报告上添加静态字段?

how to add a static field on odoo report without looping?

我需要为每个员工打印一份时间表报告,我需要在 header 处显示员工姓名,而不是在列单元格上,因为它会在每一行中重复,并且我不需要那个

我尝试使用 < t-foreach > 但是它显示的名字太多了

 <template id="19011.employee">
     <t t-call="web.html_container">
         <t t-call="web.external_layout">
             <div class="page">
                 <div class="text-center">
                     <h2> <strong>TIME SHEET</strong>
                     </h2>
                     <h2>
                         <tr t-foreach="docs" t-as="o">
                             <span t-field="o.employee_id" />
                         </tr>
                     </h2>
                 </div>
                 <table class="table table-condensed" bgcolor="#875A7B">
                     <tr>
                         <th> check in</th>
                         <th> check out</th>
                         <th> Total</th>
                     </tr>
                     <tr t-foreach="docs" t-as="o">
                         <td>
                             <t t-esc="o.check_in" />
                         </td>
                         <td>
                             <t t-esc="o.check_out" />
                         </td>
                         <td>
                             <t t-esc="o.total" />
                         </td>
                     </tr>
                     <tr bgcolor="#875A7B">
                         <td align="center"> <strong> Total Hours</strong></td>
                         <td></td>
                         <td>
                             <strong t-esc="sum([o.total for o in docs])" />
                         </td>
                     </tr>
                 </table>
             </div>
         </t>
     </t>
 </template>

好吧,让我们尝试一个简单的解决方案,如果所有时间 sheet 都是针对一个员工的,您可以通过访问第一条记录来显示其姓名。

        <div class="text-center">
             <h2> <strong>TIME SHEET</strong>
             </h2>
             <h2>
                   <t t-if="docs">
                     <!-- in case employee don't have any attending records you dont want to get index error -->
                     <span t-field="docs[0].employee_id" />
                   </t>
             </h2>
         </div>

如果时间 sheet 有不止一名员工,那么它需要比这更多的工作,对此也有多种解决方案。

我不知道 index 访问第一个 record 是否会导致 t-field 出现问题,如果它尝试设置 variable

            <t t-set="first_record" t-value="docs[0]"/>
            <span t-field="first_record.employee_id" />

如果有不止一个记录我喜欢做的是组 records 通过 employee 使用 dictionary。但我喜欢这样做 python。所以我要做的就是在 attendance 中创建 method 遍历记录并创建一个 dictionary 然后调用它 report 中的方法并用两个循环打印 dictionary

  @api.mutli
  def group_py_employee(self):
    """ group record by employee."""
    result = {}
    for rec in self:
        if rec.employee_id in result:
            result[rec.employee_d].append(rec)
        else:
            result[rec.empoloyee_ids] = [rec,]

    return result

如果您想在报告中显示雇员标准页面:

<template id="19011.employee">
 <t t-call="web.html_container">
     <t t-call="web.external_layout">
         <!-- call the method to get the dictionary -->
         <t t-set="result"  t-value="docs.group_py_employee()/>
         <t t-foreach="result.items()" t-as="time_sheet">
             <!-- here first element is the employee record, second is it's attends records -->
             <div class="page">
                 <div class="text-center">
                     <h2> <strong>TIME SHEET</strong>
                     </h2>
                     <h2>
                         <!-- show employee in index 0 it's record of employee not attendance -->
                        <span t-field="time_sheet[0].name" />
                     </h2>
                 </div>
                 <table class="table table-condensed" bgcolor="#875A7B">
                     <tr>
                         <th> check in</th>
                         <th> check out</th>
                         <th> Total</th>
                     </tr>
                     <!-- second element is list of attendance-->
                     <tr t-foreach="time_sheet[1]" t-as="o">
                         <td>
                             <t t-esc="o.check_in" />
                         </td>
                         <td>
                             <t t-esc="o.check_out" />
                         </td>
                         <td>
                             <t t-esc="o.total" />
                         </td>
                     </tr>
                     <tr bgcolor="#875A7B">
                         <td align="center"> <strong> Total Hours</strong></td>
                         <td></td>
                         <td>
                             <strong t-esc="sum([o.total for o in time_sheet[1]])" />
                         </td>
                     </tr>
                 </table>
             </div>
        </t>
     </t>
 </t>

请注意,我忘记了包含员工时间 sheet 的 model 的名称 你应该 inheritmodel 并添加这个方法 group_py_employee 希望你明白了。