Sale.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/5/3
  6. * Time: 上午11:46
  7. */
  8. namespace app\frontend\models\goods;
  9. use app\frontend\modules\orderGoods\models\PreOrderGoods;
  10. /**
  11. * Class Sale
  12. * @package app\frontend\models\goods
  13. * @property string max_point_deduct
  14. * @property string min_point_deduct
  15. * @property float ed_full
  16. * @property float ed_reduction
  17. * @property string ed_areaids
  18. * @property int ed_num
  19. * @property float ed_money
  20. * @property int has_all_point_deduct
  21. * @property float all_point_deduct
  22. */
  23. class Sale extends \app\common\models\Sale
  24. {
  25. /**
  26. * 计算满额减金额
  27. * @param float $enough
  28. * @return float
  29. */
  30. public function getEnoughReductionAmount($enough)
  31. {
  32. if ($enough < $this->ed_full) {
  33. // 未满额
  34. return 0;
  35. }
  36. if ($this->ed_full < 0) {
  37. // 减额非正数时,记录异常
  38. \Log::error('商品计算满减价格时,减额数据非正数', [$this->id, $this->ed_full, $this->ed_reduction]);
  39. return 0;
  40. }
  41. if ($this->ed_reduction > $enough) {
  42. // 减额大于商品价格时,记录异常
  43. \Log::error('商品计算满减价格时,减额大于商品价格', [$this->id, $this->ed_full, $this->ed_reduction]);
  44. }
  45. return min($this->ed_reduction, $enough);
  46. }
  47. /**
  48. * 是否包邮
  49. * @param PreOrderGoods $orderGoods
  50. * @return bool
  51. */
  52. public function isFree(PreOrderGoods $orderGoods)
  53. {
  54. if (!isset($orderGoods->order->orderAddress)) {
  55. //未选择地址时
  56. return false;
  57. }
  58. if (!$this->inFreeArea($orderGoods)) {
  59. //收货地址不在包邮区域
  60. return false;
  61. }
  62. return $this->enoughQuantity($this->goodsTotalInOrder($orderGoods)) || $this->enoughAmount($this->goodsPriceInOrder($orderGoods));
  63. }
  64. /**
  65. * 获取同订单中同商品总数(包括不同规格)
  66. * @param PreOrderGoods $orderGoods
  67. * @return int
  68. */
  69. private function goodsTotalInOrder(PreOrderGoods $orderGoods)
  70. {
  71. $result = $orderGoods->order->orderGoods->where('goods_id', $orderGoods->goods_id)->sum(function ($orderGoods) {
  72. return $orderGoods->total;
  73. });
  74. return $result;
  75. }
  76. /**
  77. * 获取同订单中同商品价格(包括不同规格)
  78. * @param $orderGoods
  79. * @return float
  80. */
  81. private function goodsPriceInOrder(PreOrderGoods $orderGoods)
  82. {
  83. $result = $orderGoods->order->orderGoods->where('goods_id', $orderGoods->goods_id)->sum(function (PreOrderGoods $orderGoods) {
  84. return $orderGoods->getPriceCalculator()->getCurrentPrice();
  85. });
  86. return $result;
  87. }
  88. /**
  89. * 收货地址不在包邮区域
  90. * @param PreOrderGoods $orderGoods
  91. * @return bool
  92. */
  93. private function inFreeArea(PreOrderGoods $orderGoods)
  94. {
  95. $ed_areaids = (explode(',', $this->ed_areaids));
  96. if (empty($ed_areaids)) {
  97. return true;
  98. }
  99. if (in_array($orderGoods->order->orderAddress->city_id, $ed_areaids)) {
  100. return false;
  101. }
  102. return true;
  103. }
  104. /**
  105. * 单商品购买数量
  106. * @param int $total
  107. * @return bool
  108. */
  109. private function enoughQuantity($total)
  110. {
  111. if ($this->ed_num == false) {
  112. return false;
  113. }
  114. return $total >= $this->ed_num;
  115. }
  116. /**
  117. * 单商品价格
  118. * @param float $price
  119. * @return bool
  120. */
  121. private function enoughAmount($price)
  122. {
  123. if ($this->ed_money == false) {
  124. return false;
  125. }
  126. return $price >= $this->ed_money;
  127. }
  128. }