2 include_once("{$GLOBALS['srcdir']}/sql.inc");
3 require_once(dirname(__FILE__) . "/classes/WSWrapper.class.php");
5 function getPatientData($pid, $given = "*, DATE_FORMAT(DOB,'%m/%d/%Y') as DOB_TS") {
6 $sql = "select $given from patient_data where pid='$pid' order by date DESC limit 0,1";
10 function getLanguages() {
11 $returnval = array('','english');
12 $sql = "select distinct lower(language) as language from patient_data";
13 $rez = sqlStatement($sql);
14 for($iter=0; $row=sqlFetchArray($rez); $iter++) {
15 if (($row["language"] != "english") && ($row["language"] != "")) {
16 array_push($returnval, $row["language"]);
24 function getInsuranceProviders() {
26 $sql = "select name, id from insurance_companies order by name, id";
27 $rez = sqlStatement($sql);
29 for($iter=0; $row=sqlFetchArray($rez); $iter++) {
30 $returnval[$row['id']] = $row['name'];
38 function getProviders() {
39 $returnval = array("");
40 $sql = "select fname,lname from users where authorized=1";
41 $rez = sqlStatement($sql);
42 for($iter=0; $row=sqlFetchArray($rez); $iter++) {
43 if (($row["fname"] != "") && ($row["lname"] != "")) {
44 array_push($returnval, $row["fname"] . " " . $row["lname"]);
52 function getProviderInfo($providerID = "%", $providers_only = true) {
54 if ($providers_only) {
55 $param1 = "AND authorized=1";
58 if ($providerID == "%") {
61 $query = "select distinct id,username,lname,fname, authorized, info, facility from users where id $command '" . mysql_real_escape_string($providerID) . "' " . $param1;
62 $rez = sqlStatement($query);
63 for($iter=0; $row=sqlFetchArray($rez); $iter++)
64 $returnval[$iter]=$row;
66 //if only one result returned take the key/value pairs in array [0] and merge them down the the base array so that $resultval[0]['key'] is also
67 //accessible from $resultval['key']
70 $akeys = array_keys($returnval[0]);
71 foreach($akeys as $key) {
73 $returnval[0][$key] = $returnval[0][$key];
79 //same as above but does not reduce if only 1 row returned
80 function getCalendarProviderInfo($providerID = "%", $providers_only = true) {
82 if ($providers_only) {
83 $param1 = "AND authorized=1";
86 if ($providerID == "%") {
89 $query = "select distinct id,username,lname,fname, authorized, info, facility from users where id $command '" . mysql_real_escape_string($providerID) . "' " . $param1;
91 $rez = sqlStatement($query);
92 for($iter=0; $row=sqlFetchArray($rez); $iter++)
93 $returnval[$iter]=$row;
100 function getProviderName($providerID) {
102 $pi = getProviderInfo($providerID);
104 if (strlen($pi[0]["lname"]) > 0) {
105 return $pi[0]['fname'] . " " . $pi[0]['lname'];
111 function getProviderId($providerName) {
112 $query = "select id from users where username = '". mysql_real_escape_string($providerName)."'";
114 $rez = sqlStatement($query);
115 for($iter=0; $row=sqlFetchArray($rez); $iter++)
116 $returnval[$iter]=$row;
121 function getEthnoRacials() {
122 $returnval = array("");
123 $sql = "select distinct lower(ethnoracial) as ethnoracial from patient_data";
124 $rez = sqlStatement($sql);
125 for($iter=0; $row=sqlFetchArray($rez); $iter++) {
126 if (($row["ethnoracial"] != "")) {
127 array_push($returnval, $row["ethnoracial"]);
135 function getHistoryData($pid, $given = "*")
137 $sql = "select $given from history_data where pid='$pid' order by date DESC limit 0,1";
138 return sqlQuery($sql);
141 function getInsuranceData($pid, $type = "primary", $given = "insd.*, DATE_FORMAT(subscriber_DOB,'%m/%d/%Y') as subscriber_DOB, ic.name as provider_name")
143 $sql = "select $given from insurance_data as insd left join insurance_companies as ic on ic.id = insd.provider where pid='$pid' and type='$type' order by date DESC limit 0,1";
144 return sqlQuery($sql);
147 function getInsuranceDataByDate( $pid, $date, $type, $given = "insd.*, DATE_FORMAT(subscriber_DOB,'%m/%d/%Y') as subscriber_DOB, ic.name as provider_name")
148 { //this must take the date in the following manner: YYYY-MM-DD
149 //this function recalls the insurance value that was most recently enterred from the
150 //given date. it will call up most recent records up to and on the date given,
151 //but not records enterred after the given date
152 $sql = "select $given from insurance_data as insd left join insurance_companies as ic on ic.id = provider where pid='$pid' and date_format(date,'%Y-%m-%d')<='$date' and type='$type' order by date DESC limit 0,1";
153 return sqlQuery($sql);
157 function getEmployerData($pid, $given = "*")
159 $sql = "select $given from employer_data where pid='$pid' order by date DESC limit 0,1";
160 return sqlQuery($sql);
163 function getPatientLnames($lname = "%", $given = "pid, id, lname, fname, mname, providerID, DATE_FORMAT(DOB,'%m/%d/%Y') as DOB_TS", $orderby = "lname ASC, fname ASC", $limit="all", $start="0")
166 $sql="select pid, MAX(id) as id from patient_data group by pid DESC order by pid ASC";
167 $res = sqlStatement($sql);
168 $sql="select $given from patient_data where lname like '$lname%' and (";
169 for ($iter = 0;$row = sqlFetchArray($res);$iter++)
170 $sql.=" id='{$row['id']}' or";
172 $sql = substr($sql, 0, -3) . ") order by $orderby";
174 $sql = substr($sql, 0, -5)."order by $orderby";
177 // WTF? That was a good way to create a 200KB sql statement.
179 $sql="select $given from patient_data where lname like '$lname%' " .
183 $sql .= " limit $start, $limit";
184 $rez = sqlStatement($sql);
186 for($iter=0; $row=sqlFetchArray($rez); $iter++)
187 $returnval[$iter]=$row;
192 function getPatientId($pid = "%", $given = "pid, id, lname, fname, mname, providerID, DATE_FORMAT(DOB,'%m/%d/%Y') as DOB_TS", $orderby = "lname ASC, fname ASC", $limit="all", $start="0")
195 $sql="select pid, MAX(id) as id from patient_data group by pid DESC order by pid ASC";
196 $res = sqlStatement($sql);
197 $sql="select $given from patient_data where pubpid like '$pid%' and (";
198 for ($iter = 0;$row = sqlFetchArray($res);$iter++)
199 $sql.=" id='{$row['id']}' or";
201 $sql = substr($sql, 0, -3) . ") order by $orderby";
203 $sql = substr($sql, 0, -5)."order by $orderby";
206 $sql = "select $given from patient_data where pubpid like '$pid%' " .
210 $sql .= " limit $start, $limit";
211 $rez = sqlStatement($sql);
212 for($iter=0; $row=sqlFetchArray($rez); $iter++)
213 $returnval[$iter]=$row;
218 function getPatientPID($pid = "%", $given = "pid, id, lname, fname, mname, providerID, DATE_FORMAT(DOB,'%m/%d/%Y') as DOB_TS", $orderby = "lname ASC, fname ASC", $limit="all", $start="0")
226 if (strstr($pid,"%"))
229 $sql="select $given from patient_data where pid $command '$pid' order by $orderby";
232 $sql .= " limit $start, $limit";
234 $rez = sqlStatement($sql);
235 for($iter=0; $row=sqlFetchArray($rez); $iter++)
236 $returnval[$iter]=$row;
242 function getPatientName($pid) {
245 $patientData = getPatientPID($pid);
246 if (empty($patientData[0]['lname']))
248 $patientName = $patientData[0]['lname'] . ", " . $patientData[0]['fname'];
253 function getPatientDOB($DOB = "%", $given = "pid, id, lname, fname, mname", $orderby = "lname ASC, fname ASC", $limit="all", $start="0")
256 $sql="select pid, MAX(id) as id from patient_data group by pid DESC order by pid ASC";
257 $res = sqlStatement($sql);
258 $sql="select $given from patient_data where DOB like '$DOB%' and (";
259 for ($iter = 0;$row = sqlFetchArray($res);$iter++)
260 $sql.=" id='{$row['id']}' or";
262 $sql = substr($sql, 0, -3) . ") order by $orderby";
264 $sql = substr($sql, 0, -5)."order by $orderby";
267 $DOB = fixDate($DOB, $DOB);
269 $sql="select $given from patient_data where DOB like '$DOB%' " .
273 $sql .= " limit $start, $limit";
275 $rez = sqlStatement($sql);
276 for($iter=0; $row=sqlFetchArray($rez); $iter++)
277 $returnval[$iter]=$row;
282 function getPatientSSN($ss = "%", $given = "pid, id, lname, fname, mname, providerID", $orderby = "lname ASC, fname ASC", $limit="all", $start="0")
285 $sql="select pid, MAX(id) as id from patient_data group by pid DESC order by pid ASC";
286 $res = sqlStatement($sql);
287 $sql="select $given from patient_data where ss like '$ss%' and (";
288 for ($iter = 0;$row = sqlFetchArray($res);$iter++)
289 $sql.=" id='{$row['id']}' or";
291 $sql = substr($sql, 0, -3) . ") order by $orderby";
293 $sql = substr($sql, 0, -5)."order by $orderby";
296 $sql="select $given from patient_data where ss like '$ss%' " .
300 $sql .= " limit $start, $limit";
302 $rez = sqlStatement($sql);
303 for($iter=0; $row=sqlFetchArray($rez); $iter++)
304 $returnval[$iter]=$row;
309 function getPatientIds($given = "pid, id, lname, fname, mname", $orderby = "id ASC", $limit="all", $start="0")
312 $sql="select pid, MAX(id) as id from patient_data group by pid DESC order by pid ASC";
313 $res = sqlStatement($sql);
314 $sql="select $given from patient_data where ";
315 for ($iter = 0;$row = sqlFetchArray($res);$iter++)
316 $sql.="id='{$row['id']}' or ";
317 $sql = substr($sql, 0, -3) . "order by $orderby";
320 $sql="select $given from patient_data order by $orderby";
323 $sql .= " limit $start, $limit";
325 $rez = sqlStatement($sql);
326 for($iter=0; $row=sqlFetchArray($rez); $iter++)
327 $returnval[$iter]=$row;
332 //----------------------input functions
333 function newPatientData( $db_id="",
351 $contact_relationship = "",
358 $migrantseasonal = "",
360 $monthly_income = "",
362 $financial_review = "",
373 $DOB = fixDate($DOB);
374 $query = ("replace into patient_data set
383 postal_code='$postal_code',
386 country_code='$country_code',
388 occupation='$occupation',
389 phone_home='$phone_home',
390 phone_biz='$phone_biz',
391 phone_contact='$phone_contact',
393 contact_relationship='$contact_relationship',
394 referrer='$referrer',
395 referrerID='$referrerID',
397 language='$language',
398 ethnoracial='$ethnoracial',
399 interpretter='$interpretter',
400 migrantseasonal='$migrantseasonal',
401 family_size='$family_size',
402 monthly_income='$monthly_income',
403 homeless='$homeless',
404 financial_review='$financial_review',
407 providerID = '$providerID',
408 genericname1 = '$genericname1',
409 genericval1 = '$genericval1',
410 genericname2 = '$genericname2',
411 genericval2 = '$genericval2',
412 phone_cell = '$phone_cell',
416 $id = sqlInsert($query);
417 $foo = sqlQuery("select pid from patient_data where id='$id' order by date limit 0,1");
419 sync_patient($id,$fname,$lname,$street,$city,$postal_code,$state,$phone_home,
425 // Supported input date formats are:
427 // mm/dd/yy (assumes 19yy)
429 // also mm-dd-yyyy, etc. and mm.dd.yyyy, etc.
431 function fixDate($date, $default="0000-00-00") {
432 $fixed_date = $default;
434 if (preg_match("'^[0-9]{1,4}[/.-][0-9]{1,2}[/.-][0-9]{1,4}$'", $date)) {
435 $dmy = preg_split("'[/.-]'", $date);
437 $fixed_date = sprintf("%04u-%02u-%02u", $dmy[0], $dmy[1], $dmy[2]);
439 if ($dmy[2] < 1000) $dmy[2] += 1900;
440 $fixed_date = sprintf("%04u-%02u-%02u", $dmy[2], $dmy[0], $dmy[1]);
448 function updatePatientData($pid,$new)
450 $real = getPatientData($pid);
451 $new['DOB'] = fixDate($new['DOB']);
452 while(list($key, $value) = each ($new))
453 $real[$key] = $value;
454 $real['date'] = "'+NOW()+'";
457 $sql = "insert into patient_data set ";
458 while(list($key, $value) = each($real))
459 $sql .= $key." = '$value', ";
460 $sql = substr($sql, 0, -2);
463 return sqlInsert($sql);
467 function newEmployerData( $pid,
476 return sqlInsert("insert into employer_data set
479 postal_code='$postal_code',
488 function updateEmployerData($pid,$new)
490 $real = getEmployerData($pid);
491 while(list($key, $value) = each ($new))
492 $real[$key] = $value;
493 $real['date'] = "'+NOW()+'";
496 $sql = "insert into employer_data set ";
497 while(list($key, $value) = each($real))
498 $sql .= $key." = '$value', ";
499 $sql = substr($sql, 0, -2);
502 return sqlInsert($sql);
505 function newInsuranceData( $pid,
511 $subscriber_lname = "",
512 $subscriber_mname = "",
513 $subscriber_fname = "",
514 $subscriber_relationship = "",
516 $subscriber_DOB = "",
517 $subscriber_street = "",
518 $subscriber_postal_code = "",
519 $subscriber_city = "",
520 $subscriber_state = "",
521 $subscriber_country = "",
522 $subscriber_phone = "",
523 $subscriber_employer = "",
524 $subscriber_employer_street = "",
525 $subscriber_employer_city = "",
526 $subscriber_employer_postal_code = "",
527 $subscriber_employer_state = "",
528 $subscriber_employer_country = "",
533 if (strlen($type) > 0) {
534 $query = "select * from insurance_data where type='" . $type . "' and pid = " . $pid . " limit 1";
539 $res = sqlQuery ($query);
542 $data['type'] = $type;
543 $data['provider'] = $provider;
544 $data['policy_number']=$policy_number;
545 $data['group_number']=$group_number;
546 $data['plan_name']=$plan_name;
547 $data['subscriber_lname']=$subscriber_lname;
548 $data['subscriber_mname']=$subscriber_mname;
549 $data['subscriber_fname']=$subscriber_fname;
550 $data['subscriber_relationship']=$subscriber_relationship;
551 $data['subscriber_ss']=$subscriber_ss;
552 $data['subscriber_DOB']=$subscriber_DOB;
553 $data['subscriber_street']=$subscriber_street;
554 $data['subscriber_postal_code']=$subscriber_postal_code;
555 $data['subscriber_city']=$subscriber_city;
556 $data['subscriber_state']=$subscriber_state;
557 $data['subscriber_country']=$subscriber_country;
558 $data['subscriber_phone']=$subscriber_phone;
559 $data['subscriber_employer']=$subscriber_employer;
560 $data['subscriber_employer_city']=$subscriber_employer_city;
561 $data['subscriber_employer_street']=$subscriber_employer_street;
562 $data['subscriber_employer_postal_code']=$subscriber_employer_postal_code;
563 $data['subscriber_employer_state']=$subscriber_employer_state;
564 $data['subscriber_employer_country']=$subscriber_employer_country;
565 $data['copay']=$copay;
566 $data['subscriber_sex']=$subscriber_sex;
568 $data['date']="NOW()";
569 // echo "updating<br><br>";
571 return updateInsuranceData($pid,$data);
574 return sqlInsert("insert into insurance_data set
576 provider='$provider',
577 policy_number='$policy_number',
578 group_number='$group_number',
579 plan_name='$plan_name',
580 subscriber_lname='$subscriber_lname',
581 subscriber_mname='$subscriber_mname',
582 subscriber_fname='$subscriber_fname',
583 subscriber_relationship='$subscriber_relationship',
584 subscriber_ss='$subscriber_ss',
585 subscriber_DOB='$subscriber_DOB',
586 subscriber_street='$subscriber_street',
587 subscriber_postal_code='$subscriber_postal_code',
588 subscriber_city='$subscriber_city',
589 subscriber_state='$subscriber_state',
590 subscriber_country='$subscriber_country',
591 subscriber_phone='$subscriber_phone',
592 subscriber_employer = '$subscriber_employer',
593 subscriber_employer_city='$subscriber_employer_city',
594 subscriber_employer_street='$subscriber_employer_street',
595 subscriber_employer_postal_code='$subscriber_employer_postal_code',
596 subscriber_employer_state='$subscriber_employer_state',
597 subscriber_employer_country='$subscriber_employer_country',
599 subscriber_sex='$subscriber_sex',
606 function updateInsuranceData($pid,$new)
608 $fields = sqlListFields("insurance_data");
610 $real = getInsuranceData($pid);
612 while(list($key, $value) = each ($new)) {
613 if (in_array($key,$fields)){
617 $real['date'] = "'+NOW()+'";
620 $sql = "replace into insurance_data set ";
621 while(list($key, $value) = each($use))
622 $sql .= $key." = '$value', ";
625 $sql = substr($sql, 0, -2);
629 return sqlInsert($sql);
633 function newHistoryData( $pid,
637 $sleep_patterns = "",
638 $exercise_patterns = "",
641 $hazardous_activities = "",
642 $last_breast_exam = "",
643 $last_mammogram = "",
644 $last_gynocological_exam = "",
645 $last_rectal_exam = "",
646 $last_prostate_exam = "",
647 $last_physical_exam = "",
648 $last_sigmoidoscopy_colonoscopy = "",
649 $history_mother = "",
650 $history_father = "",
651 $history_siblings = "",
652 $history_offspring = "",
653 $history_spouse = "",
654 $relatives_cancer = "",
655 $relatives_tuberculosis = "",
656 $relatives_diabetes = "",
657 $relatives_high_blood_pressure = "",
658 $relatives_heart_problems = "",
659 $relatives_stroke = "",
660 $relatives_epilepsy = "",
661 $relatives_mental_illness = "",
662 $relatives_suicide = "",
663 $cataract_surgery = "",
666 $cholecystestomy = "",
670 $hip_replacement = "",
671 $knee_replacement = "",
676 $additional_history = ""
679 return sqlInsert("insert into history_data set
683 sleep_patterns='$sleep_patterns',
684 exercise_patterns='$exercise_patterns',
685 seatbelt_use='$seatbelt_use',
686 counseling='$counseling',
687 hazardous_activities='$hazardous_activities',
688 last_breast_exam='$last_breast_exam',
689 last_mammogram='$last_mammogram',
690 last_gynocological_exam='$last_gynocological_exam',
691 last_rectal_exam='$last_rectal_exam',
692 last_prostate_exam='$last_prostate_exam',
693 last_physical_exam='$last_physical_exam',
694 last_sigmoidoscopy_colonoscopy='$last_sigmoidoscopy_colonoscopy',
695 history_mother='$history_mother',
696 history_father='$history_father',
697 history_siblings='$history_siblings',
698 history_offspring='$history_offspring',
699 history_spouse='$history_spouse',
700 relatives_cancer='$relatives_cancer',
701 relatives_tuberculosis ='$relatives_tuberculosis',
702 relatives_diabetes='$relatives_diabetes',
703 relatives_high_blood_pressure='$relatives_high_blood_pressure',
704 relatives_heart_problems='$relatives_heart_problems',
705 relatives_stroke='$relatives_stroke',
706 relatives_epilepsy='$relatives_epilepsy',
707 relatives_mental_illness='$relatives_mental_illness',
708 relatives_suicide='$relatives_suicide',
709 cataract_surgery='$cataract_surgery',
710 tonsillectomy='$tonsillectomy',
711 appendectomy='$appendectomy',
712 cholecystestomy='$cholecystestomy',
713 heart_surgery='$heart_surgery',
714 hysterectomy='$hysterectomy',
715 hernia_repair='$hernia_repair',
716 hip_replacement='$hip_replacement',
717 knee_replacement='$knee_replacement',
719 value_1 = '$value_1',
721 value_2 = '$value_2',
722 additional_history = '$additional_history',
728 function updateHistoryData($pid,$new)
730 $real = getHistoryData($pid);
731 while(list($key, $value) = each ($new))
732 $real[$key] = $value;
733 $real['date'] = "'+NOW()+'";
736 $sql = "insert into history_data set ";
737 while(list($key, $value) = each($real))
738 $sql .= $key." = '$value', ";
739 $sql = substr($sql, 0, -2);
742 return sqlInsert($sql);
745 function sync_patient($id,$fname,$lname,$street,$city,$postal_code,$state,$phone_home,
748 $db = $GLOBALS['adodb']['db'];
749 $customer_info = array();
751 $sql = "SELECT foreign_id,foreign_table FROM integration_mapping where local_table = 'patient_data' and local_id = '" . $id . "'";
752 $result = $db->Execute($sql);
753 if ($result && !$result->EOF) {
754 $customer_info['foreign_update'] = true;
755 $customer_info['foreign_id'] = $result->fields['foreign_id'];
756 $customer_info['foreign_table'] = $result->fields['foreign_table'];
759 ///xml rpc code to connect to accounting package and add user to it
760 $customer_info['firstname'] = $fname;
761 $customer_info['lastname'] = $lname;
762 $customer_info['address'] = $street;
763 $customer_info['suburb'] = $city;
764 $customer_info['state'] = $state;
765 $customer_info['postcode'] = $postal_code;
767 //ezybiz wants state as a code rather than abbreviation
768 $customer_info['geo_zone_id'] = "";
769 $sql = "SELECT zone_id from geo_zone_reference where zone_code = '" . strtoupper($state) . "'";
770 $db = $GLOBALS['adodb']['db'];
771 $result = $db->Execute($sql);
772 if ($result && !$result->EOF) {
773 $customer_info['geo_zone_id'] = $result->fields['zone_id'];
776 //ezybiz wants country as a code rather than abbreviation
777 $customer_info['geo_country_id'] = "";
778 $sql = "SELECT countries_id from geo_country_reference where countries_iso_code_2 = '" . strtoupper($country_code) . "'";
779 $db = $GLOBALS['adodb']['db'];
780 $result = $db->Execute($sql);
781 if ($result && !$result->EOF) {
782 $customer_info['geo_country_id'] = $result->fields['countries_id'];
786 $customer_info['phone1'] = $phone_home;
787 $customer_info['phone1comment'] = "Home Phone";
788 $customer_info['phone2'] = $phone_biz;
789 $customer_info['phone2comment'] = "Business Phone";
790 $customer_info['email'] = $email;
792 $function['ezybiz.add_customer'] = array(new xmlrpcval($customer_info,"struct"));
793 $ws = new WSWrapper($function);
795 // if the remote patient was added make an entry in the local mapping table to that updates can be made correctly
796 if (is_numeric($ws->value)) {
797 $sql = "REPLACE INTO integration_mapping set id = '" . $db->GenID("sequences") . "', foreign_id ='" . $ws->value . "', foreign_table ='customer', local_id = '" . $id . "', local_table = 'patient_data' ";
798 $db->Execute($sql) or die ("error: " . $db->ErrorMsg());
802 // Returns Date of Birth given YYYYMMDD from MySQL DATE_FORMAT(DOB,'%Y%m%d')
803 function getPatientAge($dobYMD)
806 $yearDiff = substr($tdyYMD,0,4) - substr($dobYMD,0,4);
807 $ageInMonths = ((substr($tdyYMD,0,4)*12)+substr($tdyYMD,4,2)) -
808 ((substr($dobYMD,0,4)*12)+substr($dobYMD,4,2));
809 $dayDiff = substr($tdyYMD,6,2) - substr($dobYMD,6,2);
810 if ( $dayDiff < 0 ) {
813 if ( $ageInMonths > 24 ) {
814 $age = intval($ageInMonths/12);
817 $age = "$ageInMonths month";