IncomeAndExpendController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\admin\controller\finance;
  3. use app\admin\service\sys_manage\DeptService;
  4. use app\common\Util;
  5. use app\model\Member;
  6. use support\Db;
  7. use support\Request;
  8. class IncomeAndExpendController
  9. {
  10. public function list(Request $request)
  11. {
  12. $days = $request->get('days');
  13. if (!empty($days)) {
  14. $days[0] = strtotime($days[0]);
  15. $days[1] = strtotime($days[1]);
  16. if (date('m', $days[0]) != date('m', $days[1])) {
  17. return json_fail('暂不支持跨月查询');
  18. }
  19. $month = date('Ym', $days[0]);
  20. $days[1] = strtotime(date('Y-m-d', $days[1]) . " 23:59:59");
  21. } else {
  22. $month = date('Ym');
  23. $days[0] = strtotime(date('Y-m') . '-01');
  24. $days[1] = time();
  25. }
  26. $premises = $request->get('premises', DeptService::getPremisesList());
  27. // $premisesKeyValue = [];
  28. // foreach($premises as $item){
  29. // $premisesKeyValue[$item['key']] = $item['label'];
  30. // }
  31. $premisesKey = [];
  32. foreach ($premises as $item) {
  33. $premisesKey[] = $item['key'];
  34. }
  35. $dayNum = ($days[1] - $days[0]) / (60 * 60 * 24);
  36. $currentDay = date('Y-m-d', $days[0]);
  37. $data = [];
  38. $statistics = ['income_amount' => 0, 'income_wxpay' => 0, 'income_alipay' => 0, 'income_money' => 0, 'expend_amount' => 0, 'expend_wxpay' => 0, 'expend_alipay' => 0, 'expend_money' => 0];
  39. for ($i = 0; $i <= $dayNum; $i++) {
  40. $dayData = [];
  41. $dayData['day'] = $currentDay;
  42. // 收入
  43. if (Util::schema()->hasTable("data_income_{$month}")) {
  44. $wxpay = Db::table("data_income_{$month}")
  45. ->where('data_income_days', $currentDay)
  46. ->where('data_income_pay_type', 'WXPAY')
  47. ->whereIn('join_data_income_dept_id', $premisesKey)
  48. ->selectRaw("data_income_pay_type,SUM(data_income_amount) as data_income_amount")
  49. ->groupBy('data_income_pay_type')
  50. ->first();
  51. $alipay = Db::table("data_income_{$month}")
  52. ->where('data_income_days', $currentDay)
  53. ->where('data_income_pay_type', 'ALIPAY')
  54. ->whereIn('join_data_income_dept_id', $premisesKey)
  55. ->selectRaw("data_income_pay_type,SUM(data_income_amount) as data_income_amount")
  56. ->groupBy('data_income_pay_type')
  57. ->first();
  58. $money = Db::table("data_income_{$month}")
  59. ->where('data_income_days', $currentDay)
  60. ->where('data_income_pay_type', 'MONEY')
  61. ->whereIn('join_data_income_dept_id', $premisesKey)
  62. ->selectRaw("data_income_pay_type,SUM(data_income_amount) as data_income_amount")
  63. ->groupBy('data_income_pay_type')
  64. ->first();
  65. }
  66. $dayData['income']['wxpay'] = $wxpay->data_income_amount ?? 0.00;
  67. $statistics['income_wxpay'] += $dayData['income']['wxpay'];
  68. $dayData['income']['alipay'] = $alipay->data_income_amount ?? 0.00;
  69. $statistics['income_alipay'] += $dayData['income']['alipay'];
  70. $dayData['income']['money'] = $money->data_income_amount ?? 0.00;
  71. $statistics['income_money'] += $dayData['income']['money'];
  72. $dayData['income']['amount'] = $dayData['income']['wxpay'] + $dayData['income']['alipay'] + $dayData['income']['money'];
  73. $statistics['income_amount'] += $dayData['income']['amount'];
  74. // 支出
  75. if (Util::schema()->hasTable("data_income_{$month}")) {
  76. $extendWxpay = Db::table("data_expend_{$month}")
  77. ->where('data_expend_days', $currentDay)
  78. ->where('data_expend_pay_type', 'WXPAY')
  79. ->whereIn('join_data_expend_dept_id', $premisesKey)
  80. ->selectRaw("data_expend_pay_type,SUM(data_expend_amount) as data_expend_amount")
  81. ->groupBy('data_expend_pay_type')
  82. ->first();
  83. $extendAlipay = Db::table("data_expend_{$month}")
  84. ->where('data_expend_days', $currentDay)
  85. ->where('data_expend_pay_type', 'ALIPAY')
  86. ->whereIn('join_data_expend_dept_id', $premisesKey)
  87. ->selectRaw("data_expend_pay_type,SUM(data_expend_amount) as data_expend_amount")
  88. ->groupBy('data_expend_pay_type')
  89. ->first();
  90. $extendMoney = Db::table("data_expend_{$month}")
  91. ->where('data_expend_days', $currentDay)
  92. ->where('data_expend_pay_type', 'MONEY')
  93. ->whereIn('join_data_expend_dept_id', $premisesKey)
  94. ->selectRaw("data_expend_pay_type,SUM(data_expend_amount) as data_expend_amount")
  95. ->groupBy('data_expend_pay_type')
  96. ->first();
  97. }
  98. $dayData['expend']['wxpay'] = $extendWxpay->data_expend_amount ?? 0.00;
  99. $statistics['expend_wxpay'] += $dayData['expend']['wxpay'];
  100. $dayData['expend']['alipay'] = $extendAlipay->data_expend_amount ?? 0.00;
  101. $statistics['expend_alipay'] += $dayData['expend']['alipay'];
  102. $dayData['expend']['money'] = $extendMoney->data_expend_amount ?? 0.00;
  103. $statistics['expend_money'] += $dayData['expend']['money'];
  104. $dayData['expend']['amount'] = $dayData['expend']['wxpay'] + $dayData['expend']['alipay'] + $dayData['expend']['money'];
  105. $statistics['expend_amount'] += $dayData['expend']['amount'];
  106. // 当前日期+1
  107. $currentDay = date('Y-m-d', strtotime("+1 days", strtotime($currentDay)));
  108. // 没有数据,跳过
  109. if ($dayData['expend']['amount'] == 0 && $dayData['income']['amount'] == 0) {
  110. continue;
  111. }
  112. $data[] = $dayData;
  113. }
  114. return json_success('', ['rows' => $data, 'statistics' => $statistics]);
  115. }
  116. }