Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 210
0.00% covered (danger)
0.00%
0 / 44
CRAP
0.00% covered (danger)
0.00%
0 / 1
LoyaltyCustomer
0.00% covered (danger)
0.00%
0 / 210
0.00% covered (danger)
0.00%
0 / 44
5256
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
20
 getByPhone
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
20
 update
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
12
 create
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
20
 getBuyerRating
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 matchToBuyCustomer
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
30
 getLastVisitedStore
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 getMostVisitedStore
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 getLoyaltyLog
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
30
 createTypeNumArrayString
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 getFirstName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setFirstName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLastName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setLastName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDateAdded
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setDateAdded
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getEmail
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setEmail
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPhone
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setPhone
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOptSent
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setOptSent
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOptValue
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setOptValue
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOptReceived
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setOptReceived
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getBirthday
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setBirthday
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getStoreGroup
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setStoreGroup
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOriginStore
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setOriginStore
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLastText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setLastText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLoyaltyID
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setLoyaltyID
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getZipcode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setZipcode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getReferredBy
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setReferredBy
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLastTextTimestamp
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setLastTextTimestamp
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getNexmo
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setNexmo
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace BuyerKiosk\Core\Loyalty;
4
5class LoyaltyCustomer {
6    public $loyaltyID;
7    public $firstName;
8    public $lastName;
9    public $dateAdded;
10    public $email;
11    public $phone;
12    public $optSent;
13    public $optValue;
14    public $optReceived;
15    public $birthday;
16    public $zipcode;
17    public $referredBy;
18    public $db;
19    public $storeGroup;
20    public $originStore;
21    public $lastText;
22    public $lastTextTimestamp;
23    public $nexmo;
24
25
26    public function __construct($loyaltyID = null, \PDO $db = null) {
27        if($db == null) {
28            global $db_name;
29            $db = dbConnectByName($db_name);
30            $this->db = $db;
31        } else {
32            $this->db = $db;
33        }
34        if(isset($loyaltyID)) {
35            $stmt = $db->prepare("SELECT * FROM loyaltyCustomers WHERE id = :id");
36            $stmt->bindParam(":id", $loyaltyID);
37            if($stmt->execute()) {
38                $row = $stmt->fetch(\PDO::FETCH_ASSOC);
39                $this->firstName = $row['firstName'];
40                $this->loyaltyID = $loyaltyID;
41                $this->lastName = $row['lastName'];
42                $this->dateAdded = $row['dateAdded'];
43                $this->email = $row['email'];
44                $this->phone = $row['phone'];
45                $this->birthday = $row['birthday'];
46                $this->optSent = $row['optSent'];
47                $this->optReceived = $row['optReceived'];
48                $this->optValue = $row['optValue'];
49                $this->storeGroup = $row['storeGroup'];
50                $this->originStore = $row['originStore'];
51                $this->lastText = $row['lastText'];
52                $this->nexmo = $row['nexmo'];
53                return true;
54            }
55            return false;
56        }
57        return true;
58    }
59    public function getByPhone($phone, $storeGroup) {
60        $stmt = $this->db->prepare("SELECT * FROM loyaltyCustomers WHERE phone = :phone AND storeGroup = :storeGroup");
61        $stmt->bindParam(":phone", $phone);
62        $stmt->bindParam(":storeGroup", $storeGroup);
63        if($stmt->execute()) {
64            $row = $stmt->fetch(\PDO::FETCH_ASSOC);
65            if(isset($row['id']) && $row['id'] > 0) {
66                $this->firstName = $row['firstName'];
67                $this->loyaltyID = $row['id'];
68                $this->lastName = $row['lastName'];
69                $this->dateAdded = $row['dateAdded'];
70                $this->email = $row['email'];
71                $this->phone = $row['phone'];
72                $this->birthday = $row['birthday'];
73                $this->optSent = $row['optSent'];
74                $this->optReceived = $row['optReceived'];
75                $this->optValue = $row['optValue'];
76                $this->storeGroup = $row['storeGroup'];
77                $this->originStore = $row['originStore'];
78                $this->lastText = $row['lastText'];
79                $this->nexmo = $row['nexmo'];
80                //$this->import= $row['import'];
81                return true;
82            }
83        }
84        return false;
85    }
86    public function update() {
87      $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
88      try {
89          $stmt = $this->db->prepare("UPDATE loyaltyCustomers SET firstName = :firstName, lastName = :lastName, email = :email, phone = :phone, optValue = :optValue, optSent = :optSent, optReceived = :optReceived, birthday = :birthday, storeGroup = :storeGroup, originStore = :originStore, lastText = :lastText, lastTextTimestamp = :lastTextTimeStamp, nexmo = :nexmo WHERE id = :loyaltyID");
90          $stmt->bindValue(":loyaltyID", $this->loyaltyID);
91          $stmt->bindValue(":firstName", $this->firstName);
92          $stmt->bindValue(":lastName", $this->lastName);
93          $stmt->bindValue(":email", $this->email);
94          $stmt->bindValue(":phone", $this->phone);
95          $stmt->bindValue(":optValue", $this->optValue);
96          $stmt->bindValue(":optSent", $this->optSent);
97          $stmt->bindValue(":optReceived", $this->optReceived);
98          $stmt->bindValue(":birthday", $this->birthday);
99          $stmt->bindValue(":storeGroup", $this->storeGroup);
100          $stmt->bindValue(":originStore", $this->originStore);
101          $stmt->bindValue(":lastText", $this->lastText);
102          $stmt->bindValue(":lastTextTimeStamp", $this->lastTextTimestamp);
103          $stmt->bindValue(":nexmo", $this->nexmo);
104          //$stmt->bindValue(":import", $this->import);
105          if($stmt->execute()) {
106              return true;
107          }
108      } catch (\PDOException $e) {
109          //echo $e->getMessage();
110          error_log($e->getMessage());
111      }
112      return false;
113    }
114    public function create($send = true) {
115      $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
116      //error_log("Nexmo: ".$this->getNexmo());
117      try {
118          $stmt = $this->db->prepare("INSERT INTO loyaltyCustomers (phone, firstName, lastName, birthday, email, storeGroup, originStore, zipcode, referredBy, nexmo, optSent, optReceived, optValue, lastText, dateAdded)
119VALUES (:phone, :firstName, :lastName, :birthday, :email, :storeGroup, :originStore, :zipcode, :referredBy, :nexmo, '0000-00-00 00:00:00', '0000-00-00 00:00:00', 0, 0, CURRENT_TIMESTAMP)");
120          $stmt->bindValue(":firstName", $this->firstName);
121          $stmt->bindValue(":lastName", $this->lastName);
122          $stmt->bindValue(":email", $this->email);
123          $stmt->bindValue(":phone", $this->phone);
124          $stmt->bindValue(":birthday", $this->birthday);
125          $stmt->bindValue(":storeGroup", $this->storeGroup);
126          $stmt->bindValue(":originStore", $this->originStore);
127          $stmt->bindValue(":zipcode", $this->zipcode);
128          $stmt->bindValue(":referredBy", $this->referredBy);
129          $stmt->bindValue(":nexmo", $this->nexmo);
130          //$stmt->bindValue(":import", $this->import);
131          if($stmt->execute()) {
132              $this->loyaltyID = $this->db->lastInsertId();
133              $response = new \LoyaltyResponse($this->loyaltyID);
134              //error_log("Nexmo: ".$response->getNexmo());
135              if($send) {
136                  error_log("Send Opt-In To: ".$this->phone);
137                  return true;
138                  /*if($response->sendSMSByShortName("optIn")
139                      return true;*/
140              } else {
141                  return $this->loyaltyID;
142              }
143
144          }
145      } catch (\PDOException $e) {
146          error_log($e->getMessage());
147          //echo $e->getMessage();
148          return false;
149      }
150      return false;
151    }
152    public function getBuyerRating($typeNum) {
153        $customer = $this->matchToBuyCustomer($typeNum);
154        if(isset($customer)) {
155            return $customer->getBuyerRating();
156        }
157        return null;
158    }
159    public function matchToBuyCustomer($typeNum) {
160        $store = new \Store();
161        $store->createStore($typeNum);
162        $db = dbConnectByName($store->getTypeNum());
163        try {
164            $stmt = $db->query("SELECT customerID, firstName, lastName FROM customers WHERE phone = :phone");
165            $stmt->bindValue(":phone", $this->phone);
166            if($stmt->execute()) {
167                if($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
168                    $fullName = $this->firstName." ".$this->lastName;
169                    $cFullName = $row['firstName']." ".$row['lastName'];
170                    if(similarity($fullName, $cFullName)*100 > 80) {
171                        return $customer = new \Customer($row['customerID'], $store->getTypeNum());
172                    }
173                }
174            }
175        } catch (\PDOException $e) {
176            error_log($e->getMessage());
177            return null;
178        }
179        return null;
180    }
181    public function getLastVisitedStore() {
182        try {
183            $stmt = $db = $this->db->prepare("SELECT typeNum FROM loyaltyData WHERE loyaltyID = :loyaltyID ORDER BY date DESC LIMIT 1");
184            $stmt->bindValue(":loyaltyID", $this->loyaltyID);
185            if($stmt->execute()) {
186                if($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
187                    return $row['typeNum'];
188                }
189            }
190        } catch (\PDOException $e) {
191            error_log($e->getMessage());
192        }
193        return null;
194    }
195    public function getMostVisitedStore() {
196        try {
197            $stmt = $db = $this->db->prepare("SELECT typeNum FROM loyaltyData WHERE loyaltyID = :loyaltyID ORDER BY date DESC LIMIT 1");
198            $stmt->bindValue(":loyaltyID", $this->loyaltyID);
199            if($stmt->execute()) {
200                if($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
201                    return $row['typeNum'];
202                }
203            }
204        } catch (\PDOException $e) {
205            error_log($e->getMessage());
206        }
207        return null;
208    }
209    public function getLoyaltyLog() {
210        $loyaltyGroup = new \LoyaltyGroup($this->storeGroup);
211
212        error_log($loyaltyGroup->getGroupID());
213
214        $loyaltyConfig = $loyaltyGroup->getLoyaltyConfig();
215        $storesArray = [];
216        foreach($loyaltyGroup->storesArray as $store) {
217            error_log($store->getTypeNum());
218            $storesArray[$store->getTypeNum()] = "[".$store->getStoreNum()."] - ".$store->getCity();
219        }
220
221
222        $stmt = $this->db->prepare("SELECT * FROM loyaltyData WHERE loyaltyID = :loyaltyID ORDER BY id DESC");
223        $stmt->bindValue(":loyaltyID", $this->loyaltyID);
224        $stmt->execute();
225        $dataRows = [];
226        $totals = array(
227            "salesTotal" => 0,
228            "redeemedPoints" => 0,
229            "discountReceived" => 0,
230            "subPoints" => 0,
231            "subSalesTotal" => 0,
232            "addPoints" => 0
233        );
234        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
235            if(in_array($row['typeNum'],$storesArray)) {
236                $row['store'] = $storesArray[$row['typeNum']];
237            } else {
238                $row['store'] = $row['typeNum'];
239            }
240
241            $dataRows[] = $row;
242            if($row['type'] == 1) {
243                $totals['salesTotal'] += floor(floatval($row['salesTotal']));
244            } else {
245                $totals['redeemedPoints'] += floatval($row['redeemedPoints']);
246                $totals['discountReceived'] += floatval($row['discountReceived']);
247                $totals['subPoints'] += floatval($row['subPoints']);
248                $totals['subSalesTotal'] += floor(floatval($row['subSalesTotal']));
249                $totals['addPoints'] += floatval($row['addPoints']);
250            }
251        }
252        $stmt = $this->db->prepare("SELECT COUNT(DISTINCT(typeNum)) as storesVisited FROM loyaltyData WHERE loyaltyID = :loyaltyID");
253        $stmt->bindValue(":loyaltyID", $this->loyaltyID);
254        $stmt->execute();
255        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
256        $totalPoints = ($totals['salesTotal'] - $totals['subSalesTotal']) * $loyaltyConfig->d2p;
257        error_log("Total Points: ".$totalPoints);
258        $rewardPoints = ($totalPoints + $totals['addPoints']) - $totals['redeemedPoints'] - $totals['subPoints'];
259        error_log("Sub Points: ".($totals['redeemedPoints'] - $totals['subPoints']));
260
261        return array("data" => $dataRows, "totals" => $totals, "rewardPoints" => floor($rewardPoints), "storesVisited" => $row['storesVisited']);
262    }
263    private function createTypeNumArrayString($typeNumArray) {
264        $string = ""; $count = 0;
265        foreach($typeNumArray as $typeNum) {
266            if($count < count($typeNumArray)-1) {
267                $string .= "'".$typeNumArray[$count]."',";
268            } else {
269                $string .= "'".$typeNumArray[$count]."'";
270            }
271            $count++;
272        }
273        return $string;
274    }
275    public function getFirstName()
276    {
277        return $this->firstName;
278    }
279
280    /**
281     * @param mixed $firstName
282     */
283    public function setFirstName($firstName)
284    {
285        $this->firstName = $firstName;
286    }
287
288    /**
289     * @return mixed
290     */
291    public function getLastName()
292    {
293        return $this->lastName;
294    }
295
296    /**
297     * @param mixed $lastName
298     */
299    public function setLastName($lastName)
300    {
301        $this->lastName = $lastName;
302    }
303
304    /**
305     * @return mixed
306     */
307    public function getDateAdded()
308    {
309        return $this->dateAdded;
310    }
311
312    /**
313     * @param mixed $dateAdded
314     */
315    public function setDateAdded($dateAdded)
316    {
317        $this->dateAdded = $dateAdded;
318    }
319
320    /**
321     * @return mixed
322     */
323    public function getEmail()
324    {
325        return $this->email;
326    }
327
328    /**
329     * @param mixed $email
330     */
331    public function setEmail($email)
332    {
333        $this->email = $email;
334    }
335
336    /**
337     * @return mixed
338     */
339    public function getPhone()
340    {
341        return $this->phone;
342    }
343
344    /**
345     * @param mixed $phone
346     */
347    public function setPhone($phone)
348    {
349        $this->phone = $phone;
350    }
351
352    /**
353     * @return mixed
354     */
355    public function getOptSent()
356    {
357        return $this->optSent;
358    }
359
360    /**
361     * @param mixed $optSent
362     */
363    public function setOptSent($optSent)
364    {
365        $this->optSent = $optSent;
366    }
367
368    /**
369     * @return mixed
370     */
371    public function getOptValue()
372    {
373        return $this->optValue;
374    }
375
376    /**
377     * @param mixed $optValue
378     */
379    public function setOptValue($optValue)
380    {
381        $this->optValue = $optValue;
382    }
383
384    /**
385     * @return mixed
386     */
387    public function getOptReceived()
388    {
389        return $this->optReceived;
390    }
391
392    /**
393     * @param mixed $optReceived
394     */
395    public function setOptReceived($optReceived)
396    {
397        $this->optReceived = $optReceived;
398    }
399
400    /**
401     * @return mixed
402     */
403    public function getBirthday()
404    {
405        return $this->birthday;
406    }
407
408    /**
409     * @param mixed $birthday
410     */
411    public function setBirthday($birthday)
412    {
413        $this->birthday = $birthday;
414    }
415
416    /**
417     * @return mixed
418     */
419    public function getStoreGroup()
420    {
421        return $this->storeGroup;
422    }
423
424    /**
425     * @param mixed $storeGroup
426     */
427    public function setStoreGroup($storeGroup)
428    {
429        $this->storeGroup = $storeGroup;
430    }
431
432    /**
433     * @return mixed
434     */
435    public function getOriginStore()
436    {
437        return $this->originStore;
438    }
439
440    /**
441     * @param mixed $originStore
442     */
443    public function setOriginStore($originStore)
444    {
445        $this->originStore = $originStore;
446    }
447
448    /**
449     * @return mixed
450     */
451    public function getLastText()
452    {
453        return $this->lastText;
454    }
455
456    /**
457     * @param mixed $lastText
458     */
459    public function setLastText($lastText)
460    {
461        $this->lastText = $lastText;
462    }
463
464    /**
465     * @return mixed
466     */
467    public function getLoyaltyID()
468    {
469        return $this->loyaltyID;
470    }
471
472    /**
473     * @param mixed $loyaltyID
474     */
475    public function setLoyaltyID($loyaltyID)
476    {
477        $this->loyaltyID = $loyaltyID;
478    }
479
480    /**
481     * @return mixed
482     */
483    public function getZipcode()
484    {
485        return $this->zipcode;
486    }
487
488    /**
489     * @param mixed $zipcode
490     */
491    public function setZipcode($zipcode)
492    {
493        $this->zipcode = $zipcode;
494    }
495
496    /**
497     * @return mixed
498     */
499    public function getReferredBy()
500    {
501        return $this->referredBy;
502    }
503
504    /**
505     * @param mixed $referredBy
506     */
507    public function setReferredBy($referredBy)
508    {
509        $this->referredBy = $referredBy;
510    }
511
512    /**
513     * @return mixed
514     */
515    public function getLastTextTimestamp()
516    {
517        return $this->lastTextTimestamp;
518    }
519
520    /**
521     * @param mixed $lastTextTimestamp
522     */
523    public function setLastTextTimestamp($lastTextTimestamp)
524    {
525        $this->lastTextTimestamp = $lastTextTimestamp;
526    }
527
528    /**
529     * @return mixed
530     */
531    public function getNexmo()
532    {
533        return $this->nexmo;
534    }
535
536    /**
537     * @param mixed $nexmo
538     */
539    public function setNexmo($nexmo)
540    {
541        $this->nexmo = $nexmo;
542    }
543
544
545
546
547
548
549
550
551}