123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561 |
- <?php
- namespace Modules\Finance\Http\Controllers;
- use Modules\Finance\Jobs\AgentIncome as AI;
- use Modules\Finance\Events\AgentIncome;
- use Modules\Finance\Events\OrderPay;
- use App\Models\Ordertest;
- use App\Models\Rewards;
- use App\Models\System;
- use Illuminate\Http\Request;
- use App\Models\User;
- use App\Models\Userpay;
- use App\Models\IncomeStyle;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Validator;
- use \Exception;
- class MoneyController extends Controller
- {
- /**
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function index(Request $request)
- {
- $input=$request->all();
- $page_size=$input['page_size'];
- $page_index=$input['page_index'];
- $num = ($page_index - 1) * $page_size;
- $search_name=$input['search_name'];
- $time=$input['time'];
- if(empty($time)){
- $time=date("Y-m-d H:i:s");
- }
- $where=[];
- $where['level']=3;
- $data=User::where($where);
- if(!empty($time)){
- $data->with(['user_pay'=>function($query)use($time){
- $query->where('created_at','<=',$time)->orderByDesc('id');
- }]);
- }
- if($search_name){
- $data->where(function ($query) use ($search_name) {
- $query->where('nickname', 'like', '%' . $search_name . '%')
- ->orWhere('realname', 'like', '%' . $search_name . '%')
- ->orWhere('mobile', $search_name );
- });
- }
- $count=$data->count();
- Log::error($count);
- if($count==0){
- $this->error('400001','没有数据');
- }
- $users=$data->select('id','nickname','realname','mobile','money','level')->get();
- if(!empty($time)){
- $totalMoney=0;
- foreach($users as $key=>$val){
- if(count($val->user_pay)>0){
- $totalMoney+=$val->user_pay[0]->user_money;
- }else{
- Log::info($val);
- }
- }
- $totalMoney1=User::where('level',3)->sum('money');
- }else{
- $totalMoney=$data->sum('money');
- }
- Log::error($totalMoney.'======'.$totalMoney1);
- $list['totalMoney']=round($totalMoney,2);
- $list['info']=$users;
- return $this->success_list($list,'获取数据成功',$count);
- }
- public function detail(Request $request)
- {
- $input=$request->all();
- $user_id=$input['id'];
- $page_size=$input['page_size'];
- $page_index=$input['page_index'];
- $num = ($page_index - 1) * $page_size;
- $where=[];
- $where['user_id']=$user_id;
- $userPay=Userpay::where($where);
- $user=[];
- $user['info']=User::select('id','nickname','level','money')->where('id',$user_id)->first();
- if($input['start_time'] && $input['end_time']){
- $starTime=date("Y-m-d 00:00:00",strtotime($input['start_time']));
- $endTime=date("Y-m-d 23:59:59",strtotime($input['end_time']));
- $userPay=Userpay::whereBetween('created_at',[$starTime,$endTime])->where($where);
- }
- $userPay1=clone $userPay;
- $userPay2=clone $userPay;
- $user['incomeMoney']=$userPay1->whereIn('type',['0','2'])->sum('pay_money');
- $user['spendingMoney']=$userPay2->whereIn('type',['1','3'])->sum('pay_money');
- $user['total']=$userPay->count();
- $user['list']=$userPay->select('id','pay_money','pay_num','created_at','type','style','remark','user_money')
- ->orderBy('id','desc')->skip($num)->take($page_size)->get();
- // DB::raw('CONCAT("【",style,"】",remark) as remarks'),
- return $this->success($user);
- }
- public function deductions(Request $request)
- {
- $input=$request->all();
- $rules=[
- 'user_id'=>'required',
- 'money'=>'required|regex:/^[0-9]+(.[0-9]{1,2})?$/',
- 'remark'=>'required'
- ];
- $messages=[
- 'user_id.required'=>'无代理信息.',
- 'money.required'=>'金额不能为空.',
- 'money.regex'=>'金额格式有误.',
- 'remark.required'=>'备注不能为空.'
- ];
- $validator = Validator::make($input, $rules, $messages);
- if($validator->fails()){
- return $this->error('400013',$validator->errors()->first());
- }
- if($input['money']<=0){ return $this->error('扣款金额必须大于0元');}
- $user_id=$input['user_id'];
- $user=User::where('id',$user_id)->first();
- if($user->money-$input['money']<0){ return $this->error('扣款金额不能大于代理账户余额');}
- $data['user_id']=$user_id;
- $data['pay_money']=$input['money'];
- $data['remark']=$input['remark'];
- $data['type']=3;
- $data['pay_num']=$this->pay_num($user_id);
- $data['op_name']=Auth::user()->name;
- $data['user_money']=$user->money=$user->money-$input['money'];
- DB::beginTransaction();
- $row=$user->save();
- $res=Userpay::create($data);
- if($row && $res){
- if($user->openid){
- $mes['openid']=$user->openid;
- $mes['data']=[
- 'first' => '后台人工操作扣款成功',
- 'keyword1' => '-'.number_format($input['money'], 2),
- 'keyword2' => number_format($user->money, 2),
- 'keyword3' => date("Y-m-d H:i"),
- 'keyword4' => '人工扣款',
- 'remark' => $input['remark'].PHP_EOL.'点击链接进入系统,查看详情',
- ];
- event(new OrderPay($mes));
- }
- DB::commit();
- return $this->success([]);
- }else{
- DB::rollback();
- return $this->error();
- }
- }
- public function income(Request $request)
- {
- $input=$request->all();
- $rules=[
- 'user_id'=>'required',
- 'money'=>'required|regex:/^[0-9]+(.[0-9]{1,2})?$/',
- // 'remark'=>'required',
- 'style'=>'required'
- ];
- $messages=[
- 'user_id.required'=>'无代理信息.',
- 'money.required'=>'金额不能为空.',
- 'money.regex'=>'金额格式有误.',
- // 'remark.required'=>'备注不能为空.',
- 'style.required'=>'转账方式不能为空.'
- ];
- $validator = Validator::make($input, $rules, $messages);
- if($validator->fails()){
- return $this->error('400013',$validator->errors()->first());
- }
- if($input['money']<=0){ return $this->error('450001','充值金额必须大于0元');}
- $user_id=$input['user_id'];
- $data['user_id']=$user_id;
- $data['pay_money']=$input['money'];
- $data['remark']=$input['remark'];
- $data['type']=0;
- $data['style']=$input['style'];
- $data['pay_num']=$this->pay_num($user_id);
- $data['op_name']=Auth::user()->name;
- $user=User::where('id',$user_id)->lockForUpdate()->first();
- $cur_money=$user->money+$input['money'];
- $data['user_money']=$user->money=$cur_money;
- DB::beginTransaction();
- $row=$user->save();
- $res=Userpay::create($data);
- if($row && $res){
- if($user->openid){
- $mes['openid']=$user->openid;
- $mes['data']=[
- 'first' => '您的充值金额已成功到账',
- 'keyword1' => number_format($input['money'],2),
- 'keyword2' => date("Y-m-d H:i"),
- 'keyword3' => number_format($cur_money,2),
- 'remark' => '点击链接进入系统,查看详情',
- ];
- event(new AgentIncome($mes));
- }
- DB::commit();
- return $this->success([]);
- }else{
- DB::rollback();
- return $this->error();
- }
- }
- public function export(Request $request){
- $input=$request->all();
- // $page_size=$input['page_size'];
- // $page_index=$input['page_index'];
- // $num = ($page_index - 1) * $page_size;
- $search_name=$input['search_name'];
- $time=$input['time'];
- if(empty($time)){
- $time=date("Y-m-d H:i:s");
- }
- $where=[];
- $where['level']=3;
- $data=User::where($where);
- if(!empty($time)){
- $data->with(['user_pay'=>function($query)use($time){
- $query->where('created_at','<=',$time)->orderByDesc('id');
- }]);
- }
- if($search_name){
- $data->where(function ($query) use ($search_name) {
- $query->where('nickname', 'like', '%' . $search_name . '%')
- ->orWhere('realname', 'like', '%' . $search_name . '%')
- ->orWhere('mobile', $search_name );
- });
- }
- $count=$data->count();
- if($count==0){
- $this->error('400001','没有数据');
- }
- $users=$data->select('id','nickname','realname','mobile','money','level')->get();
- if(!empty($time)){
- $totalMoney=0;
- foreach($users as $key=>$val){
- if(count($val->user_pay)>0){
- $totalMoney+=$val->user_pay[0]->user_money;
- }
- }
- }else{
- $totalMoney=$data->sum('money');
- }
- $list['totalMoney']=round($totalMoney,2);
- $list['info']=$users;
- return $this->success_list($list,'获取数据成功',$count);
- }
- public function import(Request $request){
- $input=$request->all();
- // if(empty($input['style'])){
- // return $this->error(40013,'转账方式不能为空');
- // }
- if(empty($input['data'])){
- return $this->error(40013,'充值信息不能为空');
- }
- foreach ($input['data'] as $key=>$val){
- if(empty($val['代理名称'])){
- return $this->error(40013,'第'.($key+1).'位代理,名称不能为空');
- }
- if(empty($val['手机号'])){
- return $this->error(40013,'第'.($key+1).'位代理,手机号不能为空');
- }
- $user=User::where('mobile',$val['手机号'])->first();
- if (!$user){
- return $this->error(40013,'代理'.$val['代理名称'].'('.$val['手机号'].')不存在');
- }
- if(empty($val['金额']) || $val['金额']<=0){
- return $this->error(40013,'代理'.$val['代理名称'].'('.$val['手机号'].')充值金额必须大于0元');
- }
- if(!preg_match('/^[0-9]+(.[0-9]{1,2})?$/',$val['金额'])){
- return $this->error(40013,'代理'.$val['代理名称'].'('.$val['手机号'].')充值金额格式有误');
- }
- // if(empty($val['备注'])){
- // return $this->error(40013,'代理'.$val['代理名称'].'充值备注不能为空');
- // }
- }
- DB::beginTransaction();
- try{
- foreach ($input['data'] as $key=>$val){
- $user=User::where('mobile',$val['手机号'])->lockForUpdate()->first();
- $user->money=$user->money+$val['金额'];
- if(!$user->save()){
- Log::error('批量充值失败(代理)'.$val['手机号']);
- }
- $userpay=new Userpay();
- $userpay->pay_money=$val['金额'];
- $userpay->type=0;
- $userpay->op_name=Auth::user()->name;
- $userpay->user_id=$user->id;
- $userpay->pay_num=$this->pay_num($user->id);
- $userpay->user_money=$user->money;
- $userpay->remark=isset($val['备注'])?$val['备注']:"";
- $userpay->style=isset($val['转账方式'])?$val['转账方式']:"";
- if(!$userpay->save()){
- Log::error('批量充值失败(支付)'.$val['手机号']);
- }
- // if($user->openid){
- // $mes['openid']=$user->openid;
- // $mes['data']=[
- // 'first' => '您的充值金额已成功到账',
- // 'keyword1' => number_format($val['金额'],2),
- // 'keyword2' => date("Y-m-d H:i"),
- // 'keyword3' => number_format($user->money,2),
- // 'remark' => '点击链接进入系统,查看详情',
- // ];
- // AI::dispatch($mes);
- // }
- }
- DB::commit();
- return $this->success([],'充值完成');
- }catch (Exception $exception){
- DB::rollBack();
- return $this->error(40013,'充值失败,出错代理手机号为'.$val['手机号']);
- }
- }
- //添加充值方式
- public function addIncomeStyle(Request $request){
- $input=$request->all();
- $style=IncomeStyle::where('title',$input['title'])->first();
- if($style){
- return $this->error('450001','该方式已存在');
- }
- $row=IncomeStyle::create([
- 'title'=>$input['title'],
- 'admin'=>Auth::user()->name
- ]);
- if($row){
- return $this->success([]);
- }
- return $this->error();
- }
- //获取充值方式
- public function getIncomeStyle(){
- $list=IncomeStyle::all();
- if($list){
- return $this->success($list);
- }
- return $this->error();
- }
- //删除充值转账方式
- public function deleteIncomeStyle(Request $request){
- $input=$request->all();
- $res=IncomeStyle::where('id',$input['id'])->delete();
- if($res){
- return $this->success([]);
- }
- return $this->error();
- }
- //支付单号
- public function pay_num($user_id){
- $num=date('Ymd',time()).'100000'.str_pad($user_id,6,0,STR_PAD_LEFT).rand(11111,99999);
- $count=UserPay::where('pay_num',$num)->count();
- if ($count>0){
- $num=$this->pay_num($user_id);
- return $num;
- }
- return $num;
- }
- //收支明细
- public function getPaymentList(Request $request){
- $input=$request->all();
- $where=[];
- $page_size=$input['page_size'];
- $page_index=$input['page_index'];
- $num = ($page_index - 1) * $page_size;
- $pay_no=$input['pay_no'];
- $type=$input['type'];
- $start_time=$input['start_time'];
- $end_time=$input['end_time'];
- $search_name=$input['search_name'];
- if($pay_no){
- $where['pay_num']=$pay_no;
- }
- if($type || $type == '0'){
- $where['type']=$type;
- }
- $userPay=Userpay::where($where);
- if($search_name){
- $user_ids=$this->getUserIds($search_name);
- $userPay->whereIn('user_id',$user_ids);
- }
- if($start_time && $end_time){
- $userPay->whereBetween('created_at',[$start_time,$end_time]);
- }
- $count=$userPay->count();
- // if($count<=0){
- // return $this->error('450001','没有数据');
- // }
- $list=$userPay->with(['user:id,nickname,realname,mobile'])->orderBy('created_at','desc')->skip($num)->take($page_size)->get();
- return $this->success_list($list,'成功',$count);
- }
- //导出收支明细
- public function exportPaymentList(Request $request){
- $input=$request->all();
- $where=[];
- $pay_no=$input['pay_no'];
- $search_name=$input['search_name'];
- $type=$input['type'];
- $start_time=$input['start_time'];
- $end_time=$input['end_time'];
- if($pay_no){
- $where['pay_num']=$pay_no;
- }
- if($type || $type == '0'){
- $where['type']=$type;
- }
- $userPay=Userpay::where($where);
- if($search_name){
- $user_ids=$this->getUserIds($search_name);
- $userPay->whereIn('user_id',$user_ids);
- }
- if($start_time && $end_time){
- $userPay->whereBetween('created_at',[$start_time,$end_time]);
- }
- $count=$userPay->count();
- if($count<=0){
- return $this->error('450001','没有数据');
- }
- $list=$userPay->with(['user:id,nickname,realname,mobile'])->orderBy('created_at','desc')->get();
- $ll=[];
- foreach($list as $key=>$val){
- $ll[$key]['支付流水号']=$val->pay_num;
- $ll[$key]['订单号']=$val->order_num;
- $ll[$key]['时间']=date("Y-m-d H:i:s",strtotime($val->created_at));
- $ll[$key]['代理昵称']=$val->user->nickname;
- $ll[$key]['代理姓名']=$val->user->realname;
- $ll[$key]['手机号']=$val->user->mobile;
- if($val->type==0){
- $ll[$key]['摘要']='后台充值';
- }elseif($val->type==1){
- $ll[$key]['摘要']='支付';
- }elseif($val->type==2){
- $ll[$key]['摘要']='退回';
- }elseif($val->type==3){
- $ll[$key]['摘要']='扣款';
- }
- $ll[$key]['金额']=$val->pay_money;
- $ll[$key]['支付方式']=$val->style;
- $ll[$key]['备注']=$val->remark;
- $ll[$key]['操作人']=$val->op_name;
- }
- return $this->success($ll);
- }
- //获取代理Ids
- public function getUserIds($search=null){
- return User::where(function($query) use ($search){
- $query->where('nickname','like','%'.$search.'%')
- ->orwhere('mobile','like','%'.$search.'%');
- })->pluck('id');
- }
- //退款确认 需要批量退款代理信息
- public function getManyBackMoneyUser(Request $request)
- {
- $input = $request->all();
- $page_index = $input['page_index'];
- $page_size = $input['page_size'];
- $num = ($page_index - 1) * $page_size;
- $order = Ordertest::where('pay_status', 3)->with(['get_user:id,nickname,mobile']);
- $count = $order->count();
- if ($count <= 0) {
- $this->error('450001', '暂无信息');
- }
- $list = $order->orderBy('id', 'desc')->skip($num)->take($page_size)->get();
- return $this->success_list($list, '获取数据成功', $count);
- }
- //获取门店业绩奖励货物结算信息
- public function getStoreSalesResultGoods(Request $request){
- $input=$request->all();
- $page_size=$input['page_size'];
- $page_index=$input['page_index'];
- $num = ($page_index - 1) * $page_size;
- $type=$input['type'];
- $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
- $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
- $time = [$starttime, $endtime];
- if($type==1){
- $rate=System::where('keys','store_reward_goods_job')->value('values');
- }elseif($type==2){
- $rate=System::where('keys','store_reward_goods_enjoy')->value('values');
- }
- $count = DB::table('store')
- ->select('store.name as storename', DB::raw('SUM(order_goods.totalprice) as account'))
- ->leftJoin('users', function ($join) {
- $join->on('users.store_id', '=', 'store.id');
- })
- ->leftJoin('order', 'order.user_id', '=', 'users.id')
- ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
- ->leftJoin('users as uu','uu.id','=','store.man_id')
- ->where('store.type',$type)
- ->whereIn('order.status',[2,3])
- ->whereBetween('order.created_at',$time)
- ->groupBy('store.id')
- ->get()->count();
- if($count==0){
- $this->error('400001','没有数据');
- }
- $list = DB::table('store')
- ->select('store.id','store.name as storename','store.man_id','uu.nickname', DB::raw('SUM(order_goods.totalprice) as account'))
- ->leftJoin('users', function ($join) {
- $join->on('users.store_id', '=', 'store.id');
- })
- ->leftJoin('order', 'order.user_id', '=', 'users.id')
- ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
- ->leftJoin('users as uu','uu.id','=','store.man_id')
- ->where('store.type',$type)
- ->whereIn('order.status',[2,3])
- ->whereBetween('order.created_at',$time)
- ->groupBy('store.id')
- ->orderBy('account','desc')
- ->skip($num)->take($page_size)
- ->get();
- foreach($list as $key =>$val){
- $rewards=Rewards::where('recom_id',$val->man_id)->where('type',3)->where('month',$input['time'])->first();
- if($rewards){
- $list[$key]->finished=1;
- }else{
- $list[$key]->finished=0;
- }
- $list[$key]->style=2;
- $list[$key]->rewards=round($val->account*$rate/100,2);
- }
- if ($list) {
- return $this->success_list($list,'SUCCESS',$count);
- } else {
- return $this->error();
- }
- }
- }
|