Odoo 10 Dynamic Filtering

750 Views Asked by At

Ok, so I'm trying to make a couple of dynamic filters for hr_attendances. I want to do something similar to 'current month' but I want current pay period (two week pay period) and previous pay period. is there a simple way to do this? here is my current code. Is there either a way to reference the the fields from the config settings in the xml to make the domain completely in the xml or is there a way to use the methods I have defined below in HrAttendance? I also have a cron job setting the fields in settings that runs every day. Again, is there an easier way to run this? The last question, is there a way to make these filters with JavaScript that is easier than what I'm currently looking at?

Settings:

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'),
        }

Hr_Attendance

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

And the 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

There are 1 best solutions below

0
Cristin Meravi On BEST ANSWER

I found a way to do this with some help. Here is my working answer.

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