1+ /*
2+ * Copyright (c) 2013-2019 LabKey Corporation
3+ *
4+ * Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
5+ */
6+ Ext4 . define ( 'ONPRC_EHR.data.ClinicalEncountersClientStore' , {
7+ extend : 'EHR.data.DataEntryClientStore' ,
8+
9+ constructor : function ( ) {
10+ this . callParent ( arguments ) ;
11+
12+ this . on ( 'add' , this . onAddRecord , this ) ;
13+ } ,
14+
15+ onAddRecord : function ( store , records ) {
16+ Ext4 . each ( records , function ( record ) {
17+ this . onRecordUpdate ( record , [ 'procedureid' ] ) ;
18+ } , this ) ;
19+ } ,
20+
21+ afterEdit : function ( record , modifiedFieldNames ) {
22+ this . onRecordUpdate ( record , modifiedFieldNames ) ;
23+
24+ this . callParent ( arguments ) ;
25+ } ,
26+
27+ onRecordUpdate : function ( record , modifiedFieldNames ) {
28+ if ( record . get ( 'procedureid' ) ) {
29+ modifiedFieldNames = modifiedFieldNames || [ ] ;
30+
31+ var lookupRec = this . getProcedureRecord ( record . get ( 'procedureid' ) ) ;
32+ if ( ! lookupRec )
33+ return ;
34+
35+ if ( lookupRec . get ( 'remark' ) && record . get ( 'remark' ) == null ) {
36+ record . beginEdit ( ) ;
37+ record . set ( 'remark' , lookupRec . get ( 'remark' ) ) ;
38+ record . endEdit ( true ) ;
39+ }
40+
41+ if ( lookupRec . get ( 'name' ) && lookupRec . get ( 'name' ) == 'TMB breeding' ) {
42+ record . beginEdit ( ) ;
43+ record . set ( 'chargetype' , 'TMB' ) ;
44+ record . endEdit ( true ) ;
45+ }
46+ }
47+
48+ if ( modifiedFieldNames && ( modifiedFieldNames . indexOf ( 'Id' ) > - 1 || modifiedFieldNames . indexOf ( 'project' ) > - 1 || modifiedFieldNames . indexOf ( 'chargetype' ) > - 1 ) ) {
49+ if ( record . get ( 'objectid' ) ) {
50+ var toApply = {
51+ Id : record . get ( 'Id' ) ,
52+ project : record . get ( 'project' ) ,
53+ chargetype : record . get ( 'chargetype' )
54+ } ;
55+
56+ this . storeCollection . clientStores . each ( function ( cs ) {
57+ if ( cs . storeId == this . storeCollection . collectionId + '-' + 'encounters' ) {
58+ return ;
59+ }
60+
61+ var projectField = cs . getFields ( ) . get ( 'project' ) ;
62+ var chargeTypeField = cs . getFields ( ) . get ( 'chargetype' ) ;
63+ var hasChanges = false ;
64+
65+ if ( cs . getFields ( ) . get ( 'parentid' ) ) {
66+ if ( cs . getFields ( ) . get ( 'Id' ) || cs . getFields ( ) . get ( 'project' ) ) {
67+ cs . each ( function ( r ) {
68+ if ( r . get ( 'parentid' ) === record . get ( 'objectid' ) ) {
69+ var obj = { } ;
70+
71+ if ( projectField ) {
72+ if ( ! r . get ( 'project' ) || ( projectField . inheritFromParent && r . get ( 'project' ) !== record . get ( 'project' ) ) ) {
73+ obj . project = record . get ( 'project' ) ;
74+ }
75+ }
76+
77+ if ( chargeTypeField ) {
78+ if ( ! r . get ( 'chargetype' ) || ( chargeTypeField . inheritFromParent && r . get ( 'chargetype' ) !== record . get ( 'chargetype' ) ) ) {
79+ obj . chargetype = record . get ( 'chargetype' ) ;
80+ }
81+ }
82+
83+ if ( r . get ( 'Id' ) !== record . get ( 'Id' ) ) {
84+ obj . Id = record . get ( 'Id' ) ;
85+ }
86+
87+ if ( ! Ext4 . Object . isEmpty ( obj ) ) {
88+ r . beginEdit ( ) ;
89+ r . set ( obj ) ;
90+ r . endEdit ( true ) ;
91+ hasChanges = true ;
92+ }
93+ }
94+ } , this ) ;
95+ }
96+ }
97+
98+ if ( hasChanges ) {
99+ cs . fireEvent ( 'datachanged' , cs ) ;
100+ }
101+ } , this ) ;
102+ }
103+ }
104+ } ,
105+
106+ getProcedureRecord : function ( procedureId ) {
107+ var procedureStore = EHR . DataEntryUtils . getProceduresStore ( ) ;
108+ LDK . Assert . assertNotEmpty ( 'Unable to find procedureStore from ClinicalEncountersClientStore' , procedureStore ) ;
109+
110+ // If the store is not loaded this will call again. No need to do a load callback here just return undefined if not loaded.
111+ if ( LABKEY . ext4 . Util . hasStoreLoaded ( procedureStore ) ) {
112+ var procRecIdx = procedureStore . findExact ( 'rowid' , procedureId ) ;
113+ LDK . Assert . assertTrue ( 'Unable to find procedure record in ClinicalEncountersClientStore for procedureId: [' + procedureId + ']' , procRecIdx > - 1 ) ;
114+
115+ var procedureRec = procedureStore . getAt ( procRecIdx ) ;
116+ LDK . Assert . assertNotEmpty ( 'Unable to find procedure record from ClinicalEncountersClientStore. ProcedureId was: [' + procedureId + ']' , procedureRec ) ;
117+
118+ return procedureRec ;
119+ }
120+ return undefined ;
121+ }
122+ } ) ;
0 commit comments