Odoo 10动态过滤

时间:2018-08-17 19:38:44

标签: python xml odoo-10 listview-filter

好的,所以我试图为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>

1 个答案:

答案 0 :(得分:0)

我找到了一种在某些帮助下完成此操作的方法。这是我的工作答案。

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