Changed function SLConnect() to use $GLOBALS['oer_config']['ws_accounting']['server...
[openemr.git] / library / classes / WSClaim.class.php
blob13cb588e5aa1786e9972522c58fe91b41cc987f7
1 <?php
3 require_once (dirname(__FILE__) . "/WSWrapper.class.php");
4 include_once (dirname(__FILE__) . "/../sqlconf.php");
5 include_once (dirname(__FILE__) . "/../sql.inc");
6 include_once (dirname(__FILE__) . "/../../includes/config.php");
8 class WSClaim extends WSWrapper{
10 var $patient_id;
11 var $foreign_provider_id;
12 var $foreign_patient_id;
13 var $payer_id;
14 var $encounter;
15 var $foreign_payer_id;
16 var $claim;
17 var $_db;
19 function WSClaim($patient_id, $encounter) {
20 if (!is_numeric($patient_id) && is_numeric($encounter)) return;
22 parent::WSWrapper(null,false);
24 $this->patient_id = $patient_id;
25 $this->encounter = $encounter;
26 $this->claim = null;
27 $this->_db = $GLOBALS['adodb']['db'];
28 if (!$this->_config['enabled']) return;
30 if ($this->load_claim()) {
31 $function['ezybiz.add_invoice'] = array(new xmlrpcval($this->claim,"struct"));
32 $this->send($function);
35 //print_r($this->claim);
39 function load_claim() {
40 if (!$this->load_patient_foreign_id() ||
41 !$this->load_payer_foreign_id() ||
42 !$this->load_provider_foreign_id() )
43 return false;
44 $invoice_info = array();
46 // Create invoice notes for the new invoice that list the patient's
47 // insurance plans. This is so that when payments are posted, the user
48 // can easily see if a secondary claim needs to be submitted.
50 $insnotes = "";
51 $insno = 0;
52 foreach (array("primary", "secondary", "tertiary") as $instype) {
53 ++$insno;
54 $sql = "SELECT insurance_companies.name " .
55 "FROM insurance_data, insurance_companies WHERE " .
56 "insurance_data.pid = " . $this->patient_id . " AND " .
57 "insurance_data.type = '$instype' AND " .
58 "insurance_companies.id = insurance_data.provider " .
59 "LIMIT 1";
60 $result = $this->_db->Execute($sql);
61 if ($result && !$result->EOF && $result->fields['name']) {
62 if ($insnotes) $insnotes .= "\n";
63 $insnotes .= "Ins$insno: " . $result->fields['name'];
66 $invoice_info['notes'] = $insnotes;
68 /****
69 $sql = "SELECT b.*, CONCAT(pd.fname,' ',pd.mname,' ',pd.lname) as patient_name " .
70 "FROM billing as b LEFT JOIN patient_data as pd on b.pid=pd.pid where " .
71 "b.encounter = '" . $this->encounter ."' AND b.pid = '" . $this->patient_id .
72 "' AND b.billed = 1 AND b.activity != '0' AND authorized = '1'";
73 ****/
75 $sql = "SELECT b.*, e.date AS dosdate, " .
76 "CONCAT(pd.fname,' ',pd.mname,' ',pd.lname) as patient_name " .
77 "FROM billing AS b " .
78 "LEFT JOIN form_encounter AS e ON e.encounter = b.encounter AND e.pid = b.pid " .
79 "LEFT JOIN patient_data AS pd ON b.pid = pd.pid " .
80 "WHERE " .
81 "b.encounter = '" . $this->encounter . "' AND b.pid = '" . $this->patient_id .
82 "' AND b.billed = 1 AND b.activity != '0' AND authorized = '1'";
84 $result = $this->_db->Execute($sql);
86 $invoice_info['salesman'] = $this->foreign_provider_id;
87 $invoice_info['customerid'] = $this->foreign_patient_id;
88 $invoice_info['payer_id'] = $this->foreign_payer_id;
89 // $invoice_info['invoicenumber'] = $this->patient_id . "000" . $this->encounter;
90 $invoice_info['invoicenumber'] = $this->patient_id . "." . $this->encounter;
92 $counter = 0;
93 $total = 0;
94 $patient_info = array();
95 $payer_info = array();
97 while ($result && !$result->EOF) {
99 // if ($result->fields['process_date'] == null) {
100 // //don't sync a claim that has not been processed, they may just want to mark this as billed
101 // return false;
102 // }
104 // The above is silly. If we don't want to sync, why are we here?
105 // All bills should be in the accounting system, and mark-as-cleared
106 // is the only reasonable way to process cash-only patients.
108 $process_date = ($result->fields['process_date'] == null) ?
109 date("m-d-Y") :
110 date("m-d-Y", strtotime($result->fields['process_date']));
112 if ($counter == 0) {
113 //unused but supported by ezybiz, helpful in debugging
114 // actualy the dosdate can be used if you want that as the invoice date
115 $invoice_info['customer'] = $result->fields['patient_name'];
116 $invoice_info['invoicedate'] = $process_date;
117 $invoice_info['duedate'] = $process_date;
118 $invoice_info['items'] = array();
119 // $invoice_info['dosdate'] = date("m-d-Y",strtotime($result->fields['date']));
120 $invoice_info['dosdate'] = date("m-d-Y",strtotime($result->fields['dosdate']));
123 $tii = array();
124 //This is how we set the line items for the invoice, using codes from our encounter
125 //if ($result->fields['code_type'] == "CPT4" || $result->fields['code_type'] == "HCPCS") {
126 //if( $result->fields['code_type'] != "ICD9" ) {
127 if( $result->fields['code_type'] == "COPAY")
129 $patient_info['payment_amount'] += sprintf("%01.2f",$result->fields['fee']);
131 else
133 $payer_info['payment_amount'] += sprintf("%01.2f",$result->fields['fee']);
136 $tii['maincode'] = $result->fields['code'];
137 $tii['itemtext'] = $result->fields['code_type'] .":" .
138 $result->fields['code'] . " " . $result->fields['code_text'] . " " .
139 $result->fields['justify'];
141 // $tii['qty'] = $result->fields['units'];
142 // if (!$tii['qty'] > 0) {
143 // $tii['qty'] = 1;
144 // }
145 $tii['qty'] = 1;
147 $tii['price'] = sprintf("%01.2f",$result->fields['fee']);
148 $total += $tii['price'];
149 $tii['glaccountid'] = $this->_config['income_acct'];
150 $invoice_info['items'][] = $tii;
152 $result->MoveNext();
153 $counter++;
156 for($counter = 0; $counter < 2; $counter++)
158 $fee = 0;
159 $billto = 0;
160 if($counter == 0)
162 $fee = $patient_info['payment_amount'];
163 $billto = $this->foreign_patient_id;
165 else
167 $fee = $payer_info['payment_amount'];
168 $billto = $this->foreign_payer_id;
170 $invoice_info["invoiceid$counter"] = $this->patient_id . "000" . $this->encounter;
171 $invoice_info["amount$counter"] = $fee;
172 $invoice_info["invoicenumber$counter"] = $this->patient_id . "000" . $this->encounter;
173 $invoice_info["interest$counter"] = 0;
174 $invoice_info["billtoid$counter"] = $billto;
176 $invoice_info['subtotal'] = sprintf("%01.2f",$total);
177 $invoice_info['total'] = sprintf("%01.2f",$total);
179 $this->claim = $invoice_info;
180 return true;
183 function load_provider_foreign_id() {
184 $sql = "SELECT foreign_id from integration_mapping as im LEFT JOIN billing as b on im.local_id=b.provider_id where encounter = '" . $this->encounter . "' and b.pid = '" . $this->patient_id . "' and im.local_table='users' and im.foreign_table='salesman'";
185 $result = $this->_db->Execute($sql);
186 if($result && !$result->EOF) {
187 $this->foreign_provider_id = $result->fields['foreign_id'];
188 return true;
190 else {
191 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. Provider: '" . $this->patient_id . "'<br>";
192 return false;
196 function load_patient_foreign_id() {
197 $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'";
198 $result = $this->_db->Execute($sql);
199 if($result && !$result->EOF) {
200 $this->foreign_patient_id = $result->fields['foreign_id'];
201 return true;
203 else {
204 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>";
205 return false;
209 function load_payer_foreign_id() {
210 $sql = "SELECT payer_id from billing where encounter = '" . $this->encounter . "' and pid = '" . $this->patient_id . "'";
211 $result = $this->_db->Execute($sql);
212 if($result && !$result->EOF) {
213 $this->payer_id = $result->fields['payer_id'];
215 else {
216 echo "No payer id for this claim could be found";
217 return false;
219 // See comments in globals.php:
220 if ($GLOBALS['insurance_companies_are_not_customers']) {
221 $this->foreign_payer_id = $this->payer_id;
223 else {
224 $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'";
225 $result = $this->_db->Execute($sql);
226 if($result && !$result->EOF) {
227 $this->foreign_payer_id = $result->fields['foreign_id'];
229 else {
230 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>";
231 return false;
234 return true;
238 //$wsc = new WSClaim("3","20040622");