TransactionController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace Longyi\RewardPoints\Http\Controllers\Admin;
  3. use Illuminate\Http\Request;
  4. use Webkul\Admin\Http\Controllers\Controller;
  5. use Longyi\RewardPoints\Models\RewardPointHistory;
  6. use Longyi\RewardPoints\Models\RewardPointCustomer;
  7. use Carbon\Carbon;
  8. use Maatwebsite\Excel\Facades\Excel;
  9. use Longyi\RewardPoints\Exports\TransactionsExport;
  10. class TransactionController extends Controller
  11. {
  12. protected $_config;
  13. public function __construct()
  14. {
  15. $this->_config = request('_config') ?: [];
  16. }
  17. public function earnedIndex()
  18. {
  19. request()->merge(['view_type' => 'earned']);
  20. return $this->index();
  21. }
  22. /**
  23. * Display a listing of reward point transactions
  24. */
  25. public function index()
  26. {
  27. $startDate = request()->get('start_date', Carbon::now()->subDays(30)->format('Y-m-d'));
  28. $endDate = request()->get('end_date', Carbon::now()->format('Y-m-d'));
  29. $customerEmail = request()->get('customer_email');
  30. $transactionType = request()->get('transaction_type');
  31. $amountType = request()->get('amount_type'); // all, earned, redeemed
  32. $viewType = request()->get('view_type', 'all'); // all or earned
  33. $query = RewardPointHistory::with('customer');
  34. // 日期筛选
  35. if ($startDate && $endDate) {
  36. $startDateTime = Carbon::parse($startDate)->startOfDay();
  37. $endDateTime = Carbon::parse($endDate)->endOfDay();
  38. $query->whereBetween('transaction_time', [$startDateTime, $endDateTime]);
  39. }
  40. // 客户邮箱筛选
  41. if ($customerEmail) {
  42. $query->whereHas('customer', function ($q) use ($customerEmail) {
  43. $q->where('email', 'like', "%{$customerEmail}%");
  44. });
  45. }
  46. // 交易类型筛选
  47. if ($transactionType !== null && $transactionType !== '') {
  48. $query->where('type_of_transaction', $transactionType);
  49. }
  50. // 根据视图类型筛选(仅获取或全部)
  51. if ($viewType === 'earned') {
  52. $query->where('amount', '>', 0);
  53. } elseif ($amountType === 'earned') {
  54. $query->where('amount', '>', 0);
  55. } elseif ($amountType === 'redeemed') {
  56. $query->where('amount', '<', 0);
  57. }
  58. $transactions = $query->orderBy('transaction_time', 'desc')->paginate(20);
  59. // 统计数据 - 根据视图类型调整
  60. if ($viewType === 'earned') {
  61. $totalEarned = (clone $query)->sum('amount');
  62. $totalRedeemed = 0;
  63. $totalTransactions = (clone $query)->count();
  64. } else {
  65. $totalEarned = (clone $query)->where('amount', '>', 0)->sum('amount');
  66. $totalRedeemed = abs((clone $query)->where('amount', '<', 0)->sum('amount'));
  67. $totalTransactions = (clone $query)->count();
  68. }
  69. $view = isset($this->_config['view']) ? $this->_config['view'] : 'rewardpoints::admin.transactions.index';
  70. return view($view, compact(
  71. 'transactions',
  72. 'startDate',
  73. 'endDate',
  74. 'customerEmail',
  75. 'transactionType',
  76. 'amountType',
  77. 'viewType',
  78. 'totalEarned',
  79. 'totalRedeemed',
  80. 'totalTransactions'
  81. ));
  82. }
  83. /**
  84. * Export transactions
  85. */
  86. public function export(Request $request)
  87. {
  88. $startDate = $request->get('start_date', Carbon::now()->subDays(30)->format('Y-m-d'));
  89. $endDate = $request->get('end_date', Carbon::now()->format('Y-m-d'));
  90. $customerEmail = $request->get('customer_email');
  91. $transactionType = $request->get('transaction_type');
  92. $amountType = $request->get('amount_type');
  93. $query = RewardPointHistory::with('customer');
  94. // 应用相同的筛选条件
  95. if ($startDate && $endDate) {
  96. $startDateTime = Carbon::parse($startDate)->startOfDay();
  97. $endDateTime = Carbon::parse($endDate)->endOfDay();
  98. $query->whereBetween('transaction_time', [$startDateTime, $endDateTime]);
  99. }
  100. if ($customerEmail) {
  101. $query->whereHas('customer', function ($q) use ($customerEmail) {
  102. $q->where('email', 'like', "%{$customerEmail}%");
  103. });
  104. }
  105. if ($transactionType !== null && $transactionType !== '') {
  106. $query->where('type_of_transaction', $transactionType);
  107. }
  108. if ($amountType === 'earned') {
  109. $query->where('amount', '>', 0);
  110. } elseif ($amountType === 'redeemed') {
  111. $query->where('amount', '<', 0);
  112. }
  113. $transactions = $query->orderBy('transaction_time', 'desc')->get();
  114. $filename = "reward_point_transactions_" . date('Y-m-d_His') . ".csv";
  115. $headers = [
  116. 'Content-Type' => 'text/csv',
  117. 'Content-Disposition' => "attachment; filename={$filename}",
  118. ];
  119. $callback = function() use ($transactions) {
  120. $file = fopen('php://output', 'w');
  121. // CSV 头部
  122. fputcsv($file, [
  123. 'ID',
  124. 'Customer ID',
  125. 'Customer Name',
  126. 'Customer Email',
  127. 'Transaction Type',
  128. 'Amount',
  129. 'Balance',
  130. 'Transaction Time',
  131. 'Description',
  132. 'Status'
  133. ]);
  134. $typeNames = [
  135. 1 => 'Order',
  136. 2 => 'Registration',
  137. 3 => 'Product Review',
  138. 4 => 'Daily Sign In',
  139. 5 => 'Referral',
  140. 6 => 'Birthday',
  141. 8 => 'Subscribe',
  142. 0 => 'Admin Adjustment',
  143. 99 => 'Admin Action'
  144. ];
  145. $statusNames = [
  146. 0 => 'Pending',
  147. 1 => 'Completed',
  148. 2 => 'Cancelled',
  149. 3 => 'Expired'
  150. ];
  151. foreach ($transactions as $transaction) {
  152. fputcsv($file, [
  153. $transaction->history_id,
  154. $transaction->customer_id,
  155. $transaction->customer ? $transaction->customer->first_name . ' ' . $transaction->customer->last_name : 'N/A',
  156. $transaction->customer ? $transaction->customer->email : 'N/A',
  157. $typeNames[$transaction->type_of_transaction] ?? 'Unknown',
  158. $transaction->amount,
  159. $transaction->balance,
  160. $transaction->transaction_time,
  161. $transaction->transaction_detail,
  162. $statusNames[$transaction->status] ?? 'Unknown'
  163. ]);
  164. }
  165. fclose($file);
  166. };
  167. return response()->stream($callback, 200, $headers);
  168. }
  169. }