models.py 52.3 KB
Newer Older
Administrator committed
1 2 3 4
from django.db import models
from outils.common_imports import *

from outils.common import OdooAPI
5
from outils.common import Verification
6
from members.models import CagetteMember
7
from members.models import CagetteUser
8
import shifts.fonctions
Administrator committed
9 10 11 12 13 14

from pytz import timezone

import locale
import re

15
import dateutil.parser
Administrator committed
16

17 18
tz = pytz.timezone("Europe/Paris")

Administrator committed
19 20 21 22 23 24 25 26
class CagetteShift(models.Model):
    """Class to handle cagette Odoo Shift."""

    def __init__(self):
        """Init with odoo id."""
        self.tz = pytz.timezone("Europe/Paris")
        self.o_api = OdooAPI()

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    def get_cycle_week_data(self, date=None):
        result = {}
        try:
            res_param = self.o_api.search_read('ir.config_parameter',
                                               [['key', '=', 'coop_shift.week_a_date']],
                                               ['value'])
            if res_param:
                import math
                WEEKS = ['A', 'B', 'C', 'D']
                start_A = tz.localize(datetime.datetime.strptime(res_param[0]['value'], '%Y-%m-%d'))
                result['start'] = start_A
                now = datetime.datetime.now(tz)  # + datetime.timedelta(hours=72)
                if date is not None:
                    now = tz.localize(datetime.datetime.strptime(date, '%Y-%m-%d'))
                diff = now - start_A
                weeks_diff = diff.total_seconds() / 3600 / 24 / 7
                week_index = math.floor(weeks_diff % 4)
                result['week_name'] = WEEKS[week_index]
                result['start_date'] = start_A + datetime.timedelta(weeks=math.floor(weeks_diff))

        except Exception as e:
            coop_logger.error("get_current_cycle_week_data %s", str(e))
            result['error'] = str(e)
        return result

    def is_matching_ftop_rules(self, partner_id, idNewShift, idOldShift=0):
        answer = True
        rules = getattr(settings, 'FTOP_SERVICES_RULES', {})
        if ("successive_shifts_allowed" in rules
             or
            "max_shifts_per_cycle" in rules
            ):
            try:
                now = datetime.datetime.now(tz)
                # Have to retrive shifts (from now to a cycle period forward to check rules respect)
62
                [shift_registrations, is_ftop] = shifts.fonctions.get_shift_partner(self.o_api, partner_id, now + datetime.timedelta(weeks=4))
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
                new_shift = self.get_shift(idNewShift)  # WARNING : use date_begin_tz while shift_registrations use date_begin (UTC)
                if "successive_shifts_allowed" in rules:
                    min_duration = getattr(settings, 'MIN_SHIFT_DURATION', 2)
                    for sr in shift_registrations:
                        if int(sr['shift_id'][0]) != int(idOldShift):
                            diff = (datetime.datetime.strptime(sr['date_begin'], '%Y-%m-%d %H:%M:%S').astimezone(tz)
                                    - tz.localize(datetime.datetime.strptime(new_shift['date_begin_tz'], '%Y-%m-%d %H:%M:%S')))
                            if abs(diff.total_seconds() / 3600) < (min_duration * 2) * (int(rules['successive_shifts_allowed']) + 1):
                                answer = False
                            #  coop_logger.info(sr['date_begin'] + ' - ' + new_shift['date_begin_tz'])
                            #  coop_logger.info(str(diff.total_seconds()/3600) + 'h')
                if "max_shifts_per_cycle" in rules:
                    [ymd, hms] = new_shift['date_begin_tz'].split(" ")
                    cw = self.get_cycle_week_data(ymd)
                    if 'start_date' in cw:
                        sd = cw['start_date']
                        ed = cw['start_date'] + datetime.timedelta(weeks=4)
80
                        [cycle_shift_regs, is_ftop] = shifts.fonctions.get_shift_partner(self.o_api, partner_id, start_date=sd, end_date=ed)
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
                        if len(cycle_shift_regs) >= int(rules['max_shifts_per_cycle']):
                            answer = False
                            coop_logger.info("services max par cycle atteint pour partner_id %s", str(partner_id))
            except Exception as e:
                coop_logger.error("is_shift_exchange_allowed %s %s", str(e), str(new_shift))
        return answer

    def is_shift_exchange_allowed(self, idOldShift, idNewShift, shift_type, partner_id):
        answer = True
        min_delay = getattr(settings, 'STANDARD_BLOCK_SERVICE_EXCHANGE_DELAY', 0)
        if shift_type == "ftop":
            min_delay = getattr(settings, 'FTOP_BLOCK_SERVICE_EXCHANGE_DELAY', 0)
        if min_delay > 0:
            now = datetime.datetime.now(tz)
            old_shift = self.get_shift(idOldShift)
            day_before_old_shift_date_start = \
                tz.localize(datetime.datetime.strptime(old_shift['date_begin_tz'], '%Y-%m-%d %H:%M:%S')
                            - datetime.timedelta(hours=min_delay))

            if now > day_before_old_shift_date_start:
                answer = False
            elif shift_type == "ftop":
                answer = self.is_matching_ftop_rules(partner_id, idNewShift, idOldShift)


        return answer

108 109 110 111 112 113 114 115 116 117
    def get_shift(self, id):
        """Get one shift by id"""
        cond = [['id', '=', id]]

        fields = ['date_begin_tz']
        listService = self.o_api.search_read('shift.shift', cond, fields)

        try:
            return listService[0]
        except Exception as e:
118
            coop_logger.error("get_shift %s", str(e))
119 120
            return None

121 122 123 124 125 126 127 128 129 130
    def is_matching_ftop_rules(self, partner_id, idNewShift, idOldShift=0):
        answer = True
        rules = getattr(settings, 'FTOP_SERVICES_RULES', {})
        if ("successive_shifts_allowed" in rules
             or
            "max_shifts_per_cycle" in rules
            ):
            try:
                now = datetime.datetime.now(tz)
                # Have to retrive shifts (from now to a cycle period forward to check rules respect)
131
                [shift_registrations, is_ftop] = shifts.fonctions.get_shift_partner(self.o_api, partner_id, now + datetime.timedelta(weeks=4))
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
                new_shift = self.get_shift(idNewShift)  # WARNING : use date_begin_tz while shift_registrations use date_begin (UTC)
                if "successive_shifts_allowed" in rules:
                    min_duration = getattr(settings, 'MIN_SHIFT_DURATION', 2)
                    for sr in shift_registrations:
                        if int(sr['shift_id'][0]) != int(idOldShift):
                            diff = (datetime.datetime.strptime(sr['date_begin'], '%Y-%m-%d %H:%M:%S').astimezone(tz)
                                    - tz.localize(datetime.datetime.strptime(new_shift['date_begin_tz'], '%Y-%m-%d %H:%M:%S')))
                            if abs(diff.total_seconds() / 3600) < (min_duration * 2) * (int(rules['successive_shifts_allowed']) + 1):
                                answer = False
                            #  coop_logger.info(sr['date_begin'] + ' - ' + new_shift['date_begin_tz'])
                            #  coop_logger.info(str(diff.total_seconds()/3600) + 'h')
                if "max_shifts_per_cycle" in rules:
                    [ymd, hms] = new_shift['date_begin_tz'].split(" ")
                    cw = self.get_cycle_week_data(ymd)
                    if 'start_date' in cw:
                        sd = cw['start_date']
                        ed = cw['start_date'] + datetime.timedelta(weeks=4)
149
                        [cycle_shift_regs, is_ftop] = shifts.fonctions.get_shift_partner(self.o_api, partner_id, start_date=sd, end_date=ed)
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
                        if len(cycle_shift_regs) >= int(rules['max_shifts_per_cycle']):
                            answer = False
                            coop_logger.info("services max par cycle atteint pour partner_id %s", str(partner_id))
            except Exception as e:
                coop_logger.error("is_shift_exchange_allowed %s %s", str(e), str(new_shift))
        return answer

    def is_shift_exchange_allowed(self, idOldShift, idNewShift, shift_type, partner_id):
        answer = True
        min_delay = getattr(settings, 'STANDARD_BLOCK_SERVICE_EXCHANGE_DELAY', 0)
        if shift_type == "ftop":
            min_delay = getattr(settings, 'FTOP_BLOCK_SERVICE_EXCHANGE_DELAY', 0)
        if min_delay > 0:
            now = datetime.datetime.now(tz)
            old_shift = self.get_shift(idOldShift)
            day_before_old_shift_date_start = \
                tz.localize(datetime.datetime.strptime(old_shift['date_begin_tz'], '%Y-%m-%d %H:%M:%S')
                            - datetime.timedelta(hours=min_delay))

            if now > day_before_old_shift_date_start:
                answer = False
            elif shift_type == "ftop":
                answer = self.is_matching_ftop_rules(partner_id, idNewShift, idOldShift)


        return answer

Administrator committed
177 178 179 180 181 182
    def get_data_partner(self, id):
        """Retrieve partner data useful to make decision about shift options"""
        cond = [['id', '=', id]]
        fields = ['display_name', 'display_std_points',
                  'shift_type', 'date_alert_stop', 'date_delay_stop', 'extension_ids',
                  'cooperative_state', 'final_standard_point', 'create_date',
183
                  'final_ftop_point', 'shift_type', 'leave_ids', 'makeups_to_do', 'barcode_base',
184
                  'street', 'street2', 'zip', 'city', 'mobile', 'phone', 'function', 'email',
185
                  'is_associated_people', 'parent_id', 'suppleant_member_id', 'extra_shift_done']
Administrator committed
186 187 188
        partnerData = self.o_api.search_read('res.partner', cond, fields, 1)
        if partnerData:
            partnerData = partnerData[0]
189
            if partnerData['suppleant_member_id']:
Félicie committed
190
                cond = [['id', '=', partnerData['parent_id'][0]]]
Damien Moulard committed
191
                fields = ['create_date', 'makeups_to_do', 'date_delay_stop', 'extra_shift_done']
Félicie committed
192 193 194
                parentData = self.o_api.search_read('res.partner', cond, fields, 1)
                if parentData:
                    partnerData['parent_create_date'] = parentData[0]['create_date']
195 196
                    partnerData['parent_makeups_to_do'] = parentData[0]['makeups_to_do']
                    partnerData['parent_date_delay_stop'] = parentData[0]['date_delay_stop']
197
                    partnerData['parent_extra_shift_done'] = parentData[0]['extra_shift_done']
Félicie committed
198

Administrator committed
199 200 201
            if partnerData['shift_type'] == 'standard':
                partnerData['in_ftop_team'] = False
                #  Because 'in_ftop_team' doesn't seem to be reset to False in Odoo
202
            if partnerData['suppleant_member_id']:
Félicie committed
203 204 205
                cond = [['partner_id.id', '=', partnerData['parent_id'][0]]]
            else:
                cond = [['partner_id.id', '=', id]]
Administrator committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
            fields = ['shift_template_id', 'is_current']
            shiftTemplate = self.o_api.search_read('shift.template.registration', cond, fields)
            if (shiftTemplate and len(shiftTemplate) > 0):
                s_t_id = None
                for s_t in shiftTemplate:
                    if s_t['is_current'] is True:
                        s_t_id = s_t['shift_template_id'][0]
                if not (s_t_id is None):
                    cond = [['shift_template_id.id', '=', int(s_t_id)],
                            ['date_begin_tz', '>', datetime.datetime.now().isoformat()]]
                    fields = ['date_begin_tz', 'name']
                    nextShifts = self.o_api.search_read('shift.shift', cond, fields, 1)
                    if nextShifts:
                        (d, h) = nextShifts[0]['date_begin_tz'].split(' ')
                        partnerData['next_regular_shift_date'] = d
                        partnerData['regular_shift_name'] = nextShifts[0]['name']
            partnerData['is_leave'] = False
            if len(partnerData['leave_ids']) > 0:
                # Is member in active leave period
                now = datetime.datetime.now().isoformat()
                cond = [['id', 'in', partnerData['leave_ids']],
                        ['start_date', '<', now],
                        ['stop_date', '>', now], ['state', '!=', 'cancel']]
                fields = ['start_date', 'stop_date', 'type_id', 'state']
                res_leaves = self.o_api.search_read('shift.leave', cond, fields)

                if res_leaves and len(res_leaves) > 0:
                    # TODO : Consider > 1 results
                    partnerData['is_leave'] = True
                    partnerData["leave_start_date"] = res_leaves[0]["start_date"]
                    partnerData["leave_stop_date"] = res_leaves[0]["stop_date"]
Félicie committed
237

Administrator committed
238 239
        return partnerData

240

241 242 243 244 245 246 247 248
    def shift_is_makeup(self, id):
        """vérifie si une shift est un rattrapage"""
        fields = ["is_makeup", "id"] 
        cond = [['id', '=', id]]
        shiftData = self.o_api.search_read('shift.registration', cond, fields)
        return shiftData[0]["is_makeup"]


249
    def get_shift_calendar(self, is_ftop, start, end):
Administrator committed
250
        """Récupère les shifts à partir de maintenant pour le calendier"""
251
        max_weeks_ahead = getattr(settings,'FTOP_SHIFTS_VIEW_LIMIT', None)
Administrator committed
252 253 254 255 256 257 258 259 260 261 262 263
        cond = [['date_begin', '>', datetime.datetime.now().isoformat()],
                ['state', '!=', 'cancel']]
        try:
            start_d = datetime.datetime.strptime(start, '%Y-%m-%d')
            cond.append(['date_begin', '>=', start_d.isoformat()])
        except:
            pass
        try:
            end_d = datetime.datetime.strptime(end, '%Y-%m-%d')
            cond.append(['date_end', '<=', end_d.isoformat()])
        except:
            pass
264 265 266 267
        if max_weeks_ahead and is_ftop:
            max_end = datetime.datetime.now() + datetime.timedelta(weeks=max_weeks_ahead)
            cond.append(['date_end', '<=', max_end.isoformat()])

Administrator committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
        # 2018-11-25 seats_available instead of seats_max
        fields = ['date_begin_tz',
                  'date_end_tz', 'name',
                  'shift_template_id',
                  'event_type_id', 'seats_reserved',
                  'seats_available', 'registration_ids', 'address_id', 'shift_type_id']
        listService = self.o_api.search_read('shift.shift', cond, fields)

        return listService

    def get_leave(self, idPartner):
        """Récupération des congés en cours du membre"""
        now = datetime.datetime.now().isoformat()
        cond = [['partner_id', '=', idPartner], ['start_date', '<', now], ['stop_date', '>', now]]
        fields = ['stop_date', 'id', 'start_date']
        return self.o_api.search_read('shift.leave', cond, fields)

285
    def get_shift_ticket(self,idShift, shift_type):
Administrator committed
286
        """Récupérer le shift_ticket suivant le membre et flotant ou pas"""
287
        if getattr(settings, 'USE_STANDARD_SHIFT', True) == False:
288
            shift_type = "ftop"
Administrator committed
289 290 291
        fields = ['shift_ticket_ids']
        cond = [['id', "=", idShift]]
        listeTicket = self.o_api.search_read('shift.shift', cond, fields)
292
        if shift_type == "ftop":
Administrator committed
293 294 295 296 297
            return listeTicket[0]['shift_ticket_ids'][1]
        else:
            return listeTicket[0]['shift_ticket_ids'][0]

    def set_shift(self, data):
298 299
        """Shift registration.
        Decrement makeups_to_do if shift has to be a makeup.
300 301 302 303 304 305 306
        Handle partner already registered on this shift case.
        data should coutain :
        - idPartner
        - idShift
        - shift_type
        - is_makeup
        """
Administrator committed
307 308
        st_r_id = False
        try:
309
            shift_type = "standard"
310
            if data['shift_type'] == "ftop" or getattr(settings, 'USE_STANDARD_SHIFT', True) == False:
311
                shift_type = "ftop"
Administrator committed
312 313
            fieldsDatas = { "partner_id": data['idPartner'],
                            "shift_id": data['idShift'],
314
                            "shift_ticket_id": self.get_shift_ticket(data['idShift'], data['shift_type']),
315 316
                            "shift_type": shift_type,  
                            "origin": 'memberspace',
317
                            "is_makeup": data['is_makeup'],
Administrator committed
318
                            "state": 'open'}
319 320
            if (shift_type == "standard" and data['is_makeup'] is not True) or shift_type == "ftop":
                fieldsDatas['template_created'] = 1  # It's not true but otherwise, presence add 1 standard point , which is not wanted
Administrator committed
321 322 323 324

            st_r_id = self.o_api.create('shift.registration', fieldsDatas)
        except Exception as e:
            coop_logger.error("Set shift : %s, %s", str(e), str(data))
325
            if 'This partner is already registered on this Shift' in str(e) or 'sql_constraint' in str(e):
Administrator committed
326 327 328
                res = self.reopen_shift(data)
                if res:
                    st_r_id = True
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

        partner_id = int(data['idPartner'])
        res_decrement = False
        if st_r_id:
            # decrement makeups_to_do
            if data['is_makeup'] is True:
                try:
                    # Ok if makeups_to_do go under 0, until unselect_makeup is called,
                    # (this is needed when set_shift is called from change_shift)
                    res_decrement = self.o_api.update(
                        'res.partner',
                        [partner_id],
                        {"makeups_to_do": self.get_member_makeups_to_do(partner_id) - 1}
                    )
                except Exception as e:
                    coop_logger.error("Decrement makeups to do : %s, %s", str(e), str(data))

        return st_r_id, res_decrement
Etienne Freiss committed
347 348 349 350
    
    def affect_shift(self, data):
        """Affect shift to partner, his associate or both"""
        response = None
351 352 353
        # partner_id can be 'associated_people' one, which is never use as shift partner_id reference
        # So, let's first retrieved data about the res.partner involved
        cond = [['id', '=', int(data['idPartner'])]]
354
        fields = ['parent_id', 'suppleant_member_id']
355 356
        partner = self.o_api.search_read('res.partner', cond, fields, 1)
        if partner:
357
            if partner[0]['suppleant_member_id']:
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
                partner_id = partner[0]['parent_id'][0]
            else:
                partner_id = int(data['idPartner'])
            cond = [['partner_id', '=', partner_id],
                    ['id', '=', int(data['idShiftRegistration'])]]
            fields = ['id']
            try:
                # make sure there is coherence between shift.registration id and partner_id (to avoid forged request)
                shit_to_affect = self.o_api.search_read('shift.registration', cond, fields, 1)
                if (len(shit_to_affect) == 1):
                    shift_res = shit_to_affect[0]
                    fieldsDatas = { "associate_registered":data['affected_partner']}
                    response = self.o_api.update('shift.registration', [shift_res['id']],  fieldsDatas)
            except Exception as e:
                coop_logger.error("Model affect shift : %s", str(e))
        else:
            coop_logger.error("Model affect shift nobody found : %s", str(cond))
Etienne Freiss committed
375
        return response
Administrator committed
376

377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    def cancel_shift(self, idsReg, cancellation_origin='', cancellation_description='', counter_event_name=''):
        """Annule un shift. Si le shift est un rattrapage, ajoute 1 point au compteur."""
        fieldsDatas = {
            "cancellation_origin": cancellation_origin,
            "cancellation_description": cancellation_description,
            "counter_event_name": counter_event_name
        }
        return self.o_api.execute('shift.registration', 'cancel', idsReg, fieldsDatas)

    def unselect_makeup(self, idsReg, cancellation_origin='', cancellation_description=''):
        """Déchoisit un rattrapage."""
        fieldsDatas = {
            "cancellation_origin": cancellation_origin,
            "cancellation_description": cancellation_description,
        }
        return self.o_api.execute('shift.registration', 'unselect_makeup', idsReg, fieldsDatas)
Administrator committed
393 394 395 396 397 398 399

    def reopen_shift(self, data):
        """Use when a member select a shift he has canceled before"""
        response = None
        cond = [['partner_id', '=', int(data['idPartner'])],
                ['shift_id', '=', int(data['idShift'])],
                ['state', '=', 'cancel']]
400
        fields = ['id','origin']
Administrator committed
401 402 403 404 405
        try:
            canceled_res = self.o_api.search_read('shift.registration', cond, fields, 1)
            if (len(canceled_res) == 1):
                shift_res = canceled_res[0]
                fieldsDatas = { "related_shift_state":'open',
406 407
                                "state": 'open',
                                "is_makeup":data['is_makeup'],
408
                                "origin":canceled_res[0]['origin'] + ' reopened from memberspace'}
409 410 411 412 413 414 415 416 417

                #following code is required to properly set template_created.
                #TODO : factor with set_shift code
                shift_type = "standard"
                if data['shift_type'] == "ftop" or getattr(settings, 'USE_STANDARD_SHIFT', True) == False:
                    shift_type = "ftop"
                if (shift_type == "standard" and data['is_makeup'] is not True) or shift_type == "ftop":
                    fieldsDatas['template_created'] = 1  # It's not true but otherwise, presence add 1 standard point , which is not wanted
                else: #the else does not exist in set_shift but is mandatory here as template_created value in reopened registration can be any value
418
                    fieldsDatas["template_created"] = False
419

Administrator committed
420 421 422 423 424
                response = self.o_api.update('shift.registration', [shift_res['id']],  fieldsDatas)
        except Exception as e:
            coop_logger.error("Reopen shift : %s", str(e))
        return response

425
    def create_delay(self, data, duration=28, ext_name="Extension créée depuis l'espace membre"):
426 427 428
        """
        Create a delay for a member.
        If no duration is specified, a delay is by default 28 days from the given start_date.
Administrator committed
429

430
        If the partner already has a current extension: extend it by [duration] days.
Administrator committed
431 432 433
        Else, create a 28 days delay.

        Args:
434 435 436 437 438 439 440 441
            data
                idPartner: int
                start_date: string date at iso format (eg. "2019-11-19")
                    Date from which the delay end date is calculated
                (optionnal) extension_beginning: string date at iso format
                    If specified, will be the actual starting date of the extension.
                    Should be inferior than start_date.
                    (at creation only: odoo ignores delays if today's not inside)
442
            duration: nb of days
443
            ext_name: will be displayed in odoo extensions list
Administrator committed
444 445 446 447
        """
        action = 'create'

        # Get partner extension ids
448
        cond = [['id', '=', data['idPartner']]]
Administrator committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
        fields = ['extension_ids']
        partner_extensions = self.o_api.search_read('res.partner', cond, fields)
        response = False
        # If has extensions
        if 'extension_ids' in partner_extensions[0]:
            # Look for current extension: started before today and ends after
            current_extension = False
            for ext_id in partner_extensions[0]['extension_ids']:
                cond = [['id','=',ext_id]]
                extension = self.o_api.search_read('shift.extension', cond)
                extension = extension[0]

                if datetime.datetime.strptime(extension['date_start'], '%Y-%m-%d') <= datetime.datetime.now() and\
                        datetime.datetime.strptime(extension['date_stop'], '%Y-%m-%d') > datetime.datetime.now():
                    current_extension = extension
                    break

            # Has a current extension -> Update it
            if current_extension != False:
                action = 'update'

        # Update current extension
        if action == 'update':
            ext_date_stop = datetime.datetime.strptime(extension['date_stop'], '%Y-%m-%d').date()
473
            ext_new_date_stop = (ext_date_stop + datetime.timedelta(days=duration))
Administrator committed
474 475 476 477 478 479 480 481 482 483

            update_data = {
                'date_stop': ext_new_date_stop.isoformat()
            }

            response = self.o_api.update('shift.extension', current_extension['id'],  update_data)
        # Create the extension
        else:
            # Get the 'Extension' type id
            extension_types = self.o_api.search_read('shift.extension.type')
484
            ext_type_id = getattr(settings, 'EXTENSION_TYPE_ID', 1)
Administrator committed
485 486 487
            for val in extension_types:
                if val['name'] == 'Extension':
                    ext_type_id = val['id']
488
            
Administrator committed
489
            starting_date = datetime.datetime.strptime(data['start_date'], '%Y-%m-%d').date()
490
            ending_date = (starting_date + datetime.timedelta(days=duration))
Administrator committed
491 492 493 494 495 496 497 498 499

            if 'extension_beginning' in data:
                starting_date = datetime.datetime.strptime(data['extension_beginning'], '%Y-%m-%d').date()

            fields= {
                "partner_id":   data['idPartner'],
                "type_id":      ext_type_id,
                "date_start":   starting_date.isoformat(),
                "date_stop":    ending_date.isoformat(),
500
                "name":         ext_name
Administrator committed
501
            }
502
            
Administrator committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
            response = self.o_api.create('shift.extension', fields)

        return response

    @staticmethod
    def reset_members_positive_points():
        """
        Look for all the members with standard points > 0 when registered for more than a month and reset them to 0
        -> As an intern rule, members can't have more than 0 standard point (except during the first month)

        --- Called by a cron script
        """
        api = OdooAPI()

        # Get concerned members id and points
        lastmonth = (datetime.date.today() - datetime.timedelta(days=28)).isoformat()

        cond = [['is_member', '=', True],
                ['final_standard_point', '>', 0],
                ['create_date', '<', lastmonth]]
        fields = ['id', 'final_standard_point']
        members_data = api.search_read('res.partner', cond, fields)

        # For each, set points to 0
        res = True
        for member_data in members_data:
            try:
                fields = {
                    'name': 'RAZ des points positifs',
                    'shift_id': False,
                    'type': 'standard',
                    'partner_id': member_data['id'],
                    'point_qty': -int(member_data['final_standard_point'])
                }

                api.create('shift.counter.event', fields)
            except:
                res = False

        return res

    def get_test(self, odooModel, cond, fieldsDatas):
        return self.o_api.search_read(odooModel, cond, fieldsDatas, limit = 1000)
546

547
    def get_member_makeups_to_do(self, partner_id):
548 549
        cond = [['id', '=', partner_id]]
        fields = ['makeups_to_do']
550 551
        return self.o_api.search_read('res.partner', cond, fields)[0]["makeups_to_do"]

552 553
    def member_can_have_delay(self, partner_id):
        """ Can a member have a delay? """
554 555 556 557 558 559
        answer = False
        try:
            answer = self.o_api.execute('res.partner', 'can_have_extension', [partner_id])
        except Exception as e:
            coop_logger.error("member_can_have_delay : %s", str(e))
        return answer
560 561 562

    def update_counter_event(self, fields):
        """ Add/remove points """
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
        return self.o_api.create('shift.counter.event', fields)


class CagetteServices(models.Model):
    """Class to handle cagette Odoo services."""

    @staticmethod
    def get_all_shift_templates():
        """Return all recorded shift templates recorded in Odoo database."""
        creneaux = {}
        try:
            api = OdooAPI()
            f = ['name', 'week_number', 'start_datetime_tz', 'end_datetime_tz',
                 'seats_reserved', 'shift_type_id', 'seats_max',
                 'seats_available','registration_qty']
            c = [['active', '=', True]]
            shift_templates = api.search_read('shift.template', c, f)

            # Get count of active registrations for each shift template
            # shift_templates_active_count = api.execute('lacagette_shifts', 'get_active_shifts', [])
            # With LGDS tests, seats_reserved reflects better what's shown in Odoo ...

585
            title = re.compile(r"^(\w{1})(\w{2,3})\.? - (\d{2}:\d{2}) ?-? ?(\w*)")
586 587 588 589 590 591 592 593 594 595 596 597
            for l in shift_templates:
                line = {}
                end = time.strptime(l['end_datetime_tz'], "%Y-%m-%d %H:%M:%S")
                end_min = str(end.tm_min)
                if end_min == '0':
                    end_min = '00'
                line['end'] = str(end.tm_hour) + ':' + end_min
                line['max'] = l['seats_max']
                line['reserved'] = l['registration_qty']
                line['week'] = l['week_number']
                line['id'] = l['id']
                line['type'] = l['shift_type_id'][0]
598
                line['name'] = l['name']
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
                t_elts = title.search(l['name'])
                if t_elts:
                    line['day'] = t_elts.group(2)
                    line['begin'] = t_elts.group(3)
                    line['place'] = t_elts.group(4)

                creneaux[str(l['id'])] = {'data': line}
        except Exception as e:
            coop_logger.error(str(e))
        return creneaux

    @staticmethod
    def get_shift_templates_next_shift(id):
        """Retrieve next shift template shift."""
        api = OdooAPI()
        c = [['shift_template_id.id', '=', id],
             ['date_begin', '>=', datetime.datetime.now().isoformat()]]
        f = ['date_begin']
        # c = [['id','=',2149]]
        shift = {}
        res = api.search_read('shift.shift', c, f, 1, 0, 'date_begin ASC')
        if (res and res[0]):
            locale.setlocale(locale.LC_ALL, 'fr_FR.utf8')
            local_tz = pytz.timezone('Europe/Paris')
            date, t = res[0]['date_begin'].split(' ')
            year, month, day = date.split('-')
            start = datetime.datetime(int(year), int(month), int(day),
                                      0, 0, 0, tzinfo=pytz.utc)
            start_date = start.astimezone(local_tz)
            shift['date_begin'] = start_date.strftime("%A %d %B %Y")
        return shift

    @staticmethod
    def get_services_at_time(time, tz_offset, with_members=True):
        """Retrieve present services with members linked."""

        default_acceptable_minutes_after_shift_begins = getattr(settings, 'ACCEPTABLE_ENTRANCE_MINUTES_AFTER_SHIFT_BEGINS', 15)
        minutes_before_shift_starts_delay = getattr(settings, 'ACCEPTABLE_ENTRANCE_MINUTES_BEFORE_SHIFT', 15)
        minutes_after_shift_starts_delay = default_acceptable_minutes_after_shift_begins
        late_mode = getattr(settings, 'ENTRANCE_WITH_LATE_MODE', False)
        max_duration = getattr(settings, 'MAX_DURATION', 180)
        if late_mode is True:
            minutes_after_shift_starts_delay = getattr(settings, 'ENTRANCE_VALIDATION_GRACE_DELAY', 60)
        api = OdooAPI()
        now = dateutil.parser.parse(time) - datetime.timedelta(minutes=tz_offset)
        start1 = now + datetime.timedelta(minutes=minutes_before_shift_starts_delay)
        start2 = now - datetime.timedelta(minutes=minutes_after_shift_starts_delay)
        end = start1 + datetime.timedelta(minutes=max_duration)
        cond = [['date_end_tz', '<=', end.isoformat()]]
        cond.append('|')
        cond.append(['date_begin_tz', '>=', start1.isoformat()])
        cond.append(['date_begin_tz', '>=', start2.isoformat()])
        fields = ['name', 'week_number', 'registration_ids',
                  'standard_registration_ids',
                  'shift_template_id', 'shift_ticket_ids',
                  'date_begin_tz', 'date_end_tz', 'state']
        services = api.search_read('shift.shift', cond, fields,order ="date_begin_tz ASC")
        for s in services:
            if (len(s['registration_ids']) > 0):
                if late_mode is True:
                    s['late'] = (
                                 now.replace(tzinfo=None)
                                 -
                                 dateutil.parser.parse(s['date_begin_tz']).replace(tzinfo=None)
                                 ).total_seconds() / 60 > default_acceptable_minutes_after_shift_begins
                if with_members is True:
                    cond = [['id', 'in', s['registration_ids']], ['state', 'not in', ['cancel', 'waiting', 'draft']]]
                    fields = ['partner_id', 'shift_type', 'state', 'is_late', 'associate_registered']
                    members = api.search_read('shift.registration', cond, fields)
                    s['members'] = sorted(members, key=lambda x: x['partner_id'][0])
                    if len(s['members']) > 0:
                        # search for associated people linked to these members
                        mids = []
                        for m in s['members']:
                            mids.append(m['partner_id'][0])
                        cond = [['parent_id', 'in', mids]]
675 676 677
                        fields = ['id', 'parent_id', 'name', 'barcode_base', 'suppleant_member_id']
                        attached = api.search_read('res.partner', cond, fields)
                        associated = [x for x in attached if x['suppleant_member_id']]
678 679 680 681 682 683 684 685 686 687 688 689 690

                        if len(associated) > 0:
                            for m in s['members']:
                                for a in associated:
                                    if int(a['parent_id'][0]) == int(m['partner_id'][0]):
                                        m['partner_name'] = m['partner_id'][1]
                                        m['partner_id'][1] += ' en binôme avec ' + a['name']
                                        m['associate_name'] = str(a['barcode_base']) + ' - ' + a['name']
                                        

        return services

    @staticmethod
691
    def registration_done(request, registration_id, overrided_date="", typeAction=""):
692
        """Equivalent to click present in presence form."""
693
        has_right_to_overriden_entrance_date = Verification.has_right_to_overriden_entrance_date(request)
694

695 696 697
        if (len(overrided_date) == 0 or has_right_to_overriden_entrance_date):
            api = OdooAPI()
            f = {'state': 'done'}
698

699 700 701 702 703
            if(typeAction != "normal" and typeAction != ""):
                f['associate_registered'] = typeAction

            if typeAction == "both":
                f['should_increment_extra_shift_done'] = True
704
            else:
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
                f['should_increment_extra_shift_done'] = False
                
            late_mode = getattr(settings, 'ENTRANCE_WITH_LATE_MODE', False)
            if late_mode is True:
                # services = CagetteServices.get_services_at_time('14:28',0, with_members=False)
                if len(overrided_date) > 0 and getattr(settings, 'APP_ENV', "prod") != "prod":
                    now = overrided_date
                else:
                    local_tz = pytz.timezone('Europe/Paris')
                    now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(local_tz).strftime("%H:%MZ")
                # coop_logger.info("Maintenant = %s (overrided %s) %s", now, overrided_date)
                services = CagetteServices.get_services_at_time(now, 0, with_members=False)
                if len(services) > 0:
                    # Notice : Despite is_late is defined as boolean in Odoo, 0 or 1 is needed for api call
                    is_late = 0
                    if services[0]['late'] is True:
                        is_late = 1
                    f['is_late'] = is_late
                else:
                    return False
            return api.update('shift.registration', [int(registration_id)], f)
        else:
            return False
728 729 730 731 732

    @staticmethod
    def reopen_registration(registration_id, overrided_date=""):
        api = OdooAPI()
        f = {'state': 'open'}
733 734 735 736 737 738 739
        try:
            cond = [['id', '=', int(registration_id)]]
            fields = ['partner_id']
            res = api.search_read('shift.registration', cond, fields)
            coop_logger.info("On invalide la présence de  %s ", res[0]['partner_id'][1])
        except Exception as e:
            coop_logger.error("On invalide shift_registration  %s (erreur : %s)", str(registration_id), str(e))
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
        return api.update('shift.registration', [int(registration_id)], f)

    @staticmethod
    def record_rattrapage(mid, sid, stid, typeAction):
        """Add a shift registration for member mid.

        (shift sid, shift ticket stid)
        Once created, shift presence is confirmed.
        """
        api = OdooAPI()
        fields = {
            "partner_id": mid,
            "shift_id": sid,
            "shift_ticket_id": stid,
            "shift_type": "standard",  # ou ftop -> voir condition
            "related_shift_state": 'confirm',
            "state": 'open'}
        reg_id = api.create('shift.registration', fields)

        f = {'state': 'done'}
        if(typeAction != "normal" and typeAction != ""):
            f['associate_registered'] = typeAction
        if typeAction == "both":
            f['should_increment_extra_shift_done'] = True
        else:
            f['should_increment_extra_shift_done'] = False

        return api.update('shift.registration', [int(reg_id)], f)

    @staticmethod
    def record_absences(date):
        """Called by cron script."""
        import dateutil.parser
        if len(date) > 0:
            now = dateutil.parser.parse(date)
        else:
            now = datetime.datetime.now()
        # now = dateutil.parser.parse('2020-09-15T15:00:00Z')
        date_24h_before = now - datetime.timedelta(hours=24)
        # let authorized people time to set presence for those who came in late
        end_date = now - datetime.timedelta(hours=2)
        api = OdooAPI()

        # Let's start by adding an extra shift to associated member who came together
        cond = [['date_begin', '>=', date_24h_before.isoformat()],
                ['date_begin', '<=', end_date.isoformat()],
                ['state', '=', 'done'], 
                ['associate_registered', '=', 'both'],
                ['should_increment_extra_shift_done', '=', True]]
        fields = ['id', 'state', 'partner_id', 'date_begin']
        res = api.search_read('shift.registration', cond, fields)

        extra_shift_done_incremented_srids = []  # shift registration ids
        for r in res:
            cond = [['id', '=', r['partner_id'][0]]]
            fields = ['id','extra_shift_done']
            res_partner = api.search_read('res.partner', cond, fields)

            f = {'extra_shift_done': res_partner[0]['extra_shift_done'] + 1 }
            api.update('res.partner', [r['partner_id'][0]], f)

            extra_shift_done_incremented_srids.append(int(r['id']))

        # Make sure the counter isn't incremented twice
        f = {'should_increment_extra_shift_done': False}
        api.update('shift.registration', extra_shift_done_incremented_srids, f)

        absence_status = 'excused'
        res_c = api.search_read('ir.config_parameter',
                                [['key', '=', 'lacagette_membership.absence_status']],
                                ['value'])
        if len(res_c) == 1:
            absence_status = res_c[0]['value']
        cond = [['date_begin', '>=', date_24h_before.isoformat()],
                ['date_begin', '<=', end_date.isoformat()],
                ['state', '=', 'open']]
        fields = ['state', 'partner_id', 'date_begin', 'shift_id']
        res = api.search_read('shift.registration', cond, fields)
        partner_ids = []
        shift_ids = []
        for r in res:
            partner_ids.append(int(r['partner_id'][0]))
            shift_id = int(r['shift_id'][0])
            if shift_id not in shift_ids:   
                shift_ids.append(shift_id)
825 826 827 828
        #TODO : improve name of following method
        canceled_reg_ids, excluded_partner, ids = CagetteServices.fetch_registrations_infos_excluding_exempted_people(
            api, partner_ids, res
        )
829
        f = {'state': absence_status, 'date_closed': now.isoformat()}
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
        update_shift_reg_result = {'update': False, 'reg_shift': res, 'errors': []}
        individual_update_result = {}
        for mysregid in ids:
            try:
                individual_update_result[str(mysregid)] = api.update('shift.registration', [mysregid], f)
            except Exception as e:
                coop_logger.error("Error on updating shift.registration ids %s :   %s", str(mysregid), str(e))
        update_shift_reg_result['update'] = individual_update_result

        # With new way of overriding awesomefoodcoop status management, the following line is no more useful
        # update_shift_reg_result['process_status_res'] = api.execute('res.partner','run_process_target_status', [])
        # change shift state by triggering button_done method for all related shifts
        if len(canceled_reg_ids) > 0:
            f = {'state': 'cancel', 'date_closed': now.isoformat()}
            api.update('shift.registration', canceled_reg_ids, f)
        for sid in shift_ids:
            try:
                api.execute('shift.shift', 'button_done', sid)
            except Exception as e:
                marshal_none_error = 'cannot marshal None unless allow_none is enabled'
                if not (marshal_none_error in str(e)):
                    update_shift_reg_result['errors'].append({'shift_id': sid, 'msg' :str(e)})
852 853 854

        return update_shift_reg_result

855 856 857 858 859 860
    @staticmethod
    def fetch_registrations_infos_excluding_exempted_people(api, partner_ids, res):
        #TODO : document this method
        ids = []
        excluded_partner = []
        canceled_reg_ids = []  # for exempted people
861
        res_exempted = shifts.fonctions.get_exempted_ids_from(api, partner_ids)
862 863 864 865 866 867 868 869 870 871 872 873
        for r in res_exempted:
            excluded_partner.append(int(r['id']))
        for r in res:
            if not (int(r['partner_id'][0]) in excluded_partner):
                d_begin = r['date_begin']
                (d, h) = d_begin.split(' ')
                (_h, _m, _s) = h.split(':')
                if int(_h) < 21:
                    ids.append(int(r['id']))
            else:
                canceled_reg_ids.append(int(r['id']))
        return canceled_reg_ids, excluded_partner, ids
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936

    @staticmethod
    def close_ftop_service():
        """Called by cron script"""
        # Retrieve the latest past FTOP service
        import dateutil.parser
        now = datetime.datetime.now()
        # now = dateutil.parser.parse('2019-10-20T00:00:00Z')
        cond = [['shift_type_id','=', 2],['date_end', '<=', now.isoformat()],['state','=', 'draft'], ['active', '=', True]]
        fields = ['name']
        api = OdooAPI()
        res = api.search_read('shift.shift', cond, fields,order ="date_end ASC", limit=1)
        # return res[0]['id']
        result = {}
        if res and len(res) > 0:
            result['service_found'] = True
            # Exceptions are due to the fact API returns None whereas the action is really done !...
            marshal_none_error = 'cannot marshal None unless allow_none is enabled'
            actual_errors = 0
            try:
                api.execute('shift.shift', 'button_confirm', [res[0]['id']])
            except Exception as e:
                if not (marshal_none_error in str(e)):
                    result['exeption_confirm'] = str(e)
                    actual_errors += 1
            try:
                api.execute('shift.shift', 'button_makeupok', [res[0]['id']])
            except Exception as e:
                if not (marshal_none_error in str(e)):
                    result['exeption_makeupok'] = str(e)
                    actual_errors += 1
            try:
                api.execute('shift.shift', 'button_done', [res[0]['id']])
            except Exception as e:
                if not (marshal_none_error in str(e)):
                    result['exeption_done'] = str(e)
                    actual_errors += 1
            if actual_errors == 0:
                result['done'] = True
            else:
                result['done'] = False
            result['actual_errors'] = actual_errors
        else:
            result['service_found'] = False
        return result

    @staticmethod
    def get_committees_shift_id():
        shift_id = None
        try:
            api = OdooAPI()
            res = api.search_read('ir.config_parameter',
                                  [['key','=', 'lacagette_membership.committees_shift_id']],
                                  ['value'])
            if len(res) > 0:
                try:
                    shift_id = int(res[0]['value'])
                except:
                    pass
        except:
            pass
        return shift_id

937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
    @staticmethod
    def get_exemptions_shift_id():
        shift_id = None
        try:
            api = OdooAPI()
            res = api.search_read('ir.config_parameter',
                                  [['key','=', 'lacagette_exemptions.exemptions_shift_id']],
                                  ['value'])
            if len(res) > 0:
                try:
                    shift_id = int(res[0]['value'])
                except:
                    pass
        except:
            pass
        return shift_id

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
    @staticmethod
    def get_first_ftop_shift_id():
        shift_id = None
        try:
            api = OdooAPI()
            res = api.search_read('shift.template',
                                  [['shift_type_id','=', 2]],
                                  ['id', 'registration_qty'])

            # Get the ftop shift template with the max registrations: most likely the one in use
            ftop_shift = {'id': None, 'registration_qty': 0}
            for shift_reg in res:
                if shift_reg["registration_qty"] > ftop_shift["registration_qty"]:
                    ftop_shift = shift_reg

            try:
                shift_id = int(ftop_shift['id'])
            except:
                pass
        except:
            pass
        return shift_id

    @staticmethod
    def easy_validate_shift_presence(coop_id):
        """Add a presence point if the request is valid."""
        res = {}
        try:
            committees_shift_id = CagetteServices.get_committees_shift_id()
            api = OdooAPI()
            # let verify coop_id is corresponding to a ftop subscriber
            cond = [['id', '=', coop_id]]
            fields = ['tmpl_reg_line_ids']
            coop = api.search_read('res.partner', cond, fields)
            if coop:
                if len(coop[0]['tmpl_reg_line_ids']) > 0 :
                    cond = [['id', '=', coop[0]['tmpl_reg_line_ids'][0]]]
                    fields = ['shift_template_id']
                    shift_templ_res = api.search_read('shift.template.registration.line', cond, fields)
                    if (len(shift_templ_res) > 0
                        and
                        shift_templ_res[0]['shift_template_id'][0] == committees_shift_id):
                        ok_for_adding_pt = False
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
                        mininum_seconds_interval = getattr(settings, 'MINIMUM_SECONDS_BETWEEN_TWO_COMITEE_VALIDATION', 3600 * 24)
                        evt_name = getattr(settings, 'ENTRANCE_ADD_PT_EVENT_NAME', 'Validation service comité')

                        if mininum_seconds_interval > 0:
                            #  A constraint has been set to prevent from adding more than 1 point during a time period
                            #  Let's find out when was the last time a "special point" has been addes
                            c = [['partner_id', '=', coop_id], ['name', '=', evt_name]]
                            f = ['create_date']
                            last_point_mvts = api.search_read('shift.counter.event', c, f,
                                                              order ="create_date DESC", limit=1)
                            
                            if len(last_point_mvts):
                                now = datetime.datetime.now()
                                past = datetime.datetime. strptime(last_point_mvts[0]['create_date'],
                                                                   '%Y-%m-%d %H:%M:%S')
                                if (now - past).total_seconds() >= mininum_seconds_interval:
                                    ok_for_adding_pt = True
                            else:
1015 1016
                                ok_for_adding_pt = True
                        else:
1017
                            #  mininum_seconds_interval is 0 : Point can we added without any condition
1018
                            ok_for_adding_pt = True
1019

1020 1021 1022
                        if ok_for_adding_pt is True:
                            res['evt_id'] = CagetteMember(coop_id).add_pts('ftop', 1, evt_name)
                        else:
1023
                            res['error'] = "Un point a déjà été ajouté trop récemment."
1024
                    else:
1025
                        res['error'] = "Vous n'avez pas le droit d'ajouter un point."
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
                else:
                    res['error'] = "Unregistred coop"
            else:
                res['error'] = "Invalid coop id"
        except Exception as e:
            coop_logger.error("easy_validate_shift_presence :  %s %s", str(coop_id), str(e))
        return res

class CagetteService(models.Model):
    """Class to handle cagette Odoo service."""

    def __init__(self, id):
        """Init with odoo id."""
        self.id = int(id)
        self.o_api = OdooAPI()

    def _process_associated_people_extra_shift_done(self):
        cond = [['shift_id', '=', self.id],
                ['state', '=', 'done'], 
                ['associate_registered', '=', 'both'],
                ['should_increment_extra_shift_done', '=', True]]
        fields = ['id', 'state', 'partner_id', 'date_begin']
        res = self.o_api.search_read('shift.registration', cond, fields)
        extra_shift_done_incremented_srids = []  # shift registration ids
        for r in res:
            cond = [['id', '=', r['partner_id'][0]]]
            fields = ['id','extra_shift_done']
            res_partner = self.o_api.search_read('res.partner', cond, fields)

            f = {'extra_shift_done': res_partner[0]['extra_shift_done'] + 1 }
            self.o_api.update('res.partner', [r['partner_id'][0]], f)

            extra_shift_done_incremented_srids.append(int(r['id']))

        # Make sure the counter isn't incremented twice
        f = {'should_increment_extra_shift_done': False}
        self.o_api.update('shift.registration', extra_shift_done_incremented_srids, f)

    def _process_related_shift_registrations(self):
        now = datetime.datetime.now()
        absence_status = 'excused'
        res_c = self.o_api.search_read('ir.config_parameter',
                                [['key', '=', 'lacagette_membership.absence_status']],
                                ['value'])
        if len(res_c) == 1:
            absence_status = res_c[0]['value']
        cond = [['shift_id', '=', self.id],
                ['state', '=', 'open']]
        fields = ['state', 'partner_id', 'date_begin']
        res = self.o_api.search_read('shift.registration', cond, fields)
        partner_ids = []
        for r in res:
            partner_ids.append(int(r['partner_id'][0]))
1079 1080 1081 1082
        #TODO : improve name of following method
        canceled_reg_ids, excluded_partner, ids = CagetteServices.fetch_registrations_infos_excluding_exempted_people(
            api, partner_ids, res
        )
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
        f = {'state': absence_status, 'date_closed': now.isoformat()}
        update_shift_reg_result = {'update': self.o_api.update('shift.registration', ids, f), 'reg_shift': res, 'errors': []}
        if update_shift_reg_result['update'] is True:
            update_shift_reg_result['process_status_res'] = self.o_api.execute('res.partner','run_process_target_status', [])
            # change shift state by triggering button_done method for all related shifts
            if len(canceled_reg_ids) > 0:
                f = {'state': 'cancel', 'date_closed': now.isoformat()}
                self.o_api.update('shift.registration', canceled_reg_ids, f)
           
            try:
                self.o_api.execute('shift.shift', 'button_done', self.id)
            except Exception as e:
                marshal_none_error = 'cannot marshal None unless allow_none is enabled'
                if not (marshal_none_error in str(e)):
                    update_shift_reg_result['errors'].append({'shift_id': self.id, 'msg' :str(e)})

        return update_shift_reg_result

    def record_absences(self, request):
        """Can only been executed if an Odoo user is beeing connected."""
        res = {}
        try:
            if CagetteUser.are_credentials_ok(request) is True:
                self._process_associated_people_extra_shift_done()
                res = self._process_related_shift_registrations()
            else:
                res['error'] = 'Forbidden'
        except Exception as e:
            coop_logger.error("CagetteService.record_absences :  %s %s", str(self.id), str(e))
            res['error'] = str(e)
        return res