1
2 """GNUmed external care classes.
3 """
4
5 __author__ = "K.Hilbert <Karsten.Hilbert@gmx.net>"
6 __license__ = "GPL v2 or later"
7
8
9 import sys
10 import logging
11
12
13 if __name__ == '__main__':
14 sys.path.insert(0, '../../')
15
16 from Gnumed.pycommon import gmBusinessDBObject
17 from Gnumed.pycommon import gmPG2
18 from Gnumed.pycommon import gmI18N
19
20 from Gnumed.business import gmOrganization
21
22 if __name__ == '__main__':
23 gmI18N.activate_locale()
24 gmI18N.install_domain()
25
26 from Gnumed.pycommon import gmTools
27
28 _log = logging.getLogger('gm.ext_care')
29
30
31 _SQL_get_external_care_items = """SELECT * FROM clin.v_external_care WHERE %s"""
32
34 """Represents an external care item.
35
36 Note: Upon saving .issue being (non-empty AND not None) will
37 override .fk_health_issue (IOW, if your code wants to set
38 .fk_health_issue to something other than NULL it needs to
39 unset .issue explicitly (to u'' or None)).
40 """
41 _cmd_fetch_payload = _SQL_get_external_care_items % "pk_external_care = %s"
42 _cmds_store_payload = [
43 """UPDATE clin.external_care SET
44 comment = gm.nullify_empty_string(%(comment)s),
45 fk_encounter = %(pk_encounter)s,
46 issue = gm.nullify_empty_string(%(issue)s),
47 provider = gm.nullify_empty_string(%(provider)s),
48 fk_org_unit = %(pk_org_unit)s,
49 inactive = %(inactive)s,
50 fk_health_issue = (
51 CASE
52 WHEN gm.is_null_or_blank_string(%(issue)s) IS TRUE THEN %(pk_health_issue)s
53 ELSE NULL
54 END
55 )::integer
56 WHERE
57 pk = %(pk_external_care)s
58 AND
59 xmin = %(xmin_external_care)s
60 RETURNING
61 xmin AS xmin_external_care
62 """,
63 _SQL_get_external_care_items % "pk_external_care = %(pk_external_care)s"
64 ]
65 _updatable_fields = [
66 'pk_encounter',
67 'pk_health_issue',
68 'pk_org_unit',
69 'issue',
70 'provider',
71 'comment',
72 'inactive'
73 ]
74
112
115
116 org_unit = property(_get_org_unit, lambda x:x)
117
118
119 -def get_external_care_items(order_by=None, pk_identity=None, pk_health_issue=None, exclude_inactive=False, return_pks=False):
120
121 args = {
122 'pk_pat': pk_identity,
123 'pk_issue': pk_health_issue
124 }
125 where_parts = []
126 if pk_identity is not None:
127 where_parts.append('pk_identity = %(pk_pat)s')
128 if pk_health_issue is not None:
129 where_parts.append('pk_health_issue = %(pk_issue)s')
130 if exclude_inactive is True:
131 where_parts.append('inactive IS FALSE')
132
133 if len(where_parts) == 0:
134 where = 'TRUE'
135 else:
136 where = ' AND '.join(where_parts)
137
138 if order_by is not None:
139 where = '%s ORDER BY %s' % (
140 where,
141 order_by
142 )
143
144 cmd = _SQL_get_external_care_items % where
145 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}], get_col_idx = True)
146 if return_pks:
147 return [ r['pk_external_care'] for r in rows ]
148 return [ cExternalCareItem(row = {'data': r, 'idx': idx, 'pk_field': 'pk_external_care'}) for r in rows ]
149
150
152 args = {
153 'pk_health_issue': pk_health_issue,
154 'issue': issue,
155 'pk_org_unit': pk_org_unit,
156 'enc': pk_encounter
157 }
158 cmd = """
159 INSERT INTO clin.external_care (
160 issue,
161 fk_health_issue,
162 fk_encounter,
163 fk_org_unit
164 ) VALUES (
165 gm.nullify_empty_string(%(issue)s),
166 (CASE
167 WHEN gm.is_null_or_blank_string(%(issue)s) IS TRUE THEN %(pk_health_issue)s
168 ELSE NULL
169 END)::integer,
170 %(enc)s,
171 %(pk_org_unit)s
172 )
173 RETURNING pk"""
174 rows, idx = gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}], return_data = True, get_col_idx = False)
175
176 return cExternalCareItem(aPK_obj = rows[0]['pk'])
177
178
180 args = {'pk': pk_external_care}
181 cmd = "DELETE FROM clin.external_care WHERE pk = %(pk)s"
182 gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}])
183 return True
184
185
186
187
188 if __name__ == "__main__":
189
190 if len(sys.argv) == 1:
191 sys.exit()
192
193 if sys.argv[1] != 'test':
194 sys.exit()
195
196
197
201
202
203 test_get_care_items()
204