in some php versions touch will give an error, error output supressed by @
[openemr.git] / library / patient.inc
blobbcd4f1b527cfc1107435da45b47ba4e6c4fb1733
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();
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'];
31         }
32         print_r($retval);
34         return $returnval;
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"]);
45                 }
46         }
49         return $returnval;
52 function getProviderInfo($providerID = "%", $providers_only = true) {
53         $param1 = "";
54         if ($providers_only) {
55                 $param1 = "AND authorized=1";
56         }
57         $command = "=";
58         if ($providerID == "%") {
59                 $command = "like";
60         }
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']
69         if($iter==1) {
70                 $akeys = array_keys($returnval[0]);
71                 foreach($akeys as $key) {
73                         $returnval[0][$key] = $returnval[0][$key];
74                 }
75         }
76         return $returnval;
79 //same as above but does not reduce if only 1 row returned
80 function getCalendarProviderInfo($providerID = "%", $providers_only = true) {
81         $param1 = "";
82         if ($providers_only) {
83                 $param1 = "AND authorized=1";
84         }
85         $command = "=";
86         if ($providerID == "%") {
87                 $command = "like";
88         }
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;
95         return $returnval;
100 function getProviderName($providerID) {
102         $pi = getProviderInfo($providerID);
104         if (strlen($pi[0]["lname"]) > 0) {
105                 return $pi[0]['fname'] . " " . $pi[0]['lname'];
106         }
108         return "";
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;
118         return $returnval;
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"]);
128                 }
129         }
132         return $returnval;
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")
165         /****
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";
171         if ($iter > 0)
172                 $sql = substr($sql, 0, -3) . ") order by $orderby";
173         else
174                 $sql = substr($sql, 0, -5)."order by $orderby";
175         ****/
177         // WTF? That was a good way to create a 200KB sql statement.
179         $sql="select $given from patient_data where lname like '$lname%' " .
180                 "order by $orderby";
182         if ($limit != "all")
183                 $sql .= " limit $start, $limit";
184         $rez = sqlStatement($sql);
186         for($iter=0; $row=sqlFetchArray($rez); $iter++)
187                 $returnval[$iter]=$row;
189         return $returnval;
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")
194         /****
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";
200         if ($iter > 0)
201                 $sql = substr($sql, 0, -3) . ") order by $orderby";
202         else
203                 $sql = substr($sql, 0, -5)."order by $orderby";
204         ****/
206         $sql = "select $given from patient_data where pubpid like '$pid%' " .
207                 "order by $orderby";
209         if ($limit != "all")
210                 $sql .= " limit $start, $limit";
211         $rez = sqlStatement($sql);
212         for($iter=0; $row=sqlFetchArray($rez); $iter++)
213                 $returnval[$iter]=$row;
215         return $returnval;
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")
220         $command = "=";
221         if ($pid == -1)
222                 $pid = "%";
223         elseif (empty($pid))
224                 $pid = "NULL";
226         if (strstr($pid,"%"))
227                 $command = "like";
229         $sql="select $given from patient_data where pid $command '$pid' order by $orderby";
231         if ($limit != "all")
232                 $sql .= " limit $start, $limit";
234         $rez = sqlStatement($sql);
235         for($iter=0; $row=sqlFetchArray($rez); $iter++)
236                 $returnval[$iter]=$row;
239         return $returnval;
242 function getPatientName($pid) {
243         if (empty($pid))
244                 return "";
245         $patientData = getPatientPID($pid);
246         if (empty($patientData[0]['lname']))
247                 return "";
248         $patientName =  $patientData[0]['lname'] . ", " . $patientData[0]['fname'];
249         return $patientName;
253 function getPatientDOB($DOB = "%", $given = "pid, id, lname, fname, mname", $orderby = "lname ASC, fname ASC", $limit="all", $start="0")
255         /****
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";
261         if ($iter > 0)
262                 $sql = substr($sql, 0, -3) . ") order by $orderby";
263         else
264                 $sql = substr($sql, 0, -5)."order by $orderby";
265         ****/
267         $DOB = fixDate($DOB, $DOB);
269         $sql="select $given from patient_data where DOB like '$DOB%' " .
270                 "order by $orderby";
272         if ($limit != "all")
273                 $sql .= " limit $start, $limit";
275         $rez = sqlStatement($sql);
276         for($iter=0; $row=sqlFetchArray($rez); $iter++)
277                 $returnval[$iter]=$row;
279         return $returnval;
282 function getPatientSSN($ss = "%", $given = "pid, id, lname, fname, mname, providerID", $orderby = "lname ASC, fname ASC", $limit="all", $start="0")
284         /****
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";
290         if ($iter > 0)
291                 $sql = substr($sql, 0, -3) . ") order by $orderby";
292         else
293                 $sql = substr($sql, 0, -5)."order by $orderby";
294         ****/
296         $sql="select $given from patient_data where ss like '$ss%' " .
297                 "order by $orderby";
299         if ($limit != "all")
300                 $sql .= " limit $start, $limit";
302         $rez = sqlStatement($sql);
303         for($iter=0; $row=sqlFetchArray($rez); $iter++)
304                 $returnval[$iter]=$row;
306         return $returnval;
309 function getPatientIds($given = "pid, id, lname, fname, mname", $orderby = "id ASC", $limit="all", $start="0")
311         /****
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";
318         ****/
320         $sql="select $given from patient_data 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 //----------------------input functions
333 function newPatientData(        $db_id="",
334                                 $title = "",
335                                 $fname = "",
336                                 $lname = "",
337                                 $mname = "",
338                                 $sex = "",
339                                 $DOB = "",
340                                 $street = "",
341                                 $postal_code = "",
342                                 $city = "",
343                                 $state = "",
344                                 $country_code = "",
345                                 $ss = "",
346                                 $occupation = "",
347                                 $phone_home = "",
348                                 $phone_biz = "",
349                                 $phone_contact = "",
350                                 $status = "",
351                                 $contact_relationship = "",
352                                 $referrer = "",
353                                 $referrerID = "",
354                                 $email = "",
355                                 $language = "",
356                                 $ethnoracial = "",
357                                 $interpretter = "",
358                                 $migrantseasonal = "",
359                                 $family_size = "",
360                                 $monthly_income = "",
361                                 $homeless = "",
362                                 $financial_review = "",
363                                 $pubpid = "",
364                                 $pid = "MAX(pid)+1",
365                                 $providerID = "",
366                                 $genericname1 = "",
367                                 $genericval1 = "",
368                                 $genericname2 = "",
369                                 $genericval2 = "",
370                                 $phone_cell = ""
371                         )
373         $DOB = fixDate($DOB);
374         $query = ("replace into patient_data set
375                 id='$db_id',
376                 title='$title',
377                 fname='$fname',
378                 lname='$lname',
379                 mname='$mname',
380                 sex='$sex',
381                 DOB='$DOB',
382                 street='$street',
383                 postal_code='$postal_code',
384                 city='$city',
385                 state='$state',
386                 country_code='$country_code',
387                 ss='$ss',
388                 occupation='$occupation',
389                 phone_home='$phone_home',
390                 phone_biz='$phone_biz',
391                 phone_contact='$phone_contact',
392                 status='$status',
393                 contact_relationship='$contact_relationship',
394                 referrer='$referrer',
395                 referrerID='$referrerID',
396                 email='$email',
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',
405                 pubpid='$pubpid',
406                 pid = $pid,
407                 providerID = '$providerID',
408                 genericname1 = '$genericname1',
409                 genericval1 = '$genericval1',
410                 genericname2 = '$genericname2',
411                 genericval2 = '$genericval2',
412                 phone_cell = '$phone_cell',
413                 date=NOW()
414                         ");
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,
420                                 $phone_biz,$email);
422         return $foo['pid'];
425 // Supported input date formats are:
426 //   mm/dd/yyyy
427 //   mm/dd/yy   (assumes 19yy)
428 //   yyyy/mm/dd
429 //   also mm-dd-yyyy, etc. and mm.dd.yyyy, etc.
431 function fixDate($date, $default="0000-00-00") {
432     $fixed_date = $default;
433     $date = trim($date);
434     if (preg_match("'^[0-9]{1,4}[/.-][0-9]{1,2}[/.-][0-9]{1,4}$'", $date)) {
435         $dmy = preg_split("'[/.-]'", $date);
436         if ($dmy[0] > 99) {
437             $fixed_date = sprintf("%04u-%02u-%02u", $dmy[0], $dmy[1], $dmy[2]);
438         } else {
439             if ($dmy[2] < 1000) $dmy[2] += 1900;
440             $fixed_date = sprintf("%04u-%02u-%02u", $dmy[2], $dmy[0], $dmy[1]);
441         }
442     }
444     return $fixed_date;
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()+'";
455         $real['id'] = "";
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,
468                                 $name = "",
469                                 $street = "",
470                                 $postal_code = "",
471                                 $city = "",
472                                 $state = "",
473                                 $country = ""
474                         )
476         return sqlInsert("insert into employer_data set
477                 name='$name',
478                 street='$street',
479                 postal_code='$postal_code',
480                 city='$city',
481                 state='$state',
482                 country='$country',
483                 pid='$pid',
484                 date=NOW()
485                 ");
488 function updateEmployerData($pid,$new)
490         $real = getEmployerData($pid);
491         while(list($key, $value) = each ($new))
492                 $real[$key] = $value;
493         $real['date'] = "'+NOW()+'";
494         $real['id'] = "";
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,
506                                 $type = "",
507                                 $provider = "",
508                                 $policy_number = "",
509                                 $group_number = "",
510                                 $plan_name = "",
511                                 $subscriber_lname = "",
512                                 $subscriber_mname = "",
513                                 $subscriber_fname = "",
514                                 $subscriber_relationship = "",
515                                 $subscriber_ss = "",
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 = "",
529                                 $copay = "",
530                                 $subscriber_sex = ""
531                         )
533         if (strlen($type) > 0) {
534         $query = "select * from insurance_data where type='" . $type  . "' and pid = " . $pid .  " limit 1";
535         }
536         else {
537                 return FALSE;
538         }
539         $res = sqlQuery ($query);
541         if ($res) {
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;
567                 $data['pid']=$pid;
568                 $data['date']="NOW()";
569         //      echo "updating<br><br>";
571                 return updateInsuranceData($pid,$data);
572         }
573         else {
574         return sqlInsert("insert into insurance_data set
575                 type='$type',
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',
598                 copay='$copay',
599                 subscriber_sex='$subscriber_sex',
600                 pid='$pid',
601                 date=NOW()
602                 ");
603         }
606 function updateInsuranceData($pid,$new)
608                 $fields = sqlListFields("insurance_data");
610         $real = getInsuranceData($pid);
611         $use = array();
612         while(list($key, $value) = each ($new)) {
613                         if (in_array($key,$fields)){
614                         $use[$key] = $value;
615                         }
616                 }
617         $real['date'] = "'+NOW()+'";
618         $real['id'] = "";
620         $sql = "replace into insurance_data set ";
621         while(list($key, $value) = each($use))
622                 $sql .= $key." = '$value', ";
625         $sql = substr($sql, 0, -2);
627         //echo $sql;
628         //exit;
629         return sqlInsert($sql);
633 function newHistoryData(        $pid,
634                                 $coffee = "",
635                                 $tobacco = "",
636                                 $alcohol = "",
637                                 $sleep_patterns = "",
638                                 $exercise_patterns = "",
639                                 $seatbelt_use = "",
640                                 $counseling = "",
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 = "",
664                                 $tonsillectomy = "",
665                                 $appendectomy = "",
666                                 $cholecystestomy = "",
667                                 $heart_surgery = "",
668                                 $hysterectomy = "",
669                                 $hernia_repair = "",
670                                 $hip_replacement = "",
671                                 $knee_replacement = "",
672                                 $name_1 = "",
673                                 $value_1 = "",
674                                 $name_2 = "",
675                                 $value_2 = "",
676                                 $additional_history = ""
677                                 )
679         return sqlInsert("insert into history_data set
680                 coffee='$coffee',
681                 tobacco='$tobacco',
682                 alcohol='$alcohol',
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',
718                 name_1 = '$name_1',
719                 value_1 = '$value_1',
720                 name_2 = '$name_2',
721                 value_2 = '$value_2',
722                 additional_history = '$additional_history',
723                 date=NOW(),
724                 pid='$pid'
725                 ");
728 function updateHistoryData($pid,$new)
730         $real = getHistoryData($pid);
731         while(list($key, $value) = each ($new))
732                 $real[$key] = $value;
733         $real['date'] = "'+NOW()+'";
734         $real['id'] = "";
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,
746                                 $phone_biz,$email)
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'];
757         }
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'];
774         }
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'];
783         }
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());
799         }
802 // Returns Date of Birth given YYYYMMDD from MySQL DATE_FORMAT(DOB,'%Y%m%d')
803 function getPatientAge($dobYMD)
805     $tdyYMD=date("Ymd");
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 ) {
811         $ageInMonths -= 1;
812     }
813     if ( $ageInMonths > 24 ) {
814         $age = intval($ageInMonths/12);
815     }
816     else  {
817         $age = "$ageInMonths month";
818     }
819     return $age;