Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 94 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| LoyaltyUsageReport | |
0.00% |
0 / 94 |
|
0.00% |
0 / 8 |
650 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getGroupSales | |
0.00% |
0 / 40 |
|
0.00% |
0 / 1 |
90 | |||
| getStoreSalesByDay | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
56 | |||
| getDateStart | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setDateStart | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getDateEnd | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setDateEnd | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| createTypeNumArrayString | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\Core\Loyalty; |
| 4 | |
| 5 | use BuyerKiosk\Core\Store; |
| 6 | |
| 7 | class LoyaltyUsageReport extends LoyaltyGroup { |
| 8 | |
| 9 | /** @var \DateTime $dateStart */ |
| 10 | /** @var \DateTime $dateEnd */ |
| 11 | public $dateStart; |
| 12 | public $dateEnd; |
| 13 | public $errors; |
| 14 | |
| 15 | private $db; |
| 16 | |
| 17 | private $log; |
| 18 | |
| 19 | private $typeNumArray; |
| 20 | |
| 21 | |
| 22 | public function __construct($groupID = null, $typeNum = null) |
| 23 | { |
| 24 | parent::__construct($groupID, $typeNum); |
| 25 | $this->errors = []; |
| 26 | $this->db = parent::getDb(); |
| 27 | /** @var Store $store */ |
| 28 | foreach($this->storesArray as $store) { |
| 29 | $this->typeNumArray[] = $store->getTypeNum(); |
| 30 | } |
| 31 | $this->log = new \KLogger($_ENV['LOG_DIR']."dev_log.txt", \KLogger::DEBUG); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | public function getGroupSales() |
| 36 | { |
| 37 | if ($this->dateStart instanceof \DateTime && $this->dateEnd instanceof \DateTime) { |
| 38 | try { |
| 39 | $totalSales = 0; |
| 40 | $totalRedeemed = 0; |
| 41 | $totalDiscounts = 0; |
| 42 | $statArray = []; |
| 43 | |
| 44 | //This query totals the totalSales, totalRedeemedPoints, and totalDiscounts and groups them by each store for the entire co-op. |
| 45 | $storeArrayString = self::createTypeNumArrayString($this->typeNumArray); |
| 46 | $stmt = $this->db->prepare("SELECT |
| 47 | ROUND( SUM( IF ( type = 1, FLOOR(salesTotal), 0 ) ) - IF ( type = 5, FLOOR(subSalesTotal), 0 ), 2 ) AS totalSales, |
| 48 | ROUND( SUM( IF ( type = 2, redeemedPoints, 0 ) ) ) AS totalRedeemedPoints, |
| 49 | ROUND( SUM( IF ( type = 2, discountReceived, 0 ) ),2 ) AS totalDiscounts, |
| 50 | typeNum |
| 51 | FROM |
| 52 | ( |
| 53 | SELECT |
| 54 | typeNum, |
| 55 | salesTotal, |
| 56 | redeemedPoints, |
| 57 | subSalesTotal, |
| 58 | discountReceived, |
| 59 | type |
| 60 | FROM |
| 61 | loyaltyData |
| 62 | WHERE |
| 63 | typeNum IN (".$storeArrayString.") AND date BETWEEN :dateStart AND :dateEnd |
| 64 | ) AS A |
| 65 | GROUP BY |
| 66 | typeNum"); |
| 67 | |
| 68 | $stmt->bindValue(":dateStart", $this->dateStart->format("Y-m-d H:i:s")); |
| 69 | $stmt->bindValue(":dateEnd", $this->dateEnd->format("Y-m-d H:i:s")); |
| 70 | if ($stmt->execute()) { |
| 71 | while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
| 72 | //We need to convert our typeNum into a Store variable so we have more information about the store |
| 73 | $tempStore = new Store(); |
| 74 | $tempStore->createStore($row['typeNum']); |
| 75 | |
| 76 | //Add City, State and Store Number to our Array |
| 77 | $row['city'] = $tempStore->getCity(); |
| 78 | $row['state'] = $tempStore->getState(); |
| 79 | $row['storeNum'] = $tempStore->getStoreNum(); |
| 80 | |
| 81 | $row['totalSales'] = floatval($row['totalSales']); |
| 82 | $row['totalDiscounts'] = floatval($row['totalDiscounts']); |
| 83 | $row['totalRedeemedPoints'] = (int)$row['totalRedeemedPoints']; |
| 84 | if ($row['totalDiscounts'] > 0 && $row['totalSales'] > 0) { |
| 85 | $row['discountsBySales'] = round($row['totalDiscounts']/ $row['totalSales'], 3) * 100; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | //Add our numbers to the totals |
| 90 | $totalSales += $row['totalSales']; |
| 91 | $totalDiscounts += $row['totalDiscounts']; |
| 92 | $totalRedeemed += $row['totalRedeemedPoints']; |
| 93 | |
| 94 | //Add our store's data to our stat array |
| 95 | $statArray['stores'][] = $row; |
| 96 | } |
| 97 | |
| 98 | // Add our totals to the array |
| 99 | $statArray['totals']['totalSales'] = $totalSales; |
| 100 | $statArray['totals']['totalRedeemed'] = $totalRedeemed; |
| 101 | $statArray['totals']['totalDiscounts'] = $totalDiscounts; |
| 102 | |
| 103 | foreach ($statArray['stores'] as $key => $value) { |
| 104 | $statArray['stores'][$key]['totalSalesPercent'] = round($value['totalSales'] / $statArray['totals']['totalSales'],4)*100; |
| 105 | $statArray['stores'][$key]['totalDiscountPercent'] = round($value['totalDiscounts'] / $statArray['totals']['totalDiscounts'],4)*100; |
| 106 | $statArray['stores'][$key]['totalRedeemedPercent'] = round($value['totalRedeemedPoints'] / $statArray['totals']['totalRedeemed'],4)*100; |
| 107 | } |
| 108 | |
| 109 | // Return our stats array which now has each individual store's breakdown as well and totals |
| 110 | return $statArray; |
| 111 | } else { |
| 112 | $this->errors[] = "Query Didn't Work"; |
| 113 | } |
| 114 | } catch (\PDOException $e) { |
| 115 | $this->log->LogError($e->getMessage()); |
| 116 | } |
| 117 | } else { |
| 118 | $this->errors[] = "Dates Not Set"; |
| 119 | } |
| 120 | return null; |
| 121 | } |
| 122 | |
| 123 | public function getStoreSalesByDay($typeNum) { |
| 124 | |
| 125 | $store = new Store(); |
| 126 | $store->createStore($typeNum); |
| 127 | if ($this->dateStart instanceof \DateTime && $this->dateEnd instanceof \DateTime && $store instanceof Store) { |
| 128 | try { |
| 129 | $totalSales = 0; |
| 130 | $totalRedeemed = 0; |
| 131 | $totalDiscounts = 0; |
| 132 | $statArray = []; |
| 133 | |
| 134 | $stmt = $this->db->prepare("SELECT |
| 135 | ROUND( SUM( IF ( type = 1, salesTotal, 0 ) ) - IF ( type = 5, subSalesTotal, 0 ), 2 ) AS totalSales, |
| 136 | ROUND( SUM( IF ( type = 2, redeemedPoints, 0 ) ) ) AS totalRedeemedPoints, |
| 137 | ROUND( SUM( IF ( type = 2, discountReceived, 0 ) ),2 ) AS totalDiscounts, |
| 138 | date |
| 139 | FROM |
| 140 | ( |
| 141 | SELECT |
| 142 | typeNum, |
| 143 | salesTotal, |
| 144 | redeemedPoints, |
| 145 | subSalesTotal, |
| 146 | discountReceived, |
| 147 | type, |
| 148 | date |
| 149 | FROM |
| 150 | loyaltyData |
| 151 | WHERE |
| 152 | typeNum = :typeNum AND date BETWEEN :dateStart AND :dateEnd |
| 153 | ) AS A |
| 154 | GROUP BY |
| 155 | DATE(date) ORDER BY date DESC"); |
| 156 | $this->log->LogDebug($this->dateStart->format("Y-m-d H:i:s")); |
| 157 | $this->log->LogDebug($this->dateEnd->format("Y-m-d H:i:s")); |
| 158 | $this->log->LogDebug($store->getTypeNum()); |
| 159 | $stmt->bindValue(":dateStart", $this->dateStart->format("Y-m-d H:i:s")); |
| 160 | $stmt->bindValue(":dateEnd", $this->dateEnd->format("Y-m-d H:i:s")); |
| 161 | $stmt->bindValue(":typeNum", $store->getTypeNum()); |
| 162 | |
| 163 | if ($stmt->execute()) { |
| 164 | |
| 165 | while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
| 166 | $row['totalSales'] = floatval($row['totalSales']); |
| 167 | $row['totalDiscounts'] = floatval($row['totalDiscounts']); |
| 168 | $row['totalRedeemedPoints'] = (int)$row['totalRedeemedPoints']; |
| 169 | //Add our numbers to the totals |
| 170 | $totalSales += $row['totalSales']; |
| 171 | $totalDiscounts += $row['totalDiscounts']; |
| 172 | $totalRedeemed += $row['totalRedeemedPoints']; |
| 173 | |
| 174 | $statArray['dates'][] = $row; |
| 175 | } |
| 176 | // Add our totals to the array |
| 177 | $statArray['totals']['totalSales'] = $totalSales; |
| 178 | $statArray['totals']['totalRedeemed'] = $totalRedeemed; |
| 179 | $statArray['totals']['totalDiscounts'] = $totalDiscounts; |
| 180 | |
| 181 | return $statArray; |
| 182 | } else { |
| 183 | $this->errors[] = "Query Didn't Work"; |
| 184 | } |
| 185 | } catch (\PDOException $e) { |
| 186 | $this->log->LogError($e->getMessage()); |
| 187 | } |
| 188 | } else { |
| 189 | $this->errors[] = "Dates Not Set"; |
| 190 | } |
| 191 | return null; |
| 192 | } |
| 193 | |
| 194 | public function getDateStart() |
| 195 | { |
| 196 | return $this->dateStart; |
| 197 | } |
| 198 | |
| 199 | public function setDateStart($dateStart) |
| 200 | { |
| 201 | $date = new \DateTime($dateStart, new \DateTimeZone($this->getGroupTimeZone())); |
| 202 | $date->setTimezone(new \DateTimeZone('UTC')); |
| 203 | |
| 204 | $this->dateStart = $date; |
| 205 | } |
| 206 | |
| 207 | public function getDateEnd() |
| 208 | { |
| 209 | return $this->dateEnd; |
| 210 | } |
| 211 | |
| 212 | public function setDateEnd($dateEnd) |
| 213 | { |
| 214 | $date = new \DateTime($dateEnd, new \DateTimeZone($this->getGroupTimeZone())); |
| 215 | $date->setTimezone(new \DateTimeZone('UTC')); |
| 216 | |
| 217 | $this->dateEnd = $date; |
| 218 | |
| 219 | } |
| 220 | private static function createTypeNumArrayString($typeNumArray) { |
| 221 | $string = ""; $count = 0; |
| 222 | foreach($typeNumArray as $typeNum) { |
| 223 | if($count < count($typeNumArray)-1) { |
| 224 | $string .= "'".$typeNumArray[$count]."',"; |
| 225 | } else { |
| 226 | $string .= "'".$typeNumArray[$count]."'"; |
| 227 | } |
| 228 | $count++; |
| 229 | } |
| 230 | //error_log("String: ".$string); |
| 231 | return $string; |
| 232 | } |
| 233 | } |