BaseDiscount.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shenyang
  5. * Date: 2018/5/23
  6. * Time: 下午3:55
  7. */
  8. namespace app\frontend\modules\order\discount;
  9. use app\frontend\models\order\PreOrderDiscount;
  10. use app\frontend\modules\order\models\PreOrder;
  11. abstract class BaseDiscount
  12. {
  13. /**
  14. * @var PreOrder
  15. */
  16. protected $order;
  17. /**
  18. * 优惠名
  19. * @var string
  20. */
  21. protected $name;
  22. /**
  23. * 是否不显示优惠多少金额
  24. * @var int
  25. */
  26. protected $no_show = 0;
  27. /**
  28. * 优惠码
  29. * @var
  30. */
  31. protected $code;
  32. /**
  33. * @var float
  34. */
  35. private $amount;
  36. public function __construct(PreOrder $order)
  37. {
  38. $this->order = $order;
  39. }
  40. public function getCode()
  41. {
  42. return $this->code;
  43. }
  44. public function getName()
  45. {
  46. return $this->name;
  47. }
  48. /**
  49. * @return bool
  50. */
  51. public function calculated()
  52. {
  53. return isset($this->amount);
  54. }
  55. public function preSave()
  56. {
  57. return true;
  58. }
  59. /**
  60. * 获取总金额
  61. * @return float
  62. */
  63. public function getAmount()
  64. {
  65. if (isset($this->amount)) {
  66. return $this->amount;
  67. }
  68. $this->amount = $this->_getAmount();
  69. if($this->amount && $this->preSave()){
  70. // 将抵扣总金额保存在订单优惠信息表中
  71. //统一算法记录金额保存算法为:四舍六入五成双
  72. $preOrderDiscount = new PreOrderDiscount([
  73. 'discount_code' => $this->code,
  74. 'amount' => round($this->amount,2,PHP_ROUND_HALF_EVEN),
  75. 'name' => $this->getName(),
  76. 'no_show' => $this->no_show
  77. ]);
  78. $preOrderDiscount->setOrder($this->order);
  79. }
  80. return $this->amount;
  81. }
  82. /**
  83. * 获取金额
  84. * @return int
  85. */
  86. abstract protected function _getAmount();
  87. }