Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 141
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
RemoteCheckIn
0.00% covered (danger)
0.00%
0 / 141
0.00% covered (danger)
0.00%
0 / 9
756
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 addBuy
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
56
 getDailyNum
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 getCustomerID
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
42
 insertBuy
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
30
 broadcastBuy
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
2
 sendEncodedData
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getStore
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setStore
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace BuyerKiosk\ResalePerks;
4
5class RemoteCheckIn extends Contact {
6
7    private $data;
8    /** @var \Store $store */
9    private $store;
10    /** @var \PDO $storeDB */
11    private $storeDB;
12
13
14    public function __construct($phone, $data)
15    {
16        parent::__construct();
17        $this->data = $data;
18        if($this->get($phone)) {
19            return true;
20        }
21        return false;
22    }
23
24    public function addBuy() {
25        if ($this->store instanceof \Store && $this->store !== null && $this->data['config'] !== null)
26        {
27            $this->storeDB = dbConnectByName($this->store->getDbName());
28            //Get Daily Num
29            $dailyNum = $this->getDailyNum();
30            if(!($dailyNum > 0)) {
31                return false;
32            }
33            //Update or insert customer and get customerID
34            $customer = $this->getCustomerID();
35            if($customer['customerID'] == null) {
36                error_log("CustomerID Null");
37                return false;
38            }
39            $buyID = $this->insertBuy($dailyNum,$customer['isNewCustomer'],$customer['customerID']);
40            if($buyID == null) {
41                error_log("BuyID Null");
42                return false;
43            }
44            $this->broadcastBuy($buyID);
45            return true;
46        }
47        error_log("Config Null");
48        return false;
49    }
50    private function getDailyNum() {
51        try {
52            $dateRange = getCurrentDateRange();
53            $dailyNumQuery = $this->storeDB->query("SELECT MAX(dailyNum) as lastNum FROM buyQueue WHERE timeEntered BETWEEN '" . $dateRange['dateStart']->format("Y-m-d H:i:s") . "' AND '" . $dateRange['dateEnd']->format("Y-m-d H:i:s") . "'");
54            $dailyNum = $dailyNumQuery->fetch(\PDO::FETCH_ASSOC);
55            $dailyNum = $dailyNum['lastNum'];
56            if($dailyNum == null) {
57                $dailyNum = 1;
58            } else {
59                $dailyNum++;
60            }
61            return $dailyNum;
62        } catch (\PDOException $e) {
63
64        }
65        return null;
66    }
67    private function getCustomerID() {
68        $isNewCustomer = 0;
69        $customerID = null;
70        try {
71            $customerQuery = $this->storeDB->prepare("SELECT customerID FROM customers WHERE phone = ? ORDER BY dateAdded DESC LIMIT 1");
72            $customerQuery->execute(array($this->data['config']['PhoneNum']));
73            try {
74                if ($customerQuery->rowCount() > 0) {
75                    $stmt = $this->storeDB->prepare("UPDATE customers SET firstName=:firstName,lastName=:lastName,email=:email,address=:address,city=:city,state=:state,zipcode=:zipcode,driversLicense=:driversLicense,onSMS=:onSMS,onEmail=:onEmail,referralCode=:referralCode WHERE phone=:phone");
76                    $row = $customerQuery->fetch(\PDO::FETCH_ASSOC);
77                    $customerID = $row['customerID'];
78                } else {
79                    $isNewCustomer = 1;
80                    $stmt = $this->storeDB->prepare("INSERT INTO customers ( firstName, lastName, email, address, city, state, zipcode, phone, onSMS, onEmail, driversLicense, referralCode) 
81                                                                              VALUES (:firstName,:lastName,:email,:address,:city,:state,:zipcode,:phone,:onSMS,:onEmail,:driversLicense,:referralCode)");
82                }
83                $stmt->bindValue(":firstName", $this->data['config']['FirstName']);
84                $stmt->bindValue(":lastName", $this->data['config']['LastName']);
85                $stmt->bindValue(":email", $this->data['config']['Email']);
86                $stmt->bindValue(":address", $this->data['config']['Address']);
87                $stmt->bindValue(":city", $this->data['config']['City']);
88                $stmt->bindValue(":state", $this->data['config']['State']);
89                $stmt->bindValue(":zipcode", $this->data['config']['ZipCode']);
90                $stmt->bindValue(":phone", $this->data['config']['PhoneNum']);
91                $stmt->bindValue(":onSMS", 0);
92                $stmt->bindValue(":onEmail", 0);
93                $stmt->bindValue(":driversLicense", 99999999);
94                $stmt->bindValue(":referralCode", 000000);
95                if($stmt->execute()) {
96                    if($isNewCustomer) {
97                        $customerID = $this->storeDB->lastInsertId();
98                    }
99                    return array("isNewCustomer" => $isNewCustomer, "customerID" => $customerID);
100                }
101            } catch (\PDOException $e) {
102
103            }
104        } catch (\PDOException $e) {
105
106        }
107        return null;
108    }
109
110    private function insertBuy($dailyNum, $isNewCustomer, $customerID) {
111        $buyID = null;
112        if($this->data['ReturnType'] == 1) {
113            $inStore = 1;
114        } else {
115            $inStore = 0;
116        }
117        if($this->data['DropOffType'] == 1) {
118            $remote = 1;
119            $hasDroppedOff = 1;
120        } else {
121            $remote = 2;
122            $hasDroppedOff = 0;
123        }
124        error_log("DailyNum: ".$dailyNum);
125        error_log("CustomerID: ".$customerID);
126        error_log("isNewCustomer: ".$isNewCustomer);
127        error_log("Containers: ".$this->data['Containers']);
128        error_log("UpperBound: ".$this->data['wait']['upperBound']);
129
130        try {
131            $this->storeDB->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
132            $this->storeDB->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
133            $stmt = $this->storeDB->prepare("INSERT INTO buyQueue (tabletID, dailyNum, numContainers, textMe, inStore, returnTime, isReturning, customerID, isNewCustomer, remote, hasDroppedOff, upperBound, lowerBound, waitTime, sorterID) 
134                                                                VALUES     (:tabletID,:dailyNum,:numContainers,:textMe,:inStore,:returnTime,:isReturning,:customerID,:isNewCustomer,:remote,:hasDroppedOff,:upperBound,:lowerBound,:waitTime,:sorterID)");
135            $stmt->bindValue(":tabletID", 99);
136            $stmt->bindValue(":dailyNum", $dailyNum);
137            $stmt->bindValue(":numContainers", $this->data['Containers']);
138            $stmt->bindValue(":textMe", 2);
139            $stmt->bindValue(":inStore", $inStore);
140            $stmt->bindValue(":returnTime", '0000-00-00 00:00:00');
141            $stmt->bindValue(":isReturning", 0);
142            $stmt->bindValue(":customerID", $customerID);
143            $stmt->bindValue(":isNewCustomer", $isNewCustomer);
144            $stmt->bindValue(":remote", $remote);
145            $stmt->bindValue(":hasDroppedOff", $hasDroppedOff);
146            $stmt->bindValue(":upperBound", $this->data['wait']['upperBound']);
147            $stmt->bindValue(":lowerBound", $this->data['wait']['lowerBound']);
148            $stmt->bindValue(":waitTime", $this->data['wait']['waitTime']);
149            $stmt->bindValue(":sorterID", 0);
150            if($stmt->execute()) {
151                $buyID = $this->storeDB->lastInsertId();
152                return $buyID;
153            }
154        } catch (\PDOException $e) {
155            error_log($e->getMessage());
156        }
157        return null;
158    }
159    private function broadcastBuy($buyID) {
160        $buyQueue = new \BuyQueue();
161        $buyQueue->createStore($this->store->getTypeNum());
162        //$now = new \DateTime('now', new \DateTimeZone($buyQueue->getTimeZone()));
163        $db = dbConnectByName($buyQueue->getDbName());
164        $QueueItem = $buyQueue->getOneQueueItem($db, $buyID);
165        $entryData = array(
166            'action' => 'addNewBuy'
167        , 'category' => $this->store->getTypeNum()
168        , 'buyID' => $buyID
169        , 'firstName' => $QueueItem->getFirstName()
170        , 'lastName' => $QueueItem->getLastName()
171        , 'timeEntered' => $QueueItem->getTimeEntered()
172        , 'timeStarted' => "0000-00-00 00:00:00"
173        , 'timeCompleted' => "0000-00-00 00:00:00"
174        , 'sortStarted' => "0000-00-00 00:00:00"
175        , 'sortCompleted' => "0000-00-00 00:00:00"
176        , 'buyerID' => NULL
177        , 'numContainers' => $QueueItem->numContainers
178        , 'processedContainers' => 0
179        , 'textMe' => 0
180        , 'inStore' => $QueueItem->inStore
181        , 'isNewCustomer' => $QueueItem->getIsNewCustomer()
182        , 'returnTime' => $QueueItem->returnTime
183        , 'isReturning' => $QueueItem->isReturning,
184            'hasFlags' => $QueueItem->getHasFlags(),
185            'recentBuys' => $QueueItem->getRecentBuys(),
186            'clearFlags' => false,
187            'remote' => $QueueItem->remote,
188            'hasDroppedOff' => $QueueItem->getHasDroppedOff()
189        );
190        $entryData2 = array('action' => 'drsNewBuy'
191        , 'category' => $this->store->getTypeNum()
192        , 'buyID' => $buyID
193        );
194
195        $this->sendEncodedData($entryData2);
196        $this->sendEncodedData($entryData);
197    }
198    private function sendEncodedData($data) {
199      $ably = new \Ably\AblyRest('C4Pmuw.Jz-8_g:vr6qqL5CZS6k3K1B');
200      $channel = $ably->channel($data['category']);
201
202      // Publish a message to the test channel
203      $channel->publish($data['action'],$data);
204    }
205
206    /**
207     * @return \Store
208     */
209    public function getStore()
210    {
211        return $this->store;
212    }
213
214    /**
215     * @param \Store $store
216     */
217    public function setStore($store)
218    {
219        $this->store = $store;
220    }
221
222
223}