UserGoodSendController.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Events\ReturnGoodLog;
  4. use App\Events\SendGoodLogEvent;
  5. use App\Models\GoodSelect;
  6. use App\Models\ScanStorageLog;
  7. use App\Models\Select;
  8. use App\Models\Sell;
  9. use App\Models\SendGoodLog;
  10. use App\Models\User;
  11. use App\Models\UserStorage;
  12. use Carbon\Carbon;
  13. use Illuminate\Support\Facades\Auth;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Validator;
  18. class UserGoodSendController extends Controller{
  19. /**代理发货给下级代理**/
  20. public function user_send_good(Request $request)
  21. {
  22. $code = urldecode($request->code);
  23. // $rule = [
  24. // 'id' => 'required'
  25. // ];
  26. // $message = [
  27. // 'id.required' => '缺少必填项(id)'
  28. // ];
  29. // $va = Validator::make($request->all(), $rule, $message);
  30. // if ($va->fails()) {
  31. // return $this->error(50023, '', $va->errors()->first());
  32. // }
  33. DB::beginTransaction();
  34. try {
  35. $user = Auth::user();
  36. $users = User::find($user->id);
  37. if ($users->level == 1) {
  38. return $this->error(50021, '', '您是销售主管,不能进行发货操作');
  39. }
  40. $code=trim($code);
  41. $re = Select::where('code', $code)->first();
  42. if (!$re){
  43. return $this->error(500214,'','暂未找到相应的信息');
  44. }
  45. // if (Auth::user()->level==2){
  46. // $id=User::where('id',$user->agent_id)->value('id');
  47. // if ($re->user_id != $user->id && $re->user_id !=$id){
  48. // return $this->error(50021, '', '当前货物不是您的货物,不能使用此码进行发货');
  49. // }
  50. // }else{
  51. // if ($re->user_id != $user->id) {
  52. // return $this->error(50021, '', '当前货物不是您的货物,不能使用此码进行发货');
  53. // }
  54. // }
  55. if ($re->is_true == 1) {
  56. return $this->error(40028, '', '当前货物为假货,不能进行退货处理');
  57. }
  58. if ($re->com_binding == 1) {
  59. return $this->error(40028, '', '当前货物为没有与公司绑定,不能进行发货处理');
  60. }
  61. //先判断当前二维码能否进行发货
  62. if ($re->is_full == 1) {
  63. return $this->error(50021, '', '当前二维码中的部分货物已经发货,不能使用此码进行发货');
  64. }
  65. // if ($request->id == $user->id) {
  66. // return $this->error(50021, '', '不能对自己进行发货操作');
  67. // }
  68. $code_level=0;
  69. $code_num=0;
  70. if ($re->is_lowest != 1) {
  71. //中码或者大码进行发货
  72. if ($re->pid == 0 && is_numeric($re->pid)) {//不能使用大码进行发货
  73. return $this->error(50021, '', '请使用中码或者小码发货'.$re->pid);
  74. }
  75. if ($user->level!=3){
  76. return $this->error(50021, '', '请使用小码进行发货');
  77. }
  78. $code_level=1;
  79. $code_num=$re->child_num;
  80. }
  81. //判断当前二维码的是不是最低级
  82. // if ($re->is_lowest == 1) {
  83. // $re->track_status = 2;
  84. // $re->user_id = $request->id;
  85. // if (!$re->save()) {
  86. // throw new \Exception('1');
  87. // }
  88. // $user_s = User::find($request->id);
  89. // $data = [
  90. // 'op_name' => $users->nickname,
  91. // 'op_phone' => $users->mobile,
  92. // 'op_id' => $users->id,
  93. // 'accept_name' => $user_s->nickname,
  94. // 'accept_phone' => $user_s->mobile,
  95. // 'accept_id' => $user_s->id,
  96. // 'type' => 0,
  97. // 'select_id' => $re->id
  98. // ];
  99. // event(new SendGoodLogEvent($data));
  100. // //修改大码和中码状态
  101. // $num = $this->update_full($re->pid);
  102. // if ($num == 300) {
  103. // return $this->error(50021, '', '子码更改信息失败,请重新尝试发货');
  104. // }
  105. // } else {
  106. // //中码或者大码进行发货
  107. // if ($users->level != 3) {//只有经销商能够进行扫码中码或者大码进行发货
  108. // return $this->error(50021, '', '当前等级只能使用小码进行发货');
  109. // }
  110. // $user_s = User::find($request->id);
  111. // //中码发货
  112. // if ($re->level != 1) {
  113. // $re->user_id = $request->id;
  114. // if (!$re->save()) {
  115. // throw new \Exception('1');
  116. // }
  117. // $num = $this->update_full($re->pid);
  118. // if ($num == 300) {
  119. // throw new \Exception(1);
  120. // }
  121. // $data = [
  122. // 'op_name' => $users->nickname,
  123. // 'op_phone' => $users->mobile,
  124. // 'op_id' => $users->id,
  125. // 'accept_name' => $user_s->nickname,
  126. // 'accept_phone' => $user_s->mobile,
  127. // 'accept_id' => $user_s->id,
  128. // 'type' => 0,
  129. // 'select_id' => $re->id
  130. // ];
  131. // event(new SendGoodLogEvent($data));
  132. // $level_data = Select::where('pid', $re->sort)->get();
  133. // foreach ($level_data as $k => $v) {
  134. // $res = Select::where('code', $v->code)->first();
  135. // $res->user_id = $request->id;
  136. // if (!$res->save()) {
  137. // throw new \Exception(1);
  138. // }
  139. // $data = [
  140. // 'op_name' => $users->nickname,
  141. // 'op_phone' => $users->mobile,
  142. // 'op_id' => $users->id,
  143. // 'accept_name' => $user_s->nickname,
  144. // 'accept_phone' => $user_s->mobile,
  145. // 'accept_id' => $user_s->id,
  146. // 'type' => 0,
  147. // 'select_id' => $v->id
  148. // ];
  149. // event(new SendGoodLogEvent($data));
  150. // }
  151. // } else {
  152. //
  153. // /***大码发货,并记录日志情况*/
  154. // $re->user_id = $request->id;
  155. // if (!$re->save()) {
  156. // throw new \Exception('1');
  157. // }
  158. // $data = [
  159. // 'op_name' => $users->nickname,
  160. // 'op_phone' => $users->mobile,
  161. // 'op_id' => $users->id,
  162. // 'accept_name' => $user_s->nickname,
  163. // 'accept_phone' => $user_s->mobile,
  164. // 'accept_id' => $user_s->id,
  165. // 'type' => 0,
  166. // 'select_id' => $re->id
  167. // ];
  168. // event(new SendGoodLogEvent($data));
  169. //
  170. // /**查询下级**/
  171. // $level_data = Select::where('pid', $re->sort)->get();
  172. // foreach ($level_data as $k => $v) {
  173. // /***修改所有下级的所属者*/
  174. // $res = Select::where('code', $v->code)->first();
  175. // $res->user_id = $request->id;
  176. // if (!$res->save()) {
  177. // throw new \Exception(1);
  178. // }
  179. // $data = [
  180. // 'op_name' => $users->nickname,
  181. // 'op_phone' => $users->mobile,
  182. // 'op_id' => $users->id,
  183. // 'accept_name' => $user_s->nickname,
  184. // 'accept_phone' => $user_s->mobile,
  185. // 'accept_id' => $user_s->id,
  186. // 'type' => 0,
  187. // 'select_id' => $v->id
  188. // ];
  189. // event(new SendGoodLogEvent($data));
  190. //
  191. //
  192. // /**如果现在的等级不是最低级,则执行下面的内容**/
  193. // if ($v->is_lowest != 1) {
  194. // $ress = Select::where('pid', $v->sort)->get();
  195. // foreach ($ress as $ke => $va) {
  196. // $resss = Select::where('code', $va->code)->first();
  197. // $resss->user_id = $request->id;
  198. // if (!$resss->save()) {
  199. // throw new \Exception(1);
  200. // }
  201. // $data = [
  202. // 'op_name' => $users->nickname,
  203. // 'op_phone' => $users->mobile,
  204. // 'op_id' => $users->id,
  205. // 'accept_name' => $user_s->nickname,
  206. // 'accept_phone' => $user_s->mobile,
  207. // 'accept_id' => $user_s->id,
  208. // 'type' => 0,
  209. // 'select_id' => $va->id
  210. // ];
  211. // event(new SendGoodLogEvent($data));
  212. // }
  213. // }
  214. //
  215. //
  216. // }
  217. // }
  218. // }
  219. // DB::commit();
  220. // return $this->success([]);
  221. $codes=$re->good_select_id;
  222. if ($codes==6){
  223. $codes=11;
  224. }elseif ($codes==9){
  225. $codes=6;
  226. }
  227. if ($codes>6){
  228. return $this->error(50021,'','该商品属于赠品,暂时不能发货');
  229. }
  230. 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]));
  231. return $this->success(['code'=>$code,'good'=>$codes,'code_level'=>$code_level,'code_num'=>$code_num]);
  232. } catch (\Exception $exception) {
  233. DB::rollBack();
  234. return $this->error(50024, '', '发货失败,请重新尝试' . $exception);
  235. }
  236. }
  237. public function AllSendGood(Request $request){
  238. Log::error('z');
  239. $input = $request->all();
  240. $u_id=Auth::user()->id;
  241. $users=User::find($u_id);
  242. // $name=$this->JudgePhone($users->mobile);
  243. $user = User::where('id', $request->id)->first();
  244. // Log::error($input['code']);
  245. // $code = json_decode($input['code']);
  246. $code=$input['code'];
  247. if ($u_id==16297){
  248. Log::info('隔这呢搁着呢');
  249. Log::info($code);
  250. }
  251. Log::error(1111);
  252. if ($users->level==3){
  253. $crown_idA=$users->id;
  254. }elseif($users->level==2){
  255. $crown_idA=$users->agent_id;
  256. }else{
  257. $b_user=User::find($users->agent_id);
  258. $crown_idA=$b_user->agent_id;
  259. }
  260. foreach ($code as $k=>$v){
  261. foreach ($v as $ke=>$va){
  262. $code[$k][$ke]= urldecode($va);
  263. }
  264. }
  265. if (count($code)>20){
  266. return $this->error(500214,'','最多发送20个');
  267. }
  268. DB::beginTransaction();
  269. try {
  270. foreach ($code as $ks=>$va){
  271. Log::error($va);
  272. $good=GoodSelect::where('id',$ks)->select('good_name')->first();
  273. foreach ($va as $k=>$v){
  274. $re = Select::where('code', trim($v))->first();
  275. if ($re){
  276. $before_user=User::find($re->user_id);
  277. if ($before_user){
  278. $before_id=$before_user->id;
  279. try {
  280. $scan_type=0;
  281. if ($before_user->agent_id){
  282. if ($before_user->level==3){
  283. $crown_id=$before_user->id;
  284. }elseif($before_user->level==2){
  285. $crown_id=$before_user->agent_id;
  286. }else{
  287. $b_user=User::find($before_user->agent_id);
  288. $crown_id=$b_user->agent_id;
  289. }
  290. if ($crown_id==$crown_idA && $before_user->id==$users->id){
  291. $scan_type=0;
  292. }elseif ($crown_id==$crown_idA && $before_user->id!=$users->id){
  293. $scan_type=1;
  294. }else{
  295. $scan_type=2;
  296. }
  297. }
  298. }catch (\Exception $exception){
  299. Log::error('在这呢:'.$exception->getMessage());
  300. $scan_type=2;
  301. }
  302. }else{
  303. $scan_type=1;
  304. $before_id='';
  305. }
  306. $scan_info=json_encode([]);
  307. // 判断当前二维码的是不是最低级
  308. if ($re->is_lowest == 1) {
  309. $re->track_status = 2;
  310. $re->user_id = $request->id;
  311. if (!$re->save()) {
  312. throw new \Exception('1');
  313. }
  314. $user_s = User::find($request->id);
  315. $data = [
  316. 'op_name' => $users->nickname,
  317. 'op_phone' => $users->mobile,
  318. 'op_id' => $users->id,
  319. 'accept_name' => $user_s->nickname,
  320. 'accept_phone' => $user_s->mobile,
  321. 'accept_id' => $user_s->id,
  322. 'type' => 0,
  323. 'scan_type' =>$scan_type,
  324. 'before_id' => $before_id,
  325. 'select_id' => $re->id,
  326. 'scan_info' => $scan_info,
  327. ];
  328. event(new SendGoodLogEvent($data));
  329. //修改大码和中码状态
  330. $num = $this->update_full($re->pid);
  331. if ($num == 300) {
  332. return $this->error(50021, '', '子码更改信息失败,请重新尝试发货');
  333. }
  334. }
  335. else {
  336. //中码或者大码进行发货
  337. if ($re->pid == 0 && is_numeric($re->pid)) {//不能使用大码进行发货
  338. return $this->error(50021, '', '请使用中码或者小码发货');
  339. }
  340. if ($users->level!=3){
  341. return $this->error(50021, '', '请使用小码进行发货');
  342. }
  343. $user_s = User::find($request->id);
  344. //中码发货
  345. if ($re->level != 1) {
  346. $re->user_id = $request->id;
  347. if (!$re->save()) {
  348. throw new \Exception('1');
  349. }
  350. $num = $this->update_full($re->pid);
  351. if ($num == 300) {
  352. throw new \Exception(1);
  353. }
  354. $data = [
  355. 'op_name' => $users->nickname,
  356. 'op_phone' => $users->mobile,
  357. 'op_id' => $users->id,
  358. 'accept_name' => $user_s->nickname,
  359. 'accept_phone' => $user_s->mobile,
  360. 'accept_id' => $user_s->id,
  361. 'type' => 0,
  362. 'select_id' => $re->id,
  363. 'scan_type' =>$scan_type,
  364. 'before_id' => $before_id,
  365. 'scan_info' => $scan_info
  366. ];
  367. event(new SendGoodLogEvent($data));
  368. $level_data = Select::where('pid', $re->sort)->get();
  369. foreach ($level_data as $k => $v) {
  370. $res = Select::where('code', trim($v->code))->first();
  371. $res->user_id = $request->id;
  372. if (!$res->save()) {
  373. throw new \Exception(1);
  374. }
  375. $data = [
  376. 'op_name' => $users->nickname,
  377. 'op_phone' => $users->mobile,
  378. 'op_id' => $users->id,
  379. 'accept_name' => $user_s->nickname,
  380. 'accept_phone' => $user_s->mobile,
  381. 'accept_id' => $user_s->id,
  382. 'type' => 0,
  383. 'select_id' => $v->id,
  384. 'scan_type' =>$scan_type,
  385. 'before_id' => $before_id,
  386. 'scan_info' => $scan_info
  387. ];
  388. event(new SendGoodLogEvent($data));
  389. }
  390. } else {
  391. /***大码发货,并记录日志情况*/
  392. $re->user_id = $request->id;
  393. if (!$re->save()) {
  394. throw new \Exception('1');
  395. }
  396. $data = [
  397. 'op_name' => $users->nickname,
  398. 'op_phone' => $users->mobile,
  399. 'op_id' => $users->id,
  400. 'accept_name' => $user_s->nickname,
  401. 'accept_phone' => $user_s->mobile,
  402. 'accept_id' => $user_s->id,
  403. 'type' => 0,
  404. 'select_id' => $re->id,
  405. 'scan_type' =>$scan_type,
  406. 'before_id' => $before_id,
  407. 'scan_info' => $scan_info
  408. ];
  409. event(new SendGoodLogEvent($data));
  410. /**查询下级**/
  411. $level_data = Select::where('pid', $re->sort)->get();
  412. foreach ($level_data as $kas => $vas) {
  413. /***修改所有下级的所属者*/
  414. $res = Select::where('code', trim($vas->code))->first();
  415. $res->user_id = $request->id;
  416. if (!$res->save()) {
  417. throw new \Exception(1);
  418. }
  419. $data = [
  420. 'op_name' => $users->nickname,
  421. 'op_phone' => $users->mobile,
  422. 'op_id' => $users->id,
  423. 'accept_name' => $user_s->nickname,
  424. 'accept_phone' => $user_s->mobile,
  425. 'accept_id' => $user_s->id,
  426. 'type' => 0,
  427. 'select_id' => $vas->id,
  428. 'scan_type' =>$scan_type,
  429. 'before_id' => $before_id,
  430. 'scan_info' => $scan_info
  431. ];
  432. event(new SendGoodLogEvent($data));
  433. /**如果现在的等级不是最低级,则执行下面的内容**/
  434. if ($v->is_lowest != 1) {
  435. $ress = Select::where('pid', $v->sort)->get();
  436. foreach ($ress as $kes => $vaes) {
  437. $resss = Select::where('code', trim($vaes->code))->first();
  438. $resss->user_id = $request->id;
  439. if (!$resss->save()) {
  440. throw new \Exception(1);
  441. }
  442. $data = [
  443. 'op_name' => $users->nickname,
  444. 'op_phone' => $users->mobile,
  445. 'op_id' => $users->id,
  446. 'accept_name' => $user_s->nickname,
  447. 'accept_phone' => $user_s->mobile,
  448. 'accept_id' => $user_s->id,
  449. 'type' => 0,
  450. 'select_id' => $vaes->id,
  451. 'scan_type' =>$scan_type,
  452. 'before_id' => $before_id,
  453. 'scan_info' => $scan_info
  454. ];
  455. event(new SendGoodLogEvent($data));
  456. }
  457. }
  458. }
  459. }
  460. }
  461. }else {
  462. Log::error($k+1);
  463. if ($ks==1){
  464. $m='精装版';
  465. }elseif ($ks==2){
  466. $m='简约版';
  467. }elseif ($ks==3){
  468. $m='高腰版';
  469. }elseif ($ks==4){
  470. $m='纯棉版';
  471. }elseif($ks==5){
  472. $m='精装(赠)';
  473. }elseif($ks==6){
  474. $m='青春版';
  475. }
  476. return $this->error(40023, '', '没有查到二维码对应的信息,请删除'.$m.'中第'.($k+1).'个数据重新提交。并将对应的货物单独取出');
  477. }
  478. }
  479. }
  480. // Log::info('开始计算扫码库存');
  481. // if($users->level==3){
  482. // $accept=User::find($request->id);
  483. // if($users->id != $accept->id && $accept->level!=3){
  484. // $rand_code=$this->get_rand(6);
  485. // $current=date('Y-m-d H:i:s');
  486. // foreach($request->goods_info as $key=>$val){
  487. // $type=$this->type($val['style']);
  488. // $user_storage=UserStorage::where('user_id',$users->id)->where('type',$type)
  489. // ->where('sex',$val['sex'])->where('size',$val['size'])->first();
  490. // if($user_storage){
  491. // $user_storage->num = $user_storage->num - $val['code_num'];
  492. // $user_storage->save();
  493. // }else{
  494. // UserStorage::create([
  495. // 'user_id'=>$users->id,
  496. // 'type'=>$type,
  497. // 'sex'=>$val['sex'],
  498. // 'size'=>$val['size'],
  499. // 'num'=>0-$val['code_num'],
  500. // ]);
  501. // }
  502. // if($val['style']=='精装赠'){
  503. // $type=5;
  504. // }
  505. // ScanStorageLog::create([
  506. // 'user_id'=>$users->id,
  507. // 'accept_id'=>$accept->id,
  508. // 'type'=>$type,
  509. // 'sex'=>$val['sex'],
  510. // 'size'=>$val['size'],
  511. // 'num'=>$val['code_num'],
  512. // 'created_at'=>$current,
  513. // 'rand_code'=>$rand_code,
  514. // ]);
  515. // }
  516. // }
  517. // }
  518. DB::commit();
  519. return $this->success([],'扫码发货成功','扫码发货成功');
  520. }catch (\Exception $exception){
  521. Log::error($exception->getMessage());
  522. return $this->error('2222','',$exception->getMessage());
  523. }
  524. }
  525. //扫码发货减库存日志
  526. public function getScanLog(Request $request){
  527. $page_size=$request->input('page_size');
  528. $page_index=$request->input('page_index');
  529. $start_time=$request->input('start_time');
  530. $end_time=$request->input('end_time');
  531. $search_name=$request->input('search_name');
  532. $num=$page_size*($page_index-1);
  533. $user_id=Auth::user()->id;
  534. $data=ScanStorageLog::with(['user:id,nickname,mobile','accept:id,nickname,mobile'])
  535. ->where('user_id',$user_id)
  536. ->select('user_id','accept_id','rand_code','created_at');
  537. if($start_time && $end_time){
  538. $data->whereBetween('created_at',[$start_time,$end_time]);
  539. }
  540. if($search_name){
  541. $user_ids=User::where(function($query)use($search_name){
  542. $query->where('nickname','like','%'.$search_name.'%')
  543. ->orWhere('mobile','like','%'.$search_name.'%');
  544. })->pluck('id');
  545. $data->whereIn('accept_id',$user_ids);
  546. }
  547. $total=$data->sum('num');
  548. $count=$data->groupBy('rand_code')->get()->count();
  549. $list=$data->groupBy('rand_code')->orderByDesc('created_at')->skip($num)->take($page_size)->get();
  550. foreach($list as $key=>$val){
  551. $types=ScanStorageLog::where('rand_code',$val->rand_code)->where('created_at',$val->created_at)
  552. ->select('type')->groupBy('type')->pluck('type');
  553. $goods=[];
  554. foreach($types as $k=>$v){
  555. switch($v){
  556. case 0:
  557. $goods[$k]['name']='精装版';
  558. break;
  559. case 1:
  560. $goods[$k]['name']='简约版';
  561. break;
  562. case 2:
  563. $goods[$k]['name']='高腰版';
  564. break;
  565. case 3:
  566. $goods[$k]['name']='纯棉版';
  567. break;
  568. case 4:
  569. $goods[$k]['name']='青春版';
  570. break;
  571. case 5:
  572. $goods[$k]['name']='精装赠';
  573. break;
  574. default:
  575. $goods[$k]['name']='规格错误';
  576. }
  577. $goods[$k]['skus']=ScanStorageLog::where('rand_code',$val->rand_code)->where('created_at',$val->created_at)->where('type',$v)
  578. ->select('sex','size','num')
  579. ->orderBy('sex')->get();
  580. }
  581. $list[$key]['goods']=$goods;
  582. }
  583. $info['num']=$total;
  584. $info['info']=$list;
  585. return $this->success_list($info,'',$count);
  586. }
  587. function get_rand( $length = 6 ) {
  588. // 密码字符集,可任意添加你需要的字符
  589. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_';
  590. $str ='';
  591. for ( $i = 0; $i < $length; $i++ )
  592. {
  593. // 这里提供两种字符获取方式
  594. // 第一种是使用 substr 截取$chars中的任意一位字符;
  595. // 第二种是取字符数组 $chars 的任意元素
  596. // $str .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
  597. $str .= $chars[ mt_rand(0, strlen($chars) - 1) ];
  598. }
  599. return $str;
  600. }
  601. function type($attr_main){
  602. switch($attr_main){
  603. case '精装版':
  604. case '精装赠':
  605. return 0;
  606. break;
  607. case '简约版':
  608. return 1;
  609. break;
  610. case '老人版':
  611. case '高腰版':
  612. return 2;
  613. break;
  614. case '纯棉版':
  615. return 3;
  616. break;
  617. case '青春版':
  618. return 4;
  619. break;
  620. default:
  621. return 9;
  622. }
  623. }
  624. /**查询下级代理**/
  625. public function get_lower_user(Request $request)
  626. {
  627. $id = Auth::user()->id;
  628. $ids = [$id];
  629. $data = User::where('agent_id', Auth::user()->id)->whereNull('deleted_at')->pluck('id')->toArray();
  630. $user_id = array_merge($ids, $data);
  631. $search_name=$request->input('search_name')??null;
  632. if ($search_name) {
  633. $where = function ($q) use ($search_name) {
  634. $q->where('nickname', 'like', '%' . $search_name . '%')->orwhere('mobile', 'like', '%' . $search_name . '%');
  635. };
  636. $user = User::where(function ($q)use($id,$user_id){
  637. $q->whereIn('agent_id', $user_id)
  638. ->orwhere('id','like','%'.$id.'%');
  639. })->where($where)->whereNull('deleted_at')->select('mobile as phone', 'nickname', 'realname', 'id', 'headimgurl as avatar','level');
  640. $count = $user->get()->count();
  641. $re = $user->take(20)->skip(($request->page - 1) * 20)->get();
  642. foreach ($re as $k=>$v){
  643. $re[$k]->phone=hide_phone($v->phone);
  644. }
  645. return $this->success_list($re, '', $count);
  646. } else {
  647. $re = User::where(function ($q)use($id,$user_id){
  648. $q->whereIn('agent_id', $user_id)->orwhere('id',$id);
  649. })->whereNull('deleted_at')->select('mobile as phone', 'nickname', 'realname', 'id', 'headimgurl as avatar','level');
  650. $count = $re->get()->count();
  651. $re = $re->take(20)->skip(($request->page - 1) * 20)->get();
  652. foreach ($re as $k=>$v){
  653. $re[$k]->phone=hide_phone($v->phone);
  654. }
  655. return $this->success_list($re, '', $count);
  656. }
  657. }
  658. /**代理退货**/
  659. public function user_return_good(Request $request)
  660. {
  661. $user = Auth::user();
  662. $code = urldecode($request->code);
  663. $re = Select::where('code', $code)->first();
  664. $op_user = User::find($re->user_id);
  665. if ($re) {
  666. if ($re->is_true == 1) {
  667. return $this->error(40028, '', '当前货物为假货,不能进行退货处理');
  668. }
  669. $res = $this->check_user_level($user->id, $re->user_id);
  670. if ($res['code'] == 200) {
  671. if ($re->is_lowest == 1) {
  672. $re->user_id = $user->id;
  673. $re->save();
  674. $this->update_full($re->pid);
  675. $data = [
  676. 'op_name' => $op_user->nickname,
  677. 'op_phone' => $op_user->mobile,
  678. 'op_id' => $op_user->id,
  679. 'accept_name' => $user->nickname,
  680. 'accept_phone' => $user->mobile,
  681. 'accept_id' => $user->id,
  682. 'select_id' => $re->id
  683. ];
  684. event(new ReturnGoodLog($data));
  685. } else {
  686. if ($re->level == 1) {
  687. $num = $this->is_full($re->code);
  688. if ($num == 300) {
  689. return $this->error(50024, '', '当前防伪码不能进行退货处理,请使用小码进行操作');
  690. }
  691. $re->user_id = $user->id;
  692. $re->save();
  693. $data = [
  694. 'op_name' => $op_user->nickname,
  695. 'op_phone' => $op_user->mobile,
  696. 'op_id' => $op_user->id,
  697. 'accept_name' => $user->nickname,
  698. 'accept_phone' => $user->mobile,
  699. 'accept_id' => $user->id,
  700. 'select_id' => $re->id
  701. ];
  702. event(new ReturnGoodLog($data));
  703. $lower_code = Select::where('pid', $re->sort)->get();
  704. foreach ($lower_code as $k => $v) {
  705. $ress = Select::where('code', $v->code)->first();
  706. $ress->user_id = $user->id;
  707. $ress->save();
  708. $data = [
  709. 'op_name' => $op_user->nickname,
  710. 'op_phone' => $op_user->mobile,
  711. 'op_id' => $op_user->id,
  712. 'accept_name' => $user->nickname,
  713. 'accept_phone' => $user->mobile,
  714. 'accept_id' => $user->id,
  715. 'select_id' => $v->id
  716. ];
  717. event(new ReturnGoodLog($data));
  718. if ($ress->is_lowest != 1) {
  719. $lower_codes = Select::where('pid', $v->sort)->get();
  720. foreach ($lower_codes as $ke => $va) {
  721. $resss = Select::where('code', $va->code)->first();
  722. $resss->user_id = $user->id;
  723. $resss->save();
  724. $data = [
  725. 'op_name' => $op_user->nickname,
  726. 'op_phone' => $op_user->mobile,
  727. 'op_id' => $op_user->id,
  728. 'accept_name' => $user->nickname,
  729. 'accept_phone' => $user->mobile,
  730. 'accept_id' => $user->id,
  731. 'select_id' => $va->id
  732. ];
  733. event(new ReturnGoodLog($data));
  734. }
  735. }
  736. }
  737. } else {
  738. $num = $this->is_full($re->code);
  739. if ($num == 300) {
  740. return $this->error(50024, '', '当前防伪码不能进行退货处理,请使用小码进行操作');
  741. }
  742. $re->user_id = $user->id;
  743. $re->save();
  744. $data = [
  745. 'op_name' => $op_user->nickname,
  746. 'op_phone' => $op_user->mobile,
  747. 'op_id' => $op_user->id,
  748. 'accept_name' => $user->nickname,
  749. 'accept_phone' => $user->mobile,
  750. 'accept_id' => $user->id,
  751. 'select_id' => $re->id
  752. ];
  753. event(new ReturnGoodLog($data));
  754. $this->update_full($re->pid);
  755. $lower_code = Select::where('pid', $re->sort)->get();
  756. foreach ($lower_code as $k => $v) {
  757. $ress = Select::where('code', $v->code)->first();
  758. $ress->user_id = $user->id;
  759. $ress->save();
  760. $data = [
  761. 'op_name' => $op_user->nickname,
  762. 'op_phone' => $op_user->mobile,
  763. 'op_id' => $op_user->id,
  764. 'accept_name' => $user->nickname,
  765. 'accept_phone' => $user->mobile,
  766. 'accept_id' => $user->id,
  767. 'select_id' => $v->id
  768. ];
  769. event(new ReturnGoodLog($data));
  770. }
  771. }
  772. }
  773. return $this->success([]);
  774. } else {
  775. return $this->error(40028, '', $res['message']);
  776. }
  777. } else {
  778. return $this->error(40029, '', '暂未查询到相应的信息');
  779. }
  780. }
  781. /**小码发货时更改中码和大码**/
  782. public function update_full($pid)
  783. {
  784. DB::beginTransaction();
  785. try {
  786. $re = Select::where('sort', '=', $pid)->first();
  787. $re->is_full = 1;
  788. if (!$re->save()) {
  789. throw new \Exception('1');
  790. }
  791. if ($re->level != 1) {
  792. $this->update_full($re->pid);
  793. }
  794. DB::commit();
  795. return 200;
  796. } catch (\Exception $exception) {
  797. DB::rollBack();
  798. Log::error($exception);
  799. return 300;
  800. }
  801. }
  802. /**检查用户是不是自己的下级**/
  803. public function check_user_level($id, $user_id)
  804. {
  805. if ($id == $user_id) {
  806. return ['code' => 50026, 'message' => '你不能对自己的货物进行退货操作'];
  807. }
  808. $user = User::find($user_id);
  809. if ($user->level == 3) {
  810. return ['code' => 50026, 'message' => '你不能对经销商进行退货处理'];
  811. }
  812. if ($user->agent_id != $id) {
  813. $agent_s = User::find($user->agent_id);
  814. if ($agent_s->agnet_id != $id) {
  815. return ['code' => 50026, 'message' => '当前代理不是你的下级,不能进行退货操作'];
  816. }
  817. }
  818. return ['code' => 200];
  819. }
  820. /**检查是够已经发货**/
  821. public function is_full($code)
  822. {
  823. $re = Select::where('code', $code)->first();
  824. if ($re->is_full == 1) {
  825. return 300;
  826. }
  827. return 200;
  828. }
  829. /**展示发货商品所属信息**/
  830. public function select_send_user(Request $request){
  831. $user=Auth::user();
  832. $input=$request->all();
  833. $re=Select::where('code',$input['code'])->with(['select_user','select_good'])->first();
  834. if (empty($re)){
  835. Log::error(1);
  836. return $this->error(52119,'','暂无信息查询');
  837. }
  838. if (!$re->select_user){
  839. Log::error(2);
  840. return $this->error(52120,'','暂无代理信息');
  841. }
  842. if (!$re->select_good){
  843. Log::error(3);
  844. return $this->error(52121,'','暂无商品信息');
  845. }
  846. if ($re->user_id!=$user->id){
  847. Log::error(4);
  848. return $this->error(50021,'','当前货物不是您的货物,不能使用此码进行发货');
  849. }
  850. $time=SendGoodLog::where('select_id',$re->id)->where('accept_id',$re->user_id)->orderByDesc('created_at')->first();
  851. if ($time){
  852. $times=$time->created_at->toDateString();
  853. }else{
  854. $times=Carbon::now()->toDateString();
  855. }
  856. $data=[
  857. 'style'=>$re->select_good->good_name,
  858. 'nickname'=>$re->select_user->nickname,
  859. 'avatar'=>$re->select_user->headimgurl,
  860. 'level'=>$this->judge_level($re->select_user->level),
  861. 'phone'=>$re->select_user->mobile,
  862. 'time'=>$times
  863. ];
  864. return $this->success($data);
  865. }
  866. /**展示退货商品所属信息**/
  867. public function select_return_user(Request $request){
  868. $re=Select::where('code',$request->code)->with(['select_user','select_good'])->first();
  869. if (empty($re)){
  870. return $this->error(52121,'','暂无信息查询');
  871. }
  872. if (!$re->select_user){
  873. return $this->error(52121,'','暂无代理信息');
  874. }
  875. if (!$re->select_good){
  876. return $this->error(52121,'','暂无商品信息');
  877. }
  878. $res=$this->check_user_level(Auth::user()->id,$re->user_id);
  879. if ($res['code']!=200){
  880. return $this->error($res['code'],'',$res['message']);
  881. }
  882. $time=SendGoodLog::where('select_id',$re->id)->where('accept_id',$re->user_id)->orderByDesc('created_at')->first();
  883. $data=[
  884. 'style'=>$re->select_good->good_name,
  885. 'nickname'=>$re->select_user->nickname,
  886. 'level'=>$this->judge_level($re->select_user->level),
  887. 'phone'=>$re->select_user->mobile,
  888. 'time'=>$time->created_at->toDateString()
  889. ];
  890. return $this->success($data);
  891. }
  892. /**判断等级**/
  893. public function judge_level($status){
  894. switch ($status){
  895. case 1:
  896. $level='销售员';
  897. break;
  898. case 2:
  899. $level='经销商';
  900. break;
  901. case 3:
  902. $level='批发商';
  903. break;
  904. default:
  905. $level='销售员';
  906. break;
  907. }
  908. return $level;
  909. }
  910. public function sell(Request $request){
  911. try{
  912. $id=Auth::user()->id;
  913. $code=urldecode($request->code);
  914. DB::beginTransaction();
  915. $re=Select::where('code',$code)->first();
  916. if ($re){
  917. if ($re->is_true==1){
  918. return $this->error(50024,'','当前货物是假货不能进行零售!');
  919. }
  920. if ($re->user_id!=$id){
  921. return $this->error(50024,'','当前货物绑定的代理信息不是你!');
  922. }
  923. if ($re->is_lowest!=1){
  924. return $this->error(50024,'','零售只能使用小码进行零售!');
  925. }
  926. if ($re->is_sell!=0){
  927. return $this->error(50024,'','该商品已经零售!');
  928. }
  929. $re->is_sell=1;
  930. if (!$re->save()){
  931. throw new \Exception('1');
  932. }
  933. $res=Select::where('sort',$re->pid)->first();
  934. $res->is_full=1;
  935. if (!$res->save()){
  936. throw new \Exception('1');
  937. }
  938. if ($res->level!=1){
  939. $ress=Select::where('sort',$re->pid)->first();
  940. $ress->is_full=1;
  941. if (!$ress->save()){
  942. throw new \Exception('1');
  943. }
  944. }
  945. $sell=new Sell();
  946. $sell->select_id=$re->id;
  947. $sell->select_good_id=$re->good_select_id;
  948. $sell->user_id=$re->user_id;
  949. if (!$sell->save()){
  950. throw new \Exception('1');
  951. }
  952. }
  953. DB::commit();
  954. return $this->success([]);
  955. }catch (\Exception $exception){
  956. DB::rollBack();
  957. Log::error('零售失败,原因是'.$exception);
  958. return $this->error(50021,'','操作失败,请重试!');
  959. }
  960. }
  961. public function get_arr(Request $request){
  962. $app=app('wechat.official_account');
  963. $url='http://api.app.cliu.cc/api/gzh';
  964. $app->jssdk->setUrl($url);
  965. $arr=$app->jssdk->buildConfig(array('scanQRCode'), false);
  966. return $this->success(json_decode($arr));
  967. }
  968. }