Bladeren bron

BUG 修复

bianjunhui 6 dagen geleden
bovenliggende
commit
a7b056bd81

+ 3 - 3
packages/Longyi/RewardPoints/src/Http/Controllers/Admin/TransactionController.php

@@ -62,7 +62,7 @@ class TransactionController extends Controller
         if ($viewType === 'earned') {
             $query->where('amount', '>', 0);
         } elseif ($amountType === 'earned') {
-            $query->where('amount', '>', 0);
+            $query->where('amount', '>', 0)->where('status',1);
         } elseif ($amountType === 'redeemed') {
             $query->where('amount', '<', 0);
         }
@@ -71,11 +71,11 @@ class TransactionController extends Controller
 
         // 统计数据 - 根据视图类型调整
         if ($viewType === 'earned') {
-            $totalEarned = (clone $query)->sum('amount');
+            $totalEarned = (clone $query)->where('status',1)->sum('amount');
             $totalRedeemed = 0;
             $totalTransactions = (clone $query)->count();
         } else {
-            $totalEarned = (clone $query)->where('amount', '>', 0)->sum('amount');
+            $totalEarned = (clone $query)->where('status',1)->where('amount', '>', 0)->sum('amount');
             $totalRedeemed = abs((clone $query)->where('amount', '<', 0)->sum('amount'));
             $totalTransactions = (clone $query)->count();
         }

+ 15 - 2
packages/Longyi/RewardPoints/src/Listeners/CustomerEvents.php

@@ -169,8 +169,21 @@ class CustomerEvents
             return;
         }
 
-        // 获取登录积分
-        $loginPoints = (int) $loginRule->reward_point;
+        // 检查规则是否适用于该客户
+        if (!$loginRule->isApplicableToCustomer($customer)) {
+            Log::info('Login rule not applicable to this customer group', [
+                'customer_id' => $customer->id,
+                'customer_group_id' => $customer->customer_group_id ?? null
+            ]);
+            return;
+        }
+
+        // 获取登录积分(根据客户群组)
+        $customerGroupId = $customer->customer_group_id ?? null;
+        $loginPoints = $customerGroupId !== null
+            ? $loginRule->getRewardPointForCustomerGroup($customerGroupId)
+            : (int) $loginRule->reward_point;
+
         if ($loginPoints <= 0) {
             Log::info('Login points is 0 or negative, skipping login points');
             return;

+ 33 - 4
packages/Longyi/RewardPoints/src/Listeners/OrderEvents.php

@@ -6,7 +6,7 @@ use Longyi\RewardPoints\Repositories\RewardPointRepository;
 use Longyi\RewardPoints\Models\RewardActiveRule;
 use Longyi\RewardPoints\Models\RewardPointHistory;
 use Longyi\RewardPoints\Services\GrowthValueService;
-
+use Illuminate\Support\Facades\Log;
 class OrderEvents
 {
     protected $rewardPointRepository;
@@ -32,8 +32,18 @@ class OrderEvents
         if (!$rule) {
             return;
         }
+        $customer = $order->customer;
+        // 检查规则是否适用于该客户
+        if (!$rule->isApplicableToCustomer($customer)) {
+            Log::info('Order rule not applicable to this customer group', [
+                'order_id' => $order->id,
+                'customer_id' => $order->customer_id,
+                'customer_group_id' => $customer->customer_group_id ?? null
+            ]);
+            return;
+        }
 
-        $points = $this->calculateOrderPoints($order, $rule);
+        $points = $this->calculateOrderPoints($order, $rule, $customer);
 
         if ($points > 0) {
             // 支付订单后,积分状态为 PENDING(待确认),不计入用户总积分
@@ -139,11 +149,30 @@ class OrderEvents
         $this->growthValueService->handleOrderCompleted($order);
     }
 
-    protected function calculateOrderPoints($order, $rule)
+    protected function calculateOrderPoints($order, $rule, $customer)
     {
-        $pointsPerCurrency = 10;
+        // 获取积分倍率
+        $pointsPerCurrency = $this->getPointsPerCurrency($rule, $customer);
+
+        if ($pointsPerCurrency <= 0) {
+            return 0;
+        }
+
         return floor($order->base_grand_total * $pointsPerCurrency);
     }
 
+    protected function getPointsPerCurrency($rule, $customer)
+    {
+        if ($rule->enable_different_points_by_group && $customer) {
+            $customerGroupId = $customer->customer_group_id ?? null;
+            if ($customerGroupId !== null) {
+                return $rule->getRewardPointForCustomerGroup($customerGroupId);
+            }
+        }
+
+        // 不使用群组差异化时,直接返回默认积分值
+        return (int)$rule->reward_point;
+    }
+
 }
 

+ 23 - 3
packages/Longyi/RewardPoints/src/Listeners/ReviewEvents.php

@@ -19,14 +19,34 @@ class ReviewEvents
         $rule = RewardActiveRule::where('type_of_transaction', RewardActiveRule::TYPE_REVIEW)
             ->where('status', 1)
             ->first();
+        if (!$rule || !$review->customer_id) {
+            return;
+        }
+        // 获取客户信息
+        $customer = \Webkul\Customer\Models\Customer::find($review->customer_id);
+        if (!$customer) {
+            return;
+        }
+
+        // 检查规则是否适用于该客户
+        if (!$rule->isApplicableToCustomer($customer)) {
+            return;
+        }
+
+        // 获取积分(根据客户群组)
+        $customerGroupId = $customer->customer_group_id ?? null;
+        $points = $customerGroupId !== null
+            ? $rule->getRewardPointForCustomerGroup($customerGroupId)
+            : (int) $rule->reward_point;
 
-        if ($rule && $rule->reward_point > 0 && $review->customer_id) {
+        if ($points > 0) {
             $this->rewardPointRepository->addPoints(
                 $review->customer_id,
                 RewardActiveRule::TYPE_REVIEW,
-                $rule->reward_point,
+                $points,
                 null,
-                "Points earned for product review"
+                "Points earned for product review",
+                $rule
             );
         }
     }

+ 79 - 19
packages/Longyi/RewardPoints/src/Models/RewardActiveRule.php

@@ -44,41 +44,100 @@ class RewardActiveRule extends Model
     ];
 
     /**
-     * 获取客户群组积分设置
+     * 获取特定客户群组的积分值
      */
-    public function getCustomerGroupPointsAttribute()
+    public function getRewardPointForCustomerGroup($customerGroupId)
     {
         if ($this->enable_different_points_by_group) {
-            $groupPoints = json_decode($this->customer_group_ids, true);
-            return is_array($groupPoints) ? $groupPoints : [];
+            $groupPoints = $this->getCustomerGroupPointsAttribute();
+            return isset($groupPoints[$customerGroupId]) ? (int)$groupPoints[$customerGroupId] : 0;
         }
-        return [];
-    }
 
-    /**
-     * 设置客户群组积分设置
-     */
-    public function setCustomerGroupPointsAttribute($value)
-    {
-        if ($this->enable_different_points_by_group) {
-            $this->attributes['customer_group_ids'] = json_encode($value);
+        // 如果不启用不同群组不同积分,则检查 customer_group_ids 是否为空或包含当前群组
+        // 如果 customer_group_ids 为空,表示适用于所有群组
+        if (empty($this->customer_group_ids)) {
+            return (int)$this->reward_point;
+        }
+
+        // 检查当前群组是否在允许的群组列表中
+        $allowedGroups = explode(',', $this->customer_group_ids);
+        if (in_array((string)$customerGroupId, $allowedGroups)) {
+            return (int)$this->reward_point;
         }
+
+        // 如果群组不在允许列表中,返回 0(不赠送积分)
+        return 0;
     }
 
     /**
-     * 获取特定客户群组的积分值
+     * 检查规则是否适用于指定客户
      */
-    public function getRewardPointForCustomerGroup($customerGroupId)
+    public function isApplicableToCustomer($customer)
     {
+        // 检查 store_view 是否匹配
+        if ($this->store_view && $this->store_view !== '0') {
+            $storeViews = explode(',', $this->store_view);
+            $currentChannelCode = core()->getCurrentChannelCode();
+            if (!in_array($currentChannelCode, $storeViews)) {
+                return false;
+            }
+        }
+
+        // 如果不启用不同群组不同积分,检查 customer_group_ids
+        if (!$this->enable_different_points_by_group) {
+            // 如果 customer_group_ids 为空或空字符串,表示适用于所有群组
+            if (empty($this->customer_group_ids) || trim($this->customer_group_ids) === '') {
+                return true;
+            }
+
+            // 检查客户群组是否在允许列表中
+            $allowedGroups = array_map('trim', explode(',', $this->customer_group_ids));
+
+            // 获取客户群组ID
+            if ($customer instanceof \Webkul\Customer\Models\Customer) {
+                $customerGroupId = $customer->customer_group_id;
+            } elseif (is_object($customer) && property_exists($customer, 'customer_group_id')) {
+                $customerGroupId = $customer->customer_group_id;
+            } else {
+                return false;
+            }
+
+            if ($customerGroupId === null || $customerGroupId === '') {
+                return false;
+            }
+
+            return in_array((string)$customerGroupId, $allowedGroups);
+        }
+
+        // 如果启用不同群组不同积分,检查是否有为该群组配置积分
         if ($this->enable_different_points_by_group) {
             $groupPoints = $this->getCustomerGroupPointsAttribute();
-            return isset($groupPoints[$customerGroupId]) ? (int)$groupPoints[$customerGroupId] : 0;
+
+            if (empty($groupPoints)) {
+                return false;
+            }
+
+            // 获取客户群组ID
+            if ($customer instanceof \Webkul\Customer\Models\Customer) {
+                $customerGroupId = $customer->customer_group_id;
+            } elseif (is_object($customer) && property_exists($customer, 'customer_group_id')) {
+                $customerGroupId = $customer->customer_group_id;
+            } else {
+                return false;
+            }
+
+            if ($customerGroupId === null || $customerGroupId === '') {
+                return false;
+            }
+
+            // 检查是否为该群组配置了积分(即使为 0 也算配置了)
+            return isset($groupPoints[(string)$customerGroupId]) || isset($groupPoints[$customerGroupId]);
         }
 
-        // 如果不启用不同群组不同积分,则返回统一的积分值
-        return (int)$this->reward_point;
+        return true;
     }
-     public function getTransactionTypeTextAttribute()
+
+    public function getTransactionTypeTextAttribute()
     {
         $types = [
             self::TYPE_ORDER => 'Order',
@@ -93,4 +152,5 @@ class RewardActiveRule extends Model
 
         return $types[$this->type_of_transaction] ?? 'Unknown';
     }
+
 }

+ 13 - 7
packages/Longyi/RewardPoints/src/Repositories/RewardPointRepository.php

@@ -350,27 +350,33 @@ class RewardPointRepository extends Repository
                         ->first();
 
                     if ($customerPoints) {
-                        $customerPoints->increment('mw_reward_point', $history->point_remaining);
-                        $customerPoints->save();
+                        $pointsToAdd = $history->point_remaining;
+
+                        // 增加积分
+                        $customerPoints->increment('mw_reward_point', $pointsToAdd);
+                        $customerPoints->refresh(); // 刷新获取最新值
+                        $newBalance = (int) $customerPoints->mw_reward_point;
 
-                        // 更新历史记录状态
+                        // 直接更新原记录的状态和余额
                         $history->status = RewardPointHistory::STATUS_COMPLETED;
-                        $history->balance = (int) $customerPoints->mw_reward_point;
+                        $history->balance = $newBalance;
                         $history->save();
 
-                        $totalConfirmedPoints += $history->point_remaining;
+                        $totalConfirmedPoints += $pointsToAdd;
 
                         Log::info('Pending points confirmed', [
                             'history_id' => $history->history_id,
                             'customer_id' => $customerId,
                             'order_id' => $orderId,
-                            'points' => $history->point_remaining,
-                            'new_balance' => $customerPoints->mw_reward_point
+                            'points' => $pointsToAdd,
+                            'old_balance' => $history->getOriginal('balance'),
+                            'new_balance' => $newBalance
                         ]);
                     }
                 }
 
                 return $totalConfirmedPoints;
+
             });
         } catch (\Exception $e) {
             Log::error('Error confirming pending points', [