CustomerChangeController.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. <?php
  2. namespace Modules\Service\Http\Controllers;
  3. use Modules\Service\Events\CustomerSendSmsEvent;
  4. use App\Handlers\ExpressHandler;
  5. use App\Imports\ExpressImport;
  6. use App\Models\ApplyCustomer;
  7. use App\Models\ApplyCustomerDetail;
  8. use App\Models\Express;
  9. use App\Models\ProblemCollection;
  10. use App\Models\StoreHouseLog;
  11. use App\Models\StoreHouseStorage;
  12. use App\Models\User;
  13. use App\Models\Warea;
  14. use Carbon\Carbon;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Auth;
  17. use Illuminate\Support\Facades\Cache;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Log;
  20. use Maatwebsite\Excel\Facades\Excel;
  21. class CustomerChangeController extends Controller
  22. {
  23. /***
  24. * _ooOoo_
  25. * o8888888o
  26. * 88" . "88
  27. * (| -_- |)
  28. * O\ = /O
  29. * ___/`---'\____
  30. * . ' \\| |// `.
  31. * / \\||| : |||// \
  32. * / _||||| -:- |||||- \
  33. * | | \\\ - /// | |
  34. * | \_| ''\---/'' | |
  35. * \ .-\__ `-` ___/-. /
  36. * ___`. .' /--.--\ `. . __
  37. * ."" '< `.___\_<|>_/___.' >'"".
  38. * | | : `- \`.;`\ _ /`;.`/ - ` : | |
  39. * \ \ `-. \_ __\ /__ _/ .-` / /
  40. * ======`-.____`-.___\_____/___.-`____.-'======
  41. * `=---='
  42. * .............................................
  43. * 佛曰:bug泛滥,我已瘫痪!
  44. */
  45. public function GetCrown(Request $request){
  46. $user=User::where('id',$request->input('id'))->with(['agent:id,nickname,agent_id,level,warea_id'])->with(['warea'])->select('id','agent_id','level','nickname','warea_id')->first();
  47. $data=[];
  48. if ($user->level==3){
  49. $data['nickname']=$user->nickname;
  50. $data['id']=$user->id;
  51. $data['warea_name']=$user->warea->name;
  52. }else{
  53. if ($user->agent->level==3){
  54. $data['nickname']=$user->agent->nickname;
  55. $data['id']=$user->agent->id;
  56. $data['warea_name']=Warea::where('id',$user->agent->warea_id)->value('name');
  57. }else{
  58. $user=User::where('id',$user->agent->agent_id)->with(['warea'])->select('id','agent_id','level','nickname','warea_id')->first();
  59. $data['nickname']=$user->nickname;
  60. $data['id']=$user->id;
  61. $data['warea_name']=Warea::where('id',$user->warea_id)->value('name');
  62. }
  63. }
  64. return $this->success($data);
  65. }
  66. /**客户申请表**/
  67. public function GetApplyList(Request $request){
  68. $page_index=$request->input('page_index',1);
  69. $page_size=$request->input('size',15);
  70. $num=($page_index-1)*$page_size;
  71. $search_name=$request->input('search_name');
  72. $customer_name=$request->input('customer_name');
  73. $warea_name=$request->input('warea_name');
  74. $apply_status=$request->input('apply_status');
  75. $audit_status=$request->input('audit_status');
  76. $is_finish=$request->input('is_finish');
  77. $start_time=$request->input('start_time');
  78. $end_time=$request->input('end_time');
  79. $apply_number=$request->input('apply_number')??null;
  80. $re=ApplyCustomer::query();
  81. if (Auth::user()->role_id==23){
  82. $warea_names=Warea::where('admin_id',Auth::user()->id)->value('name');
  83. $re->where('warea_name',$warea_names);
  84. }
  85. if (Auth::user()->role_id==29){
  86. $re->whereIn('apply_status',[2,1]);
  87. $re->where('is_owe',$request->input('is_owe')??0);
  88. }else{
  89. if ($request->has('is_owe')){
  90. if (is_numeric($request->input('is_owe'))){
  91. $re->where('is_owe',$request->input('is_owe'));
  92. }
  93. }
  94. if (is_numeric($apply_status)){
  95. $re->where('apply_status',$apply_status);
  96. }
  97. if ($start_time){
  98. $re->whereBetween('created_at',[$start_time,$end_time]);
  99. }
  100. }
  101. if (is_numeric($is_finish)){
  102. $re->where('is_finish',$is_finish);
  103. }
  104. if ($apply_number){
  105. $re->where('apply_number',$apply_number);
  106. }
  107. if (is_numeric($audit_status)){
  108. $re->where('audit_status',$audit_status);
  109. }
  110. if ($warea_name){
  111. $re->where('warea_name',$warea_name);
  112. }
  113. if ($search_name){
  114. $re->where('apply_user_nickname','like','%'.$search_name.'%');
  115. }
  116. if ($customer_name){
  117. $re->where('customer_name','like','%'.$customer_name.'%')->orWhere('customer_phone','like','%'.$customer_name.'%');
  118. }
  119. $count=$re->count();
  120. $data=$re->with(['applydetail'])->orderByDesc('id')->take($page_size)->skip($num)->get();
  121. return $this->success_list($data,'',$count);
  122. }
  123. /*客户申请**/
  124. public function Apply(Request $request,ApplyCustomer $applyCustomer,ApplyCustomerDetail $applyCustomerDetail){
  125. $applyUserID=$request->input('apply_user_id');
  126. $applyUserName=$request->input('apply_user_nickname');
  127. $applyCrownID=$request->input('apply_crown_id');
  128. $applyCrownName=$request->input('apply_crown_nickname');
  129. $warea_name=$request->input('warea_name');
  130. $customer_name=$request->input('customer_name');
  131. $customer_phone=$request->input('customer_phone');
  132. $province=$request->input('province');
  133. $city=$request->input('city');
  134. $area=$request->input('area');
  135. $address=$request->input('address');
  136. $total=$request->input('total');
  137. $remark=$request->input('remark');
  138. $wear_time=$request->input('wear_time');;
  139. $is_agent=$request->input('is_agent');;
  140. $problemList=$request->input('problemList');
  141. $sendback_info='';
  142. $apply_number=$this->CreateApplyNumber();
  143. $op_name=Auth::user()->name;
  144. $applyCustomer->apply_user_id=$applyUserID;
  145. $applyCustomer->apply_user_nickname=$applyUserName;
  146. $applyCustomer->apply_crown_id=$applyCrownID;
  147. $applyCustomer->apply_crown_nickname=$applyCrownName;
  148. $applyCustomer->warea_name=$warea_name;
  149. $applyCustomer->customer_name=$customer_name;
  150. $applyCustomer->customer_phone=$customer_phone;
  151. $applyCustomer->province=$province;
  152. $applyCustomer->city=$city;
  153. $applyCustomer->area=$area;
  154. $applyCustomer->address=$address;
  155. $applyCustomer->total=$total;
  156. $applyCustomer->is_agent=$is_agent;
  157. $applyCustomer->wear_time=$wear_time;
  158. $applyCustomer->remark=$remark;
  159. $applyCustomer->apply_number=$apply_number;
  160. $applyCustomer->op_name=$op_name;
  161. DB::beginTransaction();
  162. try {
  163. if (!$applyCustomer->save()){
  164. throw new \Exception('1');
  165. }
  166. foreach ($problemList as $k=>$v){
  167. $re=StoreHouseStorage::where('good_name',$v['real_good_name'])->where('size',$v['real_good_size'])->first();
  168. if (!$re){
  169. return $this->error();
  170. }
  171. $res=StoreHouseStorage::where('id',$re->id)->lockForUpdate()->first();
  172. if ($res->storage<$v['total']){
  173. throw new \Exception('商品:'.$res->good_name.'的'.$res->size.'库存不足');
  174. }
  175. $res->storage-=$v['total'];
  176. if (!$res->save()){
  177. throw new \Exception('商品保存出错');
  178. }
  179. $logs=new StoreHouseLog();
  180. $log=Auth::user()->name.'调换了商品:'.$res->good_name.',尺码:'.$res->size.'。数量为:'.$v['total'];
  181. $logs->log=$log;
  182. $logs->customer_id=$applyCustomer->id;
  183. if (!$logs->save()){
  184. throw new \Exception('1');
  185. }
  186. $applyCustomerDetail=new ApplyCustomerDetail();
  187. $applyCustomerDetail->customer_id=$applyCustomer->id;
  188. $applyCustomerDetail->problem_info=$v['problem'];
  189. $applyCustomerDetail->good_name=$v['good_name'];
  190. $applyCustomerDetail->good_size=$v['good_size'];
  191. $applyCustomerDetail->total=$v['total'];
  192. $applyCustomerDetail->real_change_size=$v['real_good_size'];
  193. $applyCustomerDetail->real_change_good=$v['real_good_name'];
  194. $applyCustomerDetail->problem_attr=$v['problem_attr'];
  195. $applyCustomerDetail->label=$v['label'];
  196. if ($v['ImgList']){
  197. $applyCustomerDetail->ImgList=json_encode($v['ImgList']);
  198. }
  199. if (!$applyCustomerDetail->save()){
  200. throw new \Exception('1');
  201. }
  202. }
  203. DB::commit();
  204. return $this->success([]);
  205. }catch (\Exception $exception){
  206. DB::rollBack();
  207. return $this->error(520014,$exception->getMessage());
  208. }
  209. }
  210. /**修改申请单**/
  211. public function UpdateApply(Request $request){
  212. $id=$request->input('id');
  213. $applyUserID=$request->input('apply_user_id');
  214. $applyUserName=$request->input('apply_user_nickname');
  215. $applyCrownID=$request->input('apply_crown_id');
  216. $applyCrownName=$request->input('apply_crown_nickname');
  217. $warea_name=$request->input('warea_name');
  218. $customer_name=$request->input('customer_name');
  219. $customer_phone=$request->input('customer_phone');
  220. $province=$request->input('province');
  221. $city=$request->input('city');
  222. $area=$request->input('area');
  223. $address=$request->input('address');
  224. $total=$request->input('total');
  225. $remark=$request->input('remark');
  226. // $wear_time=$request->input('wear_time');
  227. $is_agent=$request->input('is_agent');;
  228. $wear_time=$request->input('wear_time')??null;
  229. $NewproblemList=$request->input('NewproblemList');
  230. $UpdateproblemList=$request->input('UpdateproblemList');
  231. $DelproblemList=$request->input('DelproblemList')??[];
  232. $sendback_info='';
  233. $apply_number=$this->CreateApplyNumber();
  234. $op_name=Auth::user()->name;
  235. $applyCustomer=ApplyCustomer::where('id',$id)->first();
  236. if ($applyCustomer->audit_status==1){
  237. return $this->error(500213,'请联系审核人员退回后修改');
  238. }
  239. $applyCustomer->apply_user_id=$applyUserID;
  240. $applyCustomer->apply_user_nickname=$applyUserName;
  241. $applyCustomer->apply_crown_id=$applyCrownID;
  242. $applyCustomer->apply_crown_nickname=$applyCrownName;
  243. $applyCustomer->warea_name=$warea_name;
  244. $applyCustomer->customer_name=$customer_name;
  245. $applyCustomer->customer_phone=$customer_phone;
  246. $applyCustomer->province=$province;
  247. $applyCustomer->city=$city;
  248. $applyCustomer->area=$area;
  249. $applyCustomer->address=$address;
  250. $applyCustomer->total=$total;
  251. $applyCustomer->is_agent=$is_agent;
  252. $applyCustomer->remark=$remark;
  253. $applyCustomer->apply_number=$apply_number;
  254. $applyCustomer->op_name=$op_name;
  255. $applyCustomer->wear_time=$wear_time;
  256. DB::beginTransaction();
  257. try {
  258. if (!$applyCustomer->save()){
  259. throw new \Exception('1');
  260. }
  261. /**新增的**/
  262. foreach ($NewproblemList as $k=>$v){
  263. $applyCustomerDetail=new ApplyCustomerDetail();
  264. $applyCustomerDetail->customer_id=$applyCustomer->id;
  265. $applyCustomerDetail->problem_info=$v['problem'];
  266. $applyCustomerDetail->good_name=$v['good_name'];
  267. $applyCustomerDetail->good_size=$v['good_size'];
  268. $applyCustomerDetail->total=$v['total'];
  269. $applyCustomerDetail->real_change_size=$v['real_good_size'];
  270. $applyCustomerDetail->real_change_good=$v['real_good_name'];
  271. $applyCustomerDetail->problem_attr=$v['problem_attr'];
  272. $applyCustomerDetail->label=$v['label'];
  273. if ($v['ImgList']){
  274. $applyCustomerDetail->ImgList=json_encode($v['ImgList']);
  275. }
  276. if (!$applyCustomerDetail->save()){
  277. throw new \Exception('1');
  278. }
  279. $re=StoreHouseStorage::where('good_name',$v['real_good_name'])->where('size',$v['real_good_size'])->first();
  280. if (!$re){
  281. return $this->error();
  282. }
  283. $res=StoreHouseStorage::where('id',$re->id)->lockForUpdate()->first();
  284. if ($res->storage<$v['total']){
  285. throw new \Exception('商品:'.$res->good_name.'的'.$res->size.'库存不足');
  286. }
  287. $res->storage-=$v['total'];
  288. if (!$res->save()){
  289. throw new \Exception('商品保存出错');
  290. }
  291. $logs=new StoreHouseLog();
  292. $log=Auth::user()->name.'调换了商品:'.$res->good_name.',尺码:'.$res->size.'。数量为:'.$v['total'];
  293. $logs->log=$log;
  294. $logs->customer_id=$applyCustomer->id;
  295. if (!$logs->save()){
  296. throw new \Exception('1');
  297. }
  298. }
  299. /**修改的*/
  300. foreach ($UpdateproblemList as $k=>$v){
  301. $applyCustomerDetail=ApplyCustomerDetail::where('id',$v['id'])->first();
  302. if ($applyCustomerDetail->total<$v['total']){
  303. $num=$v['total']-$applyCustomerDetail->total;
  304. $re=StoreHouseStorage::where('good_name',$v['real_good_name'])->where('size',$v['real_good_size'])->first();
  305. if (!$re){
  306. return $this->error();
  307. }
  308. $res=StoreHouseStorage::where('id',$re->id)->lockForUpdate()->first();
  309. if ($res->storage<$num){
  310. throw new \Exception('商品:'.$res->good_name.'的'.$res->size.'库存不足');
  311. }
  312. $res->storage-=$num;
  313. if (!$res->save()){
  314. throw new \Exception('商品保存出错');
  315. }
  316. }
  317. else{
  318. $num=$applyCustomerDetail->total-$v['total'];
  319. $re=StoreHouseStorage::where('good_name',$v['real_good_name'])->where('size',$v['real_good_size'])->first();
  320. if (!$re){
  321. return $this->error();
  322. }
  323. $res=StoreHouseStorage::where('id',$re->id)->lockForUpdate()->first();
  324. // if ($res->storage<$num){
  325. // throw new \Exception('商品:'.$res->good_name.'的库存不足');
  326. // }
  327. $res->storage+=$num;
  328. if (!$res->save()){
  329. throw new \Exception('商品保存出错');
  330. }
  331. }
  332. $applyCustomerDetail->problem_info=$v['problem'];
  333. $applyCustomerDetail->good_name=$v['good_name'];
  334. $applyCustomerDetail->good_size=$v['good_size'];
  335. $applyCustomerDetail->total=$v['total'];
  336. $applyCustomerDetail->real_change_size=$v['real_good_size'];
  337. $applyCustomerDetail->real_change_good=$v['real_good_name'];
  338. $applyCustomerDetail->problem_attr=$v['problem_attr'];
  339. $applyCustomerDetail->label=$v['label'];
  340. if ($v['ImgList']){
  341. $applyCustomerDetail->ImgList=json_encode($v['ImgList']);
  342. }
  343. if (!$applyCustomerDetail->save()){
  344. throw new \Exception('1');
  345. }
  346. }
  347. /**删除的*/
  348. foreach ($DelproblemList as $k=>$v){
  349. $applyCustomerDetail=ApplyCustomerDetail::find($v);
  350. $re=StoreHouseStorage::where('good_name',$applyCustomerDetail->real_change_good)->where('size',$applyCustomerDetail->real_change_size)->first();
  351. if (!$re){
  352. return $this->error();
  353. }
  354. $res=StoreHouseStorage::where('id',$re->id)->lockForUpdate()->first();
  355. // if ($res->storage<$num){
  356. // throw new \Exception('商品:'.$res->good_name.'的库存不足');
  357. // }
  358. $res->storage+=$applyCustomerDetail->total;
  359. if (!$res->save()){
  360. throw new \Exception('商品保存出错');
  361. }
  362. $applyCustomerDetail->delete();
  363. }
  364. DB::commit();
  365. return $this->success([]);
  366. }catch (\Exception $exception){
  367. DB::rollBack();
  368. return $this->error(520014,$exception->getMessage());
  369. }
  370. }
  371. /**判断数量**/
  372. public function CheckNum(Request $request){
  373. $re=StoreHouseStorage::where('good_name',$request->input('real_change_good'))->where('size',$request->input('real_change_size'))
  374. ->lockForUpdate()
  375. ->first();
  376. if (!$re){
  377. return $this->error(500214,'没有此商品');
  378. }
  379. if ($re->storage<$request->input('total')){
  380. return $this->error(500214,'更换的商品库存不足');
  381. }
  382. return $this->success([]);
  383. }
  384. /*客户退换申请单号**/
  385. public function CreateApplyNumber(){
  386. $id=Auth::user()->id;
  387. $str='DWBS-CE-';
  388. $string=$str.$id.substr(time(),6,9).rand(1111,9999);
  389. return $string;
  390. }
  391. /**特殊尺码的库存信息**/
  392. public function StoreHouseStorage(Request $request){
  393. // $page_index=$request->input('page_index',1);
  394. // $page_size=$request->input('size',15);
  395. // $num=($page_index-1)*$page_size;
  396. // $search_name=$request->input('search_name');
  397. $re=StoreHouseStorage::query();
  398. $count=$re->count();
  399. $re=$re->get();
  400. return $this->success_list($re,'',$count);
  401. }
  402. /**更新库存**/
  403. public function UpdateStoreHouseStorage(Request $request){
  404. $id=$request->input('id');
  405. $storage=$request->input('storage');
  406. $re=StoreHouseStorage::where('id',$id)->lockForUpdate()->first();
  407. $num=$re->storage;
  408. $re->storage=$storage;
  409. $logs=new StoreHouseLog();
  410. $log=Auth::user()->name.'更新了商品:'.$re->good_name.',尺码:'.$re->size.'。更改前数量为:'.$num.',更新后数量为:'.$storage;
  411. $logs->log=$log;
  412. $logs->type=1;
  413. DB::beginTransaction();
  414. try {
  415. if ( !$re->save()){
  416. throw new \Exception('1');
  417. }
  418. if (!$logs->save()){
  419. throw new \Exception('1');
  420. }
  421. DB::commit();
  422. return $this->success([]);
  423. }catch (\Exception $exception){
  424. DB::rollBack();
  425. return $this->error(500214,$exception->getMessage());
  426. }
  427. }
  428. /**更新库存**/
  429. public function AddStoreHouseStorage(Request $request){
  430. $storage=$request->input('storage');
  431. $good_name=$request->input('good_name');
  432. $size=$request->input('size');
  433. $re=new StoreHouseStorage();
  434. $re->storage=$storage;
  435. $re->good_name=$good_name;
  436. $re->size=$size;
  437. $logs=new StoreHouseLog();
  438. $log=Auth::user()->name.'增加了商品:'.$good_name.',尺码:'.$size.'。数量为:'.$storage;
  439. $logs->log=$log;
  440. $logs->type=1;
  441. DB::beginTransaction();
  442. try {
  443. if ( !$re->save()){
  444. throw new \Exception('1');
  445. }
  446. if (!$logs->save()){
  447. throw new \Exception('1');
  448. }
  449. DB::commit();
  450. return $this->success([]);
  451. }catch (\Exception $exception){
  452. DB::rollBack();
  453. return $this->error(500214,$exception->getMessage());
  454. }
  455. }
  456. /**上传订单号**/
  457. public function UploadCustomerInfo(Request $request){
  458. $file = $request->file('file');
  459. if ( !$file->isValid()) { //判断文件是否有效
  460. return redirect()
  461. ->back()
  462. ->withErrors('文件上传失败,请重新上传');
  463. }
  464. $array = Excel::toArray(new ExpressImport, request()->file('file'));
  465. if(count($array)>5001){
  466. return $this->error('450001',"单次最多导入5000条数据");
  467. }
  468. $one = array_shift($array[0]);
  469. $express_num_keys=array_search('快递单号',$one);
  470. if(!$express_num_keys && !is_numeric($express_num_keys)){
  471. return $this->error('450001',"表头无'快递单号'字段");
  472. }
  473. $order_num_keys=array_search('申请单编号',$one);
  474. if(!$order_num_keys && !is_numeric($order_num_keys)){
  475. return $this->error('450001',"表头无'申请单编号'字段");
  476. }
  477. $express_com_keys=array_search('快递公司',$one);
  478. if(!$express_com_keys){
  479. return $this->error('450001',"表头无'快递公司'字段");
  480. }
  481. $express_code_keys=array_search('快递公司代码',$one);
  482. if(!$express_code_keys){
  483. return $this->error('450001',"表头无'快递公司代码'字段");
  484. }
  485. $arr=$array[0];
  486. $express_nums=array_column($arr,$express_num_keys);
  487. $unique_express_nums=array_unique($express_nums);
  488. $que_express_nums=array_diff_assoc($express_nums,$unique_express_nums);
  489. if(count($que_express_nums)>0){
  490. $que_express_str=implode(',',$que_express_nums);
  491. return $this->error('450001','快递单号'.$que_express_str.'重复出现');
  492. }
  493. $new_arr=[];
  494. $new_arrs=[];
  495. $z=0;
  496. foreach ($array[0] as $k=>$v){
  497. $st='';
  498. if ($v[$express_com_keys]=='中通快递'){
  499. $patterns = "/\d+/";
  500. preg_match_all($patterns,$v[$express_num_keys],$aa);
  501. foreach ($aa[0] as $kee=>$vaa){
  502. $st.=$vaa;
  503. }
  504. $new_arr[$z]=$st;
  505. }else{
  506. $new_arr[$z]=$v[$express_num_keys];
  507. }
  508. $new_arrs[$z]=$v[$order_num_keys];
  509. $z++;
  510. }
  511. $res=ApplyCustomer::whereIn('express_num',$new_arr)->pluck('express_num')->toArray();
  512. if (count($res)>0){
  513. $ar_r=array_intersect($new_arr,$res);
  514. $que_express_str=implode(',',$ar_r);
  515. return $this->error('450001','快递单号:'.$que_express_str.'已经上传',$ar_r);
  516. }
  517. $apply_arr=ApplyCustomer::whereIn('apply_number',$new_arrs)->whereNotNull('express_num')->pluck('apply_number')->toArray();
  518. if ($apply_arr){
  519. return $this->error(500214,implode(',',$apply_arr).'已经上传过快递单号,请核实数据');
  520. }
  521. DB::beginTransaction();
  522. try {
  523. foreach ($arr as $k=>$v){
  524. $apply_number=$v[$order_num_keys];
  525. $apply_info=ApplyCustomer::where('apply_number',$apply_number)->first();
  526. if (!$apply_info){
  527. return $this->error(20014,'申请单号:'.$apply_number.'不存在');
  528. }
  529. $st='';
  530. if ($v[$express_com_keys]=='中通快递'){
  531. $patterns = "/\d+/";
  532. preg_match_all($patterns,$v[$express_num_keys],$aa);
  533. foreach ($aa[0] as $kee=>$vaa){
  534. $st.=$vaa;
  535. }
  536. $apply_info->express_num=$st;
  537. }else{
  538. $apply_info->express_num=$v[$express_num_keys];
  539. }
  540. $apply_info->express_com=$v[$express_com_keys];
  541. $apply_info->express_code=$v[$express_code_keys];
  542. $apply_info->express_time=Carbon::now();
  543. $apply_info->apply_status=2;
  544. $apply_info->is_owe=0;
  545. if (!$apply_info->save()){
  546. throw new \Exception(1);
  547. }
  548. }
  549. DB::commit();
  550. /**发送 快递信息*/
  551. foreach ($arr as $k=>$v){
  552. $apply_info=ApplyCustomer::where('apply_number',$v[$order_num_keys])->first();
  553. $data=[
  554. 'mobile'=>$apply_info->customer_phone,
  555. 'express_com'=>$apply_info->express_com,
  556. 'express_num'=>$apply_info->express_num,
  557. 'status'=>0
  558. ];
  559. event(new CustomerSendSmsEvent($data));
  560. }
  561. return $this->success([]);
  562. }catch (\Exception $exception){
  563. DB::rollBack();
  564. return $this->error(500214,$exception->getMessage());
  565. }
  566. }
  567. /**上传单个物流信息**/
  568. public function Express(Request $request){
  569. $re=ApplyCustomer::find($request->input('id'));
  570. $re->express_num=$request->input('express_number');
  571. $re->express_time=Carbon::now();
  572. $re->apply_status=2;
  573. $re->is_owe=0;
  574. $re->express_com=$request->input('express_name');
  575. $re->express_code=$request->input('express_code');
  576. $data=[
  577. 'mobile'=>$re->customer_phone,
  578. 'express_com'=>$re->express_com,
  579. 'express_num'=>$re->express_num,
  580. 'status'=>0
  581. ];
  582. event(new CustomerSendSmsEvent($data));
  583. $re->save();
  584. return $this->success([]);
  585. }
  586. /**上传订单号**/
  587. public function UpdateCustomerInfo(Request $request){
  588. $file = $request->file('file');
  589. if ( !$file->isValid()) { //判断文件是否有效
  590. return redirect()
  591. ->back()
  592. ->withErrors('文件上传失败,请重新上传');
  593. }
  594. $array = Excel::toArray(new ExpressImport, request()->file('file'));
  595. if(count($array)>5001){
  596. return $this->error('450001',"单次最多导入5000条数据");
  597. }
  598. $one = array_shift($array[0]);
  599. $express_num_keys=array_search('旧快递单号',$one);
  600. if(!$express_num_keys && !is_numeric($express_num_keys)){
  601. return $this->error('450001',"表头无'旧快递单号'字段");
  602. }
  603. $new_express_num_keys=array_search('新快递单号',$one);
  604. if(!$express_num_keys && !is_numeric($express_num_keys)){
  605. return $this->error('450001',"表头无'新快递单号'字段");
  606. }
  607. $express_com_keys=array_search('快递公司',$one);
  608. if(!$express_com_keys){
  609. return $this->error('450001',"表头无'快递公司'字段");
  610. }
  611. $express_code_keys=array_search('快递公司代码',$one);
  612. if(!$express_code_keys){
  613. return $this->error('450001',"表头无'快递公司代码'字段");
  614. }
  615. $arr=$array[0];
  616. $express_nums=array_column($arr,$express_num_keys);
  617. $unique_express_nums=array_unique($express_nums);
  618. $que_express_nums=array_diff_assoc($express_nums,$unique_express_nums);
  619. if(count($que_express_nums)>0){
  620. $que_express_str=implode(',',$que_express_nums);
  621. return $this->error('450001','快递单号'.$que_express_str.'重复出现');
  622. }
  623. DB::beginTransaction();
  624. try {
  625. foreach ($arr as $k=>$v){
  626. $apply_info=ApplyCustomer::where('express_num',$v[$express_num_keys])->first();
  627. if (!$apply_info){
  628. return $this->error(20014,'申请单号:'.$v[$express_num_keys].'不存在');
  629. }
  630. $st='';
  631. if ($v[$express_com_keys]=='中通快递'){
  632. $patterns = "/\d+/";
  633. preg_match_all($patterns,$v[$new_express_num_keys],$aa);
  634. foreach ($aa[0] as $kee=>$vaa){
  635. $st.=$vaa;
  636. }
  637. $apply_info->express_num=$st;
  638. }else{
  639. $apply_info->express_num=$v[$new_express_num_keys];
  640. }
  641. $apply_info->express_com=$v[$express_com_keys];
  642. $apply_info->express_code=$v[$express_code_keys];
  643. $apply_info->express_time=Carbon::now();
  644. $apply_info->apply_status=2;
  645. $apply_info->is_owe=0;
  646. if (!$apply_info->save()){
  647. throw new \Exception(1);
  648. }
  649. }
  650. DB::commit();
  651. /**发送 快递信息*/
  652. foreach ($arr as $k=>$v){
  653. $apply_info=ApplyCustomer::where('express_num',$v[$new_express_num_keys])->first();
  654. $data=[
  655. 'mobile'=>$apply_info->customer_phone,
  656. 'express_com'=>$apply_info->express_com,
  657. 'express_num'=>$apply_info->express_num,
  658. 'status'=>1,
  659. 'old_express_num'=>$v[$express_num_keys]
  660. ];
  661. try {
  662. event(new CustomerSendSmsEvent($data));
  663. }catch (\Exception $exception){
  664. }
  665. }
  666. return $this->success([]);
  667. }catch (\Exception $exception){
  668. DB::rollBack();
  669. return $this->error(500214,$exception->getMessage());
  670. }
  671. }
  672. /**添加退货原因**/
  673. public function AddProblemCollection(Request $request,ProblemCollection $problemCollection){
  674. $problem=$request->input('problem');
  675. $problem_id=$request->input('problem_id',0);
  676. $problemCollection->problem=$problem;
  677. $problemCollection->type=1;
  678. $problemCollection->problem_id=$problem_id;
  679. $problemCollection->save();
  680. return $this->success([]);
  681. }
  682. public function GetProblemCollection(Request $request){
  683. $type=$request->input('type')??1;
  684. $re=ProblemCollection::where('type',$type)->with(['children'=>function($q){
  685. $q->select('problem as label','id as value','problem_id');
  686. }])->where('problem_id',0)->select('problem as label','id as value','problem_id')->orderByDesc('created_at')->get();
  687. return $this->success($re);
  688. }
  689. public function GetGoodNum(){
  690. $re=StoreHouseStorage::groupBy('good_name')->with(['GetGood'])->select('*')->get();
  691. return $this->success($re);
  692. }
  693. /**添加退货原因**/
  694. public function AddLabelCollection(Request $request,ProblemCollection $problemCollection){
  695. $problem=$request->input('label');
  696. $problemCollection->problem=$problem;
  697. $problemCollection->type=2;
  698. $problemCollection->save();
  699. return $this->success([]);
  700. }
  701. public function GetLabelCollection(Request $request){
  702. $type=$request->input('type')??2;
  703. $re=ProblemCollection::where('type',$type)->where('type',$type)->select('problem as label','id')->orderByDesc('created_at')->get();
  704. return $this->success($re);
  705. }
  706. /**问题统计分析**/
  707. public function ProblemAnalyse(Request $request){
  708. $start_time=$request->input('start_time');
  709. $end_time=$request->input('end_time');
  710. $re=ApplyCustomerDetail::query();
  711. if ($start_time && $end_time){
  712. $re->whereBetween('created_at',[$start_time ,$end_time]);
  713. }
  714. $re=$re->groupBy('problem_info')->select('problem_info',DB::raw('sum(total) as total'))->get();
  715. return $this->success($re);
  716. }
  717. /**战区调换数量统计**/
  718. public function WareaProblemAnalyse(Request $request){
  719. $start_time=$request->input('start_time');
  720. $end_time=$request->input('end_time');
  721. $re=ApplyCustomer::query();
  722. if ($start_time && $end_time){
  723. $re->whereBetween('created_at',[$start_time ,$end_time]);
  724. }
  725. $re=$re->groupBy('warea_name')->select('warea_name',DB::raw('sum(total) as total'))->get();
  726. return $this->success($re);
  727. }
  728. /**代理公司调换数量统计**/
  729. public function CrownProblemAnalyse(Request $request){
  730. $start_time=$request->input('start_time');
  731. $end_time=$request->input('end_time');
  732. $re=ApplyCustomer::query();
  733. if ($start_time && $end_time){
  734. $re->whereBetween('created_at',[$start_time ,$end_time]);
  735. }
  736. $re=$re->groupBy('apply_crown_id')->select('apply_crown_nickname','apply_crown_id',DB::raw('sum(total) as total'))->get();
  737. return $this->success($re);
  738. }
  739. /**战区调换数量统计**/
  740. public function WareaProblemAnalyseOnlyOne(Request $request){
  741. $start_time=$request->input('start_time');
  742. $end_time=$request->input('end_time');
  743. $warea_name=$request->input('warea_name');
  744. $re=ApplyCustomerDetail::query();
  745. $ids=ApplyCustomer::where('warea_name',$warea_name);
  746. if ($start_time && $end_time){
  747. $ids->whereBetween('created_at',[$start_time ,$end_time]);
  748. }
  749. $ids=$ids->pluck('id');
  750. $re=$re->whereIn('customer_id',$ids)->groupBy('problem_info')->select('problem_info',DB::raw('sum(total) as total'))->get();
  751. return $this->success($re);
  752. }
  753. /**代理公司调换数量统计**/
  754. public function CrownProblemAnalyseOnlyOne(Request $request){
  755. $start_time=$request->input('start_time');
  756. $end_time=$request->input('end_time');
  757. $search_id=$request->input('search_id');
  758. $re=ApplyCustomerDetail::query();
  759. $ids=ApplyCustomer::where('apply_crown_id',$search_id);
  760. if ($start_time && $end_time){
  761. $ids->whereBetween('created_at',[$start_time ,$end_time]);
  762. }
  763. $ids=$ids->pluck('id');
  764. $re=$re->whereIn('customer_id',$ids)->groupBy('problem_info')->select('problem_info',DB::raw('sum(total) as total'))->get();
  765. return $this->success($re);
  766. }
  767. /**获取物流轨迹**/
  768. public function ExpressSearch(Request $request,ExpressHandler $expressHandler){
  769. $express=ApplyCustomer::find($request->id);
  770. if (!$express){
  771. return $this->error(40011,'','该物流订单不存在');
  772. }
  773. if ($express->express_status==1){
  774. return $this->success(json_decode($express->express_data));
  775. }else{
  776. $re=$expressHandler->search($express->express_num,$express->express_code);
  777. $re=(array)$re;
  778. if ($re['State']==3){
  779. DB::beginTransaction();
  780. try{
  781. $express->express_data=json_encode($re);
  782. $express->express_status=1;
  783. $express->save();
  784. DB::commit();
  785. }catch (\Exception $exception){
  786. Log::error('-------物流信息保存出错--------'.$exception);
  787. DB::rollBack();
  788. }
  789. }
  790. return $this->success($re);
  791. }
  792. }
  793. /*导出申请单**/
  794. // public function ExportApplyList(Request $request){
  795. // $search_name=$request->input('search_name');
  796. // $customer_name=$request->input('customer_name');
  797. // $warea_name=$request->input('warea_name');
  798. // $apply_status=$request->input('apply_status');
  799. // $is_finish=$request->input('is_finish');
  800. // $re=ApplyCustomer::query();
  801. //
  802. // if (is_numeric($apply_status)){
  803. // $re->where('apply_status',$apply_status);
  804. // }
  805. // if (is_numeric($is_finish)){
  806. // $re->where('is_finish',$is_finish);
  807. // }
  808. //
  809. // if ($warea_name){
  810. // $re->where('warea_name',$warea_name);
  811. // }
  812. // if ($search_name){
  813. // $re->where('apply_user_nickname','like',"$search_name");
  814. // }
  815. // if ($customer_name){
  816. // $re->where('customer_name','like',"$customer_name")->orWhere('customer_phone','like',"$customer_name");
  817. // }
  818. // $data=$re->where('audit_status',1)->with(['applydetail'])->orderByDesc('id')->get();
  819. // return $this->success($data);
  820. // }
  821. /**毛毛审核**/
  822. public function AuditApplyList(Request $request){
  823. $ids=$request->input('id');
  824. ApplyCustomer::whereIn('id',$ids)->update(['audit_status'=>1]);
  825. return $this->success([]);
  826. }
  827. /*退回审核**/
  828. public function AuditBackApply(Request $request){
  829. $id=$request->input('id');
  830. ApplyCustomer::where('id',$id)->update(['audit_status'=>0]);
  831. return $this->success([]);
  832. }
  833. /*导出申请单**/
  834. public function ExportApplyLists(Request $request){
  835. $search_name=$request->input('search_name');
  836. $customer_name=$request->input('customer_name');
  837. $warea_name=$request->input('warea_name');
  838. $apply_status=$request->input('apply_status');
  839. $is_finish=$request->input('is_finish');
  840. $start_time=$request->input('start_time')??null;
  841. $end_time=$request->input('end_time');
  842. $re=ApplyCustomer::query();
  843. if (is_numeric($apply_status)){
  844. $re->where('apply_status',$apply_status);
  845. }
  846. if (is_numeric($is_finish)){
  847. $re->where('is_finish',$is_finish);
  848. }
  849. if ($warea_name){
  850. $re->where('warea_name',$warea_name);
  851. }
  852. if ($search_name){
  853. $re->where('apply_user_nickname','like',"$search_name");
  854. }
  855. if ($customer_name){
  856. $re->where('customer_name','like',"$customer_name")->orWhere('customer_phone','like',"$customer_name");
  857. }
  858. if (Auth::user()->role_id==23){
  859. $warea_names=Warea::where('admin_id',Auth::user()->id)->value('name');
  860. $re->where('warea_name',$warea_names);
  861. }
  862. if (Auth::user()->role_id==29){
  863. $re->where('audit_status',1);
  864. $re->where('is_owe',0);
  865. }else{
  866. if ($request->has('is_owe')){
  867. if (is_numeric($request->input('is_owe'))){
  868. $re->where('is_owe',$request->input('is_owe'));
  869. }
  870. }
  871. $audit_status=$request->input('audit_status');
  872. if (is_numeric($audit_status)){
  873. $re->where('audit_status',$audit_status);
  874. }
  875. if ($start_time){
  876. $re->whereBetween('created_at',[$start_time,$end_time]);
  877. }
  878. }
  879. /**只能导出已经审核的申请表**/
  880. $data=$re->with(['applydetail'=>function($q){
  881. $q->select('id','real_change_good','real_change_size','total as totals','customer_id','problem_info');
  882. }])->orderByDesc('id')->get();
  883. $arr=[
  884. '男款-L'=>'男L',
  885. '男款-XL'=>'男XL',
  886. '男款-XXL'=>'男XXL',
  887. '男款-3XL'=>'男3XL',
  888. '男款-4XL'=>'男4XL',
  889. '女款-M'=>'女M',
  890. '女款-L'=>'女L',
  891. '女款-XL'=>'女XL',
  892. '女款-XXL'=>'女XXL',
  893. '女款-3XL'=>'女3XL',
  894. '女款-4XL'=>'女4XL',
  895. '男款-5XL'=>'男5XL'
  896. ];
  897. // $a=["L" => null,"XL" => null,"XXL" => null, "XXXL" => null, "女M" => null, "女L" => null, "女XL" => null, "女XXL" => null, "女XXXL" => null, "女4XL" => null, "5XL" => null];
  898. // $arrs=array_keys($arr);
  899. $res=[];
  900. foreach ($data as $k=>$v){
  901. $res[$k]['调换单号']=$v->apply_number;
  902. $res[$k]['政委']=$v->op_name;
  903. $res[$k]['代理公司']=$v->apply_crown_nickname;
  904. $res[$k]['申请代理']=$v->apply_user_nickname;
  905. $res[$k]['收货人']=$v->customer_name;
  906. $res[$k]['收货手机号']=$v->customer_phone;
  907. $res[$k]['省份']=$v->province;
  908. $res[$k]['城市']=$v->city;
  909. $res[$k]['地区']=$v->area;
  910. $res[$k]['详细地址']=$v->address;
  911. $res[$k]['总数量']=$v->total;
  912. $res[$k]['穿着时间']=$v->wear_time;
  913. $res[$k]['登记时间']=$v->created_at->toDateTimeString();
  914. $res[$k]['调换原因']='';
  915. $res[$k]['男款']=0;
  916. $res[$k]['女款']=0;
  917. $res[$k]['男L']='';
  918. $res[$k]['男XL']='';
  919. $res[$k]['男XXL']='';
  920. $res[$k]['男3XL']='';
  921. $res[$k]['男4XL']='';
  922. $res[$k]['女M']='';
  923. $res[$k]['女L']='';
  924. $res[$k]['女XL']='';
  925. $res[$k]['女XXL']='';
  926. $res[$k]['女3XL']='';
  927. $res[$k]['女4XL']='';
  928. $res[$k]['男5XL']='';
  929. $res[$k]['备注']=$v->remark??'调';
  930. $res[$k]['款式']='';
  931. $arrs=[];
  932. foreach ($v->applydetail as $ke=>$va){
  933. if ($ke==0){
  934. $res[$k]['款式']=$va->real_change_good;
  935. }else{
  936. $good=$va->real_change_good;
  937. if ($good!=$res[$k]['款式']){
  938. $res[$k]['款式']='杂';
  939. }
  940. }
  941. if (isset($arrs[$va->real_change_good])){
  942. $size_arr=array_column($arrs[$va->real_change_good],'size');
  943. if (in_array($va->real_change_size,$size_arr)){
  944. foreach ($arrs[$va->real_change_good] as $kes=>$vals){
  945. if ($vals['size']==$va->real_change_size){
  946. $arrs[$va->real_change_good][$kes]['total']+=$va->totals;
  947. }
  948. }
  949. }else{
  950. $arrs[$va->real_change_good][]=['total'=>$va->totals,'size'=>$va->real_change_size,'customer_id'=>$va->customer_id];
  951. }
  952. }else{
  953. $arrs[$va->real_change_good][]=['total'=>$va->totals,'size'=>$va->real_change_size,'customer_id'=>$va->customer_id];
  954. }
  955. $sex=strpos($va->real_change_size,'男');
  956. if (is_numeric($sex)){
  957. $res[$k]['男款']+=$va->totals;
  958. }else{
  959. $res[$k]['女款']+=$va->totals;
  960. }
  961. $res[$k]['调换原因'].=$va->problem_info.',';
  962. }
  963. $res[$k]['arrs']=$arrs;
  964. foreach ($arrs as $ke=>$vas){
  965. $nu=strpos($ke,'棉');
  966. foreach ($vas as $kes=>$val){
  967. if (is_numeric($nu)){
  968. $res[$k][$arr[$val['size']]].=$val['total'].'条棉';
  969. }else{
  970. $res[$k][$arr[$val['size']]].=$val['total'].'条'.mb_substr($ke,0,1);
  971. }
  972. }
  973. }
  974. }
  975. return $this->success($res);
  976. }
  977. /**异常统计*/
  978. public function GetExceptionList(Request $request){
  979. $start_time=$request->input('start_time');
  980. $end_time=$request->input('end_time');
  981. $page_index=$request->input('page_index');
  982. $page_size=$request->input('page_size');
  983. $type=$request->input('type')??1;
  984. $num=($page_index-1)*$page_size;
  985. $re=ApplyCustomer::query();
  986. if ($start_time){
  987. $re-> whereBetween('created_at',[$start_time,$end_time]);
  988. }
  989. if (Auth::user()->role_id==23){
  990. $warea_names=Warea::where('admin_id',Auth::user()->id)->value('name');
  991. $re->where('warea_name',$warea_names);
  992. }
  993. if ($type==0){
  994. $re=$re->groupBy('customer_phone')
  995. ->select('*',DB::raw('sum(total) as totals'))
  996. ->having('totals','>=',2);
  997. $res=clone $re;
  998. $res=$res->get();
  999. $count=count($res);
  1000. }else{
  1001. $re->whereNotNull('express_time')->where('express_status',0)
  1002. ->where('express_time','<',Carbon::now()->addDays(-4))
  1003. ->select('*');
  1004. $count=$re->count();
  1005. }
  1006. $list=$re->take($page_size)->skip($num)->get();
  1007. return $this->success_list($list,'',$count);
  1008. }
  1009. public function GetExceptionExpressList(Request $request){
  1010. $start_time=$request->input('start_time');
  1011. $end_time=$request->input('end_time');
  1012. $page_index=$request->input('page_index');
  1013. $page_size=$request->input('page_size');
  1014. $num=($page_index-1)*$page_size;
  1015. $re=ApplyCustomer::query();
  1016. if ($start_time){
  1017. $re-> whereBetween('created_at',[$start_time,$end_time]);
  1018. }
  1019. if (Auth::user()->role_id==23){
  1020. $warea_names=Warea::where('admin_id',Auth::user()->id)->value('name');
  1021. $re->where('warea_name',$warea_names);
  1022. }
  1023. $re=$re->whereNotNull('express_time')->where('express_status',0)
  1024. ->where('express_time','<',Carbon::now()->addDays(-4))->groupBy('customer_phone')
  1025. ->select('*');
  1026. $count=$re->count();
  1027. $list=$re->take($page_size)->skip($num)->get();
  1028. return $this->success_list($list,'',$count);
  1029. }
  1030. /**更新状态**/
  1031. public function UpdateApplyInfos(Request $request){
  1032. $file = $request->file('file');
  1033. if ( !$file->isValid()) { //判断文件是否有效
  1034. return redirect()
  1035. ->back()
  1036. ->withErrors('文件上传失败,请重新上传');
  1037. }
  1038. $array = Excel::toArray(new ExpressImport, request()->file('file'));
  1039. if(count($array)>10001){
  1040. return $this->error('450001',"单次最多导入10000条数据");
  1041. }
  1042. $one = array_shift($array[0]);
  1043. $express_num_keys=array_search('快递单号',$one);
  1044. if(!$express_num_keys && !is_numeric($express_num_keys)){
  1045. return $this->error('450001',"表头无'快递单号'字段");
  1046. }
  1047. $arr=array_column($array[0],$express_num_keys);
  1048. }
  1049. /**标记为欠货**/
  1050. public function TagOwe(Request $request){
  1051. $ids=$request->input('id');
  1052. ApplyCustomer::whereIn('id',$ids)->update(['is_owe'=>1]);
  1053. return $this->success([]);
  1054. }
  1055. /**取消标记为欠货**/
  1056. public function CancelTagOwe(Request $request){
  1057. $ids=$request->input('id');
  1058. ApplyCustomer::whereIn('id',$ids)->update(['is_owe'=>0]);
  1059. return $this->success([]);
  1060. }
  1061. /**校验是否有重复的*/
  1062. public function CheckRepeatInfo(Request $request){
  1063. $customer_phone=$request->input('customer_phone');
  1064. $customer_name=$request->input('customer_name');
  1065. $time=Carbon::now()->addDays(-6);
  1066. $count=ApplyCustomer::where('customer_phone',$customer_phone)
  1067. ->where('customer_name',$customer_name)
  1068. ->where('created_at','>',$time)
  1069. ->count();
  1070. if ($count>0){
  1071. return $this->success(['status'=>1]);
  1072. }
  1073. return $this->success(['status'=>0]);
  1074. }
  1075. /*
  1076. * 删除申请
  1077. * **/
  1078. public function DelApplyCustomer(Request $request){
  1079. $id=$request->input('id');
  1080. // $CustomerApply=ApplyCustomer::find($id);
  1081. $ApplyDetail=ApplyCustomerDetail::where('customer_id',$id)->get();
  1082. foreach ($ApplyDetail as $k=>$v){
  1083. $re=StoreHouseStorage::where('good_name',$v->real_change_good)
  1084. ->where('size',$v->real_change_size)
  1085. ->lockForUpdate()
  1086. ->first();
  1087. $re->storage+=$v->total;
  1088. $re->save();
  1089. ApplyCustomerDetail::where('id',$v->id)->delete();
  1090. }
  1091. ApplyCustomer::where('id',$id)->delete();
  1092. return $this->success([]);
  1093. }
  1094. /*
  1095. * 获取该代理下所有的退货人信息
  1096. * **/
  1097. public function GetCustomerInfo(Request $request){
  1098. $user_id=$request->input('user_id');
  1099. $re=ApplyCustomer::where('apply_user_id',$user_id)->with(['applydetail'])->get();
  1100. return $this->success($re);
  1101. }
  1102. /*
  1103. * 手动签收
  1104. * ***/
  1105. public function Receipt(Request $request){
  1106. $re=ApplyCustomer::where('id',$request->input('id'))->first();
  1107. $re->express_status=1;
  1108. $re->remark=$re->remark.'**'.Auth::user()->name.'主动签收了改退货单';
  1109. $re->save();
  1110. return $this->success([]);
  1111. }
  1112. public function SearchUser(Request $request){
  1113. $search_name=$request->input('search_name');
  1114. $warea_id=$request->input('warea_id')??null;
  1115. $user=User::whereNull('deleted_at');
  1116. if ($warea_id){
  1117. $user->where('warea_id',$warea_id);
  1118. }
  1119. $user=$user->where(function ($q)use($search_name){
  1120. $q->where('nickname','like',"%$search_name%")->orwhere('mobile','like',"%$search_name%")->orwhere('remark_name','like',"%$search_name%");
  1121. })->with(['warea'])->select('id','nickname','remark_name','mobile','level','headimgurl','warea_id')->get();
  1122. return $this->success($user);
  1123. }
  1124. public function ExportApplyListss(Request $request){
  1125. $search_name=$request->input('search_name');
  1126. $customer_name=$request->input('customer_name');
  1127. $warea_name=$request->input('warea_name');
  1128. $apply_status=$request->input('apply_status');
  1129. $is_finish=$request->input('is_finish');
  1130. $start_time=$request->input('start_time')??null;
  1131. $end_time=$request->input('end_time');
  1132. $re=ApplyCustomer::query();
  1133. if (is_numeric($apply_status)){
  1134. $re->where('apply_status',$apply_status);
  1135. }
  1136. if (is_numeric($is_finish)){
  1137. $re->where('is_finish',$is_finish);
  1138. }
  1139. if ($warea_name){
  1140. $re->where('warea_name',$warea_name);
  1141. }
  1142. if ($search_name){
  1143. $re->where('apply_user_nickname','like',"$search_name");
  1144. }
  1145. if ($customer_name){
  1146. $re->where('customer_name','like',"$customer_name")->orWhere('customer_phone','like',"$customer_name");
  1147. }
  1148. if ($start_time){
  1149. $re->whereBetween('created_at',[$start_time,$end_time]);
  1150. }
  1151. /**只能导出已经审核的申请表**/
  1152. $data=$re->with(['applydetail'=>function($q){
  1153. $q->select('id','real_change_good','real_change_size','total as totals','customer_id','problem_info');
  1154. },'applydetails'=>function($q){
  1155. $q->select('id','real_change_good','real_change_size','total as totals','customer_id','problem_info')->groupBy('real_change_good');
  1156. }])->orderByDesc('id')->get();
  1157. $arr=[
  1158. '男款-L'=>'男L',
  1159. '男款-XL'=>'男XL',
  1160. '男款-XXL'=>'男XXL',
  1161. '男款-3XL'=>'男3XL',
  1162. '男款-4XL'=>'男4XL',
  1163. '女款-M'=>'女M',
  1164. '女款-L'=>'女L',
  1165. '女款-XL'=>'女XL',
  1166. '女款-XXL'=>'女XXL',
  1167. '女款-3XL'=>'女3XL',
  1168. '女款-4XL'=>'女4XL',
  1169. '男款-5XL'=>'男5XL'
  1170. ];
  1171. // $a=["L" => null,"XL" => null,"XXL" => null, "XXXL" => null, "女M" => null, "女L" => null, "女XL" => null, "女XXL" => null, "女XXXL" => null, "女4XL" => null, "5XL" => null];
  1172. // $arrs=array_keys($arr);
  1173. $res=[];
  1174. foreach ($data as $k=>$v){
  1175. $res[$k]['调换单号']=$v->apply_number;
  1176. $res[$k]['政委']=$v->op_name;
  1177. $res[$k]['代理公司']=$v->apply_crown_nickname;
  1178. $res[$k]['申请代理']=$v->apply_user_nickname;
  1179. $res[$k]['收货人']=$v->customer_name;
  1180. $res[$k]['收货手机号']=$v->customer_phone;
  1181. $res[$k]['省份']=$v->province;
  1182. $res[$k]['城市']=$v->city;
  1183. $res[$k]['地区']=$v->area;
  1184. $res[$k]['详细地址']=$v->address;
  1185. $res[$k]['总数量']=$v->total;
  1186. $res[$k]['穿着时间']=$v->wear_time;
  1187. $res[$k]['登记时间']=$v->created_at->toDateTimeString();
  1188. $res[$k]['调换原因']='';
  1189. $res[$k]['男款']=0;
  1190. $res[$k]['女款']=0;
  1191. $res[$k]['男L']='';
  1192. $res[$k]['男XL']='';
  1193. $res[$k]['男XXL']='';
  1194. $res[$k]['男3XL']='';
  1195. $res[$k]['男4XL']='';
  1196. $res[$k]['女M']='';
  1197. $res[$k]['女L']='';
  1198. $res[$k]['女XL']='';
  1199. $res[$k]['女XXL']='';
  1200. $res[$k]['女3XL']='';
  1201. $res[$k]['女4XL']='';
  1202. $res[$k]['男5XL']='';
  1203. $res[$k]['备注']=$v->remark??'调';
  1204. $res[$k]['款式']='';
  1205. $arrs=[];
  1206. foreach ($v->applydetail as $ke=>$va){
  1207. if ($ke==0){
  1208. $res[$k]['款式']=$va->real_change_good;
  1209. }else{
  1210. $good=$va->real_change_good;
  1211. if ($good!=$res[$k]['款式']){
  1212. $res[$k]['款式']='杂';
  1213. }
  1214. }
  1215. if (isset($arrs[$va->real_change_good])){
  1216. $size_arr=array_column($arrs[$va->real_change_good],'size');
  1217. if (in_array($va->real_change_size,$size_arr)){
  1218. foreach ($arrs[$va->real_change_good] as $kes=>$vals){
  1219. if ($vals['size']==$va->real_change_size){
  1220. $arrs[$va->real_change_good][$kes]['total']+=$va->totals;
  1221. }
  1222. }
  1223. }else{
  1224. $arrs[$va->real_change_good][]=['total'=>$va->totals,'size'=>$va->real_change_size,'customer_id'=>$va->customer_id];
  1225. }
  1226. }else{
  1227. $arrs[$va->real_change_good][]=['total'=>$va->totals,'size'=>$va->real_change_size,'customer_id'=>$va->customer_id];
  1228. }
  1229. dump($arrs);
  1230. // $arrs[$va->real_change_good]=['total'=>$va->total,''];
  1231. $sex=strpos($va->real_change_size,'男');
  1232. if (is_numeric($sex)){
  1233. $res[$k]['男款']+=$va->totals;
  1234. }else{
  1235. $res[$k]['女款']+=$va->totals;
  1236. }
  1237. $res[$k]['调换原因'].=$va->problem_info.',';
  1238. }
  1239. foreach ($arrs as $ke=>$vas){
  1240. $nu=strpos($ke,'棉');
  1241. foreach ($vas as $kes=>$val){
  1242. if (is_numeric($nu)){
  1243. $res[$k][$arr[$val['size']]].=$val['total'].'条棉';
  1244. }else{
  1245. $res[$k][$arr[$val['size']]].=$val['total'].'条'.mb_substr($ke,0,1);
  1246. }
  1247. }
  1248. }
  1249. $res[$k]['arr']=$arrs;
  1250. }
  1251. return $this->success($res);
  1252. }
  1253. }