Skip to content

Commit

Permalink
[FIX] handling tags and readers, updare access register
Browse files Browse the repository at this point in the history
  • Loading branch information
archetipo committed Nov 14, 2024
1 parent 688569b commit dc8f0a2
Show file tree
Hide file tree
Showing 22 changed files with 236 additions and 102 deletions.
12 changes: 9 additions & 3 deletions controllo_accessi_inrim_app/data/ir_config_parameter_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@
<field name="key">default_ca.internal_people_types</field>
<field name="value">
["dipendentit.d._employeetype","assegni_employeetype","dipendentit.i._employeetype","tesisti_employeetype","associazionitipoa_tipopersonale","associazionitipob_tipopersonale",
"associazionitipoc_tipopersonale",
"borse_employeetype","dottorandi_employeetype",
"stage_employeetype"]
"associazionitipoc_tipopersonale", "borse_employeetype","dottorandi_employeetype", "stage_employeetype"]
</field>
</record>

<record id="inrim_ir_config_parameter_default_payroll_types"
model="ir.config_parameter">
<field name="key">inrim_payroll_types</field>
<field name="value">
["dipendentit.d._employeetype","assegni_employeetype","dipendentit.i._employeetype"]
</field>
</record>

Expand Down
42 changes: 42 additions & 0 deletions controllo_accessi_inrim_app/models/ca_anag_registro_accesso.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytz
from odoo import models, fields

_tzs = [(tz, tz) for tz in sorted(pytz.all_timezones,
key=lambda tz: tz if not tz.startswith(
'Etc/') else '_')]


def _tz_get(self):
return _tzs


class CaAnagRegistroAccesso(models.Model):
_inherit = 'ca.anag_registro_accesso'

codice_lettore_grum = fields.Integer(
string='Codice Lettore GRUM',
)
work_id_number = fiels.Char(string='ID Number')
state = fields.Selection([
('to_sync', 'To Sync'),
('sync_done', 'Sync Done'),
('sync_error', 'Sync Error'),
], string="Sync State", readonly=True)

def aggiungi_riga_accesso(
self, ca_punto_accesso_id,
ca_tag_persona_id, datetime_event, type='manual', access_allowed=True,
tz=""
):
res = super().aggiungi_riga_accesso(
ca_punto_accesso_id, ca_tag_persona_id, datetime_event, type=type,
access_allowed=access_allowed, tz=tz)
todo = {}
if res and ca_punto_accesso_id.typology == 'stamping' and res.access_allowed:
todo['codice_lettore_grum'] = ca_punto_accesso_id.codice_lettore_grum
winfo = ca_tag_persona_id.ca_persona_id.get_current_winfo()
if winfo.ca_work_info_type_id.structured:
todo['work_id_number'] = winfo.work_id_number
todo['state'] = 'to_sync'
res.write(todo)
return res
7 changes: 4 additions & 3 deletions controllo_accessi_inrim_app/models/ca_persona.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def _cron_people_get_addressbook(self):

def update_work_info_type(self, data):
logger.info("Update tipo persona work_info_type")
ext_company = 'ditteesterne_tipopersonale'
ext_entity = 'entiesterni_tipopersonale'
payrolls = json.loads(self.env['ir.config_parameter'].sudo().get_param(
'inrim_payroll_types'))
with self.env.cr.savepoint():
try:
for dt in data:
Expand All @@ -67,11 +67,12 @@ def update_work_info_type(self, data):
vals = {
'name': dt['name'],
'code': dt['code'],
'structured': dt['code'] not in [ext_company, ext_entity]
'structured': dt['code'] not in payrolls
}
res = self.env['ca.work_info_type'].create(vals)
else:
work_info_type_id.name = dt['name']
work_info_type_id.structured = dt['code'] not in payrolls
except Exception as e:
logger.error(f"Error: {e}", exc_info=True)

Expand Down
23 changes: 23 additions & 0 deletions controllo_accessi_inrim_app/views/ca_registro_accesso_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="ca_anag_registro_accesso_view_tree_inherit" model="ir.ui.view">
<field name="name">ca.registro_accesso.view.tree.inherit</field>
<field name="model">ca.anag_registro_accesso</field>
<field name="inherit_id"
ref="inrim_controllo_accessi.ca_anag_registro_accesso_view_tree"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='ca_punto_accesso_id']"
position="after">
<field name="codice_lettore_grum"/>
</xpath>
<xpath expr="//field[@name='person_display_name']"
position="after">
<field name="work_id_number"/>
</xpath>
<xpath expr="//field[@name='system_error']"
position="after">
<field name="state"/>
</xpath>
</field>
</record>
</odoo>
15 changes: 14 additions & 1 deletion inrim_anagrafiche/data/ir.cron.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</record>

<record id="ir_cron_check_tag_validity" model="ir.cron">
<field name="name">INRIM: Check Tag Validity</field>
<field name="name">INRIM: Check Tag Persona Validity</field>
<field name="model_id" ref="model_ca_tag_persona"/>
<field name="type">ir.action.server</field>
<field name="code">model._cron_check_validity_tag()</field>
Expand All @@ -27,5 +27,18 @@
<field name="active">True</field>
<field name="interval_number">5</field>
</record>

<record id="ir_cron_check_workinfo_validity" model="ir.cron">
<field name="name">INRIM: Check Work Info Validity</field>
<field name="model_id" ref="model_ca_work_info"/>
<field name="type">ir.action.server</field>
<field name="code">model._cron_check_validity_winfo()</field>
<field name="interval_type">hours</field>
<field name="interval_number">12</field>
<field name="numbercall">-1</field>
<field name="priority">5</field>
<field name="doall">False</field>
<field name="active">True</field>
</record>
</data>
</odoo>
2 changes: 1 addition & 1 deletion inrim_anagrafiche/models/ca_persona.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CaPersona(models.Model):
vat = fields.Char()
type_ids = fields.Many2many('ca.tipo_persona')
freshman = fields.Char(groups="controllo_accessi.ca_gdpr")
work_id_number = fields.Char(string="A.C. ID Numeber",
work_id_number = fields.Char(string="A.C. ID Number",
groups="controllo_accessi.ca_gdpr")
nationality = fields.Many2one('res.country', groups="controllo_accessi.ca_gdpr")
birth_date = fields.Date(groups="controllo_accessi.ca_gdpr")
Expand Down
4 changes: 1 addition & 3 deletions inrim_anagrafiche/models/ca_tag_persona.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ def check_update_record_by_date_valididty(self):
elif self.date_start > now:
self.ca_tag_id.in_use = True
self.state = 'scheduled'
else:
self.ca_tag_id.in_use = False
self.state = 'returned'


def check_update_by_date_valididty(self):
for tag_persona in self.search([]):
Expand Down
8 changes: 4 additions & 4 deletions inrim_anagrafiche/models/ca_work_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ def rest_get_record(self):
'name': self.name,
'code': self.code,
'description': self.description,
'structured': self.structured,
}
return vals

def rest_eval_body(self, body):
body, msg = super().rest_eval_body(
body, [
'name', 'code'
'name', 'code', 'structured'
])
return body, msg

Expand All @@ -86,8 +87,7 @@ class CaWorkInfo(models.Model):
ca_persona_id = fields.Many2one(
'ca.persona', required=True)

work_id_number = fields.Char(
string="A.C. ID Numeber", groups="controllo_accessi.ca_gdpr")
work_id_number = fields.Char(string="ID Number")

ca_work_info_type_id = fields.Many2one(
'ca.work_info_type', ondelete='cascade')
Expand Down Expand Up @@ -126,7 +126,7 @@ def check_update_state(self):
self.state = 'expired'

def check_update_by_date_valididty(self):
for winfo_persona in self.search([]):
for winfo_persona in self.env['ca.work_info'].search([]):
if winfo_persona:
winfo_persona.check_update_state()

Expand Down
2 changes: 1 addition & 1 deletion inrim_anagrafiche/views/ca_persona_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
domain="[('type_ids', 'ilike', 'interno')]"/>
<filter string="External" name="external"
domain="[('type_ids', 'ilike', 'esterno')]"/>
<field name="type_ids"/>
<field name="display_name"/>
<field name="fiscalcode"/>
<field name="type_ids"/>
</search>
</field>
</record>
Expand Down
4 changes: 3 additions & 1 deletion inrim_anagrafiche/views/ca_tag_persona_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<field name="date_start"/>
<field name="date_end"/>
<field name="temp"/>
<field name="state" readonly="1"/>
<field name="state"/>
</tree>
</field>
</record>
Expand All @@ -41,6 +41,8 @@
<group>
<group>
<field name="token" invisible="1"/>
<field name="state" readonly="1"
widget="statusbar"/>
<field name="ca_persona_id"
options="{'no_create': True}"/>
<field name="available_tags_ids" invisible="1"/>
Expand Down
2 changes: 1 addition & 1 deletion inrim_controllo_accessi/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
'wizard/ca_aggiungi_movimento_accesso_views.xml',
'wizard/ca_registra_ospite_views.xml',
'views/ca_tag_lettore_views.xml',
'views/ca_punto_accesso_persona_views.xml',
'views/ca_lettore_persona_views.xml',
'views/punto_accesso_category_view.xml',
'views/ca_punto_accesso_views.xml',
'views/ca_persona_view.xml',
Expand Down
5 changes: 2 additions & 3 deletions inrim_controllo_accessi/models/ca_anag_registro_accesso.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import pytz
from odoo import models, fields

_tzs = [(tz, tz) for tz in sorted(pytz.all_timezones,
key=lambda tz: tz if not tz.startswith(
'Etc/') else '_')]
_tzs = [(tz, tz) for tz in sorted(
pytz.all_timezones, key=lambda tz: tz if not tz.startswith('Etc/') else '_')]


def _tz_get(self):
Expand Down
60 changes: 35 additions & 25 deletions inrim_controllo_accessi/models/ca_lettore_persona.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ class CaLettorePersona(models.Model):
ca_persona_id = fields.Many2one(
related="ca_tag_persona.ca_persona_id", store=True, readonly=True)

date_start = fields.Datetime(
related="ca_tag_persona.date_start", readonly=True, store=True)
date_end = fields.Datetime(
related="ca_tag_persona.date_end", readonly=True, store=True)

state = fields.Selection([
('active', 'Active'),
('expired', 'Expired'),
('scheduled', 'Scheduled')
], readonly=True)

date = fields.Date(readonly=True)
date_start = fields.Datetime(
related="ca_tag_persona.date_start", readonly=True, store=True)
date_end = fields.Datetime(
related="ca_tag_persona.date_end", readonly=True, store=True)

active = fields.Boolean(default=True)

@api.constrains(
Expand All @@ -44,55 +43,48 @@ def _check_unique(self):
('ca_tag_persona', '=', record.ca_tag_persona.id),
('date_start', '<=', record.date_start),
('date_end', '>=', record.date_end),
('state', '=', 'active')
('state', 'not in', ['expired'])
])
if punto_accesso_persona_id:
raise UserError(
_('Puo’ esistere solo una configurazione per tag lettore, tag persona, data, in stato attivo'))

@api.onchange('date_start', 'date_end')
def _compute_expired(self):
for record in self:
record.check_update_state()

def elabora_persone(self, lettore_id):
vals = []
ca_tag_lettore_ids = self.env['ca.tag_lettore'].search([
('ca_lettore_id', '=', lettore_id.id), ('expired', '=', False),
('ca_lettore_id', '=', lettore_id.id), ('state', 'not in', ['expired']),
])

if ca_tag_lettore_ids:
for tag_lettore in ca_tag_lettore_ids:
tag_lettore._compute_expired()
tag_lettore._compute_scheduled()
if not tag_lettore.expired and not tag_lettore.scheduled:
tag_lettore.check_update_state()
if tag_lettore.state == 'active':
now = fields.Datetime.now()
tag_persona_id = self.env['ca.tag_persona'].get_current_by_tag(
tag_lettore.ca_tag_id)
if tag_persona_id:
old_lettore_persona_id = self.env[
'ca.lettore_persona'
].search([
('ca_tag_lettore_id', '=', tag_lettore.id),
('ca_tag_persona', '=', tag_persona_id.id),
('date_end', '<', now),
('state', '=', 'active')
])
lettore_persona_id = self.env[
'ca.lettore_persona'
].search([
('ca_tag_lettore_id', '=', tag_lettore.id),
('ca_tag_persona', '=', tag_persona_id.id),
('date_start', '<=', now),
('date_end', '>=', now),
('state', '=', 'active')
('state', 'not in', ['expired'])
])
if old_lettore_persona_id:
old_lettore_persona_id.state = 'expired'
if not lettore_persona_id:
new_lettore_persona_id = self.env[
'ca.lettore_persona'
].create({
'ca_tag_lettore_id': tag_lettore.id,
'ca_tag_persona': tag_persona_id.id,
'date': fields.date.today(),
'state': 'active'
'ca_tag_persona': tag_persona_id.id
})
new_lettore_persona_id.check_update_state()
vals.append(new_lettore_persona_id)
return vals

Expand All @@ -104,3 +96,21 @@ def elabora_persone_lettore(self, nome_lettore):
return self.elabora_persone(lettore_id)
else:
return None

def check_update_state(self):
now = fields.Datetime.now()
self.ensure_one()
if self.date_start <= now <= self.date_end:
self.state = 'active'
elif self.date_start > now:
self.state = 'scheduled'
elif self.date_end <= now:
self.state = 'expired'

def check_update_by_date_valididty(self):
for person_reader in self.env['ca.lettore_persona'].search([]):
if person_reader:
person_reader.check_update_state()

def _cron_check_validity_winfo(self):
self.check_update_by_date_valididty()
6 changes: 4 additions & 2 deletions inrim_controllo_accessi/models/ca_punto_accesso.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,18 @@ def stamping_attach(self):
tag_lettore = self.env['ca.tag_lettore'].search(
[
('ca_tag_id', '=', tag.id),
('ca_lettore_id', "=", self.ca_lettore_id.id)
('ca_lettore_id', "=", self.ca_lettore_id.id),
('state', '=', 'active')
], limit=1)
if not tag_lettore:
self.env['ca.tag_lettore'].create({
tag_lettore = self.env['ca.tag_lettore'].create({
'ca_lettore_id': self.ca_lettore_id.id,
'ca_tag_id': tag.id,
'date_start': self.date_start,
'date_end': self.date_end,
'ca_punto_accesso_id': self.id
})
tag_lettore.check_update_state()

self.env['ca.lettore_persona'].elabora_persone(self.ca_lettore_id)

Expand Down
Loading

0 comments on commit dc8f0a2

Please sign in to comment.