Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
17.11% |
13 / 76 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
| FinancialsController | |
17.11% |
13 / 76 |
|
33.33% |
2 / 6 |
578.40 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| getLaborTotals | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| getLaborTotalsFromCache | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getLaborTotalsFromAPI | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
240 | |||
| getLaborHoursArray | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
90 | |||
| getCurrentData | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\WhenIWork\Controllers; |
| 4 | |
| 5 | use BuyerKiosk\WhenIWork\Employee; |
| 6 | |
| 7 | class FinancialsController extends \BuyerKiosk\Core\Controllers\BaseController { |
| 8 | private $store; |
| 9 | private $wiw; |
| 10 | private $date; |
| 11 | private $employeeIDArray; |
| 12 | public $isFromCache; |
| 13 | private $key; |
| 14 | private $predis; |
| 15 | public $employeeDataArray; |
| 16 | public function __construct($app, \Store $store) |
| 17 | { |
| 18 | parent::__construct($app); |
| 19 | $this->store = $store; |
| 20 | $this->wiw = new \Wheniwork($this->store->getWiwToken()); |
| 21 | $this->date = new \DateTime('now', new \DateTimeZone($this->store->getTimeZone())); |
| 22 | $this->predis = new \Predis\Client($_ENV['REDIS_URL']); |
| 23 | $this->key = $store->getTypeNum()."_laborTotals"; |
| 24 | $this->isFromCache = 0; |
| 25 | } |
| 26 | public function getLaborTotals(){ |
| 27 | if($this->predis->exists($this->key)) { |
| 28 | return $this->getLaborTotalsFromCache(); |
| 29 | } |
| 30 | return $this->getLaborTotalsFromAPI(); |
| 31 | |
| 32 | } |
| 33 | public function getLaborTotalsFromCache() { |
| 34 | $this->isFromCache = 1; |
| 35 | $result = json_decode($this->predis->get($this->key), true); |
| 36 | $this->employeeDataArray = $result['employeeData']; |
| 37 | return $result; |
| 38 | } |
| 39 | |
| 40 | public function getLaborTotalsFromAPI() { |
| 41 | //Get Total Labor Hours |
| 42 | $array = $this->getLaborHoursArray(); |
| 43 | $current = $this->getCurrentData(); |
| 44 | $totalDollars = 0; |
| 45 | $totalHours = 0; |
| 46 | if (is_array($array)) { |
| 47 | foreach($array as $item) { |
| 48 | if (is_array($item) && isset($item['value']) && isset($item['hr'])) { |
| 49 | $totalDollars += $item['value']; |
| 50 | $totalHours += $item['hr']; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | $laborPercent = 0; |
| 55 | if(is_array($current) && isset($current['salesCurrent']) && (int)$current['salesCurrent'] > 0) { |
| 56 | $laborPercent = $totalDollars / (int)$current['salesCurrent']; |
| 57 | } |
| 58 | $salesPerLaborHour = 0; |
| 59 | if($totalHours > 0 && is_array($current) && isset($current['salesCurrent'])) { |
| 60 | $salesPerLaborHour = (int)$current['salesCurrent'] / $totalHours; |
| 61 | } |
| 62 | if (class_exists('NumberFormatter')) { |
| 63 | $percent = new \NumberFormatter("en", \NumberFormatter::PERCENT); |
| 64 | $decimal = new \NumberFormatter("en", \NumberFormatter::DECIMAL); |
| 65 | $decimal->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 2); |
| 66 | $formattedLaborPercent = $percent->format($laborPercent); |
| 67 | $formattedSalesPerLaborHour = (float)$decimal->format($salesPerLaborHour); |
| 68 | } else { |
| 69 | // Fallback when NumberFormatter is not available |
| 70 | $formattedLaborPercent = number_format($laborPercent * 100, 2) . '%'; |
| 71 | $formattedSalesPerLaborHour = round($salesPerLaborHour, 2); |
| 72 | } |
| 73 | $salesCurrent = is_array($current) && isset($current['salesCurrent']) ? $current['salesCurrent'] : 0; |
| 74 | $result = array("hr" => $totalHours, "dollars" => $totalDollars, "salesCurrent"=> $salesCurrent, "laborPercent" => $formattedLaborPercent, "salesPerLaborHour" => $formattedSalesPerLaborHour, "employeeData" => $this->employeeDataArray); |
| 75 | $this->predis->set($this->key, json_encode($result)); |
| 76 | $this->predis->expire($this->key, 60*5); |
| 77 | $this->isFromCache = 0; |
| 78 | return $result; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | public function getLaborHoursArray() { |
| 83 | $tomorrow = new \DateTime('now', new \DateTimeZone($this->store->getTimeZone())); |
| 84 | $tomorrow->add(new \DateInterval("P1D")); |
| 85 | $labor = $this->wiw->get('times', array("start" => $this->date->format("Y-m-d"), "end" => $tomorrow->format("Y-m-d"))); |
| 86 | $this->employeeIDArray = []; |
| 87 | //If no one has clocked in yet, the users array will have 0 elements in it |
| 88 | if(isset($labor->users) && count($labor->users) > 0) { |
| 89 | foreach($labor->users as $user) { |
| 90 | $this->employeeIDArray[$user->id] = new Employee($user); |
| 91 | } |
| 92 | $totalLaborHours = []; |
| 93 | foreach($labor->times as $time) { |
| 94 | //Set the user's hourly rate to the position rate they are clocked in as |
| 95 | $this->employeeIDArray[$time->user_id]->hourly_rate = $time->hourly_rate; |
| 96 | $start = \DateTime::createFromFormat("D, d M Y H:i:s O", $time->start_time); |
| 97 | if($time->end_time !== null && strlen($time->end_time) > 1) { |
| 98 | $end = \DateTime::createFromFormat("D, d M Y H:i:s O", $time->end_time); |
| 99 | } else { |
| 100 | $end = new \DateTime('now', new \DateTimeZone($this->store->getTimeZone())); |
| 101 | } |
| 102 | |
| 103 | $diffMinutes = ($end->getTimestamp() - $start->getTimestamp())/60; |
| 104 | $shiftHours = 0; |
| 105 | if($diffMinutes > 0) { |
| 106 | $shiftHours = (floor(($diffMinutes/60) * 100) / 100) - (isset($time->break_time) ? $time->break_time : 0); |
| 107 | } |
| 108 | $dollarValue = $shiftHours * $time->hourly_rate; |
| 109 | $this->employeeDataArray[$time->user_id] = array("shiftHours" => $shiftHours, "dollarValue" => $dollarValue); |
| 110 | $totalLaborHours[] = array("hr" => $shiftHours, "value" => $dollarValue, ); |
| 111 | } |
| 112 | } else { |
| 113 | $totalLaborHours = array("hr" => 0, "value" => 0); |
| 114 | } |
| 115 | |
| 116 | return $totalLaborHours; |
| 117 | } |
| 118 | public function getCurrentData() { |
| 119 | try { |
| 120 | $db = dbConnectByName($this->store->getDbName()); |
| 121 | $db->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING ); |
| 122 | $stmt = $db->prepare("SELECT salesCurrent FROM LiveFinancials WHERE `date` = :date"); |
| 123 | $stmt->bindValue(":date", $this->date->format("Y-m-d")); |
| 124 | if($stmt->execute()) { |
| 125 | return $stmt->fetch(\PDO::FETCH_ASSOC); |
| 126 | |
| 127 | } else { |
| 128 | |
| 129 | error_log("No Execute"); |
| 130 | } |
| 131 | } catch (\PDOException $e) { |
| 132 | error_log("Bad Query"); |
| 133 | |
| 134 | } |
| 135 | return null; |
| 136 | } |
| 137 | } |