source: Scheduling/trunk/m/BSDX08.m@ 1452

Last change on this file since 1452 was 1452, checked in by Sam Habiel, 12 years ago

done with BSDX07

File size: 13.5 KB
Line 
1BSDX08 ; VW/UJO/SMH - WINDOWS SCHEDULING RPCS ; 6/20/12 3:52pm
2 ;;1.6;BSDX;;Aug 31, 2011;Build 18
3 ;
4 ; Original by HMW. New Written by Sam Habiel. Licensed under LGPL.
5 ;
6 ; Change History
7 ; 3101022 UJO/SMH v1.42
8 ; - Transaction now restartable. Thanks to
9 ; --> Zach Gonzalez and Rick Marshall for fix.
10 ; - Extra TROLLBACK in Lock Statement when lock fails.
11 ; --> Removed--Rollback is already in ERR tag.
12 ; - Added new statements to old SD code in AVUPDT to obviate
13 ; --> need to restore variables in transaction
14 ; - Refactored this chunk of code. Don't really know whether it
15 ; --> worked in the first place. Waiting for bug report to know.
16 ; - Refactored all of APPDEL.
17 ;
18 ; 3111125 UJO/SMH v1.5
19 ; - Added ability to remove checked in appointments. Added a couple
20 ; of units tests for that under UT2.
21 ; - Minor reformatting because of how KIDS adds tabs.
22 ;
23 ; Error Reference:
24 ; -1~BSDX08: Appt record is locked. Please contact technical support.
25 ; -2~BSDX08: Invalid Appointment ID
26 ; -3~BSDX08: Invalid Appointment ID
27 ; -4~BSDX08: Cancelled appointment does not have a Resouce ID
28 ; -5~BSDX08: Resouce ID does not exist in BSDX RESOURCE
29 ; -6~BSDX08: Invalid Hosp Location stored in Database
30 ; -7~BSDX08: Patient does not have an appointment in PIMS Clinic
31 ; -8^BSDX08: Unable to find associated PIMS appointment for this patient
32 ; -9^BSDX08: BSDXAPI returned an error: (error)
33 ; -100~BSDX08 Error: (Mumps Error)
34 ;
35APPDELD(BSDXY,BSDXAPTID,BSDXTYP,BSDXCR,BSDXNOT) ;EP
36 ;Entry point for debugging
37 ;D DEBUG^%Serenji("APPDEL^BSDX08(.BSDXY,BSDXAPTID,BSDXTYP,BSDXCR,BSDXNOT)")
38 Q
39 ;
40UT ; Unit Tests
41 N RESNAM S RESNAM="UTCLINIC"
42 N HLRESIENS ; holds output of UTCR^BSDX35 - HL IEN^Resource IEN
43 D
44 . N $ET S $ET="D ^%ZTER B"
45 . S HLRESIENS=$$UTCR^BSDX35(RESNAM)
46 . I HLRESIENS<0 S $EC=",U1," ; not supposed to happen - hard crash if so
47 ;
48 N HLIEN,RESIEN
49 S HLIEN=$P(HLRESIENS,U)
50 S RESIEN=$P(HLRESIENS,U,2)
51 ;
52 ; Get start and end times
53 N TIMES S TIMES=$$TIMES^BSDX35 ; appt time^end time
54 N APPTTIME S APPTTIME=$P(TIMES,U)
55 N ENDTIME S ENDTIME=$P(TIMES,U,2)
56 ;
57 ; Test 1: Make normal appointment and cancel it. See if every thing works
58 N ZZZ,DFN
59 S DFN=3
60 D APPADD^BSDX07(.ZZZ,APPTTIME,ENDTIME,DFN,RESNAM,30,"Sam's Note",1)
61 S APPID=+$P(^BSDXTMP($J,1),U)
62 D APPDEL^BSDX08(.ZZZ,APPID,"PC",1,"Sam's Cancel Note")
63 I $P(^BSDXAPPT(APPID,0),U,12)'>0 W "Error in Cancellation-1"
64 I $O(^SC(2,"S",APPTTIME,1,0))]"" W "Error in Cancellation-2"
65 I $P(^DPT(4,"S",APPTTIME,0),U,2)'="PC" W "Error in Cancellation-3"
66 I ^DPT(4,"S",3110123.2,"R")'="Sam's Cancel Note" W "Error in Cancellation-4"
67 ;
68 ; Test 2: Check for -1
69 ; Make appt
70 D APPADD^BSDX07(.ZZZ,3110125.2,3110125.3,4,"Dr Office",10,"Sam's Note",1)
71 ; Lock the node in another job
72 S APPID=+$P(^BSDXTMP($J,1),U)
73 ; W "Lock ^BSDXAPPT("_APPID_") in another session. You have 10 seconds." H 10
74 D APPDEL^BSDX08(.ZZZ,APPID,"PC",1,"Sam's Cancel Note")
75 ;
76 ; Test 3: Check for -100
77 S bsdxdie=1
78 D APPADD^BSDX07(.ZZZ,3110126.2,3110126.3,4,"Dr Office",10,"Sam's Note",1)
79 S APPID=+$P(^BSDXTMP($J,1),U)
80 D APPDEL^BSDX08(.ZZZ,APPID,"PC",1,"Reasons")
81 I $P(^BSDXTMP($J,1),"~")'=-100 W "Error in -100",!
82 K bsdxdie
83 ;
84 ; Test 4: Restartable transaction
85 S bsdxrestart=1
86 D APPADD^BSDX07(.ZZZ,3110128.2,3110128.3,4,"Dr Office",10,"Sam's Note",1)
87 S APPID=+$P(^BSDXTMP($J,1),U)
88 D APPDEL^BSDX08(.ZZZ,APPID,"PC",1,"Reasons")
89 I $P(^DPT(4,"S",3110128.2,0),U,2)'="PC" W "Error in Restartable Transaction",!
90 ;
91 ; Test 5: for invalid Appointment ID (-2 and -3)
92 D APPDEL^BSDX08(.ZZZ,0,"PC",1,"Reasons")
93 I $P(^BSDXTMP($J,1),"~")'=-2 W "Error in -2",!
94 D APPDEL^BSDX08(.ZZZ,999999,"PC",1,"Reasons")
95 I $P(^BSDXTMP($J,1),"~")'=-3 W "Error in -3",!
96UT2 ; More unit Tests
97 ;
98 ; Test 6: for Cancelling walkin and checked-in appointments
99 S BSDXSTART=$E($$NOW^XLFDT,1,12),BSDXEND=BSDXSTART+.0001
100 D APPADD^BSDX07(.ZZZ,BSDXSTART,BSDXEND,4,"Dr Office",10,"Sam's Note",1) ; Add appt
101 S APPID=+$P(^BSDXTMP($J,1),U)
102 I APPID=0 W "Error in test 6",!
103 D CHECKIN^BSDX25(.ZZZ,APPID,$$NOW^XLFDT) ; check-in
104 D APPDEL^BSDX08(.ZZZ,APPID,"PC",10,"Cancel Note") ; Delete appt
105 I $P(^BSDXTMP($J,1),$C(30))'="" W "Error in test 6",!
106 ;
107 ; Test 7: for cancelling walkin and checked-in appointments
108 S BSDXSTART=$E($$NOW^XLFDT,1,12)+.0001,BSDXEND=BSDXSTART+.0001
109 D APPADD^BSDX07(.ZZZ,BSDXSTART,BSDXEND,4,"Dr Office",10,"Sam's Note",1) ; Add appt
110 S APPID=+$P(^BSDXTMP($J,1),U)
111 I APPID=0 W "Error in test 6",!
112 D CHECKIN^BSDX25(.ZZZ,APPID,$$NOW^XLFDT) ; Checkin
113 S BSDXRES=$O(^BSDXRES("B","Dr Office",""))
114 S BSDXCLN=$P(^BSDXRES(BSDXRES,0),U,4)
115 S BSDXRESULT=$$RMCI^BSDXAPI(4,BSDXCLN,BSDXSTART) ; remove checkin
116 D APPDEL^BSDX08(.ZZZ,APPID,"PC",10,"Cancel Note") ; delete appt
117 I $P(^BSDXTMP($J,1),$C(30))'="" W "Error in test 6",!
118 QUIT
119APPDEL(BSDXY,BSDXAPTID,BSDXTYP,BSDXCR,BSDXNOT) ;EP
120 ;Called by RPC: BSDX CANCEL APPOINTMENT
121 ;Cancels existing appointment in BSDX APPOINTMENT and 44/2 subfiles
122 ;Input Parameters:
123 ; - BSDXAPTID is entry number in BSDX APPOINTMENT file
124 ; - BSDXTYP is C for clinic-cancelled and PC for patient cancelled
125 ; - BSDXCR is pointer to CANCELLATION REASON File (409.2)
126 ; - BSDXNOT is user note
127 ;
128 ; Returns error code in recordset field ERRORID. Empty string is success.
129 ; Returns Global Array. Must use this type in RPC.
130 ;
131 ; Return Array: set Return and clear array
132 S BSDXY=$NA(^BSDXTMP($J))
133 K ^BSDXTMP($J)
134 ;
135 ; Set min DUZ vars if they don't exist
136 D ^XBKVAR
137 ;
138 ; $ET
139 N $ET S $ET="G ETRAP^BSDX08"
140 ;
141 ; Counter
142 N BSDXI S BSDXI=0
143 ; Header Node
144 S ^BSDXTMP($J,BSDXI)="T00100ERRORID"_$C(30)
145 ;
146 ; Lock BSDX node, only to synchronize access to the globals.
147 ; It's not expected that the error will ever happen as no filing
148 ; is supposed to take 5 seconds.
149 L +^BSDXAPPT(BSDXAPTID):5 I '$T D ERR(BSDXI,"-1~BSDX08: Appt record is locked. Please contact technical support.") Q
150 ;
151 ;Restartable Transaction; restore paramters when starting.
152 ; (Params restored are what's passed here + BSDXI)
153 TSTART (BSDXY,BSDXAPTID,BSDXTYP,BSDXCR,BSDXNOT,BSDXI):T="BSDX CANCEL APPOINTEMENT^BSDX08"
154 ;
155 ; Turn off SDAM APPT PROTOCOL BSDX Entries
156 N BSDXNOEV
157 S BSDXNOEV=1 ;Don't execute BSDX CANCEL APPOINTMENT protocol
158 ;
159 ;;;test for error inside transaction. See if %ZTER works
160 I $G(bsdxdie) S X=1/0
161 ;;;test
162 ;;;test for TRESTART
163 I $G(bsdxrestart) K bsdxrestart TRESTART
164 ;;;test
165 ;
166 ; Check appointment ID and whether it exists
167 I '+BSDXAPTID D ERR(BSDXI,"-2~BSDX08: Invalid Appointment ID") Q
168 I '$D(^BSDXAPPT(BSDXAPTID,0)) D ERR(BSDXI,"-3~BSDX08: Invalid Appointment ID") Q
169 ;
170 ; Start Processing:
171 ; First, add cancellation date to appt entry in BSDX APPOINTMENT
172 N BSDXNOD S BSDXNOD=^BSDXAPPT(BSDXAPTID,0) ; BSDX Appt Node
173 N BSDXPATID S BSDXPATID=$P(BSDXNOD,U,5) ; Patient ID
174 N BSDXSTART S BSDXSTART=$P(BSDXNOD,U) ; Start Time
175 D BSDXCAN(BSDXAPTID) ; Add a cancellation date in BSDX APPOINTMENT
176 ;
177 ; Second, cancel appt in "S" nodes in file 2 and 44, then update Legacy PIMS Availability
178 N BSDXSC1 S BSDXSC1=$P(BSDXNOD,U,7) ;RESOURCEID
179 ; If the resouce id doesn't exist...
180 I BSDXSC1="" D ERR(BSDXI,"-4~BSDX08: Cancelled appointment does not have a Resouce ID") QUIT
181 I '$D(^BSDXRES(BSDXSC1,0)) D ERR(BSDXI,"-5~BSDX08: Resouce ID does not exist in BSDX RESOURCE") QUIT
182 ; Get zero node of resouce
183 S BSDXNOD=^BSDXRES(BSDXSC1,0)
184 ; Get Hosp location
185 N BSDXLOC S BSDXLOC=$P(BSDXNOD,U,4)
186 ; Error indicator for Hosp Location filing for getting out of routine
187 N BSDXERR S BSDXERR=0
188 ; Only file in 2/44 if there is an associated hospital location
189 I BSDXLOC D QUIT:BSDXERR
190 . I '$D(^SC(BSDXLOC,0)) S BSDXERR=1 D ERR(BSDXI,"-6~BSDX08: Invalid Hosp Location stored in Database") QUIT
191 . ; Get the IEN of the appointment in the "S" node of ^SC
192 . N BSDXSCIEN
193 . S BSDXSCIEN=$$SCIEN^BSDXAPI(BSDXPATID,BSDXLOC,BSDXSTART)
194 . I BSDXSCIEN="" S BSDXERR=1 D ERR(BSDXI,"-7~BSDX08: Patient does not have an appointment in PIMS Clinic") QUIT
195 . ; Get the appointment node
196 . S BSDXNOD=$G(^SC(BSDXLOC,"S",BSDXSTART,1,BSDXSCIEN,0))
197 . I BSDXNOD="" S BSDXERR=1 D ERR(BSDXI,"-8^BSDX08: Unable to find associated PIMS appointment for this patient") QUIT
198 . N BSDXLEN S BSDXLEN=$P(BSDXNOD,U,2)
199 . ; Cancel through BSDXAPI
200 . N BSDXZ
201 . D APCAN(.BSDXZ,BSDXLOC,BSDXPATID,BSDXSTART)
202 . I +BSDXZ>0 S BSDXERR=1 D ERR(BSDXI,"-9^BSDX08: BSDXAPI returned an error: "_$P(BSDXZ,U,2)) QUIT
203 . ; Update Legacy PIMS clinic Availability
204 . D AVUPDT(BSDXLOC,BSDXSTART,BSDXLEN)
205 ;
206 TCOMMIT
207 L -^BSDXAPPT(BSDXAPTID)
208 S BSDXI=BSDXI+1
209 S ^BSDXTMP($J,BSDXI)=""_$C(30)
210 S BSDXI=BSDXI+1
211 S ^BSDXTMP($J,BSDXI)=$C(31)
212 Q
213 ;
214AVUPDT(BSDXSCD,BSDXSTART,BSDXLEN) ;Update Legacy PIMS Clinic availability
215 ;See SDCNP0
216 N SD,S ; Start Date
217 S (SD,S)=BSDXSTART
218 N I ; Clinic IEN in 44
219 S I=BSDXSCD
220 ; if day has no schedule in legacy PIMS, forget about this update.
221 Q:'$D(^SC(I,"ST",SD\1,1))
222 N SL ; Clinic characteristics node (length of appt, when appts start etc)
223 S SL=^SC(I,"SL")
224 N X ; Hour Clinic Display Begins
225 S X=$P(SL,U,3)
226 N STARTDAY ; When does the day start?
227 S STARTDAY=$S($L(X):X,1:8) ; If defined, use it; otherwise, 8am
228 N SB ; ?? Who knows? Day Start - 1 divided by 100.
229 S SB=STARTDAY-1/100
230 S X=$P(SL,U,6) ; Now X is Display increments per hour
231 N HSI ; Slots per hour, try 1
232 S HSI=$S(X:X,1:4) ; if defined, use it; otherwise, 4
233 N SI ; Slots per hour, try 2
234 S SI=$S(X="":4,X<3:4,X:X,1:4) ; If slots "", or less than 3, then 4
235 N STR ; ??
236 S STR="#@!$* XXWVUTSRQPONMLKJIHGFEDCBA0123456789jklmnopqrstuvwxyz"
237 N SDDIF ; Slots per hour diff??
238 S SDDIF=$S(HSI<3:8/HSI,1:2)
239 S SL=BSDXLEN ; Dammit, reusing variable; SL now Appt Length from GUI
240 S S=^SC(I,"ST",SD\1,1) ; reusing var again; S now Day Pattern from PIMS
241 N Y ; Hours since start of Date
242 S Y=SD#1-SB*100 ;SD#1=FM Time portion; -SB minus start of day; conv to hrs
243 N ST ; ??
244 ; Y#1 -> Minutes; *SI -> * Slots per hour; \.6 trunc min to hour
245 ; Y\1 -> Hours since start of day; * SI: * slots
246 S ST=Y#1*SI\.6+(Y\1*SI)
247 N SS ; how many slots are supposed to be taken by appointment
248 S SS=SL*HSI/60 ; (nb: try SL: 30 min; HSI: 4 slots)
249 N I
250 I Y'<1 D ; If Hours since start of Date is greater than 1
251 . ; loop through pattern. Tired of documenting.
252 . F I=ST+ST:SDDIF D Q:Y="" Q:SS'>0
253 . . S Y=$E(STR,$F(STR,$E(S,I+1))) Q:Y=""
254 . . S S=$E(S,1,I)_Y_$E(S,I+2,999)
255 . . S SS=SS-1
256 . . Q:SS'>0
257 S ^SC(BSDXSCD,"ST",SD\1,1)=S ; new pattern; global set
258 Q
259 ;
260APCAN(BSDXZ,BSDXLOC,BSDXDFN,BSDXSD) ;
261 ;Cancel appointment for patient BSDXDFN in clinic BSDXSC1
262 ;at time BSDXSD
263 N BSDXC,%H
264 S BSDXC("PAT")=BSDXPATID
265 S BSDXC("CLN")=BSDXLOC
266 S BSDXC("TYP")=BSDXTYP
267 S BSDXC("ADT")=BSDXSD
268 S %H=$H D YMD^%DTC
269 S BSDXC("CDT")=X+%
270 S BSDXC("NOT")=BSDXNOT
271 S:'+$G(BSDXCR) BSDXCR=11 ;Other
272 S BSDXC("CR")=BSDXCR
273 S BSDXC("USR")=DUZ
274 ;
275 S BSDXZ=$$CANCEL^BSDXAPI(.BSDXC)
276 Q
277 ;
278BSDXCAN(BSDXAPTID) ;
279 ;Cancel BSDX APPOINTMENT entry
280 N %DT,X,BSDXDATE,Y,BSDXIENS,BSDXFDA,BSDXMSG
281 S %DT="XT",X="NOW" D ^%DT ; X ^DD("DD")
282 S BSDXDATE=Y
283 S BSDXIENS=BSDXAPTID_","
284 S BSDXFDA(9002018.4,BSDXIENS,.12)=BSDXDATE
285 K BSDXMSG
286 D FILE^DIE("","BSDXFDA","BSDXMSG")
287 Q
288 ;
289CANEVT(BSDXPAT,BSDXSTART,BSDXSC) ;EP Called by BSDX CANCEL APPOINTMENT event
290 ;when appointments cancelled via PIMS interface.
291 ;Propagates cancellation to BSDXAPPT and raises refresh event to running GUI clients
292 N BSDXFOUND,BSDXRES
293 Q:+$G(BSDXNOEV)
294 Q:'+$G(BSDXSC)
295 S BSDXFOUND=0
296 I $D(^BSDXRES("ALOC",BSDXSC)) S BSDXRES=$O(^BSDXRES("ALOC",BSDXSC,0)) S BSDXFOUND=$$CANEVT1(BSDXRES,BSDXSTART,BSDXPAT)
297 I BSDXFOUND D CANEVT3(BSDXRES) Q
298 I $D(^BXDXRES("ASSOC",BSDXSC)) S BSDXRES=$O(^BSDXRES("ASSOC",BSDXSC,0)) S BSDXFOUND=$$CANEVT1(BSDXRES,BSDXSTART,BSDXPAT)
299 I BSDXFOUND D CANEVT3(BSDXRES)
300 Q
301 ;
302CANEVT1(BSDXRES,BSDXSTART,BSDXPAT) ;
303 ;Get appointment id in BSDXAPT
304 ;If found, call BSDXCAN(BSDXAPPT) and return 1
305 ;else return 0
306 N BSDXFOUND,BSDXAPPT
307 S BSDXFOUND=0
308 Q:'+BSDXRES BSDXFOUND
309 Q:'$D(^BSDXAPPT("ARSRC",BSDXRES,BSDXSTART)) BSDXFOUND
310 S BSDXAPPT=0 F S BSDXAPPT=$O(^BSDXAPPT("ARSRC",BSDXRES,BSDXSTART,BSDXAPPT)) Q:'+BSDXAPPT D Q:BSDXFOUND
311 . S BSDXNOD=$G(^BSDXAPPT(BSDXAPPT,0)) Q:BSDXNOD=""
312 . I $P(BSDXNOD,U,5)=BSDXPAT,$P(BSDXNOD,U,12)="" S BSDXFOUND=1 Q
313 I BSDXFOUND,+$G(BSDXAPPT) D BSDXCAN(BSDXAPPT)
314 Q BSDXFOUND
315 ;
316CANEVT3(BSDXRES) ;
317 ;Call RaiseEvent to notify GUI clients
318 ;
319 N BSDXRESN
320 S BSDXRESN=$G(^BSDXRES(BSDXRES,0))
321 Q:BSDXRESN=""
322 S BSDXRESN=$P(BSDXRESN,"^")
323 ;D EVENT^BSDX23("SCHEDULE-"_BSDXRESN,"","","")
324 D EVENT^BMXMEVN("BSDX SCHEDULE",BSDXRESN)
325 Q
326 ;
327ERR(BSDXI,BSDXERR) ;Error processing
328 S BSDXI=BSDXI+1
329 S BSDXERR=$TR(BSDXERR,"^","~")
330 I $TL>0 TROLLBACK
331 S ^BSDXTMP($J,BSDXI)=BSDXERR_$C(30)
332 S BSDXI=BSDXI+1
333 S ^BSDXTMP($J,BSDXI)=$C(31)
334 L -^BSDXAPPT(BSDXAPTID)
335 QUIT
336 ;
337ETRAP ;EP Error trap entry
338 N $ET S $ET="D ^%ZTER HALT" ; Emergency Error Trap
339 ; Rollback, otherwise ^XTER will be empty from future rollback
340 I $TL>0 TROLLBACK
341 D ^%ZTER
342 S $EC="" ; Clear Error
343 ; Log error message and send to client
344 I '$D(BSDXI) N BSDXI S BSDXI=0
345 D ERR(BSDXI,"-100~BSDX08 Error: "_$G(%ZTERZE))
346 QUIT
347 ;
348 ;;;NB: This is code that is unused in both original and port.
349 ; ; If not appt in the "S" node is found in ^SC then check associated RPMS Clinic Multiple
350 ; I BSDXSCIEN="" D I 'BSDXZ Q ;Q:BSDXZ
351 ; . S BSDXERR="BSDX08: Unable to find associated RPMS appointment for this patient. "
352 ; . S BSDXZ=1
353 ; . ; Check if there are associated RPMS clinics. (not currently used) Does the multiple exist? No, then quit
354 ; . I '$D(^BSDXRES(BSDXSC1,20)) S BSDXZ=0 QUIT
355 ; . ; Loop through the multiple. Get Location and then the ^SC "S" node IEN.
356 ; . N BSDX1 S BSDX1=0
357 ; . F S BSDX1=$O(^BSDXRES(BSDXSC1,20,BSDX1)) Q:'+BSDX1 Q:BSDXZ=0 D
358 ; . . Q:'$D(^BSDXRES(BSDXSC1,20,BSDX1,0))
359 ; . . S BSDXLOC=$P(^BSDXRES(BSDXSC1,20,BSDX1,0),U)
360 ; . . S BSDXSCIEN=$$SCIEN^BSDXAPI(BSDXPATID,BSDXLOC,BSDXSTART) I +BSDXSCIEN S BSDXZ=0 Q
Note: See TracBrowser for help on using the repository browser.