incorporate standard header template into login screen (#677)
[openemr.git] / library / sl_eob.inc.php
blobfa6ffeaebeac4fd38ea9a903566b7c8346610213
1 <?php
2 // Copyright (C) 2005-2009 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 include_once("patient.inc");
10 include_once("billing.inc");
11 include_once("invoice_summary.inc.php");
13 $chart_id_cash = 0;
14 $chart_id_ar = 0;
15 $chart_id_income = 0;
16 $services_id = 0;
19 // Try to figure out our invoice number (pid.encounter) from the
20 // claim ID and other stuff in the ERA. This should be straightforward
21 // except that some payers mangle the claim ID that we give them.
23 function slInvoiceNumber(&$out) {
24 $invnumber = $out['our_claim_id'];
25 $atmp = preg_split('/[ -]/', $invnumber);
26 $acount = count($atmp);
28 $pid = 0;
29 $encounter = 0;
30 if ($acount == 2) {
31 $pid = $atmp[0];
32 $encounter = $atmp[1];
34 else if ($acount == 3) {
35 $pid = $atmp[0];
36 $brow = sqlQuery("SELECT encounter FROM billing WHERE " .
37 "pid = '$pid' AND encounter = '" . $atmp[1] . "' AND activity = 1");
39 $encounter = $brow['encounter'];
41 else if ($acount == 1) {
42 $pres = sqlStatement("SELECT pid FROM patient_data WHERE " .
43 "lname LIKE '" . addslashes($out['patient_lname']) . "' AND " .
44 "fname LIKE '" . addslashes($out['patient_fname']) . "' " .
45 "ORDER BY pid DESC");
46 while ($prow = sqlFetchArray($pres)) {
47 if (strpos($invnumber, $prow['pid']) === 0) {
48 $pid = $prow['pid'];
49 $encounter = substr($invnumber, strlen($pid));
50 break;
55 if ($pid && $encounter) $invnumber = "$pid.$encounter";
56 return array($pid, $encounter, $invnumber);
59 // This gets a posting session ID. If the payer ID is not 0 and a matching
60 // session already exists, then its ID is returned. Otherwise a new session
61 // is created.
63 function arGetSession($payer_id, $reference, $check_date, $deposit_date='', $pay_total=0) {
64 if (empty($deposit_date)) $deposit_date = $check_date;
65 if ($payer_id) {
66 $row = sqlQuery("SELECT session_id FROM ar_session WHERE " .
67 "payer_id = '$payer_id' AND reference = '$reference' AND " .
68 "check_date = '$check_date' AND deposit_date = '$deposit_date' " .
69 "ORDER BY session_id DESC LIMIT 1");
70 if (!empty($row['session_id'])) return $row['session_id'];
72 return sqlInsert("INSERT INTO ar_session ( " .
73 "payer_id, user_id, reference, check_date, deposit_date, pay_total " .
74 ") VALUES ( " .
75 "'$payer_id', " .
76 "'" . $_SESSION['authUserID'] . "', " .
77 "'$reference', " .
78 "'$check_date', " .
79 "'$deposit_date', " .
80 "'$pay_total' " .
81 ")");
83 //writing the check details to Session Table on ERA proxcessing
84 function arPostSession($payer_id,$check_number,$check_date,$pay_total,$post_to_date,$deposit_date,$debug) {
85 $query = "INSERT INTO ar_session( " .
86 "payer_id,user_id,closed,reference,check_date,pay_total,post_to_date,deposit_date,patient_id,payment_type,adjustment_code,payment_method " .
87 ") VALUES ( " .
88 "'$payer_id'," .
89 $_SESSION['authUserID']."," .
90 "0," .
91 "'ePay - $check_number'," .
92 "'$check_date', " .
93 "$pay_total, " .
94 "'$post_to_date','$deposit_date', " .
95 "0,'insurance','insurance_payment','electronic'" .
96 ")";
97 if ($debug) {
98 echo $query . "<br>\n";
99 } else {
100 $sessionId=sqlInsert($query);
101 return $sessionId;
105 // Post a payment, new style.
107 function arPostPayment($patient_id, $encounter_id, $session_id, $amount, $code, $payer_type, $memo, $debug, $time='', $codetype='') {
108 $codeonly = $code;
109 $modifier = '';
110 $tmp = strpos($code, ':');
111 if ($tmp) {
112 $codeonly = substr($code, 0, $tmp);
113 $modifier = substr($code, $tmp+1);
115 if (empty($time)) $time = date('Y-m-d H:i:s');
117 sqlBeginTrans();
118 $sequence_no = sqlQuery( "SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array($patient_id, $encounter_id));
119 $query = "INSERT INTO ar_activity ( " .
120 "pid, encounter, sequence_no, code_type, code, modifier, payer_type, post_time, post_user, " .
121 "session_id, memo, pay_amount " .
122 ") VALUES ( " .
123 "'$patient_id', " .
124 "'$encounter_id', " .
125 "'{$sequence_no['increment']}', " .
126 "'$codetype', " .
127 "'$codeonly', " .
128 "'$modifier', " .
129 "'$payer_type', " .
130 "'$time', " .
131 "'" . $_SESSION['authUserID'] . "', " .
132 "'$session_id', " .
133 "'$memo', " .
134 "'$amount' " .
135 ")";
136 sqlStatement($query);
137 sqlCommitTrans();
138 return;
141 // Post a charge. This is called only from sl_eob_process.php where
142 // automated remittance processing can create a new service item.
143 // Here we add it as an unauthorized item to the billing table.
145 function arPostCharge($patient_id, $encounter_id, $session_id, $amount, $units, $thisdate, $code, $description, $debug, $codetype='') {
146 /*****************************************************************
147 // Select an existing billing item as a template.
148 $row= sqlQuery("SELECT * FROM billing WHERE " .
149 "pid = '$patient_id' AND encounter = '$encounter_id' AND " .
150 "code_type = 'CPT4' AND activity = 1 " .
151 "ORDER BY id DESC LIMIT 1");
152 $this_authorized = 0;
153 $this_provider = 0;
154 if (!empty($row)) {
155 $this_authorized = $row['authorized'];
156 $this_provider = $row['provider_id'];
158 *****************************************************************/
160 if (empty($codetype)) {
161 // default to CPT4 if empty, which is consistent with previous functionality.
162 $codetype="CPT4";
164 $codeonly = $code;
165 $modifier = '';
166 $tmp = strpos($code, ':');
167 if ($tmp) {
168 $codeonly = substr($code, 0, $tmp);
169 $modifier = substr($code, $tmp+1);
172 addBilling($encounter_id,
173 $codetype,
174 $codeonly,
175 $description,
176 $patient_id,
179 $modifier,
180 $units,
181 $amount,
183 '');
186 // Post an adjustment, new style.
188 function arPostAdjustment($patient_id, $encounter_id, $session_id, $amount, $code, $payer_type, $reason, $debug, $time='', $codetype='') {
189 $codeonly = $code;
190 $modifier = '';
191 $tmp = strpos($code, ':');
192 if ($tmp) {
193 $codeonly = substr($code, 0, $tmp);
194 $modifier = substr($code, $tmp+1);
196 if (empty($time)) $time = date('Y-m-d H:i:s');
198 sqlBeginTrans();
199 $sequence_no = sqlQuery( "SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array($patient_id, $encounter_id));
200 $query = "INSERT INTO ar_activity ( " .
201 "pid, encounter, sequence_no, code_type, code, modifier, payer_type, post_user, post_time, " .
202 "session_id, memo, adj_amount " .
203 ") VALUES ( " .
204 "'$patient_id', " .
205 "'$encounter_id', " .
206 "'{$sequence_no['increment']}', " .
207 "'$codetype', " .
208 "'$codeonly', " .
209 "'$modifier', " .
210 "'$payer_type', " .
211 "'" . $_SESSION['authUserID'] . "', " .
212 "'$time', " .
213 "'$session_id', " .
214 "'$reason', " .
215 "'$amount' " .
216 ")";
217 sqlStatement($query);
218 sqlCommitTrans();
219 return;
222 function arGetPayerID($patient_id, $date_of_service, $payer_type) {
223 if ($payer_type < 1 || $payer_type > 3) return 0;
224 $tmp = array(1 => 'primary', 2 => 'secondary', 3 => 'tertiary');
225 $value = $tmp[$payer_type];
226 $query = "SELECT provider FROM insurance_data WHERE " .
227 "pid = ? AND type = ? AND date <= ? " .
228 "ORDER BY date DESC LIMIT 1";
229 $nprow = sqlQuery($query, array($patient_id,$value,$date_of_service) );
230 if (empty($nprow)) return 0;
231 return $nprow['provider'];
234 // Make this invoice re-billable, new style.
236 function arSetupSecondary($patient_id, $encounter_id, $debug,$crossover=0) {
237 if ($crossover==1) {
238 //if claim forwarded setting a new status
239 $status=6;
241 } else {
243 $status=1;
246 // Determine the next insurance level to be billed.
247 $ferow = sqlQuery("SELECT date, last_level_billed " .
248 "FROM form_encounter WHERE " .
249 "pid = '$patient_id' AND encounter = '$encounter_id'");
250 $date_of_service = substr($ferow['date'], 0, 10);
251 $new_payer_type = 0 + $ferow['last_level_billed'];
252 if ($new_payer_type < 3 && !empty($ferow['last_level_billed']) || $new_payer_type == 0)
253 ++$new_payer_type;
255 $new_payer_id = arGetPayerID($patient_id, $date_of_service, $new_payer_type);
257 if ($new_payer_id) {
258 // Queue up the claim.
259 if (!$debug)
260 updateClaim(true, $patient_id, $encounter_id, $new_payer_id, $new_payer_type,$status, 5, '', 'hcfa','',$crossover);
262 else {
263 // Just reopen the claim.
264 if (!$debug)
265 updateClaim(true, $patient_id, $encounter_id, -1, -1, $status, 0, '','','',$crossover);
268 return xl("Encounter ") . $encounter . xl(" is ready for re-billing.");