AMC Reporting Module: adding rules
[openemr.git] / library / ajax / left_nav_encounter_ajax.php
blobc8d9d7721bfbc1bc2d73435f1342910ce66ab576
1 <?php
2 // Copyright (C) 2011 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 $sanitize_all_escapes = true;
10 $fake_register_globals = false;
12 require_once("../../interface/globals.php");
13 require_once("$srcdir/forms.inc");
15 /*********************************************************************
16 // Get the patient's encounter ID for today, if it exists.
17 // In the case of more than one encounter today, pick the last one.
19 function todaysEncounterIf($patient_id) {
20 global $today;
21 $tmprow = sqlQuery("SELECT encounter FROM form_encounter WHERE " .
22 "pid = ? AND date = ? ORDER BY encounter DESC LIMIT 1",
23 array($patient_id, "$today 00:00:00"));
24 return empty($tmprow['encounter']) ? 0 : $tmprow['encounter'];
26 *********************************************************************/
28 // Get the patient's encounter ID for today, creating it if there is none.
30 function todaysEncounter($patient_id, $reason='') {
31 global $today, $userauthorized;
33 if (empty($reason)) $reason = xl('Please indicate visit reason');
35 // Was going to use the existing encounter for today if there is one, but
36 // decided it's right to always create a new one. Leaving the code here
37 // (and corresponding function above) in case it is ever wanted later.
38 /*******************************************************************
39 $encounter = todaysEncounterIf($patient_id);
40 if ($encounter) return $encounter;
41 *******************************************************************/
43 $tmprow = sqlQuery("SELECT username, facility, facility_id FROM users " .
44 "WHERE id = ?", array($_SESSION["authUserID"]));
45 $username = $tmprow['username'];
46 $facility = $tmprow['facility'];
47 $facility_id = $tmprow['facility_id'];
48 $conn = $GLOBALS['adodb']['db'];
49 $encounter = $conn->GenID("sequences");
50 $provider_id = $userauthorized ? $_SESSION['authUserID'] : 0;
51 addForm($encounter, "New Patient Encounter",
52 sqlInsert("INSERT INTO form_encounter SET date = ?, onset_date = ?, " .
53 "reason = ?, facility = ?, facility_id = ?, pid = ?, encounter = ?, " .
54 "provider_id = ?",
55 array($today, $today, $reason, $facility, $facility_id, $patient_id,
56 $encounter, $provider_id)
58 "newpatient", $patient_id, $userauthorized, "NOW()", $username
60 return $encounter;
63 $issue = $_GET['issue'];
64 $today = date('Y-m-d');
66 $irow = sqlQuery("SELECT title FROM lists WHERE id = ?", array($issue));
68 // Check if an encounter already exists for today.
69 // If yes, select the latest one.
70 // If not, create one and give it a title of the issue title.
71 $thisenc = todaysEncounter($pid, $irow['title']);
73 // If the encounter is not already linked to the specified issue, link it.
74 $tmp = sqlQuery("SELECT count(*) AS count FROM issue_encounter WHERE " .
75 "pid = ? AND list_id = ? AND encounter = ?",
76 array($pid, $issue, $thisenc));
77 if (empty($tmp['count'])) {
78 sqlStatement("INSERT INTO issue_encounter " .
79 "( pid, list_id, encounter ) VALUES ( ?, ?, ? )",
80 array($pid, $issue, $thisenc));
83 // Write JavaScript to open the selected encounter as the active encounter.
84 // Logic cloned from encounters.php.
86 top.restoreSession();
87 var enc = <?php echo $thisenc; ?>;
88 <?php if ($GLOBALS['concurrent_layout']) { ?>
89 setEncounter('<?php echo $today; ?>', enc, 'RBot');
90 setRadio('RBot', 'enc');
91 loadFrame2('enc2', 'RBot', 'patient_file/encounter/encounter_top.php?set_encounter=' + enc);
92 <?php } else { ?>
93 top.Title.location.href = '../encounter/encounter_title.php?set_encounter=' + enc;
94 top.Main.location.href = '../encounter/patient_encounter.php?set_encounter=' + enc;
95 <?php } ?>