123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653 |
- <?php
- namespace App\Http\Controllers;
- use App\Events\ReturnGoodLog;
- use App\Events\SendGoodLogEvent;
- use App\Models\GoodSelect;
- use App\Models\ScanStorageLog;
- use App\Models\Select;
- use App\Models\Sell;
- use App\Models\SendGoodLog;
- use App\Models\User;
- use App\Models\UserStorage;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Validator;
- class UserGoodSendController extends Controller{
- /**代理发货**/
- public function user_send_good(Request $request)
- {
- $code = urldecode($request->code);
- // $rule = [
- // 'id' => 'required'
- // ];
- // $message = [
- // 'id.required' => '缺少必填项(id)'
- // ];
- // $va = Validator::make($request->all(), $rule, $message);
- // if ($va->fails()) {
- // return $this->error(50023, '', $va->errors()->first());
- // }
- DB::beginTransaction();
- try {
- $user = Auth::user();
- $users = User::find($user->id);
- $code=trim($code);
- $re = Select::where('code', $code)->first();
- if (!$re){
- return $this->error(500214,'','暂未找到相应的信息');
- }
- if ($re->is_true == 1) {
- return $this->error(40028, '', '当前货物为假货,不能进行退货处理');
- }
- if ($re->com_binding == 1) {
- return $this->error(40028, '', '当前货物为没有与公司绑定,不能进行发货处理');
- }
- //先判断当前二维码能否进行发货
- if ($re->is_full == 1) {
- return $this->error(50021, '', '当前二维码中的部分货物已经发货,不能使用此码进行发货');
- }
- // if ($request->id == $user->id) {
- // return $this->error(50021, '', '不能对自己进行发货操作');
- // }
- $code_level=0;
- $code_num=0;
- if ($re->is_lowest != 1) {
- //中码或者大码进行发货
- if ($re->pid == 0 && is_numeric($re->pid)) {//不能使用大码进行发货
- return $this->error(50021, '', '请使用中码或者小码发货'.$re->pid);
- }
- if ($user->level!=3){
- return $this->error(50021, '', '请使用小码进行发货');
- }
- $code_level=1;
- $code_num=$re->child_num;
- }
- //判断当前二维码的是不是最低级
- // if ($re->is_lowest == 1) {
- // $re->track_status = 2;
- // $re->user_id = $request->id;
- // if (!$re->save()) {
- // throw new \Exception('1');
- // }
- // $user_s = User::find($request->id);
- // $data = [
- // 'op_name' => $users->nickname,
- // 'op_phone' => $users->mobile,
- // 'op_id' => $users->id,
- // 'accept_name' => $user_s->nickname,
- // 'accept_phone' => $user_s->mobile,
- // 'accept_id' => $user_s->id,
- // 'type' => 0,
- // 'select_id' => $re->id
- // ];
- // event(new SendGoodLogEvent($data));
- // //修改大码和中码状态
- // $num = $this->update_full($re->pid);
- // if ($num == 300) {
- // return $this->error(50021, '', '子码更改信息失败,请重新尝试发货');
- // }
- // } else {
- // //中码或者大码进行发货
- // if ($users->level != 3) {//只有经销商能够进行扫码中码或者大码进行发货
- // return $this->error(50021, '', '当前等级只能使用小码进行发货');
- // }
- // $user_s = User::find($request->id);
- // //中码发货
- // if ($re->level != 1) {
- // $re->user_id = $request->id;
- // if (!$re->save()) {
- // throw new \Exception('1');
- // }
- // $num = $this->update_full($re->pid);
- // if ($num == 300) {
- // throw new \Exception(1);
- // }
- // $data = [
- // 'op_name' => $users->nickname,
- // 'op_phone' => $users->mobile,
- // 'op_id' => $users->id,
- // 'accept_name' => $user_s->nickname,
- // 'accept_phone' => $user_s->mobile,
- // 'accept_id' => $user_s->id,
- // 'type' => 0,
- // 'select_id' => $re->id
- // ];
- // event(new SendGoodLogEvent($data));
- // $level_data = Select::where('pid', $re->sort)->get();
- // foreach ($level_data as $k => $v) {
- // $res = Select::where('code', $v->code)->first();
- // $res->user_id = $request->id;
- // if (!$res->save()) {
- // throw new \Exception(1);
- // }
- // $data = [
- // 'op_name' => $users->nickname,
- // 'op_phone' => $users->mobile,
- // 'op_id' => $users->id,
- // 'accept_name' => $user_s->nickname,
- // 'accept_phone' => $user_s->mobile,
- // 'accept_id' => $user_s->id,
- // 'type' => 0,
- // 'select_id' => $v->id
- // ];
- // event(new SendGoodLogEvent($data));
- // }
- // } else {
- //
- // /***大码发货,并记录日志情况*/
- // $re->user_id = $request->id;
- // if (!$re->save()) {
- // throw new \Exception('1');
- // }
- // $data = [
- // 'op_name' => $users->nickname,
- // 'op_phone' => $users->mobile,
- // 'op_id' => $users->id,
- // 'accept_name' => $user_s->nickname,
- // 'accept_phone' => $user_s->mobile,
- // 'accept_id' => $user_s->id,
- // 'type' => 0,
- // 'select_id' => $re->id
- // ];
- // event(new SendGoodLogEvent($data));
- //
- // /**查询**/
- // $level_data = Select::where('pid', $re->sort)->get();
- // foreach ($level_data as $k => $v) {
- // /***修改所有所属者*/
- // $res = Select::where('code', $v->code)->first();
- // $res->user_id = $request->id;
- // if (!$res->save()) {
- // throw new \Exception(1);
- // }
- // $data = [
- // 'op_name' => $users->nickname,
- // 'op_phone' => $users->mobile,
- // 'op_id' => $users->id,
- // 'accept_name' => $user_s->nickname,
- // 'accept_phone' => $user_s->mobile,
- // 'accept_id' => $user_s->id,
- // 'type' => 0,
- // 'select_id' => $v->id
- // ];
- // event(new SendGoodLogEvent($data));
- //
- //
- // /**如果现在的等级不是最低级,则执行下面的内容**/
- // if ($v->is_lowest != 1) {
- // $ress = Select::where('pid', $v->sort)->get();
- // foreach ($ress as $ke => $va) {
- // $resss = Select::where('code', $va->code)->first();
- // $resss->user_id = $request->id;
- // if (!$resss->save()) {
- // throw new \Exception(1);
- // }
- // $data = [
- // 'op_name' => $users->nickname,
- // 'op_phone' => $users->mobile,
- // 'op_id' => $users->id,
- // 'accept_name' => $user_s->nickname,
- // 'accept_phone' => $user_s->mobile,
- // 'accept_id' => $user_s->id,
- // 'type' => 0,
- // 'select_id' => $va->id
- // ];
- // event(new SendGoodLogEvent($data));
- // }
- // }
- //
- //
- // }
- // }
- // }
- // DB::commit();
- // return $this->success([]);
- $codes=$re->good_select_id;
- if ($codes==6){
- $codes=11;
- }elseif ($codes==9){
- $codes=6;
- }
- if ($codes>6){
- return $this->error(50021,'','该商品属于赠品,暂时不能发货');
- }
- Log::info(json_encode(['code'=>$code,'good'=>$re->good_select_id==9?6:$re->good_select_id,'code_level'=>$code_level,'code_num'=>$code_num]));
- return $this->success(['code'=>$code,'good'=>$codes,'code_level'=>$code_level,'code_num'=>$code_num]);
- } catch (\Exception $exception) {
- DB::rollBack();
- return $this->error(50024, '', '发货失败,请重新尝试' . $exception);
- }
- }
- //扫码发货减库存日志
- public function getScanLog(Request $request){
- $page_size=$request->input('page_size');
- $page_index=$request->input('page_index');
- $start_time=$request->input('start_time');
- $end_time=$request->input('end_time');
- $search_name=$request->input('search_name');
- $num=$page_size*($page_index-1);
- $user_id=Auth::user()->id;
- $data=ScanStorageLog::with(['user:id,nickname,mobile','accept:id,nickname,mobile'])
- ->where('user_id',$user_id)
- ->select('user_id','accept_id','rand_code','created_at');
- if($start_time && $end_time){
- $data->whereBetween('created_at',[$start_time,$end_time]);
- }
- if($search_name){
- $user_ids=User::where(function($query)use($search_name){
- $query->where('nickname','like','%'.$search_name.'%')
- ->orWhere('mobile','like','%'.$search_name.'%');
- })->pluck('id');
- $data->whereIn('accept_id',$user_ids);
- }
- $total=$data->sum('num');
- $count=$data->groupBy('rand_code')->get()->count();
- $list=$data->groupBy('rand_code')->orderByDesc('created_at')->skip($num)->take($page_size)->get();
- foreach($list as $key=>$val){
- $types=ScanStorageLog::where('rand_code',$val->rand_code)->where('created_at',$val->created_at)
- ->select('type')->groupBy('type')->pluck('type');
- $goods=[];
- foreach($types as $k=>$v){
- switch($v){
- case 0:
- $goods[$k]['name']='精装版';
- break;
- case 1:
- $goods[$k]['name']='简约版';
- break;
- case 2:
- $goods[$k]['name']='高腰版';
- break;
- case 3:
- $goods[$k]['name']='纯棉版';
- break;
- case 4:
- $goods[$k]['name']='青春版';
- break;
- case 5:
- $goods[$k]['name']='精装赠';
- break;
- default:
- $goods[$k]['name']='规格错误';
- }
- $goods[$k]['skus']=ScanStorageLog::where('rand_code',$val->rand_code)->where('created_at',$val->created_at)->where('type',$v)
- ->select('sex','size','num')
- ->orderBy('sex')->get();
- }
- $list[$key]['goods']=$goods;
- }
- $info['num']=$total;
- $info['info']=$list;
- return $this->success_list($info,'',$count);
- }
- function get_rand( $length = 6 ) {
- // 密码字符集,可任意添加你需要的字符
- $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_';
- $str ='';
- for ( $i = 0; $i < $length; $i++ )
- {
- // 这里提供两种字符获取方式
- // 第一种是使用 substr 截取$chars中的任意一位字符;
- // 第二种是取字符数组 $chars 的任意元素
- // $str .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
- $str .= $chars[ mt_rand(0, strlen($chars) - 1) ];
- }
- return $str;
- }
- function type($attr_main){
- switch($attr_main){
- case '精装版':
- case '精装赠':
- return 0;
- break;
- case '简约版':
- return 1;
- break;
- case '老人版':
- case '高腰版':
- return 2;
- break;
- case '纯棉版':
- return 3;
- break;
- case '青春版':
- return 4;
- break;
- default:
- return 9;
- }
- }
- /**查询代理**/
- public function get_lower_user(Request $request)
- {
- return $this->success_list([], '', 0);
- }
- /**代理退货**/
- public function user_return_good(Request $request)
- {
- $user = Auth::user();
- $code = urldecode($request->code);
- $re = Select::where('code', $code)->first();
- $op_user = User::find($re->user_id);
- if ($re) {
- if ($re->is_true == 1) {
- return $this->error(40028, '', '当前货物为假货,不能进行退货处理');
- }
- $res = $this->check_user_level($user->id, $re->user_id);
- if ($res['code'] == 200) {
- if ($re->is_lowest == 1) {
- $re->user_id = $user->id;
- $re->save();
- $this->update_full($re->pid);
- $data = [
- 'op_name' => $op_user->nickname,
- 'op_phone' => $op_user->mobile,
- 'op_id' => $op_user->id,
- 'accept_name' => $user->nickname,
- 'accept_phone' => $user->mobile,
- 'accept_id' => $user->id,
- 'select_id' => $re->id
- ];
- event(new ReturnGoodLog($data));
- } else {
- if ($re->level == 1) {
- $num = $this->is_full($re->code);
- if ($num == 300) {
- return $this->error(50024, '', '当前防伪码不能进行退货处理,请使用小码进行操作');
- }
- $re->user_id = $user->id;
- $re->save();
- $data = [
- 'op_name' => $op_user->nickname,
- 'op_phone' => $op_user->mobile,
- 'op_id' => $op_user->id,
- 'accept_name' => $user->nickname,
- 'accept_phone' => $user->mobile,
- 'accept_id' => $user->id,
- 'select_id' => $re->id
- ];
- event(new ReturnGoodLog($data));
- $lower_code = Select::where('pid', $re->sort)->get();
- foreach ($lower_code as $k => $v) {
- $ress = Select::where('code', $v->code)->first();
- $ress->user_id = $user->id;
- $ress->save();
- $data = [
- 'op_name' => $op_user->nickname,
- 'op_phone' => $op_user->mobile,
- 'op_id' => $op_user->id,
- 'accept_name' => $user->nickname,
- 'accept_phone' => $user->mobile,
- 'accept_id' => $user->id,
- 'select_id' => $v->id
- ];
- event(new ReturnGoodLog($data));
- if ($ress->is_lowest != 1) {
- $lower_codes = Select::where('pid', $v->sort)->get();
- foreach ($lower_codes as $ke => $va) {
- $resss = Select::where('code', $va->code)->first();
- $resss->user_id = $user->id;
- $resss->save();
- $data = [
- 'op_name' => $op_user->nickname,
- 'op_phone' => $op_user->mobile,
- 'op_id' => $op_user->id,
- 'accept_name' => $user->nickname,
- 'accept_phone' => $user->mobile,
- 'accept_id' => $user->id,
- 'select_id' => $va->id
- ];
- event(new ReturnGoodLog($data));
- }
- }
- }
- } else {
- $num = $this->is_full($re->code);
- if ($num == 300) {
- return $this->error(50024, '', '当前防伪码不能进行退货处理,请使用小码进行操作');
- }
- $re->user_id = $user->id;
- $re->save();
- $data = [
- 'op_name' => $op_user->nickname,
- 'op_phone' => $op_user->mobile,
- 'op_id' => $op_user->id,
- 'accept_name' => $user->nickname,
- 'accept_phone' => $user->mobile,
- 'accept_id' => $user->id,
- 'select_id' => $re->id
- ];
- event(new ReturnGoodLog($data));
- $this->update_full($re->pid);
- $lower_code = Select::where('pid', $re->sort)->get();
- foreach ($lower_code as $k => $v) {
- $ress = Select::where('code', $v->code)->first();
- $ress->user_id = $user->id;
- $ress->save();
- $data = [
- 'op_name' => $op_user->nickname,
- 'op_phone' => $op_user->mobile,
- 'op_id' => $op_user->id,
- 'accept_name' => $user->nickname,
- 'accept_phone' => $user->mobile,
- 'accept_id' => $user->id,
- 'select_id' => $v->id
- ];
- event(new ReturnGoodLog($data));
- }
- }
- }
- return $this->success([]);
- } else {
- return $this->error(40028, '', $res['message']);
- }
- } else {
- return $this->error(40029, '', '暂未查询到相应的信息');
- }
- }
- /**小码发货时更改中码和大码**/
- public function update_full($pid)
- {
- DB::beginTransaction();
- try {
- $re = Select::where('sort', '=', $pid)->first();
- $re->is_full = 1;
- if (!$re->save()) {
- throw new \Exception('1');
- }
- if ($re->level != 1) {
- $this->update_full($re->pid);
- }
- DB::commit();
- return 200;
- } catch (\Exception $exception) {
- DB::rollBack();
- Log::error($exception);
- return 300;
- }
- }
- /****/
- public function check_user_level($id, $user_id)
- {
- if ($id == $user_id) {
- return ['code' => 50026, 'message' => '你不能对自己的货物进行退货操作'];
- }
- return ['code' => 200];
- }
- /**检查是够已经发货**/
- public function is_full($code)
- {
- $re = Select::where('code', $code)->first();
- if ($re->is_full == 1) {
- return 300;
- }
- return 200;
- }
- /**展示发货商品所属信息**/
- public function select_send_user(Request $request){
- $user=Auth::user();
- $input=$request->all();
- $re=Select::where('code',$input['code'])->with(['select_user','select_good'])->first();
- if (empty($re)){
- Log::error(1);
- return $this->error(52119,'','暂无信息查询');
- }
- if (!$re->select_user){
- Log::error(2);
- return $this->error(52120,'','暂无代理信息');
- }
- if (!$re->select_good){
- Log::error(3);
- return $this->error(52121,'','暂无商品信息');
- }
- if ($re->user_id!=$user->id){
- Log::error(4);
- return $this->error(50021,'','当前货物不是您的货物,不能使用此码进行发货');
- }
- $time=SendGoodLog::where('select_id',$re->id)->where('accept_id',$re->user_id)->orderByDesc('created_at')->first();
- if ($time){
- $times=$time->created_at->toDateString();
- }else{
- $times=Carbon::now()->toDateString();
- }
- $data=[
- 'style'=>$re->select_good->good_name,
- 'nickname'=>$re->select_user->nickname,
- 'avatar'=>$re->select_user->headimgurl,
- 'level'=>$this->judge_level($re->select_user->level),
- 'phone'=>$re->select_user->mobile,
- 'time'=>$times
- ];
- return $this->success($data);
- }
- /**展示退货商品所属信息**/
- public function select_return_user(Request $request){
- $re=Select::where('code',$request->code)->with(['select_user','select_good'])->first();
- if (empty($re)){
- return $this->error(52121,'','暂无信息查询');
- }
- if (!$re->select_user){
- return $this->error(52121,'','暂无代理信息');
- }
- if (!$re->select_good){
- return $this->error(52121,'','暂无商品信息');
- }
- $res=$this->check_user_level(Auth::user()->id,$re->user_id);
- if ($res['code']!=200){
- return $this->error($res['code'],'',$res['message']);
- }
- $time=SendGoodLog::where('select_id',$re->id)->where('accept_id',$re->user_id)->orderByDesc('created_at')->first();
- $data=[
- 'style'=>$re->select_good->good_name,
- 'nickname'=>$re->select_user->nickname,
- 'level'=>$this->judge_level($re->select_user->level),
- 'phone'=>$re->select_user->mobile,
- 'time'=>$time->created_at->toDateString()
- ];
- return $this->success($data);
- }
- /**判断等级**/
- public function judge_level($status){
- switch ($status){
- case 2:
- $level='经销商';
- break;
- case 3:
- $level='批发商';
- break;
- default:
- $level='经销商';
- break;
- }
- return $level;
- }
- public function sell(Request $request){
- try{
- $id=Auth::user()->id;
- $code=urldecode($request->code);
- DB::beginTransaction();
- $re=Select::where('code',$code)->first();
- if ($re){
- if ($re->is_true==1){
- return $this->error(50024,'','当前货物是假货不能进行零售!');
- }
- if ($re->user_id!=$id){
- return $this->error(50024,'','当前货物绑定的代理信息不是你!');
- }
- if ($re->is_lowest!=1){
- return $this->error(50024,'','零售只能使用小码进行零售!');
- }
- if ($re->is_sell!=0){
- return $this->error(50024,'','该商品已经零售!');
- }
- $re->is_sell=1;
- if (!$re->save()){
- throw new \Exception('1');
- }
- $res=Select::where('sort',$re->pid)->first();
- $res->is_full=1;
- if (!$res->save()){
- throw new \Exception('1');
- }
- if ($res->level!=1){
- $ress=Select::where('sort',$re->pid)->first();
- $ress->is_full=1;
- if (!$ress->save()){
- throw new \Exception('1');
- }
- }
- $sell=new Sell();
- $sell->select_id=$re->id;
- $sell->select_good_id=$re->good_select_id;
- $sell->user_id=$re->user_id;
- if (!$sell->save()){
- throw new \Exception('1');
- }
- }
- DB::commit();
- return $this->success([]);
- }catch (\Exception $exception){
- DB::rollBack();
- Log::error('零售失败,原因是'.$exception);
- return $this->error(50021,'','操作失败,请重试!');
- }
- }
- public function get_arr(Request $request){
- $app=app('wechat.official_account');
- $url='http://api.app.cliu.cc/api/gzh';
- $app->jssdk->setUrl($url);
- $arr=$app->jssdk->buildConfig(array('scanQRCode'), false);
- return $this->success(json_decode($arr));
- }
- }
|