OrderDispatch.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/9
  6. * Time: 上午9:25
  7. */
  8. namespace app\frontend\modules\dispatch\models;
  9. use app\common\events\dispatch\OrderDispatchWasCalculated;
  10. use app\frontend\modules\dispatch\discount\EnoughReduce;
  11. use app\frontend\modules\dispatch\discount\LevelFreeFreight;
  12. use app\frontend\modules\dispatch\freight\BaseFreight;
  13. use app\frontend\modules\order\models\PreOrder;
  14. class OrderDispatch
  15. {
  16. /**
  17. * @var PreOrder
  18. */
  19. private $order;
  20. /**
  21. * @var float
  22. */
  23. private $freight;
  24. protected $freightPrices;
  25. /**
  26. * OrderDispatch constructor.
  27. * @param PreOrder $preOrder
  28. */
  29. public function __construct(PreOrder $preOrder)
  30. {
  31. $this->order = $preOrder;
  32. }
  33. public function getFreightPrices()
  34. {
  35. if (!isset($this->freightNodes)) {
  36. $this->freightPrices = $this->_getFreightPrices();
  37. }
  38. return $this->freightPrices;
  39. }
  40. //订单运费集合
  41. public function _getFreightPrices()
  42. {
  43. // 订单运费集合
  44. $freightPrices = collect();
  45. $freightConfig = \app\common\modules\shop\ShopConfig::current()->get('shop-foundation.order-freight');
  46. foreach ($freightConfig as $configItem) {
  47. $freightPrices->push(call_user_func($configItem['class'], $this->order, $configItem['weight']));
  48. }
  49. $freightPrices = $freightPrices->filter(function(BaseFreight $freightPrice) {
  50. //过滤不能使用的运费计算方式
  51. return $freightPrice->needDispatch();
  52. })->sortBy(function (BaseFreight $freightPrice) {
  53. // 按照weight排序
  54. return $freightPrice->getWeight();
  55. })->values();
  56. return $freightPrices;
  57. }
  58. public function freightPriority()
  59. {
  60. foreach ($this->getFreightPrices() as $freightPrice) {
  61. switch ($freightPrice->getGroup()) {
  62. case 'third_party':
  63. return $freightPrice->getAmount();
  64. break;
  65. case 'max':
  66. return $this->getFreightPrices()->max(function(BaseFreight $freight) {
  67. return $freight->getAmount();
  68. });
  69. break;
  70. case 'sum':
  71. return $this->getFreightPrices()->sum(function(BaseFreight $freight) {
  72. return $freight->getAmount();
  73. });
  74. break;
  75. default:
  76. }
  77. }
  78. return 0;
  79. }
  80. public function _getFreight()
  81. {
  82. if (is_null($this->order->getOrderDispatchType()) || !$this->order->getOrderDispatchType()->needFreight()) {
  83. // 没选配送方式 或者 不需要配送不需要运费
  84. return 0;
  85. }
  86. return $this->freightPriority();
  87. }
  88. /**
  89. * 订单运费
  90. * @return float|int
  91. */
  92. public function getFreight()
  93. {
  94. if (!isset($this->freight)) {
  95. $this->freight = $this->_getFreight();
  96. //获取满额包邮类
  97. $enoughReduce = $this->order->getEnoughReduce();
  98. //运费优惠计算
  99. //$this->freight = max($this->freight - (new EnoughReduce($this->order))->getAmount(), 0);
  100. $this->freight = max($this->freight - $enoughReduce->getAmount(), 0);
  101. $this->freight = max($this->freight - (new LevelFreeFreight($this->order))->getAmount(), 0);
  102. }
  103. return $this->freight;
  104. }
  105. }