another minor fix to prior commit
[openemr.git] / library / invoice_summary.inc.php
blobf727b74da818fffdb0390f0191b5a862164cfc79
1 <?php
2 // Copyright (C) 2005-2010 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 // This returns an associative array keyed on procedure code, representing
10 // all charge items for one invoice. This array's values are themselves
11 // associative arrays having the following keys:
13 // chg - the sum of line items, including adjustments, for the code
14 // bal - the unpaid balance
15 // adj - the (positive) sum of inverted adjustments
16 // ins - the id of the insurance company that was billed (obsolete)
17 // dtl - associative array of details, if requested
19 // Where details are requested, each dtl array is keyed on a string
20 // beginning with a date in yyyy-mm-dd format, or blanks in the case
21 // of the original charge items. The value array is:
23 // pmt - payment amount as a positive number, only for payments
24 // src - check number or other source, only for payments
25 // chg - invoice line item amount amount, only for charges or
26 // adjustments (adjustments may be zero)
27 // rsn - adjustment reason, only for adjustments
28 // plv - provided for "integrated A/R" only: 0=pt, 1=Ins1, etc.
29 // dsc - for tax charges, a description of the tax
30 // arseq - ar_activity.sequence_no when it applies.
32 require_once("sl_eob.inc.php");
33 require_once(dirname(__FILE__) . "/../custom/code_types.inc.php");
36 // for Integrated A/R.
38 function ar_get_invoice_summary($patient_id, $encounter_id, $with_detail = false) {
39 $codes = array();
40 $keysuff1 = 1000;
41 $keysuff2 = 5000;
43 // Get charges from services.
44 $res = sqlStatement("SELECT " .
45 "date, code_type, code, modifier, code_text, fee " .
46 "FROM billing WHERE " .
47 "pid = ? AND encounter = ? AND " .
48 "activity = 1 AND fee != 0.00 ORDER BY id", array($patient_id,$encounter_id) );
50 while ($row = sqlFetchArray($res)) {
51 $amount = sprintf('%01.2f', $row['fee']);
53 $code = $row['code'];
54 if (! $code) $code = "Unknown";
55 if ($row['modifier']) $code .= ':' . $row['modifier'];
56 $codes[$code]['chg'] += $amount;
57 $codes[$code]['bal'] += $amount;
59 // Pass the code type, code and code_text fields
60 // Although not all used yet, useful information
61 // to improve the statement reporting etc.
62 $codes[$code]['code_type'] = $row['code_type'];
63 $codes[$code]['code_value'] = $row['code'];
64 $codes[$code]['modifier'] = $row['modifier'];
65 $codes[$code]['code_text'] = $row['code_text'];
67 // Add the details if they want 'em.
68 if ($with_detail) {
69 if (! $codes[$code]['dtl']) $codes[$code]['dtl'] = array();
70 $tmp = array();
71 $tmp['chg'] = $amount;
72 $tmpkey = " " . $keysuff1++;
73 $codes[$code]['dtl'][$tmpkey] = $tmp;
77 // Get charges from product sales.
78 $query = "SELECT s.drug_id, s.sale_date, s.fee, s.quantity " .
79 "FROM drug_sales AS s " .
80 "WHERE " .
81 "s.pid = ? AND s.encounter = ? AND s.fee != 0 " .
82 "ORDER BY s.sale_id";
83 $res = sqlStatement($query, array($patient_id,$encounter_id) );
84 while ($row = sqlFetchArray($res)) {
85 $amount = sprintf('%01.2f', $row['fee']);
86 $code = 'PROD:' . $row['drug_id'];
87 $codes[$code]['chg'] += $amount;
88 $codes[$code]['bal'] += $amount;
89 // Add the details if they want 'em.
90 if ($with_detail) {
91 if (! $codes[$code]['dtl']) $codes[$code]['dtl'] = array();
92 $tmp = array();
93 $tmp['chg'] = $amount;
94 $tmpkey = " " . $keysuff1++;
95 $codes[$code]['dtl'][$tmpkey] = $tmp;
99 // Get payments and adjustments. (includes copays)
100 $res = sqlStatement("SELECT " .
101 "a.code_type, a.code, a.modifier, a.memo, a.payer_type, a.adj_amount, a.pay_amount, a.reason_code, " .
102 "a.post_time, a.session_id, a.sequence_no, a.account_code, " .
103 "s.payer_id, s.reference, s.check_date, s.deposit_date " .
104 ",i.name " .
105 "FROM ar_activity AS a " .
106 "LEFT OUTER JOIN ar_session AS s ON s.session_id = a.session_id " .
107 "LEFT OUTER JOIN insurance_companies AS i ON i.id = s.payer_id " .
108 "WHERE a.pid = ? AND a.encounter = ? " .
109 "ORDER BY s.check_date, a.sequence_no", array($patient_id,$encounter_id) );
110 while ($row = sqlFetchArray($res)) {
111 $code = $row['code'];
112 if (! $code) $code = "Unknown";
113 if ($row['modifier']) $code .= ':' . $row['modifier'];
114 $ins_id = 0 + $row['payer_id'];
115 $codes[$code]['bal'] -= $row['pay_amount'];
116 $codes[$code]['bal'] -= $row['adj_amount'];
117 $codes[$code]['chg'] -= $row['adj_amount'];
118 $codes[$code]['adj'] += $row['adj_amount'];
119 if ($ins_id) $codes[$code]['ins'] = $ins_id;
120 // Add the details if they want 'em.
121 if ($with_detail) {
122 if (! $codes[$code]['dtl']) $codes[$code]['dtl'] = array();
123 $tmp = array();
124 $paydate = empty($row['deposit_date']) ? substr($row['post_time'], 0, 10) : $row['deposit_date'];
125 if ($row['pay_amount'] != 0) $tmp['pmt'] = $row['pay_amount'];
126 if ( isset($row['reason_code'] ) ) {
127 $tmp['msp'] = $row['reason_code'];
129 if ($row['adj_amount'] != 0 || $row['pay_amount'] == 0) {
130 $tmp['chg'] = 0 - $row['adj_amount'];
131 // $tmp['rsn'] = (empty($row['memo']) || empty($row['session_id'])) ? 'Unknown adjustment' : $row['memo'];
132 $tmp['rsn'] = empty($row['memo']) ? 'Unknown adjustment' : $row['memo'];
133 $tmpkey = $paydate . $keysuff1++;
135 else {
136 $tmpkey = $paydate . $keysuff2++;
138 if ($row['account_code'] == "PCP") {
139 //copay
140 $tmp['src'] = 'Pt Paid';
142 else {
143 $tmp['src'] = empty($row['session_id']) ? $row['memo'] : $row['reference'];
145 $tmp['insurance_company'] = substr($row['name'], 0, 10);
146 if ($ins_id) $tmp['ins'] = $ins_id;
147 $tmp['plv'] = $row['payer_type'];
148 $tmp['arseq'] = $row['sequence_no'];
149 $codes[$code]['dtl'][$tmpkey] = $tmp;
152 return $codes;
155 // This determines the party from whom payment is currently expected.
156 // Returns: -1=Nobody, 0=Patient, 1=Ins1, 2=Ins2, 3=Ins3.
157 // for Integrated A/R.
159 function ar_responsible_party($patient_id, $encounter_id) {
160 $row = sqlQuery("SELECT date, last_level_billed, last_level_closed " .
161 "FROM form_encounter WHERE " .
162 "pid = ? AND encounter = ? " .
163 "ORDER BY id DESC LIMIT 1", array($patient_id,$encounter_id) );
164 if (empty($row)) return -1;
165 $next_level = $row['last_level_closed'] + 1;
166 if ($next_level <= $row['last_level_billed'])
167 return $next_level;
168 if (arGetPayerID($patient_id, substr($row['date'], 0, 10), $next_level))
169 return $next_level;
170 // There is no unclosed insurance, so see if there is an unpaid balance.
171 // Currently hoping that form_encounter.balance_due can be discarded.
172 $balance = 0;
173 $codes = ar_get_invoice_summary($patient_id, $encounter_id);
174 foreach ($codes as $cdata) $balance += $cdata['bal'];
175 if ($balance > 0) return 0;
176 return -1;