Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 67 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| LoyaltyData | |
0.00% |
0 / 67 |
|
0.00% |
0 / 7 |
380 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getRewardPoints | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| redeemPoints | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
30 | |||
| addSalesData | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| addBuyVisit | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| subSalesData | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| subPoints | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\Core\Loyalty; |
| 4 | |
| 5 | use BuyerKiosk\Core\Store; |
| 6 | |
| 7 | class LoyaltyData extends LoyaltyCustomer { |
| 8 | |
| 9 | private $store; |
| 10 | |
| 11 | public function __construct($loyaltyID, $typeNum) |
| 12 | { |
| 13 | parent::__construct($loyaltyID); |
| 14 | $this->store = new Store(); |
| 15 | $this->store->createStore($typeNum); |
| 16 | } |
| 17 | //TODO: Add methods to grab single records |
| 18 | |
| 19 | public function getRewardPoints() { |
| 20 | /*$loyaltyConfig = $this->store->getLoyaltyConfig(); |
| 21 | $date = getCurrentDateRange(); |
| 22 | $expireDate = $date['dateEnd']->sub(new DateInterval('P'.$loyaltyConfig->expirePoints.'D')); |
| 23 | $salesTotal = 0; $redeemedPoints = 0; $subPoints = 0; $subSalesTotal = 0; $buyVisits = 0; $nonSaleTotal = 0; $addPoints = 0; |
| 24 | |
| 25 | //Get the total sales amount for this period and the amount of points that have already been redeemed |
| 26 | try { |
| 27 | $loyaltyID = $this->getLoyaltyID(); |
| 28 | $expireDateFormatted = $expireDate->format("Y-m-d H:i:s"); |
| 29 | $stmt = $this->db->prepare("SELECT * FROM loyaltyData WHERE `loyaltyID` = :loyaltyID AND `date` > :expireDate"); |
| 30 | $stmt->bindParam(":loyaltyID", $loyaltyID); |
| 31 | $stmt->bindParam(":expireDate", $expireDateFormatted); |
| 32 | if($stmt->execute()) { |
| 33 | while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
| 34 | //If type = 1 then we have a sales transaction where we need to add it to our sales total |
| 35 | if($row['type'] == 1) { |
| 36 | $salesTotal += floor(floatval($row['salesTotal'])); |
| 37 | //If type = 2 then we have a points redemption transaction so we need to add these points to the total points redeemed |
| 38 | } elseif($row['type'] == 2) { |
| 39 | $redeemedPoints += (int)$row['redeemedPoints']; |
| 40 | //If type = 3 then we have a buy visit |
| 41 | } elseif($row['type'] == 3) { |
| 42 | $buyVisits += 1; |
| 43 | //If type = 4 then we have a points deduction transaction so we need to remove these points from the total; |
| 44 | } elseif($row['type'] == 4) { |
| 45 | $subPoints += (int)$row['subPoints']; |
| 46 | //If type = 5 then we have a sales deduction transaction so we need to subtract this sales from the total; |
| 47 | } elseif($row['type'] == 5) { |
| 48 | $subSalesTotal += floatval($row['subSalesTotal']); |
| 49 | //If type = 6 then we have a non sales related point addition |
| 50 | } elseif($row['type'] == 6) { |
| 51 | $addPoints += floatval($row['addPoints']); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } catch (PDOException $e) { |
| 56 | |
| 57 | } |
| 58 | $rewardString = ""; |
| 59 | $offer1 = ""; |
| 60 | $offer2 = ""; |
| 61 | $rewardPoints = ((round($salesTotal) + round($nonSaleTotal)) - round($subSalesTotal)) * floatval($loyaltyConfig->d2p); |
| 62 | $remainingPoints = ($rewardPoints + $addPoints) - $redeemedPoints - $subPoints; |
| 63 | if($loyaltyConfig->type == 1) { |
| 64 | if($loyaltyConfig->d2p < 1) { |
| 65 | $d2p = floatval($loyaltyConfig->d2p * 10); |
| 66 | $dollar = 10; |
| 67 | } else { |
| 68 | $d2p = $loyaltyConfig->d2p; |
| 69 | $dollar = 1; |
| 70 | } |
| 71 | $discount = floor(floatval($loyaltyConfig->minToRedeem) / floatval($loyaltyConfig->p2d)); |
| 72 | $offer1 = "Earn ".$d2p." point(s) for every $".$dollar." bought or sold."; |
| 73 | $offer2 = "Accumulate ".$loyaltyConfig->minToRedeem." points for $".$discount." off"; |
| 74 | |
| 75 | $rewardModulus = $remainingPoints % (int)$loyaltyConfig->minToRedeem; |
| 76 | $nextReward = (int)$loyaltyConfig->minToRedeem - round($rewardModulus); |
| 77 | |
| 78 | $rewardString = "Only ".$nextReward." more points for your $".$discount." in rewards"; |
| 79 | |
| 80 | } else if($loyaltyConfig->type == 2) { |
| 81 | if($loyaltyConfig->d2p < 1) { |
| 82 | $d2p = floatval($loyaltyConfig->d2p) * 10; |
| 83 | $dollar = 10; |
| 84 | } else { |
| 85 | $d2p = $loyaltyConfig->d2p; |
| 86 | $dollar = 1; |
| 87 | } |
| 88 | $offer1 = "Earn ".$d2p." point(s) for every $".$dollar." bought or sold."; |
| 89 | $rewardModulus = $remainingPoints % (int)$loyaltyConfig->threshold; |
| 90 | $nextReward = (int)$loyaltyConfig->threshold - round($rewardModulus); |
| 91 | |
| 92 | if((int)$loyaltyConfig->percentOff > 0) { |
| 93 | $offer2 = "Accumulate ". $loyaltyConfig->threshold." points for ".$loyaltyConfig->percentOff."% off"; |
| 94 | $rewardString = "Only ".$nextReward." more points for your ".$loyaltyConfig->percentOff."% off coupon"; |
| 95 | } else { |
| 96 | $offer2 = "Accumulate ". $loyaltyConfig->threshold." points for $".$loyaltyConfig->dollarsOff." off"; |
| 97 | $rewardString = "Only ".$nextReward." more points for your $".$loyaltyConfig->dollarsOff." off coupon"; |
| 98 | } |
| 99 | } else if($loyaltyConfig->type == 3) { |
| 100 | if($loyaltyConfig->d2p < 1) { |
| 101 | $d2p = floatval($loyaltyConfig->d2p) * 10; |
| 102 | $dollar = 10; |
| 103 | } else { |
| 104 | $d2p = $loyaltyConfig->d2p; |
| 105 | $dollar = 1; |
| 106 | } |
| 107 | $offer1 = "Earn ".$d2p." point(s) for every $".$dollar." bought or sold."; |
| 108 | |
| 109 | } |
| 110 | |
| 111 | |
| 112 | return array('rewardPoints' => $rewardPoints, 'buyVisits' => $buyVisits, 'salesTotal' => $salesTotal, "redeemedPoints" => $redeemedPoints, "remainingPoints" => $remainingPoints, "discountMessages" => array("nextReward" => $rewardString, "offer1" => $offer1, "offer2" => $offer2)); |
| 113 | */ |
| 114 | return null; |
| 115 | } |
| 116 | public function redeemPoints($points, $salesAmount, $discount) { |
| 117 | //Get how many reward points the customer can spend |
| 118 | $rewardPoints = $this->getRewardPoints(); |
| 119 | $rewardPoints = (int)$rewardPoints['remainingPoints']; |
| 120 | |
| 121 | $loyaltyConfig = $this->store->getLoyaltyConfig(); |
| 122 | |
| 123 | //Check to see if customer has enough reward points to redeem this many |
| 124 | if($rewardPoints < $points) { |
| 125 | return array("status" => "Not enough reward points"); |
| 126 | } |
| 127 | |
| 128 | //Check if this amount of points meets the minimum set by the store |
| 129 | if($points < $loyaltyConfig->minToRedeem) { |
| 130 | return array("status" => "Does not meet minimum to redeem"); |
| 131 | } |
| 132 | |
| 133 | //Let's insert the record |
| 134 | |
| 135 | try { |
| 136 | $typeNum = $this->store->getTypeNum(); |
| 137 | $loyaltyID = $this->getLoyaltyID(); |
| 138 | $stmt = $this->db->prepare("INSERT INTO loyaltyData (type, typeNum, loyaltyID, date, redeemedPoints, salesTotal, discountReceived) VALUES (2, :typeNum, :loyaltyID, CURRENT_TIMESTAMP , :redeemedPoints, :salesAmount, :discount)"); |
| 139 | $stmt->bindParam(":typeNum", $typeNum); |
| 140 | $stmt->bindParam(":loyaltyID", $loyaltyID); |
| 141 | $stmt->bindParam(":redeemedPoints", $points); |
| 142 | $stmt->bindParam(":salesAmount", $salesAmount); |
| 143 | $stmt->bindParam(":discount", $discount); |
| 144 | if(!$stmt->execute()) { |
| 145 | return array("status" => "Could not insert record"); |
| 146 | } |
| 147 | } catch (\PDOException $e) { |
| 148 | return array("status" => $e->getMessage()); |
| 149 | } |
| 150 | |
| 151 | //If we got this far everything worked |
| 152 | return array("status" => "success"); |
| 153 | } |
| 154 | public function addSalesData($salesTotal, $items) { |
| 155 | if(isCurrency($salesTotal)) { |
| 156 | $stmt = $this->db->prepare("INSERT INTO loyaltyData (loyaltyID, date, typeNum, storeGroup, type, salesTotal, items) VALUES (:loyaltyID, CURRENT_TIMESTAMP, :typeNum, :storeGroup, 1, :salesTotal, :items)"); |
| 157 | $stmt->bindValue(":loyaltyID", $this->loyaltyID); |
| 158 | $stmt->bindValue(":typeNum", $this->store->getTypeNum()); |
| 159 | $stmt->bindValue(":storeGroup", $this->getStoreGroup()); |
| 160 | $stmt->bindValue(":salesTotal", $salesTotal); |
| 161 | $stmt->bindValue(":items", $items); |
| 162 | if($stmt->execute()) { |
| 163 | return true; |
| 164 | } |
| 165 | } |
| 166 | return false; |
| 167 | } |
| 168 | public function addBuyVisit() { |
| 169 | $stmt = $this->db->prepare("INSERT INTO loyaltyData (loyaltyID, date, typeNum, storeGroup, type) VALUES (:loyaltyID, CURRENT_TIMESTAMP, :typeNum, :storeGroup, 3)"); |
| 170 | $stmt->bindValue(":loyaltyID", $this->loyaltyID); |
| 171 | $stmt->bindValue(":typeNum", $this->store->getTypeNum()); |
| 172 | $stmt->bindValue(":storeGroup", $this->getStoreGroup()); |
| 173 | if($stmt->execute()) { |
| 174 | return true; |
| 175 | } |
| 176 | return false; |
| 177 | } |
| 178 | public function subSalesData($salesTotal, $items) { |
| 179 | if(isCurrency($salesTotal)) { |
| 180 | $stmt = $this->db->prepare("INSERT INTO loyaltyData (loyaltyID, date, typeNum, storeGroup, type, subSalesTotal, items) VALUES (:loyaltyID, CURRENT_TIMESTAMP, :typeNum, :storeGroup, 5, :salesTotal, :items)"); |
| 181 | $stmt->bindValue(":loyaltyID", $this->loyaltyID); |
| 182 | $stmt->bindValue(":typeNum", $this->store->getTypeNum()); |
| 183 | $stmt->bindValue(":storeGroup", $this->getStoreGroup()); |
| 184 | $stmt->bindValue(":salesTotal", $salesTotal); |
| 185 | $stmt->bindValue(":items", $items); |
| 186 | if($stmt->execute()) { |
| 187 | return true; |
| 188 | } |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | public function subPoints($points) { |
| 193 | //Get how many reward points the customer can spend |
| 194 | $rewardPoints = $this->getRewardPoints(); |
| 195 | $rewardPoints = (int)$rewardPoints['remainingPoints']; |
| 196 | |
| 197 | $loyaltyConfig = $this->store->getLoyaltyConfig(); |
| 198 | |
| 199 | //Check to see if customer has enough reward points to redeem this many |
| 200 | if($rewardPoints < $points) { |
| 201 | return array("status" => "Not enough reward points"); |
| 202 | } |
| 203 | //Let's insert the record |
| 204 | |
| 205 | try { |
| 206 | $typeNum = $this->store->getTypeNum(); |
| 207 | $loyaltyID = $this->getLoyaltyID(); |
| 208 | $stmt = $this->db->prepare("INSERT INTO loyaltyData (type, typeNum, loyaltyID, date, subPoints) VALUES (4, :typeNum, :loyaltyID, CURRENT_TIMESTAMP , :subPoints)"); |
| 209 | $stmt->bindParam(":typeNum", $typeNum); |
| 210 | $stmt->bindParam(":loyaltyID", $loyaltyID); |
| 211 | $stmt->bindParam(":subPoints", $points); |
| 212 | if(!$stmt->execute()) { |
| 213 | return array("status" => "Could not insert record"); |
| 214 | } |
| 215 | } catch (\PDOException $e) { |
| 216 | return array("status" => $e->getMessage()); |
| 217 | } |
| 218 | //If we got this far everything worked |
| 219 | return array("status" => "success"); |
| 220 | } |
| 221 | } |