123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660 |
- <?php
- namespace app\admin\controller\merchant;
- use app\admin\controller\AuthController;
- use app\admin\model\order\StoreOrder as StoreOrderModel;
- use app\admin\model\order\StoreOrderStatus;
- use app\admin\model\store\StoreProductAttr;
- use app\admin\model\store\StoreProductAttrValue;
- use app\admin\model\system\ShippingTemplates;
- use app\admin\model\system\SystemStoreBill;
- use app\admin\model\system\SystemStoreExtract;
- use app\admin\model\system\SystemStorePoint;
- use app\admin\model\system\SystemStoreProductStock;
- use app\admin\model\system\SystemStoreSender;
- use app\admin\model\ump\StorePink;
- use app\admin\model\user\User;
- use app\models\store\StoreCart;
- use app\models\store\StoreOrder;
- use crmeb\repositories\OrderRepository;
- use crmeb\services\FormBuilder;
- use crmeb\services\JsonService;
- use app\admin\model\system\SystemStore as SystemStoreModel;
- use crmeb\services\UtilService;
- use Exception;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\facade\Route;
- use think\facade\Session;
- /**
- * 门店后台控制器
- * Class SystemAttachment
- * @package app\admin\controller\system
- *
- */
- class SystemStore extends AuthController
- {
- private $store_id;
- public function initialize()
- {
- parent::initialize(); // TODO: Change the autogenerated stub
- $this->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 库存管理
- }
|