SystemStore.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. <?php
  2. namespace app\admin\controller\merchant;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\order\StoreOrder as StoreOrderModel;
  5. use app\admin\model\order\StoreOrderStatus;
  6. use app\admin\model\store\StoreProductAttr;
  7. use app\admin\model\store\StoreProductAttrValue;
  8. use app\admin\model\system\ShippingTemplates;
  9. use app\admin\model\system\SystemStoreBill;
  10. use app\admin\model\system\SystemStoreExtract;
  11. use app\admin\model\system\SystemStorePoint;
  12. use app\admin\model\system\SystemStoreProductStock;
  13. use app\admin\model\ump\StorePink;
  14. use app\admin\model\user\User;
  15. use app\models\store\StoreCart;
  16. use app\models\store\StoreOrder;
  17. use crmeb\repositories\OrderRepository;
  18. use crmeb\services\FormBuilder;
  19. use crmeb\services\JsonService;
  20. use app\admin\model\system\SystemStore as SystemStoreModel;
  21. use crmeb\services\UtilService;
  22. use Exception;
  23. use think\db\exception\DataNotFoundException;
  24. use think\db\exception\DbException;
  25. use think\db\exception\ModelNotFoundException;
  26. use think\facade\Route;
  27. use think\facade\Session;
  28. /**
  29. * 门店后台控制器
  30. * Class SystemAttachment
  31. * @package app\admin\controller\system
  32. *
  33. */
  34. class SystemStore extends AuthController
  35. {
  36. private $store_id;
  37. public function initialize()
  38. {
  39. parent::initialize(); // TODO: Change the autogenerated stub
  40. $this->store_id = Session::get('store_id', 0);
  41. if ($this->storeId) $this->store_id = $this->storeId;
  42. }
  43. /**
  44. * 门店列表
  45. * @throws Exception
  46. */
  47. public function list()
  48. {
  49. $where = UtilService::getMore([
  50. ['page', 1],
  51. ['limit', 20],
  52. ['name', ''],
  53. ['level', ''],
  54. ['excel', 0],
  55. ['parent_id', $this->request->param('pid', '')],
  56. ['type', $this->request->param('type')]
  57. ]);
  58. JsonService::successlayui(SystemStoreModel::getStoreList($where));
  59. }
  60. /**
  61. * 选择门店
  62. * @param string $origin
  63. * @return string
  64. * @throws Exception
  65. */
  66. public function index($origin = '')
  67. {
  68. // var_dump($origin);
  69. // Session::delete('store_id');
  70. if ($this->storeId) {
  71. $this->redirect($origin ?: 'order');
  72. }
  73. if ($this->request->get('store_id', 0)) {
  74. Session::set('store_id', $this->request->get('store_id', 0));
  75. $this->redirect($origin ?: 'index');
  76. }
  77. $type = $this->request->param('type');
  78. $show = SystemStoreModel::where('is_show', 1)->where('is_del', 0)->count();//显示中的门店
  79. $hide = SystemStoreModel::where('is_show', 0)->count();//隐藏的门店
  80. $recycle = SystemStoreModel::where('is_del', 1)->count();//删除的门店
  81. if ($type == null) $type = 1;
  82. // $this->assign('pid', $this->request->get('pid', 0));
  83. $this->assign(compact('type', 'show', 'hide', 'recycle'));
  84. $this->assign('origin', url($origin ?: 'index'));
  85. // $this->assign('level_list', $new);
  86. return $this->fetch();
  87. }
  88. /**
  89. * @return string
  90. * @throws Exception
  91. */
  92. public function order()
  93. {
  94. // var_dump($this->storeId);
  95. if (!$this->store_id) {
  96. $this->redirect(url('index', array('origin' => 'order'))->build());
  97. }
  98. $this->assign([
  99. 'year' => get_month(),
  100. 'store_name' => SystemStoreModel::get($this->store_id)['name'],
  101. 'real_name' => $this->request->get('real_name', ''),
  102. 'status' => $this->request->param('status', ''),
  103. 'orderCount' => StoreOrderModel::orderCount($this->store_id),
  104. 'payTypeCount' => StoreOrderModel::payTypeCount($this->store_id),
  105. ]);
  106. $this->assign('store_id', $this->store_id);
  107. return $this->fetch();
  108. }
  109. public function add_order()
  110. {
  111. if (!$this->store_id) {
  112. $this->failed('未知门店');
  113. }
  114. // $store_info = SystemStoreModel::get($this->store_id);
  115. $stocks = SystemStoreProductStock::getStockList(['excel' => 0, 'page' => 1, 'limit' => PHP_INT_MAX], $this->store_id)['data'];
  116. if ($stocks) $stocks = $stocks->toArray();
  117. // dump($stocks);
  118. $products = function () use ($stocks) {
  119. $return = [];
  120. foreach ($stocks as $v) {
  121. $return[] = ['label' => $v['product_info']['store_name'] . "|" . $v['product_info']['product_attr_info']['suk'] . "【库存:{$v['stock']}】", 'value' => $v['unique']];
  122. }
  123. return $return;
  124. };
  125. $f = array();
  126. $f[] = FormBuilder::select('uni', '商品')->setOptions($products())->filterable(true)->required();
  127. $f[] = FormBuilder::number('num', '数量')->step(1)->min(0)->required();
  128. $f[] = FormBuilder::input('real_name', '购买人')->required();
  129. $f[] = FormBuilder::input('phone', '手机号码')->required();
  130. $form = FormBuilder::make_post_form('创建订单', $f, Route::buildUrl('order_save'));
  131. $this->assign(compact('form'));
  132. return $this->fetch('public/form-builder');
  133. }
  134. public function order_save()
  135. {
  136. if (!$this->store_id) {
  137. JsonService::fail('未知门店');
  138. }
  139. $date = UtilService::postMore([
  140. ['uni', ''],
  141. ['num', 0],
  142. ['real_name', ''],
  143. ['phone', ''],
  144. ], $this->request);
  145. if (!$date['uni']) JsonService::fail('请选择规格');
  146. if (!$date['num']) JsonService::fail('请输入购买数量');
  147. $uid = User::where('admin_id', $this->adminId)->value('uid');
  148. if (!$uid) JsonService::fail('请先绑定前台账户');
  149. $product = StoreProductAttrValue::where('unique', $date['uni'])->find();
  150. if (!$product) JsonService::fail('规格不存在');
  151. $res = StoreCart::setCart($uid, $product['product_id'], $date['num'], $date['uni'], 'product', 1);
  152. if ($res) {
  153. $cartId = $res->id;
  154. $cartGroup = StoreCart::getUserProductCartList($uid, [$cartId], 1, $this->store_id);
  155. if (count($cartGroup['invalid'])) JsonService::fail($cartGroup['invalid'][0]['productInfo']['store_name'] . '已失效!');
  156. if (!$cartGroup['valid']) JsonService::fail('请提交购买的商品');
  157. $cartInfo = $cartGroup['valid'];
  158. $priceGroup = StoreOrder::getOrderPriceGroup($cartInfo, 0);
  159. $other = [
  160. 'offlinePostage' => sys_config('offline_postage'),
  161. 'integralRatio' => sys_config('integral_ratio'),
  162. 'store_id' => $this->store_id,
  163. ];
  164. $orderKey = StoreOrder::cacheOrderInfo($uid, $cartInfo, $priceGroup, $other);
  165. if (!$orderKey) JsonService::fail('生成订单错误!');
  166. if (StoreOrder::be(['order_id|unique' => $orderKey, 'uid' => $uid, 'is_del' => 0]))
  167. JsonService::fail('订单已生成');
  168. $order = StoreOrder::cacheKeyCreateOrder($uid, $orderKey, 0, 'offline', false, 0, '加盟店后台订单', 0, 0, 0, 0, 0, 0, false, 1, 2, $date['real_name'], $date['phone'], $this->store_id, 0, 1);
  169. if ($order === false) JsonService::fail(StoreOrder::getErrorInfo('订单生成失败'));
  170. else JsonService::success('订单已生成');
  171. } else {
  172. JsonService::fail('加入购物车失败');
  173. }
  174. }
  175. /**
  176. * 核销码核销
  177. * @param string $verify_code
  178. * @param int $is_confirm
  179. * @return string
  180. * @throws DataNotFoundException
  181. * @throws DbException
  182. * @throws ModelNotFoundException
  183. * @throws Exception
  184. */
  185. public function write_order($verify_code = '', $is_confirm = 0)
  186. {
  187. if ($this->request->isAjax()) {
  188. if (!$this->store_id) {
  189. JsonService::fail('未知门店');
  190. }
  191. if (!$verify_code) JsonService::fail('缺少核销码!');
  192. StoreOrderModel::beginTrans();
  193. $orderInfo = StoreOrderModel::where('store_id', $this->store_id)->where('verify_code', $verify_code)->where('paid', 1)->where('refund_status', 0)->find();
  194. if (!$orderInfo) JsonService::fail('核销订单不存在!');
  195. if ($orderInfo->status > 0) JsonService::fail('订单已核销!');
  196. if ($orderInfo->combination_id && $orderInfo->pink_id) {
  197. $res = StorePink::where('id', $orderInfo->pink_id)->where('status', '<>', 2)->count();
  198. if ($res) JsonService::fail('拼团订单暂未成功无法核销!');
  199. }
  200. if ($is_confirm == 0) {
  201. $orderInfo['nickname'] = User::where(['uid' => $orderInfo['uid']])->value('nickname');
  202. JsonService::successful($orderInfo);
  203. }
  204. $orderInfo->status = 2;
  205. if ($orderInfo->save()) {
  206. OrderRepository::storeProductOrderTakeDeliveryAdmin($orderInfo);
  207. StoreOrderStatus::setStatus($orderInfo->id, 'take_delivery', '已核销');
  208. //发送短信
  209. event('ShortMssageSend', [$orderInfo['order_id'], 'Receiving']);
  210. StoreOrderModel::commitTrans();
  211. JsonService::successful('核销成功!');
  212. } else {
  213. StoreOrderModel::rollbackTrans();
  214. JsonService::fail('核销失败');
  215. }
  216. } else {
  217. if (!$this->store_id) {
  218. $this->redirect(url('index', array('origin' => 'order'))->build());
  219. }
  220. $this->assign('is_layui', 1);
  221. return $this->fetch();
  222. }
  223. }
  224. /**
  225. * 获取订单列表
  226. * return json
  227. */
  228. public function order_list()
  229. {
  230. if (!$this->store_id) {
  231. JsonService::fail('未知门店');
  232. }
  233. $where = UtilService::getMore([
  234. ['status', ''],
  235. ['real_name', $this->request->param('real_name', '')],
  236. ['is_del', 0],
  237. ['data', ''],
  238. ['type', ''],
  239. ['pay_type', ''],
  240. ['order', ''],
  241. ['page', 1],
  242. ['limit', 20],
  243. ['excel', 0]
  244. ]);
  245. $where['store_id'] = $this->store_id;
  246. JsonService::successlayui(StoreOrderModel::OrderList($where));
  247. }
  248. /**
  249. *
  250. */
  251. public function getOrderBadge()
  252. {
  253. if (!$this->store_id) {
  254. JsonService::fail('未知门店');
  255. }
  256. $where = UtilService::postMore([
  257. ['status', ''],
  258. ['real_name', ''],
  259. ['is_del', 0],
  260. ['data', ''],
  261. ['type', ''],
  262. ['pay_type', ''],
  263. ['order', '']
  264. ]);
  265. $where['store_id'] = $this->store_id;
  266. JsonService::successful(StoreOrderModel::getBadge($where));
  267. }
  268. /**
  269. * @return string
  270. * @throws Exception
  271. */
  272. public function bill()
  273. {
  274. if (!$this->store_id) {
  275. $this->redirect(url('index', array('origin' => 'bill'))->build());
  276. }
  277. $this->assign('store_info', SystemStoreModel::get($this->store_id));
  278. $this->assign('id', $this->store_id);
  279. return $this->fetch();
  280. }
  281. public function store_bill_log_list($id)
  282. {
  283. $where = UtilService::getMore([
  284. ['page', 1],
  285. ['limit', 20],
  286. ]);
  287. JsonService::successlayui(SystemStoreBill::getList($where, $id));
  288. }
  289. /**
  290. * @return string
  291. * @throws Exception
  292. */
  293. public function cash()
  294. {
  295. if (!$this->store_id) {
  296. $this->redirect(url('index', array('origin' => 'cash'))->build());
  297. }
  298. $store_info = SystemStoreModel::get($this->store_id);
  299. $f = array();
  300. $f[] = FormBuilder::radio('extract_type', '提现方式', 'bank')->setOptions([['label' => '银行', 'value' => 'bank'],
  301. // ['label' => '支付宝', 'value' => 'alipay'], ['label' => '微信', 'value' => 'weixin']
  302. ]);
  303. $f[] = FormBuilder::number('money', '提现金额', 0)->col(24)->step(0.01)->min(0)->max($store_info['brokerage_price']);
  304. $f[] = FormBuilder::input('name', '姓名', $store_info['leader']);
  305. $f[] = FormBuilder::input('bankname', '开户银行', $store_info['bank_name']);
  306. $f[] = FormBuilder::input('cardnum', '银行卡号', $store_info['bank_card']);
  307. // $f[] = FormBuilder::input('alipay_code', '支付宝账号');
  308. // $f[] = FormBuilder::input('weixin', '微信号');
  309. $form = FormBuilder::make_post_form('发起提现', $f, Route::buildUrl('cash_save'));
  310. $this->assign(compact('form'));
  311. return $this->fetch('public/form-builder');
  312. }
  313. /**
  314. * 提现申请
  315. */
  316. public function cash_save()
  317. {
  318. if (!$this->store_id) {
  319. JsonService::fail('店铺异常');
  320. }
  321. $extractInfo = UtilService::postMore([
  322. ['alipay_code', ''],
  323. ['extract_type', ''],
  324. ['money', 0],
  325. ['name', ''],
  326. ['bankname', ''],
  327. ['cardnum', ''],
  328. ['weixin', ''],
  329. ], $this->request);
  330. if (!preg_match('/^(([1-9]\d*)|0)(\.\d{1-2})?$/', $extractInfo['money'])) JsonService::fail('提现金额输入有误');
  331. $store = SystemStoreModel::get($this->store_id);
  332. if ($extractInfo['money'] > $store['brokerage_price']) JsonService::fail('可提现佣金不足');
  333. if (!$extractInfo['cardnum'] == '')
  334. if (!preg_match('/^([1-9]{1})(\d{14}|\d{18})$/', $extractInfo['cardnum']))
  335. JsonService::fail('银行卡号输入有误');
  336. if (SystemStoreExtract::userExtract($store, $extractInfo))
  337. JsonService::successful('申请提现成功!');
  338. else
  339. JsonService::fail(SystemStoreExtract::getErrorInfo('提现失败'));
  340. }
  341. /**
  342. * @return string
  343. * @throws Exception
  344. */
  345. public function info()
  346. {
  347. if (!$this->store_id) {
  348. $this->redirect(url('index', array('origin' => 'info'))->build());
  349. }
  350. $this->assign('store_info', SystemStoreModel::getStoreInfo($this->store_id) ?: []);
  351. return $this->fetch();
  352. }
  353. //TODO 自提点管理
  354. /**
  355. * @return string
  356. * @throws Exception
  357. */
  358. public function point()
  359. {
  360. if (!$this->store_id) {
  361. $this->redirect(url('index', array('origin' => 'point'))->build());
  362. }
  363. $type = $this->request->param('type');
  364. $show = SystemStorePoint::where('store_id', $this->store_id)->where('is_show', 1)->where('is_del', 0)->count();//显示中的门店
  365. $hide = SystemStorePoint::where('store_id', $this->store_id)->where('is_show', 0)->count();//隐藏的门店
  366. $recycle = SystemStorePoint::where('store_id', $this->store_id)->where('is_del', 1)->count();//删除的门店
  367. if ($type == null) $type = 1;
  368. $id = $this->store_id;
  369. $this->assign(compact('type', 'show', 'hide', 'recycle', 'id'));
  370. return $this->fetch();
  371. }
  372. public function store_point_list($id)
  373. {
  374. $where = UtilService::getMore([
  375. ['page', 1],
  376. ['limit', 20],
  377. ['name', ''],
  378. ['excel', 0],
  379. ['type', $this->request->param('type')]
  380. ]);
  381. $where['store_id'] = $id;
  382. JsonService::successlayui(SystemStorePoint::getStoreList($where));
  383. }
  384. public function add_point($id = 0, $store_id = 0)
  385. {
  386. $store = SystemStorePoint::getStoreDispose($id);
  387. $this->assign(compact('store', 'store_id'));
  388. return $this->fetch();
  389. }
  390. /**
  391. * 保存修改门店信息
  392. * @param int $id
  393. */
  394. public function save_point($id = 0)
  395. {
  396. $data = UtilService::postMore([
  397. ['name', ''],
  398. ['phone', ''],
  399. ['address', ''],
  400. ['detailed_address', ''],
  401. ['latlng', ''],
  402. ['valid_time', []],
  403. ['day_time', []],
  404. ['store_id', 0],
  405. ]);
  406. // var_dump($data['store_id']);
  407. if (!$data['store_id'] || !SystemStoreModel::getStoreDispose($data['store_id'])) JsonService::fail('无效门店');
  408. SystemStorePoint::beginTrans();
  409. try {
  410. $data['address'] = implode(',', $data['address']);
  411. $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng'];
  412. if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) JsonService::fail('请选择门店位置');
  413. $data['latitude'] = $data['latlng'][0];
  414. $data['longitude'] = $data['latlng'][1];
  415. $data['valid_time'] = implode(' - ', $data['valid_time']);
  416. $data['day_time'] = implode(' - ', $data['day_time']);
  417. unset($data['latlng']);
  418. if ($id) {
  419. if (SystemStorePoint::where('id', $id)->update($data)) {
  420. SystemStorePoint::commitTrans();
  421. JsonService::success('修改成功');
  422. } else {
  423. SystemStorePoint::rollbackTrans();
  424. JsonService::fail('修改失败或者您没有修改什么!');
  425. }
  426. } else {
  427. $data['add_time'] = time();
  428. $data['is_show'] = 1;
  429. if ($res = SystemStorePoint::create($data)) {
  430. SystemStorePoint::commitTrans();
  431. JsonService::success('保存成功', ['id' => $res->id]);
  432. } else {
  433. SystemStorePoint::rollbackTrans();
  434. JsonService::fail('保存失败!');
  435. }
  436. }
  437. } catch (\Exception $e) {
  438. SystemStorePoint::rollbackTrans();
  439. JsonService::fail($e->getMessage());
  440. }
  441. }
  442. /**
  443. * 设置单个门店是否显示
  444. * @param string $is_show
  445. * @param string $id
  446. * @return json
  447. */
  448. public function set_point_show($is_show = '', $id = '')
  449. {
  450. ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
  451. $res = SystemStorePoint::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  452. if ($res) {
  453. return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功');
  454. } else {
  455. return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败');
  456. }
  457. }
  458. /**
  459. * 删除恢复门店
  460. * @param $id
  461. */
  462. public function point_delete($id)
  463. {
  464. if (!$id) return $this->failed('数据不存在');
  465. if (!SystemStorePoint::be(['id' => $id])) return $this->failed('数据不存在');
  466. if (SystemStorePoint::be(['id' => $id, 'is_del' => 1])) {
  467. $data['is_del'] = 0;
  468. if (!SystemStorePoint::edit($data, $id))
  469. return JsonService::fail(SystemStorePoint::getErrorInfo('恢复失败,请稍候再试!'));
  470. else
  471. return JsonService::successful('恢复门店成功!');
  472. } else {
  473. $data['is_del'] = 1;
  474. if (!SystemStorePoint::edit($data, $id))
  475. return JsonService::fail(SystemStorePoint::getErrorInfo('删除失败,请稍候再试!'));
  476. else
  477. return JsonService::successful('删除门店成功!');
  478. }
  479. }
  480. /**
  481. * 立即支付
  482. * @param $id
  483. */
  484. public function offline($id)
  485. {
  486. if (!$this->store_id) {
  487. JsonService::fail('未知门店');
  488. }
  489. $order = StoreOrder::get($id);
  490. if ($order['store_id'] != $this->store_id) {
  491. JsonService::fail('订单门店异常');
  492. }
  493. $res = StoreOrderModel::updateOffline($id);
  494. if ($res) {
  495. event('StoreProductOrderOffline', [$id]);
  496. StoreOrderStatus::setStatus($id, 'offline', '线下付款');
  497. return JsonService::successful('修改成功!');
  498. } else {
  499. return JsonService::fail(StoreOrderModel::getErrorInfo('修改失败!'));
  500. }
  501. }
  502. //TODO 库存管理
  503. }