/library/freeb/Procedure.class.php

https://github.com/md-tech/openemr · PHP · 1418 lines · 990 code · 230 blank · 198 comment · 187 complexity · 1c9b40d5735f75b149cd02728b6e397a MD5 · raw file

  1. <?php
  2. require_once("DataObjectBase.class.php");
  3. require_once("xmlrpc.inc");
  4. class Procedure Extends DataObjectBase {
  5. function Procedure($xuser) {
  6. parent::DataObjectBase($xuser);
  7. $this->_addFunc("isusingclearinghouse", array( "name" => "FreeB.FBProcedure.isUsingClearingHouse",
  8. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  9. "doc" => ""));
  10. $this->_addFunc("cpt4code", array( "name" => "FreeB.FBProcedure.CPT4Code",
  11. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  12. "doc" => ""));
  13. $this->_addFunc("cpt5code", array( "name" => "FreeB.FBProcedure.CPT5Code",
  14. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  15. "doc" => ""));
  16. $this->_addFunc("cptunits", array( "name" => "FreeB.FBProcedure.CPTUnits",
  17. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  18. "doc" => ""));
  19. $this->_addFunc("cptmodifier", array( "name" => "FreeB.FBProcedure.CPTModifier",
  20. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  21. "doc" => ""));
  22. $this->_addFunc("cptcharges", array( "name" => "FreeB.FBProcedure.CPTCharges",
  23. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  24. "doc" => ""));
  25. $this->_addFunc("cptemergency", array( "name" => "FreeB.FBProcedure.CPTEmergency",
  26. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  27. "doc" => ""));
  28. $this->_addFunc("cptcob", array( "name" => "FreeB.FBProcedure.CPTCOB",
  29. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  30. "doc" => ""));
  31. $this->_addFunc("cptepsdt", array( "name" => "FreeB.FBProcedure.CPTEPSDT",
  32. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  33. "doc" => ""));
  34. $this->_addFunc("amountpaid", array( "name" => "FreeB.FBProcedure.AmountPaid",
  35. "sig" => array(XMLRPCSTRING,XMLRPCARRAY),
  36. "doc" => ""));
  37. $this->_addFunc("typeofservice", array( "name" => "FreeB.FBProcedure.TypeOfService",
  38. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  39. "doc" => ""));
  40. $this->_addFunc("priorauth", array( "name" => "FreeB.FBProcedure.PriorAuth",
  41. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  42. "doc" => ""));
  43. $this->_addFunc("dateofservicestart", array( "name" => "FreeB.FBProcedure.DateOfServiceStart",
  44. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  45. "doc" => ""));
  46. $this->_addFunc("dateofserviceend", array( "name" => "FreeB.FBProcedure.DateOfServiceEnd",
  47. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  48. "doc" => ""));
  49. $this->_addFunc("dateofhospitalstart", array( "name" => "FreeB.FBProcedure.DateOfHospitalStart",
  50. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  51. "doc" => ""));
  52. $this->_addFunc("ishospitalized", array( "name" => "FreeB.FBProcedure.isHospitalized",
  53. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  54. "doc" => ""));
  55. $this->_addFunc("isoutsidelab", array( "name" => "FreeB.FBProcedure.isOutsideLab",
  56. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  57. "doc" => ""));
  58. $this->_addFunc("outsidelabcharges", array( "name" => "FreeB.FBProcedure.OutsideLabCharges",
  59. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  60. "doc" => ""));
  61. $this->_addFunc("dateofhospitalend", array( "name" => "FreeB.FBProcedure.DateOfHospitalEnd",
  62. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  63. "doc" => ""));
  64. $this->_addFunc("medicaidresubmissioncode", array( "name" => "FreeB.FBProcedure.MedicaidResubmissionCode",
  65. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  66. "doc" => ""));
  67. $this->_addFunc("medicaidoriginalreference",array( "name" => "FreeB.FBProcedure.MedicaidOriginalReference",
  68. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  69. "doc" => ""));
  70. $this->_addFunc("weightgrams", array( "name" => "FreeB.FBProcedure.WeightGrams",
  71. "sig" => array(XMLRPCSTRING,XMLRPCINT,XMLRPCSTRING),
  72. "doc" => ""));
  73. $this->_addFunc("weightpounds", array( "name" => "FreeB.FBProcedure.WeightPounds",
  74. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  75. "doc" => ""));
  76. $this->_addFunc("hcfalocaluse10d", array( "name" => "FreeB.FBProcedure.HCFALocalUse10d",
  77. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  78. "doc" => ""));
  79. $this->_addFunc("hcfalocaluse19", array( "name" => "FreeB.FBProcedure.HCFALocalUse19",
  80. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  81. "doc" => ""));
  82. $this->_addFunc("procarray", array( "name" => "FreeB.FBProcedure.ProcArray",
  83. "sig" => array(XMLRPCSTRING,XMLRPCSTRING),
  84. "doc" => ""));
  85. $this->_addFunc("diagarray", array( "name" => "FreeB.FBProcedure.DiagArray",
  86. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  87. "doc" => ""));
  88. $this->_addFunc("patientkey", array( "name" => "FreeB.FBProcedure.PatientKey",
  89. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  90. "doc" => ""));
  91. $this->_addFunc("practicekey", array( "name" => "FreeB.FBProcedure.PracticeKey",
  92. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  93. "doc" => ""));
  94. $this->_addFunc("providerkey", array( "name" => "FreeB.FBProcedure.ProviderKey",
  95. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  96. "doc" => ""));
  97. $this->_addFunc("referringproviderkey", array( "name" => "FreeB.FBPatient.ReferringProviderKey",
  98. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  99. "doc" => ""));
  100. $this->_addFunc("insuredkey", array( "name" => "FreeB.FBProcedure.InsuredKey",
  101. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  102. "doc" => ""));
  103. $this->_addFunc("otherinsuredkey", array( "name" => "FreeB.FBProcedure.OtherInsuredKey",
  104. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  105. "doc" => ""));
  106. $this->_addFunc("payerkey", array( "name" => "FreeB.FBProcedure.PayerKey",
  107. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  108. "doc" => ""));
  109. $this->_addFunc("secondpayerkey", array( "name" => "FreeB.FBProcedure.SecondPayerKey",
  110. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  111. "doc" => ""));
  112. $this->_addFunc("facilitykey", array( "name" => "FreeB.FBProcedure.FacilityKey",
  113. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  114. "doc" => ""));
  115. $this->_addFunc("billingcontactkey",array( "name" => "FreeB.FBProcedure.BillingContactKey",
  116. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  117. "doc" => ""));
  118. $this->_addFunc("billingservicekey",array( "name" => "FreeB.FBProcedure.BillingServiceKey",
  119. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  120. "doc" => ""));
  121. $this->_addFunc("isusingbillingservice",array("name" => "FreeB.FBProcedure.isUsingBillingService",
  122. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  123. "doc" => ""));
  124. $this->_addFunc("clearinghousekey", array( "name" => "FreeB.FBProcedure.ClearingHouseKey",
  125. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  126. "doc" => ""));
  127. $this->_addFunc("ndcline", array("name" => "FreeB.FBProcedure.NDCLine",
  128. "sig" => array(XMLRPCSTRING,XMLRPCINT),
  129. "doc" => ""));
  130. }
  131. function isusingclearinghouse($m) {
  132. $err="";
  133. $retval = false;
  134. // if we generated an error, create an error return response
  135. if ($err) {
  136. return $this->_handleError($err);
  137. }
  138. else {
  139. // otherwise, we create the right response
  140. // with the state name
  141. return new xmlrpcresp(new xmlrpcval($retval,"i4"));
  142. }
  143. }
  144. function cpt4code($m) {
  145. $err="";
  146. $retval = "";
  147. $obj= $m->getparam(0);
  148. $key = $obj->getval();
  149. $sql = "SELECT * FROM billing where id = '" . $key . "'";
  150. //echo $sql;
  151. $db = $GLOBALS['adodb']['db'];
  152. $results = $db->Execute($sql);
  153. if (!$results) {
  154. $err = $db->ErrorMsg();
  155. }
  156. else {
  157. if (!$results->EOF) {
  158. $retval = $results->fields['code'];
  159. }
  160. }
  161. // if we generated an error, create an error return response
  162. if ($err) {
  163. return $this->_handleError($err);
  164. }
  165. else {
  166. // otherwise, we create the right response
  167. // with the state name
  168. return new xmlrpcresp(new xmlrpcval($retval,"string"));
  169. }
  170. }
  171. function cpt5code($m) {
  172. $err="";
  173. $pkey = false;
  174. // if we generated an error, create an error return response
  175. if ($err) {
  176. return $this->_handleError($err);
  177. }
  178. else {
  179. // otherwise, we create the right response
  180. // with the state name
  181. return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
  182. }
  183. }
  184. function cptunits($m) {
  185. $err="";
  186. $retval = "";
  187. $obj= $m->getparam(0);
  188. $key = $obj->getval();
  189. $sql = "SELECT * FROM billing where id = '" . $key . "'";
  190. //echo $sql;
  191. $db = $GLOBALS['adodb']['db'];
  192. $results = $db->Execute($sql);
  193. if (!$results) {
  194. $err = $db->ErrorMsg();
  195. }
  196. else {
  197. if (!$results->EOF) {
  198. $retval = $results->fields['units'];
  199. }
  200. }
  201. if (empty ($retval) || $retval < 1) {
  202. $retval = 1;
  203. }
  204. // if we generated an error, create an error return response
  205. if ($err) {
  206. return $this->_handleError($err);
  207. }
  208. else {
  209. // otherwise, we create the right response
  210. // with the state name
  211. return new xmlrpcresp(new xmlrpcval($retval,"string"));
  212. }
  213. }
  214. function cptmodifier($m) {
  215. $err="";
  216. $retval = "";
  217. $obj= $m->getparam(0);
  218. $key = $obj->getval();
  219. $sql = "SELECT * FROM billing where id = '" . $key . "'";
  220. //echo $sql;
  221. $db = $GLOBALS['adodb']['db'];
  222. $results = $db->Execute($sql);
  223. if (!$results) {
  224. $err = $db->ErrorMsg();
  225. }
  226. else {
  227. if (!$results->EOF) {
  228. $retval = $results->fields['modifier'];
  229. }
  230. }
  231. // if we generated an error, create an error return response
  232. if ($err) {
  233. return $this->_handleError($err);
  234. }
  235. else {
  236. // otherwise, we create the right response
  237. // with the state name
  238. return new xmlrpcresp(new xmlrpcval($retval,"string"));
  239. }
  240. }
  241. function cptcharges($m) {
  242. $err="";
  243. $obj= $m->getparam(0);
  244. $key = $obj->getval();
  245. $sql = "SELECT * FROM billing where id = '" . $key . "'";
  246. //echo $sql;
  247. $db = $GLOBALS['adodb']['db'];
  248. $results = $db->Execute($sql);
  249. if (!$results) {
  250. $err = $db->ErrorMsg();
  251. }
  252. else {
  253. if (!$results->EOF) {
  254. $retval = $results->fields['fee'];
  255. }
  256. }
  257. // if we generated an error, create an error return response
  258. if ($err) {
  259. return $this->_handleError($err);
  260. }
  261. else {
  262. // otherwise, we create the right response
  263. // with the state name
  264. return new xmlrpcresp(new xmlrpcval($retval,"double"));
  265. }
  266. }
  267. function cptemergency($m) {
  268. $err="";
  269. $pkey = "";
  270. // if we generated an error, create an error return response
  271. if ($err) {
  272. return $this->_handleError($err);
  273. }
  274. else {
  275. // otherwise, we create the right response
  276. // with the state name
  277. return new xmlrpcresp(new xmlrpcval($pkey));
  278. }
  279. }
  280. function cptcob($m) {
  281. $err="";
  282. $pkey = "";
  283. // if we generated an error, create an error return response
  284. if ($err) {
  285. return $this->_handleError($err);
  286. }
  287. else {
  288. // otherwise, we create the right response
  289. // with the state name
  290. return new xmlrpcresp(new xmlrpcval($pkey));
  291. }
  292. }
  293. function cptepsdt($m) {
  294. $err="";
  295. $pkey = "";
  296. // if we generated an error, create an error return response
  297. if ($err) {
  298. return $this->_handleError($err);
  299. }
  300. else {
  301. // otherwise, we create the right response
  302. // with the state name
  303. return new xmlrpcresp(new xmlrpcval($pkey));
  304. }
  305. }
  306. function amountpaid($m) {
  307. $err="";
  308. $obj= $m->getparam(0);
  309. $key = $obj->getval();
  310. $sql = "SELECT * FROM billing where activity = '1' and encounter = '" . $_SESSION['billkey'] . "' and pid = '" . $_SESSION['patient_id'] . "' and code_type = 'COPAY'";
  311. //echo $sql;
  312. $db = $GLOBALS['adodb']['db'];
  313. $results = $db->Execute($sql);
  314. if (!$results) {
  315. $err = $db->ErrorMsg();
  316. }
  317. else {
  318. if (!$results->EOF) {
  319. $retval = $results->fields['fee'];
  320. }
  321. }
  322. if (empty($retval)) {
  323. $retval = "0000";
  324. }
  325. $retval = str_replace(".","",$retval);
  326. // if we generated an error, create an error return response
  327. if ($err) {
  328. return $this->_handleError($err);
  329. }
  330. else {
  331. // otherwise, we create the right response
  332. // with the state name
  333. return new xmlrpcresp(new xmlrpcval($retval,"i4"));
  334. }
  335. }
  336. function typeofservice($m) {
  337. $err="";
  338. $retval = "";
  339. $obj= $m->getparam(0);
  340. $key = $obj->getval();
  341. //phased out by HIPPA, use cpt modifiers instead
  342. $retval = "";
  343. // if we generated an error, create an error return response
  344. if ($err) {
  345. return $this->_handleError($err);
  346. }
  347. else {
  348. // otherwise, we create the right response
  349. // with the state name
  350. return new xmlrpcresp(new xmlrpcval($retval,"string"));
  351. }
  352. }
  353. function priorauth($m) {
  354. $err="";
  355. $retval = "";
  356. $obj= $m->getparam(0);
  357. $key = $obj->getval();
  358. $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";
  359. //echo $sql;
  360. $db = $GLOBALS['adodb']['db'];
  361. $results = $db->Execute($sql);
  362. if (!$results) {
  363. $err = $db->ErrorMsg();
  364. }
  365. else {
  366. if (!$results->EOF) {
  367. $retval = $results->fields['prior_auth_number'];
  368. }
  369. }
  370. // if we generated an error, create an error return response
  371. if ($err) {
  372. return $this->_handleError($err);
  373. }
  374. else {
  375. // otherwise, we create the right response
  376. // with the state name
  377. return new xmlrpcresp(new xmlrpcval($retval,"string"));
  378. }
  379. }
  380. function dateofservicestart($m) {
  381. $err="";
  382. $retval = "";
  383. $obj= $m->getparam(0);
  384. $key = $obj->getval();
  385. $sql = "SELECT * FROM billing where pid = '" . $_SESSION['patient_id'] . "' and encounter = '" . $_SESSION['billkey'] . "'and id ='" . $key ."'";
  386. //echo $sql;
  387. $db = $GLOBALS['adodb']['db'];
  388. $results = $db->Execute($sql);
  389. if (!$results) {
  390. $err = $db->ErrorMsg();
  391. }
  392. else {
  393. if (!$results->EOF) {
  394. $retval = $results->fields['date'];
  395. }
  396. }
  397. $retval = $this->_isodate(date("Y-m-d",strtotime($retval)));
  398. // if we generated an error, create an error return response
  399. if ($err) {
  400. return $this->_handleError($err);
  401. }
  402. else {
  403. // otherwise, we create the right response
  404. // with the state name
  405. return new xmlrpcresp(new xmlrpcval($retval,XMLRPCDATETIME));
  406. }
  407. }
  408. function dateofserviceend($m) {
  409. $err="";
  410. $retval = "";
  411. $obj= $m->getparam(0);
  412. $key = $obj->getval();
  413. $sql = "SELECT * FROM billing where pid = '" . $_SESSION['patient_id'] . "' and encounter = '" . $_SESSION['billkey'] . "'and id ='" . $key ."'";
  414. //echo $sql;
  415. $db = $GLOBALS['adodb']['db'];
  416. $results = $db->Execute($sql);
  417. if (!$results) {
  418. $err = $db->ErrorMsg();
  419. }
  420. else {
  421. if (!$results->EOF) {
  422. $retval = $results->fields['date'];
  423. }
  424. }
  425. $retval = $this->_isodate(date("Y-m-d",strtotime($retval)));
  426. // if we generated an error, create an error return response
  427. if ($err) {
  428. return $this->_handleError($err);
  429. }
  430. else {
  431. // otherwise, we create the right response
  432. // with the state name
  433. return new xmlrpcresp(new xmlrpcval($retval,XMLRPCDATETIME));
  434. }
  435. }
  436. function dateofhospitalstart($m) {
  437. $err="";
  438. //Now implemented by OpenEMR with Form: Misc Billing Options
  439. $retval = "";
  440. $obj= $m->getparam(0);
  441. $key = $obj->getval();
  442. $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";
  443. //echo $sql;
  444. $db = $GLOBALS['adodb']['db'];
  445. $results = $db->Execute($sql);
  446. if (!$results) {
  447. $err = $db->ErrorMsg();
  448. }
  449. else {
  450. if (!$results->EOF) {
  451. $retval = $results->fields['hospitalization_date_from'];
  452. }
  453. }
  454. $retval = date("Y-m-d",strtotime($retval));
  455. $retval = $this->_isodate($retval);
  456. // if we generated an error, create an error return response
  457. if ($err) {
  458. return $this->_handleError($err);
  459. }
  460. else {
  461. // otherwise, we create the right response
  462. // with the state name
  463. return new xmlrpcresp(new xmlrpcval($retval,XMLRPCDATETIME));
  464. }
  465. }
  466. function ishospitalized($m) {
  467. $err="";
  468. //Now implemented by OpenEMR with Form: Misc Billing Options
  469. $pkey = false;
  470. $retval = "";
  471. $obj= $m->getparam(0);
  472. $key = $obj->getval();
  473. $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";
  474. //echo $sql;
  475. $db = $GLOBALS['adodb']['db'];
  476. $results = $db->Execute($sql);
  477. if (!$results) {
  478. $err = $db->ErrorMsg();
  479. }
  480. else {
  481. if (!$results->EOF) {
  482. $retval = $results->fields['is_hospitalized'];
  483. if ($retval == "1") {$pkey = true;};
  484. }
  485. }
  486. // if we generated an error, create an error return response
  487. if ($err) {
  488. return $this->_handleError($err);
  489. }
  490. else {
  491. // otherwise, we create the right response
  492. // with the state name
  493. return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
  494. }
  495. }
  496. function isoutsidelab($m) {
  497. $err="";
  498. //Now implemented by OpenEMR with Form: Misc Billing Options
  499. $pkey = false;
  500. $retval = "";
  501. $obj= $m->getparam(0);
  502. $key = $obj->getval();
  503. $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";
  504. //echo $sql;
  505. $db = $GLOBALS['adodb']['db'];
  506. $results = $db->Execute($sql);
  507. if (!$results) {
  508. $err = $db->ErrorMsg();
  509. }
  510. else {
  511. if (!$results->EOF) {
  512. $retval = $results->fields['outside_lab'];
  513. if ($retval == "1") {$pkey = true;};
  514. }
  515. }
  516. // if we generated an error, create an error return response
  517. if ($err) {
  518. return $this->_handleError($err);
  519. }
  520. else {
  521. // otherwise, we create the right response
  522. // with the state name
  523. return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
  524. }
  525. }
  526. function outsidelabcharges($m) {
  527. $err="";
  528. //Now implemented by OpenEMR with form_misc_billing_options
  529. $retval = "";
  530. $obj= $m->getparam(0);
  531. $key = $obj->getval();
  532. $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";
  533. //echo $sql;
  534. $db = $GLOBALS['adodb']['db'];
  535. $results = $db->Execute($sql);
  536. if (!$results) {
  537. $err = $db->ErrorMsg();
  538. }
  539. else {
  540. if (!$results->EOF) {
  541. $retval = $results->fields['lab_amount'];
  542. }
  543. }
  544. // if we generated an error, create an error return response
  545. if ($err) {
  546. return $this->_handleError($err);
  547. }
  548. else {
  549. // otherwise, we create the right response
  550. // with the state name
  551. return new xmlrpcresp(new xmlrpcval($retval,"double"));
  552. }
  553. }
  554. function dateofhospitalend($m) {
  555. $err="";
  556. //Now implemented by OpenEMR with Form: Misc Billing Options
  557. $retval = "";
  558. $obj= $m->getparam(0);
  559. $key = $obj->getval();
  560. $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";
  561. //echo $sql;
  562. $db = $GLOBALS['adodb']['db'];
  563. $results = $db->Execute($sql);
  564. if (!$results) {
  565. $err = $db->ErrorMsg();
  566. }
  567. else {
  568. if (!$results->EOF) {
  569. $retval = $results->fields['hospitalization_date_to'];
  570. }
  571. }
  572. $retval = date("Y-m-d",strtotime($retval));
  573. $retval = $this->_isodate($retval);
  574. // if we generated an error, create an error return response
  575. if ($err) {
  576. return $this->_handleError($err);
  577. }
  578. else {
  579. // otherwise, we create the right response
  580. // with the state name
  581. return new xmlrpcresp(new xmlrpcval($retval,XMLRPCDATETIME));
  582. }
  583. }
  584. function medicaidresubmissioncode($m) {
  585. $err="";
  586. $retval = "";
  587. $obj= $m->getparam(0);
  588. $key = $obj->getval();
  589. $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";
  590. //echo $sql;
  591. $db = $GLOBALS['adodb']['db'];
  592. $results = $db->Execute($sql);
  593. if (!$results) {
  594. $err = $db->ErrorMsg();
  595. }
  596. else {
  597. if (!$results->EOF) {
  598. $retval = $results->fields['medicaid_resubmission_code'];
  599. }
  600. }
  601. // if we generated an error, create an error return response
  602. if ($err) {
  603. return $this->_handleError($err);
  604. }
  605. else {
  606. // otherwise, we create the right response
  607. // with the state name
  608. return new xmlrpcresp(new xmlrpcval($retval,"string"));
  609. }
  610. }
  611. function medicaidoriginalreference($m) {
  612. $err="";
  613. $retval = "";
  614. $obj= $m->getparam(0);
  615. $key = $obj->getval();
  616. $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";
  617. //echo $sql;
  618. $db = $GLOBALS['adodb']['db'];
  619. $results = $db->Execute($sql);
  620. if (!$results) {
  621. $err = $db->ErrorMsg();
  622. }
  623. else {
  624. if (!$results->EOF) {
  625. $retval = $results->fields['medicaid_original_reference'];
  626. }
  627. }
  628. // if we generated an error, create an error return response
  629. if ($err) {
  630. return $this->_handleError($err);
  631. }
  632. else {
  633. // otherwise, we create the right response
  634. // with the state name
  635. return new xmlrpcresp(new xmlrpcval($retval,"string"));
  636. }
  637. }
  638. function weightgrams($m) {
  639. $err="";
  640. $pkey = "2300";
  641. // if we generated an error, create an error return response
  642. if ($err) {
  643. return $this->_handleError($err);
  644. }
  645. else {
  646. // otherwise, we create the right response
  647. // with the state name
  648. return new xmlrpcresp(new xmlrpcval($pkey));
  649. }
  650. }
  651. function weightpounds($m) {
  652. $err="";
  653. //unimplemented by OpenEMR
  654. $pkey = "";
  655. // if we generated an error, create an error return response
  656. if ($err) {
  657. return $this->_handleError($err);
  658. }
  659. else {
  660. // otherwise, we create the right response
  661. // with the state name
  662. return new xmlrpcresp(new xmlrpcval($pkey));
  663. }
  664. }
  665. function hcfalocaluse10d($m) {
  666. $err="";
  667. //this needs to be customized on a payer to payer and state to state basis
  668. $pkey = "";
  669. // if we generated an error, create an error return response
  670. if ($err) {
  671. return $this->_handleError($err);
  672. }
  673. else {
  674. // otherwise, we create the right response
  675. // with the state name
  676. return new xmlrpcresp(new xmlrpcval($pkey,"string"));
  677. }
  678. }
  679. function hcfalocaluse19($m) {
  680. $err="";
  681. //this needs to be customized on a payer to payer and state to state basis
  682. $pkey = "";
  683. // if we generated an error, create an error return response
  684. if ($err) {
  685. return $this->_handleError($err);
  686. }
  687. else {
  688. // otherwise, we create the right response
  689. // with the state name
  690. return new xmlrpcresp(new xmlrpcval($pkey,"string"));
  691. }
  692. }
  693. function procarray($m) {
  694. $err="";
  695. $procs = array();
  696. $obj= $m->getparam(0);
  697. $key = $obj->getval();
  698. $keys = split("-",$key);
  699. $patient_id = $keys[0];
  700. $encounter = $keys[1];
  701. $_SESSION['billkey'] = $encounter;
  702. $_SESSION['patient_id'] = $patient_id;
  703. // Sort by procedure timestamp in order to get some consistency. In particular
  704. // freeb determines the provider from the first procedure in this array.
  705. $sql = "SELECT * FROM billing where (code_type = 'CPT4' or code_type = 'HCPCS') AND " .
  706. "encounter = '$encounter' AND pid = '$patient_id' and activity = '1' " .
  707. "order by date, id";
  708. //echo $sql;
  709. $db = $GLOBALS['adodb']['db'];
  710. $results = $db->Execute($sql);
  711. if (!$results) {
  712. $err = $db->ErrorMsg();
  713. }
  714. else {
  715. while (!$results->EOF) {
  716. $procs[] = new xmlrpcval($results->fields['id'],"i4");
  717. $results->MoveNext();
  718. }
  719. }
  720. // if we generated an error, create an error return response
  721. if ($err) {
  722. return $this->_handleError($err);
  723. }
  724. else {
  725. // otherwise, we create the right response
  726. // with the state name
  727. return new xmlrpcresp(new xmlrpcval($procs,"array"));
  728. }
  729. }
  730. function diagarray($m) {
  731. $err="";
  732. $diags = array();
  733. $obj= $m->getparam(0);
  734. $key = $obj->getval();
  735. $sql = "SELECT * FROM billing where id = '$key'";
  736. //echo $sql;
  737. $db = $GLOBALS['adodb']['db'];
  738. $results = $db->Execute($sql);
  739. if (!$results) {
  740. $err = $db->ErrorMsg();
  741. }
  742. else {
  743. if (!$results->EOF) {
  744. $diagstring = $results->fields['justify'];
  745. }
  746. }
  747. if (!empty($diagstring)) {
  748. $diag_codes = split(":",$diagstring);
  749. $diag_sql = "";
  750. foreach ($diag_codes as $dc) {
  751. if (!empty($dc)) {
  752. $diag_sql .= "'$dc',";
  753. }
  754. }
  755. if (substr($diag_sql,strlen($diag_sql) -1) == ",") {
  756. $diag_sql = substr($diag_sql,0,strlen($diag_sql) -1);
  757. }
  758. $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'";
  759. $results = $db->Execute($sql);
  760. if (!$results) {
  761. $err .= $db->ErrorMsg();
  762. }
  763. else {
  764. while (!$results->EOF) {
  765. $diags[$results->fields['code']] = new xmlrpcval($results->fields['id'],"i4");
  766. $results->MoveNext();
  767. }
  768. }
  769. $tmp_diags = $diags;
  770. $tmp_keys = array_keys($tmp_diags);
  771. $diags = array();
  772. foreach ($diag_codes as $code) {
  773. if (in_array($code,$tmp_keys)) {
  774. $diags[] = $tmp_diags[$code];
  775. }
  776. }
  777. }
  778. else {
  779. $diags[] = new xmlrpcval(0,"i4");
  780. }
  781. // if we generated an error, create an error return response
  782. if ($err) {
  783. return $this->_handleError($err);
  784. }
  785. else {
  786. // otherwise, we create the right response
  787. // with the state name
  788. return new xmlrpcresp(new xmlrpcval($diags,"array"));
  789. }
  790. }
  791. function patientkey($m) {
  792. $err="";
  793. $pkey ="";
  794. $pkey = $_SESSION['patient_id'];
  795. // if we generated an error, create an error return response
  796. if ($err) {
  797. return $this->_handleError($err);
  798. }
  799. else {
  800. // otherwise, we create the right response
  801. // with the state name
  802. return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
  803. }
  804. }
  805. // Practice Key is the billing location.
  806. //
  807. function practicekey($m) {
  808. $err="";
  809. $sql = "SELECT * FROM facility ORDER BY billing_location DESC, id ASC LIMIT 1";
  810. $db = $GLOBALS['adodb']['db'];
  811. $results = $db->Execute($sql);
  812. if (!$results) {
  813. $err = $db->ErrorMsg();
  814. }
  815. else {
  816. if (!$results->EOF) {
  817. $fkey = $results->fields['id'];
  818. }
  819. }
  820. if ($err) {
  821. return $this->_handleError($err);
  822. }
  823. else {
  824. return new xmlrpcresp(new xmlrpcval($fkey,"i4"));
  825. }
  826. }
  827. function providerkey($m) {
  828. $err="";
  829. $pkey ="";
  830. $obj= $m->getparam(0);
  831. $key = $obj->getval();
  832. $sql = "SELECT * FROM billing where id = '" . $key . "'";
  833. //echo $sql;
  834. $db = $GLOBALS['adodb']['db'];
  835. $results = $db->Execute($sql);
  836. if (!$results) {
  837. $err = $db->ErrorMsg();
  838. }
  839. else {
  840. if (!$results->EOF) {
  841. $pkey = $results->fields['provider_id'];
  842. }
  843. }
  844. // if we generated an error, create an error return response
  845. if ($err) {
  846. return $this->_handleError($err);
  847. }
  848. else {
  849. // otherwise, we create the right response
  850. // with the state name
  851. return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
  852. }
  853. }
  854. function referringproviderkey($m) {
  855. $err="";
  856. $pkey ="";
  857. $obj= $m->getparam(0);
  858. $key = $obj->getval();
  859. $sql = "SELECT * FROM patient_data where pid = '" . $key . "'";
  860. //echo $sql;
  861. $db = $GLOBALS['adodb']['db'];
  862. $results = $db->Execute($sql);
  863. if (!$results) {
  864. $err = $db->ErrorMsg();
  865. }
  866. else {
  867. if (!$results->EOF) {
  868. $pkey = $results->fields['providerID'];
  869. }
  870. }
  871. // if we generated an error, create an error return response
  872. if ($err) {
  873. return $this->_handleError($err);
  874. }
  875. else {
  876. // otherwise, we create the right response
  877. // with the state name
  878. return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
  879. }
  880. }
  881. function insuredkey($m) {
  882. $err="";
  883. $payer_id = "";
  884. $patient_id = "";
  885. $dos = "";
  886. $obj= $m->getparam(0);
  887. $key = $obj->getval();
  888. // $sql = "SELECT * FROM billing where id = '" . $key . "'";
  889. $sql = "SELECT b.pid, b.payer_id, fe.date FROM " .
  890. "billing AS b, form_encounter AS fe WHERE " .
  891. "b.id = '$key' AND fe.pid = b.pid AND fe.encounter = b.encounter";
  892. //echo $sql;
  893. $db = $GLOBALS['adodb']['db'];
  894. $results = $db->Execute($sql);
  895. if (!$results) {
  896. // $err = $db->ErrorMsg();
  897. return $this->_handleError($db->ErrorMsg());
  898. }
  899. else {
  900. if (!$results->EOF) {
  901. $payer_id = $results->fields['payer_id'];
  902. $patient_id = $results->fields['pid'];
  903. $dos = $results->fields['date'];
  904. }
  905. }
  906. $insured_id = "";
  907. // $sql = "SELECT * FROM insurance_data WHERE pid = '" . $_SESSION['patient_id'] . "' and provider = '" . $payer_id . "'";
  908. $sql = "SELECT id FROM insurance_data WHERE " .
  909. "pid = '$patient_id' AND provider = '$payer_id' AND " .
  910. "date <= '$dos' ORDER BY date DESC LIMIT 1";
  911. //echo $sql;
  912. $db = $GLOBALS['adodb']['db'];
  913. $results = $db->Execute($sql);
  914. if (!$results) {
  915. $err = $db->ErrorMsg();
  916. }
  917. else {
  918. if (!$results->EOF) {
  919. $insured_id = $results->fields['id'];
  920. }
  921. }
  922. //we are returning the record id of the appropriate entry in insurance_data
  923. //there is no relational data, all the subscriber/insured information is kept monolithically in that table
  924. // if we generated an error, create an error return response
  925. if ($err) {
  926. return $this->_handleError($err);
  927. }
  928. else {
  929. // otherwise, we create the right response
  930. // with the state name
  931. return new xmlrpcresp(new xmlrpcval($insured_id,"i4"));
  932. }
  933. }
  934. function otherinsuredkey($m) {
  935. $err="";
  936. //openemr does not currently implement other insured
  937. $pkey = 0;
  938. // if we generated an error, create an error return response
  939. if ($err) {
  940. return $this->_handleError($err);
  941. }
  942. else {
  943. // otherwise, we create the right response
  944. // with the state name
  945. return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
  946. }
  947. }
  948. function payerkey($m) {
  949. $err="";
  950. $pkey = "";
  951. $obj= $m->getparam(0);
  952. $key = $obj->getval();
  953. $sql = "SELECT * FROM billing where id = '" . $key . "'";
  954. $db = $GLOBALS['adodb']['db'];
  955. $results = $db->Execute($sql);
  956. if (!$results) {
  957. $err = $db->ErrorMsg() . " $sql";
  958. }
  959. else {
  960. while (!$results->EOF) {
  961. $pkey = $results->fields['payer_id'];
  962. $results->MoveNext();
  963. }
  964. }
  965. // if we generated an error, create an error return response
  966. if ($err) {
  967. return $this->_handleError($err);
  968. }
  969. else {
  970. // otherwise, we create the right response
  971. // with the state name
  972. return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
  973. }
  974. }
  975. function secondpayerkey($m) {
  976. $err="";
  977. //unimplemented by OpenEMR
  978. $pkey = 0;
  979. // if we generated an error, create an error return response
  980. if ($err) {
  981. return $this->_handleError($err);
  982. }
  983. else {
  984. // otherwise, we create the right response
  985. // with the state name
  986. return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
  987. }
  988. }
  989. // This is for the facility where service was rendered.
  990. //
  991. function facilitykey($m) {
  992. $eid = "";
  993. $patient_id = "";
  994. $fname ="";
  995. $obj= $m->getparam(0);
  996. $key = $obj->getval();
  997. $sql = "SELECT pid, encounter FROM billing where id = '$key'";
  998. $db = $GLOBALS['adodb']['db'];
  999. $results = $db->Execute($sql);
  1000. if (!$results) {
  1001. $err = $db->ErrorMsg();
  1002. }
  1003. else {
  1004. if (!$results->EOF) {
  1005. $eid = $results->fields['encounter'];
  1006. $patient_id = $results->fields['pid'];
  1007. }
  1008. }
  1009. $sql = "SELECT facility_id FROM form_encounter where encounter = '$eid' and pid = '$patient_id'";
  1010. $results = $db->Execute($sql);
  1011. if (!$results) {
  1012. $err = $db->ErrorMsg();
  1013. }
  1014. else {
  1015. if (!$results->EOF) {
  1016. $facility_id = $results->fields['facility_id'];
  1017. }
  1018. }
  1019. /****
  1020. $sql = "SELECT * FROM facility where name = '" . $fname . "'";
  1021. $results = $db->Execute($sql);
  1022. if (!$results) {
  1023. echo "error";
  1024. $err = $db->ErrorMsg();
  1025. }
  1026. else {
  1027. if (!$results->EOF) {
  1028. $fkey = $results->fields['id'];
  1029. }
  1030. }
  1031. ****/
  1032. if ($err) {
  1033. return $this->_handleError($err);
  1034. }
  1035. else {
  1036. return new xmlrpcresp(new xmlrpcval($facility_id, "i4"));
  1037. }
  1038. }
  1039. function billingcontactkey($m) {
  1040. $err="";
  1041. $pkey = 0;
  1042. // if we generated an error, create an error return response
  1043. if ($err) {
  1044. return $this->_handleError($err);
  1045. }
  1046. else {
  1047. // otherwise, we create the right response
  1048. // with the state name
  1049. return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
  1050. }
  1051. }
  1052. function billingservicekey($m) {
  1053. $err="";
  1054. $pkey = 0;
  1055. // if we generated an error, create an error return response
  1056. if ($err) {
  1057. return $this->_handleError($err);
  1058. }
  1059. else {
  1060. // otherwise, we create the right response
  1061. // with the state name
  1062. return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
  1063. }
  1064. }
  1065. function isusingbillingservice($m) {
  1066. $err="";
  1067. $pkey = false;
  1068. // if we generated an error, create an error return response
  1069. if ($err) {
  1070. return $this->_handleError($err);
  1071. }
  1072. else {
  1073. // otherwise, we create the right response
  1074. // with the state name
  1075. return new xmlrpcresp(new xmlrpcval($pkey,"i4"));
  1076. }
  1077. }
  1078. function clearinghousekey($m) {
  1079. $err="";
  1080. $eid = "";
  1081. $patient_id = "";
  1082. $fname ="";
  1083. $obj= $m->getparam(0);
  1084. $key = $obj->getval();
  1085. $sql = "SELECT * FROM billing where id = '" . $key . "'";
  1086. $db = $GLOBALS['adodb']['db'];
  1087. $results = $db->Execute($sql);
  1088. if (!$results) {
  1089. $err = $db->ErrorMsg();
  1090. }
  1091. else {
  1092. if (!$results->EOF) {
  1093. $eid = $results->fields['encounter'];
  1094. $patient_id = $results->fields['pid'];
  1095. }
  1096. }
  1097. $sql = "SELECT * FROM form_encounter where encounter = '" . $eid ."' and pid = '" . $patient_id ."'";
  1098. $results = $db->Execute($sql);
  1099. if (!$results) {
  1100. $err = $db->ErrorMsg();
  1101. }
  1102. else {
  1103. if (!$results->EOF) {
  1104. $fname = $results->fields['facility'];
  1105. }
  1106. }
  1107. $sql = "SELECT * FROM facility where name = '" . $fname ."'";
  1108. $results = $db->Execute($sql);
  1109. if (!$results) {
  1110. echo "error";
  1111. $err = $db->ErrorMsg();
  1112. }
  1113. else {
  1114. if (!$results->EOF) {
  1115. $fkey = $results->fields['id'];
  1116. }
  1117. }
  1118. // if we generated an error, create an error return response
  1119. if ($err) {
  1120. return $this->_handleError($err);
  1121. }
  1122. else {
  1123. // otherwise, we create the right response
  1124. // with the state name
  1125. return new xmlrpcresp(new xmlrpcval($fkey,"i4"));
  1126. }
  1127. }
  1128. function ndcline($m) {
  1129. $err="";
  1130. $retval = "";
  1131. $obj= $m->getparam(0);
  1132. $key = $obj->getval();
  1133. $sql = "SELECT ndc_info FROM billing where id = '$key'";
  1134. $db = $GLOBALS['adodb']['db'];
  1135. $results = $db->Execute($sql);
  1136. if (!$results) {
  1137. $err = $db->ErrorMsg();
  1138. }
  1139. else {
  1140. if (!$results->EOF) {
  1141. // Medicare says the NDC number must be 11 digits with no dashes.
  1142. $ndcinfo = $results->fields['ndc_info'];
  1143. if (preg_match('/^N4(\S+)\s+(\S*)/', $ndcinfo, $tmpa)) {
  1144. $ndc = $tmpa[1];
  1145. if (preg_match('/^(\d+)-(\d+)-(\d+)$/', $ndc, $tmpb)) {
  1146. $ndc = sprintf('%05d%04d%02d', $tmpb[1], $tmpb[2], $tmpb[3]);
  1147. }
  1148. $ndcinfo = "N4$ndc " . $tmpa[2];
  1149. }
  1150. $retval = $ndcinfo;
  1151. }
  1152. }
  1153. if ($err) {
  1154. return $this->_handleError($err);
  1155. }
  1156. else {
  1157. return new xmlrpcresp(new xmlrpcval($retval, "string"));
  1158. }
  1159. }
  1160. }
  1161. //'FreeB.FBProcedure.isUsingClearingHouse' => \&FreeB_FBProcedure_isUsingClearingHouse,
  1162. //'FreeB.FBProcedure.ProcArray' => \&FreeB_FBProcedure_ProcArray,
  1163. //'FreeB.FBProcedure.DiagArray' => \&FreeB_FBProcedure_DiagArray,
  1164. //'FreeB.FBProcedure.CPT4Code' => \&FreeB_FBProcedure_CPT4Code,
  1165. //'FreeB.FBProcedure.CPT5Code' => \&FreeB_FBProcedure_CPT4Code,
  1166. //'FreeB.FBProcedure.CPTUnits' => \&FreeB_FBProcedure_CPTUnits,
  1167. //'FreeB.FBProcedure.CPTModifier' => \&FreeB_FBProcedure_CPTModifier,
  1168. //'FreeB.FBProcedure.CPTCharges' => \&FreeB_FBProcedure_CPTCharges,
  1169. //'FreeB.FBProcedure.CPTEmergency' => \&FreeB_FBProcedure_CPTEmergency,
  1170. //'FreeB.FBProcedure.CPTCOB' => \&FreeB_FBProcedure_CPTCOB,
  1171. //'FreeB.FBProcedure.CPTEPSDT' => \&FreeB_FBProcedure_CPTEPSDT,
  1172. //'FreeB.FBProcedure.AmountPaid' => \&FreeB_FBProcedure_AmountPaid,
  1173. //'FreeB.FBProcedure.TypeOfService' => \&FreeB_FBProcedure_TypeOfService,
  1174. //'FreeB.FBProcedure.PriorAuth' => \&FreeB_FBProcedure_PriorAuth,
  1175. //'FreeB.FBProcedure.DateOfServiceStart' => \&FreeB_FBProcedure_DateOfServiceStart,
  1176. //'FreeB.FBProcedure.DateOfServiceEnd' => \&FreeB_FBProcedure_DateOfServiceEnd,
  1177. //'FreeB.FBProcedure.DateOfHospitalStart' => \&FreeB_FBProcedure_DateOfHospitalStart,
  1178. //'FreeB.FBProcedure.isHospitalized' => \&FreeB_FBProcedure_isHospitalized,
  1179. //'FreeB.FBProcedure.isOutsideLab' => \&FreeB_FBProcedure_isOutsideLab,
  1180. //'FreeB.FBProcedure.OutsideLabCharges' => \&FreeB_FBProcedure_OutSideLabCharges,
  1181. //'FreeB.FBProcedure.DateOfHospitalEnd' => \&FreeB_FBProcedure_DateOfHospitalEnd,
  1182. //'FreeB.FBProcedure.MedicaidResubmissionCode' => \&FreeB_FBProcedure_MedicaidResubmissionCode,
  1183. //'FreeB.FBProcedure.MedicaidOriginalReference' => \&FreeB_FBProcedure_MedicaidOriginalReference,
  1184. //'FreeB.FBProcedure.WeightGrams' => \&FreeB_FBProcedure_WeightGrams,
  1185. //'FreeB.FBProcedure.WeightPounds' => \&FreeB_FBProcedure_WeightPounds,
  1186. //'FreeB.FBProcedure.HCFALocalUse10d' => \&FreeB_FBProcedure_HCFALocalUse10d,
  1187. //'FreeB.FBProcedure.HCFALocalUse19' => \&FreeB_FBProcedure_HCFALocalUse19,
  1188. ?>