2 * knockout.js view model for fee sheet justification
4 * Copyright (C) 2013 Kevin Yeh <kevin.y@integralemr.com> and OEMR <www.oemr.org>
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>;.
18 * @author Kevin Yeh <kevin.y@integralemr.com>
19 * @link http://www.open-emr.org
21 function start_edit(data,event)
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)
34 data.edit_mode(false);
39 function search_key(data,event)
43 $(event.target).siblings(".search_results").find(".search_result_code:first").click();
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()
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();
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')
77 parent.search_has_focus(false);
78 if(!existing.selected())
80 existing.selected(true);
81 update_diagnosis_options(parent.diagnosis_options,existing);
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);
97 parent.search_query("");
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)
109 $.post(ajax_fee_sheet_search,
111 search_query:search_query,
112 search_type: search_type,
113 search_type_id: search_type_id
117 model.search_results.removeAll();
118 if(result.codes!=null)
120 for(var idx=0;idx<result.codes.length;idx++)
122 var cur_code=result.codes[idx];
123 model.search_results.push(cur_code);
131 model.search_results.removeAll();
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);
148 for(var idx=0;idx<data.diagnosis_options().length;idx++)
150 var cur=data.diagnosis_options()[idx];
153 justify.push(cur.jsonify());
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,
163 billing_id: data.billing_id,
164 diags: JSON.stringify(justify)
175 function cancel_justify(data,event)
177 event.preventDefault();
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())
192 if(left.priority()<right.priority())
198 if(left.source==right.source)
200 if(left.source=='patient')
202 if(left.encounter_issue()!=right.encounter_issue())
204 return left.encounter_issue() ? -1 : 1;
207 if(left.source_idx>right.source_idx)
218 if(source_order[left.source]<source_order[right.source])
226 function update_diagnosis_options(diagnosis_options,data)
229 for(var idx=0;idx<diagnosis_options().length;idx++)
231 var cur=diagnosis_options()[idx];
237 var old_priority=99999;
240 data.priority(chosen);
244 old_priority=data.priority();
245 data.priority(99999);
247 // diagnosis_options.sort(priority_order);
250 for(idx=0;idx<diagnosis_options().length;idx++)
252 cur=diagnosis_options()[idx];
253 if((cur.priority()>old_priority) && cur.priority()!=99999)
255 cur.priority(cur.priority()-1);
260 function check_justify(data,event,model)
263 update_diagnosis_options(model.diagnosis_options,data);
266 function lookup_justify(current_justifications,entry)
268 for(var idx=0;idx<current_justifications.length;idx++)
270 var cur=current_justifications[idx];
271 if((cur.code()==entry.code())&&(cur.code_type()==entry.code_type()))
273 entry.priority(cur.priority());
274 entry.selected(true);
278 function setup_justify(model,current,patient,common)
281 for(var idx=0;idx<current.length;idx++)
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')
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);
298 for(idx=0;idx<patient.length;idx++)
300 cur_entry=patient[idx];
301 if((cur_entry.code!=null) || cur_entry.code_type!="")
303 new_justify=new justify_entry(cur_entry);
304 if(typeof model.added_keys[new_justify.key()]=='undefined')
306 model.added_keys[new_justify.key()]=new_justify;
307 if(new_justify.selected())
309 new_justify.encounter_issue(true);
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);
321 var entry=model.added_keys[new_justify.key()];
322 if((entry.prob_id()!=null) &&(entry.prob_id()!=cur_entry.db_id))
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)
328 model.duplicates.push(entry);
330 model.duplicates.push(new_justify);
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)
339 entry.encounter_issue(true);
345 for(idx=0;idx<common.length;idx++)
347 cur_entry=common[idx];
348 new_justify=new justify_entry(cur_entry);
349 if(typeof model.added_keys[new_justify.key()]=='undefined')
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);
360 model.diagnosis_options.sort(priority_order);
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)
370 billing_id:billing_id
371 ,encounter_id: enc_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)
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)
390 vm.search_query_throttled=ko.computed(vm.search_query).extend({throttle:300}).subscribe(function(data){search_change(data,vm)});
392 $.post(justify_ajax,{
398 setup_justify(vm,data.current,data.patient,data.common);
401 vm.search_has_focus(true);