odoo / odoo

Odoo. Open Source Apps To Grow Your Business.
https://www.odoo.com
Other
38.75k stars 25.1k forks source link

[11] Hr Attendances - Multiple check-in #27762

Closed citronpower closed 5 years ago

citronpower commented 6 years ago

Impacted versions: Odoo11

Steps to reproduce: While check-in/check-out to quickly in Kiosk mode with badges

Current behavior: Multiple records are saved instead of one and create problem at the next check-in/check-out because there is a record which has not been checked-out but also an other, more recent, which has been check-out. Then Odoo is confused.

Solution found (if someone could check it and, if it is in order, pull it): Change in hr_attendance/static/src/js/greeting_message.js function start():

start: function() {
        if (this.attendance) {
            this.attendance.check_out ? this.farewell_message() : this.welcome_message();
        }
        // remove this
        // if (this.activeBarcode) {
        //     core.bus.on('barcode_scanned', this, this._onBarcodeScanned);
        // }
    }

Thanks in advance for your feedback.

pedrobaeza commented 6 years ago

The problem maybe is because you have several tabs opened?

citronpower commented 6 years ago

No... Unfortunately its not the problem. It really happens when doing the check-in/check-out too fast (for example when many employees are leaving). If we wait enough between two check (until the greeting message page has left ~), this problem doesnt seem to occur.

hw-aka commented 6 years ago

This has happened to me too. Sometimes, when an employee does check-in/out, there are multiple check-in records in the system which blocks future records.

It looked like the scanner was sending data too quickly without delay in between, but when I tested with a text editor, there was a delay - so not the problem with scanner. Even if there is a buggy scanner which sends data with no delay, it should be in-out-in-out, not in-in-in.

I could not find the problem, had to inherit the attendance_scan(self, barcode) function to discard the second scan of the same user within five seconds.

citronpower commented 6 years ago

@hw-aka can I ask for your inherited function attendance_scan(self, barcode) ? It will probably solve my problem as well, I would be thankful!

hw-aka commented 6 years ago

@citronpower Sure.

from odoo import api, fields, models, _
from datetime import datetime
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT

class HrAttendanceFix(models.Model):
    _inherit = 'hr.employee'

    @api.model
    def attendance_scan(self, barcode):
        """ Receive a barcode scanned from the Kiosk Mode and change the attendances of corresponding employee.
            Returns either an action or a warning.
        """
        employee = self.search([('barcode', '=', barcode)], limit=1)
        # Duplicate fix: no scan for the same person within 5 secs ###
        last_attendance_obj = self.env['hr.attendance'].search([
            ('employee_id', '=', employee.id)
        ], order='id desc', limit=1)
        to_check = False
        if last_attendance_obj.check_out:
            last_activity_time = last_attendance_obj.check_out
            to_check = True
        elif last_attendance_obj.check_in:
            last_activity_time = last_attendance_obj.check_in
            to_check = True
        if to_check and (datetime.strptime(fields.Datetime.now(), DEFAULT_SERVER_DATETIME_FORMAT) - datetime.strptime(last_activity_time, DEFAULT_SERVER_DATETIME_FORMAT)).seconds < 5.0:
            return  {'warning': _('%(employee)s, please let go your card from the QR scanner.') % {'employee': employee.name.split()[0]}}
        # End of fix
        return employee and employee.attendance_action('hr_attendance.hr_attendance_action_kiosk_mode') or \
            {'warning': _('No employee corresponding to barcode %(barcode)s') % {'barcode': barcode}}
RomainLibert commented 5 years ago

Fixed by https://github.com/odoo/odoo/pull/28150