Skip to Content
Menu
This question has been flagged
2 Replies
4684 Views

i'm using odoo 11. i have create a custom module for attendance modification request which be approuved by the manager of attendance . My goal is to limit the acces to the modification of attendance and when the manager press approuve "hr.attendance" will be modified automatically whith the new values of check_in and check_out (if it's the same employee and same date between "hr.attendance" and my request modification). The problem is when i press approuve there is no error but in the same time there is no modification. Here is my code. Any idea for help please ?

modification_request.xml

<record model="ir.ui.view" id="view_attendance_modification_request_form">
    <field name="name">attendance.modification.request.form</field>
    <field name="model">attendance.modification.request</field>
    <field name="arch" type="xml">
        <form string="Attendance modification Request">
            <header>
                <field name="state" statusbar_visible="draft,waiting,approved,cancel" widget="statusbar" />
                <button name="submit_modification" string="Submit for manager" type="object" class="btn-primary"
                        attrs="{'invisible': [('state','not in','draft')]}"/>
                <button name="modification_approval" type="object" string="Approve" class="oe_highlight"
                        groups="hr_attendance.group_hr_attendance_manager"
                        attrs="{'invisible': [('state','not in','waiting')]}"/>
                <button name="modification_rejection" type="object" string="Cancel" class="oe_highlight"
                        groups="hr_attendance.group_hr_attendance_manager"
                        attrs="{'invisible': [('state','not in','waiting')]}"/>
            </header>
            <sheet>

                <h2>
                    <group>

                        <field name="employee_id"/>
                    </group>
                </h2>
                <group  col="4" colspan="4">
                    <field name="time_check_in_1"/>
                    <field name="time_check_out_1"/>
                </group>

                <label for="note"/>
                <field name="note"/>
            </sheet>
            <field name="message_follower_ids" widget="mail_followers" groups="base.group_user"/>
            <field name="activity_ids" widget="mail_activity"/>
            <field name="message_ids" widget="mail_thread"/>


        </form>
    </field>
</record>

modification_request.py

 class AttendanceModificationRequest(models.Model):
_name = 'attendance.modification.request'
_description = 'Attendance modification Request'
_inherit = ['mail.thread', 'mail.activity.mixin']

def _get_employee_id(self):
    employee_rec = self.env['hr.employee'].search([('user_id', '=', self.env.uid)], limit=1)
    return employee_rec.id
employee_id = fields.Many2one('hr.employee',"Employee", readonly=True,default=_get_employee_id, required=True)
user_id = fields.Many2one('res.users', string='User', track_visibility='onchange', readonly=True,
                          states={'draft': [('readonly', False)]}, default=lambda self: self.env.user)
state = fields.Selection([
    ('draft', 'Pending'),
    ('waiting', 'Waiting for approval'),
    ('approved', 'Approved'),
    ('cancel', 'Cancelled')], readonly=True,
    help="Gives the state of the attendance request modification .",
    default='draft')
modification_date = fields.Date("Date")
time_check_in_1 = fields.Datetime("Check in")
time_check_out_1 = fields.Datetime("Check out")
note = fields.Text("Note")
attendance_id = fields.Many2one('hr.attendance', string='Attendance')
@api.multi
def modification_approval(self):
    attend_signin_ids = self.env['hr.attendance']
    check_in_date = datetime.strptime(self.time_check_in_1, "%Y-%m-%d %H:%M:%S").date()
    check_out_date = datetime.strptime(self.time_check_out_1, "%Y-%m-%d %H:%M:%S").date()

    for obj in attend_signin_ids :
      attendance_check_in_date = datetime.strptime(obj.check_in, "%Y-%m-%d %H:%M:%S").date()
      attendance_check_out_date = datetime.strptime(obj.check_out, "%Y-%m-%d %H:%M:%S").date()
      if (obj.employee_id == self.employee_id) and (check_in_date == attendance_check_in_date):
           obj.check_in = self.time_check_in_1
           obj.check_out = self.time_check_out_1

    return self.write({
        'state': 'approved'
    })



Avatar
Discard
Best Answer

Hi,

  Change the    attend_signin_ids to self.env['hr.attendance'].search([('employee_id','=',self.employee_id.id)])

def modification_approval(self):
    attend_signin_ids = self.env['hr.attendance'].search([('employee_id','=',self.employee_id.id)])
    check_in_date = datetime.strptime(self.time_check_in_1, "%Y-%m-%d %H:%M:%S").date()
    check_out_date = datetime.strptime(self.time_check_out_1, "%Y-%m-%d %H:%M:%S").date()

    for obj in attend_signin_ids :
      attendance_check_in_date = datetime.strptime(obj.check_in, "%Y-%m-%d %H:%M:%S").date()
      attendance_check_out_date = datetime.strptime(obj.check_out, "%Y-%m-%d %H:%M:%S").date()
      if (check_in_date == attendance_check_in_date):
           obj.write({'check_in': self.time_check_in_1,
                      'check_out': self.time_check_out_1})

    return self.write({
        'state': 'approved'
    })


Avatar
Discard
Author

Thanks for you answer but there's an error that display " strptime() argument 1 must be str, not bool" Do you know how to fix it ?

check_in_date = datetime.strptime(str(self.time_check_in_1), "%Y-%m-%d %H:%M:%S").date()

conver the datetime to string in strptime()

Author

Thank you very much it works and it shows another error which said " ValueError: time data 'False' does not match format '%Y-%m-%d %H:%M:%S'". I try to found a solution so i viewed the date of the table "hr.attendance" but they are the same format ""2019-05-08 10:06:51"".I don't know where the problem is ?

Best Answer

@Dhouha have you solved this problem?

Avatar
Discard
Related Posts Replies Views Activity
2
Feb 24
13186
1
Dec 22
3859
2
Dec 22
12723
2
Jun 22
3504
0
May 22
2945