.inc files migration to .inc.php (#5897)
[openemr.git] / src / Billing / SLEOB.php
blob627236d8cd2d3299fac95669f0362496ce91c557
1 <?php
3 /**
4 * This class has various billing functions for posting charges and payments.
6 * @package OpenEMR
7 * @author Rod Roark <rod@sunsetsystems.com>
8 * @author Stephen Waite <stephen.waite@cmsvt.com>
9 * @copyright Copyright (c) 2005-2009 Rod Roark <rod@sunsetsystems.com>
10 * @copyright Copyright (c) 2018 Stephen Waite <stephen.waite@cmsvt.com>
11 * @link https://www.open-emr.org
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 namespace OpenEMR\Billing;
17 require_once(dirname(__FILE__) . "/../../library/patient.inc.php");
19 use OpenEMR\Billing\BillingUtilities;
21 class SLEOB
23 // Try to figure out our invoice number (pid.encounter) from the
24 // claim ID and other stuff in the ERA. This should be straightforward
25 // except that some payers mangle the claim ID that we give them.
27 public static function slInvoiceNumber(&$out)
29 $invnumber = $out['our_claim_id'];
30 $atmp = preg_split('/[ -]/', $invnumber);
31 $acount = count($atmp);
33 $pid = 0;
34 $encounter = 0;
35 if ($acount == 2) {
36 $pid = $atmp[0];
37 $encounter = $atmp[1];
38 } elseif ($acount == 3) {
39 $pid = $atmp[0];
40 $brow = sqlQuery("SELECT encounter FROM billing WHERE " .
41 "pid = '$pid' AND encounter = ? AND activity = 1", array($atmp[1]));
43 $encounter = $brow['encounter'];
44 } elseif ($acount == 1) {
45 $pres = sqlStatement("SELECT pid FROM patient_data WHERE " .
46 "lname LIKE ? AND " .
47 "fname LIKE ? " .
48 "ORDER BY pid DESC", array($out['patient_lname'], $out['patient_fname']));
49 while ($prow = sqlFetchArray($pres)) {
50 if (strpos($invnumber, $prow['pid']) === 0) {
51 $pid = $prow['pid'];
52 $encounter = substr($invnumber, strlen($pid));
53 break;
58 if ($pid && $encounter) {
59 $invnumber = "$pid.$encounter";
62 return array($pid, $encounter, $invnumber);
65 // This gets a posting session ID. If the payer ID is not 0 and a matching
66 // session already exists, then its ID is returned. Otherwise a new session
67 // is created.
69 public static function arGetSession($payer_id, $reference, $check_date, $deposit_date = '', $pay_total = 0)
71 if (empty($deposit_date)) {
72 $deposit_date = $check_date;
75 if ($payer_id) {
76 $row = sqlQuery("SELECT session_id FROM ar_session WHERE " .
77 "payer_id = ? AND reference = ? AND " .
78 "check_date = ? AND deposit_date = ? " .
79 "ORDER BY session_id DESC LIMIT 1", array($payer_id, $reference, $check_date, $deposit_date));
80 if (!empty($row['session_id'])) {
81 return $row['session_id'];
85 return sqlInsert("INSERT INTO ar_session ( " .
86 "payer_id, user_id, reference, check_date, deposit_date, pay_total " .
87 ") VALUES ( ?, ?, ?, ?, ?, ? )", array($payer_id, $_SESSION['authUserID'], $reference, $check_date, $deposit_date, $pay_total));
90 //writing the check details to Session Table on ERA proxcessing
91 public static function arPostSession($payer_id, $check_number, $check_date, $pay_total, $post_to_date, $deposit_date, $debug)
93 $query = "INSERT INTO ar_session( " .
94 "payer_id,user_id,closed,reference,check_date,pay_total,post_to_date,deposit_date,patient_id,payment_type,adjustment_code,payment_method " .
95 ") VALUES (?, ?, 0, ?, ?, ?, ? ,?, 0, 'insurance', 'insurance_payment', 'electronic')";
96 if ($debug) {
97 echo text($query) . "<br />\n";
98 } else {
99 $sessionId = sqlInsert($query, array($payer_id, $_SESSION['authUserID'], 'ePay - ' . $check_number, $check_date, $pay_total, $post_to_date, $deposit_date));
100 return $sessionId;
104 // Post a payment, new style.
106 public static function arPostPayment(
107 $patient_id,
108 $encounter_id,
109 $session_id,
110 $amount,
111 $code,
112 $payer_type,
113 $memo,
114 $debug,
115 $time = '',
116 $codetype = '',
117 $date = ''
119 $codeonly = $code;
120 $modifier = '';
121 $tmp = strpos($code, ':');
122 if ($tmp) {
123 $codeonly = substr($code, 0, $tmp);
124 $modifier = substr($code, $tmp + 1);
127 if (empty($time)) {
128 $time = date('Y-m-d H:i:s');
131 sqlBeginTrans();
132 $sequence_no = sqlQuery(
133 "SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?",
134 array($patient_id, $encounter_id)
136 $query = "INSERT INTO ar_activity ( " .
137 "pid, encounter, sequence_no, code_type, code, modifier, payer_type, post_time, post_date, post_user, " .
138 "session_id, memo, pay_amount " .
139 ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
140 sqlStatement(
141 $query,
142 array(
143 $patient_id,
144 $encounter_id,
145 $sequence_no['increment'],
146 $codetype,
147 $codeonly,
148 $modifier,
149 $payer_type,
150 $time,
151 $date,
152 $_SESSION['authUserID'],
153 $session_id,
154 $memo,
155 $amount
158 sqlCommitTrans();
159 return;
162 // Post a charge. This is called only from sl_eob_process.php where
163 // automated remittance processing can create a new service item.
164 // Here we add it as an unauthorized item to the billing table.
166 public static function arPostCharge($patient_id, $encounter_id, $session_id, $amount, $units, $thisdate, $code, $description, $debug, $codetype = '')
168 /*****************************************************************
169 * // Select an existing billing item as a template.
170 * $row= sqlQuery("SELECT * FROM billing WHERE " .
171 * "pid = '$patient_id' AND encounter = '$encounter_id' AND " .
172 * "code_type = 'CPT4' AND activity = 1 " .
173 * "ORDER BY id DESC LIMIT 1");
174 * $this_authorized = 0;
175 * $this_provider = 0;
176 * if (!empty($row)) {
177 * $this_authorized = $row['authorized'];
178 * $this_provider = $row['provider_id'];
180 *****************************************************************/
182 if (empty($codetype)) {
183 // default to CPT4 if empty, which is consistent with previous functionality.
184 $codetype = "CPT4";
187 $codeonly = $code;
188 $modifier = '';
189 $tmp = strpos($code, ':');
190 if ($tmp) {
191 $codeonly = substr($code, 0, $tmp);
192 $modifier = substr($code, $tmp + 1);
195 BillingUtilities::addBilling(
196 $encounter_id,
197 $codetype,
198 $codeonly,
199 $description,
200 $patient_id,
203 $modifier,
204 $units,
205 $amount,
211 // Post an adjustment, new style.
213 public static function arPostAdjustment($patient_id, $encounter_id, $session_id, $amount, $code, $payer_type, $reason, $debug, $time = '', $codetype = '')
215 $codeonly = $code;
216 $modifier = '';
217 $tmp = strpos($code, ':');
218 if ($tmp) {
219 $codeonly = substr($code, 0, $tmp);
220 $modifier = substr($code, $tmp + 1);
223 if (empty($time)) {
224 $time = date('Y-m-d H:i:s');
227 sqlBeginTrans();
228 $sequence_no = sqlQuery("SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array($patient_id, $encounter_id));
229 $query = "INSERT INTO ar_activity ( " .
230 "pid, encounter, sequence_no, code_type, code, modifier, payer_type, post_user, post_time, " .
231 "session_id, memo, adj_amount " .
232 ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
233 sqlStatement($query, array($patient_id, $encounter_id, $sequence_no['increment'], $codetype, $codeonly, $modifier, $payer_type, $_SESSION['authUserID'], $time, $session_id, $reason, $amount));
234 sqlCommitTrans();
235 return;
238 public static function arGetPayerID($patient_id, $date_of_service, $payer_type)
240 if ($payer_type < 1 || $payer_type > 3) {
241 return 0;
244 $tmp = array(1 => 'primary', 2 => 'secondary', 3 => 'tertiary');
245 $value = $tmp[$payer_type];
246 $query = "SELECT provider FROM insurance_data WHERE " .
247 "pid = ? AND type = ? AND (date <= ? OR date IS NULL) " .
248 "ORDER BY date DESC LIMIT 1";
249 $nprow = sqlQuery($query, array($patient_id, $value, $date_of_service));
250 if (empty($nprow)) {
251 return 0;
254 return $nprow['provider'];
257 // Make this invoice re-billable, new style.
259 public static function arSetupSecondary($patient_id, $encounter_id, $debug, $crossover = 0)
261 if ($crossover == 1) {
262 //if claim forwarded setting a new status
263 $status = 6;
264 } else {
265 $status = 1;
268 // Determine the next insurance level to be billed.
269 $ferow = sqlQuery("SELECT date, last_level_billed " .
270 "FROM form_encounter WHERE " .
271 "pid = ? AND encounter = ?", array($patient_id, $encounter_id));
272 $date_of_service = substr($ferow['date'], 0, 10);
273 $new_payer_type = 0 + $ferow['last_level_billed'];
274 if ($new_payer_type < 3 && !empty($ferow['last_level_billed']) || $new_payer_type == 0) {
275 ++$new_payer_type;
278 $new_payer_id = self::arGetPayerID($patient_id, $date_of_service, $new_payer_type);
280 if ($new_payer_id) {
281 // Queue up the claim.
282 if (!$debug) {
283 BillingUtilities::updateClaim(true, $patient_id, $encounter_id, $new_payer_id, $new_payer_type, $status, 5, '', 'hcfa', '', $crossover);
285 } else {
286 // Just reopen the claim.
287 if (!$debug) {
288 BillingUtilities::updateClaim(true, $patient_id, $encounter_id, -1, -1, $status, 0, '', '', '', $crossover);
292 return xl("Encounter ") . $encounter_id . xl(" is ready for re-billing.");