sort billing items so that the provider can be more consistently chosen
[openemr.git] / library / freeb / Procedure.class.php
blobc9c14d152e9517820c85165629ff324b965ff517
1 <?php
2 require_once("DataObjectBase.class.php");
3 require_once("xmlrpc.inc");
4 class Procedure Extends DataObjectBase {
6 function Procedure($xuser) {
7 parent::DataObjectBase($xuser);
8 $this->_addFunc("isusingclearinghouse", array( "name" => "FreeB.FBProcedure.isUsingClearingHouse",
9 "sig" => array(XMLRPCSTRING,XMLRPCINT),
10 "doc" => ""));
11 $this->_addFunc("cpt4code", array( "name" => "FreeB.FBProcedure.CPT4Code",
12 "sig" => array(XMLRPCSTRING,XMLRPCINT),
13 "doc" => ""));
14 $this->_addFunc("cpt5code", array( "name" => "FreeB.FBProcedure.CPT5Code",
15 "sig" => array(XMLRPCSTRING,XMLRPCINT),
16 "doc" => ""));
17 $this->_addFunc("cptunits", array( "name" => "FreeB.FBProcedure.CPTUnits",
18 "sig" => array(XMLRPCSTRING,XMLRPCINT),
19 "doc" => ""));
20 $this->_addFunc("cptmodifier", array( "name" => "FreeB.FBProcedure.CPTModifier",
21 "sig" => array(XMLRPCSTRING,XMLRPCINT),
22 "doc" => ""));
23 $this->_addFunc("cptcharges", array( "name" => "FreeB.FBProcedure.CPTCharges",
24 "sig" => array(XMLRPCSTRING,XMLRPCINT),
25 "doc" => ""));
26 $this->_addFunc("cptemergency", array( "name" => "FreeB.FBProcedure.CPTEmergency",
27 "sig" => array(XMLRPCSTRING,XMLRPCINT),
28 "doc" => ""));
29 $this->_addFunc("cptcob", array( "name" => "FreeB.FBProcedure.CPTCOB",
30 "sig" => array(XMLRPCSTRING,XMLRPCINT),
31 "doc" => ""));
32 $this->_addFunc("cptepsdt", array( "name" => "FreeB.FBProcedure.CPTEPSDT",
33 "sig" => array(XMLRPCSTRING,XMLRPCINT),
34 "doc" => ""));
35 $this->_addFunc("amountpaid", array( "name" => "FreeB.FBProcedure.AmountPaid",
36 "sig" => array(XMLRPCSTRING,XMLRPCARRAY),
37 "doc" => ""));
38 $this->_addFunc("typeofservice", array( "name" => "FreeB.FBProcedure.TypeOfService",
39 "sig" => array(XMLRPCSTRING,XMLRPCINT),
40 "doc" => ""));
41 $this->_addFunc("priorauth", array( "name" => "FreeB.FBProcedure.PriorAuth",
42 "sig" => array(XMLRPCSTRING,XMLRPCINT),
43 "doc" => ""));
44 $this->_addFunc("dateofservicestart", array( "name" => "FreeB.FBProcedure.DateOfServiceStart",
45 "sig" => array(XMLRPCSTRING,XMLRPCINT),
46 "doc" => ""));
47 $this->_addFunc("dateofserviceend", array( "name" => "FreeB.FBProcedure.DateOfServiceEnd",
48 "sig" => array(XMLRPCSTRING,XMLRPCINT),
49 "doc" => ""));
50 $this->_addFunc("dateofhospitalstart", array( "name" => "FreeB.FBProcedure.DateOfHospitalStart",
51 "sig" => array(XMLRPCSTRING,XMLRPCINT),
52 "doc" => ""));
53 $this->_addFunc("ishospitalized", array( "name" => "FreeB.FBProcedure.isHospitalized",
54 "sig" => array(XMLRPCSTRING,XMLRPCINT),
55 "doc" => ""));
56 $this->_addFunc("isoutsidelab", array( "name" => "FreeB.FBProcedure.isOutsideLab",
57 "sig" => array(XMLRPCSTRING,XMLRPCINT),
58 "doc" => ""));
59 $this->_addFunc("outsidelabcharges", array( "name" => "FreeB.FBProcedure.OutsideLabCharges",
60 "sig" => array(XMLRPCSTRING,XMLRPCINT),
61 "doc" => ""));
62 $this->_addFunc("dateofhospitalend", array( "name" => "FreeB.FBProcedure.DateOfHospitalEnd",
63 "sig" => array(XMLRPCSTRING,XMLRPCINT),
64 "doc" => ""));
65 $this->_addFunc("medicaidresubmissioncode", array( "name" => "FreeB.FBProcedure.MedicaidResubmissionCode",
66 "sig" => array(XMLRPCSTRING,XMLRPCINT),
67 "doc" => ""));
68 $this->_addFunc("medicaidoriginalreference",array( "name" => "FreeB.FBProcedure.MedicaidOriginalReference",
69 "sig" => array(XMLRPCSTRING,XMLRPCINT),
70 "doc" => ""));
71 $this->_addFunc("weightgrams", array( "name" => "FreeB.FBProcedure.WeightGrams",
72 "sig" => array(XMLRPCSTRING,XMLRPCINT,XMLRPCSTRING),
73 "doc" => ""));
74 $this->_addFunc("weightpounds", array( "name" => "FreeB.FBProcedure.WeightPounds",
75 "sig" => array(XMLRPCSTRING,XMLRPCINT),
76 "doc" => ""));
77 $this->_addFunc("hcfalocaluse10d", array( "name" => "FreeB.FBProcedure.HCFALocalUse10d",
78 "sig" => array(XMLRPCSTRING,XMLRPCINT),
79 "doc" => ""));
80 $this->_addFunc("hcfalocaluse19", array( "name" => "FreeB.FBProcedure.HCFALocalUse19",
81 "sig" => array(XMLRPCSTRING,XMLRPCINT),
82 "doc" => ""));
83 $this->_addFunc("procarray", array( "name" => "FreeB.FBProcedure.ProcArray",
84 "sig" => array(XMLRPCSTRING,XMLRPCSTRING),
85 "doc" => ""));
86 $this->_addFunc("diagarray", array( "name" => "FreeB.FBProcedure.DiagArray",
87 "sig" => array(XMLRPCSTRING,XMLRPCINT),
88 "doc" => ""));
89 $this->_addFunc("patientkey", array( "name" => "FreeB.FBProcedure.PatientKey",
90 "sig" => array(XMLRPCSTRING,XMLRPCINT),
91 "doc" => ""));
92 $this->_addFunc("practicekey", array( "name" => "FreeB.FBProcedure.PracticeKey",
93 "sig" => array(XMLRPCSTRING,XMLRPCINT),
94 "doc" => ""));
95 $this->_addFunc("providerkey", array( "name" => "FreeB.FBProcedure.ProviderKey",
96 "sig" => array(XMLRPCSTRING,XMLRPCINT),
97 "doc" => ""));
98 $this->_addFunc("referringproviderkey", array( "name" => "FreeB.FBPatient.ReferringProviderKey",
99 "sig" => array(XMLRPCSTRING,XMLRPCINT),
100 "doc" => ""));
101 $this->_addFunc("insuredkey", array( "name" => "FreeB.FBProcedure.InsuredKey",
102 "sig" => array(XMLRPCSTRING,XMLRPCINT),
103 "doc" => ""));
104 $this->_addFunc("otherinsuredkey", array( "name" => "FreeB.FBProcedure.OtherInsuredKey",
105 "sig" => array(XMLRPCSTRING,XMLRPCINT),
106 "doc" => ""));
107 $this->_addFunc("payerkey", array( "name" => "FreeB.FBProcedure.PayerKey",
108 "sig" => array(XMLRPCSTRING,XMLRPCINT),
109 "doc" => ""));
110 $this->_addFunc("secondpayerkey", array( "name" => "FreeB.FBProcedure.SecondPayerKey",
111 "sig" => array(XMLRPCSTRING,XMLRPCINT),
112 "doc" => ""));
113 $this->_addFunc("facilitykey", array( "name" => "FreeB.FBProcedure.FacilityKey",
114 "sig" => array(XMLRPCSTRING,XMLRPCINT),
115 "doc" => ""));
116 $this->_addFunc("billingcontactkey",array( "name" => "FreeB.FBProcedure.BillingContactKey",
117 "sig" => array(XMLRPCSTRING,XMLRPCINT),
118 "doc" => ""));
119 $this->_addFunc("billingservicekey",array( "name" => "FreeB.FBProcedure.BillingServiceKey",
120 "sig" => array(XMLRPCSTRING,XMLRPCINT),
121 "doc" => ""));
122 $this->_addFunc("isusingbillingservice",array( "name" => "FreeB.FBProcedure.isUsingBillingService",
123 "sig" => array(XMLRPCSTRING,XMLRPCINT),
124 "doc" => ""));
125 $this->_addFunc("clearinghousekey", array( "name" => "FreeB.FBProcedure.ClearingHouseKey",
126 "sig" => array(XMLRPCSTRING,XMLRPCINT),
127 "doc" => ""));
131 function isusingclearinghouse($m) {
133 $err="";
135 $retval = false;
137 // if we generated an error, create an error return response
138 if ($err) {
139 return $this->_handleError($err);
141 else {
142 // otherwise, we create the right response
143 // with the state name
144 return new xmlrpcresp(new xmlrpcval($retval,"i4"));
148 function cpt4code($m) {
150 $err="";
151 $retval = "";
152 $obj= $m->getparam(0);
153 $key = $obj->getval();
155 $sql = "SELECT * FROM billing where id = '" . $key . "'";
156 //echo $sql;
157 $db = $GLOBALS['adodb']['db'];
158 $results = $db->Execute($sql);
160 if (!$results) {
161 $err = $db->ErrorMsg();
163 else {
164 if (!$results->EOF) {
165 $retval = $results->fields['code'];
169 // if we generated an error, create an error return response
170 if ($err) {
171 return $this->_handleError($err);
173 else {
174 // otherwise, we create the right response
175 // with the state name
176 return new xmlrpcresp(new xmlrpcval($retval,"string"));
180 function cpt5code($m) {
182 $err="";
184 $pkey = false;
186 // if we generated an error, create an error return response
187 if ($err) {
188 return $this->_handleError($err);
190 else {
191 // otherwise, we create the right response
192 // with the state name
193 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
197 function cptunits($m) {
199 $err="";
200 $retval = "";
201 $obj= $m->getparam(0);
202 $key = $obj->getval();
204 $sql = "SELECT * FROM billing where id = '" . $key . "'";
205 //echo $sql;
206 $db = $GLOBALS['adodb']['db'];
207 $results = $db->Execute($sql);
209 if (!$results) {
210 $err = $db->ErrorMsg();
212 else {
213 if (!$results->EOF) {
214 $retval = $results->fields['units'];
218 if (empty ($retval) || $retval < 1) {
219 $retval = 1;
222 // if we generated an error, create an error return response
223 if ($err) {
224 return $this->_handleError($err);
226 else {
227 // otherwise, we create the right response
228 // with the state name
229 return new xmlrpcresp(new xmlrpcval($retval,"string"));
234 function cptmodifier($m) {
236 $err="";
237 $retval = "";
238 $obj= $m->getparam(0);
239 $key = $obj->getval();
241 $sql = "SELECT * FROM billing where id = '" . $key . "'";
242 //echo $sql;
243 $db = $GLOBALS['adodb']['db'];
244 $results = $db->Execute($sql);
246 if (!$results) {
247 $err = $db->ErrorMsg();
249 else {
250 if (!$results->EOF) {
251 $retval = $results->fields['modifier'];
255 // if we generated an error, create an error return response
256 if ($err) {
257 return $this->_handleError($err);
259 else {
260 // otherwise, we create the right response
261 // with the state name
262 return new xmlrpcresp(new xmlrpcval($retval,"string"));
267 function cptcharges($m) {
269 $err="";
271 $obj= $m->getparam(0);
272 $key = $obj->getval();
274 $sql = "SELECT * FROM billing where id = '" . $key . "'";
275 //echo $sql;
276 $db = $GLOBALS['adodb']['db'];
277 $results = $db->Execute($sql);
279 if (!$results) {
280 $err = $db->ErrorMsg();
282 else {
283 if (!$results->EOF) {
284 $retval = $results->fields['fee'];
288 // if we generated an error, create an error return response
289 if ($err) {
290 return $this->_handleError($err);
292 else {
293 // otherwise, we create the right response
294 // with the state name
295 return new xmlrpcresp(new xmlrpcval($retval,"double"));
299 function cptemergency($m) {
301 $err="";
303 $pkey = "";
305 // if we generated an error, create an error return response
306 if ($err) {
307 return $this->_handleError($err);
309 else {
310 // otherwise, we create the right response
311 // with the state name
312 return new xmlrpcresp(new xmlrpcval($pkey));
316 function cptcob($m) {
318 $err="";
320 $pkey = "";
322 // if we generated an error, create an error return response
323 if ($err) {
324 return $this->_handleError($err);
326 else {
327 // otherwise, we create the right response
328 // with the state name
329 return new xmlrpcresp(new xmlrpcval($pkey));
333 function cptepsdt($m) {
335 $err="";
337 $pkey = "";
339 // if we generated an error, create an error return response
340 if ($err) {
341 return $this->_handleError($err);
343 else {
344 // otherwise, we create the right response
345 // with the state name
346 return new xmlrpcresp(new xmlrpcval($pkey));
350 function amountpaid($m) {
352 $err="";
354 $obj= $m->getparam(0);
355 $key = $obj->getval();
357 $sql = "SELECT * FROM billing where encounter = '" . $_SESSION['billkey'] . "' and pid = '" . $_SESSION['patient_id'] . "' and code_type = 'COPAY'";
358 //echo $sql;
359 $db = $GLOBALS['adodb']['db'];
360 $results = $db->Execute($sql);
362 if (!$results) {
363 $err = $db->ErrorMsg();
365 else {
366 if (!$results->EOF) {
367 $retval = $results->fields['fee'];
371 if (empty($retval)) {
372 $retval = "0000";
375 $retval = str_replace(".","",$retval);
377 // if we generated an error, create an error return response
378 if ($err) {
379 return $this->_handleError($err);
381 else {
382 // otherwise, we create the right response
383 // with the state name
384 return new xmlrpcresp(new xmlrpcval($retval,"i4"));
388 function typeofservice($m) {
390 $err="";
391 $retval = "";
392 $obj= $m->getparam(0);
393 $key = $obj->getval();
395 //phased out by HIPPA, use cpt modifiers instead
396 $retval = "";
397 // if we generated an error, create an error return response
398 if ($err) {
399 return $this->_handleError($err);
401 else {
402 // otherwise, we create the right response
403 // with the state name
404 return new xmlrpcresp(new xmlrpcval($retval,"string"));
408 function priorauth($m) {
410 $err="";
411 $retval = "";
412 $obj= $m->getparam(0);
413 $key = $obj->getval();
415 $sql = "SELECT * FROM forms JOIN form_prior_auth as fpa on fpa.id = forms.form_id where forms.encounter = '" . $_SESSION['billkey'] . "' and forms.pid = '" . $_SESSION['patient_id'] . "' and forms.formdir = 'prior_auth' order by forms.date";
416 //echo $sql;
417 $db = $GLOBALS['adodb']['db'];
418 $results = $db->Execute($sql);
420 if (!$results) {
421 $err = $db->ErrorMsg();
423 else {
424 if (!$results->EOF) {
425 $retval = $results->fields['prior_auth_number'];
428 // if we generated an error, create an error return response
429 if ($err) {
430 return $this->_handleError($err);
432 else {
433 // otherwise, we create the right response
434 // with the state name
435 return new xmlrpcresp(new xmlrpcval($retval,"string"));
439 function dateofservicestart($m) {
441 $err="";
443 $retval = "";
444 $obj= $m->getparam(0);
445 $key = $obj->getval();
447 $sql = "SELECT * FROM billing where pid = '" . $_SESSION['patient_id'] . "' and encounter = '" . $_SESSION['billkey'] . "'and id ='" . $key ."'";
448 //echo $sql;
449 $db = $GLOBALS['adodb']['db'];
450 $results = $db->Execute($sql);
452 if (!$results) {
453 $err = $db->ErrorMsg();
455 else {
456 if (!$results->EOF) {
457 $retval = $results->fields['date'];
462 $retval = $this->_isodate(date("Y-m-d",strtotime($retval)));
464 // if we generated an error, create an error return response
465 if ($err) {
466 return $this->_handleError($err);
468 else {
469 // otherwise, we create the right response
470 // with the state name
471 return new xmlrpcresp(new xmlrpcval($retval,XMLRPCDATETIME));
475 function dateofserviceend($m) {
477 $err="";
479 $retval = "";
480 $obj= $m->getparam(0);
481 $key = $obj->getval();
483 $sql = "SELECT * FROM billing where pid = '" . $_SESSION['patient_id'] . "' and encounter = '" . $_SESSION['billkey'] . "'and id ='" . $key ."'";
484 //echo $sql;
485 $db = $GLOBALS['adodb']['db'];
486 $results = $db->Execute($sql);
488 if (!$results) {
489 $err = $db->ErrorMsg();
491 else {
492 if (!$results->EOF) {
493 $retval = $results->fields['date'];
497 $retval = $this->_isodate(date("Y-m-d",strtotime($retval)));
499 // if we generated an error, create an error return response
500 if ($err) {
501 return $this->_handleError($err);
503 else {
504 // otherwise, we create the right response
505 // with the state name
506 return new xmlrpcresp(new xmlrpcval($retval,XMLRPCDATETIME));
510 function dateofhospitalstart($m) {
512 $err="";
514 //$pkey = $this->_isodate("1970-08-20");
515 //unimplemented by OpenEMR
516 $pkey ="";
517 // if we generated an error, create an error return response
518 if ($err) {
519 return $this->_handleError($err);
521 else {
522 // otherwise, we create the right response
523 // with the state name
524 return new xmlrpcresp(new xmlrpcval($pkey,XMLRPCDATETIME));
528 function ishospitalized($m) {
530 $err="";
532 $pkey = false;
534 // if we generated an error, create an error return response
535 if ($err) {
536 return $this->_handleError($err);
538 else {
539 // otherwise, we create the right response
540 // with the state name
541 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
545 function isoutsidelab($m) {
547 $err="";
548 //unimplemented by OpenEMR
549 $pkey = false;
551 // if we generated an error, create an error return response
552 if ($err) {
553 return $this->_handleError($err);
555 else {
556 // otherwise, we create the right response
557 // with the state name
558 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
562 function outsidelabcharges($m) {
564 $err="";
566 //unimplemented by OpenEMR
567 $pkey = "";
569 // if we generated an error, create an error return response
570 if ($err) {
571 return $this->_handleError($err);
573 else {
574 // otherwise, we create the right response
575 // with the state name
576 return new xmlrpcresp(new xmlrpcval($pkey));
580 function dateofhospitalend($m) {
582 $err="";
584 //$pkey = $this->_isodate("1970-08-20");
585 //unimplemented by OpenEMR
586 $pkey ="";
587 // if we generated an error, create an error return response
588 if ($err) {
589 return $this->_handleError($err);
591 else {
592 // otherwise, we create the right response
593 // with the state name
594 return new xmlrpcresp(new xmlrpcval($pkey,XMLRPCDATETIME));
599 function medicaidresubmissioncode($m) {
601 $err="";
603 //$pkey = "ICD-9";
604 //unimplemented by OpenEMR
605 $pkey ="";
606 // if we generated an error, create an error return response
607 if ($err) {
608 return $this->_handleError($err);
610 else {
611 // otherwise, we create the right response
612 // with the state name
613 return new xmlrpcresp(new xmlrpcval($pkey));
617 function medicaidoriginalreference($m) {
619 $err="";
621 //unimplemented by OpenEMR
622 $pkey = "";
624 // if we generated an error, create an error return response
625 if ($err) {
626 return $this->_handleError($err);
628 else {
629 // otherwise, we create the right response
630 // with the state name
631 return new xmlrpcresp(new xmlrpcval($pkey,"string"));
636 function weightgrams($m) {
638 $err="";
640 $pkey = "2300";
642 // if we generated an error, create an error return response
643 if ($err) {
644 return $this->_handleError($err);
646 else {
647 // otherwise, we create the right response
648 // with the state name
649 return new xmlrpcresp(new xmlrpcval($pkey));
653 function weightpounds($m) {
655 $err="";
656 //unimplemented by OpenEMR
657 $pkey = "";
659 // if we generated an error, create an error return response
660 if ($err) {
661 return $this->_handleError($err);
663 else {
664 // otherwise, we create the right response
665 // with the state name
666 return new xmlrpcresp(new xmlrpcval($pkey));
670 function hcfalocaluse10d($m) {
672 $err="";
674 //this needs to be customized on a payer to payer and state to state basis
675 $pkey = "";
677 // if we generated an error, create an error return response
678 if ($err) {
679 return $this->_handleError($err);
681 else {
682 // otherwise, we create the right response
683 // with the state name
684 return new xmlrpcresp(new xmlrpcval($pkey,"string"));
688 function hcfalocaluse19($m) {
690 $err="";
692 //this needs to be customized on a payer to payer and state to state basis
693 $pkey = "";
695 // if we generated an error, create an error return response
696 if ($err) {
697 return $this->_handleError($err);
699 else {
700 // otherwise, we create the right response
701 // with the state name
702 return new xmlrpcresp(new xmlrpcval($pkey,"string"));
706 function procarray($m) {
708 $err="";
710 $procs = array();
712 $obj= $m->getparam(0);
713 $key = $obj->getval();
715 $keys = split("-",$key);
716 $patient_id = $keys[0];
717 $encounter = $keys[1];
719 $_SESSION['billkey'] = $encounter;
720 $_SESSION['patient_id'] = $patient_id;
722 // Sort by procedure timestamp in order to get some consistency. In particular
723 // freeb determines the provider from the first procedure in this array.
724 $sql = "SELECT * FROM billing where (code_type = 'CPT4' or code_type = 'HCPCS') AND " .
725 "encounter = '$encounter' AND pid = '$patient_id' and activity = '1' " .
726 "order by date, id";
727 //echo $sql;
728 $db = $GLOBALS['adodb']['db'];
729 $results = $db->Execute($sql);
731 if (!$results) {
732 $err = $db->ErrorMsg();
734 else {
735 while (!$results->EOF) {
736 $procs[] = new xmlrpcval($results->fields['id'],"i4");
737 $results->MoveNext();
742 // if we generated an error, create an error return response
743 if ($err) {
744 return $this->_handleError($err);
746 else {
747 // otherwise, we create the right response
748 // with the state name
749 return new xmlrpcresp(new xmlrpcval($procs,"array"));
753 function diagarray($m) {
755 $err="";
756 $diags = array();
758 $obj= $m->getparam(0);
759 $key = $obj->getval();
761 $sql = "SELECT * FROM billing where id = '$key'";
762 //echo $sql;
763 $db = $GLOBALS['adodb']['db'];
764 $results = $db->Execute($sql);
766 if (!$results) {
767 $err = $db->ErrorMsg();
769 else {
770 if (!$results->EOF) {
771 $diagstring = $results->fields['justify'];
775 if (!empty($diagstring)) {
777 $diag_codes = split(":",$diagstring);
778 $diag_sql = "";
779 foreach ($diag_codes as $dc) {
780 if (!empty($dc)) {
781 $diag_sql .= "'$dc',";
784 if (substr($diag_sql,strlen($diag_sql) -1) == ",") {
785 $diag_sql = substr($diag_sql,0,strlen($diag_sql) -1);
788 $sql = "SELECT * FROM billing where code in ($diag_sql) and code_type = 'ICD9' AND encounter = '" . $_SESSION['billkey'] . "' and pid ='" . $_SESSION['patient_id'] . "' and activity = '1'";
790 $results = $db->Execute($sql);
791 if (!$results) {
792 $err .= $db->ErrorMsg();
794 else {
795 while (!$results->EOF) {
796 $diags[$results->fields['code']] = new xmlrpcval($results->fields['id'],"i4");
797 $results->MoveNext();
800 $tmp_diags = $diags;
801 $tmp_keys = array_keys($tmp_diags);
802 $diags = array();
803 foreach ($diag_codes as $code) {
804 if (in_array($code,$tmp_keys)) {
805 $diags[] = $tmp_diags[$code];
809 else {
810 $diags[] = new xmlrpcval(0,"i4");
815 // if we generated an error, create an error return response
816 if ($err) {
817 return $this->_handleError($err);
819 else {
820 // otherwise, we create the right response
821 // with the state name
822 return new xmlrpcresp(new xmlrpcval($diags,"array"));
826 function patientkey($m) {
828 $err="";
829 $pkey ="";
831 $pkey = $_SESSION['patient_id'];
833 // if we generated an error, create an error return response
834 if ($err) {
835 return $this->_handleError($err);
837 else {
838 // otherwise, we create the right response
839 // with the state name
840 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
844 function practicekey($m) {
846 $err="";
848 $sql = "SELECT * FROM form_encounter where encounter = '" . $_SESSION['billkey'] ."' and pid = '" . $_SESSION['patient_id'] ."'";
849 //echo $sql;
850 $db = $GLOBALS['adodb']['db'];
851 $results = $db->Execute($sql);
853 if (!$results) {
854 $err = $db->ErrorMsg();
856 else {
857 if (!$results->EOF) {
858 $fname = $results->fields['facility'];
862 $sql = "SELECT * FROM facility where name = '" . $fname ."'";
863 //echo $sql;
864 $db = $GLOBALS['adodb']['db'];
865 $results = $db->Execute($sql);
867 if (!$results) {
868 $err = $db->ErrorMsg();
870 else {
871 if (!$results->EOF) {
872 $fkey = $results->fields['id'];
876 // if we generated an error, create an error return response
877 if ($err) {
878 return $this->_handleError($err);
880 else {
881 // otherwise, we create the right response
882 // with the state name
883 return new xmlrpcresp(new xmlrpcval($fkey,"i4"));
887 function providerkey($m) {
889 $err="";
891 $pkey ="";
892 $obj= $m->getparam(0);
893 $key = $obj->getval();
895 $sql = "SELECT * FROM billing where id = '" . $key . "'";
896 //echo $sql;
897 $db = $GLOBALS['adodb']['db'];
898 $results = $db->Execute($sql);
900 if (!$results) {
901 $err = $db->ErrorMsg();
903 else {
904 if (!$results->EOF) {
905 $pkey = $results->fields['provider_id'];
909 // if we generated an error, create an error return response
910 if ($err) {
911 return $this->_handleError($err);
913 else {
914 // otherwise, we create the right response
915 // with the state name
916 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
920 function referringproviderkey($m) {
922 $err="";
924 $pkey ="";
925 $obj= $m->getparam(0);
926 $key = $obj->getval();
928 $sql = "SELECT * FROM patient_data where pid = '" . $key . "'";
929 //echo $sql;
930 $db = $GLOBALS['adodb']['db'];
931 $results = $db->Execute($sql);
933 if (!$results) {
934 $err = $db->ErrorMsg();
936 else {
937 if (!$results->EOF) {
938 $pkey = $results->fields['providerID'];
943 // if we generated an error, create an error return response
944 if ($err) {
945 return $this->_handleError($err);
947 else {
948 // otherwise, we create the right response
949 // with the state name
950 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
954 function insuredkey($m) {
956 $err="";
958 $payer_id = "";
959 $obj= $m->getparam(0);
960 $key = $obj->getval();
962 $sql = "SELECT * FROM billing where id = '" . $key . "'";
963 //echo $sql;
964 $db = $GLOBALS['adodb']['db'];
965 $results = $db->Execute($sql);
967 if (!$results) {
968 $err = $db->ErrorMsg();
970 else {
971 if (!$results->EOF) {
972 $payer_id = $results->fields['payer_id'];
977 $insured_id = "";
978 $sql = "SELECT * FROM insurance_data where pid = '" . $_SESSION['patient_id'] . "' and provider = '" . $payer_id . "'";
979 //echo $sql;
980 $db = $GLOBALS['adodb']['db'];
981 $results = $db->Execute($sql);
983 if (!$results) {
984 $err = $db->ErrorMsg();
986 else {
987 if (!$results->EOF) {
988 $insured_id = $results->fields['id'];
991 //we are returning the record id of the appropriate entry in insurance_data
992 //there is no relational data, all the subscriber/insured information is kept monolithically in that table
994 // if we generated an error, create an error return response
995 if ($err) {
996 return $this->_handleError($err);
998 else {
999 // otherwise, we create the right response
1000 // with the state name
1001 return new xmlrpcresp(new xmlrpcval($insured_id,"i4"));
1005 function otherinsuredkey($m) {
1007 $err="";
1008 //openemr does not currently implement other insured
1009 $pkey = 0;
1011 // if we generated an error, create an error return response
1012 if ($err) {
1013 return $this->_handleError($err);
1015 else {
1016 // otherwise, we create the right response
1017 // with the state name
1018 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
1022 function payerkey($m) {
1023 $err="";
1025 $pkey = "";
1026 $obj= $m->getparam(0);
1027 $key = $obj->getval();
1029 $sql = "SELECT * FROM billing where id = '" . $key . "'";
1030 $db = $GLOBALS['adodb']['db'];
1031 $results = $db->Execute($sql);
1034 if (!$results) {
1035 $err = $db->ErrorMsg() . " $sql";
1037 else {
1038 while (!$results->EOF) {
1039 $pkey = $results->fields['payer_id'];
1040 $results->MoveNext();
1044 // if we generated an error, create an error return response
1045 if ($err) {
1046 return $this->_handleError($err);
1048 else {
1049 // otherwise, we create the right response
1050 // with the state name
1051 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
1055 function secondpayerkey($m) {
1057 $err="";
1058 //unimplemented by OpenEMR
1059 $pkey = 0;
1061 // if we generated an error, create an error return response
1062 if ($err) {
1063 return $this->_handleError($err);
1065 else {
1066 // otherwise, we create the right response
1067 // with the state name
1068 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
1072 function facilitykey($m) {
1074 $eid = "";
1075 $patient_id = "";
1076 $fname ="";
1078 $obj= $m->getparam(0);
1079 $key = $obj->getval();
1081 $sql = "SELECT * FROM billing where id = '" . $key . "'";
1083 $db = $GLOBALS['adodb']['db'];
1084 $results = $db->Execute($sql);
1086 if (!$results) {
1087 $err = $db->ErrorMsg();
1089 else {
1090 if (!$results->EOF) {
1091 $eid = $results->fields['encounter'];
1092 $patient_id = $results->fields['pid'];
1096 $sql = "SELECT * FROM form_encounter where encounter = '" . $eid ."' and pid = '" . $patient_id ."'";
1098 $results = $db->Execute($sql);
1100 if (!$results) {
1101 $err = $db->ErrorMsg();
1103 else {
1104 if (!$results->EOF) {
1105 $fname = $results->fields['facility'];
1109 $sql = "SELECT * FROM facility where name = '" . $fname ."'";
1110 $results = $db->Execute($sql);
1111 if (!$results) {
1112 echo "error";
1113 $err = $db->ErrorMsg();
1115 else {
1116 if (!$results->EOF) {
1117 $fkey = $results->fields['id'];
1121 // if we generated an error, create an error return response
1122 if ($err) {
1123 return $this->_handleError($err);
1125 else {
1126 // otherwise, we create the right response
1127 // with the state name
1128 return new xmlrpcresp(new xmlrpcval($fkey,"i4"));
1132 function billingcontactkey($m) {
1134 $err="";
1136 $pkey = 0;
1138 // if we generated an error, create an error return response
1139 if ($err) {
1140 return $this->_handleError($err);
1142 else {
1143 // otherwise, we create the right response
1144 // with the state name
1145 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
1148 function billingservicekey($m) {
1150 $err="";
1152 $pkey = 0;
1154 // if we generated an error, create an error return response
1155 if ($err) {
1156 return $this->_handleError($err);
1158 else {
1159 // otherwise, we create the right response
1160 // with the state name
1161 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
1166 function isusingbillingservice($m) {
1168 $err="";
1170 $pkey = false;
1172 // if we generated an error, create an error return response
1173 if ($err) {
1174 return $this->_handleError($err);
1176 else {
1177 // otherwise, we create the right response
1178 // with the state name
1179 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
1183 function clearinghousekey($m) {
1185 $err="";
1187 $eid = "";
1188 $patient_id = "";
1189 $fname ="";
1191 $obj= $m->getparam(0);
1192 $key = $obj->getval();
1194 $sql = "SELECT * FROM billing where id = '" . $key . "'";
1196 $db = $GLOBALS['adodb']['db'];
1197 $results = $db->Execute($sql);
1199 if (!$results) {
1200 $err = $db->ErrorMsg();
1202 else {
1203 if (!$results->EOF) {
1204 $eid = $results->fields['encounter'];
1205 $patient_id = $results->fields['pid'];
1209 $sql = "SELECT * FROM form_encounter where encounter = '" . $eid ."' and pid = '" . $patient_id ."'";
1211 $results = $db->Execute($sql);
1213 if (!$results) {
1214 $err = $db->ErrorMsg();
1216 else {
1217 if (!$results->EOF) {
1218 $fname = $results->fields['facility'];
1222 $sql = "SELECT * FROM facility where name = '" . $fname ."'";
1223 $results = $db->Execute($sql);
1224 if (!$results) {
1225 echo "error";
1226 $err = $db->ErrorMsg();
1228 else {
1229 if (!$results->EOF) {
1230 $fkey = $results->fields['id'];
1234 // if we generated an error, create an error return response
1235 if ($err) {
1236 return $this->_handleError($err);
1238 else {
1239 // otherwise, we create the right response
1240 // with the state name
1241 return new xmlrpcresp(new xmlrpcval($fkey,"i4"));
1250 //'FreeB.FBProcedure.isUsingClearingHouse' => \&FreeB_FBProcedure_isUsingClearingHouse,
1251 //'FreeB.FBProcedure.ProcArray' => \&FreeB_FBProcedure_ProcArray,
1252 //'FreeB.FBProcedure.DiagArray' => \&FreeB_FBProcedure_DiagArray,
1253 //'FreeB.FBProcedure.CPT4Code' => \&FreeB_FBProcedure_CPT4Code,
1254 //'FreeB.FBProcedure.CPT5Code' => \&FreeB_FBProcedure_CPT4Code,
1255 //'FreeB.FBProcedure.CPTUnits' => \&FreeB_FBProcedure_CPTUnits,
1256 //'FreeB.FBProcedure.CPTModifier' => \&FreeB_FBProcedure_CPTModifier,
1257 //'FreeB.FBProcedure.CPTCharges' => \&FreeB_FBProcedure_CPTCharges,
1258 //'FreeB.FBProcedure.CPTEmergency' => \&FreeB_FBProcedure_CPTEmergency,
1259 //'FreeB.FBProcedure.CPTCOB' => \&FreeB_FBProcedure_CPTCOB,
1260 //'FreeB.FBProcedure.CPTEPSDT' => \&FreeB_FBProcedure_CPTEPSDT,
1261 //'FreeB.FBProcedure.AmountPaid' => \&FreeB_FBProcedure_AmountPaid,
1262 //'FreeB.FBProcedure.TypeOfService' => \&FreeB_FBProcedure_TypeOfService,
1263 //'FreeB.FBProcedure.PriorAuth' => \&FreeB_FBProcedure_PriorAuth,
1264 //'FreeB.FBProcedure.DateOfServiceStart' => \&FreeB_FBProcedure_DateOfServiceStart,
1265 //'FreeB.FBProcedure.DateOfServiceEnd' => \&FreeB_FBProcedure_DateOfServiceEnd,
1266 //'FreeB.FBProcedure.DateOfHospitalStart' => \&FreeB_FBProcedure_DateOfHospitalStart,
1267 //'FreeB.FBProcedure.isHospitalized' => \&FreeB_FBProcedure_isHospitalized,
1268 //'FreeB.FBProcedure.isOutsideLab' => \&FreeB_FBProcedure_isOutsideLab,
1269 //'FreeB.FBProcedure.OutsideLabCharges' => \&FreeB_FBProcedure_OutSideLabCharges,
1270 //'FreeB.FBProcedure.DateOfHospitalEnd' => \&FreeB_FBProcedure_DateOfHospitalEnd,
1271 //'FreeB.FBProcedure.MedicaidResubmissionCode' => \&FreeB_FBProcedure_MedicaidResubmissionCode,
1272 //'FreeB.FBProcedure.MedicaidOriginalReference' => \&FreeB_FBProcedure_MedicaidOriginalReference,
1273 //'FreeB.FBProcedure.WeightGrams' => \&FreeB_FBProcedure_WeightGrams,
1274 //'FreeB.FBProcedure.WeightPounds' => \&FreeB_FBProcedure_WeightPounds,
1275 //'FreeB.FBProcedure.HCFALocalUse10d' => \&FreeB_FBProcedure_HCFALocalUse10d,
1276 //'FreeB.FBProcedure.HCFALocalUse19' => \&FreeB_FBProcedure_HCFALocalUse19,