forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoice_summary.inc.php
291 lines (266 loc) · 10.8 KB
/
invoice_summary.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
// Copyright (C) 2005-2010 Rod Roark <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This returns an associative array keyed on procedure code, representing
// all charge items for one invoice. This array's values are themselves
// associative arrays having the following keys:
//
// chg - the sum of line items, including adjustments, for the code
// bal - the unpaid balance
// adj - the (positive) sum of inverted adjustments
// ins - the id of the insurance company that was billed (obsolete)
// dtl - associative array of details, if requested
//
// Where details are requested, each dtl array is keyed on a string
// beginning with a date in yyyy-mm-dd format, or blanks in the case
// of the original charge items. The value array is:
//
// pmt - payment amount as a positive number, only for payments
// src - check number or other source, only for payments
// chg - invoice line item amount amount, only for charges or
// adjustments (adjustments may be zero)
// rsn - adjustment reason, only for adjustments
// plv - provided for "integrated A/R" only: 0=pt, 1=Ins1, etc.
// dsc - for tax charges, a description of the tax
// arseq - ar_activity.sequence_no when it applies.
require_once("sl_eob.inc.php");
require_once(dirname(__FILE__) . "/../custom/code_types.inc.php");
function get_invoice_summary($trans_id, $with_detail = false) {
global $sl_err, $sl_cash_acc;
$codes = array();
$chart_id_cash = SLQueryValue("select id from chart where accno = '$sl_cash_acc'");
if ($sl_err) die($sl_err);
if (! $chart_id_cash) die("There is no COA entry for cash account '$sl_cash_acc'");
// Request all cash entries belonging to the invoice.
$atres = SLQuery("select * from acc_trans where trans_id = $trans_id and chart_id = $chart_id_cash");
if ($sl_err) die($sl_err);
// Deduct payments for each procedure code from the respective balance owed.
$keysuffix = 5000;
for ($irow = 0; $irow < SLRowCount($atres); ++$irow) {
$row = SLGetRow($atres, $irow);
$code = strtoupper($row['memo']);
$ins_id = $row['project_id'];
if (! $code) $code = "Unknown";
$amount = $row['amount'];
$codes[$code]['bal'] += $amount; // amount is negative for a payment
if ($ins_id)
$codes[$code]['ins'] = $ins_id;
// Add the details if they want 'em.
if ($with_detail) {
if (! $codes[$code]['dtl']) $codes[$code]['dtl'] = array();
$tmpkey = $row['transdate'] . $keysuffix++;
$tmp = array();
$tmp['pmt'] = 0 - $amount;
$tmp['src'] = $row['source'];
if ($ins_id) $tmp['ins'] = $ins_id;
$codes[$code]['dtl'][$tmpkey] = $tmp;
}
}
// Request all line items with money or adjustment reasons belonging
// to the invoice.
$inres = SLQuery("SELECT * FROM invoice WHERE trans_id = $trans_id AND " .
"( sellprice != 0 OR description LIKE 'Adjustment%' OR serialnumber = 'Claim' )");
if ($sl_err) die($sl_err);
// Add charges and adjustments for each procedure code into its total and balance.
$keysuffix = 1000;
for ($irow = 0; $irow < SLRowCount($inres); ++$irow) {
$row = SLGetRow($inres, $irow);
// $amount = $row['sellprice'];
$amount = sprintf('%01.2f', $row['sellprice'] * $row['qty']);
$ins_id = $row['project_id'];
$code = "Unknown";
if ($row['serialnumber'] == 'Claim') {
$code = 'Claim';
}
else if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['serialnumber'], $matches)) {
$code = strtoupper($matches[1]);
}
else if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['description'], $matches)) {
$code = strtoupper($matches[1]);
}
else if (preg_match("/^TAX:/", $row['description'])) {
$code = 'TAX';
}
$codes[$code]['chg'] += $amount;
$codes[$code]['bal'] += $amount;
if ($amount < 0) $codes[$code]['adj'] -= $amount;
if ($ins_id)
$codes[$code]['ins'] = $ins_id;
// Add the details if they want 'em.
if ($with_detail) {
if (! $codes[$code]['dtl']) $codes[$code]['dtl'] = array();
if (preg_match("/^Adjustment\s*(\S*)\s*(.*)/", $row['description'], $matches)) {
$tmpkey = str_pad($matches[1], 10) . $keysuffix++;
$tmp = array();
$tmp['chg'] = $amount;
$tmp['rsn'] = $matches[2];
if ($ins_id) $tmp['ins'] = $ins_id;
$codes[$code]['dtl'][$tmpkey] = $tmp;
}
else {
$tmpkey = " " . $keysuffix++;
$tmp = array();
$tmp['chg'] = $amount;
if ($code == 'TAX') $tmp['dsc'] = substr($row['description'], 4);
$codes[$code]['dtl'][$tmpkey] = $tmp;
}
}
}
return $codes;
}
// Like the above, but for Integrated A/R.
//
function ar_get_invoice_summary($patient_id, $encounter_id, $with_detail = false) {
$codes = array();
$keysuff1 = 1000;
$keysuff2 = 5000;
// Get charges from services.
$res = sqlStatement("SELECT " .
"date, code_type, code, modifier, code_text, fee " .
"FROM billing WHERE " .
"pid = ? AND encounter = ? AND " .
"activity = 1 AND fee != 0.00 ORDER BY id", array($patient_id,$encounter_id) );
while ($row = sqlFetchArray($res)) {
$amount = sprintf('%01.2f', $row['fee']);
$code = $row['code'];
if (! $code) $code = "Unknown";
if ($row['modifier']) $code .= ':' . $row['modifier'];
$codes[$code]['chg'] += $amount;
$codes[$code]['bal'] += $amount;
// Pass the code type, code and code_text fields
// Although not all used yet, useful information
// to improve the statement reporting etc.
$codes[$code]['code_type'] = $row['code_type'];
$codes[$code]['code_value'] = $row['code'];
$codes[$code]['modifier'] = $row['modifier'];
$codes[$code]['code_text'] = $row['code_text'];
// Add the details if they want 'em.
if ($with_detail) {
if (! $codes[$code]['dtl']) $codes[$code]['dtl'] = array();
$tmp = array();
$tmp['chg'] = $amount;
$tmpkey = " " . $keysuff1++;
$codes[$code]['dtl'][$tmpkey] = $tmp;
}
}
// Get charges from product sales.
$query = "SELECT s.drug_id, s.sale_date, s.fee, s.quantity " .
"FROM drug_sales AS s " .
"WHERE " .
"s.pid = ? AND s.encounter = ? AND s.fee != 0 " .
"ORDER BY s.sale_id";
$res = sqlStatement($query, array($patient_id,$encounter_id) );
while ($row = sqlFetchArray($res)) {
$amount = sprintf('%01.2f', $row['fee']);
$code = 'PROD:' . $row['drug_id'];
$codes[$code]['chg'] += $amount;
$codes[$code]['bal'] += $amount;
// Add the details if they want 'em.
if ($with_detail) {
if (! $codes[$code]['dtl']) $codes[$code]['dtl'] = array();
$tmp = array();
$tmp['chg'] = $amount;
$tmpkey = " " . $keysuff1++;
$codes[$code]['dtl'][$tmpkey] = $tmp;
}
}
// Get payments and adjustments. (includes copays)
$res = sqlStatement("SELECT " .
"a.code_type, a.code, a.modifier, a.memo, a.payer_type, a.adj_amount, a.pay_amount, a.reason_code, " .
"a.post_time, a.session_id, a.sequence_no, a.account_code, " .
"s.payer_id, s.reference, s.check_date, s.deposit_date " .
",i.name " .
"FROM ar_activity AS a " .
"LEFT OUTER JOIN ar_session AS s ON s.session_id = a.session_id " .
"LEFT OUTER JOIN insurance_companies AS i ON i.id = s.payer_id " .
"WHERE a.pid = ? AND a.encounter = ? " .
"ORDER BY s.check_date, a.sequence_no", array($patient_id,$encounter_id) );
while ($row = sqlFetchArray($res)) {
$code = $row['code'];
if (! $code) $code = "Unknown";
if ($row['modifier']) $code .= ':' . $row['modifier'];
$ins_id = 0 + $row['payer_id'];
$codes[$code]['bal'] -= $row['pay_amount'];
$codes[$code]['bal'] -= $row['adj_amount'];
$codes[$code]['chg'] -= $row['adj_amount'];
$codes[$code]['adj'] += $row['adj_amount'];
if ($ins_id) $codes[$code]['ins'] = $ins_id;
// Add the details if they want 'em.
if ($with_detail) {
if (! $codes[$code]['dtl']) $codes[$code]['dtl'] = array();
$tmp = array();
$paydate = empty($row['deposit_date']) ? substr($row['post_time'], 0, 10) : $row['deposit_date'];
if ($row['pay_amount'] != 0) $tmp['pmt'] = $row['pay_amount'];
if ( isset($row['reason_code'] ) ) {
$tmp['msp'] = $row['reason_code'];
}
if ($row['adj_amount'] != 0 || $row['pay_amount'] == 0) {
$tmp['chg'] = 0 - $row['adj_amount'];
// $tmp['rsn'] = (empty($row['memo']) || empty($row['session_id'])) ? 'Unknown adjustment' : $row['memo'];
$tmp['rsn'] = empty($row['memo']) ? 'Unknown adjustment' : $row['memo'];
$tmpkey = $paydate . $keysuff1++;
}
else {
$tmpkey = $paydate . $keysuff2++;
}
if ($row['account_code'] == "PCP") {
//copay
$tmp['src'] = 'Pt Paid';
}
else {
$tmp['src'] = empty($row['session_id']) ? $row['memo'] : $row['reference'];
}
$tmp['insurance_company'] = substr($row['name'], 0, 10);
if ($ins_id) $tmp['ins'] = $ins_id;
$tmp['plv'] = $row['payer_type'];
$tmp['arseq'] = $row['sequence_no'];
$codes[$code]['dtl'][$tmpkey] = $tmp;
}
}
return $codes;
}
// This determines the party from whom payment is currently expected.
// Returns: -1=Nobody, 0=Patient, 1=Ins1, 2=Ins2, 3=Ins3.
//
function responsible_party($trans_id) {
global $sl_err;
$arres = SLQuery("select * from ar where id = $trans_id");
if ($sl_err) die($sl_err);
$arrow = SLGetRow($arres, 0);
if (! $arrow) die(xl("There is no match for invoice id = ") . $trans_id);
if ($arrow['paid'] >= $arrow['netamount']) return -1;
$insgot = strtolower($arrow['notes']);
$insdone = strtolower($arrow['shipvia']);
for ($i = 1; $i <= 3; ++$i) {
$lcvalue = "ins$i";
if (strpos($insgot, $lcvalue) !== false && strpos($insdone, $lcvalue) === false)
return $i;
}
return 0;
}
// As above but for Integrated A/R.
//
function ar_responsible_party($patient_id, $encounter_id) {
$row = sqlQuery("SELECT date, last_level_billed, last_level_closed " .
"FROM form_encounter WHERE " .
"pid = ? AND encounter = ? " .
"ORDER BY id DESC LIMIT 1", array($patient_id,$encounter_id) );
if (empty($row)) return -1;
$next_level = $row['last_level_closed'] + 1;
if ($next_level <= $row['last_level_billed'])
return $next_level;
if (arGetPayerID($patient_id, substr($row['date'], 0, 10), $next_level))
return $next_level;
// There is no unclosed insurance, so see if there is an unpaid balance.
// Currently hoping that form_encounter.balance_due can be discarded.
$balance = 0;
$codes = ar_get_invoice_summary($patient_id, $encounter_id);
foreach ($codes as $cdata) $balance += $cdata['bal'];
if ($balance > 0) return 0;
return -1;
}
?>