some optimization for IPPF forms
[openemr.git] / interface / reports / prescriptions_report.php
blob80eea7b9fca2c8bcba7e15e07a04243822b2515f
1 <?php
2 // Copyright (C) 2006 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 report lists prescriptions and their dispensations according
10 // to various input selection criteria.
12 require_once("../globals.php");
13 require_once("$srcdir/patient.inc");
14 require_once("../drugs/drugs.inc.php");
16 $form_from_date = fixDate($_POST['form_from_date'], date('Y-01-01'));
17 $form_to_date = fixDate($_POST['form_to_date'] , date('Y-m-d'));
18 $form_patient_id = trim($_POST['form_patient_id']);
19 $form_drug_name = trim($_POST['form_drug_name']);
20 $form_lot_number = trim($_POST['form_lot_number']);
21 $form_facility = isset($_POST['form_facility']) ? $_POST['form_facility'] : '';
23 <html>
24 <head>
25 <?php html_header_show();?>
26 <title><?php xl('Prescriptions and Dispensations','e'); ?></title>
27 <script type="text/javascript" src="../../library/overlib_mini.js"></script>
28 <script type="text/javascript" src="../../library/textformat.js"></script>
29 <script type="text/javascript" src="../../library/dialog.js"></script>
30 <script language="JavaScript">
32 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
34 // The OnClick handler for receipt display.
35 function show_receipt(payid) {
36 // dlgopen('../patient_file/front_payment.php?receipt=1&payid=' + payid, '_blank', 550, 400);
37 return false;
40 </script>
42 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
43 <style type="text/css">
45 /* specifically include & exclude from printing */
46 @media print {
47 #rxdrugreport_parameters {
48 visibility: hidden;
49 display: none;
51 #rxdrugreport_parameters_daterange {
52 visibility: visible;
53 display: inline;
57 /* specifically exclude some from the screen */
58 @media screen {
59 #rxdrugreport_parameters_daterange {
60 visibility: hidden;
61 display: none;
65 #rxdrugreport_parameters {
66 width: 100%;
67 background-color: #ddf;
69 #rxdrugreport_parameters table {
70 border: none;
71 border-collapse: collapse;
73 #rxdrugreport_parameters table td {
74 padding: 3px;
77 #rxdrugreport_results {
78 width: 100%;
79 margin-top: 10px;
81 #rxdrugreport_results table {
82 border: 1px solid black;
83 width: 98%;
84 border-collapse: collapse;
86 #rxdrugreport_results table thead {
87 display: table-header-group;
88 background-color: #ddd;
90 #rxdrugreport_results table th {
91 border-bottom: 1px solid black;
92 font-size: 0.7em;
94 #rxdrugreport_results table td {
95 padding: 1px;
96 margin: 2px;
97 border-bottom: 1px solid #eee;
98 font-size: 0.7em;
100 .rxdrugreport_totals td {
101 background-color: #77ff77;
102 font-weight: bold;
104 </style>
105 </head>
107 <body class="body_top">
109 <!-- Required for the popup date selectors -->
110 <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
112 <center>
114 <h2><?php xl('Prescriptions and Dispensations','e'); ?></h2>
116 <div id="rxdrugreport_parameters_daterange">
117 <?php echo date("d F Y", strtotime($form_from_date)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
118 </div>
120 <div id="rxdrugreport_parameters">
122 <form name='theform' method='post' action='prescriptions_report.php'>
124 <table>
125 <tr>
126 <td>
127 <?php
128 // Build a drop-down list of facilities.
130 $query = "SELECT id, name FROM facility ORDER BY name";
131 $fres = sqlStatement($query);
132 echo " <select name='form_facility'>\n";
133 echo " <option value=''>-- All Facilities --\n";
134 while ($frow = sqlFetchArray($fres)) {
135 $facid = $frow['id'];
136 echo " <option value='$facid'";
137 if ($facid == $form_facility) echo " selected";
138 echo ">" . $frow['name'] . "\n";
140 echo " <option value='0'";
141 if ($form_facility === '0') echo " selected";
142 echo ">-- Unspecified --\n";
143 echo " </select>\n";
145 &nbsp;<?php xl('From','e'); ?>:
146 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo $form_from_date ?>'
147 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
148 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
149 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
150 title='<?php xl('Click here to choose a date','e'); ?>'>
151 &nbsp;<?php xl('To','e'); ?>:
152 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo $form_to_date ?>'
153 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
154 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
155 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
156 title='<?php xl('Click here to choose a date','e'); ?>'>
157 &nbsp;<?php xl('Patient ID','e'); ?>:
158 <input type='text' name='form_patient_id' size='6' maxlength='6' value='<?php echo $form_patient_id ?>'
159 title='Optional numeric patient ID' />
160 &nbsp;<?php xl('Drug','e'); ?>:
161 <input type='text' name='form_drug_name' size='10' maxlength='250' value='<?php echo $form_drug_name ?>'
162 title='Optional drug name, use % as a wildcard' />
163 &nbsp;<?php xl('Lot','e'); ?>:
164 <input type='text' name='form_lot_number' size='10' maxlength='20' value='<?php echo $form_lot_number ?>'
165 title='Optional lot number, use % as a wildcard' />
166 &nbsp;
167 <input type='submit' name='form_refresh' value=<?php xl('Refresh','e'); ?>>
168 &nbsp;
169 <input type='button' value='<?php xl('Print','e'); ?>' onclick='window.print()' />
170 </td>
171 </tr>
172 </table>
173 </div> <!-- end of parameters -->
175 <div id="rxdrugreport_results">
176 <table>
177 <thead>
178 <th> <?php xl('Patient','e'); ?> </th>
179 <th> <?php xl('ID','e'); ?> </th>
180 <th> <?php xl('RX','e'); ?> </th>
181 <th> <?php xl('Drug Name','e'); ?> </th>
182 <th> <?php xl('NDC','e'); ?> </th>
183 <th> <?php xl('Units','e'); ?> </th>
184 <th> <?php xl('Refills','e'); ?> </th>
185 <th> <?php xl('Instructed','e'); ?> </th>
186 <th> <?php xl('Reactions','e'); ?> </th>
187 <th> <?php xl('Dispensed','e'); ?> </th>
188 <th> <?php xl('Qty','e'); ?> </th>
189 <th> <?php xl('Manufacturer','e'); ?> </th>
190 <th> <?php xl('Lot','e'); ?> </th>
191 </thead>
192 <tbody>
193 <?php
194 if ($_POST['form_refresh']) {
195 $where = "r.date_modified >= '$form_from_date' AND " .
196 "r.date_modified <= '$form_to_date'";
197 if ($form_patient_id) $where .= " AND r.patient_id = '$form_patient_id'";
198 if ($form_drug_name ) $where .= " AND d.name LIKE '$form_drug_name'";
199 if ($form_lot_number) $where .= " AND i.lot_number LIKE '$form_lot_number'";
201 $query = "SELECT r.id, r.patient_id, " .
202 "r.date_modified, r.dosage, r.route, r.interval, r.refills, " .
203 "d.name, d.ndc_number, d.form, d.size, d.unit, d.reactions, " .
204 "s.sale_id, s.sale_date, s.quantity, " .
205 "i.manufacturer, i.lot_number, i.expiration, " .
206 "p.fname, p.lname, p.mname, u.facility_id " .
207 "FROM prescriptions AS r " .
208 "LEFT OUTER JOIN drugs AS d ON d.drug_id = r.drug_id " .
209 "LEFT OUTER JOIN drug_sales AS s ON s.prescription_id = r.id " .
210 "LEFT OUTER JOIN drug_inventory AS i ON i.inventory_id = s.inventory_id " .
211 "LEFT OUTER JOIN patient_data AS p ON p.pid = r.patient_id " .
212 "LEFT OUTER JOIN users AS u ON u.id = r.provider_id " .
213 "WHERE $where " .
214 "ORDER BY p.lname, p.fname, r.patient_id, r.id, s.sale_id";
216 // echo "<!-- $query -->\n"; // debugging
217 $res = sqlStatement($query);
219 $last_patient_id = 0;
220 $last_prescription_id = 0;
221 while ($row = sqlFetchArray($res)) {
222 // If a facility is specified, ignore rows that do not match.
223 if ($form_facility !== '') {
224 if ($form_facility) {
225 if ($row['facility_id'] != $form_facility) continue;
227 else {
228 if (!empty($row['facility_id'])) continue;
231 $patient_name = $row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname'];
232 $patient_id = $row['patient_id'];
233 $prescription_id = $row['id'];
234 $drug_name = $row['name'];
235 $ndc_number = $row['ndc_number'];
236 $drug_units = $row['size'] . ' ' . $unit_array[$row['unit']];
237 $refills = $row['refills'];
238 $reactions = $row['reactions'];
239 $instructed = $row['dosage'] . ' ' . $form_array[$row['form']] . ' ' .
240 $interval_array[$row['interval']];
241 if ($row['patient_id'] == $last_patient_id) {
242 $patient_name = '&nbsp;';
243 $patient_id = '&nbsp;';
244 if ($row['id'] == $last_prescription_id) {
245 $prescription_id = '&nbsp;';
246 $drug_name = '&nbsp;';
247 $ndc_number = '&nbsp;';
248 $drug_units = '&nbsp;';
249 $refills = '&nbsp;';
250 $reactions = '&nbsp;';
251 $instructed = '&nbsp;';
255 <tr>
256 <td>
257 <?php echo $patient_name ?>
258 </td>
259 <td>
260 <?php echo $patient_id ?>
261 </td>
262 <td>
263 <?php echo $prescription_id ?>
264 </td>
265 <td>
266 <?php echo $drug_name ?>
267 </td>
268 <td>
269 <?php echo $ndc_number ?>
270 </td>
271 <td>
272 <?php echo $drug_units ?>
273 </td>
274 <td>
275 <?php echo $refills ?>
276 </td>
277 <td>
278 <?php echo $instructed ?>
279 </td>
280 <td>
281 <?php echo $reactions ?>
282 </td>
283 <td>
284 <a href='../drugs/dispense_drug.php?sale_id=<?php echo $row['sale_id'] ?>'
285 style='color:#0000ff' target='_blank'>
286 <?php echo $row['sale_date'] ?>
287 </a>
288 </td>
289 <td>
290 <?php echo $row['quantity'] ?>
291 </td>
292 <td>
293 <?php echo $row['manufacturer'] ?>
294 </td>
295 <td>
296 <?php echo $row['lot_number'] ?>
297 </td>
298 </tr>
299 <?php
300 $last_prescription_id = $row['id'];
301 $last_patient_id = $row['patient_id'];
302 } // end while
303 } // end if
305 </tbody>
306 </table>
307 </div> <!-- end of results -->
308 </form>
309 </center>
310 </body>
312 <!-- stuff for the popup calendar -->
313 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
314 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
315 <script type="text/javascript" src="../../library/dynarch_calendar_en.js"></script>
316 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
317 <script language="Javascript">
318 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
319 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
320 </script>
321 </html>