2d33366126559a2f3aa7e44ba0c52df594e10b81
[openemr.git] / interface / forms / fee_sheet / review / fee_sheet_justify_view_model.js
blob2d33366126559a2f3aa7e44ba0c52df594e10b81
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.create_problem=ko.observable(false);
56     retval.jsonify=function()
57     {
58         var json={};
59         json.code=this.code();
60         json.code_type=this.code_type();
61         json.description=this.description();
62         json.prob_id=this.prob_id();
63         json.create_problem=this.create_problem();
64         return json;
65     };
66     return retval;
69 function choose_search_diag(data,event,parent)
71     var code_key=data.code_type+"|"+data.code;
72     var existing=parent.added_keys[code_key];
73     if(typeof existing!='undefined')
74     {
75         parent.search_has_focus(false);
76         if(!existing.selected())
77         {
78              existing.selected(true);
79              update_diagnosis_options(parent.diagnosis_options,existing);
80         }
81     }
82     else
83     {
84         var new_justify=justify_entry({code: data.code, code_type:data.code_type, description:data.description, selected:true});
85         new_justify.source='search';
86         new_justify.source_idx=99999;
87         new_justify.create_problem(true);
88         parent.diagnosis_options.push(new_justify);
89         parent.added_keys[code_key]=new_justify;
90         update_diagnosis_options(parent.diagnosis_options,new_justify);
91         parent.search_has_focus(false);
92         parent.diagnosis_options.sort(priority_order);
93     }
94     parent.search_query("");
95     return true;
98 function search_change(data,model)
100     var search_query=data;
101     model.search_show(true);
102     var search_type= model.searchType().key;
103     var search_type_id=model.searchType().id;
104     if(search_query.length>0)
105         {
106             $.post(ajax_fee_sheet_search,
107                     {
108                         search_query:search_query,
109                         search_type: search_type,
110                         search_type_id: search_type_id
111                     },
112                     function(result)
113                     {
114                         model.search_results.removeAll();
115                         if(result.codes!=null)
116                             {
117                                 for(var idx=0;idx<result.codes.length;idx++)
118                                     {
119                                         var cur_code=result.codes[idx];
120                                         model.search_results.push(cur_code);
121                                     }                                
122                             }
123                     },
124                     "json");                
125         }
126         else
127         {
128             model.search_results.removeAll();
129         }
130    return true;
132 function search_focus(data,event)
134     data.search_show(true);
136 function search_blur(data,event)
138     data.search_show(false);
140 function update_justify(data,event)
142     event.preventDefault();
143     data.diagnosis_options.sort(priority_order);
144     var justify=[];
145     for(var idx=0;idx<data.diagnosis_options().length;idx++)
146     {
147         var cur=data.diagnosis_options()[idx];
148         if(cur.selected())
149         {
150             justify.push(cur.jsonify());
151         }
152     }
153     var skip_issues=data.duplicates().length>0;
154     top.restoreSession();
155     $.post(justify_ajax,{
156         skip_issues: skip_issues,
157         pid: data.patient_id,
158         encounter: data.encounter_id,
159         task: 'update',
160         billing_id: data.billing_id,
161         diags: JSON.stringify(justify)
162     },
163     function(data)
164         {
165             refresh_codes();
166         }
168     ); 
169     data.show(false);
172 function cancel_justify(data,event)
174     event.preventDefault();
175     data.show(false);
177 function sort_justify(data,event)
179     data.diagnosis_options.sort(priority_order);
182 var source_order={current:1,patient:2,common:3,search:4}
183 function priority_order(left,right)
185     if(left.priority()>right.priority())
186     {
187         return 1;
188     }
189     if(left.priority()<right.priority())
190     {
191         return -1;
192     }   
193     else
194     {
195         if(left.source==right.source)
196         {
197             if(left.source=='patient')
198             {
199                 if(left.encounter_issue()!=right.encounter_issue())
200                 {
201                     return left.encounter_issue() ? -1 : 1;
202                 }
203             }
204             if(left.source_idx>right.source_idx)
205             {
206                 return 1;
207             }
208             else
209             {
210                 return -1;
211             }
212         }
213         else
214         {
215             if(source_order[left.source]<source_order[right.source])
216             {
217                 return -1;
218             }
219             return 1;
220         }
221     }
223 function update_diagnosis_options(diagnosis_options,data)
225     var chosen=0;
226     for(var idx=0;idx<diagnosis_options().length;idx++)
227     {
228         var cur=diagnosis_options()[idx];
229         if(cur.selected())
230         {
231             chosen++;
232         }
233     }
234     var old_priority=99999;
235     if(data.selected())
236     {
237         data.priority(chosen);
238     }
239     else
240     {
241         old_priority=data.priority();
242         data.priority(99999);
243     }
244 //    diagnosis_options.sort(priority_order);
245     if(!data.selected())
246     {
247         for(idx=0;idx<diagnosis_options().length;idx++)
248             {
249                 cur=diagnosis_options()[idx];
250                 if((cur.priority()>old_priority) && cur.priority()!=99999)
251                 {
252                     cur.priority(cur.priority()-1);
253                 }
254             }
255     }
257 function check_justify(data,event,model)
259     
260     update_diagnosis_options(model.diagnosis_options,data);
261     return true;
263 function lookup_justify(current_justifications,entry)
265     for(var idx=0;idx<current_justifications.length;idx++)
266         {
267             var cur=current_justifications[idx];
268             if((cur.code()==entry.code())&&(cur.code_type()==entry.code_type()))
269                 {
270                     entry.priority(cur.priority());
271                     entry.selected(true);
272                 }
273         }
275 function setup_justify(model,current,patient,common)
277     model.added_keys={};
278     for(var idx=0;idx<current.length;idx++)
279     {
280         var cur_entry=current[idx];
281         var new_justify=new justify_entry(cur_entry);
282         if(typeof model.added_keys[new_justify.key()]=='undefined')
283         {
284             model.added_keys[new_justify.key()]=new_justify;
285             new_justify.selected(false);
286             lookup_justify(model.current_justify(),new_justify);
287             //new_justify.priority(idx+1);
288             new_justify.source='current';
289             new_justify.source_idx=idx;
290             model.diagnosis_options.push(new_justify);
291             
292         }
293     }
294     for(idx=0;idx<patient.length;idx++)
295     {
296         cur_entry=patient[idx];
297         if((cur_entry.code!=null) || cur_entry.code_type!="")
298         {         
299             new_justify=new justify_entry(cur_entry);
300             if(typeof model.added_keys[new_justify.key()]=='undefined')
301             {
302                 model.added_keys[new_justify.key()]=new_justify;
303                 if(new_justify.selected())
304                 {
305                     new_justify.encounter_issue(true);
306                 }
307                 new_justify.selected(false);
308                 lookup_justify(model.current_justify(),new_justify);
309                 new_justify.source='patient';
310                 new_justify.source_idx=idx;
311                 new_justify.prob_id(cur_entry.db_id);
312                 model.diagnosis_options.push(new_justify);        
313             }
314             else
315             {
316                 var entry=model.added_keys[new_justify.key()];
317                 if((entry.prob_id()!=null) &&(entry.prob_id()!=cur_entry.db_id))
318                 {
319                     new_justify.prob_id(cur_entry.db_id);
320                     if(model.duplicates().length==0)
321                         {
322                             model.duplicates.push(entry);
323                         }
324                     model.duplicates.push(new_justify);
325                 }
326                 else
327                 {
328                     entry.prob_id(cur_entry.db_id);
329                     entry.description(cur_entry.description);
330                     if(cur_entry.selected)
331                     {
332                         entry.encounter_issue(true);
333                     }
334                 }
335             }
336         }
337     }
338     for(idx=0;idx<common.length;idx++)
339     {
340         cur_entry=common[idx];
341         new_justify=new justify_entry(cur_entry);
342         if(typeof model.added_keys[new_justify.key()]=='undefined')
343         {
344             model.added_keys[new_justify.key()]=new_justify;
345             new_justify.selected(false);
346             lookup_justify(model.current_justify(),new_justify);
347             new_justify.source='common';
348             new_justify.source_idx=idx;
349             model.diagnosis_options.push(new_justify);        
350         }
351     }
352     model.diagnosis_options.sort(priority_order);
353     
355 function toggle_warning_details(data,event)
357     data.show_warning_details(!data.show_warning_details());
359 function fee_sheet_justify_view_model(billing_id,enc_id,pat_id,current_justify)
361     this.justify={
362                     billing_id:billing_id
363                     ,encounter_id: enc_id
364                     ,patient_id: pat_id
365                     ,diagnosis_options: ko.observableArray()
366                     ,current: ko.observableArray()
367                     ,patient: ko.observableArray()
368                     ,common: ko.observableArray()
369                     ,show: ko.observable(false)
370                     ,current_justify: ko.observable(current_justify)
371                     ,search_results: ko.observableArray()
372                     ,search_show: ko.observable(false).extend({throttle:300})
373                     ,search_has_focus: ko.observable(false)
374                     ,added_keys: {}
375                     ,search_query: ko.observable()
376                     ,diag_code_types: diag_code_types
377                     ,searchType: ko.observable()
378                     ,duplicates:ko.observableArray()
379                     ,show_warning_details:ko.observable(false)
380                   };
381     var vm=this.justify;
382     vm.search_query_throttled=ko.computed(vm.search_query).extend({throttle:300}).subscribe(function(data){search_change(data,vm)});
383     var mode='common';   
384     $.post(justify_ajax,{
385             pid: pat_id,
386             encounter: enc_id,
387             mode: mode,
388                 task: "retrieve"
389             },function(data){
390                 setup_justify(vm,data.current,data.patient,data.common);
391                 
392                 vm.show(true);
393                 vm.search_has_focus(true);
394             },
395             "json");
396     return this;