File: v21-clin-v_reminders_journal.sql

package info (click to toggle)
gnumed-server 22.31-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 46,268 kB
  • sloc: sql: 1,217,633; python: 15,878; sh: 1,590; makefile: 20
file content (323 lines) | stat: -rw-r--r-- 8,168 bytes parent folder | download | duplicates (5)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
-- ==============================================================
-- GNUmed database schema change script
--
-- License: GPL v2 or later
-- Author: Karsten Hilbert
--
-- ==============================================================
\set ON_ERROR_STOP 1
--set default_transaction_read_only to off;

-- --------------------------------------------------------------
drop view if exists clin.v_reminders_journal cascade;


create view clin.v_reminders_journal as

	-- clin_when: due date
	select
		d_mi.fk_patient
			as pk_patient,
		d_mi.modified_when
			as modified_when,
		d_mi.due_date
			as clin_when,
		coalesce (
			(select short_alias from dem.staff where db_user = d_mi.modified_by),
			'<' || d_mi.modified_by || '>'
		)
			as modified_by,
		NULL::text
			as soap_cat,
		_('Due today') || ' (' || d_vit.l10n_category || ' - ' || d_vit.l10n_type || ')' || E'\n'
			|| coalesce(' ' || d_mi.comment || E'\n', '')
			|| coalesce(' ' || _('Will expire:') || ' ' || to_char(d_mi.expiry_date, 'YYYY-MM-DD') || E'\n', '')
			|| ' ' || _('Importance:') || ' ' || d_mi.importance || E'\n'
			|| coalesce(' ' || _('Context:') || ' ' || array_to_string(d_mi.ufk_context, ',', '?') || E'\n', '')
			|| coalesce(' ' || _('Data:') || ' ' || d_mi.data || E'\n', '')
			|| coalesce(' ' || _('Provider:') || ' ' || d_st.short_alias, '')
		as narrative,
		(
			select c_e.pk
			from clin.encounter c_e
			where c_e.fk_patient = d_mi.fk_patient
			order by started desc
			limit 1
		)
			as pk_encounter,
		null::integer
			as pk_episode,
		null::integer
			as pk_health_issue,
		d_mi.pk
			as src_pk,
		'dem.message_inbox'::text
			as src_table,
		d_mi.row_version
			as row_version,

		-- issue
		null::text
			as health_issue,
		null::text
			as issue_laterality,
		null::boolean
			as issue_active,
		null::boolean
			as issue_clinically_relevant,
		null::boolean
			as issue_confidential,

		-- episode
		null::text
			as episode,
		null::boolean
			as episode_open,

		-- encounter
		(
			select c_e.started
			from clin.encounter c_e
			where c_e.fk_patient = d_mi.fk_patient
			order by started desc
			limit 1
		)
			as encounter_started,
		(
			select c_e.last_affirmed
			from clin.encounter c_e
			where c_e.fk_patient = d_mi.fk_patient
			order by started desc
			limit 1
		)
			as encounter_last_affirmed,
		null::text
			as encounter_type,
		null::text
			as encounter_l10n_type
	from
		dem.message_inbox d_mi
			inner join dem.v_inbox_item_type d_vit on (d_mi.fk_inbox_item_type = d_vit.pk_type)
			left join dem.staff d_st on (d_mi.fk_staff = d_st.pk)
	where
		d_mi.fk_patient is not null
			and
		d_vit.category = 'clinical'
			and
		d_mi.due_date is not null

UNION

	-- clin_when: expiry date
	select
		d_mi.fk_patient
			as pk_patient,
		d_mi.modified_when
			as modified_when,
		d_mi.expiry_date
			as clin_when,
		coalesce (
			(select short_alias from dem.staff where db_user = d_mi.modified_by),
			'<' || d_mi.modified_by || '>'
		)
			as modified_by,
		NULL::text
			as soap_cat,
		_('Epires today') || ' (' || d_vit.l10n_category || ' - ' || d_vit.l10n_type || ')' || E'\n'
			|| coalesce(' ' || d_mi.comment || E'\n', '')
			|| coalesce(' ' || _('Was due:') || ' ' || to_char(d_mi.due_date, 'YYYY-MM-DD') || E'\n', '')
			|| ' ' || _('Importance:') || ' ' || d_mi.importance || E'\n'
			|| coalesce(' ' || _('Context:') || ' ' || array_to_string(d_mi.ufk_context, ',', '?') || E'\n', '')
			|| coalesce(' ' || _('Data:') || ' ' || d_mi.data || E'\n', '')
			|| coalesce(' ' || _('Provider:') || ' ' || d_st.short_alias, '')
		as narrative,
		(
			select c_e.pk
			from clin.encounter c_e
			where c_e.fk_patient = d_mi.fk_patient
			order by started desc
			limit 1
		)
			as pk_encounter,
		null::integer
			as pk_episode,
		null::integer
			as pk_health_issue,
		d_mi.pk
			as src_pk,
		'dem.message_inbox'::text
			as src_table,
		d_mi.row_version
			as row_version,

		-- issue
		null::text
			as health_issue,
		null::text
			as issue_laterality,
		null::boolean
			as issue_active,
		null::boolean
			as issue_clinically_relevant,
		null::boolean
			as issue_confidential,

		-- episode
		null::text
			as episode,
		null::boolean
			as episode_open,

		-- encounter
		(
			select c_e.started
			from clin.encounter c_e
			where c_e.fk_patient = d_mi.fk_patient
			order by started desc
			limit 1
		)
			as encounter_started,
		(
			select c_e.last_affirmed
			from clin.encounter c_e
			where c_e.fk_patient = d_mi.fk_patient
			order by started desc
			limit 1
		)
			as encounter_last_affirmed,
		null::text
			as encounter_type,
		null::text
			as encounter_l10n_type
	from
		dem.message_inbox d_mi
			inner join dem.v_inbox_item_type d_vit on (d_mi.fk_inbox_item_type = d_vit.pk_type)
			left join dem.staff d_st on (d_mi.fk_staff = d_st.pk)
	where
		d_mi.fk_patient is not null
			and
		d_vit.category = 'clinical'
			and
		d_mi.expiry_date is not null

UNION

	-- clin_when: modified_when (= message received)
	select
		d_mi.fk_patient
			as pk_patient,
		d_mi.modified_when
			as modified_when,
		d_mi.modified_when
			as clin_when,
		coalesce (
			(select short_alias from dem.staff where db_user = d_mi.modified_by),
			'<' || d_mi.modified_by || '>'
		)
			as modified_by,
		NULL::text
			as soap_cat,
		_('Clinical reminder') || ' (' || d_vit.l10n_category || ' - ' || d_vit.l10n_type || ')' || E'\n'
			|| coalesce(' ' || d_mi.comment || E'\n', '')
			|| coalesce(' ' || _('Due:') || ' ' || to_char(d_mi.due_date, 'YYYY-MM-DD') || E'\n', '')
			|| coalesce(' ' || _('Expires:') || ' ' || to_char(d_mi.expiry_date, 'YYYY-MM-DD') || E'\n', '')
			|| ' ' || _('Importance:') || ' ' || d_mi.importance || E'\n'
			|| coalesce(' ' || _('Context:') || ' ' || array_to_string(d_mi.ufk_context, ',', '?') || E'\n', '')
			|| coalesce(' ' || _('Data:') || ' ' || d_mi.data || E'\n', '')
			|| coalesce(' ' || _('Provider:') || ' ' || d_st.short_alias, '')
		as narrative,
		(
			select c_e.pk
			from clin.encounter c_e
			where c_e.fk_patient = d_mi.fk_patient
			order by started desc
			limit 1
		)
			as pk_encounter,
		null::integer
			as pk_episode,
		null::integer
			as pk_health_issue,
		d_mi.pk
			as src_pk,
		'dem.message_inbox'::text
			as src_table,
		d_mi.row_version
			as row_version,

		-- issue
		null::text
			as health_issue,
		null::text
			as issue_laterality,
		null::boolean
			as issue_active,
		null::boolean
			as issue_clinically_relevant,
		null::boolean
			as issue_confidential,

		-- episode
		null::text
			as episode,
		null::boolean
			as episode_open,

		-- encounter
		(
			select c_e.started
			from clin.encounter c_e
			where c_e.fk_patient = d_mi.fk_patient
			order by started desc
			limit 1
		)
			as encounter_started,
		(
			select c_e.last_affirmed
			from clin.encounter c_e
			where c_e.fk_patient = d_mi.fk_patient
			order by started desc
			limit 1
		)
			as encounter_last_affirmed,
		null::text
			as encounter_type,
		null::text
			as encounter_l10n_type
	from
		dem.message_inbox d_mi
			inner join dem.v_inbox_item_type d_vit on (d_mi.fk_inbox_item_type = d_vit.pk_type)
			left join dem.staff d_st on (d_mi.fk_staff = d_st.pk)
	where
		d_mi.fk_patient is not null
			and
		d_vit.category = 'clinical'
			and
		(
			(d_mi.due_date is not null)
				or
			(d_mi.expiry_date is not null)
		)
;


grant select on clin.v_reminders_journal to group "gm-doctors";

-- --------------------------------------------------------------
select i18n.upd_tx('de', 'Clinical reminder', 'Medizinische Erinnerung');

select i18n.upd_tx('de', 'Due today', 'Heute fällig');
select i18n.upd_tx('de', 'Due:', 'Fällig:');
select i18n.upd_tx('de', 'Was due:', 'War fällig:');

select i18n.upd_tx('de', 'Epires today', 'Läuft heute ab');
select i18n.upd_tx('de', 'Expires:', 'Läuft ab:');
select i18n.upd_tx('de', 'Will expire:', 'Wird ablaufen:');

select i18n.upd_tx('de', 'Importance:', 'Bedeutsamkeit:');
select i18n.upd_tx('de', 'Context:', 'Kontext:');
select i18n.upd_tx('de', 'Data:', 'Daten:');
select i18n.upd_tx('de', 'Provider:', 'Mitarbeiter:');

-- --------------------------------------------------------------
select gm.log_script_insertion('v21-clin-v_reminders_journal.sql', '21.0');