file table.sql was added on branch rel-310 on 2009-07-31 19:39:59 +0000
[openemr.git] / library / invoice_summary.inc.php
blob4034ae0dd59e603436625c3b97956c0f95dab5e6
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
15 // dtl - associative array of details, if requested
17 function get_invoice_summary($trans_id, $with_detail = false) {
18 global $sl_err, $sl_cash_acc;
20 $codes = array();
22 $chart_id_cash = SLQueryValue("select id from chart where accno = '$sl_cash_acc'");
23 if ($sl_err) die($sl_err);
24 if (! $chart_id_cash) die("There is no COA entry for cash account '$sl_cash_acc'");
26 // Request all cash entries belonging to the invoice.
27 $atres = SLQuery("select * from acc_trans where trans_id = $trans_id and chart_id = $chart_id_cash");
28 if ($sl_err) die($sl_err);
30 // Deduct payments for each procedure code from the respective balance owed.
31 $keysuffix = 5000;
32 for ($irow = 0; $irow < SLRowCount($atres); ++$irow) {
33 $row = SLGetRow($atres, $irow);
34 $code = strtoupper($row['memo']);
35 $ins_id = $row['project_id'];
36 if (! $code) $code = "Unknown";
37 $amount = $row['amount'];
38 $codes[$code]['bal'] += $amount; // amount is negative for a payment
39 if ($ins_id)
40 $codes[$code]['ins'] = $ins_id;
42 // Add the details if they want 'em.
43 if ($with_detail) {
44 if (! $codes[$code]['dtl']) $codes[$code]['dtl'] = array();
45 $tmpkey = $row['transdate'] . $keysuffix++;
46 $tmp = array();
47 $tmp['pmt'] = 0 - $amount;
48 $tmp['src'] = $row['source'];
49 $codes[$code]['dtl'][$tmpkey] = $tmp;
53 // Request all line items with money belonging to the invoice.
54 $inres = SLQuery("select * from invoice where trans_id = $trans_id and sellprice != 0");
55 if ($sl_err) die($sl_err);
57 // Add charges and adjustments for each procedure code into its total and balance.
58 $keysuffix = 1000;
59 for ($irow = 0; $irow < SLRowCount($inres); ++$irow) {
60 $row = SLGetRow($inres, $irow);
61 $amount = $row['sellprice'];
62 $ins_id = $row['project_id'];
64 $code = "Unknown";
65 if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['serialnumber'], $matches)) {
66 $code = strtoupper($matches[1]);
68 else if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['description'], $matches)) {
69 $code = strtoupper($matches[1]);
72 $codes[$code]['chg'] += $amount;
73 $codes[$code]['bal'] += $amount;
75 if ($ins_id)
76 $codes[$code]['ins'] = $ins_id;
78 // Add the details if they want 'em.
79 if ($with_detail) {
80 if (! $codes[$code]['dtl']) $codes[$code]['dtl'] = array();
81 if (preg_match("/^Adjustment\s*(\S*)\s*(.*)/", $row['description'], $matches)) {
82 $tmpkey = str_pad($matches[1], 10) . $keysuffix++;
83 $tmp = array();
84 $tmp['chg'] = $amount;
85 $tmp['rsn'] = $matches[2];
86 $codes[$code]['dtl'][$tmpkey] = $tmp;
88 else {
89 $tmpkey = " " . $keysuffix++;
90 $tmp = array();
91 $tmp['chg'] = $amount;
92 $codes[$code]['dtl'][$tmpkey] = $tmp;
97 return $codes;