added drug templates
[openemr.git] / library / invoice_summary.inc.php
blob4beaf111e4116520677874fb067e50ea2d3bd13d
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:
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
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
29 function get_invoice_summary($trans_id, $with_detail = false) {
30 global $sl_err, $sl_cash_acc;
32 $codes = array();
34 $chart_id_cash = SLQueryValue("select id from chart where accno = '$sl_cash_acc'");
35 if ($sl_err) die($sl_err);
36 if (! $chart_id_cash) die("There is no COA entry for cash account '$sl_cash_acc'");
38 // Request all cash entries belonging to the invoice.
39 $atres = SLQuery("select * from acc_trans where trans_id = $trans_id and chart_id = $chart_id_cash");
40 if ($sl_err) die($sl_err);
42 // Deduct payments for each procedure code from the respective balance owed.
43 $keysuffix = 5000;
44 for ($irow = 0; $irow < SLRowCount($atres); ++$irow) {
45 $row = SLGetRow($atres, $irow);
46 $code = strtoupper($row['memo']);
47 $ins_id = $row['project_id'];
48 if (! $code) $code = "Unknown";
49 $amount = $row['amount'];
50 $codes[$code]['bal'] += $amount; // amount is negative for a payment
51 if ($ins_id)
52 $codes[$code]['ins'] = $ins_id;
54 // Add the details if they want 'em.
55 if ($with_detail) {
56 if (! $codes[$code]['dtl']) $codes[$code]['dtl'] = array();
57 $tmpkey = $row['transdate'] . $keysuffix++;
58 $tmp = array();
59 $tmp['pmt'] = 0 - $amount;
60 $tmp['src'] = $row['source'];
61 $codes[$code]['dtl'][$tmpkey] = $tmp;
65 // Request all line items with money or adjustment reasons belonging
66 // to the invoice.
67 $inres = SLQuery("SELECT * FROM invoice WHERE trans_id = $trans_id AND " .
68 "( sellprice != 0 OR description LIKE 'Adjustment%' )");
69 if ($sl_err) die($sl_err);
71 // Add charges and adjustments for each procedure code into its total and balance.
72 $keysuffix = 1000;
73 for ($irow = 0; $irow < SLRowCount($inres); ++$irow) {
74 $row = SLGetRow($inres, $irow);
75 $amount = $row['sellprice'];
76 $ins_id = $row['project_id'];
78 $code = "Unknown";
79 if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['serialnumber'], $matches)) {
80 $code = strtoupper($matches[1]);
82 else if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['description'], $matches)) {
83 $code = strtoupper($matches[1]);
86 $codes[$code]['chg'] += $amount;
87 $codes[$code]['bal'] += $amount;
88 if ($amount < 0) $codes[$code]['adj'] -= $amount;
90 if ($ins_id)
91 $codes[$code]['ins'] = $ins_id;
93 // Add the details if they want 'em.
94 if ($with_detail) {
95 if (! $codes[$code]['dtl']) $codes[$code]['dtl'] = array();
96 if (preg_match("/^Adjustment\s*(\S*)\s*(.*)/", $row['description'], $matches)) {
97 $tmpkey = str_pad($matches[1], 10) . $keysuffix++;
98 $tmp = array();
99 $tmp['chg'] = $amount;
100 $tmp['rsn'] = $matches[2];
101 $codes[$code]['dtl'][$tmpkey] = $tmp;
103 else {
104 $tmpkey = " " . $keysuffix++;
105 $tmp = array();
106 $tmp['chg'] = $amount;
107 $codes[$code]['dtl'][$tmpkey] = $tmp;
112 return $codes;