CardController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\admin\controller\finance;
  3. use app\model\Card;
  4. use app\model\CardMain;
  5. use app\model\Member;
  6. use app\model\MemberRole;
  7. use support\Db;
  8. use support\Request;
  9. class CardController
  10. {
  11. public function list(Request $request)
  12. {
  13. $page = $request->get('page', 1);
  14. $pageSize = $request->get('pageSize', 20);
  15. $classify = $request->get('card_main_classify');
  16. $categoryId = $request->get('join_card_main_category_id');
  17. $name = $request->get('card_main_name');
  18. $addtimes = $request->get('card_main_addtimes');
  19. $cardMain = CardMain::when(!empty($classify), function ($query) use ($classify) {
  20. $query->where('card_main_classify', $classify);
  21. })->when(!empty($categoryId), function ($query) use ($categoryId) {
  22. $query->where('join_card_main_category_id', $categoryId);
  23. })->when(!empty($name), function ($query) use ($name) {
  24. $query->where('card_main_name', 'like', '%' . $name . '%');
  25. })->when(!empty($addtimes), function ($query) use ($addtimes) {
  26. $addtimes[0] = strtotime($addtimes[0]);
  27. $addtimes[1] = strtotime($addtimes[1]);
  28. $query->whereBetween('card_main_addtimes', $addtimes);
  29. });
  30. $total = $cardMain->count();
  31. $rows = $cardMain->select('card_main_id', 'card_main_amount','card_main_name')
  32. ->orderBy('card_main_addtimes', 'DESC')
  33. ->forPage($page, $pageSize)
  34. ->get()
  35. ->toArray();
  36. $statistics = [
  37. 'total' => ['total' => 0, 'amount' => 0],
  38. 'is_issue' => ['total' => 0, 'amount' => 0],
  39. 'init' => ['total' => 0, 'amount' => 0],
  40. 'waiting' => ['total' => 0, 'amount' => 0],
  41. 'pending' => ['total' => 0, 'amount' => 0],
  42. 'used' => ['total' => 0, 'amount' => 0],
  43. 'done' => ['total' => 0, 'amount' => 0],
  44. 'paused' => ['total' => 0, 'amount' => 0]
  45. ];
  46. foreach ($rows as &$row) {
  47. // 发行统计
  48. $isIssueTotal = Card::where('is_issue', 'Y')->where('join_card_main_id', $row['card_main_id'])->count();
  49. $row['is_issue']['total'] = $isIssueTotal;
  50. $row['is_issue']['amount'] = sprintf('%.2f', $isIssueTotal * $row['card_main_amount']);
  51. $statistics['is_issue']['total'] += $isIssueTotal;
  52. $statistics['is_issue']['amount'] = sprintf('%.2f', $isIssueTotal * $row['card_main_amount'] + $statistics['is_issue']['amount']);
  53. // 待分配统计
  54. $initTotal = Card::where('card_status', 'INIT')->where('join_card_main_id', $row['card_main_id'])->count();
  55. $row['init']['total'] = $initTotal;
  56. $row['init']['amount'] = sprintf('%.2f', $initTotal * $row['card_main_amount']);
  57. $statistics['init']['total'] += $initTotal;
  58. $statistics['init']['amount'] = sprintf('%.2f', $initTotal * $row['card_main_amount'] + $statistics['init']['amount']);
  59. // 已分配,待售统计
  60. $waitingTotal = Card::where('card_status', 'WAITING')->where('join_card_main_id', $row['card_main_id'])->count();
  61. $row['waiting']['total'] = $waitingTotal;
  62. $row['waiting']['amount'] = sprintf('%.2f', $waitingTotal * $row['card_main_amount']);
  63. $statistics['waiting']['total'] += $waitingTotal;
  64. $statistics['waiting']['amount'] = sprintf('%.2f', $waitingTotal * $row['card_main_amount'] + $statistics['waiting']['amount']);
  65. // 已售,待激活统计
  66. $pendingTotal = Card::where('card_status', 'PENDING')->where('join_card_main_id', $row['card_main_id'])->count();
  67. $row['pending']['total'] = $pendingTotal;
  68. $row['pending']['amount'] = sprintf('%.2f', $pendingTotal * $row['card_main_amount']);
  69. $statistics['pending']['total'] += $pendingTotal;
  70. $statistics['pending']['amount'] = sprintf('%.2f', $pendingTotal * $row['card_main_amount'] + $statistics['pending']['amount']);
  71. // 已激活统计
  72. $usedTotal = Card::where('card_status', 'USED')->where('join_card_main_id', $row['card_main_id'])->count();
  73. $row['used']['total'] = $usedTotal;
  74. $row['used']['amount'] = sprintf('%.2f', $usedTotal * $row['card_main_amount']);
  75. $statistics['used']['total'] += $usedTotal;
  76. $statistics['used']['amount'] = sprintf('%.2f', $usedTotal * $row['card_main_amount']+$statistics['used']['amount']);
  77. // 使用完成统计
  78. $doneTotal = Card::where('card_status', 'DONE')->where('join_card_main_id', $row['card_main_id'])->count();
  79. $row['done']['total'] = $doneTotal;
  80. $row['done']['amount'] = sprintf('%.2f', $doneTotal * $row['card_main_amount']);
  81. $statistics['done']['total'] += $doneTotal;
  82. $statistics['done']['amount'] = sprintf('%.2f', $doneTotal * $row['card_main_amount'] + $statistics['done']['amount']);
  83. // 冻结统计
  84. $pausedTotal = Card::where('card_status', 'PAUSED')->where('join_card_main_id', $row['card_main_id'])->count();
  85. $row['paused']['total'] = $pausedTotal;
  86. $row['paused']['amount'] = sprintf('%.2f', $pausedTotal * $row['card_main_amount']);
  87. $statistics['paused']['total'] += $pausedTotal;
  88. $statistics['paused']['amount'] = sprintf('%.2f', $pausedTotal * $row['card_main_amount'] + $statistics['paused']['total']);
  89. }
  90. $statistics['total']['total'] = $statistics['init']['total'] + $statistics['waiting']['total']
  91. + $statistics['pending']['total'] + $statistics['used']['total'] + $statistics['done']['total']
  92. + $statistics['paused']['total'];
  93. $statistics['total']['amount'] = sprintf('%.2f', $statistics['init']['amount']
  94. + $statistics['waiting']['amount'] + $statistics['pending']['amount'] + $statistics['used']['amount']
  95. + $statistics['done']['amount'] + $statistics['paused']['amount']);
  96. return json_success('success', compact('rows', 'page', 'pageSize', 'total', 'statistics'));
  97. }
  98. /**
  99. * @Desc 导出
  100. * @Author Gorden
  101. * @Date 2024/11/19 8:56
  102. *
  103. * @param Request $request
  104. * @return \support\Response
  105. */
  106. public function exportMemberAccount(Request $request)
  107. {
  108. $days = $request->get('member_addtimes', []);
  109. $level = $request->get('level', '');
  110. $memberId = $request->get('member_id');
  111. $accountType = $request->get('account_type', 'CASH');
  112. $premisesId = intval($request->get('premises_id', ''));
  113. $memberAccountIds = $request->get('member_account_ids');
  114. if (!empty($days)) {
  115. $days[0] = strtotime($days[0]);
  116. $days[1] = strtotime($days[1]);
  117. $month = date('Ym', $days[0]);
  118. $days[1] = strtotime(date('Y-m-d', $days[1]) . " 23:59:59");
  119. } else {
  120. $month = date('Ym');
  121. }
  122. // 兼容老等级搜索
  123. $levelIds = [];
  124. if (!empty($level)) {
  125. $levelName = MemberRole::where('member_role_id', $level)->value('member_role_name');
  126. if (!empty($levelName)) {
  127. $levelIds = MemberRole::where('member_role_name', $levelName)->pluck('member_role_id')->toArray();
  128. }
  129. }
  130. $rows = Member::with([
  131. 'cert' => function ($query) {
  132. $query->select('join_cert_member_id', 'member_cert_name');
  133. },
  134. 'info' => function ($query) {
  135. $query->select('join_info_member_id', 'member_info_nickname');
  136. },
  137. 'role' => function ($query) {
  138. $query->select('member_role_id', 'member_role_name');
  139. }
  140. ])->join('member_account as ma', function ($join) use ($accountType) {
  141. $join->on('member.member_id', '=', 'ma.join_account_member_id')->where('ma.member_account_classify', '=', $accountType);
  142. })->leftJoin('member_role', 'member_role.member_role_id', 'member.join_member_role_id')
  143. ->when(!empty($level), function ($query) use ($level, $levelIds) {
  144. if ($level == '00') {
  145. $query->where('join_member_role_id', '')->orWhere('join_member_role_id', NULL);
  146. } else if ($level == 'VIP') {
  147. $query->where('member.member_is_vip', 'Y');
  148. } else if (!empty($levelIds)) {
  149. $query->whereIn('member_role.member_role_id', $levelIds);
  150. } else {
  151. $query->where('join_member_role_id', $level);
  152. }
  153. })->when(!empty($memberId), function ($query) use ($memberId) {
  154. $query->where('member.member_id', $memberId);
  155. })->when(!empty($days), function ($query) use ($days) {
  156. $query->whereBetween('member.member_addtimes', $days);
  157. })->when(!empty($premisesId), function ($query) use ($premisesId) {
  158. $query->whereJsonContains('member.member_extend_json->belong->premises', $premisesId);
  159. })->when(!empty($memberAccountIds), function ($query) use ($memberAccountIds) {
  160. $query->whereIn('member_account_id', $memberAccountIds);
  161. })->where('member.member_mobile', '<>', '0000')
  162. ->select('member.member_id', 'member.member_mobile', 'member.member_addtimes', 'member.join_member_role_id',
  163. 'ma.member_account_id as member_account_id', 'ma.member_account_income as member_account_income', 'ma.member_account_expend as member_account_expend', 'ma.member_account_surplus as member_account_surplus', 'ma.member_account_added as member_account_added'
  164. )
  165. ->orderBy('member.member_addtimes', 'DESC')
  166. ->get()
  167. ->toArray();
  168. $data = [];
  169. foreach ($rows as $row) {
  170. $data[] = [
  171. 'nickname' => $row['info']['member_info_nickname'] ?? '--',
  172. 'mobile' => $row['member_mobile'] ?? '--',
  173. 'role_name' => $row['role']['member_role_name'] ?? '--',
  174. 'balance' => sprintf('%.2f', $row['member_account_surplus'] + $row['member_account_added']),
  175. 'income' => $row['member_account_income'],
  176. 'expend' => $row['member_account_expend'],
  177. 'added' => $row['member_account_added'],
  178. 'addtimes' => $row['member_addtimes']
  179. ];
  180. }
  181. return json_success('success', $data);
  182. }
  183. }