Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 182
0.00% covered (danger)
0.00%
0 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
MassSMSRobot
0.00% covered (danger)
0.00%
0 / 182
0.00% covered (danger)
0.00%
0 / 13
3306
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
 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 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 getCustomerList
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
56
 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 / 12
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 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 createOutgoingSMS
0.00% covered (danger)
0.00%
0 / 71
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 MassSMSRobot 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        } else {
21            $this->log = $log;
22            $this->log->LogDebug("Logger Initialized");
23        }
24
25    }
26    public function findToday() {
27        $this->log->LogDebug("Find Today");
28        $date = getCurrentDateRange();
29        try {
30            $stmt = $this->globalDB->prepare("SELECT * FROM loyaltyMassSMS WHERE date = :date AND processed = 0");
31            $stmt->bindValue(":date", $date['dateStart']->format('Y-m-d'));
32            $stmt->execute();
33            return $stmt->fetchAll(\PDO::FETCH_ASSOC);
34        } catch (\PDOException $e) {
35            $this->log->LogError($e->errorInfo);
36        }
37        return null;
38    }
39    public function processToday() {
40        $todaysRecords = $this->findToday();
41        $this->log->LogDebug("Process Today");
42        foreach($todaysRecords as $massSMS) {
43            $massSMS = new LoyaltyMassSMS($massSMS['id'], $massSMS['storeGroup'], $massSMS['typeNum']);
44            $this->getCustomerList($massSMS);
45            $this->log->LogDebug("Started Working on [".$massSMS->id."]");
46            foreach($this->customer_list_array as $customer) {
47                $customer = new LoyaltyCustomer($customer);
48                if($this->createOutgoingSMS($massSMS, $customer)) {
49                    $this->log->LogDebug("Finished Processing [".$customer->loyaltyID."] - [".$massSMS->id."]");
50                }
51            }
52        }
53    }
54
55    public function getCustomerList(LoyaltyMassSMS $massSMS) {
56        switch ($massSMS->listType) {
57            case 'just_buyers':
58                $this->findJustBuyers($massSMS);
59                break;
60            case 'just_sellers':
61                $this->findJustSellers($massSMS);
62                break;
63            case 'recent_buyers':
64                $this->findRecentBuyers($massSMS);
65                break;
66            case 'recent_sellers':
67                $this->findRecentSellers($massSMS);
68                break;
69            case 'recent_both':
70                $this->findRecentBoth($massSMS);
71                break;
72            case 'full_list':
73                $this->findAll($massSMS);
74                break;
75        }
76    }
77
78    public function findJustBuyers(LoyaltyMassSMS $massSMS) {
79        try {
80            $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");
81            $stmt->bindValue(":typeNum", $massSMS->typeNum);
82            if($stmt->execute()) {
83                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
84                    $this->customer_list_array[] = $row['loyaltyID'];
85                }
86                return true;
87            }
88        } catch (\PDOException $e) {
89            $this->log->LogError($e->errorInfo);
90        }
91        return false;
92    }
93    public function findJustSellers(LoyaltyMassSMS $massSMS) {
94        try {
95            $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");
96            $stmt->bindValue(":typeNum", $massSMS->typeNum);
97            if($stmt->execute()) {
98                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
99                    $this->customer_list_array[] = $row['loyaltyID'];
100                }
101                return true;
102            }
103        } catch (\PDOException $e) {
104            $this->log->LogError($e->errorInfo);
105        }
106        return false;
107    }
108    public function findRecentBuyers(LoyaltyMassSMS $massSMS) {
109        try {
110            $date = new \DateTime("now", new \DateTimeZone('utc'));
111            $date->sub(new \DateInterval('P'.$massSMS->lastDays.'D'));
112            $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()");
113            $stmt->bindValue(":typeNum", $massSMS->typeNum);
114            $stmt->bindValue(":dateStart", $date->format("Y-m-d H:i:s"));
115            if($stmt->execute()) {
116                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
117                    $this->customer_list_array[] = $row['loyaltyID'];
118                }
119                return true;
120            }
121        } catch (\PDOException $e) {
122            $this->log->LogError($e->errorInfo);
123        }
124        return false;
125    }
126    public function findRecentSellers(LoyaltyMassSMS $massSMS) {
127        try {
128            $date = new \DateTime("now", new \DateTimeZone('utc'));
129            $date->sub(new \DateInterval('P'.$massSMS->lastDays.'D'));
130            $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()");
131            $stmt->bindValue(":typeNum", $massSMS->typeNum);
132            $stmt->bindValue(":dateStart", $date->format("Y-m-d H:i:s"));
133            if($stmt->execute()) {
134                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
135                    $this->customer_list_array[] = $row['loyaltyID'];
136                }
137                return true;
138            }
139        } catch (\PDOException $e) {
140            $this->log->LogError($e->errorInfo);
141        }
142        return false;
143    }
144    public function findRecentBoth(LoyaltyMassSMS $massSMS) {
145        try {
146            $date = new \DateTime("now", new \DateTimeZone('utc'));
147            $date->sub(new \DateInterval('P'.$massSMS->lastDays.'D'));
148            $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()");
149            $stmt->bindValue(":typeNum", $massSMS->typeNum);
150            $stmt->bindValue(":dateStart", $date->format("Y-m-d H:i:s"));
151            if($stmt->execute()) {
152                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
153                    $this->customer_list_array[] = $row['loyaltyID'];
154                }
155                return true;
156            }
157        } catch (\PDOException $e) {
158            $this->log->LogError($e->errorInfo);
159        }
160        return false;
161    }
162    public function findAll(LoyaltyMassSMS $massSMS) {
163        try {
164            $date = new \DateTime("now", new \DateTimeZone('utc'));
165            $date->sub(new \DateInterval('P'.$massSMS->lastDays.'D'));
166            $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");
167            $stmt->bindValue(":typeNum", $massSMS->typeNum);
168            $stmt->bindValue(":dateStart", $date->format("Y-m-d H:i:s"));
169            if($stmt->execute()) {
170                while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
171                    $this->customer_list_array[] = $row['loyaltyID'];
172                }
173                return true;
174            }
175        } catch (\PDOException $e) {
176            $this->log->LogError($e->errorInfo);
177        }
178        return false;
179    }
180
181    public function createOutgoingSMS(LoyaltyMassSMS $massSMS, LoyaltyCustomer $customer) {
182        $outgoing = new LoyaltyOutgoing($this->globalDB);
183
184        $store = new Store();
185        $store->createStore($massSMS->typeNum);
186
187        //Setup the Message
188        $loyaltyMessage = new LoyaltySMSMessage($massSMS->storeGroup, $massSMS->typeNum, $massSMS->messageID);
189
190        switch (substr($massSMS->typeNum, 0, 2)) {
191            case 'cm':
192                $company = "Clothes Mentor";
193                break;
194            case 'pc':
195                $company = "Plato's Closet";
196                break;
197            case 'ou':
198                $company = "Once Upon a Child";
199                break;
200            case 'se':
201                $company = "Style Encore";
202                break;
203            case 'pa':
204                $company = "Play It Again Sports";
205                break;
206            default:
207                $company = "BuyerKiosk";
208                break;
209        }
210
211        $vars = array(
212            "%company%" => $company,
213            "%customer%" => $customer->getFirstName(),
214            "&nbsp;" => " ",
215            "%new_line%" => "\n"
216        );
217
218        //If there is a coupon attached, lets get that and generate a UUID
219        $couponURL = "";
220        if($massSMS->couponID !== null) {
221            $coupon = new LoyaltyCoupon($massSMS->storeGroup, $massSMS->typeNum,$massSMS->couponID);
222            $couponUUID = $coupon->createUUID($customer->loyaltyID, $massSMS->storeGroup, $coupon->getExpireDate());
223            $couponURL = 'http://'.serverName."/c/".$couponUUID;
224            $outgoing->couponUUID = $couponUUID;
225            $outgoing->couponID = $coupon->couponID;
226
227            $vars['%coupon_url%'] = $couponURL;
228            $vars['%coupon_expire'] = $coupon->getExpireDate();
229        }
230
231        $outgoing->setCustPhone($customer->phone);
232
233        //Generate the timeframe
234        if($massSMS->timeFrame == 0) {
235            $timeFrame = rand(1,6);
236        } else {
237            $timeFrame = $massSMS->timeFrame;
238        }
239        switch($timeFrame) {
240            case 1:
241                $time = "10:00:00";
242                break;
243            case 2:
244                $time = "12:00:00";
245                break;
246            case 3:
247                $time = "14:00:00";
248                break;
249            case 4:
250                $time = "16:00:00";
251                break;
252            case 5:
253                $time = "18:00:00";
254                break;
255            case 6:
256                $time = "20:00:00";
257                break;
258            default:
259                $time = "10:00:00";
260                break;
261        }
262        $now = \DateTime::createFromFormat("H:i:s", $time, new \DateTimeZone($store->getTimeZone()));
263        $timeUTC = $now->setTimezone(new \DateTimeZone('utc'));
264        $timeUTC->format("Y:m:d H:i:s");
265
266        $outgoing->timeFrame = $timeUTC;
267
268        $message = $this->filterMessage($loyaltyMessage->message, $massSMS->typeNum, $vars);
269
270        $outgoing->message = $message;
271        $outgoing->storeFrom = $massSMS->typeNum;
272        $outgoing->storeGroupFrom = $massSMS->storeGroup;
273        $outgoing->loyaltyCustomerID = $customer->loyaltyID;
274
275        return $outgoing->create();
276    }
277
278    /**
279     * @return \KLogger
280     */
281    public function getLog()
282    {
283        return $this->log;
284    }
285
286    /**
287     * @param \KLogger $log
288     */
289    public function setLog($log)
290    {
291        $this->log = $log;
292    }
293
294
295}