StoreOrderController.php 38 KB

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