ChargeController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Illuminate\Support\Facades\Auth;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Validator;
  7. class ChargeController extends Controller
  8. {
  9. public function __construct()
  10. {
  11. }
  12. /* 缴费记录 */
  13. public function chargeList(Request $request){
  14. $where=[];
  15. $adminInfo=Auth::guard('admin')->user();
  16. if($adminInfo){
  17. $atis=$adminInfo->is_team;
  18. if($atis==2){
  19. $where['charge.team_id']=$adminInfo->team_id;
  20. }
  21. }
  22. $search_name = $request->search_name;
  23. $page_size = $request->page_size;
  24. $page_index = $request->page_index;
  25. $num = ($page_index - 1) * $page_size;
  26. $where['charge.status']=1;
  27. if(!empty($request->month)){
  28. $where['charge.month'] = date("Y-m",strtotime($request->month));
  29. }
  30. if(!empty($request->team_id)){
  31. $where['charge.team_id'] = $request->team_id;
  32. }
  33. $count = DB::table('charge')
  34. ->join('users', 'users.id', '=', 'charge.uid')
  35. ->leftJoin('team','team.id','=','charge.team_id')
  36. ->where(function ($query) use ($search_name) {
  37. $query->where('users.name', 'like', '%' . $search_name . '%')
  38. ->orWhere('users.telphone', 'like', '%' . $search_name . '%')
  39. ->orWhere('users.cre_num', 'like', '%' . $search_name . '%');
  40. })
  41. ->where($where)
  42. ->count();
  43. if ($count > 0) {
  44. $data = DB::table('charge')
  45. ->join('users', 'users.id', '=', 'charge.uid')
  46. ->leftJoin('team','team.id','=','charge.team_id')
  47. ->where(function ($query) use ($search_name) {
  48. $query->where('users.name', 'like', '%' . $search_name . '%')
  49. ->orWhere('users.telphone', 'like', '%' . $search_name . '%')
  50. ->orWhere('users.cre_num', 'like', '%' . $search_name . '%');
  51. })
  52. ->where($where)
  53. ->select('charge.id','users.name','users.telphone','users.cre_num','team.name as tname','team.id as tid','charge.fee', 'charge.status', 'charge.month', 'charge.pays_at','charge.type')
  54. ->orderBy('charge.id','desc')
  55. ->skip($num)->take($page_size)->get();
  56. if ($data) {
  57. $data=json_decode($data,true);
  58. foreach ($data as $key => $val) {
  59. $data[$key]['month'] = substr($val['month'], 0, 7);
  60. }
  61. return response()->json([
  62. 'error_code' => 200,
  63. 'msg' => '成功',
  64. 'data' => $data,
  65. 'count' => $count
  66. ]);
  67. } else {
  68. return response()->json([
  69. 'error_code' => 0,
  70. 'msg' => '获取失败'
  71. ]);
  72. }
  73. }else{
  74. return response()->json([
  75. 'error_code' => 200,
  76. 'msg' => '没有缴费信息',
  77. 'list' => [],
  78. 'count' => $count
  79. ]);
  80. }
  81. }
  82. /* 缴费 */
  83. public function payFee(Request $request){
  84. $input=$request->all();
  85. $rules=[
  86. 'name'=>'required|max:16',
  87. 'cre_num'=>'required|min:8|max:8',
  88. 'month'=>'required',
  89. 'fee'=>'required',
  90. ];
  91. $messages=[
  92. 'name.required'=>'缴费人名称不能为空.',
  93. 'name.max'=>'缴费人名称不能超过16个字符.',
  94. 'cre_num.required'=>'档案号不能为空.',
  95. 'cre_num.min'=>'档案号长度为8个字符.',
  96. 'cre_num.max'=>'档案号长度为8个字符.',
  97. 'month.required'=>'请选择缴费月份.',
  98. 'fee.required'=>'缴费金额不能为空.',
  99. ];
  100. $validator = Validator::make($input, $rules ,$messages);
  101. if($validator->fails()){
  102. return response()->json([
  103. 'error_code'=>'41113',
  104. 'msg'=>$validator->errors()->first()
  105. ]);
  106. }
  107. $where['name']=$input['name'];
  108. $where['cre_num']=$input['cre_num'];
  109. $where['del_is']=0;
  110. $teamId=DB::table('users')->where($where)->value('team_id');
  111. if(empty($teamId) || $teamId==0){
  112. return response()->json([
  113. 'error_code' => 0,
  114. 'msg' => '该用户没有支部,不能缴费'
  115. ]);
  116. }
  117. $adminInfo=Auth::guard('admin')->user();
  118. if($adminInfo){
  119. $atid=$adminInfo->team_id;
  120. $atis=$adminInfo->is_team;
  121. if($atis==2){
  122. if($teamId!=$atid){
  123. return response()->json([
  124. 'error_code' => 0,
  125. 'msg' => '该用户不属于本支部,不能缴费'
  126. ]);
  127. }
  128. }
  129. }
  130. $userId=DB::table('users')
  131. ->where($where)->value('id');
  132. if($userId){
  133. $count=DB::table('upload_commit_log')->where('uid',$userId)->count();
  134. if($count==0){
  135. return response()->json([
  136. 'error_code'=>'0',
  137. 'msg'=>'该党员未上传承诺书'
  138. ]);
  139. }
  140. $da['uid']=$userId;
  141. $da['month']=date("Y-m",strtotime($input['month']));
  142. // $da['status']=1;
  143. $chargeStatus=DB::table('charge')
  144. ->where($da)
  145. ->where(function ($query) {
  146. $query->where('status', '=', '1')
  147. ->orWhere('status', '=', '2');
  148. })
  149. ->first();
  150. if(!$chargeStatus) { //未缴费
  151. $fee=ceil(floatval($input['fee'])*100)/100;
  152. if($fee<=0){
  153. return response()->json([
  154. 'error_code'=>'0',
  155. 'msg'=>'缴费金额不能小于等于0元'
  156. ]);
  157. }elseif($fee>=100000000){
  158. return response()->json([
  159. 'error_code'=>'0',
  160. 'msg'=>'缴费金额过大,请核对缴费信息'
  161. ]);
  162. }
  163. $out_trade_no=date("YmdHis").mt_rand(1000,9999).substr(md5(mt_rand(1000,9999)),10,12);
  164. $row = DB::table('charge')->insert([
  165. 'uid' => $userId,
  166. 'month' => date("Y-m",strtotime($input['month'])),
  167. 'fee' => $fee,
  168. 'f_fee' => $fee,
  169. 'aid' => Auth::guard('admin')->user()->id,
  170. 'out_trade_no'=>$out_trade_no,
  171. 'team_id' => $teamId,
  172. 'status' => 1,
  173. 'type' => 2,//后台缴费
  174. 'is_v' => 3,//无手续费
  175. 'v_fee' => 0,
  176. 'income'=>0,
  177. 'openid' => 0,
  178. 'added_on' => date("Y-m-d H:i:s"),
  179. 'pays_at' => date("Y-m-d H:i:s"),
  180. ]);
  181. if ($row) {
  182. return response()->json([
  183. 'error_code' => 200,
  184. 'msg' => '缴费成功'
  185. ]);
  186. } else {
  187. return response()->json([
  188. 'error_code' => 0,
  189. 'msg' => '缴费失败'
  190. ]);
  191. }
  192. }else{
  193. return response()->json([
  194. 'error_code' => 0,
  195. 'msg' => '该用户本月已缴费'
  196. ]);
  197. }
  198. }else{
  199. return response()->json([
  200. 'error_code'=> 10001,
  201. 'msg'=>'用户信息有误'
  202. ]);
  203. }
  204. }
  205. }