OrderFreightDeductManager.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: blank
  5. * Date: 2022/4/13
  6. * Time: 18:18
  7. */
  8. namespace app\frontend\modules\dispatch\deduction;
  9. use app\frontend\modules\deduction\models\Deduction;
  10. use app\frontend\modules\deduction\OrderGoodsDeductionCollection;
  11. use app\frontend\modules\order\models\PreOrder;
  12. class OrderFreightDeductManager
  13. {
  14. /**
  15. * @var PreOrder
  16. */
  17. private $order;
  18. private $orderFreightDeductionCollection;
  19. public function __construct(PreOrder $order)
  20. {
  21. $this->order = $order;
  22. }
  23. /**
  24. * @return OrderGoodsDeductionCollection
  25. */
  26. public function getOrderFreightDeductions()
  27. {
  28. if (!isset($this->orderFreightDeductionCollection)) {
  29. $this->orderFreightDeductionCollection = $this->_getOrderFreightDeduction();
  30. }
  31. return $this->orderFreightDeductionCollection;
  32. }
  33. /**
  34. * 获取并订单抵扣项并载入到订单模型中
  35. */
  36. private function _getOrderFreightDeduction()
  37. {
  38. $orderDeductions = $this->getEnableDeductions()->map(function (Deduction $deduction) {
  39. $orderDeduction = new PreOrderFreightDeductionCalculation();
  40. $orderDeduction->init($deduction, $this->order->getFreightManager());
  41. return $orderDeduction;
  42. })->values();
  43. // ->filter(function (PreOrderFreightDeduction $freightDeduction) {
  44. // return $freightDeduction->isEnableDeductFreight();
  45. // })
  46. return $orderDeductions;
  47. }
  48. /**
  49. * 开启的抵扣项
  50. * @return \app\framework\Database\Eloquent\Collection
  51. */
  52. private function getEnableDeductions()
  53. {
  54. //由于获取开启抵扣都是相同的所以这里把这部分代码提取出来
  55. return \app\frontend\modules\deduction\EnableDeductionService::getInstance()->getEnableDeductions($this->order);
  56. }
  57. }