internationalized units in vitals form - supports both us and metric units
[openemr.git] / library / patient.inc
blobb2543fa280a6a781dc8835b8e31d4c3eb92d3572
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")
165 function getInsuranceData($pid, $type = "primary", $given = "insd.*, ic.name as provider_name")
167         $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";
168         return sqlQuery($sql);
171 function getInsuranceDataByDate( $pid, $date, $type, $given = "insd.*, DATE_FORMAT(subscriber_DOB,'%m/%d/%Y') as subscriber_DOB, ic.name as provider_name")
172 { //this must take the date in the following manner: YYYY-MM-DD
173         //this function recalls the insurance value that was most recently enterred from the
174         //given date. it will call up most recent records up to and on the date given,
175         //but not records enterred after the given date
176         $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";
177         return sqlQuery($sql);
181 function getEmployerData($pid, $given = "*")
183         $sql = "select $given from employer_data where pid='$pid' order by date DESC limit 0,1";
184         return sqlQuery($sql);
187 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")
189         /****
190         $sql="select pid, MAX(id) as id from patient_data group by pid DESC order by pid ASC";
191         $res = sqlStatement($sql);
192         $sql="select $given from patient_data where lname like '$lname%' and (";
193         for ($iter = 0;$row = sqlFetchArray($res);$iter++)
194                 $sql.=" id='{$row['id']}' or";
195         if ($iter > 0)
196                 $sql = substr($sql, 0, -3) . ") order by $orderby";
197         else
198                 $sql = substr($sql, 0, -5)."order by $orderby";
199         ****/
201         // WTF? That was a good way to create a 200KB sql statement.
203         // Allow the last name to be followed by a comma and some part of a first name.
204         $lname = trim($lname);
205         $fname = '';
206         if (preg_match('/^(.*\S)\s*,\s*(.*)/', $lname, $matches)) {
207                 $lname = $matches[1];
208                 $fname = $matches[2];
209         }
211         $sql="select $given from patient_data where lname like '$lname%' " .
212                 "and fname like '$fname%' " .
213                 "order by $orderby";
215         if ($limit != "all")
216                 $sql .= " limit $start, $limit";
217         $rez = sqlStatement($sql);
219         for($iter=0; $row=sqlFetchArray($rez); $iter++)
220                 $returnval[$iter]=$row;
222         return $returnval;
225 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")
227         /****
228         $sql="select pid, MAX(id) as id from patient_data group by pid DESC order by pid ASC";
229         $res = sqlStatement($sql);
230         $sql="select $given from patient_data where pubpid like '$pid%' and (";
231         for ($iter = 0;$row = sqlFetchArray($res);$iter++)
232                 $sql.=" id='{$row['id']}' or";
233         if ($iter > 0)
234                 $sql = substr($sql, 0, -3) . ") order by $orderby";
235         else
236                 $sql = substr($sql, 0, -5)."order by $orderby";
237         ****/
239         $sql = "select $given from patient_data where pubpid like '$pid%' " .
240                 "order by $orderby";
242         if ($limit != "all")
243                 $sql .= " limit $start, $limit";
244         $rez = sqlStatement($sql);
245         for($iter=0; $row=sqlFetchArray($rez); $iter++)
246                 $returnval[$iter]=$row;
248         return $returnval;
251 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")
253         $command = "=";
254         if ($pid == -1)
255                 $pid = "%";
256         elseif (empty($pid))
257                 $pid = "NULL";
259         if (strstr($pid,"%"))
260                 $command = "like";
262         $sql="select $given from patient_data where pid $command '$pid' order by $orderby";
264         if ($limit != "all")
265                 $sql .= " limit $start, $limit";
267         $rez = sqlStatement($sql);
268         for($iter=0; $row=sqlFetchArray($rez); $iter++)
269                 $returnval[$iter]=$row;
272         return $returnval;
275 function getPatientName($pid) {
276         if (empty($pid))
277                 return "";
278         $patientData = getPatientPID($pid);
279         if (empty($patientData[0]['lname']))
280                 return "";
281         $patientName =  $patientData[0]['lname'] . ", " . $patientData[0]['fname'];
282         return $patientName;
286 function getPatientDOB($DOB = "%", $given = "pid, id, lname, fname, mname", $orderby = "lname ASC, fname ASC", $limit="all", $start="0")
288         /****
289         $sql="select pid, MAX(id) as id from patient_data group by pid DESC order by pid ASC";
290         $res = sqlStatement($sql);
291         $sql="select $given from patient_data where DOB like '$DOB%' and (";
292         for ($iter = 0;$row = sqlFetchArray($res);$iter++)
293                 $sql.=" id='{$row['id']}' or";
294         if ($iter > 0)
295                 $sql = substr($sql, 0, -3) . ") order by $orderby";
296         else
297                 $sql = substr($sql, 0, -5)."order by $orderby";
298         ****/
300         $DOB = fixDate($DOB, $DOB);
302         $sql="select $given from patient_data where DOB like '$DOB%' " .
303                 "order by $orderby";
305         if ($limit != "all")
306                 $sql .= " limit $start, $limit";
308         $rez = sqlStatement($sql);
309         for($iter=0; $row=sqlFetchArray($rez); $iter++)
310                 $returnval[$iter]=$row;
312         return $returnval;
315 function getPatientSSN($ss = "%", $given = "pid, id, lname, fname, mname, providerID", $orderby = "lname ASC, fname ASC", $limit="all", $start="0")
317         /****
318         $sql="select pid, MAX(id) as id from patient_data group by pid DESC order by pid ASC";
319         $res = sqlStatement($sql);
320         $sql="select $given from patient_data where ss like '$ss%' and (";
321         for ($iter = 0;$row = sqlFetchArray($res);$iter++)
322                 $sql.=" id='{$row['id']}' or";
323         if ($iter > 0)
324                 $sql = substr($sql, 0, -3) . ") order by $orderby";
325         else
326                 $sql = substr($sql, 0, -5)."order by $orderby";
327         ****/
329         $sql="select $given from patient_data where ss like '$ss%' " .
330                 "order by $orderby";
332         if ($limit != "all")
333                 $sql .= " limit $start, $limit";
335         $rez = sqlStatement($sql);
336         for($iter=0; $row=sqlFetchArray($rez); $iter++)
337                 $returnval[$iter]=$row;
339         return $returnval;
342 function getPatientIds($given = "pid, id, lname, fname, mname", $orderby = "id ASC", $limit="all", $start="0")
344         /****
345         $sql="select pid, MAX(id) as id from patient_data group by pid DESC order by pid ASC";
346         $res = sqlStatement($sql);
347         $sql="select $given from patient_data where ";
348         for ($iter = 0;$row = sqlFetchArray($res);$iter++)
349                 $sql.="id='{$row['id']}' or ";
350         $sql = substr($sql, 0, -3) . "order by $orderby";
351         ****/
353         $sql="select $given from patient_data order by $orderby";
355         if ($limit != "all")
356                 $sql .= " limit $start, $limit";
358         $rez = sqlStatement($sql);
359         for($iter=0; $row=sqlFetchArray($rez); $iter++)
360                 $returnval[$iter]=$row;
362         return $returnval;
365 //----------------------input functions
366 function newPatientData(        $db_id="",
367                                 $title = "",
368                                 $fname = "",
369                                 $lname = "",
370                                 $mname = "",
371                                 $sex = "",
372                                 $DOB = "",
373                                 $street = "",
374                                 $postal_code = "",
375                                 $city = "",
376                                 $state = "",
377                                 $country_code = "",
378                                 $ss = "",
379                                 $occupation = "",
380                                 $phone_home = "",
381                                 $phone_biz = "",
382                                 $phone_contact = "",
383                                 $status = "",
384                                 $contact_relationship = "",
385                                 $referrer = "",
386                                 $referrerID = "",
387                                 $email = "",
388                                 $language = "",
389                                 $ethnoracial = "",
390                                 $interpretter = "",
391                                 $migrantseasonal = "",
392                                 $family_size = "",
393                                 $monthly_income = "",
394                                 $homeless = "",
395                                 $financial_review = "",
396                                 $pubpid = "",
397                                 $pid = "MAX(pid)+1",
398                                 $providerID = "",
399                                 $genericname1 = "",
400                                 $genericval1 = "",
401                                 $genericname2 = "",
402                                 $genericval2 = "",
403                                 $phone_cell = "",
404                                 $hipaa_mail = "",
405                                 $hipaa_voice = "",
406                                 $squad = 0
407                         )
409         $DOB = fixDate($DOB);
411         // Looking for problems... suspecting that $pid might be clobbered.
412         //
413         $fitness = 0;
414         if ($pid) {
415                 $rez = sqlQuery("select id, fitness from patient_data where pid = $pid");
416                 if ($db_id != $rez['id']) {
417                         $errmsg = "Internal error: Attempt to change patient_data.id from '" .
418                           $rez['id'] . "' to '$db_id' for pid '$pid'";
419                         die($errmsg);
420                 }
421                 $fitness = $rez['fitness'];
422         }
424         $query = ("replace into patient_data set
425                 id='$db_id',
426                 title='$title',
427                 fname='$fname',
428                 lname='$lname',
429                 mname='$mname',
430                 sex='$sex',
431                 DOB='$DOB',
432                 street='$street',
433                 postal_code='$postal_code',
434                 city='$city',
435                 state='$state',
436                 country_code='$country_code',
437                 ss='$ss',
438                 occupation='$occupation',
439                 phone_home='$phone_home',
440                 phone_biz='$phone_biz',
441                 phone_contact='$phone_contact',
442                 status='$status',
443                 contact_relationship='$contact_relationship',
444                 referrer='$referrer',
445                 referrerID='$referrerID',
446                 email='$email',
447                 language='$language',
448                 ethnoracial='$ethnoracial',
449                 interpretter='$interpretter',
450                 migrantseasonal='$migrantseasonal',
451                 family_size='$family_size',
452                 monthly_income='$monthly_income',
453                 homeless='$homeless',
454                 financial_review='$financial_review',
455                 pubpid='$pubpid',
456                 pid = $pid,
457                 providerID = '$providerID',
458                 genericname1 = '$genericname1',
459                 genericval1 = '$genericval1',
460                 genericname2 = '$genericname2',
461                 genericval2 = '$genericval2',
462                 phone_cell = '$phone_cell',
463                 hipaa_mail = '$hipaa_mail',
464                 hipaa_voice = '$hipaa_voice',
465                 squad = '$squad',
466                 fitness='$fitness',
467                 date=NOW()
468                         ");
470         $id = sqlInsert($query);
471         $foo = sqlQuery("select pid from patient_data where id='$id' order by date limit 0,1");
473         sync_patient($id,$fname,$lname,$street,$city,$postal_code,$state,$phone_home,
474                                 $phone_biz,$email,$pid);
476         return $foo['pid'];
479 // Supported input date formats are:
480 //   mm/dd/yyyy
481 //   mm/dd/yy   (assumes 20yy for yy < 10, else 19yy)
482 //   yyyy/mm/dd
483 //   also mm-dd-yyyy, etc. and mm.dd.yyyy, etc.
485 function fixDate($date, $default="0000-00-00") {
486     $fixed_date = $default;
487     $date = trim($date);
488     if (preg_match("'^[0-9]{1,4}[/.-][0-9]{1,2}[/.-][0-9]{1,4}$'", $date)) {
489         $dmy = preg_split("'[/.-]'", $date);
490         if ($dmy[0] > 99) {
491             $fixed_date = sprintf("%04u-%02u-%02u", $dmy[0], $dmy[1], $dmy[2]);
492         } else {
493             if ($dmy[2] < 1000) $dmy[2] += 1900;
494             if ($dmy[2] < 1910) $dmy[2] += 100;
495             $fixed_date = sprintf("%04u-%02u-%02u", $dmy[2], $dmy[0], $dmy[1]);
496         }
497     }
499     return $fixed_date;
503 function updatePatientData($pid,$new)
505         $real = getPatientData($pid);
506         $new['DOB'] = fixDate($new['DOB']);
507         while(list($key, $value) = each ($new))
508                 $real[$key] = $value;
509         $real['date'] = "'+NOW()+'";
510         $real['id'] = "";
512         $sql = "insert into patient_data set ";
513         while(list($key, $value) = each($real))
514                 $sql .= $key." = '$value', ";
515         $sql = substr($sql, 0, -2);
518         return sqlInsert($sql);
522 function newEmployerData(       $pid,
523                                 $name = "",
524                                 $street = "",
525                                 $postal_code = "",
526                                 $city = "",
527                                 $state = "",
528                                 $country = ""
529                         )
531         return sqlInsert("insert into employer_data set
532                 name='$name',
533                 street='$street',
534                 postal_code='$postal_code',
535                 city='$city',
536                 state='$state',
537                 country='$country',
538                 pid='$pid',
539                 date=NOW()
540                 ");
543 function updateEmployerData($pid,$new)
545         $real = getEmployerData($pid);
546         while(list($key, $value) = each ($new))
547                 $real[$key] = $value;
548         $real['date'] = "'+NOW()+'";
549         $real['id'] = "";
551         $sql = "insert into employer_data set ";
552         while(list($key, $value) = each($real))
553                 $sql .= $key." = '$value', ";
554         $sql = substr($sql, 0, -2);
557         return sqlInsert($sql);
560 function newInsuranceData(      $pid,
561                                 $type = "",
562                                 $provider = "",
563                                 $policy_number = "",
564                                 $group_number = "",
565                                 $plan_name = "",
566                                 $subscriber_lname = "",
567                                 $subscriber_mname = "",
568                                 $subscriber_fname = "",
569                                 $subscriber_relationship = "",
570                                 $subscriber_ss = "",
571                                 $subscriber_DOB = "",
572                                 $subscriber_street = "",
573                                 $subscriber_postal_code = "",
574                                 $subscriber_city = "",
575                                 $subscriber_state = "",
576                                 $subscriber_country = "",
577                                 $subscriber_phone = "",
578                                 $subscriber_employer = "",
579                                 $subscriber_employer_street = "",
580                                 $subscriber_employer_city = "",
581                                 $subscriber_employer_postal_code = "",
582                                 $subscriber_employer_state = "",
583                                 $subscriber_employer_country = "",
584                                 $copay = "",
585                                 $subscriber_sex = ""
586                         )
588         if (strlen($type) > 0) {
589         $query = "select * from insurance_data where type='" . $type  . "' and pid = " . $pid .  " limit 1";
590         }
591         else {
592                 return FALSE;
593         }
594         $res = sqlQuery ($query);
596         if ($res) {
597                 $data['type'] = $type;
598                 $data['provider'] = $provider;
599                 $data['policy_number']=$policy_number;
600                 $data['group_number']=$group_number;
601                 $data['plan_name']=$plan_name;
602                 $data['subscriber_lname']=$subscriber_lname;
603                 $data['subscriber_mname']=$subscriber_mname;
604                 $data['subscriber_fname']=$subscriber_fname;
605                 $data['subscriber_relationship']=$subscriber_relationship;
606                 $data['subscriber_ss']=$subscriber_ss;
607                 $data['subscriber_DOB']=$subscriber_DOB;
608                 $data['subscriber_street']=$subscriber_street;
609                 $data['subscriber_postal_code']=$subscriber_postal_code;
610                 $data['subscriber_city']=$subscriber_city;
611                 $data['subscriber_state']=$subscriber_state;
612                 $data['subscriber_country']=$subscriber_country;
613                 $data['subscriber_phone']=$subscriber_phone;
614                 $data['subscriber_employer']=$subscriber_employer;
615                 $data['subscriber_employer_city']=$subscriber_employer_city;
616                 $data['subscriber_employer_street']=$subscriber_employer_street;
617                 $data['subscriber_employer_postal_code']=$subscriber_employer_postal_code;
618                 $data['subscriber_employer_state']=$subscriber_employer_state;
619                 $data['subscriber_employer_country']=$subscriber_employer_country;
620                 $data['copay']=$copay;
621                 $data['subscriber_sex']=$subscriber_sex;
622                 $data['pid']=$pid;
623                 $data['date']="NOW()";
624         //      echo "updating<br><br>";
626                 return updateInsuranceData($pid,$data);
627         }
628         else {
629         return sqlInsert("insert into insurance_data set
630                 type='$type',
631                 provider='$provider',
632                 policy_number='$policy_number',
633                 group_number='$group_number',
634                 plan_name='$plan_name',
635                 subscriber_lname='$subscriber_lname',
636                 subscriber_mname='$subscriber_mname',
637                 subscriber_fname='$subscriber_fname',
638                 subscriber_relationship='$subscriber_relationship',
639                 subscriber_ss='$subscriber_ss',
640                 subscriber_DOB='$subscriber_DOB',
641                 subscriber_street='$subscriber_street',
642                 subscriber_postal_code='$subscriber_postal_code',
643                 subscriber_city='$subscriber_city',
644                 subscriber_state='$subscriber_state',
645                 subscriber_country='$subscriber_country',
646                 subscriber_phone='$subscriber_phone',
647                 subscriber_employer = '$subscriber_employer',
648                 subscriber_employer_city='$subscriber_employer_city',
649                 subscriber_employer_street='$subscriber_employer_street',
650                 subscriber_employer_postal_code='$subscriber_employer_postal_code',
651                 subscriber_employer_state='$subscriber_employer_state',
652                 subscriber_employer_country='$subscriber_employer_country',
653                 copay='$copay',
654                 subscriber_sex='$subscriber_sex',
655                 pid='$pid',
656                 date=NOW()
657                 ");
658         }
661 function updateInsuranceData($pid,$new)
663                 $fields = sqlListFields("insurance_data");
665         $real = getInsuranceData($pid);
666         $use = array();
667         while(list($key, $value) = each ($new)) {
668                         if (in_array($key,$fields)){
669                         $use[$key] = $value;
670                         }
671                 }
672         $real['date'] = "'+NOW()+'";
673         $real['id'] = "";
675         $sql = "replace into insurance_data set ";
676         while(list($key, $value) = each($use))
677                 $sql .= $key." = '$value', ";
680         $sql = substr($sql, 0, -2);
682         //echo $sql;
683         //exit;
684         return sqlInsert($sql);
688 function newHistoryData(        $pid,
689                                 $coffee = "",
690                                 $tobacco = "",
691                                 $alcohol = "",
692                                 $sleep_patterns = "",
693                                 $exercise_patterns = "",
694                                 $seatbelt_use = "",
695                                 $counseling = "",
696                                 $hazardous_activities = "",
697                                 $last_breast_exam = "",
698                                 $last_mammogram = "",
699                                 $last_gynocological_exam = "",
700                                 $last_rectal_exam = "",
701                                 $last_prostate_exam = "",
702                                 $last_physical_exam = "",
703                                 $last_sigmoidoscopy_colonoscopy = "",
704                                 $history_mother = "",
705                                 $history_father = "",
706                                 $history_siblings = "",
707                                 $history_offspring = "",
708                                 $history_spouse = "",
709                                 $relatives_cancer = "",
710                                 $relatives_tuberculosis = "",
711                                 $relatives_diabetes = "",
712                                 $relatives_high_blood_pressure = "",
713                                 $relatives_heart_problems = "",
714                                 $relatives_stroke = "",
715                                 $relatives_epilepsy = "",
716                                 $relatives_mental_illness = "",
717                                 $relatives_suicide = "",
718                                 $cataract_surgery = "",
719                                 $tonsillectomy = "",
720                                 $appendectomy = "",
721                                 $cholecystestomy = "",
722                                 $heart_surgery = "",
723                                 $hysterectomy = "",
724                                 $hernia_repair = "",
725                                 $hip_replacement = "",
726                                 $knee_replacement = "",
727                                 $name_1 = "",
728                                 $value_1 = "",
729                                 $name_2 = "",
730                                 $value_2 = "",
731                                 $additional_history = ""
732                                 )
734         return sqlInsert("insert into history_data set
735                 coffee='$coffee',
736                 tobacco='$tobacco',
737                 alcohol='$alcohol',
738                 sleep_patterns='$sleep_patterns',
739                 exercise_patterns='$exercise_patterns',
740                 seatbelt_use='$seatbelt_use',
741                 counseling='$counseling',
742                 hazardous_activities='$hazardous_activities',
743                 last_breast_exam='$last_breast_exam',
744                 last_mammogram='$last_mammogram',
745                 last_gynocological_exam='$last_gynocological_exam',
746                 last_rectal_exam='$last_rectal_exam',
747                 last_prostate_exam='$last_prostate_exam',
748                 last_physical_exam='$last_physical_exam',
749                 last_sigmoidoscopy_colonoscopy='$last_sigmoidoscopy_colonoscopy',
750                 history_mother='$history_mother',
751                 history_father='$history_father',
752                 history_siblings='$history_siblings',
753                 history_offspring='$history_offspring',
754                 history_spouse='$history_spouse',
755                 relatives_cancer='$relatives_cancer',
756                 relatives_tuberculosis ='$relatives_tuberculosis',
757                 relatives_diabetes='$relatives_diabetes',
758                 relatives_high_blood_pressure='$relatives_high_blood_pressure',
759                 relatives_heart_problems='$relatives_heart_problems',
760                 relatives_stroke='$relatives_stroke',
761                 relatives_epilepsy='$relatives_epilepsy',
762                 relatives_mental_illness='$relatives_mental_illness',
763                 relatives_suicide='$relatives_suicide',
764                 cataract_surgery='$cataract_surgery',
765                 tonsillectomy='$tonsillectomy',
766                 appendectomy='$appendectomy',
767                 cholecystestomy='$cholecystestomy',
768                 heart_surgery='$heart_surgery',
769                 hysterectomy='$hysterectomy',
770                 hernia_repair='$hernia_repair',
771                 hip_replacement='$hip_replacement',
772                 knee_replacement='$knee_replacement',
773                 name_1 = '$name_1',
774                 value_1 = '$value_1',
775                 name_2 = '$name_2',
776                 value_2 = '$value_2',
777                 additional_history = '$additional_history',
778                 date=NOW(),
779                 pid='$pid'
780                 ");
783 function updateHistoryData($pid,$new)
785         $real = getHistoryData($pid);
786         while(list($key, $value) = each ($new))
787                 $real[$key] = $value;
788         $real['date'] = "'+NOW()+'";
789         $real['id'] = "";
791         $sql = "insert into history_data set ";
792         while(list($key, $value) = each($real))
793                 $sql .= $key." = '$value', ";
794         $sql = substr($sql, 0, -2);
797         return sqlInsert($sql);
800 function sync_patient($id,$fname,$lname,$street,$city,$postal_code,$state,$phone_home,
801                                 $phone_biz,$email,$pid="")
803         $db = $GLOBALS['adodb']['db'];
804         $customer_info = array();
806         $sql = "SELECT foreign_id,foreign_table FROM integration_mapping where local_table = 'patient_data' and local_id = '" . $id . "'";
807         $result = $db->Execute($sql);
808         if ($result && !$result->EOF) {
809                 $customer_info['foreign_update'] = true;
810                 $customer_info['foreign_id'] = $result->fields['foreign_id'];
811                 $customer_info['foreign_table'] = $result->fields['foreign_table'];
812         }
814         ///xml rpc code to connect to accounting package and add user to it
815         $customer_info['firstname'] = $fname;
816         $customer_info['lastname'] = $lname;
817         $customer_info['address'] = $street;
818         $customer_info['suburb'] = $city;
819         $customer_info['state'] = $state;
820         $customer_info['postcode'] = $postal_code;
822         //ezybiz wants state as a code rather than abbreviation
823         $customer_info['geo_zone_id'] = "";
824         $sql = "SELECT zone_id from geo_zone_reference where zone_code = '" . strtoupper($state) . "'";
825         $db = $GLOBALS['adodb']['db'];
826         $result = $db->Execute($sql);
827         if ($result && !$result->EOF) {
828                 $customer_info['geo_zone_id'] = $result->fields['zone_id'];
829         }
831         //ezybiz wants country as a code rather than abbreviation
832         $customer_info['geo_country_id'] = "";
833         $sql = "SELECT countries_id from geo_country_reference where countries_iso_code_2 = '" . strtoupper($country_code) . "'";
834         $db = $GLOBALS['adodb']['db'];
835         $result = $db->Execute($sql);
836         if ($result && !$result->EOF) {
837                 $customer_info['geo_country_id'] = $result->fields['countries_id'];
838         }
841         $customer_info['phone1'] = $phone_home;
842         $customer_info['phone1comment'] = "Home Phone";
843         $customer_info['phone2'] = $phone_biz;
844         $customer_info['phone2comment'] = "Business Phone";
845         $customer_info['email'] = $email;
846         $customer_info['customernumber'] = $pid;
848         $function['ezybiz.add_customer'] = array(new xmlrpcval($customer_info,"struct"));
849         $ws = new WSWrapper($function);
851         // if the remote patient was added make an entry in the local mapping table to that updates can be made correctly
852         if (is_numeric($ws->value)) {
853                 $sql = "REPLACE INTO integration_mapping set id = '" . $db->GenID("sequences") . "', foreign_id ='" . $ws->value . "', foreign_table ='customer', local_id = '" . $id . "', local_table = 'patient_data' ";
854                 $db->Execute($sql) or die ("error: " . $db->ErrorMsg());
855         }
858 // Returns Date of Birth given YYYYMMDD from MySQL DATE_FORMAT(DOB,'%Y%m%d')
859 function getPatientAge($dobYMD)
861     $tdyYMD=date("Ymd");
862     $yearDiff = substr($tdyYMD,0,4) - substr($dobYMD,0,4);
863     $ageInMonths = ((substr($tdyYMD,0,4)*12)+substr($tdyYMD,4,2)) -
864                    ((substr($dobYMD,0,4)*12)+substr($dobYMD,4,2));
865     $dayDiff = substr($tdyYMD,6,2) - substr($dobYMD,6,2);
866     if ( $dayDiff < 0 ) {
867         $ageInMonths -= 1;
868     }
869     if ( $ageInMonths > 24 ) {
870         $age = intval($ageInMonths/12);
871     }
872     else  {
873         $age = "$ageInMonths month";
874     }
875     return $age;
878 function dateToDB ($date) 
880         $date=substr ($date,6,4)."-".substr ($date,3,2)."-".substr($date, 0,2);
881         return $date;
887 function DBToDate ($date)
889         $date=substr ($date,5,2)."/".substr ($date,8,2)."/".substr($date, 0,4);
890         return $date;