Merge branch 'master' into AE/groupFixes
[openemr.git] / custom / BillingExport.csv.php
blob9f43bcf2b91f763d7841b928f4e1e3949ff64058
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 {
20 // You should customize these paths. They must share the same
21 // physical disk partition so that the final rename will be an
22 // atomic operation.
23 var $TMP_DIR = "/home/billing/tmp";
24 var $TARGET_DIR = "/home/billing/ftp";
26 var $tmpname; // output filename including path
27 var $tmpfh; // output file handle
29 function fixString($string) {
30 return addslashes(trim($string));
33 function fixMI($string) {
34 return addslashes(substr(trim($string), 0, 1));
37 function fixSex($sex) {
38 $sex = substr(strtoupper(trim($sex)), 0, 1);
39 if ($sex == 'M') return 'Male';
40 if ($sex == 'F') return 'Female';
41 return '';
44 function fixPhone($phone) {
45 $tmparr = array();
46 if (preg_match("/(\d\d\d)\D*(\d\d\d)\D*(\d\d\d\d)/", $phone, $tmparr))
47 return $tmparr[1] . '-' . $tmparr[2] . '-' . $tmparr[3];
48 return '';
51 function fixSSN($ssn) {
52 $tmparr = array();
53 if (preg_match("/(\d\d\d)\D*(\d\d)\D*(\d\d\d\d)/", $ssn, $tmparr))
54 return $tmparr[1] . '-' . $tmparr[2] . '-' . $tmparr[3];
55 return '';
58 function fixMStatus($status) {
59 return ucfirst(trim($status));
62 function fixEStatus($employer) {
63 $status = strtoupper(trim($employer));
64 if (! $status) return '';
65 if ($status == 'STUDENT') return 'Student';
66 if ($status == 'RETIRED') return 'Retired';
67 return 'Full-time';
70 function fixRelation($rel) {
71 return ucfirst(trim($rel));
74 function fixCPT($code, $mod) {
75 $code = trim($code);
76 $mod = trim($mod);
77 if ($mod) $code .= '-' . $mod;
78 return addslashes($code);
81 function fixJust($str) {
82 return addslashes(trim(str_replace(':', ' ', $str)));
85 function fixDate($date) {
86 return substr($date, 0, 10);
89 // Creating a BillingExport object opens the output file.
90 // Filename format is "transYYYYMMDDHHMMSS.txt".
92 function BillingExport() {
93 $this->tmpname = $this->TMP_DIR . '/trans' . date("YmdHis") . '.txt';
94 $this->tmpfh = fopen($this->tmpname, 'w');
97 // Call this once for each claim to be processed.
99 function addClaim($patient_id, $encounter) {
101 // Patient information:
103 $query = "SELECT p.pubpid, p.ss, p.lname, p.fname, p.mname, p.DOB, " .
104 "p.street, p.city, p.state, p.postal_code, p.phone_home, p.phone_biz, " .
105 "p.status, p.sex, e.name " .
106 "FROM patient_data AS p " .
107 "LEFT OUTER JOIN employer_data AS e ON e.pid = '$patient_id' " .
108 "WHERE p.pid = '$patient_id' " .
109 "LIMIT 1";
110 $prow = sqlQuery($query);
112 // Patient line.
113 fwrite($this->tmpfh, 'PT' .
114 ',"' . $this->fixString($prow['pubpid']) . '"' .
115 ',"' . $this->fixString($prow['lname']) . '"' .
116 ',"' . $this->fixString($prow['fname']) . '"' .
117 ',"' . $this->fixMI($prow['mname']) . '"' .
118 ',"' . $this->fixString($prow['street']) . '"' .
119 ',""' .
120 ',"' . $this->fixString($prow['city']) . '"' .
121 ',"' . $this->fixString($prow['state']) . '"' .
122 ',"' . $this->fixString($prow['postal_code']) . '"' .
123 ',"' . $this->fixPhone($prow['phone_home']) . '"' .
124 ',"' . $this->fixPhone($prow['phone_biz']) . '"' .
125 ',"' . $this->fixSex($prow['sex']) . '"' .
126 ',"' . $prow['DOB'] . '"' .
127 ',"' . $this->fixSSN($prow['ss']) . '"' .
128 ',"' . $this->fixEStatus($prow['name']) . '"' .
129 ',"' . $this->fixString($prow['name']) . '"' .
130 "\n");
132 // Encounter information:
134 $query = "SELECT e.date, e.facility, " .
135 "u.id, u.lname, u.fname, u.mname, u.upin, " .
136 "f.street, f.city, f.state, f.postal_code, f.pos_code, " .
137 "f.domain_identifier AS clia_code " .
138 "FROM form_encounter AS e " .
139 "LEFT OUTER JOIN forms ON forms.formdir = 'newpatient' AND " .
140 "forms.form_id = e.id AND forms.pid = '$patient_id' " .
141 "LEFT OUTER JOIN users AS u ON u.username = forms.user " .
142 "LEFT OUTER JOIN facility AS f ON f.name = e.facility " .
143 "WHERE e.pid = '$patient_id' AND e.encounter = '$encounter' " .
144 "LIMIT 1";
145 $erow = sqlQuery($query);
147 // Performing Provider line.
148 fwrite($this->tmpfh, 'PP' .
149 ',"' . $this->fixString($erow['lname']) . '"' .
150 ',"' . $this->fixString($erow['fname']) . '"' .
151 ',"' . $this->fixMI($erow['mname']) . '"' .
152 ',"' . $this->fixString($erow['upin']) . '"' .
153 "\n");
155 // TBD: Referring Provider line when we have such a thing.
157 // Insurance information, up to 3 lines:
159 $query = "SELECT " .
160 "d.type, d.policy_number, d.group_number, " .
161 "d.subscriber_lname, d.subscriber_fname, d.subscriber_mname, " .
162 "d.subscriber_street, d.subscriber_city, d.subscriber_state, " .
163 "d.subscriber_postal_code, d.subscriber_DOB, d.subscriber_sex, " .
164 "d.subscriber_relationship, " .
165 "c.name, " .
166 "a.line1, a.line2, a.city, a.state, a.zip, " .
167 "p.area_code, p.prefix, p.number, " .
168 "n.provider_number " .
169 "FROM insurance_data AS d " .
170 "LEFT OUTER JOIN insurance_companies AS c ON c.id = d.provider " .
171 "LEFT OUTER JOIN addresses AS a ON a.foreign_id = c.id " .
172 "LEFT OUTER JOIN phone_numbers AS p ON p.foreign_id = c.id AND p.type = 2 " .
173 "LEFT OUTER JOIN insurance_numbers AS n ON n.provider_id = " .
174 $erow['id'] . " AND n.insurance_company_id = c.id " .
175 "WHERE d.pid = '$patient_id' AND d.provider != '' " .
176 "ORDER BY d.type ASC, d.date DESC";
177 $ires = sqlStatement($query);
179 $prev_type = '?';
180 while ($irow = sqlFetchArray($ires)) {
181 if (strcmp($irow['type'], $prev_type) == 0) continue;
182 $prev_type = $irow['type'];
184 fwrite($this->tmpfh, 'IN' .
185 ',"' . $this->fixString($irow['subscriber_lname']) . '"' .
186 ',"' . $this->fixString($irow['subscriber_fname']) . '"' .
187 ',"' . $this->fixMI($irow['subscriber_mname']) . '"' .
188 ',"' . $this->fixString($irow['subscriber_street']) . '"' .
189 ',"' . $this->fixString($irow['subscriber_city']) . '"' .
190 ',"' . $this->fixString($irow['subscriber_state']) . '"' .
191 ',"' . $this->fixString($irow['subscriber_postal_code']) . '"' .
192 ',"' . $irow['subscriber_DOB'] . '"' .
193 ',"' . $this->fixRelation($irow['subscriber_relationship']) . '"' .
194 ',"' . $this->fixString($irow['policy_number']) . '"' .
195 ',"' . $this->fixString($irow['group_number']) . '"' .
196 ',"' . $this->fixString($irow['name']) . '"' .
197 ',"' . $this->fixString($irow['line1']) . '"' .
198 ',"' . $this->fixString($irow['line2']) . '"' .
199 ',"' . $this->fixString($irow['city']) . '"' .
200 ',"' . $this->fixString($irow['state']) . '"' .
201 ',"' . $this->fixString($irow['zip']) . '"' .
202 ',"' . $this->fixPhone($irow['area_code'] . $irow['prefix'] . $irow['number']) . '"' .
203 ',"' . $this->fixString($irow['provider_number']) . '"' .
204 ',"' . $this->fixString($irow['provider_number']) . '"' . // TBD: referring provider
205 "\n");
208 // Procedure information:
210 $query = "SELECT id, code, modifier, justify " .
211 "FROM billing " .
212 "WHERE pid = '$patient_id' AND encounter = '$encounter' " .
213 "AND activity = 1 AND code_type = 'CPT4' " .
214 "ORDER BY id";
215 $bres = sqlStatement($query);
217 while ($brow = sqlFetchArray($bres)) {
218 fwrite($this->tmpfh, 'PR' .
219 ',"' . $this->fixCPT($brow['code'], $brow['modifier']) . '"' .
220 ',"' . $this->fixJust($brow['justify']) . '"' .
221 ',"' . $this->fixDate($erow['date']) . '"' .
222 ',"' . $this->fixString($erow['pos_code']) . '"' .
223 ',"' . $this->fixString($erow['clia_code']) . '"' .
224 ',"' . $this->fixString($erow['facility']) . '"' .
225 ',"' . $this->fixString($erow['street']) . '"' .
226 ',""' .
227 ',"' . $this->fixString($erow['city']) . '"' .
228 ',"' . $this->fixString($erow['state']) . '"' .
229 ',"' . $this->fixString($erow['postal_code']) . '"' .
230 "\n");
234 // Close the output file and move it to the ftp download area.
236 function close() {
237 fclose($this->tmpfh);
238 chmod($this->tmpname, 0666);
239 rename($this->tmpname, $this->TARGET_DIR . '/' . basename($this->tmpname));