SystemStore.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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);
  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. ];
  163. $orderKey = StoreOrder::cacheOrderInfo($uid, $cartInfo, $priceGroup, $other);
  164. if (!$orderKey) JsonService::fail('生成订单错误!');
  165. if (StoreOrder::be(['order_id|unique' => $orderKey, 'uid' => $uid, 'is_del' => 0]))
  166. JsonService::fail('订单已生成');
  167. $order = StoreOrder::cacheKeyCreateOrder($uid, $orderKey, 0, 'offline', false, 0, '加盟店后台订单', 0, 0, 0, 0, 0, false, 1, 2, $date['real_name'], $date['phone'], $this->store_id, 0, 1);
  168. if ($order === false) JsonService::fail(StoreOrder::getErrorInfo('订单生成失败'));
  169. else JsonService::success('订单已生成');
  170. } else {
  171. JsonService::fail('加入购物车失败');
  172. }
  173. }
  174. /**
  175. * 核销码核销
  176. * @param string $verify_code
  177. * @param int $is_confirm
  178. * @return string
  179. * @throws DataNotFoundException
  180. * @throws DbException
  181. * @throws ModelNotFoundException
  182. * @throws Exception
  183. */
  184. public function write_order($verify_code = '', $is_confirm = 0)
  185. {
  186. if ($this->request->isAjax()) {
  187. if (!$this->store_id) {
  188. JsonService::fail('未知门店');
  189. }
  190. if (!$verify_code) JsonService::fail('缺少核销码!');
  191. StoreOrderModel::beginTrans();
  192. $orderInfo = StoreOrderModel::where('store_id', $this->store_id)->where('verify_code', $verify_code)->where('paid', 1)->where('refund_status', 0)->find();
  193. if (!$orderInfo) JsonService::fail('核销订单不存在!');
  194. if ($orderInfo->status > 0) JsonService::fail('订单已核销!');
  195. if ($orderInfo->combination_id && $orderInfo->pink_id) {
  196. $res = StorePink::where('id', $orderInfo->pink_id)->where('status', '<>', 2)->count();
  197. if ($res) JsonService::fail('拼团订单暂未成功无法核销!');
  198. }
  199. if ($is_confirm == 0) {
  200. $orderInfo['nickname'] = User::where(['uid' => $orderInfo['uid']])->value('nickname');
  201. JsonService::successful($orderInfo);
  202. }
  203. $orderInfo->status = 2;
  204. if ($orderInfo->save()) {
  205. OrderRepository::storeProductOrderTakeDeliveryAdmin($orderInfo);
  206. StoreOrderStatus::setStatus($orderInfo->id, 'take_delivery', '已核销');
  207. //发送短信
  208. event('ShortMssageSend', [$orderInfo['order_id'], 'Receiving']);
  209. StoreOrderModel::commitTrans();
  210. JsonService::successful('核销成功!');
  211. } else {
  212. StoreOrderModel::rollbackTrans();
  213. JsonService::fail('核销失败');
  214. }
  215. } else {
  216. if (!$this->store_id) {
  217. $this->redirect(url('index', array('origin' => 'order'))->build());
  218. }
  219. $this->assign('is_layui', 1);
  220. return $this->fetch();
  221. }
  222. }
  223. /**
  224. * 获取订单列表
  225. * return json
  226. */
  227. public function order_list()
  228. {
  229. if (!$this->store_id) {
  230. JsonService::fail('未知门店');
  231. }
  232. $where = UtilService::getMore([
  233. ['status', ''],
  234. ['real_name', $this->request->param('real_name', '')],
  235. ['is_del', 0],
  236. ['data', ''],
  237. ['type', ''],
  238. ['pay_type', ''],
  239. ['order', ''],
  240. ['page', 1],
  241. ['limit', 20],
  242. ['excel', 0]
  243. ]);
  244. $where['store_id'] = $this->store_id;
  245. JsonService::successlayui(StoreOrderModel::OrderList($where));
  246. }
  247. /**
  248. *
  249. */
  250. public function getOrderBadge()
  251. {
  252. if (!$this->store_id) {
  253. JsonService::fail('未知门店');
  254. }
  255. $where = UtilService::postMore([
  256. ['status', ''],
  257. ['real_name', ''],
  258. ['is_del', 0],
  259. ['data', ''],
  260. ['type', ''],
  261. ['pay_type', ''],
  262. ['order', '']
  263. ]);
  264. $where['store_id'] = $this->store_id;
  265. JsonService::successful(StoreOrderModel::getBadge($where));
  266. }
  267. /**
  268. * @return string
  269. * @throws Exception
  270. */
  271. public function bill()
  272. {
  273. if (!$this->store_id) {
  274. $this->redirect(url('index', array('origin' => 'bill'))->build());
  275. }
  276. $this->assign('store_info', SystemStoreModel::get($this->store_id));
  277. $this->assign('id', $this->store_id);
  278. return $this->fetch();
  279. }
  280. public function store_bill_log_list($id)
  281. {
  282. $where = UtilService::getMore([
  283. ['page', 1],
  284. ['limit', 20],
  285. ]);
  286. JsonService::successlayui(SystemStoreBill::getList($where, $id));
  287. }
  288. /**
  289. * @return string
  290. * @throws Exception
  291. */
  292. public function cash()
  293. {
  294. if (!$this->store_id) {
  295. $this->redirect(url('index', array('origin' => 'cash'))->build());
  296. }
  297. $store_info = SystemStoreModel::get($this->store_id);
  298. $f = array();
  299. $f[] = FormBuilder::radio('extract_type', '提现方式', 'bank')->setOptions([['label' => '银行', 'value' => 'bank'],
  300. // ['label' => '支付宝', 'value' => 'alipay'], ['label' => '微信', 'value' => 'weixin']
  301. ]);
  302. $f[] = FormBuilder::number('money', '提现金额', 0)->col(24)->step(0.01)->min(0)->max($store_info['brokerage_price']);
  303. $f[] = FormBuilder::input('name', '姓名', $store_info['leader']);
  304. $f[] = FormBuilder::input('bankname', '开户银行', $store_info['bank_name']);
  305. $f[] = FormBuilder::input('cardnum', '银行卡号', $store_info['bank_card']);
  306. // $f[] = FormBuilder::input('alipay_code', '支付宝账号');
  307. // $f[] = FormBuilder::input('weixin', '微信号');
  308. $form = FormBuilder::make_post_form('发起提现', $f, Route::buildUrl('cash_save'));
  309. $this->assign(compact('form'));
  310. return $this->fetch('public/form-builder');
  311. }
  312. /**
  313. * 提现申请
  314. */
  315. public function cash_save()
  316. {
  317. if (!$this->store_id) {
  318. JsonService::fail('店铺异常');
  319. }
  320. $extractInfo = UtilService::postMore([
  321. ['alipay_code', ''],
  322. ['extract_type', ''],
  323. ['money', 0],
  324. ['name', ''],
  325. ['bankname', ''],
  326. ['cardnum', ''],
  327. ['weixin', ''],
  328. ], $this->request);
  329. if (!preg_match('/^(([1-9]\d*)|0)(\.\d{1-2})?$/', $extractInfo['money'])) JsonService::fail('提现金额输入有误');
  330. $store = SystemStoreModel::get($this->store_id);
  331. if ($extractInfo['money'] > $store['brokerage_price']) JsonService::fail('可提现佣金不足');
  332. if (!$extractInfo['cardnum'] == '')
  333. if (!preg_match('/^([1-9]{1})(\d{14}|\d{18})$/', $extractInfo['cardnum']))
  334. JsonService::fail('银行卡号输入有误');
  335. if (SystemStoreExtract::userExtract($store, $extractInfo))
  336. JsonService::successful('申请提现成功!');
  337. else
  338. JsonService::fail(SystemStoreExtract::getErrorInfo('提现失败'));
  339. }
  340. /**
  341. * @return string
  342. * @throws Exception
  343. */
  344. public function info()
  345. {
  346. if (!$this->store_id) {
  347. $this->redirect(url('index', array('origin' => 'info'))->build());
  348. }
  349. $this->assign('store_info', SystemStoreModel::getStoreInfo($this->store_id) ?: []);
  350. return $this->fetch();
  351. }
  352. //TODO 自提点管理
  353. /**
  354. * @return string
  355. * @throws Exception
  356. */
  357. public function point()
  358. {
  359. if (!$this->store_id) {
  360. $this->redirect(url('index', array('origin' => 'point'))->build());
  361. }
  362. $type = $this->request->param('type');
  363. $show = SystemStorePoint::where('store_id', $this->store_id)->where('is_show', 1)->where('is_del', 0)->count();//显示中的门店
  364. $hide = SystemStorePoint::where('store_id', $this->store_id)->where('is_show', 0)->count();//隐藏的门店
  365. $recycle = SystemStorePoint::where('store_id', $this->store_id)->where('is_del', 1)->count();//删除的门店
  366. if ($type == null) $type = 1;
  367. $id = $this->store_id;
  368. $this->assign(compact('type', 'show', 'hide', 'recycle', 'id'));
  369. return $this->fetch();
  370. }
  371. public function store_point_list($id)
  372. {
  373. $where = UtilService::getMore([
  374. ['page', 1],
  375. ['limit', 20],
  376. ['name', ''],
  377. ['excel', 0],
  378. ['type', $this->request->param('type')]
  379. ]);
  380. $where['store_id'] = $id;
  381. JsonService::successlayui(SystemStorePoint::getStoreList($where));
  382. }
  383. public function add_point($id = 0, $store_id = 0)
  384. {
  385. $store = SystemStorePoint::getStoreDispose($id);
  386. $this->assign(compact('store', 'store_id'));
  387. return $this->fetch();
  388. }
  389. /**
  390. * 保存修改门店信息
  391. * @param int $id
  392. */
  393. public function save_point($id = 0)
  394. {
  395. $data = UtilService::postMore([
  396. ['name', ''],
  397. ['phone', ''],
  398. ['address', ''],
  399. ['detailed_address', ''],
  400. ['latlng', ''],
  401. ['valid_time', []],
  402. ['day_time', []],
  403. ['store_id', 0],
  404. ]);
  405. // var_dump($data['store_id']);
  406. if (!$data['store_id'] || !SystemStoreModel::getStoreDispose($data['store_id'])) JsonService::fail('无效门店');
  407. SystemStorePoint::beginTrans();
  408. try {
  409. $data['address'] = implode(',', $data['address']);
  410. $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng'];
  411. if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) JsonService::fail('请选择门店位置');
  412. $data['latitude'] = $data['latlng'][0];
  413. $data['longitude'] = $data['latlng'][1];
  414. $data['valid_time'] = implode(' - ', $data['valid_time']);
  415. $data['day_time'] = implode(' - ', $data['day_time']);
  416. unset($data['latlng']);
  417. if ($id) {
  418. if (SystemStorePoint::where('id', $id)->update($data)) {
  419. SystemStorePoint::commitTrans();
  420. JsonService::success('修改成功');
  421. } else {
  422. SystemStorePoint::rollbackTrans();
  423. JsonService::fail('修改失败或者您没有修改什么!');
  424. }
  425. } else {
  426. $data['add_time'] = time();
  427. $data['is_show'] = 1;
  428. if ($res = SystemStorePoint::create($data)) {
  429. SystemStorePoint::commitTrans();
  430. JsonService::success('保存成功', ['id' => $res->id]);
  431. } else {
  432. SystemStorePoint::rollbackTrans();
  433. JsonService::fail('保存失败!');
  434. }
  435. }
  436. } catch (\Exception $e) {
  437. SystemStorePoint::rollbackTrans();
  438. JsonService::fail($e->getMessage());
  439. }
  440. }
  441. /**
  442. * 设置单个门店是否显示
  443. * @param string $is_show
  444. * @param string $id
  445. * @return json
  446. */
  447. public function set_point_show($is_show = '', $id = '')
  448. {
  449. ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
  450. $res = SystemStorePoint::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  451. if ($res) {
  452. return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功');
  453. } else {
  454. return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败');
  455. }
  456. }
  457. /**
  458. * 删除恢复门店
  459. * @param $id
  460. */
  461. public function point_delete($id)
  462. {
  463. if (!$id) return $this->failed('数据不存在');
  464. if (!SystemStorePoint::be(['id' => $id])) return $this->failed('数据不存在');
  465. if (SystemStorePoint::be(['id' => $id, 'is_del' => 1])) {
  466. $data['is_del'] = 0;
  467. if (!SystemStorePoint::edit($data, $id))
  468. return JsonService::fail(SystemStorePoint::getErrorInfo('恢复失败,请稍候再试!'));
  469. else
  470. return JsonService::successful('恢复门店成功!');
  471. } else {
  472. $data['is_del'] = 1;
  473. if (!SystemStorePoint::edit($data, $id))
  474. return JsonService::fail(SystemStorePoint::getErrorInfo('删除失败,请稍候再试!'));
  475. else
  476. return JsonService::successful('删除门店成功!');
  477. }
  478. }
  479. /**
  480. * 立即支付
  481. * @param $id
  482. */
  483. public function offline($id)
  484. {
  485. if (!$this->store_id) {
  486. JsonService::fail('未知门店');
  487. }
  488. $order = StoreOrder::get($id);
  489. if ($order['store_id'] != $this->store_id) {
  490. JsonService::fail('订单门店异常');
  491. }
  492. $res = StoreOrderModel::updateOffline($id);
  493. if ($res) {
  494. event('StoreProductOrderOffline', [$id]);
  495. StoreOrderStatus::setStatus($id, 'offline', '线下付款');
  496. return JsonService::successful('修改成功!');
  497. } else {
  498. return JsonService::fail(StoreOrderModel::getErrorInfo('修改失败!'));
  499. }
  500. }
  501. //TODO 库存管理
  502. }