default pricelevel to standard
[openemr.git] / interface / patient_file / letter.php
blob84836efc064f38ce06104ebf44b16183e6c7b001
1 <?php
2 // Copyright (C) 2007 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 include_once("../globals.php");
10 include_once("$srcdir/patient.inc");
12 $template_dir = "$webserver_root/custom/letter_templates";
14 $patdata = sqlQuery("SELECT " .
15 "p.fname, p.mname, p.lname, p.pubpid, p.DOB " .
16 "FROM patient_data AS p " .
17 "WHERE p.pid = '$pid' LIMIT 1");
19 $alertmsg = ''; // anything here pops up in an alert box
21 // If the Generate button was clicked...
22 if ($_POST['form_generate']) {
23 $form_pid = $_POST['form_pid'];
24 $form_from = $_POST['form_from'];
25 $form_to = $_POST['form_to'];
26 $form_date = $_POST['form_date'];
27 $form_template = $_POST['form_template'];
28 $form_format = $_POST['form_format'];
29 $form_body = $_POST['form_body'];
31 // Set variables that depend on the output format.
32 $lang = 'PostScript';
33 $mimetype = 'application/postscript';
34 $postprocess = '';
35 if ($form_format == 'pdf') {
36 $mimetype = 'application/pdf';
37 $postprocess = ' | ps2pdf - -';
39 /****
40 else if ($form_format == 'html') {
41 $lang = 'html';
42 $mimetype = 'text/html';
44 else if ($form_format == 'rtf') {
45 $lang = 'rtf';
46 $mimetype = 'application/rtf';
48 ****/
50 $frow = sqlQuery("SELECT * FROM users WHERE id = '$form_from'");
51 $trow = sqlQuery("SELECT * FROM users WHERE id = '$form_to'");
53 ob_start();
55 $datestr = date('j F Y', strtotime($form_date));
56 $from_title = $frow['title'] ? $frow['title'] . ' ' : '';
57 $to_title = $trow['title'] ? $trow['title'] . ' ' : '';
59 // Create the temporary data file and process it with enscript.
60 $tmpfn = tempnam("/tmp", "oemr_letter");
61 $tmpfh = fopen($tmpfn, "w");
62 $cpstring = '';
63 $fh = fopen("$template_dir/$form_template", 'r');
64 while (!feof($fh)) $cpstring .= fread($fh, 8192);
65 fclose($fh);
66 $cpstring = str_replace('{DATE}' , $datestr, $cpstring);
67 $cpstring = str_replace('{FROM_TITLE}' , $from_title, $cpstring);
68 $cpstring = str_replace('{FROM_FNAME}' , $frow['fname'], $cpstring);
69 $cpstring = str_replace('{FROM_LNAME}' , $frow['lname'], $cpstring);
70 $cpstring = str_replace('{FROM_MNAME}' , $frow['mname'], $cpstring);
71 $cpstring = str_replace('{FROM_STREET}' , $frow['street'], $cpstring);
72 $cpstring = str_replace('{FROM_CITY}' , $frow['city'], $cpstring);
73 $cpstring = str_replace('{FROM_STATE}' , $frow['state'], $cpstring);
74 $cpstring = str_replace('{FROM_POSTAL}' , $frow['zip'], $cpstring);
75 $cpstring = str_replace('{FROM_VALEDICTORY}', $frow['valedictory'], $cpstring);
76 $cpstring = str_replace('{FROM_PHONECELL}' , $frow['phonecell'], $cpstring);
77 $cpstring = str_replace('{TO_TITLE}' , $to_title, $cpstring);
78 $cpstring = str_replace('{TO_FNAME}' , $trow['fname'], $cpstring);
79 $cpstring = str_replace('{TO_LNAME}' , $trow['lname'], $cpstring);
80 $cpstring = str_replace('{TO_MNAME}' , $trow['mname'], $cpstring);
81 $cpstring = str_replace('{TO_STREET}' , $trow['street'], $cpstring);
82 $cpstring = str_replace('{TO_CITY}' , $trow['city'], $cpstring);
83 $cpstring = str_replace('{TO_STATE}' , $trow['state'], $cpstring);
84 $cpstring = str_replace('{TO_POSTAL}' , $trow['zip'], $cpstring);
85 $cpstring = str_replace('{TO_VALEDICTORY}' , $trow['valedictory'], $cpstring);
86 $cpstring = str_replace('{TO_FAX}' , $trow['fax'], $cpstring);
87 $cpstring = str_replace('{TO_ORGANIZATION}' , $trow['organization'], $cpstring);
88 $cpstring = str_replace('{PT_FNAME}' , $patdata['fname'], $cpstring);
89 $cpstring = str_replace('{PT_LNAME}' , $patdata['lname'], $cpstring);
90 $cpstring = str_replace('{PT_MNAME}' , $patdata['mname'], $cpstring);
91 $cpstring = str_replace('{PT_DOB}' , $patdata['DOB'], $cpstring);
92 $cpstring = str_replace('{MESSAGE}' , $form_body, $cpstring);
93 fwrite($tmpfh, $cpstring);
94 fclose($tmpfh);
95 $tmp0 = passthru("cd $template_dir; enscript -M A4 -B -e^ " .
96 "--margins=54:54:54:18 --word-wrap -w $lang -o - '$tmpfn'$postprocess");
97 unlink($tmpfn);
99 header("Pragma: public");
100 header("Expires: 0");
101 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
102 header("Content-Type: $mimetype");
103 header("Content-Length: " . ob_get_length());
104 header("Content-Disposition: inline; filename=letter.$form_format");
106 ob_end_flush();
108 exit;
111 // This is the case where we display the form for data entry.
113 // Get the users list.
114 $ures = sqlStatement("SELECT id, fname, lname, specialty FROM users " .
115 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
116 "ORDER BY lname, fname");
117 $i = 0;
118 $optfrom = '';
119 $optto = '';
120 $ulist = "var ulist = new Array();\n";
121 while ($urow = sqlFetchArray($ures)) {
122 $uname = $urow['lname'];
123 if ($urow['fname']) $uname .= ", " . $urow['fname'];
124 $tmp1 = " <option value='" . $urow['id'] . "'";
125 $tmp2 = ">$uname</option>\n";
126 $optto .= $tmp1 . $tmp2;
127 if ($urow['id'] == $_SESSION['authUserID']) $tmp1 .= " selected";
128 $optfrom .= $tmp1 . $tmp2;
129 $ulist .= "ulist[$i] = '" . addslashes($uname) . "|" .
130 $urow['id'] . "|" . addslashes($urow['specialty']) . "';\n";
131 ++$i;
134 // Get the unique specialties.
135 $sres = sqlStatement("SELECT DISTINCT specialty FROM users " .
136 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
137 "ORDER BY specialty");
138 $optspec = "<option value='All'>All</option>\n";
139 while ($srow = sqlFetchArray($sres)) {
140 $optspec .= " <option value='" . $srow['specialty'] . "'>" .
141 $srow['specialty'] . "</option>\n";
144 <html>
145 <head>
146 <? html_header_show();?>
147 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
148 <title><? xl('Letter Generator','e'); ?></title>
150 <style>
151 </style>
153 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
155 <script type="text/javascript" src="../../library/topdialog.js"></script>
156 <script type="text/javascript" src="../../library/dialog.js"></script>
157 <script type="text/javascript" src="../../library/textformat.js"></script>
158 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
159 <script type="text/javascript" src="../../library/dynarch_calendar_en.js"></script>
160 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
162 <script language="JavaScript">
163 <?php echo $ulist; ?>
165 // React to selection of a specialty. This rebuilds the "to" users list
166 // with users having that specialty, or all users if "All" is selected.
167 function newspecialty() {
168 var f = document.forms[0];
169 var s = f.form_specialty.value;
170 var theopts = f.form_to.options;
171 theopts.length = 0;
172 var j = 0;
173 for (var i = 0; i < ulist.length; ++i) {
174 tmp = ulist[i].split("|");
175 if (s != 'All' && s != tmp[2]) continue;
176 theopts[j++] = new Option(tmp[0], tmp[1], false, false);
180 </script>
182 </head>
184 <body <?echo $top_bg_line;?> leftmargin='0' topmargin='0' marginwidth='0'
185 marginheight='0' onunload='imclosing()'>
187 <!-- <form method='post' action='letter.php' onsubmit='return top.restoreSession()'> -->
188 <form method='post' action='letter.php'>
190 <input type='hidden' name='form_pid' value='<?php echo $pid ?>' />
192 <center>
194 <table border='0' cellspacing='8' width='98%'>
196 <tr>
197 <td colspan='4' align='center'>
198 &nbsp;<br>
199 <b><?php xl('Generate Letter regarding ','e'); echo $patdata['fname'] . " " .
200 $patdata['lname'] . " (" . $patdata['pubpid'] . ")" ?></b>
201 <br>&nbsp;
202 </td>
203 </tr>
205 <tr>
207 <td>
208 <?php xl('From','e'); ?>:
209 </td>
211 <td>
212 <select name='form_from'>
213 <?php echo $optfrom; ?>
214 </select>
215 </td>
217 <td>
218 <?php xl('Date','e'); ?>:
219 </td>
221 <td>
222 <input type='text' size='10' name='form_date' id='form_date'
223 value='<?php echo date('Y-m-d'); ?>'
224 title='<?php xl('yyyy-mm-dd date of this letter','e'); ?>'
225 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />
226 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
227 id='img_date' border='0' alt='[?]' style='cursor:pointer'
228 title='<?php xl('Click here to choose a date','e'); ?>' />
229 </td>
231 </tr>
233 <tr>
235 <td>
236 <?php xl('Specialty','e'); ?>:
237 </td>
239 <td>
240 <select name='form_specialty' onchange='newspecialty()'>
241 <?php echo $optspec; ?>
242 </select>
243 </td>
245 <td>
246 <?php xl('Template','e'); ?>:
247 </td>
249 <td>
250 <select name='form_template'>
251 <?php
252 $tpldir = "$webserver_root/custom/letter_templates";
253 $dh = opendir($tpldir);
254 if (! $dh) die("Cannot read $tpldir");
255 while (false !== ($tfname = readdir($dh))) {
256 if (preg_match('/^(.*)\.t[a-z]*$/', $tfname, $matches)) {
257 echo " <option value='$tfname'>" . $matches[1] . "</option>\n";
260 closedir($dh);
262 </select>
263 </td>
265 </tr>
267 </tr>
269 <tr>
271 <td>
272 <?php xl('To','e'); ?>:
273 </td>
275 <td>
276 <select name='form_to'>
277 <?php echo $optto; ?>
278 </select>
279 </td>
281 <td>
282 <?php xl('Format','e'); ?>:
283 </td>
285 <td>
286 <select name='form_format'>
287 <option value='pdf'>PDF</option>
288 <option value='ps'>PostScript</option>
289 </select>
290 </td>
292 </tr>
294 <tr>
295 <td colspan='4'>
296 <textarea name='form_body' rows='20' cols='30' style='width:100%'
297 title='Enter body of letter here' /></textarea>
298 </td>
299 </tr>
301 </table>
303 <input type='submit' name='form_generate' value='Generate'>
305 </center>
306 </form>
308 <script language='JavaScript'>
309 Calendar.setup({inputField:"form_date", ifFormat:"%Y-%m-%d", button:"img_date"});
310 </script>
312 </body>
313 </html>