2 include_once("{$GLOBALS['srcdir']}/sql.inc");
\r
3 require_once(dirname(__FILE__) . "/classes/WSWrapper.class.php");
\r
5 // These are for sports team use:
\r
6 $PLAYER_FITNESSES = array(
\r
9 xl('Restricted Training'),
\r
11 xl('Rehabilitation'),
\r
13 xl('International Duty')
\r
15 $PLAYER_FITCOLORS = array('#6677ff', '#00cc00', '#ffff00', '#ff3333', '#ff8800', '#ffeecc', '#ffccaa');
\r
17 function getPatientData($pid, $given = "*, DATE_FORMAT(DOB,'%m/%d/%Y') as DOB_TS") {
\r
18 $sql = "select $given from patient_data where pid='$pid' order by date DESC limit 0,1";
\r
19 return sqlQuery($sql);
\r
22 function getLanguages() {
\r
23 $returnval = array('','english');
\r
24 $sql = "select distinct lower(language) as language from patient_data";
\r
25 $rez = sqlStatement($sql);
\r
26 for($iter=0; $row=sqlFetchArray($rez); $iter++) {
\r
27 if (($row["language"] != "english") && ($row["language"] != "")) {
\r
28 array_push($returnval, $row["language"]);
\r
34 function getInsuranceProviders() {
\r
35 $returnval = array();
\r
38 $sql = "select name, id from insurance_companies order by name, id";
\r
39 $rez = sqlStatement($sql);
\r
40 for($iter=0; $row=sqlFetchArray($rez); $iter++) {
\r
41 $returnval[$row['id']] = $row['name'];
\r
45 // Please leave this here. I have a user who wants to see zip codes and PO
\r
46 // box numbers listed along with the insurance company names, as many companies
\r
47 // have different billing addresses for different plans. -- Rod Roark
\r
50 $sql = "select insurance_companies.name, insurance_companies.id, " .
\r
51 "addresses.zip, addresses.line1 " .
\r
52 "from insurance_companies, addresses " .
\r
53 "where addresses.foreign_id = insurance_companies.id " .
\r
54 "order by insurance_companies.name, addresses.zip";
\r
56 $rez = sqlStatement($sql);
\r
58 for($iter=0; $row=sqlFetchArray($rez); $iter++) {
\r
59 preg_match("/\d+/", $row['line1'], $matches);
\r
60 $returnval[$row['id']] = $row['name'] . " (" . $row['zip'] .
\r
61 "," . $matches[0] . ")";
\r
68 function getProviders() {
\r
69 $returnval = array("");
\r
70 $sql = "select fname, lname from users where authorized = 1 and " .
\r
71 "active = 1 and username != ''";
\r
72 $rez = sqlStatement($sql);
\r
73 for($iter=0; $row=sqlFetchArray($rez); $iter++) {
\r
74 if (($row["fname"] != "") && ($row["lname"] != "")) {
\r
75 array_push($returnval, $row["fname"] . " " . $row["lname"]);
\r
84 returns all facilities or just the id for the first one
\r
85 (FACILITY FILTERING (lemonsoftware))
\r
87 @param string - if 'first' return first facility ordered by id
\r
88 @return array | int for 'first' case
\r
90 function getFacilities($first = '') {
\r
91 $r = sqlStatement("SELECT * FROM facility ORDER BY id");
\r
93 while ( $row = sqlFetchArray($r) ) {
\r
97 if ( $first == 'first') {
\r
98 return $ret[0]['id'];
\r
105 GET SERVICE FACILITIES
\r
107 returns all service_location facilities or just the id for the first one
\r
108 (FACILITY FILTERING (CHEMED))
\r
110 @param string - if 'first' return first facility ordered by id
\r
111 @return array | int for 'first' case
\r
113 function getServiceFacilities($first = '') {
\r
114 $r = sqlStatement("SELECT * FROM facility WHERE service_location != 0 ORDER BY id");
\r
116 while ( $row = sqlFetchArray($r) ) {
\r
120 if ( $first == 'first') {
\r
121 return $ret[0]['id'];
\r
127 //(CHEMED) facility filter
\r
128 function getProviderInfo($providerID = "%", $providers_only = true, $facility = '' ) {
\r
130 if ($providers_only) {
\r
131 $param1 = " AND authorized=1 ";
\r
134 //--------------------------------
\r
135 //(CHEMED) facility filter
\r
138 $param2 = " AND facility_id = $facility ";
\r
140 //--------------------------------
\r
143 if ($providerID == "%") {
\r
146 $query = "select distinct id, username, lname, fname, authorized, info, facility " .
\r
147 "from users where username != '' and active = 1 and id $command '" .
\r
148 mysql_real_escape_string($providerID) . "' " . $param1 . $param2;
\r
149 $rez = sqlStatement($query);
\r
150 for($iter=0; $row=sqlFetchArray($rez); $iter++)
\r
151 $returnval[$iter]=$row;
\r
153 //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
\r
154 //accessible from $resultval['key']
\r
157 $akeys = array_keys($returnval[0]);
\r
158 foreach($akeys as $key) {
\r
160 $returnval[0][$key] = $returnval[0][$key];
\r
166 //same as above but does not reduce if only 1 row returned
\r
167 function getCalendarProviderInfo($providerID = "%", $providers_only = true) {
\r
169 if ($providers_only) {
\r
170 $param1 = "AND authorized=1";
\r
173 if ($providerID == "%") {
\r
176 $query = "select distinct id, username, lname, fname, authorized, info, facility " .
\r
177 "from users where active = 1 and username != '' and id $command '" .
\r
178 mysql_real_escape_string($providerID) . "' " . $param1;
\r
180 $rez = sqlStatement($query);
\r
181 for($iter=0; $row=sqlFetchArray($rez); $iter++)
\r
182 $returnval[$iter]=$row;
\r
187 function getProviderName($providerID) {
\r
188 $pi = getProviderInfo($providerID);
\r
189 if (strlen($pi[0]["lname"]) > 0) {
\r
190 return $pi[0]['fname'] . " " . $pi[0]['lname'];
\r
195 function getProviderId($providerName) {
\r
196 $query = "select id from users where username = '". mysql_real_escape_string($providerName)."'";
\r
197 $rez = sqlStatement($query);
\r
198 for($iter=0; $row=sqlFetchArray($rez); $iter++)
\r
199 $returnval[$iter]=$row;
\r
203 function getEthnoRacials() {
\r
204 $returnval = array("");
\r
205 $sql = "select distinct lower(ethnoracial) as ethnoracial from patient_data";
\r
206 $rez = sqlStatement($sql);
\r
207 for($iter=0; $row=sqlFetchArray($rez); $iter++) {
\r
208 if (($row["ethnoracial"] != "")) {
\r
209 array_push($returnval, $row["ethnoracial"]);
\r
215 function getHistoryData($pid, $given = "*")
\r
217 $sql = "select $given from history_data where pid='$pid' order by date DESC limit 0,1";
\r
218 return sqlQuery($sql);
\r
221 // function getInsuranceData($pid, $type = "primary", $given = "insd.*, DATE_FORMAT(subscriber_DOB,'%m/%d/%Y') as subscriber_DOB, ic.name as provider_name")
\r
222 function getInsuranceData($pid, $type = "primary", $given = "insd.*, ic.name as provider_name")
\r
224 $sql = "select $given from insurance_data as insd " .
\r
225 "left join insurance_companies as ic on ic.id = insd.provider " .
\r
226 "where pid = '$pid' and type = '$type' order by date DESC limit 1";
\r
227 return sqlQuery($sql);
\r
230 function getInsuranceDataByDate($pid, $date, $type,
\r
231 $given = "insd.*, DATE_FORMAT(subscriber_DOB,'%m/%d/%Y') as subscriber_DOB, ic.name as provider_name")
\r
232 { // this must take the date in the following manner: YYYY-MM-DD
\r
233 // this function recalls the insurance value that was most recently enterred from the
\r
234 // given date. it will call up most recent records up to and on the date given,
\r
235 // but not records enterred after the given date
\r
236 $sql = "select $given from insurance_data as insd " .
\r
237 "left join insurance_companies as ic on ic.id = provider " .
\r
238 "where pid = '$pid' and date_format(date,'%Y-%m-%d') <= '$date' and " .
\r
239 "type='$type' order by date DESC limit 1";
\r
240 return sqlQuery($sql);
\r
243 function getEmployerData($pid, $given = "*")
\r
245 $sql = "select $given from employer_data where pid='$pid' order by date DESC limit 0,1";
\r
246 return sqlQuery($sql);
\r
249 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")
\r
251 // Allow the last name to be followed by a comma and some part of a first name.
\r
252 // New behavior for searches:
\r
253 // Allows comma alone followed by some part of a first name
\r
254 // If the first letter of either name is capital, searches for name starting
\r
255 // with given substring (the expected behavior). If it is lower case, it
\r
256 // it searches for the substring anywhere in the name. This applies to either
\r
257 // last name or first name or both. The arbitrary limit of 100 results is set
\r
258 // in the sql query below. --Mark Leeds
\r
259 $lname = trim($lname);
\r
261 if (preg_match('/^(.*),(.*)/', $lname, $matches)) {
\r
262 $lname = trim($matches[1]);
\r
263 $fname = trim($matches[2]);
\r
265 $search_for_pieces1 = '';
\r
266 $search_for_pieces2 = '';
\r
267 if ($lname{0} != strtoupper($lname{0})) {$search_for_pieces1 = '%';}
\r
268 if ($fname{0} != strtoupper($fname{0})) {$search_for_pieces2 = '%';}
\r
269 $sql="select $given from patient_data where lname like '"
\r
270 .$search_for_pieces1."$lname%' "
\r
271 ."and fname like '"
\r
272 .$search_for_pieces2."$fname%' "
\r
273 ."order by $orderby limit 100";
\r
275 if ($limit != "all")
\r
276 $sql .= " limit $start, $limit";
\r
277 $rez = sqlStatement($sql);
\r
279 for($iter=0; $row=sqlFetchArray($rez); $iter++)
\r
280 $returnval[$iter]=$row;
\r
285 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")
\r
287 $sql = "select $given from patient_data where pubpid like '$pid%' " .
\r
288 "order by $orderby";
\r
290 if ($limit != "all")
\r
291 $sql .= " limit $start, $limit";
\r
292 $rez = sqlStatement($sql);
\r
293 for($iter=0; $row=sqlFetchArray($rez); $iter++)
\r
294 $returnval[$iter]=$row;
\r
299 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")
\r
304 elseif (empty($pid))
\r
307 if (strstr($pid,"%"))
\r
310 $sql="select $given from patient_data where pid $command '$pid' order by $orderby";
\r
312 if ($limit != "all")
\r
313 $sql .= " limit $start, $limit";
\r
315 $rez = sqlStatement($sql);
\r
316 for($iter=0; $row=sqlFetchArray($rez); $iter++)
\r
317 $returnval[$iter]=$row;
\r
322 function getPatientName($pid) {
\r
325 $patientData = getPatientPID($pid);
\r
326 if (empty($patientData[0]['lname']))
\r
328 $patientName = $patientData[0]['lname'] . ", " . $patientData[0]['fname'];
\r
329 return $patientName;
\r
332 function getPatientDOB($DOB = "%", $given = "pid, id, lname, fname, mname", $orderby = "lname ASC, fname ASC", $limit="all", $start="0")
\r
334 $DOB = fixDate($DOB, $DOB);
\r
336 $sql="select $given from patient_data where DOB like '$DOB%' " .
\r
337 "order by $orderby";
\r
339 if ($limit != "all")
\r
340 $sql .= " limit $start, $limit";
\r
342 $rez = sqlStatement($sql);
\r
343 for($iter=0; $row=sqlFetchArray($rez); $iter++)
\r
344 $returnval[$iter]=$row;
\r
349 function getPatientSSN($ss = "%", $given = "pid, id, lname, fname, mname, providerID", $orderby = "lname ASC, fname ASC", $limit="all", $start="0")
\r
351 $sql="select $given from patient_data where ss like '$ss%' " .
\r
352 "order by $orderby";
\r
354 if ($limit != "all")
\r
355 $sql .= " limit $start, $limit";
\r
357 $rez = sqlStatement($sql);
\r
358 for($iter=0; $row=sqlFetchArray($rez); $iter++)
\r
359 $returnval[$iter]=$row;
\r
364 //(CHEMED) Search by phone number
\r
365 function getPatientPhone($phone = "%", $given = "pid, id, lname, fname, mname, providerID", $orderby = "lname ASC, fname ASC", $limit="all", $start="0")
\r
367 $phone = ereg_replace( "[[:punct:]]","", $phone );
\r
368 $sql="select $given from patient_data where REPLACE(REPLACE(phone_home, '-', ''), ' ', '') REGEXP '$phone' " .
\r
369 "order by $orderby";
\r
371 if ($limit != "all")
\r
372 $sql .= " limit $start, $limit";
\r
374 $rez = sqlStatement($sql);
\r
375 for($iter=0; $row=sqlFetchArray($rez); $iter++)
\r
376 $returnval[$iter]=$row;
\r
381 function getPatientIds($given = "pid, id, lname, fname, mname", $orderby = "id ASC", $limit="all", $start="0")
\r
383 $sql="select $given from patient_data order by $orderby";
\r
385 if ($limit != "all")
\r
386 $sql .= " limit $start, $limit";
\r
388 $rez = sqlStatement($sql);
\r
389 for($iter=0; $row=sqlFetchArray($rez); $iter++)
\r
390 $returnval[$iter]=$row;
\r
395 //----------------------input functions
\r
396 function newPatientData( $db_id="",
\r
407 $country_code = "",
\r
412 $phone_contact = "",
\r
414 $contact_relationship = "",
\r
420 $interpretter = "",
\r
421 $migrantseasonal = "",
\r
423 $monthly_income = "",
\r
425 $financial_review = "",
\r
427 $pid = "MAX(pid)+1",
\r
429 $genericname1 = "",
\r
431 $genericname2 = "",
\r
438 $drivers_license = "",
\r
439 $hipaa_notice = "",
\r
440 $hipaa_message = ""
\r
443 $DOB = fixDate($DOB);
\r
446 $referral_source = '';
\r
448 $rez = sqlQuery("select id, fitness, referral_source from patient_data where pid = $pid");
\r
449 // Check for brain damage:
\r
450 if ($db_id != $rez['id']) {
\r
451 $errmsg = "Internal error: Attempt to change patient_data.id from '" .
\r
452 $rez['id'] . "' to '$db_id' for pid '$pid'";
\r
455 $fitness = $rez['fitness'];
\r
456 $referral_source = $rez['referral_source'];
\r
459 $query = ("replace into patient_data set
\r
468 postal_code='$postal_code',
\r
471 country_code='$country_code',
\r
472 drivers_license='$drivers_license',
\r
474 occupation='$occupation',
\r
475 phone_home='$phone_home',
\r
476 phone_biz='$phone_biz',
\r
477 phone_contact='$phone_contact',
\r
479 contact_relationship='$contact_relationship',
\r
480 referrer='$referrer',
\r
481 referrerID='$referrerID',
\r
483 language='$language',
\r
484 ethnoracial='$ethnoracial',
\r
485 interpretter='$interpretter',
\r
486 migrantseasonal='$migrantseasonal',
\r
487 family_size='$family_size',
\r
488 monthly_income='$monthly_income',
\r
489 homeless='$homeless',
\r
490 financial_review='$financial_review',
\r
493 providerID = '$providerID',
\r
494 genericname1 = '$genericname1',
\r
495 genericval1 = '$genericval1',
\r
496 genericname2 = '$genericname2',
\r
497 genericval2 = '$genericval2',
\r
498 phone_cell = '$phone_cell',
\r
499 pharmacy_id = '$pharmacy_id',
\r
500 hipaa_mail = '$hipaa_mail',
\r
501 hipaa_voice = '$hipaa_voice',
\r
502 hipaa_notice = '$hipaa_notice',
\r
503 hipaa_message = '$hipaa_message',
\r
505 fitness='$fitness',
\r
506 referral_source='$referral_source',
\r
510 $id = sqlInsert($query);
\r
511 $foo = sqlQuery("select pid from patient_data where id='$id' order by date limit 0,1");
\r
513 sync_patient($id,$fname,$lname,$street,$city,$postal_code,$state,$phone_home,
\r
514 $phone_biz,$phone_cell,$email,$pid);
\r
516 return $foo['pid'];
\r
519 // Supported input date formats are:
\r
521 // mm/dd/yy (assumes 20yy for yy < 10, else 19yy)
\r
523 // also mm-dd-yyyy, etc. and mm.dd.yyyy, etc.
\r
525 function fixDate($date, $default="0000-00-00") {
\r
526 $fixed_date = $default;
\r
527 $date = trim($date);
\r
528 if (preg_match("'^[0-9]{1,4}[/.-][0-9]{1,2}[/.-][0-9]{1,4}$'", $date)) {
\r
529 $dmy = preg_split("'[/.-]'", $date);
\r
530 if ($dmy[0] > 99) {
\r
531 $fixed_date = sprintf("%04u-%02u-%02u", $dmy[0], $dmy[1], $dmy[2]);
\r
533 if ($dmy[0] != 0 || $dmy[1] != 0 || $dmy[2] != 0) {
\r
534 if ($dmy[2] < 1000) $dmy[2] += 1900;
\r
535 if ($dmy[2] < 1910) $dmy[2] += 100;
\r
537 $fixed_date = sprintf("%04u-%02u-%02u", $dmy[2], $dmy[0], $dmy[1]);
\r
541 return $fixed_date;
\r
544 function updatePatientData($pid,$new)
\r
546 /*******************************************************************
\r
547 $real = getPatientData($pid);
\r
548 $new['DOB'] = fixDate($new['DOB']);
\r
549 while(list($key, $value) = each ($new))
\r
550 $real[$key] = $value;
\r
551 $real['date'] = "'+NOW()+'";
\r
553 $sql = "insert into patient_data set ";
\r
554 while(list($key, $value) = each($real))
\r
555 $sql .= $key." = '$value', ";
\r
556 $sql = substr($sql, 0, -2);
\r
557 return sqlInsert($sql);
\r
558 *******************************************************************/
\r
560 // The above was broken, though seems intent to insert a new patient_data
\r
561 // row for each update. A good idea, but nothing is doing that yet so
\r
562 // the code below does not yet attempt it.
\r
564 $new['DOB'] = fixDate($new['DOB']);
\r
565 $db_id = $new['id'];
\r
567 $rez = sqlQuery("SELECT * FROM patient_data WHERE id = '$db_id'");
\r
568 // Check for brain damage:
\r
569 if ($pid != $rez['pid']) {
\r
570 $errmsg = "Internal error: Attempt to change patient data with pid = '" .
\r
571 $rez['pid'] . "' when current pid is '$pid' for id '$db_id'";
\r
574 $sql = "UPDATE patient_data SET date = NOW()";
\r
575 foreach ($new as $key => $value) {
\r
576 $sql .= ", $key = '$value'";
\r
578 $sql .= " WHERE id = '$db_id'";
\r
579 $id = sqlInsert($sql);
\r
581 sync_patient($db_id,$rez['fname'],$rez['lname'],$rez['street'],$rez['city'],
\r
582 $rez['postal_code'],$rez['state'],$rez['phone_home'],$rez['phone_biz'],
\r
583 $rez['phone_cell'],$rez['email'],$rez['pid']);
\r
588 function newEmployerData( $pid,
\r
597 return sqlInsert("insert into employer_data set
\r
600 postal_code='$postal_code',
\r
603 country='$country',
\r
609 function updateEmployerData($pid, $new)
\r
611 $old = getEmployerData($pid);
\r
614 foreach (array('name','street','city','state','postal_code','country') as $key) {
\r
615 $value = empty($old[$key]) ? '' : addslashes($old[$key]);
\r
616 if (isset($new[$key]) && strcmp($new[$key], $value) != 0) {
\r
617 $value = $new[$key];
\r
620 $set .= "$key = '$value', ";
\r
623 $set .= "pid = '$pid', date = NOW()";
\r
624 return sqlInsert("INSERT INTO employer_data SET $set");
\r
629 // This updates or adds the given insurance data info, while retaining any
\r
630 // previously added insurance_data rows that should be preserved.
\r
631 // This does not directly support the maintenance of non-current insurance.
\r
633 function newInsuranceData(
\r
637 $policy_number = "",
\r
638 $group_number = "",
\r
640 $subscriber_lname = "",
\r
641 $subscriber_mname = "",
\r
642 $subscriber_fname = "",
\r
643 $subscriber_relationship = "",
\r
644 $subscriber_ss = "",
\r
645 $subscriber_DOB = "",
\r
646 $subscriber_street = "",
\r
647 $subscriber_postal_code = "",
\r
648 $subscriber_city = "",
\r
649 $subscriber_state = "",
\r
650 $subscriber_country = "",
\r
651 $subscriber_phone = "",
\r
652 $subscriber_employer = "",
\r
653 $subscriber_employer_street = "",
\r
654 $subscriber_employer_city = "",
\r
655 $subscriber_employer_postal_code = "",
\r
656 $subscriber_employer_state = "",
\r
657 $subscriber_employer_country = "",
\r
659 $subscriber_sex = "",
\r
660 $effective_date = "0000-00-00")
\r
662 if (strlen($type) <= 0) return FALSE;
\r
664 // If a bad date was passed, err on the side of caution.
\r
665 $effective_date = fixDate($effective_date, date('Y-m-d'));
\r
667 $idres = sqlStatement("SELECT * FROM insurance_data WHERE " .
\r
668 "pid = '$pid' AND type = '$type' ORDER BY date DESC");
\r
669 $idrow = sqlFetchArray($idres);
\r
671 // Replace the most recent entry in any of the following cases:
\r
672 // * Its effective date is >= this effective date.
\r
673 // * It is the first entry and it has no (insurance) provider.
\r
674 // * There is no encounter that is earlier than the new effective date but
\r
675 // on or after the old effective date.
\r
676 // Otherwise insert a new entry.
\r
680 if (strcmp($idrow['date'], $effective_date) > 0) {
\r
684 if (!$idrow['provider'] && !sqlFetchArray($idres)) {
\r
688 $ferow = sqlQuery("SELECT count(*) AS count FROM form_encounter " .
\r
689 "WHERE pid = '$pid' AND date < '$effective_date 00:00:00' AND " .
\r
690 "date >= '" . $idrow['date'] . " 00:00:00'");
\r
691 if ($ferow['count'] == 0) $replace = true;
\r
698 // TBD: This is a bit dangerous in that a typo in entering the effective
\r
699 // date can wipe out previous insurance history. So we want some data
\r
700 // entry validation somewhere.
\r
701 sqlStatement("DELETE FROM insurance_data WHERE " .
\r
702 "pid = '$pid' AND type = '$type' AND date >= '$effective_date' AND " .
\r
703 "id != " . $idrow['id']);
\r
706 $data['type'] = $type;
\r
707 $data['provider'] = $provider;
\r
708 $data['policy_number'] = $policy_number;
\r
709 $data['group_number'] = $group_number;
\r
710 $data['plan_name'] = $plan_name;
\r
711 $data['subscriber_lname'] = $subscriber_lname;
\r
712 $data['subscriber_mname'] = $subscriber_mname;
\r
713 $data['subscriber_fname'] = $subscriber_fname;
\r
714 $data['subscriber_relationship'] = $subscriber_relationship;
\r
715 $data['subscriber_ss'] = $subscriber_ss;
\r
716 $data['subscriber_DOB'] = $subscriber_DOB;
\r
717 $data['subscriber_street'] = $subscriber_street;
\r
718 $data['subscriber_postal_code'] = $subscriber_postal_code;
\r
719 $data['subscriber_city'] = $subscriber_city;
\r
720 $data['subscriber_state'] = $subscriber_state;
\r
721 $data['subscriber_country'] = $subscriber_country;
\r
722 $data['subscriber_phone'] = $subscriber_phone;
\r
723 $data['subscriber_employer'] = $subscriber_employer;
\r
724 $data['subscriber_employer_city'] = $subscriber_employer_city;
\r
725 $data['subscriber_employer_street'] = $subscriber_employer_street;
\r
726 $data['subscriber_employer_postal_code'] = $subscriber_employer_postal_code;
\r
727 $data['subscriber_employer_state'] = $subscriber_employer_state;
\r
728 $data['subscriber_employer_country'] = $subscriber_employer_country;
\r
729 $data['copay'] = $copay;
\r
730 $data['subscriber_sex'] = $subscriber_sex;
\r
731 $data['pid'] = $pid;
\r
732 $data['date'] = $effective_date;
\r
733 updateInsuranceData($idrow['id'], $data);
\r
734 return $idrow['id'];
\r
737 return sqlInsert("INSERT INTO insurance_data SET
\r
739 provider = '$provider',
\r
740 policy_number = '$policy_number',
\r
741 group_number = '$group_number',
\r
742 plan_name = '$plan_name',
\r
743 subscriber_lname = '$subscriber_lname',
\r
744 subscriber_mname = '$subscriber_mname',
\r
745 subscriber_fname = '$subscriber_fname',
\r
746 subscriber_relationship = '$subscriber_relationship',
\r
747 subscriber_ss = '$subscriber_ss',
\r
748 subscriber_DOB = '$subscriber_DOB',
\r
749 subscriber_street = '$subscriber_street',
\r
750 subscriber_postal_code = '$subscriber_postal_code',
\r
751 subscriber_city = '$subscriber_city',
\r
752 subscriber_state = '$subscriber_state',
\r
753 subscriber_country = '$subscriber_country',
\r
754 subscriber_phone = '$subscriber_phone',
\r
755 subscriber_employer = '$subscriber_employer',
\r
756 subscriber_employer_city = '$subscriber_employer_city',
\r
757 subscriber_employer_street = '$subscriber_employer_street',
\r
758 subscriber_employer_postal_code = '$subscriber_employer_postal_code',
\r
759 subscriber_employer_state = '$subscriber_employer_state',
\r
760 subscriber_employer_country = '$subscriber_employer_country',
\r
762 subscriber_sex = '$subscriber_sex',
\r
764 date = '$effective_date'
\r
769 // This is used internally only.
\r
770 function updateInsuranceData($id, $new)
\r
772 $fields = sqlListFields("insurance_data");
\r
775 while(list($key, $value) = each ($new)) {
\r
776 if (in_array($key, $fields)) {
\r
777 $use[$key] = $value;
\r
781 $sql = "UPDATE insurance_data SET ";
\r
782 while(list($key, $value) = each($use))
\r
783 $sql .= $key . " = '$value', ";
\r
784 $sql = substr($sql, 0, -2) . " WHERE id = '$id'";
\r
786 sqlStatement($sql);
\r
789 function newHistoryData($pid, $new=false) {
\r
790 $sql = "insert into history_data set pid = '$pid', date = NOW()";
\r
792 while(list($key, $value) = each($new)) {
\r
793 if (!get_magic_quotes_gpc()) $value = addslashes($value);
\r
794 $sql .= ", $key = '$value'";
\r
797 return sqlInsert($sql);
\r
800 function updateHistoryData($pid,$new)
\r
802 $real = getHistoryData($pid);
\r
803 while(list($key, $value) = each ($new))
\r
804 $real[$key] = $value;
\r
805 $real['date'] = "'+NOW()+'";
\r
808 $sql = "insert into history_data set ";
\r
809 while(list($key, $value) = each($real))
\r
810 $sql .= $key." = '$value', ";
\r
811 $sql = substr($sql, 0, -2);
\r
814 return sqlInsert($sql);
\r
817 function sync_patient($id,$fname,$lname,$street,$city,$postal_code,$state,$phone_home,
\r
818 $phone_biz,$phone_cell,$email,$pid="")
\r
820 $db = $GLOBALS['adodb']['db'];
\r
821 $customer_info = array();
\r
823 $sql = "SELECT foreign_id,foreign_table FROM integration_mapping where local_table = 'patient_data' and local_id = '" . $id . "'";
\r
824 $result = $db->Execute($sql);
\r
825 if ($result && !$result->EOF) {
\r
826 $customer_info['foreign_update'] = true;
\r
827 $customer_info['foreign_id'] = $result->fields['foreign_id'];
\r
828 $customer_info['foreign_table'] = $result->fields['foreign_table'];
\r
831 ///xml rpc code to connect to accounting package and add user to it
\r
832 $customer_info['firstname'] = $fname;
\r
833 $customer_info['lastname'] = $lname;
\r
834 $customer_info['address'] = $street;
\r
835 $customer_info['suburb'] = $city;
\r
836 $customer_info['state'] = $state;
\r
837 $customer_info['postcode'] = $postal_code;
\r
839 //ezybiz wants state as a code rather than abbreviation
\r
840 $customer_info['geo_zone_id'] = "";
\r
841 $sql = "SELECT zone_id from geo_zone_reference where zone_code = '" . strtoupper($state) . "'";
\r
842 $db = $GLOBALS['adodb']['db'];
\r
843 $result = $db->Execute($sql);
\r
844 if ($result && !$result->EOF) {
\r
845 $customer_info['geo_zone_id'] = $result->fields['zone_id'];
\r
848 //ezybiz wants country as a code rather than abbreviation
\r
849 $customer_info['geo_country_id'] = "";
\r
850 $sql = "SELECT countries_id from geo_country_reference where countries_iso_code_2 = '" . strtoupper($country_code) . "'";
\r
851 $db = $GLOBALS['adodb']['db'];
\r
852 $result = $db->Execute($sql);
\r
853 if ($result && !$result->EOF) {
\r
854 $customer_info['geo_country_id'] = $result->fields['countries_id'];
\r
857 $customer_info['phone1'] = $phone_home;
\r
858 $customer_info['phone1comment'] = "Home Phone";
\r
859 $customer_info['phone2'] = $phone_biz;
\r
860 $customer_info['phone2comment'] = "Business Phone";
\r
861 $customer_info['phone3'] = $phone_cell;
\r
862 $customer_info['phone3comment'] = "Cell Phone";
\r
863 $customer_info['email'] = $email;
\r
864 $customer_info['customernumber'] = $pid;
\r
866 $function['ezybiz.add_customer'] = array(new xmlrpcval($customer_info,"struct"));
\r
867 $ws = new WSWrapper($function);
\r
869 // if the remote patient was added make an entry in the local mapping table to that updates can be made correctly
\r
870 if (is_numeric($ws->value)) {
\r
871 $sql = "REPLACE INTO integration_mapping set id = '" . $db->GenID("sequences") . "', foreign_id ='" . $ws->value . "', foreign_table ='customer', local_id = '" . $id . "', local_table = 'patient_data' ";
\r
872 $db->Execute($sql) or die ("error: " . $db->ErrorMsg());
\r
876 // Returns Date of Birth given YYYYMMDD from MySQL DATE_FORMAT(DOB,'%Y%m%d')
\r
877 function getPatientAge($dobYMD)
\r
879 $tdyYMD=date("Ymd");
\r
880 $yearDiff = substr($tdyYMD,0,4) - substr($dobYMD,0,4);
\r
881 $ageInMonths = ((substr($tdyYMD,0,4)*12)+substr($tdyYMD,4,2)) -
\r
882 ((substr($dobYMD,0,4)*12)+substr($dobYMD,4,2));
\r
883 $dayDiff = substr($tdyYMD,6,2) - substr($dobYMD,6,2);
\r
884 if ( $dayDiff < 0 ) {
\r
887 if ( $ageInMonths > 24 ) {
\r
888 $age = intval($ageInMonths/12);
\r
891 $age = "$ageInMonths month";
\r
896 function dateToDB ($date)
\r
898 $date=substr ($date,6,4)."-".substr ($date,3,2)."-".substr($date, 0,2);
\r
902 function DBToDate ($date)
\r
904 $date=substr ($date,5,2)."/".substr ($date,8,2)."/".substr($date, 0,4);
\r