Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 116
0.00% covered (danger)
0.00%
0 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
LoyaltyGroup
0.00% covered (danger)
0.00%
0 / 116
0.00% covered (danger)
0.00%
0 / 17
3306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
42
 getLoyaltyConfig
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
 getIDByTypeNum
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
72
 getGroupByID
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
30
 addStore
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 removeStore
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
 findAllTriggers
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
 findAllSMSMessages
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
 findAllCoupons
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
 validGroup
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getGroupTimeZone
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getStoresArray
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setStoresArray
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupID
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setGroupID
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDb
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setDb
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 LoyaltyGroup {
6    private $db;
7
8    public $groupID;
9    public $storesArray;
10    public $loyaltyConfig;
11
12
13    public function __construct($groupID = null, $typeNum = null) {
14        global $db_name;
15        $this->db = dbConnectByName($db_name);
16        if(isset($groupID)) {
17            if($this->getGroupByID($groupID)) {
18                return true;
19            }
20        } else if(isset($typeNum)) {
21            if($groupID = $this->getIDByTypeNum($typeNum)) {
22                if($this->getGroupByID($groupID))
23                    return true;
24            }
25        }
26        return false;
27    }
28    public function getLoyaltyConfig() {
29        if(isset($this->loyaltyConfig)) {
30            return $this->loyaltyConfig;
31        } else {
32            if($this->validGroup()) {
33                $stmt = $this->db->prepare("SELECT * FROM loyaltyConfig WHERE groupID = :groupID ORDER BY dateChanged DESC LIMIT 1");
34                $stmt->bindValue(":groupID", $this->groupID);
35                if($stmt->execute()) {
36                    $row = $stmt->fetch(\PDO::FETCH_ASSOC);
37                    if(!empty($row)) {
38                        $this->loyaltyConfig = new \LoyaltyConfig($row);
39                        return $this->loyaltyConfig;
40                    } else {
41                        $this->loyaltyConfig = new \LoyaltyConfig();
42                        return $this->loyaltyConfig;
43                    }
44                }
45            }
46        }
47        return NULL;
48    }
49    public function getIDByTypeNum($typeNum) {
50        $tempStoreArray = [];
51        try {
52            $stmt = $this->db->prepare("SELECT * FROM loyaltyGroups WHERE store = :typeNum");
53            $stmt->bindParam(":typeNum", $typeNum);
54            if($stmt->execute()) {
55                $count = 0;
56                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
57                    $tempStore = new \Store();
58                    if($tempStore->createStore($row['store'])) {
59                        $count++;
60                        return $row['groupID'];
61                    }
62                }
63                //We have not found the store in any group. Lets create a group for this store
64                if($count == 0) {
65                    //Check if valid store
66                    $store = new \Store();
67                    if($store->createStore($typeNum)) {
68                        $stmt = $this->db->prepare("INSERT INTO loyaltyGroups (groupID, store) SELECT MAX(groupID)+1, :typeNum FROM loyaltyGroups");
69                        $stmt->bindParam(":typeNum", $typeNum);
70                        if($stmt->execute()) {
71                            $tempStoreArray[] = $store;
72                            $tempGroup = new LoyaltyGroup(null, $typeNum);
73                            return $tempGroup->getGroupID();
74                        } else {
75                            return false;
76                        }
77                    } else {
78                        return false;
79                    }
80                }
81            } else {
82                return false;
83            }
84        } catch (\PDOException $e) {
85            return false;
86        }
87    }
88    public function getGroupByID($groupID) {
89        try {
90            $tempStoreArray = [];
91            $stmt = $this->db->prepare("SELECT * FROM loyaltyGroups WHERE groupID = :groupID");
92            $stmt->bindParam(":groupID", $groupID);
93            if($stmt->execute()) {
94                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
95                    $tempStore = new \Store();
96                    if($tempStore->createStore($row['store'])) {
97                        $tempStoreArray[] = $tempStore;
98                    }
99                }
100                $this->storesArray = $tempStoreArray;
101                $this->groupID = $groupID;
102                return true;
103            } else {
104                return false;
105            }
106        } catch (\PDOException $e) {
107            return false;
108        }
109    }
110    public function addStore($typeNum) {
111        $store = new \Store();
112        if($store->createStore($typeNum) && isset($this->groupID)) {
113            $stmt = $this->db->prepare("INSERT INTO loyaltyGroups (store, groupID) VALUES (:typeNum, :groupID)");
114            $stmt->bindParam(":typeNum", $typeNum);
115            $stmt->bindParam(":groupID", $this->groupID);
116            if($stmt->execute()) {
117                return true;
118            }
119            return false;
120        } else {
121            return false;
122        }
123    }
124    public function removeStore($typeNum) {
125        $store = new \Store();
126        if($store->createStore($typeNum) && isset($this->groupID)) {
127            $stmt = $this->db->prepare("DELETE FROM loyaltyGroups WHERE store = :typeNum AND groupID = :groupID");
128            $stmt->bindParam(":typeNum", $typeNum);
129            $stmt->bindParam(":groupID", $this->groupID);
130            if($stmt->execute()) {
131                if($stmt->rowCount() > 0) {
132                    return true;
133                }
134            }
135        }
136        return false;
137    }
138    public function findAllTriggers() {
139        $triggerArray = [];
140        try {
141            //TODO: Add expiration date
142            $stmt = $this->db->query("SELECT id FROM loyaltyTriggers WHERE deleted = 0 AND storeGroup = ".$this->getGroupID());
143            if($stmt) {
144                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
145                    if($trigger = new \LoyaltyTrigger($this->getGroupID(), null, $row['id'])) {
146                        $triggerArray[] = $trigger;
147                    }
148                }
149            }
150            return $triggerArray;
151        } catch (\PDOException $e) {
152
153        }
154        return NULL;
155    }
156    public function findAllSMSMessages() {
157        $messageArray = [];
158        try {
159            //TODO: Add expiration date
160            $stmt = $this->db->query("SELECT id FROM loyaltySMSMessages WHERE storeGroup = ".$this->getGroupID());
161            if($stmt) {
162                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
163                    if($message = new \LoyaltySMSMessage($this->groupID, null, $row['id'])) {
164                        $messageArray[] = $message;
165                    }
166                }
167            }
168            return $messageArray;
169        } catch (\PDOException $e) {
170
171        }
172        return NULL;
173    }
174    public function findAllCoupons() {
175        $couponArray = [];
176        try {
177            //TODO: Add expiration date
178            $stmt = $this->db->query("SELECT id FROM loyaltyCoupons WHERE useType = 0 AND useID = ".$this->getGroupID());
179            if($stmt) {
180                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
181                    if($coupon = new \LoyaltyCoupon($this->groupID, null, $row['id'])) {
182                        $couponArray[] = $coupon;
183                    }
184                }
185            }
186            return $couponArray;
187        } catch (\PDOException $e) {
188
189        }
190        return NULL;
191    }
192    public function validGroup() {
193        if(isset($this->groupID)) {
194            return true;
195        }
196        return false;
197    }
198    public function getGroupTimeZone() {
199        return $this->storesArray[0]->getTimeZone();
200    }
201
202
203    /**
204     * @return array
205     */
206    public function getStoresArray()
207    {
208        return $this->storesArray;
209    }
210
211    /**
212     * @param array $storesArray
213     */
214    public function setStoresArray($storesArray)
215    {
216        $this->storesArray = $storesArray;
217    }
218
219    /**
220     * @return mixed
221     */
222    public function getGroupID()
223    {
224        return $this->groupID;
225    }
226
227    /**
228     * @param mixed $groupID
229     */
230    public function setGroupID($groupID)
231    {
232        $this->groupID = $groupID;
233    }
234    /**
235     * @return null|PDO
236     */
237    public function getDb()
238    {
239        return $this->db;
240    }
241
242    /**
243     * @param null|PDO $db
244     */
245    public function setDb($db)
246    {
247        $this->db = $db;
248    }
249
250
251}