123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557 |
- <?php
- namespace App\Http\Controllers;
- use App\Events\ReturnGoodLog;
- use App\Events\SendGoodLogEvent;
- use App\Models\Select;
- use App\Models\Sell;
- use App\Models\SendGoodLog;
- use App\Models\User;
- 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, '', '您是销售主管,不能进行发货操作');
- }
- $re = Select::where('code', $code)->first();
- 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, '', '不能对自己进行发货操作');
- }
- //判断当前二维码的是不是最低级
- 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([]);
- } catch (\Exception $exception) {
- DB::rollBack();
- return $this->error(50024, '', '发货失败,请重新尝试' . $exception);
- }
- }
- /**查询下级代理**/
- public function get_lower_user(Request $request)
- {
- $id = Auth::user()->id;
- $ids = [$id];
- $data = User::where('agent_id', Auth::user()->id)->pluck('id')->toArray();
- $user_id = array_merge($ids, $data);
- if ($request->has('search_name')) {
- $search_name = $request->search_name;
- $where = function ($q) use ($search_name) {
- $q->where('nickname', 'like', '%' . $search_name . '%')->orwhere('mobile', 'like', '%' . $search_name . '%');
- };
- $user = User::whereIn('agent_id', $user_id)->where($where)->select('mobile as phone', 'nickname', 'realname', 'id', 'headimgurl as avatar');
- $count = $user->get()->count();
- $re = $user->take(20)->skip(($request->page - 1) * 20)->get();
- return $this->success_list($re, '', $count);
- } else {
- $re = User::whereIn('agent_id', $user_id)->select('mobile as phone', 'nickname', 'realname', 'id', 'headimgurl as avatar');
- $count = $re->get()->count();
- $re = $re->take(20)->skip(($request->page - 1) * 20)->get();
- 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.jiuweiyun.cn/api/gzh';
- $app->jssdk->setUrl($url);
- $arr=$app->jssdk->buildConfig(array('scanQRCode'), false);
- return $this->success(json_decode($arr));
- }
- }
|