OrderTaxFeeManager.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020/8/20
  6. * Time: 16:55
  7. */
  8. namespace app\frontend\modules\order\taxFee;
  9. use app\common\modules\shop\ShopConfig;
  10. use Illuminate\Support\Collection;
  11. use app\frontend\models\order\PreOrderTaxFee;
  12. use app\frontend\modules\order\models\PreOrder;
  13. use app\frontend\modules\orderGoods\models\PreOrderGoods;
  14. class OrderTaxFeeManager
  15. {
  16. public $orderTaxFee;
  17. /**
  18. * @var PreOrder
  19. */
  20. protected $order;
  21. protected $taxFee;
  22. /**
  23. * OrderFee constructor.
  24. * @param PreOrder $order
  25. */
  26. public function __construct(PreOrder $order)
  27. {
  28. $this->order = $order;
  29. // 订单含税费集合
  30. $order->setRelation('orderTaxFees', new Collection());
  31. }
  32. public function getOrderTaxFees()
  33. {
  34. if (!isset($this->orderTaxFee)) {
  35. $orderTaxFees = $this->getEnableTaxFee()->map(function (BaseOrderTaxFee $taxFee) {
  36. $orderServiceFee = new PreOrderTaxFee();
  37. $orderServiceFee->init($taxFee, $this->order);
  38. return $orderServiceFee;
  39. });
  40. $this->orderTaxFee = $orderTaxFees;
  41. }
  42. return $this->orderTaxFee;
  43. }
  44. public function getEnableTaxFee()
  45. {
  46. if (!isset($this->taxFee)) {
  47. $this->taxFee = collect();
  48. $configs = ShopConfig::current()->get('shop-foundation.order-tax-fee');
  49. foreach ($configs as $configItem) {
  50. $class = call_user_func($configItem['class'], $this->order);
  51. if ($class->enable()) {
  52. $this->taxFee->put($configItem['key'], $class);
  53. }
  54. }
  55. }
  56. return $this->taxFee;
  57. }
  58. public function getAmount()
  59. {
  60. $this->addGoodsTaxFee();
  61. return $this->getOrderTaxFees()->sum(function (PreOrderTaxFee $orderTaxFee) {
  62. if ($orderTaxFee->getTaxFee()->isChecked()) {
  63. // 每一种含税费(可能是负数的优惠,也可能是正数的加钱,一切看配置)
  64. return $orderTaxFee->getTaxFee()->getAmount();
  65. }
  66. return 0;
  67. });
  68. }
  69. private function addGoodsTaxFee()
  70. {
  71. // 将所有订单商品的优惠
  72. $orderGoodsTaxFees = $this->order->orderGoods->reduce(function (Collection $result, PreOrderGoods $aOrderGoods) {
  73. return $result->merge($aOrderGoods->getOrderGoodsTaxFees());
  74. }, collect());
  75. $preOrderTaxFee = collect([]);
  76. // 按每个种类的优惠分组 求金额的和
  77. $orderGoodsTaxFees->each(function ($orderGoodsTaxFee) use ($preOrderTaxFee) {
  78. // 新类型添加
  79. if ($this->order->orderTaxFees->where('fee_code', $orderGoodsTaxFee->fee_code)->isEmpty()) {
  80. if ($preOrderTaxFee->where('discount_code', $orderGoodsTaxFee->fee_code)->isEmpty()) {
  81. $preTaxFee = new PreOrderTaxFee([
  82. 'fee_code' => $orderGoodsTaxFee->fee_code,
  83. 'amount' => $orderGoodsTaxFee->amount,
  84. 'name' => $orderGoodsTaxFee->name,
  85. ]);
  86. $preOrderTaxFee->push($preTaxFee);
  87. return;
  88. }
  89. // 已存在的类型累加
  90. $preOrderTaxFee->where('fee_code', $orderGoodsTaxFee->fee_code)->first()->amount += $orderGoodsTaxFee->amount;
  91. }
  92. });
  93. $preOrderTaxFee->each(function (PreOrderTaxFee $orderTaxFee) {
  94. $orderTaxFee->setOrder($this->order);
  95. });
  96. }
  97. /**
  98. * @param $code
  99. * @return BaseOrderTaxFee
  100. */
  101. public function getAmountByCode($code)
  102. {
  103. return $this->getEnableTaxFee()[$code];
  104. }
  105. }