StorageController.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Express;
  4. use App\Models\ExpressStorage;
  5. use App\Models\Goodtest;
  6. use App\Models\Ordertest;
  7. use App\Models\StorageLog;
  8. use App\Models\StoragePutLog;
  9. use App\Models\UserStorage;
  10. use Carbon\Carbon;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Auth;
  13. use Illuminate\Support\Facades\DB;
  14. class StorageController extends Controller{
  15. /**查看货物**/
  16. public function look_good(Request $request){
  17. $id=$request->id;
  18. $ids=Auth::user()->id;
  19. // $ids=39516;
  20. $re=Express::where('order_id',$id)->with(['get_express'=>function($q)use($id){
  21. $q->where('order_id',$id);
  22. },'get_order'])->orderByDesc('number')->groupBy('number')->get();
  23. $data=[];
  24. foreach ($re as $k=>$v){
  25. $data[$k]=$v;
  26. if ($ids==$v->get_order->good_user_id){
  27. $data[$k]['is_per']=true;
  28. }else{
  29. $data[$k]['is_per']=false;
  30. }
  31. }
  32. return $this->success_list($data);
  33. }
  34. /*
  35. * 货物详情
  36. * **/
  37. public function good_detail(Request $request){
  38. $re=ExpressStorage::where('express_id',$request->id)->groupBy('type')->get();
  39. $datas=Goodtest::where('is_deleted',0)->where('id','>',57)->groupBy('main_attr')->get();
  40. foreach ($datas as $k=>$v){
  41. $r[$v->main_attr]=$v->main_img;
  42. }
  43. $data=[
  44. 'hard'=>['spec'=>[],'imgurl'=>$r['大卫博士精装版'],'name'=>'大卫博士精装版'],
  45. 'simple'=>['spec'=>[],'imgurl'=>$r['大卫博士简约版'],'name'=>'大卫博士简约版'],
  46. 'old'=>['spec'=>[],'imgurl'=>$r['大卫博士高腰版'],'name'=>'大卫博士高腰版'],
  47. 'new_old'=>['spec'=>[],'imgurl'=>$r['大卫博士纯棉老人版'],'name'=>'大卫博士纯棉老人版'],
  48. 'youth'=>['spec'=>[],'imgurl'=>$r['大卫博士青春版'],'name'=>'大卫博士青春版'],
  49. 'right'=>['spec'=>[],'imgurl'=>$r['红色贺岁款(精装版)'],'name'=>'红色贺岁款(精装版)'],
  50. ];
  51. foreach ($re as $k=>$v){
  52. $res=ExpressStorage::where('express_id',$request->id)->where('type',$v->type)->select('size','sex','type','num')->get()->toArray();
  53. if ($v->type==0){
  54. $data['hard']['spec']=$res;
  55. }elseif ($v->type==1){
  56. $data['simple']['spec']=$res;
  57. }elseif ($v->type==2){
  58. $data['old']['spec']=$res;
  59. }elseif(($v->type==3)){
  60. $data['new_old']['spec']=$res;
  61. }elseif(($v->type==4)){
  62. $data['youth']['spec']=$res;
  63. }elseif(($v->type==5)){
  64. $data['right']['spec']=$res;
  65. }
  66. }
  67. return $this->success($data);
  68. }
  69. /*
  70. * 我的库存
  71. * **/
  72. public function my_storage(){
  73. $id=Auth::user()->id;
  74. // $id=39516;
  75. $re=UserStorage::where('user_id',$id)->groupBy('type')->select(DB::raw('sum(num) as total'),'type')->get();
  76. $res=UserStorage::where('user_id',$id)->first();
  77. $data=['hard'=>0,'simple'=>0,'old'=>0,'new_old'=>0,'youth'=>0];
  78. foreach ($re as $k=>$v){
  79. if ($v->type==0){
  80. $data['hard']=$v->total;
  81. }elseif ($v->type==1){
  82. $data['simple']=$v->total;
  83. }elseif ($v->type==2){
  84. $data['old']=$v->total;
  85. }elseif($v->type==3){
  86. $data['new_old']=$v->total;
  87. }elseif($v->type==4){
  88. $data['youth']=$v->total;
  89. }
  90. }
  91. if ($res){
  92. $data['status']=false;
  93. }else{
  94. $data['status']=true;
  95. }
  96. return $this->success($data);
  97. }
  98. /*
  99. *库存详情
  100. * **/
  101. public function storage_detail(Request $request){
  102. $id=Auth::user()->id;
  103. // $id=39516;
  104. $re=UserStorage::where('user_id',$id)->groupBy('type')->get();
  105. $datas=Goodtest::where('is_deleted',0)->where('id','>',57)->groupBy('main_attr')->get();
  106. foreach ($datas as $k=>$v){
  107. $r[$v->main_attr]=$v->main_img;
  108. }
  109. $data=[
  110. 'hard'=>['spec'=>[],'imgurl'=>$r['大卫博士精装版'],'name'=>'大卫博士精装版'],
  111. 'simple'=>['spec'=>[],'imgurl'=>$r['大卫博士简约版'],'name'=>'大卫博士简约版'],
  112. 'old'=>['spec'=>[],'imgurl'=>$r['大卫博士高腰版'],'name'=>'大卫博士高腰版'],
  113. 'new_old'=>['spec'=>[],'imgurl'=>$r['大卫博士纯棉老人版'],'name'=>'大卫博士纯棉版'],
  114. 'youth'=>['spec'=>[],'imgurl'=>$r['大卫博士青春版'],'name'=>'大卫博士青春版'],
  115. ];
  116. foreach ($re as $k=>$v){
  117. $res=UserStorage::where('user_id',$id)->where('type',$v->type)->select('size','sex','type','num')->get();
  118. if ($v->type==0){
  119. $data['hard']['spec']=$res;
  120. }elseif ($v->type==1){
  121. $data['simple']['spec']=$res;
  122. }elseif ($v->type==2){
  123. $data['old']['spec']=$res;
  124. }elseif($v->type==3){
  125. $data['new_old']['spec']=$res;
  126. }elseif($v->type==4){
  127. $data['youth']['spec']=$res;
  128. }
  129. }
  130. return $this->success($data);
  131. }
  132. /*
  133. * 待入库
  134. * ***/
  135. public function put_storage(Request $request){
  136. $input=$request->all();
  137. $id=Auth::user()->id;
  138. // $id=39516;
  139. $page_index=$input['page_index'];
  140. $data=Ordertest::where('user_id',$id)->where('good_user_id',$id)
  141. ->where('track_status','<>',0)
  142. ->where('created_at','>','2020-01-01 00:00:00')
  143. ->where('put_status',0)->select('id as order_id','created_at','number','order_num');
  144. $count=$data->count();
  145. $data=$data->take(20)->skip(($page_index-1)*20)->get();
  146. return $this->success_list($data,'',$count);
  147. }
  148. /*
  149. * 入库记录
  150. * ***/
  151. public function put_storage_group(Request $request){
  152. $input=$request->all();
  153. $id=Auth::user()->id;
  154. // $id=39516;
  155. $datas=Goodtest::where('is_deleted',0)->where('id','>',57)->groupBy('main_attr')->get();
  156. foreach ($datas as $k=>$v){
  157. $r[$v->main_attr]=$v->main_img;
  158. }
  159. $data=[
  160. 'hard'=>['imgurl'=>$r['大卫博士精装版'],'name'=>'大卫博士精装版','woman'=>0,'man'=>0],
  161. 'simple'=>['imgurl'=>$r['大卫博士简约版'],'name'=>'大卫博士简约版','woman'=>0,'man'=>0],
  162. 'old'=>['imgurl'=>$r['大卫博士高腰版'],'name'=>'大卫博士高腰版','woman'=>0,'man'=>0],
  163. 'new_old'=>['imgurl'=>$r['大卫博士纯棉老人版'],'name'=>'大卫博士纯棉老人版','woman'=>0,'man'=>0],
  164. 'youth'=>['imgurl'=>$r['大卫博士青春版'],'name'=>'大卫博士青春版','woman'=>0,'man'=>0],
  165. ];
  166. $where_type=[];
  167. $type=$input['record_type'];
  168. $status=$input['status'];
  169. if ($type){
  170. if ($type==1){
  171. if ($status==-1){
  172. $where_type=function ($q)use($type,$status){
  173. $q->where('storage_type',$type);
  174. };
  175. }else{
  176. $where_type=function ($q)use($type,$status){
  177. $q->where('storage_type',$type)->where('out_type',$status);
  178. };
  179. }
  180. }else{
  181. $where_type=function ($q)use($type,$status){
  182. $q->where('storage_type',$type);
  183. };
  184. }
  185. }
  186. $express=ExpressStorage::where('user_id',$id)->where($where_type);
  187. $where_start=[];
  188. $start=$input['start_time'];
  189. if ($start){
  190. $where_start=function ($q)use($start){
  191. $q->where('created_at','>=',$start);
  192. };
  193. }
  194. $where_end=[];
  195. $end=$input['end_time'];
  196. if ($end){
  197. $where_end=function ($q)use($end){
  198. $q->where('created_at','<=',$end);
  199. };
  200. }
  201. $re=$express->where($where_start)->where($where_end)->groupBy('type')->get();
  202. foreach ($re as $k=>$v){
  203. $woman=0;
  204. $man=0;
  205. if ($type==1){
  206. $res=ExpressStorage::where('user_id',$id)->where('type',$v->type)->where('storage_type',$type)->where($where_start)->where($where_end);
  207. if ($input['status']==-1){
  208. $res=$res->groupBy('sex')->select(DB::raw('sum(num) as total'),'sex')->get()->toArray();
  209. }else{
  210. $res=$res->where('out_type',$input['status'])->groupBy('sex')->select(DB::raw('sum(num) as total'),'sex')->get()->toArray();
  211. }
  212. foreach ($res as $ke=>$va){
  213. if ($va['sex']==0){
  214. $woman=$va['total'];
  215. }else{
  216. $man=$va['total'];
  217. }
  218. }
  219. }else{
  220. $res=ExpressStorage::where('user_id',$id)->where('type',$v->type)->where('storage_type',$type)->where($where_start)->where($where_end)->groupBy('sex')->select(DB::raw('sum(num) as total'),'sex')->get()->toArray();
  221. foreach ($res as $ke=>$va){
  222. if ($va['sex']==0){
  223. $woman=$va['total'];
  224. }else{
  225. $man=$va['total'];
  226. }
  227. }
  228. }
  229. if ($v->type==0){
  230. $data['hard']['man']=$man;
  231. $data['hard']['woman']=$woman;
  232. }elseif ($v->type==1){
  233. $data['simple']['man']=$man;
  234. $data['simple']['woman']=$woman;
  235. }elseif ($v->type==2){
  236. $data['old']['man']=$man;
  237. $data['old']['woman']=$woman;
  238. }elseif($v->type==3){
  239. $data['new_old']['man']=$man;
  240. $data['new_old']['woman']=$woman;
  241. }elseif($v->type==4){
  242. $data['youth']['man']=$man;
  243. $data['youth']['woman']=$woman;
  244. }
  245. }
  246. return $this->success($data);
  247. }
  248. public function put_storage_detail(Request $request){
  249. $input=$request->all();
  250. $id=Auth::user()->id;
  251. // $id=39516;
  252. $where_type=[];
  253. $status=$input['status'];
  254. if ($input['record_type']){
  255. $type=$input['record_type'];
  256. if ($type==1){
  257. if ($status!=-1){
  258. $where_type=function ($q)use($type,$status){
  259. $q->where('storage_type',$type)->where('out_type',$status);
  260. };
  261. }else{
  262. $where_type=function ($q)use($type,$status){
  263. $q->where('storage_type',$type);
  264. };
  265. }
  266. }else{
  267. $where_type=function ($q)use($type,$status){
  268. $q->where('storage_type',$type);
  269. };
  270. }
  271. }
  272. $express=ExpressStorage::where('user_id',$id)->where($where_type)->where('type',$input['type']);
  273. $where_start=[];
  274. $start=$input['start_time'];
  275. if ($start){
  276. if ($input['record_type']==1){
  277. $where_start=function ($q)use($start){
  278. $q->where('out_time','>=',$start);
  279. };
  280. }else{
  281. $where_start=function ($q)use($start){
  282. $q->where('created_at','>=',$start);
  283. };
  284. }
  285. }
  286. $where_end=[];
  287. $end=$input['end_time'];
  288. if ($end){
  289. if ($input['record_type']==1){
  290. $where_end=function ($q)use($end){
  291. $q->where('out_time','<=',$end);
  292. };
  293. }else{
  294. $where_end=function ($q)use($end){
  295. $q->where('created_at','<=',$end);
  296. };
  297. }
  298. }
  299. $re=$express->where($where_start)->where($where_end);
  300. $count=$re->count();
  301. $data=$re->take(20)->skip(($input['page_index']-1)*20)->get();
  302. return $this->success_list($data,'',$count);
  303. }
  304. public function put_good_storage(Request $request){
  305. // return $this->error(50026,'','入库操作暂停使用!如有疑问请联系客服!');
  306. $input=$request->all();
  307. $id=Auth::user()->id;
  308. // $id=39516;
  309. $d=UserStorage::where('user_id',$id)->first();
  310. if (!$d){
  311. return $this->error(50026,'','请盘库!');
  312. }
  313. $express=Express::find($input['id']);
  314. if ($express->put_status==1){
  315. return $this->error(50026,'','该货物已经入库,请退出刷新');
  316. }
  317. DB::beginTransaction();
  318. try{
  319. //查找这一箱的货物量
  320. $re=ExpressStorage::where('express_id',$input['id'])->select('sex','size','type','num')->get();
  321. //查找这一箱的信息
  322. foreach ($re as $k=>$v){
  323. //查找库存中有没有这个商品的记录
  324. $res=UserStorage::where('user_id',$id)->where('sex',$v->sex)->where('size',$v->size)->where('type',$v->type)->first();
  325. //有则加
  326. if ($res){
  327. if ($v->type==1||$v->type==2){
  328. $num=$v->num*4;
  329. }elseif ($v->type==3){
  330. $num=$v->num*6;
  331. }else{
  332. $num=$v->num;
  333. }
  334. $res->num+=$num;
  335. if (!$res->save()){
  336. throw new \Exception(1);
  337. }
  338. }else{
  339. //没有则新建
  340. $storage=new UserStorage();
  341. if ($v->type==1||$v->type==2){
  342. $storage->num=$v->num*4;
  343. }elseif ($v->type==3){
  344. $storage->num=$v->num*6;
  345. }else{
  346. $storage->num=$v->num;
  347. }
  348. $storage->size=$v->size;
  349. $storage->user_id=$id;
  350. $storage->sex=$v->sex;
  351. $storage->type=$v->type;
  352. if (!$storage->save()){
  353. throw new \Exception('1');
  354. }
  355. }
  356. $log=new StoragePutLog();
  357. $log->size=$v->size;
  358. $log->sex=$v->sex;
  359. $log->type=$v->type;;
  360. $log->user_id=$id;
  361. if ($v->type==1||$v->type==2){
  362. $log->num=$v->num*4;
  363. }elseif ($v->type==3){
  364. $log->num=$v->num*6;
  365. }else{
  366. $log->num=$v->num;
  367. }
  368. if (!$log->save()){
  369. throw new \Exception(1);
  370. }
  371. }
  372. //更改这一箱的状态为入库
  373. ExpressStorage::where('express_id',$input['id'])->select('sex','size','type','num')->update(['sure_time'=>Carbon::now(),'storage_type'=>2]);
  374. //更改这一样的装填为已完成
  375. $express->put_status=1;
  376. $express->put_time=Carbon::now();
  377. if (!$express->save()){
  378. throw new \Exception('1');
  379. }
  380. //查找订单
  381. $order=Ordertest::find($express->order_id);
  382. if ($order->track_status==2){
  383. //查看一下这个订单是不是都已经入库
  384. $count=Express::where('order_id',$order->id)->where('put_status',0)->count();
  385. if ($count==0){
  386. //全部入库后更改 订单状态为已完成的状态
  387. $order->put_status=1;
  388. if (!$order->save()){
  389. throw new \Exception(1);
  390. }
  391. }
  392. }
  393. DB::commit();
  394. return $this->success([]);
  395. }catch (\Exception $exception){
  396. DB::rollBack();
  397. return $this->error(50021,'','入库出错:'.$exception);
  398. }
  399. }
  400. /*
  401. * 判断是不是第一次盘库
  402. * ***/
  403. public function get_goodimg(){
  404. $datas=Goodtest::where('is_deleted',0)->where('id','>',57)->groupBy('main_attr')->get();
  405. foreach ($datas as $k=>$v){
  406. $r[$v->main_attr]=$v->main_img;
  407. }
  408. $data=[
  409. 'hard'=>['imgurl'=>$r['大卫博士精装版'],'name'=>'大卫博士精装版'],
  410. 'simple'=>['imgurl'=>$r['大卫博士简约版'],'name'=>'大卫博士简约版'],
  411. 'old'=>['imgurl'=>$r['大卫博士高腰版'],'name'=>'大卫博士高腰版'],
  412. 'new_old'=>['imgurl'=>$r['大卫博士纯棉老人版'],'name'=>'大卫博士纯棉老人版'],
  413. 'youth'=>['imgurl'=>$r['大卫博士青春版'],'name'=>'大卫博士青春版'],
  414. ];
  415. return $this->success($data);
  416. }
  417. /**一次性盘库**/
  418. public function one_sure_storage(Request $request){
  419. $id=Auth::user()->id;
  420. $re=UserStorage::where('user_id',$id)->first();
  421. if ($re){
  422. return $this->error(50024,'','该用户不能使用一次性盘货功能');
  423. }
  424. $input=$request->all();
  425. DB::beginTransaction();
  426. try{
  427. foreach ($input as $k=>$v){
  428. if($k=='hard'){
  429. $type=0;
  430. }elseif ($k=='simple'){
  431. $type=1;
  432. }elseif ($k=='old'){
  433. $type=2;
  434. }elseif($k=='new_old'){
  435. $type=3;
  436. }elseif($k=='youth'){
  437. $type=4;
  438. }
  439. foreach ($v as $ke=>$va){
  440. $storage=new UserStorage();
  441. $storage->user_id=$id;
  442. $storage->type=$type;
  443. $storage->size=$va['size'];
  444. $storage->num=$va['num'];
  445. $storage->sex=$va['sex'];
  446. $st_log=new StorageLog();
  447. $st_log->user_id=$id;
  448. $st_log->type=$type;
  449. $st_log->size=$va['size'];
  450. $st_log->num=$va['num'];
  451. $st_log->sex=$va['sex'];
  452. $st_log->number=1;
  453. $st_log->old_num=0;
  454. if (!$st_log->save()){
  455. throw new \Exception('1');
  456. }
  457. if (!$storage->save()){
  458. throw new \Exception('1');
  459. }
  460. }
  461. }
  462. DB::commit();
  463. return $this->success([]);
  464. }catch (\Exception $exception){
  465. DB::rollBack();
  466. return $this->error(50021,'',$exception);
  467. }
  468. }
  469. /*
  470. * 获取库存信息
  471. * ***/
  472. public function get_storage(Request $request){
  473. // $id=39516;
  474. $id=Auth::user()->id;
  475. $re=UserStorage::where('user_id',$id)->with(['get_storage'=>function($q)use($id){
  476. $q->where('user_id',$id)->select('sex','size','type','num');
  477. }])->groupBy('type')->get();
  478. $data=[
  479. 'hard'=>[],
  480. 'simple'=>[],
  481. 'old'=>[],
  482. 'new_old'=>[],
  483. 'youth'=>[],
  484. ];
  485. foreach ($re as $k=>$v){
  486. if ($v->type==0){
  487. $data['hard']=$v->get_storage;
  488. }elseif ($v->type==1){
  489. $data['simple']=$v->get_storage;
  490. }elseif ($v->type==2){
  491. $data['old']=$v->get_storage;
  492. }elseif ($v->type==4){
  493. $data['youth']=$v->get_storage;
  494. }else{
  495. $data['new_old']=$v->get_storage;
  496. }
  497. }
  498. return $this->success($data);
  499. }
  500. /**一次性盘库**/
  501. public function sure_storage(Request $request){
  502. $id=Auth::user()->id;
  503. $re=UserStorage::where('user_id',$id)->first();
  504. if (!$re){
  505. return $this->error(50024,'','该用户需要使用一次性盘货功能');
  506. }
  507. $number=StorageLog::where('user_id',$id)->max('number');
  508. $input=$request->all();
  509. DB::beginTransaction();
  510. try{
  511. foreach ($input as $k=>$v){
  512. if($k=='hard'){
  513. $type=0;
  514. }elseif ($k=='simple'){
  515. $type=1;
  516. }elseif ($k=='old'){
  517. $type=2;
  518. }elseif ($k=='youth'){
  519. $type=4;
  520. }else{
  521. $type=3;
  522. }
  523. foreach ($v as $ke=>$va){
  524. $storage=UserStorage::where('type',$type)->where('user_id',$id)->where('size',$va['size'])->where('sex',$va['sex'])->first();
  525. $st_log=new StorageLog();
  526. $st_log->number=$number+1;
  527. $st_log->sure_time=Carbon::now()->toDateTimeString();
  528. if ($storage){
  529. $st_log->old_num=$storage->num;
  530. if ($va['num']>$storage->num){
  531. $put_st=new StoragePutLog();
  532. $put_st->user_id=$id;
  533. $put_st->type=$type;
  534. $put_st->size=$va['size'];
  535. $put_st->num=$va['num']-$storage->num;
  536. $put_st->sex=$va['sex'];
  537. $put_st->storage_type=0;
  538. if (!$put_st->save()){
  539. throw new \Exception('1');
  540. }
  541. }elseif ($va['num']<$storage->num){
  542. $out_st=new ExpressStorage();
  543. $out_st->user_id=$id;
  544. $out_st->type=$type;
  545. $out_st->size=$va['size'];
  546. $out_st->num=$storage->num-$va['num'];
  547. $out_st->sex=$va['sex'];
  548. $out_st->storage_type=1;
  549. $out_st->out_time=Carbon::now();
  550. if (!$out_st->save()){
  551. throw new \Exception('1');
  552. }
  553. }
  554. $storage->num=$va['num'];
  555. }else{
  556. $storage=new UserStorage();
  557. $storage->user_id=$id;
  558. $storage->type=$type;
  559. $storage->size=$va['size'];
  560. $storage->num=$va['num'];
  561. $storage->sex=$va['sex'];
  562. $st_log->old_num=0;
  563. }
  564. $st_log->user_id=$id;
  565. $st_log->type=$type;
  566. $st_log->size=$va['size'];
  567. $st_log->num=$va['num'];
  568. $st_log->sex=$va['sex'];
  569. if (!$storage->save()){
  570. throw new \Exception('1');
  571. }
  572. if (!$st_log->save()){
  573. throw new \Exception('1');
  574. }
  575. }
  576. }
  577. DB::commit();
  578. return $this->success([]);
  579. }catch (\Exception $exception){
  580. DB::rollBack();
  581. return $this->error(50021,'',$exception);
  582. }
  583. }
  584. /***出库**/
  585. public function out_storage(Request $request){
  586. $id=Auth::user()->id;
  587. $re=UserStorage::where('user_id',$id)->first();
  588. if (!$re){
  589. return $this->error(50024,'','该用户需要使用一次性盘货功能');
  590. }
  591. $input=$request->all();
  592. DB::beginTransaction();
  593. try{
  594. foreach ($input['arr'] as $k=>$v){
  595. if($k=='hard'){
  596. $type=0;
  597. }elseif ($k=='simple'){
  598. $type=1;
  599. }elseif ($k=='old'){
  600. $type=2;
  601. }elseif ($k=='youth'){
  602. $type=4;
  603. }else{
  604. $type=3;
  605. }
  606. foreach ($v as $ke=>$va){
  607. if ($va['num']!=0){
  608. $storage=UserStorage::where('type',$type)->where('user_id',$id)->where('size',$va['size'])->where('sex',$va['sex'])->first();
  609. if (!$storage){
  610. return $this->error(50023,'','没有该商品的库存,请先使用盘库功能');
  611. }
  612. if ($storage->num<$va['num']){
  613. return $this->error(50023,'','该商品的出库量大于库存量,请先使用盘库修改库存');
  614. }
  615. $storage->num-=$va['num'];
  616. if (!$storage->save()){
  617. throw new \Exception('1');
  618. }
  619. $eStorage=new ExpressStorage();
  620. $eStorage->num=$va['num'];
  621. $eStorage->user_id=$id;
  622. $eStorage->out_type=$input['type'];
  623. $eStorage->size=$va['size'];
  624. $eStorage->sex=$va['sex'];
  625. $eStorage->type=$type;
  626. $eStorage->storage_type=1;
  627. $eStorage->out_time=Carbon::now();
  628. if (!$eStorage->save()){
  629. throw new \Exception('1');
  630. }
  631. }
  632. }
  633. }
  634. DB::commit();
  635. return $this->success([]);
  636. }catch (\Exception $exception){
  637. DB::rollBack();
  638. return $this->error(50021,'',$exception);
  639. }
  640. }
  641. /*
  642. * 盘库记录
  643. **/
  644. public function sure_storage_group(Request $request){
  645. $input=$request->all();
  646. $id=Auth::user()->id;
  647. // $id=39516;
  648. $datas=Goodtest::where('is_deleted',0)->where('id','>',57)->groupBy('main_attr')->get();
  649. foreach ($datas as $k=>$v){
  650. $r[$v->main_attr]=$v->main_img;
  651. }
  652. $data=[
  653. 'hard'=>['imgurl'=>$r['大卫博士精装版'],'name'=>'大卫博士精装版','woman'=>0,'man'=>0],
  654. 'simple'=>['imgurl'=>$r['大卫博士简约版'],'name'=>'大卫博士简约版','woman'=>0,'man'=>0],
  655. 'old'=>['imgurl'=>$r['大卫博士高腰版'],'name'=>'大卫博士高腰版','woman'=>0,'man'=>0],
  656. 'new_old'=>['imgurl'=>$r['大卫博士纯棉老人版'],'name'=>'大卫博士纯棉老人版','woman'=>0,'man'=>0],
  657. 'youth'=>['imgurl'=>$r['大卫博士青春版'],'name'=>'大卫博士青春版','woman'=>0,'man'=>0],
  658. ];
  659. $storage_log=StorageLog::where('user_id',$id);
  660. $where_start=[];
  661. $start=$input['start_time'];
  662. if ($start){
  663. $where_start=function ($q)use($start){
  664. $q->where('created_at','>=',$start);
  665. };
  666. }
  667. $where_end=[];
  668. $end=$input['end_time'];
  669. if ($end){
  670. $where_end=function ($q)use($end){
  671. $q->where('created_at','<=',$end);
  672. };
  673. }
  674. $re=$storage_log->where($where_start)->where($where_end)->groupBy('type')->get();
  675. foreach ($re as $k=>$v){
  676. $woman=0;
  677. $man=0;
  678. $res=StorageLog::where('user_id',$id)->where('type',$v->type)->where($where_start)->where($where_end)->groupBy('sex')->select(DB::raw('sum(num) as total'),'sex')->get()->toArray();
  679. foreach ($res as $ke=>$va){
  680. if ($va['sex']==0){
  681. $woman=$va['total'];
  682. }else{
  683. $man=$va['total'];
  684. }
  685. }
  686. if ($v->type==0){
  687. $data['hard']['man']=$man;
  688. $data['hard']['woman']=$woman;
  689. }elseif ($v->type==1){
  690. $data['simple']['man']=$man;
  691. $data['simple']['woman']=$woman;
  692. }elseif ($v->type==2){
  693. $data['old']['man']=$man;
  694. $data['old']['woman']=$woman;
  695. }elseif ($v->type==3){
  696. $data['new_old']['man']=$man;
  697. $data['new_old']['woman']=$woman;
  698. }elseif ($v->type==4){
  699. $data['youth']['man']=$man;
  700. $data['youth']['woman']=$woman;
  701. }
  702. }
  703. return $this->success($data);
  704. }
  705. public function sure_storage_detail(Request $request){
  706. $input=$request->all();
  707. $id=Auth::user()->id;
  708. // $id=39516;
  709. $express=StorageLog::where('user_id',$id)->where('type',$input['type']);
  710. $where_start=[];
  711. $start=$input['start_time'];
  712. if ($start){
  713. $where_start=function ($q)use($start){
  714. $q->where('created_at','>=',$start);
  715. };
  716. }
  717. $where_end=[];
  718. $end=$input['end_time'];
  719. if ($end){
  720. $where_end=function ($q)use($end){
  721. $q->where('created_at','<=',$end);
  722. };
  723. }
  724. $re=$express->where($where_start)->where($where_end);
  725. $count=$re->count();
  726. $data=$re->take(20)->skip(($input['page_index']-1)*20)->get();
  727. return $this->success_list($data,'',$count);
  728. }
  729. /*
  730. *订货入库和退货入库
  731. ***/
  732. public function put_storages(Request $request){
  733. $id=Auth::user()->id;
  734. $re=UserStorage::where('user_id',$id)->first();
  735. if (!$re){
  736. return $this->error(50024,'','该用户需要使用一次性盘货功能');
  737. }
  738. $input=$request->all();
  739. $data=$input['data'];
  740. // $put_type=0;
  741. $put_type=$input['put_type'];
  742. DB::beginTransaction();
  743. try{
  744. foreach ($data as $k=>$v){
  745. if($k=='hard'){
  746. $type=0;
  747. }elseif ($k=='simple'){
  748. $type=1;
  749. }elseif ($k=='old'){
  750. $type=2;
  751. }elseif ($k=='youth'){
  752. $type=4;
  753. }else{
  754. $type=3;
  755. }
  756. foreach ($v as $ke=>$va){
  757. if ($va['num']!=0){
  758. $st_log=new StoragePutLog();
  759. $storage=UserStorage::where('type',$type)->where('user_id',$id)->where('size',$va['size'])->where('sex',$va['sex'])->first();
  760. if ($put_type==0){
  761. if ($type==2||$type==1 || $type==4){
  762. $va['num']=$va['num']*4;
  763. }elseif ($type==3){
  764. $va['num']=$va['num']*6;
  765. }
  766. }
  767. if ($storage){
  768. $storage->num+=$va['num'];
  769. }else{
  770. $storage=new UserStorage();
  771. $storage->user_id=$id;
  772. $storage->type=$type;
  773. $storage->size=$va['size'];
  774. $storage->num=$va['num'];
  775. $storage->sex=$va['sex'];
  776. }
  777. $st_log->user_id=$id;
  778. $st_log->type=$type;
  779. $st_log->size=$va['size'];
  780. $st_log->num=$va['num'];
  781. $st_log->sex=$va['sex'];
  782. $st_log->put_type=$put_type;
  783. $st_log->storage_type=0;
  784. if (!$storage->save()){
  785. throw new \Exception('1');
  786. }
  787. if (!$st_log->save()){
  788. throw new \Exception('1');
  789. }
  790. }
  791. }
  792. }
  793. DB::commit();
  794. return $this->success([]);
  795. }catch (\Exception $exception){
  796. DB::rollBack();
  797. return $this->error(50021,'',$exception);
  798. }
  799. }
  800. /*
  801. * 入库记录
  802. * ***/
  803. public function put_storage_groups(Request $request){
  804. $input=$request->all();
  805. $id=Auth::user()->id;
  806. // $id=39516;
  807. $datas=Goodtest::where('is_deleted',0)->where('id','>',57)->groupBy('main_attr')->get();
  808. foreach ($datas as $k=>$v){
  809. $r[$v->main_attr]=$v->main_img;
  810. }
  811. $data=[
  812. 'hard'=>['imgurl'=>$r['大卫博士精装版'],'name'=>'大卫博士精装版','woman'=>0,'man'=>0],
  813. 'simple'=>['imgurl'=>$r['大卫博士简约版'],'name'=>'大卫博士简约版','woman'=>0,'man'=>0],
  814. 'old'=>['imgurl'=>$r['大卫博士高腰版'],'name'=>'大卫博士高腰版','woman'=>0,'man'=>0],
  815. 'new_old'=>['imgurl'=>$r['大卫博士纯棉老人版'],'name'=>'大卫博士纯棉老人版','woman'=>0,'man'=>0],
  816. 'youth'=>['imgurl'=>$r['大卫博士青春版'],'name'=>'大卫博士青春版','woman'=>0,'man'=>0],
  817. ];
  818. $where_type=[];
  819. $type=$input['record_type'];
  820. if ($type){
  821. if ($type==1){
  822. $where_type=function ($q)use($type){
  823. $q->where('storage_type',$type);
  824. };
  825. }else{
  826. $where_type=function ($q)use($type){
  827. $q->where('storage_type',$type);
  828. };
  829. }
  830. }
  831. $express=StoragePutLog::where('user_id',$id)->where($where_type);
  832. $where_start=[];
  833. $start=$input['start_time'];
  834. if ($start){
  835. $where_start=function ($q)use($start){
  836. $q->where('created_at','>=',$start);
  837. };
  838. }
  839. $where_end=[];
  840. $end=$input['end_time'];
  841. if ($end){
  842. $where_end=function ($q)use($end){
  843. $q->where('created_at','<=',$end);
  844. };
  845. }
  846. $re=$express->where($where_start)->where($where_end)->groupBy('type')->get();
  847. foreach ($re as $k=>$v){
  848. $woman=0;
  849. $man=0;
  850. if ($type==1){
  851. $res=StoragePutLog::where('user_id',$id)->where('type',$v->type)->where('storage_type',$type)->where($where_start)->where($where_end);
  852. if ($input['status']==-1){
  853. $res=$res->groupBy('sex')->select(DB::raw('sum(num) as total'),'sex')->get()->toArray();
  854. }else{
  855. $res=$res->where('out_type',$input['status'])->groupBy('sex')->select(DB::raw('sum(num) as total'),'sex')->get()->toArray();
  856. }
  857. foreach ($res as $ke=>$va){
  858. if ($va['sex']==0){
  859. $woman=$va['total'];
  860. }else{
  861. $man=$va['total'];
  862. }
  863. }
  864. }else{
  865. $res=StoragePutLog::where('user_id',$id)->where('type',$v->type)->where('storage_type',$type)->where($where_start)->where($where_end)->groupBy('sex')->select(DB::raw('sum(num) as total'),'sex')->get()->toArray();
  866. foreach ($res as $ke=>$va){
  867. if ($va['sex']==0){
  868. $woman=$va['total'];
  869. }else{
  870. $man=$va['total'];
  871. }
  872. }
  873. }
  874. if ($v->type==0){
  875. $data['hard']['man']=$man;
  876. $data['hard']['woman']=$woman;
  877. }elseif ($v->type==1){
  878. $data['simple']['man']=$man;
  879. $data['simple']['woman']=$woman;
  880. }elseif ($v->type==2){
  881. $data['old']['man']=$man;
  882. $data['old']['woman']=$woman;
  883. }elseif ($v->type==3){
  884. $data['new_old']['man']=$man;
  885. $data['new_old']['woman']=$woman;
  886. }elseif ($v->type==4){
  887. $data['youth']['man']=$man;
  888. $data['youth']['woman']=$woman;
  889. }
  890. }
  891. return $this->success($data);
  892. }
  893. public function put_storage_details(Request $request){
  894. $input=$request->all();
  895. $id=Auth::user()->id;
  896. // $id=39516;
  897. $where_type=[];
  898. if ($input['record_type']){
  899. $type=$input['record_type'];
  900. $where_type=function ($q)use($type){
  901. $q->where('storage_type',$type);
  902. };
  903. }
  904. $express=StoragePutLog::where('user_id',$id)->where($where_type)->where('type',$input['type']);
  905. $where_start=[];
  906. $start=$input['start_time'];
  907. if ($start){
  908. $where_start=function ($q)use($start){
  909. $q->where('created_at','>=',$start);
  910. };
  911. }
  912. $where_end=[];
  913. $end=$input['end_time'];
  914. if ($end){
  915. $where_end=function ($q)use($end){
  916. $q->where('created_at','<=',$end);
  917. };
  918. }
  919. $re=$express->where($where_start)->where($where_end);
  920. $count=$re->count();
  921. $data=$re->take(20)->skip(($input['page_index']-1)*20)->get();
  922. return $this->success_list($data,'',$count);
  923. }
  924. /*
  925. * 按照时间分组
  926. * ***/
  927. public function get_group(Request $request){
  928. // $data=DB::select('SELECT SUM(foo), DATE(mydate) FROM a_table GROUP BY DATE(a_table.mydate)');
  929. $id=Auth::user()->id;
  930. $data=Express::where('user_id',$id)->selectRaw('DATE_FORMAT(created_at,"%Y-%m-%d") as date')
  931. ->groupBy('date')->orderByDesc('date');
  932. $count=$data->get()->count();
  933. $data=$data->take($request->page_size)->skip(($request->page_index-1)*$request->page_size)->get();
  934. $arr=[];
  935. foreach ($data as $k=>$v){
  936. $start_time=date('Y-m-d H:i:s',strtotime($v->date));
  937. $end_time=Carbon::parse($start_time)->endOfDay();
  938. $counts=Express::where('user_id',$id)->whereBetween('created_at',[$start_time,$end_time])->groupBy('order_id')->get()->count();
  939. $arr[$k]=$v;
  940. $arr[$k]['count']=$counts;
  941. }
  942. return $this->success_list($arr,'',$count);
  943. }
  944. public function get_detail(Request $request){
  945. $time=$request->time;
  946. $start_time=date('Y-m-d H:i:s',strtotime($time));
  947. $end_time=Carbon::parse($start_time)->endOfDay();
  948. $id=Auth::user()->id;
  949. $data=Express::where('user_id',$id)->whereBetween('created_at',[$start_time,$end_time])->with(['get_order'=>function($q)use($start_time,$end_time,$id){
  950. $q->with(['get_express'=>function($qu)use($start_time,$end_time){
  951. $qu->whereBetween('created_at',[$start_time,$end_time])->select('id','express_com','order_id','express_number','put_status','is_gift','notes','remark');
  952. },'address'])->where('user_id',$id)->select('order_num','put_status','id','address_id','user_id','good_user_id','track_status');
  953. }])->select('order_id','user_id','created_at')->groupBy('order_id')->get();
  954. $arr=[];
  955. foreach ($data as $k=>$v){
  956. $arr[$k]=$v;
  957. if ($v['get_order']){
  958. foreach ($v['get_order']['get_express'] as $ke=>$va){
  959. $arrs[$ke]=$va;
  960. $arrs[$ke]['user_id']=$v['get_order']['user_id'];
  961. $arrs[$ke]['good_user_id']=$v['get_order']['good_user_id'];
  962. if ($va['is_gift']!=1){
  963. $arrs[$ke]['notes']=null;
  964. }
  965. if (!$va['put_status']){
  966. $arrs[$ke]['put_status']=0;
  967. }
  968. }
  969. $arr[$k]['get_order']['get_express']=$arrs;
  970. }
  971. }
  972. return $this->success_list($arr);
  973. }
  974. }