Fix for web site change courtesy of info from whimmel.
[openemr.git] / custom / statement.inc.php
blob2e4deefbc2051f07133ce74967da9be9503d13a2
1 <?php
3 // Copyright (C) 2005-2006 Rod Roark <rod@sunsetsystems.com>
4 //
5 // Windows compatibility mods 2009 Bill Cernansky [mi-squared.com]
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // Updated by Medical Information Integration, LLC to support download
13 // and multi OS use - tony@mi-squared..com 12-2009
15 //////////////////////////////////////////////////////////////////////
16 // This is a template for printing patient statements and collection
17 // letters. You must customize it to suit your practice. If your
18 // needs are simple then you do not need programming experience to do
19 // this - just read the comments and make appropriate substitutions.
20 // All you really need to do is replace the [strings in brackets].
21 //////////////////////////////////////////////////////////////////////
23 // The location/name of a temporary file to hold printable statements.
26 $STMT_TEMP_FILE = $GLOBALS['temporary_files_dir'] . "/openemr_statements.txt";
28 $STMT_PRINT_CMD = $GLOBALS['print_command'];
30 // This function builds a printable statement or collection letter from
31 // an associative array having the following keys:
33 // today = statement date yyyy-mm-dd
34 // pid = patient ID
35 // patient = patient name
36 // amount = total amount due
37 // to = array of addressee name/address lines
38 // lines = array of lines, each with the following keys:
39 // dos = date of service yyyy-mm-dd
40 // desc = description
41 // amount = charge less adjustments
42 // paid = amount paid
43 // notice = 1 for first notice, 2 for second, etc.
44 // detail = associative array of details
46 // Each detail array is keyed on a string beginning with a date in
47 // yyyy-mm-dd format, or blanks in the case of the original charge
48 // items. Its values are associative arrays like this:
50 // pmt - payment amount as a positive number, only for payments
51 // src - check number or other source, only for payments
52 // chg - invoice line item amount amount, only for charges or
53 // adjustments (adjustments may be zero)
54 // rsn - adjustment reason, only for adjustments
56 // The returned value is a string that can be sent to a printer.
57 // This example is plain text, but if you are a hotshot programmer
58 // then you could make a PDF or PostScript or whatever peels your
59 // banana. These strings are sent in succession, so append a form
60 // feed if that is appropriate.
63 // A sample of the text based format follows:
65 //[Your Clinic Name] Patient Name 2009-12-29
66 //[Your Clinic Address] Chart Number: 1848
67 //[City, State Zip] Insurance information on file
70 //ADDRESSEE REMIT TO
71 //Patient Name [Your Clinic Name]
72 //patient address [Your Clinic Address]
73 //city, state zipcode [City, State Zip]
74 // If paying by VISA/MC/AMEX/Dis
76 //Card_____________________ Exp______ Signature___________________
77 // Return above part with your payment
78 //-----------------------------------------------------------------
80 //_______________________ STATEMENT SUMMARY _______________________
82 //Visit Date Description Amount
84 //2009-08-20 Procedure 99345 198.90
85 // Paid 2009-12-15: -51.50
86 //... more details ...
87 //...
88 //...
89 // skipping blanks in example
92 //Name: Patient Name Date: 2009-12-29 Due: 147.40
93 //_________________________________________________________________
95 //Please call if any of the above information is incorrect
96 //We appreciate prompt payment of balances due
98 //[Your billing contact name]
99 // Billing Department
100 // [Your billing dept phone]
102 function create_statement($stmt) {
103 if (! $stmt['pid']) return ""; // get out if no data
105 // These are your clinics return address, contact etc. Edit them.
106 // TBD: read this from the facility table
108 // Facility (service location)
110 $clinic_name = '[Your Clinic Name]';
111 $clinic_addr = '[Your Clinic Address]';
112 $clinic_csz = '[City, State Zip]';
114 // Billing location
115 $remit_name = $clinic_name;
116 $remit_addr = $clinic_addr;
117 $remit_csz = $clinic_csz;
119 // Contacts
120 $billing_contact = '[Your billing contact name]';
121 $billing_phone = '[Your billing dept phone]';
123 // Text only labels
125 $label_addressee = xl('ADDRESSEE');
126 $label_remitto = xl('REMIT TO');
127 $label_chartnum = xl('Chart Number');
128 $label_insinfo = xl('Insurance information on file');
129 $label_totaldue = xl('Total amount due');
130 $label_payby = xl('If paying by');
131 $label_cards = xl('VISA/MC/AMEX/Dis');
132 $label_cardnum = xl('Card');
133 $label_expiry = xl('Exp');
134 $label_sign = xl('Signature');
135 $label_retpay = xl('Return above part with your payment');
136 $label_pgbrk = xl('STATEMENT SUMMARY');
137 $label_visit = xl('Visit Date');
138 $label_desc = xl('Description');
139 $label_amt = xl('Amount');
141 // This is the text for the top part of the page, up to but not
142 // including the detail lines. Some examples of variable fields are:
143 // %s = string with no minimum width
144 // %9s = right-justified string of 9 characters padded with spaces
145 // %-25s = left-justified string of 25 characters padded with spaces
146 // Note that "\n" is a line feed (new line) character.
147 // reformatted to handle i8n by tony
149 $out = sprintf("%-30s %-23s %-s\n",$clinic_name,$stmt['patient'],$stmt['today']);
150 $out .= sprintf("%-30s %s: %-s\n",$clinic_addr,$label_chartnum,$stmt['pid']);
151 $out .= sprintf("%-30s %-s\n",$clinic_csz,$label_insinfo);
152 $out .= sprintf("%-30s %s: %-s\n",null,$label_totaldue);
153 $out .= "\n\n";
154 $out .= sprintf("%-30s %-s\n",$label_addressee,$label_remitto);
155 $out .= sprintf("%-32s %s\n",$stmt['to'][0],$remit_name);
156 $out .= sprintf("%-32s %s\n",$stmt['to'][1],$remit_addr);
157 $out .= sprintf("%-32s %s\n",$stmt['to'][2],$remit_csz);
158 $out .= sprintf("%-32s %-s %-s\n",$stmt['to'][3],$label_payby,$label_cards);
159 $out .= "\n";
160 $out .= sprintf("%s_____________________ %s______ %s___________________\n",
161 $label_cardnum,$label_expiry,$label_sign);
162 $out .= sprintf("%-20s %s\n",null,$label_retpay);
163 $out .= sprintf("-----------------------------------------------------------------\n");
164 $out .= "\n";
165 $out .= sprintf("_______________________ %s _______________________\n",$label_pgbrk);
166 $out .= "\n";
167 $out .= sprintf("%-11s %-46s %s\n",$label_visit,$label_desc,$label_amt);
168 $out .= "\n";
170 // This must be set to the number of lines generated above.
172 $count = 21;
174 // This generates the detail lines. Again, note that the values must
175 // be specified in the order used.
177 foreach ($stmt['lines'] as $line) {
178 $description = $line['desc'];
179 $tmp = substr($description, 0, 14);
180 if ($tmp == 'Procedure 9920' || $tmp == 'Procedure 9921')
181 $description = xl('Office Visit');
183 $dos = $line['dos'];
184 ksort($line['detail']);
186 foreach ($line['detail'] as $dkey => $ddata) {
187 $ddate = substr($dkey, 0, 10);
188 if (preg_match('/^(\d\d\d\d)(\d\d)(\d\d)\s*$/', $ddate, $matches)) {
189 $ddate = $matches[1] . '-' . $matches[2] . '-' . $matches[3];
191 $amount = '';
193 if ($ddata['pmt']) {
194 $amount = sprintf("%.2f", 0 - $ddata['pmt']);
195 $desc = xl('Paid') .' '. $ddate .': '. $ddata['src'];
196 } else if ($ddata['rsn']) {
197 if ($ddata['chg']) {
198 $amount = sprintf("%.2f", $ddata['chg']);
199 $desc = xl('Adj') .' '. $ddate .': ' . $ddata['rsn'];
200 } else {
201 $desc = xl('Note') .' '. $ddate .': '. $ddata['rsn'];
203 } else if ($ddata['chg'] < 0) {
204 $amount = sprintf("%.2f", $ddata['chg']);
205 $desc = xl('Patient Payment');
206 } else {
207 $amount = sprintf("%.2f", $ddata['chg']);
208 $desc = $description;
211 $out .= sprintf("%-10s %-45s%8s\n", $dos, $desc, $amount);
212 $dos = '';
213 ++$count;
217 // This generates blank lines until we are at line 42.
219 while ($count++ < 42) $out .= "\n";
221 // Fixed text labels
222 $label_ptname = xl('Name');
223 $label_today = xl('Date');
224 $label_due = xl('Due');
225 $label_thanks = xl('Thank you for choosing');
226 $label_call = xl('Please call if any of the above information is incorrect');
227 $label_prompt = xl('We appreciate prompt payment of balances due');
228 $label_dept = xl('Billing Department');
230 // This is the bottom portion of the page.
232 $out .= sprintf("%-s: %-25s %-s: %-14s %-s: %8s\n",$label_ptname,$stmt['patient'],
233 $label_today,$stmt['today'],$label_due,$stmt['amount']);
234 $out .= sprintf("__________________________________________________________________\n");
235 $out .= "\n";
236 $out .= sprintf("%-s\n",$label_call);
237 $out .= sprintf("%-s\n",$label_prompt);
238 $out .= "\n";
239 $out .= sprintf("%-s\n",$billing_contact);
240 $out .= sprintf(" %-s\n",$label_dept);
241 $out .= sprintf(" %-s\n",$billing_phone);
242 $out .= "\014"; // this is a form feed
244 return $out;