changeset 4813:0b12c281e834

task #16043: Migration to GNU Health 4.0: Migrate health_ems package
author Luis Falcon <falcon@gnuhealth.org>
date Fri, 17 Dec 2021 14:18:02 +0000
parents 90316fe79c42
children e159b19e21c2
files tryton/health_ems/__init__.py tryton/health_ems/data/health_ems_sequence.xml tryton/health_ems/health_ems.py tryton/health_ems/sequences.py tryton/health_ems/view/support_request_log_tree.xml
diffstat 5 files changed, 118 insertions(+), 87 deletions(-) [+]
line wrap: on
line diff
--- a/tryton/health_ems/__init__.py
+++ b/tryton/health_ems/__init__.py
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
 ##############################################################################
 #
 #    GNU Health: The Free Health and Hospital Information System
@@ -20,16 +19,17 @@
 ##############################################################################
 
 from trytond.pool import Pool
-from .health_ems import *
-
+from . import health_ems
+from . import sequences
 
 def register():
     Pool.register(
-        GnuHealthSequences,
-        GnuHealthSequenceSetup,
-        Ambulance,
-        SupportRequest,
-        AmbulanceSupport,
-        AmbulanceHealthProfessional,
-        SupportRequestLog,
+        sequences.GnuHealthSequences,
+        sequences.SupportRequestSequence,
+        health_ems.Ambulance,
+        health_ems.SupportRequest,
+        health_ems.AmbulanceSupport,
+        health_ems.AmbulanceHealthProfessional,
+        health_ems.SupportRequestLog,
         module='health_ems', type_='model')
+
--- a/tryton/health_ems/data/health_ems_sequence.xml
+++ b/tryton/health_ems/data/health_ems_sequence.xml
@@ -6,12 +6,11 @@
 
         <record id="seq_type_gnuhealth_support_request_code" model="ir.sequence.type">
             <field name="name">Support Request</field>
-            <field name="code">gnuhealth.support_request</field>
         </record>
 
         <record id="seq_gnuhealth_support_request_code" model="ir.sequence">
             <field name="name">Support Request</field>
-            <field name="code">gnuhealth.support_request</field>
+            <field name="sequence_type" ref="seq_type_gnuhealth_support_request_code"></field>
             <field name="prefix">SR-${year}-</field>
             <field name="padding">5</field>
         </record>
--- a/tryton/health_ems/health_ems.py
+++ b/tryton/health_ems/health_ems.py
@@ -34,76 +34,16 @@
 from trytond.pool import Pool
 from trytond import backend
 from trytond.tools.multivalue import migrate_property
+from trytond.i18n import gettext
+from trytond.pyson import Id
 
+from trytond.modules.health.core import get_health_professional
 
 
 __all__ = [
-    'GnuHealthSequences','GnuHealthSequenceSetup',
     'Ambulance','SupportRequest', 'AmbulanceSupport',
     'AmbulanceHealthProfessional','SupportRequestLog']
 
-sequences = ['support_request_code_sequence']
-
-class GnuHealthSequences(ModelSingleton, ModelSQL, ModelView, ValueMixin):
-    'GNU Health Sequences'
-    __name__ = "gnuhealth.sequences"
-
-    support_request_code_sequence = fields.MultiValue(fields.Many2One(
-        'ir.sequence', 'Support Request Sequence', 
-        domain=[('code', '=', 'gnuhealth.support_request')],
-        required=True))
-
-    @classmethod
-    def multivalue_model(cls, field):
-        pool = Pool()
-
-        if field in sequences:
-            return pool.get('gnuhealth.sequence.setup')
-        return super(GnuHealthSequences, cls).multivalue_model(field)
-
-
-    @classmethod
-    def default_support_request_code_sequence(cls):
-        return cls.multivalue_model(
-            'support_request_code_sequence').default_support_request_code_sequence()
-
-# SEQUENCE SETUP
-class GnuHealthSequenceSetup(ModelSQL, ValueMixin):
-    'GNU Health Sequences Setup'
-    __name__ = 'gnuhealth.sequence.setup'
-
-    support_request_code_sequence = fields.Many2One('ir.sequence', 
-        'Support Request Sequence', required=True,
-        domain=[('code', '=', 'gnuhealth.support_request')])
-
-  
-    @classmethod
-    def __register__(cls, module_name):
-        TableHandler = backend.get('TableHandler')
-        exist = TableHandler.table_exist(cls._table)
-
-        super(GnuHealthSequenceSetup, cls).__register__(module_name)
-
-        if not exist:
-            cls._migrate_property([], [], [])
-
-    @classmethod
-    def _migrate_property(cls, field_names, value_names, fields):
-        field_names.extend(sequences)
-        value_names.extend(sequences)
-        migrate_property(
-            'gnuhealth.sequences', field_names, cls, value_names,
-            fields=fields)
-
-    @classmethod
-    def default_support_request_code_sequence(cls):
-        pool = Pool()
-        ModelData = pool.get('ir.model.data')
-        return ModelData.get_id(
-            'health_ems', 'seq_gnuhealth_support_request_code')
-
-    
-# END SEQUENCE SETUP , MIGRATION FROM FIELDS.PROPERTY
 
 class Ambulance (ModelSQL, ModelView):
     'Ambulance'
@@ -323,10 +263,7 @@
 
     @staticmethod
     def default_operator():
-        pool = Pool()
-        HealthProf= pool.get('gnuhealth.healthprofessional')
-        operator = HealthProf.get_health_professional()
-        return operator
+        return get_health_professional()
 
     @staticmethod
     def default_state():
@@ -347,18 +284,22 @@
 
         return ret_url
 
+
+    @classmethod
+    def generate_code(cls, **pattern):
+        Config = Pool().get('gnuhealth.sequences')
+        config = Config(1)
+        sequence = config.get_multivalue(
+            'support_request_code_sequence', **pattern)
+        if sequence:
+            return sequence.get()
+
     @classmethod
     def create(cls, vlist):
-        Sequence = Pool().get('ir.sequence')
-        Config = Pool().get('gnuhealth.sequences')
-
         vlist = [x.copy() for x in vlist]
         for values in vlist:
             if not values.get('code'):
-                config = Config(1)
-                values['code'] = Sequence.get_id(
-                    config.support_request_code_sequence.id)
-
+                values['code'] = cls.generate_code()
         return super(SupportRequest, cls).create(vlist)
 
 
new file mode 100644
--- /dev/null
+++ b/tryton/health_ems/sequences.py
@@ -0,0 +1,91 @@
+##############################################################################
+#
+#    GNU Health HMIS: The Free Health and Hospital Information System
+#    Copyright (C) 2008-2021 Luis Falcon <falcon@gnuhealth.org>
+#    Copyright (C) 2011-2021 GNU Solidario <health@gnusolidario.org>
+#
+#    The GNU Health HMIS component is part of the GNU Health project
+#    www.gnuhealth.org
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+# GNU Health HMIS sequences for this package
+
+from trytond.model import (ModelView, ModelSingleton, ModelSQL,
+                           ValueMixin, MultiValueMixin, fields)
+from trytond import backend
+from trytond.pyson import Id
+from trytond.pool import Pool
+from trytond.tools.multivalue import migrate_property
+
+# Sequences
+support_request_code_sequence = fields.Many2One(
+    'ir.sequence', 'Support Request sequence', required=True,
+    domain=[('sequence_type', '=', Id(
+        'health', 'seq_type_gnuhealth_support_request_code'))])
+
+
+# GNU HEALTH SEQUENCES
+class GnuHealthSequences(ModelSingleton, ModelSQL, ModelView, MultiValueMixin):
+    'Standard Sequences for GNU Health'
+    __name__ = 'gnuhealth.sequences'
+
+    support_request_code_sequence = fields.MultiValue(
+        support_request_code_sequence)
+
+
+    @classmethod
+    def default_support_request_code_sequence(cls, **pattern):
+        pool = Pool()
+        ModelData = pool.get('ir.model.data')
+        try:
+            return ModelData.get_id('health_surgery',
+                                    'seq_gnuhealth_surgery_code')
+        except KeyError:
+            return None
+
+class _ConfigurationValue(ModelSQL):
+
+    _configuration_value_field = None
+
+    @classmethod
+    def __register__(cls, module_name):
+        exist = backend.TableHandler.table_exist(cls._table)
+
+        super(_ConfigurationValue, cls).__register__(module_name)
+
+        if not exist:
+            cls._migrate_property([], [], [])
+
+    @classmethod
+    def _migrate_property(cls, field_names, value_names, fields):
+        field_names.append(cls._configuration_value_field)
+        value_names.append(cls._configuration_value_field)
+        migrate_property(
+            'gnuhealth.sequences', field_names, cls, value_names,
+            fields=fields)
+
+
+class SupportRequestSequence(_ConfigurationValue, ModelSQL, ValueMixin):
+    'Health Service Request Sequence setup'
+    __name__ = 'gnuhealth.sequences.support_request_code_sequence'
+    support_request_code_sequence = support_request_code_sequence
+    _configuration_value_field = 'support_request_code_sequence'
+
+    @classmethod
+    def check_xml_record(cls, records, values):
+        return True
+
--- a/tryton/health_ems/view/support_request_log_tree.xml
+++ b/tryton/health_ems/view/support_request_log_tree.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0"?>
-<tree editable="top">
+<tree editable="1">
     <field name="sr" expand="1"/>
     <field name="timestamp" expand="1" widget="date" string="Date"/>
     <field name="timestamp" expand="1" widget="time"/>