migrated library/js/jquery.js to standard asset
[openemr.git] / custom / BillingExport.csv.php
blobae695489a8527f0ef004a344b5052bec9b96a7b9
1 <?php
2 // Copyright (C) 2005 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This class exports billing information to an external billing
10 // system. In this case we are writing a custom CSV format, but
11 // it would be easy and more generally useful to write X12 (837p)
12 // format and then have some separate utilities for converting to
13 // HCFA 1500, UB-92, etc.
15 // To implement this feature, rename this file to BillingExport.php.
17 require_once (dirname(__FILE__) . "/../library/sql.inc");
19 class BillingExport {
21 // You should customize these paths. They must share the same
22 // physical disk partition so that the final rename will be an
23 // atomic operation.
24 var $TMP_DIR = "/home/billing/tmp";
25 var $TARGET_DIR = "/home/billing/ftp";
27 var $tmpname; // output filename including path
28 var $tmpfh; // output file handle
30 function fixString($string) {
31 return addslashes(trim($string));
34 function fixMI($string) {
35 return addslashes(substr(trim($string), 0, 1));
38 function fixSex($sex) {
39 $sex = substr(strtoupper(trim($sex)), 0, 1);
40 if ($sex == 'M') return 'Male';
41 if ($sex == 'F') return 'Female';
42 return '';
45 function fixPhone($phone) {
46 $tmparr = array();
47 if (preg_match("/(\d\d\d)\D*(\d\d\d)\D*(\d\d\d\d)/", $phone, $tmparr))
48 return $tmparr[1] . '-' . $tmparr[2] . '-' . $tmparr[3];
49 return '';
52 function fixSSN($ssn) {
53 $tmparr = array();
54 if (preg_match("/(\d\d\d)\D*(\d\d)\D*(\d\d\d\d)/", $ssn, $tmparr))
55 return $tmparr[1] . '-' . $tmparr[2] . '-' . $tmparr[3];
56 return '';
59 function fixMStatus($status) {
60 return ucfirst(trim($status));
63 function fixEStatus($employer) {
64 $status = strtoupper(trim($employer));
65 if (! $status) return '';
66 if ($status == 'STUDENT') return 'Student';
67 if ($status == 'RETIRED') return 'Retired';
68 return 'Full-time';
71 function fixRelation($rel) {
72 return ucfirst(trim($rel));
75 function fixCPT($code, $mod) {
76 $code = trim($code);
77 $mod = trim($mod);
78 if ($mod) $code .= '-' . $mod;
79 return addslashes($code);
82 function fixJust($str) {
83 return addslashes(trim(str_replace(':', ' ', $str)));
86 function fixDate($date) {
87 return substr($date, 0, 10);
90 // Creating a BillingExport object opens the output file.
91 // Filename format is "transYYYYMMDDHHMMSS.txt".
93 function BillingExport() {
94 $this->tmpname = $this->TMP_DIR . '/trans' . date("YmdHis") . '.txt';
95 $this->tmpfh = fopen($this->tmpname, 'w');
98 // Call this once for each claim to be processed.
100 function addClaim($patient_id, $encounter) {
102 // Patient information:
104 $query = "SELECT p.pubpid, p.ss, p.lname, p.fname, p.mname, p.DOB, " .
105 "p.street, p.city, p.state, p.postal_code, p.phone_home, p.phone_biz, " .
106 "p.status, p.sex, e.name " .
107 "FROM patient_data AS p " .
108 "LEFT OUTER JOIN employer_data AS e ON e.pid = '$patient_id' " .
109 "WHERE p.pid = '$patient_id' " .
110 "LIMIT 1";
111 $prow = sqlQuery($query);
113 // Patient line.
114 fwrite($this->tmpfh, 'PT' .
115 ',"' . $this->fixString($prow['pubpid']) . '"' .
116 ',"' . $this->fixString($prow['lname']) . '"' .
117 ',"' . $this->fixString($prow['fname']) . '"' .
118 ',"' . $this->fixMI($prow['mname']) . '"' .
119 ',"' . $this->fixString($prow['street']) . '"' .
120 ',""' .
121 ',"' . $this->fixString($prow['city']) . '"' .
122 ',"' . $this->fixString($prow['state']) . '"' .
123 ',"' . $this->fixString($prow['postal_code']) . '"' .
124 ',"' . $this->fixPhone($prow['phone_home']) . '"' .
125 ',"' . $this->fixPhone($prow['phone_biz']) . '"' .
126 ',"' . $this->fixSex($prow['sex']) . '"' .
127 ',"' . $prow['DOB'] . '"' .
128 ',"' . $this->fixSSN($prow['ss']) . '"' .
129 ',"' . $this->fixEStatus($prow['name']) . '"' .
130 ',"' . $this->fixString($prow['name']) . '"' .
131 "\n");
133 // Encounter information:
135 $query = "SELECT e.date, e.facility, " .
136 "u.id, u.lname, u.fname, u.mname, u.upin, " .
137 "f.street, f.city, f.state, f.postal_code, f.pos_code, " .
138 "f.domain_identifier AS clia_code " .
139 "FROM form_encounter AS e " .
140 "LEFT OUTER JOIN forms ON forms.formdir = 'newpatient' AND " .
141 "forms.form_id = e.id AND forms.pid = '$patient_id' " .
142 "LEFT OUTER JOIN users AS u ON u.username = forms.user " .
143 "LEFT OUTER JOIN facility AS f ON f.name = e.facility " .
144 "WHERE e.pid = '$patient_id' AND e.encounter = '$encounter' " .
145 "LIMIT 1";
146 $erow = sqlQuery($query);
148 // Performing Provider line.
149 fwrite($this->tmpfh, 'PP' .
150 ',"' . $this->fixString($erow['lname']) . '"' .
151 ',"' . $this->fixString($erow['fname']) . '"' .
152 ',"' . $this->fixMI($erow['mname']) . '"' .
153 ',"' . $this->fixString($erow['upin']) . '"' .
154 "\n");
156 // TBD: Referring Provider line when we have such a thing.
158 // Insurance information, up to 3 lines:
160 $query = "SELECT " .
161 "d.type, d.policy_number, d.group_number, " .
162 "d.subscriber_lname, d.subscriber_fname, d.subscriber_mname, " .
163 "d.subscriber_street, d.subscriber_city, d.subscriber_state, " .
164 "d.subscriber_postal_code, d.subscriber_DOB, d.subscriber_sex, " .
165 "d.subscriber_relationship, " .
166 "c.name, " .
167 "a.line1, a.line2, a.city, a.state, a.zip, " .
168 "p.area_code, p.prefix, p.number, " .
169 "n.provider_number " .
170 "FROM insurance_data AS d " .
171 "LEFT OUTER JOIN insurance_companies AS c ON c.id = d.provider " .
172 "LEFT OUTER JOIN addresses AS a ON a.foreign_id = c.id " .
173 "LEFT OUTER JOIN phone_numbers AS p ON p.foreign_id = c.id AND p.type = 2 " .
174 "LEFT OUTER JOIN insurance_numbers AS n ON n.provider_id = " .
175 $erow['id'] . " AND n.insurance_company_id = c.id " .
176 "WHERE d.pid = '$patient_id' AND d.provider != '' " .
177 "ORDER BY d.type ASC, d.date DESC";
178 $ires = sqlStatement($query);
180 $prev_type = '?';
181 while ($irow = sqlFetchArray($ires)) {
182 if (strcmp($irow['type'], $prev_type) == 0) continue;
183 $prev_type = $irow['type'];
185 fwrite($this->tmpfh, 'IN' .
186 ',"' . $this->fixString($irow['subscriber_lname']) . '"' .
187 ',"' . $this->fixString($irow['subscriber_fname']) . '"' .
188 ',"' . $this->fixMI($irow['subscriber_mname']) . '"' .
189 ',"' . $this->fixString($irow['subscriber_street']) . '"' .
190 ',"' . $this->fixString($irow['subscriber_city']) . '"' .
191 ',"' . $this->fixString($irow['subscriber_state']) . '"' .
192 ',"' . $this->fixString($irow['subscriber_postal_code']) . '"' .
193 ',"' . $irow['subscriber_DOB'] . '"' .
194 ',"' . $this->fixRelation($irow['subscriber_relationship']) . '"' .
195 ',"' . $this->fixString($irow['policy_number']) . '"' .
196 ',"' . $this->fixString($irow['group_number']) . '"' .
197 ',"' . $this->fixString($irow['name']) . '"' .
198 ',"' . $this->fixString($irow['line1']) . '"' .
199 ',"' . $this->fixString($irow['line2']) . '"' .
200 ',"' . $this->fixString($irow['city']) . '"' .
201 ',"' . $this->fixString($irow['state']) . '"' .
202 ',"' . $this->fixString($irow['zip']) . '"' .
203 ',"' . $this->fixPhone($irow['area_code'] . $irow['prefix'] . $irow['number']) . '"' .
204 ',"' . $this->fixString($irow['provider_number']) . '"' .
205 ',"' . $this->fixString($irow['provider_number']) . '"' . // TBD: referring provider
206 "\n");
209 // Procedure information:
211 $query = "SELECT id, code, modifier, justify " .
212 "FROM billing " .
213 "WHERE pid = '$patient_id' AND encounter = '$encounter' " .
214 "AND activity = 1 AND code_type = 'CPT4' " .
215 "ORDER BY id";
216 $bres = sqlStatement($query);
218 while ($brow = sqlFetchArray($bres)) {
219 fwrite($this->tmpfh, 'PR' .
220 ',"' . $this->fixCPT($brow['code'], $brow['modifier']) . '"' .
221 ',"' . $this->fixJust($brow['justify']) . '"' .
222 ',"' . $this->fixDate($erow['date']) . '"' .
223 ',"' . $this->fixString($erow['pos_code']) . '"' .
224 ',"' . $this->fixString($erow['clia_code']) . '"' .
225 ',"' . $this->fixString($erow['facility']) . '"' .
226 ',"' . $this->fixString($erow['street']) . '"' .
227 ',""' .
228 ',"' . $this->fixString($erow['city']) . '"' .
229 ',"' . $this->fixString($erow['state']) . '"' .
230 ',"' . $this->fixString($erow['postal_code']) . '"' .
231 "\n");
235 // Close the output file and move it to the ftp download area.
237 function close() {
238 fclose($this->tmpfh);
239 chmod($this->tmpname, 0666);
240 rename($this->tmpname, $this->TARGET_DIR . '/' . basename($this->tmpname));