added patient statements
[openemr.git] / library / invoice_summary.inc.php
blob2e0b88e3a157c9f8ee0ab1083bcd62111a0f7957
1 <?php
2 // Copyright (C) 2005 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.
10 // Its values are associative arrays having the following keys:
11 // chg - the sum of line items, including adjustments, for the code
12 // bal - the unpaid balance
13 // ins - the id of the insurance company that was billed
15 function get_invoice_summary($trans_id) {
16 global $sl_err, $sl_cash_acc;
18 $codes = array();
20 $chart_id_cash = SLQueryValue("select id from chart where accno = '$sl_cash_acc'");
21 if ($sl_err) die($sl_err);
22 if (! $chart_id_cash) die("There is no COA entry for cash account '$sl_cash_acc'");
24 // Request all cash entries belonging to the invoice.
25 $atres = SLQuery("select * from acc_trans where trans_id = $trans_id and chart_id = $chart_id_cash");
26 if ($sl_err) die($sl_err);
28 // Deduct payments for each procedure code from the respective balance owed.
29 for ($irow = 0; $irow < SLRowCount($atres); ++$irow) {
30 $row = SLGetRow($atres, $irow);
31 $code = strtoupper($row['memo']);
32 $ins_id = $row['project_id'];
33 if (! $code) $code = "Unknown";
34 $amount = $row['amount'];
35 $codes[$code]['bal'] += $amount; // amount is negative for a payment
36 if ($ins_id)
37 $codes[$code]['ins'] = $ins_id;
40 // Request all line items with money belonging to the invoice.
41 $inres = SLQuery("select * from invoice where trans_id = $trans_id and sellprice != 0");
42 if ($sl_err) die($sl_err);
44 // Add charges and adjustments for each procedure code into its total and balance.
45 for ($irow = 0; $irow < SLRowCount($inres); ++$irow) {
46 $row = SLGetRow($inres, $irow);
47 $amount = $row['sellprice'];
48 $ins_id = $row['project_id'];
50 $code = "Unknown";
51 if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['serialnumber'], $matches)) {
52 $code = strtoupper($matches[1]);
54 else if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['description'], $matches)) {
55 $code = strtoupper($matches[1]);
58 $codes[$code]['chg'] += $amount;
59 $codes[$code]['bal'] += $amount;
61 if ($ins_id)
62 $codes[$code]['ins'] = $ins_id;
65 return $codes;