First impression, update the look of the landing page (#3626)
[openemr.git] / src / Billing / SLEOB.php
blob80949239ab51b9ad87aa05981c95d97543883621
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");
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($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);
116 if (empty($time)) {
117 $time = date('Y-m-d H:i:s');
120 sqlBeginTrans();
121 $sequence_no = sqlQuery("SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array($patient_id, $encounter_id));
122 $query = "INSERT INTO ar_activity ( " .
123 "pid, encounter, sequence_no, code_type, code, modifier, payer_type, post_time, post_user, " .
124 "session_id, memo, pay_amount " .
125 ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
126 sqlStatement($query, array($patient_id, $encounter_id, $sequence_no['increment'], $codetype, $codeonly, $modifier, $payer_type, $time, $_SESSION['authUserID'], $session_id, $memo, $amount));
127 sqlCommitTrans();
128 return;
131 // Post a charge. This is called only from sl_eob_process.php where
132 // automated remittance processing can create a new service item.
133 // Here we add it as an unauthorized item to the billing table.
135 public static function arPostCharge($patient_id, $encounter_id, $session_id, $amount, $units, $thisdate, $code, $description, $debug, $codetype = '')
137 /*****************************************************************
138 * // Select an existing billing item as a template.
139 * $row= sqlQuery("SELECT * FROM billing WHERE " .
140 * "pid = '$patient_id' AND encounter = '$encounter_id' AND " .
141 * "code_type = 'CPT4' AND activity = 1 " .
142 * "ORDER BY id DESC LIMIT 1");
143 * $this_authorized = 0;
144 * $this_provider = 0;
145 * if (!empty($row)) {
146 * $this_authorized = $row['authorized'];
147 * $this_provider = $row['provider_id'];
149 *****************************************************************/
151 if (empty($codetype)) {
152 // default to CPT4 if empty, which is consistent with previous functionality.
153 $codetype = "CPT4";
156 $codeonly = $code;
157 $modifier = '';
158 $tmp = strpos($code, ':');
159 if ($tmp) {
160 $codeonly = substr($code, 0, $tmp);
161 $modifier = substr($code, $tmp + 1);
164 BillingUtilities::addBilling(
165 $encounter_id,
166 $codetype,
167 $codeonly,
168 $description,
169 $patient_id,
172 $modifier,
173 $units,
174 $amount,
180 // Post an adjustment, new style.
182 public static function arPostAdjustment($patient_id, $encounter_id, $session_id, $amount, $code, $payer_type, $reason, $debug, $time = '', $codetype = '')
184 $codeonly = $code;
185 $modifier = '';
186 $tmp = strpos($code, ':');
187 if ($tmp) {
188 $codeonly = substr($code, 0, $tmp);
189 $modifier = substr($code, $tmp + 1);
192 if (empty($time)) {
193 $time = date('Y-m-d H:i:s');
196 sqlBeginTrans();
197 $sequence_no = sqlQuery("SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array($patient_id, $encounter_id));
198 $query = "INSERT INTO ar_activity ( " .
199 "pid, encounter, sequence_no, code_type, code, modifier, payer_type, post_user, post_time, " .
200 "session_id, memo, adj_amount " .
201 ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
202 sqlStatement($query, array($patient_id, $encounter_id, $sequence_no['increment'], $codetype, $codeonly, $modifier, $payer_type, $_SESSION['authUserID'], $time, $session_id, $reason, $amount));
203 sqlCommitTrans();
204 return;
207 public static function arGetPayerID($patient_id, $date_of_service, $payer_type)
209 if ($payer_type < 1 || $payer_type > 3) {
210 return 0;
213 $tmp = array(1 => 'primary', 2 => 'secondary', 3 => 'tertiary');
214 $value = $tmp[$payer_type];
215 $query = "SELECT provider FROM insurance_data WHERE " .
216 "pid = ? AND type = ? AND (date <= ? OR date IS NULL) " .
217 "ORDER BY date DESC LIMIT 1";
218 $nprow = sqlQuery($query, array($patient_id, $value, $date_of_service));
219 if (empty($nprow)) {
220 return 0;
223 return $nprow['provider'];
226 // Make this invoice re-billable, new style.
228 public static function arSetupSecondary($patient_id, $encounter_id, $debug, $crossover = 0)
230 if ($crossover == 1) {
231 //if claim forwarded setting a new status
232 $status = 6;
233 } else {
234 $status = 1;
237 // Determine the next insurance level to be billed.
238 $ferow = sqlQuery("SELECT date, last_level_billed " .
239 "FROM form_encounter WHERE " .
240 "pid = ? AND encounter = ?", array($patient_id, $encounter_id));
241 $date_of_service = substr($ferow['date'], 0, 10);
242 $new_payer_type = 0 + $ferow['last_level_billed'];
243 if ($new_payer_type < 3 && !empty($ferow['last_level_billed']) || $new_payer_type == 0) {
244 ++$new_payer_type;
247 $new_payer_id = self::arGetPayerID($patient_id, $date_of_service, $new_payer_type);
249 if ($new_payer_id) {
250 // Queue up the claim.
251 if (!$debug) {
252 BillingUtilities::updateClaim(true, $patient_id, $encounter_id, $new_payer_id, $new_payer_type, $status, 5, '', 'hcfa', '', $crossover);
254 } else {
255 // Just reopen the claim.
256 if (!$debug) {
257 BillingUtilities::updateClaim(true, $patient_id, $encounter_id, -1, -1, $status, 0, '', '', '', $crossover);
261 return xl("Encounter ") . $encounter . xl(" is ready for re-billing.");