Followup commit to complete the renaming and replacement of pos_checkout.php.
[openemr.git] / custom / BillingExport.csv.php
blob70388eff7149e9cff3723b7553dea71fb4c82ec0
1 <?php
3 // Copyright (C) 2005 Rod Roark <rod@sunsetsystems.com>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This class exports billing information to an external billing
11 // system. In this case we are writing a custom CSV format, but
12 // it would be easy and more generally useful to write X12 (837p)
13 // format and then have some separate utilities for converting to
14 // HCFA 1500, UB-92, etc.
16 // To implement this feature, rename this file to BillingExport.php.
19 class BillingExport
22 // You should customize these paths. They must share the same
23 // physical disk partition so that the final rename will be an
24 // atomic operation.
25 var $TMP_DIR = "/home/billing/tmp";
26 var $TARGET_DIR = "/home/billing/ftp";
28 var $tmpname; // output filename including path
29 var $tmpfh; // output file handle
31 function fixString($string)
33 return addslashes(trim($string));
36 function fixMI($string)
38 return addslashes(substr(trim($string), 0, 1));
41 function fixSex($sex)
43 $sex = substr(strtoupper(trim($sex)), 0, 1);
44 if ($sex == 'M') {
45 return 'Male';
48 if ($sex == 'F') {
49 return 'Female';
52 return '';
55 function fixPhone($phone)
57 $tmparr = array();
58 if (preg_match("/(\d\d\d)\D*(\d\d\d)\D*(\d\d\d\d)/", $phone, $tmparr)) {
59 return $tmparr[1] . '-' . $tmparr[2] . '-' . $tmparr[3];
62 return '';
65 function fixSSN($ssn)
67 $tmparr = array();
68 if (preg_match("/(\d\d\d)\D*(\d\d)\D*(\d\d\d\d)/", $ssn, $tmparr)) {
69 return $tmparr[1] . '-' . $tmparr[2] . '-' . $tmparr[3];
72 return '';
75 function fixMStatus($status)
77 return ucfirst(trim($status));
80 function fixEStatus($employer)
82 $status = strtoupper(trim($employer));
83 if (! $status) {
84 return '';
87 if ($status == 'STUDENT') {
88 return 'Student';
91 if ($status == 'RETIRED') {
92 return 'Retired';
95 return 'Full-time';
98 function fixRelation($rel)
100 return ucfirst(trim($rel));
103 function fixCPT($code, $mod)
105 $code = trim($code);
106 $mod = trim($mod);
107 if ($mod) {
108 $code .= '-' . $mod;
111 return addslashes($code);
114 function fixJust($str)
116 return addslashes(trim(str_replace(':', ' ', $str)));
119 function fixDate($date)
121 return substr($date, 0, 10);
124 // Creating a BillingExport object opens the output file.
125 // Filename format is "transYYYYMMDDHHMMSS.txt".
127 function __construct()
129 $this->tmpname = $this->TMP_DIR . '/trans' . date("YmdHis") . '.txt';
130 $this->tmpfh = fopen($this->tmpname, 'w');
133 // Call this once for each claim to be processed.
135 function addClaim($patient_id, $encounter)
138 // Patient information:
140 $query = "SELECT p.pubpid, p.ss, p.lname, p.fname, p.mname, p.DOB, " .
141 "p.street, p.city, p.state, p.postal_code, p.phone_home, p.phone_biz, " .
142 "p.status, p.sex, e.name " .
143 "FROM patient_data AS p " .
144 "LEFT OUTER JOIN employer_data AS e ON e.pid = ? " .
145 "WHERE p.pid = ? " .
146 "LIMIT 1";
147 $prow = sqlQuery($query, array($patient_id, $patient_id));
149 // Patient line.
150 fwrite($this->tmpfh, 'PT' .
151 ',"' . $this->fixString($prow['pubpid']) . '"' .
152 ',"' . $this->fixString($prow['lname']) . '"' .
153 ',"' . $this->fixString($prow['fname']) . '"' .
154 ',"' . $this->fixMI($prow['mname']) . '"' .
155 ',"' . $this->fixString($prow['street']) . '"' .
156 ',""' .
157 ',"' . $this->fixString($prow['city']) . '"' .
158 ',"' . $this->fixString($prow['state']) . '"' .
159 ',"' . $this->fixString($prow['postal_code']) . '"' .
160 ',"' . $this->fixPhone($prow['phone_home']) . '"' .
161 ',"' . $this->fixPhone($prow['phone_biz']) . '"' .
162 ',"' . $this->fixSex($prow['sex']) . '"' .
163 ',"' . $prow['DOB'] . '"' .
164 ',"' . $this->fixSSN($prow['ss']) . '"' .
165 ',"' . $this->fixEStatus($prow['name']) . '"' .
166 ',"' . $this->fixString($prow['name']) . '"' .
167 "\n");
169 // Encounter information:
171 $query = "SELECT e.date, e.facility, " .
172 "u.id, u.lname, u.fname, u.mname, u.upin, " .
173 "f.street, f.city, f.state, f.postal_code, f.pos_code, " .
174 "f.domain_identifier AS clia_code " .
175 "FROM form_encounter AS e " .
176 "LEFT OUTER JOIN forms ON forms.formdir = 'newpatient' AND " .
177 "forms.form_id = e.id AND forms.pid = ? " .
178 "LEFT OUTER JOIN users AS u ON u.username = forms.user " .
179 "LEFT OUTER JOIN facility AS f ON f.name = e.facility " .
180 "WHERE e.pid = ? AND e.encounter = ? " .
181 "LIMIT 1";
182 $erow = sqlQuery($query, array($patient_id, $patient_id, $encounter));
184 // Performing Provider line.
185 fwrite($this->tmpfh, 'PP' .
186 ',"' . $this->fixString($erow['lname']) . '"' .
187 ',"' . $this->fixString($erow['fname']) . '"' .
188 ',"' . $this->fixMI($erow['mname']) . '"' .
189 ',"' . $this->fixString($erow['upin']) . '"' .
190 "\n");
192 // TBD: Referring Provider line when we have such a thing.
194 // Insurance information, up to 3 lines:
196 $query = "SELECT " .
197 "d.type, d.policy_number, d.group_number, " .
198 "d.subscriber_lname, d.subscriber_fname, d.subscriber_mname, " .
199 "d.subscriber_street, d.subscriber_city, d.subscriber_state, " .
200 "d.subscriber_postal_code, d.subscriber_DOB, d.subscriber_sex, " .
201 "d.subscriber_relationship, " .
202 "c.name, " .
203 "a.line1, a.line2, a.city, a.state, a.zip, " .
204 "p.area_code, p.prefix, p.number, " .
205 "n.provider_number " .
206 "FROM insurance_data AS d " .
207 "LEFT OUTER JOIN insurance_companies AS c ON c.id = d.provider " .
208 "LEFT OUTER JOIN addresses AS a ON a.foreign_id = c.id " .
209 "LEFT OUTER JOIN phone_numbers AS p ON p.foreign_id = c.id AND p.type = 2 " .
210 "LEFT OUTER JOIN insurance_numbers AS n ON n.provider_id = ? " .
211 "AND n.insurance_company_id = c.id " .
212 "WHERE d.pid = ? AND d.provider != '' " .
213 "ORDER BY d.type ASC, d.date DESC";
214 $ires = sqlStatement($query, array($erow['id'], $patient_id));
216 $prev_type = '?';
217 while ($irow = sqlFetchArray($ires)) {
218 if (strcmp($irow['type'], $prev_type) == 0) {
219 continue;
222 $prev_type = $irow['type'];
224 fwrite($this->tmpfh, 'IN' .
225 ',"' . $this->fixString($irow['subscriber_lname']) . '"' .
226 ',"' . $this->fixString($irow['subscriber_fname']) . '"' .
227 ',"' . $this->fixMI($irow['subscriber_mname']) . '"' .
228 ',"' . $this->fixString($irow['subscriber_street']) . '"' .
229 ',"' . $this->fixString($irow['subscriber_city']) . '"' .
230 ',"' . $this->fixString($irow['subscriber_state']) . '"' .
231 ',"' . $this->fixString($irow['subscriber_postal_code']) . '"' .
232 ',"' . $irow['subscriber_DOB'] . '"' .
233 ',"' . $this->fixRelation($irow['subscriber_relationship']) . '"' .
234 ',"' . $this->fixString($irow['policy_number']) . '"' .
235 ',"' . $this->fixString($irow['group_number']) . '"' .
236 ',"' . $this->fixString($irow['name']) . '"' .
237 ',"' . $this->fixString($irow['line1']) . '"' .
238 ',"' . $this->fixString($irow['line2']) . '"' .
239 ',"' . $this->fixString($irow['city']) . '"' .
240 ',"' . $this->fixString($irow['state']) . '"' .
241 ',"' . $this->fixString($irow['zip']) . '"' .
242 ',"' . $this->fixPhone($irow['area_code'] . $irow['prefix'] . $irow['number']) . '"' .
243 ',"' . $this->fixString($irow['provider_number']) . '"' .
244 ',"' . $this->fixString($irow['provider_number']) . '"' . // TBD: referring provider
245 "\n");
248 // Procedure information:
250 $query = "SELECT id, code, modifier, justify " .
251 "FROM billing " .
252 "WHERE pid = ? AND encounter = ? " .
253 "AND activity = 1 AND code_type = 'CPT4' " .
254 "ORDER BY id";
255 $bres = sqlStatement($query, array($patient_id, $encounter));
257 while ($brow = sqlFetchArray($bres)) {
258 fwrite($this->tmpfh, 'PR' .
259 ',"' . $this->fixCPT($brow['code'], $brow['modifier']) . '"' .
260 ',"' . $this->fixJust($brow['justify']) . '"' .
261 ',"' . $this->fixDate($erow['date']) . '"' .
262 ',"' . $this->fixString($erow['pos_code']) . '"' .
263 ',"' . $this->fixString($erow['clia_code']) . '"' .
264 ',"' . $this->fixString($erow['facility']) . '"' .
265 ',"' . $this->fixString($erow['street']) . '"' .
266 ',""' .
267 ',"' . $this->fixString($erow['city']) . '"' .
268 ',"' . $this->fixString($erow['state']) . '"' .
269 ',"' . $this->fixString($erow['postal_code']) . '"' .
270 "\n");
274 // Close the output file and move it to the ftp download area.
276 function close()
278 fclose($this->tmpfh);
279 chmod($this->tmpname, 0666);
280 rename($this->tmpname, $this->TARGET_DIR . '/' . basename($this->tmpname));