StoreOrderController.php 33 KB

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