SingleEnoughReduce.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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\common\modules\orderGoods\models\PreOrderGoods;
  10. /**
  11. * 单品满减优惠
  12. * Class SingleEnoughReduce
  13. * @package app\frontend\modules\order\discount
  14. */
  15. class SingleEnoughReduce extends BaseDiscount
  16. {
  17. protected $code = 'singleEnoughReduce';
  18. protected $name = '单品满减优惠';
  19. /**
  20. * 订单中订单商品单品满减的总金额
  21. * @return float
  22. */
  23. protected function _getAmount()
  24. {
  25. //对订单商品按goods_id去重 累加单品满减金额
  26. $result = $this->order->orderGoods->unique('goods_id')->sum(function (PreOrderGoods $orderGoods) {
  27. return $this->totalAmount($orderGoods);
  28. });
  29. return $result;
  30. }
  31. /**
  32. * 指定订单商品的单品满减金额
  33. * @param PreOrderGoods $orderGoods
  34. * @return float
  35. */
  36. private function totalAmount(PreOrderGoods $orderGoods)
  37. {
  38. // 求和所属订单中指定goods_id的订单商品支付金额
  39. $amount = $this->order->orderGoods->where('goods_id', $orderGoods->goods_id)->sum(function (PreOrderGoods $preOrderGoods) {
  40. return $preOrderGoods->getPriceBefore($this->getCode());
  41. });
  42. if (is_null($orderGoods->goods->hasOneSale)) {
  43. return 0;
  44. }
  45. // order_goods的单品满减金额
  46. return $orderGoods->goods->hasOneSale->getEnoughReductionAmount($amount);
  47. }
  48. }