Odoo 10动态滤波

2024-06-26 17:39:52 发布

您现在位置:Python中文网/ 问答频道 /正文

好的,所以我试着做几个动态过滤器来处理人事部的出席情况。我想做一些类似“本月”的事情,但我想要当前的付薪期(两周的付薪期)和上一个付薪期。有简单的方法吗?这是我当前的代码。有没有一种方法可以引用xml中配置设置中的字段,使域完全在xml中,或者有没有方法使用我在hrattendence中定义的方法?我还有一个cron作业,设置每天运行的字段。再说一遍,有没有更简单的方法来运行这个?最后一个问题是,有没有一种方法可以让JavaScript过滤器比我现在看到的更简单?在

设置:

class BaseConfigSettings(models.TransientModel):
    _inherit = 'base.config.settings'

    previous_pay_period_start = fields.Datetime(string='Beginning of previous Pay Period')
    previous_pay_period_end = fields.Datetime(string='End of previous Pay Period')
    current_pay_period_start = fields.Datetime(string='Beginning of current Pay Period')
    current_pay_period_end = fields.Datetime(string='End of current Pay Period')

    @api.model
    def set_pay_periods(self):
        import pdb; pdb.set_trace()
        set_param = self.env['ir.config_parameter'].sudo().set_param
        today = datetime.today()
        today = today.replace(hour=0, minute=0, second=0, microsecond=0)
        #set beginning of current week
        start_current_week = today - timedelta(days=today.weekday()+1)
        #empty variable to contain the beginning of the current pay period
        current_start_date = ''
        #Date which
        original_pay_period_start_date = datetime(2018, 7, 22, 0, 0, 0)
        if (((start_current_week - original_pay_period_start_date).days/7)%2) == 0:
            current_start_date = start_current_week
        else:
            current_start_date = start_current_week - timedelta(weeks=1)
        set_param('timeclock.previous_pay_period_start', (current_start_date - timedelta(weeks=2)))
        set_param('timeclock.previous_pay_period_end', (current_start_date - timedelta(days=1)))
        set_param('timeclock.current_pay_period_start', (current_start_date))
        set_param('timeclock.current_pay_period_end', (current_start_date + timedelta(weeks=2) - timedelta(days=1)))

    @api.model
    def get_default_pay_periods(self, fields):
        return self.get_pay_periods()

    @api.model
    def get_pay_periods(self):
        import pdb; pdb.set_trace()
        get_param =  self.env['ir.config_parameter'].sudo().get_param
        return {
            'previous_pay_period_start': get_param('timeclock.previous_pay_period_start', 'False'),
            'previous_pay_period_end': get_param('timeclock.previous_pay_period_end', 'False'),
            'current_pay_period_start': get_param('timeclock.current_pay_period_start', 'False'),
            'current_pay_period_end': get_param('timeclock.current_pay_period_end', 'False'),
        }

人力资源部出勤

^{pr2}$

以及XML

<record id="hr_attendance_filter_emp_locs" model="ir.ui.view">
  <field name="name">hr.attendance.filter.emp_locs</field>
  <field name="model">hr.attendance</field>
  <field name="inherit_id" ref="hr_attendance.hr_attendance_view_filter"/>
  <field name="arch" type="xml">
    <xpath expr="//filter[@name='groupby_name']" position="after">
      <filter name="location" string="Location" context="{'group_by': 'location_id'}"/>
      <filter name="zone" string="Zone" context="{'group_by': 'zone_id'}"/>
      <field name="job_id"/>
    </xpath>

    <xpath expr="//filter[@name='today']" position="before">
      <filter string="Current Pay Period" domain="current_pay_period()" />
      <filter string="Last Pay Period" domain="previous_pay_period()" />
    </xpath>

  </field>
</record>

Tags: namefieldgetdatestringparamcurrentfilter
1条回答
网友
1楼 · 发布于 2024-06-26 17:39:52

我找到了一个方法,需要一些帮助。这是我的有效答案。在

@api.model
def search(self, args, limit=None, offset=0, order=None, count=False):
    pay_periods = self.env['base.config.settings'].get_pay_periods()
    if self.env.context.get('current_pay_period', False):
        args += [('check_in', '>=', pay_periods['current_pay_period_start']),
                ('check_in', '<=', pay_periods['current_pay_period_end'])]
    if self.env.context.get('previous_pay_period', False):
        args += [('check_in', '>=', pay_periods['previous_pay_period_start']),
                    ('check_in', '<=', pay_periods['previous_pay_period_end'])]
    return super(HrAttendance, self).search(args, limit=limit)

相关问题 更多 >