MoneyController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. <?php
  2. namespace Modules\Finance\Http\Controllers;
  3. use Modules\Finance\Jobs\AgentIncome as AI;
  4. use Modules\Finance\Events\AgentIncome;
  5. use Modules\Finance\Events\OrderPay;
  6. use App\Models\Ordertest;
  7. use App\Models\Rewards;
  8. use App\Models\System;
  9. use Illuminate\Http\Request;
  10. use App\Models\User;
  11. use App\Models\Userpay;
  12. use App\Models\IncomeStyle;
  13. use Illuminate\Support\Facades\Auth;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. use Illuminate\Support\Facades\Validator;
  17. use \Exception;
  18. class MoneyController extends Controller
  19. {
  20. /**
  21. * @param Request $request
  22. * @return \Illuminate\Http\JsonResponse
  23. */
  24. public function index(Request $request)
  25. {
  26. $input=$request->all();
  27. $page_size=$input['page_size'];
  28. $page_index=$input['page_index'];
  29. $num = ($page_index - 1) * $page_size;
  30. $search_name=$input['search_name'];
  31. $time=$input['time'];
  32. if(empty($time)){
  33. $time=date("Y-m-d H:i:s");
  34. }
  35. $where=[];
  36. $where['level']=3;
  37. $data=User::where($where);
  38. if(!empty($time)){
  39. $data->with(['user_pay'=>function($query)use($time){
  40. $query->where('created_at','<=',$time)->orderByDesc('id');
  41. }]);
  42. }
  43. if($search_name){
  44. $data->where(function ($query) use ($search_name) {
  45. $query->where('nickname', 'like', '%' . $search_name . '%')
  46. ->orWhere('realname', 'like', '%' . $search_name . '%')
  47. ->orWhere('mobile', $search_name );
  48. });
  49. }
  50. $count=$data->count();
  51. Log::error($count);
  52. if($count==0){
  53. $this->error('400001','没有数据');
  54. }
  55. $users=$data->select('id','nickname','realname','mobile','money','level')->get();
  56. if(!empty($time)){
  57. $totalMoney=0;
  58. foreach($users as $key=>$val){
  59. if(count($val->user_pay)>0){
  60. $totalMoney+=$val->user_pay[0]->user_money;
  61. }else{
  62. Log::info($val);
  63. }
  64. }
  65. $totalMoney1=User::where('level',3)->sum('money');
  66. }else{
  67. $totalMoney=$data->sum('money');
  68. }
  69. Log::error($totalMoney.'======'.$totalMoney1);
  70. $list['totalMoney']=round($totalMoney,2);
  71. $list['info']=$users;
  72. return $this->success_list($list,'获取数据成功',$count);
  73. }
  74. public function detail(Request $request)
  75. {
  76. $input=$request->all();
  77. $user_id=$input['id'];
  78. $page_size=$input['page_size'];
  79. $page_index=$input['page_index'];
  80. $num = ($page_index - 1) * $page_size;
  81. $where=[];
  82. $where['user_id']=$user_id;
  83. $userPay=Userpay::where($where);
  84. $user=[];
  85. $user['info']=User::select('id','nickname','level','money')->where('id',$user_id)->first();
  86. if($input['start_time'] && $input['end_time']){
  87. $starTime=date("Y-m-d 00:00:00",strtotime($input['start_time']));
  88. $endTime=date("Y-m-d 23:59:59",strtotime($input['end_time']));
  89. $userPay=Userpay::whereBetween('created_at',[$starTime,$endTime])->where($where);
  90. }
  91. $userPay1=clone $userPay;
  92. $userPay2=clone $userPay;
  93. $user['incomeMoney']=$userPay1->whereIn('type',['0','2'])->sum('pay_money');
  94. $user['spendingMoney']=$userPay2->whereIn('type',['1','3'])->sum('pay_money');
  95. $user['total']=$userPay->count();
  96. $user['list']=$userPay->select('id','pay_money','pay_num','created_at','type','style','remark','user_money')
  97. ->orderBy('id','desc')->skip($num)->take($page_size)->get();
  98. // DB::raw('CONCAT("【",style,"】",remark) as remarks'),
  99. return $this->success($user);
  100. }
  101. public function deductions(Request $request)
  102. {
  103. $input=$request->all();
  104. $rules=[
  105. 'user_id'=>'required',
  106. 'money'=>'required|regex:/^[0-9]+(.[0-9]{1,2})?$/',
  107. 'remark'=>'required'
  108. ];
  109. $messages=[
  110. 'user_id.required'=>'无代理信息.',
  111. 'money.required'=>'金额不能为空.',
  112. 'money.regex'=>'金额格式有误.',
  113. 'remark.required'=>'备注不能为空.'
  114. ];
  115. $validator = Validator::make($input, $rules, $messages);
  116. if($validator->fails()){
  117. return $this->error('400013',$validator->errors()->first());
  118. }
  119. if($input['money']<=0){ return $this->error('扣款金额必须大于0元');}
  120. $user_id=$input['user_id'];
  121. $user=User::where('id',$user_id)->first();
  122. if($user->money-$input['money']<0){ return $this->error('扣款金额不能大于代理账户余额');}
  123. $data['user_id']=$user_id;
  124. $data['pay_money']=$input['money'];
  125. $data['remark']=$input['remark'];
  126. $data['type']=3;
  127. $data['pay_num']=$this->pay_num($user_id);
  128. $data['op_name']=Auth::user()->name;
  129. $data['user_money']=$user->money=$user->money-$input['money'];
  130. DB::beginTransaction();
  131. $row=$user->save();
  132. $res=Userpay::create($data);
  133. if($row && $res){
  134. if($user->openid){
  135. $mes['openid']=$user->openid;
  136. $mes['data']=[
  137. 'first' => '后台人工操作扣款成功',
  138. 'keyword1' => '-'.number_format($input['money'], 2),
  139. 'keyword2' => number_format($user->money, 2),
  140. 'keyword3' => date("Y-m-d H:i"),
  141. 'keyword4' => '人工扣款',
  142. 'remark' => $input['remark'].PHP_EOL.'点击链接进入系统,查看详情',
  143. ];
  144. event(new OrderPay($mes));
  145. }
  146. DB::commit();
  147. return $this->success([]);
  148. }else{
  149. DB::rollback();
  150. return $this->error();
  151. }
  152. }
  153. public function income(Request $request)
  154. {
  155. $input=$request->all();
  156. $rules=[
  157. 'user_id'=>'required',
  158. 'money'=>'required|regex:/^[0-9]+(.[0-9]{1,2})?$/',
  159. // 'remark'=>'required',
  160. 'style'=>'required'
  161. ];
  162. $messages=[
  163. 'user_id.required'=>'无代理信息.',
  164. 'money.required'=>'金额不能为空.',
  165. 'money.regex'=>'金额格式有误.',
  166. // 'remark.required'=>'备注不能为空.',
  167. 'style.required'=>'转账方式不能为空.'
  168. ];
  169. $validator = Validator::make($input, $rules, $messages);
  170. if($validator->fails()){
  171. return $this->error('400013',$validator->errors()->first());
  172. }
  173. if($input['money']<=0){ return $this->error('450001','充值金额必须大于0元');}
  174. $user_id=$input['user_id'];
  175. $data['user_id']=$user_id;
  176. $data['pay_money']=$input['money'];
  177. $data['remark']=$input['remark'];
  178. $data['type']=0;
  179. $data['style']=$input['style'];
  180. $data['pay_num']=$this->pay_num($user_id);
  181. $data['op_name']=Auth::user()->name;
  182. $user=User::where('id',$user_id)->lockForUpdate()->first();
  183. $cur_money=$user->money+$input['money'];
  184. $data['user_money']=$user->money=$cur_money;
  185. DB::beginTransaction();
  186. $row=$user->save();
  187. $res=Userpay::create($data);
  188. if($row && $res){
  189. if($user->openid){
  190. $mes['openid']=$user->openid;
  191. $mes['data']=[
  192. 'first' => '您的充值金额已成功到账',
  193. 'keyword1' => number_format($input['money'],2),
  194. 'keyword2' => date("Y-m-d H:i"),
  195. 'keyword3' => number_format($cur_money,2),
  196. 'remark' => '点击链接进入系统,查看详情',
  197. ];
  198. event(new AgentIncome($mes));
  199. }
  200. DB::commit();
  201. return $this->success([]);
  202. }else{
  203. DB::rollback();
  204. return $this->error();
  205. }
  206. }
  207. public function export(Request $request){
  208. $input=$request->all();
  209. // $page_size=$input['page_size'];
  210. // $page_index=$input['page_index'];
  211. // $num = ($page_index - 1) * $page_size;
  212. $search_name=$input['search_name'];
  213. $time=$input['time'];
  214. if(empty($time)){
  215. $time=date("Y-m-d H:i:s");
  216. }
  217. $where=[];
  218. $where['level']=3;
  219. $data=User::where($where);
  220. if(!empty($time)){
  221. $data->with(['user_pay'=>function($query)use($time){
  222. $query->where('created_at','<=',$time)->orderByDesc('id');
  223. }]);
  224. }
  225. if($search_name){
  226. $data->where(function ($query) use ($search_name) {
  227. $query->where('nickname', 'like', '%' . $search_name . '%')
  228. ->orWhere('realname', 'like', '%' . $search_name . '%')
  229. ->orWhere('mobile', $search_name );
  230. });
  231. }
  232. $count=$data->count();
  233. if($count==0){
  234. $this->error('400001','没有数据');
  235. }
  236. $users=$data->select('id','nickname','realname','mobile','money','level')->get();
  237. if(!empty($time)){
  238. $totalMoney=0;
  239. foreach($users as $key=>$val){
  240. if(count($val->user_pay)>0){
  241. $totalMoney+=$val->user_pay[0]->user_money;
  242. }
  243. }
  244. }else{
  245. $totalMoney=$data->sum('money');
  246. }
  247. $list['totalMoney']=round($totalMoney,2);
  248. $list['info']=$users;
  249. return $this->success_list($list,'获取数据成功',$count);
  250. }
  251. public function import(Request $request){
  252. $input=$request->all();
  253. // if(empty($input['style'])){
  254. // return $this->error(40013,'转账方式不能为空');
  255. // }
  256. if(empty($input['data'])){
  257. return $this->error(40013,'充值信息不能为空');
  258. }
  259. foreach ($input['data'] as $key=>$val){
  260. if(empty($val['代理名称'])){
  261. return $this->error(40013,'第'.($key+1).'位代理,名称不能为空');
  262. }
  263. if(empty($val['手机号'])){
  264. return $this->error(40013,'第'.($key+1).'位代理,手机号不能为空');
  265. }
  266. $user=User::where('mobile',$val['手机号'])->first();
  267. if (!$user){
  268. return $this->error(40013,'代理'.$val['代理名称'].'('.$val['手机号'].')不存在');
  269. }
  270. if(empty($val['金额']) || $val['金额']<=0){
  271. return $this->error(40013,'代理'.$val['代理名称'].'('.$val['手机号'].')充值金额必须大于0元');
  272. }
  273. if(!preg_match('/^[0-9]+(.[0-9]{1,2})?$/',$val['金额'])){
  274. return $this->error(40013,'代理'.$val['代理名称'].'('.$val['手机号'].')充值金额格式有误');
  275. }
  276. // if(empty($val['备注'])){
  277. // return $this->error(40013,'代理'.$val['代理名称'].'充值备注不能为空');
  278. // }
  279. }
  280. DB::beginTransaction();
  281. try{
  282. foreach ($input['data'] as $key=>$val){
  283. $user=User::where('mobile',$val['手机号'])->lockForUpdate()->first();
  284. $user->money=$user->money+$val['金额'];
  285. if(!$user->save()){
  286. Log::error('批量充值失败(代理)'.$val['手机号']);
  287. }
  288. $userpay=new Userpay();
  289. $userpay->pay_money=$val['金额'];
  290. $userpay->type=0;
  291. $userpay->op_name=Auth::user()->name;
  292. $userpay->user_id=$user->id;
  293. $userpay->pay_num=$this->pay_num($user->id);
  294. $userpay->user_money=$user->money;
  295. $userpay->remark=isset($val['备注'])?$val['备注']:"";
  296. $userpay->style=isset($val['转账方式'])?$val['转账方式']:"";
  297. if(!$userpay->save()){
  298. Log::error('批量充值失败(支付)'.$val['手机号']);
  299. }
  300. // if($user->openid){
  301. // $mes['openid']=$user->openid;
  302. // $mes['data']=[
  303. // 'first' => '您的充值金额已成功到账',
  304. // 'keyword1' => number_format($val['金额'],2),
  305. // 'keyword2' => date("Y-m-d H:i"),
  306. // 'keyword3' => number_format($user->money,2),
  307. // 'remark' => '点击链接进入系统,查看详情',
  308. // ];
  309. // AI::dispatch($mes);
  310. // }
  311. }
  312. DB::commit();
  313. return $this->success([],'充值完成');
  314. }catch (Exception $exception){
  315. DB::rollBack();
  316. return $this->error(40013,'充值失败,出错代理手机号为'.$val['手机号']);
  317. }
  318. }
  319. //添加充值方式
  320. public function addIncomeStyle(Request $request){
  321. $input=$request->all();
  322. $style=IncomeStyle::where('title',$input['title'])->first();
  323. if($style){
  324. return $this->error('450001','该方式已存在');
  325. }
  326. $row=IncomeStyle::create([
  327. 'title'=>$input['title'],
  328. 'admin'=>Auth::user()->name
  329. ]);
  330. if($row){
  331. return $this->success([]);
  332. }
  333. return $this->error();
  334. }
  335. //获取充值方式
  336. public function getIncomeStyle(){
  337. $list=IncomeStyle::all();
  338. if($list){
  339. return $this->success($list);
  340. }
  341. return $this->error();
  342. }
  343. //删除充值转账方式
  344. public function deleteIncomeStyle(Request $request){
  345. $input=$request->all();
  346. $res=IncomeStyle::where('id',$input['id'])->delete();
  347. if($res){
  348. return $this->success([]);
  349. }
  350. return $this->error();
  351. }
  352. //支付单号
  353. public function pay_num($user_id){
  354. $num=date('Ymd',time()).'100000'.str_pad($user_id,6,0,STR_PAD_LEFT).rand(11111,99999);
  355. $count=UserPay::where('pay_num',$num)->count();
  356. if ($count>0){
  357. $num=$this->pay_num($user_id);
  358. return $num;
  359. }
  360. return $num;
  361. }
  362. //收支明细
  363. public function getPaymentList(Request $request){
  364. $input=$request->all();
  365. $where=[];
  366. $page_size=$input['page_size'];
  367. $page_index=$input['page_index'];
  368. $num = ($page_index - 1) * $page_size;
  369. $pay_no=$input['pay_no'];
  370. $type=$input['type'];
  371. $start_time=$input['start_time'];
  372. $end_time=$input['end_time'];
  373. $search_name=$input['search_name'];
  374. if($pay_no){
  375. $where['pay_num']=$pay_no;
  376. }
  377. if($type || $type == '0'){
  378. $where['type']=$type;
  379. }
  380. $userPay=Userpay::where($where);
  381. if($search_name){
  382. $user_ids=$this->getUserIds($search_name);
  383. $userPay->whereIn('user_id',$user_ids);
  384. }
  385. if($start_time && $end_time){
  386. $userPay->whereBetween('created_at',[$start_time,$end_time]);
  387. }
  388. $count=$userPay->count();
  389. // if($count<=0){
  390. // return $this->error('450001','没有数据');
  391. // }
  392. $list=$userPay->with(['user:id,nickname,realname,mobile'])->orderBy('created_at','desc')->skip($num)->take($page_size)->get();
  393. return $this->success_list($list,'成功',$count);
  394. }
  395. //导出收支明细
  396. public function exportPaymentList(Request $request){
  397. $input=$request->all();
  398. $where=[];
  399. $pay_no=$input['pay_no'];
  400. $search_name=$input['search_name'];
  401. $type=$input['type'];
  402. $start_time=$input['start_time'];
  403. $end_time=$input['end_time'];
  404. if($pay_no){
  405. $where['pay_num']=$pay_no;
  406. }
  407. if($type || $type == '0'){
  408. $where['type']=$type;
  409. }
  410. $userPay=Userpay::where($where);
  411. if($search_name){
  412. $user_ids=$this->getUserIds($search_name);
  413. $userPay->whereIn('user_id',$user_ids);
  414. }
  415. if($start_time && $end_time){
  416. $userPay->whereBetween('created_at',[$start_time,$end_time]);
  417. }
  418. $count=$userPay->count();
  419. if($count<=0){
  420. return $this->error('450001','没有数据');
  421. }
  422. $list=$userPay->with(['user:id,nickname,realname,mobile'])->orderBy('created_at','desc')->get();
  423. $ll=[];
  424. foreach($list as $key=>$val){
  425. $ll[$key]['支付流水号']=$val->pay_num;
  426. $ll[$key]['订单号']=$val->order_num;
  427. $ll[$key]['时间']=date("Y-m-d H:i:s",strtotime($val->created_at));
  428. $ll[$key]['代理昵称']=$val->user->nickname;
  429. $ll[$key]['代理姓名']=$val->user->realname;
  430. $ll[$key]['手机号']=$val->user->mobile;
  431. if($val->type==0){
  432. $ll[$key]['摘要']='后台充值';
  433. }elseif($val->type==1){
  434. $ll[$key]['摘要']='支付';
  435. }elseif($val->type==2){
  436. $ll[$key]['摘要']='退回';
  437. }elseif($val->type==3){
  438. $ll[$key]['摘要']='扣款';
  439. }
  440. $ll[$key]['金额']=$val->pay_money;
  441. $ll[$key]['支付方式']=$val->style;
  442. $ll[$key]['备注']=$val->remark;
  443. $ll[$key]['操作人']=$val->op_name;
  444. }
  445. return $this->success($ll);
  446. }
  447. //获取代理Ids
  448. public function getUserIds($search=null){
  449. return User::where(function($query) use ($search){
  450. $query->where('nickname','like','%'.$search.'%')
  451. ->orwhere('mobile','like','%'.$search.'%');
  452. })->pluck('id');
  453. }
  454. //退款确认 需要批量退款代理信息
  455. public function getManyBackMoneyUser(Request $request)
  456. {
  457. $input = $request->all();
  458. $page_index = $input['page_index'];
  459. $page_size = $input['page_size'];
  460. $num = ($page_index - 1) * $page_size;
  461. $order = Ordertest::where('pay_status', 3)->with(['get_user:id,nickname,mobile']);
  462. $count = $order->count();
  463. if ($count <= 0) {
  464. $this->error('450001', '暂无信息');
  465. }
  466. $list = $order->orderBy('id', 'desc')->skip($num)->take($page_size)->get();
  467. return $this->success_list($list, '获取数据成功', $count);
  468. }
  469. //获取门店业绩奖励货物结算信息
  470. public function getStoreSalesResultGoods(Request $request){
  471. $input=$request->all();
  472. $page_size=$input['page_size'];
  473. $page_index=$input['page_index'];
  474. $num = ($page_index - 1) * $page_size;
  475. $type=$input['type'];
  476. $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
  477. $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
  478. $time = [$starttime, $endtime];
  479. if($type==1){
  480. $rate=System::where('keys','store_reward_goods_job')->value('values');
  481. }elseif($type==2){
  482. $rate=System::where('keys','store_reward_goods_enjoy')->value('values');
  483. }
  484. $count = DB::table('store')
  485. ->select('store.name as storename', DB::raw('SUM(order_goods.totalprice) as account'))
  486. ->leftJoin('users', function ($join) {
  487. $join->on('users.store_id', '=', 'store.id');
  488. })
  489. ->leftJoin('order', 'order.user_id', '=', 'users.id')
  490. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  491. ->leftJoin('users as uu','uu.id','=','store.man_id')
  492. ->where('store.type',$type)
  493. ->whereIn('order.status',[2,3])
  494. ->whereBetween('order.created_at',$time)
  495. ->groupBy('store.id')
  496. ->get()->count();
  497. if($count==0){
  498. $this->error('400001','没有数据');
  499. }
  500. $list = DB::table('store')
  501. ->select('store.id','store.name as storename','store.man_id','uu.nickname', DB::raw('SUM(order_goods.totalprice) as account'))
  502. ->leftJoin('users', function ($join) {
  503. $join->on('users.store_id', '=', 'store.id');
  504. })
  505. ->leftJoin('order', 'order.user_id', '=', 'users.id')
  506. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  507. ->leftJoin('users as uu','uu.id','=','store.man_id')
  508. ->where('store.type',$type)
  509. ->whereIn('order.status',[2,3])
  510. ->whereBetween('order.created_at',$time)
  511. ->groupBy('store.id')
  512. ->orderBy('account','desc')
  513. ->skip($num)->take($page_size)
  514. ->get();
  515. foreach($list as $key =>$val){
  516. $rewards=Rewards::where('recom_id',$val->man_id)->where('type',3)->where('month',$input['time'])->first();
  517. if($rewards){
  518. $list[$key]->finished=1;
  519. }else{
  520. $list[$key]->finished=0;
  521. }
  522. $list[$key]->style=2;
  523. $list[$key]->rewards=round($val->account*$rate/100,2);
  524. }
  525. if ($list) {
  526. return $this->success_list($list,'SUCCESS',$count);
  527. } else {
  528. return $this->error();
  529. }
  530. }
  531. }