| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- namespace Longyi\RewardPoints\Http\Controllers\Admin;
- use Illuminate\Http\Request;
- use Webkul\Admin\Http\Controllers\Controller;
- use Longyi\RewardPoints\Models\RewardPointHistory;
- use Longyi\RewardPoints\Models\RewardPointCustomer;
- use Carbon\Carbon;
- use Maatwebsite\Excel\Facades\Excel;
- use Longyi\RewardPoints\Exports\TransactionsExport;
- class TransactionController extends Controller
- {
- protected $_config;
- public function __construct()
- {
- $this->_config = request('_config') ?: [];
- }
- public function earnedIndex()
- {
- request()->merge(['view_type' => 'earned']);
- return $this->index();
- }
- /**
- * Display a listing of reward point transactions
- */
- public function index()
- {
- $startDate = request()->get('start_date', Carbon::now()->subDays(30)->format('Y-m-d'));
- $endDate = request()->get('end_date', Carbon::now()->format('Y-m-d'));
- $customerEmail = request()->get('customer_email');
- $transactionType = request()->get('transaction_type');
- $amountType = request()->get('amount_type'); // all, earned, redeemed
- $viewType = request()->get('view_type', 'all'); // all or earned
- $query = RewardPointHistory::with('customer');
- // 日期筛选
- if ($startDate && $endDate) {
- $startDateTime = Carbon::parse($startDate)->startOfDay();
- $endDateTime = Carbon::parse($endDate)->endOfDay();
- $query->whereBetween('transaction_time', [$startDateTime, $endDateTime]);
- }
- // 客户邮箱筛选
- if ($customerEmail) {
- $query->whereHas('customer', function ($q) use ($customerEmail) {
- $q->where('email', 'like', "%{$customerEmail}%");
- });
- }
- // 交易类型筛选
- if ($transactionType !== null && $transactionType !== '') {
- $query->where('type_of_transaction', $transactionType);
- }
- // 根据视图类型筛选(仅获取或全部)
- if ($viewType === 'earned') {
- $query->where('amount', '>', 0);
- } elseif ($amountType === 'earned') {
- $query->where('amount', '>', 0);
- } elseif ($amountType === 'redeemed') {
- $query->where('amount', '<', 0);
- }
- $transactions = $query->orderBy('transaction_time', 'desc')->paginate(20);
- // 统计数据 - 根据视图类型调整
- if ($viewType === 'earned') {
- $totalEarned = (clone $query)->sum('amount');
- $totalRedeemed = 0;
- $totalTransactions = (clone $query)->count();
- } else {
- $totalEarned = (clone $query)->where('amount', '>', 0)->sum('amount');
- $totalRedeemed = abs((clone $query)->where('amount', '<', 0)->sum('amount'));
- $totalTransactions = (clone $query)->count();
- }
- $view = isset($this->_config['view']) ? $this->_config['view'] : 'rewardpoints::admin.transactions.index';
- return view($view, compact(
- 'transactions',
- 'startDate',
- 'endDate',
- 'customerEmail',
- 'transactionType',
- 'amountType',
- 'viewType',
- 'totalEarned',
- 'totalRedeemed',
- 'totalTransactions'
- ));
- }
- /**
- * Export transactions
- */
- public function export(Request $request)
- {
- $startDate = $request->get('start_date', Carbon::now()->subDays(30)->format('Y-m-d'));
- $endDate = $request->get('end_date', Carbon::now()->format('Y-m-d'));
- $customerEmail = $request->get('customer_email');
- $transactionType = $request->get('transaction_type');
- $amountType = $request->get('amount_type');
- $query = RewardPointHistory::with('customer');
- // 应用相同的筛选条件
- if ($startDate && $endDate) {
- $startDateTime = Carbon::parse($startDate)->startOfDay();
- $endDateTime = Carbon::parse($endDate)->endOfDay();
- $query->whereBetween('transaction_time', [$startDateTime, $endDateTime]);
- }
- if ($customerEmail) {
- $query->whereHas('customer', function ($q) use ($customerEmail) {
- $q->where('email', 'like', "%{$customerEmail}%");
- });
- }
- if ($transactionType !== null && $transactionType !== '') {
- $query->where('type_of_transaction', $transactionType);
- }
- if ($amountType === 'earned') {
- $query->where('amount', '>', 0);
- } elseif ($amountType === 'redeemed') {
- $query->where('amount', '<', 0);
- }
- $transactions = $query->orderBy('transaction_time', 'desc')->get();
- $filename = "reward_point_transactions_" . date('Y-m-d_His') . ".csv";
- $headers = [
- 'Content-Type' => 'text/csv',
- 'Content-Disposition' => "attachment; filename={$filename}",
- ];
- $callback = function() use ($transactions) {
- $file = fopen('php://output', 'w');
- // CSV 头部
- fputcsv($file, [
- 'ID',
- 'Customer ID',
- 'Customer Name',
- 'Customer Email',
- 'Transaction Type',
- 'Amount',
- 'Balance',
- 'Transaction Time',
- 'Description',
- 'Status'
- ]);
- $typeNames = [
- 3 => 'Order',
- 2 => 'Registration',
- 4 => 'Product Review',
- 1 => 'Daily Sign In',
- 5 => 'Referral',
- 6 => 'Birthday',
- 7 => 'Share',
- 8 => 'Subscribe',
- 9 => 'login',
- 99 => 'Admin Action'
- ];
- $statusNames = [
- 0 => 'Pending',
- 1 => 'Completed',
- 2 => 'Cancelled',
- 3 => 'Expired'
- ];
- foreach ($transactions as $transaction) {
- fputcsv($file, [
- $transaction->history_id,
- $transaction->customer_id,
- $transaction->customer ? $transaction->customer->first_name . ' ' . $transaction->customer->last_name : 'N/A',
- $transaction->customer ? $transaction->customer->email : 'N/A',
- $typeNames[$transaction->type_of_transaction] ?? 'Unknown',
- $transaction->amount,
- $transaction->balance,
- $transaction->transaction_time,
- $transaction->transaction_detail,
- $statusNames[$transaction->status] ?? 'Unknown'
- ]);
- }
- fclose($file);
- };
- return response()->stream($callback, 200, $headers);
- }
- }
|