UserGoodSendController.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. $input = $request->all();
  239. $u_id=Auth::user()->id;
  240. $users=User::find($u_id);
  241. // $name=$this->JudgePhone($users->mobile);
  242. $user = User::where('id', $request->id)->first();
  243. // Log::error($input['code']);
  244. // $code = json_decode($input['code']);
  245. $code=$input['code'];
  246. if ($u_id==16297){
  247. Log::info('隔这呢搁着呢');
  248. Log::info($code);
  249. }
  250. Log::error(1111);
  251. if ($users->level==3){
  252. $crown_idA=$users->id;
  253. }elseif($users->level==2){
  254. $crown_idA=$users->agent_id;
  255. }else{
  256. $b_user=User::find($users->agent_id);
  257. $crown_idA=$b_user->agent_id;
  258. }
  259. foreach ($code as $k=>$v){
  260. foreach ($v as $ke=>$va){
  261. $code[$k][$ke]= urldecode($va);
  262. }
  263. }
  264. if (count($code)>20){
  265. return $this->error(500214,'','最多发送20个');
  266. }
  267. DB::beginTransaction();
  268. try {
  269. foreach ($code as $ks=>$va){
  270. Log::error($va);
  271. $good=GoodSelect::where('id',$ks)->select('good_name')->first();
  272. foreach ($va as $k=>$v){
  273. $re = Select::where('code', trim($v))->first();
  274. if ($re){
  275. $before_user=User::find($re->user_id);
  276. $before_id=$before_user->id;
  277. try {
  278. $scan_type=0;
  279. if ($before_user->agent_id){
  280. if ($before_user->level==3){
  281. $crown_id=$before_user->id;
  282. }elseif($before_user->level==2){
  283. $crown_id=$before_user->agent_id;
  284. }else{
  285. $b_user=User::find($before_user->agent_id);
  286. $crown_id=$b_user->agent_id;
  287. }
  288. if ($crown_id==$crown_idA && $before_user->id==$users->id){
  289. $scan_type=0;
  290. }elseif ($crown_id==$crown_idA && $before_user->id!=$users->id){
  291. $scan_type=1;
  292. }else{
  293. $scan_type=2;
  294. }
  295. }
  296. }catch (\Exception $exception){
  297. Log::error('在这呢:'.$exception->getMessage());
  298. $scan_type=2;
  299. }
  300. $scan_info=json_encode([]);
  301. // 判断当前二维码的是不是最低级
  302. if ($re->is_lowest == 1) {
  303. $re->track_status = 2;
  304. $re->user_id = $request->id;
  305. if (!$re->save()) {
  306. throw new \Exception('1');
  307. }
  308. $user_s = User::find($request->id);
  309. $data = [
  310. 'op_name' => $users->nickname,
  311. 'op_phone' => $users->mobile,
  312. 'op_id' => $users->id,
  313. 'accept_name' => $user_s->nickname,
  314. 'accept_phone' => $user_s->mobile,
  315. 'accept_id' => $user_s->id,
  316. 'type' => 0,
  317. 'scan_type' =>$scan_type,
  318. 'before_id' => $before_id,
  319. 'select_id' => $re->id,
  320. 'scan_info' => $scan_info,
  321. ];
  322. event(new SendGoodLogEvent($data));
  323. //修改大码和中码状态
  324. $num = $this->update_full($re->pid);
  325. if ($num == 300) {
  326. return $this->error(50021, '', '子码更改信息失败,请重新尝试发货');
  327. }
  328. }
  329. else {
  330. //中码或者大码进行发货
  331. if ($re->pid == 0 && is_numeric($re->pid)) {//不能使用大码进行发货
  332. return $this->error(50021, '', '请使用中码或者小码发货');
  333. }
  334. if ($users->level!=3){
  335. return $this->error(50021, '', '请使用小码进行发货');
  336. }
  337. $user_s = User::find($request->id);
  338. //中码发货
  339. if ($re->level != 1) {
  340. $re->user_id = $request->id;
  341. if (!$re->save()) {
  342. throw new \Exception('1');
  343. }
  344. $num = $this->update_full($re->pid);
  345. if ($num == 300) {
  346. throw new \Exception(1);
  347. }
  348. $data = [
  349. 'op_name' => $users->nickname,
  350. 'op_phone' => $users->mobile,
  351. 'op_id' => $users->id,
  352. 'accept_name' => $user_s->nickname,
  353. 'accept_phone' => $user_s->mobile,
  354. 'accept_id' => $user_s->id,
  355. 'type' => 0,
  356. 'select_id' => $re->id,
  357. 'scan_type' =>$scan_type,
  358. 'before_id' => $before_id,
  359. 'scan_info' => $scan_info
  360. ];
  361. event(new SendGoodLogEvent($data));
  362. $level_data = Select::where('pid', $re->sort)->get();
  363. foreach ($level_data as $k => $v) {
  364. $res = Select::where('code', trim($v->code))->first();
  365. $res->user_id = $request->id;
  366. if (!$res->save()) {
  367. throw new \Exception(1);
  368. }
  369. $data = [
  370. 'op_name' => $users->nickname,
  371. 'op_phone' => $users->mobile,
  372. 'op_id' => $users->id,
  373. 'accept_name' => $user_s->nickname,
  374. 'accept_phone' => $user_s->mobile,
  375. 'accept_id' => $user_s->id,
  376. 'type' => 0,
  377. 'select_id' => $v->id,
  378. 'scan_type' =>$scan_type,
  379. 'before_id' => $before_id,
  380. 'scan_info' => $scan_info
  381. ];
  382. event(new SendGoodLogEvent($data));
  383. }
  384. } else {
  385. /***大码发货,并记录日志情况*/
  386. $re->user_id = $request->id;
  387. if (!$re->save()) {
  388. throw new \Exception('1');
  389. }
  390. $data = [
  391. 'op_name' => $users->nickname,
  392. 'op_phone' => $users->mobile,
  393. 'op_id' => $users->id,
  394. 'accept_name' => $user_s->nickname,
  395. 'accept_phone' => $user_s->mobile,
  396. 'accept_id' => $user_s->id,
  397. 'type' => 0,
  398. 'select_id' => $re->id,
  399. 'scan_type' =>$scan_type,
  400. 'before_id' => $before_id,
  401. 'scan_info' => $scan_info
  402. ];
  403. event(new SendGoodLogEvent($data));
  404. /**查询下级**/
  405. $level_data = Select::where('pid', $re->sort)->get();
  406. foreach ($level_data as $kas => $vas) {
  407. /***修改所有下级的所属者*/
  408. $res = Select::where('code', trim($vas->code))->first();
  409. $res->user_id = $request->id;
  410. if (!$res->save()) {
  411. throw new \Exception(1);
  412. }
  413. $data = [
  414. 'op_name' => $users->nickname,
  415. 'op_phone' => $users->mobile,
  416. 'op_id' => $users->id,
  417. 'accept_name' => $user_s->nickname,
  418. 'accept_phone' => $user_s->mobile,
  419. 'accept_id' => $user_s->id,
  420. 'type' => 0,
  421. 'select_id' => $vas->id,
  422. 'scan_type' =>$scan_type,
  423. 'before_id' => $before_id,
  424. 'scan_info' => $scan_info
  425. ];
  426. event(new SendGoodLogEvent($data));
  427. /**如果现在的等级不是最低级,则执行下面的内容**/
  428. if ($v->is_lowest != 1) {
  429. $ress = Select::where('pid', $v->sort)->get();
  430. foreach ($ress as $kes => $vaes) {
  431. $resss = Select::where('code', trim($vaes->code))->first();
  432. $resss->user_id = $request->id;
  433. if (!$resss->save()) {
  434. throw new \Exception(1);
  435. }
  436. $data = [
  437. 'op_name' => $users->nickname,
  438. 'op_phone' => $users->mobile,
  439. 'op_id' => $users->id,
  440. 'accept_name' => $user_s->nickname,
  441. 'accept_phone' => $user_s->mobile,
  442. 'accept_id' => $user_s->id,
  443. 'type' => 0,
  444. 'select_id' => $vaes->id,
  445. 'scan_type' =>$scan_type,
  446. 'before_id' => $before_id,
  447. 'scan_info' => $scan_info
  448. ];
  449. event(new SendGoodLogEvent($data));
  450. }
  451. }
  452. }
  453. }
  454. }
  455. }else {
  456. Log::error($k+1);
  457. if ($ks==1 || $ks=='one'){
  458. $m='精装版';
  459. }elseif ($ks==2 || $ks=='two'){
  460. $m='简约版';
  461. }elseif ($ks==3 || $ks=='three'){
  462. $m='高腰版';
  463. }elseif ($ks==4 || $ks=='four'){
  464. $m='纯棉版';
  465. }elseif($ks==5 || $ks=='five'){
  466. $m='精装(赠)';
  467. }elseif($ks==6 || $ks=='six'){
  468. $m='青春版';
  469. }
  470. return $this->error(40023, '', '没有查到二维码对应的信息,请删除'.$m.'中第'.($k+1).'个数据重新提交。并将对应的货物单独取出');
  471. }
  472. }
  473. }
  474. Log::info('开始计算扫码库存');
  475. if($users->level==3){
  476. $accept=User::find($request->id);
  477. if($users->id != $accept->id && $accept->level!=3){
  478. $rand_code=$this->get_rand(6);
  479. $current=date('Y-m-d H:i:s');
  480. foreach($request->goods_info as $key=>$val){
  481. $type=$this->type($val['style']);
  482. $user_storage=UserStorage::where('user_id',$users->id)->where('type',$type)
  483. ->where('sex',$val['sex'])->where('size',$val['size'])->first();
  484. if($user_storage){
  485. $user_storage->num = $user_storage->num - $val['code_num'];
  486. $user_storage->save();
  487. }else{
  488. UserStorage::create([
  489. 'user_id'=>$users->id,
  490. 'type'=>$type,
  491. 'sex'=>$val['sex'],
  492. 'size'=>$val['size'],
  493. 'num'=>0-$val['code_num'],
  494. ]);
  495. }
  496. if($val['style']=='精装赠'){
  497. $type=5;
  498. }
  499. ScanStorageLog::create([
  500. 'user_id'=>$users->id,
  501. 'accept_id'=>$accept->id,
  502. 'type'=>$type,
  503. 'sex'=>$val['sex'],
  504. 'size'=>$val['size'],
  505. 'num'=>$val['code_num'],
  506. 'created_at'=>$current,
  507. 'rand_code'=>$rand_code,
  508. ]);
  509. }
  510. }
  511. }
  512. DB::commit();
  513. return $this->success([],'扫码发货成功','扫码发货成功');
  514. }catch (\Exception $exception){
  515. Log::error($exception->getMessage());
  516. return $this->error('2222','',$exception->getMessage());
  517. }
  518. }
  519. //扫码发货减库存日志
  520. public function getScanLog(Request $request){
  521. $page_size=$request->input('page_size');
  522. $page_index=$request->input('page_index');
  523. $start_time=$request->input('start_time');
  524. $end_time=$request->input('end_time');
  525. $search_name=$request->input('search_name');
  526. $num=$page_size*($page_index-1);
  527. $user_id=Auth::user()->id;
  528. $data=ScanStorageLog::with(['user:id,nickname,mobile','accept:id,nickname,mobile'])
  529. ->where('user_id',$user_id)
  530. ->select('user_id','accept_id','rand_code','created_at');
  531. if($start_time && $end_time){
  532. $data->whereBetween('created_at',[$start_time,$end_time]);
  533. }
  534. if($search_name){
  535. $user_ids=User::where(function($query)use($search_name){
  536. $query->where('nickname','like','%'.$search_name.'%')
  537. ->orWhere('mobile','like','%'.$search_name.'%');
  538. })->pluck('id');
  539. $data->whereIn('accept_id',$user_ids);
  540. }
  541. $total=$data->sum('num');
  542. $count=$data->groupBy('rand_code')->get()->count();
  543. $list=$data->groupBy('rand_code')->orderByDesc('created_at')->skip($num)->take($page_size)->get();
  544. foreach($list as $key=>$val){
  545. $types=ScanStorageLog::where('rand_code',$val->rand_code)->where('created_at',$val->created_at)
  546. ->select('type')->groupBy('type')->pluck('type');
  547. $goods=[];
  548. foreach($types as $k=>$v){
  549. switch($v){
  550. case 0:
  551. $goods[$k]['name']='精装版';
  552. break;
  553. case 1:
  554. $goods[$k]['name']='简约版';
  555. break;
  556. case 2:
  557. $goods[$k]['name']='高腰版';
  558. break;
  559. case 3:
  560. $goods[$k]['name']='纯棉版';
  561. break;
  562. case 4:
  563. $goods[$k]['name']='青春版';
  564. break;
  565. case 5:
  566. $goods[$k]['name']='精装赠';
  567. break;
  568. default:
  569. $goods[$k]['name']='规格错误';
  570. }
  571. $goods[$k]['skus']=ScanStorageLog::where('rand_code',$val->rand_code)->where('created_at',$val->created_at)->where('type',$v)
  572. ->select('sex','size','num')
  573. ->orderBy('sex')->get();
  574. }
  575. $list[$key]['goods']=$goods;
  576. }
  577. $info['num']=$total;
  578. $info['info']=$list;
  579. return $this->success_list($info,'',$count);
  580. }
  581. function get_rand( $length = 6 ) {
  582. // 密码字符集,可任意添加你需要的字符
  583. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_';
  584. $str ='';
  585. for ( $i = 0; $i < $length; $i++ )
  586. {
  587. // 这里提供两种字符获取方式
  588. // 第一种是使用 substr 截取$chars中的任意一位字符;
  589. // 第二种是取字符数组 $chars 的任意元素
  590. // $str .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
  591. $str .= $chars[ mt_rand(0, strlen($chars) - 1) ];
  592. }
  593. return $str;
  594. }
  595. function type($attr_main){
  596. switch($attr_main){
  597. case '精装版':
  598. case '精装赠':
  599. return 0;
  600. break;
  601. case '简约版':
  602. return 1;
  603. break;
  604. case '老人版':
  605. case '高腰版':
  606. return 2;
  607. break;
  608. case '纯棉版':
  609. return 3;
  610. break;
  611. case '青春版':
  612. return 4;
  613. break;
  614. default:
  615. return 9;
  616. }
  617. }
  618. /**查询下级代理**/
  619. public function get_lower_user(Request $request)
  620. {
  621. $id = Auth::user()->id;
  622. $ids = [$id];
  623. $data = User::where('agent_id', Auth::user()->id)->whereNull('deleted_at')->pluck('id')->toArray();
  624. $user_id = array_merge($ids, $data);
  625. $search_name=$request->input('search_name')??null;
  626. if ($search_name) {
  627. $where = function ($q) use ($search_name) {
  628. $q->where('nickname', 'like', '%' . $search_name . '%')->orwhere('mobile', 'like', '%' . $search_name . '%');
  629. };
  630. $user = User::where(function ($q)use($id,$user_id){
  631. $q->whereIn('agent_id', $user_id)
  632. ->orwhere('id','like','%'.$id.'%');
  633. })->where($where)->whereNull('deleted_at')->select('mobile as phone', 'nickname', 'realname', 'id', 'headimgurl as avatar','level');
  634. $count = $user->get()->count();
  635. $re = $user->take(20)->skip(($request->page - 1) * 20)->get();
  636. return $this->success_list($re, '', $count);
  637. } else {
  638. $re = User::where(function ($q)use($id,$user_id){
  639. $q->whereIn('agent_id', $user_id)->orwhere('id',$id);
  640. })->whereNull('deleted_at')->select('mobile as phone', 'nickname', 'realname', 'id', 'headimgurl as avatar','level');
  641. // $re = User::whereIn('agent_id', $user_id)->orwhere('id',$id)->whereNull('deleted_at')->select('mobile as phone', 'nickname', 'realname', 'id', 'headimgurl as avatar','level');
  642. $count = $re->get()->count();
  643. $re = $re->take(20)->skip(($request->page - 1) * 20)->get();
  644. return $this->success_list($re, '', $count);
  645. }
  646. }
  647. /**代理退货**/
  648. public function user_return_good(Request $request)
  649. {
  650. $user = Auth::user();
  651. $code = urldecode($request->code);
  652. $re = Select::where('code', $code)->first();
  653. $op_user = User::find($re->user_id);
  654. if ($re) {
  655. if ($re->is_true == 1) {
  656. return $this->error(40028, '', '当前货物为假货,不能进行退货处理');
  657. }
  658. $res = $this->check_user_level($user->id, $re->user_id);
  659. if ($res['code'] == 200) {
  660. if ($re->is_lowest == 1) {
  661. $re->user_id = $user->id;
  662. $re->save();
  663. $this->update_full($re->pid);
  664. $data = [
  665. 'op_name' => $op_user->nickname,
  666. 'op_phone' => $op_user->mobile,
  667. 'op_id' => $op_user->id,
  668. 'accept_name' => $user->nickname,
  669. 'accept_phone' => $user->mobile,
  670. 'accept_id' => $user->id,
  671. 'select_id' => $re->id
  672. ];
  673. event(new ReturnGoodLog($data));
  674. } else {
  675. if ($re->level == 1) {
  676. $num = $this->is_full($re->code);
  677. if ($num == 300) {
  678. return $this->error(50024, '', '当前防伪码不能进行退货处理,请使用小码进行操作');
  679. }
  680. $re->user_id = $user->id;
  681. $re->save();
  682. $data = [
  683. 'op_name' => $op_user->nickname,
  684. 'op_phone' => $op_user->mobile,
  685. 'op_id' => $op_user->id,
  686. 'accept_name' => $user->nickname,
  687. 'accept_phone' => $user->mobile,
  688. 'accept_id' => $user->id,
  689. 'select_id' => $re->id
  690. ];
  691. event(new ReturnGoodLog($data));
  692. $lower_code = Select::where('pid', $re->sort)->get();
  693. foreach ($lower_code as $k => $v) {
  694. $ress = Select::where('code', $v->code)->first();
  695. $ress->user_id = $user->id;
  696. $ress->save();
  697. $data = [
  698. 'op_name' => $op_user->nickname,
  699. 'op_phone' => $op_user->mobile,
  700. 'op_id' => $op_user->id,
  701. 'accept_name' => $user->nickname,
  702. 'accept_phone' => $user->mobile,
  703. 'accept_id' => $user->id,
  704. 'select_id' => $v->id
  705. ];
  706. event(new ReturnGoodLog($data));
  707. if ($ress->is_lowest != 1) {
  708. $lower_codes = Select::where('pid', $v->sort)->get();
  709. foreach ($lower_codes as $ke => $va) {
  710. $resss = Select::where('code', $va->code)->first();
  711. $resss->user_id = $user->id;
  712. $resss->save();
  713. $data = [
  714. 'op_name' => $op_user->nickname,
  715. 'op_phone' => $op_user->mobile,
  716. 'op_id' => $op_user->id,
  717. 'accept_name' => $user->nickname,
  718. 'accept_phone' => $user->mobile,
  719. 'accept_id' => $user->id,
  720. 'select_id' => $va->id
  721. ];
  722. event(new ReturnGoodLog($data));
  723. }
  724. }
  725. }
  726. } else {
  727. $num = $this->is_full($re->code);
  728. if ($num == 300) {
  729. return $this->error(50024, '', '当前防伪码不能进行退货处理,请使用小码进行操作');
  730. }
  731. $re->user_id = $user->id;
  732. $re->save();
  733. $data = [
  734. 'op_name' => $op_user->nickname,
  735. 'op_phone' => $op_user->mobile,
  736. 'op_id' => $op_user->id,
  737. 'accept_name' => $user->nickname,
  738. 'accept_phone' => $user->mobile,
  739. 'accept_id' => $user->id,
  740. 'select_id' => $re->id
  741. ];
  742. event(new ReturnGoodLog($data));
  743. $this->update_full($re->pid);
  744. $lower_code = Select::where('pid', $re->sort)->get();
  745. foreach ($lower_code as $k => $v) {
  746. $ress = Select::where('code', $v->code)->first();
  747. $ress->user_id = $user->id;
  748. $ress->save();
  749. $data = [
  750. 'op_name' => $op_user->nickname,
  751. 'op_phone' => $op_user->mobile,
  752. 'op_id' => $op_user->id,
  753. 'accept_name' => $user->nickname,
  754. 'accept_phone' => $user->mobile,
  755. 'accept_id' => $user->id,
  756. 'select_id' => $v->id
  757. ];
  758. event(new ReturnGoodLog($data));
  759. }
  760. }
  761. }
  762. return $this->success([]);
  763. } else {
  764. return $this->error(40028, '', $res['message']);
  765. }
  766. } else {
  767. return $this->error(40029, '', '暂未查询到相应的信息');
  768. }
  769. }
  770. /**小码发货时更改中码和大码**/
  771. public function update_full($pid)
  772. {
  773. DB::beginTransaction();
  774. try {
  775. $re = Select::where('sort', '=', $pid)->first();
  776. $re->is_full = 1;
  777. if (!$re->save()) {
  778. throw new \Exception('1');
  779. }
  780. if ($re->level != 1) {
  781. $this->update_full($re->pid);
  782. }
  783. DB::commit();
  784. return 200;
  785. } catch (\Exception $exception) {
  786. DB::rollBack();
  787. Log::error($exception);
  788. return 300;
  789. }
  790. }
  791. /**检查用户是不是自己的下级**/
  792. public function check_user_level($id, $user_id)
  793. {
  794. if ($id == $user_id) {
  795. return ['code' => 50026, 'message' => '你不能对自己的货物进行退货操作'];
  796. }
  797. $user = User::find($user_id);
  798. if ($user->level == 3) {
  799. return ['code' => 50026, 'message' => '你不能对代理公司进行退货处理'];
  800. }
  801. if ($user->agent_id != $id) {
  802. $agent_s = User::find($user->agent_id);
  803. if ($agent_s->agnet_id != $id) {
  804. return ['code' => 50026, 'message' => '当前代理不是你的下级,不能进行退货操作'];
  805. }
  806. }
  807. return ['code' => 200];
  808. }
  809. /**检查是够已经发货**/
  810. public function is_full($code)
  811. {
  812. $re = Select::where('code', $code)->first();
  813. if ($re->is_full == 1) {
  814. return 300;
  815. }
  816. return 200;
  817. }
  818. /**展示发货商品所属信息**/
  819. public function select_send_user(Request $request){
  820. $user=Auth::user();
  821. $input=$request->all();
  822. $re=Select::where('code',$input['code'])->with(['select_user','select_good'])->first();
  823. if (empty($re)){
  824. Log::error(1);
  825. return $this->error(52119,'','暂无信息查询');
  826. }
  827. if (!$re->select_user){
  828. Log::error(2);
  829. return $this->error(52120,'','暂无代理信息');
  830. }
  831. if (!$re->select_good){
  832. Log::error(3);
  833. return $this->error(52121,'','暂无商品信息');
  834. }
  835. if ($re->user_id!=$user->id){
  836. Log::error(4);
  837. return $this->error(50021,'','当前货物不是您的货物,不能使用此码进行发货');
  838. }
  839. $time=SendGoodLog::where('select_id',$re->id)->where('accept_id',$re->user_id)->orderByDesc('created_at')->first();
  840. if ($time){
  841. $times=$time->created_at->toDateString();
  842. }else{
  843. $times=Carbon::now()->toDateString();
  844. }
  845. $data=[
  846. 'style'=>$re->select_good->good_name,
  847. 'nickname'=>$re->select_user->nickname,
  848. 'avatar'=>$re->select_user->headimgurl,
  849. 'level'=>$this->judge_level($re->select_user->level),
  850. 'phone'=>$re->select_user->mobile,
  851. 'time'=>$times
  852. ];
  853. return $this->success($data);
  854. }
  855. /**展示退货商品所属信息**/
  856. public function select_return_user(Request $request){
  857. $re=Select::where('code',$request->code)->with(['select_user','select_good'])->first();
  858. if (empty($re)){
  859. return $this->error(52121,'','暂无信息查询');
  860. }
  861. if (!$re->select_user){
  862. return $this->error(52121,'','暂无代理信息');
  863. }
  864. if (!$re->select_good){
  865. return $this->error(52121,'','暂无商品信息');
  866. }
  867. $res=$this->check_user_level(Auth::user()->id,$re->user_id);
  868. if ($res['code']!=200){
  869. return $this->error($res['code'],'',$res['message']);
  870. }
  871. $time=SendGoodLog::where('select_id',$re->id)->where('accept_id',$re->user_id)->orderByDesc('created_at')->first();
  872. $data=[
  873. 'style'=>$re->select_good->good_name,
  874. 'nickname'=>$re->select_user->nickname,
  875. 'level'=>$this->judge_level($re->select_user->level),
  876. 'phone'=>$re->select_user->mobile,
  877. 'time'=>$time->created_at->toDateString()
  878. ];
  879. return $this->success($data);
  880. }
  881. /**判断等级**/
  882. public function judge_level($status){
  883. switch ($status){
  884. case 1:
  885. $level='销售主管';
  886. break;
  887. case 2:
  888. $level='销售经理';
  889. break;
  890. case 3:
  891. $level='代理公司';
  892. break;
  893. default:
  894. $level='销售主管';
  895. break;
  896. }
  897. return $level;
  898. }
  899. public function sell(Request $request){
  900. try{
  901. $id=Auth::user()->id;
  902. $code=urldecode($request->code);
  903. DB::beginTransaction();
  904. $re=Select::where('code',$code)->first();
  905. if ($re){
  906. if ($re->is_true==1){
  907. return $this->error(50024,'','当前货物是假货不能进行零售!');
  908. }
  909. if ($re->user_id!=$id){
  910. return $this->error(50024,'','当前货物绑定的代理信息不是你!');
  911. }
  912. if ($re->is_lowest!=1){
  913. return $this->error(50024,'','零售只能使用小码进行零售!');
  914. }
  915. if ($re->is_sell!=0){
  916. return $this->error(50024,'','该商品已经零售!');
  917. }
  918. $re->is_sell=1;
  919. if (!$re->save()){
  920. throw new \Exception('1');
  921. }
  922. $res=Select::where('sort',$re->pid)->first();
  923. $res->is_full=1;
  924. if (!$res->save()){
  925. throw new \Exception('1');
  926. }
  927. if ($res->level!=1){
  928. $ress=Select::where('sort',$re->pid)->first();
  929. $ress->is_full=1;
  930. if (!$ress->save()){
  931. throw new \Exception('1');
  932. }
  933. }
  934. $sell=new Sell();
  935. $sell->select_id=$re->id;
  936. $sell->select_good_id=$re->good_select_id;
  937. $sell->user_id=$re->user_id;
  938. if (!$sell->save()){
  939. throw new \Exception('1');
  940. }
  941. }
  942. DB::commit();
  943. return $this->success([]);
  944. }catch (\Exception $exception){
  945. DB::rollBack();
  946. Log::error('零售失败,原因是'.$exception);
  947. return $this->error(50021,'','操作失败,请重试!');
  948. }
  949. }
  950. public function get_arr(Request $request){
  951. $app=app('wechat.official_account');
  952. $url='http://api.app.jiuweiyun.cn/api/gzh';
  953. $app->jssdk->setUrl($url);
  954. $arr=$app->jssdk->buildConfig(array('scanQRCode'), false);
  955. return $this->success(json_decode($arr));
  956. }
  957. }