2 // Copyright (C) 2006, 2010 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 report lists prescriptions and their dispensations according
10 // to various input selection criteria.
12 // Fixed drug name search to work in a broader sense - tony@mi-squared.com 2010
13 // Added several reports as per EHR certification requirements for Patient Lists - OpenEMR Support LLC, 2010
15 require_once("../globals.php");
16 require_once("$srcdir/patient.inc");
17 require_once("$srcdir/options.inc.php");
18 require_once("../drugs/drugs.inc.php");
20 $report_by = $_POST['report_by'];
21 $facility_id = $_POST['form_facility'];
22 $provider_id = $_POST['form_provider'];
23 $txtSearch = $_POST['txtSearch'];
25 //billing.date = date of services
27 if ($_POST['form_refresh'])
30 concat(patient_data.fname, ' ', patient_data.lname) AS patient_name,
31 patient_data.id AS pid,
35 patient_data.city as county";
37 if($report_by == 'ICD9' ||
$report_by == 'CPT4')
40 form_encounter.encounter AS encounter_id,
41 form_encounter.date AS date_of_visit ";
42 $sql .= ", codes.code AS code, codes.code_text AS code_text,
43 concat(users.lname, ', ', users.fname) AS provider,
44 concat(users.fname, ' ', users.lname) AS provider2 FROM form_encounter
45 INNER JOIN patient_data ON patient_data.pid = form_encounter.pid
46 INNER JOIN billing ON billing.encounter = form_encounter.encounter
47 LEFT OUTER JOIN codes ON codes.code = billing.code
48 LEFT OUTER JOIN users ON users.id = billing.provider_id
49 LEFT OUTER JOIN facility ON facility.id = users.facility_id ";
53 if($report_by == 'ICD9')
55 $where_str = " WHERE billing.code_type = 'ICD9'";
57 else if($report_by == 'CPT4')
59 $where_str = " WHERE billing.code_type = 'CPT4'";
62 if(strlen($txtSearch) > 0)
64 $where_str .= " AND codes.code LIKE '%$txtSearch%'";
67 if(strlen($facility_id) > 0)
69 $where_str .= " AND facility.id = '$facility_id'";
72 if(strlen($provider_id) > 0)
74 $where_str .= " AND users.id = '$provider_id'";
79 else if($report_by == 'Drugs')
81 $sql .= ", prescriptions.date_added AS date_of_visit,
82 prescriptions.drug AS drug,
83 concat(users.lname, ', ', users.fname) AS provider,
84 concat(users.fname, ' ', users.lname) AS provider2
86 INNER JOIN patient_data ON patient_data.pid = prescriptions.patient_id
87 LEFT OUTER JOIN users ON users.id = prescriptions.provider_id
88 LEFT OUTER JOIN facility ON facility.id = users.facility_id";
92 if(strlen($txtSearch) > 0)
94 $where_str = " WHERE prescriptions.drug LIKE '%$txtSearch%'";
97 if(strlen($facility_id) > 0)
99 if(strlen($where_str) > 0)
101 $where_str .= " AND facility.id = '$facility_id'";
105 $where_str = " WHERE facility.id = '$facility_id'";
109 if(strlen($provider_id) > 0)
111 if(strlen($where_str) > 0)
113 $where_str .= " AND users.id = '$provider_id'";
117 $where_str = " WHERE users.id = '$provider_id'";
123 else if($report_by == 'Referral')
126 concat(patient_data.fname, ' ', patient_data.lname) AS patient_name,
127 patient_data.id AS pid,
131 patient_data.city as county,
132 transactions.refer_related_code AS code,
133 concat(users.lname, ', ', users.fname) AS provider,
134 concat(users.fname, ' ', users.lname) AS provider2,
135 transactions.date AS date_of_visit
137 INNER JOIN patient_data ON transactions.pid = patient_data.pid
138 INNER JOIN users ON transactions.user = users.username
139 LEFT OUTER JOIN facility ON facility.id = users.facility_id
140 WHERE transactions.title = 'Referral'";
142 if(strlen($facility_id) > 0)
144 $sql .= " AND facility.id = '$facility_id'";
147 if(strlen($provider_id) > 0)
149 $sql .= " AND users.id = '$provider_id'";
154 if ($_POST['form_refresh'] == "export")
156 $result = sqlStatement($sql);
158 if($report_by == 'ICD9' ||
$report_by == 'CPT4')
160 $out = "Code, ICD9/CPT4 Description, Patient Name, Date of Birth, Patient ID, Encounter ID, Date of Visit, Provider\n";
162 else if($report_by == 'Drugs')
164 $out = "Drugs, Patient Name, Date of Birth, Patient ID, Date of Visit, Provider\n";
166 else if($report_by == 'Referral')
168 $out = "Code, ICD9/CPT4 Description, Patient Name, Date of Birth, Patient ID, Date of Visit, Provider\n";
171 while($row = sqlFetchArray($result))
173 if($report_by == 'Referral')
175 $current_code = $row['code'];
178 if(strlen($current_code) > 0)
180 $current_code_arr = explode(":", $current_code);
181 $result_search = sqlStatement("SELECT * FROM codes WHERE code = '".$current_code_arr[1]."'");
183 if(sqlNumRows($result_search) > 0)
185 $row_search = sqlFetchArray($result_search);
186 $code_desc = $row_search['code_text'];
191 if($report_by == 'ICD9' ||
$report_by == 'CPT4')
193 $out .= "{$row['code']}, {$row['code_text']}, {$row['patient_name']}, {$row['DOB']}, {$row['pid']}, {$row['encounter_id']}, {$row['date_of_visit']}, {$row['provider2']}\n";
195 else if($report_by == 'Drugs')
197 $out .= "{$row['drug']}, {$row['patient_name']}, {$row['DOB']}, {$row['pid']}, {$row['date_of_visit']}, {$row['provider2']}\n";
199 else if($report_by == 'Referral')
201 $out .= "{$row['code']}, {$code_desc}, {$row['patient_name']}, {$row['DOB']}, {$row['pid']}, {$row['date_of_visit']}, {$row['provider2']}\n";
205 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
206 header("Content-Length: " . strlen($out));
207 header("Content-type: text/x-csv");
208 header("Content-type: application/csv");
209 header("Content-Disposition: attachment; filename=report_export.csv");
216 <?php
html_header_show();?
>
218 <?php
xl('Clinical Reports','e'); ?
>
220 <script type
="text/javascript" src
="../../library/overlib_mini.js"></script
>
221 <script type
="text/javascript" src
="../../library/textformat.js"></script
>
222 <script type
="text/javascript" src
="../../library/dialog.js"></script
>
223 <script type
="text/javascript" src
="../../library/js/jquery.1.3.2.js"></script
>
224 <script language
="JavaScript">
226 var mypcc
= '<?php echo $GLOBALS['phone_country_code
'] ?>';
228 // The OnClick handler for receipt display.
229 function show_receipt(payid
) {
230 // dlgopen('../patient_file/front_payment.php?receipt=1&payid=' + payid, '_blank', 550, 400);
235 <link rel
='stylesheet' href
='<?php echo $css_header ?>' type
='text/css'>
236 <style type
="text/css">
237 /* specifically include & exclude from printing */
243 #report_parameters_daterange {
247 #report_results table {
252 /* specifically exclude some from the screen */
254 #report_parameters_daterange {
262 if($report_by == 'Referral')
271 <script language
="javascript" type
="text/javascript">
274 if($
('#report_by').val() == 'Referral')
276 $
('.optional_area').css("display", "none");
280 $
('.optional_area').css("display", "inline");
286 <body
class="body_top">
288 <!-- Required
for the popup date selectors
-->
289 <div id
="overDiv" style
="position:absolute; visibility:hidden; z-index:1000;"></div
>
291 <?php
xl('Report - Encounters','e'); ?
>
293 <!-- Search can be done using age range
, gender
, and ethnicity filters
.
294 Search options
include diagnosis
, procedure
, prescription
, medical history
, and lab results
.
296 <div id
="report_parameters_daterange"> <?php
echo date("d F Y", strtotime($form_from_date)) ." to ". date("d F Y", strtotime($form_to_date)); ?
> </div
>
298 <form method
='post' name
='theform' id
='theform' action
='<?php echo $_SERVER['REQUEST_URI
'] ?>'>
299 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
301 <div id
="report_parameters">
305 <div style
='float:left'>
310 <?php
xl('Facility','e'); ?
>:
314 // Build a drop-down list of facilities.
316 $query = "SELECT id, name FROM facility ORDER BY name";
317 $fres = sqlStatement($query);
318 echo " <select name='form_facility'>\n";
319 echo " <option value=''>-- " . xl('All Facilities') . " --\n";
320 while ($frow = sqlFetchArray($fres)) {
321 $facid = $frow['id'];
322 echo " <option value='$facid'";
323 if ($facid == $form_facility) echo " selected";
324 echo ">" . htmlspecialchars($frow['name']) . "\n";
326 echo " <option value='0'";
327 if ($form_facility === '0') echo " selected";
328 echo ">-- " . xl('Unspecified') . " --\n";
333 <?php
xl('Provider','e'); ?
>:
338 // Build a drop-down list of providers.
341 $query = "SELECT id, lname, fname FROM users WHERE ".
342 "authorized = 1 $provider_facility_filter ORDER BY lname, fname"; //(CHEMED) facility filter
344 $ures = sqlStatement($query);
346 echo " <select name='form_provider'>\n";
347 echo " <option value=''>-- " . xl('All') . " --\n";
349 while ($urow = sqlFetchArray($ures)) {
350 $provid = $urow['id'];
351 echo " <option value='$provid'";
352 if ($provid == $_POST['form_provider']) echo " selected";
353 echo ">" . $urow['lname'] . ", " . $urow['fname'] . "\n";
365 <?php
xl('Search','e'); ?
>:
368 <select name
="report_by" id
="report_by" onChange
="checkType();">
369 <option value
="ICD9" <?
=($report_by == 'ICD9') ?
"selected='selected'" : ''; ?
>><?php
xl('ICD9','e'); ?
></option
>
370 <option value
="CPT4" <?
=($report_by == 'CPT4') ?
"selected='selected'" : ''; ?
>><?php
xl('CPT4','e'); ?
></option
>
371 <option value
="Drugs" <?
=($report_by == 'Drugs') ?
"selected='selected'" : ''; ?
>><?php
xl('Drugs','e'); ?
></option
>
372 <option value
="Referral" <?
=($report_by == 'Referral') ?
"selected='selected'" : ''; ?
>><?php
xl('Referral','e'); ?
></option
>
374 <label
for="txtSearch"></label
>
375 <input type
="text" name
="txtSearch" id
="txtSearch" value
="<?=$txtSearch?>" class="optional_area"> 
;</td
>
391 <td align
='left' valign
='middle' height
="100%">
392 <table style
='border-left:1px solid; width:100%; height:100%' >
395 <div style
='margin-left:15px'>
396 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
398 <?php
xl('Submit','e'); ?
>
402 <?php
if ($_POST['form_refresh'] ||
$_POST['form_orderby'] ) { ?
>
403 <a href
='#' class='css_button' onclick
='window.print()'>
405 <?php
xl('Print','e'); ?
>
410 <?php
if ($_POST['form_refresh']) { ?
>
411 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","export"); $("#theform").submit();'>
413 <?php
xl('Export','e'); ?
>
425 </div
> <!-- end report_parameters
-->
429 // SQL scripts for the various searches
431 if ($_POST['form_refresh'])
433 $result = sqlStatement($sql);
435 if(sqlNumRows($result) > 0)
438 <div id
="report_results">
442 if($report_by == 'ICD9' ||
$report_by == 'CPT4')
445 <th
><?php
xl('Code','e'); ?
></th
>
446 <th
><?php
xl('ICD9/CPT4 Description','e'); ?
></th
>
449 else if($report_by == 'Drugs')
452 <th
><?php
xl('Drugs','e'); ?
></th
>
455 else if($report_by == 'Referral')
458 <th
><?php
xl('Code','e'); ?
></th
>
459 <th
><?php
xl('ICD9/CPT4 Description','e'); ?
></th
>
463 <th
> <?php
xl('Patient Name','e'); ?
></th
>
464 <th
> <?php
xl('Date of Birth','e'); ?
></th
>
465 <th
> <?php
xl('Patient ID','e'); ?
></th
>
468 if($report_by != 'Referral' && $report_by != 'Drugs' )
471 <th
> <?php
xl('Encounter ID','e'); ?
></th
>
477 <th
> <?php
xl('Date of Visit','e'); ?
></th
>
478 <th
> <?php
xl('Provider','e'); ?
></th
>
482 while($row = sqlFetchArray($result))
484 if($report_by == 'Referral')
486 $current_code = $row['code'];
489 if(strlen($current_code) > 0)
491 $current_code_arr = explode(":", $current_code);
492 $result_search = sqlStatement("SELECT * FROM codes WHERE code = '".$current_code_arr[1]."'");
494 if(sqlNumRows($result_search) > 0)
496 $row_search = sqlFetchArray($result_search);
497 $code_desc = $row_search['code_text'];
504 if($report_by == 'ICD9' ||
$report_by == 'CPT4')
507 <td
> <?
=$row['code']?
> 
;</td
>
508 <td
> <?
=$row['code_text']?
> 
;</td
>
511 else if($report_by == 'Drugs')
514 <td width
="200"> <?
=$row['drug']?
> 
;</td
>
517 else if($report_by == 'Referral')
520 <td
> <?
=$row['code']?
> 
;</td
>
521 <td
> <?
=$code_desc?
> 
;</td
>
525 <td
><a target
="RBot" href
="../patient_file/summary/demographics2.php?pid=<?=$row['pid']?>"><?
=$row['patient_name']?
></a
> 
;</td
>
526 <td
> <?
=$row['DOB']?
> 
;</td
>
527 <td
> <?
=$row['pid']?
> 
;</td
>
530 if($report_by != 'Referral' && $report_by != 'Drugs' )
533 <td
> <a target
="RBot" href
="../patient_file/encounter/encounter_top.php?set_encounter=<?=$row['encounter_id']?>&pid=<?=$row['pid']?>"><?
=$row['encounter_id']?
></a
> 
;</td
>
539 <td
> <?
=$row['date_of_visit']?
> 
;</td
>
540 <td
> <?
=$row['provider']?
> 
;</td
>
548 <!-- end of results
-->
556 ?
><div
class='text'> <?php
echo xl('Please input search criteria above, and click Submit to view results.', 'e' ); ?
> </div
><?php