BaseFreightDeduction.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: blank
  5. * Date: 2022/4/13
  6. * Time: 15:27
  7. */
  8. namespace app\frontend\modules\dispatch\deduction;
  9. use app\frontend\models\order\PreOrderDeduction;
  10. use app\frontend\modules\order\models\PreOrder;
  11. use app\frontend\modules\dispatch\models\PreOrderFreightDeduction;
  12. abstract class BaseFreightDeduction
  13. {
  14. protected $name;
  15. protected $code;
  16. /**
  17. * @var PreOrder
  18. */
  19. protected $order;
  20. /**
  21. * @var \app\frontend\modules\dispatch\models\OrderFreight|mixed
  22. */
  23. protected $orderFreight;
  24. /**
  25. * @var PreOrderDeduction
  26. */
  27. protected $orderDeduction;
  28. public function __construct(PreOrder $order, PreOrderDeduction $orderDeduction)
  29. {
  30. $this->order = $order;
  31. $this->orderFreight = $order->getFreightManager();
  32. $this->orderDeduction = $orderDeduction;
  33. }
  34. public function isChecked()
  35. {
  36. return $this->orderDeduction->isChecked();
  37. }
  38. public function getCode()
  39. {
  40. return $this->orderDeduction->getCode();
  41. }
  42. public function getName()
  43. {
  44. return $this->orderDeduction->getName();
  45. }
  46. /**
  47. * @return bool
  48. */
  49. public function calculated()
  50. {
  51. return isset($this->amount);
  52. }
  53. public function preSave()
  54. {
  55. return true;
  56. }
  57. /**
  58. * 获取总金额
  59. * @return float
  60. */
  61. public function getAmount()
  62. {
  63. if (isset($this->amount)) {
  64. return $this->amount;
  65. }
  66. $this->amount = $this->_getAmount();
  67. $this->amount = $this->_getAmount();
  68. if($this->amount && $this->preSave()){
  69. // 将抵扣总金额保存在订单优惠信息表中
  70. $coin = $this->orderDeduction->newCoin()->setCoin($this->amount);
  71. $preOrderDiscount = new PreOrderFreightDeduction([
  72. 'code' => $this->getCode(),
  73. 'amount' => $this->amount,
  74. 'name' => $this->getName(),
  75. 'coin' => $coin->getCoin(),
  76. ]);
  77. $preOrderDiscount->setOrder($this->order);
  78. }
  79. return $this->amount;
  80. }
  81. /**
  82. * 获取金额
  83. * @return int
  84. */
  85. abstract protected function _getAmount();
  86. }