increment database version for previous theme bug fix
[openemr.git] / contrib / util / import_mi2xml.php
blob99cd543dd877ff076a821b6f271ace8ac3b8c1ca
1 <?php
2 // Import of XML data from XML files created by "MEDICS" offine EMR application
3 // development by mi-squared.com -2010 licences under GPL v2 or greater
4 //
5 // Provided as an example of how to import complete medical records into OpenEMR
6 // from an XML formatted file
7 //
8 // This is intended to be run by a automated command line process/cron job not a web request
9 // Based on OpenEMR 3.1 release
10 // See import_mi2xml-xpaths.txt for a field structure details
12 $ignoreAuth = true;
13 include_once("../interface/globals.php");
14 include_once("$srcdir/patient.inc");
15 include_once("$srcdir/forms.inc");
16 include_once("$srcdir/acl.inc");
18 // set name of XML file
19 if ( $argc < 1 ) {
20 echo "No XML file path provided.\n";
21 exit(-1);
24 array_shift($argv);
25 $file = $argv[0];
27 // load file
28 $medics = simplexml_load_file($file) or die ("Unable to load XML file!");
30 ////////////////////////////////////////////////////////////////////
31 process_medics( $medics );
32 ////////////////////////////////////////////////////////////////////
34 ////////////////////////////////////////////////////////////////////
35 // FUNCTION START
36 ////////////////////////////////////////////////////////////////////
38 function process_medics ( $medics ) {
40 // add patient
41 // - medical history
42 // - family/social history
44 $errors = array();
45 $msg = create_patient( $medics, $errors );
46 $has_errors = sizeof($errors) > 0;
47 if ( $has_errors ) {
48 render_errors( $errors );
49 return;
50 } else {
51 echo $msg;
55 // add encounter:
56 // - encounter form
57 // - vitals
59 $errors = array();
60 $msg = add_encounter( $medics, $errors );
61 $has_errors = sizeof($errors) > 0;
62 if ( $has_errors ) {
63 render_errors( $errors );
64 return;
65 } else {
66 echo $msg;
72 function render_errors( &$errors ) {
73 $first = true;
74 foreach( $errors as $error ) {
75 if ( !$first ) {
76 echo "|";
78 echo "ERR[$error]";
79 $first = false;
83 function create_patient( &$medics, &$errors ) {
85 $alertmsg = "";
86 $patient_pid = get_patientid($medics);
88 $pubpid = trim( $medics->pubpid );
90 // ID must be valid or 'NEWPATIENT'
91 if (empty($pubpid)) {
92 array_push( $errors, "Patient ID '$pubpid' missing, patient skipped! ");
93 return $alertmsg;
96 if ( $pubpid != 'NEWPATIENT') {
98 // 1. validate patient
99 $patient_pid = 0;
100 $query = "SELECT pid FROM patient_data WHERE pubpid LIKE '$pubpid'";
101 $res = sqlStatement($query);
102 $row = sqlFetchArray($res);
104 if ($row) {
105 $patient_pid = $row['pid'];
106 if (sqlFetchArray($res)) {
107 array_push( $errors, "Patient ID '$pubpid' is ambiguous, patient skipped! ");
108 return $alertmsg;
109 } else {
110 // array_push( $errors, "Patient ID '$pubpid' exists, updates/new encounters only. ");
115 // 2. validate insurance provider - REMOVED
117 // 3. validate billing provider
119 $tmp = array();
120 $tmp = fetchProviderInfo($medics);
121 if (!array($tmp)) {
122 array_push( $errors, "Provider '$tmp' not found, patient skipped!");
123 return $alertmsg;
126 $patient_provider_id = $tmp['id'];
127 $patient_provider_name = $tmp['username'];
128 $patient_provider_facility = $tmp['facility_id'];
130 // 4. get facility from // Move to function
131 $row = sqlQuery("SELECT id, name, pos_code FROM facility WHERE id = '$patient_provider_facility'" );
133 if (!$row['id']) {
134 array_push( $errors, "Facility '$tmp' not found, patient skipped! ");
135 return $alertmsg;
137 $patient_facility_id = $row['id'];
138 $patient_facility_name = $row['name'];
139 $patient_facility_pos = $row['pos_code'];
141 // 5. insert patient data
142 if (!$patient_pid) {
144 // Insert into patient_data.
146 $row = sqlQuery("SELECT max(pid)+1 AS pid FROM patient_data");
147 $patient_pid = $row['pid'] ? $row['pid'] : 1;
149 // Combine street lines
150 $patient_street = $medics->street . ' ' . $medics->street2;
151 // Build array
152 newPatientData(
153 '', // id
154 '', // title
155 form2db($medics->fname), // fname
156 form2db($medics->lname), // lname
157 form2db($medics->mname), // mname
158 sex($medics->sex), // sex
159 form2db($medics->dob), // dob
160 form2db($patient_street), // street
161 '', // Dutch: nstreet
162 '', // Dutch: nnr
163 '', // Dutch: nadd
164 form2db($medics->zip), // zip
165 form2db($medics->city), // city
166 form2db($medics->state), // state
167 '', // country
168 '', // ssn
169 '', // occupation
170 form2db($medics->phone_home), // phone_home
171 form2db($medics->phone_alternate), // phone_biz
172 '', // phone_contact
173 '', // status
174 '', // contact_relationship
175 '', // referrer
176 '', // referrerID
177 '', // email
178 '', // language
179 form2db($medics->ethnicity), // ethnoracial
180 '', // interpreter
181 '', // migrantseasonal
182 '', // family_size
183 '', // monthly_income
184 '', // homeless
185 '0000-00-00 00:00:00', // financial_review
186 $patient_pid, // pubpid - use PID when NEWPATIENT
187 $patient_pid, // pid
188 '', // providerID
189 '', // genericname1
190 '', // genericval1
191 '', // genericname2
192 '', // genericval2
193 '', // phone_cell
194 form2db($medics->hippa_notice), // hipaa_mail
195 form2db($medics->hippa_notice), // hipaa_voice
196 '', // squad
197 '', // pharmacy_id
198 '', // drivers_license
199 form2db($medics->hippa_notice), // hipaa_notice
200 '', // $hipaa_message
201 $dos = fixDate($medics->fromdate) // regdate
204 // Insert dummy row for employer_data.
205 newEmployerData($patient_pid);
207 // Update or Instest subscriber ins data
208 if( ($medics->pubpid == 'NEWPATIENT') || (!empty($medics->policy_id) ) ) {
209 newInsuranceData(
210 $patient_pid,
211 'primary',
212 $insurance_company_id, // (insurance) provider
213 form2db($medics->policy_id), // policy_number - same as pt identifier?
214 '', // group_number - anything special here?
215 '', // plan_name - anything special here?
216 form2db($medics->lname), // subscriber_lname
217 form2db($medics->mname), // subscriber_mname
218 form2db($medics->fname), // subscriber_fname
219 'self', // subscriber_relationship
220 '', // subscriber_ss
221 fixDate($medics->dob), // subscriber_DOB
222 form2db($medics->street), // subscriber_street
223 form2db($medics->zip), // subscriber_postal_code
224 form2db($medics->city), // subscriber_city
225 form2db($medics->state), // subscriber_state
226 '', // subscriber_country
227 form2db($medics->phone_home), // subscriber_phone
228 '', // subscriber_employer
229 '', // subscriber_employer_street
230 '', // subscriber_employer_city
231 '', // subscriber_employer_postal_code
232 '', // subscriber_employer_state
233 '', // subscriber_employer_country
234 '', // copay
235 sex($medics->sex), // subscriber_sex
236 fixDate($medics->eff_date) // effective date
240 $tmp = $medics->lname . ',' . $medics->fname;
241 $alertmsg .= "New Patient Added: '$patient_pid' / '$tmp' <br>\n";
244 $medics->pid = $patient_pid;
246 $history = array(
247 'history_father' => form2db($medics->familyinformation->father),
248 'history_mother' => form2db($medics->familyinformation->mother),
249 'history_spouse' => form2db($medics->familyinformation->spouse),
250 'history_siblings' => form2db($medics->familyinformation->siblings),
251 'history_offspring' => form2db($medics->familyinformation->offspring),
252 'relatives_cancer' => form2db($medics->medical->relativesexperience->cancer),
253 'relatives_tuberculosis' => form2db($medics->medical->relativesexperience->tuberculosis),
254 'relatives_diabetes' => form2db($medics->medical->relativesexperience->diabetes),
255 'relatives_high_blood_pressure' => form2db($medics->medical->relativesexperience->highbloodpressure),
256 'relatives_heart_problems' => form2db($medics->medical->relativesexperience->heartproblems),
257 'relatives_stroke' => form2db($medics->medical->relativesexperience->stroke),
258 'relatives_epilepsy' => form2db($medics->medical->relativesexperience->epilepsy),
259 'relatives_mental_illness' => form2db($medics->medical->relativesexperience->mentalillness),
260 'relatives_suicide' => form2db($medics->medical->relativesexperience->suicide),
261 'usertext12' => form2db($medics->medical->relativesexperience->other),
262 'coffee' => form2db($medics->medical->lifestyleusage->coffee),
263 'tobacco' => form2db($medics->medical->lifestyleusage->tobacco),
264 'alcohol' => form2db($medics->medical->lifestyleusage->alcohol),
265 'sleep_patterns' => form2db($medics->medical->lifestyleusage->sleep),
266 'exercise_patterns' => form2db($medics->medical->lifestyleusage->exercise),
267 'seatbelt_use' => form2db($medics->medical->lifestyleusage->seatbelt),
268 'counseling' => form2db($medics->medical->lifestyleusage->counseling),
269 'hazardous_activities' => form2db($medics->medical->lifestyleusage->hazardactivities),
270 'usertext13' => form2db($medics->medical->lifestyleusage->urinaryincontinence),
271 'usertext14' => form2db($medics->medical->lifestyleusage->fallhistory),
272 'additional_history' => form2db($medics->medical->lifestyleusage->other) . " " .
273 form2db($medics->medical->lifestyleusage->generalnotes)
276 // Insert/Update into history_data.
277 if ($medics->pubpid == 'NEWPATIENT') {
278 newHistoryData($patient_pid, $history);
279 } else {
280 updateHistoryData($patient_pid, $history);
283 // Add or Update History data
284 add_update_history($medics, $patient_pid, $errors);
286 // Create or update an issue for each historical medication.
288 foreach ($medics->medical->medications->medication as $medication) {
290 if (isempty($medication->name)) continue;
292 $meds = array();
293 $meds['title'] = form2db($medication->name);
294 $meds['dosage'] = form2db($medication->dosage);
295 $meds['frequency'] = form2db($medication->frequency);
296 $meds['duration'] = form2db($medication->duration); // TBD does not exsist in MEDICS
297 $meds['id'] = form2db($medication->id);
299 if ( !isempty($meds['id']) ) {
300 $row = sqlQuery("SELECT id FROM lists WHERE id = " . $meds['id'] );
301 if (!$row ) {
302 create_issue($patient_pid, 'medication', $meds );
303 } else {
304 update_issue($patient_pid, 'medication', $meds );
306 } else {
307 create_issue($patient_pid, 'medication', $meds );
311 return $alertmsg;
314 // Create a new issue in the lists table.
316 function create_issue($pid, $type, $fields) {
318 if ( !isempty( $fields['title'] ) ) {
319 echo "\nAdding new issue '" . $fields['title'] . "'";
321 sqlInsert("INSERT INTO lists SET " .
322 "date = NOW(), " .
323 "pid = '$pid', " .
324 "type = '$type', " .
325 "title = '" . $fields['title'] . "', " .
326 "activity = 1," .
327 "user = '" . $$_SESSION['authUser'] . "', " .
328 "groupname = '" . $$_SESSION['authProvider'] . "', " .
329 "outcome = '', " .
330 "destination = '', " .
331 "frequency = '" . $fields['frequency'] . "', " .
332 "dosage = '" . $fields['dosage'] . "', " .
333 "duration = '" . $fields['duration'] . "'"
335 } else {
336 echo "Skipping field creation with empty title.";
342 // update issue in the lists table.
344 function update_issue($pid, $type, $fields) {
346 echo "\nUpdating issue id " . $fields['id'] . " ('" . $fields['title'] . "')";
348 sqlInsert("update lists SET " .
349 "date = NOW(), " .
350 "pid = '$pid', " .
351 "type = '$type', " .
352 "title = '" . $fields['title'] . "', " .
353 "activity = 1," .
354 "user = '" . $$_SESSION['authUser'] . "', " .
355 "groupname = '" . $$_SESSION['authProvider'] . "', " .
356 "outcome = '', " .
357 "destination = '', " .
358 "frequency = '" . $fields['frequency'] . "', " .
359 "dosage = '" . $fields['dosage'] . "', " .
360 "duration = '" . $fields['duration'] . "' " .
361 "WHERE id = " . $fields['id']
366 function add_update_history ($medics, $patient_pid, &$errors) {
367 $msg;
368 $dos = fixDate($medics->fromdate);
370 if (!isempty($medics->medical->medicalhistory->medicationnotes) ||
371 !isempty($medics->medical->medicalhistory->allergies) ||
372 !isempty($medics->medical->medicalhistory->history) ||
373 !isempty($medics->medical->medicalhistory->surgicalhistory) ||
374 !isempty($medics->medical->medicalhistory->preventative)) {
376 $row = sqlQuery("SELECT pid FROM aperio_medical_history WHERE pid = '$patient_pid'");
377 if (!$row) {
378 sqlInsert("INSERT INTO aperio_medical_history SET " .
379 "pid = '$patient_pid', " .
380 "medication = '" . $medics->medical->medicalhistory->medicationnotes ."', " .
381 "allergies = '" . $medics->medical->medicalhistory->allergies ."', " .
382 "medical = '" . $medics->medical->medicalhistory->history ."', " .
383 "surgical = '" . $medics->medical->medicalhistory->surgicalhistory ."', " .
384 "preventative = '" . $medics->medical->medicalhistory->preventative ."' "
385 // . "updated = '$dos'"
387 // return( "Created history form for Patient ID:" . $patient_pid);
388 } else {
389 sqlStatement("UPDATE aperio_medical_history SET " .
390 "medication = '" . $medics->medical->medicalhistory->medicationnotes ."', " .
391 "allergies = '" . $medics->medical->medicalhistory->allergies ."', " .
392 "medical = '" . $medics->medical->medicalhistory->history ."', " .
393 "surgical = '" . $medics->medical->medicalhistory->surgicalhistory ."', " .
394 "preventative = '" . $medics->medical->medicalhistory->preventative ."' " .
395 // "updated = '$dos'" .
396 " WHERE pid = '$patient_pid'"
398 // return("Updated history form for Patient ID:" . $patient_pid);
402 // return("No History form created for Patient ID:" . $patient_pid);
406 function add_encounter( $medics, &$errors ) {
407 $msg;
409 $patient_pid = $medics->pid;
410 $dos = fixDate($medics->fromdate);
411 $encounter_id = $GLOBALS['adodb']['db']->GenID('sequences');
412 $encounter_reason = form2db($medics->chiefcomplaint);
413 addForm($encounter_id, "New Patient Encounter",
414 sqlInsert("INSERT INTO form_encounter SET " .
415 "date = '$dos', " .
416 "onset_date = '$dos', " .
417 "reason = '$encounter_reason', " .
418 "facility = '$patient_facility_name', " .
419 "facility_id = '$patient_facility_id', " .
420 "sensitivity = 'normal', " .
421 "pid = '$patient_pid', " .
422 "encounter = '$encounter_id'"
424 "newpatient", $patient_pid, 1, $dos
427 $msg .= "Created new encounter for " . $medics->pubpid . ".<br>\n";
429 // Custom Forms
431 $msg .= add_ros_subj ( $patient_pid, $dos, $encounter_id, $medics, $errors ); //ROS
432 $msg .= add_phyexam_obj( $patient_pid, $dos, $encounter_id, $medics, $errors ); //PE
433 $msg .= add_assessment_plan( $patient_pid, $dos, $encounter_id, $medics, $errors ); //Assessment/Plan
434 $msg .= add_vitals( $patient_pid, $dos, $encounter_id, $medics, $errors ); //
435 $msg .= add_other_dx( $patient_pid, $dos, $encounter_id, $medics, $errors ); //Other DX
437 // Fee Sheet
438 $msg .= add_billing_records( $patient_pid, $dos, $encounter_id, $medics, $errors );
441 return $msg;
444 function isempty( $str ) {
445 return $str == null || empty($str);
447 function add_ros_subj( $patient_pid, $dos, $encounter_id, $medics, &$errors ) {
449 if (
450 !isempty($medics->medical->subjective->general) ||
451 !isempty($medics->medical->subjective->neurological) ||
452 !isempty($medics->medical->subjective->heent) ||
453 !isempty($medics->medical->subjective->respiratory) ||
454 !isempty($medics->medical->subjective->cardio) ||
455 !isempty($medics->medical->subjective->gastro) ||
456 !isempty($medics->medical->subjective->skin) ||
457 !isempty($medics->medical->subjective->extremities) ||
458 !isempty($medics->medical->subjective->subjective)
461 $row = fetchProviderInfo($medics);
463 if ($row['id']) { // TBD array error check NEEDED
464 $patient_provider_name = $row['username'];
467 addForm($encounter_id, "ROS",
468 sqlInsert("INSERT INTO form_aperio_ros SET " .
469 "date = '$dos', " .
470 "pid = '$patient_pid', " .
471 "authorized = 1, " .
472 "activity = 1, " .
473 "general = '" . form2db($medics->medical->subjective->general) . "', " .
474 "neurological = '" . form2db($medics->medical->subjective->neurological) . "', " .
475 "heent = '" . form2db($medics->medical->subjective->heent) . "', " .
476 "respiratory = '" . form2db($medics->medical->subjective->respiratory) . "', " .
477 "cardio = '" . form2db($medics->medical->subjective->cardio) . "', " .
478 "gastro = '" . form2db($medics->medical->subjective->gastro) . "', " .
479 "skin = '" . form2db($medics->medical->subjective->skin) . "', " .
480 "extremities = '" . form2db($medics->medical->subjective->extremities) . "', " .
481 "subjective = '" . form2db($medics->medical->subjective->subjective) . "'"
483 "aperio_ros", $patient_pid, 1, $dos, $patient_provider_name
486 return "Added ROS for Patient ID " . $patient_pid . ".";
490 function add_phyexam_obj( $patient_pid, $dos, $encounter_id, $medics, &$errors ) {
492 if (!isempty($medics->medical->objective->general) ||
493 !isempty($medics->medical->objective->neurological) ||
494 !isempty($medics->medical->objective->heent) ||
495 !isempty($medics->medical->objective->respiratory) ||
496 !isempty($medics->medical->objective->cardio) ||
497 !isempty($medics->medical->objective->gastro) ||
498 !isempty($medics->medical->objective->skin) ||
499 !isempty($medics->medical->objective->extremities) ||
500 !isempty($medics->medical->objective->objective))
503 $row = fetchProviderInfo($medics);
504 if ($row['id']) { // TBD array error check NEEDED
505 $patient_provider_name = $row['username'];
508 addForm($encounter_id, "Physical Exam",
509 sqlInsert("INSERT INTO form_aperio_pe SET " .
510 "date = '$dos', " .
511 "pid = '$patient_pid', " .
512 "authorized = 1, " .
513 "activity = 1, " .
514 "general = '" . form2db($medics->medical->objective->general) . "', " .
515 "neurological = '" . form2db($medics->medical->objective->neurological) . "', " .
516 "heent = '" . form2db($medics->medical->objective->heent) . "', " .
517 "respiratory = '" . form2db($medics->medical->objective->respiratory) . "', " .
518 "cardio = '" . form2db($medics->medical->objective->cardio) . "', " .
519 "gastro = '" . form2db($medics->medical->objective->gastro) . "', " .
520 "skin = '" . form2db($medics->medical->objective->skin) . "', " .
521 "extremities = '" . form2db($medics->medical->objective->extremities) . "', " .
522 "objective = '" . form2db($medics->medical->objective->objective) . "'"
524 "aperio_pe", $patient_pid, 1, $dos, $patient_provider_name
527 return "Added PE for Patient ID " . $patient_pid . ".";
532 function add_assessment_plan( $patient_pid, $dos, $encounter_id, $medics, &$errors ) {
534 if (!isempty($medics->medical->systems->assessment) ||
535 !isempty($medics->medical->systems->plan))
537 $row = fetchProviderInfo($medics);
538 if ($row['id']) { // TBD array error check NEEDED
539 $patient_provider_name = $row['username'];
542 addForm($encounter_id, "Assessment-Plan",
543 sqlInsert("INSERT INTO form_aperio_ap SET " .
544 "date = '$dos', " .
545 "pid = '$patient_pid', " .
546 "authorized = 1, " .
547 "activity = 1, " .
548 "assessment = '" . form2db($medics->medical->systems->assessment) . "', " .
549 "plan = '" . form2db($medics->medical->systems->plan) . "'"
551 "aperio_ap", $patient_pid, 1, $dos, $patient_provider_name
554 return "Added Assessment for Patient ID " . $patient_pid . ".";
558 function add_other_dx( $patient_pid, $dos, $encounter_id, $medics, &$errors ) {
560 if (!isempty($medics->medical->systems->otherdx)) {
561 $row = fetchProviderInfo($medics);
562 if ($row['id']) { // TBD array error check NEEDED
563 $patient_provider_name = $row['username'];
566 addForm($encounter_id, "Other DX",
567 sqlInsert("INSERT INTO form_aperio_opd SET " .
568 "date = '$dos', " .
569 "pid = '$patient_pid', " .
570 "authorized = 1, " .
571 "activity = 1, " .
572 "opd = '" . form2db($medics->medical->systems->otherdx) . "'"
574 "aperio_opd", $patient_pid, 1, $dos, $patient_provider_name
577 return "Added OtherDX for Patient ID " . $patient_pid . ".";
582 function add_vitals( $patient_pid, $dos, $encounter_id, $medics, &$errors ) {
583 $bmi;
584 $bmi_status;
586 // Create Vitals form.
587 if (!empty($medics->medical->physicalexamsvitals->bps)) {
589 /* DISABLED
590 // Calculate BMI_status
591 if ($medics->medical->physicalexamsvitals->weight > 0 &&
592 $medics->medical->physicalexamsvitals->height > 0) {
593 $bmi = ($weight/$height/$height)*703;
594 if ( $bmi > 42 ) $bmi_status = 'Obesity III';
595 elseif ( $bmi > 34 ) $bmi_status = 'Obesity II';
596 elseif ( $bmi > 30 ) $bmi_status = 'Obesity I';
597 elseif ( $bmi > 27 ) $bmi_status = 'Overweight';
598 elseif ( $bmi > 25 ) $bmi_status = 'Normal BL';
599 elseif ( $bmi > 18.5 ) $bmi_status = 'Normal';
600 elseif ( $bmi > 10 ) $bmi_status = 'Underweight';
603 addForm($encounter_id, "Vitals",
604 sqlInsert("INSERT INTO form_vitals SET " .
605 "date = '$dos', " .
606 "pid = '$patient_pid', " .
607 "user = '" . form2db($patient_provider_name) . "', " .
608 "groupname = '', " .
609 "authorized = 1, " .
610 "activity = 1, " .
611 "mentalstate = '" . form2db($medics->medical->physicalexamsvitals->mentalstatus) . "', " .
612 "bps = '" . form2db($medics->medical->physicalexamsvitals->bps) . "', " .
613 "bpd = '" . form2db($medics->medical->physicalexamsvitals->bpd) . "', " .
614 "weight = '" . form2db($medics->medical->physicalexamsvitals->weight) . "', " .
615 "height = '" . form2db($medics->medical->physicalexamsvitals->height) . "', " .
616 "temperature = '" . form2db($medics->medical->physicalexamsvitals->temperature). "', " .
617 "temp_method = '" . form2db($medics->medical->physicalexamsvitals->tempmethods). "', " .
618 "pulse = '" . form2db($medics->medical->physicalexamsvitals->pulse) . "', " .
619 "respiration = '" . form2db($medics->medical->physicalexamsvitals->respiration). "', " .
620 "BMI = '" . form2db($medics->medical->physicalexamsvitals->bmi) . "', " .
621 "BMI_status = '$bmi_status', " .
622 "waist_circ = '" . form2db($medics->medical->physicalexamsvitals->waistcirc) . "', " .
623 "head_circ = '" . form2db($medics->medical->physicalexamsvitals->headcirc) . "', " .
624 "oxygen_saturation = '" . form2db($medics->medical->physicalexamsvitals->o2) . "'"
626 "vitals", $patient_pid, 1, $dos
629 return "Created vitals form for Patient ID " . $patient_pid . ".\n";
632 return "No vitals form created for Patient ID " . $patient_pid . ".\n";
635 function add_billing_records( $patient_pid, $dos, $encounter_id, $medics, &$errors ) {
637 $row = fetchProviderInfo($medics);
638 $patient_provider_id = $row['id'];
640 $diags = array();
642 for ( $diag = 0; $diag < 15; $diag++ ) {
644 $_code = trim($medics->medical->systems->diagnosis[$diag]->code);
645 $_codenote = trim($medics->medical->systems->diagnosis[$diag]->codenote);
646 $_codestatus = trim($medics->medical->systems->diagnosis[$diag]->codestatus);
648 if (empty($_code)) { continue; }
649 $diags[] = $_code;
651 echo "\n---> ::: $_code $_codenote $_codestatus\n";
652 add_billing(
653 $patient_pid,
654 $encounter_id,
655 $patient_provider_id,
656 'ICD9',
657 $_code,
658 $_codenote . ' (' . $_codestatus . ')'
663 if (!empty($medics->medical->billable)) {
665 $cptCode = $medics->medical->billable->service[0]->code;
666 $codeNote= $medics->medical->billable->service[0]->codenote;
668 for ($justify = '', $j = 0; $j < 4 && $j < count($diags); ++$j) {
669 $justify .= $diags[$j] . ':';
672 add_billing($patient_pid, $encounter_id, $patient_provider_id,
673 'CPT4', $cptCode, $codeNote, $justify);
677 // Write a row to the billing table.
679 function add_billing($pid, $encounter, $provider, $codetype, $code,
680 $description, $justify='', $modifier='', $units=1) {
682 // echo "\n ADD BILLING: ---> $pid $encounter $provider $codetype $code $description $justify $modifier $units\n";
684 // Get the fee from the codes table.
685 $fee = 0;
686 if ($codetype == 'CPT4') {
687 $query = "SELECT fee FROM codes WHERE code_type = 1 AND code = '$code' AND ";
688 if (empty($modifier))
689 $query .= "( modifier IS NULL OR modifier = '')";
690 else
691 $query .= "modifier = '$modifier'";
692 $row = sqlQuery($query);
693 if ($row['fee']) $fee = $row['fee'] * $units;
696 sqlInsert("INSERT INTO billing ( " .
697 "date, code_type, code, pid, provider_id, user, groupname, authorized, " .
698 "encounter, code_text, billed, activity, payer_id, modifier, units, " .
699 "fee, justify " .
700 ") VALUES ( " .
701 "NOW(), " .
702 "'$codetype', " .
703 "'$code', " .
704 "'$pid', " .
705 "'$provider', " .
706 "'" . $$_SESSION['authUser'] . "', " .
707 "'" . $$_SESSION['authProvider'] . "', " .
708 "1, " .
709 "'$encounter', " .
710 "'$description', " .
711 "0, " .
712 "1, " .
713 "'$insurance_company_id'," .
714 "'$modifier', " .
715 "'$units', " .
716 "'$fee', " .
717 "'$justify'" .
718 ")");
723 function get_patientid( $medics ) {
725 $pubpid = trim( $medics->pubpid );
726 $patient_pid = 0;
727 $query = "SELECT pid FROM patient_data WHERE pubpid LIKE '$pubpid'";
728 $res = sqlStatement($query);
729 $row = sqlFetchArray($res);
730 if ($row) {
731 $patient_pid = $row['pid'];
734 return $patient_pid;
738 // Encode a string from a form field for database writing.
740 function form2db($fldval) {
741 $fldval = trim($fldval);
742 if (!get_magic_quotes_gpc()) $fldval = addslashes($fldval);
743 return $fldval;
746 // Encode sex for OpenEMR compatibility.
748 function sex($insex) {
749 if (!empty($insex)) {
750 $insex = strtoupper(substr($insex, 0, 1));
751 if ($insex == 'M') return 'Male';
752 if ($insex == 'F') return 'Female';
754 return '';
757 function fetchProviderInfo($medics) {
758 $query = "SELECT id, username, facility_id FROM users WHERE npi = '" . $medics->provider_npi . "'";
759 $row = sqlQuery($query);
760 if (!$row['id']) {
761 array_push( $errors, "Provider '" . $medics->provider_npi . "' not found");
762 return $alertmsg;
764 return ($row);
769 # XPATHS used by import_mi2xml.php scripts
770 # Includes customized encounter forms (see the code) provided for example
772 # Some xpaths can be repeated many times, see comments below
774 # Provided by Medical Information Integration, LLC
775 # www.mi-squared.com
777 patient/pubpid
778 patient/insurance_id
779 patient/policy_id
780 patient/eff_date
781 patient/lname
782 patient/fname
783 patient/mname
784 patient/sex
785 patient/dob
786 patient/ethnicity
787 patient/street
788 patient/street2
789 patient/city
790 patient/state
791 patient/zip
792 patient/phone_home
793 patient/phone_alternate
794 patient/hippa_notice
795 patient/provider_npi
796 patient/provider_name
797 patient/fromdate
798 patient/chiefcomplaint
800 patient/familyinformation/father
801 patient/familyinformation/mother
802 patient/familyinformation/spouse
803 patient/familyinformation/siblings
804 patient/familyinformation/offspring
806 patient/medical/relativesexperience/cancer
807 patient/medical/relativesexperience/tuberculosis
808 patient/medical/relativesexperience/diabetes
809 patient/medical/relativesexperience/highbloodpressure
810 patient/medical/relativesexperience/heartproblems
811 patient/medical/relativesexperience/stroke
812 patient/medical/relativesexperience/epilepsy
813 patient/medical/relativesexperience/mentalillness
814 patient/medical/relativesexperience/suicide
815 patient/medical/relativesexperience/other
816 patient/medical/lifestyleusage/coffee
817 patient/medical/lifestyleusage/tobacco
818 patient/medical/lifestyleusage/alcohol
819 patient/medical/lifestyleusage/sleep
820 patient/medical/lifestyleusage/exercise
821 patient/medical/lifestyleusage/seatbelt
822 patient/medical/lifestyleusage/counseling
823 patient/medical/lifestyleusage/hazardactivities
824 patient/medical/lifestyleusage/urinaryincontinence
825 patient/medical/lifestyleusage/fallhistory
826 patient/medical/lifestyleusage/other
827 patient/medical/lifestyleusage/generalnotes
829 patient/medical/medicalhistory/medicationnotes
830 patient/medical/medicalhistory/allergies
831 patient/medical/medicalhistory/history
832 patient/medical/medicalhistory/surgicalhistory
833 patient/medical/medicalhistory/preventative
835 # Multiple segments supported in current script
837 patient/medical/medications/medication/name
838 patient/medical/medications/medication/dosage
839 patient/medical/medications/medication/frequency
840 patient/medical/medications/medication/id
842 # Custom Form
843 patient/medical/subjective/general
844 patient/medical/subjective/neurological
845 patient/medical/subjective/heent
846 patient/medical/subjective/respiratory
847 patient/medical/subjective/cardio
848 patient/medical/subjective/gastro
849 patient/medical/subjective/skin
850 patient/medical/subjective/extremities
851 patient/medical/subjective/subjective
853 # Custom Form
854 patient/medical/objective/general
855 patient/medical/objective/neurological
856 patient/medical/objective/heent
857 patient/medical/objective/respiratory
858 patient/medical/objective/cardio
859 patient/medical/objective/gastro
860 patient/medical/objective/skin
861 patient/medical/objective/extremities
862 patient/medical/objective/objective
864 # Custom Form
865 patient/medical/physicalexamsvitals/mentalstatus
866 patient/medical/physicalexamsvitals/bps
867 patient/medical/physicalexamsvitals/bpd
868 patient/medical/physicalexamsvitals/weight
869 patient/medical/physicalexamsvitals/height
870 patient/medical/physicalexamsvitals/temperature
871 patient/medical/physicalexamsvitals/tempmethods
872 patient/medical/physicalexamsvitals/pulse
873 patient/medical/physicalexamsvitals/respiration
874 patient/medical/physicalexamsvitals/bmi
875 patient/medical/physicalexamsvitals/waistcirc
876 patient/medical/physicalexamsvitals/headcirc
877 patient/medical/physicalexamsvitals/o2
879 # Custom Form
880 patient/medical/systems/assessment
881 patient/medical/systems/plan
883 # Multiple segments supported in current script
884 patient/medical/systems/diagnosis/code
885 patient/medical/systems/diagnosis/codenote
886 patient/medical/systems/diagnosis/codestatus
888 # Custom Form
889 patient/medical/systems/otherdx
891 # Multiple segments supported in current script
892 patient/medical/billable/service[1]/code
893 patient/medical/billable/service[1]/codenote
894 patient/medical/billable/service[1]/modifier