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.create_problem=ko.observable(false);
56 retval.jsonify=function()
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();
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')
75 parent.search_has_focus(false);
76 if(!existing.selected())
78 existing.selected(true);
79 update_diagnosis_options(parent.diagnosis_options,existing);
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);
94 parent.search_query("");
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)
106 $.post(ajax_fee_sheet_search,
108 search_query:search_query,
109 search_type: search_type,
110 search_type_id: search_type_id
114 model.search_results.removeAll();
115 if(result.codes!=null)
117 for(var idx=0;idx<result.codes.length;idx++)
119 var cur_code=result.codes[idx];
120 model.search_results.push(cur_code);
128 model.search_results.removeAll();
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);
145 for(var idx=0;idx<data.diagnosis_options().length;idx++)
147 var cur=data.diagnosis_options()[idx];
150 justify.push(cur.jsonify());
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,
160 billing_id: data.billing_id,
161 diags: JSON.stringify(justify)
172 function cancel_justify(data,event)
174 event.preventDefault();
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())
189 if(left.priority()<right.priority())
195 if(left.source==right.source)
197 if(left.source=='patient')
199 if(left.encounter_issue()!=right.encounter_issue())
201 return left.encounter_issue() ? -1 : 1;
204 if(left.source_idx>right.source_idx)
215 if(source_order[left.source]<source_order[right.source])
223 function update_diagnosis_options(diagnosis_options,data)
226 for(var idx=0;idx<diagnosis_options().length;idx++)
228 var cur=diagnosis_options()[idx];
234 var old_priority=99999;
237 data.priority(chosen);
241 old_priority=data.priority();
242 data.priority(99999);
244 // diagnosis_options.sort(priority_order);
247 for(idx=0;idx<diagnosis_options().length;idx++)
249 cur=diagnosis_options()[idx];
250 if((cur.priority()>old_priority) && cur.priority()!=99999)
252 cur.priority(cur.priority()-1);
257 function check_justify(data,event,model)
260 update_diagnosis_options(model.diagnosis_options,data);
263 function lookup_justify(current_justifications,entry)
265 for(var idx=0;idx<current_justifications.length;idx++)
267 var cur=current_justifications[idx];
268 if((cur.code()==entry.code())&&(cur.code_type()==entry.code_type()))
270 entry.priority(cur.priority());
271 entry.selected(true);
275 function setup_justify(model,current,patient,common)
278 for(var idx=0;idx<current.length;idx++)
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')
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);
294 for(idx=0;idx<patient.length;idx++)
296 cur_entry=patient[idx];
297 if((cur_entry.code!=null) || cur_entry.code_type!="")
299 new_justify=new justify_entry(cur_entry);
300 if(typeof model.added_keys[new_justify.key()]=='undefined')
302 model.added_keys[new_justify.key()]=new_justify;
303 if(new_justify.selected())
305 new_justify.encounter_issue(true);
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);
316 var entry=model.added_keys[new_justify.key()];
317 if((entry.prob_id()!=null) &&(entry.prob_id()!=cur_entry.db_id))
319 new_justify.prob_id(cur_entry.db_id);
320 if(model.duplicates().length==0)
322 model.duplicates.push(entry);
324 model.duplicates.push(new_justify);
328 entry.prob_id(cur_entry.db_id);
329 entry.description(cur_entry.description);
330 if(cur_entry.selected)
332 entry.encounter_issue(true);
338 for(idx=0;idx<common.length;idx++)
340 cur_entry=common[idx];
341 new_justify=new justify_entry(cur_entry);
342 if(typeof model.added_keys[new_justify.key()]=='undefined')
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);
352 model.diagnosis_options.sort(priority_order);
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)
362 billing_id:billing_id
363 ,encounter_id: enc_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)
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)
382 vm.search_query_throttled=ko.computed(vm.search_query).extend({throttle:300}).subscribe(function(data){search_change(data,vm)});
384 $.post(justify_ajax,{
390 setup_justify(vm,data.current,data.patient,data.common);
393 vm.search_has_focus(true);