Problem management fix for Fee Sheet Justify Search and syntax error fix in code_set_...
[openemr.git] / interface / forms / fee_sheet / review / fee_sheet_justify_view_model.js
blob19a2d332c2166864fe2f81ffe71fd7d2518e7af6
1 /**
2  * knockout.js view model for fee sheet justification
3  * 
4  * Copyright (C) 2013 Kevin Yeh <kevin.y@integralemr.com> and OEMR <www.oemr.org>
5  *
6  * LICENSE: This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
16  *
17  * @package OpenEMR
18  * @author  Kevin Yeh <kevin.y@integralemr.com>
19  * @link    http://www.open-emr.org
20  */
21 function start_edit(data,event)
23     data.edit_mode(true);
24     var elem=$(event.target).siblings("input").get(0);
26 function end_edit(data,event)
28     data.edit_mode(false);
30 function edit_key(data,event)
32     if(event.keyCode==13)
33         {
34             data.edit_mode(false);
35             return false;
36         }
37    return true;
39 function search_key(data,event)
41     if(event.keyCode==13)
42         {
43             $(event.target).siblings(".search_results").find(".search_result_code:first").click();
44             return false;
45         }
46     return true;
49 function justify_entry(json_object)
51     var retval=new code_entry(json_object);
52     retval.encounter_issue=ko.observable(false);
53     retval.edit_mode=ko.observable(false);
54     retval.prob_id=ko.observable();
55     retval.allowed_to_create_problem_from_diagnosis=ko.observable();
56     retval.create_problem=ko.observable(false);
57     retval.jsonify=function()
58     {
59         var json={};
60         json.code=this.code();
61         json.code_type=this.code_type();
62         json.description=this.description();
63         json.allowed_to_create_problem_from_diagnosis=this.allowed_to_create_problem_from_diagnosis();
64         json.prob_id=this.prob_id();
65         json.create_problem=this.create_problem();
66         return json;
67     };
68     return retval;
71 function choose_search_diag(data,event,parent)
73     var code_key=data.code_type+"|"+data.code;
74     var existing=parent.added_keys[code_key];
75     if(typeof existing!='undefined')
76     {
77         parent.search_has_focus(false);
78         if(!existing.selected())
79         {
80              existing.selected(true);
81              update_diagnosis_options(parent.diagnosis_options,existing);
82         }
83     }
84     else
85     {
86         var new_justify=justify_entry({code: data.code, code_type:data.code_type, description:data.description, selected:true});
87         new_justify.source='search';
88         new_justify.source_idx=99999;
89         new_justify.create_problem(data.allowed_to_create_problem_from_diagnosis);
90         new_justify.allowed_to_create_problem_from_diagnosis(data.allowed_to_create_problem_from_diagnosis);
91         parent.diagnosis_options.push(new_justify);
92         parent.added_keys[code_key]=new_justify;
93         update_diagnosis_options(parent.diagnosis_options,new_justify);
94         parent.search_has_focus(false);
95         parent.diagnosis_options.sort(priority_order);
96     }
97     parent.search_query("");
98     return true;
101 function search_change(data,model)
103     var search_query=data;
104     model.search_show(true);
105     var search_type= model.searchType().key;
106     var search_type_id=model.searchType().id;
107     if(search_query.length>0)
108         {
109             $.post(ajax_fee_sheet_search,
110                     {
111                         search_query:search_query,
112                         search_type: search_type,
113                         search_type_id: search_type_id
114                     },
115                     function(result)
116                     {
117                         model.search_results.removeAll();
118                         if(result.codes!=null)
119                             {
120                                 for(var idx=0;idx<result.codes.length;idx++)
121                                     {
122                                         var cur_code=result.codes[idx];
123                                         model.search_results.push(cur_code);
124                                     }                                
125                             }
126                     },
127                     "json");                
128         }
129         else
130         {
131             model.search_results.removeAll();
132         }
133    return true;
135 function search_focus(data,event)
137     data.search_show(true);
139 function search_blur(data,event)
141     data.search_show(false);
143 function update_justify(data,event)
145     event.preventDefault();
146     data.diagnosis_options.sort(priority_order);
147     var justify=[];
148     for(var idx=0;idx<data.diagnosis_options().length;idx++)
149     {
150         var cur=data.diagnosis_options()[idx];
151         if(cur.selected())
152         {
153             justify.push(cur.jsonify());
154         }
155     }
156     var skip_issues=data.duplicates().length>0;
157     top.restoreSession();
158     $.post(justify_ajax,{
159         skip_issues: skip_issues,
160         pid: data.patient_id,
161         encounter: data.encounter_id,
162         task: 'update',
163         billing_id: data.billing_id,
164         diags: JSON.stringify(justify)
165     },
166     function(data)
167         {
168             refresh_codes();
169         }
171     ); 
172     data.show(false);
175 function cancel_justify(data,event)
177     event.preventDefault();
178     data.show(false);
180 function sort_justify(data,event)
182     data.diagnosis_options.sort(priority_order);
185 var source_order={current:1,patient:2,common:3,search:4}
186 function priority_order(left,right)
188     if(left.priority()>right.priority())
189     {
190         return 1;
191     }
192     if(left.priority()<right.priority())
193     {
194         return -1;
195     }   
196     else
197     {
198         if(left.source==right.source)
199         {
200             if(left.source=='patient')
201             {
202                 if(left.encounter_issue()!=right.encounter_issue())
203                 {
204                     return left.encounter_issue() ? -1 : 1;
205                 }
206             }
207             if(left.source_idx>right.source_idx)
208             {
209                 return 1;
210             }
211             else
212             {
213                 return -1;
214             }
215         }
216         else
217         {
218             if(source_order[left.source]<source_order[right.source])
219             {
220                 return -1;
221             }
222             return 1;
223         }
224     }
226 function update_diagnosis_options(diagnosis_options,data)
228     var chosen=0;
229     for(var idx=0;idx<diagnosis_options().length;idx++)
230     {
231         var cur=diagnosis_options()[idx];
232         if(cur.selected())
233         {
234             chosen++;
235         }
236     }
237     var old_priority=99999;
238     if(data.selected())
239     {
240         data.priority(chosen);
241     }
242     else
243     {
244         old_priority=data.priority();
245         data.priority(99999);
246     }
247 //    diagnosis_options.sort(priority_order);
248     if(!data.selected())
249     {
250         for(idx=0;idx<diagnosis_options().length;idx++)
251             {
252                 cur=diagnosis_options()[idx];
253                 if((cur.priority()>old_priority) && cur.priority()!=99999)
254                 {
255                     cur.priority(cur.priority()-1);
256                 }
257             }
258     }
260 function check_justify(data,event,model)
262     
263     update_diagnosis_options(model.diagnosis_options,data);
264     return true;
266 function lookup_justify(current_justifications,entry)
268     for(var idx=0;idx<current_justifications.length;idx++)
269         {
270             var cur=current_justifications[idx];
271             if((cur.code()==entry.code())&&(cur.code_type()==entry.code_type()))
272                 {
273                     entry.priority(cur.priority());
274                     entry.selected(true);
275                 }
276         }
278 function setup_justify(model,current,patient,common)
280     model.added_keys={};
281     for(var idx=0;idx<current.length;idx++)
282     {
283         var cur_entry=current[idx];
284         var new_justify=new justify_entry(cur_entry);
285         if(typeof model.added_keys[new_justify.key()]=='undefined')
286         {
287             model.added_keys[new_justify.key()]=new_justify;
288             new_justify.selected(false);
289             lookup_justify(model.current_justify(),new_justify);
290             //new_justify.priority(idx+1);
291             new_justify.source='current';
292             new_justify.source_idx=idx;
293             new_justify.allowed_to_create_problem_from_diagnosis(cur_entry.allowed_to_create_problem_from_diagnosis);
294             model.diagnosis_options.push(new_justify);
295             
296         }
297     }
298     for(idx=0;idx<patient.length;idx++)
299     {
300         cur_entry=patient[idx];
301         if((cur_entry.code!=null) || cur_entry.code_type!="")
302         {         
303             new_justify=new justify_entry(cur_entry);
304             if(typeof model.added_keys[new_justify.key()]=='undefined')
305             {
306                 model.added_keys[new_justify.key()]=new_justify;
307                 if(new_justify.selected())
308                 {
309                     new_justify.encounter_issue(true);
310                 }
311                 new_justify.selected(false);
312                 lookup_justify(model.current_justify(),new_justify);
313                 new_justify.source='patient';
314                 new_justify.source_idx=idx;
315                 new_justify.prob_id(cur_entry.db_id);
316                 new_justify.allowed_to_create_problem_from_diagnosis(cur_entry.allowed_to_create_problem_from_diagnosis);
317                 model.diagnosis_options.push(new_justify);        
318             }
319             else
320             {
321                 var entry=model.added_keys[new_justify.key()];
322                 if((entry.prob_id()!=null) &&(entry.prob_id()!=cur_entry.db_id))
323                 {
324                     new_justify.prob_id(cur_entry.db_id);
325                     new_justify.allowed_to_create_problem_from_diagnosis(cur_entry.allowed_to_create_problem_from_diagnosis);
326                     if(model.duplicates().length==0)
327                         {
328                             model.duplicates.push(entry);
329                         }
330                     model.duplicates.push(new_justify);
331                 }
332                 else
333                 {
334                     entry.prob_id(cur_entry.db_id);
335                     entry.allowed_to_create_problem_from_diagnosis(cur_entry.allowed_to_create_problem_from_diagnosis);
336                     entry.description(cur_entry.description);
337                     if(cur_entry.selected)
338                     {
339                         entry.encounter_issue(true);
340                     }
341                 }
342             }
343         }
344     }
345     for(idx=0;idx<common.length;idx++)
346     {
347         cur_entry=common[idx];
348         new_justify=new justify_entry(cur_entry);
349         if(typeof model.added_keys[new_justify.key()]=='undefined')
350         {
351             model.added_keys[new_justify.key()]=new_justify;
352             new_justify.selected(false);
353             lookup_justify(model.current_justify(),new_justify);
354             new_justify.source='common';
355             new_justify.source_idx=idx;
356             new_justify.allowed_to_create_problem_from_diagnosis(cur_entry.allowed_to_create_problem_from_diagnosis);
357             model.diagnosis_options.push(new_justify);        
358         }
359     }
360     model.diagnosis_options.sort(priority_order);
361     
363 function toggle_warning_details(data,event)
365     data.show_warning_details(!data.show_warning_details());
367 function fee_sheet_justify_view_model(billing_id,enc_id,pat_id,current_justify)
369     this.justify={
370                     billing_id:billing_id
371                     ,encounter_id: enc_id
372                     ,patient_id: pat_id
373                     ,diagnosis_options: ko.observableArray()
374                     ,current: ko.observableArray()
375                     ,patient: ko.observableArray()
376                     ,common: ko.observableArray()
377                     ,show: ko.observable(false)
378                     ,current_justify: ko.observable(current_justify)
379                     ,search_results: ko.observableArray()
380                     ,search_show: ko.observable(false).extend({throttle:300})
381                     ,search_has_focus: ko.observable(false)
382                     ,added_keys: {}
383                     ,search_query: ko.observable()
384                     ,diag_code_types: diag_code_types
385                     ,searchType: ko.observable()
386                     ,duplicates:ko.observableArray()
387                     ,show_warning_details:ko.observable(false)
388                   };
389     var vm=this.justify;
390     vm.search_query_throttled=ko.computed(vm.search_query).extend({throttle:300}).subscribe(function(data){search_change(data,vm)});
391     var mode='common';   
392     $.post(justify_ajax,{
393             pid: pat_id,
394             encounter: enc_id,
395             mode: mode,
396                 task: "retrieve"
397             },function(data){
398                 setup_justify(vm,data.current,data.patient,data.common);
399                 
400                 vm.show(true);
401                 vm.search_has_focus(true);
402             },
403             "json");
404     return this;