Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 58 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| SafeLevelAlert | |
0.00% |
0 / 58 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| checkAlertLevels | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| checkDenomination | |
0.00% |
0 / 43 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\Cash; |
| 4 | |
| 5 | use Birke\Rememberme\Storage\PDO; |
| 6 | |
| 7 | class SafeLevelAlert |
| 8 | { |
| 9 | private $safeBalance; |
| 10 | private $safeConfigurationRepository; |
| 11 | private $safeConfiguration; |
| 12 | private $totalSafeBalance = 0; |
| 13 | private $totalNeededValue = 0; |
| 14 | |
| 15 | |
| 16 | public function __construct(CashActivity $safeBalance = null, \Store $store) |
| 17 | { |
| 18 | $this->safeBalance = $safeBalance; |
| 19 | $this->safeConfigurationRepository = new SafeConfigurationRepository($store); |
| 20 | $this->safeConfiguration = $this->safeConfigurationRepository->getLatest(); |
| 21 | } |
| 22 | |
| 23 | public function checkAlertLevels(): array |
| 24 | { |
| 25 | // Early return if safeBalance is null |
| 26 | if ($this->safeBalance === null) { |
| 27 | return [ |
| 28 | 'error' => 'No safe balance data available', |
| 29 | 'totalSafeBalance' => 0, |
| 30 | 'totalNeededValue' => 0, |
| 31 | 'safeBalance' => 0 |
| 32 | ]; |
| 33 | } |
| 34 | |
| 35 | $alerts = []; |
| 36 | // Rest of the checks remain the same |
| 37 | $this->checkDenomination($alerts, 'pennies', 'pennies', 'pennyRolls', 'penniesPerRoll'); |
| 38 | // ... other denomination checks ... |
| 39 | |
| 40 | $alerts['totalSafeBalance'] = $this->totalSafeBalance; |
| 41 | $alerts['safeBalance'] = $this->safeBalance; |
| 42 | |
| 43 | return $alerts; |
| 44 | } |
| 45 | |
| 46 | private function checkDenomination(array &$alerts, string $denomination, string $looseField, string $rollField, string $perRollField): void |
| 47 | { |
| 48 | $currentLoose = $this->safeBalance->{"get" . ucfirst($looseField)}() ?? 0; |
| 49 | $currentRolls = $this->safeBalance->{"get" . ucfirst($rollField)}() ?? 0; |
| 50 | $perRoll = $this->safeConfiguration->{"get" . ucfirst($perRollField)}() ?? 0; |
| 51 | |
| 52 | |
| 53 | $totalQuantity = $currentLoose + ($currentRolls * $perRoll); |
| 54 | $equivalentRolls = $totalQuantity / $perRoll; |
| 55 | |
| 56 | $wantedLevel = $this->safeConfiguration->{"getWanted" . ucfirst($denomination)}(); |
| 57 | $lowLevel = $this->safeConfiguration->{"getLow" . ucfirst($denomination)}(); |
| 58 | $criticalLevel = $this->safeConfiguration->{"getCritical" . ucfirst($denomination)}(); |
| 59 | |
| 60 | $denominationValues = [ |
| 61 | 'pennies' => 0.01, |
| 62 | 'nickels' => 0.05, |
| 63 | 'dimes' => 0.10, |
| 64 | 'quarters' => 0.25, |
| 65 | 'ones' => 1.00, |
| 66 | 'fives' => 5.00, |
| 67 | 'tens' => 10.00, |
| 68 | 'twenties' => 20.00, |
| 69 | 'fifties' => 50.00, |
| 70 | 'hundreds' => 100.00 |
| 71 | ]; |
| 72 | |
| 73 | $currentDollars = $totalQuantity * $denominationValues[$denomination]; |
| 74 | $this->totalSafeBalance += $currentDollars; |
| 75 | |
| 76 | if ($equivalentRolls <= $lowLevel) { |
| 77 | $rollsNeeded = $wantedLevel - $equivalentRolls; |
| 78 | $rollsNeeded = max(0, ceil($rollsNeeded)); |
| 79 | |
| 80 | // Only add alert if rolls needed is greater than 0 |
| 81 | if ($rollsNeeded > 0) { |
| 82 | $quantityNeeded = $rollsNeeded * $perRoll; |
| 83 | $dollarsNeeded = $quantityNeeded * $denominationValues[$denomination]; |
| 84 | $this->totalNeededValue += $dollarsNeeded; |
| 85 | |
| 86 | $alerts[$denomination] = [ |
| 87 | 'status' => $equivalentRolls <= $criticalLevel ? 'critical' : 'low', |
| 88 | 'current' => [ |
| 89 | 'rolls' => $equivalentRolls, |
| 90 | 'quantity' => $totalQuantity, |
| 91 | 'dollars' => $currentDollars |
| 92 | ], |
| 93 | 'needed' => [ |
| 94 | 'rolls' => $rollsNeeded, |
| 95 | 'quantity' => $quantityNeeded, |
| 96 | 'dollars' => $dollarsNeeded |
| 97 | ] |
| 98 | ]; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Add total needed value to alerts array |
| 103 | $alerts['totalNeededValue'] = $this->totalNeededValue; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | |
| 108 |