UnifyFreight.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020/8/18
  6. * Time: 11:45
  7. */
  8. namespace app\frontend\modules\dispatch\freight;
  9. use app\common\models\goods\GoodsDispatch;
  10. use app\frontend\models\OrderGoods;
  11. use app\frontend\modules\order\models\PreOrder;
  12. class UnifyFreight
  13. {
  14. protected $code = 'unify';
  15. protected $name = '统一运费';
  16. /**
  17. * @var PreOrder
  18. */
  19. protected $order;
  20. /**
  21. * 金额
  22. * @var float
  23. */
  24. protected $freightAmount;
  25. /*
  26. * 排序:数值越低权重越大
  27. */
  28. protected $weight;
  29. /**
  30. * BaseFreight constructor.
  31. * @param PreOrder $order
  32. * @param $weight
  33. */
  34. public function __construct(PreOrder $order, $weight = 0)
  35. {
  36. $this->order = $order;
  37. $this->weight = $weight;
  38. }
  39. /**
  40. * 返回运费金额
  41. * @return float|mixed
  42. */
  43. public function getAmount()
  44. {
  45. if (!isset($this->freightAmount)) {
  46. $this->freightAmount = $this->_getAmount();
  47. }
  48. return $this->freightAmount;
  49. }
  50. protected function _getAmount()
  51. {
  52. // 统一运费取所有商品统一运费的最大值
  53. $price = $this->order->orderGoods->unique('goods_id')->max(function ($orderGoods) {
  54. /**
  55. * @var $orderGoods OrderGoods
  56. */
  57. if($orderGoods->isFreeShipping())
  58. {
  59. // 免邮费
  60. return 0;
  61. }
  62. if(!isset($orderGoods->goods->hasOneGoodsDispatch)){
  63. // 没有找到商品配送关联模型
  64. return 0;
  65. }
  66. if ($orderGoods->goods->hasOneGoodsDispatch->dispatch_type == GoodsDispatch::UNIFY_TYPE) {
  67. // 商品配送类型为 统一运费
  68. return $orderGoods->goods->hasOneGoodsDispatch->dispatch_price;
  69. }
  70. return 0;
  71. });
  72. return $price;
  73. }
  74. public function needDispatch()
  75. {
  76. // 虚拟物品不需要配送
  77. if ($this->order->is_virtual) {
  78. return false;
  79. }
  80. return true;
  81. }
  82. }