amount_period_card.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace mcard;
  3. class amount_period_card extends card
  4. {
  5. public function usable(): bool
  6. {
  7. $state = $this->state();
  8. if ($state === STATE_OVER) {
  9. return false;
  10. }
  11. elseif ($state === STATE_USING)
  12. {
  13. $left = intval($this->left_amount() * 100);
  14. if ($this->end_time() < time() || $left === 0) {
  15. return false;
  16. } else {
  17. return true;
  18. }
  19. }
  20. else {
  21. return true;
  22. }
  23. }
  24. public function deduct(int $cent)
  25. {
  26. $left_cent = intval($this->left_amount() * 100 + 0.5);
  27. if ($left_cent > $cent) {
  28. $amount = $cent;
  29. $useup = false;
  30. } else {
  31. $amount = $left_cent;
  32. $useup = true;
  33. }
  34. return [$amount, $useup];
  35. }
  36. public function calc(int $cent)
  37. {
  38. $left_cent = intval($this->left_amount() * 100 + 0.5);
  39. if ($left_cent > $cent)
  40. {
  41. $amount = $cent;
  42. $useup = false;
  43. }
  44. else
  45. {
  46. $amount = $left_cent;
  47. $useup = true;
  48. }
  49. $price = round($amount * (1 - $this->discount()) / 100, 2);
  50. return [$amount, $useup,$price];
  51. }
  52. }