added 1st initial to provider name in calendar Day and Week views
[openemr.git] / library / classes / WSClaim.class.php
blobea93e957b5c411f2e79d8ea84a33a8396d9b37ad
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 $sql = "SELECT b.*, CONCAT(pd.fname,' ',pd.mname,' ',pd.lname) as patient_name FROM billing as b LEFT JOIN patient_data as pd on b.pid=pd.pid where "
47 ."b.encounter ='" . $this->encounter ."' AND b.pid = '" . $this->patient_id ."' AND b.billed = 1 AND b.activity != '0' AND authorized='1'";
49 $result = $this->_db->Execute($sql);
51 $invoice_info['salesman'] = $this->foreign_provider_id;
52 $invoice_info['customerid'] = $this->foreign_patient_id;
53 $invoice_info['payer_id'] = $this->foreign_payer_id;
54 $invoice_info['invoicenumber'] = $this->patient_id . "000" . $this->encounter;
56 $counter = 0;
57 $total = 0;
58 $patient_info = array();
59 $payer_info = array();
60 while ($result && !$result->EOF) {
61 if ($result->fields['process_date'] == null) {
62 //don't sync a claim that has not been processed, they may just want to mark this as billed
63 return false;
65 // create a paymentline item for each code
67 $invoice_info["invoiceid$counter"] = $result->fields['code'];
68 $invoice_info["amount$counter"] = $result->fields['fee'];
69 $invoice_info["invoicenumber$counter"] = $this->patient_id . "000" . $this->encounter;
70 $invoice_info["interest$counter"] = 0;
72 if ($counter == 0) {
73 //unused but supported by ezybiz, helpful in debugging
74 $invoice_info['customer'] = $result->fields['patient_name'];
75 $invoice_info['invoicedate'] = date("m-d-Y",strtotime($result->fields['process_date']));
76 $invoice_info['duedate'] = date("m-d-Y",strtotime($result->fields['process_date']));
77 $invoice_info['items'] = array();
80 $tii = array();
81 //This is how we set the line items for the invoice, using codes from our encounter
82 //if ($result->fields['code_type'] == "CPT4" || $result->fields['code_type'] == "HCPCS") {
83 //if( $result->fields['code_type'] != "ICD9" ) {
84 if( $result->fields['code_type'] == "COPAY")
86 $patient_info['payment_amount'] += sprintf("%01.2f",$result->fields['fee']);
88 else
90 $payer_info['payment_amount'] += sprintf("%01.2f",$result->fields['fee']);
92 $tii['maincode'] = $result->fields['code'];
93 $tii['itemtext'] = $result->fields['code_type'] .":" . $result->fields['code'] . " " . $result->fields['code_text'] . " "
94 . $result->fields['justify'];
96 $tii['qty'] = $result->fields['units'];
97 if (!$tii['qty'] > 0) {
98 $tii['qty'] = 1;
100 $tii['price'] = sprintf("%01.2f",$result->fields['fee']);
101 $total += $tii['price'];
102 $tii['glaccountid'] = $this->_config['income_acct'];
103 $invoice_info['items'][] = $tii;
106 $result->MoveNext();
107 $counter++;
111 for($counter = 0; $counter < 2; $counter++)
113 $fee = 0;
114 $billto = 0;
115 if($counter == 0)
117 $fee = $patient_info['payment_amount'];
118 $billto = $this->foreign_patient_id;
120 else
122 $fee = $payer_info['payment_amount'];
123 $billto = $this->foreign_payer_id;
125 $invoice_info["invoiceid$counter"] = $this->patient_id . "000" . $this->encounter;
126 $invoice_info["amount$counter"] = $fee;
127 $invoice_info["invoicenumber$counter"] = $this->patient_id . "000" . $this->encounter;
128 $invoice_info["interest$counter"] = 0;
129 $invoice_info["billtoid$counter"] = $billto;
131 $invoice_info['subtotal'] = sprintf("%01.2f",$total);
132 $invoice_info['total'] = sprintf("%01.2f",$total);
134 $this->claim = $invoice_info;
135 return true;
138 function load_provider_foreign_id() {
139 $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'";
140 $result = $this->_db->Execute($sql);
141 if($result && !$result->EOF) {
142 $this->foreign_provider_id = $result->fields['foreign_id'];
143 return true;
145 else {
146 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>";
147 return false;
150 function load_patient_foreign_id() {
151 $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'";
152 $result = $this->_db->Execute($sql);
153 if($result && !$result->EOF) {
154 $this->foreign_patient_id = $result->fields['foreign_id'];
155 return true;
157 else {
158 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>";
159 return false;
162 function load_payer_foreign_id() {
163 $sql = "SELECT payer_id from billing where encounter = '" . $this->encounter . "' and pid = '" . $this->patient_id . "'";
164 $result = $this->_db->Execute($sql);
165 if($result && !$result->EOF) {
166 $this->payer_id = $result->fields['payer_id'];
168 else {
169 echo "No payer id for this claim could be found";
170 return false;
173 $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'";
174 $result = $this->_db->Execute($sql);
175 if($result && !$result->EOF) {
176 $this->foreign_payer_id = $result->fields['foreign_id'];
177 return true;
179 else {
180 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>";
181 return false;
186 //$wsc = new WSClaim("3","20040622");