PremisesMemberController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\admin\controller\finance;
  3. use app\admin\service\sys_manage\DeptService;
  4. use app\common\Util;
  5. use support\Db;
  6. use support\Request;
  7. use function _PHPStan_cc8d35ffb\RingCentral\Psr7\str;
  8. class PremisesMemberController
  9. {
  10. private $accountType = ['CASH', 'POINTS', 'CARD'];
  11. public function list(Request $request)
  12. {
  13. $page = $request->get('page', 1);
  14. $pageSize = $request->get('pageSize', 20);
  15. $accountType = $request->get('account_type', $this->accountType);
  16. $premises = $request->get('premises', DeptService::getPremisesList());
  17. $premisesKeyValue = [];
  18. foreach($premises as $item){
  19. $premisesKeyValue[$item['key']] = $item['label'];
  20. }
  21. $months = $request->get('month', []);
  22. if (empty($months)) {
  23. $months = [date('Y-m')];
  24. // $months = ['2024-06'];
  25. }else{
  26. foreach($months as $key => $month){
  27. $months[$key] = date('Y-m',strtotime($month));
  28. }
  29. }
  30. $data = [];
  31. $statistics = ['surplus'=>0,'income'=>0,'added'=>0,'expend'=>0];
  32. foreach ($months as $month) {
  33. $day = $month . '-01';
  34. $monthStr = date('m', strtotime($day));
  35. $firstDay = date('Y-m-t', strtotime('last month', strtotime($day)));
  36. $firstMonth = date('Ym', strtotime($firstDay));
  37. $lastDay = date('Y-m-t', strtotime($month));
  38. if (strtotime("-1 days") < strtotime($lastDay)) {
  39. $lastDay = date('Y-m-d', strtotime("-1 days"));
  40. }
  41. $lastMonth = date('Ym', strtotime($lastDay));
  42. foreach ($premises as $premise) {
  43. $premiseKey = intval($premise['key']);
  44. foreach ($accountType as $item) {
  45. $firstData = [];
  46. $firstTable = "data_account_{$firstMonth}";
  47. if (Util::schema()->hasTable($firstTable)) {
  48. $firstData = Db::table("{$firstTable} as da")
  49. ->leftJoin('member', 'member.member_id', '=', 'da.join_data_account_member_id')
  50. ->whereJsonContains('member.member_extend_json->belong->premises', $premiseKey)
  51. ->where('data_account_days', $firstDay)
  52. ->where('data_account_classify', $item)
  53. ->selectRaw("SUM(data_account_income) as data_account_income,SUM(data_account_expend) as data_account_expend,SUM(data_account_surplus) as data_account_surplus,SUM(data_account_added) as data_account_added")
  54. ->groupBy('data_account_days')
  55. ->first();
  56. }
  57. $lastData = Db::table("data_account_{$lastMonth} as da")
  58. ->leftJoin('member', 'member.member_id', '=', 'da.join_data_account_member_id')
  59. ->whereJsonContains('member.member_extend_json->belong->premises', $premiseKey)
  60. ->where('data_account_days', $lastDay)
  61. ->where('data_account_classify', $item)
  62. ->selectRaw('SUM(data_account_income) as data_account_income,SUM(data_account_expend) as data_account_expend,SUM(data_account_surplus) as data_account_surplus,SUM(data_account_added) as data_account_added')
  63. ->groupBy('data_account_days')
  64. ->first();
  65. $record = [
  66. 'month' => $monthStr,
  67. 'premise' => isset($premisesKeyValue[$premiseKey]) ? $premisesKeyValue[$premiseKey] : $premiseKey,
  68. 'account_type' => $item,
  69. 'data_account_surplus' => $lastData->data_account_surplus ?? '0.00',
  70. 'data_account_income' => !empty($firstData) ? number_format($lastData->data_account_income - $firstData->data_account_income,2,'.','') : ($lastData->data_account_income ?? '0.00'),
  71. 'data_account_expend' => !empty($firstData) ? number_format($lastData->data_account_expend - $firstData->data_account_expend,2,'.','') : ($lastData->data_account_expend ?? '0.00'),
  72. 'data_account_added' => !empty($firstData) ? number_format($lastData->data_account_added - $firstData->data_account_added,2,'.','') : ($lastData->data_account_added ?? '0.00'),
  73. ];
  74. $statistics['surplus'] = number_format($statistics['surplus']+$record['data_account_surplus'],2,'.','');
  75. $statistics['income'] = number_format($statistics['income']+$record['data_account_income'],2,'.','');
  76. $statistics['expend'] = number_format($statistics['expend']+$record['data_account_expend'],2,'.','');
  77. $statistics['added'] = number_format($statistics['added']+$record['data_account_added'],2,'.','');
  78. $data[] = $record;
  79. }
  80. }
  81. }
  82. return json_success('', ['rows'=>$data,'statistics'=>$statistics]);
  83. }
  84. }