Security Fix (xss)
[openemr.git] / custom / BillingExport.csv.php
blobc8328ec1bc4dce72ef5f42628942981c26e6c905
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.
16 // This will cause the FreeB support in OpenEMR to be replaced.
18 require_once (dirname(__FILE__) . "/../library/sql.inc");
20 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) {
32 return addslashes(trim($string));
35 function fixMI($string) {
36 return addslashes(substr(trim($string), 0, 1));
39 function fixSex($sex) {
40 $sex = substr(strtoupper(trim($sex)), 0, 1);
41 if ($sex == 'M') return 'Male';
42 if ($sex == 'F') return 'Female';
43 return '';
46 function fixPhone($phone) {
47 $tmparr = array();
48 if (preg_match("/(\d\d\d)\D*(\d\d\d)\D*(\d\d\d\d)/", $phone, $tmparr))
49 return $tmparr[1] . '-' . $tmparr[2] . '-' . $tmparr[3];
50 return '';
53 function fixSSN($ssn) {
54 $tmparr = array();
55 if (preg_match("/(\d\d\d)\D*(\d\d)\D*(\d\d\d\d)/", $ssn, $tmparr))
56 return $tmparr[1] . '-' . $tmparr[2] . '-' . $tmparr[3];
57 return '';
60 function fixMStatus($status) {
61 return ucfirst(trim($status));
64 function fixEStatus($employer) {
65 $status = strtoupper(trim($employer));
66 if (! $status) return '';
67 if ($status == 'STUDENT') return 'Student';
68 if ($status == 'RETIRED') return 'Retired';
69 return 'Full-time';
72 function fixRelation($rel) {
73 return ucfirst(trim($rel));
76 function fixCPT($code, $mod) {
77 $code = trim($code);
78 $mod = trim($mod);
79 if ($mod) $code .= '-' . $mod;
80 return addslashes($code);
83 function fixJust($str) {
84 return addslashes(trim(str_replace(':', ' ', $str)));
87 function fixDate($date) {
88 return substr($date, 0, 10);
91 // Creating a BillingExport object opens the output file.
92 // Filename format is "transYYYYMMDDHHMMSS.txt".
94 function BillingExport() {
95 $this->tmpname = $this->TMP_DIR . '/trans' . date("YmdHis") . '.txt';
96 $this->tmpfh = fopen($this->tmpname, 'w');
99 // Call this once for each claim to be processed.
101 function addClaim($patient_id, $encounter) {
103 // Patient information:
105 $query = "SELECT p.pubpid, p.ss, p.lname, p.fname, p.mname, p.DOB, " .
106 "p.street, p.city, p.state, p.postal_code, p.phone_home, p.phone_biz, " .
107 "p.status, p.sex, e.name " .
108 "FROM patient_data AS p " .
109 "LEFT OUTER JOIN employer_data AS e ON e.pid = '$patient_id' " .
110 "WHERE p.pid = '$patient_id' " .
111 "LIMIT 1";
112 $prow = sqlQuery($query);
114 // Patient line.
115 fwrite($this->tmpfh, 'PT' .
116 ',"' . $this->fixString($prow['pubpid']) . '"' .
117 ',"' . $this->fixString($prow['lname']) . '"' .
118 ',"' . $this->fixString($prow['fname']) . '"' .
119 ',"' . $this->fixMI($prow['mname']) . '"' .
120 ',"' . $this->fixString($prow['street']) . '"' .
121 ',""' .
122 ',"' . $this->fixString($prow['city']) . '"' .
123 ',"' . $this->fixString($prow['state']) . '"' .
124 ',"' . $this->fixString($prow['postal_code']) . '"' .
125 ',"' . $this->fixPhone($prow['phone_home']) . '"' .
126 ',"' . $this->fixPhone($prow['phone_biz']) . '"' .
127 ',"' . $this->fixSex($prow['sex']) . '"' .
128 ',"' . $prow['DOB'] . '"' .
129 ',"' . $this->fixSSN($prow['ss']) . '"' .
130 ',"' . $this->fixEStatus($prow['name']) . '"' .
131 ',"' . $this->fixString($prow['name']) . '"' .
132 "\n");
134 // Encounter information:
136 $query = "SELECT e.date, e.facility, " .
137 "u.id, u.lname, u.fname, u.mname, u.upin, " .
138 "f.street, f.city, f.state, f.postal_code, f.pos_code, " .
139 "f.domain_identifier AS clia_code " .
140 "FROM form_encounter AS e " .
141 "LEFT OUTER JOIN forms ON forms.formdir = 'newpatient' AND " .
142 "forms.form_id = e.id AND forms.pid = '$patient_id' " .
143 "LEFT OUTER JOIN users AS u ON u.username = forms.user " .
144 "LEFT OUTER JOIN facility AS f ON f.name = e.facility " .
145 "WHERE e.pid = '$patient_id' AND e.encounter = '$encounter' " .
146 "LIMIT 1";
147 $erow = sqlQuery($query);
149 // Performing Provider line.
150 fwrite($this->tmpfh, 'PP' .
151 ',"' . $this->fixString($erow['lname']) . '"' .
152 ',"' . $this->fixString($erow['fname']) . '"' .
153 ',"' . $this->fixMI($erow['mname']) . '"' .
154 ',"' . $this->fixString($erow['upin']) . '"' .
155 "\n");
157 // TBD: Referring Provider line when we have such a thing.
159 // Insurance information, up to 3 lines:
161 $query = "SELECT " .
162 "d.type, d.policy_number, d.group_number, " .
163 "d.subscriber_lname, d.subscriber_fname, d.subscriber_mname, " .
164 "d.subscriber_street, d.subscriber_city, d.subscriber_state, " .
165 "d.subscriber_postal_code, d.subscriber_DOB, d.subscriber_sex, " .
166 "d.subscriber_relationship, " .
167 "c.name, " .
168 "a.line1, a.line2, a.city, a.state, a.zip, " .
169 "p.area_code, p.prefix, p.number, " .
170 "n.provider_number " .
171 "FROM insurance_data AS d " .
172 "LEFT OUTER JOIN insurance_companies AS c ON c.id = d.provider " .
173 "LEFT OUTER JOIN addresses AS a ON a.foreign_id = c.id " .
174 "LEFT OUTER JOIN phone_numbers AS p ON p.foreign_id = c.id AND p.type = 2 " .
175 "LEFT OUTER JOIN insurance_numbers AS n ON n.provider_id = " .
176 $erow['id'] . " AND n.insurance_company_id = c.id " .
177 "WHERE d.pid = '$patient_id' AND d.provider != '' " .
178 "ORDER BY d.type ASC, d.date DESC";
179 $ires = sqlStatement($query);
181 $prev_type = '?';
182 while ($irow = sqlFetchArray($ires)) {
183 if (strcmp($irow['type'], $prev_type) == 0) continue;
184 $prev_type = $irow['type'];
186 fwrite($this->tmpfh, 'IN' .
187 ',"' . $this->fixString($irow['subscriber_lname']) . '"' .
188 ',"' . $this->fixString($irow['subscriber_fname']) . '"' .
189 ',"' . $this->fixMI($irow['subscriber_mname']) . '"' .
190 ',"' . $this->fixString($irow['subscriber_street']) . '"' .
191 ',"' . $this->fixString($irow['subscriber_city']) . '"' .
192 ',"' . $this->fixString($irow['subscriber_state']) . '"' .
193 ',"' . $this->fixString($irow['subscriber_postal_code']) . '"' .
194 ',"' . $irow['subscriber_DOB'] . '"' .
195 ',"' . $this->fixRelation($irow['subscriber_relationship']) . '"' .
196 ',"' . $this->fixString($irow['policy_number']) . '"' .
197 ',"' . $this->fixString($irow['group_number']) . '"' .
198 ',"' . $this->fixString($irow['name']) . '"' .
199 ',"' . $this->fixString($irow['line1']) . '"' .
200 ',"' . $this->fixString($irow['line2']) . '"' .
201 ',"' . $this->fixString($irow['city']) . '"' .
202 ',"' . $this->fixString($irow['state']) . '"' .
203 ',"' . $this->fixString($irow['zip']) . '"' .
204 ',"' . $this->fixPhone($irow['area_code'] . $irow['prefix'] . $irow['number']) . '"' .
205 ',"' . $this->fixString($irow['provider_number']) . '"' .
206 ',"' . $this->fixString($irow['provider_number']) . '"' . // TBD: referring provider
207 "\n");
210 // Procedure information:
212 $query = "SELECT id, code, modifier, justify " .
213 "FROM billing " .
214 "WHERE pid = '$patient_id' AND encounter = '$encounter' " .
215 "AND activity = 1 AND code_type = 'CPT4' " .
216 "ORDER BY id";
217 $bres = sqlStatement($query);
219 while ($brow = sqlFetchArray($bres)) {
220 fwrite($this->tmpfh, 'PR' .
221 ',"' . $this->fixCPT($brow['code'], $brow['modifier']) . '"' .
222 ',"' . $this->fixJust($brow['justify']) . '"' .
223 ',"' . $this->fixDate($erow['date']) . '"' .
224 ',"' . $this->fixString($erow['pos_code']) . '"' .
225 ',"' . $this->fixString($erow['clia_code']) . '"' .
226 ',"' . $this->fixString($erow['facility']) . '"' .
227 ',"' . $this->fixString($erow['street']) . '"' .
228 ',""' .
229 ',"' . $this->fixString($erow['city']) . '"' .
230 ',"' . $this->fixString($erow['state']) . '"' .
231 ',"' . $this->fixString($erow['postal_code']) . '"' .
232 "\n");
236 // Close the output file and move it to the ftp download area.
238 function close() {
239 fclose($this->tmpfh);
240 chmod($this->tmpname, 0666);
241 rename($this->tmpname, $this->TARGET_DIR . '/' . basename($this->tmpname));