| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2021/4/12
- * Time: 16:45
- */
- namespace app\frontend\modules\cart\models;
- use app\common\models\BaseModel;
- use app\framework\Http\Request;
- use app\frontend\modules\cart\discount\models\PreCartDiscount;
- use app\frontend\modules\cart\manager\CartGoodsCollection;
- /**
- * Class ShopCart
- * @property CartGoodsCollection carts
- * @package app\frontend\modules\cart\models
- */
- class ShopCart extends BaseModel
- {
- protected $member;
- protected $request;
- protected $disable;
- /**
- * @var CartGoodsCollection
- */
- protected $allCartGoods;
- protected $failureCart;
- public function init(CartGoodsCollection $carts, $member = null, $request = null)
- {
- $this->member = $member;
- $this->setRequest($request);
- $this->allCartGoods($carts);
- $this->setCarts();
- $this->cartValidate();
- $this->setInitAttributes();
- }
- public function allCartGoods(CartGoodsCollection $carts)
- {
- $this->allCartGoods = $carts;
- }
- //注入店铺的购物车商品记录
- public function setCarts()
- {
- $normalCarts = $this->allCartGoods->filterNormalGoods();
- $this->setRelation('carts', $normalCarts);
- $this->carts->setShop($this);
- }
- //注入店铺的购物车商品记录
- public function setFailureCart()
- {
- return $this->allCartGoods->filterInvalidGoods();
- }
- public function getFailureCart()
- {
- if (!isset($this->failureCart)) {
- $this->failureCart = $this->setFailureCart();
- }
- return $this->failureCart;
- }
- public function cartValidate()
- {
- $this->carts->cartValidate();
- }
- public function setRequest(Request $request)
- {
- $this->request = $request;
- }
- /**
- * 获取request对象
- * @return Request
- */
- public function getRequest()
- {
- if (!isset($this->request)) {
- $this->request = request();
- }
- return $this->request;
- }
- /**
- * 获取url中关于本订单的参数
- * @param null $key
- * @return mixed
- */
- public function getParams($key = null)
- {
- $result = collect(json_decode($this->getRequest()->input('orders'), true))->where('pre_id', $this->pre_id)->first();
- if (isset($key)) {
- return $result[$key];
- }
- return $result;
- }
- public function isCheckedCartGoods()
- {
- return $this->carts->isCheckedCartGoods();
- }
- /**
- * 设置前端是否可选状态
- * @param ShopCart $firstShop 首选购物车店铺
- *
- */
- public function setDisable($firstShop)
- {
- $isDisable = is_null($firstShop)?false:$firstShop->isAlone();
- //是特殊店铺、已有勾选商品
- if ($this->isAlone() && !is_null($firstShop)) {
- //勾选的商品与未勾选的商品得店铺对应
- $bool = !($isDisable && $firstShop->getShopId() == $this->getShopId());
- $this->special($bool); //特殊商品
- } else {
- $this->normal($isDisable); //正常商品
- }
- }
- /**
- * 特殊商品是否可选状态
- */
- public function special($isDisable)
- {
- $this->disable = $isDisable;
- $this->setCartDisable($isDisable);
- }
- /**
- * 正常商品是否可选状态
- */
- public function normal($isDisable)
- {
- $this->disable = $isDisable;
- $this->setCartDisable($isDisable);
- }
- /**
- * 禁用购物车商品选择
- * @return mixed
- */
- public function setCartDisable($isDisable)
- {
- return $this->carts->setCartDisable($isDisable);
- }
- /**
- * 判断这个店铺只能单独下单
- * @return bool
- */
- public function isAlone()
- {
- return false;
- }
- //店铺活动优惠满减
- public function getDiscountActivity()
- {
- if(!\Setting::get('enoughReduce.open')){
- return [];
- }
- // 获取满减设置,按enough倒序
- $settings = \Setting::get('enoughReduce.enoughReduce');
- if (empty($settings)) {
- return [];
- }
- $str = '';
- foreach ($settings as $setting) {
- $str .= empty($str) ? '满'.$setting['enough'].'减'.$setting['reduce']:',满'.$setting['enough'].'减'.$setting['reduce'];
- }
- if (\Setting::get('enoughReduce.freeFreight.open')) {
- // 设置为0 全额包邮
- if (\Setting::get('enoughReduce.freeFreight.enough') === 0 || \Setting::get('enoughReduce.freeFreight.enough') === '0') {
- $str.= empty($str) ? '全额包邮':',全额包邮';
- } elseif(\Setting::get('enoughReduce.freeFreight.enough')) {
- $str.= empty($str) ? '满额'.\Setting::get('enoughReduce.freeFreight.enough').'包邮':',满额'.\Setting::get('enoughReduce.freeFreight.enough').'包邮';
- }
- }
- return [[
- 'name'=> '满减',
- 'code'=> 'enoughReduce',
- 'type'=> 'string',
- 'desc'=> $str,
- ]];
- }
- /**
- * 获取总金额
- * @return int|mixed
- * @throws \app\common\exceptions\AppException
- */
- public function getDiscountAmount($total)
- {
- if(!\Setting::get('enoughReduce.open')){
- return 0;
- }
- // 获取满减设置,按enough倒序
- $settings = collect(\Setting::get('enoughReduce.enoughReduce'));
- if (empty($settings)) {
- return 0;
- }
- $settings = $settings->sortByDesc(function ($setting) {
- return $setting['enough'];
- });
- // 订单总价满足金额,则返回优惠金额
- foreach ($settings as $setting) {
- if ($total >= $setting['enough']) {
- return min($setting['reduce'],$total);
- }
- }
- return 0;
- }
- protected $cartDiscounts;
- public function cartDiscounts()
- {
- if (isset($this->cartDiscounts)) {
- return $this->cartDiscounts;
- }
- $cartGoodsDiscounts = $this->carts->getCartGoodsDiscounts();
- $discountsItems = collect([]);
- // 按每个种类的优惠分组 求金额的和
- $cartGoodsDiscounts->each(function ($cartGoodsDiscount) use ($discountsItems) {
- // 新类型添加
- if ($discountsItems->where('code', $cartGoodsDiscount->code)->isEmpty()) {
- $preCartDiscount = new PreCartDiscount([
- 'code' => $cartGoodsDiscount->code,
- 'amount' => $cartGoodsDiscount->amount,
- 'name' => $cartGoodsDiscount->name,
- ]);
- $discountsItems->push($preCartDiscount);
- return;
- }
- // 已存在的类型累加
- $discountsItems->where('code', $cartGoodsDiscount->code)->first()->amount += $cartGoodsDiscount->amount;
- });
- return $this->cartDiscounts = $discountsItems;
- }
- protected $cartExtraCharges;
- /**
- * 其他费用
- * @return \Illuminate\Support\Collection
- */
- public function cartExtraCharges()
- {
- if (isset($this->cartExtraCharges)) {
- return $this->cartExtraCharges;
- }
- $cartGoodsExtraCharges = $this->carts->getCartGoodsExtraCharges();
- $itemsAggregate = collect([]);
- // 按每个种类分组 求金额的和
- $cartGoodsExtraCharges->each(function ($cartGoodsItem) use ($itemsAggregate) {
- // 新类型添加
- if ($itemsAggregate->where('code', $cartGoodsItem->code)->isEmpty()) {
- $model = new CartBaseModel([
- 'code' => $cartGoodsItem->code,
- 'amount' => $cartGoodsItem->amount,
- 'name' => $cartGoodsItem->name,
- ]);
- $itemsAggregate->push($model);
- return;
- }
- // 已存在的类型累加
- $itemsAggregate->where('code', $cartGoodsItem->code)->first()->amount += $cartGoodsItem->amount;
- });
- return $this->cartExtraCharges = $itemsAggregate;
- }
- protected $cartDeductions;
- /**
- * 抵扣
- * @return \Illuminate\Support\Collection
- */
- public function cartDeductions()
- {
- if (isset($this->cartDeductions)) {
- return $this->cartDeductions;
- }
- $cartDeductions = $this->carts->getCartGoodsDeductions();
- $itemsAggregate = collect([]);
- // 按每个种类分组 求金额的和
- $cartDeductions->each(function ($cartGoodsItem) use ($itemsAggregate) {
- // 新类型添加
- if ($itemsAggregate->where('code', $cartGoodsItem->code)->isEmpty()) {
- $model = new CartBaseModel([
- 'code' => $cartGoodsItem->code,
- 'amount' => $cartGoodsItem->amount,
- 'name' => $cartGoodsItem->name,
- ]);
- $itemsAggregate->push($model);
- return;
- }
- // 已存在的类型累加
- $itemsAggregate->where('code', $cartGoodsItem->code)->first()->amount += $cartGoodsItem->amount;
- });
- return $this->cartDeductions = $itemsAggregate;
- }
- public function getGoodsPrice()
- {
- return $this->carts->getPrice();
- }
- public function getPrice()
- {
- //商品总价
- return $this->getEstimatedPrice();
- }
- public function getEstimatedPrice()
- {
- return $this->carts->getEstimatedPrice();
- }
- public function getCode()
- {
- return 'shop';
- }
- public function getName()
- {
- return \Setting::get('shop.shop')['name']?:'自营';
- }
- public function getShopId()
- {
- return md5('shop');
- }
- public function getLink()
- {
- return '';
- }
- public function getMerchantId()
- {
- return 0;
- }
- public function setInitAttributes()
- {
- $attributes = array(
- 'is_alone' => $this->isAlone(),
- 'link' => $this->getLink(),
- 'code' => $this->getCode(),
- 'name' => $this->getName(),
- 'merchant_id' => $this->getMerchantId(),
- 'shop_id' => $this->getShopId(),
- 'discount_activity' => $this->getDiscountActivity(),
- 'price' => sprintf('%.2f', $this->getPrice()),
- );
- $this->setRawAttributes($attributes);
- }
- public function beforeFormat()
- {
- $this->setAttribute('disable',$this->disable);
- }
- /**
- * @return array
- */
- public function toArray()
- {
- $this->beforeFormat();
- return parent::toArray();
- }
- public function goods()
- {
- return $this->carts->first()->goods;
- }
- public function getPriceAttribute()
- {
- return sprintf('%.2f', $this->getPrice());
- }
- public function getDiscountActivityAttribute()
- {
- return $this->getDiscountActivity();
- }
- public function getCodeAttribute()
- {
- return $this->getCode();
- }
- public function getNameAttribute()
- {
- return $this->getName();
- }
- public function getShopIdAttribute()
- {
- return $this->getShopId();
- }
- }
|