PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/library/classes/WSClaim.class.php

https://github.com/md-tech/openemr
PHP | 255 lines | 197 code | 33 blank | 25 comment | 40 complexity | 1a6bee5a96b62290a59115a926280f41 MD5 | raw file
  1. <?php
  2. require_once (dirname(__FILE__) . "/WSWrapper.class.php");
  3. include_once (dirname(__FILE__) . "/../sqlconf.php");
  4. include_once (dirname(__FILE__) . "/../sql.inc");
  5. include_once (dirname(__FILE__) . "/../../includes/config.php");
  6. class WSClaim extends WSWrapper{
  7. var $patient_id;
  8. var $foreign_provider_id;
  9. var $foreign_patient_id;
  10. var $payer_id;
  11. var $encounter;
  12. var $foreign_payer_id;
  13. var $claim;
  14. var $_db;
  15. function WSClaim($patient_id, $encounter) {
  16. if (!is_numeric($patient_id) && is_numeric($encounter)) return;
  17. parent::WSWrapper(null,false);
  18. $this->patient_id = $patient_id;
  19. $this->encounter = $encounter;
  20. $this->claim = null;
  21. $this->_db = $GLOBALS['adodb']['db'];
  22. if ($this->_config['enabled'] === 2) {
  23. return;
  24. }
  25. if ($this->load_claim()) {
  26. $function['ezybiz.add_invoice'] = array(new xmlrpcval($this->claim,"struct"));
  27. $this->send($function);
  28. }
  29. //print_r($this->claim);
  30. }
  31. ///////////////////////////////////////////////////////////////////////
  32. // The remainder is used only for SQL-Ledger support, to be deprecated.
  33. ///////////////////////////////////////////////////////////////////////
  34. function load_claim() {
  35. if (!$this->load_patient_foreign_id() ||
  36. !$this->load_payer_foreign_id() ||
  37. !$this->load_provider_foreign_id() )
  38. return false;
  39. $invoice_info = array();
  40. // Get encounter date and patient name.
  41. $sql = "SELECT e.date AS dosdate, " .
  42. "CONCAT(pd.fname,' ',pd.mname,' ',pd.lname) as patient_name " .
  43. "FROM form_encounter AS e, patient_data AS pd " .
  44. "WHERE " .
  45. "e.encounter = '" . $this->encounter . "' AND " .
  46. "e.pid = '" . $this->patient_id . "' AND " .
  47. "pd.pid = e.pid";
  48. $eres = $this->_db->Execute($sql);
  49. $dosdate = substr($eres->fields['dosdate'], 0, 10);
  50. // Create invoice notes for the new invoice that list the patient's
  51. // insurance plans. This is so that when payments are posted, the user
  52. // can easily see if a secondary claim needs to be submitted.
  53. //
  54. $insnotes = "";
  55. $insno = 0;
  56. foreach (array("primary", "secondary", "tertiary") as $instype) {
  57. ++$insno;
  58. $sql = "SELECT ic.name " .
  59. "FROM insurance_data AS id, insurance_companies AS ic WHERE " .
  60. "id.pid = " . $this->patient_id . " AND " .
  61. "id.type = '$instype' AND " .
  62. "id.date <= '$dosdate' AND " .
  63. "ic.id = id.provider " .
  64. "ORDER BY id.date DESC LIMIT 1";
  65. $result = $this->_db->Execute($sql);
  66. if ($result && !$result->EOF && $result->fields['name']) {
  67. if ($insnotes) $insnotes .= "\n";
  68. $insnotes .= "Ins$insno: " . $result->fields['name'];
  69. }
  70. }
  71. $invoice_info['notes'] = $insnotes;
  72. $sql = "SELECT * FROM billing WHERE " .
  73. "encounter = '" . $this->encounter . "' AND pid = '" . $this->patient_id .
  74. "' AND billed = 1 AND activity != 0 AND authorized = 1";
  75. $result = $this->_db->Execute($sql);
  76. $invoice_info['salesman'] = $this->foreign_provider_id;
  77. $invoice_info['customerid'] = $this->foreign_patient_id;
  78. $invoice_info['payer_id'] = $this->foreign_payer_id;
  79. $invoice_info['invoicenumber'] = $this->patient_id . "." . $this->encounter;
  80. $counter = 0;
  81. $total = 0;
  82. $patient_info = array();
  83. $payer_info = array();
  84. while ($result && !$result->EOF) {
  85. // All bills should be in the accounting system, and mark-as-cleared
  86. // is the only reasonable way to process cash-only patients.
  87. //
  88. $process_date = ($result->fields['process_date'] == null) ?
  89. date("m-d-Y") :
  90. date("m-d-Y", strtotime($result->fields['process_date']));
  91. if ($counter == 0) {
  92. //unused but supported by ezybiz, helpful in debugging
  93. // actualy the dosdate can be used if you want that as the invoice date
  94. $invoice_info['customer'] = $eres->fields['patient_name'];
  95. $invoice_info['invoicedate'] = $process_date;
  96. $invoice_info['duedate'] = $process_date;
  97. $invoice_info['items'] = array();
  98. $invoice_info['dosdate'] = date("m-d-Y",strtotime($eres->fields['dosdate']));
  99. }
  100. $tii = array();
  101. //This is how we set the line items for the invoice, using codes from our encounter
  102. //if ($result->fields['code_type'] == "CPT4" || $result->fields['code_type'] == "HCPCS") {
  103. //if( $result->fields['code_type'] != "ICD9" ) {
  104. if( $result->fields['code_type'] == "COPAY")
  105. {
  106. $patient_info['payment_amount'] += sprintf("%01.2f",$result->fields['fee']);
  107. }
  108. else
  109. {
  110. $payer_info['payment_amount'] += sprintf("%01.2f",$result->fields['fee']);
  111. }
  112. // New as of 2007-06-21: wherever we put a procedure code in the
  113. // invoice, append a colon and the modifier if there is one. This way
  114. // we can better match up payments and adjustments with the billing data
  115. // in OpenEMR.
  116. $codekey = $result->fields['code'];
  117. if ($result->fields['modifier']) $codekey .= ':' . $result->fields['modifier'];
  118. $tii['maincode'] = $codekey;
  119. $tii['itemtext'] = $result->fields['code_type'] . ":" .
  120. $codekey . " " . $result->fields['code_text'] . " " .
  121. $result->fields['justify'];
  122. // New logic that respects units:
  123. $units = max(1, intval($result->fields['units']));
  124. $amount = sprintf("%01.2f", $result->fields['fee']);
  125. $price = $amount / $units;
  126. $tmp = sprintf("%01.2f", $price);
  127. if (abs($price - $tmp) < 0.000001) $price = $tmp;
  128. $tii['qty'] = $units;
  129. $tii['price'] = $price;
  130. $total += $amount;
  131. $tii['glaccountid'] = $this->_config['income_acct'];
  132. $invoice_info['items'][] = $tii;
  133. $result->MoveNext();
  134. $counter++;
  135. }
  136. // I think maybe this info is not used.
  137. //
  138. for($counter = 0; $counter < 2; $counter++)
  139. {
  140. $fee = 0;
  141. $billto = 0;
  142. if($counter == 0)
  143. {
  144. $fee = $patient_info['payment_amount'];
  145. $billto = $this->foreign_patient_id;
  146. }
  147. else
  148. {
  149. $fee = $payer_info['payment_amount'];
  150. $billto = $this->foreign_payer_id;
  151. }
  152. $invoice_info["invoiceid$counter"] = $this->patient_id . "000" . $this->encounter;
  153. $invoice_info["amount$counter"] = $fee;
  154. $invoice_info["invoicenumber$counter"] = $this->patient_id . "000" . $this->encounter;
  155. $invoice_info["interest$counter"] = 0;
  156. $invoice_info["billtoid$counter"] = $billto;
  157. }
  158. $invoice_info['subtotal'] = sprintf("%01.2f",$total);
  159. $invoice_info['total'] = sprintf("%01.2f",$total);
  160. $this->claim = $invoice_info;
  161. return true;
  162. }
  163. function load_provider_foreign_id() {
  164. $sql = "SELECT foreign_id FROM integration_mapping AS im " .
  165. "LEFT JOIN billing AS b ON im.local_id = b.provider_id WHERE " .
  166. "b.encounter = '" . $this->encounter . "' AND " .
  167. "b.pid = '" . $this->patient_id . "' AND im.local_table = 'users' " .
  168. "AND im.foreign_table = 'salesman'";
  169. $result = $this->_db->Execute($sql);
  170. if($result && !$result->EOF) {
  171. $this->foreign_provider_id = $result->fields['foreign_id'];
  172. return true;
  173. }
  174. else {
  175. echo "Provider has not been previously sent to external system or no " .
  176. "entry was found for them in the integration mapping, could not " .
  177. "send claim. Patient: '" . $this->patient_id . "', Encounter: '" .
  178. $this->encounter . "'<br>";
  179. return false;
  180. }
  181. }
  182. function load_patient_foreign_id() {
  183. $sql = "SELECT foreign_id from integration_mapping as im LEFT JOIN patient_data as pd on im.local_id=pd.id where pd.pid = '" . $this->patient_id . "' and im.local_table='patient_data' and im.foreign_table='customer'";
  184. $result = $this->_db->Execute($sql);
  185. if($result && !$result->EOF) {
  186. $this->foreign_patient_id = $result->fields['foreign_id'];
  187. return true;
  188. }
  189. else {
  190. echo "Entry has not been previously sent to external system or no entry was found for them in the integration mapping, could not send claim. Patient: '" . $this->patient_id . "'<br>";
  191. return false;
  192. }
  193. }
  194. function load_payer_foreign_id() {
  195. $sql = "SELECT payer_id from billing where encounter = '" . $this->encounter . "' and pid = '" . $this->patient_id . "'";
  196. $result = $this->_db->Execute($sql);
  197. if($result && !$result->EOF) {
  198. $this->payer_id = $result->fields['payer_id'];
  199. }
  200. else {
  201. echo "No payer id for this claim could be found";
  202. return false;
  203. }
  204. // See comments in globals.php:
  205. if ($GLOBALS['insurance_companies_are_not_customers']) {
  206. $this->foreign_payer_id = $this->payer_id;
  207. }
  208. else {
  209. $sql = "SELECT foreign_id from integration_mapping as im LEFT JOIN billing as b on im.local_id=b.payer_id where b.payer_id = '" . $this->payer_id . "' and im.local_table='insurance_companies' and im.foreign_table='customer'";
  210. $result = $this->_db->Execute($sql);
  211. if($result && !$result->EOF) {
  212. $this->foreign_payer_id = $result->fields['foreign_id'];
  213. }
  214. else {
  215. echo "Entry has not been previously sent to external system or no entry was found for them in the integration mapping, could not send claim. Insurance Company: '" . $this->payer_id . "'<br>";
  216. return false;
  217. }
  218. }
  219. return true;
  220. }
  221. } // End class WSClaim
  222. ?>