SystemStore.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\store\StoreCategory;
  5. use app\admin\model\system\SystemAdmin;
  6. use app\admin\model\system\SystemStoreBill;
  7. use app\admin\model\system\SystemStorePoint;
  8. use app\admin\model\system\SystemStoreProductStock;
  9. use app\admin\model\system\SystemStoreProductStockLog;
  10. use crmeb\services\FormBuilder;
  11. use crmeb\services\JsonService;
  12. use crmeb\services\JsonService as Json;
  13. use app\admin\model\system\SystemStore as SystemStoreModel;
  14. use crmeb\services\UtilService;
  15. use think\db\exception\DataNotFoundException;
  16. use think\db\exception\ModelNotFoundException;
  17. use think\facade\Route;
  18. /**
  19. * 门店管理控制器
  20. * Class SystemAttachment
  21. * @package app\admin\controller\system
  22. *
  23. */
  24. class SystemStore extends AuthController
  25. {
  26. /**
  27. * 门店列表
  28. */
  29. public function list()
  30. {
  31. $where = UtilService::getMore([
  32. ['page', 1],
  33. ['limit', 20],
  34. ['name', ''],
  35. ['excel', 0],
  36. ['type', $this->request->param('type')]
  37. ]);
  38. JsonService::successlayui(SystemStoreModel::getStoreList($where));
  39. }
  40. /**
  41. * 门店设置
  42. * @return string
  43. */
  44. public function index()
  45. {
  46. $type = $this->request->param('type');
  47. $show = SystemStoreModel::where('is_show', 1)->where('is_del', 0)->count();//显示中的门店
  48. $hide = SystemStoreModel::where('is_show', 0)->count();//隐藏的门店
  49. $recycle = SystemStoreModel::where('is_del', 1)->count();//删除的门店
  50. if ($type == null) $type = 1;
  51. $this->assign(compact('type', 'show', 'hide', 'recycle'));
  52. return $this->fetch();
  53. }
  54. /**
  55. * 门店添加
  56. * @param int $id
  57. * @return string
  58. */
  59. public function add($id = 0)
  60. {
  61. $store = SystemStoreModel::getStoreDispose($id);
  62. $this->assign(compact('store'));
  63. return $this->fetch();
  64. }
  65. /**
  66. * 删除恢复门店
  67. * @param $id
  68. */
  69. public function delete($id)
  70. {
  71. if (!$id) return $this->failed('数据不存在');
  72. if (!SystemStoreModel::be(['id' => $id])) return $this->failed('产品数据不存在');
  73. if (SystemStoreModel::be(['id' => $id, 'is_del' => 1])) {
  74. $data['is_del'] = 0;
  75. if (!SystemStoreModel::edit($data, $id))
  76. return Json::fail(SystemStoreModel::getErrorInfo('恢复失败,请稍候再试!'));
  77. else
  78. return Json::successful('恢复门店成功!');
  79. } else {
  80. $data['is_del'] = 1;
  81. if (!SystemStoreModel::edit($data, $id))
  82. return Json::fail(SystemStoreModel::getErrorInfo('删除失败,请稍候再试!'));
  83. else
  84. return Json::successful('删除门店成功!');
  85. }
  86. }
  87. /**
  88. * 设置单个门店是否显示
  89. * @param string $is_show
  90. * @param string $id
  91. * @return json
  92. */
  93. public function set_show($is_show = '', $id = '')
  94. {
  95. ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
  96. $res = SystemStoreModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  97. if ($res) {
  98. return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功');
  99. } else {
  100. return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败');
  101. }
  102. }
  103. /**
  104. * 位置选择
  105. * @return string|void
  106. */
  107. public function select_address()
  108. {
  109. $key = sys_config('tengxun_map_key');
  110. if (!$key) return $this->failed('请前往设置->物流设置->物流配置 配置腾讯地图KEY', '#');
  111. $this->assign(compact('key'));
  112. return $this->fetch();
  113. }
  114. /**
  115. * 保存修改门店信息
  116. * @param int $id
  117. */
  118. public function save($id = 0)
  119. {
  120. $data = UtilService::postMore([
  121. ['name', ''],
  122. ['introduction', ''],
  123. ['image', ''],
  124. ['phone', ''],
  125. ['address', ''],
  126. ['detailed_address', ''],
  127. ['latlng', ''],
  128. ['valid_time', []],
  129. ['day_time', []],
  130. ['leader', ''],
  131. ['pictures', []],
  132. ]);
  133. SystemStoreModel::beginTrans();
  134. try {
  135. $data['address'] = implode(',', $data['address']);
  136. $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng'];
  137. if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) JsonService::fail('请选择门店位置');
  138. $data['latitude'] = $data['latlng'][0];
  139. $data['longitude'] = $data['latlng'][1];
  140. $data['valid_time'] = implode(' - ', $data['valid_time']);
  141. $data['day_time'] = implode(' - ', $data['day_time']);
  142. unset($data['latlng']);
  143. if ($data['image'] && strstr($data['image'], 'http') === false) {
  144. $site_url = sys_config('site_url');
  145. $data['image'] = $site_url . $data['image'];
  146. }
  147. $new = [];
  148. foreach ($data['pictures'] as $v) {
  149. $site_url = sys_config('site_url');
  150. $new[] = $site_url . $v;
  151. }
  152. $data['pictures'] = implode(',', $new);
  153. if ($id) {
  154. if (SystemStoreModel::where('id', $id)->update($data)) {
  155. SystemStoreModel::commitTrans();
  156. JsonService::success('修改成功');
  157. } else {
  158. SystemStoreModel::rollbackTrans();
  159. JsonService::fail('修改失败或者您没有修改什么!');
  160. }
  161. } else {
  162. $data['add_time'] = time();
  163. $data['is_show'] = 1;
  164. if ($res = SystemStoreModel::create($data)) {
  165. SystemAdmin::create([
  166. 'account' => 'store_admin_' . $res->id,
  167. 'pwd' => md5('123456'),
  168. 'real_name' => $data['leader'],
  169. 'roles' => sys_config('default_store_admin', 1),
  170. 'add_time' => time(),
  171. 'status' => 1,
  172. 'level' => 2,
  173. 'store_id' => $res->id,
  174. ]);
  175. SystemStoreModel::commitTrans();
  176. JsonService::success('保存成功', ['id' => $res->id]);
  177. } else {
  178. SystemStoreModel::rollbackTrans();
  179. JsonService::fail('保存失败!');
  180. }
  181. }
  182. } catch (\Exception $e) {
  183. SystemStoreModel::rollbackTrans();
  184. JsonService::fail($e->getMessage());
  185. }
  186. }
  187. /**
  188. * @param $id
  189. * @return string
  190. * @throws Exception
  191. */
  192. public function storeProductInfo($id)
  193. {
  194. $this->assign('id', $id);
  195. $this->assign('year', get_month());
  196. return $this->fetch();
  197. }
  198. public function stock_list($id)
  199. {
  200. $where = UtilService::getMore([
  201. ['page', 1],
  202. ['data', ''],
  203. ['limit', 20],
  204. ['product_id', ''],
  205. ['excel', 0],
  206. ]);
  207. JsonService::successlayui(SystemStoreProductStock::getStockList($where, $id));
  208. }
  209. public function add_stock($id)
  210. {
  211. if (!$id) {
  212. return $this->failed('请选择门店');
  213. }
  214. $where = UtilService::getMore([
  215. ['product_id', 0],
  216. ['unique', ''],
  217. ]);
  218. if (!$where['product_id'] || !$where['unique']) {
  219. $field = [];
  220. $field[] = FormBuilder::frameImages('product', '选择商品', Route::buildUrl('productList', array('fodder' => 'product')))->icon('plus')->width('100%')->height('500px');
  221. $field[] = FormBuilder::hidden('product_id', '');
  222. $field[] = FormBuilder::hidden('unique', '');
  223. } else {
  224. $field = [];
  225. $field[] = FormBuilder::hidden('product_id', $where['product_id']);
  226. $field[] = FormBuilder::hidden('unique', $where['unique']);
  227. }
  228. $field[] = FormBuilder::number('stock', '库存增加量', 0);
  229. $form = FormBuilder::make_post_form('添加库存', $field, Route::buildUrl('save_stock', ['id' => $id]), 3);
  230. $this->assign(compact('form'));
  231. return $this->fetch('public/form-builder');
  232. }
  233. /**
  234. * @return string
  235. * @throws DataNotFoundException
  236. * @throws ModelNotFoundException
  237. * @throws Exception
  238. */
  239. public function productList()
  240. {
  241. $cate = StoreCategory::getTierList(null, 1);
  242. $this->assign('cate', $cate);
  243. return $this->fetch();
  244. }
  245. public function save_stock($id)
  246. {
  247. if (!$id) {
  248. Json::fail('请选择门店');
  249. }
  250. list($product_id, $unique, $stock) = UtilService::postMore([['product_id', []], ['unique', []], ['stock', 0]], $this->request, true);
  251. if (!count($product_id)) {
  252. Json::fail('请选择补货商品');
  253. }
  254. $res = true;
  255. SystemStoreProductStockLog::beginTrans();
  256. foreach ($product_id as $k => $v) {
  257. if ($stock > 0) {
  258. $res = $res && SystemStoreProductStockLog::income($id, $v, $unique[$k], 'add_stock', 0, $stock, $this->adminId, '后台补货' . $stock . '件', 1);
  259. } else {
  260. $res = $res && SystemStoreProductStockLog::expend($id, $v, $unique[$k], 'dec_stock', 0, abs($stock), $this->adminId, '后台减货' . $stock . '件', 1);
  261. }
  262. }
  263. if ($res) {
  264. SystemStoreProductStockLog::commitTrans();
  265. Json::success('补货成功');
  266. } else {
  267. SystemStoreProductStockLog::rollbackTrans();
  268. Json::fail(SystemStoreProductStockLog::getErrorInfo('补货失败'));
  269. }
  270. }
  271. /**
  272. * @param $id
  273. * @return string
  274. * @throws Exception
  275. */
  276. public function stock_log($id)
  277. {
  278. $this->assign('id', $id);
  279. return $this->fetch();
  280. }
  281. public function stock_log_list($id)
  282. {
  283. $where = UtilService::getMore([
  284. ['page', 1],
  285. ['limit', 20],
  286. ]);
  287. JsonService::successlayui(SystemStoreProductStockLog::getList($where, $id));
  288. }
  289. /**
  290. * @param $id
  291. * @return string
  292. * @throws Exception
  293. */
  294. public function store_bill_log($id)
  295. {
  296. $this->assign('id', $id);
  297. $this->assign('all', SystemStoreModel::get($id)['brokerage_price']);
  298. return $this->fetch();
  299. }
  300. public function store_bill_log_list($id)
  301. {
  302. $where = UtilService::getMore([
  303. ['page', 1],
  304. ['limit', 20],
  305. ]);
  306. JsonService::successlayui(SystemStoreBill::getList($where, $id));
  307. }
  308. /**
  309. * @param $id
  310. * @return string
  311. * @throws Exception
  312. */
  313. public function store_point($id)
  314. {
  315. $type = $this->request->param('type');
  316. $show = SystemStorePoint::where('store_id', $id)->where('is_show', 1)->where('is_del', 0)->count();//显示中的门店
  317. $hide = SystemStorePoint::where('store_id', $id)->where('is_show', 0)->count();//隐藏的门店
  318. $recycle = SystemStorePoint::where('store_id', $id)->where('is_del', 1)->count();//删除的门店
  319. if ($type == null) $type = 1;
  320. $this->assign(compact('type', 'show', 'hide', 'recycle', 'id'));
  321. return $this->fetch();
  322. }
  323. public function store_point_list($id)
  324. {
  325. $where = UtilService::getMore([
  326. ['page', 1],
  327. ['limit', 20],
  328. ['name', ''],
  329. ['excel', 0],
  330. ['type', $this->request->param('type')]
  331. ]);
  332. $where['store_id'] = $id;
  333. JsonService::successlayui(SystemStorePoint::getStoreList($where));
  334. }
  335. /**
  336. * 门店添加
  337. * @param int $id
  338. * @return string
  339. */
  340. public function add_point($id = 0, $store_id = 0)
  341. {
  342. $store = SystemStorePoint::getStoreDispose($id);
  343. $this->assign(compact('store', 'store_id'));
  344. return $this->fetch();
  345. }
  346. /**
  347. * 保存修改门店信息
  348. * @param int $id
  349. */
  350. public function save_point($id = 0)
  351. {
  352. $data = UtilService::postMore([
  353. ['name', ''],
  354. ['phone', ''],
  355. ['address', ''],
  356. ['detailed_address', ''],
  357. ['latlng', ''],
  358. ['valid_time', []],
  359. ['day_time', []],
  360. ['store_id', 0],
  361. ]);
  362. // var_dump($data['store_id']);
  363. if (!$data['store_id'] || !SystemStoreModel::getStoreDispose($data['store_id'])) JsonService::fail('无效门店');
  364. SystemStorePoint::beginTrans();
  365. try {
  366. $data['address'] = implode(',', $data['address']);
  367. $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng'];
  368. if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) JsonService::fail('请选择门店位置');
  369. $data['latitude'] = $data['latlng'][0];
  370. $data['longitude'] = $data['latlng'][1];
  371. $data['valid_time'] = implode(' - ', $data['valid_time']);
  372. $data['day_time'] = implode(' - ', $data['day_time']);
  373. unset($data['latlng']);
  374. if ($id) {
  375. if (SystemStorePoint::where('id', $id)->update($data)) {
  376. SystemStorePoint::commitTrans();
  377. JsonService::success('修改成功');
  378. } else {
  379. SystemStorePoint::rollbackTrans();
  380. JsonService::fail('修改失败或者您没有修改什么!');
  381. }
  382. } else {
  383. $data['add_time'] = time();
  384. $data['is_show'] = 1;
  385. if ($res = SystemStorePoint::create($data)) {
  386. SystemStorePoint::commitTrans();
  387. JsonService::success('保存成功', ['id' => $res->id]);
  388. } else {
  389. SystemStorePoint::rollbackTrans();
  390. JsonService::fail('保存失败!');
  391. }
  392. }
  393. } catch (\Exception $e) {
  394. SystemStorePoint::rollbackTrans();
  395. JsonService::fail($e->getMessage());
  396. }
  397. }
  398. /**
  399. * 设置单个门店是否显示
  400. * @param string $is_show
  401. * @param string $id
  402. * @return json
  403. */
  404. public function set_point_show($is_show = '', $id = '')
  405. {
  406. ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
  407. $res = SystemStorePoint::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  408. if ($res) {
  409. return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功');
  410. } else {
  411. return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败');
  412. }
  413. }
  414. /**
  415. * 删除恢复门店
  416. * @param $id
  417. */
  418. public function point_delete($id)
  419. {
  420. if (!$id) return $this->failed('数据不存在');
  421. if (!SystemStorePoint::be(['id' => $id])) return $this->failed('数据不存在');
  422. if (SystemStorePoint::be(['id' => $id, 'is_del' => 1])) {
  423. $data['is_del'] = 0;
  424. if (!SystemStorePoint::edit($data, $id))
  425. return Json::fail(SystemStorePoint::getErrorInfo('恢复失败,请稍候再试!'));
  426. else
  427. return Json::successful('恢复门店成功!');
  428. } else {
  429. $data['is_del'] = 1;
  430. if (!SystemStorePoint::edit($data, $id))
  431. return Json::fail(SystemStorePoint::getErrorInfo('删除失败,请稍候再试!'));
  432. else
  433. return Json::successful('删除门店成功!');
  434. }
  435. }
  436. }