dump db version
[openemr.git] / library / invoice_summary.inc.php
blobf84e963a0fd3f28f8e4e3303e0f4470f69c56670
1 <?php
2 /**
3 * @package OpenEMR
4 * @author Rod Roark <rod@sunsetsystems.com>
5 * @author Stephen Waite <stephen.waite@cmsvt.com>
6 * @copyright Copyright (c) 2005-2010 Rod Roark <rod@sunsetsystems.com>
7 * @copyright Copyright (c) 2018 Stephen Waite <stephen.waite@cmsvt.com>
8 * @link http://www.open-emr.org
9 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
12 // This returns an associative array keyed on procedure code, representing
13 // all charge items for one invoice. This array's values are themselves
14 // associative arrays having the following keys:
16 // chg - the sum of line items, including adjustments, for the code
17 // bal - the unpaid balance
18 // adj - the (positive) sum of inverted adjustments
19 // ins - the id of the insurance company that was billed (obsolete)
20 // dtl - associative array of details, if requested
22 // Where details are requested, each dtl array is keyed on a string
23 // beginning with a date in yyyy-mm-dd format, or blanks in the case
24 // of the original charge items. The value array is:
26 // pmt - payment amount as a positive number, only for payments
27 // src - check number or other source, only for payments
28 // chg - invoice line item amount amount, only for charges or
29 // adjustments (adjustments may be zero)
30 // rsn - adjustment reason, only for adjustments
31 // plv - provided for "integrated A/R" only: 0=pt, 1=Ins1, etc.
32 // dsc - for tax charges, a description of the tax
33 // arseq - ar_activity.sequence_no when it applies.
35 require_once(dirname(__FILE__) . "/../custom/code_types.inc.php");
37 use OpenEMR\Billing\SLEOB;
39 // for Integrated A/R.
41 function ar_get_invoice_summary($patient_id, $encounter_id, $with_detail = false)
43 $codes = array();
44 $keysuff1 = 1000;
45 $keysuff2 = 5000;
47 // Get charges from services.
48 $res = sqlStatement("SELECT " .
49 "date, code_type, code, modifier, code_text, fee " .
50 "FROM billing WHERE " .
51 "pid = ? AND encounter = ? AND " .
52 "activity = 1 AND fee != 0.00 ORDER BY id", array($patient_id,$encounter_id));
54 while ($row = sqlFetchArray($res)) {
55 $amount = sprintf('%01.2f', $row['fee']);
57 $code = $row['code'];
58 if (! $code) {
59 $code = "Unknown";
62 if ($row['modifier']) {
63 $code .= ':' . $row['modifier'];
66 $codes[$code]['chg'] += $amount;
67 $codes[$code]['bal'] += $amount;
69 // Pass the code type, code and code_text fields
70 // Although not all used yet, useful information
71 // to improve the statement reporting etc.
72 $codes[$code]['code_type'] = $row['code_type'];
73 $codes[$code]['code_value'] = $row['code'];
74 $codes[$code]['modifier'] = $row['modifier'];
75 $codes[$code]['code_text'] = $row['code_text'];
77 // Add the details if they want 'em.
78 if ($with_detail) {
79 if (! $codes[$code]['dtl']) {
80 $codes[$code]['dtl'] = array();
83 $tmp = array();
84 $tmp['chg'] = $amount;
85 $tmpkey = " " . $keysuff1++;
86 $codes[$code]['dtl'][$tmpkey] = $tmp;
90 // Get charges from product sales.
91 $query = "SELECT s.drug_id, s.sale_date, s.fee, s.quantity " .
92 "FROM drug_sales AS s " .
93 "WHERE " .
94 "s.pid = ? AND s.encounter = ? AND s.fee != 0 " .
95 "ORDER BY s.sale_id";
96 $res = sqlStatement($query, array($patient_id,$encounter_id));
97 while ($row = sqlFetchArray($res)) {
98 $amount = sprintf('%01.2f', $row['fee']);
99 $code = 'PROD:' . $row['drug_id'];
100 $codes[$code]['chg'] += $amount;
101 $codes[$code]['bal'] += $amount;
102 // Add the details if they want 'em.
103 if ($with_detail) {
104 if (! $codes[$code]['dtl']) {
105 $codes[$code]['dtl'] = array();
108 $tmp = array();
109 $tmp['chg'] = $amount;
110 $tmpkey = " " . $keysuff1++;
111 $codes[$code]['dtl'][$tmpkey] = $tmp;
114 // Get insurance data for stuff
115 $ins_data = array();
116 $res = sqlStatement("SELECT insurance_data.type as type, insurance_companies.name as name " .
117 "FROM insurance_data " .
118 "INNER JOIN insurance_companies ON insurance_data.provider = insurance_companies.id " .
119 "WHERE insurance_data.pid = ?", array($patient_id));
120 while ($row = sqlFetchArray($res)) {
121 $ins_data[$row['type']] = $row['name'];
123 // Get payments and adjustments. (includes copays)
124 $res = sqlStatement("SELECT " .
125 "a.code_type, a.code, a.modifier, a.memo, a.payer_type, a.adj_amount, a.pay_amount, a.reason_code, " .
126 "a.post_time, a.session_id, a.sequence_no, a.account_code, " .
127 "s.payer_id, s.reference, s.check_date, s.deposit_date " .
128 ",i.name " .
129 "FROM ar_activity AS a " .
130 "LEFT OUTER JOIN ar_session AS s ON s.session_id = a.session_id " .
131 "LEFT OUTER JOIN insurance_companies AS i ON i.id = s.payer_id " .
132 "WHERE a.pid = ? AND a.encounter = ? " .
133 "ORDER BY s.check_date, a.sequence_no", array($patient_id,$encounter_id));
134 while ($row = sqlFetchArray($res)) {
135 $code = $row['code'];
136 if (! $code) {
137 $code = "Unknown";
140 if ($row['modifier']) {
141 $code .= ':' . $row['modifier'];
144 $ins_id = 0 + $row['payer_id'];
145 $codes[$code]['bal'] -= $row['pay_amount'];
146 $codes[$code]['bal'] -= $row['adj_amount'];
147 $codes[$code]['chg'] -= $row['adj_amount'];
148 $codes[$code]['adj'] += $row['adj_amount'];
149 if ($ins_id) {
150 $codes[$code]['ins'] = $ins_id;
153 // Add the details if they want 'em.
154 if ($with_detail) {
155 if (! $codes[$code]['dtl']) {
156 $codes[$code]['dtl'] = array();
159 $tmp = array();
160 $paydate = empty($row['deposit_date']) ? substr($row['post_time'], 0, 10) : $row['deposit_date'];
161 if ($row['pay_amount'] != 0) {
162 $tmp['pmt'] = $row['pay_amount'];
165 if (isset($row['reason_code'])) {
166 $tmp['msp'] = $row['reason_code'];
169 if ($row['adj_amount'] != 0 || $row['pay_amount'] == 0) {
170 $tmp['chg'] = 0 - $row['adj_amount'];
171 // $tmp['rsn'] = (empty($row['memo']) || empty($row['session_id'])) ? 'Unknown adjustment' : $row['memo'];
172 $tmp['rsn'] = empty($row['memo']) ? 'Unknown adjustment' : $row['memo'];
173 $tmp['rsn'] = str_replace("Ins1", $ins_data['primary'], $tmp['rsn']);
174 $tmp['rsn'] = str_replace("Ins2", $ins_data['secondary'], $tmp['rsn']);
175 $tmp['rsn'] = str_replace("Ins3", $ins_data['tertiary'], $tmp['rsn']);
176 $tmpkey = $paydate . $keysuff1++;
177 } else {
178 $tmpkey = $paydate . $keysuff2++;
181 if ($row['account_code'] == "PCP") {
182 //copay
183 $tmp['src'] = 'Pt Paid';
184 } else {
185 $tmp['src'] = empty($row['session_id']) ? $row['memo'] : $row['reference'];
188 $tmp['insurance_company'] = substr($row['name'], 0, 10);
189 if ($ins_id) {
190 $tmp['ins'] = $ins_id;
193 $tmp['plv'] = $row['payer_type'];
194 $tmp['arseq'] = $row['sequence_no'];
195 $codes[$code]['dtl'][$tmpkey] = $tmp;
199 return $codes;
202 // This determines the party from whom payment is currently expected.
203 // Returns: -1=Nobody, 0=Patient, 1=Ins1, 2=Ins2, 3=Ins3.
204 // for Integrated A/R.
206 function ar_responsible_party($patient_id, $encounter_id)
208 $row = sqlQuery("SELECT date, last_level_billed, last_level_closed " .
209 "FROM form_encounter WHERE " .
210 "pid = ? AND encounter = ? " .
211 "ORDER BY id DESC LIMIT 1", array($patient_id,$encounter_id));
212 if (empty($row)) {
213 return -1;
216 $next_level = $row['last_level_closed'] + 1;
217 if ($next_level <= $row['last_level_billed']) {
218 return $next_level;
221 if (SLEOB::arGetPayerID($patient_id, substr($row['date'], 0, 10), $next_level)) {
222 return $next_level;
225 // There is no unclosed insurance, so see if there is an unpaid balance.
226 // Currently hoping that form_encounter.balance_due can be discarded.
227 $balance = 0;
228 $codes = ar_get_invoice_summary($patient_id, $encounter_id);
229 foreach ($codes as $cdata) {
230 $balance += $cdata['bal'];
233 if ($balance > 0) {
234 return 0;
237 return -1;