SystemStore.php 15 KB

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