SingleEnoughReduce.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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\orderGoods\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|int|null
  22. */
  23. protected function _getAmount()
  24. {
  25. if(!$this->getOrderGoodsPrice()){
  26. return 0;
  27. }
  28. // (订单商品成交金额/订单中同种商品总成交金额 ) * 订单单品满减金额
  29. // 商品成交金额 = 订单成交价 - 商品等级优惠
  30. return ($this->orderGoods->getPriceBefore($this->getCode()) / $this->getOrderGoodsPrice()) * $this->getAmountInOrder();
  31. }
  32. /**
  33. * 订单对应该商品的单品优惠
  34. */
  35. private function getAmountInOrder()
  36. {
  37. if(is_null($this->orderGoods->goods->hasOneSale)){
  38. return 0;
  39. }
  40. return $this->orderGoods->goods->hasOneSale->getEnoughReductionAmount($this->getOrderGoodsPrice());
  41. }
  42. /**
  43. * 订单中同商品的价格小计
  44. * @return float
  45. */
  46. protected function getOrderGoodsPrice()
  47. {
  48. return $this->orderGoods->order->orderGoods->where('goods_id', $this->orderGoods->goods_id)->sum(function (PreOrderGoods $preOrderGoods) {
  49. return $preOrderGoods->getPriceBefore($this->getCode());
  50. });
  51. }
  52. }