fixed bug in setting pid
[openemr.git] / library / classes / WSClaim.class.php
blob0c6f628675236d2d4ce9e851b3f692ea84e887c2
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;
55 $invoice_info['invoicenumber'] = $this->patient_id . "." . $this->encounter;
57 $counter = 0;
58 $total = 0;
59 $patient_info = array();
60 $payer_info = array();
61 while ($result && !$result->EOF) {
62 if ($result->fields['process_date'] == null) {
63 //don't sync a claim that has not been processed, they may just want to mark this as billed
64 return false;
66 // create a paymentline item for each code
68 $invoice_info["invoiceid$counter"] = $result->fields['code'];
69 $invoice_info["amount$counter"] = $result->fields['fee'];
70 $invoice_info["invoicenumber$counter"] = $this->patient_id . "000" . $this->encounter;
71 $invoice_info["interest$counter"] = 0;
73 if ($counter == 0) {
74 //unused but supported by ezybiz, helpful in debugging
75 $invoice_info['customer'] = $result->fields['patient_name'];
76 $invoice_info['invoicedate'] = date("m-d-Y",strtotime($result->fields['process_date']));
77 $invoice_info['duedate'] = date("m-d-Y",strtotime($result->fields['process_date']));
78 $invoice_info['items'] = array();
81 $tii = array();
82 //This is how we set the line items for the invoice, using codes from our encounter
83 //if ($result->fields['code_type'] == "CPT4" || $result->fields['code_type'] == "HCPCS") {
84 //if( $result->fields['code_type'] != "ICD9" ) {
85 if( $result->fields['code_type'] == "COPAY")
87 $patient_info['payment_amount'] += sprintf("%01.2f",$result->fields['fee']);
89 else
91 $payer_info['payment_amount'] += sprintf("%01.2f",$result->fields['fee']);
93 $tii['maincode'] = $result->fields['code'];
94 $tii['itemtext'] = $result->fields['code_type'] .":" . $result->fields['code'] . " " . $result->fields['code_text'] . " "
95 . $result->fields['justify'];
97 $tii['qty'] = $result->fields['units'];
98 if (!$tii['qty'] > 0) {
99 $tii['qty'] = 1;
101 $tii['price'] = sprintf("%01.2f",$result->fields['fee']);
102 $total += $tii['price'];
103 $tii['glaccountid'] = $this->_config['income_acct'];
104 $invoice_info['items'][] = $tii;
107 $result->MoveNext();
108 $counter++;
112 for($counter = 0; $counter < 2; $counter++)
114 $fee = 0;
115 $billto = 0;
116 if($counter == 0)
118 $fee = $patient_info['payment_amount'];
119 $billto = $this->foreign_patient_id;
121 else
123 $fee = $payer_info['payment_amount'];
124 $billto = $this->foreign_payer_id;
126 $invoice_info["invoiceid$counter"] = $this->patient_id . "000" . $this->encounter;
127 $invoice_info["amount$counter"] = $fee;
128 $invoice_info["invoicenumber$counter"] = $this->patient_id . "000" . $this->encounter;
129 $invoice_info["interest$counter"] = 0;
130 $invoice_info["billtoid$counter"] = $billto;
132 $invoice_info['subtotal'] = sprintf("%01.2f",$total);
133 $invoice_info['total'] = sprintf("%01.2f",$total);
135 $this->claim = $invoice_info;
136 return true;
139 function load_provider_foreign_id() {
140 $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'";
141 $result = $this->_db->Execute($sql);
142 if($result && !$result->EOF) {
143 $this->foreign_provider_id = $result->fields['foreign_id'];
144 return true;
146 else {
147 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>";
148 return false;
151 function load_patient_foreign_id() {
152 $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'";
153 $result = $this->_db->Execute($sql);
154 if($result && !$result->EOF) {
155 $this->foreign_patient_id = $result->fields['foreign_id'];
156 return true;
158 else {
159 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>";
160 return false;
163 function load_payer_foreign_id() {
164 $sql = "SELECT payer_id from billing where encounter = '" . $this->encounter . "' and pid = '" . $this->patient_id . "'";
165 $result = $this->_db->Execute($sql);
166 if($result && !$result->EOF) {
167 $this->payer_id = $result->fields['payer_id'];
169 else {
170 echo "No payer id for this claim could be found";
171 return false;
173 // See comments in globals.php:
174 if ($GLOBALS['insurance_companies_are_not_customers']) {
175 $this->foreign_payer_id = $this->payer_id;
177 else {
178 $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'";
179 $result = $this->_db->Execute($sql);
180 if($result && !$result->EOF) {
181 $this->foreign_payer_id = $result->fields['foreign_id'];
183 else {
184 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>";
185 return false;
188 return true;
192 //$wsc = new WSClaim("3","20040622");