FullPiece.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\facades\Setting;
  10. use app\common\modules\orderGoods\models\PreOrderGoods;
  11. class FullPiece extends BaseDiscount
  12. {
  13. protected $code = 'fullPiece';
  14. protected $name = '满件优惠';
  15. private $setting;
  16. /**
  17. * 获取总金额
  18. * @return float|int
  19. */
  20. protected function _getAmount()
  21. {
  22. $settings = $this->getSetting();
  23. if(!$settings['open']){
  24. return 0;
  25. }
  26. //只有商城,供应商订单参加
  27. if($this->orderGoods->order->plugin_id != 0){
  28. return 0;
  29. }
  30. if (empty($settings['fullPiece'])) {
  31. return 0;
  32. }
  33. // (支付金额/订单中同种商品已计算的支付总价 ) * 全场满减金额
  34. return $this->getOrderGoodsPrice($this->orderGoods);
  35. }
  36. protected function getSetting()
  37. {
  38. if (!isset($this->setting)) {
  39. $this->setting = Setting::get('shop.fullPieceNew');
  40. }
  41. return $this->setting;
  42. }
  43. /**
  44. * @param PreOrderGoods $orderGoods
  45. * @return int|mixed
  46. */
  47. private function getOrderGoodsPrice(PreOrderGoods $orderGoods)
  48. {
  49. $settings = $this->getSetting();
  50. $fullPiece = [];
  51. foreach ($settings['fullPiece'] as $k=>$v) {
  52. $fullPiece[$k] = $v;
  53. $fullPiece[$k]['goods'] = [];
  54. if (empty($v['goods'])) {
  55. continue;
  56. }
  57. foreach ($orderGoods->order->orderGoods as $item) {
  58. if (in_array($item->goods_id,$v['goods'])) {
  59. $fullPiece[$k]['goods'][] = $item->goods_id;
  60. }
  61. }
  62. }
  63. $inFullPiece = [];
  64. foreach ($fullPiece as $item) {
  65. if (empty($item['goods']) || !in_array($orderGoods->goods_id,$item['goods'])) {
  66. continue;
  67. }
  68. $inFullPiece = $item;
  69. break;
  70. }
  71. if (!$inFullPiece) {
  72. return 0;
  73. }
  74. // 求和所属订单中指定goods_id的订单商品支付金额
  75. $amount = $orderGoods->order->orderGoods->whereIn('goods_id', $inFullPiece['goods'])->sum(function (PreOrderGoods $preOrderGoods) {
  76. return $preOrderGoods->getPriceBefore($this->getCode());
  77. });
  78. $goods_count = $orderGoods->total;
  79. $goods_total_count = $orderGoods->order->orderGoods->whereIn('goods_id', $inFullPiece['goods'])->sum('total');
  80. $rules = collect($inFullPiece['rules']);
  81. $rules = $rules->sortByDesc(function ($rule) {
  82. return $rule['enough'];
  83. });
  84. $discount = 0;
  85. foreach ($rules as $rule) {
  86. if ($goods_total_count < $rule['enough']) {
  87. continue;
  88. }
  89. if ($inFullPiece['discount_type']) {//折扣
  90. $reduce = bcsub(10,$rule['reduce'],2);
  91. $discount = bcmul(bcdiv($reduce,10,2),$amount,2);
  92. break;
  93. } else {//立减
  94. $discount = min($rule['reduce'],$amount);
  95. break;
  96. }
  97. }
  98. if (!$discount) {
  99. return 0;
  100. }
  101. if ($discount == $amount) { //优惠与商品价格相同
  102. return $this->orderGoods->order->orderGoods->where('goods_id', $orderGoods->goods_id)->sum(function (PreOrderGoods $preOrderGoods) {
  103. return $preOrderGoods->getPriceBefore($this->getCode());
  104. });
  105. }
  106. return bcdiv(($discount * $goods_count),$goods_total_count,2);
  107. }
  108. }