Added CAMOS form and 'Patient Photograph' document category to pre-installed.
[openemr.git] / library / classes / WSClaim.class.php
blob682db894a727fdb685a9a0ea47ef9ca36d4270b9
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 // Get encounter date and patient name.
47 $sql = "SELECT e.date AS dosdate, " .
48 "CONCAT(pd.fname,' ',pd.mname,' ',pd.lname) as patient_name " .
49 "FROM form_encounter AS e, patient_data AS pd " .
50 "WHERE " .
51 "e.encounter = '" . $this->encounter . "' AND " .
52 "e.pid = '" . $this->patient_id . "' AND " .
53 "pd.pid = e.pid";
54 $eres = $this->_db->Execute($sql);
55 $dosdate = substr($eres->fields['dosdate'], 0, 10);
57 // Create invoice notes for the new invoice that list the patient's
58 // insurance plans. This is so that when payments are posted, the user
59 // can easily see if a secondary claim needs to be submitted.
61 $insnotes = "";
62 $insno = 0;
63 foreach (array("primary", "secondary", "tertiary") as $instype) {
64 ++$insno;
65 $sql = "SELECT ic.name " .
66 "FROM insurance_data AS id, insurance_companies AS ic WHERE " .
67 "id.pid = " . $this->patient_id . " AND " .
68 "id.type = '$instype' AND " .
69 "id.date <= '$dosdate' AND " .
70 "ic.id = id.provider " .
71 "ORDER BY id.date DESC LIMIT 1";
72 $result = $this->_db->Execute($sql);
73 if ($result && !$result->EOF && $result->fields['name']) {
74 if ($insnotes) $insnotes .= "\n";
75 $insnotes .= "Ins$insno: " . $result->fields['name'];
78 $invoice_info['notes'] = $insnotes;
80 $sql = "SELECT * FROM billing WHERE " .
81 "encounter = '" . $this->encounter . "' AND pid = '" . $this->patient_id .
82 "' AND billed = 1 AND 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 . "." . $this->encounter;
91 $counter = 0;
92 $total = 0;
93 $patient_info = array();
94 $payer_info = array();
96 while ($result && !$result->EOF) {
98 // All bills should be in the accounting system, and mark-as-cleared
99 // is the only reasonable way to process cash-only patients.
101 $process_date = ($result->fields['process_date'] == null) ?
102 date("m-d-Y") :
103 date("m-d-Y", strtotime($result->fields['process_date']));
105 if ($counter == 0) {
106 //unused but supported by ezybiz, helpful in debugging
107 // actualy the dosdate can be used if you want that as the invoice date
108 $invoice_info['customer'] = $eres->fields['patient_name'];
109 $invoice_info['invoicedate'] = $process_date;
110 $invoice_info['duedate'] = $process_date;
111 $invoice_info['items'] = array();
112 $invoice_info['dosdate'] = date("m-d-Y",strtotime($eres->fields['dosdate']));
115 $tii = array();
116 //This is how we set the line items for the invoice, using codes from our encounter
117 //if ($result->fields['code_type'] == "CPT4" || $result->fields['code_type'] == "HCPCS") {
118 //if( $result->fields['code_type'] != "ICD9" ) {
119 if( $result->fields['code_type'] == "COPAY")
121 $patient_info['payment_amount'] += sprintf("%01.2f",$result->fields['fee']);
123 else
125 $payer_info['payment_amount'] += sprintf("%01.2f",$result->fields['fee']);
128 // New as of 2007-06-21: wherever we put a procedure code in the
129 // invoice, append a colon and the modifier if there is one. This way
130 // we can better match up payments and adjustments with the billing data
131 // in OpenEMR.
132 $codekey = $result->fields['code'];
133 if ($result->fields['modifier']) $codekey .= ':' . $result->fields['modifier'];
135 $tii['maincode'] = $codekey;
136 $tii['itemtext'] = $result->fields['code_type'] .":" .
137 $codekey . " " . $result->fields['code_text'] . " " .
138 $result->fields['justify'];
140 // $tii['qty'] = $result->fields['units'];
141 // if (!$tii['qty'] > 0) {
142 // $tii['qty'] = 1;
143 // }
145 // We always store units as 1 in order to avoid awkward round-off errors
146 // that might happen when dividing the fee by units.
147 $tii['qty'] = 1;
149 $tii['price'] = sprintf("%01.2f",$result->fields['fee']);
150 $total += $tii['price'];
151 $tii['glaccountid'] = $this->_config['income_acct'];
152 $invoice_info['items'][] = $tii;
154 $result->MoveNext();
155 $counter++;
158 // I think maybe this info is not used.
160 for($counter = 0; $counter < 2; $counter++)
162 $fee = 0;
163 $billto = 0;
164 if($counter == 0)
166 $fee = $patient_info['payment_amount'];
167 $billto = $this->foreign_patient_id;
169 else
171 $fee = $payer_info['payment_amount'];
172 $billto = $this->foreign_payer_id;
174 $invoice_info["invoiceid$counter"] = $this->patient_id . "000" . $this->encounter;
175 $invoice_info["amount$counter"] = $fee;
176 $invoice_info["invoicenumber$counter"] = $this->patient_id . "000" . $this->encounter;
177 $invoice_info["interest$counter"] = 0;
178 $invoice_info["billtoid$counter"] = $billto;
181 $invoice_info['subtotal'] = sprintf("%01.2f",$total);
182 $invoice_info['total'] = sprintf("%01.2f",$total);
184 $this->claim = $invoice_info;
185 return true;
188 function load_provider_foreign_id() {
189 $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'";
190 $result = $this->_db->Execute($sql);
191 if($result && !$result->EOF) {
192 $this->foreign_provider_id = $result->fields['foreign_id'];
193 return true;
195 else {
196 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>";
197 return false;
201 function load_patient_foreign_id() {
202 $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'";
203 $result = $this->_db->Execute($sql);
204 if($result && !$result->EOF) {
205 $this->foreign_patient_id = $result->fields['foreign_id'];
206 return true;
208 else {
209 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>";
210 return false;
214 function load_payer_foreign_id() {
215 $sql = "SELECT payer_id from billing where encounter = '" . $this->encounter . "' and pid = '" . $this->patient_id . "'";
216 $result = $this->_db->Execute($sql);
217 if($result && !$result->EOF) {
218 $this->payer_id = $result->fields['payer_id'];
220 else {
221 echo "No payer id for this claim could be found";
222 return false;
224 // See comments in globals.php:
225 if ($GLOBALS['insurance_companies_are_not_customers']) {
226 $this->foreign_payer_id = $this->payer_id;
228 else {
229 $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'";
230 $result = $this->_db->Execute($sql);
231 if($result && !$result->EOF) {
232 $this->foreign_payer_id = $result->fields['foreign_id'];
234 else {
235 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>";
236 return false;
239 return true;
243 //$wsc = new WSClaim("3","20040622");