source: Scheduling/trunk/m/BSDX07.m@ 1456

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

Moved Unit Tests and UT Set-up to BSDXUT, started refactoring BSDX08.

File size: 12.6 KB
Line 
1BSDX07 ; VW/UJO/SMH - WINDOWS SCHEDULING RPCS ; 6/21/12 3:54pm
2 ;;1.7T1;BSDX;;Aug 31, 2011;Build 18
3 ; Licensed under LGPL
4 ;
5 ; Change Log:
6 ; UJO/SMH
7 ; v1.3 July 13 2010 - Add support i18n - Dates input as FM dates, not US.
8 ; v1.42 Oct 22 2010 - Transaction now restartable by providing arguments
9 ; v1.42 Oct 30 2010 - Extensive refactoring.
10 ; v1.5 Mar 15 2011 - End time does not have to have time anymore.
11 ; It could be midnight of the next day
12 ; v1.6 Apr 11 2011 - Support for Scheduling Radiology Exams...
13 ; v1.7 Jun 20 2012 - Refactoring to remove transactions - many changes
14 ;
15 ; Error Reference:
16 ; -1: Patient Record is locked. This means something is wrong!!!!
17 ; -2: Start Time is not a valid Fileman date
18 ; -3: End Time is not a valid Fileman date
19 ; v1.5:obsolete::-4: End Time does not have time inside of it.
20 ; -5: BSDXPATID is not numeric
21 ; -6: Patient Does not exist in ^DPT
22 ; -7: Resource Name does not exist in B index of BSDX RESOURCE
23 ; -8: Resouce doesn't exist in ^BSDXRES
24 ; -9: Couldn't add appointment to BSDX APPOINTMENT
25 ; -10: Couldn't add appointment to files 2 and/or 44
26 ; -100: Mumps Error
27 ;
28APPADDD(BSDXY,BSDXSTART,BSDXEND,BSDXPATID,BSDXRES,BSDXLEN,BSDXNOTE,BSDXATID) ;EP
29 ;Entry point for debugging
30 ; D DEBUG^%Serenji("APPADD^BSDX07(.BSDXY,BSDXSTART,BSDXEND,BSDXPATID,BSDXRES,BSDXLEN,BSDXNOTE,BSDXATID)")
31 Q
32 ;
33APPADD(BSDXY,BSDXSTART,BSDXEND,BSDXPATID,BSDXRES,BSDXLEN,BSDXNOTE,BSDXATID,BSDXRADEXAM) ;EP
34 ;
35 ;Called by RPC: BSDX ADD NEW APPOINTMENT
36 ;
37 ;Add new appointment to 3 files
38 ; - BSDX APPOINTMENT
39 ; - Hosp Location Appointment SubSubfile if Resource is linked to clinic
40 ; - Patient Appointment Subfile if Resource is linked to clinic
41 ;
42 ;Paramters:
43 ;BSDXY: Global Return (RPC must be set to Global Array)
44 ;BSDXSTART: FM Start Date
45 ;BSDXEND: FM End Date
46 ;BSDXPATID: Patient DFN
47 ;BSDXRES is ResourceName in BSDX RESOURCE file (not IEN)
48 ;BSDXLEN is the appointment duration in minutes
49 ;BSDXNOTE is the Appiontment Note
50 ;BSDXATID is used for 2 purposes:
51 ; if BSDXATID = "WALKIN" then BSDAPI is called to create a walkin appt.
52 ; if BSDXATID = a number, then it is the access type id (used for rebooking)
53 ;BSDXRADEXAM is used to store the Radiology Exam to which this appointment is tied to (optional)
54 ;
55 ;Return:
56 ; ADO.net Recordset having fields:
57 ; AppointmentID and ErrorNumber
58 ;
59 ; NB: Specifying BSDXLEN and BSDXEND is redundant. For future programmers
60 ; to sort out
61 ;
62 ;Test lines:
63 ;BSDX ADD NEW APPOINTMENT^3091122.0930^3091122.1000^370^Dr Office^30^EXAM^WALKIN
64 ;
65 ; Deal with optional arguments
66 S BSDXRADEXAM=$G(BSDXRADEXAM)
67 ;
68 ; Return Array; set Return and clear array
69 S BSDXY=$NA(^BSDXTMP($J))
70 K ^BSDXTMP($J)
71 ;
72 ; $ET
73 N $ET S $ET="G ETRAP^BSDX07"
74 ;
75 ; Counter
76 N BSDXI S BSDXI=0
77 ;
78 ; Lock BSDX node, only to synchronize access to the globals.
79 ; It's not expected that the error will ever happen as no filing
80 ; is supposed to take 5 seconds.
81 L +^BSDXAPPT(BSDXPATID):5 I '$T D ERR(BSDXI,"-1~Patient record is locked. Please contact technical support.") Q
82 ;
83 ; Header Node
84 S ^BSDXTMP($J,BSDXI)="I00020APPOINTMENTID^T00100ERRORID"_$C(30)
85 ;
86 ; Turn off SDAM APPT PROTOCOL BSDX Entries
87 N BSDXNOEV
88 S BSDXNOEV=1 ;Don't execute BSDX ADD APPOINTMENT protocol
89 ;
90 ; Set Error Message to be empty
91 N BSDXERR S BSDXERR=0
92 ;
93 ;;;test for error inside transaction. See if %ZTER works
94 I $G(BSDXDIE) S X=1/0
95 ;;;test
96 ;
97 ; -- Start and End Date Processing --
98 ; If C# sends the dates with extra zeros, remove them
99 S BSDXSTART=+BSDXSTART,BSDXEND=+BSDXEND
100 ; Are the dates valid? Must be FM Dates > than 2010
101 I BSDXSTART'>3100000 D ERR(BSDXI,"-2~BSDX07 Error: Invalid Start Time") Q
102 I BSDXEND'>3100000 D ERR(BSDXI,"-3~BSDX07 Error: Invalid End Time") Q
103 ;
104 ;; If Ending date doesn't have a time, this is an error --rm 1.5
105 ; I $L(BSDXEND,".")=1 D ERR(BSDXI,"-4~BSDX07 Error: Invalid End Time") Q
106 ;
107 ; If the Start Date is greater than the end date, swap dates
108 N BSDXTMP
109 I BSDXSTART>BSDXEND S BSDXTMP=BSDXEND,BSDXEND=BSDXSTART,BSDXSTART=BSDXTMP
110 ;
111 ; Check if the patient exists:
112 ; - DFN valid number?
113 ; - Valid Patient in file 2?
114 I '+BSDXPATID D ERR(BSDXI,"-5~BSDX07 Error: Invalid Patient ID") Q
115 I '$D(^DPT(BSDXPATID,0)) D ERR(BSDXI,"-6~BSDX07 Error: Invalid Patient ID") Q
116 ;
117 ;Validate Resource entry
118 I '$D(^BSDXRES("B",BSDXRES)) D ERR(BSDXI,"-7~BSDX07 Error: Invalid Resource ID") Q
119 N BSDXRESD ; Resource IEN
120 S BSDXRESD=$O(^BSDXRES("B",BSDXRES,0))
121 N BSDXRNOD ; Resouce zero node
122 S BSDXRNOD=$G(^BSDXRES(BSDXRESD,0))
123 I BSDXRNOD="" D ERR(BSDXI,"-8~BSDX07 Error: invalid Resource entry.") Q
124 ;
125 ; Walk-in (Unscheduled) Appointment?
126 N BSDXWKIN S BSDXWKIN=0
127 I BSDXATID="WALKIN" S BSDXWKIN=1
128 ; Reset Access Type ID if it doesn't say "WALKIN" and isn't a number
129 I BSDXATID'?.N&(BSDXATID'="WALKIN") S BSDXATID=""
130 ;
131 ; Now, check if PIMS has any issues with us making the appt using MAKECK
132 N BSDXSCD S BSDXSCD=$P(BSDXRNOD,U,4) ; Hosp Location IEN
133 N BSDXERR ; Variable to hold value of $$MAKE and $$MAKECK
134 N BSDXC ; Array to send to MAKE and MAKECK APIs
135 ; Only if we have a valid Hosp Location
136 I +BSDXSCD,$D(^SC(BSDXSCD,0)) D I +BSDXERR D ERR(BSDXI,"-10~BSDX07 Error: MAKECK^BSDXAPI returned error code: "_BSDXERR) Q ; no need for roll back
137 . S BSDXC("PAT")=BSDXPATID
138 . S BSDXC("CLN")=BSDXSCD
139 . S BSDXC("TYP")=3 ;3 for scheduled appts, 4 for walkins
140 . S:BSDXWKIN BSDXC("TYP")=4
141 . S BSDXC("ADT")=BSDXSTART
142 . S BSDXC("LEN")=BSDXLEN
143 . S BSDXC("OI")=$E($G(BSDXNOTE),1,150) ;File 44 has 150 character limit on OTHER field
144 . S BSDXC("OI")=$TR(BSDXC("OI"),";"," ") ;No semicolons allowed by MAKE^BSDXAPI
145 . S BSDXC("OI")=$$STRIP(BSDXC("OI")) ;Strip control characters from note
146 . S BSDXC("USR")=DUZ
147 . S BSDXERR=$$MAKECK^BSDXAPI(.BSDXC)
148 ;
149 ; Done with all checks, let's make appointment in BSDX APPOINTMENT
150 N BSDXAPPTID
151 S BSDXAPPTID=$$BSDXADD(BSDXSTART,BSDXEND,BSDXPATID,BSDXRESD,BSDXATID,BSDXRADEXAM)
152 I 'BSDXAPPTID D ERR(BSDXI,"-9~BSDX07 Error: Unable to add appointment to BSDX APPOINTMENT file.") Q ; no roll back needed! No appts made.
153 I BSDXNOTE]"" D BSDXWP(BSDXAPPTID,BSDXNOTE) ; no error checks are made here
154 ; I don't think it's important b/c users can detect right away if the WP
155 ; filing fails.
156 ;
157 I $G(BSDXSIMERR1) D ERR(BSDXI,"-11~BSDX07 Error: Simulated Error"),ROLLBACK(BSDXAPPTID,.BSDXC) Q ; UT Line
158 ;
159 ; Only if we have a valid Hosp Loc can we make an appointment in 2/44
160 ; Use BSDXC array from before.
161 ; NB: $$MAKE itself calls $$MAKECK to check again for being okay.
162 I +BSDXSCD,$D(^SC(BSDXSCD,0)) D I +BSDXERR D ERR(BSDXI,"-10~BSDX07 Error: MAKE^BSDXAPI returned error code: "_BSDXERR),ROLLBACK(BSDXAPPTID,.BSDXC) Q
163 . S BSDXERR=$$MAKE^BSDXAPI(.BSDXC)
164 . Q:BSDXERR
165 . D AVUPDT(BSDXSCD,BSDXSTART,BSDXLEN) ; Update RPMS Clinic availability
166 ;
167 ;Return Recordset
168 L -^BSDXAPPT(BSDXPATID)
169 S BSDXI=BSDXI+1
170 S ^BSDXTMP($J,BSDXI)=BSDXAPPTID_"^"_$C(30)
171 S BSDXI=BSDXI+1
172 S ^BSDXTMP($J,BSDXI)=$C(31)
173 Q
174STRIP(BSDXZ) ;Replace control characters with spaces
175 N BSDXI
176 F BSDXI=1:1:$L(BSDXZ) I (32>$A($E(BSDXZ,BSDXI))) S BSDXZ=$E(BSDXZ,1,BSDXI-1)_" "_$E(BSDXZ,BSDXI+1,999)
177 Q BSDXZ
178 ;
179BSDXADD(BSDXSTART,BSDXEND,BSDXPATID,BSDXRESD,BSDXATID,BSDXRADEXAM) ;ADD BSDX APPOINTMENT ENTRY
180 ;Returns ien in BSDXAPPT or 0 if failed
181 ;Create entry in BSDX APPOINTMENT
182 N BSDXAPPTID
183 S BSDXFDA(9002018.4,"+1,",.01)=BSDXSTART
184 S BSDXFDA(9002018.4,"+1,",.02)=BSDXEND
185 S BSDXFDA(9002018.4,"+1,",.05)=BSDXPATID
186 S BSDXFDA(9002018.4,"+1,",.07)=BSDXRESD
187 S BSDXFDA(9002018.4,"+1,",.08)=$G(DUZ)
188 S BSDXFDA(9002018.4,"+1,",.09)=$$NOW^XLFDT
189 S:BSDXATID="WALKIN" BSDXFDA(9002018.4,"+1,",.13)="y"
190 S:BSDXATID?.N BSDXFDA(9002018.4,"+1,",.06)=BSDXATID
191 S BSDXFDA(9002018.4,"+1,",.14)=$G(BSDXRADEXAM)
192 N BSDXIEN,BSDXMSG
193 D UPDATE^DIE("","BSDXFDA","BSDXIEN","BSDXMSG")
194 S BSDXAPPTID=+$G(BSDXIEN(1))
195 Q BSDXAPPTID
196 ;
197BSDXWP(BSDXAPPTID,BSDXNOTE) ;
198 ;Add WP field
199 N BSDXMSG
200 I BSDXNOTE]"" S BSDXNOTE(.5)=BSDXNOTE,BSDXNOTE=""
201 I $D(BSDXNOTE(0)) S BSDXNOTE(.5)=BSDXNOTE(0) K BSDXNOTE(0)
202 I $D(BSDXNOTE(.5)) D
203 . D WP^DIE(9002018.4,BSDXAPPTID_",",1,"","BSDXNOTE","BSDXMSG")
204 Q
205 ;
206ADDEVT(BSDXPATID,BSDXSTART,BSDXSC,BSDXSCDA) ;EP
207 ;Called by BSDX ADD APPOINTMENT protocol
208 ;BSDXSC=IEN of clinic in ^SC
209 ;BSDXSCDA=IEN for ^SC(BSDXSC,"S",BSDXSTART,1,BSDXSCDA). Use to get Length & Note
210 ;
211 N BSDXNOD,BSDXLEN,BSDXAPPTID,BSDXNODP,BSDXWKIN,BSDXRES
212 Q:+$G(BSDXNOEV)
213 I $D(^BSDXRES("ALOC",BSDXSC)) S BSDXRES=$O(^BSDXRES("ALOC",BSDXSC,0))
214 E I $D(^BSDXRES("ASSOC",BSDXSC)) S BSDXRES=$O(^BSDXRES("ASSOC",BSDXSC,0))
215 Q:'+$G(BSDXRES)
216 S BSDXNOD=$G(^SC(BSDXSC,"S",BSDXSTART,1,BSDXSCDA,0))
217 Q:BSDXNOD=""
218 S BSDXNODP=$G(^DPT(BSDXPATID,"S",BSDXSTART,0))
219 S BSDXWKIN=""
220 S:$P(BSDXNODP,U,7)=4 BSDXWKIN="WALKIN" ;Purpose of Visit field of DPT Appointment subfile
221 S BSDXLEN=$P(BSDXNOD,U,2)
222 Q:'+BSDXLEN
223 S BSDXEND=$$FMADD^XLFDT(BSDXSTART,0,0,BSDXLEN,0)
224 S BSDXAPPTID=$$BSDXADD(BSDXSTART,BSDXEND,BSDXPATID,BSDXRES,BSDXWKIN)
225 Q:'+BSDXAPPTID
226 S BSDXNOTE=$P(BSDXNOD,U,4)
227 I BSDXNOTE]"" D BSDXWP(BSDXAPPTID,BSDXNOTE)
228 D ADDEVT3(BSDXRES)
229 Q
230 ;
231ADDEVT3(BSDXRES) ;
232 ;Call RaiseEvent to notify GUI clients
233 N BSDXRESN
234 S BSDXRESN=$G(^BSDXRES(BSDXRES,0))
235 Q:BSDXRESN=""
236 S BSDXRESN=$P(BSDXRESN,"^")
237 ;D EVENT^BSDX23("SCHEDULE-"_BSDXRESN,"","","")
238 D EVENT^BMXMEVN("BSDX SCHEDULE",BSDXRESN)
239 Q
240 ;
241ROLLBACK(BSDXAPPTID,BSDXC) ; Private EP; Roll back appointment set
242 ; DO NOT USE except as an emergency measure - only if unforseen error occurs
243 ; Input:
244 ; Appointment ID to remove from ^BSDXAPPT
245 ; BSDXC array (see array format in $$MAKE^BSDXAPI)
246 ; NB: I am not sure whether I want to do $G to protect??
247 ; I send the variables to this EP from the Symbol Table in ETRAP
248 D BSDXDEL^BSDX07(BSDXAPPTID)
249 S:$D(BSDXC) %=$$UNMAKE^BSDXAPI(.BSDXC) ; rtn value always 0
250 QUIT
251 ;
252BSDXDEL(BSDXAPPTID) ;Private EP ; Deletes appointment BSDXAPPTID from ^BSDXAPPT
253 ; DO NOT USE except in emergencies to roll back an appointment set
254 N DA,DIK
255 S DIK="^BSDXAPPT(",DA=BSDXAPPTID
256 D ^DIK
257 Q
258 ;
259ERR(BSDXI,BSDXERR) ;Error processing - different from error trap.
260 S BSDXI=BSDXI+1
261 S BSDXERR=$TR(BSDXERR,"^","~")
262 S ^BSDXTMP($J,BSDXI)="0^"_BSDXERR_$C(30)
263 S BSDXI=BSDXI+1
264 S ^BSDXTMP($J,BSDXI)=$C(31)
265 L -^BSDXAPPT(BSDXPATID)
266 Q
267 ;
268ETRAP ;EP Error trap entry
269 N $ET S $ET="D ^%ZTER HALT" ; Emergency Error Trap
270 D ^%ZTER
271 S $EC="" ; Clear Error
272 I +$G(BSDXAPPTID) D ROLLBACK(BSDXAPPTID,.BSDXC) ; Rollback if BSDXAPPTID exists
273 ; Log error message and send to client
274 I '$D(BSDXI) N BSDXI S BSDXI=0
275 D ERR(BSDXI,"-100~BSDX07 Error: "_$G(%ZTERZE))
276 Q:$Q 1_U_"Mumps Error" Q
277 ;
278DAY ;;^SUN^MON^TUES^WEDNES^THURS^FRI^SATUR
279 ;
280DOW S %=$E(X,1,3),Y=$E(X,4,5),Y=Y>2&'(%#4)+$E("144025036146",Y)
281 F %=%:-1:281 S Y=%#4=1+1+Y
282 S Y=$E(X,6,7)+Y#7
283 Q
284 ;
285AVUPDT(BSDXSCD,BSDXSTART,BSDXLEN) ;Update RPMS Clinic availability
286 ;SEE SDM1
287 N Y,DFN
288 N SL,STARTDAY,X,SC,SB,HSI,SI,STR,SDDIF,SDMAX,SDDATE,SDDMAX,SDSDATE,CCXN,MXOK,COV,SDPROG
289 N X1,SDEDT,X2,SD,SM,SS,S,SDLOCK,ST,I
290 S Y=BSDXSCD,DFN=BSDXPATID
291 S SL=$G(^SC(+Y,"SL")),X=$P(SL,U,3),STARTDAY=$S($L(X):X,1:8),SC=Y,SB=STARTDAY-1/100,X=$P(SL,U,6),HSI=$S(X=1:X,X:X,1:4),SI=$S(X="":4,X<3:4,X:X,1:4),STR="#@!$* XXWVUTSRQPONMLKJIHGFEDCBA0123456789jklmnopqrstuvwxyz",SDDIF=$S(HSI<3:8/HSI,1:2) K Y
292 ;Determine maximum days for scheduling
293 S SDMAX(1)=$P($G(^SC(+SC,"SDP")),U,2) S:'SDMAX(1) SDMAX(1)=365
294 S (SDMAX,SDDMAX)=$$FMADD^XLFDT(DT,SDMAX(1))
295 S SDDATE=BSDXSTART
296 S SDSDATE=SDDATE,SDDATE=SDDATE\1
2971 ;L Q:$D(SDXXX) S CCXN=0 K MXOK,COV,SDPROT Q:DFN<0 S SC=+SC
298 Q:$D(SDXXX) S CCXN=0 K MXOK,COV,SDPROT Q:DFN<0 S SC=+SC
299 S X1=DT,SDEDT=365 S:$D(^SC(SC,"SDP")) SDEDT=$P(^SC(SC,"SDP"),"^",2)
300 S X2=SDEDT D C^%DTC S SDEDT=X
301 S Y=BSDXSTART
302EN1 S (X,SD)=Y,SM=0 D DOW
303S I '$D(^SC(SC,"ST",$P(SD,"."),1)) S SS=+$O(^SC(+SC,"T"_Y,SD)) Q:SS'>0 Q:^(SS,1)="" S ^SC(+SC,"ST",$P(SD,"."),1)=$E($P($T(DAY),U,Y+2),1,2)_" "_$E(SD,6,7)_$J("",SI+SI-6)_^(1),^(0)=$P(SD,".")
304 S S=BSDXLEN
305 ;Check if BSDXLEN evenly divisible by appointment length
306 S RPMSL=$P(SL,U)
307 I BSDXLEN<RPMSL S BSDXLEN=RPMSL
308 I BSDXLEN#RPMSL'=0 D
309 . S BSDXINC=BSDXLEN\RPMSL
310 . S BSDXINC=BSDXINC+1
311 . S BSDXLEN=RPMSL*BSDXINC
312 S SL=S_U_$P(SL,U,2,99)
313SC S SDLOCK=$S('$D(SDLOCK):1,1:SDLOCK+1) Q:SDLOCK>9
314 L +^SC(SC,"ST",$P(SD,"."),1):5 G:'$T SC
315 S SDLOCK=0,S=^SC(SC,"ST",$P(SD,"."),1)
316 S I=SD#1-SB*100,ST=I#1*SI\.6+($P(I,".")*SI),SS=SL*HSI/60*SDDIF+ST+ST
317 I (I<1!'$F(S,"["))&(S'["CAN") L -^SC(SC,"ST",$P(SD,"."),1) Q
318 I SM<7 S %=$F(S,"[",SS-1) S:'%!($P(SL,"^",6)<3) %=999 I $F(S,"]",SS)'<%!(SDDIF=2&$E(S,ST+ST+1,SS-1)["[") S SM=7
319 ;
320SP I ST+ST>$L(S),$L(S)<80 S S=S_" " G SP
321 S SDNOT=1
322 S ABORT=0
323 F I=ST+ST:SDDIF:SS-SDDIF D Q:ABORT
324 . S ST=$E(S,I+1) S:ST="" ST=" "
325 . S Y=$E(STR,$F(STR,ST)-2)
326 . I S["CAN"!(ST="X"&($D(^SC(+SC,"ST",$P(SD,"."),"CAN")))) S ABORT=1 Q
327 . I Y="" S ABORT=1 Q
328 . S:Y'?1NL&(SM<6) SM=6 S ST=$E(S,I+2,999) S:ST="" ST=" " S S=$E(S,1,I)_Y_ST
329 . Q
330 S ^SC(SC,"ST",$P(SD,"."),1)=S
331 L -^SC(SC,"ST",$P(SD,"."),1)
332 Q
Note: See TracBrowser for help on using the repository browser.