2 // Copyright (C) 2005 Rod Roark <rod@sunsetsystems.com>
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;
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.
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
52 $codes[$code]['ins'] = $ins_id;
54 // Add the details if they want 'em.
56 if (! $codes[$code]['dtl']) $codes[$code]['dtl'] = array();
57 $tmpkey = $row['transdate'] . $keysuffix++
;
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
67 $inres = SLQuery("SELECT * FROM invoice WHERE trans_id = $trans_id AND " .
68 "( sellprice != 0 OR description LIKE 'Adjustment%' OR serialnumber = 'Claim' )");
69 if ($sl_err) die($sl_err);
71 // Add charges and adjustments for each procedure code into its total and balance.
73 for ($irow = 0; $irow < SLRowCount($inres); ++
$irow) {
74 $row = SLGetRow($inres, $irow);
75 $amount = $row['sellprice'];
76 $ins_id = $row['project_id'];
79 if ($row['serialnumber'] == 'Claim') {
82 else if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['serialnumber'], $matches)) {
83 $code = strtoupper($matches[1]);
85 else if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['description'], $matches)) {
86 $code = strtoupper($matches[1]);
89 $codes[$code]['chg'] +
= $amount;
90 $codes[$code]['bal'] +
= $amount;
91 if ($amount < 0) $codes[$code]['adj'] -= $amount;
94 $codes[$code]['ins'] = $ins_id;
96 // Add the details if they want 'em.
98 if (! $codes[$code]['dtl']) $codes[$code]['dtl'] = array();
99 if (preg_match("/^Adjustment\s*(\S*)\s*(.*)/", $row['description'], $matches)) {
100 $tmpkey = str_pad($matches[1], 10) . $keysuffix++
;
102 $tmp['chg'] = $amount;
103 $tmp['rsn'] = $matches[2];
104 $codes[$code]['dtl'][$tmpkey] = $tmp;
107 $tmpkey = " " . $keysuffix++
;
109 $tmp['chg'] = $amount;
110 $codes[$code]['dtl'][$tmpkey] = $tmp;