added support for mariadb in the ubuntu-debian packages
[openemr.git] / interface / reports / prescriptions_report.php
bloba5fbac1012786347dc876f5ea253ff6457c82046
1 <?php
2 // Copyright (C) 2006-2015 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.
11 //
12 // Fix drug name search to work in a broader sense - tony@mi-squared.com 2010
14 require_once("../globals.php");
15 require_once("$srcdir/patient.inc");
16 require_once("$srcdir/options.inc.php");
17 require_once("../drugs/drugs.inc.php");
18 require_once("$srcdir/formatting.inc.php");
19 require_once "$srcdir/formdata.inc.php";
21 $form_from_date = fixDate($_POST['form_from_date'], date('Y-01-01'));
22 $form_to_date = fixDate($_POST['form_to_date'] , date('Y-m-d'));
23 $form_patient_id = trim($_POST['form_patient_id']);
24 $form_drug_name = trim($_POST['form_drug_name']);
25 $form_lot_number = trim($_POST['form_lot_number']);
26 $form_facility = isset($_POST['form_facility']) ? $_POST['form_facility'] : '';
28 <html>
29 <head>
30 <?php html_header_show();?>
31 <title><?php xl('Prescriptions and Dispensations','e'); ?></title>
32 <script type="text/javascript" src="../../library/overlib_mini.js"></script>
33 <script type="text/javascript" src="../../library/textformat.js"></script>
34 <script type="text/javascript" src="../../library/dialog.js"></script>
35 <script type="text/javascript" src="../../library/js/jquery.1.3.2.js"></script>
37 <script language="JavaScript">
39 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
41 $(document).ready(function() {
42 var win = top.printLogSetup ? top : opener.top;
43 win.printLogSetup(document.getElementById('printbutton'));
44 });
46 // The OnClick handler for receipt display.
47 function show_receipt(payid) {
48 // dlgopen('../patient_file/front_payment.php?receipt=1&payid=' + payid, '_blank', 550, 400);
49 return false;
52 </script>
54 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
55 <style type="text/css">
57 /* specifically include & exclude from printing */
58 @media print {
59 #report_parameters {
60 visibility: hidden;
61 display: none;
63 #report_parameters_daterange {
64 visibility: visible;
65 display: inline;
67 #report_results table {
68 margin-top: 0px;
72 /* specifically exclude some from the screen */
73 @media screen {
74 #report_parameters_daterange {
75 visibility: hidden;
76 display: none;
80 </style>
81 </head>
83 <body class="body_top">
85 <!-- Required for the popup date selectors -->
86 <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
88 <span class='title'><?php xl('Report','e'); ?> - <?php xl('Prescriptions and Dispensations','e'); ?></span>
90 <div id="report_parameters_daterange">
91 <?php echo date("d F Y", strtotime($form_from_date)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
92 </div>
94 <form name='theform' id='theform' method='post' action='prescriptions_report.php'>
96 <div id="report_parameters">
98 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
99 <table>
100 <tr>
101 <td width='640px'>
102 <div style='float:left'>
104 <table class='text'>
105 <tr>
106 <td class='label'>
107 <?php xl('Facility','e'); ?>:
108 </td>
109 <td>
110 <?php dropdown_facility(strip_escape_custom($form_facility), 'form_facility', true); ?>
111 </td>
112 <td class='label'>
113 <?php xl('From','e'); ?>:
114 </td>
115 <td>
116 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo $form_from_date ?>'
117 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
118 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
119 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
120 title='<?php xl('Click here to choose a date','e'); ?>'>
121 </td>
122 <td class='label'>
123 <?php xl('To','e'); ?>:
124 </td>
125 <td>
126 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo $form_to_date ?>'
127 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
128 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
129 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
130 title='<?php xl('Click here to choose a date','e'); ?>'>
131 </td>
132 </tr>
133 <tr>
134 <td class='label'>
135 <?php xl('Patient ID','e'); ?>:
136 </td>
137 <td>
138 <input type='text' name='form_patient_id' size='10' maxlength='20' value='<?php echo $form_patient_id ?>'
139 title=<?php xl('Optional numeric patient ID','e','\'','\''); ?> />
140 </td>
141 <td class='label'>
142 <?php xl('Drug','e'); ?>:
143 </td>
144 <td>
145 <input type='text' name='form_drug_name' size='10' maxlength='250' value='<?php echo $form_drug_name ?>'
146 title=<?php xl('Optional drug name, use % as a wildcard','e','\'','\''); ?> />
147 </td>
148 <td class='label'>
149 <?php xl('Lot','e'); ?>:
150 </td>
151 <td>
152 <input type='text' name='form_lot_number' size='10' maxlength='20' value='<?php echo $form_lot_number ?>'
153 title=<?php xl('Optional lot number, use % as a wildcard','e','\'','\''); ?> />
154 </td>
155 </tr>
156 </table>
158 </div>
160 </td>
161 <td align='left' valign='middle' height="100%">
162 <table style='border-left:1px solid; width:100%; height:100%' >
163 <tr>
164 <td>
165 <div style='margin-left:15px'>
166 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
167 <span>
168 <?php xl('Submit','e'); ?>
169 </span>
170 </a>
172 <?php if ($_POST['form_refresh']) { ?>
173 <a href='#' class='css_button' id='printbutton'>
174 <span>
175 <?php xl('Print','e'); ?>
176 </span>
177 </a>
178 <?php } ?>
179 </div>
180 </td>
181 </tr>
182 </table>
183 </td>
184 </tr>
185 </table>
186 </div> <!-- end of parameters -->
188 <?php
189 if ($_POST['form_refresh']) {
191 <div id="report_results">
192 <table>
193 <thead>
194 <th> <?php xl('Patient','e'); ?> </th>
195 <th> <?php xl('ID','e'); ?> </th>
196 <th> <?php xl('RX','e'); ?> </th>
197 <th> <?php xl('Drug Name','e'); ?> </th>
198 <th> <?php xl('NDC','e'); ?> </th>
199 <th> <?php xl('Units','e'); ?> </th>
200 <th> <?php xl('Refills','e'); ?> </th>
201 <th> <?php xl('Instructed','e'); ?> </th>
202 <th> <?php xl('Reactions','e'); ?> </th>
203 <th> <?php xl('Dispensed','e'); ?> </th>
204 <th> <?php xl('Qty','e'); ?> </th>
205 <th> <?php xl('Manufacturer','e'); ?> </th>
206 <th> <?php xl('Lot','e'); ?> </th>
207 </thead>
208 <tbody>
209 <?php
210 if ($_POST['form_refresh']) {
211 $where = "r.date_modified >= '$form_from_date' AND " .
212 "r.date_modified <= '$form_to_date'";
213 //if ($form_patient_id) $where .= " AND r.patient_id = '$form_patient_id'";
214 if ($form_patient_id) $where .= " AND p.pubpid = '$form_patient_id'";
215 if ($form_drug_name ) $where .= " AND (d.name LIKE '$form_drug_name' OR r.drug LIKE '$form_drug_name')";
216 if ($form_lot_number) $where .= " AND i.lot_number LIKE '$form_lot_number'";
218 $query = "SELECT r.id, r.patient_id, " .
219 "r.date_modified, r.dosage, r.route, r.interval, r.refills, r.drug, " .
220 "d.name, d.ndc_number, d.form, d.size, d.unit, d.reactions, " .
221 "s.sale_id, s.sale_date, s.quantity, " .
222 "i.manufacturer, i.lot_number, i.expiration, " .
223 "p.pubpid, ".
224 "p.fname, p.lname, p.mname, u.facility_id " .
225 "FROM prescriptions AS r " .
226 "LEFT OUTER JOIN drugs AS d ON d.drug_id = r.drug_id " .
227 "LEFT OUTER JOIN drug_sales AS s ON s.prescription_id = r.id " .
228 "LEFT OUTER JOIN drug_inventory AS i ON i.inventory_id = s.inventory_id " .
229 "LEFT OUTER JOIN patient_data AS p ON p.pid = r.patient_id " .
230 "LEFT OUTER JOIN users AS u ON u.id = r.provider_id " .
231 "WHERE $where " .
232 //"ORDER BY p.lname, p.fname, r.patient_id, r.id, s.sale_id";
233 "ORDER BY p.lname, p.fname, p.pubpid, r.id, s.sale_id";
235 // echo "<!-- $query -->\n"; // debugging
236 $res = sqlStatement($query);
238 $last_patient_id = 0;
239 $last_prescription_id = 0;
240 while ($row = sqlFetchArray($res)) {
241 // If a facility is specified, ignore rows that do not match.
242 if ($form_facility !== '') {
243 if ($form_facility) {
244 if ($row['facility_id'] != $form_facility) continue;
246 else {
247 if (!empty($row['facility_id'])) continue;
250 $patient_name = $row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname'];
251 //$patient_id = $row['patient_id'];
252 $patient_id = $row['pubpid'];
253 $prescription_id = $row['id'];
254 $drug_name = empty($row['name']) ? $row['drug'] : $row['name'];
255 $ndc_number = $row['ndc_number'];
256 $drug_units = $row['size'] . ' ' .
257 generate_display_field(array('data_type'=>'1','list_id'=>'drug_units'), $row['unit']);
258 $refills = $row['refills'];
259 $reactions = $row['reactions'];
260 $instructed = $row['dosage'] . ' ' .
261 generate_display_field(array('data_type'=>'1','list_id'=>'drug_form'), $row['form']) .
262 ' ' .
263 generate_display_field(array('data_type'=>'1','list_id'=>'drug_interval'), $row['interval']);
264 //if ($row['patient_id'] == $last_patient_id) {
265 if (strcmp($row['pubpid'], $last_patient_id) == 0) {
266 $patient_name = '&nbsp;';
267 $patient_id = '&nbsp;';
268 if ($row['id'] == $last_prescription_id) {
269 $prescription_id = '&nbsp;';
270 $drug_name = '&nbsp;';
271 $ndc_number = '&nbsp;';
272 $drug_units = '&nbsp;';
273 $refills = '&nbsp;';
274 $reactions = '&nbsp;';
275 $instructed = '&nbsp;';
279 <tr>
280 <td>
281 <?php echo $patient_name ?>
282 </td>
283 <td>
284 <?php echo $patient_id ?>
285 </td>
286 <td>
287 <?php echo $prescription_id ?>
288 </td>
289 <td>
290 <?php echo $drug_name ?>
291 </td>
292 <td>
293 <?php echo $ndc_number ?>
294 </td>
295 <td>
296 <?php echo $drug_units ?>
297 </td>
298 <td>
299 <?php echo $refills ?>
300 </td>
301 <td>
302 <?php echo $instructed ?>
303 </td>
304 <td>
305 <?php echo $reactions ?>
306 </td>
307 <td>
308 <a href='../drugs/dispense_drug.php?sale_id=<?php echo $row['sale_id'] ?>'
309 style='color:#0000ff' target='_blank'>
310 <?php echo oeFormatShortDate($row['sale_date']) ?>
311 </a>
312 </td>
313 <td>
314 <?php echo $row['quantity'] ?>
315 </td>
316 <td>
317 <?php echo $row['manufacturer'] ?>
318 </td>
319 <td>
320 <?php echo $row['lot_number'] ?>
321 </td>
322 </tr>
323 <?php
324 $last_prescription_id = $row['id'];
325 //$last_patient_id = $row['patient_id'];
326 $last_patient_id = $row['pubpid'];
327 } // end while
328 } // end if
330 </tbody>
331 </table>
332 </div> <!-- end of results -->
333 <?php } else { ?>
334 <div class='text'>
335 <?php echo xl('Please input search criteria above, and click Submit to view results.', 'e' ); ?>
336 </div>
337 <?php } ?>
338 </form>
339 </body>
341 <!-- stuff for the popup calendar -->
342 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
343 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
344 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
345 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
346 <script language="Javascript">
347 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
348 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
349 </script>
350 </html>