1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093 |
- <?php
- namespace app\api\controller\admin;
- use app\admin\model\order\StoreOrder as StoreOrderModel;
- 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\user\UserRecharge;
- use app\models\system\SystemStore;
- use app\Request;
- use crmeb\basic\BaseModel;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use app\models\user\{
- User, UserBill
- };
- use crmeb\repositories\OrderRepository;
- use crmeb\repositories\ShortLetterRepositories;
- use crmeb\services\{
- MiniProgramService, UtilService, WechatService
- };
- use app\models\store\{Card, StoreCart, StoreExchangeOrder, StoreOrder, StoreOrderStatus, StorePink, StoreService};
- use app\models\system\SystemStoreStaff;
- /**
- * 订单类
- * Class StoreOrderController
- * @package app\api\controller\admin\order
- */
- class StoreOrderController
- {
- public function pointList(Request $request)
- {
- $uid = $request->uid();
- $where = UtilService::getMore([
- ['page', 1],
- ['limit', 20],
- ['type', 1]
- ]);
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if (!$info) {
- return app('json')->fail('权限不足');
- }
- // var_dump($info->toArray());
- $where['store_id'] = $info['store_id'];
- $where['excel'] = 0;
- // $where['type'] = $info['store_id'];
- return app('json')->successful('ok', SystemStorePoint::getStoreList($where));
- }
- public function senderList(Request $request)
- {
- $uid = $request->uid();
- $where = UtilService::getMore([
- ['page', 1],
- ['limit', 20],
- ]);
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if (!$info) {
- return app('json')->fail('权限不足');
- }
- // var_dump($info->toArray());
- $where['store_id'] = $info['store_id'];
- $where['excel'] = 0;
- // $where['type'] = $info['store_id'];
- return app('json')->successful('ok', SystemStoreSender::getStoreList($where));
- }
- public function pointDetail($id, Request $request)
- {
- $uid = $request->uid();
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if (!$info) {
- return app('json')->fail('权限不足');
- }
- return app('json')->successful('ok', ['data' => SystemStorePoint::getStoreDispose($id)]);
- }
- public function sendCard(Request $request)
- {
- $uid = $request->uid();
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if (!$info) {
- return app('json')->fail('权限不足');
- }
- $code = UtilService::postMore(['code']);
- $res = Card::sendCard($info['store_id'], $code['code']);
- if ($res)
- return app('json')->successful('赠送成功');
- else return app('json')->fail(Card::getErrorInfo('赠送失败'));
- }
- public function sendCardList(Request $request)
- {
- $uid = $request->uid();
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if (!$info) {
- return app('json')->fail('权限不足');
- }
- $where = UtilService::getMore([['page', 1], ['limit', 10], ['status', '']]);
- $where['store_id'] = $info['store_id'];
- $list = Card::systemPage($where);
- unset($where['page']);
- unset($where['limit']);
- $count = Card::where($where)->count();
- return app('json')->success('ok', compact('list', 'count'));
- }
- public function checkCard(Request $request)
- {
- $uid = $request->uid();
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if (!$info) {
- return app('json')->fail('权限不足');
- }
- $code = UtilService::postMore(['code', 'password']);
- $res = Card::checkCard($info['store_id'], $code['code'], $code['password']);
- if ($res)
- return app('json')->successful('核销成功');
- else return app('json')->fail(Card::getErrorInfo('核销失败'));
- }
- public function addDetail(Request $request, $id = 0)
- {
- $uid = $request->uid();
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if (!$info) {
- return app('json')->fail('权限不足');
- }
- $data = UtilService::postMore([
- ['name', ''],
- ['phone', ''],
- ['address', ''],
- ['detailed_address', ''],
- ['latlng', ''],
- ['valid_time', []],
- ['day_time', []],
- ['is_show', 0],
- ]);
- $data['store_id'] = $info['store_id'];
- $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])) return app('json')->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) {
- $store_info = SystemStorePoint::get($id);
- if ($store_info['store_id'] != $info['store_id']) {
- return app('json')->fail('权限不足');
- }
- if (SystemStorePoint::where('id', $id)->update($data)) {
- SystemStorePoint::commitTrans();
- return app('json')->successful('修改成功');
- } else {
- SystemStorePoint::rollbackTrans();
- return app('json')->fail('修改失败或者您没有修改什么!');
- }
- } else {
- $data['add_time'] = time();
- $data['is_show'] = 1;
- if ($res = SystemStorePoint::create($data)) {
- SystemStorePoint::commitTrans();
- return app('json')->successful('保存成功', ['id' => $res->id]);
- } else {
- SystemStorePoint::rollbackTrans();
- return app('json')->fail('保存失败!');
- }
- }
- }
- /**
- * 删除恢复门店
- * @param $id
- */
- public function point_delete($id, Request $request)
- {
- if (!$id) return app('json')->fail('数据不存在');
- if (!SystemStorePoint::be(['id' => $id])) return app('json')->fail('数据不存在');
- $uid = $request->uid();
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if (!$info) {
- return app('json')->fail('权限不足');
- }
- $store_info = SystemStorePoint::get($id);
- if ($store_info['store_id'] != $info['store_id']) {
- return app('json')->fail('权限不足');
- }
- if (SystemStorePoint::be(['id' => $id, 'is_del' => 1])) {
- $data['is_del'] = 0;
- if (!SystemStorePoint::edit($data, $id))
- return app('json')->fail(SystemStorePoint::getErrorInfo('恢复失败,请稍候再试!'));
- else
- return app('json')->success('恢复门店成功!');
- } else {
- $data['is_del'] = 1;
- if (!SystemStorePoint::edit($data, $id))
- return app('json')->fail(SystemStorePoint::getErrorInfo('删除失败,请稍候再试!'));
- else
- return app('json')->success('删除门店成功!');
- }
- }
- public function stockList(Request $request)
- {
- $uid = $request->uid();
- $where = UtilService::getMore([
- ['type', ''],
- ['page', 1],
- ['limit', 20],
- ['start_time', ''],
- ['end_time', ''],
- ]);
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if (!$info) {
- return app('json')->fail('权限不足');
- }
- return app('json')->successful('ok', ['warning' => SystemStoreProductStock::getWarning($info['store_id']), 'data' => SystemStoreProductStock::getStockList($where, $info['store_id'])]);
- }
- public function billList(Request $request)
- {
- $uid = $request->uid();
- $where = UtilService::getMore([
- ['page', 1],
- ['limit', 20],
- ['type', ''],
- ['time', ''],
- ]);
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if (!$info) {
- return app('json')->fail('权限不足');
- }
- return app('json')->successful('ok', ['switch' => sys_config('withdraw_switch', 0), 'all' => SystemStore::get($info['store_id'])['brokerage_price'], 'data' => SystemStoreBill::getList($where, $info['store_id'])]);
- }
- public function cash(Request $request)
- {
- $uid = $request->uid();
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if (!$info) {
- return app('json')->fail('权限不足');
- }
- if (!in_array(sys_config('default_store_admin', 7), explode(',', $request->admin_info()['roles']))) {
- return app('json')->fail('权限不足');
- }
- $store = SystemStore::get($info['store_id']);
- $extractInfo = UtilService::postMore([
- ['money', 0],
- ['bankname', ''],
- ['name', ''],
- ['cardnum', ''],
- ], $request);
- $extractInfo['extract_type'] = 'bank';
- $extractInfo['name'] = $extractInfo['name'] ?? $store['leader'];
- if (!preg_match('/^(([1-9]\d*)|0)(\.\d{1-2})?$/', $extractInfo['money'])) app('json')->fail('提现金额输入有误');
- $store = SystemStore::get($info['store_id']);
- if ($extractInfo['money'] > $store['brokerage_price']) return app('json')->fail('可提现佣金不足');
- if (!$extractInfo['cardnum'] == '')
- if (!preg_match('/^([1-9]{1})(\d{14}|\d{18})$/', $extractInfo['cardnum']))
- return app('json')->fail('银行卡号有误');
- if (SystemStoreExtract::userExtract($store, $extractInfo))
- return app('json')->successful('申请提现成功!');
- else
- return app('json')->fail(SystemStoreExtract::getErrorInfo('提现失败'));
- }
- public function childrenList(Request $request)
- {
- $uid = $request->uid();
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if (!$info) {
- return app('json')->fail('权限不足');
- }
- $where = UtilService::getMore([
- ['page', 1],
- ['limit', 20],
- ['name', ''],
- ['level', ''],
- ['excel', 0],
- ['type', ''],
- ['parent_id', 0],
- ]);
- $where['self_store'] = $info['store_id'];
- return app('json')->successful('ok', \app\admin\model\system\SystemStore::getStoreList($where));
- }
- /**
- * 订单数据统计
- * @param Request $request
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function statistics(Request $request)
- {
- $uid = $request->uid();
- // if (!StoreService::orderServiceStatus($uid))
- // return app('json')->fail('权限不足');
- $where = [];
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if ($info) {
- $where['store_id'] = $info['store_id'];
- }
- $dataCount = StoreOrder::getOrderDataAdmin($where);
- $dataPrice = StoreOrder::getOrderTimeData($where);
- $data = array_merge($dataCount, $dataPrice);
- return app('json')->successful($data);
- }
- /**
- * 订单每月统计数据
- * @param Request $request
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function data(Request $request)
- {
- $uid = $request->uid();
- // if (!StoreService::orderServiceStatus($uid))
- // return app('json')->fail('权限不足');
- list($page, $limit, $start, $stop) = UtilService::getMore([
- ['page', 1],
- ['limit', 7],
- ['start', ''],
- ['stop', '']
- ], $request, true);
- $where = [];
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if ($info) {
- $where['store_id'] = $info['store_id'];
- }
- if (!$limit) return app('json')->successful([]);
- $data = StoreOrder::getOrderDataPriceCount($page, $limit, $start, $stop, $where);
- if ($data) return app('json')->successful($data->toArray());
- return app('json')->successful([]);
- }
- /**
- * 订单列表
- * @param Request $request
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function lst(Request $request)
- {
- $uid = $request->uid();
- // if (!StoreService::orderServiceStatus($uid))
- // return app('json')->fail('权限不足');
- $where = UtilService::getMore([
- ['status', ''],
- ['user_phone', ''],
- ['is_del', 0],
- ['data', ''],
- ['type', ''],
- ['shipping_type', ''],
- ['order', ''],
- ['page', 0],
- ['limit', 0]
- ], $request);
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if ($info) {
- $where['store_id'] = $info['store_id'];
- }
- if (!$where['limit']) return app('json')->successful([]);
- return app('json')->successful(StoreOrder::orderList($where));
- }
- /**
- * 订单列表
- * @param Request $request
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function exchange_order_lst(Request $request)
- {
- $uid = $request->uid();
- $where = UtilService::getMore([
- ['status', ''],
- ['user_phone', ''],
- ['is_del', 0],
- ['data', ''],
- ['order', ''],
- ['page', 0],
- ['limit', 0]
- ], $request);
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if ($info) {
- $where['store_id'] = $info['store_id'];
- }
- if (!$where['limit']) return app('json')->successful([]);
- return app('json')->successful(StoreExchangeOrder::orderList($where));
- }
- /**
- * 订单详情
- * @param Request $request
- * @param $orderId
- * @return mixed
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public function detail(Request $request, $orderId)
- {
- $uid = $request->uid();
- // if (!StoreService::orderServiceStatus($uid))
- // return app('json')->fail('权限不足');
- $where = [];
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if ($info) {
- $where['store_id'] = $info['store_id'];
- }
- $order = StoreOrder::getAdminOrderDetail($orderId, 'id,uid,order_id,add_time,status,total_num,total_price,total_postage,pay_price,pay_postage,paid,refund_status,remark,pink_id,combination_id,mark,seckill_id,bargain_id,delivery_type,pay_type,real_name,user_phone,user_address,coupon_price,freight_price,delivery_name,delivery_type,delivery_id,store_id,point_id', $where);
- if (!$order) return app('json')->fail('订单不存在');
- $order = $order->toArray();
- $nickname = User::getUserInfo($order['uid'], 'nickname')['nickname'];
- $orderInfo = StoreOrder::tidyAdminOrder([$order], true)[0];
- unset($orderInfo['uid'], $orderInfo['seckill_id'], $orderInfo['pink_id'], $orderInfo['combination_id'], $orderInfo['bargain_id'], $orderInfo['status'], $orderInfo['total_postage']);
- $orderInfo['nickname'] = $nickname;
- return app('json')->successful('ok', $orderInfo);
- }
- /**
- * 订单发货获取订单信息
- * @param Request $request
- * @param $orderId
- * @return mixed
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public function delivery_gain(Request $request, $orderId)
- {
- $uid = $request->uid();
- // if (!StoreService::orderServiceStatus($uid))
- // return app('json')->fail('权限不足');
- $where = [];
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if ($info) {
- $where['store_id'] = $info['store_id'];
- }
- $order = StoreOrder::getAdminOrderDetail($orderId, 'real_name,user_phone,user_address,order_id,uid,status,paid', $where);
- if (!$order) return app('json')->fail('订单不存在');
- if ($order['paid']) {
- $order['nickname'] = User::getUserInfo($order['uid'], 'nickname')['nickname'];
- $order = $order->hidden(['uid', 'status', 'paid'])->toArray();
- return app('json')->successful('ok', $order);
- }
- return app('json')->fail('状态错误');
- }
- /**
- * 订单发货
- * @param Request $request
- * @return mixed
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws DbException
- */
- public function delivery_keep(Request $request)
- {
- $uid = $request->uid();
- // if (!StoreService::orderServiceStatus($uid))
- // return app('json')->fail('权限不足');
- list($order_id, $delivery_type, $delivery_name, $delivery_id) = UtilService::postMore([
- ['order_id', ''],
- ['delivery_type', 0],
- ['delivery_name', ''],
- ['delivery_id', ''],
- ], $request, true);
- $where = [];
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if ($info) {
- $where['store_id'] = $info['store_id'];
- }
- $order = StoreOrder::getAdminOrderDetail($order_id, 'id,status,paid', $where);
- if (!$order) return app('json')->fail('订单不存在');
- if (!$order['status'] && $order['paid']) {
- $deliveryTypeArr = isset($where['store_id']) ? ['send'] : ['send', 'express', 'fictitious'];
- if (!strlen(trim($delivery_type))) return app('json')->fail('请填写发货方式');
- if (!in_array($delivery_type, $deliveryTypeArr)) return app('json')->fail('发货方式错误');
- if ($delivery_type == 'express') {
- if (!strlen(trim($delivery_name))) return app('json')->fail('请选择快递公司');
- if (!strlen(trim($delivery_id))) return app('json')->fail('请填写快递单号');
- }
- if ($delivery_type == 'send') {
- if (!strlen(trim($delivery_name))) return app('json')->fail('请填写发货人');
- if (!strlen(trim($delivery_id))) return app('json')->fail('请填写发货手机号');
- }
- $data['status'] = 1;
- $data['delivery_type'] = $delivery_type;
- $data['delivery_name'] = $delivery_name;
- $data['delivery_id'] = $delivery_id;
- $res = StoreOrder::edit($data, $order['id'], 'id');
- if ($res) {
- if ($delivery_type == 'express') {
- StoreOrderStatus::status($order['id'], 'delivery_goods', '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']);
- } else if ($delivery_type == 'send') {
- StoreOrderStatus::status($order['id'], 'delivery', '已配送 发货人:' . $delivery_name . ' 发货人电话:' . $delivery_id);
- } else if ($delivery_type == 'fictitious') {
- StoreOrderStatus::status($order['id'], 'delivery_fictitious', '虚拟产品已发货');
- }
- }
- event('StoreProductOrderDeliveryGoodsAfter', [$data, $order['id']]);
- return app('json')->successful('发货成功!');
- }
- return app('json')->fail('状态错误');
- }
- /**
- * 订单改价
- * @param Request $request
- * @return mixed
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public function price(Request $request)
- {
- $uid = $request->uid();
- // if (!StoreService::orderServiceStatus($uid))
- // return app('json')->fail('权限不足');
- list($order_id, $price) = UtilService::postMore([
- ['order_id', ''],
- ['price', '']
- ], $request, true);
- $where = [];
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if ($info) {
- $where['store_id'] = $info['store_id'];
- }
- $order = StoreOrder::getAdminOrderDetail($order_id, 'id,paid,user_phone,pay_price,order_id,total_price,total_postage,pay_postage,gain_integral', $where);
- if (!$order) return app('json')->fail('订单不存在');
- $order = $order->toArray();
- if (!$order['paid']) {
- if ($price === '') return app('json')->fail('请填写实际支付金额');
- if ($price < 0) return app('json')->fail('实际支付金额不能小于0元');
- if ($order['pay_price'] == $price) return app('json')->successful('修改成功');
- $order['order_id'] = StoreOrder::changeOrderId($order['order_id']);
- if (!StoreOrder::edit(['pay_price' => $price, 'order_id' => $order['order_id']], $order['id'], 'id'))
- return app('json')->fail('状态错误');
- $order['pay_price'] = $price;
- event('StoreProductOrderEditAfter', [$order, $order['id']]);
- StoreOrderStatus::status($order['id'], 'order_edit', '修改实际支付金额' . $price);
- //改价短信提醒
- if ($price != $order['pay_price']) {
- $switch = sys_config('price_revision_switch') ? true : false;
- ShortLetterRepositories::send($switch, $order['user_phone'], ['order_id' => $order_id, 'pay_price' => $order['pay_price']], 'PRICE_REVISION_CODE');
- }
- return app('json')->successful('修改成功');
- }
- return app('json')->fail('状态错误');
- }
- /**
- * 订单备注
- * @param Request $request
- * @return mixed
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public function remark(Request $request)
- {
- $uid = $request->uid();
- // if (!StoreService::orderServiceStatus($uid))
- // return app('json')->fail('权限不足');
- list($order_id, $remark) = UtilService::postMore([
- ['order_id', ''],
- ['remark', '']
- ], $request, true);
- $where = [];
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if ($info) {
- $where['store_id'] = $info['store_id'];
- }
- $order = StoreOrder::getAdminOrderDetail($order_id, 'id', $where);
- if (!$order) return app('json')->fail('订单不存在');
- $order = $order->toArray();
- if (!strlen(trim($remark))) return app('json')->fail('请填写备注内容');
- if (!StoreOrder::edit(['remark' => $remark], $order['id']))
- return app('json')->fail('备注失败');
- return app('json')->successful('备注成功');
- }
- /**
- * 订单交易额/订单数量时间统计
- * @param Request $request
- * @return bool
- */
- public function time(Request $request)
- {
- $uid = $request->uid();
- // if (!StoreService::orderServiceStatus($uid))
- // return app('json')->fail('权限不足');
- list($start, $stop, $type) = UtilService::getMore([
- ['start', strtotime(date('Y-m'))],
- ['stop', time()],
- ['type', 1]
- ], $request, true);
- $where = [];
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if ($info) {
- $where['store_id'] = $info['store_id'];
- }
- if ($start == $stop) {
- return app('json')->fail('开始时间不能等于结束时间');
- }
- if ($start > $stop) {
- $middle = $stop;
- $stop = $start;
- $start = $middle;
- }
- $space = bcsub($stop, $start, 0);//间隔时间段
- $front = bcsub($start, $space, 0);//第一个时间段
- if ($type == 1) {//销售额
- $frontPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($front, $start, $where);
- $afterPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($start, $stop, $where);
- $chartInfo = StoreOrder::chartTimePrice($start, $stop, $where);
- $data['chart'] = $chartInfo;//营业额图表数据
- $data['time'] = $afterPrice;//时间区间营业额
- $increase = (float)bcsub($afterPrice, $frontPrice, 2); //同比上个时间区间增长营业额
- $growthRate = abs($increase);
- if ($growthRate == 0) $data['growth_rate'] = 0;
- else if ($frontPrice == 0) $data['growth_rate'] = $growthRate;
- else $data['growth_rate'] = (int)bcmul(bcdiv($growthRate, $frontPrice, 2), 100, 0);//时间区间增长率
- $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
- $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
- } else {//订单数
- $frontNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($front, $start, $where);
- $afterNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($start, $stop, $where);
- $chartInfo = StoreOrder::chartTimeNumber($start, $stop, $where);
- $data['chart'] = $chartInfo;//订单数图表数据
- $data['time'] = $afterNumber;//时间区间订单数
- $increase = (int)bcsub($afterNumber, $frontNumber, 0); //同比上个时间区间增长订单数
- $growthRate = abs($increase);
- if ($growthRate == 0) $data['growth_rate'] = 0;
- else if ($frontNumber == 0) $data['growth_rate'] = $growthRate;
- else $data['growth_rate'] = (int)bcmul(bcdiv($growthRate, $frontNumber, 2), 100, 0);//时间区间增长率
- $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
- $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
- }
- return app('json')->successful($data);
- }
- public function month(Request $request)
- {
- $uid = $request->uid();
- $where = [];
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if ($info) {
- $where['store_id'] = $info['store_id'];
- }
- $start = StoreOrder::min('add_time');
- $data = [];
- while ($start < time()) {
- $start_month = date('Y-m', $start);
- $end = strtotime('+1month', strtotime($start_month)) - 1;
- $frontPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($start, $end, $where);
- $frontNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($start, $end, $where);
- $start = $end + 1;
- $data[$start_month] = compact('frontPrice', 'frontNumber');
- }
- return app('json')->successful($data);
- }
- /**
- * 订单支付
- * @param Request $request
- * @return mixed
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public function offline(Request $request)
- {
- $uid = $request->uid();
- list($orderId) = UtilService::postMore([['order_id', '']], $request, true);
- $where = [];
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if ($info) {
- $where['store_id'] = $info['store_id'];
- }
- $orderInfo = StoreOrder::getAdminOrderDetail($orderId, 'id', $where);
- if (!$orderInfo) return app('json')->fail('参数错误');
- $id = $orderInfo->id;
- $res = StoreOrder::updateOffline($id);
- if ($res) {
- event('StoreProductOrderOffline', [$id]);
- StoreOrderStatus::status($id, 'offline', '线下付款');
- return app('json')->successful('修改成功!');
- }
- return app('json')->fail(StoreOrder::getErrorInfo('修改失败!'));
- }
- /**
- * 订单退款
- * @param Request $request
- * @return mixed
- * @throws \think\Exception
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public function refund(Request $request)
- {
- $uid = $request->uid();
- list($orderId, $price, $type) = UtilService::postMore([
- ['order_id', ''],
- ['price', 0],
- ['type', 1],
- ], $request, true);
- $where = [];
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if ($info) {
- $where['store_id'] = $info['store_id'];
- }
- if (!strlen(trim($orderId))) return app('json')->fail('参数错误');
- $orderInfo = StoreOrder::getAdminOrderDetail($orderId, '*', $where);
- if (!$orderInfo) return app('json')->fail('数据不存在!');
- $orderInfo = $orderInfo->toArray();
- if ($type == 1)
- $data['refund_status'] = 2;
- else if ($type == 2)
- $data['refund_status'] = 0;
- else
- return app('json')->fail('退款修改状态错误');
- if ($orderInfo['pay_price'] == 0 || $type == 2) {
- StoreOrder::update($data, ['order_id' => $orderId]);
- return app('json')->successful('修改退款状态成功!');
- }
- if ($orderInfo['pay_price'] == $orderInfo['refund_price']) return app('json')->fail('已退完支付金额!不能再退款了');
- if (!$price) return app('json')->fail('请输入退款金额');
- $data['refund_price'] = bcadd($price, $orderInfo['refund_price'], 2);
- $bj = bccomp((float)$orderInfo['pay_price'], (float)$data['refund_price'], 2);
- if ($bj < 0) return app('json')->fail('退款金额大于支付金额,请修改退款金额');
- $refundData['pay_price'] = bcadd($orderInfo['pay_price'], $orderInfo['deposit'], 2);
- $refundData['refund_price'] = $price;
- $refundData['refund_id'] = $orderInfo['order_id'] . time();
- if ($orderInfo['pay_type'] == 'weixin') {
- if ($orderInfo['is_channel'] == 1) {// 小程序
- try {
- MiniProgramService::payOrderRefund($orderInfo['order_id'], $refundData);
- } catch (\Exception $e) {
- return app('json')->fail($e->getMessage());
- }
- } else {// 公众号
- try {
- WechatService::payOrderRefund($orderInfo['order_id'], $refundData);
- } catch (\Exception $e) {
- return app('json')->fail($e->getMessage());
- }
- }
- } else if ($orderInfo['pay_type'] == 'yue') {//余额
- StoreOrder::beginTrans();
- $userInfo = User::getUserInfo($orderInfo['uid'], 'now_money');
- if (!$userInfo) {
- StoreOrder::rollbackTrans();
- return app('json')->fail('订单用户不存在');
- }
- $res1 = User::bcInc($orderInfo['uid'], 'now_money', $price, 'uid');
- $res2 = $res2 = UserBill::income('商品退款', $orderInfo['uid'], 'now_money', 'pay_product_refund', $price, $orderInfo['id'], bcadd($userInfo['now_money'], $price, 2), '订单退款到余额' . floatval($price) . '元');
- try {
- OrderRepository::storeOrderYueRefund($orderInfo, $refundData);
- } catch (\Exception $e) {
- StoreOrder::rollbackTrans();
- return app('json')->fail($e->getMessage());
- }
- $res = $res1 && $res2;
- StoreOrder::checkTrans($res);
- if (!$res) return app('json')->fail('余额退款失败!');
- }
- $resEdit = StoreOrder::edit($data, $orderInfo['id'], 'id');
- if ($resEdit) {
- $data['type'] = $type;
- if ($data['type'] == 1) StorePink::setRefundPink($orderInfo['id']);
- try {
- OrderRepository::storeProductOrderRefundY($data, $orderInfo['id']);
- } catch (\Exception $e) {
- return app('json')->fail($e->getMessage());
- }
- StoreOrderStatus::status($orderInfo['id'], 'refund_price', '退款给用户' . $price . '元');
- SystemStoreBill::where('category', 'brokerage_price')
- //->where('type', 'brokerage')
- ->where('link_id', $orderId)
- ->where('pm', 1)
- ->where('status', 0)
- ->update(['status' => -1]);
- \app\admin\model\user\UserBill::where('category', 'now_money')
- ->where('type', 'brokerage')
- ->where('link_id', $orderId)
- ->where('pm', 1)
- ->where('status', 0)
- ->update(['status' => -1]);
- //退佣金
- $brokerage_list = UserBill::where('category', 'now_money')
- ->where('type', 'brokerage')
- ->where('link_id', $orderId)
- ->where('pm', 1)
- ->where('status', 1)
- ->select()->toArray();
- if ($brokerage_list)
- foreach ($brokerage_list as $item) {
- $usermoney = User::where('uid', $item['uid'])->value('brokerage_price');
- if ($item['number'] > $usermoney)
- $item['number'] = $usermoney;
- User::bcDec($item['uid'], 'brokerage_price', $item['number'], 'uid');
- UserBill::expend('退款退佣金', $item['uid'], 'now_money', 'brokerage', $item['number'], $orderId, bcsub($usermoney, $item['number'], 2), '订单退款扣除佣金' . floatval($item['number']) . '元');
- }
- return app('json')->successful('修改成功!');
- } else {
- StoreOrderStatus::status($orderInfo['id'], 'refund_price', '退款给用户' . $price . '元失败');
- return app('json')->successful('修改失败!');
- }
- }
- /**
- * 门店核销
- * @param Request $request
- * @return
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function order_verific(Request $request)
- {
- $uid = $request->uid();
- list($verify_code, $is_confirm) = UtilService::postMore([
- ['verify_code', ''],
- ['is_confirm', 0]
- ], $request, true);
- if (!$verify_code) return app('json')->fail('缺少核销码');
- $orderInfo = StoreOrder::where('verify_code', $verify_code)->where('store_id', 'in', [SystemStoreStaff::where('uid', $uid)->value('store_id'), 0])->where('paid', 1)->where('refund_status', 0)->find();
- if (!$orderInfo) return app('json')->fail('核销的订单不存在于该门店或未支付或已退款');
- if ($orderInfo->status > 0) return app('json')->fail('订单已经核销');
- if ($orderInfo->combination_id && $orderInfo->pink_id) {
- $res = StorePink::where('id', $orderInfo->pink_id)->where('status', '<>', 2)->count();
- if ($res) return app('json')->fail('拼团订单暂未成功无法核销!');
- }
- if (!$is_confirm) {
- $orderInfo['image'] = StoreCart::getProductImage($orderInfo->cart_id);
- return app('json')->success($orderInfo->toArray());
- }
- StoreOrder::beginTrans();
- try {
- if (SystemStoreStaff::verifyStatus($uid) && ($storeStaff = SystemStoreStaff::where('uid', $uid)->field(['store_id', 'id'])->find())) {
- // $orderInfo->store_id = $storeStaff['store_id'];
- $orderInfo->clerk_id = $storeStaff['id'];
- }
- $orderInfo->status = 2;
- $orderInfo->verify_time = time();
- if ($orderInfo->store_id == 0) {
- $orderInfo->store_id = SystemStoreStaff::where('uid', $uid)->value('store_id');
- $store = SystemStore::get($orderInfo->store_id);
- $orderInfo->province = explode(',', $store['address'])[0] ?? '';
- $orderInfo->city = explode(',', $store['address'])[1] ?? '';
- $orderInfo->district = explode(',', $store['address'])[2] ?? '';
- SystemStore::decStock($orderInfo);
- }
- if ($orderInfo->save()) {
- OrderRepository::storeProductOrderTakeDeliveryAdmin($orderInfo);
- StoreOrderStatus::status($orderInfo->id, 'take_delivery', '已核销');
- event('ShortMssageSend', [$orderInfo['order_id'], 'Receiving']);
- StoreOrder::commitTrans();
- return app('json')->success('核销成功');
- } else {
- StoreOrder::rollbackTrans();
- return app('json')->fail('核销失败');
- }
- } catch (\PDOException $e) {
- StoreOrder::rollbackTrans();
- return app('json')->fail($e->getMessage());
- } catch (\Exception $e) {
- StoreOrder::rollbackTrans();
- return app('json')->fail($e->getMessage());
- }
- }
- /**
- * 门店核销
- * @param Request $request
- * @return
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function exchange_order_verific(Request $request)
- {
- $uid = $request->uid();
- list($verify_code, $is_confirm) = UtilService::postMore([
- ['verify_code', ''],
- ['is_confirm', 0]
- ], $request, true);
- if (!$verify_code) return app('json')->fail('缺少核销码');
- $orderInfo = StoreExchangeOrder::where('verify_code', $verify_code)->where('store_id', 'in', [SystemStoreStaff::where('uid', $uid)->value('store_id'), 0])->where('refund_status', 0)->find();
- if (!$orderInfo) return app('json')->fail('核销的兑换券不存在于该门店或未支付或已退款');
- if ($orderInfo->status == 2) return app('json')->fail('兑换券已经失效');
- if ($orderInfo->status > 0) return app('json')->fail('兑换券已经核销');
- if (!$is_confirm) {
- $orderInfo['image'] = StoreCart::getProductImage($orderInfo->cart_id);
- return app('json')->success($orderInfo->toArray());
- }
- StoreExchangeOrder::beginTrans();
- try {
- if (SystemStoreStaff::verifyStatus($uid) && ($storeStaff = SystemStoreStaff::where('uid', $uid)->field(['store_id', 'id'])->find())) {
- $orderInfo->clerk_id = $storeStaff['id'];
- }
- $orderInfo->status = 1;
- $orderInfo->verify_time = time();
- if ($orderInfo->store_id == 0) {
- $orderInfo->store_id = SystemStoreStaff::where('uid', $uid)->value('store_id');
- SystemStore::decStock($orderInfo);
- }
- if ($orderInfo->save()) {
- StoreExchangeOrder::commitTrans();
- return app('json')->success('核销成功');
- } else {
- StoreExchangeOrder::rollbackTrans();
- return app('json')->fail('核销失败');
- }
- } catch (\PDOException $e) {
- StoreExchangeOrder::rollbackTrans();
- return app('json')->fail($e->getMessage());
- } catch (\Exception $e) {
- StoreExchangeOrder::rollbackTrans();
- return app('json')->fail($e->getMessage());
- }
- }
- public function rechargeList(Request $request)
- {
- $uid = $request->uid();
- $where = UtilService::getMore([
- ['page', 1],
- ['limit', 20],
- ]);
- $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
- if (!$info) {
- return app('json')->fail('权限不足');
- }
- $where['store_id'] = $info['store_id'];
- return app('json')->successful('ok', UserRecharge::getStoreRechargeList($where));
- }
- public function back_deposit(Request $request)
- {
- $uid = $request->uid();
- $data = UtilService::postMore([
- ['order_id', ''],
- ['refund_price', 0],
- ['type', 1],
- ]);
- if (!$data['order_id']) return app('json')->fail('数据不存在');
- $orderInfo = StoreOrder::where('order_id', $data['order_id'])->where('store_id', SystemStoreStaff::where('uid', $uid)->value('store_id'))->find();
- if (!$orderInfo) return app('json')->fail('订单不存在于该门店');
- if ($orderInfo['deposit'] == $orderInfo['deposit_back']) return app('json')->fail('已退完押金!不能再退款了');
- if (!$data['refund_price']) return app('json')->fail('请输入退款金额');
- $refund_price = $data['refund_price'];
- $data['deposit_back'] = bcadd($data['refund_price'], $orderInfo['deposit_back'], 2);
- $bj = bccomp((float)$orderInfo['deposit'], (float)$data['deposit_back'], 2);
- if ($bj < 0) return app('json')->fail('退押金额大于支付押金,请修改退押金额');
- if ($data['type'] == 1) {
- $data['deposit_status'] = 1;
- } else if ($data['type'] == 2) {
- $data['deposit_status'] = 0;
- }
- // $type = $data['type'];
- unset($data['type']);
- unset($data['refund_price']);
- $refund_data['pay_price'] = bcadd($orderInfo['pay_price'], $orderInfo['deposit'], 2);
- $refund_data['refund_price'] = $refund_price;
- $refund_data['refund_id'] = 'refund_deposit' . $orderInfo['order_id'] . time();
- if ($orderInfo['pay_type'] == 'weixin') {
- if ($orderInfo['is_channel'] == 1) {//小程序
- try {
- MiniProgramService::payOrderRefund($orderInfo['order_id'], $refund_data);//2.5.36
- } catch (\Exception $e) {
- return app('json')->fail($e->getMessage());
- }
- } else {//TODO 公众号
- try {
- WechatService::payOrderRefund($orderInfo['order_id'], $refund_data);
- } catch (\Exception $e) {
- return app('json')->fail($e->getMessage());
- }
- }
- } else if ($orderInfo['pay_type'] == 'yue') {
- BaseModel::beginTrans();
- $usermoney = User::where('uid', $orderInfo['uid'])->value('now_money');
- $res1 = User::bcInc($orderInfo['uid'], 'now_money', $refund_price, 'uid');
- $res2 = UserBill::income('商品退押金', $orderInfo['uid'], 'now_money', 'pay_deposit_refund', $refund_price, $orderInfo['id'], bcadd($usermoney, $refund_price, 2), '订单退押金到余额' . floatval($refund_price) . '元');
- $res = $res1 && $res2;
- BaseModel::checkTrans($res);
- if (!$res) return app('json')->fail('余额退押金失败!');
- }
- $resEdit = StoreOrder::edit($data, $orderInfo['id']);
- if ($resEdit) {
- \app\admin\model\order\StoreOrderStatus::setStatus($orderInfo['id'], 'refund_deposit', '退押金给用户' . $refund_price . '元');
- return app('json')->success('成功!');
- } else {
- \app\admin\model\order\StoreOrderStatus::setStatus($orderInfo['id'], 'refund_deposit', '退押金给用户' . $refund_price . '元失败');
- return app('json')->fail('失败!');
- }
- }
- public function exchange_order_back_deposit(Request $request)
- {
- $uid = $request->uid();
- $data = UtilService::postMore([
- ['id', ''],
- ['refund_price', 0],
- ['type', 1],
- ], $request);
- // var_dump($data);
- if (!$data['id']) return app('json')->fail('数据不存在');
- $orderInfo = StoreExchangeOrder::where('id', $data['id'])->where('store_id', SystemStoreStaff::where('uid', $uid)->value('store_id'))->where('refund_status', 0)->find();
- if (!$orderInfo) return app('json')->fail('订单不存在于该门店或未支付或已退款');
- if ($orderInfo['deposit'] == $orderInfo['deposit_back']) return app('json')->fail('已退完押金!不能再退款了');
- if (!$data['refund_price']) return app('json')->fail('请输入退款金额');
- $refund_price = $data['refund_price'];
- $data['deposit_back'] = bcadd($data['refund_price'], $orderInfo['deposit_back'], 2);
- $bj = bccomp((float)$orderInfo['deposit'], (float)$data['deposit_back'], 2);
- if ($bj < 0) return app('json')->fail('退押金额大于支付押金,请修改退押金额');
- if ($data['type'] == 1) {
- $data['deposit_status'] = 1;
- } else if ($data['type'] == 2) {
- $data['deposit_status'] = 0;
- }
- // $type = $data['type'];
- unset($data['type']);
- unset($data['refund_price']);
- $refund_data['pay_price'] = bcadd($orderInfo['pay_price'], $orderInfo['deposit'], 2);
- $refund_data['refund_price'] = $refund_price;
- $refund_data['refund_id'] = 'refund_deposit' . $orderInfo['order_id'] . time();
- $order = StoreOrderModel::where('order_id', $orderInfo['order_id'])->find();
- if ($order['pay_type'] == 'weixin') {
- if ($order['is_channel'] == 1) {//小程序
- try {
- MiniProgramService::payOrderRefund($orderInfo['order_id'], $refund_data);//2.5.36
- } catch (\Exception $e) {
- return app('json')->fail($e->getMessage());
- }
- } else {//TODO 公众号
- try {
- WechatService::payOrderRefund($orderInfo['order_id'], $refund_data);
- } catch (\Exception $e) {
- return app('json')->fail($e->getMessage());
- }
- }
- } else if ($orderInfo['pay_type'] == 'yue') {
- BaseModel::beginTrans();
- $usermoney = User::where('uid', $orderInfo['uid'])->value('now_money');
- $res1 = User::bcInc($orderInfo['uid'], 'now_money', $refund_price, 'uid');
- $res2 = UserBill::income('商品退押金', $orderInfo['uid'], 'now_money', 'pay_deposit_refund', $refund_price, $orderInfo['id'], bcadd($usermoney, $refund_price, 2), '订单退押金到余额' . floatval($refund_price) . '元');
- $res = $res1 && $res2;
- BaseModel::checkTrans($res);
- if (!$res) return app('json')->fail('余额退押金失败!');
- }
- $resEdit = StoreExchangeOrder::edit($data, $orderInfo['id']);
- if ($resEdit) {
- \app\admin\model\order\StoreOrderStatus::setStatus($orderInfo['oid'], 'refund_deposit', '退押金给用户' . $refund_price . '元');
- return app('json')->success('成功!');
- } else {
- \app\admin\model\order\StoreOrderStatus::setStatus($orderInfo['oid'], 'refund_deposit', '退押金给用户' . $refund_price . '元失败');
- return app('json')->fail('失败!');
- }
- }
- }
|