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
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 return [ cExternalCareItem(row = {'data': r, 'idx': idx, 'pk_field': 'pk_external_care'}) for r in rows ]
147
148
150 args = {
151 'pk_health_issue': pk_health_issue,
152 'issue': issue,
153 'pk_org_unit': pk_org_unit,
154 'enc': pk_encounter
155 }
156 cmd = """
157 INSERT INTO clin.external_care (
158 issue,
159 fk_health_issue,
160 fk_encounter,
161 fk_org_unit
162 ) VALUES (
163 gm.nullify_empty_string(%(issue)s),
164 (CASE
165 WHEN gm.is_null_or_blank_string(%(issue)s) IS TRUE THEN %(pk_health_issue)s
166 ELSE NULL
167 END)::integer,
168 %(enc)s,
169 %(pk_org_unit)s
170 )
171 RETURNING pk"""
172 rows, idx = gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}], return_data = True, get_col_idx = False)
173
174 return cExternalCareItem(aPK_obj = rows[0]['pk'])
175
176
178 args = {'pk': pk_external_care}
179 cmd = "DELETE FROM clin.external_care WHERE pk = %(pk)s"
180 gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}])
181 return True
182
183
184
185
186 if __name__ == "__main__":
187
188 if len(sys.argv) == 1:
189 sys.exit()
190
191 if sys.argv[1] != 'test':
192 sys.exit()
193
194
195
199
200
201 test_get_care_items()
202