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
),
11 $this->_addFunc("cpt4code", array( "name" => "FreeB.FBProcedure.CPT4Code",
12 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
14 $this->_addFunc("cpt5code", array( "name" => "FreeB.FBProcedure.CPT5Code",
15 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
17 $this->_addFunc("cptunits", array( "name" => "FreeB.FBProcedure.CPTUnits",
18 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
20 $this->_addFunc("cptmodifier", array( "name" => "FreeB.FBProcedure.CPTModifier",
21 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
23 $this->_addFunc("cptcharges", array( "name" => "FreeB.FBProcedure.CPTCharges",
24 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
26 $this->_addFunc("cptemergency", array( "name" => "FreeB.FBProcedure.CPTEmergency",
27 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
29 $this->_addFunc("cptcob", array( "name" => "FreeB.FBProcedure.CPTCOB",
30 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
32 $this->_addFunc("cptepsdt", array( "name" => "FreeB.FBProcedure.CPTEPSDT",
33 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
35 $this->_addFunc("amountpaid", array( "name" => "FreeB.FBProcedure.AmountPaid",
36 "sig" => array(XMLRPCSTRING
,XMLRPCARRAY
),
38 $this->_addFunc("typeofservice", array( "name" => "FreeB.FBProcedure.TypeOfService",
39 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
41 $this->_addFunc("priorauth", array( "name" => "FreeB.FBProcedure.PriorAuth",
42 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
44 $this->_addFunc("dateofservicestart", array( "name" => "FreeB.FBProcedure.DateOfServiceStart",
45 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
47 $this->_addFunc("dateofserviceend", array( "name" => "FreeB.FBProcedure.DateOfServiceEnd",
48 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
50 $this->_addFunc("dateofhospitalstart", array( "name" => "FreeB.FBProcedure.DateOfHospitalStart",
51 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
53 $this->_addFunc("ishospitalized", array( "name" => "FreeB.FBProcedure.isHospitalized",
54 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
56 $this->_addFunc("isoutsidelab", array( "name" => "FreeB.FBProcedure.isOutsideLab",
57 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
59 $this->_addFunc("outsidelabcharges", array( "name" => "FreeB.FBProcedure.OutsideLabCharges",
60 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
62 $this->_addFunc("dateofhospitalend", array( "name" => "FreeB.FBProcedure.DateOfHospitalEnd",
63 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
65 $this->_addFunc("medicaidresubmissioncode", array( "name" => "FreeB.FBProcedure.MedicaidResubmissionCode",
66 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
68 $this->_addFunc("medicaidoriginalreference",array( "name" => "FreeB.FBProcedure.MedicaidOriginalReference",
69 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
71 $this->_addFunc("weightgrams", array( "name" => "FreeB.FBProcedure.WeightGrams",
72 "sig" => array(XMLRPCSTRING
,XMLRPCINT
,XMLRPCSTRING
),
74 $this->_addFunc("weightpounds", array( "name" => "FreeB.FBProcedure.WeightPounds",
75 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
77 $this->_addFunc("hcfalocaluse10d", array( "name" => "FreeB.FBProcedure.HCFALocalUse10d",
78 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
80 $this->_addFunc("hcfalocaluse19", array( "name" => "FreeB.FBProcedure.HCFALocalUse19",
81 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
83 $this->_addFunc("procarray", array( "name" => "FreeB.FBProcedure.ProcArray",
84 "sig" => array(XMLRPCSTRING
,XMLRPCSTRING
),
86 $this->_addFunc("diagarray", array( "name" => "FreeB.FBProcedure.DiagArray",
87 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
89 $this->_addFunc("patientkey", array( "name" => "FreeB.FBProcedure.PatientKey",
90 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
92 $this->_addFunc("practicekey", array( "name" => "FreeB.FBProcedure.PracticeKey",
93 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
95 $this->_addFunc("providerkey", array( "name" => "FreeB.FBProcedure.ProviderKey",
96 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
98 $this->_addFunc("referringproviderkey", array( "name" => "FreeB.FBPatient.ReferringProviderKey",
99 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
101 $this->_addFunc("insuredkey", array( "name" => "FreeB.FBProcedure.InsuredKey",
102 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
104 $this->_addFunc("otherinsuredkey", array( "name" => "FreeB.FBProcedure.OtherInsuredKey",
105 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
107 $this->_addFunc("payerkey", array( "name" => "FreeB.FBProcedure.PayerKey",
108 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
110 $this->_addFunc("secondpayerkey", array( "name" => "FreeB.FBProcedure.SecondPayerKey",
111 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
113 $this->_addFunc("facilitykey", array( "name" => "FreeB.FBProcedure.FacilityKey",
114 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
116 $this->_addFunc("billingcontactkey",array( "name" => "FreeB.FBProcedure.BillingContactKey",
117 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
119 $this->_addFunc("billingservicekey",array( "name" => "FreeB.FBProcedure.BillingServiceKey",
120 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
122 $this->_addFunc("isusingbillingservice",array("name" => "FreeB.FBProcedure.isUsingBillingService",
123 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
125 $this->_addFunc("clearinghousekey", array( "name" => "FreeB.FBProcedure.ClearingHouseKey",
126 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
128 $this->_addFunc("ndcline", array("name" => "FreeB.FBProcedure.NDCLine",
129 "sig" => array(XMLRPCSTRING
,XMLRPCINT
),
134 function isusingclearinghouse($m) {
140 // if we generated an error, create an error return response
142 return $this->_handleError($err);
145 // otherwise, we create the right response
146 // with the state name
147 return new xmlrpcresp(new xmlrpcval($retval,"i4"));
151 function cpt4code($m) {
155 $obj= $m->getparam(0);
156 $key = $obj->getval();
158 $sql = "SELECT * FROM billing where id = '" . $key . "'";
160 $db = $GLOBALS['adodb']['db'];
161 $results = $db->Execute($sql);
164 $err = $db->ErrorMsg();
167 if (!$results->EOF
) {
168 $retval = $results->fields
['code'];
172 // if we generated an error, create an error return response
174 return $this->_handleError($err);
177 // otherwise, we create the right response
178 // with the state name
179 return new xmlrpcresp(new xmlrpcval($retval,"string"));
183 function cpt5code($m) {
189 // if we generated an error, create an error return response
191 return $this->_handleError($err);
194 // otherwise, we create the right response
195 // with the state name
196 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
200 function cptunits($m) {
204 $obj= $m->getparam(0);
205 $key = $obj->getval();
207 $sql = "SELECT * FROM billing where id = '" . $key . "'";
209 $db = $GLOBALS['adodb']['db'];
210 $results = $db->Execute($sql);
213 $err = $db->ErrorMsg();
216 if (!$results->EOF
) {
217 $retval = $results->fields
['units'];
221 if (empty ($retval) ||
$retval < 1) {
225 // if we generated an error, create an error return response
227 return $this->_handleError($err);
230 // otherwise, we create the right response
231 // with the state name
232 return new xmlrpcresp(new xmlrpcval($retval,"string"));
237 function cptmodifier($m) {
241 $obj= $m->getparam(0);
242 $key = $obj->getval();
244 $sql = "SELECT * FROM billing where id = '" . $key . "'";
246 $db = $GLOBALS['adodb']['db'];
247 $results = $db->Execute($sql);
250 $err = $db->ErrorMsg();
253 if (!$results->EOF
) {
254 $retval = $results->fields
['modifier'];
258 // if we generated an error, create an error return response
260 return $this->_handleError($err);
263 // otherwise, we create the right response
264 // with the state name
265 return new xmlrpcresp(new xmlrpcval($retval,"string"));
270 function cptcharges($m) {
274 $obj= $m->getparam(0);
275 $key = $obj->getval();
277 $sql = "SELECT * FROM billing where id = '" . $key . "'";
279 $db = $GLOBALS['adodb']['db'];
280 $results = $db->Execute($sql);
283 $err = $db->ErrorMsg();
286 if (!$results->EOF
) {
287 $retval = $results->fields
['fee'];
291 // if we generated an error, create an error return response
293 return $this->_handleError($err);
296 // otherwise, we create the right response
297 // with the state name
298 return new xmlrpcresp(new xmlrpcval($retval,"double"));
302 function cptemergency($m) {
308 // if we generated an error, create an error return response
310 return $this->_handleError($err);
313 // otherwise, we create the right response
314 // with the state name
315 return new xmlrpcresp(new xmlrpcval($pkey));
319 function cptcob($m) {
325 // if we generated an error, create an error return response
327 return $this->_handleError($err);
330 // otherwise, we create the right response
331 // with the state name
332 return new xmlrpcresp(new xmlrpcval($pkey));
336 function cptepsdt($m) {
342 // if we generated an error, create an error return response
344 return $this->_handleError($err);
347 // otherwise, we create the right response
348 // with the state name
349 return new xmlrpcresp(new xmlrpcval($pkey));
353 function amountpaid($m) {
357 $obj= $m->getparam(0);
358 $key = $obj->getval();
360 $sql = "SELECT * FROM billing where activity = '1' and encounter = '" . $_SESSION['billkey'] . "' and pid = '" . $_SESSION['patient_id'] . "' and code_type = 'COPAY'";
362 $db = $GLOBALS['adodb']['db'];
363 $results = $db->Execute($sql);
366 $err = $db->ErrorMsg();
369 if (!$results->EOF
) {
370 $retval = $results->fields
['fee'];
374 if (empty($retval)) {
378 $retval = str_replace(".","",$retval);
380 // if we generated an error, create an error return response
382 return $this->_handleError($err);
385 // otherwise, we create the right response
386 // with the state name
387 return new xmlrpcresp(new xmlrpcval($retval,"i4"));
391 function typeofservice($m) {
395 $obj= $m->getparam(0);
396 $key = $obj->getval();
398 //phased out by HIPPA, use cpt modifiers instead
400 // if we generated an error, create an error return response
402 return $this->_handleError($err);
405 // otherwise, we create the right response
406 // with the state name
407 return new xmlrpcresp(new xmlrpcval($retval,"string"));
411 function priorauth($m) {
415 $obj= $m->getparam(0);
416 $key = $obj->getval();
418 $sql = "SELECT * FROM forms JOIN form_misc_billing_options as fpa on fpa.id = forms.form_id where forms.encounter = '" . $_SESSION['billkey'] . "' and forms.pid = '" . $_SESSION['patient_id'] . "' and forms.formdir = 'misc_billing_options' order by forms.date";
420 $db = $GLOBALS['adodb']['db'];
421 $results = $db->Execute($sql);
424 $err = $db->ErrorMsg();
427 if (!$results->EOF
) {
428 $retval = $results->fields
['prior_auth_number'];
431 // if we generated an error, create an error return response
433 return $this->_handleError($err);
436 // otherwise, we create the right response
437 // with the state name
438 return new xmlrpcresp(new xmlrpcval($retval,"string"));
442 function dateofservicestart($m) {
447 $obj= $m->getparam(0);
448 $key = $obj->getval();
450 $sql = "SELECT * FROM billing where pid = '" . $_SESSION['patient_id'] . "' and encounter = '" . $_SESSION['billkey'] . "'and id ='" . $key ."'";
452 $db = $GLOBALS['adodb']['db'];
453 $results = $db->Execute($sql);
456 $err = $db->ErrorMsg();
459 if (!$results->EOF
) {
460 $retval = $results->fields
['date'];
465 $retval = $this->_isodate(date("Y-m-d",strtotime($retval)));
467 // if we generated an error, create an error return response
469 return $this->_handleError($err);
472 // otherwise, we create the right response
473 // with the state name
474 return new xmlrpcresp(new xmlrpcval($retval,XMLRPCDATETIME
));
478 function dateofserviceend($m) {
483 $obj= $m->getparam(0);
484 $key = $obj->getval();
486 $sql = "SELECT * FROM billing where pid = '" . $_SESSION['patient_id'] . "' and encounter = '" . $_SESSION['billkey'] . "'and id ='" . $key ."'";
488 $db = $GLOBALS['adodb']['db'];
489 $results = $db->Execute($sql);
492 $err = $db->ErrorMsg();
495 if (!$results->EOF
) {
496 $retval = $results->fields
['date'];
500 $retval = $this->_isodate(date("Y-m-d",strtotime($retval)));
502 // if we generated an error, create an error return response
504 return $this->_handleError($err);
507 // otherwise, we create the right response
508 // with the state name
509 return new xmlrpcresp(new xmlrpcval($retval,XMLRPCDATETIME
));
513 function dateofhospitalstart($m) {
515 //Now implemented by OpenEMR with Form: Misc Billing Options
517 $obj= $m->getparam(0);
518 $key = $obj->getval();
520 $sql = "SELECT * FROM forms JOIN form_misc_billing_options as fpa on fpa.id = forms.form_id where forms.encounter = '" . $_SESSION['billkey'] . "' and forms.pid = '" . $_SESSION['patient_id'] . "' and forms.formdir = 'misc_billing_options' order by forms.date";
522 $db = $GLOBALS['adodb']['db'];
523 $results = $db->Execute($sql);
526 $err = $db->ErrorMsg();
529 if (!$results->EOF
) {
530 $retval = $results->fields
['hospitalization_date_from'];
533 $retval = date("Y-m-d",strtotime($retval));
534 $retval = $this->_isodate($retval);
535 // if we generated an error, create an error return response
537 return $this->_handleError($err);
540 // otherwise, we create the right response
541 // with the state name
542 return new xmlrpcresp(new xmlrpcval($retval,XMLRPCDATETIME
));
548 function ishospitalized($m) {
550 //Now implemented by OpenEMR with Form: Misc Billing Options
553 $obj= $m->getparam(0);
554 $key = $obj->getval();
556 $sql = "SELECT * FROM forms JOIN form_misc_billing_options as fpa on fpa.id = forms.form_id where forms.encounter = '" . $_SESSION['billkey'] . "' and forms.pid = '" . $_SESSION['patient_id'] . "' and forms.formdir = 'misc_billing_options' order by forms.date";
558 $db = $GLOBALS['adodb']['db'];
559 $results = $db->Execute($sql);
562 $err = $db->ErrorMsg();
565 if (!$results->EOF
) {
566 $retval = $results->fields
['is_hospitalized'];
567 if ($retval == "1") {$pkey = true;};
570 // if we generated an error, create an error return response
572 return $this->_handleError($err);
575 // otherwise, we create the right response
576 // with the state name
577 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
582 function isoutsidelab($m) {
584 //Now implemented by OpenEMR with Form: Misc Billing Options
587 $obj= $m->getparam(0);
588 $key = $obj->getval();
590 $sql = "SELECT * FROM forms JOIN form_misc_billing_options as fpa on fpa.id = forms.form_id where forms.encounter = '" . $_SESSION['billkey'] . "' and forms.pid = '" . $_SESSION['patient_id'] . "' and forms.formdir = 'misc_billing_options' order by forms.date";
592 $db = $GLOBALS['adodb']['db'];
593 $results = $db->Execute($sql);
596 $err = $db->ErrorMsg();
599 if (!$results->EOF
) {
600 $retval = $results->fields
['outside_lab'];
601 if ($retval == "1") {$pkey = true;};
604 // if we generated an error, create an error return response
606 return $this->_handleError($err);
609 // otherwise, we create the right response
610 // with the state name
611 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
615 function outsidelabcharges($m) {
619 //Now implemented by OpenEMR with form_misc_billing_options
621 $obj= $m->getparam(0);
622 $key = $obj->getval();
624 $sql = "SELECT * FROM forms JOIN form_misc_billing_options as fpa on fpa.id = forms.form_id where forms.encounter = '" . $_SESSION['billkey'] . "' and forms.pid = '" . $_SESSION['patient_id'] . "' and forms.formdir = 'misc_billing_options' order by forms.date";
626 $db = $GLOBALS['adodb']['db'];
627 $results = $db->Execute($sql);
630 $err = $db->ErrorMsg();
633 if (!$results->EOF
) {
634 $retval = $results->fields
['lab_amount'];
638 // if we generated an error, create an error return response
640 return $this->_handleError($err);
643 // otherwise, we create the right response
644 // with the state name
645 return new xmlrpcresp(new xmlrpcval($retval,"double"));
649 function dateofhospitalend($m) {
651 //Now implemented by OpenEMR with Form: Misc Billing Options
653 $obj= $m->getparam(0);
654 $key = $obj->getval();
656 $sql = "SELECT * FROM forms JOIN form_misc_billing_options as fpa on fpa.id = forms.form_id where forms.encounter = '" . $_SESSION['billkey'] . "' and forms.pid = '" . $_SESSION['patient_id'] . "' and forms.formdir = 'misc_billing_options' order by forms.date";
658 $db = $GLOBALS['adodb']['db'];
659 $results = $db->Execute($sql);
662 $err = $db->ErrorMsg();
665 if (!$results->EOF
) {
666 $retval = $results->fields
['hospitalization_date_to'];
669 $retval = date("Y-m-d",strtotime($retval));
670 $retval = $this->_isodate($retval);
671 // if we generated an error, create an error return response
673 return $this->_handleError($err);
676 // otherwise, we create the right response
677 // with the state name
678 return new xmlrpcresp(new xmlrpcval($retval,XMLRPCDATETIME
));
684 function medicaidresubmissioncode($m) {
688 $obj= $m->getparam(0);
689 $key = $obj->getval();
691 $sql = "SELECT * FROM forms JOIN form_misc_billing_options as fpa on fpa.id = forms.form_id where forms.encounter = '" . $_SESSION['billkey'] . "' and forms.pid = '" . $_SESSION['patient_id'] . "' and forms.formdir = 'misc_billing_options' order by forms.date";
693 $db = $GLOBALS['adodb']['db'];
694 $results = $db->Execute($sql);
697 $err = $db->ErrorMsg();
700 if (!$results->EOF
) {
701 $retval = $results->fields
['medicaid_resubmission_code'];
705 // if we generated an error, create an error return response
707 return $this->_handleError($err);
710 // otherwise, we create the right response
711 // with the state name
712 return new xmlrpcresp(new xmlrpcval($retval,"string"));
716 function medicaidoriginalreference($m) {
720 $obj= $m->getparam(0);
721 $key = $obj->getval();
723 $sql = "SELECT * FROM forms JOIN form_misc_billing_options as fpa on fpa.id = forms.form_id where forms.encounter = '" . $_SESSION['billkey'] . "' and forms.pid = '" . $_SESSION['patient_id'] . "' and forms.formdir = 'misc_billing_options' order by forms.date";
725 $db = $GLOBALS['adodb']['db'];
726 $results = $db->Execute($sql);
729 $err = $db->ErrorMsg();
732 if (!$results->EOF
) {
733 $retval = $results->fields
['medicaid_original_reference'];
737 // if we generated an error, create an error return response
739 return $this->_handleError($err);
742 // otherwise, we create the right response
743 // with the state name
744 return new xmlrpcresp(new xmlrpcval($retval,"string"));
749 function weightgrams($m) {
755 // if we generated an error, create an error return response
757 return $this->_handleError($err);
760 // otherwise, we create the right response
761 // with the state name
762 return new xmlrpcresp(new xmlrpcval($pkey));
766 function weightpounds($m) {
769 //unimplemented by OpenEMR
772 // if we generated an error, create an error return response
774 return $this->_handleError($err);
777 // otherwise, we create the right response
778 // with the state name
779 return new xmlrpcresp(new xmlrpcval($pkey));
783 function hcfalocaluse10d($m) {
787 //this needs to be customized on a payer to payer and state to state basis
790 // if we generated an error, create an error return response
792 return $this->_handleError($err);
795 // otherwise, we create the right response
796 // with the state name
797 return new xmlrpcresp(new xmlrpcval($pkey,"string"));
801 function hcfalocaluse19($m) {
805 //this needs to be customized on a payer to payer and state to state basis
808 // if we generated an error, create an error return response
810 return $this->_handleError($err);
813 // otherwise, we create the right response
814 // with the state name
815 return new xmlrpcresp(new xmlrpcval($pkey,"string"));
819 function procarray($m) {
825 $obj= $m->getparam(0);
826 $key = $obj->getval();
828 $keys = split("-",$key);
829 $patient_id = $keys[0];
830 $encounter = $keys[1];
832 $_SESSION['billkey'] = $encounter;
833 $_SESSION['patient_id'] = $patient_id;
835 // Sort by procedure timestamp in order to get some consistency. In particular
836 // freeb determines the provider from the first procedure in this array.
837 $sql = "SELECT * FROM billing where (code_type = 'CPT4' or code_type = 'HCPCS') AND " .
838 "encounter = '$encounter' AND pid = '$patient_id' and activity = '1' " .
841 $db = $GLOBALS['adodb']['db'];
842 $results = $db->Execute($sql);
845 $err = $db->ErrorMsg();
848 while (!$results->EOF
) {
849 $procs[] = new xmlrpcval($results->fields
['id'],"i4");
850 $results->MoveNext();
855 // if we generated an error, create an error return response
857 return $this->_handleError($err);
860 // otherwise, we create the right response
861 // with the state name
862 return new xmlrpcresp(new xmlrpcval($procs,"array"));
866 function diagarray($m) {
871 $obj= $m->getparam(0);
872 $key = $obj->getval();
874 $sql = "SELECT * FROM billing where id = '$key'";
876 $db = $GLOBALS['adodb']['db'];
877 $results = $db->Execute($sql);
880 $err = $db->ErrorMsg();
883 if (!$results->EOF
) {
884 $diagstring = $results->fields
['justify'];
888 if (!empty($diagstring)) {
890 $diag_codes = split(":",$diagstring);
892 foreach ($diag_codes as $dc) {
894 $diag_sql .= "'$dc',";
897 if (substr($diag_sql,strlen($diag_sql) -1) == ",") {
898 $diag_sql = substr($diag_sql,0,strlen($diag_sql) -1);
901 $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'";
903 $results = $db->Execute($sql);
905 $err .= $db->ErrorMsg();
908 while (!$results->EOF
) {
909 $diags[$results->fields
['code']] = new xmlrpcval($results->fields
['id'],"i4");
910 $results->MoveNext();
914 $tmp_keys = array_keys($tmp_diags);
916 foreach ($diag_codes as $code) {
917 if (in_array($code,$tmp_keys)) {
918 $diags[] = $tmp_diags[$code];
923 $diags[] = new xmlrpcval(0,"i4");
928 // if we generated an error, create an error return response
930 return $this->_handleError($err);
933 // otherwise, we create the right response
934 // with the state name
935 return new xmlrpcresp(new xmlrpcval($diags,"array"));
939 function patientkey($m) {
944 $pkey = $_SESSION['patient_id'];
946 // if we generated an error, create an error return response
948 return $this->_handleError($err);
951 // otherwise, we create the right response
952 // with the state name
953 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
957 // Practice Key is the billing location.
959 function practicekey($m) {
963 $sql = "SELECT * FROM facility ORDER BY billing_location DESC, id ASC LIMIT 1";
964 $db = $GLOBALS['adodb']['db'];
965 $results = $db->Execute($sql);
968 $err = $db->ErrorMsg();
971 if (!$results->EOF
) {
972 $fkey = $results->fields
['id'];
977 return $this->_handleError($err);
980 return new xmlrpcresp(new xmlrpcval($fkey,"i4"));
984 function providerkey($m) {
989 $obj= $m->getparam(0);
990 $key = $obj->getval();
992 $sql = "SELECT * FROM billing where id = '" . $key . "'";
994 $db = $GLOBALS['adodb']['db'];
995 $results = $db->Execute($sql);
998 $err = $db->ErrorMsg();
1001 if (!$results->EOF
) {
1002 $pkey = $results->fields
['provider_id'];
1006 // if we generated an error, create an error return response
1008 return $this->_handleError($err);
1011 // otherwise, we create the right response
1012 // with the state name
1013 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
1017 function referringproviderkey($m) {
1022 $obj= $m->getparam(0);
1023 $key = $obj->getval();
1025 $sql = "SELECT * FROM patient_data where pid = '" . $key . "'";
1027 $db = $GLOBALS['adodb']['db'];
1028 $results = $db->Execute($sql);
1031 $err = $db->ErrorMsg();
1034 if (!$results->EOF
) {
1035 $pkey = $results->fields
['ref_providerID'];
1040 // if we generated an error, create an error return response
1042 return $this->_handleError($err);
1045 // otherwise, we create the right response
1046 // with the state name
1047 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
1051 function insuredkey($m) {
1058 $obj= $m->getparam(0);
1059 $key = $obj->getval();
1061 // $sql = "SELECT * FROM billing where id = '" . $key . "'";
1062 $sql = "SELECT b.pid, b.payer_id, fe.date FROM " .
1063 "billing AS b, form_encounter AS fe WHERE " .
1064 "b.id = '$key' AND fe.pid = b.pid AND fe.encounter = b.encounter";
1067 $db = $GLOBALS['adodb']['db'];
1068 $results = $db->Execute($sql);
1071 // $err = $db->ErrorMsg();
1072 return $this->_handleError($db->ErrorMsg());
1075 if (!$results->EOF
) {
1076 $payer_id = $results->fields
['payer_id'];
1077 $patient_id = $results->fields
['pid'];
1078 $dos = $results->fields
['date'];
1083 // $sql = "SELECT * FROM insurance_data WHERE pid = '" . $_SESSION['patient_id'] . "' and provider = '" . $payer_id . "'";
1084 $sql = "SELECT id FROM insurance_data WHERE " .
1085 "pid = '$patient_id' AND provider = '$payer_id' AND " .
1086 "date <= '$dos' ORDER BY date DESC LIMIT 1";
1089 $db = $GLOBALS['adodb']['db'];
1090 $results = $db->Execute($sql);
1093 $err = $db->ErrorMsg();
1096 if (!$results->EOF
) {
1097 $insured_id = $results->fields
['id'];
1100 //we are returning the record id of the appropriate entry in insurance_data
1101 //there is no relational data, all the subscriber/insured information is kept monolithically in that table
1103 // if we generated an error, create an error return response
1105 return $this->_handleError($err);
1108 // otherwise, we create the right response
1109 // with the state name
1110 return new xmlrpcresp(new xmlrpcval($insured_id,"i4"));
1114 function otherinsuredkey($m) {
1117 //openemr does not currently implement other insured
1120 // if we generated an error, create an error return response
1122 return $this->_handleError($err);
1125 // otherwise, we create the right response
1126 // with the state name
1127 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
1131 function payerkey($m) {
1135 $obj= $m->getparam(0);
1136 $key = $obj->getval();
1138 $sql = "SELECT * FROM billing where id = '" . $key . "'";
1139 $db = $GLOBALS['adodb']['db'];
1140 $results = $db->Execute($sql);
1144 $err = $db->ErrorMsg() . " $sql";
1147 while (!$results->EOF
) {
1148 $pkey = $results->fields
['payer_id'];
1149 $results->MoveNext();
1153 // if we generated an error, create an error return response
1155 return $this->_handleError($err);
1158 // otherwise, we create the right response
1159 // with the state name
1160 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
1164 function secondpayerkey($m) {
1167 //unimplemented by OpenEMR
1170 // if we generated an error, create an error return response
1172 return $this->_handleError($err);
1175 // otherwise, we create the right response
1176 // with the state name
1177 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
1181 // This is for the facility where service was rendered.
1183 function facilitykey($m) {
1189 $obj= $m->getparam(0);
1190 $key = $obj->getval();
1192 $sql = "SELECT pid, encounter FROM billing where id = '$key'";
1194 $db = $GLOBALS['adodb']['db'];
1195 $results = $db->Execute($sql);
1198 $err = $db->ErrorMsg();
1201 if (!$results->EOF
) {
1202 $eid = $results->fields
['encounter'];
1203 $patient_id = $results->fields
['pid'];
1207 $sql = "SELECT facility_id FROM form_encounter where encounter = '$eid' and pid = '$patient_id'";
1209 $results = $db->Execute($sql);
1212 $err = $db->ErrorMsg();
1215 if (!$results->EOF
) {
1216 $facility_id = $results->fields
['facility_id'];
1221 $sql = "SELECT * FROM facility where name = '" . $fname . "'";
1222 $results = $db->Execute($sql);
1225 $err = $db->ErrorMsg();
1228 if (!$results->EOF) {
1229 $fkey = $results->fields['id'];
1235 return $this->_handleError($err);
1238 return new xmlrpcresp(new xmlrpcval($facility_id, "i4"));
1242 function billingcontactkey($m) {
1248 // if we generated an error, create an error return response
1250 return $this->_handleError($err);
1253 // otherwise, we create the right response
1254 // with the state name
1255 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
1258 function billingservicekey($m) {
1264 // if we generated an error, create an error return response
1266 return $this->_handleError($err);
1269 // otherwise, we create the right response
1270 // with the state name
1271 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
1276 function isusingbillingservice($m) {
1282 // if we generated an error, create an error return response
1284 return $this->_handleError($err);
1287 // otherwise, we create the right response
1288 // with the state name
1289 return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
1293 function clearinghousekey($m) {
1301 $obj= $m->getparam(0);
1302 $key = $obj->getval();
1304 $sql = "SELECT * FROM billing where id = '" . $key . "'";
1306 $db = $GLOBALS['adodb']['db'];
1307 $results = $db->Execute($sql);
1310 $err = $db->ErrorMsg();
1313 if (!$results->EOF
) {
1314 $eid = $results->fields
['encounter'];
1315 $patient_id = $results->fields
['pid'];
1319 $sql = "SELECT * FROM form_encounter where encounter = '" . $eid ."' and pid = '" . $patient_id ."'";
1321 $results = $db->Execute($sql);
1324 $err = $db->ErrorMsg();
1327 if (!$results->EOF
) {
1328 $fname = $results->fields
['facility'];
1332 $sql = "SELECT * FROM facility where name = '" . $fname ."'";
1333 $results = $db->Execute($sql);
1336 $err = $db->ErrorMsg();
1339 if (!$results->EOF
) {
1340 $fkey = $results->fields
['id'];
1344 // if we generated an error, create an error return response
1346 return $this->_handleError($err);
1349 // otherwise, we create the right response
1350 // with the state name
1351 return new xmlrpcresp(new xmlrpcval($fkey,"i4"));
1356 function ndcline($m) {
1359 $obj= $m->getparam(0);
1360 $key = $obj->getval();
1361 $sql = "SELECT ndc_info FROM billing where id = '$key'";
1362 $db = $GLOBALS['adodb']['db'];
1363 $results = $db->Execute($sql);
1365 $err = $db->ErrorMsg();
1368 if (!$results->EOF
) {
1369 // Medicare says the NDC number must be 11 digits with no dashes.
1370 $ndcinfo = $results->fields
['ndc_info'];
1371 if (preg_match('/^N4(\S+)\s+(\S*)/', $ndcinfo, $tmpa)) {
1373 if (preg_match('/^(\d+)-(\d+)-(\d+)$/', $ndc, $tmpb)) {
1374 $ndc = sprintf('%05d%04d%02d', $tmpb[1], $tmpb[2], $tmpb[3]);
1376 $ndcinfo = "N4$ndc " . $tmpa[2];
1382 return $this->_handleError($err);
1385 return new xmlrpcresp(new xmlrpcval($retval, "string"));
1391 //'FreeB.FBProcedure.isUsingClearingHouse' => \&FreeB_FBProcedure_isUsingClearingHouse,
1392 //'FreeB.FBProcedure.ProcArray' => \&FreeB_FBProcedure_ProcArray,
1393 //'FreeB.FBProcedure.DiagArray' => \&FreeB_FBProcedure_DiagArray,
1394 //'FreeB.FBProcedure.CPT4Code' => \&FreeB_FBProcedure_CPT4Code,
1395 //'FreeB.FBProcedure.CPT5Code' => \&FreeB_FBProcedure_CPT4Code,
1396 //'FreeB.FBProcedure.CPTUnits' => \&FreeB_FBProcedure_CPTUnits,
1397 //'FreeB.FBProcedure.CPTModifier' => \&FreeB_FBProcedure_CPTModifier,
1398 //'FreeB.FBProcedure.CPTCharges' => \&FreeB_FBProcedure_CPTCharges,
1399 //'FreeB.FBProcedure.CPTEmergency' => \&FreeB_FBProcedure_CPTEmergency,
1400 //'FreeB.FBProcedure.CPTCOB' => \&FreeB_FBProcedure_CPTCOB,
1401 //'FreeB.FBProcedure.CPTEPSDT' => \&FreeB_FBProcedure_CPTEPSDT,
1402 //'FreeB.FBProcedure.AmountPaid' => \&FreeB_FBProcedure_AmountPaid,
1403 //'FreeB.FBProcedure.TypeOfService' => \&FreeB_FBProcedure_TypeOfService,
1404 //'FreeB.FBProcedure.PriorAuth' => \&FreeB_FBProcedure_PriorAuth,
1405 //'FreeB.FBProcedure.DateOfServiceStart' => \&FreeB_FBProcedure_DateOfServiceStart,
1406 //'FreeB.FBProcedure.DateOfServiceEnd' => \&FreeB_FBProcedure_DateOfServiceEnd,
1407 //'FreeB.FBProcedure.DateOfHospitalStart' => \&FreeB_FBProcedure_DateOfHospitalStart,
1408 //'FreeB.FBProcedure.isHospitalized' => \&FreeB_FBProcedure_isHospitalized,
1409 //'FreeB.FBProcedure.isOutsideLab' => \&FreeB_FBProcedure_isOutsideLab,
1410 //'FreeB.FBProcedure.OutsideLabCharges' => \&FreeB_FBProcedure_OutSideLabCharges,
1411 //'FreeB.FBProcedure.DateOfHospitalEnd' => \&FreeB_FBProcedure_DateOfHospitalEnd,
1412 //'FreeB.FBProcedure.MedicaidResubmissionCode' => \&FreeB_FBProcedure_MedicaidResubmissionCode,
1413 //'FreeB.FBProcedure.MedicaidOriginalReference' => \&FreeB_FBProcedure_MedicaidOriginalReference,
1414 //'FreeB.FBProcedure.WeightGrams' => \&FreeB_FBProcedure_WeightGrams,
1415 //'FreeB.FBProcedure.WeightPounds' => \&FreeB_FBProcedure_WeightPounds,
1416 //'FreeB.FBProcedure.HCFALocalUse10d' => \&FreeB_FBProcedure_HCFALocalUse10d,
1417 //'FreeB.FBProcedure.HCFALocalUse19' => \&FreeB_FBProcedure_HCFALocalUse19,