|
|
@@ -0,0 +1,266 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Longyi\RewardPoints\Http\Controllers\Admin;
|
|
|
+
|
|
|
+use Illuminate\Http\Request;
|
|
|
+use Webkul\Admin\Http\Controllers\Controller;
|
|
|
+use Longyi\RewardPoints\Repositories\RewardPointRepository;
|
|
|
+use Longyi\RewardPoints\Models\RewardPointCustomer;
|
|
|
+use Longyi\RewardPoints\Models\RewardActiveRule;
|
|
|
+use Webkul\Customer\Models\Customer;
|
|
|
+use Carbon\Carbon;
|
|
|
+
|
|
|
+class CustomerController extends Controller
|
|
|
+{
|
|
|
+ protected $rewardPointRepository;
|
|
|
+ protected $_config;
|
|
|
+
|
|
|
+ public function __construct(RewardPointRepository $rewardPointRepository)
|
|
|
+ {
|
|
|
+ $this->rewardPointRepository = $rewardPointRepository;
|
|
|
+ $this->_config = request('_config');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Display a listing of customers with reward points
|
|
|
+ */
|
|
|
+ public function index()
|
|
|
+ {
|
|
|
+ $customers = RewardPointCustomer::with('customer')
|
|
|
+ ->orderBy('mw_reward_point', 'desc')
|
|
|
+ ->paginate(15);
|
|
|
+
|
|
|
+ $totalPoints = RewardPointCustomer::sum('mw_reward_point');
|
|
|
+ $totalCustomers = RewardPointCustomer::count();
|
|
|
+ $averagePoints = $totalCustomers > 0 ? $totalPoints / $totalCustomers : 0;
|
|
|
+
|
|
|
+ return view($this->_config['view'], compact('customers', 'totalPoints', 'totalCustomers', 'averagePoints'));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Show customer reward points details
|
|
|
+ */
|
|
|
+ public function show($customerId)
|
|
|
+ {
|
|
|
+ $customer = Customer::findOrFail($customerId);
|
|
|
+ $rewardData = RewardPointCustomer::where('customer_id', $customerId)->first();
|
|
|
+
|
|
|
+ if (!$rewardData) {
|
|
|
+ $rewardData = new RewardPointCustomer();
|
|
|
+ $rewardData->customer_id = $customerId;
|
|
|
+ $rewardData->mw_reward_point = 0;
|
|
|
+ $rewardData->mw_friend_id = 0;
|
|
|
+ $rewardData->last_checkout = Carbon::now();
|
|
|
+ }
|
|
|
+
|
|
|
+ $history = $this->rewardPointRepository->getHistory($customerId, 20);
|
|
|
+ $signRecords = $rewardData->signRecords()->orderBy('sign_date', 'desc')->limit(30)->get();
|
|
|
+
|
|
|
+ return view($this->_config['view'], compact('customer', 'rewardData', 'history', 'signRecords'));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Add points to customer
|
|
|
+ */
|
|
|
+ public function addPoints(Request $request)
|
|
|
+ {
|
|
|
+ $this->validate($request, [
|
|
|
+ 'customer_id' => 'required|integer|exists:customers,id',
|
|
|
+ 'points' => 'required|integer|min:1',
|
|
|
+ 'type' => 'required|integer|in:1,2,3,4,5,6',
|
|
|
+ 'reason' => 'nullable|string|max:500'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ try {
|
|
|
+ $customerId = $request->input('customer_id');
|
|
|
+ $points = $request->input('points');
|
|
|
+ $type = $request->input('type');
|
|
|
+ $reason = $request->input('reason', 'Admin adjustment');
|
|
|
+
|
|
|
+ $history = $this->rewardPointRepository->addPoints(
|
|
|
+ $customerId,
|
|
|
+ $type,
|
|
|
+ $points,
|
|
|
+ null,
|
|
|
+ "Admin: " . $reason
|
|
|
+ );
|
|
|
+
|
|
|
+ if ($history) {
|
|
|
+ session()->flash('success', "Successfully added {$points} points to customer.");
|
|
|
+ } else {
|
|
|
+ session()->flash('error', 'Failed to add points.');
|
|
|
+ }
|
|
|
+
|
|
|
+ return redirect()->back();
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ session()->flash('error', 'Error adding points: ' . $e->getMessage());
|
|
|
+ return redirect()->back()->withInput();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Deduct points from customer
|
|
|
+ */
|
|
|
+ public function deductPoints(Request $request)
|
|
|
+ {
|
|
|
+ $this->validate($request, [
|
|
|
+ 'customer_id' => 'required|integer|exists:customers,id',
|
|
|
+ 'points' => 'required|integer|min:1',
|
|
|
+ 'reason' => 'nullable|string|max:500'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ try {
|
|
|
+ $customerId = $request->input('customer_id');
|
|
|
+ $points = $request->input('points');
|
|
|
+ $reason = $request->input('reason', 'Admin adjustment');
|
|
|
+
|
|
|
+ $result = $this->rewardPointRepository->deductPoints(
|
|
|
+ $customerId,
|
|
|
+ $points,
|
|
|
+ null,
|
|
|
+ "Admin: " . $reason
|
|
|
+ );
|
|
|
+
|
|
|
+ if ($result) {
|
|
|
+ session()->flash('success', "Successfully deducted {$points} points from customer.");
|
|
|
+ } else {
|
|
|
+ session()->flash('error', 'Insufficient points or customer not found.');
|
|
|
+ }
|
|
|
+
|
|
|
+ return redirect()->back();
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ session()->flash('error', 'Error deducting points: ' . $e->getMessage());
|
|
|
+ return redirect()->back()->withInput();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Export customer points report
|
|
|
+ */
|
|
|
+ public function export(Request $request)
|
|
|
+ {
|
|
|
+ $customers = RewardPointCustomer::with('customer')
|
|
|
+ ->orderBy('mw_reward_point', 'desc')
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ $filename = "reward_points_report_" . Carbon::now()->format('Y-m-d_H-i-s') . ".csv";
|
|
|
+
|
|
|
+ $headers = [
|
|
|
+ 'Content-Type' => 'text/csv',
|
|
|
+ 'Content-Disposition' => "attachment; filename={$filename}",
|
|
|
+ ];
|
|
|
+
|
|
|
+ $callback = function() use ($customers) {
|
|
|
+ $file = fopen('php://output', 'w');
|
|
|
+
|
|
|
+ // Add CSV headers
|
|
|
+ fputcsv($file, [
|
|
|
+ 'Customer ID',
|
|
|
+ 'Customer Email',
|
|
|
+ 'Customer Name',
|
|
|
+ 'Reward Points',
|
|
|
+ 'Friend ID',
|
|
|
+ 'Subscribed Balance Update',
|
|
|
+ 'Subscribed Point Expiration',
|
|
|
+ 'Last Checkout'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // Add data rows
|
|
|
+ foreach ($customers as $customer) {
|
|
|
+ fputcsv($file, [
|
|
|
+ $customer->customer_id,
|
|
|
+ $customer->customer->email ?? '',
|
|
|
+ $customer->customer->first_name . ' ' . $customer->customer->last_name,
|
|
|
+ $customer->mw_reward_point,
|
|
|
+ $customer->mw_friend_id,
|
|
|
+ $customer->subscribed_balance_update ? 'Yes' : 'No',
|
|
|
+ $customer->subscribed_point_expiration ? 'Yes' : 'No',
|
|
|
+ $customer->last_checkout
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ fclose($file);
|
|
|
+ };
|
|
|
+
|
|
|
+ return response()->stream($callback, 200, $headers);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Bulk update points
|
|
|
+ */
|
|
|
+ public function bulkUpdate(Request $request)
|
|
|
+ {
|
|
|
+ $this->validate($request, [
|
|
|
+ 'customer_ids' => 'required|array',
|
|
|
+ 'customer_ids.*' => 'integer|exists:customers,id',
|
|
|
+ 'points' => 'required|integer',
|
|
|
+ 'action' => 'required|in:add,set'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ try {
|
|
|
+ $customerIds = $request->input('customer_ids');
|
|
|
+ $points = $request->input('points');
|
|
|
+ $action = $request->input('action');
|
|
|
+ $reason = $request->input('reason', 'Admin bulk update');
|
|
|
+
|
|
|
+ $updatedCount = 0;
|
|
|
+
|
|
|
+ foreach ($customerIds as $customerId) {
|
|
|
+ if ($action === 'add') {
|
|
|
+ $result = $this->rewardPointRepository->addPoints(
|
|
|
+ $customerId,
|
|
|
+ 0, // 0 for admin action
|
|
|
+ $points,
|
|
|
+ null,
|
|
|
+ "Admin bulk: " . $reason
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ // Set points to specific value
|
|
|
+ $customerPoints = RewardPointCustomer::firstOrCreate(
|
|
|
+ ['customer_id' => $customerId],
|
|
|
+ [
|
|
|
+ 'mw_reward_point' => 0,
|
|
|
+ 'mw_friend_id' => 0,
|
|
|
+ 'last_checkout' => Carbon::now()
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ $oldPoints = $customerPoints->mw_reward_point;
|
|
|
+ $customerPoints->mw_reward_point = $points;
|
|
|
+ $customerPoints->save();
|
|
|
+
|
|
|
+ // Add history record
|
|
|
+ $this->rewardPointRepository->create([
|
|
|
+ 'customer_id' => $customerId,
|
|
|
+ 'type_of_transaction' => 0,
|
|
|
+ 'amount' => $points - $oldPoints,
|
|
|
+ 'balance' => $points,
|
|
|
+ 'transaction_detail' => "Admin bulk set: " . $reason,
|
|
|
+ 'transaction_time' => Carbon::now(),
|
|
|
+ 'history_order_id' => 0,
|
|
|
+ 'expired_day' => 0,
|
|
|
+ 'expired_time' => null,
|
|
|
+ 'point_remaining' => 0,
|
|
|
+ 'check_time' => 1,
|
|
|
+ 'status' => 1
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $result = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($result) {
|
|
|
+ $updatedCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ session()->flash('success', "Successfully updated {$updatedCount} customers.");
|
|
|
+ return redirect()->back();
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ session()->flash('error', 'Error in bulk update: ' . $e->getMessage());
|
|
|
+ return redirect()->back()->withInput();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|