3 * Class ImportPharmacies
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
;
15 use OpenEMR\Common\Http\oeHttp
;
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
34 public function importPharmacies($city, $state)
36 $address = new Address();
40 'enumeration_type' => '',
41 'taxonomy_description' => 'pharmacy',
44 'organization_name' => '',
45 'address_purpose' => '',
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);
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) {
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();
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']);
100 private function findNcpdp($identifiers)
102 foreach ($identifiers as $identifier => $value) {
103 if ($value['desc'] == 'Other') {
104 return $value['identifier'];
111 * Look to see if the pharmacy is in the database already.
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) {