问题说明:门店在有自建分类权限情况下品类券关联分类不同
修改文件:app/services/activity/coupon/StoreCouponIssueServices.php
修改方法:getCanUseCoupon()
public function getCanUseCoupon(int $uid, array $cartList = [], array $promotions = [], int $store_id = 0, bool $isMax = true)
{
$where = ['receive_type' => 1, 'category' => 1];
//传入是付费会员 获取付费会员券
$isVip = $this->getItem('isVip', 0);
if ($uid && !$isVip) {
/** @var UserServices $userService */
$userService = app()->make(UserServices::class);
$isVip = $userService->checkUserIsSvip($uid);
}
if ($isVip) {
unset($where['category']);
}
/** @var StoreCouponUserServices $coupServices */
$coupServices = app()->make(StoreCouponUserServices::class);
//用户持有有效券
$userCoupons = $coupServices->getUserAllCoupon($uid);
if ($userCoupons) {
$where['not_id'] = array_column($userCoupons, 'cid');
}
$where['relation_id'] = [0, $store_id];
$counpons = $this->dao->getValidList($where, '*', 0, 0, ['used' => function ($query) use ($uid) {
$query->where(['uid' => $uid]);
}]);
//用户已经领取的 && 没有领取的
$counpons = array_merge($counpons, $userCoupons);
$result = [];
if ($counpons) {
$promotionsList = [];
if ($promotions) {
$promotionsList = array_combine(array_column($promotions, 'id'), $promotions);
}
//验证是否适用门店
$isApplicableStore = function ($couponInfo) use ($store_id) {
if (isset($couponInfo['applicable_type']) && isset($couponInfo['applicable_store_id']) && isset($couponInfo['coupon_issue_type']) && isset($couponInfo['relation_id'])) {
$applicable_store_id = is_array($couponInfo['applicable_store_id']) ? $couponInfo['applicable_store_id'] : explode(',', $couponInfo['applicable_store_id']);
if (!$store_id && in_array($couponInfo['coupon_issue_type'],[1,2]) || $store_id && ($couponInfo['applicable_type'] == 0 || ($couponInfo['applicable_type'] == 2 && !in_array($store_id, $applicable_store_id)) || ($couponInfo['coupon_issue_type'] == 1 && $couponInfo['relation_id'] != $store_id))) {
return false;
}
}
return true;
};
//
$isOverlay = function ($cart) use ($promotionsList) {
$productInfo = $cart['productInfo'] ?? [];
if (!$productInfo) {
return false;
}
//门店独立商品 不使用优惠券
// $isBranchProduct = isset($productInfo['type']) && isset($productInfo['pid']) && $productInfo['type'] == 1 && !$productInfo['pid'];
// if ($isBranchProduct) {
// return false;
// }
if (isset($cart['promotions_id']) && $cart['promotions_id']) {
foreach ($cart['promotions_id'] as $key => $promotions_id) {
$promotions = $promotionsList[$promotions_id] ?? [];
if ($promotions && $promotions['promotions_type'] != 4) {
$overlay = is_string($promotions['overlay']) ? explode(',', $promotions['overlay']) : $promotions['overlay'];
if (!in_array(5, $overlay)) {
return false;
}
}
}
}
return true;
};
/** @var StoreProductCategoryServices $storeCategoryServices */
$storeCategoryServices = app()->make(StoreProductCategoryServices::class);
/** @var StoreBrandServices $storeBrandServices */
$storeBrandServices = app()->make(StoreBrandServices::class);
foreach ($counpons as $coupon) {
if (isset($coupon['used'])) {
$isValid = false;
foreach ($coupon['used'] as $item) {//是否存在有效的
if ($item['start_time']<= time() && $item['end_time'] >= time()) {
$isValid = true;
continue;
}
}
if (!$isValid) {
if (count($coupon['used']) >= $coupon['quantity_count'] && !$coupon['is_claimed']) {//不能再次领取了
continue;
} else {//领取的是过期无效,重新领取
$coupon['used'] = [];
}
}
}
if (!$isApplicableStore($coupon)) {//不适用门店跳过
continue;
}
$price = 0;
$count = 0;
if (isset($coupon['coupon_applicable_type'])) {//已经领取 有效优惠券
$coupon['type'] = $coupon['coupon_applicable_type'];
$coupon['used'][] = ['id' => $coupon['id'], 'cid' => $coupon['cid']];
}
//&& in_array($promotions['promotions_type'], $overlay)
switch ($coupon['type']) {
case 0:
foreach ($cartList as $cart) {
if (!$isOverlay($cart) || (isset($cart['cart_type']) && $cart['cart_type'] > 0)) continue;
$price = bcadd((string)$price, (string)$cart['pay_price'], 2);
$count++;
}
break;
case 1://品类券
$cateGorys = $storeCategoryServices->getAllById((int)$coupon['category_id']);
if ($cateGorys) {
$cateIds = array_column($cateGorys, 'id');
foreach ($cartList as $cart) {
if (!$isOverlay($cart) || (isset($cart['cart_type']) && $cart['cart_type'] > 0)) continue;
if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds) || isset($cart['productInfo']['store_cate_id']) && array_intersect(explode(',', $cart['productInfo']['store_cate_id']), $cateIds)) {
$price = bcadd((string)$price, (string)$cart['pay_price'], 2);
$count++;
}
}
}
break;
case 2://商品券
foreach ($cartList as $cart) {
if (!$isOverlay($cart) || (isset($cart['cart_type']) && $cart['cart_type'] > 0)) continue;
$product_id = isset($cart['productInfo']['id']) && $cart['productInfo']['id'] ? $cart['productInfo']['id'] : ($cart['product_id'] ?? 0);
if ($product_id && in_array($product_id, explode(',', $coupon['product_id']))) {
$price = bcadd((string)$price, (string)$cart['pay_price'], 2);
$count++;
}
}
break;
case 3://品牌券
$brands = $storeBrandServices->getAllById((int)$coupon['brand_id']);
if ($brands) {
$brandIds = array_column($brands, 'id');
foreach ($cartList as $cart) {
if (!$isOverlay($cart) || (isset($cart['cart_type']) && $cart['cart_type'] > 0)) continue;
if (isset($cart['productInfo']['brand_id']) && in_array($cart['productInfo']['brand_id'], $brandIds)) {
$price = bcadd((string)$price, (string)$cart['pay_price'], 2);
$count++;
}
}
}
break;
}
if ($count && $coupon['use_min_price'] <= $price) {
//满减券
if ($coupon['coupon_type'] == 1) {
$couponPrice = $coupon['coupon_price'] > $price ? $price : $coupon['coupon_price'];
} else {
if ($coupon['coupon_price'] <= 0) {//0折
$couponPrice = $price;
} else if ($coupon['coupon_price'] >= 100) {
$couponPrice = 0;
} else {
$truePrice = (float)bcmul((string)$price, bcdiv((string)$coupon['coupon_price'], '100', 2), 2);
$couponPrice = (float)bcsub((string)$price, (string)$truePrice, 2);
}
if ($coupon['top_discount_price'] > 0 && $couponPrice > $coupon['top_discount_price']) {
$couponPrice = $coupon['top_discount_price'];
}
}
$coupon['coupon_price'] = floatval($coupon['coupon_price']);
$coupon['use_min_price'] = floatval($coupon['use_min_price']);
$coupon['true_coupon_price'] = $couponPrice;
$result[] = $coupon;
}
}
}
if ($result) {
$trueCouponPrice = array_column($result, 'true_coupon_price');
array_multisort($trueCouponPrice, SORT_DESC, $result);
}
//最优的一个
if ($isMax) {
$useCoupon = $result[0] ?? [];
return $useCoupon;
}
return $result;
}
