首页 > 解决方案 > Odoo 10 动态过滤

问题描述

好的,所以我正在尝试为 hr_attendances 制作几个动态过滤器。我想做类似于“当前月份”的事情,但我想要当前的支付期(两周支付期)和上一个支付期。有没有一种简单的方法可以做到这一点?这是我当前的代码。有没有办法从 xml 中的配置设置中引用字段以使域完全在 xml 中,或者有没有办法使用我在下面在 HrAttendance 中定义的方法?我还有一个 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'),
        }

人力资源出勤

class HrAttendance(models.Model):
    _inherit = "hr.attendance"

    @api.model
    def current_pay_period(self):
        import pdb; pdb.set_trace()
        settings = self.env['base.config.settings'].get_pay_periods()
        current_pay_period_start = settings['current_pay_period_start']
        current_pay_period_end = settings['current_pay_period_end']
        return [('check_in', '>=', current_pay_period_start.replace(hour=0, minute=0, second=0)), ('check_in', '<=', current_pay_period_end.replace(hour=23, minute=59, second=59))]

    @api.model
    def previous_pay_period(self):
        import pdb; pdb.set_trace()
        settings = self.env['base.config.settings'].get_pay_periods()
        previous_pay_period_start = settings['previous_pay_period_start']
        previous_pay_period_end = settings['previous_pay_period_end']
        return [('check_in', '>=', previous_pay_period_start.replace(hour=0, minute=0, second=0)), ('check_in', '<=', previous_pay_period_end.replace(hour=23, minute=59, second=59))]

和 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>

标签: pythonxmlodoo-10listview-filter

解决方案


在一些帮助下,我找到了一种方法。这是我的工作答案。

@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)

推荐阅读