1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\Express;
- use App\Models\ExpressStorage;
- use App\Models\Goodtest;
- use App\Models\Ordertest;
- use App\Models\StorageLog;
- use App\Models\StoragePutLog;
- use App\Models\UserStorage;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class StorageController extends Controller{
- /**查看货物**/
- public function look_good(Request $request){
- $id=$request->id;
- $ids=Auth::user()->id;
- // $ids=39516;
- $re=Express::where('order_id',$id)->with(['get_express'=>function($q)use($id){
- $q->where('order_id',$id);
- },'get_order'])->orderByDesc('number')->groupBy('number')->get();
- $data=[];
- //app下级循环无法获取上级is_per数据,添加下级is_per
- foreach ($re as $k=>$v){
- $data[$k]=$v;
- $data[$k]['get_order']['address']=$v->get_order->address;
- if ($ids==$v->get_order->good_user_id){
- $data[$k]['is_per']=true;
- $is_per=true;
- }else{
- $data[$k]['is_per']=false;
- $is_per=false;
- }
- $express=$v->get_express;
- foreach($express as $key=>$val){
- if (!$val->put_status){
- $express[$key]->put_status=0;
- }
- if($is_per){
- $express[$key]->is_per=true;
- }else{
- $express[$key]->is_per=false;
- }
- }
- $data[$k]->get_express=$express;
- }
- return $this->success_list($data);
- }
- /*
- * 货物详情
- * **/
- public function good_detail(Request $request){
- $re=ExpressStorage::where('express_id',$request->id)->groupBy('type')->get();
- $datas=Goodtest::where('is_deleted',0)->where('id','>',57)->groupBy('main_attr')->get();
- foreach ($datas as $k=>$v){
- $r[$v->main_attr]=$v->main_img;
- }
- $data=[
- 'hard'=>['spec'=>[],'imgurl'=>$r['大卫博士精装版'],'name'=>'大卫博士精装版'],
- 'simple'=>['spec'=>[],'imgurl'=>$r['大卫博士简约版'],'name'=>'大卫博士简约版'],
- 'old'=>['spec'=>[],'imgurl'=>$r['大卫博士高腰版'],'name'=>'大卫博士高腰版'],
- 'new_old'=>['spec'=>[],'imgurl'=>$r['大卫博士纯棉老人版'],'name'=>'大卫博士纯棉老人版'],
- 'youth'=>['spec'=>[],'imgurl'=>$r['大卫博士青春版'],'name'=>'大卫博士青春版'],
- ];
- foreach ($re as $k=>$v){
- $res=ExpressStorage::where('express_id',$request->id)->where('type',$v->type)->select('size','sex','type','num')->get()->toArray();
- if ($v->type==0){
- $data['hard']['spec']=$res;
- }elseif ($v->type==1){
- $data['simple']['spec']=$res;
- }elseif ($v->type==2){
- $data['old']['spec']=$res;
- }elseif(($v->type==3)){
- $data['new_old']['spec']=$res;
- }elseif(($v->type==4)){
- $data['youth']['spec']=$res;
- }
- }
- return $this->success($data);
- }
- /*
- * 我的库存
- * **/
- public function my_storage(){
- $id=Auth::user()->id;
- // $id=39516;
- $re=UserStorage::where('user_id',$id)->groupBy('type')->select(DB::raw('sum(num) as total'),'type')->get();
- $res=UserStorage::where('user_id',$id)->first();
- $data=['hard'=>0,'simple'=>0,'old'=>0,'new_old'=>0,'youth'=>0];
- foreach ($re as $k=>$v){
- if ($v->type==0){
- $data['hard']=$v->total;
- }elseif ($v->type==1){
- $data['simple']=$v->total;
- }elseif ($v->type==2){
- $data['old']=$v->total;
- }elseif($v->type==3){
- $data['new_old']=$v->total;
- }elseif($v->type==4){
- $data['youth']=$v->total;
- }
- }
- if ($res){
- $data['status']=false;
- }else{
- $data['status']=true;
- }
- return $this->success($data);
- }
- /*
- *库存详情
- * **/
- public function storage_detail(Request $request){
- $id=Auth::user()->id;
- // $id=39516;
- $re=UserStorage::where('user_id',$id)->groupBy('type')->get();
- $datas=Goodtest::where('is_deleted',0)->where('id','>',57)->groupBy('main_attr')->get();
- foreach ($datas as $k=>$v){
- $r[$v->main_attr]=$v->main_img;
- }
- $data=[
- 'hard'=>['spec'=>[],'imgurl'=>$r['大卫博士精装版'],'name'=>'大卫博士精装版'],
- 'simple'=>['spec'=>[],'imgurl'=>$r['大卫博士简约版'],'name'=>'大卫博士简约版'],
- 'old'=>['spec'=>[],'imgurl'=>$r['大卫博士高腰版'],'name'=>'大卫博士高腰版'],
- 'new_old'=>['spec'=>[],'imgurl'=>$r['大卫博士纯棉老人版'],'name'=>'大卫博士纯棉版'],
- 'youth'=>['spec'=>[],'imgurl'=>$r['大卫博士青春版'],'name'=>'大卫博士青春版'],
- ];
- $goodtest=new Goodtest();
- $storage_hard=$goodtest->storage_hard;
- foreach($storage_hard as $key=>$val){
- $storage_hard[$key]['num']=UserStorage::where('user_id',$id)->where('size',$val['size'])->where('sex',$val['sex'])->where('type',$val['type'])->value('num')??0;
- }
- $data['hard']['spec']=$storage_hard;
- $storage_simple=$goodtest->storage_simple;
- foreach($storage_simple as $key=>$val){
- $storage_simple[$key]['num']=UserStorage::where('user_id',$id)->where('size',$val['size'])->where('sex',$val['sex'])->where('type',$val['type'])->value('num')??0;
- }
- $data['simple']['spec']=$storage_simple;
- $storage_old=$goodtest->storage_old;
- foreach($storage_old as $key=>$val){
- $storage_old[$key]['num']=UserStorage::where('user_id',$id)->where('size',$val['size'])->where('sex',$val['sex'])->where('type',$val['type'])->value('num')??0;
- }
- $data['old']['spec']=$storage_old;
- $storage_new_old=$goodtest->storage_new_old;
- foreach($storage_new_old as $key=>$val){
- $storage_new_old[$key]['num']=UserStorage::where('user_id',$id)->where('size',$val['size'])->where('sex',$val['sex'])->where('type',$val['type'])->value('num')??0;
- }
- $data['new_old']['spec']=$storage_new_old;
- $storage_youth=$goodtest->storage_youth;
- foreach($storage_youth as $key=>$val){
- $storage_youth[$key]['num']=UserStorage::where('user_id',$id)->where('size',$val['size'])->where('sex',$val['sex'])->where('type',$val['type'])->value('num')??0;
- }
- $data['youth']['spec']=$storage_youth;
- // foreach ($re as $k=>$v){
- // $res=UserStorage::where('user_id',$id)->where('type',$v->type)->select('size','sex','type','num')->get();
- // if ($v->type==0){
- // $data['hard']['spec']=$res?$res:$goodtest->storage_hard;
- // }elseif ($v->type==1){
- // $data['simple']['spec']=$res?$res:$goodtest->storage_simple;
- // }elseif ($v->type==2){
- // $data['old']['spec']=$res?$res:$goodtest->storage_old;
- // }elseif($v->type==3){
- // $data['new_old']['spec']=$res?$res:$goodtest->storage_new_old;
- // }elseif($v->type==4){
- // $data['youth']['spec']=$res?$res:$goodtest->storage_youth;
- // }
- // }
- return $this->success($data);
- }
- /*
- *app库存详情
- * **/
- public function app_storage_detail(Request $request){
- $id=Auth::user()->id;
- // $id=39516;
- $re_total=UserStorage::where('user_id',$id)->groupBy('type')->select(DB::raw('sum(num) as total'),'type')->get();
- // $data=['hard'=>0,'simple'=>0,'old'=>0,'new_old'=>0,'youth'=>0];
- $num=0;$money=0;
- foreach ($re_total as $k=>$v){
- $num+=$v->total;
- if ($v->type==0){
- // $data['hard']=$v->total;
- $money+=$v->total* 149;
- }elseif ($v->type==1){
- // $data['simple']=$v->total;
- $money+=$v->total* 79;
- }elseif ($v->type==2){
- // $data['old']=$v->total;
- $money+=$v->total* 89;
- }elseif($v->type==3){
- // $data['new_old']=$v->total;
- $money+=$v->total* 47;
- }elseif($v->type==4){
- // $data['youth']=$v->total;
- $money+=$v->total* 89;
- }
- }
- $re=UserStorage::where('user_id',$id)->groupBy('type')->get();
- $datas=Goodtest::where('is_deleted',0)->where('id','>',57)->groupBy('main_attr')->get();
- foreach ($datas as $k=>$v){
- $r[$v->main_attr]=$v->main_img;
- }
- $data=[
- 'hard'=>['spec'=>[],'imgurl'=>$r['大卫博士精装版'],'name'=>'大卫博士精装版'],
- 'simple'=>['spec'=>[],'imgurl'=>$r['大卫博士简约版'],'name'=>'大卫博士简约版'],
- 'old'=>['spec'=>[],'imgurl'=>$r['大卫博士高腰版'],'name'=>'大卫博士高腰版'],
- 'new_old'=>['spec'=>[],'imgurl'=>$r['大卫博士纯棉老人版'],'name'=>'大卫博士纯棉版'],
- 'youth'=>['spec'=>[],'imgurl'=>$r['大卫博士青春版'],'name'=>'大卫博士青春版'],
- ];
- foreach ($re as $k=>$v){
- $res=UserStorage::where('user_id',$id)->where('type',$v->type)->select('size','sex','type','num')->get();
- if ($v->type==0){
- $data['hard']['spec']=$res;
- }elseif ($v->type==1){
- $data['simple']['spec']=$res;
- }elseif ($v->type==2){
- $data['old']['spec']=$res;
- }elseif($v->type==3){
- $data['new_old']['spec']=$res;
- }elseif($v->type==4){
- $data['youth']['spec']=$res;
- }
- }
- $data['all']['total']=$num;
- $data['all']['money']=$money;
- return $this->success($data);
- }
- /*
- * 待入库
- * ***/
- public function put_storage(Request $request){
- $input=$request->all();
- $id=Auth::user()->id;
- // $id=39516;
- $page_index=$input['page_index'];
- $data=Ordertest::where('user_id',$id)->where('good_user_id',$id)
- ->where('track_status','<>',0)
- ->where('created_at','>','2020-01-01 00:00:00')
- ->where('put_status',0)->select('id as order_id','created_at','number','order_num');
- $count=$data->count();
- $data=$data->take(20)->skip(($page_index-1)*20)->get();
- return $this->success_list($data,'',$count);
- }
- /*
- * 入库记录
- * ***/
- public function put_storage_group(Request $request){
- $input=$request->all();
- $id=Auth::user()->id;
- // $id=39516;
- $datas=Goodtest::where('is_deleted',0)->where('id','>',57)->groupBy('main_attr')->get();
- foreach ($datas as $k=>$v){
- $r[$v->main_attr]=$v->main_img;
- }
- $data=[
- 'hard'=>['imgurl'=>$r['大卫博士精装版'],'name'=>'大卫博士精装版','woman'=>0,'man'=>0],
- 'simple'=>['imgurl'=>$r['大卫博士简约版'],'name'=>'大卫博士简约版','woman'=>0,'man'=>0],
- 'old'=>['imgurl'=>$r['大卫博士高腰版'],'name'=>'大卫博士高腰版','woman'=>0,'man'=>0],
- 'new_old'=>['imgurl'=>$r['大卫博士纯棉老人版'],'name'=>'大卫博士纯棉老人版','woman'=>0,'man'=>0],
- 'youth'=>['imgurl'=>$r['大卫博士青春版'],'name'=>'大卫博士青春版','woman'=>0,'man'=>0],
- ];
- $where_type=[];
- $type=$input['record_type'];
- $status=$input['status'];
- if ($type){
- if ($type==1){
- if ($status==-1){
- $where_type=function ($q)use($type,$status){
- $q->where('storage_type',$type);
- };
- }else{
- $where_type=function ($q)use($type,$status){
- $q->where('storage_type',$type)->where('out_type',$status);
- };
- }
- }else{
- $where_type=function ($q)use($type,$status){
- $q->where('storage_type',$type);
- };
- }
- }
- $express=ExpressStorage::where('user_id',$id)->where($where_type);
- $where_start=[];
- $start=$input['start_time'];
- if ($start){
- $where_start=function ($q)use($start){
- $q->where('created_at','>=',$start);
- };
- }
- $where_end=[];
- $end=$input['end_time'];
- if ($end){
- $where_end=function ($q)use($end){
- $q->where('created_at','<=',$end);
- };
- }
- $re=$express->where($where_start)->where($where_end)->groupBy('type')->get();
- foreach ($re as $k=>$v){
- $woman=0;
- $man=0;
- if ($type==1){
- $res=ExpressStorage::where('user_id',$id)->where('type',$v->type)->where('storage_type',$type)->where($where_start)->where($where_end);
- if ($input['status']==-1){
- $res=$res->groupBy('sex')->select(DB::raw('sum(num) as total'),'sex')->get()->toArray();
- }else{
- $res=$res->where('out_type',$input['status'])->groupBy('sex')->select(DB::raw('sum(num) as total'),'sex')->get()->toArray();
- }
- foreach ($res as $ke=>$va){
- if ($va['sex']==0){
- $woman=$va['total'];
- }else{
- $man=$va['total'];
- }
- }
- }else{
- $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();
- foreach ($res as $ke=>$va){
- if ($va['sex']==0){
- $woman=$va['total'];
- }else{
- $man=$va['total'];
- }
- }
- }
- if ($v->type==0){
- $data['hard']['man']=$man;
- $data['hard']['woman']=$woman;
- }elseif ($v->type==1){
- $data['simple']['man']=$man;
- $data['simple']['woman']=$woman;
- }elseif ($v->type==2){
- $data['old']['man']=$man;
- $data['old']['woman']=$woman;
- }elseif($v->type==3){
- $data['new_old']['man']=$man;
- $data['new_old']['woman']=$woman;
- }elseif($v->type==4){
- $data['youth']['man']=$man;
- $data['youth']['woman']=$woman;
- }
- }
- return $this->success($data);
- }
- public function put_storage_detail(Request $request){
- $input=$request->all();
- $id=Auth::user()->id;
- // $id=39516;
- $where_type=[];
- $status=$input['status'];
- if ($input['record_type']){
- $type=$input['record_type'];
- if ($type==1){
- if ($status!=-1){
- $where_type=function ($q)use($type,$status){
- $q->where('storage_type',$type)->where('out_type',$status);
- };
- }else{
- $where_type=function ($q)use($type,$status){
- $q->where('storage_type',$type);
- };
- }
- }else{
- $where_type=function ($q)use($type,$status){
- $q->where('storage_type',$type);
- };
- }
- }
- $express=ExpressStorage::where('user_id',$id)->where($where_type)->where('type',$input['type']);
- $where_start=[];
- $start=$input['start_time'];
- if ($start){
- if ($input['record_type']==1){
- $where_start=function ($q)use($start){
- $q->where('out_time','>=',$start);
- };
- }else{
- $where_start=function ($q)use($start){
- $q->where('created_at','>=',$start);
- };
- }
- }
- $where_end=[];
- $end=$input['end_time'];
- if ($end){
- if ($input['record_type']==1){
- $where_end=function ($q)use($end){
- $q->where('out_time','<=',$end);
- };
- }else{
- $where_end=function ($q)use($end){
- $q->where('created_at','<=',$end);
- };
- }
- }
- $re=$express->where($where_start)->where($where_end);
- $count=$re->count();
- $data=$re->take(20)->skip(($input['page_index']-1)*20)->get();
- return $this->success_list($data,'',$count);
- }
- public function put_good_storage(Request $request){
- // return $this->error(50026,'','入库操作暂停使用!如有疑问请联系政委!');
- $input=$request->all();
- $id=Auth::user()->id;
- // $id=39516;
- $d=UserStorage::where('user_id',$id)->first();
- if (!$d){
- return $this->error(50026,'','请盘库!');
- }
- $express=Express::find($input['id']);
- if ($express->put_status==1){
- return $this->error(50026,'','该货物已经入库,请退出刷新');
- }
- DB::beginTransaction();
- try{
- //查找这一箱的货物量
- $re=ExpressStorage::where('express_id',$input['id'])->select('sex','size','type','num')->get();
- //查找这一箱的信息
- foreach ($re as $k=>$v){
- //查找库存中有没有这个商品的记录
- $res=UserStorage::where('user_id',$id)->where('sex',$v->sex)->where('size',$v->size)->where('type',$v->type)->first();
- //有则加
- if ($res){
- if ($v->type==1||$v->type==2){
- $num=$v->num*4;
- }elseif ($v->type==3){
- $num=$v->num*6;
- }else{
- $num=$v->num;
- }
- $res->num+=$num;
- if (!$res->save()){
- throw new \Exception(1);
- }
- }else{
- //没有则新建
- $storage=new UserStorage();
- if ($v->type==1||$v->type==2){
- $storage->num=$v->num*4;
- }elseif ($v->type==3){
- $storage->num=$v->num*6;
- }else{
- $storage->num=$v->num;
- }
- $storage->size=$v->size;
- $storage->user_id=$id;
- $storage->sex=$v->sex;
- $storage->type=$v->type;
- if (!$storage->save()){
- throw new \Exception('1');
- }
- }
- $log=new StoragePutLog();
- $log->size=$v->size;
- $log->sex=$v->sex;
- $log->type=$v->type;;
- $log->user_id=$id;
- if ($v->type==1||$v->type==2){
- $log->num=$v->num*4;
- }elseif ($v->type==3){
- $log->num=$v->num*6;
- }else{
- $log->num=$v->num;
- }
- if (!$log->save()){
- throw new \Exception(1);
- }
- }
- //更改这一箱的状态为入库
- ExpressStorage::where('express_id',$input['id'])->select('sex','size','type','num')->update(['sure_time'=>Carbon::now(),'storage_type'=>2]);
- //更改这一样的装填为已完成
- $express->put_status=1;
- $express->put_time=Carbon::now();
- if (!$express->save()){
- throw new \Exception('1');
- }
- //查找订单
- $order=Ordertest::find($express->order_id);
- if ($order->track_status==2){
- //查看一下这个订单是不是都已经入库
- $count=Express::where('order_id',$order->id)->where('put_status',0)->count();
- if ($count==0){
- //全部入库后更改 订单状态为已完成的状态
- $order->put_status=1;
- if (!$order->save()){
- throw new \Exception(1);
- }
- }
- }
- DB::commit();
- return $this->success([]);
- }catch (\Exception $exception){
- DB::rollBack();
- return $this->error(50021,'','入库出错:'.$exception);
- }
- }
- /*
- * 判断是不是第一次盘库
- * ***/
- public function get_goodimg(){
- $datas=Goodtest::where('is_deleted',0)->where('id','>',57)->groupBy('main_attr')->get();
- foreach ($datas as $k=>$v){
- $r[$v->main_attr]=$v->main_img;
- }
- $data=[
- 'hard'=>['imgurl'=>$r['大卫博士精装版'],'name'=>'大卫博士精装版'],
- 'simple'=>['imgurl'=>$r['大卫博士简约版'],'name'=>'大卫博士简约版'],
- 'old'=>['imgurl'=>$r['大卫博士高腰版'],'name'=>'大卫博士高腰版'],
- 'new_old'=>['imgurl'=>$r['大卫博士纯棉老人版'],'name'=>'大卫博士纯棉老人版'],
- 'youth'=>['imgurl'=>$r['大卫博士青春版'],'name'=>'大卫博士青春版'],
- ];
- return $this->success($data);
- }
- /**一次性盘库**/
- public function one_sure_storage(Request $request){
- $id=Auth::user()->id;
- $re=UserStorage::where('user_id',$id)->first();
- if ($re){
- return $this->error(50024,'','该用户不能使用一次性盘货功能');
- }
- $input=$request->all();
- DB::beginTransaction();
- try{
- foreach ($input as $k=>$v){
- if($k=='hard'){
- $type=0;
- }elseif ($k=='simple'){
- $type=1;
- }elseif ($k=='old'){
- $type=2;
- }elseif($k=='new_old'){
- $type=3;
- }elseif($k=='youth'){
- $type=4;
- }
- foreach ($v as $ke=>$va){
- $storage=new UserStorage();
- $storage->user_id=$id;
- $storage->type=$type;
- $storage->size=$va['size'];
- $storage->num=$va['num'];
- $storage->sex=$va['sex'];
- $st_log=new StorageLog();
- $st_log->user_id=$id;
- $st_log->type=$type;
- $st_log->size=$va['size'];
- $st_log->num=$va['num'];
- $st_log->sex=$va['sex'];
- $st_log->number=1;
- $st_log->old_num=0;
- if (!$st_log->save()){
- throw new \Exception('1');
- }
- if (!$storage->save()){
- throw new \Exception('1');
- }
- }
- }
- DB::commit();
- return $this->success([]);
- }catch (\Exception $exception){
- DB::rollBack();
- return $this->error(50021,'',$exception);
- }
- }
- /*
- * 获取库存信息
- * ***/
- public function get_storage(Request $request){
- // $id=39516;
- $id=Auth::user()->id;
- $re=UserStorage::where('user_id',$id)->with(['get_storage'=>function($q)use($id){
- $q->where('user_id',$id)->select('sex','size','type','num');
- }])->groupBy('type')->get();
- $data=[
- 'hard'=>[],
- 'simple'=>[],
- 'old'=>[],
- 'new_old'=>[],
- 'youth'=>[],
- ];
- foreach ($re as $k=>$v){
- if ($v->type==0){
- $data['hard']=$v->get_storage;
- }elseif ($v->type==1){
- $data['simple']=$v->get_storage;
- }elseif ($v->type==2){
- $data['old']=$v->get_storage;
- }elseif ($v->type==4){
- $data['youth']=$v->get_storage;
- }else{
- $data['new_old']=$v->get_storage;
- }
- }
- return $this->success($data);
- }
- /**一次性盘库**/
- public function sure_storage(Request $request){
- $id=Auth::user()->id;
- $re=UserStorage::where('user_id',$id)->first();
- if (!$re){
- return $this->error(50024,'','该用户需要使用一次性盘货功能');
- }
- $number=StorageLog::where('user_id',$id)->max('number');
- //适配app数据结构
- if($request->has('data')){
- $input=$request->input('data');
- }else{
- $input=$request->all();
- }
- Log::info($input);
- DB::beginTransaction();
- try{
- foreach ($input as $k=>$v){
- if($k=='hard'){
- $type=0;
- }elseif ($k=='simple'){
- $type=1;
- }elseif ($k=='old'){
- $type=2;
- }elseif ($k=='youth'){
- $type=4;
- }else{
- $type=3;
- }
- foreach ($v as $ke=>$va){
- $storage=UserStorage::where('type',$type)->where('user_id',$id)->where('size',$va['size'])->where('sex',$va['sex'])->first();
- $st_log=new StorageLog();
- $st_log->number=$number+1;
- $st_log->sure_time=Carbon::now()->toDateTimeString();
- if ($storage){
- $st_log->old_num=$storage->num;
- if ($va['num']>$storage->num){
- $put_st=new StoragePutLog();
- $put_st->user_id=$id;
- $put_st->type=$type;
- $put_st->size=$va['size'];
- $put_st->num=$va['num']-$storage->num;
- $put_st->sex=$va['sex'];
- $put_st->storage_type=0;
- if (!$put_st->save()){
- throw new \Exception('1');
- }
- }elseif ($va['num']<$storage->num){
- $out_st=new ExpressStorage();
- $out_st->user_id=$id;
- $out_st->type=$type;
- $out_st->size=$va['size'];
- $out_st->num=$storage->num-$va['num'];
- $out_st->sex=$va['sex'];
- $out_st->storage_type=1;
- $out_st->out_time=Carbon::now();
- if (!$out_st->save()){
- throw new \Exception('1');
- }
- }
- $storage->num=$va['num'];
- }else{
- $storage=new UserStorage();
- $storage->user_id=$id;
- $storage->type=$type;
- $storage->size=$va['size'];
- $storage->num=$va['num'];
- $storage->sex=$va['sex'];
- $st_log->old_num=0;
- }
- $st_log->user_id=$id;
- $st_log->type=$type;
- $st_log->size=$va['size'];
- $st_log->num=$va['num'];
- $st_log->sex=$va['sex'];
- if (!$storage->save()){
- throw new \Exception('1');
- }
- if (!$st_log->save()){
- throw new \Exception('1');
- }
- }
- }
- DB::commit();
- return $this->success([],'盘库成功','盘库成功');
- }catch (\Exception $exception){
- DB::rollBack();
- return $this->error(50021,'',$exception);
- }
- }
- /***出库**/
- public function out_storage(Request $request){
- $id=Auth::user()->id;
- $re=UserStorage::where('user_id',$id)->first();
- if (!$re){
- return $this->error(50024,'','该用户需要使用一次性盘货功能');
- }
- $input=$request->all();
- //app适配统一参数data
- if($request->has('arr')){
- $arr=$input['arr'];
- }elseif($request->has('data')){
- $arr=$input['data'];
- }else{
- $arr=[];
- }
- DB::beginTransaction();
- try{
- foreach ($arr as $k=>$v){ //$input['arr']
- if($k=='hard'){
- $type=0;
- }elseif ($k=='simple'){
- $type=1;
- }elseif ($k=='old'){
- $type=2;
- }elseif ($k=='youth'){
- $type=4;
- }else{
- $type=3;
- }
- foreach ($v as $ke=>$va){
- if ($va['num']!=0){
- $storage=UserStorage::where('type',$type)->where('user_id',$id)->where('size',$va['size'])->where('sex',$va['sex'])->first();
- if (!$storage){
- return $this->error(50023,'','没有该商品的库存,请先使用盘库功能');
- }
- if ($storage->num<$va['num']){
- return $this->error(50023,'','该商品的出库量大于库存量,请先使用盘库修改库存');
- }
- $storage->num-=$va['num'];
- if (!$storage->save()){
- throw new \Exception('1');
- }
- $eStorage=new ExpressStorage();
- $eStorage->num=$va['num'];
- $eStorage->user_id=$id;
- $eStorage->out_type=$input['type'];
- $eStorage->size=$va['size'];
- $eStorage->sex=$va['sex'];
- $eStorage->type=$type;
- $eStorage->storage_type=1;
- $eStorage->out_time=Carbon::now();
- if (!$eStorage->save()){
- throw new \Exception('1');
- }
- }
- }
- }
- DB::commit();
- return $this->success([],'出库成功','出库成功');
- }catch (\Exception $exception){
- DB::rollBack();
- return $this->error(50021,'',$exception);
- }
- }
- /*
- * 盘库记录
- **/
- public function sure_storage_group(Request $request){
- $input=$request->all();
- $id=Auth::user()->id;
- // $id=39516;
- $datas=Goodtest::where('is_deleted',0)->where('id','>',57)->groupBy('main_attr')->get();
- foreach ($datas as $k=>$v){
- $r[$v->main_attr]=$v->main_img;
- }
- $data=[
- 'hard'=>['imgurl'=>$r['大卫博士精装版'],'name'=>'大卫博士精装版','woman'=>0,'man'=>0],
- 'simple'=>['imgurl'=>$r['大卫博士简约版'],'name'=>'大卫博士简约版','woman'=>0,'man'=>0],
- 'old'=>['imgurl'=>$r['大卫博士高腰版'],'name'=>'大卫博士高腰版','woman'=>0,'man'=>0],
- 'new_old'=>['imgurl'=>$r['大卫博士纯棉老人版'],'name'=>'大卫博士纯棉老人版','woman'=>0,'man'=>0],
- 'youth'=>['imgurl'=>$r['大卫博士青春版'],'name'=>'大卫博士青春版','woman'=>0,'man'=>0],
- ];
- $storage_log=StorageLog::where('user_id',$id);
- $where_start=[];
- $start=$input['start_time'];
- if ($start){
- $where_start=function ($q)use($start){
- $q->where('created_at','>=',$start);
- };
- }
- $where_end=[];
- $end=$input['end_time'];
- if ($end){
- $where_end=function ($q)use($end){
- $q->where('created_at','<=',$end);
- };
- }
- $re=$storage_log->where($where_start)->where($where_end)->groupBy('type')->get();
- foreach ($re as $k=>$v){
- $woman=0;
- $man=0;
- $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();
- foreach ($res as $ke=>$va){
- if ($va['sex']==0){
- $woman=$va['total'];
- }else{
- $man=$va['total'];
- }
- }
- if ($v->type==0){
- $data['hard']['man']=$man;
- $data['hard']['woman']=$woman;
- }elseif ($v->type==1){
- $data['simple']['man']=$man;
- $data['simple']['woman']=$woman;
- }elseif ($v->type==2){
- $data['old']['man']=$man;
- $data['old']['woman']=$woman;
- }elseif ($v->type==3){
- $data['new_old']['man']=$man;
- $data['new_old']['woman']=$woman;
- }elseif ($v->type==4){
- $data['youth']['man']=$man;
- $data['youth']['woman']=$woman;
- }
- }
- return $this->success($data);
- }
- public function sure_storage_detail(Request $request){
- $input=$request->all();
- $id=Auth::user()->id;
- // $id=39516;
- $express=StorageLog::where('user_id',$id)->where('type',$input['type']);
- $where_start=[];
- $start=$input['start_time'];
- if ($start){
- $where_start=function ($q)use($start){
- $q->where('created_at','>=',$start);
- };
- }
- $where_end=[];
- $end=$input['end_time'];
- if ($end){
- $where_end=function ($q)use($end){
- $q->where('created_at','<=',$end);
- };
- }
- $re=$express->where($where_start)->where($where_end);
- $count=$re->count();
- $data=$re->take(20)->skip(($input['page_index']-1)*20)->get();
- return $this->success_list($data,'',$count);
- }
- /*
- *订货入库和退货入库
- ***/
- public function put_storages(Request $request){
- $id=Auth::user()->id;
- $re=UserStorage::where('user_id',$id)->first();
- if (!$re){
- return $this->error(50024,'','该用户需要使用一次性盘货功能');
- }
- $input=$request->all();
- $data=$input['data'];
- // $put_type=0;
- $put_type=$input['put_type'];
- DB::beginTransaction();
- try{
- foreach ($data as $k=>$v){
- if($k=='hard'){
- $type=0;
- }elseif ($k=='simple'){
- $type=1;
- }elseif ($k=='old'){
- $type=2;
- }elseif ($k=='youth'){
- $type=4;
- }else{
- $type=3;
- }
- foreach ($v as $ke=>$va){
- if ($va['num']!=0){
- $st_log=new StoragePutLog();
- $storage=UserStorage::where('type',$type)->where('user_id',$id)->where('size',$va['size'])->where('sex',$va['sex'])->first();
- if ($put_type==0){
- if ($type==2||$type==1 || $type==4){
- $va['num']=$va['num']*4;
- }elseif ($type==3){
- $va['num']=$va['num']*6;
- }
- }
- if ($storage){
- $storage->num+=$va['num'];
- }else{
- $storage=new UserStorage();
- $storage->user_id=$id;
- $storage->type=$type;
- $storage->size=$va['size'];
- $storage->num=$va['num'];
- $storage->sex=$va['sex'];
- }
- $st_log->user_id=$id;
- $st_log->type=$type;
- $st_log->size=$va['size'];
- $st_log->num=$va['num'];
- $st_log->sex=$va['sex'];
- $st_log->put_type=$put_type;
- $st_log->storage_type=0;
- if (!$storage->save()){
- throw new \Exception('1');
- }
- if (!$st_log->save()){
- throw new \Exception('1');
- }
- }
- }
- }
- DB::commit();
- return $this->success([],'入库成功','入库成功');
- }catch (\Exception $exception){
- DB::rollBack();
- return $this->error(50021,'',$exception);
- }
- }
- /*
- * 入库记录
- * ***/
- public function put_storage_groups(Request $request){
- $input=$request->all();
- $id=Auth::user()->id;
- // $id=39516;
- $datas=Goodtest::where('is_deleted',0)->where('id','>',57)->groupBy('main_attr')->get();
- foreach ($datas as $k=>$v){
- $r[$v->main_attr]=$v->main_img;
- }
- $data=[
- 'hard'=>['imgurl'=>$r['大卫博士精装版'],'name'=>'大卫博士精装版','woman'=>0,'man'=>0],
- 'simple'=>['imgurl'=>$r['大卫博士简约版'],'name'=>'大卫博士简约版','woman'=>0,'man'=>0],
- 'old'=>['imgurl'=>$r['大卫博士高腰版'],'name'=>'大卫博士高腰版','woman'=>0,'man'=>0],
- 'new_old'=>['imgurl'=>$r['大卫博士纯棉老人版'],'name'=>'大卫博士纯棉老人版','woman'=>0,'man'=>0],
- 'youth'=>['imgurl'=>$r['大卫博士青春版'],'name'=>'大卫博士青春版','woman'=>0,'man'=>0],
- ];
- $where_type=[];
- $type=$input['record_type'];
- if ($type){
- if ($type==1){
- $where_type=function ($q)use($type){
- $q->where('storage_type',$type);
- };
- }else{
- $where_type=function ($q)use($type){
- $q->where('storage_type',$type);
- };
- }
- }
- $express=StoragePutLog::where('user_id',$id)->where($where_type);
- $where_start=[];
- $start=$input['start_time'];
- if ($start){
- $where_start=function ($q)use($start){
- $q->where('created_at','>=',$start);
- };
- }
- $where_end=[];
- $end=$input['end_time'];
- if ($end){
- $where_end=function ($q)use($end){
- $q->where('created_at','<=',$end);
- };
- }
- $re=$express->where($where_start)->where($where_end)->groupBy('type')->get();
- foreach ($re as $k=>$v){
- $woman=0;
- $man=0;
- if ($type==1){
- $res=StoragePutLog::where('user_id',$id)->where('type',$v->type)->where('storage_type',$type)->where($where_start)->where($where_end);
- if ($input['status']==-1){
- $res=$res->groupBy('sex')->select(DB::raw('sum(num) as total'),'sex')->get()->toArray();
- }else{
- $res=$res->where('out_type',$input['status'])->groupBy('sex')->select(DB::raw('sum(num) as total'),'sex')->get()->toArray();
- }
- foreach ($res as $ke=>$va){
- if ($va['sex']==0){
- $woman=$va['total'];
- }else{
- $man=$va['total'];
- }
- }
- }else{
- $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();
- foreach ($res as $ke=>$va){
- if ($va['sex']==0){
- $woman=$va['total'];
- }else{
- $man=$va['total'];
- }
- }
- }
- if ($v->type==0){
- $data['hard']['man']=(int)$man;
- $data['hard']['woman']=(int)$woman;
- }elseif ($v->type==1){
- $data['simple']['man']=(int)$man;
- $data['simple']['woman']=(int)$woman;
- }elseif ($v->type==2){
- $data['old']['man']=(int)$man;
- $data['old']['woman']=(int)$woman;
- }elseif ($v->type==3){
- $data['new_old']['man']=(int)$man;
- $data['new_old']['woman']=(int)$woman;
- }elseif ($v->type==4){
- $data['youth']['man']=(int)$man;
- $data['youth']['woman']=(int)$woman;
- }
- }
- return $this->success($data);
- }
- public function put_storage_details(Request $request){
- $input=$request->all();
- $id=Auth::user()->id;
- // $id=39516;
- $where_type=[];
- if ($input['record_type']){
- $type=$input['record_type'];
- $where_type=function ($q)use($type){
- $q->where('storage_type',$type);
- };
- }
- $express=StoragePutLog::where('user_id',$id)->where($where_type)->where('type',$input['type']);
- $where_start=[];
- $start=$input['start_time'];
- if ($start){
- $where_start=function ($q)use($start){
- $q->where('created_at','>=',$start);
- };
- }
- $where_end=[];
- $end=$input['end_time'];
- if ($end){
- $where_end=function ($q)use($end){
- $q->where('created_at','<=',$end);
- };
- }
- $re=$express->where($where_start)->where($where_end);
- $count=$re->count();
- $data=$re->take(20)->skip(($input['page_index']-1)*20)->get();
- return $this->success_list($data,'',$count);
- }
- /*
- * 按照时间分组
- * ***/
- public function get_group(Request $request){
- // $data=DB::select('SELECT SUM(foo), DATE(mydate) FROM a_table GROUP BY DATE(a_table.mydate)');
- $id=Auth::user()->id;
- $data=Express::where('user_id',$id)->selectRaw('DATE_FORMAT(created_at,"%Y-%m-%d") as date')
- ->groupBy('date')->orderByDesc('date');
- $count=$data->get()->count();
- $data=$data->take($request->page_size)->skip(($request->page_index-1)*$request->page_size)->get();
- $arr=[];
- foreach ($data as $k=>$v){
- $start_time=date('Y-m-d H:i:s',strtotime($v->date));
- $end_time=Carbon::parse($start_time)->endOfDay();
- $counts=Express::where('user_id',$id)->whereBetween('created_at',[$start_time,$end_time])->groupBy('order_id')->get()->count();
- $arr[$k]=$v;
- $arr[$k]['count']=$counts;
- // $time=$v->date;
- // $start_time=date('Y-m-d H:i:s',strtotime($time));
- // $end_time=Carbon::parse($start_time)->endOfDay();
- // $id=Auth::user()->id;
- $data=Express::where('user_id',$id)->whereBetween('created_at',[$start_time,$end_time])->with(['get_order'=>function($q)use($start_time,$end_time,$id){
- $q->with(['get_express'=>function($qu)use($start_time,$end_time){
- $qu->whereBetween('created_at',[$start_time,$end_time])->select('id','express_com','order_id','express_number','put_status','is_gift','notes','remark');
- },'address'])->where('user_id',$id)->select('order_num','put_status','id','address_id','user_id','good_user_id','track_status');
- }])->select('order_id','user_id','created_at')->groupBy('order_id')->get();
- $array=[];
- foreach ($data as $key=>$val){
- $array[$key]=$val;
- if ($val['get_order']){
- $n=0;
- if ($id==$val['get_order']->good_user_id){
- $data[$key]['get_order']['is_per']=true;
- $is_per=true;
- }else{
- $data[$key]['get_order']['is_per']=false;
- $is_per=false;
- }
- foreach ($val['get_order']['get_express'] as $ke=>$va){
- $n++;
- $arrs[$ke]=$va;
- $arrs[$ke]['count']=$n;
- $arrs[$ke]['user_id']=$val['get_order']['user_id'];
- $arrs[$ke]['good_user_id']=$val['get_order']['good_user_id'];
- if ($va['is_gift']!=1){
- $arrs[$ke]['notes']=null;
- }
- if (!$va['put_status']){
- $arrs[$ke]['put_status']=0;
- }
- if($is_per){
- $arrs[$ke]['is_per']=true;
- }else{
- $arrs[$ke]['is_per']=false;
- }
- }
- $array[$key]['get_order']['get_express']=$arrs;
- }
- }
- $arr[$k]['detail']=$array;
- }
- return $this->success_list($arr,'',$count);
- }
- public function get_detail(Request $request){
- $time=$request->time;
- $start_time=date('Y-m-d H:i:s',strtotime($time));
- $end_time=Carbon::parse($start_time)->endOfDay();
- $id=Auth::user()->id;
- $data=Express::where('user_id',$id)->whereBetween('created_at',[$start_time,$end_time])->with(['get_order'=>function($q)use($start_time,$end_time,$id){
- $q->with(['get_express'=>function($qu)use($start_time,$end_time){
- $qu->whereBetween('created_at',[$start_time,$end_time])->select('id','express_com','order_id','express_number','put_status','is_gift','notes','remark');
- },'address'])->where('user_id',$id)->select('order_num','put_status','id','address_id','user_id','good_user_id','track_status');
- }])->select('order_id','user_id','created_at')->groupBy('order_id')->get();
- $arr=[];
- foreach ($data as $k=>$v){
- $arr[$k]=$v;
- if ($v['get_order']){
- foreach ($v['get_order']['get_express'] as $ke=>$va){
- $arrs[$ke]=$va;
- $arrs[$ke]['user_id']=$v['get_order']['user_id'];
- $arrs[$ke]['good_user_id']=$v['get_order']['good_user_id'];
- if ($va['is_gift']!=1){
- $arrs[$ke]['notes']=null;
- }
- if (!$va['put_status']){
- $arrs[$ke]['put_status']=0;
- }
- }
- $arr[$k]['get_order']['get_express']=$arrs;
- }
- }
- return $this->success_list($arr);
- }
- }
|