Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 263
0.00% covered (danger)
0.00%
0 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
MassSMSFinder
0.00% covered (danger)
0.00%
0 / 263
0.00% covered (danger)
0.00%
0 / 15
6162
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 findToday
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 processToday
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
 getCustomerList
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
72
 findJustBuyers
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 findJustSellers
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 findRecentBuyers
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
20
 findRecentSellers
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 findRecentBoth
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 findAll
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 findAllStoreGroup
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 findCustomerCount
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 1
240
 createOutgoingSMS
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 1
272
 getLog
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setLog
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace BuyerKiosk\Core\Robot;
4
5use BuyerKiosk\Core\Store;
6use BuyerKiosk\Core\Loyalty\LoyaltyCustomer;
7use BuyerKiosk\Core\Loyalty\LoyaltyCoupon;
8use BuyerKiosk\Core\Loyalty\LoyaltyMassSMS;
9use BuyerKiosk\Core\Loyalty\LoyaltyOutgoing;
10use BuyerKiosk\Core\Loyalty\LoyaltySMSMessage;
11
12class MassSMSFinder extends Robot {
13    public $log;
14    private $customer_list_array;
15
16    public function __construct(\KLogger $log = null) {
17        parent::__construct();
18        if($log == null) {
19            $this->log = new \KLogger("massSMSRobot.log", \KLogger::DEBUG);
20            //$this->log->LogDebug("Logger Instantiated");
21        } else {
22            $this->log = $log;
23            //$this->log->LogDebug("Logger Found");
24        }
25
26    }
27    public function findToday() {
28        //$this->log->LogDebug("Find Today");
29        $date = getCurrentDateRange();
30        $this->log->LogDebug($date['dateStart']->format('Y-m-d'));
31        try {
32            $stmt = $this->globalDB->prepare("SELECT * FROM loyaltyMassSMS WHERE date = :date AND processed = 0");
33            $stmt->bindValue(":date", $date['dateStart']->format('Y-m-d'));
34            $stmt->execute();
35            return $stmt->fetchAll(\PDO::FETCH_ASSOC);
36        } catch (\PDOException $e) {
37            $this->log->LogError($e->errorInfo);
38        }
39        return null;
40    }
41    public function processToday() {
42        $todaysRecords = $this->findToday();
43        //$this->log->LogDebug("Count: ".count($todaysRecords));
44        //$this->log->LogDebug("Process Today");
45        foreach($todaysRecords as $massSMS) {
46            $massSMS = new LoyaltyMassSMS($massSMS['id'], $massSMS['storeGroup'], $massSMS['typeNum']);
47            $this->getCustomerList($massSMS);
48            $this->log->LogDebug("Started Working on [".$massSMS->id."]");
49            $this->log->LogDebug("Customer Count: ".count($this->customer_list_array));
50
51            if(count($this->customer_list_array) > 0) {
52                foreach($this->customer_list_array as $customer) {
53                    $customer = new LoyaltyCustomer($customer);
54                    if($this->createOutgoingSMS($massSMS, $customer)) {
55                        $this->log->LogDebug("Finished Processing [".$customer->loyaltyID."] - [".$massSMS->id."]");
56                    }
57                }
58                $massSMS->processed = 1;
59                $massSMS->update();
60            }
61        }
62    }
63
64    public function getCustomerList(LoyaltyMassSMS $massSMS) {
65        switch ($massSMS->listType) {
66            case 'just_buyers':
67                $this->findJustBuyers($massSMS);
68                break;
69            case 'just_sellers':
70                $this->findJustSellers($massSMS);
71                break;
72            case 'recent_buyers':
73                $this->findRecentBuyers($massSMS);
74                break;
75            case 'recent_sellers':
76                $this->findRecentSellers($massSMS);
77                break;
78            case 'recent_both':
79                $this->findRecentBoth($massSMS);
80                break;
81            case 'full_list':
82                $this->findAll($massSMS);
83                break;
84            case 'full_store_group':
85                $this->findAllStoreGroup($massSMS);
86                break;
87        }
88    }
89
90    public function findJustBuyers(LoyaltyMassSMS $massSMS) {
91        try {
92            $stmt = $this->globalDB->prepare("SELECT DISTINCT(loyaltyID), optValue FROM loyaltyData JOIN loyaltyCustomers ON loyaltyData.loyaltyID = loyaltyCustomers.id WHERE loyaltyCustomers.optValue = 1 AND loyaltyData.type IN(1,2) AND loyaltyData.typeNum = :typeNum");
93            $stmt->bindValue(":typeNum", $massSMS->typeNum);
94            if($stmt->execute()) {
95                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
96                    $this->customer_list_array[] = $row['loyaltyID'];
97                }
98                return true;
99            }
100        } catch (\PDOException $e) {
101            $this->log->LogError($e->errorInfo);
102        }
103        return false;
104    }
105    public function findJustSellers(LoyaltyMassSMS $massSMS) {
106        try {
107            $stmt = $this->globalDB->prepare("SELECT DISTINCT(loyaltyID), optValue FROM loyaltyData JOIN loyaltyCustomers ON loyaltyData.loyaltyID = loyaltyCustomers.id WHERE loyaltyCustomers.optValue = 1 AND loyaltyData.type = 3 AND loyaltyData.typeNum = :typeNum");
108            $stmt->bindValue(":typeNum", $massSMS->typeNum);
109            if($stmt->execute()) {
110                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
111                    $this->customer_list_array[] = $row['loyaltyID'];
112                }
113                return true;
114            }
115        } catch (\PDOException $e) {
116            $this->log->LogError($e->errorInfo);
117        }
118        return false;
119    }
120    public function findRecentBuyers(LoyaltyMassSMS $massSMS) {
121        $this->log->LogDebug("Find Recent Buyers");
122
123        try {
124            $date = new \DateTime("now", new \DateTimeZone('utc'));
125            $date->sub(new \DateInterval('P'.$massSMS->lastDays.'D'));
126            $this->log->LogDebug("Date: ".$date->format("Y-m-d")." 05:01:00");
127            $stmt = $this->globalDB->prepare("SELECT DISTINCT(loyaltyID), optValue FROM loyaltyData JOIN loyaltyCustomers ON loyaltyData.loyaltyID = loyaltyCustomers.id WHERE loyaltyCustomers.optValue = 1 AND loyaltyData.type IN(1,2) AND loyaltyData.typeNum = :typeNum AND loyaltyData.date BETWEEN :dateStart AND CURRENT_TIMESTAMP()");
128            $stmt->bindValue(":typeNum", $massSMS->typeNum);
129            $stmt->bindValue(":dateStart", $date->format("Y-m-d")." 05:01:00");
130            if($stmt->execute()) {
131                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
132                    $this->log->LogDebug("LoyaltyID: ".$row['loyaltyID']);
133                    $this->customer_list_array[] = $row['loyaltyID'];
134                }
135                return true;
136            }
137        } catch (\PDOException $e) {
138            $this->log->LogError($e->errorInfo);
139        }
140        return false;
141    }
142    public function findRecentSellers(LoyaltyMassSMS $massSMS) {
143        try {
144            $date = new \DateTime("now", new \DateTimeZone('utc'));
145            $date->sub(new \DateInterval('P'.$massSMS->lastDays.'D'));
146            $stmt = $this->globalDB->prepare("SELECT DISTINCT(loyaltyID), optValue FROM loyaltyData JOIN loyaltyCustomers ON loyaltyData.loyaltyID = loyaltyCustomers.id WHERE loyaltyCustomers.optValue = 1 AND loyaltyData.type = 3 AND loyaltyData.typeNum = :typeNum AND loyaltyData.date BETWEEN :dateStart AND CURRENT_TIMESTAMP()");
147            $stmt->bindValue(":typeNum", $massSMS->typeNum);
148            $stmt->bindValue(":dateStart", $date->format("Y-m-d H:i:s"));
149            if($stmt->execute()) {
150                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
151                    $this->customer_list_array[] = $row['loyaltyID'];
152                }
153                return true;
154            }
155        } catch (\PDOException $e) {
156            $this->log->LogError($e->errorInfo);
157        }
158        return false;
159    }
160    public function findRecentBoth(LoyaltyMassSMS $massSMS) {
161        try {
162            $date = new \DateTime("now", new \DateTimeZone('utc'));
163            $date->sub(new \DateInterval('P'.$massSMS->lastDays.'D'));
164            $stmt = $this->globalDB->prepare("SELECT DISTINCT(loyaltyID), optValue FROM loyaltyData JOIN loyaltyCustomers ON loyaltyData.loyaltyID = loyaltyCustomers.id WHERE loyaltyCustomers.optValue = 1 AND loyaltyData.type IN(1,2,3) AND loyaltyData.typeNum = :typeNum AND loyaltyData.date BETWEEN :dateStart AND CURRENT_TIMESTAMP()");
165            $stmt->bindValue(":typeNum", $massSMS->typeNum);
166            $stmt->bindValue(":dateStart", $date->format("Y-m-d H:i:s"));
167            if($stmt->execute()) {
168                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
169                    $this->customer_list_array[] = $row['loyaltyID'];
170                }
171                return true;
172            }
173        } catch (\PDOException $e) {
174            $this->log->LogError($e->errorInfo);
175        }
176        return false;
177    }
178    public function findAll(LoyaltyMassSMS $massSMS) {
179        try {
180            $this->log->LogDebug("TypeNum: ".$massSMS->typeNum);
181            $stmt = $this->globalDB->prepare("SELECT DISTINCT(loyaltyID), optValue FROM loyaltyData JOIN loyaltyCustomers ON loyaltyData.loyaltyID = loyaltyCustomers.id WHERE loyaltyCustomers.optValue = 1 AND loyaltyData.type IN(1,2,3) AND loyaltyData.typeNum = :typeNum");
182            $stmt->bindValue(":typeNum", $massSMS->typeNum);
183            if($stmt->execute()) {
184                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
185                    $this->customer_list_array[] = $row['loyaltyID'];
186                }
187                //$this->log->LogDebug("Customer Count: ".count($this->customer_list_array));
188                return true;
189            }
190        } catch (\PDOException $e) {
191            $this->log->LogError($e->errorInfo);
192        }
193        return false;
194    }
195    public function findAllStoreGroup(LoyaltyMassSMS $massSMS) {
196        try {
197            $stmt = $this->globalDB->prepare("SELECT DISTINCT( id ) as loyaltyID, optValue FROM loyaltyCustomers WHERE optValue = 1  AND storeGroup = :storeGroup");
198            $stmt->bindValue(":storeGroup", $massSMS->groupID);
199            if($stmt->execute()) {
200                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
201                    $this->customer_list_array[] = $row['loyaltyID'];
202                }
203                //$this->log->LogDebug("Customer Count: ".count($this->customer_list_array));
204                return true;
205            }
206        } catch (\PDOException $e) {
207            $this->log->LogError($e->errorInfo);
208        }
209        return false;
210    }
211    public function findCustomerCount($type = "full_list", $lastDays = 0, $typeNum) {
212        $count = 0;
213        switch ($type) {
214            case 'just_buyers':
215                $stmt = $this->globalDB->prepare("SELECT count(DISTINCT(loyaltyID)) as totalCustomers FROM loyaltyData JOIN loyaltyCustomers ON loyaltyData.loyaltyID = loyaltyCustomers.id WHERE loyaltyCustomers.optValue = 1 AND loyaltyData.type IN(1,2) AND loyaltyData.typeNum = :typeNum");
216                $stmt->bindValue(":typeNum", $typeNum);
217                if($stmt->execute()) {
218                    $row = $stmt->fetch(\PDO::FETCH_ASSOC);
219                    $count = (int)($row['totalCustomers']);
220                }
221                break;
222            case 'just_sellers':
223                $stmt = $this->globalDB->prepare("SELECT count(DISTINCT(loyaltyID)) as totalCustomers FROM loyaltyData JOIN loyaltyCustomers ON loyaltyData.loyaltyID = loyaltyCustomers.id WHERE loyaltyCustomers.optValue = 1 AND loyaltyData.type = 3 AND loyaltyData.typeNum = :typeNum");
224                $stmt->bindValue(":typeNum", $typeNum);
225                if($stmt->execute()) {
226                    $row = $stmt->fetch(\PDO::FETCH_ASSOC);
227                    $count = (int)($row['totalCustomers']);
228                }
229                break;
230            case 'recent_buyers':
231                $date = new \DateTime("now", new \DateTimeZone('utc'));
232                $date->sub(new \DateInterval('P'.$lastDays.'D'));
233                $stmt = $this->globalDB->prepare("SELECT count(DISTINCT(loyaltyID)) as totalCustomers FROM loyaltyData JOIN loyaltyCustomers ON loyaltyData.loyaltyID = loyaltyCustomers.id WHERE loyaltyCustomers.optValue = 1 AND loyaltyData.type IN(1,2) AND loyaltyData.typeNum = :typeNum AND loyaltyData.date BETWEEN :dateStart AND CURRENT_TIMESTAMP()");
234                $stmt->bindValue(":typeNum", $typeNum);
235                $stmt->bindValue(":dateStart", $date->format("Y-m-d H:i:s"));
236                if($stmt->execute()) {
237                    $row = $stmt->fetch(\PDO::FETCH_ASSOC);
238                    $count = (int)($row['totalCustomers']);
239                }
240                break;
241            case 'recent_sellers':
242                $date = new \DateTime("now", new \DateTimeZone('utc'));
243                $date->sub(new \DateInterval('P'.$lastDays.'D'));
244                $stmt = $this->globalDB->prepare("SELECT count(DISTINCT(loyaltyID)) as totalCustomers FROM loyaltyData JOIN loyaltyCustomers ON loyaltyData.loyaltyID = loyaltyCustomers.id WHERE loyaltyCustomers.optValue = 1 AND loyaltyData.type = 3 AND loyaltyData.typeNum = :typeNum AND loyaltyData.date BETWEEN :dateStart AND CURRENT_TIMESTAMP()");
245                $stmt->bindValue(":typeNum", $typeNum);
246                $stmt->bindValue(":dateStart", $date->format("Y-m-d H:i:s"));
247                if($stmt->execute()) {
248                    $row = $stmt->fetch(\PDO::FETCH_ASSOC);
249                    $count = (int)($row['totalCustomers']);
250                }
251                break;
252            case 'recent_both':
253                $date = new \DateTime("now", new \DateTimeZone('utc'));
254                $date->sub(new \DateInterval('P'.$lastDays.'D'));
255                $stmt = $this->globalDB->prepare("SELECT count(DISTINCT(loyaltyID)) as totalCustomers FROM loyaltyData JOIN loyaltyCustomers ON loyaltyData.loyaltyID = loyaltyCustomers.id WHERE loyaltyCustomers.optValue = 1 AND loyaltyData.type IN(1,2,3) AND loyaltyData.typeNum = :typeNum AND loyaltyData.date BETWEEN :dateStart AND CURRENT_TIMESTAMP()");
256                $stmt->bindValue(":typeNum", $typeNum);
257                $stmt->bindValue(":dateStart", $date->format("Y-m-d H:i:s"));
258                if($stmt->execute()) {
259                    $row = $stmt->fetch(\PDO::FETCH_ASSOC);
260                    $count = (int)($row['totalCustomers']);
261                }
262                break;
263            case 'full_list':
264                $stmt = $this->globalDB->prepare("SELECT count(DISTINCT(loyaltyID)) as totalCustomers FROM loyaltyData JOIN loyaltyCustomers ON loyaltyData.loyaltyID = loyaltyCustomers.id WHERE loyaltyCustomers.optValue = 1 AND loyaltyData.type IN(1,2,3) AND loyaltyData.typeNum = :typeNum");
265                $stmt->bindValue(":typeNum", $typeNum);
266                if($stmt->execute()) {
267                    $row = $stmt->fetch(\PDO::FETCH_ASSOC);
268                    $count = (int)($row['totalCustomers']);
269                }
270                break;
271            case 'full_store_group':
272                $store = new Store();
273                $store->createStore($typeNum);
274                $storeGroup = $store->getLoyaltyGroup()->getGroupID();
275                $stmt = $this->globalDB->prepare("SELECT count(DISTINCT( id )) as totalCustomers FROM loyaltyCustomers WHERE optValue = 1  AND storeGroup = :storeGroup");
276                $stmt->bindValue(":storeGroup",$storeGroup);
277                if($stmt->execute()) {
278                    $row = $stmt->fetch(\PDO::FETCH_ASSOC);
279                    $count = (int)($row['totalCustomers']);
280                }
281                break;
282        }
283        return $count;
284    }
285
286    public function createOutgoingSMS(LoyaltyMassSMS $massSMS, LoyaltyCustomer $customer) {
287        $outgoing = new LoyaltyOutgoing($this->globalDB,null,$this->log);
288
289        $store = new Store();
290        $store->createStore($massSMS->typeNum);
291
292        //Setup the Message
293        $loyaltyMessage = new LoyaltySMSMessage($massSMS->groupID, $massSMS->typeNum, $massSMS->messageID);
294
295        switch (substr($massSMS->typeNum, 0, 2)) {
296            case 'cm':
297                $company = "Clothes Mentor";
298                break;
299            case 'pc':
300                $company = "Plato's Closet";
301                break;
302            case 'ou':
303                $company = "Once Upon a Child";
304                break;
305            case 'se':
306                $company = "Style Encore";
307                break;
308            case 'pa':
309                $company = "Play It Again Sports";
310                break;
311            default:
312                $company = "BuyerKiosk";
313                break;
314        }
315
316        $vars = array(
317            "%company%" => $company,
318            "%customer%" => $customer->getFirstName(),
319            "&nbsp;" => " ",
320            "%new_line%" => "\n",
321            "%coop%" => "Plato's Closet DFW",
322            "%store%" => $store->getCity()
323        );
324
325        //If there is a coupon attached, lets get that and generate a UUID
326        $couponURL = "";
327        if($massSMS->couponID !== null) {
328            $coupon = new LoyaltyCoupon($massSMS->groupID, $massSMS->typeNum,$massSMS->couponID);
329            $couponUUID = $coupon->createUUID($customer->loyaltyID, $massSMS->groupID, $coupon->getExpireDate());
330            $couponURL = 'http://'.serverName."/c/".$couponUUID;
331            $outgoing->couponUUID = $couponUUID;
332            $outgoing->couponID = $coupon->couponID;
333
334            $vars['%coupon_url%'] = $couponURL;
335            $vars['%coupon_expire'] = $coupon->getExpireDate();
336        }
337
338        $outgoing->setCustPhone($customer->phone);
339
340        //Generate the timeframe
341        if($massSMS->timeFrame == 0) {
342            $timeFrame = rand(1,6);
343        } else {
344            $timeFrame = $massSMS->timeFrame;
345        }
346        switch($timeFrame) {
347            case 1:
348                $time = "10:00:00";
349                break;
350            case 2:
351                $time = "12:00:00";
352                break;
353            case 3:
354                $time = "14:00:00";
355                break;
356            case 4:
357                $time = "16:00:00";
358                break;
359            case 5:
360                $time = "18:00:00";
361                break;
362            case 6:
363                $time = "20:00:00";
364                break;
365            default:
366                $time = "10:00:00";
367                break;
368        }
369        $now = \DateTime::createFromFormat("H:i:s", $time, new \DateTimeZone($store->getTimeZone()));
370        $timeUTC = $now->setTimezone(new \DateTimeZone('utc'));
371        $timeUTC = $timeUTC->format("Y-m-d H:i:s");
372
373        $outgoing->timeFrame = $timeUTC;
374
375        $message = $this->filterMessage($loyaltyMessage->message, $massSMS->typeNum, $vars);
376
377        $outgoing->message = $message;
378        $outgoing->storeFrom = $massSMS->typeNum;
379        $outgoing->storeGroupFrom = $massSMS->groupID;
380        $outgoing->loyaltyCustomerID = $customer->loyaltyID;
381        $outgoing->massSMSID = $massSMS->id;
382
383        //$outgoing->logVariables();
384
385        return $outgoing->create();
386    }
387
388    /**
389     * @return \KLogger
390     */
391    public function getLog()
392    {
393        return $this->log;
394    }
395
396    /**
397     * @param \KLogger $log
398     */
399    public function setLog($log)
400    {
401        $this->log = $log;
402    }
403
404
405}