1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
# Copyright (C) 2016-Today: La Louve (<http://www.lalouve.fr/>)
# @author: La Louve
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html
from openerp import _, api, fields, models
import logging
_logger = logging.getLogger(__name__)
class ShiftTemplateRegistrationLine(models.Model):
_inherit = 'shift.template.registration.line'
@api.multi
def update_partner_shift_type(self):
for record in self:
ticket_shift_type = record.registration_id.shift_ticket_id.shift_type
partner = record.registration_id.partner_id
partner_shift_type = partner.shift_type
# only update if current shift_type of partner != shift_type of ticket
if ticket_shift_type != partner_shift_type:
partner.shift_type = ticket_shift_type
@api.multi
def check_update_partner_shift_type(self):
today = fields.Date.context_today(self)
for record in self:
if record.date_begin <= today:
if record.date_end and record.date_end >= today:
record.update_partner_shift_type()
elif not record.date_end:
record.update_partner_shift_type()
@api.multi
def write(self, vals):
res = super(ShiftTemplateRegistrationLine, self).write(vals)
self.check_update_partner_shift_type()
return res
@api.model
def create(self, vals):
res = super(ShiftTemplateRegistrationLine, self).create(vals)
res.check_update_partner_shift_type()
return res
@api.model
def cron_update_partner_shift_type(self):
today = fields.Date.context_today(self)
template_env = self.env['shift.template.registration.line']
# Use SQL here to filter only record
# have partner.shift_type != registration.line.shift_type
# to get high performance
SQL = """
SELECT STRL.id
FROM shift_template_registration_line STRL
LEFT JOIN res_partner RP ON STRL.partner_id=RP.id
LEFT JOIN shift_template_registration STR
ON STRL.registration_id=STR.id
LEFT JOIN shift_template_ticket STT
ON STR.shift_ticket_id=STT.id
WHERE
(STRL.date_begin <= '%s')
AND (STRL.date_end >= '%s' OR STRL.date_end is NULL)
AND RP.shift_type != STT.shift_type
"""
SQL = SQL % (today, today)
_logger.info(">>>>> START cron_update_partner_shift_type:")
self.env.cr.execute(SQL)
line_ids = [x[0] for x in self.env.cr.fetchall()]
_logger.debug(
">>>>> cron_update_partner_shift_type - total: %s - line ids: %s",
len(line_ids), line_ids
)
# update data by normal ORM method
tmpl_registation_lines = template_env.search([('id', 'in', line_ids)])
tmpl_registation_lines.update_partner_shift_type()
_logger.info(">>>>> STOP cron_update_partner_shift_type:")