rewardPointRepository = app(RewardPointRepository::class); } /** * Get available points for current customer */ public function getAvailablePoints() { if (!Customer::check()) { return 0; } return $this->rewardPointRepository->getCustomerPoints(Customer::getCustomerId()); } /** * Get points used in current cart */ public function getPointsUsed() { if (!session()->has(self::POINTS_USED_KEY)) { return 0; } return session()->get(self::POINTS_USED_KEY); } /** * Get discount amount from points */ public function getDiscountAmount($points = null) { $pointsUsed = $points ?: $this->getPointsUsed(); if ($pointsUsed <= 0) { return 0; } $pointValue = config('rewardpoints.general.point_value', 0.01); return $pointsUsed * $pointValue; } /** * Apply points to cart */ public function applyPoints($points) { $cart = Cart::getCart(); if (!$cart) { return false; } $availablePoints = $this->getAvailablePoints(); if ($points <= 0) { $this->removePoints(); return false; } if ($points > $availablePoints) { return false; } // Calculate maximum points that can be used based on cart total $maxPointsByAmount = $this->getMaxPointsByCartTotal(); if ($points > $maxPointsByAmount) { return false; } // Save points to session session()->put(self::POINTS_USED_KEY, $points); // Recalculate cart Cart::collectTotals(); return true; } /** * Remove points from cart */ public function removePoints() { session()->forget(self::POINTS_USED_KEY); Cart::collectTotals(); return true; } /** * Calculate discount based on points */ public function calculateDiscount() { $pointsUsed = $this->getPointsUsed(); if ($pointsUsed <= 0) { return 0; } $discountAmount = $this->getDiscountAmount($pointsUsed); return $discountAmount; } /** * Get maximum points that can be used based on cart total */ protected function getMaxPointsByCartTotal() { $cart = Cart::getCart(); if (!$cart) { return 0; } $cartTotal = $cart->grand_total; // Get maximum discount percentage (default: 100%) $maxDiscountPercentage = core()->getConfigData('rewardpoints.general.max_discount_percentage', 100); // Calculate maximum discount amount $maxDiscountAmount = ($cartTotal * $maxDiscountPercentage) / 100; // Convert discount amount to points $pointValue = core()->getConfigData('rewardpoints.general.point_value', 0.01); $maxPoints = $pointValue > 0 ? floor($maxDiscountAmount / $pointValue) : 0; return $maxPoints; } /** * Get discount details for cart */ public function getDiscountDetails() { $pointsUsed = $this->getPointsUsed(); if ($pointsUsed <= 0) { return null; } $discountAmount = $this->getDiscountAmount(); return [ 'points_used' => $pointsUsed, 'discount_amount' => $discountAmount, 'formatted_discount' => core()->formatPrice($discountAmount), 'available_points' => $this->getAvailablePoints(), 'points_remaining' => $this->getAvailablePoints() - $pointsUsed ]; } /** * Process order points */ public function processOrderPoints($order) { $pointsUsed = $this->getPointsUsed(); if ($pointsUsed <= 0) { return false; } $customerId = $order->customer_id; if (!$customerId) { return false; } // Deduct points from customer $discountAmount = $this->getDiscountAmount($pointsUsed); $result = $this->rewardPointRepository->deductPoints( $customerId, $pointsUsed, 0, "Points redeemed for order #{$order->increment_id} (Discount: " . core()->formatPrice($discountAmount) . ")", $order->id ); // Clear session $this->removePoints(); return $result; } /** * Get points value in currency */ public function getPointsValue($points) { $pointValue = core()->getConfigData('rewardpoints.general.point_value', 0.01); return $points * $pointValue; } /** * Get currency value in points */ public function getPointsFromValue($value) { $pointValue = core()->getConfigData('rewardpoints.general.point_value', 0.01); return $pointValue > 0 ? floor($value / $pointValue) : 0; } /** * Validate if points can be applied to cart */ public function validatePoints($points) { $cart = Cart::getCart(); if (!$cart) { return 'Cart not found'; } $availablePoints = $this->getAvailablePoints(); if ($points <= 0) { return 'Points must be greater than 0'; } if ($points > $availablePoints) { return "You only have {$availablePoints} points available"; } $maxPoints = $this->getMaxPointsByCartTotal(); if ($points > $maxPoints) { return "Maximum {$maxPoints} points can be used for this order"; } return true; } }