Highway to PSR2
[openemr.git] / interface / forms / newpatient / common.php
blob0f95bc3e0b22e0e1558548b27586b17c44593445
1 <?php
2 /**
3 * Common script for the encounter form (new and view) scripts.
5 * LICENSE: This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
16 * @package OpenEMR
17 * @author Brady Miller <brady.g.miller@gmail.com>
18 * @link http://www.open-emr.org
21 require_once("$srcdir/options.inc.php");
22 require_once("$srcdir/acl.inc");
23 require_once("$srcdir/lists.inc");
25 $facilityService = new \services\FacilityService();
27 if ($GLOBALS['enable_group_therapy']) {
28 require_once("$srcdir/group.inc");
31 $months = array("01","02","03","04","05","06","07","08","09","10","11","12");
32 $days = array("01","02","03","04","05","06","07","08","09","10","11","12","13","14",
33 "15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
34 $thisyear = date("Y");
35 $years = array($thisyear-1, $thisyear, $thisyear+1, $thisyear+2);
37 if ($viewmode) {
38 $id = (isset($_REQUEST['id'])) ? $_REQUEST['id'] : '';
39 $result = sqlQuery("SELECT * FROM form_encounter WHERE id = ?", array($id));
40 $encounter = $result['encounter'];
41 if ($result['sensitivity'] && !acl_check('sensitivities', $result['sensitivity'])) {
42 echo "<body>\n<html>\n";
43 echo "<p>" . xlt('You are not authorized to see this encounter.') . "</p>\n";
44 echo "</body>\n</html>\n";
45 exit();
49 // Sort comparison for sensitivities by their order attribute.
50 function sensitivity_compare($a, $b)
52 return ($a[2] < $b[2]) ? -1 : 1;
55 // get issues
56 $ires = sqlStatement("SELECT id, type, title, begdate FROM lists WHERE " .
57 "pid = ? AND enddate IS NULL " .
58 "ORDER BY type, begdate", array($pid));
60 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
61 <html>
62 <head>
63 <?php html_header_show();?>
64 <title><?php echo xlt('Patient Encounter'); ?></title>
66 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
67 <link rel="stylesheet" href="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">
69 <link rel="stylesheet" type="text/css" href="<?php echo $GLOBALS['webroot'] ?>/library/js/fancybox-1.3.4/jquery.fancybox-1.3.4.css" media="screen" />
70 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-7-2/index.js"></script>
71 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/common.js?v=<?php echo $v_js_includes; ?>"></script>
72 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/fancybox-1.3.4/jquery.fancybox-1.3.4.pack.js"></script>
73 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dialog.js?v=<?php echo $v_js_includes; ?>"></script>
74 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/textformat.js?v=<?php echo $v_js_includes; ?>"></script>
75 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script>
77 <!-- validation library -->
78 <?php
79 //Not lbf forms use the new validation, please make sure you have the corresponding values in the list Page validation
80 $use_validate_js = 1;
81 require_once($GLOBALS['srcdir'] . "/validation/validation_script.js.php"); ?>
83 <?php include_once("{$GLOBALS['srcdir']}/ajax/facility_ajax_jav.inc.php"); ?>
84 <script language="JavaScript">
86 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
88 // Process click on issue title.
89 function newissue() {
90 dlgopen('../../patient_file/summary/add_edit_issue.php', '_blank', 800, 600);
91 return false;
94 // callback from add_edit_issue.php:
95 function refreshIssue(issue, title) {
96 var s = document.forms[0]['issues[]'];
97 s.options[s.options.length] = new Option(title, issue, true, true);
100 <?php
101 //Gets validation rules from Page Validation list.
102 //Note that for technical reasons, we are bypassing the standard validateUsingPageRules() call.
103 $collectthis = collectValidationPageRules("/interface/forms/newpatient/common.php");
104 if (empty($collectthis)) {
105 $collectthis = "undefined";
106 } else {
107 $collectthis = $collectthis["new_encounter"]["rules"];
110 var collectvalidation = <?php echo($collectthis); ?>;
111 $(document).ready(function(){
112 window.saveClicked = function(event) {
113 var submit = submitme(1, event, 'new-encounter-form', collectvalidation);
114 if (submit) {
115 top.restoreSession();
116 $('#new-encounter-form').submit();
120 enable_big_modals();
122 $('.datepicker').datetimepicker({
123 <?php $datetimepicker_timepicker = false; ?>
124 <?php $datetimepicker_showseconds = false; ?>
125 <?php $datetimepicker_formatInput = false; ?>
126 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
127 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
131 function bill_loc(){
132 var pid=<?php echo attr($pid);?>;
133 var dte=document.getElementById('form_date').value;
134 var facility=document.forms[0].facility_id.value;
135 ajax_bill_loc(pid,dte,facility);
138 // Handler for Cancel clicked when creating a new encounter.
139 // Show demographics or encounters list depending on what frame we're in.
140 function cancelClicked() {
141 if (window.name == 'RBot') {
142 parent.left_nav.loadFrame('ens1', window.name, 'patient_file/history/encounters.php');
144 else {
145 parent.left_nav.loadFrame('dem1', window.name, 'patient_file/summary/demographics.php');
147 return false;
150 </script>
151 </head>
153 <?php if ($viewmode) { ?>
154 <body class="body_top">
155 <?php } else { ?>
156 <body class="body_top" onload="javascript:document.new_encounter.reason.focus();">
157 <?php } ?>
159 <!-- Required for the popup date selectors -->
160 <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
162 <form id="new-encounter-form" method='post' action="<?php echo $rootdir ?>/forms/newpatient/save.php" name='new_encounter'>
164 <div style='float:left'>
165 <?php if ($viewmode) { ?>
166 <input type=hidden name='mode' value='update'>
167 <input type=hidden name='id' value='<?php echo (isset($_GET["id"])) ? attr($_GET["id"]) : '' ?>'>
168 <span class=title><?php echo xlt('Patient Encounter Form'); ?></span>
169 <?php } else { ?>
170 <input type='hidden' name='mode' value='new'>
171 <span class='title'><?php echo xlt('New Encounter Form'); ?></span>
172 <?php } ?>
173 </div>
175 <div>
176 <div style = 'float:left; margin-left:8px;margin-top:-3px'>
177 <a href="javascript:saveClicked(undefined);" class="css_button link_submit"><span><?php echo xlt('Save'); ?></span></a>
178 <?php if ($viewmode || !isset($_GET["autoloaded"]) || $_GET["autoloaded"] != "1") { ?>
179 </div>
180 <div style = 'float:left; margin-top:-3px'>
181 <a href="<?php echo "$rootdir/patient_file/encounter/encounter_top.php"; ?>"
182 class="css_button link_submit" onClick="top.restoreSession()"><span><?php echo xlt('Cancel'); ?></span></a>
183 <?php } else { // not $viewmode ?>
184 <a href="" class="css_button link_submit" onClick="return cancelClicked()">
185 <span><?php echo xlt('Cancel'); ?></span></a>
186 <?php } // end not $viewmode ?>
187 </div>
188 </div>
190 <br> <br>
192 <table width='96%'>
194 <tr>
195 <td width='33%' nowrap class='bold'><?php echo xlt('Consultation Brief Description'); ?>:</td>
196 <td width='34%' rowspan='2' align='center' valign='center' class='text'>
197 <table>
199 <tr>
200 <td class='bold' nowrap><?php echo xlt('Visit Category:'); ?></td>
201 <td class='text'>
202 <select name='pc_catid' id='pc_catid'>
203 <option value='_blank'>-- <?php echo xlt('Select One'); ?> --</option>
204 <?php
205 $visitSQL = "SELECT pc_catid, pc_catname, pc_cattype
206 FROM openemr_postcalendar_categories
207 WHERE pc_active = 1 ORDER BY pc_seq";
208 $visitResult = sqlStatement($visitSQL);
209 $therapyGroupCategories = [];
211 while ($row = sqlFetchArray($visitResult)) {
212 $catId = $row['pc_catid'];
213 $name = $row['pc_catname'];
215 if ($row['pc_cattype'] == 3) {
216 $therapyGroupCategories[] = $catId;
219 if (($catId < 9 && $catId != "5") || $catId === "_blank") {
220 continue;
223 if ($row['pc_cattype'] == 3 && !$GLOBALS['enable_group_therapy']) {
224 continue;
227 // Fetch acl for category of given encounter. Only if has write auth for a category, then can create an encounter of that category.
228 $postCalendarCategoryACO = fetchPostCalendarCategoryACO($catId);
229 if ($postCalendarCategoryACO) {
230 $postCalendarCategoryACO = explode('|', $postCalendarCategoryACO);
231 $authPostCalendarCategoryWrite = acl_check($postCalendarCategoryACO[0], $postCalendarCategoryACO[1], '', 'write');
232 } else { // if no aco is set for category
233 $authPostCalendarCategoryWrite = true;
236 //if no permission for category write, don't show in drop-down
237 if (!$authPostCalendarCategoryWrite) {
238 continue;
241 $optionStr = '<option value="%pc_catid%" %selected%>%pc_catname%</option>';
242 $optionStr = str_replace("%pc_catid%", attr($catId), $optionStr);
243 $optionStr = str_replace("%pc_catname%", text(xl_appt_category($name)), $optionStr);
244 if ($viewmode) {
245 $selected = ($result['pc_catid'] == $catId) ? " selected" : "";
246 } else {
247 $selected = ($GLOBALS['default_visit_category'] == $catId) ? " selected" : "";
250 $optionStr = str_replace("%selected%", $selected, $optionStr);
251 echo $optionStr;
254 </select>
255 </td>
256 </tr>
258 <tr>
259 <td class='bold' nowrap><?php echo xlt('Facility:'); ?></td>
260 <td class='text'>
261 <select name='facility_id' onChange="bill_loc()">
262 <?php
264 if ($viewmode) {
265 $def_facility = $result['facility_id'];
266 } else {
267 $dres = sqlStatement("select facility_id from users where username = ?", array($_SESSION['authUser']));
268 $drow = sqlFetchArray($dres);
269 $def_facility = $drow['facility_id'];
272 $facilities = $facilityService->getAllServiceLocations();
273 if ($facilities) {
274 foreach ($facilities as $iter) {
276 <option value="<?php echo attr($iter['id']); ?>" <?php if ($def_facility == $iter['id']) {
277 echo "selected";
278 }?>><?php echo text($iter['name']); ?></option>
279 <?php
283 </select>
284 </td>
285 </tr>
286 <tr>
287 <td class='bold' nowrap><?php echo xlt('Billing Facility'); ?>:</td>
288 <td class='text'>
289 <div id="ajaxdiv">
290 <?php
291 billing_facility('billing_facility', $result['billing_facility']);
293 </div>
294 </td>
295 </tr>
296 <?php if ($GLOBALS['set_pos_code_encounter']) { ?>
297 <tr>
298 <td><span class='bold' nowrap><?php echo xlt('POS Code'); ?>: </span></td>
299 <td colspan="6">
300 <select name="pos_code">
301 <?php
303 $pc = new POSRef();
305 foreach ($pc->get_pos_ref() as $pos) {
306 echo "<option value=\"" . attr($pos["code"]) . "\" ";
307 if ($pos["code"] == $result['pos_code']) {
308 echo "selected";
311 echo ">" . text($pos['code']) . ": ". xlt($pos['title']);
312 echo "</option>\n";
316 </select>
317 </td>
318 </tr>
319 <?php } ?>
320 <tr>
321 <?php
322 $sensitivities = acl_get_sensitivities();
323 if ($sensitivities && count($sensitivities)) {
324 usort($sensitivities, "sensitivity_compare");
326 <td class='bold' nowrap><?php echo xlt('Sensitivity:'); ?></td>
327 <td class='text'>
328 <select name='form_sensitivity'>
329 <?php
330 foreach ($sensitivities as $value) {
331 // Omit sensitivities to which this user does not have access.
332 if (acl_check('sensitivities', $value[1])) {
333 echo " <option value='" . attr($value[1]) . "'";
334 if ($viewmode && $result['sensitivity'] == $value[1]) {
335 echo " selected";
338 echo ">" . xlt($value[3]) . "</option>\n";
342 echo " <option value=''";
343 if ($viewmode && !$result['sensitivity']) {
344 echo " selected";
347 echo ">" . xlt('None'). "</option>\n";
349 </select>
350 </td>
351 <?php
352 } else {
354 <td colspan='2'><!-- sensitivities not used --></td>
355 <?php
358 </tr>
360 <tr<?php if (!$GLOBALS['gbl_visit_referral_source']) {
361 echo " style='visibility:hidden;'";
362 } ?>>
363 <td class='bold' nowrap><?php echo xlt('Referral Source'); ?>:</td>
364 <td class='text'>
365 <?php
366 echo generate_select_list('form_referral_source', 'refsource', $viewmode ? $result['referral_source'] : '', '');
368 </td>
369 </tr>
371 <?php if ($GLOBALS['enable_group_therapy']) { ?>
372 <!-- select group name - showing just if therapy group type is selected -->
373 <tr id="therapy_group_name" style="display: none">
374 <td class='bold' nowrap><?php echo xlt('Group name'); ?>:</td>
375 <td>
376 <input type='text' size='10' name='form_group' id="form_group" style='width:100%;cursor:pointer;cursor:hand' placeholder='<?php echo xla('Click to select');?>' value='<?php echo $viewmode && in_array($result['pc_catid'], $therapyGroupCategories) ? attr(getGroup($result['external_id'])['group_name']) : ''; ?>' onclick='sel_group()' title='<?php echo xla('Click to select group'); ?>' readonly />
377 <input type='hidden' name='form_gid' value='<?php echo $viewmode && in_array($result['pc_catid'], $therapyGroupCategories) ? attr($result['external_id']) : '' ?>' />
378 </td>
379 </tr>
381 <?php }?>
382 <tr>
383 <td class='bold' nowrap><?php echo xlt('Date of Service:'); ?></td>
384 <td class='text' nowrap>
385 <input type='text' size='10' class='datepicker' name='form_date' id='form_date' <?php echo $disabled ?>
386 value='<?php echo $viewmode ? substr($result['date'], 0, 10) : date('Y-m-d'); ?>'
387 title='<?php echo xla('yyyy-mm-dd Date of service'); ?>' />
388 </td>
389 </tr>
391 <tr<?php if ($GLOBALS['ippf_specific']) {
392 echo " style='visibility:hidden;'";
393 } ?>>
394 <td class='bold' nowrap><?php echo xlt('Onset/hosp. date:'); ?></td>
395 <td class='text' nowrap><!-- default is blank so that while generating claim the date is blank. -->
396 <input type='text' size='10' class='datepicker' name='form_onset_date' id='form_onset_date'
397 value='<?php echo $viewmode && $result['onset_date']!='0000-00-00 00:00:00' ? substr($result['onset_date'], 0, 10) : ''; ?>'
398 title='<?php echo xla('yyyy-mm-dd Date of onset or hospitalization'); ?>' />
399 </td>
400 </tr>
401 <tr>
402 <td class='text' colspan='2' style='padding-top:1em'>
403 </td>
404 </tr>
405 </table>
407 </td>
410 <td class='bold' width='33%' nowrap>
412 <?php
413 // To see issues stuff user needs write access to all issue types.
414 $issuesauth = true;
415 foreach ($ISSUE_TYPES as $type => $dummy) {
416 if (!acl_check_issue($type, '', 'write')) {
417 $issuesauth = false;
418 break;
422 if ($issuesauth) {
424 <div style='float:left'>
425 <?php echo xlt('Issues (Injuries/Medical/Allergy)'); ?>
426 </div>
427 <div style='float:left;margin-left:8px;margin-top:-3px'>
428 <?php if (acl_check('patients', 'med', '', 'write')) { ?>
429 <a href="../../patient_file/summary/add_edit_issue.php" class="css_button_small link_submit iframe"
430 onclick="top.restoreSession()"><span><?php echo xlt('Add'); ?></span></a>
431 <?php } ?>
432 </div>
433 <?php } ?>
435 </td>
436 </tr>
438 <tr>
439 <td class='text' valign='top'>
440 <textarea name='reason' cols='40' rows='12' wrap='virtual' style='width:96%'
441 ><?php echo $viewmode ? text($result['reason']) : text($GLOBALS['default_chief_complaint']); ?></textarea>
442 </td>
443 <td class='text' valign='top'>
445 <?php if ($issuesauth) { ?>
446 <select multiple name='issues[]' size='8' style='width:100%'
447 title='<?php echo xla('Hold down [Ctrl] for multiple selections or to unselect'); ?>'>
448 <?php
449 while ($irow = sqlFetchArray($ires)) {
450 $list_id = $irow['id'];
451 $tcode = $irow['type'];
452 if ($ISSUE_TYPES[$tcode]) {
453 $tcode = $ISSUE_TYPES[$tcode][2];
456 echo " <option value='" . attr($list_id) . "'";
457 if ($viewmode) {
458 $perow = sqlQuery("SELECT count(*) AS count FROM issue_encounter WHERE " .
459 "pid = ? AND encounter = ? AND list_id = ?", array($pid,$encounter,$list_id));
460 if ($perow['count']) {
461 echo " selected";
463 } else {
464 // For new encounters the invoker may pass an issue ID.
465 if (!empty($_REQUEST['issue']) && $_REQUEST['issue'] == $list_id) {
466 echo " selected";
470 echo ">" . text($tcode) . ": " . text($irow['begdate']) . " " .
471 text(substr($irow['title'], 0, 40)) . "</option>\n";
474 </select>
475 <p><i><?php echo xlt('To link this encounter/consult to an existing issue, click the '
476 . 'desired issue above to highlight it and then click [Save]. '
477 . 'Hold down [Ctrl] button to select multiple issues.'); ?></i></p>
478 <?php } ?>
480 </td>
481 </tr>
483 </table>
485 </form>
487 </body>
489 <script language="javascript">
490 <?php
491 if (!$viewmode) { ?>
492 function duplicateVisit(enc, datestr) {
493 if (!confirm('<?php echo xls("A visit already exists for this patient today. Click Cancel to open it, or OK to proceed with creating a new one.") ?>')) {
494 // User pressed the cancel button, so re-direct to today's encounter
495 top.restoreSession();
496 parent.left_nav.setEncounter(datestr, enc, window.name);
497 parent.left_nav.loadFrame('enc2', window.name, 'patient_file/encounter/encounter_top.php?set_encounter=' + enc);
498 return;
500 // otherwise just continue normally
502 <?php
504 // Search for an encounter from today
505 $erow = sqlQuery("SELECT fe.encounter, fe.date " .
506 "FROM form_encounter AS fe, forms AS f WHERE " .
507 "fe.pid = ? " .
508 " AND fe.date >= ? " .
509 " AND fe.date <= ? " .
510 " AND " .
511 "f.formdir = 'newpatient' AND f.form_id = fe.id AND f.deleted = 0 " .
512 "ORDER BY fe.encounter DESC LIMIT 1", array($pid,date('Y-m-d 00:00:00'),date('Y-m-d 23:59:59')));
514 if (!empty($erow['encounter'])) {
515 // If there is an encounter from today then present the duplicate visit dialog
516 echo "duplicateVisit('" . $erow['encounter'] . "', '" .
517 oeFormatShortDate(substr($erow['date'], 0, 10)) . "');\n";
522 <?php if ($GLOBALS['enable_group_therapy']) { ?>
523 /* hide / show group name input */
524 var groupCategories = <?php echo json_encode($therapyGroupCategories); ?>;
525 $('#pc_catid').on('change', function () {
526 if(groupCategories.indexOf($(this).val()) > -1){
527 $('#therapy_group_name').show();
528 } else {
529 $('#therapy_group_name').hide();
533 function sel_group() {
534 top.restoreSession();
535 var url = '<?php echo $GLOBALS['webroot']?>/interface/main/calendar/find_group_popup.php';
536 dlgopen(url, '_blank', 500, 400);
538 // This is for callback by the find-group popup.
539 function setgroup(gid, name) {
540 var f = document.forms[0];
541 f.form_group.value = name;
542 f.form_gid.value = gid;
545 <?php if ($viewmode && in_array($result['pc_catid'], $therapyGroupCategories)) {?>
546 $('#therapy_group_name').show();
547 <?php } ?>
548 <?php } ?>
549 </script>
551 </html>