store_id = Session::get('store_id', 0); if ($this->storeId) $this->store_id = $this->storeId; } /** * 门店列表 * @throws Exception */ public function list() { $where = UtilService::getMore([ ['page', 1], ['limit', 20], ['name', ''], ['level', ''], ['excel', 0], ['parent_id', $this->request->param('pid', '')], ['type', $this->request->param('type')] ]); JsonService::successlayui(SystemStoreModel::getStoreList($where)); } /** * 选择门店 * @param string $origin * @return string * @throws Exception */ public function index($origin = '') { // var_dump($origin); // Session::delete('store_id'); if ($this->storeId) { $this->redirect($origin ?: 'order'); } if ($this->request->get('store_id', 0)) { Session::set('store_id', $this->request->get('store_id', 0)); $this->redirect($origin ?: 'index'); } $type = $this->request->param('type'); $show = SystemStoreModel::where('is_show', 1)->where('is_del', 0)->count();//显示中的门店 $hide = SystemStoreModel::where('is_show', 0)->count();//隐藏的门店 $recycle = SystemStoreModel::where('is_del', 1)->count();//删除的门店 if ($type == null) $type = 1; // $this->assign('pid', $this->request->get('pid', 0)); $this->assign(compact('type', 'show', 'hide', 'recycle')); $this->assign('origin', url($origin ?: 'index')); // $this->assign('level_list', $new); return $this->fetch(); } /** * @return string * @throws Exception */ public function order() { // var_dump($this->storeId); if (!$this->store_id) { $this->redirect(url('index', array('origin' => 'order'))->build()); } $this->assign([ 'year' => get_month(), 'store_name' => SystemStoreModel::get($this->store_id)['name'], 'real_name' => $this->request->get('real_name', ''), 'status' => $this->request->param('status', ''), 'orderCount' => StoreOrderModel::orderCount($this->store_id), 'payTypeCount' => StoreOrderModel::payTypeCount($this->store_id), ]); $this->assign('store_id', $this->store_id); return $this->fetch(); } public function add_order() { if (!$this->store_id) { $this->failed('未知门店'); } // $store_info = SystemStoreModel::get($this->store_id); $stocks = SystemStoreProductStock::getStockList(['excel' => 0, 'page' => 1, 'limit' => PHP_INT_MAX], $this->store_id)['data']; if ($stocks) $stocks = $stocks->toArray(); // dump($stocks); $products = function () use ($stocks) { $return = []; foreach ($stocks as $v) { $return[] = ['label' => $v['product_info']['store_name'] . "|" . $v['product_info']['product_attr_info']['suk'] . "【库存:{$v['stock']}】", 'value' => $v['unique']]; } return $return; }; $f = array(); $f[] = FormBuilder::select('uni', '商品')->setOptions($products())->filterable(true)->required(); $f[] = FormBuilder::number('num', '数量')->step(1)->min(0)->required(); $f[] = FormBuilder::input('real_name', '购买人')->required(); $f[] = FormBuilder::input('phone', '手机号码')->required(); $form = FormBuilder::make_post_form('创建订单', $f, Route::buildUrl('order_save')); $this->assign(compact('form')); return $this->fetch('public/form-builder'); } public function order_save() { if (!$this->store_id) { JsonService::fail('未知门店'); } $date = UtilService::postMore([ ['uni', ''], ['num', 0], ['real_name', ''], ['phone', ''], ], $this->request); if (!$date['uni']) JsonService::fail('请选择规格'); if (!$date['num']) JsonService::fail('请输入购买数量'); $uid = User::where('admin_id', $this->adminId)->value('uid'); if (!$uid) JsonService::fail('请先绑定前台账户'); $product = StoreProductAttrValue::where('unique', $date['uni'])->find(); if (!$product) JsonService::fail('规格不存在'); $res = StoreCart::setCart($uid, $product['product_id'], $date['num'], $date['uni'], 'product', 1); if ($res) { $cartId = $res->id; $cartGroup = StoreCart::getUserProductCartList($uid, [$cartId], 1, $this->store_id); if (count($cartGroup['invalid'])) JsonService::fail($cartGroup['invalid'][0]['productInfo']['store_name'] . '已失效!'); if (!$cartGroup['valid']) JsonService::fail('请提交购买的商品'); $cartInfo = $cartGroup['valid']; $priceGroup = StoreOrder::getOrderPriceGroup($cartInfo, 0); $other = [ 'offlinePostage' => sys_config('offline_postage'), 'integralRatio' => sys_config('integral_ratio'), 'store_id' => $this->store_id, ]; $orderKey = StoreOrder::cacheOrderInfo($uid, $cartInfo, $priceGroup, $other); if (!$orderKey) JsonService::fail('生成订单错误!'); if (StoreOrder::be(['order_id|unique' => $orderKey, 'uid' => $uid, 'is_del' => 0])) JsonService::fail('订单已生成'); $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); if ($order === false) JsonService::fail(StoreOrder::getErrorInfo('订单生成失败')); else JsonService::success('订单已生成'); } else { JsonService::fail('加入购物车失败'); } } /** * 核销码核销 * @param string $verify_code * @param int $is_confirm * @return string * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @throws Exception */ public function write_order($verify_code = '', $is_confirm = 0) { if ($this->request->isAjax()) { if (!$this->store_id) { JsonService::fail('未知门店'); } if (!$verify_code) JsonService::fail('缺少核销码!'); StoreOrderModel::beginTrans(); $orderInfo = StoreOrderModel::where('store_id', $this->store_id)->where('verify_code', $verify_code)->where('paid', 1)->where('refund_status', 0)->find(); if (!$orderInfo) JsonService::fail('核销订单不存在!'); if ($orderInfo->status > 0) JsonService::fail('订单已核销!'); if ($orderInfo->combination_id && $orderInfo->pink_id) { $res = StorePink::where('id', $orderInfo->pink_id)->where('status', '<>', 2)->count(); if ($res) JsonService::fail('拼团订单暂未成功无法核销!'); } if ($is_confirm == 0) { $orderInfo['nickname'] = User::where(['uid' => $orderInfo['uid']])->value('nickname'); JsonService::successful($orderInfo); } $orderInfo->status = 2; if ($orderInfo->save()) { OrderRepository::storeProductOrderTakeDeliveryAdmin($orderInfo); StoreOrderStatus::setStatus($orderInfo->id, 'take_delivery', '已核销'); //发送短信 event('ShortMssageSend', [$orderInfo['order_id'], 'Receiving']); StoreOrderModel::commitTrans(); JsonService::successful('核销成功!'); } else { StoreOrderModel::rollbackTrans(); JsonService::fail('核销失败'); } } else { if (!$this->store_id) { $this->redirect(url('index', array('origin' => 'order'))->build()); } $this->assign('is_layui', 1); return $this->fetch(); } } /** * 获取订单列表 * return json */ public function order_list() { if (!$this->store_id) { JsonService::fail('未知门店'); } $where = UtilService::getMore([ ['status', ''], ['real_name', $this->request->param('real_name', '')], ['is_del', 0], ['data', ''], ['type', ''], ['pay_type', ''], ['order', ''], ['page', 1], ['limit', 20], ['excel', 0] ]); $where['store_id'] = $this->store_id; JsonService::successlayui(StoreOrderModel::OrderList($where)); } /** * */ public function getOrderBadge() { if (!$this->store_id) { JsonService::fail('未知门店'); } $where = UtilService::postMore([ ['status', ''], ['real_name', ''], ['is_del', 0], ['data', ''], ['type', ''], ['pay_type', ''], ['order', ''] ]); $where['store_id'] = $this->store_id; JsonService::successful(StoreOrderModel::getBadge($where)); } /** * @return string * @throws Exception */ public function bill() { if (!$this->store_id) { $this->redirect(url('index', array('origin' => 'bill'))->build()); } $this->assign('store_info', SystemStoreModel::get($this->store_id)); $this->assign('id', $this->store_id); return $this->fetch(); } public function store_bill_log_list($id) { $where = UtilService::getMore([ ['page', 1], ['limit', 20], ]); JsonService::successlayui(SystemStoreBill::getList($where, $id)); } /** * @return string * @throws Exception */ public function cash() { if (!$this->store_id) { $this->redirect(url('index', array('origin' => 'cash'))->build()); } $store_info = SystemStoreModel::get($this->store_id); $f = array(); $f[] = FormBuilder::radio('extract_type', '提现方式', 'bank')->setOptions([['label' => '银行', 'value' => 'bank'], // ['label' => '支付宝', 'value' => 'alipay'], ['label' => '微信', 'value' => 'weixin'] ]); $f[] = FormBuilder::number('money', '提现金额', 0)->col(24)->step(0.01)->min(0)->max($store_info['brokerage_price']); $f[] = FormBuilder::input('name', '姓名', $store_info['leader']); $f[] = FormBuilder::input('bankname', '开户银行', $store_info['bank_name']); $f[] = FormBuilder::input('cardnum', '银行卡号', $store_info['bank_card']); // $f[] = FormBuilder::input('alipay_code', '支付宝账号'); // $f[] = FormBuilder::input('weixin', '微信号'); $form = FormBuilder::make_post_form('发起提现', $f, Route::buildUrl('cash_save')); $this->assign(compact('form')); return $this->fetch('public/form-builder'); } /** * 提现申请 */ public function cash_save() { if (!$this->store_id) { JsonService::fail('店铺异常'); } $extractInfo = UtilService::postMore([ ['alipay_code', ''], ['extract_type', ''], ['money', 0], ['name', ''], ['bankname', ''], ['cardnum', ''], ['weixin', ''], ], $this->request); if (!preg_match('/^(([1-9]\d*)|0)(\.\d{1-2})?$/', $extractInfo['money'])) JsonService::fail('提现金额输入有误'); $store = SystemStoreModel::get($this->store_id); if ($extractInfo['money'] > $store['brokerage_price']) JsonService::fail('可提现佣金不足'); if (!$extractInfo['cardnum'] == '') if (!preg_match('/^([1-9]{1})(\d{14}|\d{18})$/', $extractInfo['cardnum'])) JsonService::fail('银行卡号输入有误'); if (SystemStoreExtract::userExtract($store, $extractInfo)) JsonService::successful('申请提现成功!'); else JsonService::fail(SystemStoreExtract::getErrorInfo('提现失败')); } /** * @return string * @throws Exception */ public function info() { if (!$this->store_id) { $this->redirect(url('index', array('origin' => 'info'))->build()); } $this->assign('store_info', SystemStoreModel::getStoreInfo($this->store_id) ?: []); return $this->fetch(); } //TODO 自提点管理 /** * @return string * @throws Exception */ public function point() { if (!$this->store_id) { $this->redirect(url('index', array('origin' => 'point'))->build()); } $type = $this->request->param('type'); $show = SystemStorePoint::where('store_id', $this->store_id)->where('is_show', 1)->where('is_del', 0)->count();//显示中的门店 $hide = SystemStorePoint::where('store_id', $this->store_id)->where('is_show', 0)->count();//隐藏的门店 $recycle = SystemStorePoint::where('store_id', $this->store_id)->where('is_del', 1)->count();//删除的门店 if ($type == null) $type = 1; $id = $this->store_id; $this->assign(compact('type', 'show', 'hide', 'recycle', 'id')); return $this->fetch(); } public function store_point_list($id) { $where = UtilService::getMore([ ['page', 1], ['limit', 20], ['name', ''], ['excel', 0], ['type', $this->request->param('type')] ]); $where['store_id'] = $id; JsonService::successlayui(SystemStorePoint::getStoreList($where)); } public function add_point($id = 0, $store_id = 0) { $store = SystemStorePoint::getStoreDispose($id); $this->assign(compact('store', 'store_id')); return $this->fetch(); } /** * 保存修改门店信息 * @param int $id */ public function save_point($id = 0) { $data = UtilService::postMore([ ['name', ''], ['phone', ''], ['address', ''], ['detailed_address', ''], ['latlng', ''], ['valid_time', []], ['day_time', []], ['store_id', 0], ]); // var_dump($data['store_id']); if (!$data['store_id'] || !SystemStoreModel::getStoreDispose($data['store_id'])) JsonService::fail('无效门店'); SystemStorePoint::beginTrans(); try { $data['address'] = implode(',', $data['address']); $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng']; if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) JsonService::fail('请选择门店位置'); $data['latitude'] = $data['latlng'][0]; $data['longitude'] = $data['latlng'][1]; $data['valid_time'] = implode(' - ', $data['valid_time']); $data['day_time'] = implode(' - ', $data['day_time']); unset($data['latlng']); if ($id) { if (SystemStorePoint::where('id', $id)->update($data)) { SystemStorePoint::commitTrans(); JsonService::success('修改成功'); } else { SystemStorePoint::rollbackTrans(); JsonService::fail('修改失败或者您没有修改什么!'); } } else { $data['add_time'] = time(); $data['is_show'] = 1; if ($res = SystemStorePoint::create($data)) { SystemStorePoint::commitTrans(); JsonService::success('保存成功', ['id' => $res->id]); } else { SystemStorePoint::rollbackTrans(); JsonService::fail('保存失败!'); } } } catch (\Exception $e) { SystemStorePoint::rollbackTrans(); JsonService::fail($e->getMessage()); } } /** * 设置单个门店是否显示 * @param string $is_show * @param string $id * @return json */ public function set_point_show($is_show = '', $id = '') { ($is_show == '' || $id == '') && JsonService::fail('缺少参数'); $res = SystemStorePoint::where(['id' => $id])->update(['is_show' => (int)$is_show]); if ($res) { return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功'); } else { return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败'); } } /** * 删除恢复门店 * @param $id */ public function point_delete($id) { if (!$id) return $this->failed('数据不存在'); if (!SystemStorePoint::be(['id' => $id])) return $this->failed('数据不存在'); if (SystemStorePoint::be(['id' => $id, 'is_del' => 1])) { $data['is_del'] = 0; if (!SystemStorePoint::edit($data, $id)) return JsonService::fail(SystemStorePoint::getErrorInfo('恢复失败,请稍候再试!')); else return JsonService::successful('恢复门店成功!'); } else { $data['is_del'] = 1; if (!SystemStorePoint::edit($data, $id)) return JsonService::fail(SystemStorePoint::getErrorInfo('删除失败,请稍候再试!')); else return JsonService::successful('删除门店成功!'); } } //TODO 自提点管理 /** * @return string * @throws Exception */ public function sender() { if (!$this->store_id) { $this->redirect(url('index', array('origin' => 'sender'))->build()); } $type = $this->request->param('type'); $show = SystemStoreSender::where('store_id', $this->store_id)->where('is_show', 1)->where('is_del', 0)->count();//显示中的门店 $hide = SystemStoreSender::where('store_id', $this->store_id)->where('is_show', 0)->count();//隐藏的门店 $recycle = SystemStoreSender::where('store_id', $this->store_id)->where('is_del', 1)->count();//删除的门店 if ($type == null) $type = 1; $id = $this->store_id; $this->assign(compact('type', 'show', 'hide', 'recycle', 'id')); return $this->fetch(); } public function store_sender_list($id) { $where = UtilService::getMore([ ['page', 1], ['limit', 20], ['name', ''], ['excel', 0], ['type', $this->request->param('type')] ]); $where['store_id'] = $id; JsonService::successlayui(SystemStoreSender::getStoreList($where)); } public function add_sender($id = 0, $store_id = 0) { $store = SystemStoreSender::getStoreDispose($id); $this->assign(compact('store', 'store_id')); return $this->fetch(); } /** * 保存修改门店信息 * @param int $id */ public function save_sender($id = 0) { $data = UtilService::postMore([ ['name', ''], ['phone', ''], ['store_id', 0], ]); // var_dump($data['store_id']); if (!$data['store_id'] || !SystemStoreModel::getStoreDispose($data['store_id'])) JsonService::fail('无效门店'); SystemStoreSender::beginTrans(); try { if ($id) { if (SystemStoreSender::where('id', $id)->update($data)) { SystemStoreSender::commitTrans(); JsonService::success('修改成功'); } else { SystemStoreSender::rollbackTrans(); JsonService::fail('修改失败或者您没有修改什么!'); } } else { $data['add_time'] = time(); $data['is_show'] = 1; if ($res = SystemStoreSender::create($data)) { SystemStoreSender::commitTrans(); JsonService::success('保存成功', ['id' => $res->id]); } else { SystemStoreSender::rollbackTrans(); JsonService::fail('保存失败!'); } } } catch (\Exception $e) { SystemStoreSender::rollbackTrans(); JsonService::fail($e->getMessage()); } } /** * 设置单个门店是否显示 * @param string $is_show * @param string $id * @return json */ public function set_sender_show($is_show = '', $id = '') { ($is_show == '' || $id == '') && JsonService::fail('缺少参数'); $res = SystemStoreSender::where(['id' => $id])->update(['is_show' => (int)$is_show]); if ($res) { return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功'); } else { return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败'); } } /** * 删除恢复门店 * @param $id */ public function sender_delete($id) { if (!$id) return $this->failed('数据不存在'); if (!SystemStoreSender::be(['id' => $id])) return $this->failed('数据不存在'); if (SystemStoreSender::be(['id' => $id, 'is_del' => 1])) { $data['is_del'] = 0; if (!SystemStoreSender::edit($data, $id)) return JsonService::fail(SystemStoreSender::getErrorInfo('恢复失败,请稍候再试!')); else return JsonService::successful('恢复成功!'); } else { $data['is_del'] = 1; if (!SystemStoreSender::edit($data, $id)) return JsonService::fail(SystemStoreSender::getErrorInfo('删除失败,请稍候再试!')); else return JsonService::successful('删除成功!'); } } /** * 立即支付 * @param $id */ public function offline($id) { if (!$this->store_id) { JsonService::fail('未知门店'); } $order = StoreOrder::get($id); if ($order['store_id'] != $this->store_id) { JsonService::fail('订单门店异常'); } $res = StoreOrderModel::updateOffline($id); if ($res) { event('StoreProductOrderOffline', [$id]); StoreOrderStatus::setStatus($id, 'offline', '线下付款'); return JsonService::successful('修改成功!'); } else { return JsonService::fail(StoreOrderModel::getErrorInfo('修改失败!')); } } //TODO 库存管理 }