123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011 |
- <?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);
- if ($users->level == 1) {
- return $this->error(50021, '', '您是销售主管,不能进行发货操作');
- }
- $code=trim($code);
- $re = Select::where('code', $code)->first();
- if (!$re){
- return $this->error(500214,'','暂未找到相应的信息');
- }
- // if (Auth::user()->level==2){
- // $id=User::where('id',$user->agent_id)->value('id');
- // if ($re->user_id != $user->id && $re->user_id !=$id){
- // return $this->error(50021, '', '当前货物不是您的货物,不能使用此码进行发货');
- // }
- // }else{
- // if ($re->user_id != $user->id) {
- // return $this->error(50021, '', '当前货物不是您的货物,不能使用此码进行发货');
- // }
- // }
- 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 AllSendGood(Request $request){
- Log::error('z');
- $input = $request->all();
- $u_id=Auth::user()->id;
- $users=User::find($u_id);
- // $name=$this->JudgePhone($users->mobile);
- $user = User::where('id', $request->id)->first();
- // Log::error($input['code']);
- // $code = json_decode($input['code']);
- $code=$input['code'];
- if ($u_id==16297){
- Log::info('隔这呢搁着呢');
- Log::info($code);
- }
- Log::error(1111);
- if ($users->level==3){
- $crown_idA=$users->id;
- }elseif($users->level==2){
- $crown_idA=$users->agent_id;
- }else{
- $b_user=User::find($users->agent_id);
- $crown_idA=$b_user->agent_id;
- }
- foreach ($code as $k=>$v){
- foreach ($v as $ke=>$va){
- $code[$k][$ke]= urldecode($va);
- }
- }
- if (count($code)>20){
- return $this->error(500214,'','最多发送20个');
- }
- DB::beginTransaction();
- try {
- foreach ($code as $ks=>$va){
- Log::error($va);
- $good=GoodSelect::where('id',$ks)->select('good_name')->first();
- foreach ($va as $k=>$v){
- $re = Select::where('code', trim($v))->first();
- if ($re){
- $before_user=User::find($re->user_id);
- if ($before_user){
- $before_id=$before_user->id;
- try {
- $scan_type=0;
- if ($before_user->agent_id){
- if ($before_user->level==3){
- $crown_id=$before_user->id;
- }elseif($before_user->level==2){
- $crown_id=$before_user->agent_id;
- }else{
- $b_user=User::find($before_user->agent_id);
- $crown_id=$b_user->agent_id;
- }
- if ($crown_id==$crown_idA && $before_user->id==$users->id){
- $scan_type=0;
- }elseif ($crown_id==$crown_idA && $before_user->id!=$users->id){
- $scan_type=1;
- }else{
- $scan_type=2;
- }
- }
- }catch (\Exception $exception){
- Log::error('在这呢:'.$exception->getMessage());
- $scan_type=2;
- }
- }else{
- $scan_type=1;
- $before_id='';
- }
- $scan_info=json_encode([]);
- // 判断当前二维码的是不是最低级
- 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,
- 'scan_type' =>$scan_type,
- 'before_id' => $before_id,
- 'select_id' => $re->id,
- 'scan_info' => $scan_info,
- ];
- event(new SendGoodLogEvent($data));
- //修改大码和中码状态
- $num = $this->update_full($re->pid);
- if ($num == 300) {
- return $this->error(50021, '', '子码更改信息失败,请重新尝试发货');
- }
- }
- else {
- //中码或者大码进行发货
- if ($re->pid == 0 && is_numeric($re->pid)) {//不能使用大码进行发货
- return $this->error(50021, '', '请使用中码或者小码发货');
- }
- 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,
- 'scan_type' =>$scan_type,
- 'before_id' => $before_id,
- 'scan_info' => $scan_info
- ];
- event(new SendGoodLogEvent($data));
- $level_data = Select::where('pid', $re->sort)->get();
- foreach ($level_data as $k => $v) {
- $res = Select::where('code', trim($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,
- 'scan_type' =>$scan_type,
- 'before_id' => $before_id,
- 'scan_info' => $scan_info
- ];
- 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,
- 'scan_type' =>$scan_type,
- 'before_id' => $before_id,
- 'scan_info' => $scan_info
- ];
- event(new SendGoodLogEvent($data));
- /**查询下级**/
- $level_data = Select::where('pid', $re->sort)->get();
- foreach ($level_data as $kas => $vas) {
- /***修改所有下级的所属者*/
- $res = Select::where('code', trim($vas->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' => $vas->id,
- 'scan_type' =>$scan_type,
- 'before_id' => $before_id,
- 'scan_info' => $scan_info
- ];
- event(new SendGoodLogEvent($data));
- /**如果现在的等级不是最低级,则执行下面的内容**/
- if ($v->is_lowest != 1) {
- $ress = Select::where('pid', $v->sort)->get();
- foreach ($ress as $kes => $vaes) {
- $resss = Select::where('code', trim($vaes->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' => $vaes->id,
- 'scan_type' =>$scan_type,
- 'before_id' => $before_id,
- 'scan_info' => $scan_info
- ];
- event(new SendGoodLogEvent($data));
- }
- }
- }
- }
- }
- }else {
- Log::error($k+1);
- if ($ks==1){
- $m='精装版';
- }elseif ($ks==2){
- $m='简约版';
- }elseif ($ks==3){
- $m='高腰版';
- }elseif ($ks==4){
- $m='纯棉版';
- }elseif($ks==5){
- $m='精装(赠)';
- }elseif($ks==6){
- $m='青春版';
- }
- return $this->error(40023, '', '没有查到二维码对应的信息,请删除'.$m.'中第'.($k+1).'个数据重新提交。并将对应的货物单独取出');
- }
- }
- }
- // Log::info('开始计算扫码库存');
- // if($users->level==3){
- // $accept=User::find($request->id);
- // if($users->id != $accept->id && $accept->level!=3){
- // $rand_code=$this->get_rand(6);
- // $current=date('Y-m-d H:i:s');
- // foreach($request->goods_info as $key=>$val){
- // $type=$this->type($val['style']);
- // $user_storage=UserStorage::where('user_id',$users->id)->where('type',$type)
- // ->where('sex',$val['sex'])->where('size',$val['size'])->first();
- // if($user_storage){
- // $user_storage->num = $user_storage->num - $val['code_num'];
- // $user_storage->save();
- // }else{
- // UserStorage::create([
- // 'user_id'=>$users->id,
- // 'type'=>$type,
- // 'sex'=>$val['sex'],
- // 'size'=>$val['size'],
- // 'num'=>0-$val['code_num'],
- // ]);
- // }
- // if($val['style']=='精装赠'){
- // $type=5;
- // }
- // ScanStorageLog::create([
- // 'user_id'=>$users->id,
- // 'accept_id'=>$accept->id,
- // 'type'=>$type,
- // 'sex'=>$val['sex'],
- // 'size'=>$val['size'],
- // 'num'=>$val['code_num'],
- // 'created_at'=>$current,
- // 'rand_code'=>$rand_code,
- // ]);
- // }
- // }
- // }
- DB::commit();
- return $this->success([],'扫码发货成功','扫码发货成功');
- }catch (\Exception $exception){
- Log::error($exception->getMessage());
- return $this->error('2222','',$exception->getMessage());
- }
- }
- //扫码发货减库存日志
- 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)
- {
- $id = Auth::user()->id;
- $ids = [$id];
- $data = User::where('agent_id', Auth::user()->id)->whereNull('deleted_at')->pluck('id')->toArray();
- $user_id = array_merge($ids, $data);
- $search_name=$request->input('search_name')??null;
- if ($search_name) {
- $where = function ($q) use ($search_name) {
- $q->where('nickname', 'like', '%' . $search_name . '%')->orwhere('mobile', 'like', '%' . $search_name . '%');
- };
- $user = User::where(function ($q)use($id,$user_id){
- $q->whereIn('agent_id', $user_id)
- ->orwhere('id','like','%'.$id.'%');
- })->where($where)->whereNull('deleted_at')->select('mobile as phone', 'nickname', 'realname', 'id', 'headimgurl as avatar','level');
- $count = $user->get()->count();
- $re = $user->take(20)->skip(($request->page - 1) * 20)->get();
- foreach ($re as $k=>$v){
- $re[$k]->phone=hide_phone($v->phone);
- }
- return $this->success_list($re, '', $count);
- } else {
- $re = User::where(function ($q)use($id,$user_id){
- $q->whereIn('agent_id', $user_id)->orwhere('id',$id);
- })->whereNull('deleted_at')->select('mobile as phone', 'nickname', 'realname', 'id', 'headimgurl as avatar','level');
- $count = $re->get()->count();
- $re = $re->take(20)->skip(($request->page - 1) * 20)->get();
- foreach ($re as $k=>$v){
- $re[$k]->phone=hide_phone($v->phone);
- }
- return $this->success_list($re, '', $count);
- }
- }
- /**代理退货**/
- 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' => '你不能对自己的货物进行退货操作'];
- }
- $user = User::find($user_id);
- if ($user->level == 3) {
- return ['code' => 50026, 'message' => '你不能对经销商进行退货处理'];
- }
- if ($user->agent_id != $id) {
- $agent_s = User::find($user->agent_id);
- if ($agent_s->agnet_id != $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 1:
- $level='销售员';
- break;
- 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));
- }
- }
|