|
|
@@ -28,83 +28,86 @@ class RewardPointRepository extends Repository
|
|
|
|
|
|
public function addPoints($customerId, $type, $amount, $orderId = null, $detail = null)
|
|
|
{
|
|
|
- $customerPoints = RewardPointCustomer::firstOrCreate(
|
|
|
- ['customer_id' => $customerId],
|
|
|
- [
|
|
|
- 'mw_reward_point' => 0,
|
|
|
- 'mw_friend_id' => 0,
|
|
|
- 'subscribed_balance_update' => 1,
|
|
|
- 'subscribed_point_expiration' => 1,
|
|
|
- 'last_checkout' => Carbon::now()
|
|
|
- ]
|
|
|
- );
|
|
|
-
|
|
|
- $currentBalance = $customerPoints->mw_reward_point;
|
|
|
- $newBalance = $currentBalance + $amount;
|
|
|
-
|
|
|
- // Get rule for expiration
|
|
|
- $rule = RewardActiveRule::where('type_of_transaction', $type)
|
|
|
- ->where('status', 1)
|
|
|
- ->first();
|
|
|
-
|
|
|
- $expiredDay = $rule ? $rule->expired_day : 0;
|
|
|
- $expiredTime = $expiredDay > 0 ? Carbon::now()->addDays($expiredDay) : null;
|
|
|
-
|
|
|
- // Create history
|
|
|
- $history = $this->create([
|
|
|
- 'customer_id' => $customerId,
|
|
|
- 'type_of_transaction' => $type,
|
|
|
- 'amount' => $amount,
|
|
|
- 'balance' => $newBalance,
|
|
|
- 'transaction_detail' => $detail,
|
|
|
- 'transaction_time' => Carbon::now(),
|
|
|
- 'history_order_id' => $orderId ?? 0,
|
|
|
- 'expired_day' => $expiredDay,
|
|
|
- 'expired_time' => $expiredTime,
|
|
|
- 'point_remaining' => $amount,
|
|
|
- 'check_time' => 1,
|
|
|
- 'status' => RewardPointHistory::STATUS_COMPLETED
|
|
|
- ]);
|
|
|
-
|
|
|
- // Update customer points
|
|
|
- $customerPoints->mw_reward_point = $newBalance;
|
|
|
- $customerPoints->save();
|
|
|
-
|
|
|
- return $history;
|
|
|
+ return \DB::transaction(function () use ($customerId, $type, $amount, $orderId, $detail) {
|
|
|
+ $customerPoints = RewardPointCustomer::lockForUpdate()->firstOrCreate(
|
|
|
+ ['customer_id' => $customerId],
|
|
|
+ [
|
|
|
+ 'mw_reward_point' => 0,
|
|
|
+ 'mw_friend_id' => 0,
|
|
|
+ 'subscribed_balance_update' => 1,
|
|
|
+ 'subscribed_point_expiration' => 1,
|
|
|
+ 'last_checkout' => Carbon::now()
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ $currentBalance = $customerPoints->mw_reward_point;
|
|
|
+ $newBalance = $currentBalance + $amount;
|
|
|
+
|
|
|
+ // Get rule for expiration
|
|
|
+ $rule = RewardActiveRule::where('type_of_transaction', $type)
|
|
|
+ ->where('status', 1)
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ $expiredDay = $rule ? $rule->expired_day : 0;
|
|
|
+ $expiredTime = $expiredDay > 0 ? Carbon::now()->addDays($expiredDay) : null;
|
|
|
+
|
|
|
+ // Create history
|
|
|
+ $history = $this->create([
|
|
|
+ 'customer_id' => $customerId,
|
|
|
+ 'type_of_transaction' => $type,
|
|
|
+ 'amount' => $amount,
|
|
|
+ 'balance' => $newBalance,
|
|
|
+ 'transaction_detail' => $detail,
|
|
|
+ 'transaction_time' => Carbon::now(),
|
|
|
+ 'history_order_id' => $orderId ?? 0,
|
|
|
+ 'expired_day' => $expiredDay,
|
|
|
+ 'expired_time' => $expiredTime,
|
|
|
+ 'point_remaining' => $amount,
|
|
|
+ 'check_time' => 1,
|
|
|
+ 'status' => RewardPointHistory::STATUS_COMPLETED
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // Update customer points
|
|
|
+ $customerPoints->mw_reward_point = $newBalance;
|
|
|
+ $customerPoints->save();
|
|
|
+
|
|
|
+ return $history;
|
|
|
+ });
|
|
|
}
|
|
|
-
|
|
|
public function deductPoints($customerId, $amount, $orderId = null, $detail = null)
|
|
|
{
|
|
|
- $customerPoints = RewardPointCustomer::where('customer_id', $customerId)->first();
|
|
|
+ return \DB::transaction(function () use ($customerId, $amount, $orderId, $detail) {
|
|
|
+ $customerPoints = RewardPointCustomer::lockForUpdate()->where('customer_id', $customerId)->first();
|
|
|
|
|
|
- if (!$customerPoints || $customerPoints->mw_reward_point < $amount) {
|
|
|
- return false;
|
|
|
- }
|
|
|
+ if (!$customerPoints || $customerPoints->mw_reward_point < $amount) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- $currentBalance = $customerPoints->mw_reward_point;
|
|
|
- $newBalance = $currentBalance - $amount;
|
|
|
-
|
|
|
- // Create history for deduction (using negative amount)
|
|
|
- $history = $this->create([
|
|
|
- 'customer_id' => $customerId,
|
|
|
- 'type_of_transaction' => 0, // 0 for deduction
|
|
|
- 'amount' => -$amount,
|
|
|
- 'balance' => $newBalance,
|
|
|
- 'transaction_detail' => $detail,
|
|
|
- 'transaction_time' => Carbon::now(),
|
|
|
- 'history_order_id' => $orderId ?? 0,
|
|
|
- 'expired_day' => 0,
|
|
|
- 'expired_time' => null,
|
|
|
- 'point_remaining' => 0,
|
|
|
- 'check_time' => 1,
|
|
|
- 'status' => RewardPointHistory::STATUS_COMPLETED
|
|
|
- ]);
|
|
|
-
|
|
|
- // Update customer points
|
|
|
- $customerPoints->mw_reward_point = $newBalance;
|
|
|
- $customerPoints->save();
|
|
|
-
|
|
|
- return $history;
|
|
|
+ $currentBalance = $customerPoints->mw_reward_point;
|
|
|
+ $newBalance = $currentBalance - $amount;
|
|
|
+
|
|
|
+ // Create history for deduction (using negative amount)
|
|
|
+ $history = $this->create([
|
|
|
+ 'customer_id' => $customerId,
|
|
|
+ 'type_of_transaction' => 0,
|
|
|
+ 'amount' => -$amount,
|
|
|
+ 'balance' => $newBalance,
|
|
|
+ 'transaction_detail' => $detail,
|
|
|
+ 'transaction_time' => Carbon::now(),
|
|
|
+ 'history_order_id' => $orderId ?? 0,
|
|
|
+ 'expired_day' => 0,
|
|
|
+ 'expired_time' => null,
|
|
|
+ 'point_remaining' => 0,
|
|
|
+ 'check_time' => 1,
|
|
|
+ 'status' => RewardPointHistory::STATUS_COMPLETED
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // Update customer points
|
|
|
+ $customerPoints->mw_reward_point = $newBalance;
|
|
|
+ $customerPoints->save();
|
|
|
+
|
|
|
+ return $history;
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
public function getHistory($customerId, $limit = 20)
|