added appointments-encounters report
[openemr.git] / library / invoice_summary.inc.php
blobd323c3e4794d13eb6f4f8f888ed1b2de86caff3c
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, representing
10 // all charge items for one invoice. This array's values are themselves
11 // associative arrays having the following keys:
12 // chg - the sum of line items, including adjustments, for the code
13 // bal - the unpaid balance
14 // ins - the id of the insurance company that was billed
16 function get_invoice_summary($trans_id) {
17 global $sl_err, $sl_cash_acc;
19 $codes = array();
21 $chart_id_cash = SLQueryValue("select id from chart where accno = '$sl_cash_acc'");
22 if ($sl_err) die($sl_err);
23 if (! $chart_id_cash) die("There is no COA entry for cash account '$sl_cash_acc'");
25 // Request all cash entries belonging to the invoice.
26 $atres = SLQuery("select * from acc_trans where trans_id = $trans_id and chart_id = $chart_id_cash");
27 if ($sl_err) die($sl_err);
29 // Deduct payments for each procedure code from the respective balance owed.
30 for ($irow = 0; $irow < SLRowCount($atres); ++$irow) {
31 $row = SLGetRow($atres, $irow);
32 $code = strtoupper($row['memo']);
33 $ins_id = $row['project_id'];
34 if (! $code) $code = "Unknown";
35 $amount = $row['amount'];
36 $codes[$code]['bal'] += $amount; // amount is negative for a payment
37 if ($ins_id)
38 $codes[$code]['ins'] = $ins_id;
41 // Request all line items with money belonging to the invoice.
42 $inres = SLQuery("select * from invoice where trans_id = $trans_id and sellprice != 0");
43 if ($sl_err) die($sl_err);
45 // Add charges and adjustments for each procedure code into its total and balance.
46 for ($irow = 0; $irow < SLRowCount($inres); ++$irow) {
47 $row = SLGetRow($inres, $irow);
48 $amount = $row['sellprice'];
49 $ins_id = $row['project_id'];
51 $code = "Unknown";
52 if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['serialnumber'], $matches)) {
53 $code = strtoupper($matches[1]);
55 else if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['description'], $matches)) {
56 $code = strtoupper($matches[1]);
59 $codes[$code]['chg'] += $amount;
60 $codes[$code]['bal'] += $amount;
62 if ($ins_id)
63 $codes[$code]['ins'] = $ins_id;
66 return $codes;