PreOrderFreightDeductionCalculation.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: blank
  5. * Date: 2022/4/13
  6. * Time: 18:23
  7. */
  8. namespace app\frontend\modules\dispatch\deduction;
  9. use app\common\exceptions\AppException;
  10. use app\frontend\models\order\PreOrderDeduction;
  11. use app\frontend\models\order\PreOrderDiscount;
  12. use app\common\models\VirtualCoin;
  13. use app\frontend\models\MemberCoin;
  14. use app\frontend\modules\deduction\models\Deduction;
  15. use app\frontend\modules\dispatch\models\OrderFreight;
  16. use app\frontend\modules\order\models\PreOrder;
  17. class PreOrderFreightDeductionCalculation
  18. {
  19. /**
  20. * @var PreOrder
  21. */
  22. public $order;
  23. /**
  24. * @var Deduction
  25. */
  26. private $deduction;
  27. /**
  28. * @var OrderFreight
  29. */
  30. private $orderFreight;
  31. public function init(Deduction $deduction, OrderFreight $orderFreight)
  32. {
  33. $this->deduction = $deduction;
  34. $this->orderFreight = $orderFreight;
  35. $this->order = $orderFreight->getOrder();
  36. }
  37. public function getUidAttribute()
  38. {
  39. return $this->order->uid;
  40. }
  41. public function getCodeAttribute()
  42. {
  43. return $this->getCode();
  44. }
  45. public function getNameAttribute()
  46. {
  47. return $this->getName();
  48. }
  49. /**
  50. * @return string
  51. */
  52. public function getCode()
  53. {
  54. return $this->getDeduction()->getCode();
  55. }
  56. /**
  57. * @return string
  58. */
  59. public function getName()
  60. {
  61. return $this->getDeduction()->getName();
  62. }
  63. /**
  64. * @return bool
  65. */
  66. public function getIsEnableAttribute()
  67. {
  68. return $this->isEnableDeductFreight();
  69. }
  70. /**
  71. * 此抵扣对应的虚拟币
  72. * @return VirtualCoin
  73. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  74. */
  75. public function newCoin()
  76. {
  77. return app('CoinManager')->make($this->getCode());
  78. }
  79. /**
  80. * @return mixed
  81. * @throws \app\common\exceptions\AppException
  82. */
  83. public function getAmountAttribute()
  84. {
  85. return $this->getAmount();
  86. }
  87. protected $deductionAmount;
  88. /**
  89. * @return mixed
  90. * @throws \app\common\exceptions\AppException
  91. */
  92. public function getAmount()
  93. {
  94. if (!$this->deductionAmount) {
  95. $this->deductionAmount = $this->newCoin();
  96. // 购买者不存在虚拟币记录
  97. if (!$this->getMemberCoin()) {
  98. trace_log()->deduction('订单抵扣', "{$this->getName()} 用户没有对应虚拟币");
  99. return $this->deductionAmount;
  100. }
  101. $deductionFreightAmount = $this->orderFreight->getPriceBefore($this->getCode().'Deduction');
  102. $amount = min($this->getMemberCoin()->getMaxUsableCoin()->getMoney(), $deductionFreightAmount);
  103. $this->deductionAmount = $this->newCoin()->setMoney($amount);
  104. }
  105. return $this->deductionAmount;
  106. }
  107. public function getOrder()
  108. {
  109. return $this->order;
  110. }
  111. public function getDeduction()
  112. {
  113. return $this->deduction;
  114. }
  115. /**
  116. * @return Deduction
  117. */
  118. public function getOrderDeduction()
  119. {
  120. return $this->order->getOrderDeductManager()->getOrderDeductions()->where('code', $this->getCode())->first();
  121. }
  122. public function isChecked()
  123. {
  124. $this->getOrderDeduction()->isChecked();
  125. }
  126. /**
  127. * 下单用户此抵扣对应虚拟币的余额
  128. * @return MemberCoin
  129. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  130. */
  131. private function getMemberCoin()
  132. {
  133. if (isset($this->memberCoin)) {
  134. return $this->memberCoin;
  135. }
  136. $code = $this->getCode();
  137. return \app\frontend\modules\deduction\EnableDeductionService::getInstance()->getMemberCoin($code);
  138. }
  139. //是否开启运费抵扣
  140. public function isEnableDeductFreight()
  141. {
  142. return $this->getDeduction()->isEnableDeductDispatchPrice();
  143. }
  144. }