StoreController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Store;
  4. use App\Models\User;
  5. use App\Models\StoreUser;
  6. use App\Models\Ordertest;
  7. use App\Models\StoreRewardSet;
  8. use App\Models\StoreRewards;
  9. use App\Models\StoreRewardInfo;
  10. use App\Models\Orderdetail;
  11. use Carbon\Carbon;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Validation\Rule;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. use Illuminate\Support\Facades\Auth;
  17. use Illuminate\Support\Facades\Validator;
  18. use Illuminate\Support\Facades\Storage;
  19. use \Exception;
  20. use Excel;
  21. use App\Imports\ReportImport;
  22. class StoreController extends Controller
  23. {
  24. //获取门店列表
  25. public function getStoreList(Request $request)
  26. {
  27. $input = $request->all();
  28. $page_size = $input['page_size'];
  29. $page_index = $input['page_index'];
  30. $num = ($page_index - 1) * $page_size;
  31. $search_name = $input['search_name'];
  32. $type = $input['type'];
  33. // $search_name='';
  34. // $type='';
  35. $where = [];
  36. $data = Store::where($where);
  37. if ($type) {
  38. $data->where('type', $type);
  39. }
  40. if ($search_name) {
  41. $data->where('name', 'like', '%' . $search_name . '%');
  42. }
  43. $count = $data->count();
  44. if ($count == 0) {
  45. $this->error('400001', '没有数据');
  46. }
  47. $list = $data->with('agent', 'manager:id,nickname,mobile,level')
  48. ->skip($num)->take($page_size)
  49. ->orderBy('id', 'desc')
  50. ->get();
  51. if (empty($list)) {
  52. return $this->error('400002', '没有获取到数据');
  53. }
  54. return $this->success_list($list, 'success', $count);
  55. }
  56. //添加门店信息
  57. public function uploadStore(Request $request)
  58. {
  59. $input = $request->all();
  60. $rules = [
  61. 'name' => [
  62. 'required',
  63. Rule::unique('store')
  64. ->where(function ($query) {
  65. $query->where('deleted_at', null);
  66. })
  67. ],
  68. ];
  69. $messages = [
  70. 'name.required' => '门店名称不能为空.',
  71. 'name.unique' => '门店名称已存在.',
  72. ];
  73. $validator = Validator::make($input, $rules, $messages);
  74. if ($validator->fails()) {
  75. return $this->error('400013', $validator->errors()->first());
  76. }
  77. if ($input['type'] == 1 || $input['type'] == 3) {
  78. if (count($input['member']) != 1) {
  79. return $this->error('400111', '工作室与优享店只能有一个成员');
  80. }
  81. }
  82. foreach ($input['member'] as $key => $val) {
  83. $man = User::where('mobile', $val)->first();
  84. if ($man) {
  85. // if($man->store_id){
  86. // return $this->error('400117','代理('.$val.')已有所属门店');
  87. // }
  88. if ($input['type'] == 1) {
  89. if (!in_array($man->level, [2, 3])) {
  90. return $this->error('400113', '代理(' . $val . ')等级与门店类型不符');
  91. }
  92. } elseif ($input['type'] == 2 || $input['type'] == 3) {
  93. if ($man->level != 3) {
  94. return $this->error('400113', '店铺成员必须为代理公司');
  95. }
  96. }
  97. } else {
  98. return $this->error('400119', '代理(' . $val . ')信息不存在');
  99. }
  100. }
  101. if (!in_array($input['stationMobile'], $input['member'])) {
  102. return $this->error('400120', '提交代理信息有误');
  103. }
  104. $manager = User::where('mobile', $input['stationMobile'])->first();
  105. if ($manager) {
  106. if ($manager->warea_id) {
  107. $data['warea_id'] = $manager->warea_id;
  108. $data['man_id'] = $manager->id;
  109. } else {
  110. return $this->error('400118', '代理(' . $input['stationMobile'] . ')未选择战区');
  111. }
  112. } else {
  113. return $this->error('400119', '代理(' . $input['stationMobile'] . ')信息不存在');
  114. }
  115. $data['name'] = $input['name'];
  116. $data['contents'] = $input['contents'];
  117. $data['type'] = $input['type'];
  118. $data['status'] = 1;
  119. $data['address'] = $input['address'];
  120. $data['province'] = $input['province'];
  121. $data['city'] = $input['city'];
  122. $data['area'] = $input['area'];
  123. $data['size'] = $input['size'];
  124. $data['decorate_account'] = $input['decorate'];
  125. $data['img'] = json_encode($input['img']);
  126. DB::beginTransaction();
  127. try {
  128. $row = Store::create($data);
  129. foreach ($input['member'] as $key => $val) {
  130. $uid = User::where('mobile', $val)->value('id');
  131. if ($uid) {
  132. if ($val == $input['stationMobile']) {
  133. StoreUser::create(['store_id' => $row->id, 'user_id' => $uid, 'user_mobile' => $val]);
  134. } else {
  135. StoreUser::create(['store_id' => $row->id, 'user_id' => $uid, 'user_mobile' => $val]);
  136. }
  137. }
  138. }
  139. DB::commit();
  140. Log::info('管理员:' . Auth::user()->name . '(id=' . Auth::user()->id . ')添加门店信息成功(id=' . $row->id . ')');
  141. return $this->success([]);
  142. } catch (Exception $e) {
  143. DB::rollBack();
  144. return $e->getMessage();
  145. return $this->error();
  146. }
  147. }
  148. //修改门店信息
  149. public function updateStore(Request $request)
  150. {
  151. $input = $request->all();
  152. $rules = [
  153. 'name' => [
  154. 'required',
  155. Rule::unique('store')
  156. ->ignore($input['id'], 'id')
  157. ->where(function ($query) {
  158. $query->where('deleted_at', null);
  159. })
  160. ],
  161. ];
  162. $messages = [
  163. 'name.required' => '门店名称不能为空.',
  164. 'name.unique' => '门店名称已存在.',
  165. ];
  166. $validator = Validator::make($input, $rules, $messages);
  167. if ($validator->fails()) {
  168. return $this->error('400013', $validator->errors()->first());
  169. }
  170. if (count($input['member']) != count(array_unique($input['member']))) {
  171. return $this->error('450001', '店铺成员重复提交');
  172. }
  173. $oldAgent = StoreUser::where('store_id', $input['id'])->pluck('user_mobile')->toArray();
  174. $inter = array_intersect($oldAgent, $input['member']);//交际
  175. $oldDiff = array_diff($oldAgent, $inter);//删除
  176. $AgeDiff = array_diff($input['member'], $inter);//新增
  177. if ($input['type'] == 1 || $input['type'] == 3) {
  178. if (count($input['member']) != 1) {
  179. return $this->error('400111', '工作室和优享店只能有一个代理');
  180. }
  181. }
  182. if ($AgeDiff) {
  183. foreach ($AgeDiff as $key => $val) {
  184. $man = User::where('mobile', $val)->first();
  185. if ($man) {
  186. // if($man->store_id){
  187. // return $this->error('400117','代理('.$val.')已有所属门店');
  188. // }
  189. if ($input['type'] == 1) {
  190. if (!in_array($man->level, [2, 3])) {
  191. return $this->error('400113', '代理(' . $val . ')等级与门店类型不符');
  192. }
  193. } elseif ($input['type'] == 2 || $input['type'] == 3) {
  194. if ($man->level != 3) {
  195. return $this->error('400113', '店铺成员必须为代理公司');
  196. }
  197. }
  198. } else {
  199. return $this->error('400119', '代理(' . $val . ')信息不存在');
  200. }
  201. }
  202. }
  203. if (!in_array($input['stationMobile'], $input['member'])) {
  204. return $this->error('400120', '提交代理信息有误');
  205. }
  206. $manager = User::where('mobile', $input['stationMobile'])->first();
  207. if ($manager) {
  208. if ($manager->warea_id) {
  209. $data['warea_id'] = $manager->warea_id;
  210. $data['man_id'] = $manager->id;
  211. } else {
  212. return $this->error('400118', '代理(' . $input['stationMobile'] . ')未选择战区');
  213. }
  214. } else {
  215. return $this->error('400119', '代理(' . $input['stationMobile'] . ')信息不存在');
  216. }
  217. $data['name'] = $input['name'];
  218. $data['contents'] = $input['contents'];
  219. $data['type'] = $input['type'];
  220. $data['address'] = $input['address'];
  221. $data['province'] = $input['province'];
  222. $data['city'] = $input['city'];
  223. $data['area'] = $input['area'];
  224. $data['size'] = $input['size'];
  225. $data['decorate_account'] = $input['decorate'];
  226. $data['img'] = json_encode($input['img']);
  227. DB::beginTransaction();
  228. try {
  229. Store::where('id', $input['id'])->update($data);
  230. if ($AgeDiff) {
  231. foreach ($AgeDiff as $key => $val) {
  232. $u_id = User::where('mobile', $val)->value('id');
  233. if ($val == $input['stationMobile']) {
  234. StoreUser::create(['store_id' => $input['id'], 'user_id' => $u_id, 'user_mobile' => $val]);
  235. } else {
  236. StoreUser::create(['store_id' => $input['id'], 'user_id' => $u_id, 'user_mobile' => $val]);
  237. }
  238. }
  239. }
  240. if ($oldDiff) {
  241. foreach ($oldDiff as $key => $val) {
  242. StoreUser::where('user_mobile', $val)->where('store_id', $input['id'])->delete();
  243. }
  244. }
  245. DB::commit();
  246. Log::info('管理员:' . Auth::user()->name . '(id=' . Auth::user()->id . ')修改门店信息成功(id=' . $input['id'] . ')');
  247. return $this->success([]);
  248. } catch (Exception $e) {
  249. DB::rollBack();
  250. // return $e->getMessage();
  251. return $this->error();
  252. }
  253. }
  254. /* 门店禁用/启用 */
  255. public function shelvedStore(Request $request)
  256. {
  257. $store = Store::find($request->post('id'));
  258. $store->status = ($user->status == 0) ? '1' : '0';
  259. $row = $store->save();
  260. if ($row) {
  261. Log::info('管理员:' . Auth::user()->name . '(id=' . Auth::user()->id . ')禁用门店信息成功(id=' . $row->id . ')');
  262. return $this->success([]);
  263. }
  264. return $this->error();
  265. }
  266. //删除门店信息
  267. public function destoryStore(Request $request)
  268. {
  269. $input = $request->all();
  270. $store = Store::find($input['id']);
  271. DB::beginTransaction();
  272. try {
  273. $store->delete();
  274. StoreUser::where('store_id', $input['id'])->delete();
  275. DB::commit();
  276. return $this->success([]);
  277. } catch (Exception $e) {
  278. DB::rollBack();
  279. return $this->error();
  280. }
  281. }
  282. public function uploadStoreImg(Request $request)
  283. {
  284. $input = $request->all();
  285. $path_url = 'public/store';
  286. $path = $request->file('file')->store($path_url);
  287. $url = Storage::url($path);
  288. if ($url) {
  289. return $this->success($url);
  290. }
  291. return $this->error();
  292. }
  293. public function getStoreRewardInfo(Request $request)
  294. {
  295. $input = $request->all();
  296. $page_index = $input['page_index'];
  297. $page_size = $input['page_size'];
  298. $num = $page_size * ($page_index - 1);
  299. // $month = $input['month'];
  300. $where = [];
  301. // if ($month) {
  302. // $where['month'] = $month;
  303. // }
  304. $count = StoreRewardSet::where($where)->count();
  305. $list = StoreRewardSet::with(['rewards'])->where($where)
  306. ->skip($num)->take($page_size)
  307. ->orderBy('month', 'desc')
  308. ->get();
  309. return $this->success_list($list, '成功', $count);
  310. }
  311. public function addStoreRewardInfo(Request $request)
  312. {
  313. $input = $request->all();
  314. $rules = [
  315. 'month' => [
  316. 'required',
  317. Rule::unique('store_rewards_set'),
  318. // ->ignore($input['id'], 'id')
  319. // ->where(function ($query) {
  320. // $query->where('deleted_at', null);
  321. // })
  322. ],
  323. ];
  324. $messages = [
  325. 'month.required' => '月份不能为空.',
  326. 'month.unique' => '月份已存在.',
  327. ];
  328. $validator = Validator::make($input, $rules, $messages);
  329. if ($validator->fails()) {
  330. return $this->error('400013', $validator->errors()->first());
  331. }
  332. $row = StoreRewardSet::create([
  333. 'month' => $input['month'],
  334. 'account_rate' => $input['account_rate'],
  335. // 'goods' => json_encode($input['goods']),
  336. // 'money' => $input['money'],
  337. 'enjoy_hard' => $input['enjoy_hard'],
  338. 'enjoy_other' => $input['enjoy_other'],
  339. 'over_enjoy_hard' => $input['over_enjoy_hard'],
  340. 'over_enjoy_other' => $input['over_enjoy_other'],
  341. 'experience_hard' => $input['experience_hard'],
  342. 'experience_other' => $input['experience_other'],
  343. ]);
  344. foreach($input['goods'] as $key=>$val){
  345. StoreRewards::create([
  346. 'month'=>$input['month'],
  347. 'name'=>$val['name'],
  348. 'price'=>$val['price'],
  349. ]);
  350. }
  351. if ($row) {
  352. return $this->success([]);
  353. }
  354. return $this->error();
  355. }
  356. public function updateStoreRewardInfo(Request $request)
  357. {
  358. $input = $request->all();
  359. $rules = [
  360. 'month' => [
  361. 'required',
  362. Rule::unique('store_rewards_set')
  363. ->ignore($input['id'], 'id')
  364. ],
  365. ];
  366. $messages = [
  367. 'month.required' => '月份不能为空.',
  368. 'month.unique' => '月份已存在.',
  369. ];
  370. $validator = Validator::make($input, $rules, $messages);
  371. if ($validator->fails()) {
  372. return $this->error('400013', $validator->errors()->first());
  373. }
  374. $row = StoreRewardSet::where('id',$input['id'])->update([
  375. 'month' => $input['month'],
  376. 'account_rate' => $input['account_rate'],
  377. 'enjoy_hard' => $input['enjoy_hard'],
  378. 'enjoy_other' => $input['enjoy_other'],
  379. 'over_enjoy_hard' => $input['over_enjoy_hard'],
  380. 'over_enjoy_other' => $input['over_enjoy_other'],
  381. 'experience_hard' => $input['experience_hard'],
  382. 'experience_other' => $input['experience_other'],
  383. ]);
  384. $before_ids=StoreRewards::where('month',$input['month'])->pluck('id')->toArray();
  385. // return $before_ids;
  386. $after_ids=[];
  387. foreach($input['goods'] as $key=>$val){
  388. if(array_key_exists('isAdd',$val)){
  389. StoreRewards::create([
  390. 'month'=>$input['month'],
  391. 'name'=>$val['name'],
  392. 'price'=>$val['price'],
  393. ]);
  394. }else{
  395. $after_ids[]=$val['id'];
  396. }
  397. }
  398. $diff_ids=array_diff($before_ids,$after_ids);
  399. StoreRewards::whereIn('id',$diff_ids)->delete();
  400. if ($row) {
  401. return $this->success([]);
  402. }
  403. return $this->error();
  404. }
  405. public function deleteStoreRewardInfo(Request $request)
  406. {
  407. $input = $request->all();
  408. $row = StoreRewardSet::where('id', $input['id'])->delete();
  409. if ($row) {
  410. return $this->success([]);
  411. }
  412. return $this->error();
  413. }
  414. public function getStoreRewardList(Request $request)
  415. {
  416. $input = $request->all();
  417. $page_index = $input['page_index'];
  418. $page_size = $input['page_size'];
  419. $num = $page_size * ($page_index - 1);
  420. $month = $input['month'];
  421. $search_name = $input['search_name'];
  422. $type = $input['type'];//店铺级别 1工作室 3优享店 2体验店
  423. //<<<<<<< HEAD
  424. // $where = [];
  425. // $where['store.type'] = $type;
  426. // $alr_store_ids=StoreRewardInfo::where('month',$month)->groupBy('store_id')->pluck('store_id');
  427. // $count=Store::whereIn('id',$alr_store_ids)
  428. // ->where('type',$type)
  429. // ->where('name','like','%'.$search_name.'%')
  430. // ->count();
  431. // $list=Store::whereIn('id',$alr_store_ids)
  432. // ->where('type',$type)
  433. // ->where('name','like','%'.$search_name.'%')
  434. //=======
  435. $alr_store_ids=StoreRewardInfo::where('month',$month)->groupBy('store_id')->pluck('store_id');
  436. $count=Store::whereIn('id',$alr_store_ids)->where('name','like','%'.$search_name.'%')->where('type',$type)->count();
  437. $list=Store::whereIn('id',$alr_store_ids)
  438. ->where('name','like','%'.$search_name.'%')
  439. ->where('type',$type)
  440. //>>>>>>> store_rewards
  441. ->skip($num)->take($page_size)
  442. ->orderBy('account', 'desc')
  443. ->get();
  444. foreach($list as $key=>$val){
  445. //<<<<<<< HEAD
  446. $list[$key]->rewards=StoreRewardInfo::where('month',$month)->where('store_id',$val->id)
  447. ->select('content','rewards','type','created_at','money')
  448. ->groupBy('store_id','month','type')->get();
  449. //=======
  450. // $list[$key]->rewards=StoreRewardInfo::where('month',$month)->where('store_id',$val->id)
  451. // ->groupBy('type')->get();
  452. //>>>>>>> store_rewards
  453. }
  454. return $this->success_list($list,'成功',$count);
  455. }
  456. public function getStoreNoRewardList1(Request $request)
  457. {
  458. $input = $request->all();
  459. $page_index = $input['page_index'];
  460. $page_size = $input['page_size'];
  461. $num = $page_size * ($page_index - 1);
  462. $month = $input['month'];
  463. $search_name = $input['search_name'];
  464. $type = $input['type'];//店铺级别 1工作室 3优享店 2体验店
  465. $where = [];
  466. $where['store.type'] = $type;
  467. $start_time = Carbon::parse($month)->startOfMonth()->toDateTimeString();
  468. $end_time = Carbon::parse($month)->endOfMonth()->toDateTimeString();
  469. $alr_store_ids=StoreRewardInfo::where('month',$month)->groupBy('store_id')->pluck('store_id');
  470. $store_ids=Store::whereNotIn('id',$alr_store_ids)->pluck('id');
  471. $data = Store::where($where)->whereIn('store.id',$store_ids)
  472. ->join('store_user as su', 'su.store_id', '=', 'store.id')
  473. ->join('users', 'users.id', '=', 'su.user_id')
  474. ->leftJoin('order_test as ot', function ($query) use ($start_time, $end_time) {
  475. $query->on('ot.user_id', '=', 'users.id')
  476. ->whereIn('ot.status', [3, 4])
  477. ->whereBetween('ot.created_at', [$start_time, $end_time]);
  478. })
  479. ->groupBy('store.id');
  480. $count=$data->get()->count();
  481. $list=$data->select('store.id', 'store.name', 'store.type','store.account as store_account','store.decorate_account', DB::raw('sum(ifNull(ot.money,0)) as account'), DB::raw('sum(ifNull(ot.total,0)) as total'))
  482. ->skip($num)->take($page_size)
  483. ->orderBy('account', 'desc')
  484. ->get();
  485. foreach ($list as $key => $val) {
  486. $user_ids = StoreUser::where('store_id', $val->id)->pluck('user_id');
  487. $list[$key]->goods = [];
  488. if ($user_ids) {
  489. $order_ids = Ordertest::whereIn('user_id', $user_ids)
  490. ->whereIn('status', [3, 4])
  491. ->whereBetween('created_at', [$start_time, $end_time])
  492. ->pluck('id');
  493. $info = Orderdetail::join('goods_test as gt','gt.id','=','order_detail.goods_id')
  494. ->whereIn('order_detail.order_id', $order_ids)->groupBy('gt.main_attr')
  495. ->select('gt.main_attr','gt.unit', DB::raw('sum(order_detail.num) as total') ,DB::raw('sum(order_detail.money) as account'))->get();
  496. $list[$key]->goods = $info;
  497. $rewardInfo=StoreRewardInfo::where('month',$month)->where('store_id',$val->id)->first();
  498. $list[$key]->reward=$rewardInfo;
  499. }
  500. }
  501. return $this->success_list($list,'成功',$count);
  502. }
  503. public function getStoreNoRewardList(Request $request)
  504. {
  505. $input = $request->all();
  506. $page_index = $input['page_index'];
  507. $page_size = $input['page_size'];
  508. $num = $page_size * ($page_index - 1);
  509. $month = $input['month'];
  510. $search_name = $input['search_name'];
  511. $type = $input['type'];//店铺级别 1工作室 3优享店 2体验店
  512. $where = [];
  513. $where['store.type'] = $type;
  514. $start_time = Carbon::parse($month)->startOfMonth()->toDateTimeString();
  515. $end_time = Carbon::parse($month)->endOfMonth()->toDateTimeString();
  516. $alr_store_ids=StoreRewardInfo::where('month',$month)->groupBy('store_id')->pluck('store_id');
  517. $count=Store::where('type',$type)->where('name','like','%'.$search_name.'%')->whereNotIn('id',$alr_store_ids)->count();
  518. $list=Store::where('store.type',$type)->where('store.name','like','%'.$search_name.'%')->whereNotIn('store.id',$alr_store_ids)
  519. ->join('store_user as su','su.store_id','=','store.id')
  520. ->leftJoin('order_test as ot',function($query) use ($start_time, $end_time){
  521. $query->on('ot.user_id','=','su.user_id')
  522. ->whereIn('ot.status',[3,4])
  523. ->whereBetween('ot.created_at', [$start_time, $end_time]);
  524. })
  525. ->select('store.name','store.id','store.account as store_account','store.decorate_account','store.type',DB::raw('sum(ifNull(ot.money,0)) as account'),DB::raw('sum(ifNull(ot.total,0)) as total'))
  526. ->groupBy('store.id')
  527. ->skip($num)->take($page_size)
  528. ->orderBy('account', 'desc')
  529. ->get();
  530. foreach($list as $key=>$val){
  531. $user_ids=StoreUser::where('store_id',$val->id)->pluck('user_id');
  532. if($type==2){
  533. $original_orders_ids=Ordertest::whereIn('status',[3,4])->where('user_id',$user_ids)->whereBetween('created_at', [$start_time, $end_time])->pluck('id')->toArray();
  534. $manual_orders_ids=Ordertest::whereIn('status',[3,4])->where('store_id',$val->id)
  535. ->whereBetween('created_at', [$start_time, $end_time])->pluck('id')->toArray();
  536. $order_ids=array_unique(array_merge($original_orders_ids,$manual_orders_ids));
  537. }else{
  538. $order_ids=Ordertest::whereIn('status',[3,4])->where('user_id',$user_ids)->whereBetween('created_at', [$start_time, $end_time])->pluck('id');
  539. }
  540. $goods=Ordertest::join('order_detail as od','od.order_id','=','order_test.id')
  541. ->join('goods_test as gt','gt.id','=','od.goods_id')->whereIn('order_test.id',$order_ids)
  542. ->groupBy('gt.main_attr')
  543. ->select('gt.main_attr','gt.unit', DB::raw('sum(od.num) as total') ,DB::raw('sum(od.money) as account'))->get();
  544. $list[$key]->goods=$goods;
  545. }
  546. return $this->success_list($list,'成功',$count);
  547. }
  548. public function settlement(Request $request)
  549. {
  550. $input = $request->all();
  551. $store_id = $input['store_id'];
  552. $month = $input['month'];
  553. $info=$input['info'];
  554. $reward=StoreRewardInfo::where('month',$month)->where('store_id',$store_id)->get();
  555. if(count($reward)>0){
  556. return $this->error('450001','该门店当月已结算奖励');
  557. }
  558. $start_time = Carbon::parse($month)->startOfMonth()->toDateTimeString();
  559. $end_time = Carbon::parse($month)->endOfMonth()->toDateTimeString();
  560. $set = StoreRewardSet::where('month', $month)->first();
  561. $user_ids = StoreUser::where('store_id', $store_id)->pluck('user_id');
  562. if (count($user_ids) <= 0) {
  563. return $this->error('门店无代理,不能结算');
  564. }
  565. $account = Ordertest::whereIn('user_id', $user_ids)
  566. ->whereIn('status', [3, 4])->whereBetween('created_at', [$start_time, $end_time])
  567. ->sum('money');
  568. if ($account <= 0) {
  569. return $this->error('当月该门店总金额为0,不能结算');
  570. }
  571. $store = Store::where('id',$store_id)->first();
  572. DB::beginTransaction();
  573. try {
  574. if ($store->type == 1) {//工作室
  575. $goods = $input['goods'];
  576. $amount = 0;
  577. foreach ($goods as $key => $val) {
  578. $amount += $val['price'] * $val['number'];
  579. }
  580. if ($amount > $account * $set->account_rate) {
  581. return $this->error('450001','商品总价超出门店总金额,不能结算');
  582. }
  583. foreach ($goods as $key => $val) {
  584. StoreRewardInfo::create([
  585. 'month' => $month,
  586. 'store_id' => $store_id,
  587. 'type' => 1,
  588. 'rewards' => json_encode($goods, JSON_UNESCAPED_UNICODE),
  589. 'rewards_id' => $val['id'],
  590. 'content' => json_encode($info, JSON_UNESCAPED_UNICODE),
  591. 'num' => $val['number'],
  592. 'money' => null
  593. ]);
  594. }
  595. } elseif ($store->type == 2) {//体验店
  596. $goods = $input['goods'];
  597. $amount = 0;//总数量
  598. foreach ($goods as $key => $val) {
  599. $amount += $val['number'] * $val['price'];
  600. }
  601. Store::where('id',$store_id)->increment('account',$amount);
  602. StoreRewardInfo::create([
  603. 'month' => $month,
  604. 'store_id' => $store_id,
  605. 'type' => 2,
  606. 'rewards_id' => null,
  607. 'num' => null,
  608. 'rewards' => json_encode($goods, JSON_UNESCAPED_UNICODE),
  609. 'content' => json_encode($info, JSON_UNESCAPED_UNICODE),
  610. 'money' => $amount,
  611. ]);
  612. } else {//优享店
  613. $decorate = $store->decorate_account;//装修金额
  614. if (empty($decorate)) {
  615. return $this->error('当前门店无装修金额,不能结算');
  616. }
  617. $goods = $input['goods'];
  618. $amount = 0;
  619. foreach ($goods as $key => $val) {
  620. $amount += $val['price'] * $val['number'];
  621. }
  622. if ($amount > $account * $set->account_rate) {
  623. return $this->error('商品总价超出门店总金额,不能结算');
  624. }
  625. foreach ($goods as $key => $val) {
  626. StoreRewardInfo::create([
  627. 'month' => $month,
  628. 'store_id' => $store_id,
  629. 'type' => 1,
  630. 'rewards' => json_encode($goods, JSON_UNESCAPED_UNICODE),
  631. 'rewards_id' => $val['id'],
  632. 'content' => json_encode($info, JSON_UNESCAPED_UNICODE),
  633. 'num' => $val['number'],
  634. 'money' => null
  635. ]);
  636. }
  637. $goods_price = $input['goods_price'];
  638. $amount = 0;//总数量
  639. foreach ($goods_price as $key => $val) {
  640. $amount += $val['number1'] * $val['price1'] + $val['number2'] * $val['price2'];
  641. }
  642. Store::where('id',$store_id)->increment('account',$amount);
  643. StoreRewardInfo::create([
  644. 'month' => $month,
  645. 'store_id' => $store_id,
  646. 'type' => 2,
  647. 'rewards_id' => null,
  648. 'num' => null,
  649. 'rewards' => json_encode($goods_price, JSON_UNESCAPED_UNICODE),
  650. 'content' => json_encode($info, JSON_UNESCAPED_UNICODE),
  651. 'money' => $amount,
  652. ]);
  653. }
  654. DB::commit();
  655. return $this->success([]);
  656. }catch(Exception $e){
  657. DB::rollBack();
  658. return $this->error($e->getMessage());
  659. }
  660. }
  661. public function undoStoreRewardsInfo(Request $request){
  662. $input=$request->all();
  663. $month=$input['month'];
  664. $store_id=$input['store_id'];
  665. $next_month=date("Y-m",strtotime('+1 month',strtotime($month)));
  666. $curr_month=date("Y-m");
  667. if($next_month!=$curr_month){
  668. return $this->error('450001','当前月份兑换不能撤销');
  669. }
  670. DB::beginTransaction();
  671. try{
  672. $amount=StoreRewardInfo::where('month',$month)->where('store_id',$store_id)->where('type',2)->value('money');
  673. if($amount>0){
  674. Store::where('id',$store_id)->decrement('money',$amount);
  675. }
  676. StoreRewardInfo::where('month',$month)->where('store_id',$store_id)->delete();
  677. DB::commit();
  678. return $this->success([]);
  679. }catch(Exception $e){
  680. DB::rollBack();
  681. return $this->error();
  682. }
  683. }
  684. public function getStoreInfo(Request $request){
  685. $input=$request->all();
  686. $month=$input['month'];
  687. $info=StoreRewardSet::where('month',$month)->first();
  688. $goods=StoreRewards::where('month',$month)->get();
  689. if($info){
  690. $info->goods=$goods;
  691. return $this->success($info);
  692. }else{
  693. return $this->error('450001','当月未设置兑换信息');
  694. }
  695. }
  696. //导入门店
  697. public function import(Request $request){
  698. if(!$request->hasFile('file')){
  699. exit('上传文件为空!');
  700. }
  701. $data=[];
  702. $array = Excel::toArray(new ReportImport, request()->file('file'));
  703. DB::beginTransaction();
  704. try{
  705. foreach($array[0] as $key => $val){
  706. if(!empty($val[0]) && $val[0]!='昵称'){
  707. $user=User::where('mobile',$val[2])->select('id','nickname','warea_id')->first();
  708. if(empty($user)){
  709. return $this->error('450001',$val[0].'手机号不存在');
  710. }
  711. if(!isset($user->warea_id) || empty($user->warea_id) ){
  712. return $this->error('450001',$val[0].'没有战区');
  713. }
  714. $data[$key]['name']=$val[0].$val[4];
  715. $data[$key]['type']=1;
  716. $data[$key]['status']=1;
  717. $data[$key]['man_id']=$user->id;
  718. if($val[4]=='工作室'){
  719. $type=1;
  720. }elseif($val[4]=='优享店'){
  721. $type=3;
  722. }else{
  723. $type=2;
  724. }
  725. $row=Store::create(['name'=>$val[0].$val[4],'type'=>$type,'status'=>1,'man_id'=>$user->id,'warea_id'=>$user->warea_id]);
  726. StoreUser::create(['user_id'=>$user->id,'user_mobile'=>$val[2],'store_id'=>$row->id]);
  727. }
  728. }
  729. DB::commit();
  730. return $this->success([]);
  731. }catch(\Exception $e){
  732. DB::rollback();
  733. return $this->error();
  734. }
  735. }
  736. }