GroupManager.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2021/4/6
  6. * Time: 18:11
  7. */
  8. namespace app\frontend\modules\cart\services;
  9. use app\frontend\modules\cart\manager\CartGroupCollection;
  10. use app\frontend\modules\cart\manager\MemberCartCollection;
  11. use app\frontend\modules\cart\models\ShopCart;
  12. use Illuminate\Database\Eloquent\Collection;
  13. use app\common\models\Member;
  14. use app\common\models\MemberCart;
  15. /**
  16. * Class GroupManager
  17. * @package app\frontend\modules\cart\services
  18. */
  19. class GroupManager extends Collection
  20. {
  21. /**
  22. * @var MemberCartCollection
  23. */
  24. protected $memberCart;
  25. /**
  26. * @var Member
  27. */
  28. protected $member;
  29. /**
  30. * @var CartGroupCollection
  31. */
  32. protected $group;
  33. public function init($cartList, $member = null)
  34. {
  35. $this->member = $member;
  36. $this->setMemberCarts($cartList);
  37. //分组
  38. $this->cartGroup();
  39. //区分选择
  40. $this->group->firstCheckedCart();
  41. }
  42. //失效购物车
  43. protected function invalidCart()
  44. {
  45. $invalidCart = $this->group->getCartInvalidGoods();
  46. $invalidCart->map(function ($cart) {
  47. $cart->goods_title = $cart->goods->title;
  48. $cart->goods_thumb = yz_tomedia($cart->goods->thumb);
  49. $cart->goods_price = $cart->goodsOption?$cart->goodsOption->product_price:$cart->goods->price;
  50. $cart->goods_option_title = $cart->goodsOption?$cart->goodsOption->title:'';
  51. unset($cart->hasManyAddress);
  52. unset($cart->hasManyMemberAddress);
  53. unset($cart->goods);
  54. unset($cart->goodsOption);
  55. return $cart;
  56. });
  57. return $invalidCart;
  58. }
  59. protected function setMemberCarts($cartList)
  60. {
  61. $result = new MemberCartCollection($cartList);
  62. $this->memberCart = $result;
  63. }
  64. public function cartList()
  65. {
  66. $data = $this->attributesToArray();
  67. $data['list'] = $this->group->filter(function (ShopCart $shopCart) {
  68. return $shopCart->carts->isNotEmpty();
  69. })->values()->toArray();
  70. $data['invalid_cart'] = $this->invalidCart();
  71. return $data;
  72. }
  73. public function attributesToArray()
  74. {
  75. return [
  76. 'total_goods_price' => sprintf('%.2f', $this->group->sum(function (ShopCart $shop) {
  77. return $shop->getGoodsPrice();
  78. })),
  79. 'total_amount' => sprintf('%.2f', $this->group->sum('price')),
  80. 'discount_amount_items' => $this->getDiscountAmountItems(),
  81. 'deduction_amount_items' => $this->getDeductionAmountItems(),
  82. 'extra_charges_amount_items' => $this->getExtraChargesAmountItems(),
  83. 'total_discount_amount' => sprintf('%.2f', $this->group->getSingleCartDiscount()),
  84. 'total_deduction_amount' => sprintf('%.2f', $this->group->getSingleCartDeductions()),
  85. 'total_extra_charges_amount' => sprintf('%.2f', $this->group->getSingleCartExtraCharges()),
  86. 'sharin_is_open' => $this->sharinIsOpen(),
  87. 'point_mall_total' => $this->pointMallTotal(),
  88. ];
  89. }
  90. /**
  91. * [sharinIsOpen 分享购物车插件是否开启]
  92. * @return [type] [description]
  93. */
  94. protected function sharinIsOpen()
  95. {
  96. $sharin_is_open = 0;
  97. if (app('plugins')->isEnabled('cart-sharing') && \Setting::get('plugin.cart-sharing.is_open') == "1") {
  98. $sharin_is_open = 1;
  99. }
  100. return $sharin_is_open;
  101. }
  102. /**
  103. * 计算所有选中购物车中积分商城抵扣积分总和
  104. * @return int
  105. */
  106. protected function pointMallTotal()
  107. {
  108. $point = 0;
  109. $this->group->map(function ($cartList) use (&$point){
  110. $cartList->carts->map(function ($cart) use (&$point) {
  111. if ($cart->pointGoods && $cart->checked) {
  112. $point += bcmul($cart->pointGoods->point,$cart->total,2);
  113. }
  114. });
  115. });
  116. return $point;
  117. }
  118. /**
  119. * 按商品类型进行购物车分组
  120. */
  121. public function cartGroup()
  122. {
  123. $groups = $this->memberCart->groupByGroupId()->values();
  124. $member = $this->member;
  125. $request = request();
  126. $cartGroupCollection = $groups->map(function (MemberCartCollection $memberCartCollection) use ($member, $request) {
  127. return $memberCartCollection->getGroup($memberCartCollection->getPlugin(), $member, $request);
  128. });
  129. $this->group = new CartGroupCollection($cartGroupCollection->all());
  130. }
  131. /**
  132. * @return mixed
  133. */
  134. private function getDiscountAmountItems()
  135. {
  136. // 按照code合并
  137. $itemsAggregate = [];
  138. foreach ($this->group->getCartDiscounts() as $orderDiscount) {
  139. if (isset($itemsAggregate[$orderDiscount['code']])) {
  140. $itemsAggregate[$orderDiscount['code']]['amount'] += $orderDiscount['amount'];
  141. } else {
  142. if ($orderDiscount['amount'] > 0) {
  143. $itemsAggregate[$orderDiscount['code']] = [
  144. 'code' => $orderDiscount['code'],
  145. 'name' => $orderDiscount['name'],
  146. 'amount' => $orderDiscount['amount'],
  147. ];
  148. }
  149. }
  150. }
  151. foreach ($itemsAggregate as &$item) {
  152. $item['amount'] = sprintf('%.2f', $item['amount']);
  153. }
  154. return array_values($itemsAggregate);
  155. }
  156. /**
  157. * @return mixed
  158. */
  159. private function getDeductionAmountItems()
  160. {
  161. // 按照code合并
  162. $itemsAggregate = [];
  163. foreach ($this->group->getCartDeductions() as $cartItem) {
  164. if (isset($itemsAggregate[$cartItem['code']])) {
  165. $itemsAggregate[$cartItem['code']]['amount'] += $cartItem['amount'];
  166. } else {
  167. if ($cartItem['amount'] > 0) {
  168. $itemsAggregate[$cartItem['code']] = [
  169. 'code' => $cartItem['code'],
  170. 'name' => $cartItem['name'],
  171. 'amount' => $cartItem['amount'],
  172. ];
  173. }
  174. }
  175. }
  176. foreach ($itemsAggregate as &$item) {
  177. $item['amount'] = sprintf('%.2f', $item['amount']);
  178. }
  179. return array_values($itemsAggregate);
  180. }
  181. /**
  182. * @return mixed
  183. */
  184. private function getExtraChargesAmountItems()
  185. {
  186. // 按照code合并
  187. $itemsAggregate = [];
  188. foreach ($this->group->getCartExtraCharges() as $cartItem) {
  189. if (isset($itemsAggregate[$cartItem['code']])) {
  190. $itemsAggregate[$cartItem['code']]['amount'] += $cartItem['amount'];
  191. } else {
  192. if ($cartItem['amount'] > 0) {
  193. $itemsAggregate[$cartItem['code']] = [
  194. 'code' => $cartItem['code'],
  195. 'name' => $cartItem['name'],
  196. 'amount' => $cartItem['amount'],
  197. ];
  198. }
  199. }
  200. }
  201. foreach ($itemsAggregate as &$item) {
  202. $item['amount'] = sprintf('%.2f', $item['amount']);
  203. }
  204. return array_values($itemsAggregate);
  205. }
  206. }