StoreOrderController.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. <?php
  2. namespace app\api\controller\admin;
  3. use app\admin\model\system\SystemStoreBill;
  4. use app\admin\model\system\SystemStoreExtract;
  5. use app\admin\model\system\SystemStorePoint;
  6. use app\admin\model\system\SystemStoreProductStock;
  7. use app\admin\model\user\UserRecharge;
  8. use app\models\system\SystemStore;
  9. use app\Request;
  10. use think\db\exception\DataNotFoundException;
  11. use think\db\exception\DbException;
  12. use think\db\exception\ModelNotFoundException;
  13. use app\models\user\{
  14. User, UserBill
  15. };
  16. use crmeb\repositories\OrderRepository;
  17. use crmeb\repositories\ShortLetterRepositories;
  18. use crmeb\services\{
  19. MiniProgramService, UtilService, WechatService
  20. };
  21. use app\models\store\{
  22. StoreCart, StoreOrder, StoreOrderStatus, StorePink, StoreService
  23. };
  24. use app\models\system\SystemStoreStaff;
  25. /**
  26. * 订单类
  27. * Class StoreOrderController
  28. * @package app\api\controller\admin\order
  29. */
  30. class StoreOrderController
  31. {
  32. public function pointList(Request $request)
  33. {
  34. $uid = $request->uid();
  35. $where = UtilService::getMore([
  36. ['page', 1],
  37. ['limit', 20],
  38. ['type', 1]
  39. ]);
  40. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  41. if (!$info) {
  42. return app('json')->fail('权限不足');
  43. }
  44. // var_dump($info->toArray());
  45. $where['store_id'] = $info['store_id'];
  46. $where['excel'] = 0;
  47. // $where['type'] = $info['store_id'];
  48. return app('json')->successful('ok', SystemStorePoint::getStoreList($where));
  49. }
  50. public function pointDetail($id, Request $request)
  51. {
  52. $uid = $request->uid();
  53. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  54. if (!$info) {
  55. return app('json')->fail('权限不足');
  56. }
  57. return app('json')->successful('ok', ['data' => SystemStorePoint::getStoreDispose($id)]);
  58. }
  59. public function addDetail(Request $request, $id = 0)
  60. {
  61. $uid = $request->uid();
  62. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  63. if (!$info) {
  64. return app('json')->fail('权限不足');
  65. }
  66. $data = UtilService::postMore([
  67. ['name', ''],
  68. ['phone', ''],
  69. ['address', ''],
  70. ['detailed_address', ''],
  71. ['latlng', ''],
  72. ['valid_time', []],
  73. ['day_time', []],
  74. ['is_show', 0],
  75. ]);
  76. $data['store_id'] = $info['store_id'];
  77. $data['address'] = implode(',', $data['address']);
  78. $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng'];
  79. if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) return app('json')->fail('请选择门店位置');
  80. $data['latitude'] = $data['latlng'][0];
  81. $data['longitude'] = $data['latlng'][1];
  82. $data['valid_time'] = implode(' - ', $data['valid_time']);
  83. $data['day_time'] = implode(' - ', $data['day_time']);
  84. unset($data['latlng']);
  85. if ($id) {
  86. $store_info = SystemStorePoint::get($id);
  87. if ($store_info['store_id'] != $info['store_id']) {
  88. return app('json')->fail('权限不足');
  89. }
  90. if (SystemStorePoint::where('id', $id)->update($data)) {
  91. SystemStorePoint::commitTrans();
  92. return app('json')->successful('修改成功');
  93. } else {
  94. SystemStorePoint::rollbackTrans();
  95. return app('json')->fail('修改失败或者您没有修改什么!');
  96. }
  97. } else {
  98. $data['add_time'] = time();
  99. $data['is_show'] = 1;
  100. if ($res = SystemStorePoint::create($data)) {
  101. SystemStorePoint::commitTrans();
  102. return app('json')->successful('保存成功', ['id' => $res->id]);
  103. } else {
  104. SystemStorePoint::rollbackTrans();
  105. return app('json')->fail('保存失败!');
  106. }
  107. }
  108. }
  109. /**
  110. * 删除恢复门店
  111. * @param $id
  112. */
  113. public function point_delete($id, Request $request)
  114. {
  115. if (!$id) return app('json')->fail('数据不存在');
  116. if (!SystemStorePoint::be(['id' => $id])) return app('json')->fail('数据不存在');
  117. $uid = $request->uid();
  118. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  119. if (!$info) {
  120. return app('json')->fail('权限不足');
  121. }
  122. $store_info = SystemStorePoint::get($id);
  123. if ($store_info['store_id'] != $info['store_id']) {
  124. return app('json')->fail('权限不足');
  125. }
  126. if (SystemStorePoint::be(['id' => $id, 'is_del' => 1])) {
  127. $data['is_del'] = 0;
  128. if (!SystemStorePoint::edit($data, $id))
  129. return app('json')->fail(SystemStorePoint::getErrorInfo('恢复失败,请稍候再试!'));
  130. else
  131. return app('json')->success('恢复门店成功!');
  132. } else {
  133. $data['is_del'] = 1;
  134. if (!SystemStorePoint::edit($data, $id))
  135. return app('json')->fail(SystemStorePoint::getErrorInfo('删除失败,请稍候再试!'));
  136. else
  137. return app('json')->success('删除门店成功!');
  138. }
  139. }
  140. public function stockList(Request $request)
  141. {
  142. $uid = $request->uid();
  143. $where = UtilService::getMore([
  144. ['type', ''],
  145. ['page', 1],
  146. ['limit', 20],
  147. ['start_time', ''],
  148. ['end_time', ''],
  149. ]);
  150. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  151. if (!$info) {
  152. return app('json')->fail('权限不足');
  153. }
  154. return app('json')->successful('ok', ['warning' => SystemStoreProductStock::getWarning($info['store_id']), 'data' => SystemStoreProductStock::getStockList($where, $info['store_id'])]);
  155. }
  156. public function billList(Request $request)
  157. {
  158. $uid = $request->uid();
  159. $where = UtilService::getMore([
  160. ['page', 1],
  161. ['limit', 20],
  162. ['type', ''],
  163. ['time', ''],
  164. ]);
  165. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  166. if (!$info) {
  167. return app('json')->fail('权限不足');
  168. }
  169. 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'])]);
  170. }
  171. public function cash(Request $request)
  172. {
  173. $uid = $request->uid();
  174. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  175. if (!$info) {
  176. return app('json')->fail('权限不足');
  177. }
  178. if (!in_array(sys_config('default_store_admin', 7), explode(',', $request->admin_info()['roles']))) {
  179. return app('json')->fail('权限不足');
  180. }
  181. $store = SystemStore::get($info['store_id']);
  182. $extractInfo = UtilService::postMore([
  183. ['money', 0],
  184. ], $request);
  185. $extractInfo['extract_type'] = 'bank';
  186. $extractInfo['name'] = $store['leader'];
  187. $extractInfo['bankname'] = $store['bank_name'];
  188. $extractInfo['cardnum'] = $store['bank_card'];
  189. if (!preg_match('/^(([1-9]\d*)|0)(\.\d{1-2})?$/', $extractInfo['money'])) app('json')->fail('提现金额输入有误');
  190. $store = SystemStore::get($info['store_id']);
  191. if ($extractInfo['money'] > $store['brokerage_price']) return app('json')->fail('可提现佣金不足');
  192. if (!$extractInfo['cardnum'] == '')
  193. if (!preg_match('/^([1-9]{1})(\d{14}|\d{18})$/', $extractInfo['cardnum']))
  194. return app('json')->fail('银行卡号有误');
  195. if (SystemStoreExtract::userExtract($store, $extractInfo))
  196. return app('json')->successful('申请提现成功!');
  197. else
  198. return app('json')->fail(SystemStoreExtract::getErrorInfo('提现失败'));
  199. }
  200. public function childrenList(Request $request)
  201. {
  202. $uid = $request->uid();
  203. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  204. if (!$info) {
  205. return app('json')->fail('权限不足');
  206. }
  207. $where = UtilService::getMore([
  208. ['page', 1],
  209. ['limit', 20],
  210. ['name', ''],
  211. ['level', ''],
  212. ['excel', 0],
  213. ['type', ''],
  214. ['parent_id', 0],
  215. ]);
  216. $where['self_store'] = $info['store_id'];
  217. return app('json')->successful('ok', \app\admin\model\system\SystemStore::getStoreList($where));
  218. }
  219. /**
  220. * 订单数据统计
  221. * @param Request $request
  222. * @return mixed
  223. * @throws DataNotFoundException
  224. * @throws DbException
  225. * @throws ModelNotFoundException
  226. */
  227. public function statistics(Request $request)
  228. {
  229. $uid = $request->uid();
  230. // if (!StoreService::orderServiceStatus($uid))
  231. // return app('json')->fail('权限不足');
  232. $where = [];
  233. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  234. if ($info) {
  235. $where['store_id'] = $info['store_id'];
  236. }
  237. $dataCount = StoreOrder::getOrderDataAdmin($where);
  238. $dataPrice = StoreOrder::getOrderTimeData($where);
  239. $data = array_merge($dataCount, $dataPrice);
  240. return app('json')->successful($data);
  241. }
  242. /**
  243. * 订单每月统计数据
  244. * @param Request $request
  245. * @return mixed
  246. * @throws DataNotFoundException
  247. * @throws DbException
  248. * @throws ModelNotFoundException
  249. */
  250. public function data(Request $request)
  251. {
  252. $uid = $request->uid();
  253. // if (!StoreService::orderServiceStatus($uid))
  254. // return app('json')->fail('权限不足');
  255. list($page, $limit, $start, $stop) = UtilService::getMore([
  256. ['page', 1],
  257. ['limit', 7],
  258. ['start', ''],
  259. ['stop', '']
  260. ], $request, true);
  261. $where = [];
  262. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  263. if ($info) {
  264. $where['store_id'] = $info['store_id'];
  265. }
  266. if (!$limit) return app('json')->successful([]);
  267. $data = StoreOrder::getOrderDataPriceCount($page, $limit, $start, $stop, $where);
  268. if ($data) return app('json')->successful($data->toArray());
  269. return app('json')->successful([]);
  270. }
  271. /**
  272. * 订单列表
  273. * @param Request $request
  274. * @return mixed
  275. * @throws DataNotFoundException
  276. * @throws DbException
  277. * @throws ModelNotFoundException
  278. */
  279. public function lst(Request $request)
  280. {
  281. $uid = $request->uid();
  282. // if (!StoreService::orderServiceStatus($uid))
  283. // return app('json')->fail('权限不足');
  284. $where = UtilService::getMore([
  285. ['status', ''],
  286. ['is_del', 0],
  287. ['data', ''],
  288. ['type', ''],
  289. ['shipping_type', ''],
  290. ['order', ''],
  291. ['page', 0],
  292. ['limit', 0]
  293. ], $request);
  294. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  295. if ($info) {
  296. $where['store_id'] = $info['store_id'];
  297. }
  298. if (!$where['limit']) return app('json')->successful([]);
  299. return app('json')->successful(StoreOrder::orderList($where));
  300. }
  301. /**
  302. * 订单详情
  303. * @param Request $request
  304. * @param $orderId
  305. * @return mixed
  306. * @throws DataNotFoundException
  307. * @throws ModelNotFoundException
  308. * @throws DbException
  309. */
  310. public function detail(Request $request, $orderId)
  311. {
  312. $uid = $request->uid();
  313. // if (!StoreService::orderServiceStatus($uid))
  314. // return app('json')->fail('权限不足');
  315. $where = [];
  316. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  317. if ($info) {
  318. $where['store_id'] = $info['store_id'];
  319. }
  320. $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', $where);
  321. if (!$order) return app('json')->fail('订单不存在');
  322. $order = $order->toArray();
  323. $nickname = User::getUserInfo($order['uid'], 'nickname')['nickname'];
  324. $orderInfo = StoreOrder::tidyAdminOrder([$order], true)[0];
  325. unset($orderInfo['uid'], $orderInfo['seckill_id'], $orderInfo['pink_id'], $orderInfo['combination_id'], $orderInfo['bargain_id'], $orderInfo['status'], $orderInfo['total_postage']);
  326. $orderInfo['nickname'] = $nickname;
  327. return app('json')->successful('ok', $orderInfo);
  328. }
  329. /**
  330. * 订单发货获取订单信息
  331. * @param Request $request
  332. * @param $orderId
  333. * @return mixed
  334. * @throws DataNotFoundException
  335. * @throws ModelNotFoundException
  336. * @throws DbException
  337. */
  338. public function delivery_gain(Request $request, $orderId)
  339. {
  340. $uid = $request->uid();
  341. // if (!StoreService::orderServiceStatus($uid))
  342. // return app('json')->fail('权限不足');
  343. $where = [];
  344. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  345. if ($info) {
  346. $where['store_id'] = $info['store_id'];
  347. }
  348. $order = StoreOrder::getAdminOrderDetail($orderId, 'real_name,user_phone,user_address,order_id,uid,status,paid', $where);
  349. if (!$order) return app('json')->fail('订单不存在');
  350. if ($order['paid']) {
  351. $order['nickname'] = User::getUserInfo($order['uid'], 'nickname')['nickname'];
  352. $order = $order->hidden(['uid', 'status', 'paid'])->toArray();
  353. return app('json')->successful('ok', $order);
  354. }
  355. return app('json')->fail('状态错误');
  356. }
  357. /**
  358. * 订单发货
  359. * @param Request $request
  360. * @return mixed
  361. * @throws DataNotFoundException
  362. * @throws ModelNotFoundException
  363. * @throws \think\exception\DbException
  364. * @throws DbException
  365. */
  366. public function delivery_keep(Request $request)
  367. {
  368. $uid = $request->uid();
  369. // if (!StoreService::orderServiceStatus($uid))
  370. // return app('json')->fail('权限不足');
  371. list($order_id, $delivery_type, $delivery_name, $delivery_id) = UtilService::postMore([
  372. ['order_id', ''],
  373. ['delivery_type', 0],
  374. ['delivery_name', ''],
  375. ['delivery_id', ''],
  376. ], $request, true);
  377. $where = [];
  378. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  379. if ($info) {
  380. $where['store_id'] = $info['store_id'];
  381. }
  382. $order = StoreOrder::getAdminOrderDetail($order_id, 'id,status,paid', $where);
  383. if (!$order) return app('json')->fail('订单不存在');
  384. if (!$order['status'] && $order['paid']) {
  385. $deliveryTypeArr = isset($where['store_id']) ? ['send'] : ['send', 'express', 'fictitious'];
  386. if (!strlen(trim($delivery_type))) return app('json')->fail('请填写发货方式');
  387. if (!in_array($delivery_type, $deliveryTypeArr)) return app('json')->fail('发货方式错误');
  388. if ($delivery_type == 'express') {
  389. if (!strlen(trim($delivery_name))) return app('json')->fail('请选择快递公司');
  390. if (!strlen(trim($delivery_id))) return app('json')->fail('请填写快递单号');
  391. }
  392. if ($delivery_type == 'send') {
  393. if (!strlen(trim($delivery_name))) return app('json')->fail('请填写发货人');
  394. if (!strlen(trim($delivery_id))) return app('json')->fail('请填写发货手机号');
  395. }
  396. $data['status'] = 1;
  397. $data['delivery_type'] = $delivery_type;
  398. $data['delivery_name'] = $delivery_name;
  399. $data['delivery_id'] = $delivery_id;
  400. $res = StoreOrder::edit($data, $order['id'], 'id');
  401. if ($res) {
  402. if ($delivery_type == 'express') {
  403. StoreOrderStatus::status($order['id'], 'delivery_goods', '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']);
  404. } else if ($delivery_type == 'send') {
  405. StoreOrderStatus::status($order['id'], 'delivery', '已配送 发货人:' . $delivery_name . ' 发货人电话:' . $delivery_id);
  406. } else if ($delivery_type == 'fictitious') {
  407. StoreOrderStatus::status($order['id'], 'delivery_fictitious', '虚拟产品已发货');
  408. }
  409. }
  410. event('StoreProductOrderDeliveryGoodsAfter', [$data, $order['id']]);
  411. return app('json')->successful('发货成功!');
  412. }
  413. return app('json')->fail('状态错误');
  414. }
  415. /**
  416. * 订单改价
  417. * @param Request $request
  418. * @return mixed
  419. * @throws DataNotFoundException
  420. * @throws ModelNotFoundException
  421. * @throws DbException
  422. */
  423. public function price(Request $request)
  424. {
  425. $uid = $request->uid();
  426. // if (!StoreService::orderServiceStatus($uid))
  427. // return app('json')->fail('权限不足');
  428. list($order_id, $price) = UtilService::postMore([
  429. ['order_id', ''],
  430. ['price', '']
  431. ], $request, true);
  432. $where = [];
  433. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  434. if ($info) {
  435. $where['store_id'] = $info['store_id'];
  436. }
  437. $order = StoreOrder::getAdminOrderDetail($order_id, 'id,paid,user_phone,pay_price,order_id,total_price,total_postage,pay_postage,gain_integral', $where);
  438. if (!$order) return app('json')->fail('订单不存在');
  439. $order = $order->toArray();
  440. if (!$order['paid']) {
  441. if ($price === '') return app('json')->fail('请填写实际支付金额');
  442. if ($price < 0) return app('json')->fail('实际支付金额不能小于0元');
  443. if ($order['pay_price'] == $price) return app('json')->successful('修改成功');
  444. $order['order_id'] = StoreOrder::changeOrderId($order['order_id']);
  445. if (!StoreOrder::edit(['pay_price' => $price, 'order_id' => $order['order_id']], $order['id'], 'id'))
  446. return app('json')->fail('状态错误');
  447. $order['pay_price'] = $price;
  448. event('StoreProductOrderEditAfter', [$order, $order['id']]);
  449. StoreOrderStatus::status($order['id'], 'order_edit', '修改实际支付金额' . $price);
  450. //改价短信提醒
  451. if ($price != $order['pay_price']) {
  452. $switch = sys_config('price_revision_switch') ? true : false;
  453. ShortLetterRepositories::send($switch, $order['user_phone'], ['order_id' => $order_id, 'pay_price' => $order['pay_price']], 'PRICE_REVISION_CODE');
  454. }
  455. return app('json')->successful('修改成功');
  456. }
  457. return app('json')->fail('状态错误');
  458. }
  459. /**
  460. * 订单备注
  461. * @param Request $request
  462. * @return mixed
  463. * @throws DataNotFoundException
  464. * @throws ModelNotFoundException
  465. * @throws DbException
  466. */
  467. public function remark(Request $request)
  468. {
  469. $uid = $request->uid();
  470. // if (!StoreService::orderServiceStatus($uid))
  471. // return app('json')->fail('权限不足');
  472. list($order_id, $remark) = UtilService::postMore([
  473. ['order_id', ''],
  474. ['remark', '']
  475. ], $request, true);
  476. $where = [];
  477. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  478. if ($info) {
  479. $where['store_id'] = $info['store_id'];
  480. }
  481. $order = StoreOrder::getAdminOrderDetail($order_id, 'id', $where);
  482. if (!$order) return app('json')->fail('订单不存在');
  483. $order = $order->toArray();
  484. if (!strlen(trim($remark))) return app('json')->fail('请填写备注内容');
  485. if (!StoreOrder::edit(['remark' => $remark], $order['id']))
  486. return app('json')->fail('备注失败');
  487. return app('json')->successful('备注成功');
  488. }
  489. /**
  490. * 订单交易额/订单数量时间统计
  491. * @param Request $request
  492. * @return bool
  493. */
  494. public function time(Request $request)
  495. {
  496. $uid = $request->uid();
  497. // if (!StoreService::orderServiceStatus($uid))
  498. // return app('json')->fail('权限不足');
  499. list($start, $stop, $type) = UtilService::getMore([
  500. ['start', strtotime(date('Y-m'))],
  501. ['stop', time()],
  502. ['type', 1]
  503. ], $request, true);
  504. $where = [];
  505. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  506. if ($info) {
  507. $where['store_id'] = $info['store_id'];
  508. }
  509. if ($start == $stop) {
  510. return app('json')->fail('开始时间不能等于结束时间');
  511. }
  512. if ($start > $stop) {
  513. $middle = $stop;
  514. $stop = $start;
  515. $start = $middle;
  516. }
  517. $space = bcsub($stop, $start, 0);//间隔时间段
  518. $front = bcsub($start, $space, 0);//第一个时间段
  519. if ($type == 1) {//销售额
  520. $frontPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($front, $start, $where);
  521. $afterPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($start, $stop, $where);
  522. $chartInfo = StoreOrder::chartTimePrice($start, $stop, $where);
  523. $data['chart'] = $chartInfo;//营业额图表数据
  524. $data['time'] = $afterPrice;//时间区间营业额
  525. $increase = (float)bcsub($afterPrice, $frontPrice, 2); //同比上个时间区间增长营业额
  526. $growthRate = abs($increase);
  527. if ($growthRate == 0) $data['growth_rate'] = 0;
  528. else if ($frontPrice == 0) $data['growth_rate'] = $growthRate;
  529. else $data['growth_rate'] = (int)bcmul(bcdiv($growthRate, $frontPrice, 2), 100, 0);//时间区间增长率
  530. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  531. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  532. } else {//订单数
  533. $frontNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($front, $start, $where);
  534. $afterNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($start, $stop, $where);
  535. $chartInfo = StoreOrder::chartTimeNumber($start, $stop, $where);
  536. $data['chart'] = $chartInfo;//订单数图表数据
  537. $data['time'] = $afterNumber;//时间区间订单数
  538. $increase = (int)bcsub($afterNumber, $frontNumber, 0); //同比上个时间区间增长订单数
  539. $growthRate = abs($increase);
  540. if ($growthRate == 0) $data['growth_rate'] = 0;
  541. else if ($frontNumber == 0) $data['growth_rate'] = $growthRate;
  542. else $data['growth_rate'] = (int)bcmul(bcdiv($growthRate, $frontNumber, 2), 100, 0);//时间区间增长率
  543. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  544. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  545. }
  546. return app('json')->successful($data);
  547. }
  548. /**
  549. * 订单支付
  550. * @param Request $request
  551. * @return mixed
  552. * @throws DataNotFoundException
  553. * @throws ModelNotFoundException
  554. * @throws DbException
  555. */
  556. public function offline(Request $request)
  557. {
  558. $uid = $request->uid();
  559. list($orderId) = UtilService::postMore([['order_id', '']], $request, true);
  560. $where = [];
  561. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  562. if ($info) {
  563. $where['store_id'] = $info['store_id'];
  564. }
  565. $orderInfo = StoreOrder::getAdminOrderDetail($orderId, 'id', $where);
  566. if (!$orderInfo) return app('json')->fail('参数错误');
  567. $id = $orderInfo->id;
  568. $res = StoreOrder::updateOffline($id);
  569. if ($res) {
  570. event('StoreProductOrderOffline', [$id]);
  571. StoreOrderStatus::status($id, 'offline', '线下付款');
  572. return app('json')->successful('修改成功!');
  573. }
  574. return app('json')->fail(StoreOrder::getErrorInfo('修改失败!'));
  575. }
  576. /**
  577. * 订单退款
  578. * @param Request $request
  579. * @return mixed
  580. * @throws \think\Exception
  581. * @throws DataNotFoundException
  582. * @throws ModelNotFoundException
  583. * @throws DbException
  584. */
  585. public function refund(Request $request)
  586. {
  587. $uid = $request->uid();
  588. list($orderId, $price, $type) = UtilService::postMore([
  589. ['order_id', ''],
  590. ['price', 0],
  591. ['type', 1],
  592. ], $request, true);
  593. $where = [];
  594. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  595. if ($info) {
  596. $where['store_id'] = $info['store_id'];
  597. }
  598. if (!strlen(trim($orderId))) return app('json')->fail('参数错误');
  599. $orderInfo = StoreOrder::getAdminOrderDetail($orderId, '*', $where);
  600. if (!$orderInfo) return app('json')->fail('数据不存在!');
  601. $orderInfo = $orderInfo->toArray();
  602. if ($type == 1)
  603. $data['refund_status'] = 2;
  604. else if ($type == 2)
  605. $data['refund_status'] = 0;
  606. else
  607. return app('json')->fail('退款修改状态错误');
  608. if ($orderInfo['pay_price'] == 0 || $type == 2) {
  609. StoreOrder::update($data, ['order_id' => $orderId]);
  610. return app('json')->successful('修改退款状态成功!');
  611. }
  612. if ($orderInfo['pay_price'] == $orderInfo['refund_price']) return app('json')->fail('已退完支付金额!不能再退款了');
  613. if (!$price) return app('json')->fail('请输入退款金额');
  614. $data['refund_price'] = bcadd($price, $orderInfo['refund_price'], 2);
  615. $bj = bccomp((float)$orderInfo['pay_price'], (float)$data['refund_price'], 2);
  616. if ($bj < 0) return app('json')->fail('退款金额大于支付金额,请修改退款金额');
  617. $refundData['pay_price'] = $orderInfo['pay_price'];
  618. $refundData['refund_price'] = $price;
  619. if ($orderInfo['pay_type'] == 'weixin') {
  620. if ($orderInfo['is_channel'] == 1) {// 小程序
  621. try {
  622. MiniProgramService::payOrderRefund($orderInfo['order_id'], $refundData);
  623. } catch (\Exception $e) {
  624. return app('json')->fail($e->getMessage());
  625. }
  626. } else {// 公众号
  627. try {
  628. WechatService::payOrderRefund($orderInfo['order_id'], $refundData);
  629. } catch (\Exception $e) {
  630. return app('json')->fail($e->getMessage());
  631. }
  632. }
  633. } else if ($orderInfo['pay_type'] == 'yue') {//余额
  634. StoreOrder::beginTrans();
  635. $userInfo = User::getUserInfo($orderInfo['uid'], 'now_money');
  636. if (!$userInfo) {
  637. StoreOrder::rollbackTrans();
  638. return app('json')->fail('订单用户不存在');
  639. }
  640. $res1 = User::bcInc($orderInfo['uid'], 'now_money', $price, 'uid');
  641. $res2 = $res2 = UserBill::income('商品退款', $orderInfo['uid'], 'now_money', 'pay_product_refund', $price, $orderInfo['id'], bcadd($userInfo['now_money'], $price, 2), '订单退款到余额' . floatval($price) . '元');
  642. try {
  643. OrderRepository::storeOrderYueRefund($orderInfo, $refundData);
  644. } catch (\Exception $e) {
  645. StoreOrder::rollbackTrans();
  646. return app('json')->fail($e->getMessage());
  647. }
  648. $res = $res1 && $res2;
  649. StoreOrder::checkTrans($res);
  650. if (!$res) return app('json')->fail('余额退款失败!');
  651. }
  652. $resEdit = StoreOrder::edit($data, $orderInfo['id'], 'id');
  653. if ($resEdit) {
  654. $data['type'] = $type;
  655. if ($data['type'] == 1) StorePink::setRefundPink($orderInfo['id']);
  656. try {
  657. OrderRepository::storeProductOrderRefundY($data, $orderInfo['id']);
  658. } catch (\Exception $e) {
  659. return app('json')->fail($e->getMessage());
  660. }
  661. StoreOrderStatus::status($orderInfo['id'], 'refund_price', '退款给用户' . $price . '元');
  662. SystemStoreBill::where('category', 'brokerage_price')
  663. //->where('type', 'brokerage')
  664. ->where('link_id', $orderId)
  665. ->where('pm', 1)
  666. ->where('status', 0)
  667. ->update(['status' => -1]);
  668. \app\admin\model\user\UserBill::where('category', 'now_money')
  669. ->where('type', 'brokerage')
  670. ->where('link_id', $orderId)
  671. ->where('pm', 1)
  672. ->where('status', 0)
  673. ->update(['status' => -1]);
  674. //退佣金
  675. $brokerage_list = UserBill::where('category', 'now_money')
  676. ->where('type', 'brokerage')
  677. ->where('link_id', $orderId)
  678. ->where('pm', 1)
  679. ->where('status', 1)
  680. ->select()->toArray();
  681. if ($brokerage_list)
  682. foreach ($brokerage_list as $item) {
  683. $usermoney = User::where('uid', $item['uid'])->value('brokerage_price');
  684. if ($item['number'] > $usermoney)
  685. $item['number'] = $usermoney;
  686. User::bcDec($item['uid'], 'brokerage_price', $item['number'], 'uid');
  687. UserBill::expend('退款退佣金', $item['uid'], 'now_money', 'brokerage', $item['number'], $orderId, bcsub($usermoney, $item['number'], 2), '订单退款扣除佣金' . floatval($item['number']) . '元');
  688. }
  689. return app('json')->successful('修改成功!');
  690. } else {
  691. StoreOrderStatus::status($orderInfo['id'], 'refund_price', '退款给用户' . $price . '元失败');
  692. return app('json')->successful('修改失败!');
  693. }
  694. }
  695. /**
  696. * 门店核销
  697. * @param Request $request
  698. * @return
  699. * @throws DataNotFoundException
  700. * @throws DbException
  701. * @throws ModelNotFoundException
  702. */
  703. public function order_verific(Request $request)
  704. {
  705. $uid = $request->uid();
  706. list($verify_code, $is_confirm) = UtilService::postMore([
  707. ['verify_code', ''],
  708. ['is_confirm', 0]
  709. ], $request, true);
  710. if (!$verify_code) return app('json')->fail('缺少核销码');
  711. $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();
  712. if (!$orderInfo) return app('json')->fail('核销的订单不存在于该门店或未支付或已退款');
  713. if ($orderInfo->status > 0) return app('json')->fail('订单已经核销');
  714. if ($orderInfo->combination_id && $orderInfo->pink_id) {
  715. $res = StorePink::where('id', $orderInfo->pink_id)->where('status', '<>', 2)->count();
  716. if ($res) return app('json')->fail('拼团订单暂未成功无法核销!');
  717. }
  718. if (!$is_confirm) {
  719. $orderInfo['image'] = StoreCart::getProductImage($orderInfo->cart_id);
  720. return app('json')->success($orderInfo->toArray());
  721. }
  722. StoreOrder::beginTrans();
  723. try {
  724. if (SystemStoreStaff::verifyStatus($uid) && ($storeStaff = SystemStoreStaff::where('uid', $uid)->field(['store_id', 'id'])->find())) {
  725. // $orderInfo->store_id = $storeStaff['store_id'];
  726. $orderInfo->clerk_id = $storeStaff['id'];
  727. }
  728. $orderInfo->status = 2;
  729. if ($orderInfo->store_id == 0) {
  730. $orderInfo->store_id = SystemStoreStaff::where('uid', $uid)->value('store_id');
  731. $store = SystemStore::get($orderInfo->store_id);
  732. $orderInfo->province = explode(',', $store['address'])[0] ?? '';
  733. $orderInfo->city = explode(',', $store['address'])[1] ?? '';
  734. $orderInfo->district = explode(',', $store['address'])[2] ?? '';
  735. SystemStore::decStock($orderInfo);
  736. }
  737. if ($orderInfo->save()) {
  738. OrderRepository::storeProductOrderTakeDeliveryAdmin($orderInfo);
  739. StoreOrderStatus::status($orderInfo->id, 'take_delivery', '已核销');
  740. event('ShortMssageSend', [$orderInfo['order_id'], 'Receiving']);
  741. StoreOrder::commitTrans();
  742. return app('json')->success('核销成功');
  743. } else {
  744. StoreOrder::rollbackTrans();
  745. return app('json')->fail('核销失败');
  746. }
  747. } catch (\PDOException $e) {
  748. StoreOrder::rollbackTrans();
  749. return app('json')->fail($e->getMessage());
  750. } catch (\Exception $e) {
  751. StoreOrder::rollbackTrans();
  752. return app('json')->fail($e->getMessage());
  753. }
  754. }
  755. public function rechargeList(Request $request)
  756. {
  757. $uid = $request->uid();
  758. $where = UtilService::getMore([
  759. ['page', 1],
  760. ['limit', 20],
  761. ]);
  762. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  763. if (!$info) {
  764. return app('json')->fail('权限不足');
  765. }
  766. $where['store_id'] = $info['store_id'];
  767. return app('json')->successful('ok', UserRecharge::getStoreRechargeList($where));
  768. }
  769. }