dump db version
[openemr.git] / interface / weno / import_pharmacies.php
blob9ab44acb4c2644f1b88373eb6469c147105126dd
1 <?php
2 /**
3 * weno rx pharmacy import.
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Sherwin Gaddis <sherwingaddis@gmail.com>
8 * @author Alfonzo Perez <aperez@hitechcompliance.net>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2016-2017 Sherwin Gaddis <sherwingaddis@gmail.com>
11 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 require_once('../globals.php');
18 if (!verifyCsrfToken($_GET["csrf_token_form"])) {
19 csrfNotVerified();
22 $state = filter_input(INPUT_POST, "form_state"); //stores the variable sent in the post
23 $srchCity = filter_input(INPUT_POST, "form_city");
24 $ref = $_SERVER["HTTP_REFERER"]; //stores the url the post came from to redirect back to
27 * Opens the CSV file and reads each line
29 $path = '../../contrib/weno/pharmacyList.csv';
30 $entrys = new SplFileObject($path);
31 $entrys->setFlags(SplFileObject::READ_CSV);
33 sqlStatementNoLog("SET autocommit=0");
34 sqlStatementNoLog("START TRANSACTION"); // Just in case someone else is adding.
36 $tm = 1; // Let's count how many.
37 foreach ($entrys as $entry) {//This loop continues till the end of the last line is reached.
39 //check entry 7 to match state
40 if (strtoupper($entry[7]) == strtoupper($state) && strtoupper($entry[6]) == strtoupper($srchCity)) { //In the next iteration this needs to be gotten from the globals
43 * check the name is in the table
44 * if it is skip to the next name on the list
46 $sql = "SELECT id FROM pharmacies WHERE name = ? And npi = ?";
47 $getNameId = sqlQuery($sql, array($entry[3], $entry[2]));
49 if (empty($getNameId)) {
50 $phone = str_replace(" ", "-", $entry[10]); //reformat the phone numbers and fax number
51 $fax = str_replace(" ", "-", $entry[11]);
52 if (strlen($phone) == 10) { // Not Formatted
53 $phone = preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "$1-$2-$3", $phone);
55 if (strlen($fax) == 10) {
56 $fax = preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "$1-$2-$3", $fax);
59 $sql = "SELECT MAX(id) as id FROM pharmacies"; // Find last record in the table
60 $getMaxId = sqlQuery($sql); //load to variable
61 $id = $getMaxId['id'] + 1; // set start import ID to max id plus 1
62 $sql = "INSERT INTO pharmacies (id, name, transmit_method, email, ncpdp, npi) VALUES (?,?,?,?,?,?)";
63 $newInsert = array($id, $entry[3], 1, null, $entry[1], $entry[2]);
64 sqlStatement($sql, $newInsert);
66 // Add Address
67 $sql = "SELECT MAX(id) as id FROM addresses"; // Let's do this for case others insert addresses besides pharmacies.
68 $aid = sqlQuery($sql);
69 $aid = $aid['id'] + 1; // ++ with arrays can be troublesome..
70 //Insert Address into address table
71 $fid = $id; // Set the foreign_id to the id in the pharmacies table.
72 $asql = "INSERT INTO addresses (`id`, `line1`, `line2`, `city`, `state`, `zip`, `plus_four`, `country`, `foreign_id`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
73 $addressInsert = array($aid, $entry[4], $entry[5], $entry[6], $entry[7], $entry[8], '','USA', $fid);
74 sqlStatement($asql, $addressInsert);
76 //Insert Phone and Fax number
77 $exPhone = explode("-", $phone);
78 $exFax = explode("-", $fax);
80 $sql = "SELECT MAX(id) as id FROM phone_numbers"; // Let's do this for the case others insert numbers besides pharmacies.
81 $aid = sqlQuery($sql);
82 $aid = $aid['id'] + 1;
83 $psql = "INSERT INTO phone_numbers (id, country_code, area_code, prefix, number, type, foreign_id) VALUES (?,?,?,?,?,?,?)";
84 $phoneInsert = array($aid, 1, $exPhone[0], $exPhone[1], $exPhone[2], 2, $fid);
85 sqlStatement($psql, $phoneInsert);
86 ++$aid;
87 $faxInsert = array($aid, 1, $exFax[0], $exFax[1], $exFax[2], 5, $fid);
88 sqlStatement($psql, $faxInsert);
90 $tm++;
91 } //data insert if not present
92 } //loop conditional
93 } //end of loop
95 sqlStatementNoLog("COMMIT"); // What else!
96 sqlStatementNoLog("SET autocommit=1");
98 header("Location: ". $ref."?status=finished");
101 <i class="fa fa-refresh fa-spin fa-3x fa-fw"></i>
102 <span class="sr-only"><?php echo xlt("Loading... Please wait"); ?></span>