CouponPrice.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/25
  6. * Time: 下午5:14
  7. */
  8. namespace app\frontend\modules\coupon\services\models\Price;
  9. use app\common\models\coupon\GoodsMemberCoupon;
  10. use app\frontend\modules\coupon\services\models\Coupon;
  11. use app\common\modules\orderGoods\models\PreOrderGoods;
  12. use app\frontend\modules\orderGoods\models\PreOrderGoodsCollection;
  13. use app\frontend\modules\order\models\PreOrder;
  14. abstract class CouponPrice
  15. {
  16. protected $isSet = false;
  17. protected $amount;
  18. /**
  19. * 优惠券数据库model
  20. * @var \app\common\models\Coupon
  21. */
  22. protected $dbCoupon;
  23. /**
  24. * @var Coupon
  25. */
  26. protected $coupon;
  27. /**
  28. * @var PreOrder
  29. */
  30. protected $orderModel;
  31. /**
  32. * @var PreOrderGoodsCollection
  33. */
  34. protected $orderGoodsModelGroup;
  35. public function __construct(Coupon $coupon)
  36. {
  37. $this->coupon = $coupon;
  38. $this->dbCoupon = $coupon->getMemberCoupon()->belongsToCoupon;
  39. $this->orderModel = $coupon->getPreOrder();
  40. //dd($this->orderModel);
  41. }
  42. /**
  43. * 有效的
  44. * @return bool
  45. */
  46. public function valid()
  47. {
  48. // 商品价格中未使用优惠的金额 不小于 满减额度
  49. $unusedEnoughMoney = $this->getOrderGoodsCollectionUnusedEnoughMoney();
  50. if (!float_lesser($unusedEnoughMoney, $this->dbCoupon->enough)) {
  51. return true;
  52. }
  53. trace_log()->coupon("优惠券{$this->dbCoupon->id}","不满足额度({$unusedEnoughMoney}<{$this->dbCoupon->enough})");
  54. return false;
  55. }
  56. /**
  57. * 有效的
  58. * @return bool
  59. */
  60. public function isOptional()
  61. {
  62. $orderGoodsCollectionPrice = $this->getOrderGoodsCollectionPrice();
  63. // 商品价格 不小于 满减额度
  64. if (!float_lesser($orderGoodsCollectionPrice, $this->dbCoupon->enough)) {
  65. return true;
  66. }
  67. trace_log()->coupon("优惠券{$this->dbCoupon->id}","不满足额度({$orderGoodsCollectionPrice}<{$this->dbCoupon->enough})");
  68. return false;
  69. }
  70. /**
  71. * 累加所有商品未使用优惠的金额
  72. * @return mixed
  73. */
  74. protected function getOrderGoodsCollectionUnusedEnoughMoney()
  75. {
  76. //todo 这里为什么要累加已使用优惠券使用条件的金额,再拿订单商品优惠券节点前金额减去。
  77. //理解为:防止优惠券选择优先问题,原因是会员可以先用使用条件较大的优惠券再用小的
  78. //判断一张优惠券是否可以都需要先减去已使用优惠券条件金额,再去对比是否满足
  79. $enough = $this->coupon->getOrderGoodsInScope()->sum(function ($orderGoods) {
  80. if (!isset($orderGoods->coupons)) {
  81. return 0;
  82. }
  83. return $orderGoods->coupons->sum('enough');
  84. });
  85. return $this->getOrderGoodsCollectionPrice() - $enough;
  86. }
  87. public function getPrice(){
  88. if(!isset($this->amount)){
  89. $this->amount = $this->_getAmount();
  90. }
  91. return $this->amount;
  92. }
  93. /**
  94. * 订单获取优惠券 金额
  95. * @return mixed
  96. */
  97. abstract protected function _getAmount();
  98. /**
  99. * 累加所有商品会员价
  100. * @return int
  101. */
  102. protected function getOrderGoodsCollectionPrice()
  103. {
  104. return $this->coupon->getOrderGoodsInScope()->sum(function (PreOrderGoods $orderGoods) {
  105. return $orderGoods->getPriceBefore('coupon');
  106. });
  107. }
  108. /**
  109. * 累加所有商品支付金额
  110. * @return int
  111. */
  112. protected function getOrderGoodsCollectionPaymentAmount()
  113. {
  114. return $this->coupon->getOrderGoodsInScope()->sum(function (PreOrderGoods $preOrderGoods){
  115. return $preOrderGoods->getPriceBefore('coupon');
  116. });
  117. }
  118. protected function getExchangeOrderGoodsCollectionPayment()
  119. {
  120. $goodsModel = $this->coupon->getOrderGoodsInScope()->first();
  121. return bcdiv($goodsModel->getPriceBefore('coupon'),$goodsModel->total,2) ;
  122. }
  123. /**
  124. * 分配优惠金额 立减折扣券使用 商品折扣后价格计算
  125. */
  126. public function setOrderGoodsDiscountPrice()
  127. {
  128. if($this->isSet){
  129. return;
  130. }
  131. $this->coupon->getOrderGoodsInScope()->map(function ($orderGoods) {
  132. /**
  133. * @var $orderGoods PreOrderGoods
  134. */
  135. $goodsMemberCoupon = new GoodsMemberCoupon();
  136. $goodsMemberCoupon->amount = $orderGoods->getPriceBefore('coupon') / $this->getOrderGoodsCollectionPaymentAmount() * $this->getPrice();
  137. $goodsMemberCoupon->enough = $orderGoods->getPriceBefore('coupon') / $this->getOrderGoodsCollectionPaymentAmount() * $this->dbCoupon->enough;
  138. //todo 需要按照订单方式修改
  139. if (!isset($orderGoods->coupons)) {
  140. $orderGoods->coupons = collect();
  141. }
  142. $orderGoods->coupons->push($goodsMemberCoupon);
  143. });
  144. $this->isSet = true;
  145. }
  146. }