quick minor path updates (#1968)
[openemr.git] / custom / BillingExport.csv.php
blobfa28389a5c4cff2805c4903b5b84830ce79765d1
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.
18 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)
32 return addslashes(trim($string));
35 function fixMI($string)
37 return addslashes(substr(trim($string), 0, 1));
40 function fixSex($sex)
42 $sex = substr(strtoupper(trim($sex)), 0, 1);
43 if ($sex == 'M') {
44 return 'Male';
47 if ($sex == 'F') {
48 return 'Female';
51 return '';
54 function fixPhone($phone)
56 $tmparr = array();
57 if (preg_match("/(\d\d\d)\D*(\d\d\d)\D*(\d\d\d\d)/", $phone, $tmparr)) {
58 return $tmparr[1] . '-' . $tmparr[2] . '-' . $tmparr[3];
61 return '';
64 function fixSSN($ssn)
66 $tmparr = array();
67 if (preg_match("/(\d\d\d)\D*(\d\d)\D*(\d\d\d\d)/", $ssn, $tmparr)) {
68 return $tmparr[1] . '-' . $tmparr[2] . '-' . $tmparr[3];
71 return '';
74 function fixMStatus($status)
76 return ucfirst(trim($status));
79 function fixEStatus($employer)
81 $status = strtoupper(trim($employer));
82 if (! $status) {
83 return '';
86 if ($status == 'STUDENT') {
87 return 'Student';
90 if ($status == 'RETIRED') {
91 return 'Retired';
94 return 'Full-time';
97 function fixRelation($rel)
99 return ucfirst(trim($rel));
102 function fixCPT($code, $mod)
104 $code = trim($code);
105 $mod = trim($mod);
106 if ($mod) {
107 $code .= '-' . $mod;
110 return addslashes($code);
113 function fixJust($str)
115 return addslashes(trim(str_replace(':', ' ', $str)));
118 function fixDate($date)
120 return substr($date, 0, 10);
123 // Creating a BillingExport object opens the output file.
124 // Filename format is "transYYYYMMDDHHMMSS.txt".
126 function __construct()
128 $this->tmpname = $this->TMP_DIR . '/trans' . date("YmdHis") . '.txt';
129 $this->tmpfh = fopen($this->tmpname, 'w');
132 // Call this once for each claim to be processed.
134 function addClaim($patient_id, $encounter)
137 // Patient information:
139 $query = "SELECT p.pubpid, p.ss, p.lname, p.fname, p.mname, p.DOB, " .
140 "p.street, p.city, p.state, p.postal_code, p.phone_home, p.phone_biz, " .
141 "p.status, p.sex, e.name " .
142 "FROM patient_data AS p " .
143 "LEFT OUTER JOIN employer_data AS e ON e.pid = ? " .
144 "WHERE p.pid = ? " .
145 "LIMIT 1";
146 $prow = sqlQuery($query, array($patient_id, $patient_id));
148 // Patient line.
149 fwrite($this->tmpfh, 'PT' .
150 ',"' . $this->fixString($prow['pubpid']) . '"' .
151 ',"' . $this->fixString($prow['lname']) . '"' .
152 ',"' . $this->fixString($prow['fname']) . '"' .
153 ',"' . $this->fixMI($prow['mname']) . '"' .
154 ',"' . $this->fixString($prow['street']) . '"' .
155 ',""' .
156 ',"' . $this->fixString($prow['city']) . '"' .
157 ',"' . $this->fixString($prow['state']) . '"' .
158 ',"' . $this->fixString($prow['postal_code']) . '"' .
159 ',"' . $this->fixPhone($prow['phone_home']) . '"' .
160 ',"' . $this->fixPhone($prow['phone_biz']) . '"' .
161 ',"' . $this->fixSex($prow['sex']) . '"' .
162 ',"' . $prow['DOB'] . '"' .
163 ',"' . $this->fixSSN($prow['ss']) . '"' .
164 ',"' . $this->fixEStatus($prow['name']) . '"' .
165 ',"' . $this->fixString($prow['name']) . '"' .
166 "\n");
168 // Encounter information:
170 $query = "SELECT e.date, e.facility, " .
171 "u.id, u.lname, u.fname, u.mname, u.upin, " .
172 "f.street, f.city, f.state, f.postal_code, f.pos_code, " .
173 "f.domain_identifier AS clia_code " .
174 "FROM form_encounter AS e " .
175 "LEFT OUTER JOIN forms ON forms.formdir = 'newpatient' AND " .
176 "forms.form_id = e.id AND forms.pid = ? " .
177 "LEFT OUTER JOIN users AS u ON u.username = forms.user " .
178 "LEFT OUTER JOIN facility AS f ON f.name = e.facility " .
179 "WHERE e.pid = ? AND e.encounter = ? " .
180 "LIMIT 1";
181 $erow = sqlQuery($query, array($patient_id, $patient_id, $encounter));
183 // Performing Provider line.
184 fwrite($this->tmpfh, 'PP' .
185 ',"' . $this->fixString($erow['lname']) . '"' .
186 ',"' . $this->fixString($erow['fname']) . '"' .
187 ',"' . $this->fixMI($erow['mname']) . '"' .
188 ',"' . $this->fixString($erow['upin']) . '"' .
189 "\n");
191 // TBD: Referring Provider line when we have such a thing.
193 // Insurance information, up to 3 lines:
195 $query = "SELECT " .
196 "d.type, d.policy_number, d.group_number, " .
197 "d.subscriber_lname, d.subscriber_fname, d.subscriber_mname, " .
198 "d.subscriber_street, d.subscriber_city, d.subscriber_state, " .
199 "d.subscriber_postal_code, d.subscriber_DOB, d.subscriber_sex, " .
200 "d.subscriber_relationship, " .
201 "c.name, " .
202 "a.line1, a.line2, a.city, a.state, a.zip, " .
203 "p.area_code, p.prefix, p.number, " .
204 "n.provider_number " .
205 "FROM insurance_data AS d " .
206 "LEFT OUTER JOIN insurance_companies AS c ON c.id = d.provider " .
207 "LEFT OUTER JOIN addresses AS a ON a.foreign_id = c.id " .
208 "LEFT OUTER JOIN phone_numbers AS p ON p.foreign_id = c.id AND p.type = 2 " .
209 "LEFT OUTER JOIN insurance_numbers AS n ON n.provider_id = ? " .
210 "AND n.insurance_company_id = c.id " .
211 "WHERE d.pid = ? AND d.provider != '' " .
212 "ORDER BY d.type ASC, d.date DESC";
213 $ires = sqlStatement($query, array($erow['id'], $patient_id));
215 $prev_type = '?';
216 while ($irow = sqlFetchArray($ires)) {
217 if (strcmp($irow['type'], $prev_type) == 0) {
218 continue;
221 $prev_type = $irow['type'];
223 fwrite($this->tmpfh, 'IN' .
224 ',"' . $this->fixString($irow['subscriber_lname']) . '"' .
225 ',"' . $this->fixString($irow['subscriber_fname']) . '"' .
226 ',"' . $this->fixMI($irow['subscriber_mname']) . '"' .
227 ',"' . $this->fixString($irow['subscriber_street']) . '"' .
228 ',"' . $this->fixString($irow['subscriber_city']) . '"' .
229 ',"' . $this->fixString($irow['subscriber_state']) . '"' .
230 ',"' . $this->fixString($irow['subscriber_postal_code']) . '"' .
231 ',"' . $irow['subscriber_DOB'] . '"' .
232 ',"' . $this->fixRelation($irow['subscriber_relationship']) . '"' .
233 ',"' . $this->fixString($irow['policy_number']) . '"' .
234 ',"' . $this->fixString($irow['group_number']) . '"' .
235 ',"' . $this->fixString($irow['name']) . '"' .
236 ',"' . $this->fixString($irow['line1']) . '"' .
237 ',"' . $this->fixString($irow['line2']) . '"' .
238 ',"' . $this->fixString($irow['city']) . '"' .
239 ',"' . $this->fixString($irow['state']) . '"' .
240 ',"' . $this->fixString($irow['zip']) . '"' .
241 ',"' . $this->fixPhone($irow['area_code'] . $irow['prefix'] . $irow['number']) . '"' .
242 ',"' . $this->fixString($irow['provider_number']) . '"' .
243 ',"' . $this->fixString($irow['provider_number']) . '"' . // TBD: referring provider
244 "\n");
247 // Procedure information:
249 $query = "SELECT id, code, modifier, justify " .
250 "FROM billing " .
251 "WHERE pid = ? AND encounter = ? " .
252 "AND activity = 1 AND code_type = 'CPT4' " .
253 "ORDER BY id";
254 $bres = sqlStatement($query, array($patient_id, $encounter));
256 while ($brow = sqlFetchArray($bres)) {
257 fwrite($this->tmpfh, 'PR' .
258 ',"' . $this->fixCPT($brow['code'], $brow['modifier']) . '"' .
259 ',"' . $this->fixJust($brow['justify']) . '"' .
260 ',"' . $this->fixDate($erow['date']) . '"' .
261 ',"' . $this->fixString($erow['pos_code']) . '"' .
262 ',"' . $this->fixString($erow['clia_code']) . '"' .
263 ',"' . $this->fixString($erow['facility']) . '"' .
264 ',"' . $this->fixString($erow['street']) . '"' .
265 ',""' .
266 ',"' . $this->fixString($erow['city']) . '"' .
267 ',"' . $this->fixString($erow['state']) . '"' .
268 ',"' . $this->fixString($erow['postal_code']) . '"' .
269 "\n");
273 // Close the output file and move it to the ftp download area.
275 function close()
277 fclose($this->tmpfh);
278 chmod($this->tmpname, 0666);
279 rename($this->tmpname, $this->TARGET_DIR . '/' . basename($this->tmpname));