代码之家  ›  专栏  ›  技术社区  ›  Cristin Meravi

Odoo 10动态滤波

  •  -1
  • Cristin Meravi  · 技术社区  · 6 年前

    好的,所以我试着为人力资源部的出席制作几个动态过滤器。我想做一些类似于“本月”的事情,但我想要当前的付款期(两周的付款期)和以前的付款期。有什么简单的方法可以做到这一点吗?这是我现在的密码。有没有一种方法可以引用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'),
            }
    

    人力资源部

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

    <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 回复  |  直到 6 年前
        1
  •  0
  •   Cristin Meravi    6 年前

    我找到了一个有人帮忙的方法。这是我的工作答案。

    @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)
    
    推荐文章