CCR Import, made changes per comments in github.
[openemr.git] / library / ajax / ccr_import_ajax.php
blob78c037b4ba90d388ef878f4612a26d37002e7a13
1 <?php
2 /**
3 * library/ccr_import_ajax.php Functions related to patient CCR/CCD/CCDA parsing.
5 * Functions related to patient CCR/CCD/CCDA parsing and insert/update to corresponding tables.
7 * Copyright (C) 2013 Z&H Consultancy Services Private Limited <sam@zhservices.com>
9 * LICENSE: This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 3
12 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
20 * @package OpenEMR
21 * @author Eldho Chacko <eldho@zhservices.com>
22 * @author Ajil P M <ajilpm@zhservices.com>
23 * @link http://www.open-emr.org
26 //SANITIZE ALL ESCAPES
27 $sanitize_all_escapes=true;
30 //STOP FAKE REGISTER GLOBALS
31 $fake_register_globals=false;
34 require_once(dirname(__FILE__) . "/../../interface/globals.php");
35 require_once(dirname(__FILE__) . "/../parse_patient_xml.php");
36 require_once(dirname(__FILE__) . "/../classes/Document.class.php");
37 require_once(dirname(__FILE__) . "/../classes/CouchDB.class.php");
39 if($_REQUEST["ccr_ajax"] == "yes"){
40 $doc_id = $_REQUEST["document_id"];
41 $d = new Document($doc_id);
42 $url = $d->get_url();
43 $storagemethod = $d->get_storagemethod();
44 $couch_docid = $d->get_couch_docid();
45 $couch_revid = $d->get_couch_revid();
46 if($storagemethod == 1){
47 $couch = new CouchDB();
48 $data = array($GLOBALS['couchdb_dbase'],$couch_docid);
49 $resp = $couch->retrieve_doc($data);
50 $content = $resp->data;
51 if($content=='' && $GLOBALS['couchdb_log']==1){
52 $log_content = date('Y-m-d H:i:s')." ==> Retrieving document\r\n";
53 $log_content = date('Y-m-d H:i:s')." ==> URL: ".$url."\r\n";
54 $log_content .= date('Y-m-d H:i:s')." ==> CouchDB Document Id: ".$couch_docid."\r\n";
55 $log_content .= date('Y-m-d H:i:s')." ==> CouchDB Revision Id: ".$couch_revid."\r\n";
56 $log_content .= date('Y-m-d H:i:s')." ==> Failed to fetch document content from CouchDB.\r\n";
57 $log_content .= date('Y-m-d H:i:s')." ==> Will try to download file from HardDisk if exists.\r\n\r\n";
58 $this->document_upload_download_log($d->get_foreign_id(),$log_content);
59 die(xlt("File retrieval from CouchDB failed"));
61 $content = base64_decode($content);
62 }else{
63 $url = preg_replace("|^(.*)://|","",$url);
64 $from_all = explode("/",$url);
65 $from_filename = array_pop($from_all);
66 $from_pathname_array = array();
67 for ($i=0;$i<$d->get_path_depth();$i++) {
68 $from_pathname_array[] = array_pop($from_all);
70 $from_pathname_array = array_reverse($from_pathname_array);
71 $from_pathname = implode("/",$from_pathname_array);
72 $temp_url = $GLOBALS['OE_SITE_DIR'] . '/documents/' . $from_pathname . '/' . $from_filename;
73 if (!file_exists($temp_url)) {
74 echo xlt('The requested document is not present at the expected location on the filesystem or there are not sufficient permissions to access it') . '.' . $temp_url;
75 }else{
76 $content = file_get_contents($temp_url);
79 //fields to which the corresponding elements are to be inserted
80 //format - level 1 key is the main tag in the XML eg:- //Problems or //Problems/Problem according to the content in the XML.
81 //level 2 key is 'table name:field name' and level 2 value is the sub tag under the main tag given in level 1 key
82 //eg:- 'Type/Text' if the XML format is '//Problems/Problem/Type/Text' or 'id/@extension' if it is an attribute
83 //level 2 key can be 'table name:#some value' for checking whether a particular tag exits in the XML section
84 $field_mapping = array(
85 '//Problems/Problem' => array(
86 'lists1:diagnosis' => 'Description/Code/Value',
87 'lists1:comments' => 'CommentID',
88 'lists1:activity' => 'Status/Text',
90 '//Alerts/Alert' => array(
91 'lists2:type' => 'Type/Text',
92 'lists2:diagnosis' => 'Description/Code/Value',
93 'lists2:date' => 'Agent/EnvironmentalAgents/EnvironmentalAgent/DateTime/ExactDateTime',
94 'lists2:title' => 'Agent/EnvironmentalAgents/EnvironmentalAgent/Description/Text',
95 'lists2:reaction' => 'Reaction/Description/Text',
97 '//Medications/Medication' => array(
98 'prescriptions:date_added' => 'DateTime/ExactDateTime',
99 'prescriptions:active' => 'Status/Text',
100 'prescriptions:drug' => 'Product/ProductName/Text',
101 'prescriptions:size' => 'Product/Strength/Value',
102 'prescriptions:unit' => 'Product/Strength/Units/Unit',
103 'prescriptions:form' => 'Product/Form/Text',
104 'prescriptions:quantity' => 'Quantity/Value',
105 'prescriptions:note' => 'PatientInstructions/Instruction/Text',
106 'prescriptions:refills' => 'Refills/Refill/Number',
108 '//Immunizations/Immunization' => array(
109 'immunizations:administered_date' => 'DateTime/ExactDateTime',
110 'immunizations:note' => 'Directions/Direction/Description/Text',
112 '//Results/Result' => array(
113 'procedure_result:date' => 'DateTime/ExactDateTime',
114 'procedure_type:name' => 'Test/Description/Text',
115 'procedure_result:result' => 'Test/TestResult/Value',
116 'procedure_result:range' => 'Test/NormalResult/Normal/Value',
117 'procedure_result:abnormal' => 'Test/Flag/Text',
119 '//Actors/Actor' => array(
120 'patient_data:fname' => 'Person/Name/CurrentName/Given',
121 'patient_data:lname' => 'Person/Name/CurrentName/Family',
122 'patient_data:DOB' => 'Person/DateOfBirth/ExactDateTime',
123 'patient_data:sex' => 'Person/Gender/Text',
124 'patient_data:abname' => 'InformationSystem/Name',
125 'patient_data:#Type' => 'InformationSystem/Type',
126 'patient_data:pubpid' => 'IDs/ID',
127 'patient_data:street' => 'Address/Line1',
128 'patient_data:city' => 'Address/City',
129 'patient_data:state' => 'Address/State',
130 'patient_data:postal_code' => 'Address/PostalCode',
131 'patient_data:phone_contact' => 'Telephone/Value',
134 if(!empty($content)){
135 $var = array();
136 $res = parseXmlStream($content,$field_mapping);
137 $var = array(
138 'approval_status' => 1,
139 'type' => 11,
140 'ip_address' => $_SERVER['REMOTE_ADDR'],
142 foreach($res as $sections=>$details){
143 foreach($details as $cnt=>$vals){
144 foreach($vals as $key=>$val){
145 if(array_key_exists('#Type',$res[$sections][$cnt])){
146 if($key == 'postal_code'){
147 $var['field_name_value_array']['misc_address_book'][$cnt]['zip'] = $val;
148 }elseif($key == 'phone_contact'){
149 $var['field_name_value_array']['misc_address_book'][$cnt]['phone'] = $val;
150 }elseif($key == 'abname'){
151 $values = explode(' ',$val);
152 if($values[0]){
153 $var['field_name_value_array']['misc_address_book'][$cnt]['lname'] = $values[0];
155 if($values[1]){
156 $var['field_name_value_array']['misc_address_book'][$cnt]['fname'] = $values[1];
158 }else{
159 $var['field_name_value_array']['misc_address_book'][$cnt][$key] = $val;
161 $var['entry_identification_array']['misc_address_book'][$cnt] = $cnt;
162 }else{
163 if($sections == 'lists1' && $key == 'activity'){
164 if($val == 'Active'){
165 $val = 1;
166 }else{
167 $val = 0;
170 if($sections == 'lists2' && $key == 'type'){
171 if(strpos($val,"-")){
172 $vals = explode("-",$val);
173 $val = $vals[0];
174 }else{
175 $val = "";
178 if($sections == 'prescriptions' && $key == 'active'){
179 if($val == 'Active'){
180 $val = 1;
181 }else{
182 $val = 0;
185 $var['field_name_value_array'][$sections][$cnt][$key] = $val;
186 $var['entry_identification_array'][$sections][$cnt] = $cnt;
189 if(array_key_exists('#Type',$var['field_name_value_array']['misc_address_book'][$cnt])){
190 unset($var['field_name_value_array']['misc_address_book'][$cnt]['#Type']);
194 $var['field_name_value_array']['documents'][0]['id'] = $doc_id;
195 insert_ccr_into_audit_data($var);
196 $d->update_imported($doc_id);
197 echo xlt('Successfully Imported the details. Please approve the patient from the Pending Approval Screen').'.';
198 }else{
199 exit(xlt('Could not read the file'));
201 exit;