Feature custom loading of dutch prescription class.
[openemr.git] / library / patient.inc
bloba34543ab8a2705806c7db2c6b661b1fdc2e34914
1 <?php
2 include_once("{$GLOBALS['srcdir']}/sql.inc");
3 require_once(dirname(__FILE__) . "/classes/WSWrapper.class.php");
5 // These are for sports team use:
6 $PLAYER_FITNESSES = array(
7   xl('Full Play'),
8   xl('Full Training'),
9   xl('Restricted Training'),
10   xl('Injured Out'),
11   xl('Rehabilitation'),
12   xl('Illness'),
13   xl('International Duty')
15 $PLAYER_FITCOLORS = array('#6677ff', '#00cc00', '#ffff00', '#ff3333', '#ff8800', '#ffeecc', '#ffccaa');
17 function getPatientData($pid, $given = "*, DATE_FORMAT(DOB,'%m/%d/%Y') as DOB_TS") {
18         $sql = "select $given from patient_data where pid='$pid' order by date DESC limit 0,1";
19         return sqlQuery($sql);
22 function getLanguages() {
23         $returnval = array('','english');
24         $sql = "select distinct lower(language) as language from patient_data";
25         $rez = sqlStatement($sql);
26         for($iter=0; $row=sqlFetchArray($rez); $iter++) {
27                 if (($row["language"] != "english") && ($row["language"] != "")) {
28                         array_push($returnval, $row["language"]);
29                 }
30         }
31         return $returnval;
34 function getInsuranceProviders() {
35         $returnval = array();
37         if (true) {
38                 $sql = "select name, id from insurance_companies order by name, id";
39                 $rez = sqlStatement($sql);
40                 for($iter=0; $row=sqlFetchArray($rez); $iter++) {
41                         $returnval[$row['id']] = $row['name'];
42                 }
43         }
45         // Please leave this here. I have a user who wants to see zip codes and PO
46         // box numbers listed along with the insurance company names, as many companies
47         // have different billing addresses for different plans.  -- Rod Roark
48         //
49         else {
50                 $sql = "select insurance_companies.name, insurance_companies.id, " .
51                   "addresses.zip, addresses.line1 " .
52                   "from insurance_companies, addresses " .
53                   "where addresses.foreign_id = insurance_companies.id " .
54                   "order by insurance_companies.name, addresses.zip";
56                 $rez = sqlStatement($sql);
58                 for($iter=0; $row=sqlFetchArray($rez); $iter++) {
59                         preg_match("/\d+/", $row['line1'], $matches);
60                         $returnval[$row['id']] = $row['name'] . " (" . $row['zip'] .
61                           "," . $matches[0] . ")";
62                 }
63         }
65         return $returnval;
68 function getProviders() {
69         $returnval = array("");
70         $sql = "select fname, lname from users where authorized = 1 and " .
71                 "active = 1 and username != ''";
72         $rez = sqlStatement($sql);
73         for($iter=0; $row=sqlFetchArray($rez); $iter++) {
74                 if (($row["fname"] != "") && ($row["lname"] != "")) {
75                         array_push($returnval, $row["fname"] . " " . $row["lname"]);
76                 }
77         }
78         return $returnval;
81 function getProviderInfo($providerID = "%", $providers_only = true) {
82         $param1 = "";
83         if ($providers_only) {
84                 $param1 = "AND authorized=1";
85         }
86         $command = "=";
87         if ($providerID == "%") {
88                 $command = "like";
89         }
90         $query = "select distinct id, username, lname, fname, authorized, info, facility " .
91                 "from users where username != '' and active = 1 and id $command '" .
92                 mysql_real_escape_string($providerID) . "' " . $param1;
93         $rez = sqlStatement($query);
94         for($iter=0; $row=sqlFetchArray($rez); $iter++)
95                 $returnval[$iter]=$row;
97         //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
98         //accessible from $resultval['key']
100         if($iter==1) {
101                 $akeys = array_keys($returnval[0]);
102                 foreach($akeys as $key) {
104                         $returnval[0][$key] = $returnval[0][$key];
105                 }
106         }
107         return $returnval;
110 //same as above but does not reduce if only 1 row returned
111 function getCalendarProviderInfo($providerID = "%", $providers_only = true) {
112         $param1 = "";
113         if ($providers_only) {
114                 $param1 = "AND authorized=1";
115         }
116         $command = "=";
117         if ($providerID == "%") {
118                 $command = "like";
119         }
120         $query = "select distinct id, username, lname, fname, authorized, info, facility " .
121                 "from users where active = 1 and username != '' and id $command '" .
122                 mysql_real_escape_string($providerID) . "' " . $param1;
124         $rez = sqlStatement($query);
125         for($iter=0; $row=sqlFetchArray($rez); $iter++)
126                 $returnval[$iter]=$row;
128         return $returnval;
131 function getProviderName($providerID) {
132         $pi = getProviderInfo($providerID);
133         if (strlen($pi[0]["lname"]) > 0) {
134                 return $pi[0]['fname'] . " " . $pi[0]['lname'];
135         }
136         return "";
139 function getProviderId($providerName) {
140         $query = "select id from users where username = '". mysql_real_escape_string($providerName)."'";
141         $rez = sqlStatement($query);
142         for($iter=0; $row=sqlFetchArray($rez); $iter++)
143                 $returnval[$iter]=$row;
144         return $returnval;
147 function getEthnoRacials() {
148         $returnval = array("");
149         $sql = "select distinct lower(ethnoracial) as ethnoracial from patient_data";
150         $rez = sqlStatement($sql);
151         for($iter=0; $row=sqlFetchArray($rez); $iter++) {
152                 if (($row["ethnoracial"] != "")) {
153                         array_push($returnval, $row["ethnoracial"]);
154                 }
155         }
156         return $returnval;
159 function getHistoryData($pid, $given = "*")
161         $sql = "select $given from history_data where pid='$pid' order by date DESC limit 0,1";
162         return sqlQuery($sql);
165 // function getInsuranceData($pid, $type = "primary", $given = "insd.*, DATE_FORMAT(subscriber_DOB,'%m/%d/%Y') as subscriber_DOB, ic.name as provider_name")
166 function getInsuranceData($pid, $type = "primary", $given = "insd.*, ic.name as provider_name")
168         $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";
169         return sqlQuery($sql);
172 function getInsuranceDataByDate( $pid, $date, $type, $given = "insd.*, DATE_FORMAT(subscriber_DOB,'%m/%d/%Y') as subscriber_DOB, ic.name as provider_name")
173 { //this must take the date in the following manner: YYYY-MM-DD
174         //this function recalls the insurance value that was most recently enterred from the
175         //given date. it will call up most recent records up to and on the date given,
176         //but not records enterred after the given date
177         $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";
178         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         // Allow the last name to be followed by a comma and some part of a first name.
190         // New behavior for searches:
191         // Allows comma alone followed by some part of a first name
192         // If the first letter of either name is capital, searches for name starting
193         // with given substring (the expected behavior).  If it is lower case, it
194         // it searches for the substring anywhere in the name.  This applies to either
195         // last name or first name or both.  The arbitrary limit of 100 results is set
196         // in the sql query below. --Mark Leeds
197         $lname = trim($lname);
198         $fname = '';
199         if (preg_match('/^(.*),(.*)/', $lname, $matches)) {
200                 $lname = trim($matches[1]);
201                 $fname = trim($matches[2]);
202         }
203         $search_for_pieces1 = '';
204         $search_for_pieces2 = '';
205         if ($lname{0} != strtoupper($lname{0})) {$search_for_pieces1 = '%';}
206         if ($fname{0} != strtoupper($fname{0})) {$search_for_pieces2 = '%';}
207         $sql="select $given from patient_data where lname like '"
208                 .$search_for_pieces1."$lname%' "
209                 ."and fname like '"
210                 .$search_for_pieces2."$fname%' "
211                 ."order by $orderby limit 100";
213         if ($limit != "all")
214                 $sql .= " limit $start, $limit";
215         $rez = sqlStatement($sql);
217         for($iter=0; $row=sqlFetchArray($rez); $iter++)
218                 $returnval[$iter]=$row;
220         return $returnval;
223 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")
225         $sql = "select $given from patient_data where pubpid like '$pid%' " .
226                 "order by $orderby";
228         if ($limit != "all")
229                 $sql .= " limit $start, $limit";
230         $rez = sqlStatement($sql);
231         for($iter=0; $row=sqlFetchArray($rez); $iter++)
232                 $returnval[$iter]=$row;
234         return $returnval;
237 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")
239         $command = "=";
240         if ($pid == -1)
241                 $pid = "%";
242         elseif (empty($pid))
243                 $pid = "NULL";
245         if (strstr($pid,"%"))
246                 $command = "like";
248         $sql="select $given from patient_data where pid $command '$pid' order by $orderby";
250         if ($limit != "all")
251                 $sql .= " limit $start, $limit";
253         $rez = sqlStatement($sql);
254         for($iter=0; $row=sqlFetchArray($rez); $iter++)
255                 $returnval[$iter]=$row;
257         return $returnval;
260 function getPatientName($pid) {
261         if (empty($pid))
262                 return "";
263         $patientData = getPatientPID($pid);
264         if (empty($patientData[0]['lname']))
265                 return "";
266         $patientName =  $patientData[0]['lname'] . ", " . $patientData[0]['fname'];
267         return $patientName;
270 function getPatientDOB($DOB = "%", $given = "pid, id, lname, fname, mname", $orderby = "lname ASC, fname ASC", $limit="all", $start="0")
272         $DOB = fixDate($DOB, $DOB);
274         $sql="select $given from patient_data where DOB like '$DOB%' " .
275                 "order by $orderby";
277         if ($limit != "all")
278                 $sql .= " limit $start, $limit";
280         $rez = sqlStatement($sql);
281         for($iter=0; $row=sqlFetchArray($rez); $iter++)
282                 $returnval[$iter]=$row;
284         return $returnval;
287 function getPatientSSN($ss = "%", $given = "pid, id, lname, fname, mname, providerID", $orderby = "lname ASC, fname ASC", $limit="all", $start="0")
289         $sql="select $given from patient_data where ss like '$ss%' " .
290                 "order by $orderby";
292         if ($limit != "all")
293                 $sql .= " limit $start, $limit";
295         $rez = sqlStatement($sql);
296         for($iter=0; $row=sqlFetchArray($rez); $iter++)
297                 $returnval[$iter]=$row;
299         return $returnval;
302 function getPatientIds($given = "pid, id, lname, fname, mname", $orderby = "id ASC", $limit="all", $start="0")
304         $sql="select $given from patient_data order by $orderby";
306         if ($limit != "all")
307                 $sql .= " limit $start, $limit";
309         $rez = sqlStatement($sql);
310         for($iter=0; $row=sqlFetchArray($rez); $iter++)
311                 $returnval[$iter]=$row;
313         return $returnval;
316 //----------------------input functions
317 function newPatientData(        $db_id="",
318                                 $title = "",
319                                 $fname = "",
320                                 $lname = "",
321                                 $mname = "",
322                                 $sex = "",
323                                 $DOB = "",
324                                 $street = "",
325                                 $postal_code = "",
326                                 $city = "",
327                                 $state = "",
328                                 $country_code = "",
329                                 $ss = "",
330                                 $occupation = "",
331                                 $phone_home = "",
332                                 $phone_biz = "",
333                                 $phone_contact = "",
334                                 $status = "",
335                                 $contact_relationship = "",
336                                 $referrer = "",
337                                 $referrerID = "",
338                                 $email = "",
339                                 $language = "",
340                                 $ethnoracial = "",
341                                 $interpretter = "",
342                                 $migrantseasonal = "",
343                                 $family_size = "",
344                                 $monthly_income = "",
345                                 $homeless = "",
346                                 $financial_review = "",
347                                 $pubpid = "",
348                                 $pid = "MAX(pid)+1",
349                                 $providerID = "",
350                                 $genericname1 = "",
351                                 $genericval1 = "",
352                                 $genericname2 = "",
353                                 $genericval2 = "",
354                                 $phone_cell = "",
355                                 $hipaa_mail = "",
356                                 $hipaa_voice = "",
357                                 $squad = 0,
358                                 $pharmacy_id = 0,
359                                 $drivers_license = "",
360                                 $hipaa_notice = "",
361                                 $hipaa_message = ""
362                         )
364         $DOB = fixDate($DOB);
366         $fitness = 0;
367         $referral_source = '';
368         if ($pid) {
369                 $rez = sqlQuery("select id, fitness, referral_source from patient_data where pid = $pid");
370                 // Check for brain damage:
371                 if ($db_id != $rez['id']) {
372                         $errmsg = "Internal error: Attempt to change patient_data.id from '" .
373                           $rez['id'] . "' to '$db_id' for pid '$pid'";
374                         die($errmsg);
375                 }
376                 $fitness = $rez['fitness'];
377                 $referral_source = $rez['referral_source'];
378         }
380         $query = ("replace into patient_data set
381                 id='$db_id',
382                 title='$title',
383                 fname='$fname',
384                 lname='$lname',
385                 mname='$mname',
386                 sex='$sex',
387                 DOB='$DOB',
388                 street='$street',
389                 postal_code='$postal_code',
390                 city='$city',
391                 state='$state',
392                 country_code='$country_code',
393                 drivers_license='$drivers_license',
394                 ss='$ss',
395                 occupation='$occupation',
396                 phone_home='$phone_home',
397                 phone_biz='$phone_biz',
398                 phone_contact='$phone_contact',
399                 status='$status',
400                 contact_relationship='$contact_relationship',
401                 referrer='$referrer',
402                 referrerID='$referrerID',
403                 email='$email',
404                 language='$language',
405                 ethnoracial='$ethnoracial',
406                 interpretter='$interpretter',
407                 migrantseasonal='$migrantseasonal',
408                 family_size='$family_size',
409                 monthly_income='$monthly_income',
410                 homeless='$homeless',
411                 financial_review='$financial_review',
412                 pubpid='$pubpid',
413                 pid = $pid,
414                 providerID = '$providerID',
415                 genericname1 = '$genericname1',
416                 genericval1 = '$genericval1',
417                 genericname2 = '$genericname2',
418                 genericval2 = '$genericval2',
419                 phone_cell = '$phone_cell',
420                 pharmacy_id = '$pharmacy_id',
421                 hipaa_mail = '$hipaa_mail',
422                 hipaa_voice = '$hipaa_voice',
423                 hipaa_notice = '$hipaa_notice',
424                 hipaa_message = '$hipaa_message',
425                 squad = '$squad',
426                 fitness='$fitness',
427                 referral_source='$referral_source',
428                 date=NOW()
429                         ");
431         $id = sqlInsert($query);
432         $foo = sqlQuery("select pid from patient_data where id='$id' order by date limit 0,1");
434         sync_patient($id,$fname,$lname,$street,$city,$postal_code,$state,$phone_home,
435                                 $phone_biz,$email,$pid);
437         return $foo['pid'];
440 // Supported input date formats are:
441 //   mm/dd/yyyy
442 //   mm/dd/yy   (assumes 20yy for yy < 10, else 19yy)
443 //   yyyy/mm/dd
444 //   also mm-dd-yyyy, etc. and mm.dd.yyyy, etc.
446 function fixDate($date, $default="0000-00-00") {
447     $fixed_date = $default;
448     $date = trim($date);
449     if (preg_match("'^[0-9]{1,4}[/.-][0-9]{1,2}[/.-][0-9]{1,4}$'", $date)) {
450         $dmy = preg_split("'[/.-]'", $date);
451         if ($dmy[0] > 99) {
452             $fixed_date = sprintf("%04u-%02u-%02u", $dmy[0], $dmy[1], $dmy[2]);
453         } else {
454             if ($dmy[2] < 1000) $dmy[2] += 1900;
455             if ($dmy[2] < 1910) $dmy[2] += 100;
456             $fixed_date = sprintf("%04u-%02u-%02u", $dmy[2], $dmy[0], $dmy[1]);
457         }
458     }
460     return $fixed_date;
463 function updatePatientData($pid,$new)
465         $real = getPatientData($pid);
466         $new['DOB'] = fixDate($new['DOB']);
467         while(list($key, $value) = each ($new))
468                 $real[$key] = $value;
469         $real['date'] = "'+NOW()+'";
470         $real['id'] = "";
472         $sql = "insert into patient_data set ";
473         while(list($key, $value) = each($real))
474                 $sql .= $key." = '$value', ";
475         $sql = substr($sql, 0, -2);
478         return sqlInsert($sql);
481 function newEmployerData(       $pid,
482                                 $name = "",
483                                 $street = "",
484                                 $postal_code = "",
485                                 $city = "",
486                                 $state = "",
487                                 $country = ""
488                         )
490         return sqlInsert("insert into employer_data set
491                 name='$name',
492                 street='$street',
493                 postal_code='$postal_code',
494                 city='$city',
495                 state='$state',
496                 country='$country',
497                 pid='$pid',
498                 date=NOW()
499                 ");
502 function updateEmployerData($pid,$new)
504         $real = getEmployerData($pid);
505         while(list($key, $value) = each ($new))
506                 $real[$key] = $value;
507         $real['date'] = "'+NOW()+'";
508         $real['id'] = "";
510         $sql = "insert into employer_data set ";
511         while(list($key, $value) = each($real))
512                 $sql .= $key." = '$value', ";
513         $sql = substr($sql, 0, -2);
516         return sqlInsert($sql);
519 function newInsuranceData(      $pid,
520                                 $type = "",
521                                 $provider = "",
522                                 $policy_number = "",
523                                 $group_number = "",
524                                 $plan_name = "",
525                                 $subscriber_lname = "",
526                                 $subscriber_mname = "",
527                                 $subscriber_fname = "",
528                                 $subscriber_relationship = "",
529                                 $subscriber_ss = "",
530                                 $subscriber_DOB = "",
531                                 $subscriber_street = "",
532                                 $subscriber_postal_code = "",
533                                 $subscriber_city = "",
534                                 $subscriber_state = "",
535                                 $subscriber_country = "",
536                                 $subscriber_phone = "",
537                                 $subscriber_employer = "",
538                                 $subscriber_employer_street = "",
539                                 $subscriber_employer_city = "",
540                                 $subscriber_employer_postal_code = "",
541                                 $subscriber_employer_state = "",
542                                 $subscriber_employer_country = "",
543                                 $copay = "",
544                                 $subscriber_sex = ""
545                         )
547         if (strlen($type) > 0) {
548         $query = "select * from insurance_data where type='" . $type  . "' and pid = " . $pid .  " limit 1";
549         }
550         else {
551                 return FALSE;
552         }
553         $res = sqlQuery ($query);
555         if ($res) {
556                 $data['type'] = $type;
557                 $data['provider'] = $provider;
558                 $data['policy_number']=$policy_number;
559                 $data['group_number']=$group_number;
560                 $data['plan_name']=$plan_name;
561                 $data['subscriber_lname']=$subscriber_lname;
562                 $data['subscriber_mname']=$subscriber_mname;
563                 $data['subscriber_fname']=$subscriber_fname;
564                 $data['subscriber_relationship']=$subscriber_relationship;
565                 $data['subscriber_ss']=$subscriber_ss;
566                 $data['subscriber_DOB']=$subscriber_DOB;
567                 $data['subscriber_street']=$subscriber_street;
568                 $data['subscriber_postal_code']=$subscriber_postal_code;
569                 $data['subscriber_city']=$subscriber_city;
570                 $data['subscriber_state']=$subscriber_state;
571                 $data['subscriber_country']=$subscriber_country;
572                 $data['subscriber_phone']=$subscriber_phone;
573                 $data['subscriber_employer']=$subscriber_employer;
574                 $data['subscriber_employer_city']=$subscriber_employer_city;
575                 $data['subscriber_employer_street']=$subscriber_employer_street;
576                 $data['subscriber_employer_postal_code']=$subscriber_employer_postal_code;
577                 $data['subscriber_employer_state']=$subscriber_employer_state;
578                 $data['subscriber_employer_country']=$subscriber_employer_country;
579                 $data['copay']=$copay;
580                 $data['subscriber_sex']=$subscriber_sex;
581                 $data['pid']=$pid;
582                 $data['date']="NOW()";
583         //      echo "updating<br><br>";
585                 return updateInsuranceData($pid,$data);
586         }
587         else {
588         return sqlInsert("insert into insurance_data set
589                 type='$type',
590                 provider='$provider',
591                 policy_number='$policy_number',
592                 group_number='$group_number',
593                 plan_name='$plan_name',
594                 subscriber_lname='$subscriber_lname',
595                 subscriber_mname='$subscriber_mname',
596                 subscriber_fname='$subscriber_fname',
597                 subscriber_relationship='$subscriber_relationship',
598                 subscriber_ss='$subscriber_ss',
599                 subscriber_DOB='$subscriber_DOB',
600                 subscriber_street='$subscriber_street',
601                 subscriber_postal_code='$subscriber_postal_code',
602                 subscriber_city='$subscriber_city',
603                 subscriber_state='$subscriber_state',
604                 subscriber_country='$subscriber_country',
605                 subscriber_phone='$subscriber_phone',
606                 subscriber_employer = '$subscriber_employer',
607                 subscriber_employer_city='$subscriber_employer_city',
608                 subscriber_employer_street='$subscriber_employer_street',
609                 subscriber_employer_postal_code='$subscriber_employer_postal_code',
610                 subscriber_employer_state='$subscriber_employer_state',
611                 subscriber_employer_country='$subscriber_employer_country',
612                 copay='$copay',
613                 subscriber_sex='$subscriber_sex',
614                 pid='$pid',
615                 date=NOW()
616                 ");
617         }
620 function updateInsuranceData($pid,$new)
622                 $fields = sqlListFields("insurance_data");
624         $real = getInsuranceData($pid);
625         $use = array();
626         while(list($key, $value) = each ($new)) {
627                         if (in_array($key,$fields)){
628                         $use[$key] = $value;
629                         }
630                 }
631         $real['date'] = "'+NOW()+'";
632         $real['id'] = "";
634         $sql = "replace into insurance_data set ";
635         while(list($key, $value) = each($use))
636                 $sql .= $key." = '$value', ";
639         $sql = substr($sql, 0, -2);
641         //echo $sql;
642         //exit;
643         return sqlInsert($sql);
646 /****
647 function newHistoryData(        $pid,
648                                 $coffee = "",
649                                 $tobacco = "",
650                                 $alcohol = "",
651                                 $sleep_patterns = "",
652                                 $exercise_patterns = "",
653                                 $seatbelt_use = "",
654                                 $counseling = "",
655                                 $hazardous_activities = "",
656                                 $last_breast_exam = "",
657                                 $last_mammogram = "",
658                                 $last_gynocological_exam = "",
659                                 $last_rectal_exam = "",
660                                 $last_prostate_exam = "",
661                                 $last_physical_exam = "",
662                                 $last_sigmoidoscopy_colonoscopy = "",
663                                 $history_mother = "",
664                                 $history_father = "",
665                                 $history_siblings = "",
666                                 $history_offspring = "",
667                                 $history_spouse = "",
668                                 $relatives_cancer = "",
669                                 $relatives_tuberculosis = "",
670                                 $relatives_diabetes = "",
671                                 $relatives_high_blood_pressure = "",
672                                 $relatives_heart_problems = "",
673                                 $relatives_stroke = "",
674                                 $relatives_epilepsy = "",
675                                 $relatives_mental_illness = "",
676                                 $relatives_suicide = "",
677                                 $cataract_surgery = "",
678                                 $tonsillectomy = "",
679                                 $appendectomy = "",
680                                 $cholecystestomy = "",
681                                 $heart_surgery = "",
682                                 $hysterectomy = "",
683                                 $hernia_repair = "",
684                                 $hip_replacement = "",
685                                 $knee_replacement = "",
686                                 $name_1 = "",
687                                 $value_1 = "",
688                                 $name_2 = "",
689                                 $value_2 = "",
690                                 $additional_history = "",
691                                 $last_ecg = "",
692                                 $last_cardiac_echo = "",
693                                 $last_exam_results = ""
694                                 )
696         return sqlInsert("insert into history_data set
697                 coffee='$coffee',
698                 tobacco='$tobacco',
699                 alcohol='$alcohol',
700                 sleep_patterns='$sleep_patterns',
701                 exercise_patterns='$exercise_patterns',
702                 seatbelt_use='$seatbelt_use',
703                 counseling='$counseling',
704                 hazardous_activities='$hazardous_activities',
705                 last_breast_exam='$last_breast_exam',
706                 last_mammogram='$last_mammogram',
707                 last_gynocological_exam='$last_gynocological_exam',
708                 last_rectal_exam='$last_rectal_exam',
709                 last_prostate_exam='$last_prostate_exam',
710                 last_physical_exam='$last_physical_exam',
711                 last_sigmoidoscopy_colonoscopy='$last_sigmoidoscopy_colonoscopy',
712                 last_ecg='$last_ecg',
713                 last_cardiac_echo='$last_cardiac_echo',
714                 last_exam_results='$last_exam_results',
715                 history_mother='$history_mother',
716                 history_father='$history_father',
717                 history_siblings='$history_siblings',
718                 history_offspring='$history_offspring',
719                 history_spouse='$history_spouse',
720                 relatives_cancer='$relatives_cancer',
721                 relatives_tuberculosis ='$relatives_tuberculosis',
722                 relatives_diabetes='$relatives_diabetes',
723                 relatives_high_blood_pressure='$relatives_high_blood_pressure',
724                 relatives_heart_problems='$relatives_heart_problems',
725                 relatives_stroke='$relatives_stroke',
726                 relatives_epilepsy='$relatives_epilepsy',
727                 relatives_mental_illness='$relatives_mental_illness',
728                 relatives_suicide='$relatives_suicide',
729                 cataract_surgery='$cataract_surgery',
730                 tonsillectomy='$tonsillectomy',
731                 appendectomy='$appendectomy',
732                 cholecystestomy='$cholecystestomy',
733                 heart_surgery='$heart_surgery',
734                 hysterectomy='$hysterectomy',
735                 hernia_repair='$hernia_repair',
736                 hip_replacement='$hip_replacement',
737                 knee_replacement='$knee_replacement',
738                 name_1 = '$name_1',
739                 value_1 = '$value_1',
740                 name_2 = '$name_2',
741                 value_2 = '$value_2',
742                 additional_history = '$additional_history',
743                 date=NOW(),
744                 pid='$pid'
745                 ");
747 ****/
749 function newHistoryData($pid, $new=false) {
750   $sql = "insert into history_data set pid = '$pid', date = NOW()";
751   if ($new) {
752     while(list($key, $value) = each($new)) {
753       if (!get_magic_quotes_gpc()) $value = addslashes($value);
754       $sql .= ", $key = '$value'";
755     }
756   }
757   return sqlInsert($sql);
760 function updateHistoryData($pid,$new)
762         $real = getHistoryData($pid);
763         while(list($key, $value) = each ($new))
764                 $real[$key] = $value;
765         $real['date'] = "'+NOW()+'";
766         $real['id'] = "";
768         $sql = "insert into history_data set ";
769         while(list($key, $value) = each($real))
770                 $sql .= $key." = '$value', ";
771         $sql = substr($sql, 0, -2);
774         return sqlInsert($sql);
777 function sync_patient($id,$fname,$lname,$street,$city,$postal_code,$state,$phone_home,
778                                 $phone_biz,$email,$pid="")
780         $db = $GLOBALS['adodb']['db'];
781         $customer_info = array();
783         $sql = "SELECT foreign_id,foreign_table FROM integration_mapping where local_table = 'patient_data' and local_id = '" . $id . "'";
784         $result = $db->Execute($sql);
785         if ($result && !$result->EOF) {
786                 $customer_info['foreign_update'] = true;
787                 $customer_info['foreign_id'] = $result->fields['foreign_id'];
788                 $customer_info['foreign_table'] = $result->fields['foreign_table'];
789         }
791         ///xml rpc code to connect to accounting package and add user to it
792         $customer_info['firstname'] = $fname;
793         $customer_info['lastname'] = $lname;
794         $customer_info['address'] = $street;
795         $customer_info['suburb'] = $city;
796         $customer_info['state'] = $state;
797         $customer_info['postcode'] = $postal_code;
799         //ezybiz wants state as a code rather than abbreviation
800         $customer_info['geo_zone_id'] = "";
801         $sql = "SELECT zone_id from geo_zone_reference where zone_code = '" . strtoupper($state) . "'";
802         $db = $GLOBALS['adodb']['db'];
803         $result = $db->Execute($sql);
804         if ($result && !$result->EOF) {
805                 $customer_info['geo_zone_id'] = $result->fields['zone_id'];
806         }
808         //ezybiz wants country as a code rather than abbreviation
809         $customer_info['geo_country_id'] = "";
810         $sql = "SELECT countries_id from geo_country_reference where countries_iso_code_2 = '" . strtoupper($country_code) . "'";
811         $db = $GLOBALS['adodb']['db'];
812         $result = $db->Execute($sql);
813         if ($result && !$result->EOF) {
814                 $customer_info['geo_country_id'] = $result->fields['countries_id'];
815         }
818         $customer_info['phone1'] = $phone_home;
819         $customer_info['phone1comment'] = "Home Phone";
820         $customer_info['phone2'] = $phone_biz;
821         $customer_info['phone2comment'] = "Business Phone";
822         $customer_info['email'] = $email;
823         $customer_info['customernumber'] = $pid;
825         $function['ezybiz.add_customer'] = array(new xmlrpcval($customer_info,"struct"));
826         $ws = new WSWrapper($function);
828         // if the remote patient was added make an entry in the local mapping table to that updates can be made correctly
829         if (is_numeric($ws->value)) {
830                 $sql = "REPLACE INTO integration_mapping set id = '" . $db->GenID("sequences") . "', foreign_id ='" . $ws->value . "', foreign_table ='customer', local_id = '" . $id . "', local_table = 'patient_data' ";
831                 $db->Execute($sql) or die ("error: " . $db->ErrorMsg());
832         }
835 // Returns Date of Birth given YYYYMMDD from MySQL DATE_FORMAT(DOB,'%Y%m%d')
836 function getPatientAge($dobYMD)
838     $tdyYMD=date("Ymd");
839     $yearDiff = substr($tdyYMD,0,4) - substr($dobYMD,0,4);
840     $ageInMonths = ((substr($tdyYMD,0,4)*12)+substr($tdyYMD,4,2)) -
841                    ((substr($dobYMD,0,4)*12)+substr($dobYMD,4,2));
842     $dayDiff = substr($tdyYMD,6,2) - substr($dobYMD,6,2);
843     if ( $dayDiff < 0 ) {
844         $ageInMonths -= 1;
845     }
846     if ( $ageInMonths > 24 ) {
847         $age = intval($ageInMonths/12);
848     }
849     else  {
850         $age = "$ageInMonths month";
851     }
852     return $age;
855 function dateToDB ($date) 
857         $date=substr ($date,6,4)."-".substr ($date,3,2)."-".substr($date, 0,2);
858         return $date;
861 function DBToDate ($date)
863         $date=substr ($date,5,2)."/".substr ($date,8,2)."/".substr($date, 0,4);
864         return $date;