New feature to import pharmacies for any city and state. (#2680)
[openemr.git] / src / Pharmacy / Services / ImportPharmacies.php
blob071edb313fab18b26d5f0a1416bdfcc07c8d6924
1 <?php
2 /**
3 * Class ImportPharmacies
4 * @package OpenEMR
5 * @link http://www.open-emr.org
6 * @author Sherwin Gaddis <sherwingaddis@gmail.com>
7 * @copyright Copyright (c) 2019 Sherwin Gaddis <sherwingaddis@gmail.com>
8 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
12 namespace OpenEMR\Pharmacy\Services;
14 use Address;
15 use OpenEMR\Common\Http\oeHttp;
16 use Pharmacy;
19 /**
20 * @package Import
21 * This class extends the Pharmacy class to import pharmacies listed with CMS.
22 * It can be adapted to work in other countries if a similar API is available.
23 * There is a duplication check using the NPI number. If the NPI number exist in the table. The entry is skipped.
24 * However, if the pharmacy gets a new NPI number, there could be two entries with the same address and different
25 * NPI numbers. I have discovered that some times erroneous entries can be retrieved.
27 class ImportPharmacies
29 /**
30 * @param $city
31 * @param $state
32 * @return string
34 public function importPharmacies($city, $state)
36 $address = new Address();
38 $query = [
39 'number' => '',
40 'enumeration_type' => '',
41 'taxonomy_description' => 'pharmacy',
42 'first_name' => '',
43 'last_name' => '',
44 'organization_name' => '',
45 'address_purpose' => '',
46 'city' => $city,
47 'state' => $state,
48 'postal_code' => '',
49 'country_code' => '',
50 'limit' => '100',
51 'skip' => '',
52 'version' => '2.1',
54 $response = oeHttp::get('https://npiregistry.cms.hhs.gov/api/', $query);
56 $body = $response->body(); // already should be json.
58 $pharmacyObj = json_decode($body, true, 512, 0);
59 $i=0;
60 foreach ($pharmacyObj as $obj => $value) {
61 foreach ($value as $key => $show) {
62 /*********************Skip duplicates*******************/
63 $npi = $show['number'];
64 if (self::entryCheck($npi) === true) {
65 continue;
67 /*************Check Zip Code Length**********************/
68 $zipCode = $show['addresses'][0]['postal_code'];
69 if (strlen($zipCode) > 5) {
70 $zip = substr($zipCode, 0, -4);
72 /******************************************************/
73 $identifiers = $show['identifiers'];
74 $ncpdp = self::findNcpdp($identifiers);
76 $pharmacy = new Pharmacy();
77 $pharmacy->set_id();
78 $pharmacy->set_name($show['basic']['name']);
79 $pharmacy->set_ncpdp($ncpdp);
80 $pharmacy->set_npi($show['number']);
81 $pharmacy->set_address_line1($show['addresses'][0]['address_1']);
82 $pharmacy->set_city($show['addresses'][0]['city']);
83 $pharmacy->set_state($show['addresses'][0]['state']);
84 $pharmacy->set_zip($zip);
85 $pharmacy->set_fax($show['addresses'][0]['fax_number']);
86 $pharmacy->set_phone($show['addresses'][0]['telephone_number']);
87 $pharmacy->persist();
88 ++$i;
92 $response = $i;
93 return $response;
96 /**
97 * @param $identifiers
98 * @return mixed
100 private function findNcpdp($identifiers)
102 foreach ($identifiers as $identifier => $value) {
103 if ($value['desc'] == 'Other') {
104 return $value['identifier'];
107 return null;
111 * Look to see if the pharmacy is in the database already.
112 * @param $npi
113 * @return bool
116 private function entryCheck($npi)
118 $sql = "SELECT count(*) AS num FROM pharmacies WHERE npi = ?";
119 $query = sqlQuery($sql, [$npi]);
120 if ($query['num'] > 0) {
121 return true;
122 } else {
123 return false;