in fixDate() default 2-digit years to 20yy if yy < 10
[openemr.git] / library / patient.inc
blob0a85b2c67d65b9f10a0a9ea84d954ff93403a8f4
1 <?php
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";
7         return sqlQuery($sql);
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"]);
17                 }
18         }
21         return $returnval;
24 function getInsuranceProviders() {
25         $returnval = array();
27         if (true) {
28                 $sql = "select name, id from insurance_companies order by name, id";
29                 $rez = sqlStatement($sql);
30                 for($iter=0; $row=sqlFetchArray($rez); $iter++) {
31                         $returnval[$row['id']] = $row['name'];
32                 }
33         }
35         // Please leave this here. I have a user who wants to see zip codes and PO
36         // box numbers listed along with the insurance company names, as many companies
37         // have different billing addresses for different plans.  -- Rod Roark
38         //
39         else {
40                 $sql = "select insurance_companies.name, insurance_companies.id, " .
41                   "addresses.zip, addresses.line1 " .
42                   "from insurance_companies, addresses " .
43                   "where addresses.foreign_id = insurance_companies.id " .
44                   "order by insurance_companies.name, addresses.zip";
46                 $rez = sqlStatement($sql);
48                 for($iter=0; $row=sqlFetchArray($rez); $iter++) {
49                         preg_match("/\d+/", $row['line1'], $matches);
50                         $returnval[$row['id']] = $row['name'] . " (" . $row['zip'] .
51                           "," . $matches[0] . ")";
52                 }
53         }
55         // print_r($retval);
57         return $returnval;
61 function getProviders() {
62         $returnval = array("");
63         $sql = "select fname,lname from users where authorized=1";
64         $rez = sqlStatement($sql);
65         for($iter=0; $row=sqlFetchArray($rez); $iter++) {
66                 if (($row["fname"] != "") && ($row["lname"] != "")) {
67                         array_push($returnval, $row["fname"] . " " . $row["lname"]);
68                 }
69         }
72         return $returnval;
75 function getProviderInfo($providerID = "%", $providers_only = true) {
76         $param1 = "";
77         if ($providers_only) {
78                 $param1 = "AND authorized=1";
79         }
80         $command = "=";
81         if ($providerID == "%") {
82                 $command = "like";
83         }
84         $query = "select distinct id,username,lname,fname, authorized, info, facility from users where id $command '" . mysql_real_escape_string($providerID) . "' " . $param1;
85         $rez = sqlStatement($query);
86         for($iter=0; $row=sqlFetchArray($rez); $iter++)
87                 $returnval[$iter]=$row;
89         //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
90         //accessible from $resultval['key']
92         if($iter==1) {
93                 $akeys = array_keys($returnval[0]);
94                 foreach($akeys as $key) {
96                         $returnval[0][$key] = $returnval[0][$key];
97                 }
98         }
99         return $returnval;
102 //same as above but does not reduce if only 1 row returned
103 function getCalendarProviderInfo($providerID = "%", $providers_only = true) {
104         $param1 = "";
105         if ($providers_only) {
106                 $param1 = "AND authorized=1";
107         }
108         $command = "=";
109         if ($providerID == "%") {
110                 $command = "like";
111         }
112         $query = "select distinct id,username,lname,fname, authorized, info, facility from users where id $command '" . mysql_real_escape_string($providerID) . "' " . $param1;
114         $rez = sqlStatement($query);
115         for($iter=0; $row=sqlFetchArray($rez); $iter++)
116                 $returnval[$iter]=$row;
118         return $returnval;
123 function getProviderName($providerID) {
125         $pi = getProviderInfo($providerID);
127         if (strlen($pi[0]["lname"]) > 0) {
128                 return $pi[0]['fname'] . " " . $pi[0]['lname'];
129         }
131         return "";
134 function getProviderId($providerName) {
135         $query = "select id from users where username = '". mysql_real_escape_string($providerName)."'";
137         $rez = sqlStatement($query);
138         for($iter=0; $row=sqlFetchArray($rez); $iter++)
139                 $returnval[$iter]=$row;
141         return $returnval;
144 function getEthnoRacials() {
145         $returnval = array("");
146         $sql = "select distinct lower(ethnoracial) as ethnoracial from patient_data";
147         $rez = sqlStatement($sql);
148         for($iter=0; $row=sqlFetchArray($rez); $iter++) {
149                 if (($row["ethnoracial"] != "")) {
150                         array_push($returnval, $row["ethnoracial"]);
151                 }
152         }
155         return $returnval;
158 function getHistoryData($pid, $given = "*")
160         $sql = "select $given from history_data where pid='$pid' order by date DESC limit 0,1";
161         return sqlQuery($sql);
164 function getInsuranceData($pid, $type = "primary", $given = "insd.*, DATE_FORMAT(subscriber_DOB,'%m/%d/%Y') as subscriber_DOB, ic.name as provider_name")
166         $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";
167         return sqlQuery($sql);
170 function getInsuranceDataByDate( $pid, $date, $type, $given = "insd.*, DATE_FORMAT(subscriber_DOB,'%m/%d/%Y') as subscriber_DOB, ic.name as provider_name")
171 { //this must take the date in the following manner: YYYY-MM-DD
172         //this function recalls the insurance value that was most recently enterred from the
173         //given date. it will call up most recent records up to and on the date given,
174         //but not records enterred after the given date
175         $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";
176         return sqlQuery($sql);
180 function getEmployerData($pid, $given = "*")
182         $sql = "select $given from employer_data where pid='$pid' order by date DESC limit 0,1";
183         return sqlQuery($sql);
186 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")
188         /****
189         $sql="select pid, MAX(id) as id from patient_data group by pid DESC order by pid ASC";
190         $res = sqlStatement($sql);
191         $sql="select $given from patient_data where lname like '$lname%' and (";
192         for ($iter = 0;$row = sqlFetchArray($res);$iter++)
193                 $sql.=" id='{$row['id']}' or";
194         if ($iter > 0)
195                 $sql = substr($sql, 0, -3) . ") order by $orderby";
196         else
197                 $sql = substr($sql, 0, -5)."order by $orderby";
198         ****/
200         // WTF? That was a good way to create a 200KB sql statement.
202         $sql="select $given from patient_data where lname like '$lname%' " .
203                 "order by $orderby";
205         if ($limit != "all")
206                 $sql .= " limit $start, $limit";
207         $rez = sqlStatement($sql);
209         for($iter=0; $row=sqlFetchArray($rez); $iter++)
210                 $returnval[$iter]=$row;
212         return $returnval;
215 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")
217         /****
218         $sql="select pid, MAX(id) as id from patient_data group by pid DESC order by pid ASC";
219         $res = sqlStatement($sql);
220         $sql="select $given from patient_data where pubpid like '$pid%' and (";
221         for ($iter = 0;$row = sqlFetchArray($res);$iter++)
222                 $sql.=" id='{$row['id']}' or";
223         if ($iter > 0)
224                 $sql = substr($sql, 0, -3) . ") order by $orderby";
225         else
226                 $sql = substr($sql, 0, -5)."order by $orderby";
227         ****/
229         $sql = "select $given from patient_data where pubpid like '$pid%' " .
230                 "order by $orderby";
232         if ($limit != "all")
233                 $sql .= " limit $start, $limit";
234         $rez = sqlStatement($sql);
235         for($iter=0; $row=sqlFetchArray($rez); $iter++)
236                 $returnval[$iter]=$row;
238         return $returnval;
241 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")
243         $command = "=";
244         if ($pid == -1)
245                 $pid = "%";
246         elseif (empty($pid))
247                 $pid = "NULL";
249         if (strstr($pid,"%"))
250                 $command = "like";
252         $sql="select $given from patient_data where pid $command '$pid' order by $orderby";
254         if ($limit != "all")
255                 $sql .= " limit $start, $limit";
257         $rez = sqlStatement($sql);
258         for($iter=0; $row=sqlFetchArray($rez); $iter++)
259                 $returnval[$iter]=$row;
262         return $returnval;
265 function getPatientName($pid) {
266         if (empty($pid))
267                 return "";
268         $patientData = getPatientPID($pid);
269         if (empty($patientData[0]['lname']))
270                 return "";
271         $patientName =  $patientData[0]['lname'] . ", " . $patientData[0]['fname'];
272         return $patientName;
276 function getPatientDOB($DOB = "%", $given = "pid, id, lname, fname, mname", $orderby = "lname ASC, fname ASC", $limit="all", $start="0")
278         /****
279         $sql="select pid, MAX(id) as id from patient_data group by pid DESC order by pid ASC";
280         $res = sqlStatement($sql);
281         $sql="select $given from patient_data where DOB like '$DOB%' and (";
282         for ($iter = 0;$row = sqlFetchArray($res);$iter++)
283                 $sql.=" id='{$row['id']}' or";
284         if ($iter > 0)
285                 $sql = substr($sql, 0, -3) . ") order by $orderby";
286         else
287                 $sql = substr($sql, 0, -5)."order by $orderby";
288         ****/
290         $DOB = fixDate($DOB, $DOB);
292         $sql="select $given from patient_data where DOB like '$DOB%' " .
293                 "order by $orderby";
295         if ($limit != "all")
296                 $sql .= " limit $start, $limit";
298         $rez = sqlStatement($sql);
299         for($iter=0; $row=sqlFetchArray($rez); $iter++)
300                 $returnval[$iter]=$row;
302         return $returnval;
305 function getPatientSSN($ss = "%", $given = "pid, id, lname, fname, mname, providerID", $orderby = "lname ASC, fname ASC", $limit="all", $start="0")
307         /****
308         $sql="select pid, MAX(id) as id from patient_data group by pid DESC order by pid ASC";
309         $res = sqlStatement($sql);
310         $sql="select $given from patient_data where ss like '$ss%' and (";
311         for ($iter = 0;$row = sqlFetchArray($res);$iter++)
312                 $sql.=" id='{$row['id']}' or";
313         if ($iter > 0)
314                 $sql = substr($sql, 0, -3) . ") order by $orderby";
315         else
316                 $sql = substr($sql, 0, -5)."order by $orderby";
317         ****/
319         $sql="select $given from patient_data where ss like '$ss%' " .
320                 "order by $orderby";
322         if ($limit != "all")
323                 $sql .= " limit $start, $limit";
325         $rez = sqlStatement($sql);
326         for($iter=0; $row=sqlFetchArray($rez); $iter++)
327                 $returnval[$iter]=$row;
329         return $returnval;
332 function getPatientIds($given = "pid, id, lname, fname, mname", $orderby = "id ASC", $limit="all", $start="0")
334         /****
335         $sql="select pid, MAX(id) as id from patient_data group by pid DESC order by pid ASC";
336         $res = sqlStatement($sql);
337         $sql="select $given from patient_data where ";
338         for ($iter = 0;$row = sqlFetchArray($res);$iter++)
339                 $sql.="id='{$row['id']}' or ";
340         $sql = substr($sql, 0, -3) . "order by $orderby";
341         ****/
343         $sql="select $given from patient_data order by $orderby";
345         if ($limit != "all")
346                 $sql .= " limit $start, $limit";
348         $rez = sqlStatement($sql);
349         for($iter=0; $row=sqlFetchArray($rez); $iter++)
350                 $returnval[$iter]=$row;
352         return $returnval;
355 //----------------------input functions
356 function newPatientData(        $db_id="",
357                                 $title = "",
358                                 $fname = "",
359                                 $lname = "",
360                                 $mname = "",
361                                 $sex = "",
362                                 $DOB = "",
363                                 $street = "",
364                                 $postal_code = "",
365                                 $city = "",
366                                 $state = "",
367                                 $country_code = "",
368                                 $ss = "",
369                                 $occupation = "",
370                                 $phone_home = "",
371                                 $phone_biz = "",
372                                 $phone_contact = "",
373                                 $status = "",
374                                 $contact_relationship = "",
375                                 $referrer = "",
376                                 $referrerID = "",
377                                 $email = "",
378                                 $language = "",
379                                 $ethnoracial = "",
380                                 $interpretter = "",
381                                 $migrantseasonal = "",
382                                 $family_size = "",
383                                 $monthly_income = "",
384                                 $homeless = "",
385                                 $financial_review = "",
386                                 $pubpid = "",
387                                 $pid = "MAX(pid)+1",
388                                 $providerID = "",
389                                 $genericname1 = "",
390                                 $genericval1 = "",
391                                 $genericname2 = "",
392                                 $genericval2 = "",
393                                 $phone_cell = "",
394                                 $hipaa_mail = "",
395                                 $hipaa_voice = ""
396                         )
398         $DOB = fixDate($DOB);
400         // Looking for problems... suspecting that $pid might be clobbered.
401         //
402         if ($pid) {
403                 $rez = sqlQuery("select id from patient_data where pid = $pid");
404                 if ($db_id != $rez['id']) {
405                         $errmsg = "Internal error: Attempt to change patient_data.id from '" .
406                           $rez['id'] . "' to '$db_id' for pid '$pid'";
407                         die($errmsg);
408                 }
409         }
410         
411         $query = ("replace into patient_data set
412                 id='$db_id',
413                 title='$title',
414                 fname='$fname',
415                 lname='$lname',
416                 mname='$mname',
417                 sex='$sex',
418                 DOB='$DOB',
419                 street='$street',
420                 postal_code='$postal_code',
421                 city='$city',
422                 state='$state',
423                 country_code='$country_code',
424                 ss='$ss',
425                 occupation='$occupation',
426                 phone_home='$phone_home',
427                 phone_biz='$phone_biz',
428                 phone_contact='$phone_contact',
429                 status='$status',
430                 contact_relationship='$contact_relationship',
431                 referrer='$referrer',
432                 referrerID='$referrerID',
433                 email='$email',
434                 language='$language',
435                 ethnoracial='$ethnoracial',
436                 interpretter='$interpretter',
437                 migrantseasonal='$migrantseasonal',
438                 family_size='$family_size',
439                 monthly_income='$monthly_income',
440                 homeless='$homeless',
441                 financial_review='$financial_review',
442                 pubpid='$pubpid',
443                 pid = $pid,
444                 providerID = '$providerID',
445                 genericname1 = '$genericname1',
446                 genericval1 = '$genericval1',
447                 genericname2 = '$genericname2',
448                 genericval2 = '$genericval2',
449                 phone_cell = '$phone_cell',
450                 hipaa_mail = '$hipaa_mail',
451                 hipaa_voice = '$hipaa_voice',
452                 date=NOW()
453                         ");
455         $id = sqlInsert($query);
456         $foo = sqlQuery("select pid from patient_data where id='$id' order by date limit 0,1");
458         sync_patient($id,$fname,$lname,$street,$city,$postal_code,$state,$phone_home,
459                                 $phone_biz,$email,$pid);
461         return $foo['pid'];
464 // Supported input date formats are:
465 //   mm/dd/yyyy
466 //   mm/dd/yy   (assumes 20yy for yy < 10, else 19yy)
467 //   yyyy/mm/dd
468 //   also mm-dd-yyyy, etc. and mm.dd.yyyy, etc.
470 function fixDate($date, $default="0000-00-00") {
471     $fixed_date = $default;
472     $date = trim($date);
473     if (preg_match("'^[0-9]{1,4}[/.-][0-9]{1,2}[/.-][0-9]{1,4}$'", $date)) {
474         $dmy = preg_split("'[/.-]'", $date);
475         if ($dmy[0] > 99) {
476             $fixed_date = sprintf("%04u-%02u-%02u", $dmy[0], $dmy[1], $dmy[2]);
477         } else {
478             if ($dmy[2] < 1000) $dmy[2] += 1900;
479             if ($dmy[2] < 1910) $dmy[2] += 100;
480             $fixed_date = sprintf("%04u-%02u-%02u", $dmy[2], $dmy[0], $dmy[1]);
481         }
482     }
484     return $fixed_date;
488 function updatePatientData($pid,$new)
490         $real = getPatientData($pid);
491         $new['DOB'] = fixDate($new['DOB']);
492         while(list($key, $value) = each ($new))
493                 $real[$key] = $value;
494         $real['date'] = "'+NOW()+'";
495         $real['id'] = "";
497         $sql = "insert into patient_data set ";
498         while(list($key, $value) = each($real))
499                 $sql .= $key." = '$value', ";
500         $sql = substr($sql, 0, -2);
503         return sqlInsert($sql);
507 function newEmployerData(       $pid,
508                                 $name = "",
509                                 $street = "",
510                                 $postal_code = "",
511                                 $city = "",
512                                 $state = "",
513                                 $country = ""
514                         )
516         return sqlInsert("insert into employer_data set
517                 name='$name',
518                 street='$street',
519                 postal_code='$postal_code',
520                 city='$city',
521                 state='$state',
522                 country='$country',
523                 pid='$pid',
524                 date=NOW()
525                 ");
528 function updateEmployerData($pid,$new)
530         $real = getEmployerData($pid);
531         while(list($key, $value) = each ($new))
532                 $real[$key] = $value;
533         $real['date'] = "'+NOW()+'";
534         $real['id'] = "";
536         $sql = "insert into employer_data set ";
537         while(list($key, $value) = each($real))
538                 $sql .= $key." = '$value', ";
539         $sql = substr($sql, 0, -2);
542         return sqlInsert($sql);
545 function newInsuranceData(      $pid,
546                                 $type = "",
547                                 $provider = "",
548                                 $policy_number = "",
549                                 $group_number = "",
550                                 $plan_name = "",
551                                 $subscriber_lname = "",
552                                 $subscriber_mname = "",
553                                 $subscriber_fname = "",
554                                 $subscriber_relationship = "",
555                                 $subscriber_ss = "",
556                                 $subscriber_DOB = "",
557                                 $subscriber_street = "",
558                                 $subscriber_postal_code = "",
559                                 $subscriber_city = "",
560                                 $subscriber_state = "",
561                                 $subscriber_country = "",
562                                 $subscriber_phone = "",
563                                 $subscriber_employer = "",
564                                 $subscriber_employer_street = "",
565                                 $subscriber_employer_city = "",
566                                 $subscriber_employer_postal_code = "",
567                                 $subscriber_employer_state = "",
568                                 $subscriber_employer_country = "",
569                                 $copay = "",
570                                 $subscriber_sex = ""
571                         )
573         if (strlen($type) > 0) {
574         $query = "select * from insurance_data where type='" . $type  . "' and pid = " . $pid .  " limit 1";
575         }
576         else {
577                 return FALSE;
578         }
579         $res = sqlQuery ($query);
581         if ($res) {
582                 $data['type'] = $type;
583                 $data['provider'] = $provider;
584                 $data['policy_number']=$policy_number;
585                 $data['group_number']=$group_number;
586                 $data['plan_name']=$plan_name;
587                 $data['subscriber_lname']=$subscriber_lname;
588                 $data['subscriber_mname']=$subscriber_mname;
589                 $data['subscriber_fname']=$subscriber_fname;
590                 $data['subscriber_relationship']=$subscriber_relationship;
591                 $data['subscriber_ss']=$subscriber_ss;
592                 $data['subscriber_DOB']=$subscriber_DOB;
593                 $data['subscriber_street']=$subscriber_street;
594                 $data['subscriber_postal_code']=$subscriber_postal_code;
595                 $data['subscriber_city']=$subscriber_city;
596                 $data['subscriber_state']=$subscriber_state;
597                 $data['subscriber_country']=$subscriber_country;
598                 $data['subscriber_phone']=$subscriber_phone;
599                 $data['subscriber_employer']=$subscriber_employer;
600                 $data['subscriber_employer_city']=$subscriber_employer_city;
601                 $data['subscriber_employer_street']=$subscriber_employer_street;
602                 $data['subscriber_employer_postal_code']=$subscriber_employer_postal_code;
603                 $data['subscriber_employer_state']=$subscriber_employer_state;
604                 $data['subscriber_employer_country']=$subscriber_employer_country;
605                 $data['copay']=$copay;
606                 $data['subscriber_sex']=$subscriber_sex;
607                 $data['pid']=$pid;
608                 $data['date']="NOW()";
609         //      echo "updating<br><br>";
611                 return updateInsuranceData($pid,$data);
612         }
613         else {
614         return sqlInsert("insert into insurance_data set
615                 type='$type',
616                 provider='$provider',
617                 policy_number='$policy_number',
618                 group_number='$group_number',
619                 plan_name='$plan_name',
620                 subscriber_lname='$subscriber_lname',
621                 subscriber_mname='$subscriber_mname',
622                 subscriber_fname='$subscriber_fname',
623                 subscriber_relationship='$subscriber_relationship',
624                 subscriber_ss='$subscriber_ss',
625                 subscriber_DOB='$subscriber_DOB',
626                 subscriber_street='$subscriber_street',
627                 subscriber_postal_code='$subscriber_postal_code',
628                 subscriber_city='$subscriber_city',
629                 subscriber_state='$subscriber_state',
630                 subscriber_country='$subscriber_country',
631                 subscriber_phone='$subscriber_phone',
632                 subscriber_employer = '$subscriber_employer',
633                 subscriber_employer_city='$subscriber_employer_city',
634                 subscriber_employer_street='$subscriber_employer_street',
635                 subscriber_employer_postal_code='$subscriber_employer_postal_code',
636                 subscriber_employer_state='$subscriber_employer_state',
637                 subscriber_employer_country='$subscriber_employer_country',
638                 copay='$copay',
639                 subscriber_sex='$subscriber_sex',
640                 pid='$pid',
641                 date=NOW()
642                 ");
643         }
646 function updateInsuranceData($pid,$new)
648                 $fields = sqlListFields("insurance_data");
650         $real = getInsuranceData($pid);
651         $use = array();
652         while(list($key, $value) = each ($new)) {
653                         if (in_array($key,$fields)){
654                         $use[$key] = $value;
655                         }
656                 }
657         $real['date'] = "'+NOW()+'";
658         $real['id'] = "";
660         $sql = "replace into insurance_data set ";
661         while(list($key, $value) = each($use))
662                 $sql .= $key." = '$value', ";
665         $sql = substr($sql, 0, -2);
667         //echo $sql;
668         //exit;
669         return sqlInsert($sql);
673 function newHistoryData(        $pid,
674                                 $coffee = "",
675                                 $tobacco = "",
676                                 $alcohol = "",
677                                 $sleep_patterns = "",
678                                 $exercise_patterns = "",
679                                 $seatbelt_use = "",
680                                 $counseling = "",
681                                 $hazardous_activities = "",
682                                 $last_breast_exam = "",
683                                 $last_mammogram = "",
684                                 $last_gynocological_exam = "",
685                                 $last_rectal_exam = "",
686                                 $last_prostate_exam = "",
687                                 $last_physical_exam = "",
688                                 $last_sigmoidoscopy_colonoscopy = "",
689                                 $history_mother = "",
690                                 $history_father = "",
691                                 $history_siblings = "",
692                                 $history_offspring = "",
693                                 $history_spouse = "",
694                                 $relatives_cancer = "",
695                                 $relatives_tuberculosis = "",
696                                 $relatives_diabetes = "",
697                                 $relatives_high_blood_pressure = "",
698                                 $relatives_heart_problems = "",
699                                 $relatives_stroke = "",
700                                 $relatives_epilepsy = "",
701                                 $relatives_mental_illness = "",
702                                 $relatives_suicide = "",
703                                 $cataract_surgery = "",
704                                 $tonsillectomy = "",
705                                 $appendectomy = "",
706                                 $cholecystestomy = "",
707                                 $heart_surgery = "",
708                                 $hysterectomy = "",
709                                 $hernia_repair = "",
710                                 $hip_replacement = "",
711                                 $knee_replacement = "",
712                                 $name_1 = "",
713                                 $value_1 = "",
714                                 $name_2 = "",
715                                 $value_2 = "",
716                                 $additional_history = ""
717                                 )
719         return sqlInsert("insert into history_data set
720                 coffee='$coffee',
721                 tobacco='$tobacco',
722                 alcohol='$alcohol',
723                 sleep_patterns='$sleep_patterns',
724                 exercise_patterns='$exercise_patterns',
725                 seatbelt_use='$seatbelt_use',
726                 counseling='$counseling',
727                 hazardous_activities='$hazardous_activities',
728                 last_breast_exam='$last_breast_exam',
729                 last_mammogram='$last_mammogram',
730                 last_gynocological_exam='$last_gynocological_exam',
731                 last_rectal_exam='$last_rectal_exam',
732                 last_prostate_exam='$last_prostate_exam',
733                 last_physical_exam='$last_physical_exam',
734                 last_sigmoidoscopy_colonoscopy='$last_sigmoidoscopy_colonoscopy',
735                 history_mother='$history_mother',
736                 history_father='$history_father',
737                 history_siblings='$history_siblings',
738                 history_offspring='$history_offspring',
739                 history_spouse='$history_spouse',
740                 relatives_cancer='$relatives_cancer',
741                 relatives_tuberculosis ='$relatives_tuberculosis',
742                 relatives_diabetes='$relatives_diabetes',
743                 relatives_high_blood_pressure='$relatives_high_blood_pressure',
744                 relatives_heart_problems='$relatives_heart_problems',
745                 relatives_stroke='$relatives_stroke',
746                 relatives_epilepsy='$relatives_epilepsy',
747                 relatives_mental_illness='$relatives_mental_illness',
748                 relatives_suicide='$relatives_suicide',
749                 cataract_surgery='$cataract_surgery',
750                 tonsillectomy='$tonsillectomy',
751                 appendectomy='$appendectomy',
752                 cholecystestomy='$cholecystestomy',
753                 heart_surgery='$heart_surgery',
754                 hysterectomy='$hysterectomy',
755                 hernia_repair='$hernia_repair',
756                 hip_replacement='$hip_replacement',
757                 knee_replacement='$knee_replacement',
758                 name_1 = '$name_1',
759                 value_1 = '$value_1',
760                 name_2 = '$name_2',
761                 value_2 = '$value_2',
762                 additional_history = '$additional_history',
763                 date=NOW(),
764                 pid='$pid'
765                 ");
768 function updateHistoryData($pid,$new)
770         $real = getHistoryData($pid);
771         while(list($key, $value) = each ($new))
772                 $real[$key] = $value;
773         $real['date'] = "'+NOW()+'";
774         $real['id'] = "";
776         $sql = "insert into history_data set ";
777         while(list($key, $value) = each($real))
778                 $sql .= $key." = '$value', ";
779         $sql = substr($sql, 0, -2);
782         return sqlInsert($sql);
785 function sync_patient($id,$fname,$lname,$street,$city,$postal_code,$state,$phone_home,
786                                 $phone_biz,$email,$pid="")
788         $db = $GLOBALS['adodb']['db'];
789         $customer_info = array();
791         $sql = "SELECT foreign_id,foreign_table FROM integration_mapping where local_table = 'patient_data' and local_id = '" . $id . "'";
792         $result = $db->Execute($sql);
793         if ($result && !$result->EOF) {
794                 $customer_info['foreign_update'] = true;
795                 $customer_info['foreign_id'] = $result->fields['foreign_id'];
796                 $customer_info['foreign_table'] = $result->fields['foreign_table'];
797         }
799         ///xml rpc code to connect to accounting package and add user to it
800         $customer_info['firstname'] = $fname;
801         $customer_info['lastname'] = $lname;
802         $customer_info['address'] = $street;
803         $customer_info['suburb'] = $city;
804         $customer_info['state'] = $state;
805         $customer_info['postcode'] = $postal_code;
807         //ezybiz wants state as a code rather than abbreviation
808         $customer_info['geo_zone_id'] = "";
809         $sql = "SELECT zone_id from geo_zone_reference where zone_code = '" . strtoupper($state) . "'";
810         $db = $GLOBALS['adodb']['db'];
811         $result = $db->Execute($sql);
812         if ($result && !$result->EOF) {
813                 $customer_info['geo_zone_id'] = $result->fields['zone_id'];
814         }
816         //ezybiz wants country as a code rather than abbreviation
817         $customer_info['geo_country_id'] = "";
818         $sql = "SELECT countries_id from geo_country_reference where countries_iso_code_2 = '" . strtoupper($country_code) . "'";
819         $db = $GLOBALS['adodb']['db'];
820         $result = $db->Execute($sql);
821         if ($result && !$result->EOF) {
822                 $customer_info['geo_country_id'] = $result->fields['countries_id'];
823         }
826         $customer_info['phone1'] = $phone_home;
827         $customer_info['phone1comment'] = "Home Phone";
828         $customer_info['phone2'] = $phone_biz;
829         $customer_info['phone2comment'] = "Business Phone";
830         $customer_info['email'] = $email;
831         $customer_info['customernumber'] = $pid;
833         $function['ezybiz.add_customer'] = array(new xmlrpcval($customer_info,"struct"));
834         $ws = new WSWrapper($function);
836         // if the remote patient was added make an entry in the local mapping table to that updates can be made correctly
837         if (is_numeric($ws->value)) {
838                 $sql = "REPLACE INTO integration_mapping set id = '" . $db->GenID("sequences") . "', foreign_id ='" . $ws->value . "', foreign_table ='customer', local_id = '" . $id . "', local_table = 'patient_data' ";
839                 $db->Execute($sql) or die ("error: " . $db->ErrorMsg());
840         }
843 // Returns Date of Birth given YYYYMMDD from MySQL DATE_FORMAT(DOB,'%Y%m%d')
844 function getPatientAge($dobYMD)
846     $tdyYMD=date("Ymd");
847     $yearDiff = substr($tdyYMD,0,4) - substr($dobYMD,0,4);
848     $ageInMonths = ((substr($tdyYMD,0,4)*12)+substr($tdyYMD,4,2)) -
849                    ((substr($dobYMD,0,4)*12)+substr($dobYMD,4,2));
850     $dayDiff = substr($tdyYMD,6,2) - substr($dobYMD,6,2);
851     if ( $dayDiff < 0 ) {
852         $ageInMonths -= 1;
853     }
854     if ( $ageInMonths > 24 ) {
855         $age = intval($ageInMonths/12);
856     }
857     else  {
858         $age = "$ageInMonths month";
859     }
860     return $age;
863 function dateToDB ($date) 
865         $date=substr ($date,6,4)."-".substr ($date,3,2)."-".substr($date, 0,2);
866         return $date;
872 function DBToDate ($date)
874         $date=substr ($date,5,2)."/".substr ($date,8,2)."/".substr($date, 0,4);
875         return $date;